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

Subversion Repositories xulalx25soc

[/] [xulalx25soc/] [trunk/] [rtl/] [cpu/] [pfcache.v] - Blame information for rev 95

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

Line No. Rev Author Line
1 21 dgisselq
////////////////////////////////////////////////////////////////////////////////
2
//
3
// Filename:    pfcache.v
4
//
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
        wire    [(AW-1):PW]     lasttag;
77 51 dgisselq
        reg                     illegal_valid;
78 21 dgisselq
        reg     [(AW-1):PW]     illegal_cache;
79
 
80
        initial o_i = 32'h76_00_00_00;  // A NOOP instruction
81
        initial o_pc = 0;
82
        always @(posedge i_clk)
83
                if (~r_v)
84
                begin
85
                        o_i <= cache[lastpc[(CW-1):0]];
86
                        o_pc <= lastpc;
87
                end else if ((i_stall_n)||(i_new_pc))
88
                begin
89
                        o_i <= cache[i_pc[(CW-1):0]];
90
                        o_pc <= i_pc;
91
                end
92
 
93
        initial tagval = 0;
94
        always @(posedge i_clk)
95 50 dgisselq
                // It may be possible to recover a clock once the cache line
96
                // has been filled, but our prior attempt to do so has lead
97
                // to a race condition, so we keep this logic simple.
98
                if (((r_v)&&(i_stall_n))||(i_clear_cache)||(i_new_pc))
99 51 dgisselq
                        tagval <= tags[i_pc[(CW-1):PW]];
100 50 dgisselq
                else
101
                        tagval <= tags[lastpc[(CW-1):PW]];
102 21 dgisselq
 
103
        // i_pc will only increment when everything else isn't stalled, thus
104
        // we can set it without worrying about that.   Doing this enables
105
        // us to work in spite of stalls.  For example, if the next address
106
        // isn't valid, but the decoder is stalled, get the next address
107
        // anyway.
108
        initial lastpc = 0;
109
        always @(posedge i_clk)
110
                if (((r_v)&&(i_stall_n))||(i_clear_cache)||(i_new_pc))
111
                        lastpc <= i_pc;
112
 
113
        assign  lasttag = lastpc[(AW-1):PW];
114
        // initial      lasttag = 0;
115
        // always @(posedge i_clk)
116
                // if (((r_v)&&(i_stall_n))||(i_clear_cache)||(i_new_pc))
117
                        // lasttag <= i_pc[(AW-1):PW];
118
 
119
        wire    r_v_from_pc, r_v_from_last;
120
        assign  r_v_from_pc = ((i_pc[(AW-1):PW] == lasttag)
121
                                &&(tagval == i_pc[(AW-1):CW])
122
                                &&(vmask[i_pc[(CW-1):PW]]));
123
        assign  r_v_from_last = (
124
                                //(lastpc[(AW-1):PW] == lasttag)&&
125
                                (tagval == lastpc[(AW-1):CW])
126
                                &&(vmask[lastpc[(CW-1):PW]]));
127
 
128
        reg     [1:0]    delay;
129
 
130
        initial delay = 2'h3;
131
        initial r_v = 1'b0;
132
        always @(posedge i_clk)
133
                if ((i_rst)||(i_clear_cache)||(i_new_pc)||((r_v)&&(i_stall_n)))
134
                begin
135
                        r_v <= r_v_from_pc;
136
                        delay <= 2'h2;
137
                end else if (~r_v) begin // Otherwise, r_v was true and we were
138
                        r_v <= r_v_from_last;   // stalled, hence only if ~r_v
139
                        if (o_wb_cyc)
140
                                delay <= 2'h2;
141
                        else if (delay != 0)
142
                                delay <= delay + 2'b11; // i.e. delay -= 1;
143
                end
144
 
145
        assign  o_v = (r_v)&&(~i_new_pc);
146
 
147
 
148
        initial o_wb_cyc  = 1'b0;
149
        initial o_wb_stb  = 1'b0;
