-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataMemory.sv
87 lines (83 loc) · 2.15 KB
/
DataMemory.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
module DataMemory(input logic [31:0] addr, wdata, input logic [2:0] mask, input logic wr_en, rd_en, clk,
output logic [31:0] rdata);
logic [31:0] memory [1023:0]; // 1 KB Memory size
logic [31:0] data, write_data;
always_comb begin
if (rd_en)
data <= memory[addr[31:2]];
else
data <= 0;
end
always_comb begin
if (rd_en) begin // Load instruction
case (mask)
3'b000: begin // Load byte (Signed)
case (addr[1:0])
0: rdata <= {{24{data[7]}}, data[7:0]};
1: rdata <= {{24{data[15]}}, data[15:8]};
2: rdata <= {{24{data[23]}}, data[23:16]};
3: rdata <= {{24{data[31]}}, data[31:24]};
default rdata <= 0;
endcase
end
3'b001: begin // Load halfword (Signed)
case (addr[1])
0: rdata <= {{16{data[15]}}, data[15:0]};
1: rdata <= {{16{data[31]}}, data[31:16]};
default rdata <= 0;
endcase
end
3'b010: begin // Load word
rdata <= data;
end
3'b100: begin // Load byte (Unsigned)
case (addr[1:0])
0: rdata <= {24'b0, data[7:0]};
1: rdata <= {24'b0, data[15:8]};
2: rdata <= {24'b0, data[23:16]};
3: rdata <= {24'b0, data[31:24]};
default rdata <= 0;
endcase
end
3'b101: begin // Load halfword (Unsigned)
case (addr[1])
0: rdata <= {16'b0, data[15:0]};
1: rdata <= {16'b0, data[31:16]};
default rdata <= 0;
endcase
end
default: rdata <= 0;
endcase
end
end
always_comb begin
if (wr_en) begin // S-type instruction
case (mask)
3'b000: begin // Store byte
case (addr[1:0])
0: write_data <= {24'b0, wdata[7:0]};
1: write_data <= {24'b0, wdata[15:8]};
2: write_data <= {24'b0, wdata[23:16]};
3: write_data <= {24'b0, wdata[31:24]};
default ;
endcase
end
3'b001: begin // Store halfword
case (addr[1])
0: write_data <= {16'b0, wdata[15:0]};
1: write_data <= {16'b0, wdata[31:16]};
default ;
endcase
end
3'b010: begin // Store word
write_data <= wdata;
end
endcase
end
end
always_ff @(negedge clk) begin
if (wr_en) begin
memory[addr[31:2]] <= write_data;
end
end
endmodule