OpenCores
URL https://opencores.org/ocsvn/zipcpu/zipcpu/trunk

Subversion Repositories zipcpu

[/] [zipcpu/] [trunk/] [rtl/] [peripherals/] [flashcache.v] - Blame information for rev 201

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 201 dgisselq
////////////////////////////////////////////////////////////////////////////////
2 2 dgisselq
//
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 201 dgisselq
////////////////////////////////////////////////////////////////////////////////
45 2 dgisselq
//
46 201 dgisselq
// Copyright (C) 2015,2017, Gisselquist Technology, LLC
47 2 dgisselq
//
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 201 dgisselq
// You should have received a copy of the GNU General Public License along
59
// with this program.  (It's in the $(ROOT)/doc directory.  Run make with no
60
// target there if the PDF file isn't present.)  If not, see
61
// <http://www.gnu.org/licenses/> for a copy.
62
//
63 2 dgisselq
// License:     GPL, v3, as defined and found on www.gnu.org,
64
//              http://www.gnu.org/licenses/gpl.html
65
//
66
//
67 201 dgisselq
////////////////////////////////////////////////////////////////////////////////
68 2 dgisselq
//
69 201 dgisselq
//
70 2 dgisselq
module  flashcache(i_clk,
71
                // Wishbone contrl interface
72
                i_wb_cyc, i_wb_stb,i_wb_ctrl_stb, i_wb_we, i_wb_addr, i_wb_data,
73
                        o_wb_ack, o_wb_stall, o_wb_data,
74
                // Wishbone copy interface
75
                o_cp_cyc, o_cp_stb, o_cp_we, o_cp_addr, o_cp_data,
76
                        i_cp_ack, i_cp_stall, i_cp_data,
77
                o_int);
78
        parameter       LGCACHELEN=10; // 4 kB
79
        input                   i_clk;
80
        // Control interface, CPU interface to cache
81
        input                   i_wb_cyc, i_wb_stb,i_wb_ctrl_stb, i_wb_we;
82
        input           [(LGCACHELEN-1):0]       i_wb_addr;
83
        input           [31:0]   i_wb_data;
84
        output  reg             o_wb_ack;
85
        output  wire            o_wb_stall;
86
        output  wire    [31:0]   o_wb_data;
87
        // Interface to peripheral bus, including flash
88
        output  reg             o_cp_cyc, o_cp_stb;
89
        output  wire            o_cp_we;
90
        output  reg     [31:0]   o_cp_addr;
91
        output  wire    [31:0]   o_cp_data;
92
        input                   i_cp_ack, i_cp_stall;
93
        input           [31:0]   i_cp_data;
94
        // And an interrupt to send once we complete
95
        output  reg             o_int;
96
 
97
        reg             loading;
98
        reg     [31:0]   cache_base;
99
        reg     [31:0]   cache   [0:((1<<LGCACHELEN)-1)];
100
 
101
        // Decouple writing the cache base from the highly delayed bus lines
102
        reg             wr_cache_base_flag;
103
        reg     [31:0]   wr_cache_base_value;
104
        always @(posedge i_clk)
105
                wr_cache_base_flag <= ((i_wb_cyc)&&(i_wb_ctrl_stb)&&(i_wb_we));
106
        always @(posedge i_clk)
