-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory.v
37 lines (31 loc) · 1.02 KB
/
memory.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
module memory (
input clk,
input reset,
input mem_read_control,
input mem_write_control,
input [31:0] mem_data_in,
input [31:0] mem_address,
output reg [31:0] mem_data_out
);
parameter MEMORY_SIZE = 256; // 1KB memory
reg [31:0] memory [MEMORY_SIZE-1:0]; // memory init
// memory read
always @(*) begin
if (mem_read_control) begin
mem_data_out = memory[mem_address[31:2]];
end else begin
mem_data_out = 32'b0;
end
end
// memory write
always @(posedge clk or posedge reset) begin
if (reset) begin
integer i;
for (i = 0; i < MEMORY_SIZE; i = i + 1) begin
memory[i] <= 32'b0;
end
end else if (mem_write_control) begin
memory[mem_address[31:2]] <= mem_data_in;
end
end
endmodule