2012-03-05 14:31:41 -05:00
|
|
|
from migen.fhdl.structure import *
|
|
|
|
from migen.fhdl import verilog
|
|
|
|
from migen.sim.ipc import *
|
|
|
|
|
|
|
|
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",
|
|
|
|
clk_name="sys_clk", clk_period=10, rst_name="sys_rst"):
|
|
|
|
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.clk_name = clk_name
|
|
|
|
self.clk_period = clk_period
|
|
|
|
self.rst_name = rst_name
|
|
|
|
|
|
|
|
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.clk_name,
|
|
|
|
hclk_period=str(self.clk_period/2),
|
|
|
|
rst_name=self.rst_name,
|
|
|
|
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
|
|
|
|
|
|
|
class Simulator:
|
|
|
|
def __init__(self, fragment, sim_runner, top_level=None, sockaddr="simsocket"):
|
|
|
|
self.fragment = fragment
|
|
|
|
if top_level is None:
|
|
|
|
self.top_level = TopLevel()
|
|
|
|
else:
|
|
|
|
self.top_level = top_level
|
|
|
|
self.ipc = Initiator(sockaddr)
|
|
|
|
|
|
|
|
c_top = self.top_level.get(sockaddr)
|
|
|
|
|
|
|
|
clk_signal = Signal(name_override=self.top_level.clk_name)
|
|
|
|
rst_signal = Signal(name_override=self.top_level.rst_name)
|
|
|
|
c_fragment, self.namespace = verilog.convert(fragment,
|
|
|
|
{clk_signal, rst_signal},
|
|
|
|
name=self.top_level.dut_type,
|
|
|
|
clk_signal=clk_signal,
|
|
|
|
rst_signal=rst_signal,
|
|
|
|
return_ns=True)
|
|
|
|
|
|
|
|
self.cycle_counter = 0
|
|
|
|
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))
|
|
|
|
self.fragment.call_sim(self, -1)
|
2012-03-05 14:31:41 -05:00
|
|
|
|
|
|
|
def run(self, ncycles=-1):
|
|
|
|
counter = 0
|
|
|
|
while not self.interrupt and (ncycles < 0 or counter < ncycles):
|
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))
|
2012-03-06 09:00:02 -05:00
|
|
|
self.fragment.call_sim(self, self.cycle_counter)
|
2012-03-05 14:31:41 -05:00
|
|
|
self.cycle_counter += 1
|
|
|
|
counter += 1
|
|
|
|
|
|
|
|
def rd(self, signal):
|
|
|
|
name = self.top_level.top_name + "." \
|
|
|
|
+ self.top_level.dut_name + "." \
|
|
|
|
+ self.namespace.get_name(signal)
|
|
|
|
self.ipc.send(MessageRead(name))
|
|
|
|
reply = self.ipc.recv()
|
|
|
|
assert(isinstance(reply, MessageReadReply))
|
2012-03-06 10:46:18 -05:00
|
|
|
nbits = signal.bv.width
|
|
|
|
value = reply.value & (2**nbits - 1)
|
|
|
|
if signal.bv.signed and (value & 2**(nbits - 1)):
|
|
|
|
value -= 2**nbits
|
|
|
|
return value
|
2012-03-05 14:31:41 -05:00
|
|
|
|
|
|
|
def wr(self, signal, value):
|
|
|
|
name = self.top_level.top_name + "." \
|
|
|
|
+ self.top_level.dut_name + "." \
|
|
|
|
+ self.namespace.get_name(signal)
|
2012-03-06 10:46:18 -05:00
|
|
|
if value < 0:
|
|
|
|
value += 2**signal.bv.width
|
|
|
|
assert(value >= 0 and value < 2**signal.bv.width)
|
2012-03-05 14:31:41 -05:00
|
|
|
self.ipc.send(MessageWrite(name, value))
|