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

Subversion Repositories zipcpu

[/] [zipcpu/] [trunk/] [rtl/] [core/] [pfcache.v] - Blame information for rev 118

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 69 dgisselq
////////////////////////////////////////////////////////////////////////////////
2
//
3 82 dgisselq
// Filename:    pfcache.v
4 69 dgisselq
//
5
// Project:     Zip CPU -- a small, lightweight, RISC CPU soft core
6
//
7
// Purpose:     Keeping our CPU fed with instructions, at one per clock and
8
//              with no stalls.  An unusual feature of this cache is the
9
//      requirement that the entire cache may be cleared (if necessary).
10
//
11
// Creator:     Dan Gisselquist, Ph.D.
12
//              Gisselquist Technology, LLC
13
//
14
////////////////////////////////////////////////////////////////////////////////
15
//
16
// Copyright (C) 2015, Gisselquist Technology, LLC
17
//
18
// This program is free software (firmware): you can redistribute it and/or
19
// modify it under the terms of  the GNU General Public License as published
20
// by the Free Software Foundation, either version 3 of the License, or (at
21
// your option) any later version.
22
//
23
// This program is distributed in the hope that it will be useful, but WITHOUT
24
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
25
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
26
// for more details.
27
//
28
// License:     GPL, v3, as defined and found on www.gnu.org,
29
//              http://www.gnu.org/licenses/gpl.html
30
//
31
//
32
////////////////////////////////////////////////////////////////////////////////
33
//
34
module  pfcache(i_clk, i_rst, i_new_pc, i_clear_cache,
35
                        // i_early_branch, i_from_addr,
36
                        i_stall_n, i_pc, o_i, o_pc, o_v,
37
                o_wb_cyc, o_wb_stb, o_wb_we, o_wb_addr, o_wb_data,
38
                        i_wb_ack, i_wb_stall, i_wb_err, i_wb_data,
39
                        o_illegal);
40
        parameter       LGCACHELEN = 8, ADDRESS_WIDTH=24,
41
                        CACHELEN=(1<<LGCACHELEN), BUSW=32, AW=ADDRESS_WIDTH,
42
                        CW=LGCACHELEN, PW=LGCACHELEN-5;
43
        input                           i_clk, i_rst, i_new_pc;
44
        input                           i_clear_cache;
45
        input                           i_stall_n;
46
        input           [(AW-1):0]       i_pc;
47
        output  reg     [(BUSW-1):0]     o_i;
48
        output  reg     [(AW-1):0]       o_pc;
49
        output  wire                    o_v;
50
        //
51
        output  reg             o_wb_cyc, o_wb_stb;
52
        output  wire            o_wb_we;
53
        output  reg     [(AW-1):0]       o_wb_addr;
54
        output  wire    [(BUSW-1):0]     o_wb_data;
55
        //
56
        input                           i_wb_ack, i_wb_stall, i_wb_err;
57
        input           [(BUSW-1):0]     i_wb_data;
58
        //
59
        output  reg                     o_illegal;
60
 
61
        // Fixed bus outputs: we read from the bus only, never write.
62
        // Thus the output data is ... irrelevant and don't care.  We set it
63
        // to zero just to set it to something.
64
        assign  o_wb_we = 1'b0;
65
        assign  o_wb_data = 0;
66
 
67
        reg                     r_v;
68
        (* ram_style = "distributed" *)
69
        reg     [(BUSW-1):0]     cache   [0:((1<<CW)-1)];
70
        reg     [(AW-CW-1):0]    tags    [0:((1<<(CW-PW))-1)];
71
        reg     [((1<<(CW-PW))-1):0]     vmask;
72
 
73
        reg     [(AW-1):0]       lastpc;
74
        reg     [(CW-1):0]       rdaddr;
75
        reg     [(AW-1):CW]     tagval;
76 82 dgisselq
        wire    [(AW-1):PW]     lasttag;
77
        reg     [(AW-1):PW]     illegal_cache;
78 69 dgisselq
 
