2020-11-29 08:06:18 +00:00
|
|
|
module mips_cpu_memory(
|
2020-12-02 14:41:04 +00:00
|
|
|
input logic clk,
|
2020-11-29 08:06:18 +00:00
|
|
|
|
|
|
|
//Data Memory
|
|
|
|
input logic[31:0] data_address,
|
|
|
|
input logic data_write,
|
|
|
|
input logic data_read,
|
|
|
|
input logic[31:0] data_writedata,
|
|
|
|
output logic[31:0] data_readdata,
|
|
|
|
|
|
|
|
//Instruction Memory
|
|
|
|
input logic[31:0] instr_address,
|
|
|
|
output logic[31:0] instr_readdata
|
|
|
|
|
|
|
|
);
|
2020-12-13 05:54:53 +00:00
|
|
|
parameter INSTR_INIT_FILE = "";
|
|
|
|
parameter DATA_INIT_FILE = "";
|
2020-12-16 15:29:04 +00:00
|
|
|
reg [31:0] data_memory [0:63];
|
2020-12-16 15:00:46 +00:00
|
|
|
reg [31:0] instr_memory [0:63];
|
2020-11-29 08:06:18 +00:00
|
|
|
|
|
|
|
initial begin
|
|
|
|
integer i;
|
|
|
|
//Initialise to zero by default
|
2020-12-10 10:14:16 +00:00
|
|
|
for (i=0; i<$size(data_memory); i++) begin
|
|
|
|
data_memory[i] = 0;
|
|
|
|
end
|
|
|
|
for (i=0; i<$size(instr_memory); i++) begin
|
|
|
|
instr_memory[i] = 0;
|
2020-11-29 08:06:18 +00:00
|
|
|
end
|
|
|
|
//Load contents from file if specified
|
2020-12-13 05:54:53 +00:00
|
|
|
if (INSTR_INIT_FILE != "") begin
|
|
|
|
$display("RAM: Loading RAM contents from %s", INSTR_INIT_FILE);
|
|
|
|
$readmemh(INSTR_INIT_FILE, instr_memory);
|
2020-11-29 08:06:18 +00:00
|
|
|
end
|
|
|
|
|
2020-12-10 10:14:16 +00:00
|
|
|
for (integer j = 0; j<$size(instr_memory); j++) begin
|
|
|
|
$display("byte +%h: %h", 32'hBFC00000+j*4, instr_memory[j]);
|
2020-12-06 06:44:58 +00:00
|
|
|
end
|
2020-12-11 10:45:13 +00:00
|
|
|
|
2020-12-13 05:54:53 +00:00
|
|
|
if (DATA_INIT_FILE != "") begin
|
|
|
|
$display("MEM: Loading MEM contents from %s", DATA_INIT_FILE);
|
|
|
|
$readmemh(DATA_INIT_FILE, data_memory);
|
2020-12-11 10:45:13 +00:00
|
|
|
end else begin
|
|
|
|
$display("MEM FILE NOT GIVEN");
|
|
|
|
end
|
|
|
|
|
|
|
|
for (integer k = 0; k<$size(data_memory); k++) begin
|
|
|
|
$display("byte +%h: %h", 32'h00001000+k*4, data_memory[k]);
|
|
|
|
end
|
2020-11-29 08:44:08 +00:00
|
|
|
end
|
|
|
|
|
2020-12-09 07:47:58 +00:00
|
|
|
//Combinatorial read path for data and instruction.
|
2020-12-11 10:45:13 +00:00
|
|
|
assign data_readdata = data_read ? data_memory[(data_address-32'h00001000)>>2] : 32'hxxxxxxxx;
|
2020-12-10 10:14:16 +00:00
|
|
|
assign instr_readdata = (instr_address >= 32'hBFC00000 && instr_address < 32'hBFC00000+$size(instr_memory)) ? instr_memory[(instr_address-32'hBFC00000)>>2] : 32'hxxxxxxxx;
|
2020-12-09 07:47:58 +00:00
|
|
|
|
2020-11-29 08:06:18 +00:00
|
|
|
|
|
|
|
//Synchronous write path
|
2020-12-02 14:41:04 +00:00
|
|
|
always_ff @(posedge clk) begin
|
2020-12-16 08:38:46 +00:00
|
|
|
//$display("Instruction: %h", instr_readdata);
|
2020-11-29 08:44:08 +00:00
|
|
|
//$display("RAM : INFO : data_read=%h, data_address = %h, mem=%h", data_read, data_address, memory[data_address]);
|
2020-12-13 05:40:16 +00:00
|
|
|
if (data_write) begin //cannot read and write to memory in the same cycle
|
2020-11-29 08:44:08 +00:00
|
|
|
if (instr_address != data_address) begin //cannot modify the instruction being read
|
2020-12-13 05:40:16 +00:00
|
|
|
data_memory[(data_address-32'h00001000)>>2] <= data_writedata;
|
|
|
|
$display("Store in memory");
|
|
|
|
$display(data_writedata);
|
|
|
|
end
|
|
|
|
for (integer k = 0; k<$size(data_memory); k++) begin
|
|
|
|
$display("byte +%h: %h", 32'h00001000+k*4, data_memory[k]);
|
2020-11-29 08:44:08 +00:00
|
|
|
end
|
2020-11-29 08:06:18 +00:00
|
|
|
end
|
2020-12-13 05:40:16 +00:00
|
|
|
|
2020-11-29 08:06:18 +00:00
|
|
|
end
|
2020-11-29 08:44:08 +00:00
|
|
|
endmodule
|
2020-12-11 10:45:13 +00:00
|
|
|
|