FileBasedCircuit.py#

ICE40 ASC-file circuit implementation.

Provides FileBasedCircuit, which reads and writes bitstreams in the IceStorm ASCII (.asc) format, compiles them via icepack, and uploads to hardware via iceprog. Uses memory-mapped file I/O for performance.

Note

Magic tile coordinates and routing types are specific to the ICE40 HX1K. Different FPGA models will require different constants.

class Circuit.FileBasedCircuit.FileBasedCircuit(*, index: int, filename: str, template: Path, logger: Logger, directories: Directories, routing_type: str, accessed_columns: list[int])[source]#

Represents a Circuit that is based on an ASC file, in a format ready to be compiled by IceStorm tools. Provides useful methods for working with hardware files

__compile()#

Compile circuit ASC file to a BIN file for hardware upload.

__log_event(level, *event)#

Emit an event-level log. This function is fulfilled through the logger.

__run_at_each_modifiable(lambda_func, hardware_file=None, accessible_columns=None, routing_type=None)#

Runs the lambda_func at every modifiable position Args passed to lambda_func: value of bit (as a byte), row, col If lambda_func returns a value (byte), then the bit is set to that number If lambda_func returns None, then the bit is left unmodified Keep in mind the bytes are the ASCII codes, so for example 49 = 1

Warning

Go over this with someone who can clarify what all of the data types are.

Parameters:
  • lambda_func (Callable) – This function is called for each modifiable bit. The bit value is passed into lambda_func. If lambda_func returns None, the bit is left unmodified. If lambda_func returns another value, then the bit at that position is replaced with the return value of lambda_func.

  • hardware_file (str | None) – The path to the hardware file to read from/write to. If no value provided, uses this Circuit’s hardware file.

  • accessible_columns (list[str] | None) – The accessible columns. If no value provided, uses the current configuration value.

  • routing_type (str | None) – The routing type (MOORE or NEWSE). If no value provided, uses the current configuration value.

__tile_is_included(hardware_file, pos)#

Determines whether a given tile is available for modificiation. NOTE: Tile = the .logic_tile in the asc file.

Warning

Preexisting todo: Replace magic values with a more generalized solution. These magic values are indicative of the underlying hardware (ice40kh1k)

Parameters:
  • hardware_file (mmap) – Memory Mapped hardware file

  • pos (int) – Index of the first byte in the .asc file for the hardware

Returns:

True if the tile at that position is valid (The Tiles we can modigy)

Return type:

bool

compile(fpga: FPGA_Compilation_Data) Result[None, Exception][source]#

Compiles and uploads the compiled circuit and runs it on the FPGA

copy_from(other: FileBasedCircuit)[source]#

Fully copy the bitstream from the other circuit

Parameters:

other (Circuit) – The other circuit to copy the bitstream from

get_bitstream() list[bool][source]#

Returns the full bitstream of the circuit

get_file_attribute(attribute) str | None[source]#

Returns the value of the stored attribute for this Circuit Circuits are capable of storing string name-value pairs in their hardware file, for purposes such as tracking most recently-evaluated fitness of a Circuit

Parameters:

attrbute (str) – The name of the attribute of this circuit you want

Returns:

The value of the attribute

Return type:

str

static get_file_attribute_st(mmapped_file, attribute)[source]#

Returns the value of the stored attribute from the hardware file. Circuits are capable of storing string name-value pairs in their hardware file, for purposes such as tracking most recently-evaluated fitness of a Circuit Static version of get_file_attribute that requires the memory-mapped file to be provided

Parameters:
  • mmapped_file (mmap) – The memory-mapped hardware file of the circuit

  • attribute (str) – Attribute name to lookup

Returns:

File attribute value

Return type:

str

set_bitstream(bitstream: list[bool])[source]#

Sets the bitstream of the circuit

set_file_attribute(attribute, value)[source]#

Sets this Circuit’s file attribute to the specified value Circuits are capable of storing string name-value pairs in their hardware file, for purposes such as tracking most recently-evaluated fitness of a Circuit

Parameters:
  • attribute (str) – The name of the attribute to modify

  • value (str) – The value to assign to the attribute

static set_file_attribute_st(hardware_file, attribute, value)[source]#

Sets a Circuit’s file attribute to the specified value Circuits are capable of storing string name-value pairs in their hardware file, for purposes such as tracking most recently-evaluated fitness of a Circuit Static version of set_file_attribute that requires the memory-mapped file to be provided

Parameters:
  • hardware_file (mmap) – The memory-mapped hardware file of the Circuit

  • attribute (str) – The name of the attribute to modify

  • value (str) – The value to assign to the attribute