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

Subversion Repositories zipcpu

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

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

Line No. Rev Author Line
1 2 dgisselq
///////////////////////////////////////////////////////////////////////////////
2
//
3
// Filename:    zipcpu.v
4
//
5
// Project:     Zip CPU -- a small, lightweight, RISC CPU soft core
6
//
7
// Purpose:     This is the top level module holding the core of the Zip CPU
8
//              together.  The Zip CPU is designed to be as simple as possible.
9 56 dgisselq
//      (actual implementation aside ...)  The instruction set is about as
10
//      RISC as you can get, there are only 16 instruction types supported.
11
//      Please see the accompanying spec.pdf file for a description of these
12
//      instructions.
13 2 dgisselq
//
14 56 dgisselq
//      All instructions are 32-bits wide.  All bus accesses, both address and
15
//      data, are 32-bits over a wishbone bus.
16 2 dgisselq
//
17
//      The Zip CPU is fully pipelined with the following pipeline stages:
18
//
19 56 dgisselq
//              1. Prefetch, returns the instruction from memory. 
20 2 dgisselq
//
21
//              2. Instruction Decode
22
//
23
//              3. Read Operands
24
//
25
//              4. Apply Instruction
26
//
27
//              4. Write-back Results
28
//
29 56 dgisselq
//      Further information about the inner workings of this CPU may be
30
//      found in the spec.pdf file.  (The documentation within this file
31
//      had become out of date and out of sync with the spec.pdf, so look
32
//      to the spec.pdf for accurate and up to date information.)
33 2 dgisselq
//
34
//
35 69 dgisselq
//      In general, the pipelining is controlled by three pieces of logic
36
//      per stage: _ce, _stall, and _valid.  _valid means that the stage
37
//      holds a valid instruction.  _ce means that the instruction from the
38
//      previous stage is to move into this one, and _stall means that the
39
//      instruction from the previous stage may not move into this one.
40
//      The difference between these control signals allows individual stages
41
//      to propagate instructions independently.  In general, the logic works
42
//      as:
43
//
44
//
45
//      assign  (n)_ce = (n-1)_valid && (~(n)_stall)
46
//
47
//
48
//      always @(posedge i_clk)
49
//              if ((i_rst)||(clear_pipeline))
50
//                      (n)_valid = 0
51
//              else if (n)_ce
52
//                      (n)_valid = 1
53
//              else if (n+1)_ce
54
//                      (n)_valid = 0
55
//
56
//      assign (n)_stall = (  (n-1)_valid && ( pipeline hazard detection )  )
57
//                      || (  (n)_valid && (n+1)_stall );
58
//
59
//      and ...
60
//
61
//      always @(posedge i_clk)
62
//              if (n)_ce
63
//                      (n)_variable = ... whatever logic for this stage
64
//
65
//      Note that a stage can stall even if no instruction is loaded into
66
//      it.
67
//
68
//
69 2 dgisselq
// Creator:     Dan Gisselquist, Ph.D.
70 69 dgisselq
//              Gisselquist Technology, LLC
71 2 dgisselq
//
72
///////////////////////////////////////////////////////////////////////////////
73
//
74
// Copyright (C) 2015, Gisselquist Technology, LLC
75
//
76
// This program is free software (firmware): you can redistribute it and/or
77
// modify it under the terms of  the GNU General Public License as published
78
// by the Free Software Foundation, either version 3 of the License, or (at
79
// your option) any later version.
80
//
81
// This program is distributed in the hope that it will be useful, but WITHOUT
82
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
83
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
84
// for more details.
85
//
86
// License:     GPL, v3, as defined and found on www.gnu.org,
87
//              http://www.gnu.org/licenses/gpl.html
88
//
89
//
90
///////////////////////////////////////////////////////////////////////////////
91
//
92 36 dgisselq
// We can either pipeline our fetches, or issue one fetch at a time.  Pipelined
93
// fetches are more complicated and therefore use more FPGA resources, while
94
// single fetches will cause the CPU to stall for about 5 stalls each 
95
// instruction cycle, effectively reducing the instruction count per clock to
96
// about 0.2.  However, the area cost may be worth it.  Consider:
97
//
98
//      Slice LUTs              ZipSystem       ZipCPU
99
//      Single Fetching         2521            1734
100
//      Pipelined fetching      2796            2046
101
//
102
//
103
//
104 25 dgisselq
`define CPU_CC_REG      4'he
105 2 dgisselq
`define CPU_PC_REG      4'hf
106 69 dgisselq
`define CPU_FPUERR_BIT  12      // Floating point error flag, set on error
107
`define CPU_DIVERR_BIT  11      // Divide error flag, set on divide by zero
108
`define CPU_BUSERR_BIT  10      // Bus error flag, set on error
109
`define CPU_TRAP_BIT    9       // User TRAP has taken place
110
`define CPU_ILL_BIT     8       // Illegal instruction
111 2 dgisselq
`define CPU_BREAK_BIT   7
112 69 dgisselq
`define CPU_STEP_BIT    6       // Will step one or two (VLIW) instructions
113 2 dgisselq
`define CPU_GIE_BIT     5
114
`define CPU_SLEEP_BIT   4
115 36 dgisselq
// Compile time defines
116 56 dgisselq
//
117
`include "cpudefs.v"
118
//
119 65 dgisselq
//
120 2 dgisselq
module  zipcpu(i_clk, i_rst, i_interrupt,
121
                // Debug interface
122 18 dgisselq
                i_halt, i_clear_pf_cache, i_dbg_reg, i_dbg_we, i_dbg_data,
123
                        o_dbg_stall, o_dbg_reg, o_dbg_cc,
124 2 dgisselq
                        o_break,
125
                // CPU interface to the wishbone bus
126 36 dgisselq
                o_wb_gbl_cyc, o_wb_gbl_stb,
127
                        o_wb_lcl_cyc, o_wb_lcl_stb,
128
                        o_wb_we, o_wb_addr, o_wb_data,
129 2 dgisselq
                        i_wb_ack, i_wb_stall, i_wb_data,
130 36 dgisselq
                        i_wb_err,
131 2 dgisselq
                // Accounting/CPU usage interface
132 65 dgisselq
                o_op_stall, o_pf_stall, o_i_count
133
`ifdef  DEBUG_SCOPE
134
                , o_debug
135
`endif
136
                );
137 48 dgisselq
        parameter       RESET_ADDRESS=32'h0100000, ADDRESS_WIDTH=24,
138 69 dgisselq
                        LGICACHE=6;
139 56 dgisselq
`ifdef  OPT_MULTIPLY
140
        parameter       IMPLEMENT_MPY = 1;
141
`else
142
        parameter       IMPLEMENT_MPY = 0;
143
`endif
144 71 dgisselq
`ifdef  OPT_DIVIDE
145
        parameter       IMPLEMENT_DIVIDE = 1;
146
`else
147
        parameter       IMPLEMENT_DIVIDE = 0;
148
`endif
149
`ifdef  OPT_IMPLEMENT_FPU
150
        parameter       IMPLEMENT_FPU = 1,
151
`else
152
        parameter       IMPLEMENT_FPU = 0,
153
`endif
154 69 dgisselq
                        IMPLEMENT_LOCK=1;
155
`ifdef  OPT_EARLY_BRANCHING
156
        parameter       EARLY_BRANCHING = 1;
157
`else
158
        parameter       EARLY_BRANCHING = 0;
159
`endif
160
        parameter       AW=ADDRESS_WIDTH;
161 2 dgisselq
        input                   i_clk, i_rst, i_interrupt;
162
        // Debug interface -- inputs
163 18 dgisselq
        input                   i_halt, i_clear_pf_cache;
164 2 dgisselq
        input           [4:0]    i_dbg_reg;
165
        input                   i_dbg_we;
166
        input           [31:0]   i_dbg_data;
167
        // Debug interface -- outputs
168
        output  reg             o_dbg_stall;
169
        output  reg     [31:0]   o_dbg_reg;
170 56 dgisselq
        output  reg     [3:0]    o_dbg_cc;
171 2 dgisselq
        output  wire            o_break;
172
        // Wishbone interface -- outputs
173 36 dgisselq
        output  wire            o_wb_gbl_cyc, o_wb_gbl_stb;
174
        output  wire            o_wb_lcl_cyc, o_wb_lcl_stb, o_wb_we;
175 48 dgisselq
        output  wire    [(AW-1):0]       o_wb_addr;
176
        output  wire    [31:0]   o_wb_data;
177 2 dgisselq
        // Wishbone interface -- inputs
178
        input                   i_wb_ack, i_wb_stall;
179
        input           [31:0]   i_wb_data;
180 36 dgisselq
        input                   i_wb_err;
181 2 dgisselq
        // Accounting outputs ... to help us count stalls and usage
182 9 dgisselq
        output  wire            o_op_stall;
183 2 dgisselq
        output  wire            o_pf_stall;
184 9 dgisselq
        output  wire            o_i_count;
185 56 dgisselq
        //
186 65 dgisselq
`ifdef  DEBUG_SCOPE
187 56 dgisselq
        output  reg     [31:0]   o_debug;
188 65 dgisselq
`endif
189 2 dgisselq
 
190 25 dgisselq
 
191 2 dgisselq
        // Registers
192 56 dgisselq
        //
193
        //      The distributed RAM style comment is necessary on the
194
        // SPARTAN6 with XST to prevent XST from oversimplifying the register
195
        // set and in the process ruining everything else.  It basically
196
        // optimizes logic away, to where it no longer works.  The logic
197
        // as described herein will work, this just makes sure XST implements
198
        // that logic.
199
        //
200
        (* ram_style = "distributed" *)
201 2 dgisselq
        reg     [31:0]   regset [0:31];
202 9 dgisselq
 
203
        // Condition codes
204 56 dgisselq
        // (BUS, TRAP,ILL,BREAKEN,STEP,GIE,SLEEP ), V, N, C, Z
205
        reg     [3:0]    flags, iflags;
206 69 dgisselq
        wire    [12:0]   w_uflags, w_iflags;
207 25 dgisselq
        reg             trap, break_en, step, gie, sleep;
208 38 dgisselq
`ifdef  OPT_ILLEGAL_INSTRUCTION
209 65 dgisselq
        reg             ill_err_u, ill_err_i;
210 38 dgisselq
`else
211 65 dgisselq
        wire            ill_err_u, ill_err_i;
