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

Subversion Repositories openarty

[/] [openarty/] [trunk/] [rtl/] [cpu/] [zipcpu.v] - Blame information for rev 49

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

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

powered by: WebSVN 2.1.0

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