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

Subversion Repositories zipcpu

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

Go to most recent revision | 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
//              Gisselquist Tecnology, LLC
30
//
31
////////////////////////////////////////////////////////////////////////////////
32
//
33
// Copyright (C) 2015, Gisselquist Technology, LLC
34
//
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
// License:     GPL, v3, as defined and found on www.gnu.org,
46
//              http://www.gnu.org/licenses/gpl.html
47
//
48
//
49
////////////////////////////////////////////////////////////////////////////////
50
//
51 18 dgisselq
module  pipefetch(i_clk, i_rst, i_new_pc, i_clear_cache, i_stall_n, i_pc,
52 2 dgisselq
                        o_i, o_pc, o_v,
53
                o_wb_cyc, o_wb_stb, o_wb_we, o_wb_addr, o_wb_data,
54 36 dgisselq
                        i_wb_ack, i_wb_stall, i_wb_err, i_wb_data, i_wb_request,
55
                        o_illegal);
56 3 dgisselq
        parameter       RESET_ADDRESS=32'h0010_0000,
57 48 dgisselq
                        LGCACHELEN = 6, ADDRESS_WIDTH=24,
58
                        CACHELEN=(1<<LGCACHELEN), BUSW=32, AW=ADDRESS_WIDTH;
59 18 dgisselq
        input                           i_clk, i_rst, i_new_pc,
60
                                        i_clear_cache, i_stall_n;
61 48 dgisselq
        input           [(AW-1):0]       i_pc;
62 2 dgisselq
        output  reg     [(BUSW-1):0]     o_i;
63 48 dgisselq
        output  reg     [(AW-1):0]       o_pc;
64 2 dgisselq
        output  wire                    o_v;
65
        //
66
        output  reg             o_wb_cyc, o_wb_stb;
67
        output  wire            o_wb_we;
68 48 dgisselq
        output  reg     [(AW-1):0]       o_wb_addr;
69 2 dgisselq
        output  wire    [(BUSW-1):0]     o_wb_data;
70
        //
71 36 dgisselq
        input                   i_wb_ack, i_wb_stall, i_wb_err;
72 2 dgisselq
        input           [(BUSW-1):0]     i_wb_data;
73 3 dgisselq
        //
74
        // Is the (data) memory unit also requesting access to the bus?
75
        input                           i_wb_request;
76 36 dgisselq
        output  wire                    o_illegal;
77 2 dgisselq
 
78 36 dgisselq
        assign  o_illegal = 1'b0;
79
 
80 2 dgisselq
        // Fixed bus outputs: we read from the bus only, never write.
81
        // Thus the output data is ... irrelevant and don't care.  We set it
82
        // to zero just to set it to something.
83
        assign  o_wb_we = 1'b0;
84
        assign  o_wb_data = 0;
85
 
86 48 dgisselq
        reg     [(AW-1):0]               r_cache_base;
87 2 dgisselq
        reg     [(LGCACHELEN):0] r_nvalid, r_acks_waiting;
88
        reg     [(BUSW-1):0]             cache[0:(CACHELEN-1)];
89
 
90 56 dgisselq
        wire    [(LGCACHELEN-1):0]       w_cache_offset;
91
        reg     [1:0]                    r_cache_offset;
92 2 dgisselq
 
93
        reg                     r_addr_set;
94 48 dgisselq
        reg     [(AW-1):0]       r_addr;
95 2 dgisselq
 
96 48 dgisselq
        wire    [(AW-1):0]       bus_nvalid;
