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

Subversion Repositories zipcpu

[/] [zipcpu/] [trunk/] [rtl/] [core/] [pipefetch.v] - Blame information for rev 209

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 dgisselq
////////////////////////////////////////////////////////////////////////////////
2
//
3 3 dgisselq
// Filename:    pipefetch.v
4 2 dgisselq
//
5
// Project:     Zip CPU -- a small, lightweight, RISC CPU soft core
6
//
7 3 dgisselq
// Purpose:     Keeping our CPU fed with instructions, at one per clock and
8
//              with no stalls, can be quite a chore.  Worse, the Wishbone
9
//              takes a couple of cycles just to read one instruction from
10
//              the bus.  However, if we use pipeline accesses to the Wishbone
11
//              bus, then we can read more and faster.  Further, if we cache
12
//              these results so that we have them before we need them, then
13
//              we have a chance of keeping our CPU from stalling.  Those are
14
//              the purposes of this instruction fetch module: 1) Pipeline
15
//              wishbone accesses, and 2) an instruction cache.
16 2 dgisselq
//
17 36 dgisselq
//      20150919 -- Fixed a nasty race condition whereby the pipefetch routine
18
//              would produce either the same instruction twice, or skip
19
//              an instruction.  This condition was dependent on the CPU stall
20
//              condition, and would only take place if the pipeline wasn't 
21
//              completely full throughout the stall.
22
//
23
//              Interface support was also added for trapping on illegal
24
//              instructions (i.e., instruction fetches that cause bus errors),
25
//              however the internal interface has not caught up to supporting
26
//              these exceptions yet.
27
//
28 2 dgisselq
// Creator:     Dan Gisselquist, Ph.D.
29 69 dgisselq
//              Gisselquist Technology, LLC
30 2 dgisselq
//
31
////////////////////////////////////////////////////////////////////////////////
32
//
33 209 dgisselq
// Copyright (C) 2015,2017,2019 Gisselquist Technology, LLC
34 2 dgisselq
//
35
// This program is free software (firmware): you can redistribute it and/or
36
// modify it under the terms of  the GNU General Public License as published
37
// by the Free Software Foundation, either version 3 of the License, or (at
38
// your option) any later version.
39
//
40
// This program is distributed in the hope that it will be useful, but WITHOUT
41
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
42
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
43
// for more details.
44
//
45 201 dgisselq
// You should have received a copy of the GNU General Public License along
46
// with this program.  (It's in the $(ROOT)/doc directory.  Run make with no
47
// target there if the PDF file isn't present.)  If not, see
48
// <http://www.gnu.org/licenses/> for a copy.
49
//
50 2 dgisselq
// License:     GPL, v3, as defined and found on www.gnu.org,
51
//              http://www.gnu.org/licenses/gpl.html
52
//
53
//
54
////////////////////////////////////////////////////////////////////////////////
55
//
56 201 dgisselq
//
57 209 dgisselq
`default_nettype        none
58
//
59 18 dgisselq
module  pipefetch(i_clk, i_rst, i_new_pc, i_clear_cache, i_stall_n, i_pc,
60 2 dgisselq
                        o_i, o_pc, o_v,
61
                o_wb_cyc, o_wb_stb, o_wb_we, o_wb_addr, o_wb_data,
62 36 dgisselq
                        i_wb_ack, i_wb_stall, i_wb_err, i_wb_data, i_wb_request,
63
                        o_illegal);
64 3 dgisselq
        parameter       RESET_ADDRESS=32'h0010_0000,
65 48 dgisselq
                        LGCACHELEN = 6, ADDRESS_WIDTH=24,
66
                        CACHELEN=(1<<LGCACHELEN), BUSW=32, AW=ADDRESS_WIDTH;
67 209 dgisselq
        input   wire                    i_clk, i_rst, i_new_pc,
68 18 dgisselq
                                        i_clear_cache, i_stall_n;
69 209 dgisselq
        input   wire    [(AW-1):0]       i_pc;
70 2 dgisselq
        output  reg     [(BUSW-1):0]     o_i;
71 48 dgisselq
        output  reg     [(AW-1):0]       o_pc;
72 2 dgisselq
        output  wire                    o_v;
73
        //
74
        output  reg             o_wb_cyc, o_wb_stb;
75
        output  wire            o_wb_we;
76 48 dgisselq
        output  reg     [(AW-1):0]       o_wb_addr;
77 2 dgisselq
        output  wire    [(BUSW-1):0]     o_wb_data;
78
        //
79 209 dgisselq
        input   wire            i_wb_ack, i_wb_stall, i_wb_err;
80
        input   wire    [(BUSW-1):0]     i_wb_data;
81 3 dgisselq
        //
82
        // Is the (data) memory unit also requesting access to the bus?
83 209 dgisselq
        input   wire                    i_wb_request;
84 36 dgisselq
        output  wire                    o_illegal;
85 2 dgisselq
 
86
        // Fixed bus outputs: we read from the bus only, never write.
87
        // Thus the output data is ... irrelevant and don't care.  We set it
88
        // to zero just to set it to something.
89
        assign  o_wb_we = 1'b0;
90
        assign  o_wb_data = 0;
91
 
92 48 dgisselq
        reg     [(AW-1):0]               r_cache_base;
93 2 dgisselq
        reg     [(LGCACHELEN):0] r_nvalid, r_acks_waiting;
94
        reg     [(BUSW-1):0]             cache[0:(CACHELEN-1)];
95
 
96 56 dgisselq
        wire    [(LGCACHELEN-1):0]       w_cache_offset;
97
        reg     [1:0]                    r_cache_offset;
98 2 dgisselq
 
99
        reg                     r_addr_set;
100 48 dgisselq
        reg     [(AW-1):0]       r_addr;
101 2 dgisselq
 
102 48 dgisselq
        wire    [(AW-1):0]       bus_nvalid;
103
        assign  bus_nvalid = { {(AW-LGCACHELEN-1){1'b0}}, r_nvalid };
104 2 dgisselq
 
105 3 dgisselq
        // What are some of the conditions for which we need to restart the
106
        // cache?
107
        wire    w_pc_out_of_bounds;
108
        assign  w_pc_out_of_bounds = ((i_new_pc)&&((r_nvalid == 0)
109
                                        ||(i_pc < r_cache_base)
110 48 dgisselq
                                        ||(i_pc >= r_cache_base + CACHELEN)
111
                                        ||(i_pc >= r_cache_base + bus_nvalid+5)));
112 3 dgisselq
        wire    w_ran_off_end_of_cache;
113
        assign  w_ran_off_end_of_cache =((r_addr_set)&&((r_addr < r_cache_base)
114 48 dgisselq
                                        ||(r_addr >= r_cache_base + CACHELEN)
115
                                        ||(r_addr >= r_cache_base + bus_nvalid+5)));
116 3 dgisselq
        wire    w_running_out_of_cache;
117
        assign  w_running_out_of_cache = (r_addr_set)
118 56 dgisselq
                        &&(r_addr >= r_cache_base +
119
                                // {{(AW-LGCACHELEN-1),{1'b0}},2'b11,
120
                                //              {(LGCACHELEN-1){1'b0}}})
121
                                // (1<<(LGCACHELEN-2)) + (1<<(LGCACHELEN-1)))
122
                                +(3<<(LGCACHELEN-2)))
123 48 dgisselq
                        &&(|r_nvalid[(LGCACHELEN):(LGCACHELEN-1)]);
124
 
125 209 dgisselq
        initial r_cache_base = RESET_ADDRESS[(AW+1):2];
126 2 dgisselq
        always @(posedge i_clk)
127
        begin
128 63 dgisselq
                if ((i_rst)||(i_clear_cache)||((o_wb_cyc)&&(i_wb_err)))
129 3 dgisselq
                begin
130 2 dgisselq
                        o_wb_cyc <= 1'b0;
131 18 dgisselq
                        o_wb_stb <= 1'b0;
132 3 dgisselq
                        // r_cache_base <= RESET_ADDRESS;
133
                // end else if ((~o_wb_cyc)&&(i_new_pc)&&(r_nvalid != 0)
134
                //              &&(i_pc >= r_cache_base)
135
                //              &&(i_pc < r_cache_base + bus_nvalid))
136
                // begin
137 2 dgisselq
                        // The new instruction is in our cache, do nothing
138
                        // with the bus here.
139 3 dgisselq
                end else if ((o_wb_cyc)&&(w_pc_out_of_bounds))
140 2 dgisselq
                begin
141
                        // We need to abandon our bus action to start over in
142
                        // a new region, setting up a new cache.  This may
143
                        // happen mid cycle while waiting for a result.  By
144
                        // dropping o_wb_cyc, we state that we are no longer
145
                        // interested in that result--whatever it might be.
146
                        o_wb_cyc <= 1'b0;
147
                        o_wb_stb <= 1'b0;
148 3 dgisselq
                end else if ((~o_wb_cyc)&&(~r_nvalid[LGCACHELEN])&&(~i_wb_request)&&(r_addr_set))
149
                begin
150
                        // Restart a bus cycle that was interrupted when the
151
                        // data section wanted access to our bus.
152
                        o_wb_cyc <= 1'b1;
153
                        o_wb_stb <= 1'b1;
154
                        // o_wb_addr <= r_cache_base + bus_nvalid;
155 2 dgisselq
                end else if ((~o_wb_cyc)&&(
156 3 dgisselq
                                (w_pc_out_of_bounds)||(w_ran_off_end_of_cache)))
157 2 dgisselq
                begin
158
                        // Start a bus transaction
159
                        o_wb_cyc <= 1'b1;
160
                        o_wb_stb <= 1'b1;
161 3 dgisselq
                        // o_wb_addr <= (i_new_pc) ? i_pc : r_addr;
162
                        // r_nvalid <= 0;
163
                        // r_cache_base <= (i_new_pc) ? i_pc : r_addr;
164 56 dgisselq
                        // w_cache_offset <= 0;
165 3 dgisselq
                end else if ((~o_wb_cyc)&&(w_running_out_of_cache))
166 2 dgisselq
                begin
167
                        // If we're using the last quarter of the cache, then
168
                        // let's start a bus transaction to extend the cache.
169
                        o_wb_cyc <= 1'b1;
170
                        o_wb_stb <= 1'b1;
171 3 dgisselq
                        // o_wb_addr <= r_cache_base + (1<<(LGCACHELEN));
172
                        // r_nvalid <= r_nvalid - (1<<(LGCACHELEN-2));
173
                        // r_cache_base <= r_cache_base + (1<<(LGCACHELEN-2));
174 56 dgisselq
                        // w_cache_offset <= w_cache_offset + (1<<(LGCACHELEN-2));
175 2 dgisselq
                end else if (o_wb_cyc)
176
                begin
177
                        // This handles everything ... but the case where
178
                        // while reading we need to extend our cache.
179
                        if ((o_wb_stb)&&(~i_wb_stall))
180
                        begin
181 3 dgisselq
                                // o_wb_addr <= o_wb_addr + 1;
182
                                if ((o_wb_addr - r_cache_base >= CACHELEN-1)
183
                                        ||(i_wb_request))
184 2 dgisselq
                                        o_wb_stb <= 1'b0;
185
                        end
186
 
187
                        if (i_wb_ack)
188
                        begin
189 3 dgisselq
                                // r_nvalid <= r_nvalid + 1;
190 2 dgisselq
                                if ((r_acks_waiting == 1)&&(~o_wb_stb))
191
                                        o_wb_cyc <= 1'b0;
192 36 dgisselq
                        end else if ((r_acks_waiting == 0)&&(~o_wb_stb))
193
                                o_wb_cyc <= 1'b0;
194 2 dgisselq
                end
195
        end
196
 
197 63 dgisselq
 
198 36 dgisselq
        initial r_nvalid = 0;
199 3 dgisselq
        always @(posedge i_clk)
200 18 dgisselq
                if ((i_rst)||(i_clear_cache)) // Required, so we can reload memoy and then reset
201 11 dgisselq
                        r_nvalid <= 0;
202
                else if ((~o_wb_cyc)&&(
203 3 dgisselq
                                (w_pc_out_of_bounds)||(w_ran_off_end_of_cache)))
204
                        r_nvalid <= 0;
205
                else if ((~o_wb_cyc)&&(w_running_out_of_cache))
206 56 dgisselq
                        r_nvalid[LGCACHELEN:(LGCACHELEN-2)]
207
                                <= r_nvalid[LGCACHELEN:(LGCACHELEN-2)] +3'b111;
208
                                        // i.e.  - (1<<(LGCACHELEN-2));
209 3 dgisselq
                else if ((o_wb_cyc)&&(i_wb_ack))
210 56 dgisselq
                        r_nvalid <= r_nvalid + {{(LGCACHELEN){1'b0}},1'b1}; // +1;
211 3 dgisselq
 
212
        always @(posedge i_clk)
213 18 dgisselq
                if (i_clear_cache)
214
                        r_cache_base <= i_pc;
215
                else if ((~o_wb_cyc)&&(
216
                                (w_pc_out_of_bounds)
217
                                ||(w_ran_off_end_of_cache)))
218 3 dgisselq
                        r_cache_base <= (i_new_pc) ? i_pc : r_addr;
219
                else if ((~o_wb_cyc)&&(w_running_out_of_cache))
220 56 dgisselq
                        r_cache_base[(AW-1):(LGCACHELEN-2)]
221
                                <= r_cache_base[(AW-1):(LGCACHELEN-2)]
222
                                        + {{(AW-LGCACHELEN+1){1'b0}},1'b1};
223
                                        // i.e.  + (1<<(LGCACHELEN-2));
224 3 dgisselq
 
225
        always @(posedge i_clk)
226 18 dgisselq
                if (i_clear_cache)
227 3 dgisselq
                        r_cache_offset <= 0;
228 18 dgisselq
                else if ((~o_wb_cyc)&&(
229
                                (w_pc_out_of_bounds)
230
                                ||(w_ran_off_end_of_cache)))
231
                        r_cache_offset <= 0;
232 3 dgisselq
                else if ((~o_wb_cyc)&&(w_running_out_of_cache))
233 56 dgisselq
                        r_cache_offset[1:0] <= r_cache_offset[1:0] + 2'b01;
234
        assign  w_cache_offset = { r_cache_offset, {(LGCACHELEN-2){1'b0}} };
235 3 dgisselq
 
236
        always @(posedge i_clk)
237 18 dgisselq
                if (i_clear_cache)
238
                        o_wb_addr <= i_pc;
239 38 dgisselq
                else if ((o_wb_cyc)&&(w_pc_out_of_bounds))
240
                begin
241
                        if (i_wb_ack)
242
                                o_wb_addr <= r_cache_base + bus_nvalid+1;
243
                        else
244
                                o_wb_addr <= r_cache_base + bus_nvalid;
245
                end else if ((~o_wb_cyc)&&((w_pc_out_of_bounds)
246 3 dgisselq
                                        ||(w_ran_off_end_of_cache)))
247
                        o_wb_addr <= (i_new_pc) ? i_pc : r_addr;
248 63 dgisselq
                else if ((o_wb_stb)&&(~i_wb_stall))     // && o_wb_cyc
249 3 dgisselq
                        o_wb_addr <= o_wb_addr + 1;
250
 
251
        initial r_acks_waiting = 0;
252
        always @(posedge i_clk)
253
                if (~o_wb_cyc)
254
                        r_acks_waiting <= 0;
255 63 dgisselq
                // o_wb_cyc *must* be true for all following
256
                else if ((o_wb_stb)&&(~i_wb_stall)&&(~i_wb_ack)) //&&(o_wb_cyc)
257 56 dgisselq
                        r_acks_waiting <= r_acks_waiting + {{(LGCACHELEN){1'b0}},1'b1};
258 63 dgisselq
                else if ((i_wb_ack)&&((~o_wb_stb)||(i_wb_stall))) //&&(o_wb_cyc)
259 56 dgisselq
                        r_acks_waiting <= r_acks_waiting + {(LGCACHELEN+1){1'b1}}; // - 1;
260 3 dgisselq
 
261
        always @(posedge i_clk)
262
                if ((o_wb_cyc)&&(i_wb_ack))
263 56 dgisselq
                        cache[r_nvalid[(LGCACHELEN-1):0]+w_cache_offset]
264 3 dgisselq
                                        <= i_wb_data;
265
 
266 2 dgisselq
        initial r_addr_set = 1'b0;
267
        always @(posedge i_clk)
268 177 dgisselq
                if ((i_rst)||(i_new_pc))
269
                        r_addr_set <= 1'b1;
270
                else if (i_clear_cache)
271 2 dgisselq
                        r_addr_set <= 1'b0;
272
 
273
        // Now, read from the cache
274
        wire    w_cv;   // Cache valid, address is in the cache
275
        reg     r_cv;
276
        assign  w_cv = ((r_nvalid != 0)&&(r_addr>=r_cache_base)
277
                        &&(r_addr-r_cache_base < bus_nvalid));
278
        always @(posedge i_clk)
279 36 dgisselq
                r_cv <= (~i_new_pc)&&((w_cv)||((~i_stall_n)&&(r_cv)));
280 2 dgisselq
        assign  o_v = (r_cv)&&(~i_new_pc);
281
 
282
        always @(posedge i_clk)
283
                if (i_new_pc)
284
                        r_addr <= i_pc;
285 36 dgisselq
                else if ( ((i_stall_n)&&(w_cv)) || ((~i_stall_n)&&(w_cv)&&(r_addr == o_pc)) )
286 56 dgisselq
                        r_addr <= r_addr + {{(AW-1){1'b0}},1'b1};
287 2 dgisselq
 
288
        wire    [(LGCACHELEN-1):0]       c_rdaddr, c_cache_base;
289
        assign  c_cache_base   = r_cache_base[(LGCACHELEN-1):0];
290 56 dgisselq
        assign  c_rdaddr = r_addr[(LGCACHELEN-1):0]-c_cache_base+w_cache_offset;
291 2 dgisselq
        always @(posedge i_clk)
292 36 dgisselq
                if ((~o_v)||((i_stall_n)&&(o_v)))
293 2 dgisselq
                        o_i <= cache[c_rdaddr];
294
        always @(posedge i_clk)
295 36 dgisselq
                if ((~o_v)||((i_stall_n)&&(o_v)))
296 2 dgisselq
                        o_pc <= r_addr;
297
 
298 63 dgisselq
        reg     [(AW-1):0]       ill_address;
299
        initial ill_address = 0;
300
        always @(posedge i_clk)
301
                if ((o_wb_cyc)&&(i_wb_err))
302
                        ill_address <= o_wb_addr - {{(AW-LGCACHELEN-1){1'b0}}, r_acks_waiting};
303
 
304 177 dgisselq
        assign  o_illegal = (o_pc == ill_address)&&(~i_rst)&&(~i_new_pc)&&(~i_clear_cache);
305 63 dgisselq
 
306 2 dgisselq
endmodule

powered by: WebSVN 2.1.0

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