212 36 dgisselq
`endif
213 65 dgisselq
        reg             ibus_err_flag, ubus_err_flag;
214 69 dgisselq
        wire            idiv_err_flag, udiv_err_flag;
215
        wire            ifpu_err_flag, ufpu_err_flag;
216
        wire            ihalt_phase, uhalt_phase;
217 2 dgisselq
 
218 9 dgisselq
        // The master chip enable
219
        wire            master_ce;
220 2 dgisselq
 
221
        //
222
        //
223
        //      PIPELINE STAGE #1 :: Prefetch
224
        //              Variable declarations
225
        //
226 48 dgisselq
        reg     [(AW-1):0]       pf_pc;
227 69 dgisselq
        reg     new_pc;
228 18 dgisselq
        wire    clear_pipeline;
229 69 dgisselq
        assign  clear_pipeline = new_pc || i_clear_pf_cache;
230 9 dgisselq
 
231
        wire            dcd_stalled;
232 36 dgisselq
        wire            pf_cyc, pf_stb, pf_we, pf_busy, pf_ack, pf_stall, pf_err;
233 48 dgisselq
        wire    [(AW-1):0]       pf_addr;
234
        wire    [31:0]           pf_data;
235
        wire    [31:0]           instruction;
236
        wire    [(AW-1):0]       instruction_pc;
237 36 dgisselq
        wire    pf_valid, instruction_gie, pf_illegal;
238 2 dgisselq
 
239
        //
240
        //
241
        //      PIPELINE STAGE #2 :: Instruction Decode
242
        //              Variable declarations
243
        //
244
        //
245 25 dgisselq
        reg             opvalid, opvalid_mem, opvalid_alu, op_wr_pc;
246 69 dgisselq
        reg             opvalid_div, opvalid_fpu;
247
        wire            op_stall, dcd_ce, dcd_phase;
248
        wire    [3:0]    dcdOp;
249
        wire    [4:0]    dcdA, dcdB, dcdR;
250
        wire            dcdA_cc, dcdB_cc, dcdA_pc, dcdB_pc, dcdR_cc, dcdR_pc;
251
        wire    [3:0]    dcdF;
252
        wire            dcdR_wr, dcdA_rd, dcdB_rd,
253
                                dcdALU, dcdM, dcdDV, dcdFP,
254 71 dgisselq
                                dcdF_wr, dcd_gie, dcd_break, dcd_lock,
255
                                dcd_pipe;
256 69 dgisselq
        reg             r_dcdvalid;
257
        wire            dcdvalid;
258
        wire    [(AW-1):0]       dcd_pc;
259
        wire    [31:0]   dcdI;
260
        wire            dcd_zI; // true if dcdI == 0
261 2 dgisselq
        wire    dcdA_stall, dcdB_stall, dcdF_stall;
262
 
263 69 dgisselq
        wire    dcd_illegal;
264
        wire                    dcd_early_branch;
265 48 dgisselq
        wire    [(AW-1):0]       dcd_branch_pc;
266 2 dgisselq
 
267
 
268
        //
269
        //
270
        //      PIPELINE STAGE #3 :: Read Operands
271
        //              Variable declarations
272
        //
273
        //
274
        //
275
        // Now, let's read our operands
276
        reg     [4:0]    alu_reg;
277
        reg     [3:0]    opn;
278
        reg     [4:0]    opR;
279 48 dgisselq
        reg     [31:0]   r_opA, r_opB;
280
        reg     [(AW-1):0]       op_pc;
281 25 dgisselq
        wire    [31:0]   w_opA, w_opB;
282 2 dgisselq
        wire    [31:0]   opA_nowait, opB_nowait, opA, opB;
283 56 dgisselq
        reg             opR_wr, opR_cc, opF_wr, op_gie;
284 69 dgisselq
        wire    [12:0]   opFl;
285 56 dgisselq
        reg     [5:0]    r_opF;
286
        wire    [7:0]    opF;
287
        reg     [2:0]    opF_cp;
288 69 dgisselq
        wire            op_ce, op_phase;
289 56 dgisselq
        // Some pipeline control wires
290 69 dgisselq
`ifdef  OPT_PIPELINED
291 56 dgisselq
        reg     opA_alu, opA_mem;
292
        reg     opB_alu, opB_mem;
293
`endif
294 38 dgisselq
`ifdef  OPT_ILLEGAL_INSTRUCTION
295 36 dgisselq
        reg     op_illegal;
296
`endif
297 69 dgisselq
        reg     op_break;
298
        wire    op_lock;
299 2 dgisselq
 
300
 
301
        //
302
        //
303
        //      PIPELINE STAGE #4 :: ALU / Memory
304
        //              Variable declarations
305
        //
306
        //
307 48 dgisselq
        reg     [(AW-1):0]       alu_pc;
308 69 dgisselq
        reg             alu_pc_valid;
309
        wire            alu_phase;
310 2 dgisselq
        wire            alu_ce, alu_stall;
311
        wire    [31:0]   alu_result;
312
        wire    [3:0]    alu_flags;
313 71 dgisselq
        wire            alu_valid, alu_busy;
314 2 dgisselq
        wire            set_cond;
315
        reg             alu_wr, alF_wr, alu_gie;
316 56 dgisselq
        wire            alu_illegal_op;
317 38 dgisselq
        wire            alu_illegal;
318 2 dgisselq
 
319
 
320
 
321
        wire    mem_ce, mem_stalled;
322 38 dgisselq
`ifdef  OPT_PIPELINED_BUS_ACCESS
323
        wire    mem_pipe_stalled;
324
`endif
325 36 dgisselq
        wire    mem_valid, mem_ack, mem_stall, mem_err, bus_err,
326
                mem_cyc_gbl, mem_cyc_lcl, mem_stb_gbl, mem_stb_lcl, mem_we;
327 48 dgisselq
        wire    [4:0]            mem_wreg;
328 9 dgisselq
 
329 48 dgisselq
        wire                    mem_busy, mem_rdbusy;
330
        wire    [(AW-1):0]       mem_addr;
331
        wire    [31:0]           mem_data, mem_result;
332 2 dgisselq
 
333 69 dgisselq
        wire    div_ce, div_error, div_busy, div_valid;
334
        wire    [31:0]   div_result;
335
        wire    [3:0]    div_flags;
336 2 dgisselq
 
337 69 dgisselq
        assign  div_ce = (master_ce)&&(~clear_pipeline)&&(opvalid_div)
338
                                &&(~mem_rdbusy)&&(~div_busy)&&(~fpu_busy)
339
                                &&(set_cond);
340 2 dgisselq
 
341 69 dgisselq
        wire    fpu_ce, fpu_error, fpu_busy, fpu_valid;
342
        wire    [31:0]   fpu_result;
343
        wire    [3:0]    fpu_flags;
344
 
345
        assign  fpu_ce = (master_ce)&&(~clear_pipeline)&&(opvalid_fpu)
346
                                &&(~mem_rdbusy)&&(~div_busy)&&(~fpu_busy)
347
                                &&(set_cond);
348
 
349
 
350 2 dgisselq
        //
351
        //
352
        //      PIPELINE STAGE #5 :: Write-back
353
        //              Variable declarations
354
        //
355 25 dgisselq
        wire            wr_reg_ce, wr_flags_ce, wr_write_pc, wr_write_cc;
356 2 dgisselq
        wire    [4:0]    wr_reg_id;
357
        wire    [31:0]   wr_reg_vl;
358
        wire    w_switch_to_interrupt, w_release_from_interrupt;
359 48 dgisselq
        reg     [(AW-1):0]       upc, ipc;
360 2 dgisselq
 
361
 
362
 
363
        //
364
        //      MASTER: clock enable.
365
        //
366 38 dgisselq
        assign  master_ce = (~i_halt)&&(~o_break)&&(~sleep);
367 2 dgisselq
 
368
 
369
        //
370
        //      PIPELINE STAGE #1 :: Prefetch
371
        //              Calculate stall conditions
372 65 dgisselq
        //
373
        //      These are calculated externally, within the prefetch module.
374
        //
375 2 dgisselq
 
376
        //
377
        //      PIPELINE STAGE #2 :: Instruction Decode
378
        //              Calculate stall conditions
379 69 dgisselq
`ifdef  OPT_PIPELINED
380
        assign          dcd_ce = ((~dcdvalid)||(~dcd_stalled))&&(~clear_pipeline);
381
`else
382
        assign          dcd_ce = 1'b1;
383
`endif
384
`ifdef  OPT_PIPELINED
385
        assign          dcd_stalled = (dcdvalid)&&(op_stall);
386
`else
387
        // If not pipelined, there will be no opvalid_ anything, and the
388
        // op_stall will be false, dcdX_stall will be false, thus we can simply
389
        // do a ...
390
        assign          dcd_stalled = 1'b0;
391
`endif
392 2 dgisselq
        //
393
        //      PIPELINE STAGE #3 :: Read Operands
394
        //              Calculate stall conditions
395 69 dgisselq
        wire    op_lock_stall;
396
`ifdef  OPT_PIPELINED
397
        assign  op_stall = (opvalid)&&( // Only stall if we're loaded w/validins
398
                        // Stall if we're stopped, and not allowed to execute
399
                        // an instruction
400
                        // (~master_ce)         // Already captured in alu_stall
401
                        //
402 56 dgisselq
                        // Stall if going into the ALU and the ALU is stalled
403
                        //      i.e. if the memory is busy, or we are single
404 69 dgisselq
                        //      stepping.  This also includes our stalls for
405
                        //      op_break and op_lock, so we don't need to
406
                        //      include those as well here.
407
                        ((opvalid)&&(alu_stall))
408
                        // Stall if the divide is busy, since we can't have
409
                        // two parallel stages writing back at the same time
410
                        ||(div_busy)
411
                        // Same for the floating point unit
412
                        ||(fpu_busy)
413 56 dgisselq
                        //
414
                        // ||((opvalid_alu)&&(mem_rdbusy)) // part of alu_stall
415
                        // Stall if we are going into memory with an operation
416
                        //      that cannot be pipelined, and the memory is
417
                        //      already busy
418 69 dgisselq
                        ||((opvalid_mem)&&(mem_stalled))
419 71 dgisselq
                        // ||((opvalid_mem)&&(dcdvalid)&&(dcdM)&&(~dcd_pipe))
420 69 dgisselq
                        )
421
                        ||(dcdvalid)&&(
422 71 dgisselq
                                // Stall if we need to wait for an operand A
423 69 dgisselq
                                // to be ready to read
424 71 dgisselq
                                (dcdA_stall)
425 69 dgisselq
                                // Likewise for B, also includes logic
426
                                // regarding immediate offset (register must
427
                                // be in register file if we need to add to
428
                                // an immediate)
429
                                ||(dcdB_stall)
430
                                // Or if we need to wait on flags to work on the
431
                                // CC register
432
                                ||(dcdF_stall)
433
                        );
434 71 dgisselq
        assign  op_ce = ((dcdvalid)||(dcd_illegal))&&(~op_stall)&&(~clear_pipeline);
435 65 dgisselq
`else
436 69 dgisselq
        assign  op_stall = (opvalid)&&(~master_ce);
437 71 dgisselq
        assign  op_ce = ((dcdvalid)||(dcd_illegal));
438 65 dgisselq
`endif
439 2 dgisselq
 
440
        //
441
        //      PIPELINE STAGE #4 :: ALU / Memory
442
        //              Calculate stall conditions
443 36 dgisselq
        //
444
        // 1. Basic stall is if the previous stage is valid and the next is
445
        //      busy.  
446
        // 2. Also stall if the prior stage is valid and the master clock enable
447
        //      is de-selected
448 56 dgisselq
        // 3. Stall if someone on the other end is writing the CC register,
449
        //      since we don't know if it'll put us to sleep or not.
450 36 dgisselq
        // 4. Last case: Stall if we would otherwise move a break instruction
451
        //      through the ALU.  Break instructions are not allowed through
452
        //      the ALU.
