netlist_carpentry.utils.gate_lib_base_classes
¶
This module provides a set of classes for modeling digital circuits at the gate level. It currently includes base classes for primitive gates, unary gates, binary gates, and clocked gates, as well as methods for evaluating the output signals of these gates. They provide a common interface for working with different types of gates, including methods for setting input signals, evaluating output signals, and updating gate states. See the gate_lib.py module for further information.
Classes:
-
PrimitiveGate–A base class for all primitive gates.
-
UnaryGate–A base class for unary gates.
-
ReduceGate–A base class for reduce gates.
-
BinaryGate–A base class for binary gates.
-
ArithmeticGate–A base class for arithmetic gates.
-
BinaryNto1Gate– -
StorageGate– -
ClkMixin–A mixin class for clocked gates. Clocked gates are gates that have a clock signal.
-
EnMixin– -
RstMixin– -
ScanMixin–
PrimitiveGate
¶
Bases: Instance, BaseModel
A base class for all primitive gates.
Primitive gates are the basic building blocks of digital circuits, and they can be combined to create more complex circuits. This class provides a common interface for all primitive gates, including methods for evaluating the gate's output and setting its output signal.
Methods:
-
p2v–Converts a Port object to its corresponding Verilog structure by using the connected wire segments.
-
set–Sets the signal on a port.
-
evaluate–Evaluates the gate's output signal based on its input signals.
-
change_mutability–Change the mutability status of this instance and optionally its ports.
-
all_connections–Returns a dictionary mapping port names to their corresponding connection paths.
-
connect–Add connections to the specified port of this instance.
-
disconnect–Remove an existing connection from a specified port of this instance.
-
get_connection–Retrieve the connection path associated with a specified port and bit index.
-
modify_connection–Modify an existing connection within a specified port of this instance.
-
connect_modify–Add a new connection or modify an existing one within the specified port of this instance.
-
tie_port–Set a constant signal value for the specified port and bit index.
-
has_tied_ports–Checks the ports of this instance whether any are tied to constant values.
-
has_tied_inputs–Checks the input ports of this instance whether any are tied to constant values.
-
has_tied_outputs–Checks the output ports of this instance whether any are tied to constant values.
-
update_signedness–Retrieves the signedness of the given port and updates it in this gate instance's parameter dictionary.
-
split–Performs a bit-wise split on this instance.
Attributes:
-
instance_type(str) –Identifier for instances of this gate type.
-
parameters(TypedParams) –Parameters of this gate, e.g. data width, signedness or polarity.
-
y_width(PositiveInt) –Width of the gate, based on a certain port's width, depending on the actual gate.
-
a_width(PositiveInt) –Width of the gate's
Aport. -
b_width(PositiveInt) –Width of the gate's
Bport. -
is_combinational(bool) –Whether instances of this gate are considered combinational gates.
-
is_sequential(bool) –Whether instances of this gate are considered sequential gates.
-
output_port(Port[Instance]) –The output port of the gate.
-
data_width(int) –The data width of this instance.
-
verilog_net_map(Dict[str, str]) –The parts of the Verilog representation of this gate instance as a dict.
-
name(str) –The name of the element.
-
metadata(MetadataMixin) –Metadata of a netlist element.
-
path(InstancePath) –Returns the InstancePath of the netlist element.
-
type(EType) –The type of the element, which is an instance.
-
hierarchy_level(int) –The level of hierarchy of the element in the design.
-
has_parent(bool) –Whether this object has a parent.
-
circuit('Circuit') –The circuit object to which this netlist element belongs to.
-
has_circuit(bool) –Whether this netlist element has a defined circuit it belongs to.
-
locked(bool) –True if this NetlistElement instance is locked (i.e. it is currently structurally unchangeable), False if it is mutable.
-
is_placeholder_instance(bool) –A placeholder represents an element that does not have a specific path.
-
can_carry_signal(bool) –Whether this exact object is able to receive, pass or hold signal values.
-
connections(Dict[str, Dict[int, WireSegmentPath]]) –A dictionary mapping port names to their corresponding connection paths.
-
connection_str_paths(Dict[str, Dict[int, str]]) –Returns a dictionary mapping port names to their corresponding connection paths as strings.
-
ports(CustomDict[str, Port[Instance]]) –A dictionary mapping port names to their corresponding Port objects.
-
input_ports(Tuple[Port[Instance], ...]) –Returns a tuple of input ports associated with this instance.
-
output_ports(Tuple[Port[Instance], ...]) –Returns a tuple of output ports associated with this instance.
-
has_unconnected_port_segments(bool) –Returns True if the instance has at least one unconnected port segment.
-
signals(Dict[str, Dict[int, Signal]]) –A dictionary with all signals currently present on all ports of this instance.
-
is_blackbox(bool) –Flag indicating whether the instance represents neither a primitive element nor a module instance.
-
splittable(bool) –Whether n-bit wide instances of this type can be split into n 1-bit wide instances.
-
verilog(str) –Generates the Verilog code for this instance.
instance_type
class-attribute
instance-attribute
¶
instance_type: str = id_internal
Identifier for instances of this gate type.
parameters
class-attribute
instance-attribute
¶
Parameters of this gate, e.g. data width, signedness or polarity.
y_width
property
¶
Width of the gate, based on a certain port's width, depending on the actual gate.
is_combinational
property
¶
is_combinational: bool
Whether instances of this gate are considered combinational gates.
True for combinational gates, such as AND gates or arithmetic gates.
False for flip-flops and latches.
is_sequential
property
¶
is_sequential: bool
Whether instances of this gate are considered sequential gates.
False for combinational gates, such as AND gates or arithmetic gates.
True for flip-flops and latches.
data_width
property
¶
data_width: int
The data width of this instance.
Defaults to the data width of the output port. Can be overwritten in extended classes.
In contrast to self.width, which is used for the creation and initialization,
this property is linked to the data width of the output port.
This property is useful when the data width of the output port can be changed.
verilog_net_map
property
¶
name
instance-attribute
¶
name: str
The name of the element.
!!! Do not modify this variable after instantiating it. Use NetlistElement.set_name() instead. Modifying this variable might lead to inconsistencies later on.
metadata
class-attribute
instance-attribute
¶
metadata: MetadataMixin = MetadataMixin()
Metadata of a netlist element.
Can be user-defined, or e. g. by Yosys (such as src for the HDL source).
Is also grouped by categories, i.e. all metadata from Yosys can be accessed via Module.metadata["yosys"],
or via Module.metadata.yosys, which both return a dictionary of all metadata.
Read the documentation of MetaDataMixin for more information.
path
property
¶
path: InstancePath
Returns the InstancePath of the netlist element.
The InstancePath object is constructed using the element's type and its raw hierarchical path.
Returns:
-
InstancePath(InstancePath) –The hierarchical path of the netlist element.
hierarchy_level
property
¶
hierarchy_level: int
The level of hierarchy of the element in the design.
The hierarchy level is the number of separators in the element's raw path. For example, a top-level instance has a hierarchy level of 0, while a direct submodule instance has a hierarchy level of 1.
Returns:
-
int(int) –The hierarchy level of the element.
circuit
property
¶
The circuit object to which this netlist element belongs to.
- For a module, returns the circuit to which the module belongs.
- For any other netlist element, recursively returns the circuit of the parent, which ultimately leads to a module, to which the netlist element belongs.
Raises:
-
ParentNotFoundError–If a parent cannot be resolved somewhere in the hierarchical chain.
-
ObjectNotFoundError–If for a module no circuit is set.
has_circuit
property
¶
has_circuit: bool
Whether this netlist element has a defined circuit it belongs to.
Tries to access self.circuit and returns whether the call was successful.
Can be used instead of a try-except clause around the call to NetlistElement.circuit.
locked
property
¶
locked: bool
True if this NetlistElement instance is locked (i.e. it is currently structurally unchangeable), False if it is mutable.
Can be changed via NetlistElement.change_mutability(bool).
Immutability is used to prevent accidental changes to the design.
is_placeholder_instance
property
¶
is_placeholder_instance: bool
A placeholder represents an element that does not have a specific path.
True if this NetlistElement instance represents a placeholder, False otherwise.
can_carry_signal
property
¶
can_carry_signal: bool
Whether this exact object is able to receive, pass or hold signal values.
True for ports (as they drive or receive values) and wires (as they pass the signals
from driver to load ports) as well as their respective segments.
False for instances and modules.
connections
property
¶
connections: Dict[str, Dict[int, WireSegmentPath]]
A dictionary mapping port names to their corresponding connection paths.
The keys of the outer dictionary are the names of the ports.
The values are dictionaries where the keys are the port bit numbers and the values are WireSegmentPath objects.
The element paths are the paths of the wires connected to the corresponding bits of the port.
Elements with no connection (i.e. the path is None) are excluded.
In case these are needed, use Instance.get_connections() instead.
Example
{ 'port1': {1: WireSegmentPath('path/to/element1'), 2: WireSegmentPath('path/to/element2')}, 'port2': {1: WireSegmentPath('path/to/element3')} }
connection_str_paths
property
¶
Returns a dictionary mapping port names to their corresponding connection paths as strings.
The keys are the names of the ports. The values are dictionaries where the keys are the port bit numbers and the values are the raw string paths.
Example
{ 'port1': {0: 'path.to.element1', 1: 'path.to.element2'}, 'port2': {0: 'path.to.element3'} }
ports
property
¶
ports: CustomDict[str, Port[Instance]]
A dictionary mapping port names to their corresponding Port objects.
The keys of the dictionary are the names of the ports. The values are Port objects representing the ports.
Returns:
-
CustomDict[str, Port[Instance]]–A dictionary mapping port names to their corresponding Port objects.
input_ports
property
¶
output_ports
property
¶
has_unconnected_port_segments
property
¶
has_unconnected_port_segments: bool
Returns True if the instance has at least one unconnected port segment.
signals
property
¶
A dictionary with all signals currently present on all ports of this instance.
is_blackbox
property
¶
is_blackbox: bool
Flag indicating whether the instance represents neither a primitive element nor a module instance.
If True, this instance does not have a module definition, and is also not a basic component (i.e. a primitive gate instance) from the internal gate library, such as a gate or flip-flop.
splittable
property
¶
splittable: bool
Whether n-bit wide instances of this type can be split into n 1-bit wide instances.
Supported for gate instances, where splitting does not change the overall behavior, e.g. splitting an 8-bit AND gate into 8 1-bit AND gates works fine, but an 8bit OR-REDUCE gate cannot be split, as this would change the behavior of the circuit.
verilog
property
¶
verilog: str
Generates the Verilog code for this instance.
This property uses the verilog_template to generate the actual Verilog code by replacing the placeholders with
the instance type, name, and port connections. It returns a string that can be used directly in a Verilog file.
Returns:
-
str(str) –The generated Verilog code.
p2v
¶
Converts a Port object to its corresponding Verilog structure by using the connected wire segments.
This method takes the connected wire segments of a Port object and converts them to their corresponding Verilog signal structure (p2ws2v -> Port to WireSegment to Verilog signal syntax). The method requires that the currently selected module matches the module of the Port object, which is derived from the design path of the Port object. For each segment of the port, it checks whether a corresponding connected wire segment exists in the current module. If the port is set to a constant, the corresponding constant wire segment placeholder is used instead. Port segments can be excluded from the conversion by providing a list of indices, indicating which segments should be excluded from the conversion (e.g. segments that are known to be unconnected).
Parameters:
-
(port¶Port) –The Port object to convert.
-
(exclude_indices¶List[int], default:None) –A list of indices to exclude from the conversion. Defaults to an empty list.
Returns:
-
str(str) –The Verilog signal structure as a string.
Raises:
-
AttributeError–If the currently selected module does not match the module of the port.
set
¶
set(port_name: str, new_signal: SignalOrLogicLevel) -> None
evaluate
¶
Evaluates the gate's output signal based on its input signals.
This method is called when the gate's input signals change, and it updates the gate's output signal accordingly.
change_mutability
¶
change_mutability(is_now_locked: bool, recursive: bool = False) -> Self
Change the mutability status of this instance and optionally its ports.
This method allows setting whether this instance can be modified or not.
If recursive is set to True, it also applies the same mutability status to all ports of this instance.
Parameters:
all_connections
¶
all_connections(
include_unconnected: bool,
) -> Dict[str, Dict[int, WireSegmentPath]]
Returns a dictionary mapping port names to their corresponding connection paths.
The keys of the outer dictionary are the names of the ports. The values are dictionaries where the keys are the port bit numbers and the values are WireSegmentPath objects. The element paths are the paths of the wires connected to the corresponding bits of the port.
Parameters:
Returns:
-
Dict[str, Dict[int, WireSegmentPath]]–A dictionary mapping port names to their corresponding connection paths.
connect
¶
connect(
port_name: str,
ws_path: Optional[WireSegmentPath],
direction: Direction = UNKNOWN,
index: NonNegativeInt = 0,
width: PositiveInt = 1,
) -> None
Add connections to the specified port of this instance.
This method can establish multiple connections if a range of indices is provided.
Parameters:
-
(port_name¶str) –The name of the port where the connection(s) should be established.
-
(ws_path¶Optional[WireSegmentPath]) –The path of the wire segment that will be connected to this port. If None, the associated port (segment) remains unconnected.
-
(direction¶Direction, default:UNKNOWN) –The direction of the port. Defaults to Direction.UNKNOWN.
-
(index¶NonNegativeInt, default:0) –The starting bit index within the port for these connections. Defaults to 0.
-
(width¶PositiveInt, default:1) –The number of consecutive bits in the port that should be connected. Defaults to 1.
Raises:
-
ObjectLockedError–If this instance is currently locked, and no connection can be made.
disconnect
¶
Remove an existing connection from a specified port of this instance.
This method disconnects the wire segment at the given index within the specified port_name.
Parameters:
-
(port_name¶str) –The name of the port where the connection should be removed.
-
(index¶Optional[int], default:None) –The bit index within the port for this disconnection. Defaults to None, which completely disconnects the port.
Raises:
-
ObjectLockedError–If this instance is locked.
-
ObjectNotFoundError–If no port exists with the given name.
get_connection
¶
get_connection(
port_name: str, index: Optional[NonNegativeInt] = None
) -> Union[WireSegmentPath, Dict[int, WireSegmentPath], None]
Retrieve the connection path associated with a specified port and bit index.
If index is provided, this method returns the wire segment path connected to that specific bit within the port.
Otherwise, it returns all connections for the given port as a dictionary mapping indices to WireSegmentPaths.
Parameters:
-
(port_name¶str) –The name of the port for which to retrieve the connection(s).
-
(index¶Optional[NonNegativeInt], default:None) –The bit index within the port. Defaults to None.
Returns:
-
Union[WireSegmentPath, Dict[int, WireSegmentPath], None]–Union[WireSegmentPath, Dict[int, WireSegmentPath]]: Either a single WireSegmentPath or a dictionary mapping indices to WireSegmentPaths.
modify_connection
¶
modify_connection(
port_name: str, ws_path: WireSegmentPath, index: NonNegativeInt = 0
) -> None
Modify an existing connection within a specified port of this instance.
This method updates the wire segment path at the given index within the specified port_name.
If the index does not exist yet, it is newly added, changing the width of the port.
Parameters:
-
(port_name¶str) –The name of the port where the connection should be modified.
-
(ws_path¶WireSegmentPath) –The new wire segment path for this connection.
-
(index¶NonNegativeInt, default:0) –The bit index within the port. Defaults to 0.
connect_modify
¶
connect_modify(
port_name: str,
ws_path: WireSegmentPath,
direction: Direction = UNKNOWN,
index: NonNegativeInt = 0,
width: PositiveInt = 1,
) -> None
Add a new connection or modify an existing one within the specified port of this instance.
If the connection already exists at the given index and port_name, it will be modified.
Otherwise, a new connection is added.
Parameters:
-
(port_name¶str) –The name of the port where the connection should be established or updated.
-
(ws_path¶WireSegmentPath) –The path of the wire segment that will be connected to this port.
-
(direction¶Direction, default:UNKNOWN) –The direction of the port. Defaults to Direction.UNKNOWN.
-
(index¶NonNegativeInt, default:0) –The starting bit index within the port for these connections. Defaults to 0.
-
(width¶PositiveInt, default:1) –The number of consecutive bits in the port that should be connected. Defaults to 1.
tie_port
¶
Set a constant signal value for the specified port and bit index.
If the specified port does not exist, an error message is logged and the function returns False. Otherwise, the method tries to set the constant signal value for that port and returns True if successful.
Does not work for instance output ports, as they are always driven by their parent instances.
Parameters:
-
(name¶str) –The name of the port.
-
(index¶NonNegativeInt) –The bit index within the port.
-
(sig_value¶LogicLevel) –The constant signal value to be set ('0', '1', or 'Z').
Raises:
-
ObjectNotFoundError–If no such port or port segment exists.
-
AlreadyConnectedError–(raised by: PortSegment.tie_signal) If this segment is belongs to a load port and is already connected to a wire, from which it receives its value.
-
InvalidDirectionError–(raised by: PortSegment.tie_signal) If this port segment belongs to an instance output port, which is driven by the instance inputs and the instance's internal logic.
-
InvalidSignalError–(raised by: PortSegment.tie_signal) If an invalid value is provided.
has_tied_ports
¶
has_tied_ports() -> bool
Checks the ports of this instance whether any are tied to constant values.
Instance ports can be tied to a constant value for several reasons, but if all input or all output ports are tied, the instance is rendered useless.
Returns:
-
bool(bool) –True if any ports of this instance are tied to constant values. False otherwise, i.e. all ports are connected to wires.
has_tied_inputs
¶
has_tied_inputs() -> bool
Checks the input ports of this instance whether any are tied to constant values.
Instance ports can be tied to a constant value for several reasons, but if all input or all output ports are tied, the instance is rendered useless.
Returns:
-
bool(bool) –True if any input ports of this instance are tied to constant values. False otherwise, i.e. all input ports are connected to wires.
has_tied_outputs
¶
has_tied_outputs() -> bool
Checks the output ports of this instance whether any are tied to constant values.
Instance ports can be tied to a constant value for several reasons, but if all input or all output ports are tied, the instance is rendered useless.
Returns:
-
bool(bool) –True if any output ports of this instance are tied to constant values. False otherwise, i.e. all output ports are connected to wires.
update_signedness
¶
split
¶
Performs a bit-wise split on this instance.
If this instance supports splitting and has a data width of n bit, this method splits this instance into n 1-bit instances. This works for instances, where each output bit depends only on the corresponding input bit(s), e.g. an AND gate or a D-Flipflop.
Returns:
-
Dict[NonNegativeInt, Self]–Dict[int, Instance]: An n-bit large dictionary, where each key conforms to a bit of the original instance, and each value is an 1-bit instance representing the corresponding "slice" of the original instance.
Raises:
-
SplittingUnsupportedError–If this instance does not support splitting. Happens for any gate or instance whose behavior depends on the whole bus, and splitting would make it lose its meaning.
UnaryGate
¶
Bases: PrimitiveGate, BaseModel
A base class for unary gates.
Unary gates are gates that have a single input signal, and they produce a single output signal. This class provides a common interface for all unary gates, including methods for evaluating the gate's output and setting its output signal.
Methods:
-
model_post_init–Initializes the gate's ports and connections.
-
signal_in–The input signal of the gate.
-
signal_out–The output signal of the gate.
-
change_mutability–Change the mutability status of this instance and optionally its ports.
-
evaluate–Evaluates the gate's output signal based on its input signals.
-
all_connections–Returns a dictionary mapping port names to their corresponding connection paths.
-
connect–Add connections to the specified port of this instance.
-
disconnect–Remove an existing connection from a specified port of this instance.
-
get_connection–Retrieve the connection path associated with a specified port and bit index.
-
modify_connection–Modify an existing connection within a specified port of this instance.
-
connect_modify–Add a new connection or modify an existing one within the specified port of this instance.
-
tie_port–Set a constant signal value for the specified port and bit index.
-
has_tied_ports–Checks the ports of this instance whether any are tied to constant values.
-
has_tied_inputs–Checks the input ports of this instance whether any are tied to constant values.
-
has_tied_outputs–Checks the output ports of this instance whether any are tied to constant values.
-
update_signedness–Retrieves the signedness of the given port and updates it in this gate instance's parameter dictionary.
-
split–Performs a bit-wise split on this instance.
-
p2v–Converts a Port object to its corresponding Verilog structure by using the connected wire segments.
-
set–Sets the signal on a port.
Attributes:
-
input_port(Port[Instance]) –The input port of the gate.
-
a_signed(bool) –The signedness of input port A.
-
output_port(Port[Instance]) –The output port of the gate.
-
name(str) –The name of the element.
-
metadata(MetadataMixin) –Metadata of a netlist element.
-
path(InstancePath) –Returns the InstancePath of the netlist element.
-
type(EType) –The type of the element, which is an instance.
-
hierarchy_level(int) –The level of hierarchy of the element in the design.
-
has_parent(bool) –Whether this object has a parent.
-
circuit('Circuit') –The circuit object to which this netlist element belongs to.
-
has_circuit(bool) –Whether this netlist element has a defined circuit it belongs to.
-
locked(bool) –True if this NetlistElement instance is locked (i.e. it is currently structurally unchangeable), False if it is mutable.
-
is_placeholder_instance(bool) –A placeholder represents an element that does not have a specific path.
-
can_carry_signal(bool) –Whether this exact object is able to receive, pass or hold signal values.
-
instance_type(str) –Identifier for instances of this gate type.
-
connections(Dict[str, Dict[int, WireSegmentPath]]) –A dictionary mapping port names to their corresponding connection paths.
-
connection_str_paths(Dict[str, Dict[int, str]]) –Returns a dictionary mapping port names to their corresponding connection paths as strings.
-
ports(CustomDict[str, Port[Instance]]) –A dictionary mapping port names to their corresponding Port objects.
-
input_ports(Tuple[Port[Instance], ...]) –Returns a tuple of input ports associated with this instance.
-
output_ports(Tuple[Port[Instance], ...]) –Returns a tuple of output ports associated with this instance.
-
has_unconnected_port_segments(bool) –Returns True if the instance has at least one unconnected port segment.
-
signals(Dict[str, Dict[int, Signal]]) –A dictionary with all signals currently present on all ports of this instance.
-
is_blackbox(bool) –Flag indicating whether the instance represents neither a primitive element nor a module instance.
-
y_width(PositiveInt) –Width of the gate, based on a certain port's width, depending on the actual gate.
-
a_width(PositiveInt) –Width of the gate's
Aport. -
is_combinational(bool) –Whether instances of this gate are considered combinational gates.
-
is_sequential(bool) –Whether instances of this gate are considered sequential gates.
-
data_width(int) –The data width of this instance.
name
instance-attribute
¶
name: str
The name of the element.
!!! Do not modify this variable after instantiating it. Use NetlistElement.set_name() instead. Modifying this variable might lead to inconsistencies later on.
metadata
class-attribute
instance-attribute
¶
metadata: MetadataMixin = MetadataMixin()
Metadata of a netlist element.
Can be user-defined, or e. g. by Yosys (such as src for the HDL source).
Is also grouped by categories, i.e. all metadata from Yosys can be accessed via Module.metadata["yosys"],
or via Module.metadata.yosys, which both return a dictionary of all metadata.
Read the documentation of MetaDataMixin for more information.
path
property
¶
path: InstancePath
Returns the InstancePath of the netlist element.
The InstancePath object is constructed using the element's type and its raw hierarchical path.
Returns:
-
InstancePath(InstancePath) –The hierarchical path of the netlist element.
hierarchy_level
property
¶
hierarchy_level: int
The level of hierarchy of the element in the design.
The hierarchy level is the number of separators in the element's raw path. For example, a top-level instance has a hierarchy level of 0, while a direct submodule instance has a hierarchy level of 1.
Returns:
-
int(int) –The hierarchy level of the element.
circuit
property
¶
The circuit object to which this netlist element belongs to.
- For a module, returns the circuit to which the module belongs.
- For any other netlist element, recursively returns the circuit of the parent, which ultimately leads to a module, to which the netlist element belongs.
Raises:
-
ParentNotFoundError–If a parent cannot be resolved somewhere in the hierarchical chain.
-
ObjectNotFoundError–If for a module no circuit is set.
has_circuit
property
¶
has_circuit: bool
Whether this netlist element has a defined circuit it belongs to.
Tries to access self.circuit and returns whether the call was successful.
Can be used instead of a try-except clause around the call to NetlistElement.circuit.
locked
property
¶
locked: bool
True if this NetlistElement instance is locked (i.e. it is currently structurally unchangeable), False if it is mutable.
Can be changed via NetlistElement.change_mutability(bool).
Immutability is used to prevent accidental changes to the design.
is_placeholder_instance
property
¶
is_placeholder_instance: bool
A placeholder represents an element that does not have a specific path.
True if this NetlistElement instance represents a placeholder, False otherwise.
can_carry_signal
property
¶
can_carry_signal: bool
Whether this exact object is able to receive, pass or hold signal values.
True for ports (as they drive or receive values) and wires (as they pass the signals
from driver to load ports) as well as their respective segments.
False for instances and modules.
instance_type
class-attribute
instance-attribute
¶
instance_type: str = id_internal
Identifier for instances of this gate type.
connections
property
¶
connections: Dict[str, Dict[int, WireSegmentPath]]
A dictionary mapping port names to their corresponding connection paths.
The keys of the outer dictionary are the names of the ports.
The values are dictionaries where the keys are the port bit numbers and the values are WireSegmentPath objects.
The element paths are the paths of the wires connected to the corresponding bits of the port.
Elements with no connection (i.e. the path is None) are excluded.
In case these are needed, use Instance.get_connections() instead.
Example
{ 'port1': {1: WireSegmentPath('path/to/element1'), 2: WireSegmentPath('path/to/element2')}, 'port2': {1: WireSegmentPath('path/to/element3')} }
connection_str_paths
property
¶
Returns a dictionary mapping port names to their corresponding connection paths as strings.
The keys are the names of the ports. The values are dictionaries where the keys are the port bit numbers and the values are the raw string paths.
Example
{ 'port1': {0: 'path.to.element1', 1: 'path.to.element2'}, 'port2': {0: 'path.to.element3'} }
ports
property
¶
ports: CustomDict[str, Port[Instance]]
A dictionary mapping port names to their corresponding Port objects.
The keys of the dictionary are the names of the ports. The values are Port objects representing the ports.
Returns:
-
CustomDict[str, Port[Instance]]–A dictionary mapping port names to their corresponding Port objects.
input_ports
property
¶
output_ports
property
¶
has_unconnected_port_segments
property
¶
has_unconnected_port_segments: bool
Returns True if the instance has at least one unconnected port segment.
signals
property
¶
A dictionary with all signals currently present on all ports of this instance.
is_blackbox
property
¶
is_blackbox: bool
Flag indicating whether the instance represents neither a primitive element nor a module instance.
If True, this instance does not have a module definition, and is also not a basic component (i.e. a primitive gate instance) from the internal gate library, such as a gate or flip-flop.
y_width
property
¶
Width of the gate, based on a certain port's width, depending on the actual gate.
is_combinational
property
¶
is_combinational: bool
Whether instances of this gate are considered combinational gates.
True for combinational gates, such as AND gates or arithmetic gates.
False for flip-flops and latches.
is_sequential
property
¶
is_sequential: bool
Whether instances of this gate are considered sequential gates.
False for combinational gates, such as AND gates or arithmetic gates.
True for flip-flops and latches.
data_width
property
¶
data_width: int
The data width of this instance.
Defaults to the data width of the output port. Can be overwritten in extended classes.
In contrast to self.width, which is used for the creation and initialization,
this property is linked to the data width of the output port.
This property is useful when the data width of the output port can be changed.
model_post_init
¶
Initializes the gate's ports and connections.
This method is called after the gate's attributes have been initialized, and it sets up the gate's ports and connections.
change_mutability
¶
change_mutability(is_now_locked: bool, recursive: bool = False) -> Self
Change the mutability status of this instance and optionally its ports.
This method allows setting whether this instance can be modified or not.
If recursive is set to True, it also applies the same mutability status to all ports of this instance.
Parameters:
evaluate
¶
Evaluates the gate's output signal based on its input signals.
This method is called when the gate's input signals change, and it updates the gate's output signal accordingly.
all_connections
¶
all_connections(
include_unconnected: bool,
) -> Dict[str, Dict[int, WireSegmentPath]]
Returns a dictionary mapping port names to their corresponding connection paths.
The keys of the outer dictionary are the names of the ports. The values are dictionaries where the keys are the port bit numbers and the values are WireSegmentPath objects. The element paths are the paths of the wires connected to the corresponding bits of the port.
Parameters:
Returns:
-
Dict[str, Dict[int, WireSegmentPath]]–A dictionary mapping port names to their corresponding connection paths.
connect
¶
connect(
port_name: str,
ws_path: Optional[WireSegmentPath],
direction: Direction = UNKNOWN,
index: NonNegativeInt = 0,
width: PositiveInt = 1,
) -> None
Add connections to the specified port of this instance.
This method can establish multiple connections if a range of indices is provided.
Parameters:
-
(port_name¶str) –The name of the port where the connection(s) should be established.
-
(ws_path¶Optional[WireSegmentPath]) –The path of the wire segment that will be connected to this port. If None, the associated port (segment) remains unconnected.
-
(direction¶Direction, default:UNKNOWN) –The direction of the port. Defaults to Direction.UNKNOWN.
-
(index¶NonNegativeInt, default:0) –The starting bit index within the port for these connections. Defaults to 0.
-
(width¶PositiveInt, default:1) –The number of consecutive bits in the port that should be connected. Defaults to 1.
Raises:
-
ObjectLockedError–If this instance is currently locked, and no connection can be made.
disconnect
¶
Remove an existing connection from a specified port of this instance.
This method disconnects the wire segment at the given index within the specified port_name.
Parameters:
-
(port_name¶str) –The name of the port where the connection should be removed.
-
(index¶Optional[int], default:None) –The bit index within the port for this disconnection. Defaults to None, which completely disconnects the port.
Raises:
-
ObjectLockedError–If this instance is locked.
-
ObjectNotFoundError–If no port exists with the given name.
get_connection
¶
get_connection(
port_name: str, index: Optional[NonNegativeInt] = None
) -> Union[WireSegmentPath, Dict[int, WireSegmentPath], None]
Retrieve the connection path associated with a specified port and bit index.
If index is provided, this method returns the wire segment path connected to that specific bit within the port.
Otherwise, it returns all connections for the given port as a dictionary mapping indices to WireSegmentPaths.
Parameters:
-
(port_name¶str) –The name of the port for which to retrieve the connection(s).
-
(index¶Optional[NonNegativeInt], default:None) –The bit index within the port. Defaults to None.
Returns:
-
Union[WireSegmentPath, Dict[int, WireSegmentPath], None]–Union[WireSegmentPath, Dict[int, WireSegmentPath]]: Either a single WireSegmentPath or a dictionary mapping indices to WireSegmentPaths.
modify_connection
¶
modify_connection(
port_name: str, ws_path: WireSegmentPath, index: NonNegativeInt = 0
) -> None
Modify an existing connection within a specified port of this instance.
This method updates the wire segment path at the given index within the specified port_name.
If the index does not exist yet, it is newly added, changing the width of the port.
Parameters:
-
(port_name¶str) –The name of the port where the connection should be modified.
-
(ws_path¶WireSegmentPath) –The new wire segment path for this connection.
-
(index¶NonNegativeInt, default:0) –The bit index within the port. Defaults to 0.
connect_modify
¶
connect_modify(
port_name: str,
ws_path: WireSegmentPath,
direction: Direction = UNKNOWN,
index: NonNegativeInt = 0,
width: PositiveInt = 1,
) -> None
Add a new connection or modify an existing one within the specified port of this instance.
If the connection already exists at the given index and port_name, it will be modified.
Otherwise, a new connection is added.
Parameters:
-
(port_name¶str) –The name of the port where the connection should be established or updated.
-
(ws_path¶WireSegmentPath) –The path of the wire segment that will be connected to this port.
-
(direction¶Direction, default:UNKNOWN) –The direction of the port. Defaults to Direction.UNKNOWN.
-
(index¶NonNegativeInt, default:0) –The starting bit index within the port for these connections. Defaults to 0.
-
(width¶PositiveInt, default:1) –The number of consecutive bits in the port that should be connected. Defaults to 1.
tie_port
¶
Set a constant signal value for the specified port and bit index.
If the specified port does not exist, an error message is logged and the function returns False. Otherwise, the method tries to set the constant signal value for that port and returns True if successful.
Does not work for instance output ports, as they are always driven by their parent instances.
Parameters:
-
(name¶str) –The name of the port.
-
(index¶NonNegativeInt) –The bit index within the port.
-
(sig_value¶LogicLevel) –The constant signal value to be set ('0', '1', or 'Z').
Raises:
-
ObjectNotFoundError–If no such port or port segment exists.
-
AlreadyConnectedError–(raised by: PortSegment.tie_signal) If this segment is belongs to a load port and is already connected to a wire, from which it receives its value.
-
InvalidDirectionError–(raised by: PortSegment.tie_signal) If this port segment belongs to an instance output port, which is driven by the instance inputs and the instance's internal logic.
-
InvalidSignalError–(raised by: PortSegment.tie_signal) If an invalid value is provided.
has_tied_ports
¶
has_tied_ports() -> bool
Checks the ports of this instance whether any are tied to constant values.
Instance ports can be tied to a constant value for several reasons, but if all input or all output ports are tied, the instance is rendered useless.
Returns:
-
bool(bool) –True if any ports of this instance are tied to constant values. False otherwise, i.e. all ports are connected to wires.
has_tied_inputs
¶
has_tied_inputs() -> bool
Checks the input ports of this instance whether any are tied to constant values.
Instance ports can be tied to a constant value for several reasons, but if all input or all output ports are tied, the instance is rendered useless.
Returns:
-
bool(bool) –True if any input ports of this instance are tied to constant values. False otherwise, i.e. all input ports are connected to wires.
has_tied_outputs
¶
has_tied_outputs() -> bool
Checks the output ports of this instance whether any are tied to constant values.
Instance ports can be tied to a constant value for several reasons, but if all input or all output ports are tied, the instance is rendered useless.
Returns:
-
bool(bool) –True if any output ports of this instance are tied to constant values. False otherwise, i.e. all output ports are connected to wires.
update_signedness
¶
split
¶
Performs a bit-wise split on this instance.
If this instance supports splitting and has a data width of n bit, this method splits this instance into n 1-bit instances. This works for instances, where each output bit depends only on the corresponding input bit(s), e.g. an AND gate or a D-Flipflop.
Returns:
-
Dict[NonNegativeInt, Self]–Dict[int, Instance]: An n-bit large dictionary, where each key conforms to a bit of the original instance, and each value is an 1-bit instance representing the corresponding "slice" of the original instance.
Raises:
-
SplittingUnsupportedError–If this instance does not support splitting. Happens for any gate or instance whose behavior depends on the whole bus, and splitting would make it lose its meaning.
p2v
¶
Converts a Port object to its corresponding Verilog structure by using the connected wire segments.
This method takes the connected wire segments of a Port object and converts them to their corresponding Verilog signal structure (p2ws2v -> Port to WireSegment to Verilog signal syntax). The method requires that the currently selected module matches the module of the Port object, which is derived from the design path of the Port object. For each segment of the port, it checks whether a corresponding connected wire segment exists in the current module. If the port is set to a constant, the corresponding constant wire segment placeholder is used instead. Port segments can be excluded from the conversion by providing a list of indices, indicating which segments should be excluded from the conversion (e.g. segments that are known to be unconnected).
Parameters:
-
(port¶Port) –The Port object to convert.
-
(exclude_indices¶List[int], default:None) –A list of indices to exclude from the conversion. Defaults to an empty list.
Returns:
-
str(str) –The Verilog signal structure as a string.
Raises:
-
AttributeError–If the currently selected module does not match the module of the port.
ReduceGate
¶
Bases: UnaryGate, BaseModel
A base class for reduce gates.
Reduce gates are gates that have an n-bit input signal, and they produce a 1-bit output signal by performing a given reducing operation. This class provides a common interface for all reduce gates, including methods for evaluating the gate's output and setting its output signal.
Methods:
-
model_post_init–Initializes the gate's ports and connections.
-
signal_out–The output signal of the gate.
-
change_mutability–Change the mutability status of this instance and optionally its ports.
-
evaluate–Evaluates the gate's output signal based on its input signals.
-
all_connections–Returns a dictionary mapping port names to their corresponding connection paths.
-
connect–Add connections to the specified port of this instance.
-
disconnect–Remove an existing connection from a specified port of this instance.
-
get_connection–Retrieve the connection path associated with a specified port and bit index.
-
modify_connection–Modify an existing connection within a specified port of this instance.
-
connect_modify–Add a new connection or modify an existing one within the specified port of this instance.
-
tie_port–Set a constant signal value for the specified port and bit index.
-
has_tied_ports–Checks the ports of this instance whether any are tied to constant values.
-
has_tied_inputs–Checks the input ports of this instance whether any are tied to constant values.
-
has_tied_outputs–Checks the output ports of this instance whether any are tied to constant values.
-
update_signedness–Retrieves the signedness of the given port and updates it in this gate instance's parameter dictionary.
-
split–Performs a bit-wise split on this instance.
-
p2v–Converts a Port object to its corresponding Verilog structure by using the connected wire segments.
-
set–Sets the signal on a port.
-
signal_in–The input signal of the gate.
Attributes:
-
y_width(PositiveInt) –Width of the gate, based on a certain port's width, depending on the actual gate.
-
name(str) –The name of the element.
-
metadata(MetadataMixin) –Metadata of a netlist element.
-
path(InstancePath) –Returns the InstancePath of the netlist element.
-
type(EType) –The type of the element, which is an instance.
-
hierarchy_level(int) –The level of hierarchy of the element in the design.
-
has_parent(bool) –Whether this object has a parent.
-
circuit('Circuit') –The circuit object to which this netlist element belongs to.
-
has_circuit(bool) –Whether this netlist element has a defined circuit it belongs to.
-
locked(bool) –True if this NetlistElement instance is locked (i.e. it is currently structurally unchangeable), False if it is mutable.
-
is_placeholder_instance(bool) –A placeholder represents an element that does not have a specific path.
-
can_carry_signal(bool) –Whether this exact object is able to receive, pass or hold signal values.
-
instance_type(str) –Identifier for instances of this gate type.
-
connections(Dict[str, Dict[int, WireSegmentPath]]) –A dictionary mapping port names to their corresponding connection paths.
-
connection_str_paths(Dict[str, Dict[int, str]]) –Returns a dictionary mapping port names to their corresponding connection paths as strings.
-
ports(CustomDict[str, Port[Instance]]) –A dictionary mapping port names to their corresponding Port objects.
-
input_ports(Tuple[Port[Instance], ...]) –Returns a tuple of input ports associated with this instance.
-
output_ports(Tuple[Port[Instance], ...]) –Returns a tuple of output ports associated with this instance.
-
has_unconnected_port_segments(bool) –Returns True if the instance has at least one unconnected port segment.
-
signals(Dict[str, Dict[int, Signal]]) –A dictionary with all signals currently present on all ports of this instance.
-
is_blackbox(bool) –Flag indicating whether the instance represents neither a primitive element nor a module instance.
-
a_width(PositiveInt) –Width of the gate's
Aport. -
is_combinational(bool) –Whether instances of this gate are considered combinational gates.
-
is_sequential(bool) –Whether instances of this gate are considered sequential gates.
-
output_port(Port[Instance]) –The output port of the gate.
-
data_width(int) –The data width of this instance.
-
input_port(Port[Instance]) –The input port of the gate.
-
a_signed(bool) –The signedness of input port A.
y_width
property
¶
Width of the gate, based on a certain port's width, depending on the actual gate.
name
instance-attribute
¶
name: str
The name of the element.
!!! Do not modify this variable after instantiating it. Use NetlistElement.set_name() instead. Modifying this variable might lead to inconsistencies later on.
metadata
class-attribute
instance-attribute
¶
metadata: MetadataMixin = MetadataMixin()
Metadata of a netlist element.
Can be user-defined, or e. g. by Yosys (such as src for the HDL source).
Is also grouped by categories, i.e. all metadata from Yosys can be accessed via Module.metadata["yosys"],
or via Module.metadata.yosys, which both return a dictionary of all metadata.
Read the documentation of MetaDataMixin for more information.
path
property
¶
path: InstancePath
Returns the InstancePath of the netlist element.
The InstancePath object is constructed using the element's type and its raw hierarchical path.
Returns:
-
InstancePath(InstancePath) –The hierarchical path of the netlist element.
hierarchy_level
property
¶
hierarchy_level: int
The level of hierarchy of the element in the design.
The hierarchy level is the number of separators in the element's raw path. For example, a top-level instance has a hierarchy level of 0, while a direct submodule instance has a hierarchy level of 1.
Returns:
-
int(int) –The hierarchy level of the element.
circuit
property
¶
The circuit object to which this netlist element belongs to.
- For a module, returns the circuit to which the module belongs.
- For any other netlist element, recursively returns the circuit of the parent, which ultimately leads to a module, to which the netlist element belongs.
Raises:
-
ParentNotFoundError–If a parent cannot be resolved somewhere in the hierarchical chain.
-
ObjectNotFoundError–If for a module no circuit is set.
has_circuit
property
¶
has_circuit: bool
Whether this netlist element has a defined circuit it belongs to.
Tries to access self.circuit and returns whether the call was successful.
Can be used instead of a try-except clause around the call to NetlistElement.circuit.
locked
property
¶
locked: bool
True if this NetlistElement instance is locked (i.e. it is currently structurally unchangeable), False if it is mutable.
Can be changed via NetlistElement.change_mutability(bool).
Immutability is used to prevent accidental changes to the design.
is_placeholder_instance
property
¶
is_placeholder_instance: bool
A placeholder represents an element that does not have a specific path.
True if this NetlistElement instance represents a placeholder, False otherwise.
can_carry_signal
property
¶
can_carry_signal: bool
Whether this exact object is able to receive, pass or hold signal values.
True for ports (as they drive or receive values) and wires (as they pass the signals
from driver to load ports) as well as their respective segments.
False for instances and modules.
instance_type
class-attribute
instance-attribute
¶
instance_type: str = id_internal
Identifier for instances of this gate type.
connections
property
¶
connections: Dict[str, Dict[int, WireSegmentPath]]
A dictionary mapping port names to their corresponding connection paths.
The keys of the outer dictionary are the names of the ports.
The values are dictionaries where the keys are the port bit numbers and the values are WireSegmentPath objects.
The element paths are the paths of the wires connected to the corresponding bits of the port.
Elements with no connection (i.e. the path is None) are excluded.
In case these are needed, use Instance.get_connections() instead.
Example
{ 'port1': {1: WireSegmentPath('path/to/element1'), 2: WireSegmentPath('path/to/element2')}, 'port2': {1: WireSegmentPath('path/to/element3')} }
connection_str_paths
property
¶
Returns a dictionary mapping port names to their corresponding connection paths as strings.
The keys are the names of the ports. The values are dictionaries where the keys are the port bit numbers and the values are the raw string paths.
Example
{ 'port1': {0: 'path.to.element1', 1: 'path.to.element2'}, 'port2': {0: 'path.to.element3'} }
ports
property
¶
ports: CustomDict[str, Port[Instance]]
A dictionary mapping port names to their corresponding Port objects.
The keys of the dictionary are the names of the ports. The values are Port objects representing the ports.
Returns:
-
CustomDict[str, Port[Instance]]–A dictionary mapping port names to their corresponding Port objects.
input_ports
property
¶
output_ports
property
¶
has_unconnected_port_segments
property
¶
has_unconnected_port_segments: bool
Returns True if the instance has at least one unconnected port segment.
signals
property
¶
A dictionary with all signals currently present on all ports of this instance.
is_blackbox
property
¶
is_blackbox: bool
Flag indicating whether the instance represents neither a primitive element nor a module instance.
If True, this instance does not have a module definition, and is also not a basic component (i.e. a primitive gate instance) from the internal gate library, such as a gate or flip-flop.
is_combinational
property
¶
is_combinational: bool
Whether instances of this gate are considered combinational gates.
True for combinational gates, such as AND gates or arithmetic gates.
False for flip-flops and latches.
is_sequential
property
¶
is_sequential: bool
Whether instances of this gate are considered sequential gates.
False for combinational gates, such as AND gates or arithmetic gates.
True for flip-flops and latches.
data_width
property
¶
data_width: int
The data width of this instance.
Defaults to the data width of the output port. Can be overwritten in extended classes.
In contrast to self.width, which is used for the creation and initialization,
this property is linked to the data width of the output port.
This property is useful when the data width of the output port can be changed.
model_post_init
¶
Initializes the gate's ports and connections.
This method is called after the gate's attributes have been initialized, and it sets up the gate's ports and connections.
change_mutability
¶
change_mutability(is_now_locked: bool, recursive: bool = False) -> Self
Change the mutability status of this instance and optionally its ports.
This method allows setting whether this instance can be modified or not.
If recursive is set to True, it also applies the same mutability status to all ports of this instance.
Parameters:
evaluate
¶
Evaluates the gate's output signal based on its input signals.
This method is called when the gate's input signals change, and it updates the gate's output signal accordingly.
all_connections
¶
all_connections(
include_unconnected: bool,
) -> Dict[str, Dict[int, WireSegmentPath]]
Returns a dictionary mapping port names to their corresponding connection paths.
The keys of the outer dictionary are the names of the ports. The values are dictionaries where the keys are the port bit numbers and the values are WireSegmentPath objects. The element paths are the paths of the wires connected to the corresponding bits of the port.
Parameters:
Returns:
-
Dict[str, Dict[int, WireSegmentPath]]–A dictionary mapping port names to their corresponding connection paths.
connect
¶
connect(
port_name: str,
ws_path: Optional[WireSegmentPath],
direction: Direction = UNKNOWN,
index: NonNegativeInt = 0,
width: PositiveInt = 1,
) -> None
Add connections to the specified port of this instance.
This method can establish multiple connections if a range of indices is provided.
Parameters:
-
(port_name¶str) –The name of the port where the connection(s) should be established.
-
(ws_path¶Optional[WireSegmentPath]) –The path of the wire segment that will be connected to this port. If None, the associated port (segment) remains unconnected.
-
(direction¶Direction, default:UNKNOWN) –The direction of the port. Defaults to Direction.UNKNOWN.
-
(index¶NonNegativeInt, default:0) –The starting bit index within the port for these connections. Defaults to 0.
-
(width¶PositiveInt, default:1) –The number of consecutive bits in the port that should be connected. Defaults to 1.
Raises:
-
ObjectLockedError–If this instance is currently locked, and no connection can be made.
disconnect
¶
Remove an existing connection from a specified port of this instance.
This method disconnects the wire segment at the given index within the specified port_name.
Parameters:
-
(port_name¶str) –The name of the port where the connection should be removed.
-
(index¶Optional[int], default:None) –The bit index within the port for this disconnection. Defaults to None, which completely disconnects the port.
Raises:
-
ObjectLockedError–If this instance is locked.
-
ObjectNotFoundError–If no port exists with the given name.
get_connection
¶
get_connection(
port_name: str, index: Optional[NonNegativeInt] = None
) -> Union[WireSegmentPath, Dict[int, WireSegmentPath], None]
Retrieve the connection path associated with a specified port and bit index.
If index is provided, this method returns the wire segment path connected to that specific bit within the port.
Otherwise, it returns all connections for the given port as a dictionary mapping indices to WireSegmentPaths.
Parameters:
-
(port_name¶str) –The name of the port for which to retrieve the connection(s).
-
(index¶Optional[NonNegativeInt], default:None) –The bit index within the port. Defaults to None.
Returns:
-
Union[WireSegmentPath, Dict[int, WireSegmentPath], None]–Union[WireSegmentPath, Dict[int, WireSegmentPath]]: Either a single WireSegmentPath or a dictionary mapping indices to WireSegmentPaths.
modify_connection
¶
modify_connection(
port_name: str, ws_path: WireSegmentPath, index: NonNegativeInt = 0
) -> None
Modify an existing connection within a specified port of this instance.
This method updates the wire segment path at the given index within the specified port_name.
If the index does not exist yet, it is newly added, changing the width of the port.
Parameters:
-
(port_name¶str) –The name of the port where the connection should be modified.
-
(ws_path¶WireSegmentPath) –The new wire segment path for this connection.
-
(index¶NonNegativeInt, default:0) –The bit index within the port. Defaults to 0.
connect_modify
¶
connect_modify(
port_name: str,
ws_path: WireSegmentPath,
direction: Direction = UNKNOWN,
index: NonNegativeInt = 0,
width: PositiveInt = 1,
) -> None
Add a new connection or modify an existing one within the specified port of this instance.
If the connection already exists at the given index and port_name, it will be modified.
Otherwise, a new connection is added.
Parameters:
-
(port_name¶str) –The name of the port where the connection should be established or updated.
-
(ws_path¶WireSegmentPath) –The path of the wire segment that will be connected to this port.
-
(direction¶Direction, default:UNKNOWN) –The direction of the port. Defaults to Direction.UNKNOWN.
-
(index¶NonNegativeInt, default:0) –The starting bit index within the port for these connections. Defaults to 0.
-
(width¶PositiveInt, default:1) –The number of consecutive bits in the port that should be connected. Defaults to 1.
tie_port
¶
Set a constant signal value for the specified port and bit index.
If the specified port does not exist, an error message is logged and the function returns False. Otherwise, the method tries to set the constant signal value for that port and returns True if successful.
Does not work for instance output ports, as they are always driven by their parent instances.
Parameters:
-
(name¶str) –The name of the port.
-
(index¶NonNegativeInt) –The bit index within the port.
-
(sig_value¶LogicLevel) –The constant signal value to be set ('0', '1', or 'Z').
Raises:
-
ObjectNotFoundError–If no such port or port segment exists.
-
AlreadyConnectedError–(raised by: PortSegment.tie_signal) If this segment is belongs to a load port and is already connected to a wire, from which it receives its value.
-
InvalidDirectionError–(raised by: PortSegment.tie_signal) If this port segment belongs to an instance output port, which is driven by the instance inputs and the instance's internal logic.
-
InvalidSignalError–(raised by: PortSegment.tie_signal) If an invalid value is provided.
has_tied_ports
¶
has_tied_ports() -> bool
Checks the ports of this instance whether any are tied to constant values.
Instance ports can be tied to a constant value for several reasons, but if all input or all output ports are tied, the instance is rendered useless.
Returns:
-
bool(bool) –True if any ports of this instance are tied to constant values. False otherwise, i.e. all ports are connected to wires.
has_tied_inputs
¶
has_tied_inputs() -> bool
Checks the input ports of this instance whether any are tied to constant values.
Instance ports can be tied to a constant value for several reasons, but if all input or all output ports are tied, the instance is rendered useless.
Returns:
-
bool(bool) –True if any input ports of this instance are tied to constant values. False otherwise, i.e. all input ports are connected to wires.
has_tied_outputs
¶
has_tied_outputs() -> bool
Checks the output ports of this instance whether any are tied to constant values.
Instance ports can be tied to a constant value for several reasons, but if all input or all output ports are tied, the instance is rendered useless.
Returns:
-
bool(bool) –True if any output ports of this instance are tied to constant values. False otherwise, i.e. all output ports are connected to wires.
update_signedness
¶
split
¶
Performs a bit-wise split on this instance.
If this instance supports splitting and has a data width of n bit, this method splits this instance into n 1-bit instances. This works for instances, where each output bit depends only on the corresponding input bit(s), e.g. an AND gate or a D-Flipflop.
Returns:
-
Dict[NonNegativeInt, Self]–Dict[int, Instance]: An n-bit large dictionary, where each key conforms to a bit of the original instance, and each value is an 1-bit instance representing the corresponding "slice" of the original instance.
Raises:
-
SplittingUnsupportedError–If this instance does not support splitting. Happens for any gate or instance whose behavior depends on the whole bus, and splitting would make it lose its meaning.
p2v
¶
Converts a Port object to its corresponding Verilog structure by using the connected wire segments.
This method takes the connected wire segments of a Port object and converts them to their corresponding Verilog signal structure (p2ws2v -> Port to WireSegment to Verilog signal syntax). The method requires that the currently selected module matches the module of the Port object, which is derived from the design path of the Port object. For each segment of the port, it checks whether a corresponding connected wire segment exists in the current module. If the port is set to a constant, the corresponding constant wire segment placeholder is used instead. Port segments can be excluded from the conversion by providing a list of indices, indicating which segments should be excluded from the conversion (e.g. segments that are known to be unconnected).
Parameters:
-
(port¶Port) –The Port object to convert.
-
(exclude_indices¶List[int], default:None) –A list of indices to exclude from the conversion. Defaults to an empty list.
Returns:
-
str(str) –The Verilog signal structure as a string.
Raises:
-
AttributeError–If the currently selected module does not match the module of the port.
set
¶
set(port_name: str, new_signal: SignalOrLogicLevel) -> None
BinaryGate
¶
Bases: PrimitiveGate, BaseModel
A base class for binary gates.
Binary gates are gates that have two input signals, and they produce a single output signal. This class provides a common interface for all binary gates, including methods for evaluating the gate's output and setting its output signal.
Methods:
-
model_post_init–Initializes the gate's ports and connections.
-
signals_in–The input signals of the gate.
-
signal_out–The output signal of the gate.
-
change_mutability–Change the mutability status of this instance and optionally its ports.
-
evaluate–Evaluates the gate's output signal based on its input signals.
-
all_connections–Returns a dictionary mapping port names to their corresponding connection paths.
-
connect–Add connections to the specified port of this instance.
-
disconnect–Remove an existing connection from a specified port of this instance.
-
get_connection–Retrieve the connection path associated with a specified port and bit index.
-
modify_connection–Modify an existing connection within a specified port of this instance.
-
connect_modify–Add a new connection or modify an existing one within the specified port of this instance.
-
tie_port–Set a constant signal value for the specified port and bit index.
-
has_tied_ports–Checks the ports of this instance whether any are tied to constant values.
-
has_tied_inputs–Checks the input ports of this instance whether any are tied to constant values.
-
has_tied_outputs–Checks the output ports of this instance whether any are tied to constant values.
-
update_signedness–Retrieves the signedness of the given port and updates it in this gate instance's parameter dictionary.
-
split–Performs a bit-wise split on this instance.
-
p2v–Converts a Port object to its corresponding Verilog structure by using the connected wire segments.
-
set–Sets the signal on a port.
Attributes:
-
input_ports(Tuple[Port[Instance], Port[Instance]]) –The input ports of the gate as a 2-tuple.
-
a_signed(bool) –The signedness of input port A.
-
b_signed(bool) –The signedness of input port B.
-
output_port(Port[Instance]) –The output port of the gate.
-
name(str) –The name of the element.
-
metadata(MetadataMixin) –Metadata of a netlist element.
-
path(InstancePath) –Returns the InstancePath of the netlist element.
-
type(EType) –The type of the element, which is an instance.
-
hierarchy_level(int) –The level of hierarchy of the element in the design.
-
has_parent(bool) –Whether this object has a parent.
-
circuit('Circuit') –The circuit object to which this netlist element belongs to.
-
has_circuit(bool) –Whether this netlist element has a defined circuit it belongs to.
-
locked(bool) –True if this NetlistElement instance is locked (i.e. it is currently structurally unchangeable), False if it is mutable.
-
is_placeholder_instance(bool) –A placeholder represents an element that does not have a specific path.
-
can_carry_signal(bool) –Whether this exact object is able to receive, pass or hold signal values.
-
instance_type(str) –Identifier for instances of this gate type.
-
connections(Dict[str, Dict[int, WireSegmentPath]]) –A dictionary mapping port names to their corresponding connection paths.
-
connection_str_paths(Dict[str, Dict[int, str]]) –Returns a dictionary mapping port names to their corresponding connection paths as strings.
-
ports(CustomDict[str, Port[Instance]]) –A dictionary mapping port names to their corresponding Port objects.
-
output_ports(Tuple[Port[Instance], ...]) –Returns a tuple of output ports associated with this instance.
-
has_unconnected_port_segments(bool) –Returns True if the instance has at least one unconnected port segment.
-
signals(Dict[str, Dict[int, Signal]]) –A dictionary with all signals currently present on all ports of this instance.
-
is_blackbox(bool) –Flag indicating whether the instance represents neither a primitive element nor a module instance.
-
y_width(PositiveInt) –Width of the gate, based on a certain port's width, depending on the actual gate.
-
a_width(PositiveInt) –Width of the gate's
Aport. -
b_width(PositiveInt) –Width of the gate's
Bport. -
is_combinational(bool) –Whether instances of this gate are considered combinational gates.
-
is_sequential(bool) –Whether instances of this gate are considered sequential gates.
-
data_width(int) –The data width of this instance.
input_ports
property
¶
The input ports of the gate as a 2-tuple.
name
instance-attribute
¶
name: str
The name of the element.
!!! Do not modify this variable after instantiating it. Use NetlistElement.set_name() instead. Modifying this variable might lead to inconsistencies later on.
metadata
class-attribute
instance-attribute
¶
metadata: MetadataMixin = MetadataMixin()
Metadata of a netlist element.
Can be user-defined, or e. g. by Yosys (such as src for the HDL source).
Is also grouped by categories, i.e. all metadata from Yosys can be accessed via Module.metadata["yosys"],
or via Module.metadata.yosys, which both return a dictionary of all metadata.
Read the documentation of MetaDataMixin for more information.
path
property
¶
path: InstancePath
Returns the InstancePath of the netlist element.
The InstancePath object is constructed using the element's type and its raw hierarchical path.
Returns:
-
InstancePath(InstancePath) –The hierarchical path of the netlist element.
hierarchy_level
property
¶
hierarchy_level: int
The level of hierarchy of the element in the design.
The hierarchy level is the number of separators in the element's raw path. For example, a top-level instance has a hierarchy level of 0, while a direct submodule instance has a hierarchy level of 1.
Returns:
-
int(int) –The hierarchy level of the element.
circuit
property
¶
The circuit object to which this netlist element belongs to.
- For a module, returns the circuit to which the module belongs.
- For any other netlist element, recursively returns the circuit of the parent, which ultimately leads to a module, to which the netlist element belongs.
Raises:
-
ParentNotFoundError–If a parent cannot be resolved somewhere in the hierarchical chain.
-
ObjectNotFoundError–If for a module no circuit is set.
has_circuit
property
¶
has_circuit: bool
Whether this netlist element has a defined circuit it belongs to.
Tries to access self.circuit and returns whether the call was successful.
Can be used instead of a try-except clause around the call to NetlistElement.circuit.
locked
property
¶
locked: bool
True if this NetlistElement instance is locked (i.e. it is currently structurally unchangeable), False if it is mutable.
Can be changed via NetlistElement.change_mutability(bool).
Immutability is used to prevent accidental changes to the design.
is_placeholder_instance
property
¶
is_placeholder_instance: bool
A placeholder represents an element that does not have a specific path.
True if this NetlistElement instance represents a placeholder, False otherwise.
can_carry_signal
property
¶
can_carry_signal: bool
Whether this exact object is able to receive, pass or hold signal values.
True for ports (as they drive or receive values) and wires (as they pass the signals
from driver to load ports) as well as their respective segments.
False for instances and modules.
instance_type
class-attribute
instance-attribute
¶
instance_type: str = id_internal
Identifier for instances of this gate type.
connections
property
¶
connections: Dict[str, Dict[int, WireSegmentPath]]
A dictionary mapping port names to their corresponding connection paths.
The keys of the outer dictionary are the names of the ports.
The values are dictionaries where the keys are the port bit numbers and the values are WireSegmentPath objects.
The element paths are the paths of the wires connected to the corresponding bits of the port.
Elements with no connection (i.e. the path is None) are excluded.
In case these are needed, use Instance.get_connections() instead.
Example
{ 'port1': {1: WireSegmentPath('path/to/element1'), 2: WireSegmentPath('path/to/element2')}, 'port2': {1: WireSegmentPath('path/to/element3')} }
connection_str_paths
property
¶
Returns a dictionary mapping port names to their corresponding connection paths as strings.
The keys are the names of the ports. The values are dictionaries where the keys are the port bit numbers and the values are the raw string paths.
Example
{ 'port1': {0: 'path.to.element1', 1: 'path.to.element2'}, 'port2': {0: 'path.to.element3'} }
ports
property
¶
ports: CustomDict[str, Port[Instance]]
A dictionary mapping port names to their corresponding Port objects.
The keys of the dictionary are the names of the ports. The values are Port objects representing the ports.
Returns:
-
CustomDict[str, Port[Instance]]–A dictionary mapping port names to their corresponding Port objects.
output_ports
property
¶
has_unconnected_port_segments
property
¶
has_unconnected_port_segments: bool
Returns True if the instance has at least one unconnected port segment.
signals
property
¶
A dictionary with all signals currently present on all ports of this instance.
is_blackbox
property
¶
is_blackbox: bool
Flag indicating whether the instance represents neither a primitive element nor a module instance.
If True, this instance does not have a module definition, and is also not a basic component (i.e. a primitive gate instance) from the internal gate library, such as a gate or flip-flop.
y_width
property
¶
Width of the gate, based on a certain port's width, depending on the actual gate.
is_combinational
property
¶
is_combinational: bool
Whether instances of this gate are considered combinational gates.
True for combinational gates, such as AND gates or arithmetic gates.
False for flip-flops and latches.
is_sequential
property
¶
is_sequential: bool
Whether instances of this gate are considered sequential gates.
False for combinational gates, such as AND gates or arithmetic gates.
True for flip-flops and latches.
data_width
property
¶
data_width: int
The data width of this instance.
Defaults to the data width of the output port. Can be overwritten in extended classes.
In contrast to self.width, which is used for the creation and initialization,
this property is linked to the data width of the output port.
This property is useful when the data width of the output port can be changed.
model_post_init
¶
Initializes the gate's ports and connections.
This method is called after the gate's attributes have been initialized, and it sets up the gate's ports and connections.
signals_in
¶
change_mutability
¶
change_mutability(is_now_locked: bool, recursive: bool = False) -> Self
Change the mutability status of this instance and optionally its ports.
This method allows setting whether this instance can be modified or not.
If recursive is set to True, it also applies the same mutability status to all ports of this instance.
Parameters:
evaluate
¶
Evaluates the gate's output signal based on its input signals.
This method is called when the gate's input signals change, and it updates the gate's output signal accordingly.
all_connections
¶
all_connections(
include_unconnected: bool,
) -> Dict[str, Dict[int, WireSegmentPath]]
Returns a dictionary mapping port names to their corresponding connection paths.
The keys of the outer dictionary are the names of the ports. The values are dictionaries where the keys are the port bit numbers and the values are WireSegmentPath objects. The element paths are the paths of the wires connected to the corresponding bits of the port.
Parameters:
Returns:
-
Dict[str, Dict[int, WireSegmentPath]]–A dictionary mapping port names to their corresponding connection paths.
connect
¶
connect(
port_name: str,
ws_path: Optional[WireSegmentPath],
direction: Direction = UNKNOWN,
index: NonNegativeInt = 0,
width: PositiveInt = 1,
) -> None
Add connections to the specified port of this instance.
This method can establish multiple connections if a range of indices is provided.
Parameters:
-
(port_name¶str) –The name of the port where the connection(s) should be established.
-
(ws_path¶Optional[WireSegmentPath]) –The path of the wire segment that will be connected to this port. If None, the associated port (segment) remains unconnected.
-
(direction¶Direction, default:UNKNOWN) –The direction of the port. Defaults to Direction.UNKNOWN.
-
(index¶NonNegativeInt, default:0) –The starting bit index within the port for these connections. Defaults to 0.
-
(width¶PositiveInt, default:1) –The number of consecutive bits in the port that should be connected. Defaults to 1.
Raises:
-
ObjectLockedError–If this instance is currently locked, and no connection can be made.
disconnect
¶
Remove an existing connection from a specified port of this instance.
This method disconnects the wire segment at the given index within the specified port_name.
Parameters:
-
(port_name¶str) –The name of the port where the connection should be removed.
-
(index¶Optional[int], default:None) –The bit index within the port for this disconnection. Defaults to None, which completely disconnects the port.
Raises:
-
ObjectLockedError–If this instance is locked.
-
ObjectNotFoundError–If no port exists with the given name.
get_connection
¶
get_connection(
port_name: str, index: Optional[NonNegativeInt] = None
) -> Union[WireSegmentPath, Dict[int, WireSegmentPath], None]
Retrieve the connection path associated with a specified port and bit index.
If index is provided, this method returns the wire segment path connected to that specific bit within the port.
Otherwise, it returns all connections for the given port as a dictionary mapping indices to WireSegmentPaths.
Parameters:
-
(port_name¶str) –The name of the port for which to retrieve the connection(s).
-
(index¶Optional[NonNegativeInt], default:None) –The bit index within the port. Defaults to None.
Returns:
-
Union[WireSegmentPath, Dict[int, WireSegmentPath], None]–Union[WireSegmentPath, Dict[int, WireSegmentPath]]: Either a single WireSegmentPath or a dictionary mapping indices to WireSegmentPaths.
modify_connection
¶
modify_connection(
port_name: str, ws_path: WireSegmentPath, index: NonNegativeInt = 0
) -> None
Modify an existing connection within a specified port of this instance.
This method updates the wire segment path at the given index within the specified port_name.
If the index does not exist yet, it is newly added, changing the width of the port.
Parameters:
-
(port_name¶str) –The name of the port where the connection should be modified.
-
(ws_path¶WireSegmentPath) –The new wire segment path for this connection.
-
(index¶NonNegativeInt, default:0) –The bit index within the port. Defaults to 0.
connect_modify
¶
connect_modify(
port_name: str,
ws_path: WireSegmentPath,
direction: Direction = UNKNOWN,
index: NonNegativeInt = 0,
width: PositiveInt = 1,
) -> None
Add a new connection or modify an existing one within the specified port of this instance.
If the connection already exists at the given index and port_name, it will be modified.
Otherwise, a new connection is added.
Parameters:
-
(port_name¶str) –The name of the port where the connection should be established or updated.
-
(ws_path¶WireSegmentPath) –The path of the wire segment that will be connected to this port.
-
(direction¶Direction, default:UNKNOWN) –The direction of the port. Defaults to Direction.UNKNOWN.
-
(index¶NonNegativeInt, default:0) –The starting bit index within the port for these connections. Defaults to 0.
-
(width¶PositiveInt, default:1) –The number of consecutive bits in the port that should be connected. Defaults to 1.
tie_port
¶
Set a constant signal value for the specified port and bit index.
If the specified port does not exist, an error message is logged and the function returns False. Otherwise, the method tries to set the constant signal value for that port and returns True if successful.
Does not work for instance output ports, as they are always driven by their parent instances.
Parameters:
-
(name¶str) –The name of the port.
-
(index¶NonNegativeInt) –The bit index within the port.
-
(sig_value¶LogicLevel) –The constant signal value to be set ('0', '1', or 'Z').
Raises:
-
ObjectNotFoundError–If no such port or port segment exists.
-
AlreadyConnectedError–(raised by: PortSegment.tie_signal) If this segment is belongs to a load port and is already connected to a wire, from which it receives its value.
-
InvalidDirectionError–(raised by: PortSegment.tie_signal) If this port segment belongs to an instance output port, which is driven by the instance inputs and the instance's internal logic.
-
InvalidSignalError–(raised by: PortSegment.tie_signal) If an invalid value is provided.
has_tied_ports
¶
has_tied_ports() -> bool
Checks the ports of this instance whether any are tied to constant values.
Instance ports can be tied to a constant value for several reasons, but if all input or all output ports are tied, the instance is rendered useless.
Returns:
-
bool(bool) –True if any ports of this instance are tied to constant values. False otherwise, i.e. all ports are connected to wires.
has_tied_inputs
¶
has_tied_inputs() -> bool
Checks the input ports of this instance whether any are tied to constant values.
Instance ports can be tied to a constant value for several reasons, but if all input or all output ports are tied, the instance is rendered useless.
Returns:
-
bool(bool) –True if any input ports of this instance are tied to constant values. False otherwise, i.e. all input ports are connected to wires.
has_tied_outputs
¶
has_tied_outputs() -> bool
Checks the output ports of this instance whether any are tied to constant values.
Instance ports can be tied to a constant value for several reasons, but if all input or all output ports are tied, the instance is rendered useless.
Returns:
-
bool(bool) –True if any output ports of this instance are tied to constant values. False otherwise, i.e. all output ports are connected to wires.
update_signedness
¶
split
¶
Performs a bit-wise split on this instance.
If this instance supports splitting and has a data width of n bit, this method splits this instance into n 1-bit instances. This works for instances, where each output bit depends only on the corresponding input bit(s), e.g. an AND gate or a D-Flipflop.
Returns:
-
Dict[NonNegativeInt, Self]–Dict[int, Instance]: An n-bit large dictionary, where each key conforms to a bit of the original instance, and each value is an 1-bit instance representing the corresponding "slice" of the original instance.
Raises:
-
SplittingUnsupportedError–If this instance does not support splitting. Happens for any gate or instance whose behavior depends on the whole bus, and splitting would make it lose its meaning.
p2v
¶
Converts a Port object to its corresponding Verilog structure by using the connected wire segments.
This method takes the connected wire segments of a Port object and converts them to their corresponding Verilog signal structure (p2ws2v -> Port to WireSegment to Verilog signal syntax). The method requires that the currently selected module matches the module of the Port object, which is derived from the design path of the Port object. For each segment of the port, it checks whether a corresponding connected wire segment exists in the current module. If the port is set to a constant, the corresponding constant wire segment placeholder is used instead. Port segments can be excluded from the conversion by providing a list of indices, indicating which segments should be excluded from the conversion (e.g. segments that are known to be unconnected).
Parameters:
-
(port¶Port) –The Port object to convert.
-
(exclude_indices¶List[int], default:None) –A list of indices to exclude from the conversion. Defaults to an empty list.
Returns:
-
str(str) –The Verilog signal structure as a string.
Raises:
-
AttributeError–If the currently selected module does not match the module of the port.
ArithmeticGate
¶
Bases: BinaryGate, BaseModel
A base class for arithmetic gates.
Arithmetic gates are gates that have two input signals representing numeric values, and they produce a single output signal. This class provides a common interface for all arithmetic gates, including methods for evaluating the gate's output and setting its output signal.
Methods:
-
evaluate–Evaluates the gate's output signal based on its input signals.
-
change_mutability–Change the mutability status of this instance and optionally its ports.
-
all_connections–Returns a dictionary mapping port names to their corresponding connection paths.
-
connect–Add connections to the specified port of this instance.
-
disconnect–Remove an existing connection from a specified port of this instance.
-
get_connection–Retrieve the connection path associated with a specified port and bit index.
-
modify_connection–Modify an existing connection within a specified port of this instance.
-
connect_modify–Add a new connection or modify an existing one within the specified port of this instance.
-
tie_port–Set a constant signal value for the specified port and bit index.
-
has_tied_ports–Checks the ports of this instance whether any are tied to constant values.
-
has_tied_inputs–Checks the input ports of this instance whether any are tied to constant values.
-
has_tied_outputs–Checks the output ports of this instance whether any are tied to constant values.
-
update_signedness–Retrieves the signedness of the given port and updates it in this gate instance's parameter dictionary.
-
split–Performs a bit-wise split on this instance.
-
p2v–Converts a Port object to its corresponding Verilog structure by using the connected wire segments.
-
set–Sets the signal on a port.
-
model_post_init–Initializes the gate's ports and connections.
-
signals_in–The input signals of the gate.
-
signal_out–The output signal of the gate.
Attributes:
-
input_ports(Tuple[Port[Instance], Port[Instance]]) –The input ports of the gate.
-
output_port(Port[Instance]) –The output port of the gate.
-
a_signed(bool) –The signedness of input port A.
-
b_signed(bool) –The signedness of input port B.
-
name(str) –The name of the element.
-
metadata(MetadataMixin) –Metadata of a netlist element.
-
path(InstancePath) –Returns the InstancePath of the netlist element.
-
type(EType) –The type of the element, which is an instance.
-
hierarchy_level(int) –The level of hierarchy of the element in the design.
-
has_parent(bool) –Whether this object has a parent.
-
circuit('Circuit') –The circuit object to which this netlist element belongs to.
-
has_circuit(bool) –Whether this netlist element has a defined circuit it belongs to.
-
locked(bool) –True if this NetlistElement instance is locked (i.e. it is currently structurally unchangeable), False if it is mutable.
-
is_placeholder_instance(bool) –A placeholder represents an element that does not have a specific path.
-
can_carry_signal(bool) –Whether this exact object is able to receive, pass or hold signal values.
-
instance_type(str) –Identifier for instances of this gate type.
-
connections(Dict[str, Dict[int, WireSegmentPath]]) –A dictionary mapping port names to their corresponding connection paths.
-
connection_str_paths(Dict[str, Dict[int, str]]) –Returns a dictionary mapping port names to their corresponding connection paths as strings.
-
ports(CustomDict[str, Port[Instance]]) –A dictionary mapping port names to their corresponding Port objects.
-
output_ports(Tuple[Port[Instance], ...]) –Returns a tuple of output ports associated with this instance.
-
has_unconnected_port_segments(bool) –Returns True if the instance has at least one unconnected port segment.
-
signals(Dict[str, Dict[int, Signal]]) –A dictionary with all signals currently present on all ports of this instance.
-
is_blackbox(bool) –Flag indicating whether the instance represents neither a primitive element nor a module instance.
-
y_width(PositiveInt) –Width of the gate, based on a certain port's width, depending on the actual gate.
-
a_width(PositiveInt) –Width of the gate's
Aport. -
b_width(PositiveInt) –Width of the gate's
Bport. -
is_combinational(bool) –Whether instances of this gate are considered combinational gates.
-
is_sequential(bool) –Whether instances of this gate are considered sequential gates.
-
data_width(int) –The data width of this instance.
input_ports
property
¶
output_port
property
¶
name
instance-attribute
¶
name: str
The name of the element.
!!! Do not modify this variable after instantiating it. Use NetlistElement.set_name() instead. Modifying this variable might lead to inconsistencies later on.
metadata
class-attribute
instance-attribute
¶
metadata: MetadataMixin = MetadataMixin()
Metadata of a netlist element.
Can be user-defined, or e. g. by Yosys (such as src for the HDL source).
Is also grouped by categories, i.e. all metadata from Yosys can be accessed via Module.metadata["yosys"],
or via Module.metadata.yosys, which both return a dictionary of all metadata.
Read the documentation of MetaDataMixin for more information.
path
property
¶
path: InstancePath
Returns the InstancePath of the netlist element.
The InstancePath object is constructed using the element's type and its raw hierarchical path.
Returns:
-
InstancePath(InstancePath) –The hierarchical path of the netlist element.
hierarchy_level
property
¶
hierarchy_level: int
The level of hierarchy of the element in the design.
The hierarchy level is the number of separators in the element's raw path. For example, a top-level instance has a hierarchy level of 0, while a direct submodule instance has a hierarchy level of 1.
Returns:
-
int(int) –The hierarchy level of the element.
circuit
property
¶
The circuit object to which this netlist element belongs to.
- For a module, returns the circuit to which the module belongs.
- For any other netlist element, recursively returns the circuit of the parent, which ultimately leads to a module, to which the netlist element belongs.
Raises:
-
ParentNotFoundError–If a parent cannot be resolved somewhere in the hierarchical chain.
-
ObjectNotFoundError–If for a module no circuit is set.
has_circuit
property
¶
has_circuit: bool
Whether this netlist element has a defined circuit it belongs to.
Tries to access self.circuit and returns whether the call was successful.
Can be used instead of a try-except clause around the call to NetlistElement.circuit.
locked
property
¶
locked: bool
True if this NetlistElement instance is locked (i.e. it is currently structurally unchangeable), False if it is mutable.
Can be changed via NetlistElement.change_mutability(bool).
Immutability is used to prevent accidental changes to the design.
is_placeholder_instance
property
¶
is_placeholder_instance: bool
A placeholder represents an element that does not have a specific path.
True if this NetlistElement instance represents a placeholder, False otherwise.
can_carry_signal
property
¶
can_carry_signal: bool
Whether this exact object is able to receive, pass or hold signal values.
True for ports (as they drive or receive values) and wires (as they pass the signals
from driver to load ports) as well as their respective segments.
False for instances and modules.
instance_type
class-attribute
instance-attribute
¶
instance_type: str = id_internal
Identifier for instances of this gate type.
connections
property
¶
connections: Dict[str, Dict[int, WireSegmentPath]]
A dictionary mapping port names to their corresponding connection paths.
The keys of the outer dictionary are the names of the ports.
The values are dictionaries where the keys are the port bit numbers and the values are WireSegmentPath objects.
The element paths are the paths of the wires connected to the corresponding bits of the port.
Elements with no connection (i.e. the path is None) are excluded.
In case these are needed, use Instance.get_connections() instead.
Example
{ 'port1': {1: WireSegmentPath('path/to/element1'), 2: WireSegmentPath('path/to/element2')}, 'port2': {1: WireSegmentPath('path/to/element3')} }
connection_str_paths
property
¶
Returns a dictionary mapping port names to their corresponding connection paths as strings.
The keys are the names of the ports. The values are dictionaries where the keys are the port bit numbers and the values are the raw string paths.
Example
{ 'port1': {0: 'path.to.element1', 1: 'path.to.element2'}, 'port2': {0: 'path.to.element3'} }
ports
property
¶
ports: CustomDict[str, Port[Instance]]
A dictionary mapping port names to their corresponding Port objects.
The keys of the dictionary are the names of the ports. The values are Port objects representing the ports.
Returns:
-
CustomDict[str, Port[Instance]]–A dictionary mapping port names to their corresponding Port objects.
output_ports
property
¶
has_unconnected_port_segments
property
¶
has_unconnected_port_segments: bool
Returns True if the instance has at least one unconnected port segment.
signals
property
¶
A dictionary with all signals currently present on all ports of this instance.
is_blackbox
property
¶
is_blackbox: bool
Flag indicating whether the instance represents neither a primitive element nor a module instance.
If True, this instance does not have a module definition, and is also not a basic component (i.e. a primitive gate instance) from the internal gate library, such as a gate or flip-flop.
y_width
property
¶
Width of the gate, based on a certain port's width, depending on the actual gate.
is_combinational
property
¶
is_combinational: bool
Whether instances of this gate are considered combinational gates.
True for combinational gates, such as AND gates or arithmetic gates.
False for flip-flops and latches.
is_sequential
property
¶
is_sequential: bool
Whether instances of this gate are considered sequential gates.
False for combinational gates, such as AND gates or arithmetic gates.
True for flip-flops and latches.
data_width
property
¶
data_width: int
The data width of this instance.
Defaults to the data width of the output port. Can be overwritten in extended classes.
In contrast to self.width, which is used for the creation and initialization,
this property is linked to the data width of the output port.
This property is useful when the data width of the output port can be changed.
evaluate
¶
Evaluates the gate's output signal based on its input signals.
This method is called when the gate's input signals change, and it updates the gate's output signal accordingly.
change_mutability
¶
change_mutability(is_now_locked: bool, recursive: bool = False) -> Self
Change the mutability status of this instance and optionally its ports.
This method allows setting whether this instance can be modified or not.
If recursive is set to True, it also applies the same mutability status to all ports of this instance.
Parameters:
all_connections
¶
all_connections(
include_unconnected: bool,
) -> Dict[str, Dict[int, WireSegmentPath]]
Returns a dictionary mapping port names to their corresponding connection paths.
The keys of the outer dictionary are the names of the ports. The values are dictionaries where the keys are the port bit numbers and the values are WireSegmentPath objects. The element paths are the paths of the wires connected to the corresponding bits of the port.
Parameters:
Returns:
-
Dict[str, Dict[int, WireSegmentPath]]–A dictionary mapping port names to their corresponding connection paths.
connect
¶
connect(
port_name: str,
ws_path: Optional[WireSegmentPath],
direction: Direction = UNKNOWN,
index: NonNegativeInt = 0,
width: PositiveInt = 1,
) -> None
Add connections to the specified port of this instance.
This method can establish multiple connections if a range of indices is provided.
Parameters:
-
(port_name¶str) –The name of the port where the connection(s) should be established.
-
(ws_path¶Optional[WireSegmentPath]) –The path of the wire segment that will be connected to this port. If None, the associated port (segment) remains unconnected.
-
(direction¶Direction, default:UNKNOWN) –The direction of the port. Defaults to Direction.UNKNOWN.
-
(index¶NonNegativeInt, default:0) –The starting bit index within the port for these connections. Defaults to 0.
-
(width¶PositiveInt, default:1) –The number of consecutive bits in the port that should be connected. Defaults to 1.
Raises:
-
ObjectLockedError–If this instance is currently locked, and no connection can be made.
disconnect
¶
Remove an existing connection from a specified port of this instance.
This method disconnects the wire segment at the given index within the specified port_name.
Parameters:
-
(port_name¶str) –The name of the port where the connection should be removed.
-
(index¶Optional[int], default:None) –The bit index within the port for this disconnection. Defaults to None, which completely disconnects the port.
Raises:
-
ObjectLockedError–If this instance is locked.
-
ObjectNotFoundError–If no port exists with the given name.
get_connection
¶
get_connection(
port_name: str, index: Optional[NonNegativeInt] = None
) -> Union[WireSegmentPath, Dict[int, WireSegmentPath], None]
Retrieve the connection path associated with a specified port and bit index.
If index is provided, this method returns the wire segment path connected to that specific bit within the port.
Otherwise, it returns all connections for the given port as a dictionary mapping indices to WireSegmentPaths.
Parameters:
-
(port_name¶str) –The name of the port for which to retrieve the connection(s).
-
(index¶Optional[NonNegativeInt], default:None) –The bit index within the port. Defaults to None.
Returns:
-
Union[WireSegmentPath, Dict[int, WireSegmentPath], None]–Union[WireSegmentPath, Dict[int, WireSegmentPath]]: Either a single WireSegmentPath or a dictionary mapping indices to WireSegmentPaths.
modify_connection
¶
modify_connection(
port_name: str, ws_path: WireSegmentPath, index: NonNegativeInt = 0
) -> None
Modify an existing connection within a specified port of this instance.
This method updates the wire segment path at the given index within the specified port_name.
If the index does not exist yet, it is newly added, changing the width of the port.
Parameters:
-
(port_name¶str) –The name of the port where the connection should be modified.
-
(ws_path¶WireSegmentPath) –The new wire segment path for this connection.
-
(index¶NonNegativeInt, default:0) –The bit index within the port. Defaults to 0.
connect_modify
¶
connect_modify(
port_name: str,
ws_path: WireSegmentPath,
direction: Direction = UNKNOWN,
index: NonNegativeInt = 0,
width: PositiveInt = 1,
) -> None
Add a new connection or modify an existing one within the specified port of this instance.
If the connection already exists at the given index and port_name, it will be modified.
Otherwise, a new connection is added.
Parameters:
-
(port_name¶str) –The name of the port where the connection should be established or updated.
-
(ws_path¶WireSegmentPath) –The path of the wire segment that will be connected to this port.
-
(direction¶Direction, default:UNKNOWN) –The direction of the port. Defaults to Direction.UNKNOWN.
-
(index¶NonNegativeInt, default:0) –The starting bit index within the port for these connections. Defaults to 0.
-
(width¶PositiveInt, default:1) –The number of consecutive bits in the port that should be connected. Defaults to 1.
tie_port
¶
Set a constant signal value for the specified port and bit index.
If the specified port does not exist, an error message is logged and the function returns False. Otherwise, the method tries to set the constant signal value for that port and returns True if successful.
Does not work for instance output ports, as they are always driven by their parent instances.
Parameters:
-
(name¶str) –The name of the port.
-
(index¶NonNegativeInt) –The bit index within the port.
-
(sig_value¶LogicLevel) –The constant signal value to be set ('0', '1', or 'Z').
Raises:
-
ObjectNotFoundError–If no such port or port segment exists.
-
AlreadyConnectedError–(raised by: PortSegment.tie_signal) If this segment is belongs to a load port and is already connected to a wire, from which it receives its value.
-
InvalidDirectionError–(raised by: PortSegment.tie_signal) If this port segment belongs to an instance output port, which is driven by the instance inputs and the instance's internal logic.
-
InvalidSignalError–(raised by: PortSegment.tie_signal) If an invalid value is provided.
has_tied_ports
¶
has_tied_ports() -> bool
Checks the ports of this instance whether any are tied to constant values.
Instance ports can be tied to a constant value for several reasons, but if all input or all output ports are tied, the instance is rendered useless.
Returns:
-
bool(bool) –True if any ports of this instance are tied to constant values. False otherwise, i.e. all ports are connected to wires.
has_tied_inputs
¶
has_tied_inputs() -> bool
Checks the input ports of this instance whether any are tied to constant values.
Instance ports can be tied to a constant value for several reasons, but if all input or all output ports are tied, the instance is rendered useless.
Returns:
-
bool(bool) –True if any input ports of this instance are tied to constant values. False otherwise, i.e. all input ports are connected to wires.
has_tied_outputs
¶
has_tied_outputs() -> bool
Checks the output ports of this instance whether any are tied to constant values.
Instance ports can be tied to a constant value for several reasons, but if all input or all output ports are tied, the instance is rendered useless.
Returns:
-
bool(bool) –True if any output ports of this instance are tied to constant values. False otherwise, i.e. all output ports are connected to wires.
update_signedness
¶
split
¶
Performs a bit-wise split on this instance.
If this instance supports splitting and has a data width of n bit, this method splits this instance into n 1-bit instances. This works for instances, where each output bit depends only on the corresponding input bit(s), e.g. an AND gate or a D-Flipflop.
Returns:
-
Dict[NonNegativeInt, Self]–Dict[int, Instance]: An n-bit large dictionary, where each key conforms to a bit of the original instance, and each value is an 1-bit instance representing the corresponding "slice" of the original instance.
Raises:
-
SplittingUnsupportedError–If this instance does not support splitting. Happens for any gate or instance whose behavior depends on the whole bus, and splitting would make it lose its meaning.
p2v
¶
Converts a Port object to its corresponding Verilog structure by using the connected wire segments.
This method takes the connected wire segments of a Port object and converts them to their corresponding Verilog signal structure (p2ws2v -> Port to WireSegment to Verilog signal syntax). The method requires that the currently selected module matches the module of the Port object, which is derived from the design path of the Port object. For each segment of the port, it checks whether a corresponding connected wire segment exists in the current module. If the port is set to a constant, the corresponding constant wire segment placeholder is used instead. Port segments can be excluded from the conversion by providing a list of indices, indicating which segments should be excluded from the conversion (e.g. segments that are known to be unconnected).
Parameters:
-
(port¶Port) –The Port object to convert.
-
(exclude_indices¶List[int], default:None) –A list of indices to exclude from the conversion. Defaults to an empty list.
Returns:
-
str(str) –The Verilog signal structure as a string.
Raises:
-
AttributeError–If the currently selected module does not match the module of the port.
set
¶
set(port_name: str, new_signal: SignalOrLogicLevel) -> None
model_post_init
¶
Initializes the gate's ports and connections.
This method is called after the gate's attributes have been initialized, and it sets up the gate's ports and connections.
signals_in
¶
BinaryNto1Gate
¶
Bases: BinaryGate, BaseModel
Methods:
-
model_post_init–Initializes the gate's ports and connections.
-
change_mutability–Change the mutability status of this instance and optionally its ports.
-
all_connections–Returns a dictionary mapping port names to their corresponding connection paths.
-
connect–Add connections to the specified port of this instance.
-
disconnect–Remove an existing connection from a specified port of this instance.
-
get_connection–Retrieve the connection path associated with a specified port and bit index.
-
modify_connection–Modify an existing connection within a specified port of this instance.
-
connect_modify–Add a new connection or modify an existing one within the specified port of this instance.
-
tie_port–Set a constant signal value for the specified port and bit index.
-
has_tied_ports–Checks the ports of this instance whether any are tied to constant values.
-
has_tied_inputs–Checks the input ports of this instance whether any are tied to constant values.
-
has_tied_outputs–Checks the output ports of this instance whether any are tied to constant values.
-
update_signedness–Retrieves the signedness of the given port and updates it in this gate instance's parameter dictionary.
-
split–Performs a bit-wise split on this instance.
-
p2v–Converts a Port object to its corresponding Verilog structure by using the connected wire segments.
-
set–Sets the signal on a port.
-
signals_in–The input signals of the gate.
-
signal_out–The output signal of the gate.
Attributes:
-
y_width(PositiveInt) –Width of the gate, based on a certain port's width, depending on the actual gate.
-
name(str) –The name of the element.
-
metadata(MetadataMixin) –Metadata of a netlist element.
-
path(InstancePath) –Returns the InstancePath of the netlist element.
-
type(EType) –The type of the element, which is an instance.
-
hierarchy_level(int) –The level of hierarchy of the element in the design.
-
has_parent(bool) –Whether this object has a parent.
-
circuit('Circuit') –The circuit object to which this netlist element belongs to.
-
has_circuit(bool) –Whether this netlist element has a defined circuit it belongs to.
-
locked(bool) –True if this NetlistElement instance is locked (i.e. it is currently structurally unchangeable), False if it is mutable.
-
is_placeholder_instance(bool) –A placeholder represents an element that does not have a specific path.
-
can_carry_signal(bool) –Whether this exact object is able to receive, pass or hold signal values.
-
instance_type(str) –Identifier for instances of this gate type.
-
connections(Dict[str, Dict[int, WireSegmentPath]]) –A dictionary mapping port names to their corresponding connection paths.
-
connection_str_paths(Dict[str, Dict[int, str]]) –Returns a dictionary mapping port names to their corresponding connection paths as strings.
-
ports(CustomDict[str, Port[Instance]]) –A dictionary mapping port names to their corresponding Port objects.
-
input_ports(Tuple[Port[Instance], Port[Instance]]) –The input ports of the gate as a 2-tuple.
-
output_ports(Tuple[Port[Instance], ...]) –Returns a tuple of output ports associated with this instance.
-
has_unconnected_port_segments(bool) –Returns True if the instance has at least one unconnected port segment.
-
signals(Dict[str, Dict[int, Signal]]) –A dictionary with all signals currently present on all ports of this instance.
-
is_blackbox(bool) –Flag indicating whether the instance represents neither a primitive element nor a module instance.
-
a_width(PositiveInt) –Width of the gate's
Aport. -
b_width(PositiveInt) –Width of the gate's
Bport. -
is_combinational(bool) –Whether instances of this gate are considered combinational gates.
-
is_sequential(bool) –Whether instances of this gate are considered sequential gates.
-
output_port(Port[Instance]) –The output port of the gate.
-
data_width(int) –The data width of this instance.
-
a_signed(bool) –The signedness of input port A.
-
b_signed(bool) –The signedness of input port B.
y_width
property
¶
Width of the gate, based on a certain port's width, depending on the actual gate.
name
instance-attribute
¶
name: str
The name of the element.
!!! Do not modify this variable after instantiating it. Use NetlistElement.set_name() instead. Modifying this variable might lead to inconsistencies later on.
metadata
class-attribute
instance-attribute
¶
metadata: MetadataMixin = MetadataMixin()
Metadata of a netlist element.
Can be user-defined, or e. g. by Yosys (such as src for the HDL source).
Is also grouped by categories, i.e. all metadata from Yosys can be accessed via Module.metadata["yosys"],
or via Module.metadata.yosys, which both return a dictionary of all metadata.
Read the documentation of MetaDataMixin for more information.
path
property
¶
path: InstancePath
Returns the InstancePath of the netlist element.
The InstancePath object is constructed using the element's type and its raw hierarchical path.
Returns:
-
InstancePath(InstancePath) –The hierarchical path of the netlist element.
hierarchy_level
property
¶
hierarchy_level: int
The level of hierarchy of the element in the design.
The hierarchy level is the number of separators in the element's raw path. For example, a top-level instance has a hierarchy level of 0, while a direct submodule instance has a hierarchy level of 1.
Returns:
-
int(int) –The hierarchy level of the element.
circuit
property
¶
The circuit object to which this netlist element belongs to.
- For a module, returns the circuit to which the module belongs.
- For any other netlist element, recursively returns the circuit of the parent, which ultimately leads to a module, to which the netlist element belongs.
Raises:
-
ParentNotFoundError–If a parent cannot be resolved somewhere in the hierarchical chain.
-
ObjectNotFoundError–If for a module no circuit is set.
has_circuit
property
¶
has_circuit: bool
Whether this netlist element has a defined circuit it belongs to.
Tries to access self.circuit and returns whether the call was successful.
Can be used instead of a try-except clause around the call to NetlistElement.circuit.
locked
property
¶
locked: bool
True if this NetlistElement instance is locked (i.e. it is currently structurally unchangeable), False if it is mutable.
Can be changed via NetlistElement.change_mutability(bool).
Immutability is used to prevent accidental changes to the design.
is_placeholder_instance
property
¶
is_placeholder_instance: bool
A placeholder represents an element that does not have a specific path.
True if this NetlistElement instance represents a placeholder, False otherwise.
can_carry_signal
property
¶
can_carry_signal: bool
Whether this exact object is able to receive, pass or hold signal values.
True for ports (as they drive or receive values) and wires (as they pass the signals
from driver to load ports) as well as their respective segments.
False for instances and modules.
instance_type
class-attribute
instance-attribute
¶
instance_type: str = id_internal
Identifier for instances of this gate type.
connections
property
¶
connections: Dict[str, Dict[int, WireSegmentPath]]
A dictionary mapping port names to their corresponding connection paths.
The keys of the outer dictionary are the names of the ports.
The values are dictionaries where the keys are the port bit numbers and the values are WireSegmentPath objects.
The element paths are the paths of the wires connected to the corresponding bits of the port.
Elements with no connection (i.e. the path is None) are excluded.
In case these are needed, use Instance.get_connections() instead.
Example
{ 'port1': {1: WireSegmentPath('path/to/element1'), 2: WireSegmentPath('path/to/element2')}, 'port2': {1: WireSegmentPath('path/to/element3')} }
connection_str_paths
property
¶
Returns a dictionary mapping port names to their corresponding connection paths as strings.
The keys are the names of the ports. The values are dictionaries where the keys are the port bit numbers and the values are the raw string paths.
Example
{ 'port1': {0: 'path.to.element1', 1: 'path.to.element2'}, 'port2': {0: 'path.to.element3'} }
ports
property
¶
ports: CustomDict[str, Port[Instance]]
A dictionary mapping port names to their corresponding Port objects.
The keys of the dictionary are the names of the ports. The values are Port objects representing the ports.
Returns:
-
CustomDict[str, Port[Instance]]–A dictionary mapping port names to their corresponding Port objects.
input_ports
property
¶
The input ports of the gate as a 2-tuple.
output_ports
property
¶
has_unconnected_port_segments
property
¶
has_unconnected_port_segments: bool
Returns True if the instance has at least one unconnected port segment.
signals
property
¶
A dictionary with all signals currently present on all ports of this instance.
is_blackbox
property
¶
is_blackbox: bool
Flag indicating whether the instance represents neither a primitive element nor a module instance.
If True, this instance does not have a module definition, and is also not a basic component (i.e. a primitive gate instance) from the internal gate library, such as a gate or flip-flop.
is_combinational
property
¶
is_combinational: bool
Whether instances of this gate are considered combinational gates.
True for combinational gates, such as AND gates or arithmetic gates.
False for flip-flops and latches.
is_sequential
property
¶
is_sequential: bool
Whether instances of this gate are considered sequential gates.
False for combinational gates, such as AND gates or arithmetic gates.
True for flip-flops and latches.
data_width
property
¶
data_width: int
The data width of this instance.
Defaults to the data width of the output port. Can be overwritten in extended classes.
In contrast to self.width, which is used for the creation and initialization,
this property is linked to the data width of the output port.
This property is useful when the data width of the output port can be changed.
model_post_init
¶
Initializes the gate's ports and connections.
This method is called after the gate's attributes have been initialized, and it sets up the gate's ports and connections.
change_mutability
¶
change_mutability(is_now_locked: bool, recursive: bool = False) -> Self
Change the mutability status of this instance and optionally its ports.
This method allows setting whether this instance can be modified or not.
If recursive is set to True, it also applies the same mutability status to all ports of this instance.
Parameters:
all_connections
¶
all_connections(
include_unconnected: bool,
) -> Dict[str, Dict[int, WireSegmentPath]]
Returns a dictionary mapping port names to their corresponding connection paths.
The keys of the outer dictionary are the names of the ports. The values are dictionaries where the keys are the port bit numbers and the values are WireSegmentPath objects. The element paths are the paths of the wires connected to the corresponding bits of the port.
Parameters:
Returns:
-
Dict[str, Dict[int, WireSegmentPath]]–A dictionary mapping port names to their corresponding connection paths.
connect
¶
connect(
port_name: str,
ws_path: Optional[WireSegmentPath],
direction: Direction = UNKNOWN,
index: NonNegativeInt = 0,
width: PositiveInt = 1,
) -> None
Add connections to the specified port of this instance.
This method can establish multiple connections if a range of indices is provided.
Parameters:
-
(port_name¶str) –The name of the port where the connection(s) should be established.
-
(ws_path¶Optional[WireSegmentPath]) –The path of the wire segment that will be connected to this port. If None, the associated port (segment) remains unconnected.
-
(direction¶Direction, default:UNKNOWN) –The direction of the port. Defaults to Direction.UNKNOWN.
-
(index¶NonNegativeInt, default:0) –The starting bit index within the port for these connections. Defaults to 0.
-
(width¶PositiveInt, default:1) –The number of consecutive bits in the port that should be connected. Defaults to 1.
Raises:
-
ObjectLockedError–If this instance is currently locked, and no connection can be made.
disconnect
¶
Remove an existing connection from a specified port of this instance.
This method disconnects the wire segment at the given index within the specified port_name.
Parameters:
-
(port_name¶str) –The name of the port where the connection should be removed.
-
(index¶Optional[int], default:None) –The bit index within the port for this disconnection. Defaults to None, which completely disconnects the port.
Raises:
-
ObjectLockedError–If this instance is locked.
-
ObjectNotFoundError–If no port exists with the given name.
get_connection
¶
get_connection(
port_name: str, index: Optional[NonNegativeInt] = None
) -> Union[WireSegmentPath, Dict[int, WireSegmentPath], None]
Retrieve the connection path associated with a specified port and bit index.
If index is provided, this method returns the wire segment path connected to that specific bit within the port.
Otherwise, it returns all connections for the given port as a dictionary mapping indices to WireSegmentPaths.
Parameters:
-
(port_name¶str) –The name of the port for which to retrieve the connection(s).
-
(index¶Optional[NonNegativeInt], default:None) –The bit index within the port. Defaults to None.
Returns:
-
Union[WireSegmentPath, Dict[int, WireSegmentPath], None]–Union[WireSegmentPath, Dict[int, WireSegmentPath]]: Either a single WireSegmentPath or a dictionary mapping indices to WireSegmentPaths.
modify_connection
¶
modify_connection(
port_name: str, ws_path: WireSegmentPath, index: NonNegativeInt = 0
) -> None
Modify an existing connection within a specified port of this instance.
This method updates the wire segment path at the given index within the specified port_name.
If the index does not exist yet, it is newly added, changing the width of the port.
Parameters:
-
(port_name¶str) –The name of the port where the connection should be modified.
-
(ws_path¶WireSegmentPath) –The new wire segment path for this connection.
-
(index¶NonNegativeInt, default:0) –The bit index within the port. Defaults to 0.
connect_modify
¶
connect_modify(
port_name: str,
ws_path: WireSegmentPath,
direction: Direction = UNKNOWN,
index: NonNegativeInt = 0,
width: PositiveInt = 1,
) -> None
Add a new connection or modify an existing one within the specified port of this instance.
If the connection already exists at the given index and port_name, it will be modified.
Otherwise, a new connection is added.
Parameters:
-
(port_name¶str) –The name of the port where the connection should be established or updated.
-
(ws_path¶WireSegmentPath) –The path of the wire segment that will be connected to this port.
-
(direction¶Direction, default:UNKNOWN) –The direction of the port. Defaults to Direction.UNKNOWN.
-
(index¶NonNegativeInt, default:0) –The starting bit index within the port for these connections. Defaults to 0.
-
(width¶PositiveInt, default:1) –The number of consecutive bits in the port that should be connected. Defaults to 1.
tie_port
¶
Set a constant signal value for the specified port and bit index.
If the specified port does not exist, an error message is logged and the function returns False. Otherwise, the method tries to set the constant signal value for that port and returns True if successful.
Does not work for instance output ports, as they are always driven by their parent instances.
Parameters:
-
(name¶str) –The name of the port.
-
(index¶NonNegativeInt) –The bit index within the port.
-
(sig_value¶LogicLevel) –The constant signal value to be set ('0', '1', or 'Z').
Raises:
-
ObjectNotFoundError–If no such port or port segment exists.
-
AlreadyConnectedError–(raised by: PortSegment.tie_signal) If this segment is belongs to a load port and is already connected to a wire, from which it receives its value.
-
InvalidDirectionError–(raised by: PortSegment.tie_signal) If this port segment belongs to an instance output port, which is driven by the instance inputs and the instance's internal logic.
-
InvalidSignalError–(raised by: PortSegment.tie_signal) If an invalid value is provided.
has_tied_ports
¶
has_tied_ports() -> bool
Checks the ports of this instance whether any are tied to constant values.
Instance ports can be tied to a constant value for several reasons, but if all input or all output ports are tied, the instance is rendered useless.
Returns:
-
bool(bool) –True if any ports of this instance are tied to constant values. False otherwise, i.e. all ports are connected to wires.
has_tied_inputs
¶
has_tied_inputs() -> bool
Checks the input ports of this instance whether any are tied to constant values.
Instance ports can be tied to a constant value for several reasons, but if all input or all output ports are tied, the instance is rendered useless.
Returns:
-
bool(bool) –True if any input ports of this instance are tied to constant values. False otherwise, i.e. all input ports are connected to wires.
has_tied_outputs
¶
has_tied_outputs() -> bool
Checks the output ports of this instance whether any are tied to constant values.
Instance ports can be tied to a constant value for several reasons, but if all input or all output ports are tied, the instance is rendered useless.
Returns:
-
bool(bool) –True if any output ports of this instance are tied to constant values. False otherwise, i.e. all output ports are connected to wires.
update_signedness
¶
split
¶
Performs a bit-wise split on this instance.
If this instance supports splitting and has a data width of n bit, this method splits this instance into n 1-bit instances. This works for instances, where each output bit depends only on the corresponding input bit(s), e.g. an AND gate or a D-Flipflop.
Returns:
-
Dict[NonNegativeInt, Self]–Dict[int, Instance]: An n-bit large dictionary, where each key conforms to a bit of the original instance, and each value is an 1-bit instance representing the corresponding "slice" of the original instance.
Raises:
-
SplittingUnsupportedError–If this instance does not support splitting. Happens for any gate or instance whose behavior depends on the whole bus, and splitting would make it lose its meaning.
p2v
¶
Converts a Port object to its corresponding Verilog structure by using the connected wire segments.
This method takes the connected wire segments of a Port object and converts them to their corresponding Verilog signal structure (p2ws2v -> Port to WireSegment to Verilog signal syntax). The method requires that the currently selected module matches the module of the Port object, which is derived from the design path of the Port object. For each segment of the port, it checks whether a corresponding connected wire segment exists in the current module. If the port is set to a constant, the corresponding constant wire segment placeholder is used instead. Port segments can be excluded from the conversion by providing a list of indices, indicating which segments should be excluded from the conversion (e.g. segments that are known to be unconnected).
Parameters:
-
(port¶Port) –The Port object to convert.
-
(exclude_indices¶List[int], default:None) –A list of indices to exclude from the conversion. Defaults to an empty list.
Returns:
-
str(str) –The Verilog signal structure as a string.
Raises:
-
AttributeError–If the currently selected module does not match the module of the port.
set
¶
set(port_name: str, new_signal: SignalOrLogicLevel) -> None
signals_in
¶
StorageGate
¶
Bases: PrimitiveGate, BaseModel
Methods:
-
model_post_init–Initializes the gate's ports and connections.
-
change_mutability–Change the mutability status of this instance and optionally its ports.
-
evaluate–Evaluates the gate's output signal based on its input signals.
-
all_connections–Returns a dictionary mapping port names to their corresponding connection paths.
-
connect–Add connections to the specified port of this instance.
-
disconnect–Remove an existing connection from a specified port of this instance.
-
get_connection–Retrieve the connection path associated with a specified port and bit index.
-
modify_connection–Modify an existing connection within a specified port of this instance.
-
connect_modify–Add a new connection or modify an existing one within the specified port of this instance.
-
tie_port–Set a constant signal value for the specified port and bit index.
-
has_tied_ports–Checks the ports of this instance whether any are tied to constant values.
-
has_tied_inputs–Checks the input ports of this instance whether any are tied to constant values.
-
has_tied_outputs–Checks the output ports of this instance whether any are tied to constant values.
-
update_signedness–Retrieves the signedness of the given port and updates it in this gate instance's parameter dictionary.
-
split–Performs a bit-wise split on this instance.
-
p2v–Converts a Port object to its corresponding Verilog structure by using the connected wire segments.
-
set–Sets the signal on a port.
Attributes:
-
input_port(Port[Instance]) –The input port of the gate.
-
output_port(Port[Instance]) –The output port of the gate.
-
name(str) –The name of the element.
-
metadata(MetadataMixin) –Metadata of a netlist element.
-
path(InstancePath) –Returns the InstancePath of the netlist element.
-
type(EType) –The type of the element, which is an instance.
-
hierarchy_level(int) –The level of hierarchy of the element in the design.
-
has_parent(bool) –Whether this object has a parent.
-
circuit('Circuit') –The circuit object to which this netlist element belongs to.
-
has_circuit(bool) –Whether this netlist element has a defined circuit it belongs to.
-
locked(bool) –True if this NetlistElement instance is locked (i.e. it is currently structurally unchangeable), False if it is mutable.
-
is_placeholder_instance(bool) –A placeholder represents an element that does not have a specific path.
-
can_carry_signal(bool) –Whether this exact object is able to receive, pass or hold signal values.
-
instance_type(str) –Identifier for instances of this gate type.
-
connections(Dict[str, Dict[int, WireSegmentPath]]) –A dictionary mapping port names to their corresponding connection paths.
-
connection_str_paths(Dict[str, Dict[int, str]]) –Returns a dictionary mapping port names to their corresponding connection paths as strings.
-
ports(CustomDict[str, Port[Instance]]) –A dictionary mapping port names to their corresponding Port objects.
-
input_ports(Tuple[Port[Instance], ...]) –Returns a tuple of input ports associated with this instance.
-
output_ports(Tuple[Port[Instance], ...]) –Returns a tuple of output ports associated with this instance.
-
has_unconnected_port_segments(bool) –Returns True if the instance has at least one unconnected port segment.
-
signals(Dict[str, Dict[int, Signal]]) –A dictionary with all signals currently present on all ports of this instance.
-
is_blackbox(bool) –Flag indicating whether the instance represents neither a primitive element nor a module instance.
-
verilog(str) –Generates the Verilog code for this instance.
-
a_width(PositiveInt) –Width of the gate's
Aport. -
b_width(PositiveInt) –Width of the gate's
Bport. -
is_sequential(bool) –Whether instances of this gate are considered sequential gates.
-
data_width(int) –The data width of this instance.
name
instance-attribute
¶
name: str
The name of the element.
!!! Do not modify this variable after instantiating it. Use NetlistElement.set_name() instead. Modifying this variable might lead to inconsistencies later on.
metadata
class-attribute
instance-attribute
¶
metadata: MetadataMixin = MetadataMixin()
Metadata of a netlist element.
Can be user-defined, or e. g. by Yosys (such as src for the HDL source).
Is also grouped by categories, i.e. all metadata from Yosys can be accessed via Module.metadata["yosys"],
or via Module.metadata.yosys, which both return a dictionary of all metadata.
Read the documentation of MetaDataMixin for more information.
path
property
¶
path: InstancePath
Returns the InstancePath of the netlist element.
The InstancePath object is constructed using the element's type and its raw hierarchical path.
Returns:
-
InstancePath(InstancePath) –The hierarchical path of the netlist element.
hierarchy_level
property
¶
hierarchy_level: int
The level of hierarchy of the element in the design.
The hierarchy level is the number of separators in the element's raw path. For example, a top-level instance has a hierarchy level of 0, while a direct submodule instance has a hierarchy level of 1.
Returns:
-
int(int) –The hierarchy level of the element.
circuit
property
¶
The circuit object to which this netlist element belongs to.
- For a module, returns the circuit to which the module belongs.
- For any other netlist element, recursively returns the circuit of the parent, which ultimately leads to a module, to which the netlist element belongs.
Raises:
-
ParentNotFoundError–If a parent cannot be resolved somewhere in the hierarchical chain.
-
ObjectNotFoundError–If for a module no circuit is set.
has_circuit
property
¶
has_circuit: bool
Whether this netlist element has a defined circuit it belongs to.
Tries to access self.circuit and returns whether the call was successful.
Can be used instead of a try-except clause around the call to NetlistElement.circuit.
locked
property
¶
locked: bool
True if this NetlistElement instance is locked (i.e. it is currently structurally unchangeable), False if it is mutable.
Can be changed via NetlistElement.change_mutability(bool).
Immutability is used to prevent accidental changes to the design.
is_placeholder_instance
property
¶
is_placeholder_instance: bool
A placeholder represents an element that does not have a specific path.
True if this NetlistElement instance represents a placeholder, False otherwise.
can_carry_signal
property
¶
can_carry_signal: bool
Whether this exact object is able to receive, pass or hold signal values.
True for ports (as they drive or receive values) and wires (as they pass the signals
from driver to load ports) as well as their respective segments.
False for instances and modules.
instance_type
class-attribute
instance-attribute
¶
instance_type: str = id_internal
Identifier for instances of this gate type.
connections
property
¶
connections: Dict[str, Dict[int, WireSegmentPath]]
A dictionary mapping port names to their corresponding connection paths.
The keys of the outer dictionary are the names of the ports.
The values are dictionaries where the keys are the port bit numbers and the values are WireSegmentPath objects.
The element paths are the paths of the wires connected to the corresponding bits of the port.
Elements with no connection (i.e. the path is None) are excluded.
In case these are needed, use Instance.get_connections() instead.
Example
{ 'port1': {1: WireSegmentPath('path/to/element1'), 2: WireSegmentPath('path/to/element2')}, 'port2': {1: WireSegmentPath('path/to/element3')} }
connection_str_paths
property
¶
Returns a dictionary mapping port names to their corresponding connection paths as strings.
The keys are the names of the ports. The values are dictionaries where the keys are the port bit numbers and the values are the raw string paths.
Example
{ 'port1': {0: 'path.to.element1', 1: 'path.to.element2'}, 'port2': {0: 'path.to.element3'} }
ports
property
¶
ports: CustomDict[str, Port[Instance]]
A dictionary mapping port names to their corresponding Port objects.
The keys of the dictionary are the names of the ports. The values are Port objects representing the ports.
Returns:
-
CustomDict[str, Port[Instance]]–A dictionary mapping port names to their corresponding Port objects.
input_ports
property
¶
output_ports
property
¶
has_unconnected_port_segments
property
¶
has_unconnected_port_segments: bool
Returns True if the instance has at least one unconnected port segment.
signals
property
¶
A dictionary with all signals currently present on all ports of this instance.
is_blackbox
property
¶
is_blackbox: bool
Flag indicating whether the instance represents neither a primitive element nor a module instance.
If True, this instance does not have a module definition, and is also not a basic component (i.e. a primitive gate instance) from the internal gate library, such as a gate or flip-flop.
verilog
property
¶
verilog: str
Generates the Verilog code for this instance.
This property uses the verilog_template to generate the actual Verilog code by replacing the placeholders with
the instance type, name, and port connections. It returns a string that can be used directly in a Verilog file.
Returns:
-
str(str) –The generated Verilog code.
is_sequential
property
¶
is_sequential: bool
Whether instances of this gate are considered sequential gates.
False for combinational gates, such as AND gates or arithmetic gates.
True for flip-flops and latches.
data_width
property
¶
data_width: int
The data width of this instance.
Defaults to the data width of the output port. Can be overwritten in extended classes.
In contrast to self.width, which is used for the creation and initialization,
this property is linked to the data width of the output port.
This property is useful when the data width of the output port can be changed.
model_post_init
¶
Initializes the gate's ports and connections.
This method is called after the gate's attributes have been initialized, and it sets up the gate's ports and connections.
change_mutability
¶
change_mutability(is_now_locked: bool, recursive: bool = False) -> Self
Change the mutability status of this instance and optionally its ports.
This method allows setting whether this instance can be modified or not.
If recursive is set to True, it also applies the same mutability status to all ports of this instance.
Parameters:
evaluate
¶
Evaluates the gate's output signal based on its input signals.
This method is called when the gate's input signals change, and it updates the gate's output signal accordingly.
all_connections
¶
all_connections(
include_unconnected: bool,
) -> Dict[str, Dict[int, WireSegmentPath]]
Returns a dictionary mapping port names to their corresponding connection paths.
The keys of the outer dictionary are the names of the ports. The values are dictionaries where the keys are the port bit numbers and the values are WireSegmentPath objects. The element paths are the paths of the wires connected to the corresponding bits of the port.
Parameters:
Returns:
-
Dict[str, Dict[int, WireSegmentPath]]–A dictionary mapping port names to their corresponding connection paths.
connect
¶
connect(
port_name: str,
ws_path: Optional[WireSegmentPath],
direction: Direction = UNKNOWN,
index: NonNegativeInt = 0,
width: PositiveInt = 1,
) -> None
Add connections to the specified port of this instance.
This method can establish multiple connections if a range of indices is provided.
Parameters:
-
(port_name¶str) –The name of the port where the connection(s) should be established.
-
(ws_path¶Optional[WireSegmentPath]) –The path of the wire segment that will be connected to this port. If None, the associated port (segment) remains unconnected.
-
(direction¶Direction, default:UNKNOWN) –The direction of the port. Defaults to Direction.UNKNOWN.
-
(index¶NonNegativeInt, default:0) –The starting bit index within the port for these connections. Defaults to 0.
-
(width¶PositiveInt, default:1) –The number of consecutive bits in the port that should be connected. Defaults to 1.
Raises:
-
ObjectLockedError–If this instance is currently locked, and no connection can be made.
disconnect
¶
Remove an existing connection from a specified port of this instance.
This method disconnects the wire segment at the given index within the specified port_name.
Parameters:
-
(port_name¶str) –The name of the port where the connection should be removed.
-
(index¶Optional[int], default:None) –The bit index within the port for this disconnection. Defaults to None, which completely disconnects the port.
Raises:
-
ObjectLockedError–If this instance is locked.
-
ObjectNotFoundError–If no port exists with the given name.
get_connection
¶
get_connection(
port_name: str, index: Optional[NonNegativeInt] = None
) -> Union[WireSegmentPath, Dict[int, WireSegmentPath], None]
Retrieve the connection path associated with a specified port and bit index.
If index is provided, this method returns the wire segment path connected to that specific bit within the port.
Otherwise, it returns all connections for the given port as a dictionary mapping indices to WireSegmentPaths.
Parameters:
-
(port_name¶str) –The name of the port for which to retrieve the connection(s).
-
(index¶Optional[NonNegativeInt], default:None) –The bit index within the port. Defaults to None.
Returns:
-
Union[WireSegmentPath, Dict[int, WireSegmentPath], None]–Union[WireSegmentPath, Dict[int, WireSegmentPath]]: Either a single WireSegmentPath or a dictionary mapping indices to WireSegmentPaths.
modify_connection
¶
modify_connection(
port_name: str, ws_path: WireSegmentPath, index: NonNegativeInt = 0
) -> None
Modify an existing connection within a specified port of this instance.
This method updates the wire segment path at the given index within the specified port_name.
If the index does not exist yet, it is newly added, changing the width of the port.
Parameters:
-
(port_name¶str) –The name of the port where the connection should be modified.
-
(ws_path¶WireSegmentPath) –The new wire segment path for this connection.
-
(index¶NonNegativeInt, default:0) –The bit index within the port. Defaults to 0.
connect_modify
¶
connect_modify(
port_name: str,
ws_path: WireSegmentPath,
direction: Direction = UNKNOWN,
index: NonNegativeInt = 0,
width: PositiveInt = 1,
) -> None
Add a new connection or modify an existing one within the specified port of this instance.
If the connection already exists at the given index and port_name, it will be modified.
Otherwise, a new connection is added.
Parameters:
-
(port_name¶str) –The name of the port where the connection should be established or updated.
-
(ws_path¶WireSegmentPath) –The path of the wire segment that will be connected to this port.
-
(direction¶Direction, default:UNKNOWN) –The direction of the port. Defaults to Direction.UNKNOWN.
-
(index¶NonNegativeInt, default:0) –The starting bit index within the port for these connections. Defaults to 0.
-
(width¶PositiveInt, default:1) –The number of consecutive bits in the port that should be connected. Defaults to 1.
tie_port
¶
Set a constant signal value for the specified port and bit index.
If the specified port does not exist, an error message is logged and the function returns False. Otherwise, the method tries to set the constant signal value for that port and returns True if successful.
Does not work for instance output ports, as they are always driven by their parent instances.
Parameters:
-
(name¶str) –The name of the port.
-
(index¶NonNegativeInt) –The bit index within the port.
-
(sig_value¶LogicLevel) –The constant signal value to be set ('0', '1', or 'Z').
Raises:
-
ObjectNotFoundError–If no such port or port segment exists.
-
AlreadyConnectedError–(raised by: PortSegment.tie_signal) If this segment is belongs to a load port and is already connected to a wire, from which it receives its value.
-
InvalidDirectionError–(raised by: PortSegment.tie_signal) If this port segment belongs to an instance output port, which is driven by the instance inputs and the instance's internal logic.
-
InvalidSignalError–(raised by: PortSegment.tie_signal) If an invalid value is provided.
has_tied_ports
¶
has_tied_ports() -> bool
Checks the ports of this instance whether any are tied to constant values.
Instance ports can be tied to a constant value for several reasons, but if all input or all output ports are tied, the instance is rendered useless.
Returns:
-
bool(bool) –True if any ports of this instance are tied to constant values. False otherwise, i.e. all ports are connected to wires.
has_tied_inputs
¶
has_tied_inputs() -> bool
Checks the input ports of this instance whether any are tied to constant values.
Instance ports can be tied to a constant value for several reasons, but if all input or all output ports are tied, the instance is rendered useless.
Returns:
-
bool(bool) –True if any input ports of this instance are tied to constant values. False otherwise, i.e. all input ports are connected to wires.
has_tied_outputs
¶
has_tied_outputs() -> bool
Checks the output ports of this instance whether any are tied to constant values.
Instance ports can be tied to a constant value for several reasons, but if all input or all output ports are tied, the instance is rendered useless.
Returns:
-
bool(bool) –True if any output ports of this instance are tied to constant values. False otherwise, i.e. all output ports are connected to wires.
update_signedness
¶
split
¶
Performs a bit-wise split on this instance.
If this instance supports splitting and has a data width of n bit, this method splits this instance into n 1-bit instances. This works for instances, where each output bit depends only on the corresponding input bit(s), e.g. an AND gate or a D-Flipflop.
Returns:
-
Dict[NonNegativeInt, Self]–Dict[int, Instance]: An n-bit large dictionary, where each key conforms to a bit of the original instance, and each value is an 1-bit instance representing the corresponding "slice" of the original instance.
Raises:
-
SplittingUnsupportedError–If this instance does not support splitting. Happens for any gate or instance whose behavior depends on the whole bus, and splitting would make it lose its meaning.
p2v
¶
Converts a Port object to its corresponding Verilog structure by using the connected wire segments.
This method takes the connected wire segments of a Port object and converts them to their corresponding Verilog signal structure (p2ws2v -> Port to WireSegment to Verilog signal syntax). The method requires that the currently selected module matches the module of the Port object, which is derived from the design path of the Port object. For each segment of the port, it checks whether a corresponding connected wire segment exists in the current module. If the port is set to a constant, the corresponding constant wire segment placeholder is used instead. Port segments can be excluded from the conversion by providing a list of indices, indicating which segments should be excluded from the conversion (e.g. segments that are known to be unconnected).
Parameters:
-
(port¶Port) –The Port object to convert.
-
(exclude_indices¶List[int], default:None) –A list of indices to exclude from the conversion. Defaults to an empty list.
Returns:
-
str(str) –The Verilog signal structure as a string.
Raises:
-
AttributeError–If the currently selected module does not match the module of the port.
ClkMixin
¶
Bases: StorageGate
A mixin class for clocked gates. Clocked gates are gates that have a clock signal. This class provides a common interface for all clocked gates, including methods for evaluating the gate's output and setting its output signal.
Methods:
-
model_post_init–Initializes the gate's ports and connections.
-
set_clk–Sets the clock signal.
-
change_mutability–Change the mutability status of this instance and optionally its ports.
-
evaluate–Evaluates the gate's output signal based on its input signals.
-
all_connections–Returns a dictionary mapping port names to their corresponding connection paths.
-
connect–Add connections to the specified port of this instance.
-
disconnect–Remove an existing connection from a specified port of this instance.
-
get_connection–Retrieve the connection path associated with a specified port and bit index.
-
modify_connection–Modify an existing connection within a specified port of this instance.
-
connect_modify–Add a new connection or modify an existing one within the specified port of this instance.
-
tie_port–Set a constant signal value for the specified port and bit index.
-
has_tied_ports–Checks the ports of this instance whether any are tied to constant values.
-
has_tied_inputs–Checks the input ports of this instance whether any are tied to constant values.
-
has_tied_outputs–Checks the output ports of this instance whether any are tied to constant values.
-
update_signedness–Retrieves the signedness of the given port and updates it in this gate instance's parameter dictionary.
-
split–Performs a bit-wise split on this instance.
-
p2v–Converts a Port object to its corresponding Verilog structure by using the connected wire segments.
-
set–Sets the signal on a port.
Attributes:
-
clk_polarity(Signal) –Which clock edge activates the flip-flop. Default is Signal.HIGH, i.e. rising edge.
-
clk_port(Port[Instance]) –The clock port of the gate.
-
name(str) –The name of the element.
-
metadata(MetadataMixin) –Metadata of a netlist element.
-
path(InstancePath) –Returns the InstancePath of the netlist element.
-
type(EType) –The type of the element, which is an instance.
-
hierarchy_level(int) –The level of hierarchy of the element in the design.
-
has_parent(bool) –Whether this object has a parent.
-
circuit('Circuit') –The circuit object to which this netlist element belongs to.
-
has_circuit(bool) –Whether this netlist element has a defined circuit it belongs to.
-
locked(bool) –True if this NetlistElement instance is locked (i.e. it is currently structurally unchangeable), False if it is mutable.
-
is_placeholder_instance(bool) –A placeholder represents an element that does not have a specific path.
-
can_carry_signal(bool) –Whether this exact object is able to receive, pass or hold signal values.
-
instance_type(str) –Identifier for instances of this gate type.
-
connections(Dict[str, Dict[int, WireSegmentPath]]) –A dictionary mapping port names to their corresponding connection paths.
-
connection_str_paths(Dict[str, Dict[int, str]]) –Returns a dictionary mapping port names to their corresponding connection paths as strings.
-
ports(CustomDict[str, Port[Instance]]) –A dictionary mapping port names to their corresponding Port objects.
-
input_ports(Tuple[Port[Instance], ...]) –Returns a tuple of input ports associated with this instance.
-
output_ports(Tuple[Port[Instance], ...]) –Returns a tuple of output ports associated with this instance.
-
has_unconnected_port_segments(bool) –Returns True if the instance has at least one unconnected port segment.
-
signals(Dict[str, Dict[int, Signal]]) –A dictionary with all signals currently present on all ports of this instance.
-
is_blackbox(bool) –Flag indicating whether the instance represents neither a primitive element nor a module instance.
-
verilog(str) –Generates the Verilog code for this instance.
-
a_width(PositiveInt) –Width of the gate's
Aport. -
b_width(PositiveInt) –Width of the gate's
Bport. -
is_sequential(bool) –Whether instances of this gate are considered sequential gates.
-
output_port(Port[Instance]) –The output port of the gate.
-
data_width(int) –The data width of this instance.
-
input_port(Port[Instance]) –The input port of the gate.
clk_polarity
property
writable
¶
clk_polarity: Signal
Which clock edge activates the flip-flop. Default is Signal.HIGH, i.e. rising edge.
name
instance-attribute
¶
name: str
The name of the element.
!!! Do not modify this variable after instantiating it. Use NetlistElement.set_name() instead. Modifying this variable might lead to inconsistencies later on.
metadata
class-attribute
instance-attribute
¶
metadata: MetadataMixin = MetadataMixin()
Metadata of a netlist element.
Can be user-defined, or e. g. by Yosys (such as src for the HDL source).
Is also grouped by categories, i.e. all metadata from Yosys can be accessed via Module.metadata["yosys"],
or via Module.metadata.yosys, which both return a dictionary of all metadata.
Read the documentation of MetaDataMixin for more information.
path
property
¶
path: InstancePath
Returns the InstancePath of the netlist element.
The InstancePath object is constructed using the element's type and its raw hierarchical path.
Returns:
-
InstancePath(InstancePath) –The hierarchical path of the netlist element.
hierarchy_level
property
¶
hierarchy_level: int
The level of hierarchy of the element in the design.
The hierarchy level is the number of separators in the element's raw path. For example, a top-level instance has a hierarchy level of 0, while a direct submodule instance has a hierarchy level of 1.
Returns:
-
int(int) –The hierarchy level of the element.
circuit
property
¶
The circuit object to which this netlist element belongs to.
- For a module, returns the circuit to which the module belongs.
- For any other netlist element, recursively returns the circuit of the parent, which ultimately leads to a module, to which the netlist element belongs.
Raises:
-
ParentNotFoundError–If a parent cannot be resolved somewhere in the hierarchical chain.
-
ObjectNotFoundError–If for a module no circuit is set.
has_circuit
property
¶
has_circuit: bool
Whether this netlist element has a defined circuit it belongs to.
Tries to access self.circuit and returns whether the call was successful.
Can be used instead of a try-except clause around the call to NetlistElement.circuit.
locked
property
¶
locked: bool
True if this NetlistElement instance is locked (i.e. it is currently structurally unchangeable), False if it is mutable.
Can be changed via NetlistElement.change_mutability(bool).
Immutability is used to prevent accidental changes to the design.
is_placeholder_instance
property
¶
is_placeholder_instance: bool
A placeholder represents an element that does not have a specific path.
True if this NetlistElement instance represents a placeholder, False otherwise.
can_carry_signal
property
¶
can_carry_signal: bool
Whether this exact object is able to receive, pass or hold signal values.
True for ports (as they drive or receive values) and wires (as they pass the signals
from driver to load ports) as well as their respective segments.
False for instances and modules.
instance_type
class-attribute
instance-attribute
¶
instance_type: str = id_internal
Identifier for instances of this gate type.
connections
property
¶
connections: Dict[str, Dict[int, WireSegmentPath]]
A dictionary mapping port names to their corresponding connection paths.
The keys of the outer dictionary are the names of the ports.
The values are dictionaries where the keys are the port bit numbers and the values are WireSegmentPath objects.
The element paths are the paths of the wires connected to the corresponding bits of the port.
Elements with no connection (i.e. the path is None) are excluded.
In case these are needed, use Instance.get_connections() instead.
Example
{ 'port1': {1: WireSegmentPath('path/to/element1'), 2: WireSegmentPath('path/to/element2')}, 'port2': {1: WireSegmentPath('path/to/element3')} }
connection_str_paths
property
¶
Returns a dictionary mapping port names to their corresponding connection paths as strings.
The keys are the names of the ports. The values are dictionaries where the keys are the port bit numbers and the values are the raw string paths.
Example
{ 'port1': {0: 'path.to.element1', 1: 'path.to.element2'}, 'port2': {0: 'path.to.element3'} }
ports
property
¶
ports: CustomDict[str, Port[Instance]]
A dictionary mapping port names to their corresponding Port objects.
The keys of the dictionary are the names of the ports. The values are Port objects representing the ports.
Returns:
-
CustomDict[str, Port[Instance]]–A dictionary mapping port names to their corresponding Port objects.
input_ports
property
¶
output_ports
property
¶
has_unconnected_port_segments
property
¶
has_unconnected_port_segments: bool
Returns True if the instance has at least one unconnected port segment.
signals
property
¶
A dictionary with all signals currently present on all ports of this instance.
is_blackbox
property
¶
is_blackbox: bool
Flag indicating whether the instance represents neither a primitive element nor a module instance.
If True, this instance does not have a module definition, and is also not a basic component (i.e. a primitive gate instance) from the internal gate library, such as a gate or flip-flop.
verilog
property
¶
verilog: str
Generates the Verilog code for this instance.
This property uses the verilog_template to generate the actual Verilog code by replacing the placeholders with
the instance type, name, and port connections. It returns a string that can be used directly in a Verilog file.
Returns:
-
str(str) –The generated Verilog code.
is_sequential
property
¶
is_sequential: bool
Whether instances of this gate are considered sequential gates.
False for combinational gates, such as AND gates or arithmetic gates.
True for flip-flops and latches.
data_width
property
¶
data_width: int
The data width of this instance.
Defaults to the data width of the output port. Can be overwritten in extended classes.
In contrast to self.width, which is used for the creation and initialization,
this property is linked to the data width of the output port.
This property is useful when the data width of the output port can be changed.
model_post_init
¶
Initializes the gate's ports and connections.
This method is called after the gate's attributes have been initialized, and it sets up the gate's ports and connections.
set_clk
¶
set_clk(new_signal: SignalOrLogicLevel) -> None
change_mutability
¶
change_mutability(is_now_locked: bool, recursive: bool = False) -> Self
Change the mutability status of this instance and optionally its ports.
This method allows setting whether this instance can be modified or not.
If recursive is set to True, it also applies the same mutability status to all ports of this instance.
Parameters:
evaluate
¶
Evaluates the gate's output signal based on its input signals.
This method is called when the gate's input signals change, and it updates the gate's output signal accordingly.
all_connections
¶
all_connections(
include_unconnected: bool,
) -> Dict[str, Dict[int, WireSegmentPath]]
Returns a dictionary mapping port names to their corresponding connection paths.
The keys of the outer dictionary are the names of the ports. The values are dictionaries where the keys are the port bit numbers and the values are WireSegmentPath objects. The element paths are the paths of the wires connected to the corresponding bits of the port.
Parameters:
Returns:
-
Dict[str, Dict[int, WireSegmentPath]]–A dictionary mapping port names to their corresponding connection paths.
connect
¶
connect(
port_name: str,
ws_path: Optional[WireSegmentPath],
direction: Direction = UNKNOWN,
index: NonNegativeInt = 0,
width: PositiveInt = 1,
) -> None
Add connections to the specified port of this instance.
This method can establish multiple connections if a range of indices is provided.
Parameters:
-
(port_name¶str) –The name of the port where the connection(s) should be established.
-
(ws_path¶Optional[WireSegmentPath]) –The path of the wire segment that will be connected to this port. If None, the associated port (segment) remains unconnected.
-
(direction¶Direction, default:UNKNOWN) –The direction of the port. Defaults to Direction.UNKNOWN.
-
(index¶NonNegativeInt, default:0) –The starting bit index within the port for these connections. Defaults to 0.
-
(width¶PositiveInt, default:1) –The number of consecutive bits in the port that should be connected. Defaults to 1.
Raises:
-
ObjectLockedError–If this instance is currently locked, and no connection can be made.
disconnect
¶
Remove an existing connection from a specified port of this instance.
This method disconnects the wire segment at the given index within the specified port_name.
Parameters:
-
(port_name¶str) –The name of the port where the connection should be removed.
-
(index¶Optional[int], default:None) –The bit index within the port for this disconnection. Defaults to None, which completely disconnects the port.
Raises:
-
ObjectLockedError–If this instance is locked.
-
ObjectNotFoundError–If no port exists with the given name.
get_connection
¶
get_connection(
port_name: str, index: Optional[NonNegativeInt] = None
) -> Union[WireSegmentPath, Dict[int, WireSegmentPath], None]
Retrieve the connection path associated with a specified port and bit index.
If index is provided, this method returns the wire segment path connected to that specific bit within the port.
Otherwise, it returns all connections for the given port as a dictionary mapping indices to WireSegmentPaths.
Parameters:
-
(port_name¶str) –The name of the port for which to retrieve the connection(s).
-
(index¶Optional[NonNegativeInt], default:None) –The bit index within the port. Defaults to None.
Returns:
-
Union[WireSegmentPath, Dict[int, WireSegmentPath], None]–Union[WireSegmentPath, Dict[int, WireSegmentPath]]: Either a single WireSegmentPath or a dictionary mapping indices to WireSegmentPaths.
modify_connection
¶
modify_connection(
port_name: str, ws_path: WireSegmentPath, index: NonNegativeInt = 0
) -> None
Modify an existing connection within a specified port of this instance.
This method updates the wire segment path at the given index within the specified port_name.
If the index does not exist yet, it is newly added, changing the width of the port.
Parameters:
-
(port_name¶str) –The name of the port where the connection should be modified.
-
(ws_path¶WireSegmentPath) –The new wire segment path for this connection.
-
(index¶NonNegativeInt, default:0) –The bit index within the port. Defaults to 0.
connect_modify
¶
connect_modify(
port_name: str,
ws_path: WireSegmentPath,
direction: Direction = UNKNOWN,
index: NonNegativeInt = 0,
width: PositiveInt = 1,
) -> None
Add a new connection or modify an existing one within the specified port of this instance.
If the connection already exists at the given index and port_name, it will be modified.
Otherwise, a new connection is added.
Parameters:
-
(port_name¶str) –The name of the port where the connection should be established or updated.
-
(ws_path¶WireSegmentPath) –The path of the wire segment that will be connected to this port.
-
(direction¶Direction, default:UNKNOWN) –The direction of the port. Defaults to Direction.UNKNOWN.
-
(index¶NonNegativeInt, default:0) –The starting bit index within the port for these connections. Defaults to 0.
-
(width¶PositiveInt, default:1) –The number of consecutive bits in the port that should be connected. Defaults to 1.
tie_port
¶
Set a constant signal value for the specified port and bit index.
If the specified port does not exist, an error message is logged and the function returns False. Otherwise, the method tries to set the constant signal value for that port and returns True if successful.
Does not work for instance output ports, as they are always driven by their parent instances.
Parameters:
-
(name¶str) –The name of the port.
-
(index¶NonNegativeInt) –The bit index within the port.
-
(sig_value¶LogicLevel) –The constant signal value to be set ('0', '1', or 'Z').
Raises:
-
ObjectNotFoundError–If no such port or port segment exists.
-
AlreadyConnectedError–(raised by: PortSegment.tie_signal) If this segment is belongs to a load port and is already connected to a wire, from which it receives its value.
-
InvalidDirectionError–(raised by: PortSegment.tie_signal) If this port segment belongs to an instance output port, which is driven by the instance inputs and the instance's internal logic.
-
InvalidSignalError–(raised by: PortSegment.tie_signal) If an invalid value is provided.
has_tied_ports
¶
has_tied_ports() -> bool
Checks the ports of this instance whether any are tied to constant values.
Instance ports can be tied to a constant value for several reasons, but if all input or all output ports are tied, the instance is rendered useless.
Returns:
-
bool(bool) –True if any ports of this instance are tied to constant values. False otherwise, i.e. all ports are connected to wires.
has_tied_inputs
¶
has_tied_inputs() -> bool
Checks the input ports of this instance whether any are tied to constant values.
Instance ports can be tied to a constant value for several reasons, but if all input or all output ports are tied, the instance is rendered useless.
Returns:
-
bool(bool) –True if any input ports of this instance are tied to constant values. False otherwise, i.e. all input ports are connected to wires.
has_tied_outputs
¶
has_tied_outputs() -> bool
Checks the output ports of this instance whether any are tied to constant values.
Instance ports can be tied to a constant value for several reasons, but if all input or all output ports are tied, the instance is rendered useless.
Returns:
-
bool(bool) –True if any output ports of this instance are tied to constant values. False otherwise, i.e. all output ports are connected to wires.
update_signedness
¶
split
¶
Performs a bit-wise split on this instance.
If this instance supports splitting and has a data width of n bit, this method splits this instance into n 1-bit instances. This works for instances, where each output bit depends only on the corresponding input bit(s), e.g. an AND gate or a D-Flipflop.
Returns:
-
Dict[NonNegativeInt, Self]–Dict[int, Instance]: An n-bit large dictionary, where each key conforms to a bit of the original instance, and each value is an 1-bit instance representing the corresponding "slice" of the original instance.
Raises:
-
SplittingUnsupportedError–If this instance does not support splitting. Happens for any gate or instance whose behavior depends on the whole bus, and splitting would make it lose its meaning.
p2v
¶
Converts a Port object to its corresponding Verilog structure by using the connected wire segments.
This method takes the connected wire segments of a Port object and converts them to their corresponding Verilog signal structure (p2ws2v -> Port to WireSegment to Verilog signal syntax). The method requires that the currently selected module matches the module of the Port object, which is derived from the design path of the Port object. For each segment of the port, it checks whether a corresponding connected wire segment exists in the current module. If the port is set to a constant, the corresponding constant wire segment placeholder is used instead. Port segments can be excluded from the conversion by providing a list of indices, indicating which segments should be excluded from the conversion (e.g. segments that are known to be unconnected).
Parameters:
-
(port¶Port) –The Port object to convert.
-
(exclude_indices¶List[int], default:None) –A list of indices to exclude from the conversion. Defaults to an empty list.
Returns:
-
str(str) –The Verilog signal structure as a string.
Raises:
-
AttributeError–If the currently selected module does not match the module of the port.
EnMixin
¶
Bases: StorageGate
Methods:
-
set_en–Sets the enable signal.
-
change_mutability–Change the mutability status of this instance and optionally its ports.
-
evaluate–Evaluates the gate's output signal based on its input signals.
-
all_connections–Returns a dictionary mapping port names to their corresponding connection paths.
-
connect–Add connections to the specified port of this instance.
-
disconnect–Remove an existing connection from a specified port of this instance.
-
get_connection–Retrieve the connection path associated with a specified port and bit index.
-
modify_connection–Modify an existing connection within a specified port of this instance.
-
connect_modify–Add a new connection or modify an existing one within the specified port of this instance.
-
tie_port–Set a constant signal value for the specified port and bit index.
-
has_tied_ports–Checks the ports of this instance whether any are tied to constant values.
-
has_tied_inputs–Checks the input ports of this instance whether any are tied to constant values.
-
has_tied_outputs–Checks the output ports of this instance whether any are tied to constant values.
-
update_signedness–Retrieves the signedness of the given port and updates it in this gate instance's parameter dictionary.
-
split–Performs a bit-wise split on this instance.
-
p2v–Converts a Port object to its corresponding Verilog structure by using the connected wire segments.
-
set–Sets the signal on a port.
Attributes:
-
en_polarity(Signal) –Which EN-signal level enables writing on the data storage. Default is Signal.HIGH.
-
en_port(Port[Instance]) –The enable port of the gate.
-
en_signal(Signal) –The enable signal of the gate.
-
name(str) –The name of the element.
-
metadata(MetadataMixin) –Metadata of a netlist element.
-
path(InstancePath) –Returns the InstancePath of the netlist element.
-
type(EType) –The type of the element, which is an instance.
-
hierarchy_level(int) –The level of hierarchy of the element in the design.
-
has_parent(bool) –Whether this object has a parent.
-
circuit('Circuit') –The circuit object to which this netlist element belongs to.
-
has_circuit(bool) –Whether this netlist element has a defined circuit it belongs to.
-
locked(bool) –True if this NetlistElement instance is locked (i.e. it is currently structurally unchangeable), False if it is mutable.
-
is_placeholder_instance(bool) –A placeholder represents an element that does not have a specific path.
-
can_carry_signal(bool) –Whether this exact object is able to receive, pass or hold signal values.
-
instance_type(str) –Identifier for instances of this gate type.
-
connections(Dict[str, Dict[int, WireSegmentPath]]) –A dictionary mapping port names to their corresponding connection paths.
-
connection_str_paths(Dict[str, Dict[int, str]]) –Returns a dictionary mapping port names to their corresponding connection paths as strings.
-
ports(CustomDict[str, Port[Instance]]) –A dictionary mapping port names to their corresponding Port objects.
-
input_ports(Tuple[Port[Instance], ...]) –Returns a tuple of input ports associated with this instance.
-
output_ports(Tuple[Port[Instance], ...]) –Returns a tuple of output ports associated with this instance.
-
has_unconnected_port_segments(bool) –Returns True if the instance has at least one unconnected port segment.
-
signals(Dict[str, Dict[int, Signal]]) –A dictionary with all signals currently present on all ports of this instance.
-
is_blackbox(bool) –Flag indicating whether the instance represents neither a primitive element nor a module instance.
-
verilog(str) –Generates the Verilog code for this instance.
-
a_width(PositiveInt) –Width of the gate's
Aport. -
b_width(PositiveInt) –Width of the gate's
Bport. -
is_sequential(bool) –Whether instances of this gate are considered sequential gates.
-
output_port(Port[Instance]) –The output port of the gate.
-
data_width(int) –The data width of this instance.
-
input_port(Port[Instance]) –The input port of the gate.
en_polarity
property
writable
¶
en_polarity: Signal
Which EN-signal level enables writing on the data storage. Default is Signal.HIGH.
name
instance-attribute
¶
name: str
The name of the element.
!!! Do not modify this variable after instantiating it. Use NetlistElement.set_name() instead. Modifying this variable might lead to inconsistencies later on.
metadata
class-attribute
instance-attribute
¶
metadata: MetadataMixin = MetadataMixin()
Metadata of a netlist element.
Can be user-defined, or e. g. by Yosys (such as src for the HDL source).
Is also grouped by categories, i.e. all metadata from Yosys can be accessed via Module.metadata["yosys"],
or via Module.metadata.yosys, which both return a dictionary of all metadata.
Read the documentation of MetaDataMixin for more information.
path
property
¶
path: InstancePath
Returns the InstancePath of the netlist element.
The InstancePath object is constructed using the element's type and its raw hierarchical path.
Returns:
-
InstancePath(InstancePath) –The hierarchical path of the netlist element.
hierarchy_level
property
¶
hierarchy_level: int
The level of hierarchy of the element in the design.
The hierarchy level is the number of separators in the element's raw path. For example, a top-level instance has a hierarchy level of 0, while a direct submodule instance has a hierarchy level of 1.
Returns:
-
int(int) –The hierarchy level of the element.
circuit
property
¶
The circuit object to which this netlist element belongs to.
- For a module, returns the circuit to which the module belongs.
- For any other netlist element, recursively returns the circuit of the parent, which ultimately leads to a module, to which the netlist element belongs.
Raises:
-
ParentNotFoundError–If a parent cannot be resolved somewhere in the hierarchical chain.
-
ObjectNotFoundError–If for a module no circuit is set.
has_circuit
property
¶
has_circuit: bool
Whether this netlist element has a defined circuit it belongs to.
Tries to access self.circuit and returns whether the call was successful.
Can be used instead of a try-except clause around the call to NetlistElement.circuit.
locked
property
¶
locked: bool
True if this NetlistElement instance is locked (i.e. it is currently structurally unchangeable), False if it is mutable.
Can be changed via NetlistElement.change_mutability(bool).
Immutability is used to prevent accidental changes to the design.
is_placeholder_instance
property
¶
is_placeholder_instance: bool
A placeholder represents an element that does not have a specific path.
True if this NetlistElement instance represents a placeholder, False otherwise.
can_carry_signal
property
¶
can_carry_signal: bool
Whether this exact object is able to receive, pass or hold signal values.
True for ports (as they drive or receive values) and wires (as they pass the signals
from driver to load ports) as well as their respective segments.
False for instances and modules.
instance_type
class-attribute
instance-attribute
¶
instance_type: str = id_internal
Identifier for instances of this gate type.
connections
property
¶
connections: Dict[str, Dict[int, WireSegmentPath]]
A dictionary mapping port names to their corresponding connection paths.
The keys of the outer dictionary are the names of the ports.
The values are dictionaries where the keys are the port bit numbers and the values are WireSegmentPath objects.
The element paths are the paths of the wires connected to the corresponding bits of the port.
Elements with no connection (i.e. the path is None) are excluded.
In case these are needed, use Instance.get_connections() instead.
Example
{ 'port1': {1: WireSegmentPath('path/to/element1'), 2: WireSegmentPath('path/to/element2')}, 'port2': {1: WireSegmentPath('path/to/element3')} }
connection_str_paths
property
¶
Returns a dictionary mapping port names to their corresponding connection paths as strings.
The keys are the names of the ports. The values are dictionaries where the keys are the port bit numbers and the values are the raw string paths.
Example
{ 'port1': {0: 'path.to.element1', 1: 'path.to.element2'}, 'port2': {0: 'path.to.element3'} }
ports
property
¶
ports: CustomDict[str, Port[Instance]]
A dictionary mapping port names to their corresponding Port objects.
The keys of the dictionary are the names of the ports. The values are Port objects representing the ports.
Returns:
-
CustomDict[str, Port[Instance]]–A dictionary mapping port names to their corresponding Port objects.
input_ports
property
¶
output_ports
property
¶
has_unconnected_port_segments
property
¶
has_unconnected_port_segments: bool
Returns True if the instance has at least one unconnected port segment.
signals
property
¶
A dictionary with all signals currently present on all ports of this instance.
is_blackbox
property
¶
is_blackbox: bool
Flag indicating whether the instance represents neither a primitive element nor a module instance.
If True, this instance does not have a module definition, and is also not a basic component (i.e. a primitive gate instance) from the internal gate library, such as a gate or flip-flop.
verilog
property
¶
verilog: str
Generates the Verilog code for this instance.
This property uses the verilog_template to generate the actual Verilog code by replacing the placeholders with
the instance type, name, and port connections. It returns a string that can be used directly in a Verilog file.
Returns:
-
str(str) –The generated Verilog code.
is_sequential
property
¶
is_sequential: bool
Whether instances of this gate are considered sequential gates.
False for combinational gates, such as AND gates or arithmetic gates.
True for flip-flops and latches.
data_width
property
¶
data_width: int
The data width of this instance.
Defaults to the data width of the output port. Can be overwritten in extended classes.
In contrast to self.width, which is used for the creation and initialization,
this property is linked to the data width of the output port.
This property is useful when the data width of the output port can be changed.
set_en
¶
set_en(new_signal: SignalOrLogicLevel) -> None
change_mutability
¶
change_mutability(is_now_locked: bool, recursive: bool = False) -> Self
Change the mutability status of this instance and optionally its ports.
This method allows setting whether this instance can be modified or not.
If recursive is set to True, it also applies the same mutability status to all ports of this instance.
Parameters:
evaluate
¶
Evaluates the gate's output signal based on its input signals.
This method is called when the gate's input signals change, and it updates the gate's output signal accordingly.
all_connections
¶
all_connections(
include_unconnected: bool,
) -> Dict[str, Dict[int, WireSegmentPath]]
Returns a dictionary mapping port names to their corresponding connection paths.
The keys of the outer dictionary are the names of the ports. The values are dictionaries where the keys are the port bit numbers and the values are WireSegmentPath objects. The element paths are the paths of the wires connected to the corresponding bits of the port.
Parameters:
Returns:
-
Dict[str, Dict[int, WireSegmentPath]]–A dictionary mapping port names to their corresponding connection paths.
connect
¶
connect(
port_name: str,
ws_path: Optional[WireSegmentPath],
direction: Direction = UNKNOWN,
index: NonNegativeInt = 0,
width: PositiveInt = 1,
) -> None
Add connections to the specified port of this instance.
This method can establish multiple connections if a range of indices is provided.
Parameters:
-
(port_name¶str) –The name of the port where the connection(s) should be established.
-
(ws_path¶Optional[WireSegmentPath]) –The path of the wire segment that will be connected to this port. If None, the associated port (segment) remains unconnected.
-
(direction¶Direction, default:UNKNOWN) –The direction of the port. Defaults to Direction.UNKNOWN.
-
(index¶NonNegativeInt, default:0) –The starting bit index within the port for these connections. Defaults to 0.
-
(width¶PositiveInt, default:1) –The number of consecutive bits in the port that should be connected. Defaults to 1.
Raises:
-
ObjectLockedError–If this instance is currently locked, and no connection can be made.
disconnect
¶
Remove an existing connection from a specified port of this instance.
This method disconnects the wire segment at the given index within the specified port_name.
Parameters:
-
(port_name¶str) –The name of the port where the connection should be removed.
-
(index¶Optional[int], default:None) –The bit index within the port for this disconnection. Defaults to None, which completely disconnects the port.
Raises:
-
ObjectLockedError–If this instance is locked.
-
ObjectNotFoundError–If no port exists with the given name.
get_connection
¶
get_connection(
port_name: str, index: Optional[NonNegativeInt] = None
) -> Union[WireSegmentPath, Dict[int, WireSegmentPath], None]
Retrieve the connection path associated with a specified port and bit index.
If index is provided, this method returns the wire segment path connected to that specific bit within the port.
Otherwise, it returns all connections for the given port as a dictionary mapping indices to WireSegmentPaths.
Parameters:
-
(port_name¶str) –The name of the port for which to retrieve the connection(s).
-
(index¶Optional[NonNegativeInt], default:None) –The bit index within the port. Defaults to None.
Returns:
-
Union[WireSegmentPath, Dict[int, WireSegmentPath], None]–Union[WireSegmentPath, Dict[int, WireSegmentPath]]: Either a single WireSegmentPath or a dictionary mapping indices to WireSegmentPaths.
modify_connection
¶
modify_connection(
port_name: str, ws_path: WireSegmentPath, index: NonNegativeInt = 0
) -> None
Modify an existing connection within a specified port of this instance.
This method updates the wire segment path at the given index within the specified port_name.
If the index does not exist yet, it is newly added, changing the width of the port.
Parameters:
-
(port_name¶str) –The name of the port where the connection should be modified.
-
(ws_path¶WireSegmentPath) –The new wire segment path for this connection.
-
(index¶NonNegativeInt, default:0) –The bit index within the port. Defaults to 0.
connect_modify
¶
connect_modify(
port_name: str,
ws_path: WireSegmentPath,
direction: Direction = UNKNOWN,
index: NonNegativeInt = 0,
width: PositiveInt = 1,
) -> None
Add a new connection or modify an existing one within the specified port of this instance.
If the connection already exists at the given index and port_name, it will be modified.
Otherwise, a new connection is added.
Parameters:
-
(port_name¶str) –The name of the port where the connection should be established or updated.
-
(ws_path¶WireSegmentPath) –The path of the wire segment that will be connected to this port.
-
(direction¶Direction, default:UNKNOWN) –The direction of the port. Defaults to Direction.UNKNOWN.
-
(index¶NonNegativeInt, default:0) –The starting bit index within the port for these connections. Defaults to 0.
-
(width¶PositiveInt, default:1) –The number of consecutive bits in the port that should be connected. Defaults to 1.
tie_port
¶
Set a constant signal value for the specified port and bit index.
If the specified port does not exist, an error message is logged and the function returns False. Otherwise, the method tries to set the constant signal value for that port and returns True if successful.
Does not work for instance output ports, as they are always driven by their parent instances.
Parameters:
-
(name¶str) –The name of the port.
-
(index¶NonNegativeInt) –The bit index within the port.
-
(sig_value¶LogicLevel) –The constant signal value to be set ('0', '1', or 'Z').
Raises:
-
ObjectNotFoundError–If no such port or port segment exists.
-
AlreadyConnectedError–(raised by: PortSegment.tie_signal) If this segment is belongs to a load port and is already connected to a wire, from which it receives its value.
-
InvalidDirectionError–(raised by: PortSegment.tie_signal) If this port segment belongs to an instance output port, which is driven by the instance inputs and the instance's internal logic.
-
InvalidSignalError–(raised by: PortSegment.tie_signal) If an invalid value is provided.
has_tied_ports
¶
has_tied_ports() -> bool
Checks the ports of this instance whether any are tied to constant values.
Instance ports can be tied to a constant value for several reasons, but if all input or all output ports are tied, the instance is rendered useless.
Returns:
-
bool(bool) –True if any ports of this instance are tied to constant values. False otherwise, i.e. all ports are connected to wires.
has_tied_inputs
¶
has_tied_inputs() -> bool
Checks the input ports of this instance whether any are tied to constant values.
Instance ports can be tied to a constant value for several reasons, but if all input or all output ports are tied, the instance is rendered useless.
Returns:
-
bool(bool) –True if any input ports of this instance are tied to constant values. False otherwise, i.e. all input ports are connected to wires.
has_tied_outputs
¶
has_tied_outputs() -> bool
Checks the output ports of this instance whether any are tied to constant values.
Instance ports can be tied to a constant value for several reasons, but if all input or all output ports are tied, the instance is rendered useless.
Returns:
-
bool(bool) –True if any output ports of this instance are tied to constant values. False otherwise, i.e. all output ports are connected to wires.
update_signedness
¶
split
¶
Performs a bit-wise split on this instance.
If this instance supports splitting and has a data width of n bit, this method splits this instance into n 1-bit instances. This works for instances, where each output bit depends only on the corresponding input bit(s), e.g. an AND gate or a D-Flipflop.
Returns:
-
Dict[NonNegativeInt, Self]–Dict[int, Instance]: An n-bit large dictionary, where each key conforms to a bit of the original instance, and each value is an 1-bit instance representing the corresponding "slice" of the original instance.
Raises:
-
SplittingUnsupportedError–If this instance does not support splitting. Happens for any gate or instance whose behavior depends on the whole bus, and splitting would make it lose its meaning.
p2v
¶
Converts a Port object to its corresponding Verilog structure by using the connected wire segments.
This method takes the connected wire segments of a Port object and converts them to their corresponding Verilog signal structure (p2ws2v -> Port to WireSegment to Verilog signal syntax). The method requires that the currently selected module matches the module of the Port object, which is derived from the design path of the Port object. For each segment of the port, it checks whether a corresponding connected wire segment exists in the current module. If the port is set to a constant, the corresponding constant wire segment placeholder is used instead. Port segments can be excluded from the conversion by providing a list of indices, indicating which segments should be excluded from the conversion (e.g. segments that are known to be unconnected).
Parameters:
-
(port¶Port) –The Port object to convert.
-
(exclude_indices¶List[int], default:None) –A list of indices to exclude from the conversion. Defaults to an empty list.
Returns:
-
str(str) –The Verilog signal structure as a string.
Raises:
-
AttributeError–If the currently selected module does not match the module of the port.
RstMixin
¶
Bases: StorageGate
Methods:
-
set_rst–Sets the reset signal.
-
change_mutability–Change the mutability status of this instance and optionally its ports.
-
evaluate–Evaluates the gate's output signal based on its input signals.
-
all_connections–Returns a dictionary mapping port names to their corresponding connection paths.
-
connect–Add connections to the specified port of this instance.
-
disconnect–Remove an existing connection from a specified port of this instance.
-
get_connection–Retrieve the connection path associated with a specified port and bit index.
-
modify_connection–Modify an existing connection within a specified port of this instance.
-
connect_modify–Add a new connection or modify an existing one within the specified port of this instance.
-
tie_port–Set a constant signal value for the specified port and bit index.
-
has_tied_ports–Checks the ports of this instance whether any are tied to constant values.
-
has_tied_inputs–Checks the input ports of this instance whether any are tied to constant values.
-
has_tied_outputs–Checks the output ports of this instance whether any are tied to constant values.
-
update_signedness–Retrieves the signedness of the given port and updates it in this gate instance's parameter dictionary.
-
split–Performs a bit-wise split on this instance.
-
p2v–Converts a Port object to its corresponding Verilog structure by using the connected wire segments.
-
set–Sets the signal on a port.
Attributes:
-
rst_polarity(Signal) –Which reset level resets the flip-flop. Default is Signal.HIGH: the flipflop is in reset, if the reset signal is HIGH.
-
rst_val_int(int) –Reset value of the flip-flop as integer. Default is 0.
-
rst_port(Port[Instance]) –The reset port of the gate.
-
rst_val(Dict[int, Signal]) –The value of the flipflop during and after reset. Default is Signal.LOW, i.e. the initial flipflop state is 0 by default.
-
in_reset(bool) –True if the gate is currently in reset, False otherwise.
-
name(str) –The name of the element.
-
metadata(MetadataMixin) –Metadata of a netlist element.
-
path(InstancePath) –Returns the InstancePath of the netlist element.
-
type(EType) –The type of the element, which is an instance.
-
hierarchy_level(int) –The level of hierarchy of the element in the design.
-
has_parent(bool) –Whether this object has a parent.
-
circuit('Circuit') –The circuit object to which this netlist element belongs to.
-
has_circuit(bool) –Whether this netlist element has a defined circuit it belongs to.
-
locked(bool) –True if this NetlistElement instance is locked (i.e. it is currently structurally unchangeable), False if it is mutable.
-
is_placeholder_instance(bool) –A placeholder represents an element that does not have a specific path.
-
can_carry_signal(bool) –Whether this exact object is able to receive, pass or hold signal values.
-
instance_type(str) –Identifier for instances of this gate type.
-
connections(Dict[str, Dict[int, WireSegmentPath]]) –A dictionary mapping port names to their corresponding connection paths.
-
connection_str_paths(Dict[str, Dict[int, str]]) –Returns a dictionary mapping port names to their corresponding connection paths as strings.
-
ports(CustomDict[str, Port[Instance]]) –A dictionary mapping port names to their corresponding Port objects.
-
input_ports(Tuple[Port[Instance], ...]) –Returns a tuple of input ports associated with this instance.
-
output_ports(Tuple[Port[Instance], ...]) –Returns a tuple of output ports associated with this instance.
-
has_unconnected_port_segments(bool) –Returns True if the instance has at least one unconnected port segment.
-
signals(Dict[str, Dict[int, Signal]]) –A dictionary with all signals currently present on all ports of this instance.
-
is_blackbox(bool) –Flag indicating whether the instance represents neither a primitive element nor a module instance.
-
verilog(str) –Generates the Verilog code for this instance.
-
a_width(PositiveInt) –Width of the gate's
Aport. -
b_width(PositiveInt) –Width of the gate's
Bport. -
is_sequential(bool) –Whether instances of this gate are considered sequential gates.
-
output_port(Port[Instance]) –The output port of the gate.
-
data_width(int) –The data width of this instance.
-
input_port(Port[Instance]) –The input port of the gate.
rst_polarity
property
writable
¶
rst_polarity: Signal
Which reset level resets the flip-flop. Default is Signal.HIGH: the flipflop is in reset, if the reset signal is HIGH.
rst_val_int
property
writable
¶
rst_val_int: int
Reset value of the flip-flop as integer. Default is 0.
rst_val
property
¶
The value of the flipflop during and after reset. Default is Signal.LOW, i.e. the initial flipflop state is 0 by default.
name
instance-attribute
¶
name: str
The name of the element.
!!! Do not modify this variable after instantiating it. Use NetlistElement.set_name() instead. Modifying this variable might lead to inconsistencies later on.
metadata
class-attribute
instance-attribute
¶
metadata: MetadataMixin = MetadataMixin()
Metadata of a netlist element.
Can be user-defined, or e. g. by Yosys (such as src for the HDL source).
Is also grouped by categories, i.e. all metadata from Yosys can be accessed via Module.metadata["yosys"],
or via Module.metadata.yosys, which both return a dictionary of all metadata.
Read the documentation of MetaDataMixin for more information.
path
property
¶
path: InstancePath
Returns the InstancePath of the netlist element.
The InstancePath object is constructed using the element's type and its raw hierarchical path.
Returns:
-
InstancePath(InstancePath) –The hierarchical path of the netlist element.
hierarchy_level
property
¶
hierarchy_level: int
The level of hierarchy of the element in the design.
The hierarchy level is the number of separators in the element's raw path. For example, a top-level instance has a hierarchy level of 0, while a direct submodule instance has a hierarchy level of 1.
Returns:
-
int(int) –The hierarchy level of the element.
circuit
property
¶
The circuit object to which this netlist element belongs to.
- For a module, returns the circuit to which the module belongs.
- For any other netlist element, recursively returns the circuit of the parent, which ultimately leads to a module, to which the netlist element belongs.
Raises:
-
ParentNotFoundError–If a parent cannot be resolved somewhere in the hierarchical chain.
-
ObjectNotFoundError–If for a module no circuit is set.
has_circuit
property
¶
has_circuit: bool
Whether this netlist element has a defined circuit it belongs to.
Tries to access self.circuit and returns whether the call was successful.
Can be used instead of a try-except clause around the call to NetlistElement.circuit.
locked
property
¶
locked: bool
True if this NetlistElement instance is locked (i.e. it is currently structurally unchangeable), False if it is mutable.
Can be changed via NetlistElement.change_mutability(bool).
Immutability is used to prevent accidental changes to the design.
is_placeholder_instance
property
¶
is_placeholder_instance: bool
A placeholder represents an element that does not have a specific path.
True if this NetlistElement instance represents a placeholder, False otherwise.
can_carry_signal
property
¶
can_carry_signal: bool
Whether this exact object is able to receive, pass or hold signal values.
True for ports (as they drive or receive values) and wires (as they pass the signals
from driver to load ports) as well as their respective segments.
False for instances and modules.
instance_type
class-attribute
instance-attribute
¶
instance_type: str = id_internal
Identifier for instances of this gate type.
connections
property
¶
connections: Dict[str, Dict[int, WireSegmentPath]]
A dictionary mapping port names to their corresponding connection paths.
The keys of the outer dictionary are the names of the ports.
The values are dictionaries where the keys are the port bit numbers and the values are WireSegmentPath objects.
The element paths are the paths of the wires connected to the corresponding bits of the port.
Elements with no connection (i.e. the path is None) are excluded.
In case these are needed, use Instance.get_connections() instead.
Example
{ 'port1': {1: WireSegmentPath('path/to/element1'), 2: WireSegmentPath('path/to/element2')}, 'port2': {1: WireSegmentPath('path/to/element3')} }
connection_str_paths
property
¶
Returns a dictionary mapping port names to their corresponding connection paths as strings.
The keys are the names of the ports. The values are dictionaries where the keys are the port bit numbers and the values are the raw string paths.
Example
{ 'port1': {0: 'path.to.element1', 1: 'path.to.element2'}, 'port2': {0: 'path.to.element3'} }
ports
property
¶
ports: CustomDict[str, Port[Instance]]
A dictionary mapping port names to their corresponding Port objects.
The keys of the dictionary are the names of the ports. The values are Port objects representing the ports.
Returns:
-
CustomDict[str, Port[Instance]]–A dictionary mapping port names to their corresponding Port objects.
input_ports
property
¶
output_ports
property
¶
has_unconnected_port_segments
property
¶
has_unconnected_port_segments: bool
Returns True if the instance has at least one unconnected port segment.
signals
property
¶
A dictionary with all signals currently present on all ports of this instance.
is_blackbox
property
¶
is_blackbox: bool
Flag indicating whether the instance represents neither a primitive element nor a module instance.
If True, this instance does not have a module definition, and is also not a basic component (i.e. a primitive gate instance) from the internal gate library, such as a gate or flip-flop.
verilog
property
¶
verilog: str
Generates the Verilog code for this instance.
This property uses the verilog_template to generate the actual Verilog code by replacing the placeholders with
the instance type, name, and port connections. It returns a string that can be used directly in a Verilog file.
Returns:
-
str(str) –The generated Verilog code.
is_sequential
property
¶
is_sequential: bool
Whether instances of this gate are considered sequential gates.
False for combinational gates, such as AND gates or arithmetic gates.
True for flip-flops and latches.
data_width
property
¶
data_width: int
The data width of this instance.
Defaults to the data width of the output port. Can be overwritten in extended classes.
In contrast to self.width, which is used for the creation and initialization,
this property is linked to the data width of the output port.
This property is useful when the data width of the output port can be changed.
set_rst
¶
set_rst(new_signal: SignalOrLogicLevel) -> None
change_mutability
¶
change_mutability(is_now_locked: bool, recursive: bool = False) -> Self
Change the mutability status of this instance and optionally its ports.
This method allows setting whether this instance can be modified or not.
If recursive is set to True, it also applies the same mutability status to all ports of this instance.
Parameters:
evaluate
¶
Evaluates the gate's output signal based on its input signals.
This method is called when the gate's input signals change, and it updates the gate's output signal accordingly.
all_connections
¶
all_connections(
include_unconnected: bool,
) -> Dict[str, Dict[int, WireSegmentPath]]
Returns a dictionary mapping port names to their corresponding connection paths.
The keys of the outer dictionary are the names of the ports. The values are dictionaries where the keys are the port bit numbers and the values are WireSegmentPath objects. The element paths are the paths of the wires connected to the corresponding bits of the port.
Parameters:
Returns:
-
Dict[str, Dict[int, WireSegmentPath]]–A dictionary mapping port names to their corresponding connection paths.
connect
¶
connect(
port_name: str,
ws_path: Optional[WireSegmentPath],
direction: Direction = UNKNOWN,
index: NonNegativeInt = 0,
width: PositiveInt = 1,
) -> None
Add connections to the specified port of this instance.
This method can establish multiple connections if a range of indices is provided.
Parameters:
-
(port_name¶str) –The name of the port where the connection(s) should be established.
-
(ws_path¶Optional[WireSegmentPath]) –The path of the wire segment that will be connected to this port. If None, the associated port (segment) remains unconnected.
-
(direction¶Direction, default:UNKNOWN) –The direction of the port. Defaults to Direction.UNKNOWN.
-
(index¶NonNegativeInt, default:0) –The starting bit index within the port for these connections. Defaults to 0.
-
(width¶PositiveInt, default:1) –The number of consecutive bits in the port that should be connected. Defaults to 1.
Raises:
-
ObjectLockedError–If this instance is currently locked, and no connection can be made.
disconnect
¶
Remove an existing connection from a specified port of this instance.
This method disconnects the wire segment at the given index within the specified port_name.
Parameters:
-
(port_name¶str) –The name of the port where the connection should be removed.
-
(index¶Optional[int], default:None) –The bit index within the port for this disconnection. Defaults to None, which completely disconnects the port.
Raises:
-
ObjectLockedError–If this instance is locked.
-
ObjectNotFoundError–If no port exists with the given name.
get_connection
¶
get_connection(
port_name: str, index: Optional[NonNegativeInt] = None
) -> Union[WireSegmentPath, Dict[int, WireSegmentPath], None]
Retrieve the connection path associated with a specified port and bit index.
If index is provided, this method returns the wire segment path connected to that specific bit within the port.
Otherwise, it returns all connections for the given port as a dictionary mapping indices to WireSegmentPaths.
Parameters:
-
(port_name¶str) –The name of the port for which to retrieve the connection(s).
-
(index¶Optional[NonNegativeInt], default:None) –The bit index within the port. Defaults to None.
Returns:
-
Union[WireSegmentPath, Dict[int, WireSegmentPath], None]–Union[WireSegmentPath, Dict[int, WireSegmentPath]]: Either a single WireSegmentPath or a dictionary mapping indices to WireSegmentPaths.
modify_connection
¶
modify_connection(
port_name: str, ws_path: WireSegmentPath, index: NonNegativeInt = 0
) -> None
Modify an existing connection within a specified port of this instance.
This method updates the wire segment path at the given index within the specified port_name.
If the index does not exist yet, it is newly added, changing the width of the port.
Parameters:
-
(port_name¶str) –The name of the port where the connection should be modified.
-
(ws_path¶WireSegmentPath) –The new wire segment path for this connection.
-
(index¶NonNegativeInt, default:0) –The bit index within the port. Defaults to 0.
connect_modify
¶
connect_modify(
port_name: str,
ws_path: WireSegmentPath,
direction: Direction = UNKNOWN,
index: NonNegativeInt = 0,
width: PositiveInt = 1,
) -> None
Add a new connection or modify an existing one within the specified port of this instance.
If the connection already exists at the given index and port_name, it will be modified.
Otherwise, a new connection is added.
Parameters:
-
(port_name¶str) –The name of the port where the connection should be established or updated.
-
(ws_path¶WireSegmentPath) –The path of the wire segment that will be connected to this port.
-
(direction¶Direction, default:UNKNOWN) –The direction of the port. Defaults to Direction.UNKNOWN.
-
(index¶NonNegativeInt, default:0) –The starting bit index within the port for these connections. Defaults to 0.
-
(width¶PositiveInt, default:1) –The number of consecutive bits in the port that should be connected. Defaults to 1.
tie_port
¶
Set a constant signal value for the specified port and bit index.
If the specified port does not exist, an error message is logged and the function returns False. Otherwise, the method tries to set the constant signal value for that port and returns True if successful.
Does not work for instance output ports, as they are always driven by their parent instances.
Parameters:
-
(name¶str) –The name of the port.
-
(index¶NonNegativeInt) –The bit index within the port.
-
(sig_value¶LogicLevel) –The constant signal value to be set ('0', '1', or 'Z').
Raises:
-
ObjectNotFoundError–If no such port or port segment exists.
-
AlreadyConnectedError–(raised by: PortSegment.tie_signal) If this segment is belongs to a load port and is already connected to a wire, from which it receives its value.
-
InvalidDirectionError–(raised by: PortSegment.tie_signal) If this port segment belongs to an instance output port, which is driven by the instance inputs and the instance's internal logic.
-
InvalidSignalError–(raised by: PortSegment.tie_signal) If an invalid value is provided.
has_tied_ports
¶
has_tied_ports() -> bool
Checks the ports of this instance whether any are tied to constant values.
Instance ports can be tied to a constant value for several reasons, but if all input or all output ports are tied, the instance is rendered useless.
Returns:
-
bool(bool) –True if any ports of this instance are tied to constant values. False otherwise, i.e. all ports are connected to wires.
has_tied_inputs
¶
has_tied_inputs() -> bool
Checks the input ports of this instance whether any are tied to constant values.
Instance ports can be tied to a constant value for several reasons, but if all input or all output ports are tied, the instance is rendered useless.
Returns:
-
bool(bool) –True if any input ports of this instance are tied to constant values. False otherwise, i.e. all input ports are connected to wires.
has_tied_outputs
¶
has_tied_outputs() -> bool
Checks the output ports of this instance whether any are tied to constant values.
Instance ports can be tied to a constant value for several reasons, but if all input or all output ports are tied, the instance is rendered useless.
Returns:
-
bool(bool) –True if any output ports of this instance are tied to constant values. False otherwise, i.e. all output ports are connected to wires.
update_signedness
¶
split
¶
Performs a bit-wise split on this instance.
If this instance supports splitting and has a data width of n bit, this method splits this instance into n 1-bit instances. This works for instances, where each output bit depends only on the corresponding input bit(s), e.g. an AND gate or a D-Flipflop.
Returns:
-
Dict[NonNegativeInt, Self]–Dict[int, Instance]: An n-bit large dictionary, where each key conforms to a bit of the original instance, and each value is an 1-bit instance representing the corresponding "slice" of the original instance.
Raises:
-
SplittingUnsupportedError–If this instance does not support splitting. Happens for any gate or instance whose behavior depends on the whole bus, and splitting would make it lose its meaning.
p2v
¶
Converts a Port object to its corresponding Verilog structure by using the connected wire segments.
This method takes the connected wire segments of a Port object and converts them to their corresponding Verilog signal structure (p2ws2v -> Port to WireSegment to Verilog signal syntax). The method requires that the currently selected module matches the module of the Port object, which is derived from the design path of the Port object. For each segment of the port, it checks whether a corresponding connected wire segment exists in the current module. If the port is set to a constant, the corresponding constant wire segment placeholder is used instead. Port segments can be excluded from the conversion by providing a list of indices, indicating which segments should be excluded from the conversion (e.g. segments that are known to be unconnected).
Parameters:
-
(port¶Port) –The Port object to convert.
-
(exclude_indices¶List[int], default:None) –A list of indices to exclude from the conversion. Defaults to an empty list.
Returns:
-
str(str) –The Verilog signal structure as a string.
Raises:
-
AttributeError–If the currently selected module does not match the module of the port.
ScanMixin
¶
Bases: StorageGate
Methods:
-
set_se–Sets the scan enable signal.
-
change_mutability–Change the mutability status of this instance and optionally its ports.
-
evaluate–Evaluates the gate's output signal based on its input signals.
-
all_connections–Returns a dictionary mapping port names to their corresponding connection paths.
-
connect–Add connections to the specified port of this instance.
-
disconnect–Remove an existing connection from a specified port of this instance.
-
get_connection–Retrieve the connection path associated with a specified port and bit index.
-
modify_connection–Modify an existing connection within a specified port of this instance.
-
connect_modify–Add a new connection or modify an existing one within the specified port of this instance.
-
tie_port–Set a constant signal value for the specified port and bit index.
-
has_tied_ports–Checks the ports of this instance whether any are tied to constant values.
-
has_tied_inputs–Checks the input ports of this instance whether any are tied to constant values.
-
has_tied_outputs–Checks the output ports of this instance whether any are tied to constant values.
-
update_signedness–Retrieves the signedness of the given port and updates it in this gate instance's parameter dictionary.
-
split–Performs a bit-wise split on this instance.
-
p2v–Converts a Port object to its corresponding Verilog structure by using the connected wire segments.
-
set–Sets the signal on a port.
Attributes:
-
se_signal(Signal) –The scan enable signal of the gate.
-
scan_ff_equivalent(Type[ClkMixin]) –Returns the Scan-FF type equivalent for normal FF and the FF type equivalent for Scan-FF.
-
name(str) –The name of the element.
-
metadata(MetadataMixin) –Metadata of a netlist element.
-
path(InstancePath) –Returns the InstancePath of the netlist element.
-
type(EType) –The type of the element, which is an instance.
-
hierarchy_level(int) –The level of hierarchy of the element in the design.
-
has_parent(bool) –Whether this object has a parent.
-
circuit('Circuit') –The circuit object to which this netlist element belongs to.
-
has_circuit(bool) –Whether this netlist element has a defined circuit it belongs to.
-
locked(bool) –True if this NetlistElement instance is locked (i.e. it is currently structurally unchangeable), False if it is mutable.
-
is_placeholder_instance(bool) –A placeholder represents an element that does not have a specific path.
-
can_carry_signal(bool) –Whether this exact object is able to receive, pass or hold signal values.
-
instance_type(str) –Identifier for instances of this gate type.
-
connections(Dict[str, Dict[int, WireSegmentPath]]) –A dictionary mapping port names to their corresponding connection paths.
-
connection_str_paths(Dict[str, Dict[int, str]]) –Returns a dictionary mapping port names to their corresponding connection paths as strings.
-
ports(CustomDict[str, Port[Instance]]) –A dictionary mapping port names to their corresponding Port objects.
-
input_ports(Tuple[Port[Instance], ...]) –Returns a tuple of input ports associated with this instance.
-
output_ports(Tuple[Port[Instance], ...]) –Returns a tuple of output ports associated with this instance.
-
has_unconnected_port_segments(bool) –Returns True if the instance has at least one unconnected port segment.
-
signals(Dict[str, Dict[int, Signal]]) –A dictionary with all signals currently present on all ports of this instance.
-
is_blackbox(bool) –Flag indicating whether the instance represents neither a primitive element nor a module instance.
-
verilog(str) –Generates the Verilog code for this instance.
-
a_width(PositiveInt) –Width of the gate's
Aport. -
b_width(PositiveInt) –Width of the gate's
Bport. -
is_sequential(bool) –Whether instances of this gate are considered sequential gates.
-
output_port(Port[Instance]) –The output port of the gate.
-
data_width(int) –The data width of this instance.
-
input_port(Port[Instance]) –The input port of the gate.
scan_ff_equivalent
property
¶
Returns the Scan-FF type equivalent for normal FF and the FF type equivalent for Scan-FF.
name
instance-attribute
¶
name: str
The name of the element.
!!! Do not modify this variable after instantiating it. Use NetlistElement.set_name() instead. Modifying this variable might lead to inconsistencies later on.
metadata
class-attribute
instance-attribute
¶
metadata: MetadataMixin = MetadataMixin()
Metadata of a netlist element.
Can be user-defined, or e. g. by Yosys (such as src for the HDL source).
Is also grouped by categories, i.e. all metadata from Yosys can be accessed via Module.metadata["yosys"],
or via Module.metadata.yosys, which both return a dictionary of all metadata.
Read the documentation of MetaDataMixin for more information.
path
property
¶
path: InstancePath
Returns the InstancePath of the netlist element.
The InstancePath object is constructed using the element's type and its raw hierarchical path.
Returns:
-
InstancePath(InstancePath) –The hierarchical path of the netlist element.
hierarchy_level
property
¶
hierarchy_level: int
The level of hierarchy of the element in the design.
The hierarchy level is the number of separators in the element's raw path. For example, a top-level instance has a hierarchy level of 0, while a direct submodule instance has a hierarchy level of 1.
Returns:
-
int(int) –The hierarchy level of the element.
circuit
property
¶
The circuit object to which this netlist element belongs to.
- For a module, returns the circuit to which the module belongs.
- For any other netlist element, recursively returns the circuit of the parent, which ultimately leads to a module, to which the netlist element belongs.
Raises:
-
ParentNotFoundError–If a parent cannot be resolved somewhere in the hierarchical chain.
-
ObjectNotFoundError–If for a module no circuit is set.
has_circuit
property
¶
has_circuit: bool
Whether this netlist element has a defined circuit it belongs to.
Tries to access self.circuit and returns whether the call was successful.
Can be used instead of a try-except clause around the call to NetlistElement.circuit.
locked
property
¶
locked: bool
True if this NetlistElement instance is locked (i.e. it is currently structurally unchangeable), False if it is mutable.
Can be changed via NetlistElement.change_mutability(bool).
Immutability is used to prevent accidental changes to the design.
is_placeholder_instance
property
¶
is_placeholder_instance: bool
A placeholder represents an element that does not have a specific path.
True if this NetlistElement instance represents a placeholder, False otherwise.
can_carry_signal
property
¶
can_carry_signal: bool
Whether this exact object is able to receive, pass or hold signal values.
True for ports (as they drive or receive values) and wires (as they pass the signals
from driver to load ports) as well as their respective segments.
False for instances and modules.
instance_type
class-attribute
instance-attribute
¶
instance_type: str = id_internal
Identifier for instances of this gate type.
connections
property
¶
connections: Dict[str, Dict[int, WireSegmentPath]]
A dictionary mapping port names to their corresponding connection paths.
The keys of the outer dictionary are the names of the ports.
The values are dictionaries where the keys are the port bit numbers and the values are WireSegmentPath objects.
The element paths are the paths of the wires connected to the corresponding bits of the port.
Elements with no connection (i.e. the path is None) are excluded.
In case these are needed, use Instance.get_connections() instead.
Example
{ 'port1': {1: WireSegmentPath('path/to/element1'), 2: WireSegmentPath('path/to/element2')}, 'port2': {1: WireSegmentPath('path/to/element3')} }
connection_str_paths
property
¶
Returns a dictionary mapping port names to their corresponding connection paths as strings.
The keys are the names of the ports. The values are dictionaries where the keys are the port bit numbers and the values are the raw string paths.
Example
{ 'port1': {0: 'path.to.element1', 1: 'path.to.element2'}, 'port2': {0: 'path.to.element3'} }
ports
property
¶
ports: CustomDict[str, Port[Instance]]
A dictionary mapping port names to their corresponding Port objects.
The keys of the dictionary are the names of the ports. The values are Port objects representing the ports.
Returns:
-
CustomDict[str, Port[Instance]]–A dictionary mapping port names to their corresponding Port objects.
input_ports
property
¶
output_ports
property
¶
has_unconnected_port_segments
property
¶
has_unconnected_port_segments: bool
Returns True if the instance has at least one unconnected port segment.
signals
property
¶
A dictionary with all signals currently present on all ports of this instance.
is_blackbox
property
¶
is_blackbox: bool
Flag indicating whether the instance represents neither a primitive element nor a module instance.
If True, this instance does not have a module definition, and is also not a basic component (i.e. a primitive gate instance) from the internal gate library, such as a gate or flip-flop.
verilog
property
¶
verilog: str
Generates the Verilog code for this instance.
This property uses the verilog_template to generate the actual Verilog code by replacing the placeholders with
the instance type, name, and port connections. It returns a string that can be used directly in a Verilog file.
Returns:
-
str(str) –The generated Verilog code.
is_sequential
property
¶
is_sequential: bool
Whether instances of this gate are considered sequential gates.
False for combinational gates, such as AND gates or arithmetic gates.
True for flip-flops and latches.
data_width
property
¶
data_width: int
The data width of this instance.
Defaults to the data width of the output port. Can be overwritten in extended classes.
In contrast to self.width, which is used for the creation and initialization,
this property is linked to the data width of the output port.
This property is useful when the data width of the output port can be changed.
set_se
¶
set_se(new_signal: SignalOrLogicLevel) -> None
change_mutability
¶
change_mutability(is_now_locked: bool, recursive: bool = False) -> Self
Change the mutability status of this instance and optionally its ports.
This method allows setting whether this instance can be modified or not.
If recursive is set to True, it also applies the same mutability status to all ports of this instance.
Parameters:
evaluate
¶
Evaluates the gate's output signal based on its input signals.
This method is called when the gate's input signals change, and it updates the gate's output signal accordingly.
all_connections
¶
all_connections(
include_unconnected: bool,
) -> Dict[str, Dict[int, WireSegmentPath]]
Returns a dictionary mapping port names to their corresponding connection paths.
The keys of the outer dictionary are the names of the ports. The values are dictionaries where the keys are the port bit numbers and the values are WireSegmentPath objects. The element paths are the paths of the wires connected to the corresponding bits of the port.
Parameters:
Returns:
-
Dict[str, Dict[int, WireSegmentPath]]–A dictionary mapping port names to their corresponding connection paths.
connect
¶
connect(
port_name: str,
ws_path: Optional[WireSegmentPath],
direction: Direction = UNKNOWN,
index: NonNegativeInt = 0,
width: PositiveInt = 1,
) -> None
Add connections to the specified port of this instance.
This method can establish multiple connections if a range of indices is provided.
Parameters:
-
(port_name¶str) –The name of the port where the connection(s) should be established.
-
(ws_path¶Optional[WireSegmentPath]) –The path of the wire segment that will be connected to this port. If None, the associated port (segment) remains unconnected.
-
(direction¶Direction, default:UNKNOWN) –The direction of the port. Defaults to Direction.UNKNOWN.
-
(index¶NonNegativeInt, default:0) –The starting bit index within the port for these connections. Defaults to 0.
-
(width¶PositiveInt, default:1) –The number of consecutive bits in the port that should be connected. Defaults to 1.
Raises:
-
ObjectLockedError–If this instance is currently locked, and no connection can be made.
disconnect
¶
Remove an existing connection from a specified port of this instance.
This method disconnects the wire segment at the given index within the specified port_name.
Parameters:
-
(port_name¶str) –The name of the port where the connection should be removed.
-
(index¶Optional[int], default:None) –The bit index within the port for this disconnection. Defaults to None, which completely disconnects the port.
Raises:
-
ObjectLockedError–If this instance is locked.
-
ObjectNotFoundError–If no port exists with the given name.
get_connection
¶
get_connection(
port_name: str, index: Optional[NonNegativeInt] = None
) -> Union[WireSegmentPath, Dict[int, WireSegmentPath], None]
Retrieve the connection path associated with a specified port and bit index.
If index is provided, this method returns the wire segment path connected to that specific bit within the port.
Otherwise, it returns all connections for the given port as a dictionary mapping indices to WireSegmentPaths.
Parameters:
-
(port_name¶str) –The name of the port for which to retrieve the connection(s).
-
(index¶Optional[NonNegativeInt], default:None) –The bit index within the port. Defaults to None.
Returns:
-
Union[WireSegmentPath, Dict[int, WireSegmentPath], None]–Union[WireSegmentPath, Dict[int, WireSegmentPath]]: Either a single WireSegmentPath or a dictionary mapping indices to WireSegmentPaths.
modify_connection
¶
modify_connection(
port_name: str, ws_path: WireSegmentPath, index: NonNegativeInt = 0
) -> None
Modify an existing connection within a specified port of this instance.
This method updates the wire segment path at the given index within the specified port_name.
If the index does not exist yet, it is newly added, changing the width of the port.
Parameters:
-
(port_name¶str) –The name of the port where the connection should be modified.
-
(ws_path¶WireSegmentPath) –The new wire segment path for this connection.
-
(index¶NonNegativeInt, default:0) –The bit index within the port. Defaults to 0.
connect_modify
¶
connect_modify(
port_name: str,
ws_path: WireSegmentPath,
direction: Direction = UNKNOWN,
index: NonNegativeInt = 0,
width: PositiveInt = 1,
) -> None
Add a new connection or modify an existing one within the specified port of this instance.
If the connection already exists at the given index and port_name, it will be modified.
Otherwise, a new connection is added.
Parameters:
-
(port_name¶str) –The name of the port where the connection should be established or updated.
-
(ws_path¶WireSegmentPath) –The path of the wire segment that will be connected to this port.
-
(direction¶Direction, default:UNKNOWN) –The direction of the port. Defaults to Direction.UNKNOWN.
-
(index¶NonNegativeInt, default:0) –The starting bit index within the port for these connections. Defaults to 0.
-
(width¶PositiveInt, default:1) –The number of consecutive bits in the port that should be connected. Defaults to 1.
tie_port
¶
Set a constant signal value for the specified port and bit index.
If the specified port does not exist, an error message is logged and the function returns False. Otherwise, the method tries to set the constant signal value for that port and returns True if successful.
Does not work for instance output ports, as they are always driven by their parent instances.
Parameters:
-
(name¶str) –The name of the port.
-
(index¶NonNegativeInt) –The bit index within the port.
-
(sig_value¶LogicLevel) –The constant signal value to be set ('0', '1', or 'Z').
Raises:
-
ObjectNotFoundError–If no such port or port segment exists.
-
AlreadyConnectedError–(raised by: PortSegment.tie_signal) If this segment is belongs to a load port and is already connected to a wire, from which it receives its value.
-
InvalidDirectionError–(raised by: PortSegment.tie_signal) If this port segment belongs to an instance output port, which is driven by the instance inputs and the instance's internal logic.
-
InvalidSignalError–(raised by: PortSegment.tie_signal) If an invalid value is provided.
has_tied_ports
¶
has_tied_ports() -> bool
Checks the ports of this instance whether any are tied to constant values.
Instance ports can be tied to a constant value for several reasons, but if all input or all output ports are tied, the instance is rendered useless.
Returns:
-
bool(bool) –True if any ports of this instance are tied to constant values. False otherwise, i.e. all ports are connected to wires.
has_tied_inputs
¶
has_tied_inputs() -> bool
Checks the input ports of this instance whether any are tied to constant values.
Instance ports can be tied to a constant value for several reasons, but if all input or all output ports are tied, the instance is rendered useless.
Returns:
-
bool(bool) –True if any input ports of this instance are tied to constant values. False otherwise, i.e. all input ports are connected to wires.
has_tied_outputs
¶
has_tied_outputs() -> bool
Checks the output ports of this instance whether any are tied to constant values.
Instance ports can be tied to a constant value for several reasons, but if all input or all output ports are tied, the instance is rendered useless.
Returns:
-
bool(bool) –True if any output ports of this instance are tied to constant values. False otherwise, i.e. all output ports are connected to wires.
update_signedness
¶
split
¶
Performs a bit-wise split on this instance.
If this instance supports splitting and has a data width of n bit, this method splits this instance into n 1-bit instances. This works for instances, where each output bit depends only on the corresponding input bit(s), e.g. an AND gate or a D-Flipflop.
Returns:
-
Dict[NonNegativeInt, Self]–Dict[int, Instance]: An n-bit large dictionary, where each key conforms to a bit of the original instance, and each value is an 1-bit instance representing the corresponding "slice" of the original instance.
Raises:
-
SplittingUnsupportedError–If this instance does not support splitting. Happens for any gate or instance whose behavior depends on the whole bus, and splitting would make it lose its meaning.
p2v
¶
Converts a Port object to its corresponding Verilog structure by using the connected wire segments.
This method takes the connected wire segments of a Port object and converts them to their corresponding Verilog signal structure (p2ws2v -> Port to WireSegment to Verilog signal syntax). The method requires that the currently selected module matches the module of the Port object, which is derived from the design path of the Port object. For each segment of the port, it checks whether a corresponding connected wire segment exists in the current module. If the port is set to a constant, the corresponding constant wire segment placeholder is used instead. Port segments can be excluded from the conversion by providing a list of indices, indicating which segments should be excluded from the conversion (e.g. segments that are known to be unconnected).
Parameters:
-
(port¶Port) –The Port object to convert.
-
(exclude_indices¶List[int], default:None) –A list of indices to exclude from the conversion. Defaults to an empty list.
Returns:
-
str(str) –The Verilog signal structure as a string.
Raises:
-
AttributeError–If the currently selected module does not match the module of the port.