-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_memory.v
67 lines (59 loc) · 1.51 KB
/
data_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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
`timescale 1ns/100ps
module data_memory(
clock,
reset,
read,
write,
address,
writedata,
readdata,
busywait
);
input clock;
input reset;
input read;
input write;
input [5:0] address;
input [31:0] writedata;
output reg [31:0] readdata;
output reg busywait;
//Declare memory array 256x8-bits
reg [7:0] memory_array [255:0];
//Detecting an incoming memory access
reg readaccess, writeaccess;
always @(read, write) begin
busywait = (read || write)? 1 : 0;
readaccess = (read && !write)? 1 : 0;
writeaccess = (!read && write)? 1 : 0;
end
//Reading & writing
always @(posedge clock) begin
if(readaccess) begin
readdata[7:0] = #40 memory_array[{address,2'b00}];
readdata[15:8] = #40 memory_array[{address,2'b01}];
readdata[23:16] = #40 memory_array[{address,2'b10}];
readdata[31:24] = #40 memory_array[{address,2'b11}];
busywait = 0;
readaccess = 0;
end
if(writeaccess) begin
memory_array[{address,2'b00}] = #40 writedata[7:0];
memory_array[{address,2'b01}] = #40 writedata[15:8];
memory_array[{address,2'b10}] = #40 writedata[23:16];
memory_array[{address,2'b11}] = #40 writedata[31:24];
busywait = 0;
writeaccess = 0;
end
end
integer i;
//Reset memory
always @(posedge reset) begin
if (reset) begin
for (i = 0; i < 256; i = i + 1)
memory_array[i] = 0;
busywait = 0;
readaccess = 0;
writeaccess = 0;
end
end
endmodule