2012-01-27 14:22:17 -05:00
|
|
|
from migen.fhdl.structure import *
|
|
|
|
|
2012-09-10 17:45:02 -04:00
|
|
|
def handler(memory, ns, clock_domains):
|
2012-01-27 14:22:17 -05:00
|
|
|
r = ""
|
|
|
|
gn = ns.get_name
|
|
|
|
adrbits = bits_for(memory.depth-1)
|
|
|
|
|
|
|
|
r += "reg [" + str(memory.width-1) + ":0] " \
|
2012-03-06 12:33:44 -05:00
|
|
|
+ gn(memory) \
|
2012-01-27 14:22:17 -05:00
|
|
|
+ "[0:" + str(memory.depth-1) + "];\n"
|
|
|
|
|
|
|
|
adr_regs = {}
|
|
|
|
data_regs = {}
|
|
|
|
for port in memory.ports:
|
|
|
|
if not port.async_read:
|
|
|
|
if port.mode == WRITE_FIRST and port.we is not None:
|
|
|
|
adr_reg = Signal(name_override="memadr")
|
|
|
|
r += "reg [" + str(adrbits-1) + ":0] " \
|
|
|
|
+ gn(adr_reg) + ";\n"
|
|
|
|
adr_regs[id(port)] = adr_reg
|
|
|
|
else:
|
|
|
|
data_reg = Signal(name_override="memdat")
|
|
|
|
r += "reg [" + str(memory.width-1) + ":0] " \
|
|
|
|
+ gn(data_reg) + ";\n"
|
|
|
|
data_regs[id(port)] = data_reg
|
|
|
|
|
|
|
|
for port in memory.ports:
|
2012-09-10 17:45:02 -04:00
|
|
|
r += "always @(posedge " + gn(clock_domains[port.clock_domain].clk) + ") begin\n"
|
2012-01-27 14:22:17 -05:00
|
|
|
if port.we is not None:
|
|
|
|
if port.we_granularity:
|
|
|
|
n = memory.width//port.we_granularity
|
|
|
|
for i in range(n):
|
|
|
|
m = i*port.we_granularity
|
|
|
|
M = (i+1)*port.we_granularity-1
|
|
|
|
sl = "[" + str(M) + ":" + str(m) + "]"
|
|
|
|
r += "\tif (" + gn(port.we) + "[" + str(i) + "])\n"
|
2012-03-06 12:33:44 -05:00
|
|
|
r += "\t\t" + gn(memory) + "[" + gn(port.adr) + "]" + sl + " <= " + gn(port.dat_w) + sl + ";\n"
|
2012-01-27 14:22:17 -05:00
|
|
|
else:
|
|
|
|
r += "\tif (" + gn(port.we) + ")\n"
|
2012-03-06 12:33:44 -05:00
|
|
|
r += "\t\t" + gn(memory) + "[" + gn(port.adr) + "] <= " + gn(port.dat_w) + ";\n"
|
2012-01-27 14:22:17 -05:00
|
|
|
if not port.async_read:
|
|
|
|
if port.mode == WRITE_FIRST and port.we is not None:
|
2012-01-27 15:39:23 -05:00
|
|
|
rd = "\t" + gn(adr_regs[id(port)]) + " <= " + gn(port.adr) + ";\n"
|
2012-01-27 14:22:17 -05:00
|
|
|
else:
|
2012-03-06 12:33:44 -05:00
|
|
|
bassign = gn(data_regs[id(port)]) + " <= " + gn(memory) + "[" + gn(port.adr) + "];\n"
|
2012-01-27 14:22:17 -05:00
|
|
|
if port.mode == READ_FIRST or port.we is None:
|
2012-01-27 15:39:23 -05:00
|
|
|
rd = "\t" + bassign
|
2012-01-27 14:22:17 -05:00
|
|
|
elif port.mode == NO_CHANGE:
|
2012-01-27 15:39:23 -05:00
|
|
|
rd = "\tif (!" + gn(port.we) + ")\n" \
|
|
|
|
+ "\t\t" + bassign
|
|
|
|
if port.re is None:
|
|
|
|
r += rd
|
|
|
|
else:
|
|
|
|
r += "\tif (" + gn(port.re) + ")\n"
|
|
|
|
r += "\t" + rd.replace("\n\t", "\n\t\t")
|
2012-09-10 17:45:02 -04:00
|
|
|
r += "end\n\n"
|
2012-01-27 14:22:17 -05:00
|
|
|
|
|
|
|
for port in memory.ports:
|
|
|
|
if port.async_read:
|
2012-03-06 12:33:44 -05:00
|
|
|
r += "assign " + gn(port.dat_r) + " = " + gn(memory) + "[" + gn(port.adr) + "];\n"
|
2012-01-27 14:22:17 -05:00
|
|
|
else:
|
|
|
|
if port.mode == WRITE_FIRST and port.we is not None:
|
2012-03-06 12:33:44 -05:00
|
|
|
r += "assign " + gn(port.dat_r) + " = " + gn(memory) + "[" + gn(adr_regs[id(port)]) + "];\n"
|
2012-01-27 14:22:17 -05:00
|
|
|
else:
|
|
|
|
r += "assign " + gn(port.dat_r) + " = " + gn(data_regs[id(port)]) + ";\n"
|
|
|
|
r += "\n"
|
|
|
|
|
|
|
|
if memory.init is not None:
|
|
|
|
r += "initial begin\n"
|
|
|
|
for i, c in enumerate(memory.init):
|
2012-03-06 12:33:44 -05:00
|
|
|
r += "\t" + gn(memory) + "[" + str(i) + "] <= " + str(memory.width) + "'d" + str(c) + ";\n"
|
2012-01-27 14:22:17 -05:00
|
|
|
r += "end\n\n"
|
|
|
|
|
|
|
|
return r
|