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

Subversion Repositories openarty

[/] [openarty/] [trunk/] [rtl/] [cpu/] [zipcpuhs.v] - Blame information for rev 25

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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