From 5f045b7649bd897284b8259b024781f2fc3ed324 Mon Sep 17 00:00:00 2001 From: Robert Jordens Date: Fri, 20 Mar 2015 15:10:40 -0600 Subject: [PATCH] sim: keep track of unreferenced items * items that are never referenced in any statements do not end up in the namespace or in the verilog * this memorizes items if they can not be found in the namespace and keeps track of their values --- migen/sim/generic.py | 44 +++++++++++++++++++++++++++++++++----------- 1 file changed, 33 insertions(+), 11 deletions(-) diff --git a/migen/sim/generic.py b/migen/sim/generic.py index b290405bf..2977cf9d8 100644 --- a/migen/sim/generic.py +++ b/migen/sim/generic.py @@ -103,6 +103,7 @@ class Simulator: self.sim_functions = fragment.sim self.active_sim_functions = set(f for f in fragment.sim if not hasattr(f, "passive") or not f.passive) + self.unreferenced = {} def run(self, ncycles=None): counter = 0 @@ -140,28 +141,43 @@ class Simulator: except KeyError: pass + def get_unreferenced(self, item, index): + try: + return self.unreferenced[(item, index)] + except KeyError: + if isinstance(item, Memory): + try: + init = item.init[index] + except (TypeError, IndexError): + init = 0 + else: + init = item.reset + self.unreferenced[(item, index)] = init + return init + def rd(self, item, index=0): - name = self.top_level.top_name + "." \ - + self.top_level.dut_name + "." \ - + self.namespace.get_name(item) - self.ipc.send(MessageRead(name, Int32(index))) - reply = self.ipc.recv() - assert(isinstance(reply, MessageReadReply)) + try: + name = self.top_level.top_name + "." \ + + self.top_level.dut_name + "." \ + + self.namespace.get_name(item) + self.ipc.send(MessageRead(name, Int32(index))) + reply = self.ipc.recv() + assert(isinstance(reply, MessageReadReply)) + value = reply.value + except KeyError: + value = self.get_unreferenced(item, index) if isinstance(item, Memory): signed = False nbits = item.width else: signed = item.signed nbits = flen(item) - value = reply.value & (2**nbits - 1) + value = value & (2**nbits - 1) if signed and (value & 2**(nbits - 1)): value -= 2**nbits return value def wr(self, item, value, index=0): - name = self.top_level.top_name + "." \ - + self.top_level.dut_name + "." \ - + self.namespace.get_name(item) if isinstance(item, Memory): nbits = item.width else: @@ -169,7 +185,13 @@ class Simulator: if value < 0: value += 2**nbits assert(value >= 0 and value < 2**nbits) - self.ipc.send(MessageWrite(name, Int32(index), value)) + try: + name = self.top_level.top_name + "." \ + + self.top_level.dut_name + "." \ + + self.namespace.get_name(item) + self.ipc.send(MessageWrite(name, Int32(index), value)) + except KeyError: + self.unreferenced[(item, index)] = value def __del__(self): if hasattr(self, "ipc"):