]>
Commit | Line | Data |
---|---|---|
7e757d70 JW |
1 | /* 16 cache entries, 64-byte long cache lines */ |
2 | ||
3 | module ICache( | |
4 | input clk, | |
c808669d JW |
5 | |
6 | /* ARM core interface */ | |
7e757d70 JW |
7 | input [31:0] rd_addr, |
8 | input rd_req, | |
9 | output reg rd_wait, | |
c808669d JW |
10 | output reg [31:0] rd_data, |
11 | ||
12 | /* bus interface */ | |
90ff449a | 13 | output wire bus_req, |
c808669d JW |
14 | input bus_ack, |
15 | output reg [31:0] bus_addr, | |
cb1e103e JW |
16 | input [31:0] bus_rdata, |
17 | output wire [31:0] bus_wdata, | |
c808669d JW |
18 | output reg bus_rd, |
19 | output wire bus_wr, | |
20 | input bus_ready); | |
21 | ||
22 | assign bus_wr = 0; | |
cb1e103e | 23 | assign bus_wdata = 0; |
7e757d70 | 24 | |
f83818d5 | 25 | /* [31 tag 10] [9 cache index 6] [5 data index 0] |
7e757d70 JW |
26 | * so the data index is 6 bits long |
27 | * so the cache index is 4 bits long | |
f83818d5 | 28 | * so the tag is 22 bits long. c.c |
7e757d70 JW |
29 | */ |
30 | ||
31 | reg cache_valid [15:0]; | |
f83818d5 | 32 | reg [21:0] cache_tags [15:0]; |
28c904cc | 33 | reg [31:0] cache_data [15:0 /* line */] [15:0 /* word */]; |
7e757d70 | 34 | |
cb1e103e | 35 | reg [4:0] i; |
7e757d70 | 36 | initial |
cb1e103e | 37 | for (i = 0; i < 16; i = i + 1) |
90ff449a | 38 | begin |
cb1e103e | 39 | cache_valid[i[3:0]] = 0; |
90ff449a JW |
40 | cache_tags[i[3:0]] = 0; |
41 | end | |
7e757d70 JW |
42 | |
43 | wire [5:0] rd_didx = rd_addr[5:0]; | |
44 | wire [3:0] rd_didx_word = rd_didx[5:2]; | |
f83818d5 JW |
45 | wire [3:0] rd_idx = rd_addr[9:6]; |
46 | wire [21:0] rd_tag = rd_addr[31:10]; | |
7e757d70 | 47 | |
8b417b45 JW |
48 | reg [31:0] prev_rd_addr = 32'hFFFFFFFF; |
49 | ||
c808669d JW |
50 | wire cache_hit = cache_valid[rd_idx] && (cache_tags[rd_idx] == rd_tag); |
51 | ||
7e757d70 | 52 | always @(*) begin /* XXX does this work nowadays? */ |
327f45ff | 53 | rd_wait = rd_req && !cache_hit; |
7e757d70 JW |
54 | rd_data = cache_data[rd_idx][rd_didx_word]; |
55 | end | |
c808669d JW |
56 | |
57 | reg [3:0] cache_fill_pos = 0; | |
45fa96c0 | 58 | assign bus_req = rd_req && !cache_hit; /* xxx, needed for Verilator */ |
28c904cc | 59 | always @(*) |
45fa96c0 JW |
60 | if (rd_req && !cache_hit && bus_ack) begin |
61 | bus_addr = {rd_addr[31:6], cache_fill_pos[3:0], 2'b00 /* reads are 32-bits */}; | |
62 | bus_rd = 1; | |
327f45ff | 63 | end else begin |
327f45ff JW |
64 | bus_addr = 0; |
65 | bus_rd = 0; | |
66 | end | |
67 | ||
8b417b45 JW |
68 | always @(posedge clk) begin |
69 | prev_rd_addr <= {rd_addr[31:6], 6'b0}; | |
70 | if (cache_fill_pos != 0 && ((prev_rd_addr != {rd_addr[31:6], 6'b0}) || cache_hit)) /* If this wasn't from the same line, or we've moved on somehow, reset the fill circuitry. */ | |
71 | cache_fill_pos <= 0; | |
72 | else if (rd_req && !cache_hit) begin | |
327f45ff | 73 | if (bus_ready) begin /* Started the fill, and we have data. */ |
8b417b45 | 74 | $display("ICACHE: FILL: rd addr %08x; bus addr %08x; bus data %08x", rd_addr, bus_addr, bus_rdata); |
45fa96c0 | 75 | cache_data[rd_idx][cache_fill_pos] <= bus_rdata; |
327f45ff | 76 | cache_fill_pos <= cache_fill_pos + 1; |
cb1e103e | 77 | if (cache_fill_pos == 15) begin /* Done? */ |
45fa96c0 JW |
78 | cache_tags[rd_idx] <= rd_tag; |
79 | cache_valid[rd_idx] <= 1; | |
327f45ff JW |
80 | end |
81 | end | |
c808669d | 82 | end |
8b417b45 | 83 | end |
7e757d70 | 84 | endmodule |