-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdcache_controller.v
247 lines (225 loc) · 7.41 KB
/
dcache_controller.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
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
module dcache_controller
(
// System clock, reset and stall
clk_i,
rst_i,
// to Data Memory interface
mem_data_i,
mem_ack_i,
mem_data_o,
mem_addr_o,
mem_enable_o,
mem_write_o,
// to CPU interface
cpu_data_i,
cpu_addr_i,
cpu_MemRead_i,
cpu_MemWrite_i,
cpu_data_o,
cpu_stall_o
);
//
// System clock, start
//
input clk_i;
input rst_i;
//
// to Data_Memory interface
//
input [255:0] mem_data_i;
input mem_ack_i;
output [255:0] mem_data_o;
output [31:0] mem_addr_o;
output mem_enable_o;
output mem_write_o;
//
// to CPU interface
//
input [31:0] cpu_data_i;
input [31:0] cpu_addr_i;
input cpu_MemRead_i;
input cpu_MemWrite_i;
output [31:0] cpu_data_o;
output cpu_stall_o;
//
// to SRAM interface
//
wire [3:0] cache_sram_index;
wire cache_sram_enable;
wire [24:0] cache_sram_tag;
wire [255:0] cache_sram_data;
wire cache_sram_write;
wire [24:0] sram_cache_tag;
wire [255:0] sram_cache_data;
wire sram_cache_hit;
// cache
wire sram_valid;
wire sram_dirty;
// controller
parameter STATE_IDLE = 3'h0,
STATE_READMISS = 3'h1,
STATE_READMISSOK = 3'h2,
STATE_WRITEBACK = 3'h3,
STATE_MISS = 3'h4;
reg [2:0] state;
reg mem_enable;
reg mem_write;
reg cache_write;
wire cache_dirty;
reg write_back;
// regs & wires
wire [4:0] cpu_offset;
wire [3:0] cpu_index;
wire [22:0] cpu_tag;
wire [255:0] r_hit_data;
wire [21:0] sram_tag;
wire hit;
reg [255:0] w_hit_data;
wire write_hit;
wire cpu_req;
reg [31:0] cpu_data;
// to CPU interface
assign cpu_req = cpu_MemRead_i | cpu_MemWrite_i;
assign cpu_tag = cpu_addr_i[31:9];
assign cpu_index = cpu_addr_i[8:5];
assign cpu_offset = cpu_addr_i[4:0];
assign cpu_stall_o = ~hit & cpu_req;
assign cpu_data_o = cpu_data;
// to SRAM interface
assign sram_valid = sram_cache_tag[24];
assign sram_dirty = sram_cache_tag[23];
assign sram_tag = sram_cache_tag[22:0];
assign cache_sram_index = cpu_index;
assign cache_sram_enable = cpu_req;
assign cache_sram_write = cache_write | write_hit;
assign cache_sram_tag = {1'b1, cache_dirty, cpu_tag};
assign cache_sram_data = (hit) ? w_hit_data : mem_data_i;
// to Data_Memory interface
assign mem_enable_o = mem_enable;
assign mem_addr_o = (write_back) ? {sram_tag, cpu_index, 5'b0} : {cpu_tag, cpu_index, 5'b0};
assign mem_data_o = sram_cache_data;
assign mem_write_o = mem_write;
assign write_hit = hit & cpu_MemWrite_i;
assign cache_dirty = write_hit;
// TODO: add your code here! (r_hit_data=...?)
assign r_hit_data = (hit) ? sram_cache_data : 256'b0;
// read data : 256-bit to 32-bit
always@(cpu_offset or r_hit_data) begin
// TODO: add your code here! (cpu_data=...?)
case (cpu_offset)
5'd0: cpu_data <= r_hit_data[31:0];
5'd4: cpu_data <= r_hit_data[63:32];
5'd8: cpu_data <= r_hit_data[95:64];
5'd12: cpu_data <= r_hit_data[127:96];
5'd16: cpu_data <= r_hit_data[159:128];
5'd20: cpu_data <= r_hit_data[191:160];
5'd24: cpu_data <= r_hit_data[223:192];
5'd28: cpu_data <= r_hit_data[255:224];
endcase
end
// write data : 32-bit to 256-bit
always@(cpu_offset or r_hit_data or cpu_data_i) begin
// TODO: add your code here! (w_hit_data=...?)
w_hit_data <= r_hit_data;
case (cpu_offset)
5'd0: w_hit_data[31:0] <= cpu_data_i;
5'd4: w_hit_data[63:32] <= cpu_data_i;
5'd8: w_hit_data[95:64] <= cpu_data_i;
5'd12: w_hit_data[127:96] <= cpu_data_i;
5'd16: w_hit_data[159:128] <= cpu_data_i;
5'd20: w_hit_data[191:160] <= cpu_data_i;
5'd24: w_hit_data[223:192] <= cpu_data_i;
5'd28: w_hit_data[255:224] <= cpu_data_i;
endcase
end
// controller
always@(posedge clk_i or posedge rst_i) begin
if(rst_i) begin
state <= STATE_IDLE;
mem_enable <= 1'b0;
mem_write <= 1'b0;
cache_write <= 1'b0;
write_back <= 1'b0;
end
else begin
case(state)
STATE_IDLE: begin
if(cpu_req && !hit) begin // wait for request
state <= STATE_MISS;
end
else begin
state <= STATE_IDLE;
end
end
STATE_MISS: begin
if(sram_dirty) begin // write back if dirty
// TODO: add your code here!
state <= STATE_WRITEBACK;
mem_enable <= 1'b1;
mem_write <= 1'b1;
cache_write <= 1'b0;
write_back <= 1'b1;
end
else begin // write allocate: write miss = read miss + write hit; read miss = read miss + read hit
// TODO: add your code here!
state <= STATE_READMISS;
mem_enable <= 1'b1;
mem_write <= 1'b0;
cache_write <= 1'b0;
write_back <= 1'b0;
end
end
STATE_READMISS: begin
if(mem_ack_i) begin // wait for data memory acknowledge
// TODO: add your code here!
state <= STATE_READMISSOK;
mem_enable <= 1'b0;
mem_write <= 1'b0;
cache_write <= 1'b1;
write_back <= 1'b0;
end
else begin
state <= STATE_READMISS;
end
end
STATE_READMISSOK: begin // wait for data memory acknowledge
// TODO: add your code here!
state <= STATE_IDLE;
mem_enable <= 1'b0;
mem_write <= 1'b0;
cache_write <= 1'b0;
write_back <= 1'b0;
end
STATE_WRITEBACK: begin
if(mem_ack_i) begin // wait for data memory acknowledge
// TODO: add your code here!
state <= STATE_READMISS;
mem_enable <= 1'b1;
mem_write <= 1'b0;
cache_write <= 1'b0;
write_back <= 1'b0;
end
else begin
state <= STATE_WRITEBACK;
end
end
endcase
end
end
//
// SRAM (cache memory part)
//
dcache_sram dcache_sram
(
.clk_i (clk_i),
.rst_i (rst_i),
.addr_i (cache_sram_index),
.tag_i (cache_sram_tag),
.data_i (cache_sram_data),
.enable_i (cache_sram_enable),
.write_i (cache_sram_write),
.tag_o (sram_cache_tag),
.data_o (sram_cache_data),
.hit_o (hit)
);
endmodule