453 69 dgisselq
`ifdef  OPT_PIPELINED
454 71 dgisselq
        assign  alu_stall = (((~master_ce)||(mem_rdbusy)||(alu_busy))&&(opvalid_alu)) //Case 1&2
455 56 dgisselq
                        // Old case #3--this isn't an ALU stall though ...
456
                        ||((opvalid_alu)&&(wr_reg_ce)&&(wr_reg_id[4] == op_gie)
457
                                &&(wr_write_cc)) // Case 3
458 69 dgisselq
                        ||((opvalid)&&(op_lock)&&(op_lock_stall))
459
                        ||((opvalid)&&(op_break))
460
                        ||(div_busy)||(fpu_busy);
461 71 dgisselq
        assign  alu_ce = (master_ce)&&((opvalid_alu)||(op_illegal))
462 69 dgisselq
                                &&(~alu_stall)
463
                                &&(~clear_pipeline);
464
`else
465
        assign  alu_stall = ((~master_ce)&&(opvalid_alu))
466
                                ||((opvalid_alu)&&(op_break));
467 71 dgisselq
        assign  alu_ce = (master_ce)&&((opvalid_alu)||(op_illegal))&&(~alu_stall);
468 69 dgisselq
`endif
469 2 dgisselq
        //
470 65 dgisselq
 
471
        //
472
        // Note: if you change the conditions for mem_ce, you must also change
473
        // alu_pc_valid.
474
        //
475 69 dgisselq
`ifdef  OPT_PIPELINED
476
        assign  mem_ce = (master_ce)&&(opvalid_mem)&&(~mem_stalled)
477 71 dgisselq
                        &&(~clear_pipeline);
478 69 dgisselq
`else
479
        // If we aren't pipelined, then no one will be changing what's in the
480
        // pipeline (i.e. clear_pipeline), while our only instruction goes
481
        // through the ... pipeline.
482 71 dgisselq
        assign  mem_ce = (master_ce)&&(opvalid_mem)&&(~mem_stalled);
483 69 dgisselq
`endif
484 65 dgisselq
`ifdef  OPT_PIPELINED_BUS_ACCESS
485 71 dgisselq
        assign  mem_stalled = (~master_ce)||(alu_busy)||((opvalid_mem)&&(
486 38 dgisselq
                                (mem_pipe_stalled)
487
                                ||((~op_pipe)&&(mem_busy))
488 69 dgisselq
                                ||(div_busy)
489
                                ||(fpu_busy)
490 38 dgisselq
                                // Stall waiting for flags to be valid
491
                                // Or waiting for a write to the PC register
492
                                // Or CC register, since that can change the
493
                                //  PC as well
494
                                ||((wr_reg_ce)&&(wr_reg_id[4] == op_gie)
495
                                        &&((wr_write_pc)||(wr_write_cc)))));
496
`else
497 69 dgisselq
`ifdef  OPT_PIPELINED
498 25 dgisselq
        assign  mem_stalled = (mem_busy)||((opvalid_mem)&&(
499 2 dgisselq
                                (~master_ce)
500
                                // Stall waiting for flags to be valid
501
                                // Or waiting for a write to the PC register
502 25 dgisselq
                                // Or CC register, since that can change the
503
                                //  PC as well
504
                                ||((wr_reg_ce)&&(wr_reg_id[4] == op_gie)&&((wr_write_pc)||(wr_write_cc)))));
505 69 dgisselq
`else
506
        assign  mem_stalled = (opvalid_mem)&&(~master_ce);
507 38 dgisselq
`endif
508 69 dgisselq
`endif
509 2 dgisselq
 
510
 
511
        //
512
        //
513
        //      PIPELINE STAGE #1 :: Prefetch
514
        //
515
        //
516 38 dgisselq
`ifdef  OPT_SINGLE_FETCH
517 9 dgisselq
        wire            pf_ce;
518
 
519 69 dgisselq
        assign          pf_ce = (~pf_valid)&&(~dcdvalid)&&(~opvalid)&&(~alu_valid);
520 48 dgisselq
        prefetch        #(ADDRESS_WIDTH)
521 69 dgisselq
                        pf(i_clk, i_rst, (pf_ce), (~dcd_stalled), pf_pc, gie,
522 2 dgisselq
                                instruction, instruction_pc, instruction_gie,
523 36 dgisselq
                                        pf_valid, pf_illegal,
524
                                pf_cyc, pf_stb, pf_we, pf_addr, pf_data,
525
                                pf_ack, pf_stall, pf_err, i_wb_data);
526 69 dgisselq
 
527
        initial r_dcdvalid = 1'b0;
528
        always @(posedge i_clk)
529
                if (i_rst)
530
                        r_dcdvalid <= 1'b0;
531
                else if (dcd_ce)
532
                        r_dcdvalid <= (pf_valid)&&(~clear_pipeline)&&((~r_dcdvalid)||(~dcd_early_branch));
533
                else if ((op_ce)||(clear_pipeline))
534
                        r_dcdvalid <= 1'b0;
535
        assign  dcdvalid = r_dcdvalid;
536
 
537 2 dgisselq
`else // Pipe fetch
538 69 dgisselq
 
539
`ifdef  OPT_TRADITIONAL_PFCACHE
540
        pfcache #(LGICACHE, ADDRESS_WIDTH)
541
                pf(i_clk, i_rst, (new_pc)||((dcd_early_branch)&&(dcdvalid)),
542
                                        i_clear_pf_cache,
543
                                // dcd_pc,
544
                                ~dcd_stalled,
545
                                ((dcd_early_branch)&&(dcdvalid)&&(~new_pc))
546
                                        ? dcd_branch_pc:pf_pc,
547
                                instruction, instruction_pc, pf_valid,
548
                                pf_cyc, pf_stb, pf_we, pf_addr, pf_data,
549
                                        pf_ack, pf_stall, pf_err, i_wb_data,
550
                                pf_illegal);
551
`else
552 48 dgisselq
        pipefetch       #(RESET_ADDRESS, LGICACHE, ADDRESS_WIDTH)
553 69 dgisselq
                        pf(i_clk, i_rst, (new_pc)||((dcd_early_branch)&&(dcdvalid)),
554 36 dgisselq
                                        i_clear_pf_cache, ~dcd_stalled,
555
                                        (new_pc)?pf_pc:dcd_branch_pc,
556 2 dgisselq
                                        instruction, instruction_pc, pf_valid,
557
                                pf_cyc, pf_stb, pf_we, pf_addr, pf_data,
558 36 dgisselq
                                        pf_ack, pf_stall, pf_err, i_wb_data,
559 69 dgisselq
//`ifdef        OPT_PRECLEAR_BUS
560
                                //((dcd_clear_bus)&&(dcdvalid))
561
                                //||((op_clear_bus)&&(opvalid))
562
                                //||
563
//`endif
564 36 dgisselq
                                (mem_cyc_lcl)||(mem_cyc_gbl),
565
                                pf_illegal);
566 69 dgisselq
`endif
567 2 dgisselq
        assign  instruction_gie = gie;
568
 
569 69 dgisselq
        initial r_dcdvalid = 1'b0;
570 2 dgisselq
        always @(posedge i_clk)
571 69 dgisselq
                if ((i_rst)||(clear_pipeline))
572
                        r_dcdvalid <= 1'b0;
573 2 dgisselq
                else if (dcd_ce)
574 69 dgisselq
                        r_dcdvalid <= (pf_valid)&&(~clear_pipeline)&&((~r_dcdvalid)||(~dcd_early_branch));
575
                else if (op_ce)
576
                        r_dcdvalid <= 1'b0;
577
        assign  dcdvalid = r_dcdvalid;
578 36 dgisselq
`endif
579 2 dgisselq
 
580 69 dgisselq
`ifdef  OPT_NEW_INSTRUCTION_SET
581
        idecode #(AW, IMPLEMENT_MPY, EARLY_BRANCHING, IMPLEMENT_DIVIDE,
582
                        IMPLEMENT_FPU)
583
                instruction_decoder(i_clk, (i_rst)||(clear_pipeline),
584
                        dcd_ce, dcd_stalled, instruction, instruction_gie,
585
                        instruction_pc, pf_valid, pf_illegal, dcd_phase,
586
                        dcd_illegal, dcd_pc, dcd_gie,
587
                        { dcdR_cc, dcdR_pc, dcdR },
588
                        { dcdA_cc, dcdA_pc, dcdA },
589
                        { dcdB_cc, dcdB_pc, dcdB },
590
                        dcdI, dcd_zI, dcdF, dcdF_wr, dcdOp,
591
                        dcdALU, dcdM, dcdDV, dcdFP, dcd_break, dcd_lock,
592
                        dcdR_wr,dcdA_rd, dcdB_rd,
593
                        dcd_early_branch,
594 71 dgisselq
                        dcd_branch_pc,
595
                        dcd_pipe);
596 36 dgisselq
`else
597 69 dgisselq
        idecode_deprecated
598
                #(AW, IMPLEMENT_MPY, EARLY_BRANCHING, IMPLEMENT_DIVIDE,
599
                        IMPLEMENT_FPU)
600
                instruction_decoder(i_clk, (i_rst)||(clear_pipeline),
601
                        dcd_ce, dcd_stalled, instruction, instruction_gie,
602
                        instruction_pc, pf_valid, pf_illegal, dcd_phase,
603
                        dcd_illegal, dcd_pc, dcd_gie,
604
                        { dcdR_cc, dcdR_pc, dcdR },
605
                        { dcdA_cc, dcdA_pc, dcdA },
606
                        { dcdB_cc, dcdB_pc, dcdB },
607
                        dcdI, dcd_zI, dcdF, dcdF_wr, dcdOp,
608
                        dcdALU, dcdM, dcdDV, dcdFP, dcd_break, dcd_lock,
609
                        dcdR_wr,dcdA_rd, dcdB_rd,
610
                        dcd_early_branch,
611 71 dgisselq
                        dcd_branch_pc,
612
                        dcd_pipe);
613 36 dgisselq
`endif
614 2 dgisselq
 
615 38 dgisselq
`ifdef  OPT_PIPELINED_BUS_ACCESS
616
        reg             op_pipe;
617 2 dgisselq
 
618 38 dgisselq
        initial op_pipe = 1'b0;
619
        // To be a pipeable operation, there must be 
620
        //      two valid adjacent instructions
621
        //      Both must be memory instructions
622
        //      Both must be writes, or both must be reads
623
        //      Both operations must be to the same identical address,
624
        //              or at least a single (one) increment above that address
625 71 dgisselq
        //
626
        // However ... we need to know this before this clock, hence this is
627
        // calculated in the instruction decoder.
628 38 dgisselq
        always @(posedge i_clk)
629
                if (op_ce)
630 71 dgisselq
                        op_pipe <= dcd_pipe;
631 38 dgisselq
`endif
632
 
633 2 dgisselq
        //
634
        //
635
        //      PIPELINE STAGE #3 :: Read Operands (Registers)
636
        //
637
        //
638 25 dgisselq
        assign  w_opA = regset[dcdA];
639
        assign  w_opB = regset[dcdB];
640 56 dgisselq
 
641
        wire    [31:0]   w_pcA_v;
642
        generate
643
        if (AW < 32)
644
                assign  w_pcA_v = {{(32-AW){1'b0}}, (dcdA[4] == dcd_gie)?dcd_pc:upc };
645
        else
646
                assign  w_pcA_v = (dcdA[4] == dcd_gie)?dcd_pc:upc;
647
        endgenerate
648 71 dgisselq
 
649
`ifdef  OPT_PIPELINED
650
        reg     [4:0]    opA_id, opB_id;
651
        reg             opA_rd, opB_rd;
652 2 dgisselq
        always @(posedge i_clk)
