2019-04-26 21:21:50 +02:00
|
|
|
# See LICENSE for licensing information.
|
|
|
|
|
#
|
2019-06-14 17:43:41 +02:00
|
|
|
# Copyright (c) 2016-2019 Regents of the University of California and The Board
|
|
|
|
|
# of Regents for the Oklahoma Agricultural and Mechanical College
|
|
|
|
|
# (acting for and on behalf of Oklahoma State University)
|
|
|
|
|
# All rights reserved.
|
2019-04-26 21:21:50 +02:00
|
|
|
#
|
2020-09-28 18:53:01 +02:00
|
|
|
import math
|
2016-11-08 18:57:35 +01:00
|
|
|
|
2020-11-17 20:12:59 +01:00
|
|
|
|
2016-11-08 18:57:35 +01:00
|
|
|
class verilog:
|
2020-11-03 15:29:17 +01:00
|
|
|
"""
|
2019-01-11 23:15:16 +01:00
|
|
|
Create a behavioral Verilog file for simulation.
|
|
|
|
|
This is inherited by the sram_base class.
|
|
|
|
|
"""
|
|
|
|
|
def __init__(self):
|
|
|
|
|
pass
|
2020-11-03 15:29:17 +01:00
|
|
|
|
2020-11-17 20:12:59 +01:00
|
|
|
def verilog_write(self, verilog_name):
|
2017-12-19 18:01:24 +01:00
|
|
|
""" Write a behavioral Verilog model. """
|
2016-11-08 18:57:35 +01:00
|
|
|
self.vf = open(verilog_name, "w")
|
|
|
|
|
|
|
|
|
|
self.vf.write("// OpenRAM SRAM model\n")
|
|
|
|
|
self.vf.write("// Words: {0}\n".format(self.num_words))
|
2019-07-04 19:34:14 +02:00
|
|
|
self.vf.write("// Word size: {0}\n".format(self.word_size))
|
2019-08-21 23:29:57 +02:00
|
|
|
if self.write_size:
|
2019-07-04 19:34:14 +02:00
|
|
|
self.vf.write("// Write size: {0}\n\n".format(self.write_size))
|
2019-07-06 00:55:03 +02:00
|
|
|
else:
|
|
|
|
|
self.vf.write("\n")
|
2019-01-11 23:15:16 +01:00
|
|
|
|
|
|
|
|
self.vf.write("module {0}(\n".format(self.name))
|
|
|
|
|
for port in self.all_ports:
|
|
|
|
|
if port in self.readwrite_ports:
|
|
|
|
|
self.vf.write("// Port {0}: RW\n".format(port))
|
|
|
|
|
elif port in self.read_ports:
|
|
|
|
|
self.vf.write("// Port {0}: R\n".format(port))
|
|
|
|
|
elif port in self.write_ports:
|
|
|
|
|
self.vf.write("// Port {0}: W\n".format(port))
|
|
|
|
|
if port in self.readwrite_ports:
|
2019-07-04 19:34:14 +02:00
|
|
|
self.vf.write(" clk{0},csb{0},web{0},".format(port))
|
2019-08-21 23:29:57 +02:00
|
|
|
if self.write_size:
|
2019-07-04 19:34:14 +02:00
|
|
|
self.vf.write("wmask{},".format(port))
|
2019-08-21 22:45:34 +02:00
|
|
|
self.vf.write("addr{0},din{0},dout{0}".format(port))
|
2019-01-11 23:15:16 +01:00
|
|
|
elif port in self.write_ports:
|
2019-07-04 19:34:14 +02:00
|
|
|
self.vf.write(" clk{0},csb{0},".format(port))
|
2019-08-21 23:29:57 +02:00
|
|
|
if self.write_size:
|
2019-07-04 19:34:14 +02:00
|
|
|
self.vf.write("wmask{},".format(port))
|
2019-08-21 22:45:34 +02:00
|
|
|
self.vf.write("addr{0},din{0}".format(port))
|
2019-01-11 23:15:16 +01:00
|
|
|
elif port in self.read_ports:
|
2019-08-21 22:45:34 +02:00
|
|
|
self.vf.write(" clk{0},csb{0},addr{0},dout{0}".format(port))
|
2019-01-11 23:15:16 +01:00
|
|
|
# Continue for every port on a new line
|
|
|
|
|
if port != self.all_ports[-1]:
|
|
|
|
|
self.vf.write(",\n")
|
|
|
|
|
self.vf.write("\n );\n\n")
|
2019-07-06 00:08:59 +02:00
|
|
|
|
2019-08-21 23:29:57 +02:00
|
|
|
if self.write_size:
|
2020-09-28 18:53:01 +02:00
|
|
|
self.num_wmasks = int(math.ceil(self.word_size / self.write_size))
|
2019-07-19 23:58:37 +02:00
|
|
|
self.vf.write(" parameter NUM_WMASKS = {0} ;\n".format(self.num_wmasks))
|
2019-07-06 00:08:59 +02:00
|
|
|
self.vf.write(" parameter DATA_WIDTH = {0} ;\n".format(self.word_size))
|
2019-07-22 22:18:52 +02:00
|
|
|
self.vf.write(" parameter ADDR_WIDTH = {0} ;\n".format(self.addr_size))
|
2016-11-08 18:57:35 +01:00
|
|
|
self.vf.write(" parameter RAM_DEPTH = 1 << ADDR_WIDTH;\n")
|
2019-01-11 23:15:16 +01:00
|
|
|
self.vf.write(" // FIXME: This delay is arbitrary.\n")
|
2016-11-08 18:57:35 +01:00
|
|
|
self.vf.write(" parameter DELAY = 3 ;\n")
|
|
|
|
|
self.vf.write("\n")
|
2019-01-11 23:15:16 +01:00
|
|
|
|
|
|
|
|
for port in self.all_ports:
|
|
|
|
|
self.add_inputs_outputs(port)
|
|
|
|
|
|
2016-11-08 18:57:35 +01:00
|
|
|
self.vf.write("\n")
|
2020-11-03 15:29:17 +01:00
|
|
|
|
2019-01-11 23:15:16 +01:00
|
|
|
for port in self.all_ports:
|
|
|
|
|
self.register_inputs(port)
|
|
|
|
|
|
|
|
|
|
# This is the memory array itself
|
|
|
|
|
self.vf.write("reg [DATA_WIDTH-1:0] mem [0:RAM_DEPTH-1];\n")
|
|
|
|
|
|
|
|
|
|
for port in self.all_ports:
|
|
|
|
|
if port in self.write_ports:
|
|
|
|
|
self.add_write_block(port)
|
|
|
|
|
if port in self.read_ports:
|
|
|
|
|
self.add_read_block(port)
|
2020-11-03 15:29:17 +01:00
|
|
|
|
|
|
|
|
self.vf.write("\n")
|
2016-11-08 18:57:35 +01:00
|
|
|
self.vf.write("endmodule\n")
|
2019-01-11 23:15:16 +01:00
|
|
|
self.vf.close()
|
2016-11-08 18:57:35 +01:00
|
|
|
|
2019-01-11 23:15:16 +01:00
|
|
|
def register_inputs(self, port):
|
|
|
|
|
"""
|
|
|
|
|
Register the control signal, address and data inputs.
|
|
|
|
|
"""
|
|
|
|
|
self.add_regs(port)
|
|
|
|
|
self.add_flops(port)
|
2020-11-03 15:29:17 +01:00
|
|
|
|
2019-01-11 23:15:16 +01:00
|
|
|
def add_regs(self, port):
|
2020-11-03 15:29:17 +01:00
|
|
|
"""
|
2019-01-11 23:15:16 +01:00
|
|
|
Create the input regs for the given port.
|
|
|
|
|
"""
|
2019-01-11 23:16:57 +01:00
|
|
|
self.vf.write(" reg csb{0}_reg;\n".format(port))
|
2019-01-11 23:15:16 +01:00
|
|
|
if port in self.readwrite_ports:
|
2019-01-11 23:16:57 +01:00
|
|
|
self.vf.write(" reg web{0}_reg;\n".format(port))
|
2019-07-06 00:08:59 +02:00
|
|
|
if port in self.write_ports:
|
2019-08-21 23:29:57 +02:00
|
|
|
if self.write_size:
|
2019-07-19 23:58:37 +02:00
|
|
|
self.vf.write(" reg [NUM_WMASKS-1:0] wmask{0}_reg;\n".format(port))
|
2019-08-21 22:45:34 +02:00
|
|
|
self.vf.write(" reg [ADDR_WIDTH-1:0] addr{0}_reg;\n".format(port))
|
2019-01-11 23:15:16 +01:00
|
|
|
if port in self.write_ports:
|
2019-08-21 22:45:34 +02:00
|
|
|
self.vf.write(" reg [DATA_WIDTH-1:0] din{0}_reg;\n".format(port))
|
2019-01-11 23:15:16 +01:00
|
|
|
if port in self.read_ports:
|
2019-08-21 22:45:34 +02:00
|
|
|
self.vf.write(" reg [DATA_WIDTH-1:0] dout{0};\n".format(port))
|
2020-11-03 15:29:17 +01:00
|
|
|
|
2019-01-11 23:15:16 +01:00
|
|
|
def add_flops(self, port):
|
|
|
|
|
"""
|
|
|
|
|
Add the flop behavior logic for a port.
|
|
|
|
|
"""
|
|
|
|
|
self.vf.write("\n")
|
|
|
|
|
self.vf.write(" // All inputs are registers\n")
|
|
|
|
|
self.vf.write(" always @(posedge clk{0})\n".format(port))
|
|
|
|
|
self.vf.write(" begin\n")
|
|
|
|
|
self.vf.write(" csb{0}_reg = csb{0};\n".format(port))
|
|
|
|
|
if port in self.readwrite_ports:
|
|
|
|
|
self.vf.write(" web{0}_reg = web{0};\n".format(port))
|
2019-07-06 00:08:59 +02:00
|
|
|
if port in self.write_ports:
|
2019-08-21 23:29:57 +02:00
|
|
|
if self.write_size:
|
2019-07-06 00:08:59 +02:00
|
|
|
self.vf.write(" wmask{0}_reg = wmask{0};\n".format(port))
|
2019-08-21 22:45:34 +02:00
|
|
|
self.vf.write(" addr{0}_reg = addr{0};\n".format(port))
|
2019-09-27 23:18:49 +02:00
|
|
|
if port in self.read_ports:
|
|
|
|
|
self.add_write_read_checks(port)
|
2020-11-03 15:29:17 +01:00
|
|
|
|
2019-01-11 23:15:16 +01:00
|
|
|
if port in self.write_ports:
|
2019-08-21 22:45:34 +02:00
|
|
|
self.vf.write(" din{0}_reg = din{0};\n".format(port))
|
2019-01-11 23:15:16 +01:00
|
|
|
if port in self.read_ports:
|
2020-11-18 01:56:00 +01:00
|
|
|
self.vf.write(" dout{0} = {1}'bx;\n".format(port, self.word_size))
|
2019-01-11 23:15:16 +01:00
|
|
|
if port in self.readwrite_ports:
|
|
|
|
|
self.vf.write(" if ( !csb{0}_reg && web{0}_reg ) \n".format(port))
|
2019-08-21 22:45:34 +02:00
|
|
|
self.vf.write(" $display($time,\" Reading %m addr{0}=%b dout{0}=%b\",addr{0}_reg,mem[addr{0}_reg]);\n".format(port))
|
2019-01-11 23:15:16 +01:00
|
|
|
elif port in self.read_ports:
|
|
|
|
|
self.vf.write(" if ( !csb{0}_reg ) \n".format(port))
|
2019-08-21 22:45:34 +02:00
|
|
|
self.vf.write(" $display($time,\" Reading %m addr{0}=%b dout{0}=%b\",addr{0}_reg,mem[addr{0}_reg]);\n".format(port))
|
2019-01-11 23:15:16 +01:00
|
|
|
if port in self.readwrite_ports:
|
|
|
|
|
self.vf.write(" if ( !csb{0}_reg && !web{0}_reg )\n".format(port))
|
2019-08-21 23:29:57 +02:00
|
|
|
if self.write_size:
|
2019-08-21 22:45:34 +02:00
|
|
|
self.vf.write(" $display($time,\" Writing %m addr{0}=%b din{0}=%b wmask{0}=%b\",addr{0}_reg,din{0}_reg,wmask{0}_reg);\n".format(port))
|
2019-07-06 00:08:59 +02:00
|
|
|
else:
|
2019-08-21 22:45:34 +02:00
|
|
|
self.vf.write(" $display($time,\" Writing %m addr{0}=%b din{0}=%b\",addr{0}_reg,din{0}_reg);\n".format(port))
|
2019-01-11 23:15:16 +01:00
|
|
|
elif port in self.write_ports:
|
|
|
|
|
self.vf.write(" if ( !csb{0}_reg )\n".format(port))
|
2019-08-21 23:29:57 +02:00
|
|
|
if self.write_size:
|
2019-08-21 22:45:34 +02:00
|
|
|
self.vf.write(" $display($time,\" Writing %m addr{0}=%b din{0}=%b wmask{0}=%b\",addr{0}_reg,din{0}_reg,wmask{0}_reg);\n".format(port))
|
2019-07-06 00:08:59 +02:00
|
|
|
else:
|
2019-08-21 22:45:34 +02:00
|
|
|
self.vf.write(" $display($time,\" Writing %m addr{0}=%b din{0}=%b\",addr{0}_reg,din{0}_reg);\n".format(port))
|
2019-07-06 00:08:59 +02:00
|
|
|
|
2019-01-11 23:15:16 +01:00
|
|
|
self.vf.write(" end\n\n")
|
2020-11-03 15:29:17 +01:00
|
|
|
|
2019-01-11 23:15:16 +01:00
|
|
|
def add_inputs_outputs(self, port):
|
|
|
|
|
"""
|
|
|
|
|
Add the module input and output declaration for a port.
|
|
|
|
|
"""
|
2019-01-11 23:16:57 +01:00
|
|
|
self.vf.write(" input clk{0}; // clock\n".format(port))
|
|
|
|
|
self.vf.write(" input csb{0}; // active low chip select\n".format(port))
|
2019-01-11 23:15:16 +01:00
|
|
|
if port in self.readwrite_ports:
|
2019-01-11 23:16:57 +01:00
|
|
|
self.vf.write(" input web{0}; // active low write control\n".format(port))
|
2019-08-21 23:29:57 +02:00
|
|
|
if self.write_size:
|
2019-07-19 23:58:37 +02:00
|
|
|
self.vf.write(" input [NUM_WMASKS-1:0] wmask{0}; // write mask\n".format(port))
|
2019-08-21 22:45:34 +02:00
|
|
|
self.vf.write(" input [ADDR_WIDTH-1:0] addr{0};\n".format(port))
|
2019-01-11 23:15:16 +01:00
|
|
|
if port in self.write_ports:
|
2019-08-21 22:45:34 +02:00
|
|
|
self.vf.write(" input [DATA_WIDTH-1:0] din{0};\n".format(port))
|
2019-01-11 23:15:16 +01:00
|
|
|
if port in self.read_ports:
|
2019-08-21 22:45:34 +02:00
|
|
|
self.vf.write(" output [DATA_WIDTH-1:0] dout{0};\n".format(port))
|
2016-11-08 18:57:35 +01:00
|
|
|
|
2019-01-11 23:15:16 +01:00
|
|
|
def add_write_block(self, port):
|
|
|
|
|
"""
|
|
|
|
|
Add a write port block. Multiple simultaneous writes to the same address
|
|
|
|
|
have arbitrary priority and are not allowed.
|
|
|
|
|
"""
|
|
|
|
|
self.vf.write("\n")
|
|
|
|
|
self.vf.write(" // Memory Write Block Port {0}\n".format(port))
|
|
|
|
|
self.vf.write(" // Write Operation : When web{0} = 0, csb{0} = 0\n".format(port))
|
|
|
|
|
self.vf.write(" always @ (negedge clk{0})\n".format(port))
|
|
|
|
|
self.vf.write(" begin : MEM_WRITE{0}\n".format(port))
|
|
|
|
|
if port in self.readwrite_ports:
|
2019-08-21 23:29:57 +02:00
|
|
|
if self.write_size:
|
2019-07-06 20:29:34 +02:00
|
|
|
self.vf.write(" if ( !csb{0}_reg && !web{0}_reg ) begin\n".format(port))
|
|
|
|
|
else:
|
|
|
|
|
self.vf.write(" if ( !csb{0}_reg && !web{0}_reg )\n".format(port))
|
2019-01-11 23:15:16 +01:00
|
|
|
else:
|
2019-08-21 23:29:57 +02:00
|
|
|
if self.write_size:
|
2019-07-06 20:29:34 +02:00
|
|
|
self.vf.write(" if (!csb{0}_reg) begin\n".format(port))
|
|
|
|
|
else:
|
|
|
|
|
self.vf.write(" if (!csb{0}_reg)\n".format(port))
|
2019-07-04 19:34:14 +02:00
|
|
|
|
2019-08-21 23:29:57 +02:00
|
|
|
if self.write_size:
|
2020-09-28 18:53:01 +02:00
|
|
|
remainder_bits = self.word_size % self.write_size
|
2020-11-18 01:56:00 +01:00
|
|
|
for mask in range(0, self.num_wmasks):
|
2019-07-04 19:34:14 +02:00
|
|
|
lower = mask * self.write_size
|
2020-09-28 18:53:01 +02:00
|
|
|
if (remainder_bits and mask == self.num_wmasks - 1):
|
|
|
|
|
upper = lower + remainder_bits - 1
|
|
|
|
|
else:
|
|
|
|
|
upper = lower + self.write_size - 1
|
2020-11-18 01:56:00 +01:00
|
|
|
self.vf.write(" if (wmask{0}_reg[{1}])\n".format(port, mask))
|
|
|
|
|
self.vf.write(" mem[addr{0}_reg][{1}:{2}] = din{0}_reg[{1}:{2}];\n".format(port, upper, lower))
|
2019-07-06 20:29:34 +02:00
|
|
|
self.vf.write(" end\n")
|
2019-07-04 19:34:14 +02:00
|
|
|
else:
|
2019-08-21 22:45:34 +02:00
|
|
|
self.vf.write(" mem[addr{0}_reg] = din{0}_reg;\n".format(port))
|
2019-01-11 23:15:16 +01:00
|
|
|
self.vf.write(" end\n")
|
2020-11-03 15:29:17 +01:00
|
|
|
|
2019-01-11 23:15:16 +01:00
|
|
|
def add_read_block(self, port):
|
|
|
|
|
"""
|
|
|
|
|
Add a read port block.
|
|
|
|
|
"""
|
|
|
|
|
self.vf.write("\n")
|
|
|
|
|
self.vf.write(" // Memory Read Block Port {0}\n".format(port))
|
|
|
|
|
self.vf.write(" // Read Operation : When web{0} = 1, csb{0} = 0\n".format(port))
|
|
|
|
|
self.vf.write(" always @ (negedge clk{0})\n".format(port))
|
|
|
|
|
self.vf.write(" begin : MEM_READ{0}\n".format(port))
|
|
|
|
|
if port in self.readwrite_ports:
|
|
|
|
|
self.vf.write(" if (!csb{0}_reg && web{0}_reg)\n".format(port))
|
|
|
|
|
else:
|
|
|
|
|
self.vf.write(" if (!csb{0}_reg)\n".format(port))
|
2019-08-21 22:45:34 +02:00
|
|
|
self.vf.write(" dout{0} <= #(DELAY) mem[addr{0}_reg];\n".format(port))
|
2019-01-11 23:15:16 +01:00
|
|
|
self.vf.write(" end\n")
|
2019-09-27 23:18:49 +02:00
|
|
|
|
|
|
|
|
def add_address_check(self, wport, rport):
|
|
|
|
|
""" Output a warning if the two addresses match """
|
|
|
|
|
# If the rport is actually reading... and addresses match.
|
|
|
|
|
if rport in self.readwrite_ports:
|
|
|
|
|
rport_control = "!csb{0} && web{0}".format(rport)
|
|
|
|
|
else:
|
|
|
|
|
rport_control = "!csb{0}".format(rport)
|
|
|
|
|
if wport in self.readwrite_ports:
|
|
|
|
|
wport_control = "!csb{0} && !web{0}".format(wport)
|
|
|
|
|
else:
|
|
|
|
|
wport_control = "!csb{0}".format(wport)
|
2020-11-03 15:29:17 +01:00
|
|
|
|
2020-11-18 01:56:00 +01:00
|
|
|
self.vf.write(" if ({1} && {3} && (addr{0} == addr{2}))\n".format(wport, wport_control, rport, rport_control))
|
|
|
|
|
self.vf.write(" $display($time,\" WARNING: Writing and reading addr{0}=%b and addr{1}=%b simultaneously!\",addr{0},addr{1});\n".format(wport, rport))
|
2019-09-27 23:18:49 +02:00
|
|
|
|
|
|
|
|
def add_write_read_checks(self, rport):
|
2020-11-03 15:29:17 +01:00
|
|
|
"""
|
2019-09-27 23:18:49 +02:00
|
|
|
Add a warning if we read from an address that we are currently writing.
|
|
|
|
|
Can be fixed if we appropriately size the write drivers to do this .
|
|
|
|
|
"""
|
|
|
|
|
for wport in self.write_ports:
|
|
|
|
|
if wport == rport:
|
|
|
|
|
continue
|
|
|
|
|
else:
|
2020-11-18 01:56:00 +01:00
|
|
|
self.add_address_check(wport, rport)
|