view modules/bluespec/Pygar/lab4/DataCacheBlocking.bsv @ 53:2991344775f8 pygar svn.54

[svn r54] mixer integrated
author punk
date Sun, 09 May 2010 10:58:40 -0400
parents 9fe5ed4af92d
children 6179c07c21d7
line wrap: on
line source
1 // The MIT License
3 // Copyright (c) 2009 Massachusetts Institute of Technology
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 // THE SOFTWARE.
23 // Local includes
24 `include "asim/provides/low_level_platform_interface.bsh"
25 `include "asim/provides/soft_connections.bsh"
26 `include "asim/provides/processor_library.bsh"
27 `include "asim/provides/fpga_components.bsh"
28 `include "asim/provides/common_services.bsh"
30 import Connectable::*;
31 import GetPut::*;
32 import ClientServer::*;
33 import RegFile::*;
34 import FIFO::*;
35 import FIFOF::*;
36 import Trace::*;
38 interface DCache#( type req_t, type resp_t );
40 // Interface from processor to cache
41 interface Server#(req_t,resp_t) proc_server;
43 // Interface from cache to main memory
44 interface Client#(MainMemReq,MainMemResp) mmem_client;
46 // Interface for enabling/disabling statistics
47 interface Put#(Bool) statsEn_put;
49 endinterface
52 //----------------------------------------------------------------------
53 // Cache Types
54 //----------------------------------------------------------------------
56 typedef 10 CacheLineIndexSz;
57 typedef 20 CacheLineTagSz;
58 typedef 32 CacheLineSz;
60 typedef Bit#(CacheLineIndexSz) CacheLineIndex;
61 typedef Bit#(CacheLineTagSz) CacheLineTag;
62 typedef Bit#(CacheLineSz) CacheLine;
64 typedef enum
65 {
66 Init,
67 Access,
68 RefillReq,
69 RefillResp
70 }
71 CacheStage
72 deriving (Eq,Bits);
74 //----------------------------------------------------------------------
75 // Helper functions
76 //----------------------------------------------------------------------
78 function Bit#(AddrSz) getAddr( DataReq req );
80 Bit#(AddrSz) addr = ?;
81 case ( req ) matches
82 tagged LoadReq .ld : addr = ld.addr;
83 tagged StoreReq .st : addr = st.addr;
84 endcase
86 return addr;
88 endfunction
90 function CacheLineIndex getCacheLineIndex( DataReq req );
91 Bit#(AddrSz) addr = getAddr(req);
92 Bit#(CacheLineIndexSz) index = truncate( addr >> 2 );
93 return index;
94 endfunction
96 function CacheLineTag getCacheLineTag( DataReq req );
97 Bit#(AddrSz) addr = getAddr(req);
98 Bit#(CacheLineTagSz) tag = truncate( addr >> fromInteger(valueOf(CacheLineIndexSz)) >> 2 );
99 return tag;
100 endfunction
102 function Bit#(AddrSz) getCacheLineAddr( DataReq req );
103 Bit#(AddrSz) addr = getAddr(req);
104 return ((addr >> 2) << 2);
105 endfunction
107 //----------------------------------------------------------------------
108 // Main module
109 //----------------------------------------------------------------------
111 module [CONNECTED_MODULE] mkDataCache( DCache#(DataReq,DataResp) );
113 //-----------------------------------------------------------
114 // State
116 Reg#(CacheStage) stage <- mkReg(Init);
118 LUTRAM#(CacheLineIndex,Maybe#(CacheLineTag)) cacheTagRam <- mkLUTRAMU_RegFile();
119 LUTRAM#(CacheLineIndex,CacheLine) cacheDataRam <- mkLUTRAMU_RegFile();
121 FIFO#(DataReq) reqQ <- mkFIFO();
122 FIFOF#(DataResp) respQ <- mkBFIFOF1();
124 FIFO#(MainMemReq) mainMemReqQ <- mkBFIFO1();
125 FIFO#(MainMemResp) mainMemRespQ <- mkFIFO();
127 Reg#(CacheLineIndex) initCounter <- mkReg(1);
129 // Statistics state
131 Reg#(Bool) statsEn <- mkReg(False);
132 //rlm:
133 //STAT num_accesses <- mkStatCounter(`STATS_DATA_CACHE_NUM_ACCESSES);
134 //STAT num_misses <- mkStatCounter(`STATS_DATA_CACHE_NUM_MISSES);
135 //STAT num_writebacks <- mkStatCounter(`STATS_DATA_CACHE_NUM_WRITEBACKS);
137 //-----------------------------------------------------------
138 // Name some wires
140 let req = reqQ.first();
141 let reqIndex = getCacheLineIndex(req);
142 let reqTag = getCacheLineTag(req);
143 let reqCacheLineAddr = getCacheLineAddr(req);
145 //-----------------------------------------------------------
146 // Initialize
148 rule init ( stage == Init );
149 traceTiny("mkDataCacheBlocking", "stage","i");
150 initCounter <= initCounter + 1;
151 cacheTagRam.upd(initCounter,Invalid);
152 if ( initCounter == 0 )
153 stage <= Access;
154 endrule
156 //-----------------------------------------------------------
157 // Access cache rule
159 rule access ( (stage == Access) && respQ.notFull() );
161 // Statistics
162 //rlm:
163 //if ( statsEn )
164 // num_accesses.incr();
167 // Get the corresponding tag from the rams
169 Maybe#(CacheLineTag) cacheLineTag = cacheTagRam.sub(reqIndex);
171 // Handle cache hits ...
173 if ( isValid(cacheLineTag) && ( unJust(cacheLineTag) == reqTag ) )
174 begin
175 traceTiny("mkDataCacheBlocking", "hitMiss","h");
176 reqQ.deq();
178 case ( req ) matches
180 tagged LoadReq .ld :
181 respQ.enq( LoadResp { tag: ld.tag, data: cacheDataRam.sub(reqIndex) } );
183 tagged StoreReq .st :
184 begin
185 respQ.enq( StoreResp { tag : st.tag } );
186 cacheDataRam.upd(reqIndex,st.data);
187 end
189 endcase
191 end
193 // Handle cache misses ...
195 else
196 begin
197 traceTiny("mkDataCacheBlocking", "hitMiss","m");
198 //rlm:
199 //if ( statsEn )
200 // num_misses.incr();
202 // Currently we don't use dirty bits so we always writeback the data if it is valid
204 if ( isValid(cacheLineTag) )
205 begin
206 //rlm:
207 // if ( statsEn )
208 // num_writebacks.incr();
210 MainMemReq wbReq
211 = StoreReq { tag : 0,
212 addr : { unJust(cacheLineTag), reqIndex, 2'b0 },
213 data : cacheDataRam.sub(reqIndex) };
215 mainMemReqQ.enq(wbReq);
216 stage <= RefillReq;
217 end
219 // Otherwise we can issue the refill request now
221 else
222 begin
223 mainMemReqQ.enq( LoadReq { tag: 0, addr: reqCacheLineAddr } );
224 stage <= RefillResp;
225 end
227 end
229 endrule
231 //-----------------------------------------------------------
232 // Refill request rule
234 rule refillReq ( stage == RefillReq );
235 traceTiny("mkDataCacheBlocking", "stage","r");
236 mainMemReqQ.enq( LoadReq { tag: 0, addr: reqCacheLineAddr } );
237 stage <= RefillResp;
238 endrule
240 //-----------------------------------------------------------
241 // Refill response rule
243 rule refillResp ( stage == RefillResp );
244 traceTiny("mkDataCacheBlocking", "stage","R");
245 traceTiny("mkDataCacheBlocking", "refill",mainMemRespQ.first());
247 // Write the new data into the cache and update the tag
249 mainMemRespQ.deq();
250 case ( mainMemRespQ.first() ) matches
252 tagged LoadResp .ld :
253 begin
254 cacheTagRam.upd(reqIndex,Valid(reqTag));
255 cacheDataRam.upd(reqIndex,ld.data);
256 end
258 tagged StoreResp .st :
259 noAction;
261 endcase
263 stage <= Access;
264 endrule
266 //-----------------------------------------------------------
267 // Methods
269 interface Client mmem_client;
270 interface Get request = fifoToGet(mainMemReqQ);
271 interface Put response = fifoToPut(mainMemRespQ);
272 endinterface
274 interface Server proc_server;
275 interface Put request = tracePut("mkDataCacheBlocking", "reqTiny",fifoToPut(reqQ));
276 interface Get response = traceGet("mkDataCacheBlocking", "respTiny",fifofToGet(respQ));
277 endinterface
279 interface Put statsEn_put = regToPut(statsEn);
281 endmodule