653 71 dgisselq
                if (op_ce)
654
                begin
655
                        opA_id <= dcdA;
656
                        opB_id <= dcdB;
657
                        opA_rd <= dcdA_rd;
658
                        opB_rd <= dcdB_rd;
659
                end
660
`endif
661
 
662
        always @(posedge i_clk)
663 2 dgisselq
                if (op_ce) // &&(dcdvalid))
664
                begin
665
                        if ((wr_reg_ce)&&(wr_reg_id == dcdA))
666
                                r_opA <= wr_reg_vl;
667 25 dgisselq
                        else if (dcdA_pc)
668 56 dgisselq
                                r_opA <= w_pcA_v;
669 25 dgisselq
                        else if (dcdA_cc)
670 69 dgisselq
                                r_opA <= { w_opA[31:13], (dcdA[4])?w_uflags:w_iflags };
671 2 dgisselq
                        else
672 25 dgisselq
                                r_opA <= w_opA;
673 69 dgisselq
`ifdef  OPT_PIPELINED
674 71 dgisselq
                end else
675 48 dgisselq
                begin // We were going to pick these up when they became valid,
676
                        // but for some reason we're stuck here as they became
677
                        // valid.  Pick them up now anyway
678 71 dgisselq
                        // if (((opA_alu)&&(alu_wr))||((opA_mem)&&(mem_valid)))
679
                                // r_opA <= wr_reg_vl;
680
                        if ((wr_reg_ce)&&(wr_reg_id == opA_id)&&(opA_rd))
681 48 dgisselq
                                r_opA <= wr_reg_vl;
682 56 dgisselq
`endif
683 2 dgisselq
                end
684 56 dgisselq
 
685 69 dgisselq
        wire    [31:0]   w_opBnI, w_pcB_v;
686 56 dgisselq
        generate
687
        if (AW < 32)
688
                assign  w_pcB_v = {{(32-AW){1'b0}}, (dcdB[4] == dcd_gie)?dcd_pc:upc };
689
        else
690
                assign  w_pcB_v = (dcdB[4] == dcd_gie)?dcd_pc:upc;
691
        endgenerate
692
 
693 36 dgisselq
        assign  w_opBnI = (~dcdB_rd) ? 32'h00
694 56 dgisselq
                : (((wr_reg_ce)&&(wr_reg_id == dcdB)) ? wr_reg_vl
695
                : ((dcdB_pc) ? w_pcB_v
696 69 dgisselq
                : ((dcdB_cc) ? { w_opB[31:13], (dcdB[4])?w_uflags:w_iflags}
697 56 dgisselq
                : w_opB)));
698
 
699 2 dgisselq
        always @(posedge i_clk)
700
                if (op_ce) // &&(dcdvalid))
701 36 dgisselq
                        r_opB <= w_opBnI + dcdI;
702 69 dgisselq
`ifdef  OPT_PIPELINED
703 71 dgisselq
                else if ((wr_reg_ce)&&(opB_id == wr_reg_id)&&(opB_rd))
704 48 dgisselq
                        r_opB <= wr_reg_vl;
705 56 dgisselq
`endif
706 2 dgisselq
 
707
        // The logic here has become more complex than it should be, no thanks
708
        // to Xilinx's Vivado trying to help.  The conditions are supposed to
709
        // be two sets of four bits: the top bits specify what bits matter, the
710
        // bottom specify what those top bits must equal.  However, two of
711
        // conditions check whether bits are on, and those are the only two
712
        // conditions checking those bits.  Therefore, Vivado complains that
713
        // these two bits are redundant.  Hence the convoluted expression
714
        // below, arriving at what we finally want in the (now wire net)
715
        // opF.
716
        always @(posedge i_clk)
717
                if (op_ce)
718 36 dgisselq
                begin // Set the flag condition codes, bit order is [3:0]=VNCZ
719 2 dgisselq
                        case(dcdF[2:0])
720 56 dgisselq
                        3'h0:   r_opF <= 6'h00; // Always
721 69 dgisselq
`ifdef  OPT_NEW_INSTRUCTION_SET
722
                        // These were remapped as part of the new instruction
723
                        // set in order to make certain that the low order
724
                        // two bits contained the most commonly used 
725
                        // conditions: Always, LT, Z, and NZ.
726
                        3'h1:   r_opF <= 6'h24; // LT
727
                        3'h2:   r_opF <= 6'h11; // Z
728
                        3'h3:   r_opF <= 6'h10; // NE
729
                        3'h4:   r_opF <= 6'h30; // GT (!N&!Z)
730
                        3'h5:   r_opF <= 6'h20; // GE (!N)
731
`else
732 56 dgisselq
                        3'h1:   r_opF <= 6'h11; // Z
733
                        3'h2:   r_opF <= 6'h10; // NE
734
                        3'h3:   r_opF <= 6'h20; // GE (!N)
735
                        3'h4:   r_opF <= 6'h30; // GT (!N&!Z)
736
                        3'h5:   r_opF <= 6'h24; // LT
737 69 dgisselq
`endif
738 56 dgisselq
                        3'h6:   r_opF <= 6'h02; // C
739
                        3'h7:   r_opF <= 6'h08; // V
740 2 dgisselq
                        endcase
741 36 dgisselq
                end // Bit order is { (flags_not_used), VNCZ mask, VNCZ value }
742 56 dgisselq
        assign  opF = { r_opF[3], r_opF[5], r_opF[1], r_opF[4:0] };
743
        always @(posedge i_clk)
744
                if (op_ce)
745
                        opF_cp[2:0] <= dcdF[2:0];
746 2 dgisselq
 
747 69 dgisselq
        wire    w_opvalid;
748
        assign  w_opvalid = (~clear_pipeline)&&(dcdvalid);
749 36 dgisselq
        initial opvalid     = 1'b0;
750
        initial opvalid_alu = 1'b0;
751
        initial opvalid_mem = 1'b0;
752 2 dgisselq
        always @(posedge i_clk)
753
                if (i_rst)
754 25 dgisselq
                begin
755
                        opvalid     <= 1'b0;
756
                        opvalid_alu <= 1'b0;
757
                        opvalid_mem <= 1'b0;
758
                end else if (op_ce)
759
                begin
760 2 dgisselq
                        // Do we have a valid instruction?
761
                        //   The decoder may vote to stall one of its
762
                        //   instructions based upon something we currently
763
                        //   have in our queue.  This instruction must then
764
                        //   move forward, and get a stall cycle inserted.
765
                        //   Hence, the test on dcd_stalled here.  If we must
766
                        //   wait until our operands are valid, then we aren't
767
                        //   valid yet until then.
768 69 dgisselq
                        opvalid<= w_opvalid;
769 38 dgisselq
`ifdef  OPT_ILLEGAL_INSTRUCTION
770 69 dgisselq
                        opvalid_alu <= ((dcdALU)||(dcd_illegal))&&(w_opvalid);
771
                        opvalid_mem <= (dcdM)&&(~dcd_illegal)&&(w_opvalid);
772
                        opvalid_div <= (dcdDV)&&(~dcd_illegal)&&(w_opvalid);
773
                        opvalid_fpu <= (dcdFP)&&(~dcd_illegal)&&(w_opvalid);
774 36 dgisselq
`else
775 69 dgisselq
                        opvalid_alu <= (dcdALU)&&(w_opvalid);
776
                        opvalid_mem <= (dcdM)&&(w_opvalid);
777
                        opvalid_div <= (dcdDV)&&(w_opvalid);
778
                        opvalid_fpu <= (dcdFP)&&(w_opvalid);
779 36 dgisselq
`endif
780 69 dgisselq
                end else if ((clear_pipeline)||(alu_ce)||(mem_ce)||(div_ce)||(fpu_ce))
781 25 dgisselq
                begin
782
                        opvalid     <= 1'b0;
783
                        opvalid_alu <= 1'b0;
784
                        opvalid_mem <= 1'b0;
785 69 dgisselq
                        opvalid_div <= 1'b0;
786
                        opvalid_fpu <= 1'b0;
787 25 dgisselq
                end
788 2 dgisselq
 
789
        // Here's part of our debug interface.  When we recognize a break
790
        // instruction, we set the op_break flag.  That'll prevent this
791
        // instruction from entering the ALU, and cause an interrupt before
792
        // this instruction.  Thus, returning to this code will cause the
793
        // break to repeat and continue upon return.  To get out of this
794
        // condition, replace the break instruction with what it is supposed
795
        // to be, step through it, and then replace it back.  In this fashion,
796
        // a debugger can step through code.
797 25 dgisselq
        // assign w_op_break = (dcd_break)&&(r_dcdI[15:0] == 16'h0001);
798
        initial op_break = 1'b0;
799 2 dgisselq
        always @(posedge i_clk)
800 25 dgisselq
                if (i_rst)      op_break <= 1'b0;
801
                else if (op_ce) op_break <= (dcd_break);
802
                else if ((clear_pipeline)||(~opvalid))
803
                                op_break <= 1'b0;
804 2 dgisselq
 
805 69 dgisselq
`ifdef  OPT_PIPELINED
806
        generate
807
        if (IMPLEMENT_LOCK != 0)
808
        begin
809
                reg     r_op_lock, r_op_lock_stall;
810
 
811
                initial r_op_lock_stall = 1'b0;
812
                always @(posedge i_clk)
813
                        if (i_rst)
814
                                r_op_lock_stall <= 1'b0;
815
                        else
816
                                r_op_lock_stall <= (~opvalid)||(~op_lock)
817
                                                ||(~dcdvalid)||(~pf_valid);
818
 
819
                assign  op_lock_stall = r_op_lock_stall;
820
 
821
                initial r_op_lock = 1'b0;
822
                always @(posedge i_clk)
823
                        if (i_rst)
824
                                r_op_lock <= 1'b0;
825
                        else if ((op_ce)&&(dcd_lock))
826
                                r_op_lock <= 1'b1;
827
                        else if ((op_ce)||(clear_pipeline))
828
                                r_op_lock <= 1'b0;
829
                assign  op_lock = r_op_lock;
830
 
831
        end else begin
832
                assign  op_lock_stall = 1'b0;
833
                assign  op_lock = 1'b0;
834
        end endgenerate
835
 
836
`else
837
        assign op_lock_stall = 1'b0;
838
        assign op_lock       = 1'b0;
839
`endif
840
 
841 38 dgisselq
`ifdef  OPT_ILLEGAL_INSTRUCTION
842 71 dgisselq
        initial op_illegal = 1'b0;
843 2 dgisselq
        always @(posedge i_clk)
844 71 dgisselq
                if ((i_rst)||(clear_pipeline))
845
                        op_illegal <= 1'b0;
846
                else if(op_ce)
847 69 dgisselq
`ifdef  OPT_PIPELINED
848
                        op_illegal <=(dcd_illegal)||((dcd_lock)&&(IMPLEMENT_LOCK == 0));
