In [1]:
import magma as m
from mantle import DFF
In [2]:
class TFF(m.Circuit):
io = m.IO(O=m.Out(m.Bit)) + m.ClockIO()
# instance a dff to hold the state of the toggle flip-flop - this needs to be done first
dff = DFF()
# compute the next state as the not of the old state ff.O
io.O <= dff(~dff.O)
def tff():
return TFF()()
Test using the python simulator.
In [3]:
from fault import PythonTester
tester = PythonTester(TFF, TFF.CLK)
tester.eval()
val = tester.peek(TFF.O)
assert val == False
for i in range(10):
val = not val
tester.step() # toggle clock - now High
assert val == tester.peek(TFF.O)
tester.step() # toggle clock - now Low
assert val == tester.peek(TFF.O)
print("Success!")
Generate verilog with coreir.
In [4]:
m.compile("build/TFF", TFF, inline=True)
%cat build/TFF.v
In [5]:
%cat build/TFF.json
In [6]:
!coreir -i build/TFF.json -p instancecount
Here's an example of testing using fault's staged Tester class and the verilator simulator.
In [7]:
import fault
tester = fault.Tester(TFF, TFF.CLK)
for i in range(5):
tester.step(2)
tester.print("TFF.O=%d\n", TFF.O)
tester.compile_and_run("verilator", disp_type='realtime')