forked from praveenmunagapati/mipscpu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
i_cache.v
252 lines (221 loc) · 6.5 KB
/
i_cache.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
248
249
250
251
252
/* i_cache.v
* Author: Pravin P. Prabhu
* Last Revision: 1/5/11
* Abstract:
* Provides caching of instructions from imem for quick access. Note that this
* is a read only cache, and thus self-modifying code is not supported.
*/
module i_cache #(
parameter DATA_WIDTH = 32,
parameter ADDRESS_WIDTH = 21,
//parameter TAG_WIDTH = 14,
//parameter INDEX_WIDTH = 5,
parameter BLOCK_OFFSET_WIDTH = 2,
//parameter CACHE_SIZE = 9, // size of cache
parameter ASSOCIATIVITY = 1, // for n-way associative caching
parameter INDEX_WIDTH = 10 - (ASSOCIATIVITY - 1), //number of sets (size of cache)
parameter TAG_WIDTH = ADDRESS_WIDTH - INDEX_WIDTH - BLOCK_OFFSET_WIDTH
)
(
// General
input i_Clk,
input i_Reset_n,
// Requests
input i_Valid,
input [TAG_WIDTH+INDEX_WIDTH+BLOCK_OFFSET_WIDTH:0] i_Address,
// DMEM Transaction
output reg o_MEM_Valid, // If request to DMEM is valid
output reg [TAG_WIDTH+INDEX_WIDTH+BLOCK_OFFSET_WIDTH:0] o_MEM_Address, // Address request to DMEM
input i_MEM_Valid, // If data from main mem is valid
input i_MEM_Last, // If main mem is sending the last piece of data
input [DATA_WIDTH-1:0] i_MEM_Data, // Data from main mem
// Outputs
output o_Ready,
output reg o_Valid, // If the output is correct.
output reg [DATA_WIDTH-1:0] o_Data // The data requested.
);
// consts
localparam FALSE = 1'b0;
localparam TRUE = 1'b1;
// Internal
// Reg'd inputs
reg [BLOCK_OFFSET_WIDTH-1:0] r_i_BlockOffset;
reg [INDEX_WIDTH-1:0] r_i_Index;
reg [TAG_WIDTH-1:0] r_i_Tag;
// Parsing
wire [BLOCK_OFFSET_WIDTH-1:0] i_BlockOffset = i_Address[BLOCK_OFFSET_WIDTH-1:0];
wire [INDEX_WIDTH-1:0] i_Index = i_Address[INDEX_WIDTH+BLOCK_OFFSET_WIDTH-1:BLOCK_OFFSET_WIDTH];
wire [TAG_WIDTH-1:0] i_Tag = i_Address[TAG_WIDTH+INDEX_WIDTH+BLOCK_OFFSET_WIDTH-1:INDEX_WIDTH+BLOCK_OFFSET_WIDTH];
// Tags
reg [TAG_WIDTH-1:0] Tag_Array [0:(1<<INDEX_WIDTH)-1] [0:ASSOCIATIVITY-1];
// Data
reg [(DATA_WIDTH*4)-1:0] Data_Array [0:(1<<INDEX_WIDTH)-1] [0:ASSOCIATIVITY-1];
// Valid
reg Valid_Array [0:(1<<INDEX_WIDTH)-1] [0:ASSOCIATIVITY-1];
// States
reg [5:0] State;
reg [5:0] NextState;
localparam STATE_READY = 0; // Ready for incoming requests
localparam STATE_MISS_READ = 1; // Missing on a read
// Counters
integer i;
integer a;
reg [8:0] Gen_Count; // General counter
// Cache location
reg [3:0] Location;
reg [3:0] r_Location;
// Hardwired assignments
assign o_Ready = (State==STATE_READY);
// Logic for determining cache location for n-way associativity
always @(*)
begin
Location = 0;
case (ASSOCIATIVITY)
1: Location = 0;
2:
begin
if ( Tag_Array[i_Index][0] == i_Tag )
Location = 0;
else if ( Tag_Array[i_Index][1] == i_Tag )
Location = 1;
end
default: Location = 1'bx;
endcase
end
// Combinatorial logic: What state are we in? How should we handle I/O?
always @(*)
begin
// Set defaults to avoid latch inference
NextState <= State;
o_Valid <= FALSE;
o_Data <= {DATA_WIDTH{1'bx}};
o_MEM_Valid <= FALSE;
o_MEM_Address <= {TAG_WIDTH+INDEX_WIDTH+BLOCK_OFFSET_WIDTH{1'bx}};
// Act asynchronously based on state
case(State)
// We're ready for requests
STATE_READY:
begin
// Valid request?
if( i_Valid )
begin
if( Valid_Array[i_Index][Location] &&
( Tag_Array[i_Index][Location] == i_Tag) )
begin
// Hit!
o_Valid <= TRUE;
case( i_BlockOffset )
// Verilog doesn't allow generic indexing into arrays with operators..
0: o_Data <= Data_Array[i_Index][Location][(DATA_WIDTH*1)-1:0];
1: o_Data <= Data_Array[i_Index][Location][(DATA_WIDTH*2)-1:(DATA_WIDTH*1)];
2: o_Data <= Data_Array[i_Index][Location][(DATA_WIDTH*3)-1:(DATA_WIDTH*2)];
3: o_Data <= Data_Array[i_Index][Location][(DATA_WIDTH*4)-1:(DATA_WIDTH*3)];
default: o_Data <= {DATA_WIDTH{1'bx}};
endcase
end
else
begin
// Miss. Will proceed to Miss state.
NextState <= STATE_MISS_READ;
end
end
else
begin
// Invalid output
end
end
// We are handling a read miss
STATE_MISS_READ:
begin
// Submit our request.
o_MEM_Valid <= TRUE;
o_MEM_Address <= {r_i_Tag,r_i_Index,{BLOCK_OFFSET_WIDTH{1'b0}},1'b0};
// Mem is communicating?
if( i_MEM_Valid )
begin
// Is this the data we were waiting on?
if( Gen_Count == r_i_BlockOffset )
begin
// Yes
o_Valid <= TRUE;
o_Data <= i_MEM_Data;
end
// Last piece of transaction?
if( i_MEM_Last )
begin
// This is the last piece of data. We will transition on the next cycle.
NextState <= STATE_READY;
end
end
end
// Invalid state
default:
begin
$display("Warning: Invalid state @ i_cache.v: %d",$time);
end
endcase
end
// State driver
always @(posedge i_Clk or negedge i_Reset_n)
begin
if( !i_Reset_n )
begin
State <= STATE_READY;
end
else
begin
State <= NextState;
// Initialize for next state
case( State )
STATE_READY:
begin
if( NextState == STATE_MISS_READ )
begin
// Prepare counter
Gen_Count <= 0;
// Latch inputs
r_i_BlockOffset <= i_BlockOffset;
r_i_Index <= i_Index;
r_i_Tag <= i_Tag;
r_Location <= Location;
end
end
STATE_MISS_READ:
begin
if( NextState == STATE_READY )
begin
// Record info about transaction in tags & valid bits
Tag_Array[r_i_Index][r_Location] <= r_i_Tag;
Valid_Array[r_i_Index][r_Location] <= TRUE;
end
if( i_MEM_Valid )
begin
case( Gen_Count )
0: Data_Array[r_i_Index][r_Location][(DATA_WIDTH*1)-1:0] <= i_MEM_Data;
1: Data_Array[r_i_Index][r_Location][(DATA_WIDTH*2)-1:(DATA_WIDTH*1)] <= i_MEM_Data;
2: Data_Array[r_i_Index][r_Location][(DATA_WIDTH*3)-1:(DATA_WIDTH*2)] <= i_MEM_Data;
3: Data_Array[r_i_Index][r_Location][(DATA_WIDTH*4)-1:(DATA_WIDTH*3)] <= i_MEM_Data;
default: $display("Warning: Invalid Gen Count value @ i_cache");
endcase
// Account for the input
Gen_Count <= Gen_Count + 9'b1;
end
end
default:
begin
end
endcase
end
end
initial
begin
// Mark everything as invalid
for(a=0;a<ASSOCIATIVITY; a=a+1)
begin
for(i=0;i<(1<<INDEX_WIDTH);i=i+1)
begin
Valid_Array[i][a] = FALSE;
end
end
end
endmodule