2011-10-26 08:02:47 +02:00
|
|
|
package Top {
|
|
|
|
|
2011-11-09 23:52:17 +01:00
|
|
|
import Chisel._;
|
2011-10-26 08:02:47 +02:00
|
|
|
import Node._;
|
2011-11-09 23:52:17 +01:00
|
|
|
import Constants._;
|
2011-10-26 08:02:47 +02:00
|
|
|
import scala.math._;
|
|
|
|
|
2011-11-10 06:54:11 +01:00
|
|
|
// interface between I$ and pipeline/ITLB (32 bits wide)
|
2011-10-26 08:02:47 +02:00
|
|
|
class ioImem(view: List[String] = null) extends Bundle (view)
|
|
|
|
{
|
2011-11-14 13:13:13 +01:00
|
|
|
val invalidate = Bool('input);
|
2011-11-12 09:25:06 +01:00
|
|
|
val itlb_miss = Bool('input);
|
2011-10-26 08:02:47 +02:00
|
|
|
val req_val = Bool('input);
|
|
|
|
val req_rdy = Bool('output);
|
2011-11-12 09:25:06 +01:00
|
|
|
val req_idx = Bits(PGIDX_BITS, 'input);
|
|
|
|
val req_ppn = Bits(PPN_BITS, 'input);
|
2011-10-26 08:02:47 +02:00
|
|
|
val resp_data = Bits(32, 'output);
|
|
|
|
val resp_val = Bool('output);
|
|
|
|
}
|
|
|
|
|
|
|
|
// interface between I$ and memory (128 bits wide)
|
|
|
|
class ioIcache(view: List[String] = null) extends Bundle (view)
|
|
|
|
{
|
2011-12-12 15:49:16 +01:00
|
|
|
val req_addr = UFix(PADDR_BITS - OFFSET_BITS, 'input);
|
2011-10-26 08:02:47 +02:00
|
|
|
val req_val = Bool('input);
|
|
|
|
val req_rdy = Bool('output);
|
2011-12-09 09:42:43 +01:00
|
|
|
val resp_data = Bits(MEM_DATA_BITS, 'output);
|
2011-10-26 08:02:47 +02:00
|
|
|
val resp_val = Bool('output);
|
|
|
|
}
|
|
|
|
|
2011-11-05 04:52:21 +01:00
|
|
|
class ioICacheDM extends Bundle()
|
|
|
|
{
|
2011-10-26 08:02:47 +02:00
|
|
|
val cpu = new ioImem();
|
|
|
|
val mem = new ioIcache().flip();
|
|
|
|
}
|
|
|
|
|
|
|
|
// basic direct mapped instruction cache
|
2011-11-10 09:50:09 +01:00
|
|
|
// 32 bit wide cpu port, 128 bit wide memory port, 64 byte cachelines
|
2011-10-26 08:02:47 +02:00
|
|
|
// parameters :
|
|
|
|
// lines = # cache lines
|
2011-11-09 23:52:17 +01:00
|
|
|
class rocketICacheDM(lines: Int) extends Component {
|
2011-10-26 08:02:47 +02:00
|
|
|
val io = new ioICacheDM();
|
2011-11-07 09:58:25 +01:00
|
|
|
|
2011-11-09 23:52:17 +01:00
|
|
|
val addrbits = PADDR_BITS;
|
2011-10-26 08:02:47 +02:00
|
|
|
val indexbits = ceil(log10(lines)/log10(2)).toInt;
|
2011-12-12 15:49:16 +01:00
|
|
|
val offsetbits = OFFSET_BITS;
|
2011-10-26 08:02:47 +02:00
|
|
|
val tagmsb = addrbits - 1;
|
|
|
|
val taglsb = indexbits+offsetbits;
|
2011-11-12 09:25:06 +01:00
|
|
|
val tagbits = addrbits-taglsb;
|
2011-10-26 08:02:47 +02:00
|
|
|
val indexmsb = taglsb-1;
|
|
|
|
val indexlsb = offsetbits;
|
|
|
|
val offsetmsb = indexlsb-1;
|
2011-11-07 09:58:25 +01:00
|
|
|
val databits = 32;
|
2011-12-09 09:42:43 +01:00
|
|
|
val offsetlsb = ceil(log(databits/8)/log(2)).toInt;
|
|
|
|
val rf_cnt_bits = ceil(log(REFILL_CYCLES)/log(2)).toInt;
|
2011-10-26 08:02:47 +02:00
|
|
|
|
|
|
|
val s_reset :: s_ready :: s_request :: s_refill_wait :: s_refill :: s_resolve_miss :: Nil = Enum(6) { UFix() };
|
|
|
|
val state = Reg(resetVal = s_reset);
|
|
|
|
|
2011-12-12 15:49:16 +01:00
|
|
|
val r_cpu_req_idx = Reg { Bits(width = PGIDX_BITS) }
|
|
|
|
val r_cpu_req_ppn = Reg { Bits(width = PPN_BITS) }
|
2011-11-12 09:25:06 +01:00
|
|
|
val r_cpu_req_val = Reg(resetVal = Bool(false));
|
2011-10-26 08:02:47 +02:00
|
|
|
|
2011-11-12 09:25:06 +01:00
|
|
|
when (io.cpu.req_val && io.cpu.req_rdy) {
|
|
|
|
r_cpu_req_idx <== io.cpu.req_idx;
|
|
|
|
}
|
2011-11-13 00:00:45 +01:00
|
|
|
when (state === s_ready && r_cpu_req_val && !io.cpu.itlb_miss) {
|
2011-11-12 09:25:06 +01:00
|
|
|
r_cpu_req_ppn <== io.cpu.req_ppn;
|
|
|
|
}
|
|
|
|
when (io.cpu.req_rdy) {
|
|
|
|
r_cpu_req_val <== io.cpu.req_val;
|
2011-11-05 04:52:21 +01:00
|
|
|
}
|
|
|
|
otherwise {
|
|
|
|
r_cpu_req_val <== Bool(false);
|
|
|
|
}
|
2011-10-26 08:02:47 +02:00
|
|
|
|
2011-11-13 07:13:29 +01:00
|
|
|
// refill counter
|
2011-12-09 09:42:43 +01:00
|
|
|
val refill_count = Reg(resetVal = UFix(0, rf_cnt_bits));
|
2011-11-05 04:52:21 +01:00
|
|
|
when (io.mem.resp_val) {
|
|
|
|
refill_count <== refill_count + UFix(1);
|
|
|
|
}
|
2011-11-12 09:25:06 +01:00
|
|
|
val tag_addr =
|
|
|
|
Mux((state === s_refill_wait), r_cpu_req_idx(PGIDX_BITS-1,offsetbits),
|
|
|
|
io.cpu.req_idx(PGIDX_BITS-1,offsetbits)).toUFix;
|
|
|
|
val tag_we = (state === s_refill_wait) && io.mem.resp_val;
|
2011-12-04 04:41:15 +01:00
|
|
|
|
|
|
|
val tag_array = Mem4(lines, r_cpu_req_ppn);
|
2011-12-05 09:33:17 +01:00
|
|
|
tag_array.setReadLatency(SRAM_READ_LATENCY);
|
|
|
|
// tag_array.setTarget('inst);
|
2011-12-04 04:41:15 +01:00
|
|
|
val tag_rdata = tag_array.rw(tag_addr, r_cpu_req_ppn, tag_we);
|
2011-12-04 10:18:38 +01:00
|
|
|
|
2011-10-26 08:02:47 +02:00
|
|
|
// valid bit array
|
|
|
|
val vb_array = Reg(resetVal = Bits(0, lines));
|
2011-11-14 13:13:13 +01:00
|
|
|
when (io.cpu.invalidate) {
|
|
|
|
vb_array <== Bits(0,lines);
|
|
|
|
}
|
2011-11-12 09:25:06 +01:00
|
|
|
when (tag_we) {
|
|
|
|
vb_array <== vb_array.bitSet(r_cpu_req_idx(PGIDX_BITS-1,offsetbits).toUFix, UFix(1,1));
|
2011-11-05 04:52:21 +01:00
|
|
|
}
|
2011-10-26 08:02:47 +02:00
|
|
|
|
2011-11-12 09:25:06 +01:00
|
|
|
val tag_valid = Reg(vb_array(tag_addr)).toBool;
|
|
|
|
val tag_match = (tag_rdata === io.cpu.req_ppn);
|
|
|
|
|
2011-10-26 08:02:47 +02:00
|
|
|
// data array
|
2011-12-04 04:41:15 +01:00
|
|
|
val data_addr =
|
2011-11-12 09:25:06 +01:00
|
|
|
Mux((state === s_refill_wait) || (state === s_refill), Cat(r_cpu_req_idx(PGIDX_BITS-1, offsetbits), refill_count),
|
|
|
|
io.cpu.req_idx(PGIDX_BITS-1, offsetmsb-1)).toUFix;
|
2011-12-09 09:42:43 +01:00
|
|
|
val data_array = Mem4(lines*REFILL_CYCLES, io.mem.resp_data);
|
2011-12-05 09:33:17 +01:00
|
|
|
data_array.setReadLatency(SRAM_READ_LATENCY);
|
|
|
|
// data_array.setTarget('inst);
|
2011-12-04 04:41:15 +01:00
|
|
|
val data_array_rdata = data_array.rw(data_addr, io.mem.resp_data, io.mem.resp_val);
|
|
|
|
|
2011-11-07 09:58:25 +01:00
|
|
|
// output signals
|
2011-11-12 09:25:06 +01:00
|
|
|
io.cpu.resp_val := !io.cpu.itlb_miss && (state === s_ready) && r_cpu_req_val && tag_valid && tag_match;
|
|
|
|
io.cpu.req_rdy := !io.cpu.itlb_miss && (state === s_ready) && (!r_cpu_req_val || (tag_valid && tag_match));
|
2011-12-17 12:26:11 +01:00
|
|
|
io.cpu.resp_data := data_array_rdata >> Cat(r_cpu_req_idx(offsetmsb-rf_cnt_bits,offsetlsb), UFix(0, log2up(databits))).toUFix
|
2011-10-26 08:02:47 +02:00
|
|
|
io.mem.req_val := (state === s_request);
|
2011-12-12 15:49:16 +01:00
|
|
|
io.mem.req_addr := Cat(r_cpu_req_ppn, r_cpu_req_idx(indexmsb,indexlsb)).toUFix
|
2011-10-26 08:02:47 +02:00
|
|
|
|
|
|
|
// control state machine
|
|
|
|
switch (state) {
|
|
|
|
is (s_reset) {
|
|
|
|
state <== s_ready;
|
|
|
|
}
|
|
|
|
is (s_ready) {
|
2011-11-13 00:00:45 +01:00
|
|
|
when (io.cpu.itlb_miss) {
|
|
|
|
state <== s_ready;
|
|
|
|
}
|
|
|
|
when (r_cpu_req_val && !(tag_valid && tag_match)) {
|
|
|
|
state <== s_request;
|
|
|
|
}
|
2011-10-26 08:02:47 +02:00
|
|
|
}
|
|
|
|
is (s_request)
|
|
|
|
{
|
2011-11-12 09:25:06 +01:00
|
|
|
when (io.mem.req_rdy) {
|
|
|
|
state <== s_refill_wait;
|
|
|
|
}
|
2011-10-26 08:02:47 +02:00
|
|
|
}
|
|
|
|
is (s_refill_wait) {
|
2011-11-12 09:25:06 +01:00
|
|
|
when (io.mem.resp_val) {
|
|
|
|
state <== s_refill;
|
|
|
|
}
|
2011-10-26 08:02:47 +02:00
|
|
|
}
|
|
|
|
is (s_refill) {
|
2011-12-09 09:42:43 +01:00
|
|
|
when (io.mem.resp_val && (~refill_count === UFix(0))) {
|
2011-11-12 09:25:06 +01:00
|
|
|
state <== s_resolve_miss;
|
|
|
|
}
|
2011-10-26 08:02:47 +02:00
|
|
|
}
|
|
|
|
is (s_resolve_miss) {
|
|
|
|
state <== s_ready;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|