Skip to content

Circuit

Circuit

Circuit(name: str | None = None, vcd: str | None = None)

A circuit: holds the components and runs the simulation. Use it in a with statement, with Circuit(vcd="waves.vcd") as circuit:, to close the VCD file at the end.

Parameters:

Name Type Description Default
name str | None

The circuit name, needed to save the circuit.

None
vcd str | None

A VCD file to write waveforms to, from init().

None

name property

name: str | None

Get the circuit name

time_ns property

time_ns: int

Get the current simulation time (ns)

components property

components: list[Component]

Get the components in this circuit

init

init() -> None

Initialize the circuit: set the time to 0 and all ports to "X", then let the components set their default state. Call it after adding and connecting components.

run

run(
    s: float | None = None,
    ms: float | None = None,
    us: float | None = None,
    ns: float | None = None,
    single_step: bool = False,
) -> bool

Run the simulation for a period of time. The times are added together, so run(ms=1, us=500) runs for 1.5 ms.

Parameters:

Name Type Description Default
s float | None

Seconds.

None
ms float | None

Milliseconds.

None
us float | None

Microseconds.

None
ns float | None

Nanoseconds.

None
single_step bool

Stop after the first event on a port of a top-level component, at the end of its time (the GUI's Single Step).

False

Returns:

Type Description
bool

True if the run stopped for a single step.

run_until

run_until(
    s: float | None = None,
    ms: float | None = None,
    us: float | None = None,
    ns: float | None = None,
) -> None

Run the simulation until an absolute time, nothing happens if it has passed. The times are added together.

Parameters:

Name Type Description Default
s float | None

Seconds.

None
ms float | None

Milliseconds.

None
us float | None

Microseconds.

None
ns float | None

Nanoseconds.

None

run_until_edge

run_until_edge(
    rising: Clock | Port | None = None,
    falling: Clock | Port | None = None,
    max_ns: int = 1000000000,
) -> bool

Run the simulation through the next rising (or falling) edge of a clock or a port, to drive a simulation by its clock instead of setting a clock port:

circuit.init()
circuit.run_until_edge(rising=clock)  # The flip-flops have their new values
counter.up.value = 0  # Set inputs for the next edge
circuit.run_until_edge(rising=clock)

With a Clock (or its output), the run stops just before the clock's next edge, half a period later, so that what the edge started has settled. With another port, it stops after the events at the time of the edge (the port went from 0 to 1, or from 1 to 0).

Parameters:

Name Type Description Default
rising Clock | Port | None

The clock or port of a rising edge.

None
falling Clock | Port | None

The clock or port of a falling edge (instead of rising).

None
max_ns int

The most time it runs to find the edge of a port.

1000000000

Returns:

Type Description
bool

If there was an edge (False if a port had none in max_ns).

Raises:

Type Description
CircuitError

Without one of rising and falling.

DigsimException

If the clock doesn't run (before init()).

run_within

run_within(
    seconds: float,
    s: float | None = None,
    ms: float | None = None,
    us: float | None = None,
    ns: float | None = None,
    single_step: bool = False,
) -> RunResult

Run the simulation like run(), but for at most a time on the clock: a circuit that oscillates makes so many events that a run could take minutes. It runs slices of the time with run(), and stops after the slice that takes it past the limit. The events are the same as with one run(), only where it stops can be earlier.

Parameters:

Name Type Description Default
seconds float

The most time on the clock it runs (a slice can take more).

required
s float | None

Seconds of simulated time.

None
ms float | None

Milliseconds.

None
us float | None

Microseconds.

None
ns float | None

Nanoseconds.

None
single_step bool

Stop after the first event on a port of a top-level component, like run().

False

Returns:

Type Description
RunResult

If it stopped for a single step, and if it ran all the time.

restart_slices

restart_slices() -> None

Start run_within() with a short slice again, like after an input changed: it can start an oscillation, which a long slice would run for long

oscillating_ports

oscillating_ports(
    ns: int = 200,
    changes: int = 10,
    max_seconds: float | None = None,
) -> list[Port]

The ports of the top-level components that oscillate: it runs the simulation for a while, a nanosecond at a time, and counts how often each output changes. A port that changes at least 'changes' times in 'ns' nanoseconds oscillates. Run it while the inputs of the circuit don't change. The ones that change the most come first.

Parameters:

Name Type Description Default
ns int

How long it runs, in nanoseconds.

200
changes int

How many changes make an oscillation.

10
max_seconds float | None

Stop after this long on the clock, with the changes counted so far, in a circuit where a nanosecond takes long.

None

process_single_event

process_single_event(
    stop_time_ns: int | None = None,
) -> tuple[bool, bool]

Process only the next event, to step through a simulation.

Parameters:

Name Type Description Default
stop_time_ns int | None

Don't process an event after this time, keep it for later.

None

Returns:

Type Description
bool

If an event was processed (False if there were none, or the next one is after

bool

stop_time_ns), and if it was on a port of a top-level component, rather than

tuple[bool, bool]

inside one like a Yosys component.

vcd

vcd(filename: str, timescale: str = '1 ns') -> None

Start writing waveforms to a VCD file, for GTKWave or another viewer.

Parameters:

Name Type Description Default
filename str

The VCD file.

required
timescale str

The time of a nanosecond of the simulation in the file, like "1 ps" when a nanosecond stands for a picosecond (as for cocotb).

'1 ns'

Raises:

Type Description
CircuitError

If a VCD file is already open.

OSError

If the file can't be opened.

vcd_close

vcd_close() -> None

Finish and close the VCD file, if one is open

get_component

get_component(component_name: str) -> Component

Get a component by its name.

Raises:

Type Description
CircuitError

If the circuit has no component with the name.

get_toplevel_components

get_toplevel_components() -> list[Component]

Get toplevel components in the circuit

delete_component

delete_component(component: Component) -> None

Delete a component, and the components it is built from, from the circuit

clear

clear() -> None

Remove all components, and the events of their ports

to_json_file

to_json_file(filename: str | Path) -> None

Save the circuit in a JSON file. File paths in it, like a Verilog file, are stored relative to the file's folder. The circuit needs a name.

from_json_file

from_json_file(
    filename: str | Path, folder: str | None = None
) -> None

Load a circuit from a JSON file, replacing the circuit's components.

Parameters:

Name Type Description Default
filename str | Path

The file.

required
folder str | None

The folder that relative file paths in the file are read from, by default the file's folder (where to_json_file() stores them relative to).

None

to_dataclass

to_dataclass(folder: str = '.') -> CircuitDataClass

The circuit as a data class, used when saving it.

Parameters:

Name Type Description Default
folder str

File paths, like a Verilog file, are stored relative to this folder.

'.'

from_dataclass

from_dataclass(
    circuit_dc: CircuitDataClass,
    folder: str = "",
    component_exceptions: bool = True,
    connect_exceptions: bool = True,
) -> list[str]

Replace the circuit's components with the ones in a data class.

Parameters:

Name Type Description Default
circuit_dc CircuitDataClass

The circuit data class, for example from a loaded file.

required
folder str

The folder that relative file paths are read from.

''
component_exceptions bool

Raise when a component can't be created, otherwise skip it and report it in the returned list.

True
connect_exceptions bool

Raise when a wire can't be connected, otherwise skip it and report it in the returned list.

True

Returns:

Type Description
list[str]

The errors for the skipped components and wires.

RunResult

Bases: NamedTuple

How a run with a time limit ended (see Circuit.run_within()): if it stopped for a single step, and if it ran all the time it was asked to