849
`else
850
                        op_illegal <= (dcd_illegal)||(dcd_lock);
851 36 dgisselq
`endif
852 69 dgisselq
`endif
853 36 dgisselq
 
854 71 dgisselq
        // No generate on EARLY_BRANCHING here, since if EARLY_BRANCHING is not
855
        // set, dcd_early_branch will simply be a wire connected to zero and
856
        // this logic should just optimize.
857
        always @(posedge i_clk)
858
                if (op_ce)
859
                begin
860
                        opF_wr <= (dcdF_wr)&&((~dcdR_cc)||(~dcdR_wr))&&(~dcd_early_branch);
861
                        opR_wr <= (dcdR_wr)&&(~dcd_early_branch);
862
                        op_wr_pc <= ((dcdR_wr)&&(dcdR_pc)
863
                                        &&(dcdR[4] == dcd_gie))
864
                                        &&(~dcd_early_branch);
865
                end
866 69 dgisselq
 
867 36 dgisselq
        always @(posedge i_clk)
868 2 dgisselq
                if (op_ce)
869
                begin
870
                        opn    <= dcdOp;        // Which ALU operation?
871 25 dgisselq
                        // opM  <= dcdM;        // Is this a memory operation?
872 2 dgisselq
                        // What register will these results be written into?
873 69 dgisselq
                        opR    <= dcdR;
874
                        opR_cc <= (dcdR_cc)&&(dcdR_wr)&&(dcdR[4]==dcd_gie);
875 2 dgisselq
                        // User level (1), vs supervisor (0)/interrupts disabled
876
                        op_gie <= dcd_gie;
877
 
878 69 dgisselq
 
879 2 dgisselq
                        //
880 48 dgisselq
                        op_pc  <= (dcd_early_branch)?dcd_branch_pc:dcd_pc;
881 2 dgisselq
                end
882
        assign  opFl = (op_gie)?(w_uflags):(w_iflags);
883
 
884 69 dgisselq
`ifdef  OPT_VLIW
885
        reg     r_op_phase;
886
        initial r_op_phase = 1'b0;
887
        always @(posedge i_clk)
888
                if ((i_rst)||(clear_pipeline))
889
                        r_op_phase <= 1'b0;
890
                else if (op_ce)
891
                        r_op_phase <= dcd_phase;
892
        assign  op_phase = r_op_phase;
893
`else
894
        assign  op_phase = 1'b0;
895
`endif
896
 
897 2 dgisselq
        // This is tricky.  First, the PC and Flags registers aren't kept in
898
        // register set but in special registers of their own.  So step one
899
        // is to select the right register.  Step to is to replace that
900
        // register with the results of an ALU or memory operation, if such
901
        // results are now available.  Otherwise, we'd need to insert a wait
902
        // state of some type.
903
        //
904
        // The alternative approach would be to define some sort of
905
        // op_stall wire, which would stall any upstream stage.
906
        // We'll create a flag here to start our coordination.  Once we
907
        // define this flag to something other than just plain zero, then
908
        // the stalls will already be in place.
909 69 dgisselq
`ifdef  OPT_PIPELINED
910 71 dgisselq
        assign  opA = ((wr_reg_ce)&&(wr_reg_id == opA_id)&&(opA_rd))
911
                        ?  wr_reg_vl : r_opA;
912 56 dgisselq
`else
913
        assign  opA = r_opA;
914
`endif
915 48 dgisselq
 
916 69 dgisselq
`ifdef  OPT_PIPELINED
917 25 dgisselq
        assign  dcdA_stall = (dcdvalid)&&(dcdA_rd)&&(
918 71 dgisselq
                                ((opvalid_alu)&&(opF_wr)&&(dcdA_cc))
919
                                );
920 56 dgisselq
`else
921 69 dgisselq
        // There are no pipeline hazards, if we aren't pipelined
922
        assign  dcdA_stall = 1'b0;
923 56 dgisselq
`endif
924 36 dgisselq
 
925 69 dgisselq
`ifdef  OPT_PIPELINED
926 71 dgisselq
        assign  opB = ((wr_reg_ce)&&(wr_reg_id == opB_id)&&(opB_rd))
927
                        ? wr_reg_vl: r_opB;
928 56 dgisselq
`else
929
        assign  opB = r_opB;
