-
Notifications
You must be signed in to change notification settings - Fork 0
/
L1C_data.sv
executable file
·293 lines (251 loc) · 8.4 KB
/
L1C_data.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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
//================================================
// Auther: Chen Yun-Ru (May)
// Filename: L1C_data.sv
// Description: L1 Cache for data
// Version: 0.1
//================================================
`include "../include/def.svh"
module L1C_data(
input clk,
input rst,
// Core to CPU wrapper
input [`DATA_BITS-1:0] core_addr,
input core_req,
input core_write,
input [`DATA_BITS-1:0] core_in,
input [`CACHE_TYPE_BITS-1:0] core_type,
// Mem to CPU wrapper
input [`DATA_BITS-1:0] D_out,
input D_wait,
// CPU wrapper to core
output logic [`DATA_BITS-1:0] core_out,
output core_wait,
// CPU wrapper to Mem
output logic D_req,
output logic [`DATA_BITS-1:0] D_addr,
output D_write,
output [`DATA_BITS-1:0] D_in,
output [`CACHE_TYPE_BITS-1:0] D_type
);
logic [`CACHE_INDEX_BITS-1:0] index;
logic [`CACHE_DATA_BITS-1:0] DA_out;
logic [`CACHE_DATA_BITS-1:0] DA_in;
logic [`CACHE_WRITE_BITS-1:0] DA_write;
logic DA_read;
logic [`CACHE_TAG_BITS-1:0] TA_out;
logic [`CACHE_TAG_BITS-1:0] TA_in;
logic TA_write;
logic TA_read;
logic [`CACHE_LINES-1:0] valid;
//--------------- complete this part by yourself -----------------//
data_array_wrapper DA(
.A(index),
.DO(DA_out),
.DI(DA_in),
.CK(clk),
.WEB(DA_write),
.OE(DA_read),
.CS(1'b1)
);
tag_array_wrapper TA(
.A(index),
.DO(TA_out),
.DI(TA_in),
.CK(clk),
.WEB(TA_write),
.OE(TA_read),
.CS(1'b1)
);
logic [1:0] counter;
logic[2:0] state, nxt_state;
assign DA_read = 1'b1;
assign TA_read = 1'b1;
localparam Idle = 3'd0;
localparam Judge_R = 3'd1;
localparam ReadMiss = 3'd2; // read a line from memory
localparam Uncacheable = 3'd3;
localparam Judge_W = 3'd4;
localparam WriteHit = 3'd5; // write data into cache and memory
localparam WriteMiss = 3'd6; // only write data into memory
// instant for data accessing
assign index = core_addr[9:4];
logic [1:0] offset;
logic TA_match;
logic cacheable;
assign TA_in = core_addr [31:10];
assign offset = core_addr [3:2];
assign TA_match = (TA_in == TA_out);
assign cacheable = core_addr [31:10] != 22'h040000;
//Read
always_ff @( posedge clk) begin
if(rst)state <= 3'b0;
else state <= nxt_state;
end
// comb circuit for nxt_state_R
always_comb begin
case(state)
Idle : nxt_state = (core_req)? ((core_write)? Judge_W : Judge_R) : Idle;
Judge_R : nxt_state = (TA_match && valid[index])? Idle : ReadMiss; // TA_out is obtaion by core_addr in Idle
ReadMiss : begin
if(cacheable) begin
nxt_state = (counter == 2'b11 && !D_wait)? Judge_R : ReadMiss;
end
else begin
nxt_state = Uncacheable;
end
end
Uncacheable : nxt_state = (!D_wait)? Idle : Uncacheable;
Judge_W : nxt_state = (TA_match && valid[index])? WriteHit : WriteMiss;
WriteHit : nxt_state = (!D_wait)? Idle : WriteHit;
WriteMiss : nxt_state = (!D_wait)? Idle : WriteMiss;
default : nxt_state = Idle;
endcase
end
// T: total, M: miss, H: hit
logic [31:0] RTCnt,RMCnt, WHCnt, WMCnt;
always_ff @( posedge clk) begin
if(rst)begin
RTCnt <= 32'b0;
RMCnt <= 32'b0;
WHCnt <= 32'b0;
WMCnt <= 32'b0;
end
else begin
// Read
if(state == Judge_R) begin
if(valid[index] && (TA_match)) begin
RTCnt <= RTCnt + 32'b1;
end
else begin
RMCnt <= RMCnt + 32'b1;
end
end
// Write
if(state == Judge_W) begin
if(valid[index] && (TA_match)) begin
WHCnt <= WHCnt + 32'b1;
end
else begin
WMCnt <= WMCnt + 32'b1;
end
end
end
end
logic Hit, Miss;
assign Hit = (state == Judge_R || state == Judge_W) && valid[index] && (TA_match);
assign Miss = (state == Judge_R || state == Judge_W) && !(valid[index] && (TA_match));
always_comb begin
// core_wait and core_out when Read Hit
if ((state == Judge_R && (TA_match && valid[index]))) begin
case(offset)
2'b00 : core_out = DA_out[31:0];
2'b01 : core_out = DA_out[63:32];
2'b10 : core_out = DA_out[95:64];
2'b11 : core_out = DA_out[127:96];
endcase
end
else if(state == Uncacheable && !D_wait) begin
core_out = D_out;
end
else begin
core_out = 32'b0;
end
end
assign core_wait = ((state==Judge_R) && (TA_match && valid[index])) || (state == Uncacheable && !D_wait)? 1'b0 : 1'b1;
assign D_req = (state == ReadMiss || state == WriteMiss || state == WriteHit)? 1'b1 : 1'b0;
assign D_addr = (state == ReadMiss || state == WriteMiss || state == WriteHit)? core_addr : 32'b0;
always_ff @( posedge clk) begin
if(rst) begin
counter <= 2'b0;
valid <= 64'b0;
end
else begin
if(state == ReadMiss) begin
counter <= (!D_wait)? counter + 2'b1 : counter;
end
else begin
counter <= 2'b0;
end
if(state == ReadMiss && !D_wait && counter == 2'b11) begin
valid[index] <= 1'b1;
end
end
end
assign D_write = (state == WriteHit || state == WriteMiss)? 1'b1 : 1'b0;
assign D_in = (state == WriteHit || state == WriteMiss)? core_in : 32'b0;
assign D_type = core_type;
logic[3:0] data_type, data_type_shf;
logic[31:0] core_in_shf;
assign data_type = (core_type == `CACHE_BYTE) ? 4'b1110:
(core_type == `CACHE_HWORD)? 4'b1100: 4'b0000;
always_comb begin
case(core_addr[1:0])
// left
2'd0: begin
data_type_shf = data_type;
core_in_shf = core_in;
end
2'd1: begin
data_type_shf = {data_type[2:0],data_type[3]};
core_in_shf = {core_in[23:0],core_in[31:24]};
end
2'd2: begin
data_type_shf = {data_type[1:0],data_type[3:2]};
core_in_shf = {core_in[15:0],core_in[31:16]};
end
2'd3: begin
data_type_shf = {data_type[0],data_type[3:1]};
core_in_shf = {core_in[7:0],core_in[31:8]};
end
endcase
end
always_comb begin
if(state == ReadMiss && !D_wait) begin
case(counter)
2'b00:begin
DA_in = {96'b0,D_out};
DA_write = {16'hFFF0};
end
2'b01:begin
DA_in = {64'b0,D_out,32'b0};
DA_write = {16'hFF0F};
end
2'b10:begin
DA_in = {32'b0,D_out,64'b0};
DA_write = {16'hF0FF};
end
2'b11:begin
DA_in = {D_out,96'b0};
DA_write = {16'h0FFF};
end
endcase
TA_write = 1'b0;
end
else if(state == WriteHit) begin
case(offset)
2'b00:begin
DA_in = {96'b0,core_in_shf};
DA_write = {12'hFFF,data_type_shf};
end
2'b01:begin
DA_in = {64'b0,core_in_shf,32'b0};
DA_write = {8'hFF, data_type_shf, 4'hF};
end
2'b10:begin
DA_in = {32'b0,core_in_shf,64'b0};
DA_write = {4'hF, data_type_shf, 8'hFF};
end
2'b11:begin
DA_in = {core_in_shf,96'b0};
DA_write = {data_type_shf,12'hFFF};
end
endcase
TA_write = 1'b1;
end
else begin
DA_in = 128'b0;
DA_write = 16'hFFFF;
TA_write = 1'b1;
end
end
endmodule