97
        assign  bus_nvalid = { {(AW-LGCACHELEN-1){1'b0}}, r_nvalid };
98 2 dgisselq
 
99 3 dgisselq
        // What are some of the conditions for which we need to restart the
100
        // cache?
101
        wire    w_pc_out_of_bounds;
102
        assign  w_pc_out_of_bounds = ((i_new_pc)&&((r_nvalid == 0)
103
                                        ||(i_pc < r_cache_base)
104 48 dgisselq
                                        ||(i_pc >= r_cache_base + CACHELEN)
105
                                        ||(i_pc >= r_cache_base + bus_nvalid+5)));
106 3 dgisselq
        wire    w_ran_off_end_of_cache;
107
        assign  w_ran_off_end_of_cache =((r_addr_set)&&((r_addr < r_cache_base)
108 48 dgisselq
                                        ||(r_addr >= r_cache_base + CACHELEN)
109
                                        ||(r_addr >= r_cache_base + bus_nvalid+5)));
110 3 dgisselq
        wire    w_running_out_of_cache;
111
        assign  w_running_out_of_cache = (r_addr_set)
112 56 dgisselq
                        &&(r_addr >= r_cache_base +
113
                                // {{(AW-LGCACHELEN-1),{1'b0}},2'b11,
114
                                //              {(LGCACHELEN-1){1'b0}}})
115
                                // (1<<(LGCACHELEN-2)) + (1<<(LGCACHELEN-1)))
116
                                +(3<<(LGCACHELEN-2)))
117 48 dgisselq
                        &&(|r_nvalid[(LGCACHELEN):(LGCACHELEN-1)]);
118
 
119 3 dgisselq
        initial r_cache_base = RESET_ADDRESS;
120 2 dgisselq
        always @(posedge i_clk)
121
        begin
122 18 dgisselq
                if ((i_rst)||(i_clear_cache))
123 3 dgisselq
                begin
124 2 dgisselq
                        o_wb_cyc <= 1'b0;
125 18 dgisselq
                        o_wb_stb <= 1'b0;
126 3 dgisselq
                        // r_cache_base <= RESET_ADDRESS;
127
                // end else if ((~o_wb_cyc)&&(i_new_pc)&&(r_nvalid != 0)
128
                //              &&(i_pc >= r_cache_base)
129
                //              &&(i_pc < r_cache_base + bus_nvalid))
130
                // begin
131 2 dgisselq
                        // The new instruction is in our cache, do nothing
132
                        // with the bus here.
133 3 dgisselq
                end else if ((o_wb_cyc)&&(w_pc_out_of_bounds))
134 2 dgisselq
                begin
135
                        // We need to abandon our bus action to start over in
136
                        // a new region, setting up a new cache.  This may
137
                        // happen mid cycle while waiting for a result.  By
138
                        // dropping o_wb_cyc, we state that we are no longer
139
                        // interested in that result--whatever it might be.
140
                        o_wb_cyc <= 1'b0;
141
                        o_wb_stb <= 1'b0;
142 3 dgisselq
                end else if ((~o_wb_cyc)&&(~r_nvalid[LGCACHELEN])&&(~i_wb_request)&&(r_addr_set))
143
                begin
144
                        // Restart a bus cycle that was interrupted when the
145
                        // data section wanted access to our bus.
146
                        o_wb_cyc <= 1'b1;
147
                        o_wb_stb <= 1'b1;
148
                        // o_wb_addr <= r_cache_base + bus_nvalid;
149 2 dgisselq
                end else if ((~o_wb_cyc)&&(
150 3 dgisselq
                                (w_pc_out_of_bounds)||(w_ran_off_end_of_cache)))
151 2 dgisselq
                begin
152
                        // Start a bus transaction
153
                        o_wb_cyc <= 1'b1;
154
                        o_wb_stb <= 1'b1;
155 3 dgisselq
                        // o_wb_addr <= (i_new_pc) ? i_pc : r_addr;
156
                        // r_nvalid <= 0;
157
                        // r_cache_base <= (i_new_pc) ? i_pc : r_addr;
158 56 dgisselq
                        // w_cache_offset <= 0;
159 3 dgisselq
                end else if ((~o_wb_cyc)&&(w_running_out_of_cache))
160 2 dgisselq
                begin
161
                        // If we're using the last quarter of the cache, then
162
                        // let's start a bus transaction to extend the cache.
163
                        o_wb_cyc <= 1'b1;
164
                        o_wb_stb <= 1'b1;
165 3 dgisselq
                        // o_wb_addr <= r_cache_base + (1<<(LGCACHELEN));
166
                        // r_nvalid <= r_nvalid - (1<<(LGCACHELEN-2));
167
                        // r_cache_base <= r_cache_base + (1<<(LGCACHELEN-2));
168 56 dgisselq
                        // w_cache_offset <= w_cache_offset + (1<<(LGCACHELEN-2));
169 2 dgisselq
                end else if (o_wb_cyc)
170
                begin
171
                        // This handles everything ... but the case where
172
                        // while reading we need to extend our cache.
173
                        if ((o_wb_stb)&&(~i_wb_stall))
174
                        begin
175 3 dgisselq
                                // o_wb_addr <= o_wb_addr + 1;
176
                                if ((o_wb_addr - r_cache_base >= CACHELEN-1)
177
                                        ||(i_wb_request))
178 2 dgisselq
                                        o_wb_stb <= 1'b0;
179
                        end
180
 
181
                        if (i_wb_ack)
182
                        begin
183 3 dgisselq
                                // r_nvalid <= r_nvalid + 1;
184 2 dgisselq
                                if ((r_acks_waiting == 1)&&(~o_wb_stb))
185
                                        o_wb_cyc <= 1'b0;
186 36 dgisselq
                        end else if ((r_acks_waiting == 0)&&(~o_wb_stb))
187
                                o_wb_cyc <= 1'b0;
188 2 dgisselq
                end
189
        end
190
 
191 36 dgisselq
        initial r_nvalid = 0;
192 3 dgisselq
        always @(posedge i_clk)
193 18 dgisselq
                if ((i_rst)||(i_clear_cache)) // Required, so we can reload memoy and then reset
194 11 dgisselq
                        r_nvalid <= 0;
195
                else if ((~o_wb_cyc)&&(
196 3 dgisselq
                                (w_pc_out_of_bounds)||(w_ran_off_end_of_cache)))
197
                        r_nvalid <= 0;
198
                else if ((~o_wb_cyc)&&(w_running_out_of_cache))
199 56 dgisselq
                        r_nvalid[LGCACHELEN:(LGCACHELEN-2)]
200
                                <= r_nvalid[LGCACHELEN:(LGCACHELEN-2)] +3'b111;
201
                                        // i.e.  - (1<<(LGCACHELEN-2));
202 3 dgisselq
                else if ((o_wb_cyc)&&(i_wb_ack))
203 56 dgisselq
                        r_nvalid <= r_nvalid + {{(LGCACHELEN){1'b0}},1'b1}; // +1;
204 3 dgisselq
 
205
        always @(posedge i_clk)
206 18 dgisselq
                if (i_clear_cache)
207
                        r_cache_base <= i_pc;
208
                else if ((~o_wb_cyc)&&(
209
                                (w_pc_out_of_bounds)
210
                                ||(w_ran_off_end_of_cache)))
211 3 dgisselq
                        r_cache_base <= (i_new_pc) ? i_pc : r_addr;
212
                else if ((~o_wb_cyc)&&(w_running_out_of_cache))
213 56 dgisselq
                        r_cache_base[(AW-1):(LGCACHELEN-2)]
214
                                <= r_cache_base[(AW-1):(LGCACHELEN-2)]
215
                                        + {{(AW-LGCACHELEN+1){1'b0}},1'b1};
216
                                        // i.e.  + (1<<(LGCACHELEN-2));
217 3 dgisselq
 
218
        always @(posedge i_clk)
219 18 dgisselq
                if (i_clear_cache)
220 3 dgisselq
                        r_cache_offset <= 0;
221 18 dgisselq
                else if ((~o_wb_cyc)&&(
222
                                (w_pc_out_of_bounds)
223
                                ||(w_ran_off_end_of_cache)))
224
                        r_cache_offset <= 0;
225 3 dgisselq
                else if ((~o_wb_cyc)&&(w_running_out_of_cache))
226 56 dgisselq
                        r_cache_offset[1:0] <= r_cache_offset[1:0] + 2'b01;
227
        assign  w_cache_offset = { r_cache_offset, {(LGCACHELEN-2){1'b0}} };
228 3 dgisselq
 
229
        always @(posedge i_clk)
230 18 dgisselq
                if (i_clear_cache)
231
                        o_wb_addr <= i_pc;
232 38 dgisselq
                else if ((o_wb_cyc)&&(w_pc_out_of_bounds))
233
                begin
234
                        if (i_wb_ack)
235
                                o_wb_addr <= r_cache_base + bus_nvalid+1;
236
                        else
237
                                o_wb_addr <= r_cache_base + bus_nvalid;
238
                end else if ((~o_wb_cyc)&&((w_pc_out_of_bounds)
239 3 dgisselq
                                        ||(w_ran_off_end_of_cache)))
240
                        o_wb_addr <= (i_new_pc) ? i_pc : r_addr;
241
                else if ((o_wb_cyc)&&(o_wb_stb)&&(~i_wb_stall))
242
                        o_wb_addr <= o_wb_addr + 1;
243
 
244
        initial r_acks_waiting = 0;
245
        always @(posedge i_clk)
246
                if (~o_wb_cyc)
247
                        r_acks_waiting <= 0;
248 38 dgisselq
                else if ((o_wb_cyc)&&(o_wb_stb)&&(~i_wb_stall)&&(~i_wb_ack))
249 56 dgisselq
                        r_acks_waiting <= r_acks_waiting + {{(LGCACHELEN){1'b0}},1'b1};
250 38 dgisselq
                else if ((o_wb_cyc)&&(i_wb_ack)&&((~o_wb_stb)||(i_wb_stall)))
251 56 dgisselq
                        r_acks_waiting <= r_acks_waiting + {(LGCACHELEN+1){1'b1}}; // - 1;
252 3 dgisselq
 
253
        always @(posedge i_clk)
254
                if ((o_wb_cyc)&&(i_wb_ack))
255 56 dgisselq
                        cache[r_nvalid[(LGCACHELEN-1):0]+w_cache_offset]
256 3 dgisselq
                                        <= i_wb_data;
257
 
258 2 dgisselq
        initial r_addr_set = 1'b0;
259
        always @(posedge i_clk)
260 18 dgisselq
                if ((i_rst)||(i_clear_cache))
261 2 dgisselq
                        r_addr_set <= 1'b0;
262
                else if (i_new_pc)
263
                        r_addr_set <= 1'b1;
264
 
265
        // Now, read from the cache
266
        wire    w_cv;   // Cache valid, address is in the cache
267
        reg     r_cv;
268
        assign  w_cv = ((r_nvalid != 0)&&(r_addr>=r_cache_base)
269
                        &&(r_addr-r_cache_base < bus_nvalid));
270
        always @(posedge i_clk)
271 36 dgisselq
                r_cv <= (~i_new_pc)&&((w_cv)||((~i_stall_n)&&(r_cv)));
272 2 dgisselq
        assign  o_v = (r_cv)&&(~i_new_pc);
273
 
274
        always @(posedge i_clk)
275
                if (i_new_pc)
276
                        r_addr <= i_pc;
277 36 dgisselq
                else if ( ((i_stall_n)&&(w_cv)) || ((~i_stall_n)&&(w_cv)&&(r_addr == o_pc)) )
278 56 dgisselq
                        r_addr <= r_addr + {{(AW-1){1'b0}},1'b1};
279 2 dgisselq
 
280
        wire    [(LGCACHELEN-1):0]       c_rdaddr, c_cache_base;
281
        assign  c_cache_base   = r_cache_base[(LGCACHELEN-1):0];
282 56 dgisselq
        assign  c_rdaddr = r_addr[(LGCACHELEN-1):0]-c_cache_base+w_cache_offset;
283 2 dgisselq
        always @(posedge i_clk)
284 36 dgisselq
                if ((~o_v)||((i_stall_n)&&(o_v)))
285 2 dgisselq
                        o_i <= cache[c_rdaddr];
286
        always @(posedge i_clk)
287 36 dgisselq
                if ((~o_v)||((i_stall_n)&&(o_v)))
288 2 dgisselq
                        o_pc <= r_addr;
289
 
290
endmodule

powered by: WebSVN 2.1.0

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