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

Subversion Repositories s6soc

[/] [s6soc/] [trunk/] [rtl/] [cpu/] [zipcpu.v] - Blame information for rev 16

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

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

powered by: WebSVN 2.1.0

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