107
                wr_cache_base_value<= { i_wb_data[31:1], 1'b0 };
108
 
109
        initial cache_base = 32'hffffffff;
110
        always @(posedge i_clk)
111
                if (wr_cache_base_flag)
112
                        cache_base <= wr_cache_base_value;
113
 
114
        reg     new_cache_base;
115
        initial new_cache_base = 1'b0;
116
        always @(posedge i_clk)
117
                if ((wr_cache_base_flag)&&(cache_base != wr_cache_base_value))
118
                        new_cache_base <= 1'b1;
119
                else
120
                        new_cache_base <= 1'b0;
121
 
122
        reg     [(LGCACHELEN-1):0]       rdaddr;
123
        initial loading = 1'b0;
124
        always @(posedge i_clk)
125
                if (new_cache_base)
126
                begin
127
                        loading <= 1'b1;
128
                        o_cp_cyc <= 1'b0;
129
                end else if ((~o_cp_cyc)&&(loading))
130
                begin
131
                        o_cp_cyc <= 1'b1;
132
                end else if (o_cp_cyc)
133
                begin
134
                        // Handle the ack/read line
135
                        if (i_cp_ack)
136
                        begin
137
                                if (&rdaddr)
138
                                begin
139
                                        o_cp_cyc <= 1'b0;
140
                                        loading <= 1'b0;
141
                                end
142
                        end
143
                end
144
        always @(posedge i_clk)
145
                if (~o_cp_cyc)
146
                        o_cp_addr <= cache_base;
147
                else if ((o_cp_cyc)&&(o_cp_stb)&&(~i_cp_stall))
148
                        o_cp_addr <= o_cp_addr + 1;;
149
        always @(posedge i_clk)
150
                if ((~o_cp_cyc)&&(loading))
151
                        o_cp_stb  <= 1'b1;
152
                else if ((o_cp_cyc)&&(o_cp_stb)&&(~i_cp_stall))
153
                begin
154
                        // We've made our last request
155
                        if (o_cp_addr >= cache_base + { {(32-LGCACHELEN-1){1'b0}}, 1'b1, {(LGCACHELEN){1'b0}}})
156
                                o_cp_stb <= 1'b0;
157
                end
158
        always @(posedge i_clk)
159
                if (~loading)
160
                        rdaddr    <= 0;
161
                else if ((o_cp_cyc)&&(i_cp_ack))
162
                        rdaddr <= rdaddr + 1;
163
 
164
        initial o_int = 1'b0;
165
        always @(posedge i_clk)
166
                if ((o_cp_cyc)&&(i_cp_ack)&&(&rdaddr))
167
                        o_int <= 1'b1;
168
                else
169
                        o_int <= 1'b0;
170
 
171
        assign  o_cp_we = 1'b0;
172
        assign  o_cp_data = 32'h00;
173
 
174
 
175
        //
176
        //      Writes to our cache ... always delayed by a clock.
177
        //              Clock 0 :       Write request
178
        //              Clock 1 :       Write takes place
179
        //              Clock 2 :       Available for reading
180
        //
181
        reg                             we;
182
        reg     [(LGCACHELEN-1):0]       waddr;
183
        reg     [31:0]                   wval;
184
        always @(posedge i_clk)
185
                we <= (loading)?((o_cp_cyc)&&(i_cp_ack)):(i_wb_cyc)&&(i_wb_stb)&&(i_wb_we);
186
        always @(posedge i_clk)
187
                waddr <= (loading)?rdaddr:i_wb_addr;
188
        always @(posedge i_clk)
189
                wval <= (loading)?i_cp_data:i_wb_data;
190
 
191
        always @(posedge i_clk)
192
                if (we)
193
                        cache[waddr] <= wval;
194
 
195
        reg     [31:0]   cache_data;
196
        always @(posedge i_clk)
197
                if ((i_wb_cyc)&&(i_wb_stb))
198
                        cache_data <= cache[i_wb_addr];
199
 
200
        always @(posedge i_clk)
201
                o_wb_ack <= (i_wb_cyc)&&(
202
                                ((i_wb_stb)&&(~loading))
203
                                ||(i_wb_ctrl_stb));
204
        reg     ctrl;
205
        always @(posedge i_clk)
206
                ctrl <= i_wb_ctrl_stb;
207
        assign  o_wb_data = (ctrl)?({cache_base[31:1],loading}):cache_data;
208
        assign  o_wb_stall = (loading)&&(~o_wb_ack);
209
 
210
endmodule

powered by: WebSVN 2.1.0

© copyright 1999-2022 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.