2013-09-21 07:04:07 -04:00
|
|
|
from migen.fhdl.std import *
|
|
|
|
from migen.fhdl import verilog
|
|
|
|
from migen.bus import csr
|
2014-04-18 04:33:05 -04:00
|
|
|
from migen.sim.generic import run_simulation
|
2013-09-21 07:04:07 -04:00
|
|
|
from migen.bus.transactions import *
|
|
|
|
|
2014-04-18 04:33:05 -04:00
|
|
|
from miscope.std import *
|
2013-09-22 05:45:30 -04:00
|
|
|
from miscope.storage import *
|
2013-09-21 07:04:07 -04:00
|
|
|
|
|
|
|
from mibuild.tools import write_to_file
|
2014-04-18 04:33:05 -04:00
|
|
|
from miscope.tools.regs import *
|
|
|
|
from miscope.tools.truthtable import *
|
2013-09-21 07:04:07 -04:00
|
|
|
|
2014-04-18 04:33:05 -04:00
|
|
|
from cpuif import *
|
2013-09-21 07:04:07 -04:00
|
|
|
|
|
|
|
class Csr2Trans():
|
|
|
|
def __init__(self):
|
|
|
|
self.t = []
|
|
|
|
|
|
|
|
def write_csr(self, adr, value):
|
|
|
|
self.t.append(TWrite(adr//4, value))
|
|
|
|
|
|
|
|
def read_csr(self, adr):
|
|
|
|
self.t.append(TRead(adr//4))
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
|
|
triggered = False
|
|
|
|
dat = 0
|
|
|
|
|
|
|
|
rec_done = False
|
|
|
|
|
|
|
|
dat_rdy = False
|
|
|
|
|
|
|
|
rec_length = 128
|
|
|
|
|
2014-04-18 04:33:05 -04:00
|
|
|
def csr_configure(bus, regs):
|
2013-09-21 07:04:07 -04:00
|
|
|
# Length
|
2014-04-18 04:33:05 -04:00
|
|
|
regs.recorder_length.write(rec_length)
|
2013-09-21 07:04:07 -04:00
|
|
|
|
|
|
|
# Offset
|
2014-04-18 04:33:05 -04:00
|
|
|
regs.recorder_offset.write(0)
|
2013-09-21 07:04:07 -04:00
|
|
|
|
|
|
|
# Trigger
|
2014-04-18 04:33:05 -04:00
|
|
|
regs.recorder_trigger.write(1)
|
2013-09-21 07:04:07 -04:00
|
|
|
|
|
|
|
return bus.t
|
|
|
|
|
2014-04-18 04:33:05 -04:00
|
|
|
def csr_read_data(bus, regs):
|
2013-09-21 07:04:07 -04:00
|
|
|
for i in range(rec_length+100):
|
2014-04-18 04:33:05 -04:00
|
|
|
regs.recorder_read_dat.read()
|
|
|
|
regs.recorder_read_en.write(1)
|
2013-09-21 07:04:07 -04:00
|
|
|
return bus.t
|
|
|
|
|
2014-04-18 04:33:05 -04:00
|
|
|
def csr_transactions(bus, regs):
|
|
|
|
for t in csr_configure(bus, regs):
|
2013-09-21 07:04:07 -04:00
|
|
|
yield t
|
|
|
|
|
|
|
|
for t in range(100):
|
|
|
|
yield None
|
|
|
|
|
|
|
|
global triggered
|
|
|
|
triggered = True
|
|
|
|
|
|
|
|
for t in range(512):
|
|
|
|
yield None
|
|
|
|
|
2014-04-18 04:33:05 -04:00
|
|
|
for t in csr_read_data(bus, regs):
|
2013-09-21 07:04:07 -04:00
|
|
|
yield t
|
|
|
|
|
|
|
|
for t in range(100):
|
|
|
|
yield None
|
|
|
|
|
|
|
|
|
|
|
|
class TB(Module):
|
|
|
|
csr_base = 0
|
|
|
|
csr_map = {
|
|
|
|
"recorder": 1,
|
|
|
|
}
|
2014-04-18 04:33:05 -04:00
|
|
|
def __init__(self, addrmap=None):
|
2013-09-21 07:04:07 -04:00
|
|
|
self.csr_base = 0
|
|
|
|
|
|
|
|
# Recorder
|
|
|
|
self.submodules.recorder = Recorder(32, 1024)
|
|
|
|
|
|
|
|
# Csr
|
|
|
|
self.submodules.csrbankarray = csrgen.BankArray(self,
|
|
|
|
lambda name, memory: self.csr_map[name if memory is None else name + "_" + memory.name_override])
|
2014-04-18 04:33:05 -04:00
|
|
|
|
|
|
|
# Csr Master
|
|
|
|
csr_header = get_csr_csv(self.csr_base, self.csrbankarray)
|
|
|
|
write_to_file("csr.csv", csr_header)
|
|
|
|
|
|
|
|
bus = Csr2Trans()
|
|
|
|
regs = build_map(addrmap, bus.read_csr, bus.write_csr)
|
|
|
|
self.submodules.master = csr.Initiator(csr_transactions(bus, regs))
|
|
|
|
|
|
|
|
self.submodules.csrcon = csr.Interconnect(self.master.bus, self.csrbankarray.get_buses())
|
2013-09-21 07:04:07 -04:00
|
|
|
|
|
|
|
# Recorder Data
|
2014-04-18 04:33:05 -04:00
|
|
|
def recorder_data(self, selfp):
|
|
|
|
selfp.recorder.dat_sink.stb = 1
|
2013-09-21 07:04:07 -04:00
|
|
|
if not hasattr(self, "cnt"):
|
|
|
|
self.cnt = 0
|
|
|
|
self.cnt += 1
|
|
|
|
|
2014-04-18 04:33:05 -04:00
|
|
|
selfp.recorder.dat_sink.dat = self.cnt
|
2013-09-21 07:04:07 -04:00
|
|
|
|
|
|
|
global triggered
|
|
|
|
if triggered:
|
2014-04-18 04:33:05 -04:00
|
|
|
selfp.recorder.trig_sink.stb = 1
|
|
|
|
selfp.recorder.trig_sink.hit = 1
|
2013-09-21 07:04:07 -04:00
|
|
|
triggered = False
|
|
|
|
else:
|
2014-04-18 04:33:05 -04:00
|
|
|
selfp.recorder.trig_sink.stb = 0
|
|
|
|
selfp.recorder.trig_sink.hit = 0
|
2013-09-21 07:04:07 -04:00
|
|
|
|
|
|
|
# Simulation
|
2014-04-18 04:33:05 -04:00
|
|
|
def end_simulation(self, selfp):
|
|
|
|
if self.master.done:
|
|
|
|
raise StopSimulation
|
2013-09-21 07:04:07 -04:00
|
|
|
|
2014-04-18 04:33:05 -04:00
|
|
|
def do_simulation(self, selfp):
|
|
|
|
self.recorder_data(selfp)
|
|
|
|
self.end_simulation(selfp)
|
2013-09-21 07:04:07 -04:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2014-04-18 04:33:05 -04:00
|
|
|
tb = TB(addrmap="csr.csv")
|
|
|
|
run_simulation(tb, ncycles=2000, vcd_name="tb_recorder_csr.vcd")
|
2013-09-21 07:04:07 -04:00
|
|
|
print("Sim Done")
|
|
|
|
input()
|
|
|
|
|
|
|
|
main()
|