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

Subversion Repositories xulalx25soc

[/] [xulalx25soc/] [trunk/] [rtl/] [cpu/] [zipcpu.v] - Blame information for rev 113

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

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

powered by: WebSVN 2.1.0

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