litex/migen/sim/generic.py

202 lines
5.1 KiB
Python
Raw Normal View History

from migen.fhdl.std import *
2012-03-05 14:31:41 -05:00
from migen.fhdl import verilog
from migen.sim.ipc import *
2013-02-09 11:04:53 -05:00
from migen.sim import icarus
2012-03-05 14:31:41 -05:00
class TopLevel:
2012-03-06 09:26:04 -05:00
def __init__(self, vcd_name=None, vcd_level=1,
top_name="top", dut_type="dut", dut_name="dut",
cd_name="sys", clk_period=10):
2012-03-06 09:26:04 -05:00
self.vcd_name = vcd_name
self.vcd_level = vcd_level
2012-03-05 14:31:41 -05:00
self.top_name = top_name
self.dut_type = dut_type
self.dut_name = dut_name
self._cd_name = cd_name
self._clk_period = clk_period
cd = ClockDomain(self._cd_name)
self.clock_domains = [cd]
self.ios = {cd.clk, cd.rst}
2012-03-05 14:31:41 -05:00
def get(self, sockaddr):
2012-03-06 09:26:04 -05:00
template1 = """`timescale 1ns / 1ps
module {top_name}();
2012-03-05 14:31:41 -05:00
reg {clk_name};
reg {rst_name};
initial begin
{rst_name} <= 1'b1;
@(posedge {clk_name});
{rst_name} <= 1'b0;
end
always begin
{clk_name} <= 1'b0;
#{hclk_period};
{clk_name} <= 1'b1;
#{hclk_period};
end
{dut_type} {dut_name}(
.{rst_name}({rst_name}),
.{clk_name}({clk_name})
);
initial $migensim_connect("{sockaddr}");
always @(posedge {clk_name}) $migensim_tick;
"""
2012-03-06 09:26:04 -05:00
template2 = """
initial begin
$dumpfile("{vcd_name}");
$dumpvars({vcd_level}, {dut_name});
end
"""
r = template1.format(top_name=self.top_name,
2012-03-05 14:31:41 -05:00
dut_type=self.dut_type,
dut_name=self.dut_name,
clk_name=self._cd_name + "_clk",
rst_name=self._cd_name + "_rst",
hclk_period=str(self._clk_period/2),
2012-03-05 14:31:41 -05:00
sockaddr=sockaddr)
2012-03-06 09:26:04 -05:00
if self.vcd_name is not None:
r += template2.format(vcd_name=self.vcd_name,
vcd_level=str(self.vcd_level),
dut_name=self.dut_name)
r += "\nendmodule"
return r
2012-03-05 14:31:41 -05:00
2013-03-15 14:15:48 -04:00
def _call_sim(fragment, simulator):
for s in fragment.sim:
if simulator.cycle_counter >= 0 or (hasattr(s, "initialize") and s.initialize):
s(simulator)
2012-03-05 14:31:41 -05:00
class Simulator:
2013-02-09 11:04:53 -05:00
def __init__(self, fragment, top_level=None, sim_runner=None, sockaddr="simsocket", **vopts):
if not isinstance(fragment, Fragment):
fragment = fragment.get_fragment()
2012-03-05 14:31:41 -05:00
if top_level is None:
2013-02-09 11:04:53 -05:00
top_level = TopLevel()
if sim_runner is None:
sim_runner = icarus.Runner()
self.fragment = fragment + Fragment(clock_domains=top_level.clock_domains)
2013-02-09 11:04:53 -05:00
self.top_level = top_level
2012-03-05 14:31:41 -05:00
self.ipc = Initiator(sockaddr)
2013-02-09 11:04:53 -05:00
self.sim_runner = sim_runner
2012-03-05 14:31:41 -05:00
c_top = self.top_level.get(sockaddr)
c_fragment, self.namespace = verilog.convert(self.fragment,
ios=self.top_level.ios,
2012-03-05 14:31:41 -05:00
name=self.top_level.dut_type,
return_ns=True,
**vopts)
2012-03-05 14:31:41 -05:00
2012-03-06 13:29:39 -05:00
self.cycle_counter = -1
2012-03-05 14:31:41 -05:00
self.interrupt = False
2012-03-06 08:20:26 -05:00
self.sim_runner = sim_runner
self.sim_runner.start(c_top, c_fragment)
self.ipc.accept()
2012-03-06 09:00:02 -05:00
reply = self.ipc.recv()
assert(isinstance(reply, MessageTick))
2013-03-15 14:15:48 -04:00
_call_sim(self.fragment, self)
2012-03-05 14:31:41 -05:00
def run(self, ncycles=-1):
2012-03-06 13:29:39 -05:00
self.interrupt = False
2012-03-05 14:31:41 -05:00
counter = 0
while not self.interrupt and (ncycles < 0 or counter < ncycles):
2012-03-06 13:29:39 -05:00
self.cycle_counter += 1
counter += 1
2012-03-06 09:00:02 -05:00
self.ipc.send(MessageGo())
2012-03-05 14:31:41 -05:00
reply = self.ipc.recv()
assert(isinstance(reply, MessageTick))
2013-03-15 14:15:48 -04:00
_call_sim(self.fragment, self)
2012-03-05 14:31:41 -05:00
2012-03-06 13:29:39 -05:00
def rd(self, item, index=0):
2012-03-05 14:31:41 -05:00
name = self.top_level.top_name + "." \
+ self.top_level.dut_name + "." \
2012-03-06 13:29:39 -05:00
+ self.namespace.get_name(item)
self.ipc.send(MessageRead(name, Int32(index)))
2012-03-05 14:31:41 -05:00
reply = self.ipc.recv()
assert(isinstance(reply, MessageReadReply))
2012-03-06 13:29:39 -05:00
if isinstance(item, Memory):
signed = False
nbits = item.width
else:
signed = item.signed
nbits = flen(item)
2012-03-06 10:46:18 -05:00
value = reply.value & (2**nbits - 1)
2012-03-06 13:29:39 -05:00
if signed and (value & 2**(nbits - 1)):
2012-03-06 10:46:18 -05:00
value -= 2**nbits
return value
2012-03-05 14:31:41 -05:00
2012-03-06 13:29:39 -05:00
def wr(self, item, value, index=0):
2012-03-05 14:31:41 -05:00
name = self.top_level.top_name + "." \
+ self.top_level.dut_name + "." \
2012-03-06 13:29:39 -05:00
+ self.namespace.get_name(item)
if isinstance(item, Memory):
nbits = item.width
else:
nbits = flen(item)
2012-03-06 10:46:18 -05:00
if value < 0:
2012-03-06 13:29:39 -05:00
value += 2**nbits
assert(value >= 0 and value < 2**nbits)
self.ipc.send(MessageWrite(name, Int32(index), value))
2012-06-08 11:52:32 -04:00
def multiread(self, obj):
if isinstance(obj, Signal):
return self.rd(obj)
elif isinstance(obj, list):
r = []
for item in obj:
rd = self.multiread(item)
if isinstance(item, Signal) or rd:
r.append(rd)
return r
2012-06-08 11:52:32 -04:00
elif hasattr(obj, "__dict__"):
r = {}
for k, v in obj.__dict__.items():
rd = self.multiread(v)
if isinstance(v, Signal) or rd:
r[k] = rd
return r
2012-06-08 11:52:32 -04:00
def multiwrite(self, obj, value):
if isinstance(obj, Signal):
self.wr(obj, value)
elif isinstance(obj, list):
for target, source in zip(obj, value):
self.multiwrite(target, source)
else:
for k, v in value.items():
self.multiwrite(getattr(obj, k), v)
2012-08-04 18:16:11 -04:00
def __del__(self):
del self.ipc
del self.sim_runner
2012-03-30 10:40:26 -04:00
2012-06-08 11:52:32 -04:00
# Contrary to multiread/multiwrite, Proxy fetches the necessary signals only and
# immediately forwards writes into the simulation.
2012-03-30 10:40:26 -04:00
class Proxy:
def __init__(self, sim, obj):
self.__dict__["_sim"] = sim
self.__dict__["_obj"] = obj
def __getattr__(self, name):
item = getattr(self._obj, name)
if isinstance(item, Signal):
return self._sim.rd(item)
2012-04-01 11:19:53 -04:00
elif isinstance(item, list):
return [Proxy(self._sim, si) for si in item]
2012-03-30 10:40:26 -04:00
else:
return Proxy(self._sim, item)
def __setattr__(self, name, value):
item = getattr(self._obj, name)
assert(isinstance(item, Signal))
self._sim.wr(item, value)