-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache_controller.v
More file actions
205 lines (176 loc) · 6.2 KB
/
Copy pathcache_controller.v
File metadata and controls
205 lines (176 loc) · 6.2 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
module cache_controller #(
parameter ADDR_WIDTH = 32,
parameter DATA_WIDTH = 32,
parameter BLOCKS = 16,
parameter INDEX_WIDTH = $clog2(BLOCKS),
parameter OFFSET_WIDTH = $clog2(DATA_WIDTH/8),
parameter TAG_WIDTH = ADDR_WIDTH - INDEX_WIDTH - OFFSET_WIDTH
)(
input wire clk,
input wire reset,
// CPU interface
input wire cpu_req,
input wire cpu_write,
input wire [ADDR_WIDTH-1:0] cpu_addr,
input wire [DATA_WIDTH-1:0] cpu_wdata,
output reg [DATA_WIDTH-1:0] cpu_rdata,
output reg cpu_ready,
// Cache interface
output wire [INDEX_WIDTH-1:0] cache_index,
input wire [TAG_WIDTH-1:0] cache_tag,
input wire [DATA_WIDTH-1:0] cache_data,
input wire cache_valid,
input wire cache_dirty,
output reg cache_write_en,
output reg [TAG_WIDTH-1:0] cache_tag_in,
output reg [DATA_WIDTH-1:0] cache_data_in,
output reg cache_valid_in,
output reg cache_dirty_in,
// Main memory interface
output reg mem_req,
output reg mem_write,
output reg [ADDR_WIDTH-1:0] mem_addr,
output reg [DATA_WIDTH-1:0] mem_wdata,
input wire [DATA_WIDTH-1:0] mem_rdata,
input wire mem_ready
);
// State declarations
localparam IDLE = 3'd0;
localparam LOOKUP = 3'd1;
localparam WRITE_BACK = 3'd2;
localparam ALLOCATE = 3'd3;
localparam UPDATE = 3'd4;
reg [2:0] state;
reg [ADDR_WIDTH-1:0] req_addr;
reg [DATA_WIDTH-1:0] req_wdata;
reg req_write;
wire [TAG_WIDTH-1:0] req_tag;
wire [INDEX_WIDTH-1:0] req_index;
wire cache_hit;
assign req_tag = req_addr[ADDR_WIDTH-1:OFFSET_WIDTH + INDEX_WIDTH];
assign req_index = req_addr[OFFSET_WIDTH + INDEX_WIDTH - 1:OFFSET_WIDTH];
assign cache_index = req_index;
assign cache_hit = cache_valid && (cache_tag == req_tag);
// Sequential operation
always @(posedge clk) begin
if (reset) begin
state <= IDLE;
req_addr <= {ADDR_WIDTH{1'b0}};
req_wdata <= {DATA_WIDTH{1'b0}};
req_write <= 1'b0;
cpu_rdata <= {DATA_WIDTH{1'b0}};
cpu_ready <= 1'b0;
cache_write_en <= 1'b0;
cache_tag_in <= {TAG_WIDTH{1'b0}};
cache_data_in <= {DATA_WIDTH{1'b0}};
cache_valid_in <= 1'b0;
cache_dirty_in <= 1'b0;
end
else begin
// Default values
cpu_ready <= 1'b0;
cache_write_en <= 1'b0;
// Cache FSM : State control and cache output control
case (state)
IDLE: begin
if (cpu_req) begin
req_addr <= cpu_addr;
req_wdata <= cpu_wdata;
req_write <= cpu_write;
state <= LOOKUP; // Next state
end
end
LOOKUP: begin
// For HIT
if (cache_hit) begin
if (req_write) begin
cache_write_en <= 1'b1;
cache_tag_in <= req_tag;
cache_data_in <= req_wdata;
cache_valid_in <= 1'b1;
cache_dirty_in <= 1'b1;
cpu_ready <= 1'b1;
end
else begin
cpu_rdata <= cache_data;
cpu_ready <= 1'b1;
end
state <= IDLE; // Next state
end
// For MISS
else begin
// Next state
if (cache_valid && cache_dirty)
state <= WRITE_BACK;
else
state <= ALLOCATE;
end
end
WRITE_BACK: begin
if (mem_ready) begin
state <= ALLOCATE;
end
end
ALLOCATE: begin
if (mem_ready) begin
cache_write_en <= 1'b1;
cache_tag_in <= req_tag;
cache_data_in <= mem_rdata;
cache_valid_in <= 1'b1;
cache_dirty_in <= 1'b0;
// For Read MISS
if (!req_write) begin
cpu_rdata <= mem_rdata;
cpu_ready <= 1'b1;
state <= IDLE;
end
// For Write MISS
else begin
state <= UPDATE;
end
end
end
UPDATE: begin
cache_write_en <= 1'b1;
cache_tag_in <= req_tag;
cache_data_in <= req_wdata;
cache_valid_in <= 1'b1;
cache_dirty_in <= 1'b1;
cpu_ready <= 1'b1;
state <= IDLE;
end
default: begin
state <= IDLE;
end
endcase
end
end
// Memory request logic
// 'mem_req' is suppressed while 'mem_ready' is asserted,
// to prevent the memory from re-accepting a completed
// request before the FSM changes state.
always @(*) begin
// Default values
mem_req = 1'b0;
mem_write = 1'b0;
mem_addr = {ADDR_WIDTH{1'b0}};
mem_wdata = {DATA_WIDTH{1'b0}};
case (state)
WRITE_BACK: begin
if (!mem_ready) begin
mem_req = 1'b1;
mem_write = 1'b1;
mem_addr = {cache_tag,req_index,{OFFSET_WIDTH{1'b0}}};
mem_wdata = cache_data;
end
end
ALLOCATE: begin
if (!mem_ready) begin
mem_req = 1'b1;
mem_write = 1'b0;
mem_addr = {req_tag,req_index,{OFFSET_WIDTH{1'b0}}};
end
end
endcase
end
endmodule