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

Subversion Repositories openarty

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

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

powered by: WebSVN 2.1.0

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