930
`endif
931
 
932 69 dgisselq
`ifdef  OPT_PIPELINED
933 25 dgisselq
        assign  dcdB_stall = (dcdvalid)&&(dcdB_rd)&&(
934 38 dgisselq
                                // Stall on memory ops writing to my register
935
                                //      (i.e. loads), or on any write to my
936
                                //      register if I have an immediate offset
937
                                // Note the exception for writing to the PC:
938
                                //      if I write to the PC, the whole next
939
                                //      instruction is invalid, not just the
940
                                //      operand.  That'll get wiped in the
941
                                //      next operation anyway, so don't stall
942
                                //      here.
943 71 dgisselq
                                ((~dcd_zI)&&(dcdB_rd)&&(opR == dcdB)&&(opR_wr)
944
                                        &&(opR != { op_gie, `CPU_PC_REG } )
945
                                        &&((opvalid)||(mem_rdbusy)
946
                                                ||(div_busy)||(fpu_busy)))
947 38 dgisselq
                                // Stall on any write to the flags register,
948
                                // if we're going to need the flags value for
949
                                // opB.
950 30 dgisselq
                                ||((opvalid_alu)&&(opF_wr)&&(dcdB_cc))
951 38 dgisselq
                                // Stall on any ongoing memory operation that
952 71 dgisselq
                                // will write to opB -- captured above
953
                                // ||((mem_busy)&&(~mem_we)&&(mem_last_reg==dcdB)&&(~dcd_zI))
954
                                );
955 56 dgisselq
`else
956 69 dgisselq
        // No stalls without pipelining, 'cause how can you have a pipeline
957
        // hazard without the pipeline?
958
        assign  dcdB_stall = 1'b0;
959 56 dgisselq
`endif
960 69 dgisselq
        assign  dcdF_stall = (dcdvalid)&&((~dcdF[3])
961
                                        ||((dcdA_rd)&&(dcdA_cc))
962
                                        ||((dcdB_rd)&&(dcdB_cc)))
963 30 dgisselq
                                        &&(opvalid)&&(opR_cc);
964 2 dgisselq
        //
965
        //
966
        //      PIPELINE STAGE #4 :: Apply Instruction
967
        //
968
        //
969 69 dgisselq
`ifdef  OPT_NEW_INSTRUCTION_SET
970 56 dgisselq
        cpuops  #(IMPLEMENT_MPY) doalu(i_clk, i_rst, alu_ce,
971 25 dgisselq
                        (opvalid_alu), opn, opA, opB,
972 71 dgisselq
                        alu_result, alu_flags, alu_valid, alu_illegal_op,
973
                        alu_busy);
974 69 dgisselq
`else
975
        cpuops_deprecated       #(IMPLEMENT_MPY) doalu(i_clk, i_rst, alu_ce,
976
                        (opvalid_alu), opn, opA, opB,
977
                        alu_result, alu_flags, alu_valid, alu_illegal_op);
978 71 dgisselq
        assign  alu_busy = 1'b0;
979 69 dgisselq
`endif
980 2 dgisselq
 
981 69 dgisselq
        generate
982
        if (IMPLEMENT_DIVIDE != 0)
983
        begin
984
                div thedivide(i_clk, i_rst, div_ce, opn[0],
985
                        opA, opB, div_busy, div_valid, div_error, div_result,
986
                        div_flags);
987
        end else begin
988
                assign  div_error = 1'b1;
989
                assign  div_busy  = 1'b0;
990
                assign  div_valid = 1'b0;
991
                assign  div_result= 32'h00;
992
                assign  div_flags = 4'h0;
993
        end endgenerate
994
 
995
        generate
996
        if (IMPLEMENT_FPU != 0)
997
        begin
998
                //
999
                // sfpu thefpu(i_clk, i_rst, fpu_ce,
1000
                //      opA, opB, fpu_busy, fpu_valid, fpu_err, fpu_result,
1001
                //      fpu_flags);
1002
                //
1003
                assign  fpu_error = 1'b1;
1004
                assign  fpu_busy  = 1'b0;
1005
                assign  fpu_valid = 1'b0;
1006
                assign  fpu_result= 32'h00;
1007
                assign  fpu_flags = 4'h0;
1008
        end else begin
1009
                assign  fpu_error = 1'b1;
1010
                assign  fpu_busy  = 1'b0;
1011
                assign  fpu_valid = 1'b0;
1012
                assign  fpu_result= 32'h00;
1013
                assign  fpu_flags = 4'h0;
1014
        end endgenerate
1015
 
1016
 
1017 2 dgisselq
        assign  set_cond = ((opF[7:4]&opFl[3:0])==opF[3:0]);
1018
        initial alF_wr   = 1'b0;
1019
        initial alu_wr   = 1'b0;
1020
        always @(posedge i_clk)
1021
                if (i_rst)
1022
                begin
1023
                        alu_wr   <= 1'b0;
1024
                        alF_wr   <= 1'b0;
1025
                end else if (alu_ce)
1026
                begin
1027 65 dgisselq
                        // alu_reg <= opR;
1028 2 dgisselq
                        alu_wr  <= (opR_wr)&&(set_cond);
1029
                        alF_wr  <= (opF_wr)&&(set_cond);
1030 71 dgisselq
                end else if (~alu_busy) begin
1031 2 dgisselq
                        // These are strobe signals, so clear them if not
1032
                        // set for any particular clock
1033 65 dgisselq
                        alu_wr <= (i_halt)&&(i_dbg_we);
1034 2 dgisselq
                        alF_wr <= 1'b0;
1035
                end
1036 69 dgisselq
 
1037
`ifdef  OPT_VLIW
1038
        reg     r_alu_phase;
1039
        initial r_alu_phase = 1'b0;
1040 2 dgisselq
        always @(posedge i_clk)
1041 69 dgisselq
                if (i_rst)
1042
                        r_alu_phase <= 1'b0;
1043
                else if ((alu_ce)||(mem_ce)||(div_ce)||(fpu_ce))
1044
                        r_alu_phase <= op_phase;
1045
        assign  alu_phase = r_alu_phase;
1046
`else
1047
        assign  alu_phase = 1'b0;
1048
`endif
1049
 
1050
        always @(posedge i_clk)
1051
                if ((alu_ce)||(div_ce)||(fpu_ce))
1052 65 dgisselq
                        alu_reg <= opR;
1053
                else if ((i_halt)&&(i_dbg_we))
1054
                        alu_reg <= i_dbg_reg;
1055 69 dgisselq
 
1056 65 dgisselq
        reg     [31:0]   dbg_val;
1057
        reg             dbgv;
1058
        always @(posedge i_clk)
1059
                dbg_val <= i_dbg_data;
1060
        initial dbgv = 1'b0;
1061
        always @(posedge i_clk)
1062
                dbgv <= (~i_rst)&&(~alu_ce)&&((i_halt)&&(i_dbg_we));
1063
        always @(posedge i_clk)
1064 2 dgisselq
                if ((alu_ce)||(mem_ce))
1065
                        alu_gie  <= op_gie;
1066
        always @(posedge i_clk)
1067 65 dgisselq
                if ((alu_ce)||((master_ce)&&(opvalid_mem)&&(~clear_pipeline)
1068
                                &&(~mem_stalled)))
1069 2 dgisselq
                        alu_pc  <= op_pc;
1070 65 dgisselq
 
1071 38 dgisselq
`ifdef  OPT_ILLEGAL_INSTRUCTION
1072 56 dgisselq
        reg     r_alu_illegal;
1073
        initial r_alu_illegal = 0;
1074 38 dgisselq
        always @(posedge i_clk)
1075 71 dgisselq
                if (clear_pipeline)
1076
                        r_alu_illegal <= 1'b0;
1077
                else if ((alu_ce)||(mem_ce))
1078 56 dgisselq
                        r_alu_illegal <= op_illegal;
1079
        assign  alu_illegal = (alu_illegal_op)||(r_alu_illegal);
1080 38 dgisselq
`endif
1081
 
1082 65 dgisselq
        // This _almost_ is equal to (alu_ce)||(mem_ce).  The only
1083
        // problem is that mem_ce is gated by the set_cond, and
1084
        // the PC will be valid independent of the set condition.  Hence, this
1085
        // equals (alu_ce)||(everything in mem_ce but the set condition)
1086 2 dgisselq
        initial alu_pc_valid = 1'b0;
1087
        always @(posedge i_clk)
1088 65 dgisselq
                alu_pc_valid <= ((alu_ce)
1089
                        ||((master_ce)&&(opvalid_mem)&&(~clear_pipeline)&&(~mem_stalled)));
1090 2 dgisselq
 
1091 69 dgisselq
        wire    bus_lock;
1092
`ifdef  OPT_PIPELINED
1093
        generate
1094
        if (IMPLEMENT_LOCK != 0)
1095
        begin
1096
                reg     r_bus_lock;
1097
                initial r_bus_lock = 1'b0;
1098
                always @(posedge i_clk)
1099
                        if (i_rst)
1100
                                r_bus_lock <= 1'b0;
1101
                        else if ((op_ce)&&(op_lock))
1102
                                r_bus_lock <= 1'b1;
1103
                        else if (~opvalid_mem)
1104
                                r_bus_lock <= 1'b0;
1105
                assign  bus_lock = r_bus_lock;
1106
        end else begin
1107
                assign  bus_lock = 1'b0;
1108
        end endgenerate
1109
`else
1110
        assign  bus_lock = 1'b0;
1111
`endif
1112
 
1113 38 dgisselq
`ifdef  OPT_PIPELINED_BUS_ACCESS
1114 71 dgisselq
        pipemem #(AW,IMPLEMENT_LOCK) domem(i_clk, i_rst,(mem_ce)&&(set_cond), bus_lock,
1115 38 dgisselq
                                (opn[0]), opB, opA, opR,
1116
                                mem_busy, mem_pipe_stalled,
1117
                                mem_valid, bus_err, mem_wreg, mem_result,
1118
                        mem_cyc_gbl, mem_cyc_lcl,
1119
                                mem_stb_gbl, mem_stb_lcl,
1120
                                mem_we, mem_addr, mem_data,
1121
                                mem_ack, mem_stall, mem_err, i_wb_data);
1122
 
1123
`else // PIPELINED_BUS_ACCESS
1124 71 dgisselq
        memops  #(AW,IMPLEMENT_LOCK) domem(i_clk, i_rst,(mem_ce)&&(set_cond), bus_lock,
1125 2 dgisselq
                                (opn[0]), opB, opA, opR,
1126 38 dgisselq
                                mem_busy,
1127
                                mem_valid, bus_err, mem_wreg, mem_result,
1128 36 dgisselq
                        mem_cyc_gbl, mem_cyc_lcl,
1129
                                mem_stb_gbl, mem_stb_lcl,
1130
                                mem_we, mem_addr, mem_data,
1131
                                mem_ack, mem_stall, mem_err, i_wb_data);
1132 38 dgisselq
`endif // PIPELINED_BUS_ACCESS
1133 65 dgisselq
        assign  mem_rdbusy = ((mem_busy)&&(~mem_we));
1134 2 dgisselq
 
1135
        // Either the prefetch or the instruction gets the memory bus, but 
1136
        // never both.
1137 48 dgisselq
        wbdblpriarb     #(32,AW) pformem(i_clk, i_rst,
1138 36 dgisselq
                // Memory access to the arbiter, priority position
1139
                mem_cyc_gbl, mem_cyc_lcl, mem_stb_gbl, mem_stb_lcl,
1140
                        mem_we, mem_addr, mem_data, mem_ack, mem_stall, mem_err,
1141 2 dgisselq
                // Prefetch access to the arbiter
1142 36 dgisselq
                pf_cyc, 1'b0, pf_stb, 1'b0, pf_we, pf_addr, pf_data,
1143
                        pf_ack, pf_stall, pf_err,
1144 2 dgisselq
                // Common wires, in and out, of the arbiter
1145 36 dgisselq
                o_wb_gbl_cyc, o_wb_lcl_cyc, o_wb_gbl_stb, o_wb_lcl_stb,
1146
                        o_wb_we, o_wb_addr, o_wb_data,
1147
                        i_wb_ack, i_wb_stall, i_wb_err);
1148 2 dgisselq
 
1149
        //
1150
        //
1151
        //      PIPELINE STAGE #5 :: Write-back results
1152
        //
1153
        //
1154
        // This stage is not allowed to stall.  If results are ready to be
1155
        // written back, they are written back at all cost.  Sleepy CPU's
1156
        // won't prevent write back, nor debug modes, halting the CPU, nor
1157
        // anything else.  Indeed, the (master_ce) bit is only as relevant
1158
        // as knowinig something is available for writeback.
1159
 
1160
        //
1161
        // Write back to our generic register set ...
1162
        // When shall we write back?  On one of two conditions
1163
        //      Note that the flags needed to be checked before issuing the
1164
        //      bus instruction, so they don't need to be checked here.
1165
        //      Further, alu_wr includes (set_cond), so we don't need to
1166
        //      check for that here either.
1167 38 dgisselq
`ifdef  OPT_ILLEGAL_INSTRUCTION
1168 71 dgisselq
        assign  wr_reg_ce = (~alu_illegal)&&((alu_wr)&&(alu_valid)&&(~clear_pipeline))||(mem_valid)||(div_valid)||(fpu_valid);
1169 36 dgisselq
`else
1170 69 dgisselq
        assign  wr_reg_ce = ((alu_wr)&&(~clear_pipeline))||(mem_valid)||(div_valid)||(fpu_valid);
1171 36 dgisselq
`endif
1172 2 dgisselq
        // Which register shall be written?
1173 38 dgisselq
        //      COULD SIMPLIFY THIS: by adding three bits to these registers,
1174
        //              One or PC, one for CC, and one for GIE match
1175 69 dgisselq
        //      Note that the alu_reg is the register to write on a divide or
1176
        //      FPU operation.
1177 2 dgisselq
        assign  wr_reg_id = (alu_wr)?alu_reg:mem_wreg;
1178 25 dgisselq
        // Are we writing to the CC register?
1179
        assign  wr_write_cc = (wr_reg_id[3:0] == `CPU_CC_REG);
1180 2 dgisselq
        // Are we writing to the PC?
1181
        assign  wr_write_pc = (wr_reg_id[3:0] == `CPU_PC_REG);
1182
        // What value to write?
1183 71 dgisselq
        assign  wr_reg_vl = ((mem_valid) ? mem_result
1184
                                :((div_valid|fpu_valid))
1185
                                        ? ((div_valid) ? div_result:fpu_result)
1186
                                :((dbgv) ? dbg_val : alu_result));
1187 2 dgisselq
        always @(posedge i_clk)
1188
                if (wr_reg_ce)
1189
                        regset[wr_reg_id] <= wr_reg_vl;
1190
 
1191
        //
1192
        // Write back to the condition codes/flags register ...
1193
        // When shall we write to our flags register?  alF_wr already
1194
        // includes the set condition ...
1195 69 dgisselq
        assign  wr_flags_ce = ((alF_wr)||(div_valid)||(fpu_valid))&&(~clear_pipeline)&&(~alu_illegal);
1196 71 dgisselq
        assign  w_uflags = { ufpu_err_flag,
1197
                        udiv_err_flag, ubus_err_flag, trap, ill_err_u,
1198
                        1'b0, step, 1'b1, sleep,
1199
                        ((wr_flags_ce)&&(alu_gie))?alu_flags:flags };
1200
        assign  w_iflags = { ifpu_err_flag,
1201
                        idiv_err_flag, ibus_err_flag, trap, ill_err_i,
1202
                        break_en, 1'b0, 1'b0, sleep,
1203
                        ((wr_flags_ce)&&(~alu_gie))?alu_flags:iflags };
1204 69 dgisselq
 
1205
 
1206 2 dgisselq
        // What value to write?
1207
        always @(posedge i_clk)
1208
                // If explicitly writing the register itself
1209 25 dgisselq
                if ((wr_reg_ce)&&(wr_reg_id[4])&&(wr_write_cc))
1210 2 dgisselq
                        flags <= wr_reg_vl[3:0];
1211
                // Otherwise if we're setting the flags from an ALU operation
1212
                else if ((wr_flags_ce)&&(alu_gie))
1213 69 dgisselq
                        flags <= (div_valid)?div_flags:((fpu_valid)?fpu_flags
1214
                                : alu_flags);
1215 2 dgisselq
 
1216
        always @(posedge i_clk)
1217 25 dgisselq
                if ((wr_reg_ce)&&(~wr_reg_id[4])&&(wr_write_cc))
1218 2 dgisselq
                        iflags <= wr_reg_vl[3:0];
1219
                else if ((wr_flags_ce)&&(~alu_gie))
1220 69 dgisselq
                        iflags <= (div_valid)?div_flags:((fpu_valid)?fpu_flags
1221
                                : alu_flags);
1222 2 dgisselq
 
1223
        // The 'break' enable  bit.  This bit can only be set from supervisor
1224
        // mode.  It control what the CPU does upon encountering a break
1225
        // instruction.
1226
        //
1227
        // The goal, upon encountering a break is that the CPU should stop and
1228
        // not execute the break instruction, choosing instead to enter into
1229
        // either interrupt mode or halt first.  
1230
        //      if ((break_en) AND (break_instruction)) // user mode or not
1231
        //              HALT CPU
1232
        //      else if (break_instruction) // only in user mode
1233
        //              set an interrupt flag, go to supervisor mode
1234
        //              allow supervisor to step the CPU.
1235
        //      Upon a CPU halt, any break condition will be reset.  The
1236
        //      external debugger will then need to deal with whatever
1237
        //      condition has taken place.
1238
        initial break_en = 1'b0;
1239
        always @(posedge i_clk)
1240
                if ((i_rst)||(i_halt))
1241
                        break_en <= 1'b0;
1242 25 dgisselq
                else if ((wr_reg_ce)&&(~wr_reg_id[4])&&(wr_write_cc))
1243 2 dgisselq
                        break_en <= wr_reg_vl[`CPU_BREAK_BIT];
1244 38 dgisselq
`ifdef  OPT_ILLEGAL_INSTRUCTION
1245 36 dgisselq
        assign  o_break = ((break_en)||(~op_gie))&&(op_break)
1246
                                &&(~alu_valid)&&(~mem_valid)&&(~mem_busy)
1247 69 dgisselq
                                &&(~div_busy)&&(~fpu_busy)
1248 36 dgisselq
                                &&(~clear_pipeline)
1249
                        ||((~alu_gie)&&(bus_err))
1250 69 dgisselq
                        ||((~alu_gie)&&(div_valid)&&(div_error))
1251
                        ||((~alu_gie)&&(fpu_valid)&&(fpu_error))
1252 71 dgisselq
                        ||((~alu_gie)&&(alu_pc_valid)&&(alu_illegal));
1253 36 dgisselq
`else
1254
        assign  o_break = (((break_en)||(~op_gie))&&(op_break)
1255
                                &&(~alu_valid)&&(~mem_valid)&&(~mem_busy)
1256
                                &&(~clear_pipeline))
1257 38 dgisselq
                        ||((~alu_gie)&&(bus_err));
1258 36 dgisselq
`endif
1259 2 dgisselq
 
1260
 
1261
        // The sleep register.  Setting the sleep register causes the CPU to
1262
        // sleep until the next interrupt.  Setting the sleep register within
1263
        // interrupt mode causes the processor to halt until a reset.  This is
1264 25 dgisselq
        // a panic/fault halt.  The trick is that you cannot be allowed to
1265
        // set the sleep bit and switch to supervisor mode in the same 
1266
        // instruction: users are not allowed to halt the CPU.
1267 2 dgisselq
        always @(posedge i_clk)
1268 69 dgisselq
                if ((i_rst)||(w_switch_to_interrupt))
1269 2 dgisselq
                        sleep <= 1'b0;
1270 25 dgisselq
                else if ((wr_reg_ce)&&(wr_write_cc)&&(~alu_gie))
1271
                        // In supervisor mode, we have no protections.  The
1272
                        // supervisor can set the sleep bit however he wants.
1273 69 dgisselq
                        // Well ... not quite.  Switching to user mode and
1274
                        // sleep mode shouold only be possible if the interrupt
1275
                        // flag isn't set.
1276
                        //      Thus: if (i_interrupt)&&(wr_reg_vl[GIE])
1277
                        //              don't set the sleep bit
1278
                        //      otherwise however it would o.w. be set
1279
                        sleep <= (wr_reg_vl[`CPU_SLEEP_BIT])
1280
                                &&((~i_interrupt)||(~wr_reg_vl[`CPU_GIE_BIT]));
1281 25 dgisselq
                else if ((wr_reg_ce)&&(wr_write_cc)&&(wr_reg_vl[`CPU_GIE_BIT]))
1282
                        // In user mode, however, you can only set the sleep
1283
                        // mode while remaining in user mode.  You can't switch
1284
                        // to sleep mode *and* supervisor mode at the same
1285
                        // time, lest you halt the CPU.
1286
                        sleep <= wr_reg_vl[`CPU_SLEEP_BIT];
1287 2 dgisselq
 
1288
        always @(posedge i_clk)
1289
                if ((i_rst)||(w_switch_to_interrupt))
1290
                        step <= 1'b0;
1291 25 dgisselq
                else if ((wr_reg_ce)&&(~alu_gie)&&(wr_reg_id[4])&&(wr_write_cc))
1292 2 dgisselq
                        step <= wr_reg_vl[`CPU_STEP_BIT];
1293 38 dgisselq
                else if ((alu_pc_valid)&&(step)&&(gie))
1294 2 dgisselq
                        step <= 1'b0;
1295
 
1296
        // The GIE register.  Only interrupts can disable the interrupt register
1297
        assign  w_switch_to_interrupt = (gie)&&(
1298
                        // On interrupt (obviously)
1299 69 dgisselq
                        ((i_interrupt)&&(~alu_phase)&&(~bus_lock))
1300 2 dgisselq
                        // If we are stepping the CPU
1301 69 dgisselq
                        ||((alu_pc_valid)&&(step)&&(~alu_phase)&&(~bus_lock))
1302 2 dgisselq
                        // If we encounter a break instruction, if the break
1303 36 dgisselq
                        //      enable isn't set.
1304 69 dgisselq
                        ||((master_ce)&&(~mem_rdbusy)&&(~div_busy)&&(~fpu_busy)
1305
                                &&(op_break)&&(~break_en))
1306 38 dgisselq
`ifdef  OPT_ILLEGAL_INSTRUCTION
1307 36 dgisselq
                        // On an illegal instruction
1308 71 dgisselq
                        ||((alu_pc_valid)&&(alu_illegal))
1309 36 dgisselq
`endif
1310 71 dgisselq
                        // On division by zero.  If the divide isn't
1311
                        // implemented, div_valid and div_error will be short
1312
                        // circuited and that logic will be bypassed
1313
                        ||((div_valid)&&(div_error))
1314
                        // Same thing on a floating point error.
1315
                        ||((fpu_valid)&&(fpu_error))
1316
                        //      
1317 69 dgisselq
                        ||(bus_err)
1318 2 dgisselq
                        // If we write to the CC register
1319
                        ||((wr_reg_ce)&&(~wr_reg_vl[`CPU_GIE_BIT])
1320 25 dgisselq
                                &&(wr_reg_id[4])&&(wr_write_cc))
1321 2 dgisselq
                        );
1322
        assign  w_release_from_interrupt = (~gie)&&(~i_interrupt)
1323
                        // Then if we write the CC register
1324
                        &&(((wr_reg_ce)&&(wr_reg_vl[`CPU_GIE_BIT])
1325 25 dgisselq
                                &&(~wr_reg_id[4])&&(wr_write_cc))
1326 2 dgisselq
                        );
1327
        always @(posedge i_clk)
1328
                if (i_rst)
1329
                        gie <= 1'b0;
1330
                else if (w_switch_to_interrupt)
1331
                        gie <= 1'b0;
1332
                else if (w_release_from_interrupt)
1333
                        gie <= 1'b1;
1334
 
1335 25 dgisselq
        initial trap = 1'b0;
1336
        always @(posedge i_clk)
1337
                if (i_rst)
1338
                        trap <= 1'b0;
1339 69 dgisselq
                else if ((alu_gie)&&(wr_reg_ce)&&(~wr_reg_vl[`CPU_GIE_BIT])
1340
                                &&(wr_write_cc)) // &&(wr_reg_id[4]) implied
1341 25 dgisselq
                        trap <= 1'b1;
1342
                else if (w_release_from_interrupt)
1343
                        trap <= 1'b0;
1344
 
1345 38 dgisselq
`ifdef  OPT_ILLEGAL_INSTRUCTION
1346 65 dgisselq
        initial ill_err_i = 1'b0;
1347 36 dgisselq
        always @(posedge i_clk)
1348
                if (i_rst)
1349 65 dgisselq
                        ill_err_i <= 1'b0;
1350
                // The debug interface can clear this bit
1351
                else if ((dbgv)&&(wr_reg_id == {1'b0, `CPU_CC_REG})