79
        initial o_i = 32'h76_00_00_00;  // A NOOP instruction
80
        initial o_pc = 0;
81
        always @(posedge i_clk)
82
                if (~r_v)
83
                begin
84
                        o_i <= cache[lastpc[(CW-1):0]];
85
                        o_pc <= lastpc;
86
                end else if ((i_stall_n)||(i_new_pc))
87
                begin
88
                        o_i <= cache[i_pc[(CW-1):0]];
89
                        o_pc <= i_pc;
90
                end
91
 
92
        initial tagval = 0;
93
        always @(posedge i_clk)
94 118 dgisselq
                // It may be possible to recover a clock once the cache line
95
                // has been filled, but our prior attempt to do so has lead
96
                // to a race condition, so we keep this logic simple.
97
                if (((r_v)&&(i_stall_n))||(i_clear_cache)||(i_new_pc))
98
                        lastpc <= tags[i_pc[(CW-1):PW]];
99
                else
100
                        tagval <= tags[lastpc[(CW-1):PW]];
101 69 dgisselq
 
102
        // i_pc will only increment when everything else isn't stalled, thus
103
        // we can set it without worrying about that.   Doing this enables
104
        // us to work in spite of stalls.  For example, if the next address
105
        // isn't valid, but the decoder is stalled, get the next address
106
        // anyway.
107
        initial lastpc = 0;
108
        always @(posedge i_clk)
109 71 dgisselq
                if (((r_v)&&(i_stall_n))||(i_clear_cache)||(i_new_pc))
110 69 dgisselq
                        lastpc <= i_pc;
111
 
112 82 dgisselq
        assign  lasttag = lastpc[(AW-1):PW];
113
        // initial      lasttag = 0;
114
        // always @(posedge i_clk)
115
                // if (((r_v)&&(i_stall_n))||(i_clear_cache)||(i_new_pc))
116
                        // lasttag <= i_pc[(AW-1):PW];
117 69 dgisselq
 
118
        wire    r_v_from_pc, r_v_from_last;
119
        assign  r_v_from_pc = ((i_pc[(AW-1):PW] == lasttag)
120
                                &&(tagval == i_pc[(AW-1):CW])
121
                                &&(vmask[i_pc[(CW-1):PW]]));
122 82 dgisselq
        assign  r_v_from_last = (
123
                                //(lastpc[(AW-1):PW] == lasttag)&&
124
                                (tagval == lastpc[(AW-1):CW])
125 69 dgisselq
                                &&(vmask[lastpc[(CW-1):PW]]));
126
 
127
        reg     [1:0]    delay;
128
 
129
        initial delay = 2'h3;
130
        initial r_v = 1'b0;
131
        always @(posedge i_clk)
132 71 dgisselq
                if ((i_rst)||(i_clear_cache)||(i_new_pc)||((r_v)&&(i_stall_n)))
133 69 dgisselq
                begin
134
                        r_v <= r_v_from_pc;
135
                        delay <= 2'h2;
136 71 dgisselq
                end else if (~r_v) begin // Otherwise, r_v was true and we were
137
                        r_v <= r_v_from_last;   // stalled, hence only if ~r_v
138 69 dgisselq
                        if (o_wb_cyc)
139
                                delay <= 2'h2;
140
                        else if (delay != 0)
141 88 dgisselq
                                delay <= delay + 2'b11; // i.e. delay -= 1;
142 69 dgisselq
                end
143
 
144
        assign  o_v = (r_v)&&(~i_new_pc);
145
 
146
 
147
        initial o_wb_cyc  = 1'b0;
148
        initial o_wb_stb  = 1'b0;