150
        initial o_wb_addr = {(AW){1'b0}};
151
        initial rdaddr    = 0;
152
        always @(posedge i_clk)
153
                if ((i_rst)||(i_clear_cache))
154
                begin
155
                        o_wb_cyc <= 1'b0;
156
                        o_wb_stb <= 1'b0;
157
                end else if (o_wb_cyc)
158
                begin
159 51 dgisselq
                        if (i_wb_err)
160
                                o_wb_stb <= 1'b0;
161
                        else if ((o_wb_stb)&&(~i_wb_stall))
162 21 dgisselq
                        begin
163
                                if (o_wb_addr[(PW-1):0] == {(PW){1'b1}})
164
                                        o_wb_stb <= 1'b0;
165
                                else
166
                                        o_wb_addr[(PW-1):0] <= o_wb_addr[(PW-1):0]+1;
167
                        end
168
 
169
                        if (i_wb_ack)
170
                        begin
171
                                rdaddr <= rdaddr + 1;
172 51 dgisselq
                                tags[o_wb_addr[(CW-1):PW]] <= o_wb_addr[(AW-1):CW];
173 21 dgisselq
                        end
174
 
175
                        if (((i_wb_ack)&&(rdaddr[(PW-1):0]=={(PW){1'b1}}))||(i_wb_err))
176
                                o_wb_cyc <= 1'b0;
177
 
178
                        // else if (rdaddr[(PW-1):1] == {(PW-1){1'b1}})
179
                        //      tags[lastpc[(CW-1):PW]] <= lastpc[(AW-1):CW];
180
 
181
                end else if ((~r_v)&&(delay==0)
182
                        &&((tagval != lastpc[(AW-1):CW])
183
                                ||(~vmask[lastpc[(CW-1):PW]]))
184 51 dgisselq
                        &&((~illegal_valid)||(lastpc[(AW-1):PW] != illegal_cache)))
185 21 dgisselq
                begin
186
                        o_wb_cyc  <= 1'b1;
187
                        o_wb_stb  <= 1'b1;
188
                        o_wb_addr <= { lastpc[(AW-1):PW], {(PW){1'b0}} };
189
                        rdaddr <= { lastpc[(CW-1):PW], {(PW){1'b0}} };
190
                end
191
 
192
        // Can't initialize an array, so leave cache uninitialized
193
        always @(posedge i_clk)
194
                if ((o_wb_cyc)&&(i_wb_ack))
195
                        cache[rdaddr] <= i_wb_data;
196
 
197
        // VMask ... is a section loaded?
198
        initial vmask = 0;
199
        always @(posedge i_clk)
200
                if ((i_rst)||(i_clear_cache))
201
                        vmask <= 0;
202 50 dgisselq
                else begin
203
                        if ((o_wb_cyc)&&(i_wb_ack)&&(rdaddr[(PW-1):0] == {(PW){1'b1}}))
204
                                vmask[rdaddr[(CW-1):PW]] <= 1'b1;
205
                        if ((~r_v)&&(tagval != lastpc[(AW-1):CW])&&(delay == 0))
206
                                vmask[lastpc[(CW-1):PW]] <= 1'b0;
207
                end
208 21 dgisselq
 
209
        initial illegal_cache = 0;
210
        initial illegal_valid = 0;
211
        always @(posedge i_clk)
212
                if ((i_rst)||(i_clear_cache))
213
                begin
214
                        illegal_cache <= 0;
215
                        illegal_valid <= 0;
216
                end else if ((o_wb_cyc)&&(i_wb_err))
217
                begin
218 51 dgisselq
                        illegal_cache <= o_wb_addr[(AW-1):PW];
219 21 dgisselq
                        illegal_valid <= 1'b1;
220
                end
221
 
222
        initial o_illegal = 1'b0;
223
        always @(posedge i_clk)
224
                if ((i_rst)||(i_clear_cache))
225
                        o_illegal <= 1'b0;
226
                else
227
                        o_illegal <= (illegal_valid)
228
                                &&(illegal_cache == i_pc[(AW-1):PW]);
229
 
230
endmodule

powered by: WebSVN 2.1.0

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