1352
                                &&(~wr_reg_vl[`CPU_ILL_BIT]))
1353
                        ill_err_i <= 1'b0;
1354 71 dgisselq
                else if ((alu_pc_valid)&&(alu_illegal)&&(~alu_gie))
1355 65 dgisselq
                        ill_err_i <= 1'b1;
1356
        initial ill_err_u = 1'b0;
1357
        always @(posedge i_clk)
1358
                if (i_rst)
1359
                        ill_err_u <= 1'b0;
1360
                // The bit is automatically cleared on release from interrupt
1361 36 dgisselq
                else if (w_release_from_interrupt)
1362 65 dgisselq
                        ill_err_u <= 1'b0;
1363
                // If the supervisor writes to this register, clearing the
1364
                // bit, then clear it
1365
                else if (((~alu_gie)||(dbgv))
1366
                                &&(wr_reg_ce)&&(~wr_reg_vl[`CPU_ILL_BIT])
1367
                                &&(wr_reg_id[4])&&(wr_write_cc))
1368
                        ill_err_u <= 1'b0;
1369 71 dgisselq
                else if ((alu_pc_valid)&&(alu_illegal)&&(alu_gie))
1370 65 dgisselq
                        ill_err_u <= 1'b1;
1371 38 dgisselq
`else
1372 65 dgisselq
        assign ill_err_u = 1'b0;
1373
        assign ill_err_i = 1'b0;
1374 36 dgisselq
`endif
1375 65 dgisselq
        // Supervisor/interrupt bus error flag -- this will crash the CPU if
1376
        // ever set.
1377
        initial ibus_err_flag = 1'b0;
1378 36 dgisselq
        always @(posedge i_clk)
1379
                if (i_rst)
1380 65 dgisselq
                        ibus_err_flag <= 1'b0;
