| 1 |
60 |
robfinch |
// ============================================================================
|
| 2 |
|
|
// __
|
| 3 |
|
|
// \\__/ o\ (C) 2018 Robert Finch, Waterloo
|
| 4 |
|
|
// \ __ / All rights reserved.
|
| 5 |
|
|
// \/_// robfinch<remove>@finitron.ca
|
| 6 |
|
|
// ||
|
| 7 |
|
|
//
|
| 8 |
|
|
// FT64_dcache.v
|
| 9 |
|
|
// - a simple direct mapped cache
|
| 10 |
|
|
// - three cycle latency
|
| 11 |
|
|
//
|
| 12 |
|
|
//
|
| 13 |
|
|
// This source file is free software: you can redistribute it and/or modify
|
| 14 |
|
|
// it under the terms of the GNU Lesser General Public License as published
|
| 15 |
|
|
// by the Free Software Foundation, either version 3 of the License, or
|
| 16 |
|
|
// (at your option) any later version.
|
| 17 |
|
|
//
|
| 18 |
|
|
// This source file is distributed in the hope that it will be useful,
|
| 19 |
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 20 |
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 21 |
|
|
// GNU General Public License for more details.
|
| 22 |
|
|
//
|
| 23 |
|
|
// You should have received a copy of the GNU General Public License
|
| 24 |
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
| 25 |
|
|
//
|
| 26 |
|
|
//
|
| 27 |
|
|
// ============================================================================
|
| 28 |
|
|
//
|
| 29 |
|
|
module FT64_dcache(rst, dce, wclk, wr, sel, wadr, whit, i, li, rclk, rdsize, radr, o, lo, rhit);
|
| 30 |
|
|
input rst;
|
| 31 |
|
|
input dce; // data cache enable
|
| 32 |
|
|
input wclk;
|
| 33 |
|
|
input wr;
|
| 34 |
|
|
input [7:0] sel;
|
| 35 |
|
|
input [37:0] wadr;
|
| 36 |
|
|
output whit;
|
| 37 |
|
|
input [63:0] i;
|
| 38 |
|
|
input [255:0] li; // line input
|
| 39 |
|
|
input rclk;
|
| 40 |
|
|
input [2:0] rdsize;
|
| 41 |
|
|
input [37:0] radr;
|
| 42 |
|
|
output reg [63:0] o;
|
| 43 |
|
|
output reg [255:0] lo; // line out
|
| 44 |
|
|
output reg rhit;
|
| 45 |
|
|
parameter byt = 3'd0;
|
| 46 |
|
|
parameter wyde = 3'd1;
|
| 47 |
|
|
parameter tetra = 3'd2;
|
| 48 |
|
|
parameter octa = 3'd3;
|
| 49 |
|
|
|
| 50 |
|
|
wire [255:0] dc;
|
| 51 |
|
|
wire [31:0] v;
|
| 52 |
|
|
wire rhita;
|
| 53 |
|
|
|
| 54 |
|
|
dcache_mem u1 (
|
| 55 |
|
|
.rst(rst),
|
| 56 |
|
|
.clka(wclk),
|
| 57 |
|
|
.ena(dce & wr),
|
| 58 |
|
|
.wea(sel),
|
| 59 |
|
|
.addra(wadr[13:0]),
|
| 60 |
|
|
.dina(i),
|
| 61 |
|
|
.clkb(rclk),
|
| 62 |
|
|
.enb(dce),
|
| 63 |
|
|
.addrb(radr[13:0]),
|
| 64 |
|
|
.doutb(dc),
|
| 65 |
|
|
.ov(v)
|
| 66 |
|
|
);
|
| 67 |
|
|
|
| 68 |
|
|
FT64_dcache_tag u3
|
| 69 |
|
|
(
|
| 70 |
|
|
.wclk(wclk),
|
| 71 |
|
|
.dce(dce),
|
| 72 |
|
|
.wr(wr && wadr[4:3]==2'b11),
|
| 73 |
|
|
.wadr(wadr),
|
| 74 |
|
|
.rclk(rclk),
|
| 75 |
|
|
.radr(radr),
|
| 76 |
|
|
.whit(whit),
|
| 77 |
|
|
.rhit(rhita)
|
| 78 |
|
|
);
|
| 79 |
|
|
|
| 80 |
|
|
wire [7:0] va = v >> radr[4:0];
|
| 81 |
|
|
always @(posedge rclk)
|
| 82 |
|
|
begin
|
| 83 |
|
|
case(rdsize)
|
| 84 |
|
|
byt: rhit <= rhita & va[ 0];
|
| 85 |
|
|
wyde: rhit <= rhita & &va[1:0];
|
| 86 |
|
|
tetra: rhit <= rhita & &va[3:0];
|
| 87 |
|
|
default:rhit <= rhita & &va[7:0];
|
| 88 |
|
|
endcase
|
| 89 |
|
|
end
|
| 90 |
|
|
|
| 91 |
|
|
// hit is also delayed by a clock already
|
| 92 |
|
|
always @(posedge rclk)
|
| 93 |
|
|
lo <= dc;
|
| 94 |
|
|
always @(posedge rclk)
|
| 95 |
|
|
o <= dc >> {radr[4:3],6'b0};
|
| 96 |
|
|
|
| 97 |
|
|
endmodule
|
| 98 |
|
|
|
| 99 |
|
|
// -----------------------------------------------------------------------------
|
| 100 |
|
|
// -----------------------------------------------------------------------------
|
| 101 |
|
|
|
| 102 |
|
|
module dcache_mem(rst, clka, ena, wea, addra, dina, clkb, enb, addrb, doutb, ov);
|
| 103 |
|
|
input rst;
|
| 104 |
|
|
input clka;
|
| 105 |
|
|
input ena;
|
| 106 |
|
|
input [7:0] wea;
|
| 107 |
|
|
input [13:0] addra;
|
| 108 |
|
|
input [63:0] dina;
|
| 109 |
|
|
input clkb;
|
| 110 |
|
|
input enb;
|
| 111 |
|
|
input [13:0] addrb;
|
| 112 |
|
|
output reg [255:0] doutb;
|
| 113 |
|
|
output reg [31:0] ov;
|
| 114 |
|
|
|
| 115 |
|
|
reg [255:0] mem [0:511];
|
| 116 |
|
|
reg [31:0] valid [0:511];
|
| 117 |
|
|
reg [255:0] doutb1;
|
| 118 |
|
|
reg [31:0] ov1;
|
| 119 |
|
|
|
| 120 |
|
|
integer n;
|
| 121 |
|
|
|
| 122 |
|
|
initial begin
|
| 123 |
|
|
for (n = 0; n < 512; n = n + 1)
|
| 124 |
|
|
valid[n] = 32'h00;
|
| 125 |
|
|
end
|
| 126 |
|
|
|
| 127 |
|
|
genvar g;
|
| 128 |
|
|
generate begin
|
| 129 |
|
|
for (g = 0; g < 4; g = g + 1)
|
| 130 |
|
|
always @(posedge clka)
|
| 131 |
|
|
begin
|
| 132 |
|
|
if (ena & wea[0] & addra[4:3]==g) mem[addra[13:5]][g*64+7:g*64] <= dina[7:0];
|
| 133 |
|
|
if (ena & wea[1] & addra[4:3]==g) mem[addra[13:5]][g*64+15:g*64+8] <= dina[15:8];
|
| 134 |
|
|
if (ena & wea[2] & addra[4:3]==g) mem[addra[13:5]][g*64+23:g*64+16] <= dina[23:16];
|
| 135 |
|
|
if (ena & wea[3] & addra[4:3]==g) mem[addra[13:5]][g*64+31:g*64+24] <= dina[31:24];
|
| 136 |
|
|
if (ena & wea[4] & addra[4:3]==g) mem[addra[13:5]][g*64+39:g*64+32] <= dina[39:32];
|
| 137 |
|
|
if (ena & wea[5] & addra[4:3]==g) mem[addra[13:5]][g*64+47:g*64+40] <= dina[47:40];
|
| 138 |
|
|
if (ena & wea[6] & addra[4:3]==g) mem[addra[13:5]][g*64+55:g*64+48] <= dina[55:48];
|
| 139 |
|
|
if (ena & wea[7] & addra[4:3]==g) mem[addra[13:5]][g*64+63:g*64+56] <= dina[63:56];
|
| 140 |
|
|
if (ena & wea[0] & addra[4:3]==g) valid[addra[13:5]][g*8] <= 1'b1;
|
| 141 |
|
|
if (ena & wea[1] & addra[4:3]==g) valid[addra[13:5]][g*8+1] <= 1'b1;
|
| 142 |
|
|
if (ena & wea[2] & addra[4:3]==g) valid[addra[13:5]][g*8+2] <= 1'b1;
|
| 143 |
|
|
if (ena & wea[3] & addra[4:3]==g) valid[addra[13:5]][g*8+3] <= 1'b1;
|
| 144 |
|
|
if (ena & wea[4] & addra[4:3]==g) valid[addra[13:5]][g*8+4] <= 1'b1;
|
| 145 |
|
|
if (ena & wea[5] & addra[4:3]==g) valid[addra[13:5]][g*8+5] <= 1'b1;
|
| 146 |
|
|
if (ena & wea[6] & addra[4:3]==g) valid[addra[13:5]][g*8+6] <= 1'b1;
|
| 147 |
|
|
if (ena & wea[7] & addra[4:3]==g) valid[addra[13:5]][g*8+7] <= 1'b1;
|
| 148 |
|
|
end
|
| 149 |
|
|
end
|
| 150 |
|
|
endgenerate
|
| 151 |
|
|
always @(posedge clkb)
|
| 152 |
|
|
if (enb)
|
| 153 |
|
|
doutb1 <= mem[addrb[13:5]];
|
| 154 |
|
|
always @(posedge clkb)
|
| 155 |
|
|
if (enb)
|
| 156 |
|
|
doutb <= doutb1;
|
| 157 |
|
|
always @(posedge clkb)
|
| 158 |
|
|
if (enb)
|
| 159 |
|
|
ov1 <= valid[addrb[13:5]];
|
| 160 |
|
|
always @(posedge clkb)
|
| 161 |
|
|
if (enb)
|
| 162 |
|
|
ov <= ov1;
|
| 163 |
|
|
endmodule
|
| 164 |
|
|
|
| 165 |
|
|
// -----------------------------------------------------------------------------
|
| 166 |
|
|
// -----------------------------------------------------------------------------
|
| 167 |
|
|
|
| 168 |
|
|
module FT64_dcache_tag(wclk, dce, wr, wadr, rclk, radr, whit, rhit);
|
| 169 |
|
|
input wclk;
|
| 170 |
|
|
input dce; // data cache enable
|
| 171 |
|
|
input wr;
|
| 172 |
|
|
input [37:0] wadr;
|
| 173 |
|
|
input rclk;
|
| 174 |
|
|
input [37:0] radr;
|
| 175 |
|
|
output reg whit; // write hit
|
| 176 |
|
|
output reg rhit; // read hit
|
| 177 |
|
|
|
| 178 |
|
|
wire [31:0] rtago;
|
| 179 |
|
|
wire [31:0] wtago;
|
| 180 |
|
|
|
| 181 |
|
|
FT64_dcache_tag2 u1 (
|
| 182 |
|
|
.clka(wclk),
|
| 183 |
|
|
.ena(dce),
|
| 184 |
|
|
.wea(wr),
|
| 185 |
|
|
.addra(wadr[13:5]),
|
| 186 |
|
|
.dina(wadr[37:14]),
|
| 187 |
|
|
.douta(wtago),
|
| 188 |
|
|
.clkb(rclk),
|
| 189 |
|
|
.web(1'b0),
|
| 190 |
|
|
.dinb(32'd0),
|
| 191 |
|
|
.enb(dce),
|
| 192 |
|
|
.addrb(radr[13:5]),
|
| 193 |
|
|
.doutb(rtago)
|
| 194 |
|
|
);
|
| 195 |
|
|
|
| 196 |
|
|
always @(posedge rclk)
|
| 197 |
|
|
rhit <= rtago[23:0]==radr[37:14];
|
| 198 |
|
|
always @(posedge wclk)
|
| 199 |
|
|
whit <= wtago[23:0]==wadr[37:14];
|
| 200 |
|
|
|
| 201 |
|
|
endmodule
|
| 202 |
|
|
|