-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cache.bsv
250 lines (217 loc) · 9.22 KB
/
Cache.bsv
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
import ClientServer::*;
import GetPut::*;
import FIFO::*;
import FIFOF::*;
import SpecialFIFOs::*;
import BRAM::*;
import Connectable::*;
import AvalonCommon::*;
export Cache(..);
export mkCache;
interface Cache#(numeric type address_width, numeric type inst_width, numeric type data_width);
method Action clear();
interface Client#(AvalonRequest#(address_width,32), Bit#(32)) busClient;
interface Server#(AvalonRequest#(address_width,32), Bit#(32)) instCache;
interface Server#(AvalonRequest#(address_width,32), Bit#(32)) dataCache;
endinterface
interface SingleCache#(numeric type address_width, numeric type cache_width);
method Action clear();
interface Put#(CacheLine#(address_width)) prefetch;
interface Client#(AvalonRequest#(address_width,32), Bit#(32)) busClient;
interface Server#(AvalonRequest#(address_width,32), Bit#(32)) thisCache;
endinterface
typedef struct {
Bit#(address_width) addr;
Maybe#(Bit#(32)) data;
} CacheLine#(numeric type address_width) deriving (Bits, Eq);
module mkSingleCache#(function Bool ignoreCache(Bit#(address_width) addr)) (SingleCache#(address_width,cache_width))
provisos (Add#(a__, cache_width, address_width),
Add#(b__, cache_width, TAdd#(cache_width, 1)));
BRAM_Configure cfg = defaultValue;
BRAM2Port#(Bit#(cache_width), CacheLine#(address_width)) cacheLines <- mkBRAM2Server(cfg);
RWire#(Bit#(cache_width)) portAaddr <- mkRWire;
Reg#(Bit#(TAdd#(cache_width,1))) resetCounter <- mkReg(0);
Integer resetLastBit = valueof(cache_width);
Bool resetState = resetCounter[resetLastBit] == 1'b0;
FIFO#(AvalonRequest#(address_width,32)) pendingReq <- mkPipelineFIFO;
FIFO#(Bit#(0)) mainMemReq <- mkBypassFIFO;
FIFO#(Bit#(32)) dataResponse <- mkBypassFIFO;
rule doReset(resetState);
let cacheLine = CacheLine{addr: 0, data: tagged Invalid};
Bit#(cache_width) address = truncate(resetCounter);
let req = BRAMRequest{write: True,
responseOnWrite: False,
address: address,
datain: cacheLine};
cacheLines.portA.request.put(req);
req.address = address + 1;
cacheLines.portB.request.put(req);
resetCounter <= resetCounter + 2;
endrule
rule getResponseFromBRAM(!resetState);
let cacheLine <- cacheLines.portA.response.get;
let req = pendingReq.first;
if(req.command == Read)
begin
if(!ignoreCache(req.addr) && cacheLine.addr == req.addr &&& cacheLine.data matches tagged Valid .data)
begin
dataResponse.enq(data);
pendingReq.deq;
end
else
begin
mainMemReq.enq(?);
end
end
else // req.command == Write
begin
mainMemReq.enq(?);
end
endrule
method Action clear() if (!resetState);
resetCounter <= 0;
endmethod
interface Client busClient;
interface Get request;
method ActionValue#(AvalonRequest#(address_width,32)) get();
let req = pendingReq.first;
mainMemReq.deq;
if(req.command == Write)
pendingReq.deq;
return req;
endmethod
endinterface
interface Put response;
method Action put(Bit#(32) data);
let req = pendingReq.first;
Bit#(cache_width) address = truncate(req.addr);
Bool willConflict = False;
if(portAaddr.wget matches tagged Valid .x)
willConflict = x == address;
if(!willConflict)
begin
let cacheLine = CacheLine{addr: req.addr, data: tagged Valid data};
let bramReq = BRAMRequest{write: True,
responseOnWrite: False,
address: address,
datain: cacheLine};
cacheLines.portB.request.put(bramReq);
end
dataResponse.enq(data);
pendingReq.deq;
endmethod
endinterface
endinterface
interface Put prefetch;
method Action put(CacheLine#(address_width) cacheLine) if (!resetState);
Bit#(cache_width) address = truncate(cacheLine.addr);
Bool willConflict = False;
if(portAaddr.wget matches tagged Valid .x)
willConflict = x == address;
if(!willConflict)
begin
let bramReq = BRAMRequest{write: True,
responseOnWrite: False,
address: address,
datain: cacheLine};
cacheLines.portB.request.put(bramReq);
end
endmethod
endinterface
interface Server thisCache;
interface Put request;
method Action put(AvalonRequest#(address_width,32) req) if (!resetState);
Bit#(cache_width) address = truncate(req.addr);
portAaddr.wset(address);
let bramReq = BRAMRequest{write: req.command == Write,
responseOnWrite: True,
address: address,
datain: ?};
if(req.command == Write)
bramReq.datain = CacheLine{addr: req.addr, data: tagged Valid req.data};
cacheLines.portA.request.put(bramReq);
pendingReq.enq(req);
endmethod
endinterface
interface Get response = toGet(dataResponse);
endinterface
endmodule
typedef enum {
Instruction,
Data,
InstructionPrefetch
} CacheReqType deriving (Bits,Eq);
module mkCache#(function Bool ignoreCache(Bit#(address_width) addr)) (Cache#(address_width,inst_width,data_width))
provisos (Add#(a__, data_width, address_width),
Add#(b__, data_width, TAdd#(data_width, 1)),
Add#(c__, inst_width, address_width),
Add#(d__, inst_width, TAdd#(inst_width, 1)));
function ignoreNever(Bit#(address_width) addr) = False;
SingleCache#(address_width,inst_width) instSCache <- mkSingleCache(ignoreNever);
SingleCache#(address_width,data_width) dataSCache <- mkSingleCache(ignoreCache);
FIFOF#(AvalonRequest#(address_width,32)) instReq <- mkBypassFIFOF;
FIFOF#(AvalonRequest#(address_width,32)) dataReq <- mkBypassFIFOF;
Reg#(Bit#(1)) arbitration <- mkReg(0);
FIFO#(AvalonRequest#(address_width,32)) outReq <- mkBypassFIFO;
FIFO#(CacheReqType) pendingReq <- mkFIFO;
FIFO#(Bit#(32)) inResp <- mkBypassFIFO;
Reg#(Bit#(address_width)) nextPrefetch <- mkReg('h100);
FIFOF#(Bit#(address_width)) pendingPrefetch <- mkFIFOF1;
mkConnection(instSCache.busClient.request, toPut(instReq));
mkConnection(dataSCache.busClient.request, toPut(dataReq));
(* mutually_exclusive = "peekInstReq, peekDataReq, doInstPrefetch" *)
rule peekInstReq(instReq.notEmpty && (arbitration == 0 || !dataReq.notEmpty));
arbitration <= ~arbitration;
nextPrefetch <= instReq.first.addr + 3;
outReq.enq(instReq.first);
if(instReq.first.command != Read)
begin
$display("Cache error: Trying to write to the instruction cache");
$finish();
end
pendingReq.enq(Instruction);
instReq.deq;
endrule
rule peekDataReq(dataReq.notEmpty && (arbitration == 1 || !instReq.notEmpty));
arbitration <= ~arbitration;
outReq.enq(dataReq.first);
if(dataReq.first.command == Read)
pendingReq.enq(Data);
dataReq.deq;
endrule
rule doInstPrefetch(!instReq.notEmpty && !dataReq.notEmpty);
outReq.enq(AvalonRequest{command: Read, addr: nextPrefetch, data: ?});
pendingReq.enq(InstructionPrefetch);
pendingPrefetch.enq(nextPrefetch);
nextPrefetch <= nextPrefetch + 1;
endrule
(* mutually_exclusive = "peekInstResp, peekDataResp, peekInstPrefetchResp" *)
rule peekInstResp(pendingReq.first == Instruction);
instSCache.busClient.response.put(inResp.first);
inResp.deq;
pendingReq.deq;
endrule
rule peekDataResp(pendingReq.first == Data);
dataSCache.busClient.response.put(inResp.first);
inResp.deq;
pendingReq.deq;
endrule
rule peekInstPrefetchResp(pendingReq.first == InstructionPrefetch);
let cacheLine = CacheLine{addr: pendingPrefetch.first,
data: tagged Valid inResp.first};
instSCache.prefetch.put(cacheLine);
inResp.deq;
pendingPrefetch.deq;
pendingReq.deq;
endrule
method Action clear();
instSCache.clear;
dataSCache.clear;
endmethod
interface Client busClient;
interface Get request = toGet(outReq);
interface Put response = toPut(inResp);
endinterface
interface Server instCache = instSCache.thisCache;
interface Server dataCache = dataSCache.thisCache;
endmodule