Skip to content

Using the component library

intrepyd.lib collects reusable building blocks so you assemble a model from vetted parts instead of rebuilding the same latches, counters and timers. It has two submodules: intrepyd.lib.eda for hardware/EDA blocks and intrepyd.lib.plc for the IEC 61131-3 function blocks a PLC program uses.

The two shapes

A block is either a function or a class, by a simple rule:

  • a combinational block, or one that builds its own state and needs nothing back from you, is a function mk_<name>(ctx, ...) returning its output net(s) — mk_mux, mk_decoder_2to4, mk_comparator, mk_clock, mk_clock_signal, mk_counter, mk_ring_counter, and every plc block;
  • a stateful block whose next value depends on signals you provide after construction is a class: build it, then call set_init_next(...) to close the loop — LatchD, Delay, FlipFlopD, FlipFlopDE, ShiftRegister, Fifo.

EDA example: a shift register

import intrepyd as ip
from intrepyd.lib.eda import ShiftRegister

ctx = ip.Context()
bool_t = ctx.mk_boolean_type()

d = ctx.mk_input('d', bool_t)
sr = ShiftRegister(ctx, 'sr', bool_t, length=3)   # 3-stage register
sr.set_init_next(ctx.mk_false(), d)               # fill with F, shift in d
ctx.mk_output(sr.q)                               # q is d delayed by 3 cycles

EDA example: a clocked flip-flop

A clock makes a step a part of its period: mk_clock_signal is true for high steps, then false for low steps, and again, starting high (or as its phase is at step 0). A flip-flop it drives takes its input at the steps the clock rises, once a period.

import intrepyd as ip
from intrepyd.lib.eda import FlipFlopD, mk_clock_signal

ctx = ip.Context()
bool_t = ctx.mk_boolean_type()

clk = mk_clock_signal(ctx, 'clk')                 # T, F, T, F, ...
d = ctx.mk_input('d', bool_t)
ff = FlipFlopD(ctx, 'ff', bool_t, clk)
ff.set_init_next(ctx.mk_false(), d)               # q is d, taken every two steps
ctx.mk_output(ff.q)

To check a property for both phases, give the clock an input as its phase: mk_clock_signal(ctx, 'clk', phase=ctx.mk_input('p', bool_t)); only its value at step 0 is read. mk_clock(ctx, 'clk') does just that, and returns the clock and that input.

EDA example: a FIFO, and what it guarantees

A Fifo of depth places takes its inputs after it is built, so they may depend on its own outputs: here it is pushed only while it is not full. PDR then proves, for every input, that it is never empty and full at once.

import intrepyd as ip
from intrepyd.engine import EngineResult
from intrepyd.lib.eda import Fifo

ctx = ip.Context()
bool_t, int8_t = ctx.mk_boolean_type(), ctx.mk_int8_type()

fifo = Fifo(ctx, 'fifo', int8_t, depth=4)
want = ctx.mk_input('want', bool_t)
fifo.set_init_next(ctx.mk_number('0', int8_t),           # the places at the start
                   ctx.mk_input('d', int8_t),            # the value pushed
                   ctx.mk_and(want, ctx.mk_not(fifo.full)),  # push while not full
                   ctx.mk_input('pop', bool_t))          # pop: q leaves the queue

pdr = ctx.mk_pdr()
pdr.add_target(ctx.mk_and(fifo.empty, fifo.full))
assert pdr.reach_targets() == EngineResult.UNREACHABLE

PLC example: an up counter

Every plc block works on a synchronous scan cycle (one simulation step is one cycle), and the timers measure their preset time in cycles.

import intrepyd as ip
from intrepyd.lib.plc import mk_ctu

ctx = ip.Context()
bool_t = ctx.mk_boolean_type()
int8_t = ctx.mk_int8_type()

cu = ctx.mk_input('cu', bool_t)        # count-up (counted on its rising edge)
reset = ctx.mk_input('reset', bool_t)
pv = ctx.mk_number('4', int8_t)        # preset value

cv, q = mk_ctu(ctx, 'ctu', cu, reset, pv, int8_t)
ctx.mk_output(q)                       # q is true once cv reaches 4

You can then simulate the model (see Simulating) or check a property on it (see Model checking); the counter's q makes a natural target.

What is in the library

Module Blocks
intrepyd.lib.eda mk_clock, mk_clock_signal, mk_counter, mk_ring_counter, mk_mux, mk_decoder_2to4, mk_comparator, LatchD, Delay, FlipFlopD, FlipFlopDE, ShiftRegister, Fifo
intrepyd.lib.plc mk_r_trig, mk_f_trig, mk_sr, mk_rs, mk_ctu, mk_ctd, mk_ctud, mk_ton, mk_tof, mk_tp

See the component library reference for the full signatures and the exact semantics of each block.