@@ -80,33 +80,73 @@ def mkTop():
8080
8181 # FSM definition
8282 fsm = lib .FSM (m , 'fsm' , clk , rst )
83-
84- init = fsm . current ()
83+
84+ # initialize
8585 fsm .add ( bramif .init () )
86+ fsm .add (bramif .datain (- Int (4 )))
8687 fsm .goto_next ()
88+
89+ # write
90+ cond = bramif .addr < 128
91+ fsm .add (bramif .addr .inc (), bramif .write (1 ), bramif .datain (bramif .datain + 4 ), cond = cond )
92+ fsm .add (Systask ('display' , 'addr:%x write: %x' , bramif .addr - 1 , bramif .datain ),
93+ cond = cond , delay = 1 )
8794
88- first = fsm .current ()
95+ fsm .add (bramif .init (), cond = Not (cond ))
96+ fsm .goto_next (cond = Not (cond ))
97+
98+ # read
99+ cond = bramif .addr < 128
100+ fsm .add (bramif .addr .inc (), cond = cond )
101+ prev_addr = m .TmpReg (addrwidth , initval = 0 )
102+ fsm .add (prev_addr (bramif .addr ), delay = 1 )
103+ fsm .add (Systask ('display' , 'addr:%x read : %x' , prev_addr - 1 , bramif .dataout ),
104+ cond = cond , delay = 2 )
89105
90- fsm .add ( bramif .datain (bramif .datain + 4 ) )
91- fsm .goto_next ()
106+ fsm .add (bramif .init (), cond = Not (cond ))
107+ fsm .goto_next (cond = Not (cond ))
108+
109+ fsm .make_always (reset = bramif .init ())
92110
93- fsm .add ( bramif .write (0 ) )
94- fsm .goto_next ()
111+ return m
112+
113+ #-------------------------------------------------------------------------------
114+ # Testbench
115+ #-------------------------------------------------------------------------------
116+ def mkTest ():
117+ m = Module ('test' )
118+
119+ # target instance
120+ top = mkTop ()
121+
122+ # copy paras and ports
123+ params = m .copy_params (top )
124+ ports = m .copy_sim_ports (top )
125+
126+ clk = ports ['CLK' ]
127+ rst = ports ['RST' ]
95128
96- fsm .add (
97- If (bramif .addr == 128 )(
98- bramif .addr (0 )
99- ).Else (
100- bramif .addr (bramif .addr + 1 )
101- ))
102- fsm .goto (init , cond = (bramif .addr == 128 ), else_dst = first )
103-
104- fsm .make_always (reset = [bramif .addr (0 ), bramif .datain (0 ), bramif .write (0 )])
129+ uut = m .Instance (top , 'uut' ,
130+ params = m .connect_params (top ),
131+ ports = m .connect_ports (top ))
105132
133+ #lib.simulation.setup_waveform(m, uut)
134+ lib .simulation .setup_clock (m , clk , hperiod = 5 )
135+ init = lib .simulation .setup_reset (m , rst , m .make_reset (), period = 100 )
136+
137+ init .add (
138+ Delay (10000 ),
139+ Systask ('finish' ),
140+ )
141+
106142 return m
107143
108144#-------------------------------------------------------------------------------
109145if __name__ == '__main__' :
110- top = mkTop ()
111- verilog = top .to_verilog ()
146+ test = mkTest ()
147+ verilog = test .to_verilog ()
112148 print (verilog )
149+
150+ sim = lib .simulation .Simulator (test )
151+ rslt = sim .run ()
152+ print (rslt )
0 commit comments