Components¶
All components are in digsim.circuit.components. They are created with the circuit
as their first argument, and an optional name:
from digsim.circuit import Circuit
from digsim.circuit.components import AND
circuit = Circuit()
gate = AND(circuit, "gate", ports=3)
The base class of all components, with the methods they share, is Component.
Inputs¶
PushButton
¶
PushButton(circuit: Circuit, name: str | None = None)
Bases: CallbackComponent
A push button. Its output is 1 while it is pushed, and 0 otherwise.
Ports
O: Output.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
circuit
|
Circuit
|
The circuit to add the component to. |
required |
name
|
str | None
|
The component name, the class name if not given. |
None
|
OnOffSwitch
¶
OnOffSwitch(circuit: Circuit, name: str | None = None)
Bases: CallbackComponent
A switch that stays on or off. Its output is 1 while it is on.
Ports
O: Output.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
circuit
|
Circuit
|
The circuit to add the component to. |
required |
name
|
str | None
|
The component name, the class name if not given. |
None
|
DipSwitch
¶
DipSwitch(
circuit: Circuit, name: str | None = None, bits: int = 8
)
Bases: CallbackComponent
A row of switches, with one output for each switch.
Ports
0, 1, ...: Outputs, one for each switch, numbered from 0.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
circuit
|
Circuit
|
The circuit to add the component to. |
required |
name
|
str | None
|
The component name, the class name if not given. |
None
|
bits
|
int
|
The number of switches, 2 to 8. |
8
|
Clock
¶
Clock(
circuit: Circuit,
name: str | None = None,
frequency: int = 1,
)
Bases: CallbackComponent
A clock, its output is a square wave. It starts low at init(), and rises half a period later. A test bench can drive a circuit by its edges, see Circuit.run_until_edge().
Ports
O: Output.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
circuit
|
Circuit
|
The circuit to add the component to. |
required |
name
|
str | None
|
The component name, the class name if not given. |
None
|
frequency
|
int
|
The frequency in Hz. |
1
|
half_period_ns
property
¶
half_period_ns: int
The time between two edges of the output, in nanoseconds
next_edge_ns
¶
next_edge_ns(rising: bool = True) -> int
The time of the next rising (or falling) edge of the output, in nanoseconds.
Raises:
| Type | Description |
|---|---|
DigsimException
|
If the circuit isn't initialized (the clock doesn't run). |
StaticValue
¶
StaticValue(
circuit: Circuit,
name: str | None = None,
width: int = 1,
value: int = 0,
radix: str = "dec",
)
Bases: Component
A constant value on a wire or a bus.
Ports
O: Output, width bits.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
circuit
|
Circuit
|
The circuit to add the component to. |
required |
name
|
str | None
|
The component name, the class name if not given. |
None
|
width
|
int
|
The number of bits, 1 to 16. |
1
|
value
|
int
|
The value. |
0
|
radix
|
str
|
How the value is shown in the GUI: "dec", "hex" or "bin". |
'dec'
|
text
¶
text() -> str
The value as text, in the radix: hexadecimal (0x) and binary (0b) with all digits for the width. A single bit is 0 or 1.
Outputs¶
Led
¶
Led(
circuit: Circuit,
name: str | None = None,
callback: Callable[[Component], None] | None = None,
color: str = "red",
)
Bases: CallbackComponent
An LED, lit while its input is 1.
Ports
I: Input.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
circuit
|
Circuit
|
The circuit to add the component to. |
required |
name
|
str | None
|
The component name, the class name if not given. |
None
|
callback
|
Callable[[Component], None] | None
|
Called with the component when its input changes. |
None
|
color
|
str
|
The color of the LED in the GUI: "red", "green", "blue" or "yellow". |
'red'
|
LedBar
¶
LedBar(
circuit: Circuit,
name: str | None = None,
width: int = 8,
color: str = "red",
)
Bases: CallbackComponent
A column of LEDs, with one input for each LED.
Ports
0, 1, ...: Inputs, one for each LED, numbered from 0.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
circuit
|
Circuit
|
The circuit to add the component to. |
required |
name
|
str | None
|
The component name, the class name if not given. |
None
|
width
|
int
|
The number of LEDs, 1 to 16. |
8
|
color
|
str
|
The color of the LEDs in the GUI: "red", "green", "blue" or "yellow". |
'red'
|
HexDigit
¶
HexDigit(
circuit: Circuit,
name: str | None = None,
digits: int = 1,
dot: bool = True,
)
Bases: CallbackComponent
Hexadecimal digits that show the value of a bus.
Ports
val: Input, 4 bits for each digit. The first digit shows the most significant bits.
dot: Input, one bit for each decimal point (only with dot).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
circuit
|
Circuit
|
The circuit to add the component to. |
required |
name
|
str | None
|
The component name, the class name if not given. |
None
|
digits
|
int
|
The number of digits, 1 to 8. |
1
|
dot
|
bool
|
Add the decimal point input. |
True
|
SevenSegment
¶
SevenSegment(circuit: Circuit, name: str | None = None)
Bases: CallbackComponent
A 7-segment display, with an input for each segment.
Ports
A, B, ..., G: Inputs, one for each segment. dot: Input for the decimal point.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
circuit
|
Circuit
|
The circuit to add the component to. |
required |
name
|
str | None
|
The component name, the class name if not given. |
None
|
HD44780
¶
HD44780(
circuit: Circuit,
name: str | None = None,
backlight: str = "green",
)
Bases: CallbackComponent
A 16x2 character LCD module with an HD44780 controller and a backlight, programmed like the real one, with the A00 (English and Japanese) character ROM.
The controller reads RS and RW, and the data on D0 to D7, on the falling edge of E. A write with RS 0 is an instruction and with RS 1 data for the DDRAM or the CGRAM. While E is 1 with RW 1 it drives the data pins: with RS 0 the busy flag (bit 7) and the address counter, with RS 1 the data at the address. An instruction is done at once, so the busy flag is always 0.
After a 4-bit Function Set (DL 0) the data is on D4 to D7, the high nibble first, and D0 to D3 are not used. The data pins that nothing drives are 1, the controller has pull-ups on them.
It starts like after power on: in 8-bit mode with one line (only the first line of the display is shown), the display off and the DDRAM filled with spaces. Function Set with N 1 selects two lines.
Ports
RS: Register select input, 0 for an instruction and 1 for data. RW: Read/write input, 0 to write and 1 to read. E: Enable input, a write is done on its falling edge. D0, D1, ..., D7: Data inout ports.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
circuit
|
Circuit
|
The circuit to add the component to. |
required |
name
|
str | None
|
The component name, the class name if not given. |
None
|
backlight
|
str
|
The color of the backlight in the GUI: "green" (yellow-green with dark characters), "blue" (white characters), "amber", "white" or "red". |
'green'
|
codes
¶
codes() -> list[list[int | None]]
The character code shown in each place of the display, a list for each line. In one-line mode the second line is None.
lines
¶
lines() -> list[str]
The text of the two lines of the display, 16 characters each, also while the display is off. A character of the CGRAM (codes 0 to 15) is chr(code), and a code without a character is a space.
rom_glyph
staticmethod
¶
rom_glyph(code: int) -> tuple[int, ...]
The glyph of a code in the character ROM, see glyph(). Codes 0 to 15 are blank.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
code
|
int
|
The character code, 0 to 255. |
required |
glyph
¶
glyph(code: int) -> tuple[int, ...]
The 8 rows of 5 pixels of a character, the top row first, the left pixel in bit 4. Codes 0 to 15 are the 8 characters of the CGRAM (twice).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
code
|
int
|
The character code, 0 to 255. |
required |
pixels
¶
pixels() -> list[list[tuple[int, ...]]]
What the display shows: the rows of pixels of each character (see glyph()), a list for each line, with the cursor. Nothing is shown while the display is off.
read_ddram
¶
read_ddram(address: int) -> int
Return the character code at a DDRAM address.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
address
|
int
|
The address, 0x00 to 0x27 and 0x40 to 0x67 in two-line mode, otherwise 0x00 to 0x4F. |
required |
Buzzer
¶
Buzzer(
circuit: Circuit,
name: str | None = None,
tone: str = "A",
)
Bases: CallbackComponent
A buzzer, plays a tone in the GUI while its input is 1.
Ports
I: Input.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
circuit
|
Circuit
|
The circuit to add the component to. |
required |
name
|
str | None
|
The component name, the class name if not given. |
None
|
tone
|
str
|
The note, "A" to "G". |
'A'
|
LogicAnalyzer
¶
LogicAnalyzer(
circuit: Circuit,
name: str | None = None,
sample_rate: int = 100,
)
Bases: CallbackComponent
A logic analyzer, keeps the last 100 samples of each input to show them in the GUI. The samples are cleared when the circuit is initialized (a reset in the GUI). A sample is the value an input had just before the time of the sample: an input that changes at the same time, like on the edge of a clock, is sampled before the change, whatever the order of the events at that time.
Ports
A, B, ..., H: Inputs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
circuit
|
Circuit
|
The circuit to add the component to. |
required |
name
|
str | None
|
The component name, the class name if not given. |
None
|
sample_rate
|
int
|
The samples per second. |
100
|
Probe
¶
Probe(
circuit: Circuit,
name: str | None = None,
width: int = 1,
radix: str = "hex",
)
Bases: CallbackComponent
Shows the value of a wire or a bus, in the GUI. In Python, its value is I.value, or
text() for the value as the GUI shows it.
Ports
I: Input, width bits.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
circuit
|
Circuit
|
The circuit to add the component to. |
required |
name
|
str | None
|
The component name, the class name if not given. |
None
|
width
|
int
|
The number of bits, 1 to 32. |
1
|
radix
|
str
|
How the value is shown: "hex", "dec" or "bin". |
'hex'
|
Gates¶
AND
¶
AND(
circuit: Circuit,
name: str | None = None,
ports: int = 2,
)
Bases: ConfigPortsComponent
An AND gate: Y is 1 when all inputs are 1.
Ports
A, B, ...: Inputs, one for each of ports, named from A.
Y: Output.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
circuit
|
Circuit
|
The circuit to add the component to. |
required |
name
|
str | None
|
The component name, the class name if not given. |
None
|
ports
|
int
|
The number of inputs, 2 to 8. |
2
|
OR
¶
OR(
circuit: Circuit,
name: str | None = None,
ports: int = 2,
)
Bases: ConfigPortsComponent
An OR gate: Y is 1 when at least one input is 1.
Ports
A, B, ...: Inputs, one for each of ports, named from A.
Y: Output.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
circuit
|
Circuit
|
The circuit to add the component to. |
required |
name
|
str | None
|
The component name, the class name if not given. |
None
|
ports
|
int
|
The number of inputs, 2 to 8. |
2
|
XOR
¶
XOR(
circuit: Circuit,
name: str | None = None,
ports: int = 2,
)
Bases: ConfigPortsComponent
An XOR gate: Y is 1 when an odd number of inputs are 1.
Ports
A, B, ...: Inputs, one for each of ports, named from A.
Y: Output.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
circuit
|
Circuit
|
The circuit to add the component to. |
required |
name
|
str | None
|
The component name, the class name if not given. |
None
|
ports
|
int
|
The number of inputs, 2 to 8. |
2
|
XNOR
¶
XNOR(
circuit: Circuit,
name: str | None = None,
ports: int = 2,
)
Bases: ConfigPortsComponent
An XNOR gate: Y is 1 when an even number of inputs are 1.
Ports
A, B, ...: Inputs, one for each of ports, named from A.
Y: Output.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
circuit
|
Circuit
|
The circuit to add the component to. |
required |
name
|
str | None
|
The component name, the class name if not given. |
None
|
ports
|
int
|
The number of inputs, 2 to 8. |
2
|
NAND
¶
NAND(
circuit: Circuit,
name: str | None = None,
ports: int = 2,
)
Bases: ConfigPortsComponent
A NAND gate: Y is 0 when all inputs are 1.
Ports
A, B, ...: Inputs, one for each of ports, named from A.
Y: Output.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
circuit
|
Circuit
|
The circuit to add the component to. |
required |
name
|
str | None
|
The component name, the class name if not given. |
None
|
ports
|
int
|
The number of inputs, 2 to 8. |
2
|
NOR
¶
NOR(
circuit: Circuit,
name: str | None = None,
ports: int = 2,
)
Bases: ConfigPortsComponent
A NOR gate: Y is 0 when at least one input is 1.
Ports
A, B, ...: Inputs, one for each of ports, named from A.
Y: Output.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
circuit
|
Circuit
|
The circuit to add the component to. |
required |
name
|
str | None
|
The component name, the class name if not given. |
None
|
ports
|
int
|
The number of inputs, 2 to 8. |
2
|
MUX
¶
MUX(
circuit: Circuit,
name: str | None = None,
ports: int = 2,
width: int = 1,
)
Bases: Component
A multiplexer: Y is the input that S selects.
Ports
A, B, ...: Inputs, one for each of ports, width bits.
S: Select input, 0 selects A, 1 selects B and so on.
Y: Output, width bits.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
circuit
|
Circuit
|
The circuit to add the component to. |
required |
name
|
str | None
|
The component name, the class name if not given. |
None
|
ports
|
int
|
The number of inputs, 2, 4, 8 or 16. |
2
|
width
|
int
|
The number of bits of each input. |
1
|
Decoder
¶
Decoder(
circuit: Circuit,
name: str | None = None,
select: int = 2,
enable: bool = False,
)
Bases: _Selector
A decoder: the output that S selects is 1, the others are 0.
Ports
S: Select input, select bits.
EN: Enable input, all outputs are 0 while it is 0 (only with enable).
Y0, Y1, ...: Outputs, one for each value of S.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
circuit
|
Circuit
|
The circuit to add the component to. |
required |
name
|
str | None
|
The component name, the class name if not given. |
None
|
select
|
int
|
The number of select bits, 1 to 4 (2 to 16 outputs). |
2
|
enable
|
bool
|
Add the enable input EN. |
False
|
Demultiplexer
¶
Demultiplexer(
circuit: Circuit,
name: str | None = None,
select: int = 1,
width: int = 1,
)
Bases: _Selector
A demultiplexer: the output that S selects is I, the others are 0.
Ports
I: Input, width bits.
S: Select input, select bits.
Y0, Y1, ...: Outputs, width bits, one for each value of S.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
circuit
|
Circuit
|
The circuit to add the component to. |
required |
name
|
str | None
|
The component name, the class name if not given. |
None
|
select
|
int
|
The number of select bits, 1 to 4 (2 to 16 outputs). |
1
|
width
|
int
|
The number of bits of I and the outputs, 1 to 32. |
1
|
Flip-flops¶
DFF
¶
DFF(
circuit: Circuit,
name: str | None = None,
async_reset: bool = False,
clock_enable: bool = False,
width: int = 1,
)
Bases: Component
A D flip-flop: Q takes the value of D on a rising edge of C.
Ports
D: Data input, width bits.
R: Reset input, 1 sets Q to 0 at once (only with async_reset).
E: Clock enable input, the clock edge is ignored while it is 0 (only with clock_enable).
C: Clock input.
Q: Output, width bits.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
circuit
|
Circuit
|
The circuit to add the component to. |
required |
name
|
str | None
|
The component name, the class name if not given. |
None
|
async_reset
|
bool
|
Add the reset input R. |
False
|
clock_enable
|
bool
|
Add the clock enable input E. |
False
|
width
|
int
|
The number of bits. |
1
|
SR
¶
SR(circuit: Circuit, name: str | None = None)
Bases: MultiComponent
An SR latch built from two NOR gates: S sets Q to 1 and R resets it to 0.
Ports
S: Set input. R: Reset input. Q: Output. nQ: Inverted output.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
circuit
|
Circuit
|
The circuit to add the component to. |
required |
name
|
str | None
|
The component name, the class name if not given. |
None
|
SRFF
¶
SRFF(circuit: Circuit, name: str | None = None)
Bases: Component
An SR flip-flop: S sets Q to 1 and R resets it to 0, without a clock. R wins when both are 1.
Ports
S: Set input. R: Reset input. Q: Output.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
circuit
|
Circuit
|
The circuit to add the component to. |
required |
name
|
str | None
|
The component name, the class name if not given. |
None
|
ClockedSRFF
¶
ClockedSRFF(circuit: Circuit, name: str | None = None)
Bases: Component
A clocked SR flip-flop: on a rising clock edge, S sets Q to 1 and R resets it to 0. R wins when both are 1.
Ports
S: Set input. C: Clock input. R: Reset input. Q: Output.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
circuit
|
Circuit
|
The circuit to add the component to. |
required |
name
|
str | None
|
The component name, the class name if not given. |
None
|
ClockedJKFF
¶
ClockedJKFF(circuit: Circuit, name: str | None = None)
Bases: Component
A JK flip-flop: on a rising clock edge, J sets Q to 1, K resets it to 0, and both toggle it.
Ports
J: Input. C: Clock input. K: Input. Q: Output.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
circuit
|
Circuit
|
The circuit to add the component to. |
required |
name
|
str | None
|
The component name, the class name if not given. |
None
|
FlipFlop
¶
FlipFlop(
circuit: Circuit,
name: str | None = None,
display_name: str | None = None,
)
Bases: Component
Lets the GUI choose a flip-flop type. In Python, create SRFF, ClockedSRFF, ClockedJKFF or ClockedTFF directly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
circuit
|
Circuit
|
The circuit to add the component to. |
required |
name
|
str | None
|
The component name, the class name if not given. |
None
|
Counter
¶
Counter(
circuit: Circuit,
name: str | None = None,
width: int = 4,
enable: bool = False,
up_down: bool = False,
load: bool = False,
)
Bases: Component
A binary counter: Q counts up (or down) by one on each rising edge of clk, and wraps around. It starts at 0.
Ports
clk: Clock input.
R: Reset input, 1 sets Q to 0 at once.
EN: Clock enable input, the clock edge is ignored while it is 0 (only with
enable).
UP: Direction input, 1 counts up and 0 down (only with up_down).
LD: Load input, 1 loads D on the clock edge instead of counting (only with load).
D: Data input to load, width bits (only with load).
Q: Output, width bits.
CO: Carry output, 1 at the last count before Q wraps around: the highest value
counting up, 0 counting down.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
circuit
|
Circuit
|
The circuit to add the component to. |
required |
name
|
str | None
|
The component name, the class name if not given. |
None
|
width
|
int
|
The number of bits, 1 to 32. |
4
|
enable
|
bool
|
Add the clock enable input EN. |
False
|
up_down
|
bool
|
Add the direction input UP, otherwise it counts up. |
False
|
load
|
bool
|
Add the load inputs LD and D. |
False
|
Arithmetic¶
Adder
¶
Adder(
circuit: Circuit,
name: str | None = None,
width: int = 8,
carry_in: bool = False,
)
Bases: _Arithmetic
Adds two numbers: S is A + B (+ Cin), and Cout is the carry out.
Ports
A: Input, width bits.
B: Input, width bits.
Cin: Carry input (only with carry_in).
S: Sum output, width bits.
Cout: Carry output.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
circuit
|
Circuit
|
The circuit to add the component to. |
required |
name
|
str | None
|
The component name, the class name if not given. |
None
|
width
|
int
|
The number of bits, 1 to 32. |
8
|
carry_in
|
bool
|
Add the carry input Cin, to chain adders. |
False
|
Subtractor
¶
Subtractor(
circuit: Circuit,
name: str | None = None,
width: int = 8,
borrow_in: bool = False,
)
Bases: _Arithmetic
Subtracts two numbers: D is A - B (- Bin), and Bout is 1 when it borrows (A is smaller).
Ports
A: Input, width bits.
B: Input, width bits.
Bin: Borrow input (only with borrow_in).
D: Difference output, width bits.
Bout: Borrow output.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
circuit
|
Circuit
|
The circuit to add the component to. |
required |
name
|
str | None
|
The component name, the class name if not given. |
None
|
width
|
int
|
The number of bits, 1 to 32. |
8
|
borrow_in
|
bool
|
Add the borrow input Bin, to chain subtractors. |
False
|
Multiplier
¶
Multiplier(
circuit: Circuit,
name: str | None = None,
width: int = 8,
)
Bases: _Arithmetic
Multiplies two numbers: P is A * B, twice as wide as them.
Ports
A: Input, width bits.
B: Input, width bits.
P: Product output, 2 * width bits.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
circuit
|
Circuit
|
The circuit to add the component to. |
required |
name
|
str | None
|
The component name, the class name if not given. |
None
|
width
|
int
|
The number of bits of A and B, 1 to 16. |
8
|
Comparator
¶
Comparator(
circuit: Circuit,
name: str | None = None,
width: int = 8,
signed: bool = False,
)
Bases: _Arithmetic
Compares two numbers: LT is 1 when A < B, EQ when A = B and GT when A > B.
Ports
A: Input, width bits.
B: Input, width bits.
LT: Output, A is less than B.
EQ: Output, A equals B.
GT: Output, A is greater than B.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
circuit
|
Circuit
|
The circuit to add the component to. |
required |
name
|
str | None
|
The component name, the class name if not given. |
None
|
width
|
int
|
The number of bits, 1 to 32. |
8
|
signed
|
bool
|
Compare A and B as two's complement numbers. |
False
|
Negator
¶
Negator(
circuit: Circuit,
name: str | None = None,
width: int = 8,
)
Bases: _Arithmetic
Negates a number: Y is -A, in two's complement.
Ports
A: Input, width bits.
Y: Output, width bits.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
circuit
|
Circuit
|
The circuit to add the component to. |
required |
name
|
str | None
|
The component name, the class name if not given. |
None
|
width
|
int
|
The number of bits, 1 to 32. |
8
|
Buses and labels¶
Bus2Wires
¶
Bus2Wires(
circuit: Circuit,
name: str | None = None,
width: int = 1,
enable: list[int] | None = None,
)
Bases: Component
Splits a bus into single-bit wires.
Ports
bus: Input, width bits.
bus_0, bus_1, ...: Outputs, one for each enabled bit. bus_0 is the least significant bit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
circuit
|
Circuit
|
The circuit to add the component to. |
required |
name
|
str | None
|
The component name, the class name if not given. |
None
|
width
|
int
|
The number of bits. |
1
|
enable
|
list[int] | None
|
The bits to have outputs for, all if None. |
None
|
Wires2Bus
¶
Wires2Bus(
circuit: Circuit,
name: str | None = None,
width: int = 1,
)
Bases: Component
Merges single-bit wires into a bus.
Ports
bus_0, bus_1, ...: Inputs, one for each bit. bus_0 is the least significant bit.
bus: Output, width bits.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
circuit
|
Circuit
|
The circuit to add the component to. |
required |
name
|
str | None
|
The component name, the class name if not given. |
None
|
width
|
int
|
The number of bits. |
1
|
Splitter
¶
Splitter(
circuit: Circuit,
name: str | None = None,
groups: list[int] | None = None,
direction: str = "split",
)
Bases: Component
Splits a bus into groups of bits, like [7:4] and [3:0], or merges groups into a bus.
Ports
bus: The bus, sum(groups) bits. An input when splitting, an output when merging.
bus_3_0, bus_7_4, ...: The groups, from bit 0. A group of one bit is bus_5.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
circuit
|
Circuit
|
The circuit to add the component to. |
required |
name
|
str | None
|
The component name, the class name if not given. |
None
|
groups
|
list[int] | None
|
The width of each group, the first from bit 0. [4, 4] splits 8 bits into bus_3_0 and bus_7_4. |
None
|
direction
|
str
|
"split" (bus in, groups out) or "merge" (groups in, bus out). |
'split'
|
TristateBuffer
¶
TristateBuffer(
circuit: Circuit,
name: str | None = None,
width: int = 1,
active_low: bool = False,
)
Bases: Component
A tristate buffer: drives its input to its output while enabled, and does not drive it at all ("Z", high impedance) otherwise. Several outputs can drive the same wire, like the outputs of the chips on a bus. The one that is enabled drives it, and if more than one drives different values, the value is undefined ("X").
Ports
A: Input, width bits.
OE: Output enable input, active high (or low, with active_low=True).
Y: Tristate output, width bits.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
circuit
|
Circuit
|
The circuit to add the component to. |
required |
name
|
str | None
|
The component name, the class name if not given. |
None
|
width
|
int
|
The number of bits. |
1
|
active_low
|
bool
|
The output is enabled when OE is low. |
False
|
LabelWireIn
¶
LabelWireIn(
circuit: Circuit,
name: str | None = None,
label: str = "",
width: int = 1,
)
Bases: Component
A Wire Sink: passes its input to every LabelWireOut with the same label, without a drawn wire.
Ports
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
circuit
|
Circuit
|
The circuit to add the component to. |
required |
name
|
str | None
|
The component name, the class name if not given. |
None
|
label
|
str
|
The label. |
''
|
width
|
int
|
The number of bits. |
1
|
LabelWireOut
¶
LabelWireOut(
circuit: Circuit,
name: str | None = None,
label: str = "",
width: int = 1,
)
Bases: Component
A Wire Source: outputs the signal of the LabelWireIn with the same label.
Ports
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
circuit
|
Circuit
|
The circuit to add the component to. |
required |
name
|
str | None
|
The component name, the class name if not given. |
None
|
label
|
str
|
The label. |
''
|
width
|
int
|
The number of bits, used when there is no LabelWireIn with the label yet. |
1
|
Circuit ports¶
CircuitInput
¶
CircuitInput(
circuit: Circuit,
name: str | None = None,
label: str = "in",
width: int = 1,
)
Bases: CircuitPort
An input of the circuit. In the circuit itself it drives a value, 0 at the start, that set_value() sets. In a subcircuit, its input with the label takes its place.
Ports
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
circuit
|
Circuit
|
The circuit to add the component to. |
required |
name
|
str | None
|
The component name, the class name if not given. |
None
|
label
|
str
|
The name of the port. |
'in'
|
width
|
int
|
The number of bits, 1 to 32. |
1
|
CircuitOutput
¶
CircuitOutput(
circuit: Circuit,
name: str | None = None,
label: str = "out",
width: int = 1,
)
Bases: CircuitPort
An output of the circuit, it shows the value of its input. In a subcircuit, its output with the label takes its place.
Ports
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
circuit
|
Circuit
|
The circuit to add the component to. |
required |
name
|
str | None
|
The component name, the class name if not given. |
None
|
label
|
str
|
The name of the port. |
'out'
|
width
|
int
|
The number of bits, 1 to 32. |
1
|
CircuitInOut
¶
CircuitInOut(
circuit: Circuit,
name: str | None = None,
label: str = "io",
width: int = 1,
)
Bases: CircuitPort
An inout port of the circuit: it shows the value of its wire, and drives a value that set_value() sets, or nothing ("Z") as at the start. In a subcircuit, its inout port with the label takes its place.
Ports
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
circuit
|
Circuit
|
The circuit to add the component to. |
required |
name
|
str | None
|
The component name, the class name if not given. |
None
|
label
|
str
|
The name of the port. |
'io'
|
width
|
int
|
The number of bits, 1 to 32. |
1
|
value
property
¶
value: VALUE_TYPE
The value of the wire, what it drives resolved with what the others drive
set_value
¶
set_value(value: int | str) -> None
Drive a value on the wire, or nothing ("Z").
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
int | str
|
The value, 0 to 2^width - 1, or "Z". |
required |
SubCircuit
¶
SubCircuit(
circuit: Circuit,
path: str | Path,
name: str | None = None,
)
Bases: MultiComponent
Another circuit file used as a component. Its ports are the circuit ports of the file (CircuitInput, CircuitOutput and CircuitInOut): an input, an output or an inout port for each, named and as wide as it, in the order of the circuit ports in the file from top to bottom. The components of the file are parts of the subcircuit (a VCD file names them "subcircuit.component"), connected like in the file, with the ports of the subcircuit where the circuit ports were. A subcircuit in the file is loaded the same way, its file relative to that file.
Ports
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
circuit
|
Circuit
|
The circuit to add the component to. |
required |
path
|
str | Path
|
The circuit file. |
required |
name
|
str | None
|
The component name, the name of the file without .circuit if not given. |
None
|
layout
¶
layout() -> AppFileDataClass
The circuit file as it was when the parts were built: positions and lines
has_layout
¶
has_layout() -> bool
Is it from a GUI file, with positions and lines (not a circuit made in Python)
parts
¶
parts() -> list[Component]
The parts, the components of the file (and the bridges of the inout ports)
part
¶
part(name: str) -> Component | None
A part by its name in the file, it can have another name in the circuit
file_ports
¶
file_ports() -> list[FilePort]
The circuit ports of the file, in the order of the ports
outer_port
¶
outer_port(label: str) -> Port
The port of the subcircuit for a circuit port of the file
file_interface
staticmethod
¶
file_interface(
path: str | Path,
) -> dict[str, tuple[str, int]]
The ports of a subcircuit of a file, label: (direction, width)
reload
¶
reload() -> None
Build the parts again from the file, which can have changed. Its circuit ports must be the same (see interface()), they can be in another order in the file or be other components: the ports keep their order.
Raises:
| Type | Description |
|---|---|
SubCircuitException
|
If the file can't be read, or its ports changed. |
Verilog and ICs¶
VerilogComponent
¶
VerilogComponent(
circuit: Circuit,
path: str | Path | None = None,
*,
functional: bool = False,
signals: bool = False,
states: str | list[str] | None = None,
parameters: str | Mapping[str, int] | None = None,
module: str | None = None,
)
Bases: YosysComponent
A component built from a Verilog file (or a Yosys JSON netlist), named after its module. It is a YosysComponent, by a name that says what it is made from: Yosys synthesizes the Verilog into the netlist that DigSim simulates. It takes the same arguments.
Ports
The ports of the Verilog module, with the same names and widths. An inout port is a PortInOut, wire it both ways to another inout port.
Example
counter = VerilogComponent(circuit, "counter.v", functional=True)
YosysComponent
¶
YosysComponent(
circuit: Circuit,
path: str | Path | None = None,
*,
functional: bool = False,
signals: bool = False,
states: str | list[str] | None = None,
parameters: str | Mapping[str, int] | None = None,
module: str | None = None,
)
Bases: MultiComponent
A component built from a Verilog file or a Yosys JSON netlist, named after its module.
Ports
The ports of the Verilog module, with the same names and widths. An inout port is a PortInOut, wire it both ways to another inout port.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
circuit
|
Circuit
|
The circuit to add the component to. |
required |
path
|
str | Path | None
|
A Verilog file (.v, or SystemVerilog .sv) or Yosys netlist (.json) with one module, or None to create the component later with create_from_netlist(). The component is the top module of a Verilog file, the one that the other modules are a part of (they are flattened into it). |
None
|
functional
|
bool
|
Synthesize a Verilog file into functional blocks, like adders and multiplexers, instead of gates, which simulates faster. A netlist is used as it is. |
False
|
signals
|
bool
|
Make the named signals of the netlist (the wires and registers of the
Verilog) readable with signal(), and write them to the VCD file. It makes the
simulation slower. A signal with names for its values (an enum, or a signal
with the width of at least two parameters) is written as its value and, in
" |
False
|
states
|
str | list[str] | None
|
More signals (with signals=True) to write with the names of their values: signals of one bit, or with one name. A list, or names separated by commas. |
None
|
parameters
|
str | Mapping[str, int] | None
|
Values for parameters of the Verilog module instead of their defaults: a dict like {"WIDTH": 16}, or "WIDTH=16,DEPTH=4". The values are integers. A netlist file has no parameters. |
None
|
module
|
str | None
|
The module of a Verilog file to make the component from, instead of its top module: for a file with several modules that don't instantiate each other, or to make a component of a submodule. |
None
|
Raises:
| Type | Description |
|---|---|
YosysComponentException
|
If parameters or a module are given for a netlist file, the parameters can't be read, the file has no such module, or several top modules and no module is given. |
source_file
property
¶
source_file: str | None
The Verilog or netlist file of the component, None if it was made from a netlist
parameters
property
¶
parameters: dict[str, int]
The parameters of the Verilog module that have other values than their defaults
module
property
¶
module: str
The module of the Verilog file that the component was asked to be made from, "" for its top module (module_name() is the module that it is made from)
variant
property
¶
variant: YosysVariant
How the Verilog is synthesized: 'functional', the parameters and the module
set_parameters
¶
set_parameters(
parameters: str | Mapping[str, int] | None,
) -> None
Change the parameters of the Verilog module, without building the component again: for a netlist synthesized with them, that reload_from_netlist() builds it from next (or a circuit built again from the settings of its components).
Raises:
| Type | Description |
|---|---|
YosysComponentException
|
If the parameters can't be read, or the component is made from a netlist file. |
module_name
¶
module_name() -> str
The name of the module of the netlist, the name the component gets
create_from_netlist
¶
create_from_netlist(netlist_object: YosysNetlist) -> None
Create component from netlist object
reload_from_netlist
¶
reload_from_netlist(netlist_object: YosysNetlist) -> None
Reload netlist from netlist object
given_netlists
staticmethod
¶
given_netlists(
netlists: Iterable[
tuple[str | Path, YosysVariant, YosysNetlist]
],
) -> Iterator[None]
Components built in the context use given netlists instead of reading their files, like when a circuit is built again: (file, variant, netlist) for each component, in the order that the components are built. A component of a file that has no netlist left reads its file.
check_netlist
¶
check_netlist(netlist_object: YosysNetlist) -> None
Check that the component can be built from a netlist (see reload_from_netlist), before it is taken apart.
Raises:
| Type | Description |
|---|---|
YosysComponentException
|
If a cell can't be simulated, or a signal in the 'states' argument is not in the netlist. |
netlist_parts
¶
netlist_parts() -> NetlistParts
What the component is built from now, a new one when it is built again
netlist_interface
staticmethod
¶
netlist_interface(
netlist_object: YosysNetlist,
) -> dict[str, tuple[str, int]]
The ports of the (first) module of a netlist: name to (direction, width), the direction "input", "output" or "inout"
interface
¶
interface() -> dict[str, tuple[str, int]]
The ports of the component: name to (direction, width), see netlist_interface()
signal_names
¶
signal_names() -> list[str]
The named signals of the netlist, the wires and registers of the Verilog, like "PC" or "ALU.OUT" (from the ALU submodule)
signal
¶
signal(name: str) -> VALUE_TYPE
The value of a named signal, see signal_names().
Raises:
| Type | Description |
|---|---|
YosysComponentException
|
If the component was created without signals=True, or it has no signal with the name. |
deposit
¶
deposit(
name: str, value: VALUE_TYPE, bit: int | None = None
) -> None
Deposit a value on a named signal, like a Verilog deposit (vpi_put_value()): the signal and the logic it drives have the value now, and the cells that drive it keep it until they drive it again, a register until its next active clock edge, a gate until one of its inputs changes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name of the signal, see signal_names(). |
required |
value
|
VALUE_TYPE
|
The value, a number or "X". |
required |
bit
|
int | None
|
Only this bit of the signal (0 the least significant), the value 0, 1 or "X". |
None
|
Raises:
| Type | Description |
|---|---|
YosysComponentException
|
If the component was created without signals=True, or it has no signal with the name, or a bit of the signal is constant, not driven, or a port of the component (set the port instead), or the native engine is older than digsim-native 0.6.0. |
state
¶
state(name: str) -> str
The name of the value of a signal with named values (see the 'states' argument), like the state of a state machine. A value without a name is returned as a number.
Raises:
| Type | Description |
|---|---|
YosysComponentException
|
If the component was created without signals=True, or the signal has no names for its values. |
synthesize_file
staticmethod
¶
synthesize_file(
path: str | Path,
functional: bool = False,
log: list[str] | None = None,
cached: bool = True,
top: str | None = None,
parameters: Mapping[str, int] | None = None,
module: str = "",
) -> dict[str, Any]
Synthesize a module of a Verilog file into a netlist dict, as a YosysComponent does. The last netlists are kept, by the contents of the file, and a component made from the same file uses its netlist.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
The Verilog file (.v or .sv). |
required |
functional
|
bool
|
Synthesize into functional blocks, see YosysComponent. |
False
|
log
|
list[str] | None
|
A list to add the Yosys log to. |
None
|
cached
|
bool
|
Use the netlist of the last synthesis of the same file contents. With False Yosys always runs (to get its log), and the netlist is kept. |
True
|
top
|
str | None
|
The top module of the file, if it is known (see top_module()). Else the file is checked to have one. |
None
|
parameters
|
Mapping[str, int] | None
|
Values for parameters of the module, see YosysComponent. |
None
|
module
|
str
|
The module to synthesize, "" for the top module, see YosysComponent. |
''
|
Raises:
| Type | Description |
|---|---|
SynthesisException
|
If Yosys reports an error, like a syntax error. |
YosysComponentException
|
If the file has no such module, or several top modules and no module is given. |
OSError
|
If the file can't be read. |
IntegratedCircuit
¶
IntegratedCircuit(
circuit: Circuit, ic_name: str | None = None
)
Bases: YosysComponent
A classic logic chip, from the Yosys netlists in digsim/circuit/components/ic. It is named after the netlist's module, like ttl_74162.
Ports
The ports of the chip.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
circuit
|
Circuit
|
The circuit to add the component to. |
required |
ic_name
|
str | None
|
The netlist's file name without .json, like "74162" or "7448". |
None
|
available
classmethod
¶
available() -> dict[str, str]
The ICs in the IC folder, with their descriptions (empty if there is none)
Memory¶
RAM
¶
RAM(
circuit: Circuit,
name: str | None = None,
address_width: int = 8,
data_width: int = 8,
mode: str = "sync",
edge: str = "rising",
output_enable: bool = False,
path: str | Path | None = None,
load_address: int = 0,
byte_enable: bool = False,
)
Bases: _Memory
A RAM with 2^address_width words of data_width bits. It starts out with zeros, or
with the contents of a file, and is filled again from the file when the circuit is
initialized or reset.
The mode sets how the RAM is clocked:
- "sync": DataIn is written, while WE is 1, or DataOut read, while WE is 0, on the clock edge. A write leaves DataOut as it is. Like the block RAM of an FPGA.
- "async_read": DataIn is written on the clock edge while WE is 1, DataOut is the word at the address at all times.
- "async": no clock. DataIn is written while WE is 1, DataOut is the word at the address at all times. Like an asynchronous SRAM.
With byte_enable, a write only changes the bytes of the word that BE selects, like
the byte strobes of a processor's store: bit 0 of BE is bits 7:0 of the word, bit 1
bits 15:8 and so on. BE is read together with WE. A write of some of the bytes, with
the old word, DataIn or BE undefined, makes the word "X". write() always writes the
whole word.
The file is binary, with the bytes of each word in little-endian order, or text (.hex, .mem or .txt) with hexadecimal words like Verilog's $readmemh reads, also with @address lines.
Ports
clk: Clock input, not in "async" mode.
Address: Input, address_width bits.
DataIn: Input, data_width bits.
WE: Write enable input.
BE: Byte enable input, with byte_enable, one bit for each byte of a word (8 bits
of data_width, rounded up).
OE: Output enable input, with output_enable. DataOut is "Z" while it is 0.
DataOut: Output, data_width bits. Tristate with output_enable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
circuit
|
Circuit
|
The circuit to add the component to. |
required |
name
|
str | None
|
The component name, the class name if not given. |
None
|
address_width
|
int
|
The number of address bits, 1 to 20. |
8
|
data_width
|
int
|
The number of bits in a word, 1 to 32. |
8
|
mode
|
str
|
"sync", "async_read" or "async", see above. |
'sync'
|
edge
|
str
|
The clock edge, "rising" or "falling". |
'rising'
|
output_enable
|
bool
|
Add an output enable input. |
False
|
path
|
str | Path | None
|
A file with the contents to load, optional. |
None
|
load_address
|
int
|
The address to load the file at. |
0
|
byte_enable
|
bool
|
Add a byte enable input, see above. |
False
|
reload_contents
¶
reload_contents() -> None
Read the file again, after it changed: the memory has its contents when the circuit is initialized or reset.
read
¶
read(address: int) -> VALUE_TYPE
Return the word at an address.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
address
|
int
|
The address. |
required |
write
¶
write(address: int, value: VALUE_TYPE) -> None
Write a word to an address, like a testbench preparing the memory. Call it after Circuit.init(), which fills the memory again from its file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
address
|
int
|
The address. |
required |
value
|
VALUE_TYPE
|
The word. |
required |
ROM
¶
ROM(
circuit: Circuit,
path: str | Path,
name: str | None = None,
address_width: int = 8,
data_width: int = 8,
mode: str = "async",
edge: str = "rising",
output_enable: bool = False,
load_address: int = 0,
)
Bases: _Memory
A ROM with 2^address_width words of data_width bits, with the contents of a file.
The words that the file doesn't fill are zero.
The mode sets how the ROM is read:
- "async": no clock, DataOut is the word at the address at all times.
- "sync": DataOut is read on the clock edge.
The file is binary, with the bytes of each word in little-endian order, or text (.hex, .mem or .txt) with hexadecimal words like Verilog's $readmemh reads, also with @address lines.
Ports
clk: Clock input, in "sync" mode.
Address: Input, address_width bits.
OE: Output enable input, with output_enable. DataOut is "Z" while it is 0.
DataOut: Output, data_width bits. Tristate with output_enable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
circuit
|
Circuit
|
The circuit to add the component to. |
required |
path
|
str | Path
|
The file with the contents. |
required |
name
|
str | None
|
The component name, the class name if not given. |
None
|
address_width
|
int
|
The number of address bits, 1 to 20. |
8
|
data_width
|
int
|
The number of bits in a word, 1 to 32. |
8
|
mode
|
str
|
"async" or "sync", see above. |
'async'
|
edge
|
str
|
The clock edge, "rising" or "falling". |
'rising'
|
output_enable
|
bool
|
Add an output enable input. |
False
|
load_address
|
int
|
The address to load the file at. |
0
|
MemStdOut
¶
MemStdOut(circuit: Circuit, address: int = 32768)
Bases: Component
Prints the characters written to one address, like a memory-mapped terminal. A newline (0x0A) prints the whole line.
Ports
clk: Clock input. Address: Input, 16 bits. DataIn: Input, 8 bits. WE: Write enable input.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
circuit
|
Circuit
|
The circuit to add the component to. |
required |
address
|
int
|
The address to print the characters written to. |
32768
|
Other¶
Helper functions¶
The file formats and parameters the components read, for scripts that work with the same files:
read_memory_file
¶
read_memory_file(
path: str | Path,
address_width: int,
data_width: int,
load_address: int = 0,
) -> dict[int, int]
Read the contents of a memory from a file, as a dict from address to word.
A file ending in .hex, .mem or .txt is text, read like Verilog's $readmemh. Other files are binary, with the bytes of each word in little-endian order.
file_modules
¶
file_modules(
path: str | Path, log: list[str] | None = None
) -> dict[str, list[str]]
The modules of a Verilog file, each with the modules of the file it instantiates (see Synthesis.module_hierarchy()). The last files are kept, by their contents.
Raises:
| Type | Description |
|---|---|
SynthesisException
|
If Yosys reports an error, like a syntax error. |
OSError
|
If the file can't be read. |
files_used
¶
files_used(path: str | Path) -> set[Path]
The circuit files that a subcircuit's file uses, through its subcircuits and theirs (resolved paths). A file that can't be read uses nothing more.
parameter_dict
¶
parameter_dict(
parameters: str | Mapping[str, int] | None,
) -> dict[str, int]
Parameters by name, from a dict or from "NAME=VALUE,..." (a value like 8, -1 or 0xff)
Raises:
| Type | Description |
|---|---|
YosysComponentException
|
If the text can't be read. |
parameter_text
¶
parameter_text(parameters: Mapping[str, int]) -> str
Parameters as "NAME=VALUE,...", sorted by name
NetlistParts
¶
Bases: NamedTuple
What a component was built from: the ports of the module, its cells (with the comparisons grouped, see group_comparisons()), the component of each cell (and of the constants, "VDD" and "GND"), and the nets between them