1381
                else if ((dbgv)&&(wr_reg_id == {1'b0, `CPU_CC_REG})
1382
                                &&(~wr_reg_vl[`CPU_BUSERR_BIT]))
1383
                        ibus_err_flag <= 1'b0;
1384
                else if ((bus_err)&&(~alu_gie))
1385
                        ibus_err_flag <= 1'b1;
1386
        // User bus error flag -- if ever set, it will cause an interrupt to
1387
        // supervisor mode.  
1388
        initial ubus_err_flag = 1'b0;
1389
        always @(posedge i_clk)
1390
                if (i_rst)
1391
                        ubus_err_flag <= 1'b0;
1392 36 dgisselq
                else if (w_release_from_interrupt)
1393 65 dgisselq
                        ubus_err_flag <= 1'b0;
1394
                // else if ((i_halt)&&(i_dbg_we)&&(~i_dbg_reg[4])
1395
                                // &&(i_dbg_reg == {1'b1, `CPU_CC_REG})
1396
                                // &&(~i_dbg_data[`CPU_BUSERR_BIT]))
1397
                        // ubus_err_flag <= 1'b0;
1398
                else if (((~alu_gie)||(dbgv))&&(wr_reg_ce)
1399
                                &&(~wr_reg_vl[`CPU_BUSERR_BIT])
1400
                                &&(wr_reg_id[4])&&(wr_write_cc))
1401
                        ubus_err_flag <= 1'b0;
1402 36 dgisselq
                else if ((bus_err)&&(alu_gie))
1403 65 dgisselq
                        ubus_err_flag <= 1'b1;
1404 36 dgisselq
 
1405 69 dgisselq
        generate
1406
        if (IMPLEMENT_DIVIDE != 0)
1407
        begin
1408
                reg     r_idiv_err_flag, r_udiv_err_flag;
1409
 
1410
                // Supervisor/interrupt divide (by zero) error flag -- this will
1411
                // crash the CPU if ever set.  This bit is thus available for us
1412
                // to be able to tell if/why the CPU crashed.
1413
                initial r_idiv_err_flag = 1'b0;
1414
                always @(posedge i_clk)
1415
                        if (i_rst)
1416
                                r_idiv_err_flag <= 1'b0;
1417
                        else if ((dbgv)&&(wr_reg_id == {1'b0, `CPU_CC_REG})
1418
                                        &&(~wr_reg_vl[`CPU_DIVERR_BIT]))
1419
                                r_idiv_err_flag <= 1'b0;
1420
                        else if ((div_error)&&(div_valid)&&(~alu_gie))
1421
                                r_idiv_err_flag <= 1'b1;
1422
                // User divide (by zero) error flag -- if ever set, it will
1423
                // cause a sudden switch interrupt to supervisor mode.  
1424
                initial r_udiv_err_flag = 1'b0;
1425
                always @(posedge i_clk)
1426
                        if (i_rst)
1427
                                r_udiv_err_flag <= 1'b0;
1428
                        else if (w_release_from_interrupt)
1429
                                r_udiv_err_flag <= 1'b0;
1430
                        else if (((~alu_gie)||(dbgv))&&(wr_reg_ce)
1431
                                        &&(~wr_reg_vl[`CPU_DIVERR_BIT])
1432
                                        &&(wr_reg_id[4])&&(wr_write_cc))
1433
                                r_udiv_err_flag <= 1'b0;
1434
                        else if ((div_error)&&(alu_gie)&&(div_valid))
1435
                                r_udiv_err_flag <= 1'b1;
1436
 
1437
                assign  idiv_err_flag = r_idiv_err_flag;
1438
                assign  udiv_err_flag = r_udiv_err_flag;
1439
        end else begin
1440
                assign  idiv_err_flag = 1'b0;
1441
                assign  udiv_err_flag = 1'b0;
1442
        end endgenerate
1443
 
1444
        generate
1445
        if (IMPLEMENT_FPU !=0)
1446
        begin
1447
                // Supervisor/interrupt floating point error flag -- this will
1448
                // crash the CPU if ever set.
1449
                reg             r_ifpu_err_flag, r_ufpu_err_flag;
1450
                initial r_ifpu_err_flag = 1'b0;
1451
                always @(posedge i_clk)
1452
                        if (i_rst)
1453
                                r_ifpu_err_flag <= 1'b0;
1454
                        else if ((dbgv)&&(wr_reg_id == {1'b0, `CPU_CC_REG})
1455
                                        &&(~wr_reg_vl[`CPU_FPUERR_BIT]))
1456
                                r_ifpu_err_flag <= 1'b0;
1457
                        else if ((fpu_error)&&(fpu_valid)&&(~alu_gie))
1458
                                r_ifpu_err_flag <= 1'b1;
1459
                // User floating point error flag -- if ever set, it will cause
1460
                // a sudden switch interrupt to supervisor mode.  
1461
                initial r_ufpu_err_flag = 1'b0;
1462
                always @(posedge i_clk)
1463
                        if (i_rst)
1464
                                r_ufpu_err_flag <= 1'b0;
1465
                        else if (w_release_from_interrupt)
1466
                                r_ufpu_err_flag <= 1'b0;
1467
                        else if (((~alu_gie)||(dbgv))&&(wr_reg_ce)
1468
                                        &&(~wr_reg_vl[`CPU_FPUERR_BIT])
1469
                                        &&(wr_reg_id[4])&&(wr_write_cc))
1470
                                r_ufpu_err_flag <= 1'b0;
1471
                        else if ((fpu_error)&&(alu_gie)&&(fpu_valid))
1472
                                r_ufpu_err_flag <= 1'b1;
1473
 
1474
                assign  ifpu_err_flag = r_ifpu_err_flag;
1475
                assign  ufpu_err_flag = r_ufpu_err_flag;
1476
        end else begin
1477
                assign  ifpu_err_flag = 1'b0;
1478
                assign  ufpu_err_flag = 1'b0;
1479
        end endgenerate
1480
 
1481
`ifdef  OPT_VLIW
1482
        reg             r_ihalt_phase, r_uhalt_phase;
1483
 
1484
        initial r_ihalt_phase = 0;
1485
        initial r_uhalt_phase = 0;
1486
        always @(posedge i_clk)
1487
                if (~alu_gie)
1488
                        r_ihalt_phase <= alu_phase;
1489
        always @(posedge i_clk)
1490
                if (alu_gie)
1491
                        r_uhalt_phase <= alu_phase;
1492
                else if (w_release_from_interrupt)
1493
                        r_uhalt_phase <= 1'b0;
1494
 
1495
        assign  ihalt_phase = r_ihalt_phase;
1496
        assign  uhalt_phase = r_uhalt_phase;
1497
`else
1498
        assign  ihalt_phase = 1'b0;
1499
        assign  uhalt_phase = 1'b0;
1500
`endif
1501
 
1502 2 dgisselq
        //
1503
        // Write backs to the PC register, and general increments of it
1504
        //      We support two: upc and ipc.  If the instruction is normal,
1505
        // we increment upc, if interrupt level we increment ipc.  If
1506
        // the instruction writes the PC, we write whichever PC is appropriate.
1507
        //
1508
        // Do we need to all our partial results from the pipeline?
1509
        // What happens when the pipeline has gie and ~gie instructions within
1510
        // it?  Do we clear both?  What if a gie instruction tries to clear
1511
        // a non-gie instruction?
1512
        always @(posedge i_clk)
1513 9 dgisselq
                if ((wr_reg_ce)&&(wr_reg_id[4])&&(wr_write_pc))
1514 48 dgisselq
                        upc <= wr_reg_vl[(AW-1):0];
1515 36 dgisselq
                else if ((alu_gie)&&(alu_pc_valid)&&(~clear_pipeline))
1516 2 dgisselq
                        upc <= alu_pc;
1517
 
1518
        always @(posedge i_clk)
1519
                if (i_rst)
1520
                        ipc <= RESET_ADDRESS;
1521
                else if ((wr_reg_ce)&&(~wr_reg_id[4])&&(wr_write_pc))
1522 48 dgisselq
                        ipc <= wr_reg_vl[(AW-1):0];
1523 36 dgisselq
                else if ((~alu_gie)&&(alu_pc_valid)&&(~clear_pipeline))
1524 2 dgisselq
                        ipc <= alu_pc;
1525
 
1526
        always @(posedge i_clk)
1527
                if (i_rst)
1528
                        pf_pc <= RESET_ADDRESS;
1529
                else if (w_switch_to_interrupt)
1530
                        pf_pc <= ipc;
1531
                else if (w_release_from_interrupt)
1532
                        pf_pc <= upc;
1533
                else if ((wr_reg_ce)&&(wr_reg_id[4] == gie)&&(wr_write_pc))
1534 48 dgisselq
                        pf_pc <= wr_reg_vl[(AW-1):0];
1535 69 dgisselq
`ifdef  OPT_PIPELINED
1536
                else if ((~new_pc)&&((dcd_early_branch)&&(dcdvalid)))
1537
                        pf_pc <= dcd_branch_pc + 1;
1538
                else if ((new_pc)||((~dcd_stalled)&&(pf_valid)))
1539 56 dgisselq
                        pf_pc <= pf_pc + {{(AW-1){1'b0}},1'b1};
1540 69 dgisselq
`else
1541
                else if ((alu_pc_valid)&&(~clear_pipeline))
1542
                        pf_pc <= alu_pc;
1543
`endif
1544 2 dgisselq
 
1545
        initial new_pc = 1'b1;
1546
        always @(posedge i_clk)
1547 18 dgisselq
                if ((i_rst)||(i_clear_pf_cache))
1548 2 dgisselq
                        new_pc <= 1'b1;
1549
                else if (w_switch_to_interrupt)
1550
                        new_pc <= 1'b1;
1551
                else if (w_release_from_interrupt)
1552
                        new_pc <= 1'b1;
1553
                else if ((wr_reg_ce)&&(wr_reg_id[4] == gie)&&(wr_write_pc))
1554
                        new_pc <= 1'b1;
1555
                else
1556
                        new_pc <= 1'b0;
1557
 
1558
        //
1559
        // The debug interface
1560 56 dgisselq
        generate
1561
        if (AW<32)
1562
        begin
1563
                always @(posedge i_clk)
1564 2 dgisselq
                begin
1565
                        o_dbg_reg <= regset[i_dbg_reg];
1566
                        if (i_dbg_reg[3:0] == `CPU_PC_REG)
1567 48 dgisselq
                                o_dbg_reg <= {{(32-AW){1'b0}},(i_dbg_reg[4])?upc:ipc};
1568 2 dgisselq
                        else if (i_dbg_reg[3:0] == `CPU_CC_REG)
1569 56 dgisselq
                        begin
1570 69 dgisselq
                                o_dbg_reg[12:0] <= (i_dbg_reg[4])?w_uflags:w_iflags;
1571 56 dgisselq
                                o_dbg_reg[`CPU_GIE_BIT] <= gie;
1572
                        end
1573 2 dgisselq
                end
1574 56 dgisselq
        end else begin
1575
                always @(posedge i_clk)
1576
                begin
1577
                        o_dbg_reg <= regset[i_dbg_reg];
1578
                        if (i_dbg_reg[3:0] == `CPU_PC_REG)
1579
                                o_dbg_reg <= (i_dbg_reg[4])?upc:ipc;
1580
                        else if (i_dbg_reg[3:0] == `CPU_CC_REG)
1581
                        begin
1582 69 dgisselq
                                o_dbg_reg[12:0] <= (i_dbg_reg[4])?w_uflags:w_iflags;
1583 56 dgisselq
                                o_dbg_reg[`CPU_GIE_BIT] <= gie;
1584
                        end
1585
                end
1586
        end endgenerate
1587
 
1588 2 dgisselq
        always @(posedge i_clk)
1589 56 dgisselq
                o_dbg_cc <= { o_break, bus_err, gie, sleep };
1590 18 dgisselq
 
1591
        always @(posedge i_clk)
1592 25 dgisselq
                o_dbg_stall <= (i_halt)&&(
1593 36 dgisselq
                        (pf_cyc)||(mem_cyc_gbl)||(mem_cyc_lcl)||(mem_busy)
1594 2 dgisselq
                        ||((~opvalid)&&(~i_rst))
1595 25 dgisselq
                        ||((~dcdvalid)&&(~i_rst)));
1596 2 dgisselq
 
1597
        //
1598
        //
1599
        // Produce accounting outputs: Account for any CPU stalls, so we can
1600
        // later evaluate how well we are doing.
1601
        //
1602
        //
1603 71 dgisselq
        assign  o_op_stall = (master_ce)&&(op_stall);
1604 9 dgisselq
        assign  o_pf_stall = (master_ce)&&(~pf_valid);
1605 38 dgisselq
        assign  o_i_count  = (alu_pc_valid)&&(~clear_pipeline);
1606 56 dgisselq
 
1607 65 dgisselq
`ifdef  DEBUG_SCOPE
1608 56 dgisselq
        always @(posedge i_clk)
1609 65 dgisselq
                o_debug <= {
1610 69 dgisselq
                        pf_pc[3:0], flags,
1611 56 dgisselq
                        pf_valid, dcdvalid, opvalid, alu_valid, mem_valid,
1612
                        op_ce, alu_ce, mem_ce,
1613 65 dgisselq
                        //
1614
                        master_ce, opvalid_alu, opvalid_mem,
1615
                        //
1616
                        alu_stall, mem_busy, op_pipe, mem_pipe_stalled,
1617
                        mem_we,
1618
                        // ((opvalid_alu)&&(alu_stall))
1619
                        // ||((opvalid_mem)&&(~op_pipe)&&(mem_busy))
1620
                        // ||((opvalid_mem)&&( op_pipe)&&(mem_pipe_stalled)));
1621
                        // opA[23:20], opA[3:0],
1622
                        gie, sleep,
1623 71 dgisselq
                        wr_reg_ce, wr_reg_vl[4:0]
1624
                /*
1625 69 dgisselq
                        i_rst, master_ce, (new_pc),
1626
                        ((dcd_early_branch)&&(dcdvalid)),
1627
                        pf_valid, pf_illegal,
1628
                        op_ce, dcd_ce, dcdvalid, dcd_stalled,
1629
                        pf_cyc, pf_stb, pf_we, pf_ack, pf_stall, pf_err,
1630
                        pf_pc[7:0], pf_addr[7:0]
1631 71 dgisselq
                */
1632 56 dgisselq
                        };
1633 65 dgisselq
`endif
1634 56 dgisselq
 
1635 2 dgisselq
endmodule

powered by: WebSVN 2.1.0

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