fhdl/specials/memory: use $readmemh to initialize memories
authorFlorent Kermarrec <florent@enjoy-digital.fr>
Wed, 18 Mar 2015 14:16:11 +0000 (15:16 +0100)
committerFlorent Kermarrec <florent@enjoy-digital.fr>
Wed, 18 Mar 2015 14:27:01 +0000 (15:27 +0100)
migen/fhdl/specials.py

index 78098dd11dd26fa7a84ed15f1c1d303dfb1d2b4a..768f55e4b6e2cacd944ad218e6b69b1985441044 100644 (file)
@@ -307,11 +307,20 @@ class Memory(Special):
                r += "\n"
 
                if memory.init is not None:
+                       memory_filename = gn(memory) + ".init"
+
+                       # XXX move I/O to mibuild?
+                       # (Implies mem init won't work with simple Migen examples?)
+                       f = open(memory_filename, "w")
+                       for d in memory.init:
+                               f.write("{:x}\n".format(d))
+                       f.close()
+
                        r += "initial begin\n"
-                       for i, c in enumerate(memory.init):
-                               r += "\t" + gn(memory) + "[" + str(i) + "] <= " + str(memory.width) + "'d" + str(c) + ";\n"
+                       r += "$readmemh(\"" + memory_filename + "\", " + gn(memory) + ");\n"
                        r += "end\n\n"
 
+
                return r
 
 class SynthesisDirective(Special):