-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathl1_cache_dm_8kb_simple.v
More file actions
222 lines (198 loc) · 7.78 KB
/
l1_cache_dm_8kb_simple.v
File metadata and controls
222 lines (198 loc) · 7.78 KB
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
// Simplified SINGLE-CLOCK L1 D-Cache (NO CDC issues!)
// This version uses only CPU clock - memory is modeled as taking multiple cycles
`include "alu_gates_64.v"
`include "addr_alu.v"
`include "regs_and_counters.v"
module l1_cache_dm_8kb_simple #(
parameter ADDR_W=32, LINE_BYTES=64, LINE_BITS=512, L1_BYTES=8192,
parameter LINES=L1_BYTES/LINE_BYTES, BUS_BYTES=8
)(
// Single clock domain
input wire clk, rst,
input wire cpu_req_valid, cpu_req_write,
input wire [ADDR_W-1:0] cpu_req_addr,
input wire [31:0] cpu_req_wdata,
output reg cpu_req_ready,
output reg cpu_resp_valid,
output reg [31:0] cpu_resp_rdata,
// Per-phase controls
input wire stat_clear,
input wire invalidate,
// Stats out
output wire [31:0] stat_accesses,
output wire [31:0] stat_misses,
output wire [31:0] stat_mem_txns,
output wire [31:0] stat_beats,
output wire [63:0] stat_bytes
);
localparam OFFSET_BITS=6, INDEX_BITS=7, TAG_BITS=ADDR_W-OFFSET_BITS-INDEX_BITS;
reg valid [0:LINES-1];
reg dirty [0:LINES-1];
reg [TAG_BITS-1:0] tags [0:LINES-1];
reg [LINE_BITS-1:0] data [0:LINES-1];
integer ii;
// Latched request
reg rq_write;
reg [ADDR_W-1:0] rq_addr;
reg [31:0] rq_wdata;
reg [INDEX_BITS-1:0] rq_index;
reg [TAG_BITS-1:0] rq_tag;
reg [OFFSET_BITS-1:0]rq_off;
wire [3:0] word_sel = rq_off[5:2];
// Tag compare
wire [TAG_BITS-1:0] tag_xor = tags[rq_index] ^ rq_tag;
wire tag_diff_any = |tag_xor;
wire hit = valid[rq_index] && !tag_diff_any;
typedef enum logic [2:0] {S_IDLE,S_LOOKUP,S_EVICT,S_FILL,S_RESP} state_t;
state_t cstate;
// Beat counter for memory transfers
reg [2:0] beat;
reg [ADDR_W-1:0] cur_addr;
wire [ADDR_W-1:0] line_base = {rq_addr[ADDR_W-1:OFFSET_BITS], {OFFSET_BITS{1'b0}} };
wire [ADDR_W-1:0] cur_addr_plus8;
add8 #(ADDR_W) ADD8(.a(cur_addr), .y(cur_addr_plus8));
// Memory model: simple array (instant access, but we model latency)
reg [7:0] memory [0:4*1024*1024-1];
reg [2:0] mem_delay; // Cycles to wait per beat
// Stats
reg stat_clear_q;
always @(posedge clk) stat_clear_q <= stat_clear;
wire acc_inc = (cstate==S_IDLE) && cpu_req_valid && cpu_req_ready;
wire miss_inc = (cstate==S_LOOKUP) && !hit;
wire beat_inc = ((cstate==S_EVICT || cstate==S_FILL) && mem_delay==0);
counter_inc1 #(32) CACC (.clk(clk), .rst(rst), .clr(stat_clear_q), .inc(acc_inc), .q(stat_accesses));
counter_inc1 #(32) CMIS (.clk(clk), .rst(rst), .clr(stat_clear_q), .inc(miss_inc), .q(stat_misses));
counter_inc1 #(32) CBEAT(.clk(clk), .rst(rst), .clr(stat_clear_q), .inc(beat_inc), .q(stat_beats));
counter_inc8 #(64) CBYTE(.clk(clk), .rst(rst), .clr(stat_clear_q), .inc8(beat_inc), .q(stat_bytes));
reg txn_inc;
counter_inc1 #(32) CTXN (.clk(clk), .rst(rst), .clr(stat_clear_q), .inc(txn_inc), .q(stat_mem_txns));
// Main FSM
always @(posedge clk) begin
if (rst) begin
cstate <= S_IDLE;
cpu_req_ready <= 1'b1;
cpu_resp_valid<= 1'b0;
beat <= 0;
mem_delay <= 0;
txn_inc <= 0;
for (ii=0; ii<LINES; ii=ii+1) begin
valid[ii]<=1'b0; dirty[ii]<=1'b0;
tags[ii]<={TAG_BITS{1'b0}}; data[ii]<={LINE_BITS{1'b0}};
end
end else begin
cpu_resp_valid <= 1'b0;
txn_inc <= 0;
if (invalidate) begin
for (ii = 0; ii < LINES; ii = ii + 1) begin
valid[ii] <= 1'b0;
dirty[ii] <= 1'b0;
end
end
case (cstate)
S_IDLE: begin
cpu_req_ready <= 1'b1;
if (cpu_req_valid && cpu_req_ready) begin
rq_write<=cpu_req_write; rq_addr<=cpu_req_addr; rq_wdata<=cpu_req_wdata;
rq_off <=cpu_req_addr[OFFSET_BITS-1:0];
rq_index<=cpu_req_addr[OFFSET_BITS +: INDEX_BITS];
rq_tag <=cpu_req_addr[OFFSET_BITS+INDEX_BITS +: TAG_BITS];
cpu_req_ready <= 1'b0;
cstate <= S_LOOKUP;
end
end
S_LOOKUP: begin
if (hit) begin
if (rq_write) begin
data[rq_index][ (word_sel*32) +: 32 ] <= rq_wdata;
dirty[rq_index] <= 1'b1;
end else begin
cpu_resp_rdata <= data[rq_index][ (word_sel*32) +: 32 ];
end
cpu_resp_valid <= 1'b1;
cpu_req_ready <= 1'b1;
cstate <= S_IDLE;
end else begin
// Miss - need to evict if dirty, then fill
if (valid[rq_index] && dirty[rq_index]) begin
beat <= 0;
mem_delay <= 3'd4; // 5 cycles per beat (200MHz mem / 1GHz cpu)
cur_addr <= {tags[rq_index], rq_index, {OFFSET_BITS{1'b0}}};
txn_inc <= 1;
cstate <= S_EVICT;
end else begin
beat <= 0;
mem_delay <= 3'd4;
cur_addr <= line_base;
txn_inc <= 1;
cstate <= S_FILL;
end
end
end
S_EVICT: begin
if (mem_delay > 0) begin
mem_delay <= mem_delay - 1;
end else begin
// Write beat to memory
memory[cur_addr + 0] <= data[rq_index][ (beat*64) +: 8 ];
memory[cur_addr + 1] <= data[rq_index][ (beat*64+8) +: 8 ];
memory[cur_addr + 2] <= data[rq_index][ (beat*64+16) +: 8 ];
memory[cur_addr + 3] <= data[rq_index][ (beat*64+24) +: 8 ];
memory[cur_addr + 4] <= data[rq_index][ (beat*64+32) +: 8 ];
memory[cur_addr + 5] <= data[rq_index][ (beat*64+40) +: 8 ];
memory[cur_addr + 6] <= data[rq_index][ (beat*64+48) +: 8 ];
memory[cur_addr + 7] <= data[rq_index][ (beat*64+56) +: 8 ];
if (beat == 7) begin
// Eviction complete, start fill
dirty[rq_index] <= 1'b0;
beat <= 0;
mem_delay <= 3'd4;
cur_addr <= line_base;
txn_inc <= 1; // Count the fill transaction
cstate <= S_FILL;
end else begin
beat <= beat + 1;
cur_addr <= cur_addr_plus8;
mem_delay <= 3'd4;
end
end
end
S_FILL: begin
if (mem_delay > 0) begin
mem_delay <= mem_delay - 1;
end else begin
// Read beat from memory
data[rq_index][ (beat*64) +: 8 ] <= memory[cur_addr + 0];
data[rq_index][ (beat*64+8) +: 8 ] <= memory[cur_addr + 1];
data[rq_index][ (beat*64+16) +: 8 ]<= memory[cur_addr + 2];
data[rq_index][ (beat*64+24) +: 8 ]<= memory[cur_addr + 3];
data[rq_index][ (beat*64+32) +: 8 ]<= memory[cur_addr + 4];
data[rq_index][ (beat*64+40) +: 8 ]<= memory[cur_addr + 5];
data[rq_index][ (beat*64+48) +: 8 ]<= memory[cur_addr + 6];
data[rq_index][ (beat*64+56) +: 8 ]<= memory[cur_addr + 7];
if (beat == 7) begin
// Fill complete
valid[rq_index] <= 1'b1;
tags[rq_index] <= rq_tag;
cstate <= S_RESP;
end else begin
beat <= beat + 1;
cur_addr <= cur_addr_plus8;
mem_delay <= 3'd4;
end
end
end
S_RESP: begin
if (rq_write) begin
data[rq_index][ (word_sel*32) +: 32 ] <= rq_wdata;
dirty[rq_index] <= 1'b1;
end else begin
cpu_resp_rdata <= data[rq_index][ (word_sel*32) +: 32 ];
end
cpu_resp_valid <= 1'b1;
cpu_req_ready <= 1'b1;
cstate <= S_IDLE;
end
endcase
end
end
endmodule