-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMEM.v
179 lines (163 loc) · 4.97 KB
/
MEM.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
`include "lib/defines.vh"
module MEM(
input wire clk,
input wire rst,
input wire flush,
input wire [`StallBus-1:0] stall,
input wire [`EX_TO_MEM_WD-1:0] ex_to_mem_bus,
input wire [31:0] data_sram_rdata,
output wire [`MEM_TO_WB_WD-1:0] mem_to_wb_bus
);
reg [`EX_TO_MEM_WD-1:0] ex_to_mem_bus_r;
always @ (posedge clk) begin
if (rst) begin
ex_to_mem_bus_r <= `EX_TO_MEM_WD'b0;
end
else if (flush) begin
ex_to_mem_bus_r <= `EX_TO_MEM_WD'b0;
end
else if (stall[3]==`Stop && stall[4]==`NoStop) begin
ex_to_mem_bus_r <= `EX_TO_MEM_WD'b0;
end
else if (stall[3]==`NoStop) begin
ex_to_mem_bus_r <= ex_to_mem_bus;
end
end
wire [65:0] hilo_bus;
wire [31:0] mem_pc;
wire data_ram_en;
wire [3:0] data_ram_wen;
wire sel_rf_res;
wire [4:0] mem_op;
wire rf_we;
wire [4:0] rf_waddr;
wire [31:0] rf_wdata;
wire [31:0] ex_result;
wire [31:0] mem_result;
assign {
hilo_bus, // 146:81
mem_op, // 80:76
mem_pc, // 75:44
data_ram_en, // 43
data_ram_wen, // 42:39
sel_rf_res, // 38
rf_we, // 37
rf_waddr, // 36:32
ex_result // 31:0
} = ex_to_mem_bus_r;
wire inst_lb, inst_lbu, inst_lh, inst_lhu, inst_lw;
assign {
inst_lb,
inst_lbu,
inst_lh,
inst_lhu,
inst_lw
} = mem_op;
//Load Data
reg [31:0] mem_result_r;
always @ (*) begin
case(1'b1)
inst_lb:
begin
case(ex_result[1:0])
2'b00:
begin
mem_result_r <= {{24{data_sram_rdata[7]}},data_sram_rdata[7:0]};
end
2'b01:
begin
mem_result_r <= {{24{data_sram_rdata[15]}},data_sram_rdata[15:8]};
end
2'b10:
begin
mem_result_r <= {{24{data_sram_rdata[23]}},data_sram_rdata[23:16]};
end
2'b11:
begin
mem_result_r <= {{24{data_sram_rdata[31]}},data_sram_rdata[31:24]};
end
default:
begin
mem_result_r <= 32'b0;
end
endcase
end
inst_lbu:
begin
case(ex_result[1:0])
2'b00:
begin
mem_result_r <= {{24{1'b0}},data_sram_rdata[7:0]};
end
2'b01:
begin
mem_result_r <= {{24{1'b0}},data_sram_rdata[15:8]};
end
2'b10:
begin
mem_result_r <= {{24{1'b0}},data_sram_rdata[23:16]};
end
2'b11:
begin
mem_result_r <= {{24{1'b0}},data_sram_rdata[31:24]};
end
default:
begin
mem_result_r <= 32'b0;
end
endcase
end
inst_lh:
begin
case(ex_result[1:0])
2'b00:
begin
mem_result_r <= {{16{data_sram_rdata[15]}},data_sram_rdata[15:0]};
end
2'b10:
begin
mem_result_r <= {{16{data_sram_rdata[31]}},data_sram_rdata[31:16]};
end
default:
begin
mem_result_r <= 32'b0;
end
endcase
end
inst_lhu:
begin
case(ex_result[1:0])
2'b00:
begin
mem_result_r <= {{16{1'b0}},data_sram_rdata[15:0]};
end
2'b10:
begin
mem_result_r <= {{16{1'b0}},data_sram_rdata[31:16]};
end
default:
begin
mem_result_r <= 32'b0;
end
endcase
end
inst_lw:
begin
mem_result_r <= data_sram_rdata;
end
default:
begin
mem_result_r <= 32'b0;
end
endcase
end
assign mem_result = mem_result_r;
assign rf_wdata = sel_rf_res ? mem_result : ex_result;
assign mem_to_wb_bus = {
hilo_bus, // 135:70
mem_pc, // 69:38
rf_we, // 37
rf_waddr, // 36:32
rf_wdata // 31:0
};
endmodule