149
        initial o_wb_addr = {(AW){1'b0}};
150
        initial rdaddr    = 0;
151
        always @(posedge i_clk)
152
                if ((i_rst)||(i_clear_cache))
153
                begin
154
                        o_wb_cyc <= 1'b0;
155
                        o_wb_stb <= 1'b0;
156
                end else if (o_wb_cyc)
157
                begin
158
                        if ((o_wb_stb)&&(~i_wb_stall))
159
                        begin
160
                                if (o_wb_addr[(PW-1):0] == {(PW){1'b1}})
161
                                        o_wb_stb <= 1'b0;
162
                                else
163
                                        o_wb_addr[(PW-1):0] <= o_wb_addr[(PW-1):0]+1;
164
                        end
165
 
166
                        if (i_wb_ack)
167 82 dgisselq
                        begin
168 69 dgisselq
                                rdaddr <= rdaddr + 1;
169 82 dgisselq
                                if (rdaddr[(PW-1):0] == {(PW){1'b1}})
170
                                        tags[o_wb_addr[(CW-1):PW]] <= o_wb_addr[(AW-1):CW];
171
                        end
172
 
173
                        if (((i_wb_ack)&&(rdaddr[(PW-1):0]=={(PW){1'b1}}))||(i_wb_err))
174 69 dgisselq
                                o_wb_cyc <= 1'b0;
175 82 dgisselq
 
176 69 dgisselq
                        // else if (rdaddr[(PW-1):1] == {(PW-1){1'b1}})
177
                        //      tags[lastpc[(CW-1):PW]] <= lastpc[(AW-1):CW];
178
 
179
                end else if ((~r_v)&&(delay==0)
180
                        &&((tagval != lastpc[(AW-1):CW])
181 71 dgisselq
                                ||(~vmask[lastpc[(CW-1):PW]]))
182
                        &&(~o_illegal))
183 69 dgisselq
                begin
184
                        o_wb_cyc  <= 1'b1;
185
                        o_wb_stb  <= 1'b1;
186
                        o_wb_addr <= { lastpc[(AW-1):PW], {(PW){1'b0}} };
187
                        rdaddr <= { lastpc[(CW-1):PW], {(PW){1'b0}} };
188
                end
189
 
190
        // Can't initialize an array, so leave cache uninitialized
191
        always @(posedge i_clk)
192
                if ((o_wb_cyc)&&(i_wb_ack))
193
                        cache[rdaddr] <= i_wb_data;
194
 
195
        // VMask ... is a section loaded?
196
        initial vmask = 0;
197
        always @(posedge i_clk)
198
                if ((i_rst)||(i_clear_cache))
199
                        vmask <= 0;
200 118 dgisselq
                else begin
201
                        if ((o_wb_cyc)&&(i_wb_ack)&&(rdaddr[(PW-1):0] == {(PW){1'b1}}))
202
                                vmask[rdaddr[(CW-1):PW]] <= 1'b1;
203
                        if ((~r_v)&&(tagval != lastpc[(AW-1):CW])&&(delay == 0))
204
                                vmask[lastpc[(CW-1):PW]] <= 1'b0;
205
                end
206 69 dgisselq
 
207 71 dgisselq
        reg     illegal_valid;
208 69 dgisselq
        initial illegal_cache = 0;
209 71 dgisselq
        initial illegal_valid = 0;
210 69 dgisselq
        always @(posedge i_clk)
211
                if ((i_rst)||(i_clear_cache))
212 71 dgisselq
                begin
213 69 dgisselq
                        illegal_cache <= 0;
214 71 dgisselq
                        illegal_valid <= 0;
215
                end else if ((o_wb_cyc)&&(i_wb_err))
216
                begin
217 69 dgisselq
                        illegal_cache <= lastpc[(AW-1):PW];
218 71 dgisselq
                        illegal_valid <= 1'b1;
219
                end
220 69 dgisselq
 
221
        initial o_illegal = 1'b0;
222
        always @(posedge i_clk)
223 71 dgisselq
                if ((i_rst)||(i_clear_cache))
224
                        o_illegal <= 1'b0;
225
                else
226
                        o_illegal <= (illegal_valid)
227
                                &&(tagval == i_pc[(AW-1):CW])
228
                                &&(illegal_cache == i_pc[(AW-1):PW]);
229 69 dgisselq
 
230
endmodule

powered by: WebSVN 2.1.0

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