litex/sim/tb_miscope.py

193 lines
4.6 KiB
Python
Raw Normal View History

2012-09-09 14:38:01 -04:00
from migen.fhdl.structure import *
2013-03-21 07:23:44 -04:00
from migen.fhdl import verilog
2012-09-09 14:38:01 -04:00
from migen.bus import csr
from migen.sim.generic import Simulator, PureSimulable, TopLevel
from migen.sim.icarus import Runner
from migen.bus.transactions import *
2013-03-23 08:57:59 -04:00
from miscope.trigger import *
from miscope.recorder import *
2013-02-26 17:25:10 -05:00
from miscope.tools.truthtable import *
from miscope.tools.vcd import *
2012-09-09 14:38:01 -04:00
TRIGGER_ADDR = 0x0000
RECORDER_ADDR = 0x0200
rec_done = False
dat_rdy = False
2013-03-23 08:57:59 -04:00
dat_vcd = VcdDat(32)
rec_size = 64
2012-09-09 14:38:01 -04:00
def term_prog(off, dat):
for i in range(4):
yield TWrite(off+3-i, (dat>>(8*i))&0xFF)
def sum_prog(off, addr, dat):
we = 2
yield TWrite(off+3, addr%0xFF)
yield TWrite(off+2, (addr>>8)%0xFF)
yield TWrite(off+1, we+dat)
yield TWrite(off+0, 0)
for i in range(4):
yield TWrite(off+i,0)
def csr_transactions(trigger0, recorder0):
# Trigger Prog
##############################
# Term Prog
term_trans = []
2013-03-21 07:23:44 -04:00
term_trans += [term_prog(trigger0.ports[0].reg_p.base+0, 0xFFFFFFFF)]
term_trans += [term_prog(trigger0.ports[0].reg_p.base+4, 0x00000000)]
term_trans += [term_prog(trigger0.ports[1].reg_p.base+0, 0xFFFFFFFF)]
term_trans += [term_prog(trigger0.ports[1].reg_p.base+4, 0x00000004)]
term_trans += [term_prog(trigger0.ports[2].reg_p.base+0, 0xFFFFFFFF)]
term_trans += [term_prog(trigger0.ports[2].reg_p.base+4, 0x00000008)]
term_trans += [term_prog(trigger0.ports[3].reg_p.base+0, 0xFFFFFFFF)]
term_trans += [term_prog(trigger0.ports[3].reg_p.base+4, 0x0000000C)]
2012-09-09 14:38:01 -04:00
for t in term_trans:
for r in t:
yield r
# Sum Prog
sum_tt = gen_truth_table("term0 | term1 | term2 | term3")
sum_trans = []
for i in range(len(sum_tt)):
2013-03-21 07:23:44 -04:00
sum_trans.append(sum_prog(trigger0.sum.reg_p.base, i, sum_tt[i]))
2012-09-09 14:38:01 -04:00
for t in sum_trans:
for r in t:
yield r
# Recorder Prog
##############################
#Reset
2013-03-23 08:57:59 -04:00
yield TWrite(recorder0.address + REC_RST_BASE, 1)
yield TWrite(recorder0.address + REC_RST_BASE, 0)
# RLE
yield TWrite(REC_RLE_BASE, 0)
2012-09-09 14:38:01 -04:00
#Size
2013-03-23 08:57:59 -04:00
yield TWrite(recorder0.address + REC_SIZE_BASE + 0, 0)
yield TWrite(recorder0.address + REC_OFFSET_BASE + 1, rec_size)
2012-09-09 14:38:01 -04:00
#Offset
2013-03-23 08:57:59 -04:00
yield TWrite(recorder0.address + REC_OFFSET_BASE + 0, 0)
yield TWrite(recorder0.address + REC_OFFSET_BASE + 1, 16)
2012-09-09 14:38:01 -04:00
#Arm
2013-03-23 08:57:59 -04:00
yield TWrite(recorder0.address + REC_ARM_BASE, 1)
yield TWrite(recorder0.address + REC_ARM_BASE, 0)
2012-09-09 14:38:01 -04:00
2012-09-09 14:51:15 -04:00
# Wait Record to be done
##############################
2012-09-09 14:38:01 -04:00
global rec_done
while not rec_done:
yield None
2012-09-09 14:51:15 -04:00
# Read recorded data
##############################
2012-09-09 14:38:01 -04:00
global dat_rdy
2013-03-23 08:57:59 -04:00
for t in range(rec_size):
yield TWrite(recorder0.address + REC_READ_BASE, 1)
2012-09-09 14:38:01 -04:00
dat_rdy = False
2013-03-23 08:57:59 -04:00
yield TWrite(recorder0.address + REC_READ_BASE, 0)
yield TRead(recorder0.address + REC_READ_DATA_BASE + 0)
yield TRead(recorder0.address + REC_READ_DATA_BASE + 1)
yield TRead(recorder0.address + REC_READ_DATA_BASE + 2)
yield TRead(recorder0.address + REC_READ_DATA_BASE + 3)
2012-09-09 14:38:01 -04:00
dat_rdy = True
dat_rdy = False
for t in range(512):
yield None
trig_sig_val = 0
def main():
# Trigger
2013-03-23 08:57:59 -04:00
term0 = Term(32)
term1 = Term(32)
term2 = Term(32)
term3 = Term(32)
trigger0 = Trigger(32, [term0, term1, term2, term3], address=TRIGGER_ADDR)
2012-09-09 14:38:01 -04:00
# Recorder
2013-03-23 08:57:59 -04:00
recorder0 = Recorder(32, 1024, address=RECORDER_ADDR)
2012-09-09 14:38:01 -04:00
# Csr Master
csr_master0 = csr.Initiator(csr_transactions(trigger0, recorder0))
# Csr Interconnect
csrcon0 = csr.Interconnect(csr_master0.bus,
[
2013-03-21 07:23:44 -04:00
trigger0.bank.bus,
recorder0.bank.bus
2012-09-09 14:38:01 -04:00
])
2013-01-03 16:57:26 -05:00
trig_sig = Signal(32)
2013-03-21 07:23:44 -04:00
comb =[
trigger0.trig.eq(trig_sig)
2012-09-09 14:38:01 -04:00
]
comb += [
2013-03-21 07:23:44 -04:00
recorder0.dat.eq(trig_sig),
recorder0.hit.eq(trigger0.hit)
2012-09-09 14:38:01 -04:00
]
# Term Test
def term_stimuli(s):
global trig_sig_val
s.wr(trig_sig,trig_sig_val)
trig_sig_val += 1
trig_sig_val = trig_sig_val % 256
# Recorder Data
def recorder_data(s):
global rec_done
2013-03-23 08:57:59 -04:00
if s.rd(recorder0.sequencer.done) == 1:
2012-09-09 14:38:01 -04:00
rec_done = True
global dat_rdy
if dat_rdy:
2013-03-21 07:23:44 -04:00
print("%08X" %s.rd(recorder0._pull_dat.field.w))
2012-09-09 17:46:26 -04:00
global dat_vcd
2013-03-21 07:23:44 -04:00
dat_vcd.append(s.rd(recorder0._pull_dat.field.w))
2012-09-09 14:38:01 -04:00
# Simulation
def end_simulation(s):
s.interrupt = csr_master0.done
myvcd = Vcd()
2013-03-23 08:57:59 -04:00
myvcd.add(Var("trig_dat", 32, dat_vcd))
2013-03-21 07:23:44 -04:00
f = open("tb_miscope_out.vcd", "w")
2012-09-09 14:38:01 -04:00
f.write(str(myvcd))
f.close()
2013-03-21 07:23:44 -04:00
fragment = term0.get_fragment()
fragment += term1.get_fragment()
fragment += term2.get_fragment()
fragment += term3.get_fragment()
fragment += trigger0.get_fragment()
fragment += recorder0.get_fragment()
fragment += csr_master0.get_fragment()
fragment += csrcon0.get_fragment()
2012-09-09 14:38:01 -04:00
fragment += Fragment(comb=comb)
fragment += Fragment(sim=[term_stimuli])
fragment += Fragment(sim=[recorder_data])
fragment += Fragment(sim=[end_simulation])
2013-03-21 07:23:44 -04:00
sim = Simulator(fragment, TopLevel("tb_miscope.vcd"))
2012-09-09 14:38:01 -04:00
sim.run(2000)
main()
2013-03-21 07:23:44 -04:00
print("Sim Done")
2012-09-09 14:38:01 -04:00
input()