| 1 | /* 16 cache entries, 64-byte long cache lines */ |
| 2 | |
| 3 | module ICache( |
| 4 | input clk, |
| 5 | |
| 6 | /* ARM core interface */ |
| 7 | input [31:0] rd_addr, |
| 8 | input rd_req, |
| 9 | output reg rd_wait, |
| 10 | output reg [31:0] rd_data, |
| 11 | |
| 12 | /* bus interface */ |
| 13 | output wire bus_req, |
| 14 | input bus_ack, |
| 15 | output reg [31:0] bus_addr, |
| 16 | input [31:0] bus_rdata, |
| 17 | output wire [31:0] bus_wdata, |
| 18 | output reg bus_rd, |
| 19 | output wire bus_wr, |
| 20 | input bus_ready); |
| 21 | |
| 22 | assign bus_wr = 0; |
| 23 | assign bus_wdata = 0; |
| 24 | |
| 25 | /* [31 tag 10] [9 cache index 6] [5 data index 0] |
| 26 | * so the data index is 6 bits long |
| 27 | * so the cache index is 4 bits long |
| 28 | * so the tag is 22 bits long. c.c |
| 29 | */ |
| 30 | |
| 31 | reg cache_valid [15:0]; |
| 32 | reg [21:0] cache_tags [15:0]; |
| 33 | reg [31:0] cache_data [15:0 /* line */] [15:0 /* word */]; |
| 34 | |
| 35 | reg [4:0] i; |
| 36 | initial |
| 37 | for (i = 0; i < 16; i = i + 1) |
| 38 | begin |
| 39 | cache_valid[i[3:0]] = 0; |
| 40 | cache_tags[i[3:0]] = 0; |
| 41 | end |
| 42 | |
| 43 | wire [5:0] rd_didx = rd_addr[5:0]; |
| 44 | wire [3:0] rd_didx_word = rd_didx[5:2]; |
| 45 | wire [3:0] rd_idx = rd_addr[9:6]; |
| 46 | wire [21:0] rd_tag = rd_addr[31:10]; |
| 47 | |
| 48 | wire cache_hit = cache_valid[rd_idx] && (cache_tags[rd_idx] == rd_tag); |
| 49 | |
| 50 | always @(*) begin /* XXX does this work nowadays? */ |
| 51 | rd_wait = rd_req && !cache_hit; |
| 52 | rd_data = cache_data[rd_idx][rd_didx_word]; |
| 53 | end |
| 54 | |
| 55 | reg [3:0] cache_fill_pos = 0; |
| 56 | assign bus_req = rd_req && !cache_hit; /* xxx, needed for Verilator */ |
| 57 | always @(*) |
| 58 | if (rd_req && !cache_hit && bus_ack) begin |
| 59 | bus_addr = {rd_addr[31:6], cache_fill_pos[3:0], 2'b00 /* reads are 32-bits */}; |
| 60 | bus_rd = 1; |
| 61 | end else begin |
| 62 | bus_addr = 0; |
| 63 | bus_rd = 0; |
| 64 | end |
| 65 | |
| 66 | always @(posedge clk) |
| 67 | if (rd_req && !cache_hit) begin |
| 68 | if (bus_ready) begin /* Started the fill, and we have data. */ |
| 69 | cache_data[rd_idx][cache_fill_pos] <= bus_rdata; |
| 70 | cache_fill_pos <= cache_fill_pos + 1; |
| 71 | if (cache_fill_pos == 15) begin /* Done? */ |
| 72 | cache_tags[rd_idx] <= rd_tag; |
| 73 | cache_valid[rd_idx] <= 1; |
| 74 | end |
| 75 | end |
| 76 | end |
| 77 | endmodule |