-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_mem.sv
65 lines (60 loc) · 1.86 KB
/
data_mem.sv
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
module data_mem
(
input logic clk,
input logic rd_en,
input logic wr_en,
input logic [31:0] addr,
input logic [ 2:0] mem_acc_mode,
input logic [31:0] wdata,
output logic [31:0] rdata
);
parameter BYTE = 3'b000;
parameter HALFWORD = 3'b001;
parameter WORD = 3'b010;
parameter BYTE_UNSIGNED = 3'b011;
parameter HALFWORD_UNSIGNED = 3'b100;
logic [7:0] data_mem [100];
// Here I'm using big-endian byte ordering in which MSB is stored at lower address and LSB at higher address
// asynchronous read
always_comb
begin
if (rd_en)
begin
case (mem_acc_mode)
BYTE:
rdata = $signed (data_mem[addr]) ;
HALFWORD:
rdata = $signed({data_mem[addr], data_mem[addr+1]});
WORD:
rdata = $signed({data_mem[addr], data_mem[addr+1], data_mem[addr+2], data_mem[addr+3]});
BYTE_UNSIGNED:
rdata = {24'b0, {data_mem[addr]}};
HALFWORD_UNSIGNED:
rdata = {16'b0, {data_mem[addr]}, {data_mem[addr+1]}};
endcase
end
end
//synchronous write
always_ff @(posedge clk)
begin
if (wr_en)
begin
case (mem_acc_mode)
BYTE:
data_mem[addr] <= wdata[7:0];
HALFWORD:
begin
data_mem[addr ] <= wdata[15:8];
data_mem[addr+1] <= wdata[ 7:0];
end
WORD:
begin
data_mem[addr ] <= wdata[31:24];
data_mem[addr+1] <= wdata[23:16];
data_mem[addr+2] <= wdata[15: 8];
data_mem[addr+3] <= wdata[ 7: 0];
end
endcase
end
end
endmodule