1
0
rocket-chip/rocket/src/main/scala/icache.scala

159 lines
4.7 KiB
Scala
Raw Normal View History

package Top {
2011-11-09 23:52:17 +01:00
import Chisel._;
import Node._;
2011-11-09 23:52:17 +01:00
import Constants._;
import scala.math._;
// interface between I$ and pipeline/ITLB (32 bits wide)
class ioImem(view: List[String] = null) extends Bundle (view)
{
val invalidate = Bool(INPUT);
val itlb_miss = Bool(INPUT);
val req_val = Bool(INPUT);
val req_idx = Bits(PGIDX_BITS, INPUT);
val req_ppn = Bits(PPN_BITS, INPUT);
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)
{
val req_addr = UFix(PADDR_BITS - OFFSET_BITS, INPUT);
val req_val = Bool(INPUT);
val req_rdy = Bool(OUTPUT);
val resp_data = Bits(MEM_DATA_BITS, OUTPUT);
val resp_val = Bool(OUTPUT);
}
2011-11-05 04:52:21 +01:00
class ioICacheDM extends Bundle()
{
val cpu = new ioImem();
val mem = new ioIcache().flip();
}
// basic direct mapped instruction cache
// 32 bit wide cpu port, 128 bit wide memory port, 64 byte cachelines
// parameters :
// lines = # cache lines
2011-11-09 23:52:17 +01:00
class rocketICacheDM(lines: Int) extends Component {
val io = new ioICacheDM();
2011-11-09 23:52:17 +01:00
val addrbits = PADDR_BITS;
2012-01-25 00:13:49 +01:00
val indexbits = log2up(lines);
val offsetbits = OFFSET_BITS;
val tagmsb = addrbits - 1;
val taglsb = indexbits+offsetbits;
2011-11-12 09:25:06 +01:00
val tagbits = addrbits-taglsb;
val indexmsb = taglsb-1;
val indexlsb = offsetbits;
val offsetmsb = indexlsb-1;
val databits = 32;
2012-01-25 00:13:49 +01:00
val offsetlsb = log2up(databits/8);
val rf_cnt_bits = log2up(REFILL_CYCLES);
require(PGIDX_BITS >= taglsb); // virtually-indexed, physically-tagged constraint
2012-01-12 03:27:11 +01:00
val s_reset :: s_ready :: s_request :: s_refill_wait :: s_refill :: Nil = Enum(5) { UFix() };
val state = Reg(resetVal = s_reset);
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));
val rdy = Wire() { Bool() }
when (io.cpu.req_val && rdy) {
2012-01-25 00:13:49 +01:00
r_cpu_req_val <== Bool(true)
r_cpu_req_idx <== io.cpu.req_idx
2011-11-05 04:52:21 +01:00
}
otherwise {
2012-01-25 00:13:49 +01:00
r_cpu_req_val <== Bool(false)
2011-11-05 04:52:21 +01:00
}
2012-01-25 00:13:49 +01:00
when (state === s_ready && r_cpu_req_val && !io.cpu.itlb_miss) {
r_cpu_req_ppn <== io.cpu.req_ppn
}
val r_cpu_hit_addr = Cat(io.cpu.req_ppn, r_cpu_req_idx)
val r_cpu_hit_tag = r_cpu_hit_addr(tagmsb,taglsb)
val r_cpu_miss_addr = Cat(r_cpu_req_ppn, r_cpu_req_idx)
val r_cpu_miss_tag = r_cpu_miss_addr(tagmsb,taglsb)
// 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 =
2012-01-25 00:13:49 +01:00
Mux((state === s_refill_wait), r_cpu_req_idx(indexmsb,indexlsb),
io.cpu.req_idx(indexmsb,indexlsb)).toUFix;
2011-11-12 09:25:06 +01:00
val tag_we = (state === s_refill_wait) && io.mem.resp_val;
2012-01-25 00:13:49 +01:00
val tag_array = Mem4(lines, r_cpu_miss_tag);
tag_array.setReadLatency(1);
2012-01-14 03:18:48 +01:00
tag_array.setTarget('inst);
2012-01-25 00:13:49 +01:00
val tag_rdata = tag_array.rw(tag_addr, r_cpu_miss_tag, tag_we);
// valid bit array
val vb_array = Reg(resetVal = Bits(0, lines));
when (io.cpu.invalidate) {
vb_array <== Bits(0,lines);
}
2011-11-12 09:25:06 +01:00
when (tag_we) {
2012-01-25 00:13:49 +01:00
vb_array <== vb_array.bitSet(r_cpu_req_idx(indexmsb,indexlsb).toUFix, UFix(1,1));
2011-11-05 04:52:21 +01:00
}
2012-01-25 00:13:49 +01:00
val tag_valid = vb_array(r_cpu_req_idx(indexmsb,indexlsb)).toBool;
val tag_hit = tag_valid && (tag_rdata === r_cpu_hit_addr(tagmsb,taglsb))
2011-11-12 09:25:06 +01:00
// data array
val data_addr =
2012-01-25 00:13:49 +01:00
Mux((state === s_refill_wait) || (state === s_refill), Cat(r_cpu_req_idx(indexmsb,offsetbits), refill_count),
io.cpu.req_idx(indexmsb, offsetbits-rf_cnt_bits)).toUFix;
2011-12-09 09:42:43 +01:00
val data_array = Mem4(lines*REFILL_CYCLES, io.mem.resp_data);
data_array.setReadLatency(1);
2012-01-14 03:18:48 +01:00
data_array.setTarget('inst);
val data_array_rdata = data_array.rw(data_addr, io.mem.resp_data, io.mem.resp_val);
// output signals
2012-01-25 00:13:49 +01:00
io.cpu.resp_val := !io.cpu.itlb_miss && (state === s_ready) && r_cpu_req_val && tag_hit;
rdy <== !io.cpu.itlb_miss && (state === s_ready) && (!r_cpu_req_val || tag_hit);
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
io.mem.req_val := (state === s_request);
2012-01-25 00:13:49 +01:00
io.mem.req_addr := r_cpu_miss_addr(tagmsb,indexlsb).toUFix
// 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;
}
2012-01-25 00:13:49 +01:00
when (r_cpu_req_val && !tag_hit) {
2011-11-13 00:00:45 +01:00
state <== s_request;
}
}
is (s_request)
{
2011-11-12 09:25:06 +01:00
when (io.mem.req_rdy) {
state <== s_refill_wait;
}
}
is (s_refill_wait) {
2011-11-12 09:25:06 +01:00
when (io.mem.resp_val) {
state <== s_refill;
}
}
is (s_refill) {
2011-12-09 09:42:43 +01:00
when (io.mem.resp_val && (~refill_count === UFix(0))) {
2012-01-12 03:27:11 +01:00
state <== s_ready;
2011-11-12 09:25:06 +01:00
}
}
}
}
}