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

Subversion Repositories xulalx25soc

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

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

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

powered by: WebSVN 2.1.0

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