-
Notifications
You must be signed in to change notification settings - Fork 0
/
dcache_sram.v
77 lines (68 loc) · 2.34 KB
/
dcache_sram.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
module dcache_sram
(
clk_i,
rst_i,
addr_i,
tag_i,
data_i,
enable_i,
write_i,
tag_o,
data_o,
hit_o
);
// I/O Interface from/to controller
input clk_i;
input rst_i;
input [3:0] addr_i;
input [24:0] tag_i;
input [255:0] data_i;
input enable_i;
input write_i;
output [24:0] tag_o;
output [255:0] data_o;
output hit_o;
// Memory
reg [24:0] tag [0:15][0:1];//valid, dirty, tag
reg [255:0] data[0:15][0:1];//2^5bytes * 8bits
integer i, j;
// Write Data
// 1. Write hit
// 2. Read miss: Read from memory
always@(posedge clk_i or posedge rst_i) begin
if (rst_i) begin
for (i=0;i<16;i=i+1) begin
for (j=0;j<2;j=j+1) begin
tag[i][j] <= 25'b0;
data[i][j] <= 256'b0;
end
end
end
if (enable_i && write_i) begin
// TODO: Handle your write of 2-way associative cache + LRU here (0->recent)
//write hit
if((tag[addr_i][0][22:0] === tag_i[22:0]) & (tag[addr_i][0][24] === 1'b1)) begin
data[addr_i][0] <= data_i;
tag[addr_i][0] <= tag_i;
end
else if((tag[addr_i][1][22:0] === tag_i[22:0]) & (tag[addr_i][1][24] === 1'b1)) begin
data[addr_i][1] <= data[addr_i][0]; //change position
tag[addr_i][1] <= tag[addr_i][0]; //change position
data[addr_i][0] <= data_i; //write in new data
tag[addr_i][0] <= tag_i; //change position
end
//read miss
else begin
data[addr_i][1] <= data[addr_i][0]; //change position
tag[addr_i][1] <= tag[addr_i][0]; //change position
data[addr_i][0] <= data_i; //write in new data
tag[addr_i][0] <= tag_i; //change tag
end
end
end
// Read Data
// TODO: tag_o=? data_o=? hit_o=?
assign hit_o = ((tag[addr_i][0][22:0] === tag_i[22:0]) & (tag[addr_i][0][24] === 1'b1)) | ((tag[addr_i][1][22:0] === tag_i[22:0]) & (tag[addr_i][1][24] === 1'b1));
assign tag_o = (tag[addr_i][0][22:0] === tag_i[22:0]) ? tag[addr_i][0] : tag[addr_i][1]; //if both don't hit, treated the same as the 2nd is hit.
assign data_o = (tag[addr_i][0][22:0] === tag_i[22:0]) ? data[addr_i][0] : data[addr_i][1];
endmodule