1 |
2 |
dgisselq |
///////////////////////////////////////////////////////////////////////////
|
2 |
|
|
//
|
3 |
|
|
// Filename: flashcache.v
|
4 |
|
|
//
|
5 |
|
|
// Project: Zip CPU -- a small, lightweight, RISC CPU soft core
|
6 |
|
|
//
|
7 |
|
|
// Purpose: Since my Zip CPU has primary access to a flash, which requires
|
8 |
|
|
// nearly 24 clock cycles per read, this 'cache' module
|
9 |
|
|
// is offered to minimize the effect. The CPU may now request
|
10 |
|
|
// some amount of flash to be copied into this on-chip RAM,
|
11 |
|
|
// and then access it with nearly zero latency.
|
12 |
|
|
//
|
13 |
69 |
dgisselq |
// Status: This file is no longer being used as an active file within
|
14 |
|
|
// the ZipCPU project. It's an older file from an idea that
|
15 |
|
|
// never really caught traction.
|
16 |
|
|
//
|
17 |
2 |
dgisselq |
// Interface:
|
18 |
|
|
// FlashCache sits on the Wishbone bus as both a slave and a master.
|
19 |
|
|
// Slave requests for memory will get mapped to a local RAM, from which
|
20 |
|
|
// reads and writes may take place.
|
21 |
|
|
//
|
22 |
|
|
// This cache supports a single control register: the base wishbone address
|
23 |
|
|
// of the device to copy memory from. The bottom bit if this address must
|
24 |
|
|
// be zero (or it will be silently rendered as zero). When read, this
|
25 |
|
|
// bottom bit will indicate 1) that the controller is still loading memory
|
26 |
|
|
// into the cache, or 0) that the cache is ready to be used.
|
27 |
|
|
//
|
28 |
|
|
// Writing to this register will initiate a memory copy from the (new)
|
29 |
|
|
// address. Once done, the loading bit will be cleared and an interrupt
|
30 |
|
|
// generated.
|
31 |
|
|
//
|
32 |
|
|
// Where this memory is placed on the wishbone bus is entirely up to the
|
33 |
|
|
// wishbone bus control logic. Setting the memory base to an
|
34 |
|
|
// address controlled by this flashcache will produce unusable
|
35 |
|
|
// results, and may well hang the bus.
|
36 |
|
|
// Reads from the memory before complete will return immediately with
|
37 |
|
|
// the value if read address is less than the current copy
|
38 |
|
|
// address, or else they will stall until the read address is
|
39 |
|
|
// less than the copy address.
|
40 |
|
|
//
|
41 |
|
|
// Creator: Dan Gisselquist, Ph.D.
|
42 |
69 |
dgisselq |
// Gisselquist Technology, LLC
|
43 |
2 |
dgisselq |
//
|
44 |
|
|
///////////////////////////////////////////////////////////////////////////
|
45 |
|
|
//
|
46 |
|
|
// Copyright (C) 2015, Gisselquist Technology, LLC
|
47 |
|
|
//
|
48 |
|
|
// This program is free software (firmware): you can redistribute it and/or
|
49 |
|
|
// modify it under the terms of the GNU General Public License as published
|
50 |
|
|
// by the Free Software Foundation, either version 3 of the License, or (at
|
51 |
|
|
// your option) any later version.
|
52 |
|
|
//
|
53 |
|
|
// This program is distributed in the hope that it will be useful, but WITHOUT
|
54 |
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
|
55 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
56 |
|
|
// for more details.
|
57 |
|
|
//
|
58 |
|
|
// License: GPL, v3, as defined and found on www.gnu.org,
|
59 |
|
|
// http://www.gnu.org/licenses/gpl.html
|
60 |
|
|
//
|
61 |
|
|
//
|
62 |
|
|
///////////////////////////////////////////////////////////////////////////
|
63 |
|
|
//
|
64 |
|
|
module flashcache(i_clk,
|
65 |
|
|
// Wishbone contrl interface
|
66 |
|
|
i_wb_cyc, i_wb_stb,i_wb_ctrl_stb, i_wb_we, i_wb_addr, i_wb_data,
|
67 |
|
|
o_wb_ack, o_wb_stall, o_wb_data,
|
68 |
|
|
// Wishbone copy interface
|
69 |
|
|
o_cp_cyc, o_cp_stb, o_cp_we, o_cp_addr, o_cp_data,
|
70 |
|
|
i_cp_ack, i_cp_stall, i_cp_data,
|
71 |
|
|
o_int);
|
72 |
|
|
parameter LGCACHELEN=10; // 4 kB
|
73 |
|
|
input i_clk;
|
74 |
|
|
// Control interface, CPU interface to cache
|
75 |
|
|
input i_wb_cyc, i_wb_stb,i_wb_ctrl_stb, i_wb_we;
|
76 |
|
|
input [(LGCACHELEN-1):0] i_wb_addr;
|
77 |
|
|
input [31:0] i_wb_data;
|
78 |
|
|
output reg o_wb_ack;
|
79 |
|
|
output wire o_wb_stall;
|
80 |
|
|
output wire [31:0] o_wb_data;
|
81 |
|
|
// Interface to peripheral bus, including flash
|
82 |
|
|
output reg o_cp_cyc, o_cp_stb;
|
83 |
|
|
output wire o_cp_we;
|
84 |
|
|
output reg [31:0] o_cp_addr;
|
85 |
|
|
output wire [31:0] o_cp_data;
|
86 |
|
|
input i_cp_ack, i_cp_stall;
|
87 |
|
|
input [31:0] i_cp_data;
|
88 |
|
|
// And an interrupt to send once we complete
|
89 |
|
|
output reg o_int;
|
90 |
|
|
|
91 |
|
|
reg loading;
|
92 |
|
|
reg [31:0] cache_base;
|
93 |
|
|
reg [31:0] cache [0:((1<<LGCACHELEN)-1)];
|
94 |
|
|
|
95 |
|
|
// Decouple writing the cache base from the highly delayed bus lines
|
96 |
|
|
reg wr_cache_base_flag;
|
97 |
|
|
reg [31:0] wr_cache_base_value;
|
98 |
|
|
always @(posedge i_clk)
|
99 |
|
|
wr_cache_base_flag <= ((i_wb_cyc)&&(i_wb_ctrl_stb)&&(i_wb_we));
|
100 |
|
|
always @(posedge i_clk)
|
101 |
|
|
wr_cache_base_value<= { i_wb_data[31:1], 1'b0 };
|
102 |
|
|
|
103 |
|
|
initial cache_base = 32'hffffffff;
|
104 |
|
|
always @(posedge i_clk)
|
105 |
|
|
if (wr_cache_base_flag)
|
106 |
|
|
cache_base <= wr_cache_base_value;
|
107 |
|
|
|
108 |
|
|
reg new_cache_base;
|
109 |
|
|
initial new_cache_base = 1'b0;
|
110 |
|
|
always @(posedge i_clk)
|
111 |
|
|
if ((wr_cache_base_flag)&&(cache_base != wr_cache_base_value))
|
112 |
|
|
new_cache_base <= 1'b1;
|
113 |
|
|
else
|
114 |
|
|
new_cache_base <= 1'b0;
|
115 |
|
|
|
116 |
|
|
reg [(LGCACHELEN-1):0] rdaddr;
|
117 |
|
|
initial loading = 1'b0;
|
118 |
|
|
always @(posedge i_clk)
|
119 |
|
|
if (new_cache_base)
|
120 |
|
|
begin
|
121 |
|
|
loading <= 1'b1;
|
122 |
|
|
o_cp_cyc <= 1'b0;
|
123 |
|
|
end else if ((~o_cp_cyc)&&(loading))
|
124 |
|
|
begin
|
125 |
|
|
o_cp_cyc <= 1'b1;
|
126 |
|
|
end else if (o_cp_cyc)
|
127 |
|
|
begin
|
128 |
|
|
// Handle the ack/read line
|
129 |
|
|
if (i_cp_ack)
|
130 |
|
|
begin
|
131 |
|
|
if (&rdaddr)
|
132 |
|
|
begin
|
133 |
|
|
o_cp_cyc <= 1'b0;
|
134 |
|
|
loading <= 1'b0;
|
135 |
|
|
end
|
136 |
|
|
end
|
137 |
|
|
end
|
138 |
|
|
always @(posedge i_clk)
|
139 |
|
|
if (~o_cp_cyc)
|
140 |
|
|
o_cp_addr <= cache_base;
|
141 |
|
|
else if ((o_cp_cyc)&&(o_cp_stb)&&(~i_cp_stall))
|
142 |
|
|
o_cp_addr <= o_cp_addr + 1;;
|
143 |
|
|
always @(posedge i_clk)
|
144 |
|
|
if ((~o_cp_cyc)&&(loading))
|
145 |
|
|
o_cp_stb <= 1'b1;
|
146 |
|
|
else if ((o_cp_cyc)&&(o_cp_stb)&&(~i_cp_stall))
|
147 |
|
|
begin
|
148 |
|
|
// We've made our last request
|
149 |
|
|
if (o_cp_addr >= cache_base + { {(32-LGCACHELEN-1){1'b0}}, 1'b1, {(LGCACHELEN){1'b0}}})
|
150 |
|
|
o_cp_stb <= 1'b0;
|
151 |
|
|
end
|
152 |
|
|
always @(posedge i_clk)
|
153 |
|
|
if (~loading)
|
154 |
|
|
rdaddr <= 0;
|
155 |
|
|
else if ((o_cp_cyc)&&(i_cp_ack))
|
156 |
|
|
rdaddr <= rdaddr + 1;
|
157 |
|
|
|
158 |
|
|
initial o_int = 1'b0;
|
159 |
|
|
always @(posedge i_clk)
|
160 |
|
|
if ((o_cp_cyc)&&(i_cp_ack)&&(&rdaddr))
|
161 |
|
|
o_int <= 1'b1;
|
162 |
|
|
else
|
163 |
|
|
o_int <= 1'b0;
|
164 |
|
|
|
165 |
|
|
assign o_cp_we = 1'b0;
|
166 |
|
|
assign o_cp_data = 32'h00;
|
167 |
|
|
|
168 |
|
|
|
169 |
|
|
//
|
170 |
|
|
// Writes to our cache ... always delayed by a clock.
|
171 |
|
|
// Clock 0 : Write request
|
172 |
|
|
// Clock 1 : Write takes place
|
173 |
|
|
// Clock 2 : Available for reading
|
174 |
|
|
//
|
175 |
|
|
reg we;
|
176 |
|
|
reg [(LGCACHELEN-1):0] waddr;
|
177 |
|
|
reg [31:0] wval;
|
178 |
|
|
always @(posedge i_clk)
|
179 |
|
|
we <= (loading)?((o_cp_cyc)&&(i_cp_ack)):(i_wb_cyc)&&(i_wb_stb)&&(i_wb_we);
|
180 |
|
|
always @(posedge i_clk)
|
181 |
|
|
waddr <= (loading)?rdaddr:i_wb_addr;
|
182 |
|
|
always @(posedge i_clk)
|
183 |
|
|
wval <= (loading)?i_cp_data:i_wb_data;
|
184 |
|
|
|
185 |
|
|
always @(posedge i_clk)
|
186 |
|
|
if (we)
|
187 |
|
|
cache[waddr] <= wval;
|
188 |
|
|
|
189 |
|
|
reg [31:0] cache_data;
|
190 |
|
|
always @(posedge i_clk)
|
191 |
|
|
if ((i_wb_cyc)&&(i_wb_stb))
|
192 |
|
|
cache_data <= cache[i_wb_addr];
|
193 |
|
|
|
194 |
|
|
always @(posedge i_clk)
|
195 |
|
|
o_wb_ack <= (i_wb_cyc)&&(
|
196 |
|
|
((i_wb_stb)&&(~loading))
|
197 |
|
|
||(i_wb_ctrl_stb));
|
198 |
|
|
reg ctrl;
|
199 |
|
|
always @(posedge i_clk)
|
200 |
|
|
ctrl <= i_wb_ctrl_stb;
|
201 |
|
|
assign o_wb_data = (ctrl)?({cache_base[31:1],loading}):cache_data;
|
202 |
|
|
assign o_wb_stall = (loading)&&(~o_wb_ack);
|
203 |
|
|
|
204 |
|
|
endmodule
|