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

Subversion Repositories zipcpu

[/] [zipcpu/] [trunk/] [rtl/] [core/] [zipcpu.v] - Blame information for rev 21

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
//              The instruction set is about as RISC as you can get, there are
10
//              only 16 instruction types supported (of which one isn't yet
11
//              supported ...)  Please see the accompanying iset.html file
12
//              for a description of these instructions.
13
//
14
//              All instructions are 32-bits wide.  All bus accesses, both
15
//              address and 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.  On the
20
//              Basys board that I'm working on, one instruction may be
21
//              issued every 20 clocks or so, unless and until I implement a
22
//              cache or local memory.
23
//
24
//              2. Instruction Decode
25
//
26
//              3. Read Operands
27
//
28
//              4. Apply Instruction
29
//
30
//              4. Write-back Results
31
//
32
//      A lot of difficult work has been placed into the pipeline stall
33
//      handling.  My original proposal was not to allow pipeline stalls at all.
34
//      The idea would be that the CPU would just run every clock and whatever
35
//      stalled answer took place would just get fixed a clock or two later,
36
//      meaning that the compiler could just schedule everything out.
37
//      This idea died at the memory interface, which can take a variable
38
//      amount of time to read or write any value, thus the whole CPU needed
39
//      to stall on a stalled memory access.
40
//
41
//      My next idea was to just let things complete.  I.e., once an instrution
42
//      starts, it continues to completion no matter what and we go on.  This
43
//      failed at writing the PC.  If the PC gets written in something such as
44
//      a MOV PC,PC+5 instruction, 3 (or however long the pipeline is) clocks
45
//      later, if whether or not something happens in those clocks depends
46
//      upon the instruction fetch filling the pipeline, then the CPU has a
47
//      non-deterministic behavior.
48
//
49
//      This leads to two possibilities: either *everything* stalls upon a 
50
//      stall condition, or partial results need to be destroyed before
51
//      they are written.  This is made more difficult by the fact that
52
//      once a command is written to the memory unit, whether it be a
53
//      read or a write, there is no undoing it--since peripherals on the
54
//      bus may act upon the answer with whatever side effects they might
55
//      have.  (For example, writing a '1' to the interrupt register will
56
//      clear certain interrupts ...)  Further, since the memory ops depend
57
//      upon conditions, the we'll need to wait for the condition codes to
58
//      be available before executing a memory op.  Thus, memory ops can 
59
//      proceed without stalling whenever either the previous instruction
60
//      doesn't write the flags register, or when the memory instruction doesn't
61
//      depend upon the flags register.
62
//
63
//      The other possibility is that we leave independent instruction
64
//      execution behind, so that the pipeline is always full and stalls,
65
//      or moves forward, together on every clock.
66
//
67
//      For now, we pick the first approach: independent instruction execution.
68
//      Thus, if stage 2 stalls, stages 3-5 may still complete the instructions
69
//      in their pipeline.  This leaves another problem: what happens on a
70
//      MOV -1+PC,PC instruction?  There will be four instructions behind this
71
//      one (or is it five?) that will need to be 'cancelled'.  So here's
72
//      the plan: Anything can be cancelled before the ALU/MEM stage,
73
//      since memory ops cannot be canceled after being issued.  Thus, the
74
//      ALU/MEM stage must stall if any prior instruction is going to write
75
//      the PC register (i.e. JMP).
76
//
77
//      Further, let's define a "STALL" as a reason to not execute a stage
78
//      due to some condition at or beyond the stage, and let's define
79
//      a VALID flag to mean that this stage has completed.  Thus, the clock
80
//      enable for a stage is (STG[n-1]VALID)&&((~STG[n]VALID)||(~STG[n]STALL)).
81
//      The ALU/MEM stages will also depend upon a master clock enable
82
//      (~SLEEP) condition as well.
83
//
84
//
85
//
86
// Creator:     Dan Gisselquist, Ph.D.
87
//              Gisselquist Tecnology, LLC
88
//
89
///////////////////////////////////////////////////////////////////////////////
90
//
91
// Copyright (C) 2015, Gisselquist Technology, LLC
92
//
93
// This program is free software (firmware): you can redistribute it and/or
94
// modify it under the terms of  the GNU General Public License as published
95
// by the Free Software Foundation, either version 3 of the License, or (at
96
// your option) any later version.
97
//
98
// This program is distributed in the hope that it will be useful, but WITHOUT
99
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
100
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
101
// for more details.
102
//
103
// License:     GPL, v3, as defined and found on www.gnu.org,
104
//              http://www.gnu.org/licenses/gpl.html
105
//
106
//
107
///////////////////////////////////////////////////////////////////////////////
108
//
109
`define CPU_PC_REG      4'hf
110
`define CPU_CC_REG      4'he
111
`define CPU_BREAK_BIT   7
112
`define CPU_STEP_BIT    6
113
`define CPU_GIE_BIT     5
114
`define CPU_SLEEP_BIT   4
115
module  zipcpu(i_clk, i_rst, i_interrupt,
116
                // Debug interface
117 18 dgisselq
                i_halt, i_clear_pf_cache, i_dbg_reg, i_dbg_we, i_dbg_data,
118
                        o_dbg_stall, o_dbg_reg, o_dbg_cc,
119 2 dgisselq
                        o_break,
120
                // CPU interface to the wishbone bus
121
                o_wb_cyc, o_wb_stb, o_wb_we, o_wb_addr, o_wb_data,
122
                        i_wb_ack, i_wb_stall, i_wb_data,
123
                // Accounting/CPU usage interface
124 9 dgisselq
                o_op_stall, o_pf_stall, o_i_count);
125 2 dgisselq
        parameter       RESET_ADDRESS=32'h0100000;
126
        input                   i_clk, i_rst, i_interrupt;
127
        // Debug interface -- inputs
128 18 dgisselq
        input                   i_halt, i_clear_pf_cache;
129 2 dgisselq
        input           [4:0]    i_dbg_reg;
130
        input                   i_dbg_we;
131
        input           [31:0]   i_dbg_data;
132
        // Debug interface -- outputs
133
        output  reg             o_dbg_stall;
134
        output  reg     [31:0]   o_dbg_reg;
135 18 dgisselq
        output  reg     [3:0]    o_dbg_cc;
136 2 dgisselq
        output  wire            o_break;
137
        // Wishbone interface -- outputs
138
        output  wire            o_wb_cyc, o_wb_stb, o_wb_we;
139
        output  wire    [31:0]   o_wb_addr, o_wb_data;
140
        // Wishbone interface -- inputs
141
        input                   i_wb_ack, i_wb_stall;
142
        input           [31:0]   i_wb_data;
143
        // Accounting outputs ... to help us count stalls and usage
144 9 dgisselq
        output  wire            o_op_stall;
145 2 dgisselq
        output  wire            o_pf_stall;
146 9 dgisselq
        output  wire            o_i_count;
147 2 dgisselq
 
148
 
149
        // Registers
150
        reg     [31:0]   regset [0:31];
151 9 dgisselq
 
152
        // Condition codes
153 2 dgisselq
        reg     [3:0]    flags, iflags;  // (BREAKEN,STEP,GIE,SLEEP ), V, N, C, Z
154
        wire    [7:0]    w_uflags, w_iflags;
155 9 dgisselq
        reg             break_en, step, gie, sleep;
156 2 dgisselq
 
157 9 dgisselq
        // The master chip enable
158
        wire            master_ce;
159 2 dgisselq
 
160
        //
161
        //
162
        //      PIPELINE STAGE #1 :: Prefetch
163
        //              Variable declarations
164
        //
165 9 dgisselq
        reg     [31:0]   pf_pc;
166
        reg             new_pc;
167 18 dgisselq
        wire    clear_pipeline;
168
        assign  clear_pipeline = new_pc || i_clear_pf_cache;
169 9 dgisselq
 
170
        wire            dcd_stalled;
171 2 dgisselq
        wire            pf_cyc, pf_stb, pf_we, pf_busy, pf_ack, pf_stall;
172
        wire    [31:0]   pf_addr, pf_data;
173
        wire    [31:0]   instruction, instruction_pc;
174
        wire    pf_valid, instruction_gie;
175
 
176
        //
177
        //
178
        //      PIPELINE STAGE #2 :: Instruction Decode
179
        //              Variable declarations
180
        //
181
        //
182
        reg             opvalid, op_wr_pc, op_break;
183
        wire            op_stall, dcd_ce;
184
        reg     [3:0]    dcdOp;
185
        reg     [4:0]    dcdA, dcdB;
186
        reg     [3:0]    dcdF;
187
        reg             dcdA_rd, dcdA_wr, dcdB_rd, dcdvalid,
188
                                dcdM, dcdF_wr, dcd_gie, dcd_break;
189
        reg     [31:0]   dcd_pc;
190
        reg     [23:0]   r_dcdI;
191
        wire    dcdA_stall, dcdB_stall, dcdF_stall;
192
 
193
 
194
 
195
        //
196
        //
197
        //      PIPELINE STAGE #3 :: Read Operands
198
        //              Variable declarations
199
        //
200
        //
201
        //
202
        // Now, let's read our operands
203
        reg     [4:0]    alu_reg;
204
        reg     [3:0]    opn;
205
        reg     [4:0]    opR;
206
        reg     [1:0]    opA_cc, opB_cc;
207
        reg     [31:0]   r_opA, r_opB, op_pc;
208
        wire    [31:0]   opA_nowait, opB_nowait, opA, opB;
209
        reg             opR_wr, opM, opF_wr, op_gie,
210
                        opA_rd, opB_rd;
211 3 dgisselq
        wire    [7:0]    opFl;
212
        reg     [6:0]    r_opF;
213 2 dgisselq
        wire    [8:0]    opF;
214
        wire            op_ce;
215
 
216
 
217
 
218
        //
219
        //
220
        //      PIPELINE STAGE #4 :: ALU / Memory
221
        //              Variable declarations
222
        //
223
        //
224
        reg     [31:0]   alu_pc;
225
        reg             alu_pc_valid;;
226
        wire            alu_ce, alu_stall;
227
        wire    [31:0]   alu_result;
228
        wire    [3:0]    alu_flags;
229
        wire            alu_valid;
230
        wire            set_cond;
231
        reg             alu_wr, alF_wr, alu_gie;
232
 
233
 
234
 
235
        wire    mem_ce, mem_stalled;
236
        wire    mem_valid, mem_ack, mem_stall,
237
                mem_cyc, mem_stb, mem_we;
238 9 dgisselq
        wire    [4:0]    mem_wreg;
239
 
240
        wire            mem_busy, mem_rdbusy;
241 2 dgisselq
        wire    [31:0]   mem_addr, mem_data, mem_result;
242
 
243
 
244
 
245
        //
246
        //
247
        //      PIPELINE STAGE #5 :: Write-back
248
        //              Variable declarations
249
        //
250
        wire            wr_reg_ce, wr_flags_ce, wr_write_pc;
251
        wire    [4:0]    wr_reg_id;
252
        wire    [31:0]   wr_reg_vl;
253
        wire    w_switch_to_interrupt, w_release_from_interrupt;
254
        reg     [31:0]   upc, ipc;
255
 
256
 
257
 
258
        //
259
        //      MASTER: clock enable.
260
        //
261
        assign  master_ce = (~i_halt)&&(~o_break)&&(~sleep)&&(~mem_rdbusy);
262
 
263
 
264
        //
265
        //      PIPELINE STAGE #1 :: Prefetch
266
        //              Calculate stall conditions
267
 
268
        //
269
        //      PIPELINE STAGE #2 :: Instruction Decode
270
        //              Calculate stall conditions
271
        assign          dcd_ce = (pf_valid)&&(~dcd_stalled);
272
        assign          dcd_stalled = (dcdvalid)&&(
273
                                        (op_stall)
274
                                        ||((dcdA_stall)||(dcdB_stall)||(dcdF_stall))
275
                                        ||((opvalid)&&(op_wr_pc)));
276
        //
277
        //      PIPELINE STAGE #3 :: Read Operands
278
        //              Calculate stall conditions
279
        assign  op_stall = (opvalid)&&(
280
                                ((mem_stalled)&&(opM))
281
                                ||((alu_stall)&&(~opM)));
282
        assign  op_ce = (dcdvalid)&&((~opvalid)||(~op_stall));
283
 
284
        //
285
        //      PIPELINE STAGE #4 :: ALU / Memory
286
        //              Calculate stall conditions
287
        assign  alu_stall = (((~master_ce)||(mem_rdbusy))&&(opvalid)&&(~opM))
288
                        ||((opvalid)&&(wr_reg_ce)&&(wr_reg_id == { op_gie, `CPU_PC_REG }));
289 18 dgisselq
        assign  alu_ce = (master_ce)&&(opvalid)&&(~opM)&&(~alu_stall)&&(~clear_pipeline);
290 2 dgisselq
        //
291 18 dgisselq
        assign  mem_ce = (master_ce)&&(opvalid)&&(opM)&&(~mem_stalled)&&(~clear_pipeline)&&(set_cond);
292 2 dgisselq
        assign  mem_stalled = (mem_busy)||((opvalid)&&(opM)&&(
293
                                (~master_ce)
294
                                // Stall waiting for flags to be valid
295
                                ||((~opF[8])&&(
296
                                        ((wr_reg_ce)&&(wr_reg_id[4:0] == {op_gie,`CPU_CC_REG}))))
297
                                        // Do I need this last condition?
298
                                        //||((wr_flags_ce)&&(alu_gie==op_gie))))
299
                                // Or waiting for a write to the PC register
300
                                ||((wr_reg_ce)&&(wr_reg_id[4] == op_gie)&&(wr_write_pc))));
301
 
302
 
303
        //
304
        //
305
        //      PIPELINE STAGE #1 :: Prefetch
306
        //
307
        //
308
`ifdef  SINGLE_FETCH
309 9 dgisselq
        wire            pf_ce;
310
 
311
        assign          pf_ce = (~dcd_stalled);
312 2 dgisselq
        prefetch        pf(i_clk, i_rst, (pf_ce), pf_pc, gie,
313
                                instruction, instruction_pc, instruction_gie,
314
                                        pf_valid,
315
                                pf_cyc, pf_stb, pf_we, pf_addr,
316
                                        pf_data,
317
                                pf_ack, pf_stall, i_wb_data);
318
`else // Pipe fetch
319 3 dgisselq
        pipefetch       #(RESET_ADDRESS)
320 18 dgisselq
                        pf(i_clk, i_rst, new_pc, i_clear_pf_cache, ~dcd_stalled, pf_pc,
321 2 dgisselq
                                        instruction, instruction_pc, pf_valid,
322
                                pf_cyc, pf_stb, pf_we, pf_addr, pf_data,
323 3 dgisselq
                                        pf_ack, pf_stall, i_wb_data,
324
                                mem_cyc);
325 2 dgisselq
        assign  instruction_gie = gie;
326
`endif
327
 
328
        always @(posedge i_clk)
329
                if (i_rst)
330
                        dcdvalid <= 1'b0;
331
                else if (dcd_ce)
332 18 dgisselq
                        dcdvalid <= (~clear_pipeline);
333
                else if ((~dcd_stalled)||(clear_pipeline))
334 2 dgisselq
                        dcdvalid <= 1'b0;
335
 
336
        always @(posedge i_clk)
337
                if (dcd_ce)
338
                begin
339
                        dcd_pc <= instruction_pc+1;
340
 
341
                        // Record what operation we are doing
342
                        dcdOp <= instruction[31:28];
343
 
344
                        // Default values
345
                        dcdA[4:0] <= { instruction_gie, instruction[27:24] };
346
                        dcdB[4:0] <= { instruction_gie, instruction[19:16] };
347
                        dcdM    <= 1'b0;
348
                        dcdF_wr <= 1'b1;
349
                        dcd_break <= 1'b0;
350
 
351
                        // Set the condition under which we do this operation
352
                        // The top four bits are a mask, the bottom four the
353
                        // value the flags must equal once anded with the mask
354
                        dcdF <= { (instruction[23:21]==3'h0), instruction[23:21] };
355
                        casez(instruction[31:28])
356
                        4'h2: begin // Move instruction
357
                                if (~instruction_gie)
358
                                begin
359
                                        dcdA[4] <= instruction[20];
360
                                        dcdB[4] <= instruction[15];
361
                                end
362
                                dcdA_wr <= 1'b1;
363
                                dcdA_rd <= 1'b0;
364
                                dcdB_rd <= 1'b1;
365
                                r_dcdI <= { {(9){instruction[14]}}, instruction[14:0] };
366
                                dcdF_wr <= 1'b0; // Don't write flags
367
                                end
368
                        4'h3: begin // Load immediate
369
                                dcdA_wr <= 1'b1;
370
                                dcdA_rd <= 1'b0;
371
                                dcdB_rd <= 1'b0;
372
                                r_dcdI <= { instruction[23:0] };
373
                                dcdF_wr <= 1'b0; // Don't write flags
374
                                dcdF    <= 4'h8; // This is unconditional
375
                                dcdOp <= 4'h2;
376
                                end
377
                        4'h4: begin // Load immediate special
378
                                dcdF_wr <= 1'b0; // Don't write flags
379
                                r_dcdI <= { 8'h00, instruction[15:0] };
380
                                if (instruction[27:24] == 4'he)
381
                                begin
382
                                        // NOOP instruction
383
                                        dcdA_wr <= 1'b0;
384
                                        dcdA_rd <= 1'b0;
385
                                        dcdB_rd <= 1'b0;
386
                                        dcdOp <= 4'h2;
387
                                        dcd_break <= 1'b1;//Could be a break ins
388
                                end else if (instruction[27:24] == 4'hf)
389
                                begin // Load partial immediate(s)
390
                                        dcdA_wr <= 1'b1;
391
                                        dcdA_rd <= 1'b1;
392
                                        dcdB_rd <= 1'b0;
393
                                        dcdA[4:0] <= { instruction_gie, instruction[19:16] };
394
                                        dcdOp <= { 3'h3, instruction[20] };
395
                                end else begin
396
                                        ; // Multiply instruction place holder
397
                                end end
398
                        4'b011?: begin // Load/Store
399
                                dcdF_wr <= 1'b0; // Don't write flags
400
                                dcdA_wr <= (~instruction[28]); // Write on loads
401
                                dcdA_rd <= (instruction[28]); // Read on stores
402
                                dcdB_rd <= instruction[20];
403
                                if (instruction[20])
404
                                        r_dcdI <= { {(8){instruction[15]}}, instruction[15:0] };
405
                                else
406
                                        r_dcdI <= { {(4){instruction[19]}}, instruction[19:0] };
407
                                dcdM <= 1'b1; // Memory operation
408
                                end
409
                        default: begin
410
                                dcdA <= { instruction_gie, instruction[27:24] };
411
                                dcdB <= { instruction_gie, instruction[19:16] };
412
                                dcdA_wr <= (instruction[31])||(instruction[31:28]==4'h5);
413
                                dcdA_rd <= 1'b1;
414
                                dcdB_rd <= instruction[20];
415
                                if (instruction[20])
416
                                        r_dcdI <= { {(8){instruction[15]}}, instruction[15:0] };
417
                                else
418
                                        r_dcdI <= { {(4){instruction[19]}}, instruction[19:0] };
419
                                end
420
                        endcase
421
 
422
 
423
                        dcd_gie <= instruction_gie;
424
                end
425
 
426
 
427
        //
428
        //
429
        //      PIPELINE STAGE #3 :: Read Operands (Registers)
430
        //
431
        //
432
 
433
        always @(posedge i_clk)
434
                if (op_ce) // &&(dcdvalid))
435
                begin
436
                        if ((wr_reg_ce)&&(wr_reg_id == dcdA))
437
                                r_opA <= wr_reg_vl;
438
                        else if (dcdA == { dcd_gie, `CPU_PC_REG })
439
                                r_opA <= dcd_pc;
440
                        else if (dcdA[3:0] == `CPU_PC_REG)
441
                                r_opA <= (dcdA[4])?upc:ipc;
442
                        else
443
                                r_opA <= regset[dcdA];
444
                end
445
        wire    [31:0]   dcdI;
446
        assign  dcdI = { {(8){r_dcdI[23]}}, r_dcdI };
447
        always @(posedge i_clk)
448
                if (op_ce) // &&(dcdvalid))
449
                begin
450
                        if (~dcdB_rd)
451
                                r_opB <= dcdI;
452
                        else if ((wr_reg_ce)&&(wr_reg_id == dcdB))
453
                                r_opB <= wr_reg_vl + dcdI;
454
                        else if (dcdB == { dcd_gie, `CPU_PC_REG })
455
                                r_opB <= dcd_pc + dcdI;
456
                        else if (dcdB[3:0] == `CPU_PC_REG)
457
                                r_opB <= ((dcdB[4])?upc:ipc) + dcdI;
458
                        else
459
                                r_opB <= regset[dcdB] + dcdI;
460
                end
461
 
462
        // The logic here has become more complex than it should be, no thanks
463
        // to Xilinx's Vivado trying to help.  The conditions are supposed to
464
        // be two sets of four bits: the top bits specify what bits matter, the
465
        // bottom specify what those top bits must equal.  However, two of
466
        // conditions check whether bits are on, and those are the only two
467
        // conditions checking those bits.  Therefore, Vivado complains that
468
        // these two bits are redundant.  Hence the convoluted expression
469
        // below, arriving at what we finally want in the (now wire net)
470
        // opF.
471 3 dgisselq
`define NEWCODE
472 2 dgisselq
`ifdef  NEWCODE
473
        always @(posedge i_clk)
474
                if (op_ce)
475
                begin // Set the flag condition codes
476
                        case(dcdF[2:0])
477
                        3'h0:   r_opF <= 7'h80; // Always
478
                        3'h1:   r_opF <= 7'h11; // Z
479
                        3'h2:   r_opF <= 7'h10; // NE
480
                        3'h3:   r_opF <= 7'h20; // GE (!N)
481
                        3'h4:   r_opF <= 7'h30; // GT (!N&!Z)
482
                        3'h5:   r_opF <= 7'h24; // LT
483
                        3'h6:   r_opF <= 7'h02; // C
484
                        3'h7:   r_opF <= 7'h08; // V
485
                        endcase
486
                end
487
        assign  opF = { r_opF[6], r_opF[3], r_opF[5], r_opF[1], r_opF[4:0] };
488
`else
489
        always @(posedge i_clk)
490
                if (op_ce)
491
                begin // Set the flag condition codes
492
                        case(dcdF[2:0])
493
                        3'h0:   opF <= 9'h100;  // Always
494
                        3'h1:   opF <= 9'h011;  // Z
495
                        3'h2:   opF <= 9'h010;  // NE
496
                        3'h3:   opF <= 9'h040;  // GE (!N)
497
                        3'h4:   opF <= 9'h050;  // GT (!N&!Z)
498
                        3'h5:   opF <= 9'h044;  // LT
499
                        3'h6:   opF <= 9'h022;  // C
500
                        3'h7:   opF <= 9'h088;  // V
501
                        endcase
502
                end
503
`endif
504
 
505
        always @(posedge i_clk)
506
                if (i_rst)
507
                        opvalid <= 1'b0;
508
                else if (op_ce)
509
                        // Do we have a valid instruction?
510
                        //   The decoder may vote to stall one of its
511
                        //   instructions based upon something we currently
512
                        //   have in our queue.  This instruction must then
513
                        //   move forward, and get a stall cycle inserted.
514
                        //   Hence, the test on dcd_stalled here.  If we must
515
                        //   wait until our operands are valid, then we aren't
516
                        //   valid yet until then.
517 18 dgisselq
                        opvalid<= (~clear_pipeline)&&(dcdvalid)&&(~dcd_stalled);
518
                else if ((~op_stall)||(clear_pipeline))
519 2 dgisselq
                        opvalid <= 1'b0;
520
 
521
        // Here's part of our debug interface.  When we recognize a break
522
        // instruction, we set the op_break flag.  That'll prevent this
523
        // instruction from entering the ALU, and cause an interrupt before
524
        // this instruction.  Thus, returning to this code will cause the
525
        // break to repeat and continue upon return.  To get out of this
526
        // condition, replace the break instruction with what it is supposed
527
        // to be, step through it, and then replace it back.  In this fashion,
528
        // a debugger can step through code.
529
        always @(posedge i_clk)
530
                if (i_rst)
531
                        op_break <= 1'b0;
532
                else if (op_ce)
533
                        op_break <= (dcd_break)&&(r_dcdI[15:0] == 16'h0001);
534 18 dgisselq
                else if ((~op_stall)||(clear_pipeline))
535 2 dgisselq
                        op_break <= 1'b0;
536
 
537
        always @(posedge i_clk)
538
                if (op_ce)
539
                begin
540
                        opn    <= dcdOp;        // Which ALU operation?
541
                        opM    <= dcdM;         // Is this a memory operation?
542
                        // Will we write the flags/CC Register with our result?
543
                        opF_wr <= dcdF_wr;
544
                        // Will we be writing our results into a register?
545
                        opR_wr <= dcdA_wr;
546
                        // What register will these results be written into?
547
                        opR    <= dcdA;
548
                        // User level (1), vs supervisor (0)/interrupts disabled
549
                        op_gie <= dcd_gie;
550
 
551
                        // We're not done with these yet--we still need them
552
                        // for the unclocked assign.  We need the unclocked
553
                        // assign so that there's no wait state between an
554
                        // ALU or memory result and the next register that may
555
                        // use that value.
556
                        opA_cc <= {dcdA[4], (dcdA[3:0] == `CPU_CC_REG) };
557
                        opA_rd <= dcdA_rd;
558
                        opB_cc <= {dcdB[4], (dcdB[3:0] == `CPU_CC_REG) };
559
                        opB_rd <= dcdB_rd;
560
                        op_pc  <= dcd_pc;
561
                        //
562
                        op_wr_pc <= ((dcdA_wr)&&(dcdA[3:0] == `CPU_PC_REG));
563
                end
564
        assign  opFl = (op_gie)?(w_uflags):(w_iflags);
565
 
566
        // This is tricky.  First, the PC and Flags registers aren't kept in
567
        // register set but in special registers of their own.  So step one
568
        // is to select the right register.  Step to is to replace that
569
        // register with the results of an ALU or memory operation, if such
570
        // results are now available.  Otherwise, we'd need to insert a wait
571
        // state of some type.
572
        //
573
        // The alternative approach would be to define some sort of
574
        // op_stall wire, which would stall any upstream stage.
575
        // We'll create a flag here to start our coordination.  Once we
576
        // define this flag to something other than just plain zero, then
577
        // the stalls will already be in place.
578
        assign  dcdA_stall = (dcdvalid)&&(dcdA_rd)&&
579
                                (((opvalid)&&(opR_wr)&&(opR == dcdA))
580
                                        ||((mem_busy)&&(~mem_we)&&(mem_wreg == dcdA))
581
                                        ||((mem_valid)&&(mem_wreg == dcdA)));
582
        assign  dcdB_stall = (dcdvalid)&&(dcdB_rd)
583
                                &&(((opvalid)&&(opR_wr)&&(opR == dcdB))
584
                                        ||((mem_busy)&&(~mem_we)&&(mem_wreg == dcdB))
585
                                        ||((mem_valid)&&(mem_wreg == dcdB)));
586
        assign  dcdF_stall = (dcdvalid)&&(((dcdF[3])
587
                                        ||(dcdA[3:0]==`CPU_CC_REG)
588
                                        ||(dcdB[3:0]==`CPU_CC_REG))
589
                                &&((opvalid)&&(opR[3:0] == `CPU_CC_REG))
590
                        ||((dcdF[3])&&(dcdM)&&(opvalid)&&(opF_wr)));
591
        assign  opA = { r_opA[31:8], ((opA_cc[0]) ?
592
                        ((opA_cc[1])?w_uflags:w_iflags) : r_opA[7:0]) };
593
        assign  opB = { r_opB[31:8], ((opB_cc[0]) ?
594 3 dgisselq
                        ((opB_cc[1])?w_uflags:w_iflags) : r_opB[7:0]) };
595 2 dgisselq
 
596
        //
597
        //
598
        //      PIPELINE STAGE #4 :: Apply Instruction
599
        //
600
        //
601
        cpuops  doalu(i_clk, i_rst, alu_ce,
602
                        (opvalid)&&(~opM), opn, opA, opB,
603
                        alu_result, alu_flags, alu_valid);
604
 
605
        assign  set_cond = ((opF[7:4]&opFl[3:0])==opF[3:0]);
606
        initial alF_wr   = 1'b0;
607
        initial alu_wr   = 1'b0;
608
        always @(posedge i_clk)
609
                if (i_rst)
610
                begin
611
                        alu_wr   <= 1'b0;
612
                        alF_wr   <= 1'b0;
613
                end else if (alu_ce)
614
                begin
615
                        alu_reg <= opR;
616
                        alu_wr  <= (opR_wr)&&(set_cond);
617
                        alF_wr  <= (opF_wr)&&(set_cond);
618
                end else begin
619
                        // These are strobe signals, so clear them if not
620
                        // set for any particular clock
621
                        alu_wr <= 1'b0;
622
                        alF_wr <= 1'b0;
623
                end
624
        always @(posedge i_clk)
625
                if ((alu_ce)||(mem_ce))
626
                        alu_gie  <= op_gie;
627
        always @(posedge i_clk)
628
                if ((alu_ce)||(mem_ce))
629
                        alu_pc  <= op_pc;
630
        initial alu_pc_valid = 1'b0;
631
        always @(posedge i_clk)
632 18 dgisselq
                alu_pc_valid <= (~i_rst)&&(master_ce)&&(opvalid)&&(~clear_pipeline)
633 2 dgisselq
                                        &&((~opM)
634
                                                ||(~mem_stalled));
635
 
636
        memops  domem(i_clk, i_rst, mem_ce,
637
                                (opn[0]), opB, opA, opR,
638
                                mem_busy, mem_valid, mem_wreg, mem_result,
639
                        mem_cyc, mem_stb, mem_we, mem_addr, mem_data,
640
                                mem_ack, mem_stall, i_wb_data);
641
        assign  mem_rdbusy = ((mem_cyc)&&(~mem_we));
642
 
643
        // Either the prefetch or the instruction gets the memory bus, but 
644
        // never both.
645
        wbarbiter       #(32,32) pformem(i_clk, i_rst,
646
                // Prefetch access to the arbiter
647
                pf_addr, pf_data, pf_we, pf_stb, pf_cyc, pf_ack, pf_stall,
648
                // Memory access to the arbiter
649
                mem_addr, mem_data, mem_we, mem_stb, mem_cyc, mem_ack, mem_stall,
650
                // Common wires, in and out, of the arbiter
651
                o_wb_addr, o_wb_data, o_wb_we, o_wb_stb, o_wb_cyc, i_wb_ack,
652
                        i_wb_stall);
653
 
654
        //
655
        //
656
        //      PIPELINE STAGE #5 :: Write-back results
657
        //
658
        //
659
        // This stage is not allowed to stall.  If results are ready to be
660
        // written back, they are written back at all cost.  Sleepy CPU's
661
        // won't prevent write back, nor debug modes, halting the CPU, nor
662
        // anything else.  Indeed, the (master_ce) bit is only as relevant
663
        // as knowinig something is available for writeback.
664
 
665
        //
666
        // Write back to our generic register set ...
667
        // When shall we write back?  On one of two conditions
668
        //      Note that the flags needed to be checked before issuing the
669
        //      bus instruction, so they don't need to be checked here.
670
        //      Further, alu_wr includes (set_cond), so we don't need to
671
        //      check for that here either.
672
        assign  wr_reg_ce = ((alu_wr)&&(alu_valid))||(mem_valid);
673
        // Which register shall be written?
674
        assign  wr_reg_id = (alu_wr)?alu_reg:mem_wreg;
675
        // Are we writing to the PC?
676
        assign  wr_write_pc = (wr_reg_id[3:0] == `CPU_PC_REG);
677
        // What value to write?
678
        assign  wr_reg_vl = (alu_wr)?alu_result:mem_result;
679
        always @(posedge i_clk)
680
                if (wr_reg_ce)
681
                        regset[wr_reg_id] <= wr_reg_vl;
682 18 dgisselq
                else if ((i_halt)&&(i_dbg_we))
683
                        regset[i_dbg_reg] <= i_dbg_data[31:0];
684 2 dgisselq
 
685
        //
686
        // Write back to the condition codes/flags register ...
687
        // When shall we write to our flags register?  alF_wr already
688
        // includes the set condition ...
689
        assign  wr_flags_ce = (alF_wr)&&(alu_valid);
690
        assign  w_uflags = { 1'b0, step, 1'b1, sleep, ((wr_flags_ce)&&(alu_gie))?alu_flags:flags };
691
        assign  w_iflags = { break_en, 1'b0, 1'b0, sleep, ((wr_flags_ce)&&(~alu_gie))?alu_flags:iflags };
692
        // What value to write?
693
        always @(posedge i_clk)
694
                // If explicitly writing the register itself
695
                if ((wr_reg_ce)&&(wr_reg_id[4:0] == { 1'b1, `CPU_CC_REG }))
696
                        flags <= wr_reg_vl[3:0];
697
                // Otherwise if we're setting the flags from an ALU operation
698
                else if ((wr_flags_ce)&&(alu_gie))
699
                        flags <= alu_flags;
700
                else if ((i_halt)&&(i_dbg_we)
701
                                &&(i_dbg_reg == { 1'b1, `CPU_CC_REG }))
702
                        flags <= i_dbg_data[3:0];
703
 
704
        always @(posedge i_clk)
705
                if ((wr_reg_ce)&&(wr_reg_id[4:0] == { 1'b0, `CPU_CC_REG }))
706
                        iflags <= wr_reg_vl[3:0];
707
                else if ((wr_flags_ce)&&(~alu_gie))
708
                        iflags <= alu_flags;
709
                else if ((i_halt)&&(i_dbg_we)
710
                                &&(i_dbg_reg == { 1'b0, `CPU_CC_REG }))
711
                        iflags <= i_dbg_data[3:0];
712
 
713
        // The 'break' enable  bit.  This bit can only be set from supervisor
714
        // mode.  It control what the CPU does upon encountering a break
715
        // instruction.
716
        //
717
        // The goal, upon encountering a break is that the CPU should stop and
718
        // not execute the break instruction, choosing instead to enter into
719
        // either interrupt mode or halt first.  
720
        //      if ((break_en) AND (break_instruction)) // user mode or not
721
        //              HALT CPU
722
        //      else if (break_instruction) // only in user mode
723
        //              set an interrupt flag, go to supervisor mode
724
        //              allow supervisor to step the CPU.
725
        //      Upon a CPU halt, any break condition will be reset.  The
726
        //      external debugger will then need to deal with whatever
727
        //      condition has taken place.
728
        initial break_en = 1'b0;
729
        always @(posedge i_clk)
730
                if ((i_rst)||(i_halt))
731
                        break_en <= 1'b0;
732
                else if ((wr_reg_ce)&&(wr_reg_id[4:0] == {1'b0, `CPU_CC_REG}))
733
                        break_en <= wr_reg_vl[`CPU_BREAK_BIT];
734
        assign  o_break = (break_en)&&(op_break);
735
 
736
 
737
        // The sleep register.  Setting the sleep register causes the CPU to
738
        // sleep until the next interrupt.  Setting the sleep register within
739
        // interrupt mode causes the processor to halt until a reset.  This is
740
        // a panic/fault halt.
741
        always @(posedge i_clk)
742
                if ((i_rst)||((i_interrupt)&&(gie)))
743
                        sleep <= 1'b0;
744
                else if ((wr_reg_ce)&&(wr_reg_id[3:0] == `CPU_CC_REG))
745
                        sleep <= wr_reg_vl[`CPU_SLEEP_BIT];
746
                else if ((i_halt)&&(i_dbg_we)
747
                                &&(i_dbg_reg == { 1'b1, `CPU_CC_REG }))
748
                        sleep <= i_dbg_data[`CPU_SLEEP_BIT];
749
 
750
        always @(posedge i_clk)
751
                if ((i_rst)||(w_switch_to_interrupt))
752
                        step <= 1'b0;
753
                else if ((wr_reg_ce)&&(~alu_gie)&&(wr_reg_id[4:0] == {1'b1,`CPU_CC_REG}))
754
                        step <= wr_reg_vl[`CPU_STEP_BIT];
755
                else if ((i_halt)&&(i_dbg_we)
756
                                &&(i_dbg_reg == { 1'b1, `CPU_CC_REG }))
757
                        step <= i_dbg_data[`CPU_STEP_BIT];
758
                else if ((master_ce)&&(alu_pc_valid)&&(step)&&(gie))
759
                        step <= 1'b0;
760
 
761
        // The GIE register.  Only interrupts can disable the interrupt register
762
        assign  w_switch_to_interrupt = (gie)&&(
763
                        // On interrupt (obviously)
764
                        (i_interrupt)
765
                        // If we are stepping the CPU
766
                        ||((master_ce)&&(alu_pc_valid)&&(step))
767
                        // If we encounter a break instruction, if the break
768
                        //      enable isn't not set.
769
                        ||((master_ce)&&(op_break))
770
                        // If we write to the CC register
771
                        ||((wr_reg_ce)&&(~wr_reg_vl[`CPU_GIE_BIT])
772
                                &&(wr_reg_id[4:0] == { 1'b1, `CPU_CC_REG }))
773
                        // Or if, in debug mode, we write to the CC register
774
                        ||((i_halt)&&(i_dbg_we)&&(~i_dbg_data[`CPU_GIE_BIT])
775
                                &&(i_dbg_reg == { 1'b1, `CPU_CC_REG}))
776
                        );
777
        assign  w_release_from_interrupt = (~gie)&&(~i_interrupt)
778
                        // Then if we write the CC register
779
                        &&(((wr_reg_ce)&&(wr_reg_vl[`CPU_GIE_BIT])
780
                                &&(wr_reg_id[4:0] == { 1'b0, `CPU_CC_REG }))
781
                        // Or if, in debug mode, we write the CC register
782
                          ||((i_halt)&&(i_dbg_we)&&(i_dbg_data[`CPU_GIE_BIT])
783
                                &&(i_dbg_reg == { 1'b0, `CPU_CC_REG}))
784
                        );
785
        always @(posedge i_clk)
786
                if (i_rst)
787
                        gie <= 1'b0;
788
                else if (w_switch_to_interrupt)
789
                        gie <= 1'b0;
790
                else if (w_release_from_interrupt)
791
                        gie <= 1'b1;
792
 
793
        //
794
        // Write backs to the PC register, and general increments of it
795
        //      We support two: upc and ipc.  If the instruction is normal,
796
        // we increment upc, if interrupt level we increment ipc.  If
797
        // the instruction writes the PC, we write whichever PC is appropriate.
798
        //
799
        // Do we need to all our partial results from the pipeline?
800
        // What happens when the pipeline has gie and ~gie instructions within
801
        // it?  Do we clear both?  What if a gie instruction tries to clear
802
        // a non-gie instruction?
803
        always @(posedge i_clk)
804 9 dgisselq
                if ((wr_reg_ce)&&(wr_reg_id[4])&&(wr_write_pc))
805 2 dgisselq
                        upc <= wr_reg_vl;
806
                else if ((alu_gie)&&(alu_pc_valid))
807
                        upc <= alu_pc;
808
                else if ((i_halt)&&(i_dbg_we)
809
                                &&(i_dbg_reg == { 1'b1, `CPU_PC_REG }))
810
                        upc <= i_dbg_data;
811
 
812
        always @(posedge i_clk)
813
                if (i_rst)
814
                        ipc <= RESET_ADDRESS;
815
                else if ((wr_reg_ce)&&(~wr_reg_id[4])&&(wr_write_pc))
816
                        ipc <= wr_reg_vl;
817
                else if ((~alu_gie)&&(alu_pc_valid))
818
                        ipc <= alu_pc;
819
                else if ((i_halt)&&(i_dbg_we)
820
                                &&(i_dbg_reg == { 1'b0, `CPU_PC_REG }))
821
                        ipc <= i_dbg_data;
822
 
823
        always @(posedge i_clk)
824
                if (i_rst)
825
                        pf_pc <= RESET_ADDRESS;
826
                else if (w_switch_to_interrupt)
827
                        pf_pc <= ipc;
828
                else if (w_release_from_interrupt)
829
                        pf_pc <= upc;
830
                else if ((wr_reg_ce)&&(wr_reg_id[4] == gie)&&(wr_write_pc))
831
                        pf_pc <= wr_reg_vl;
832
                else if ((i_halt)&&(i_dbg_we)
833
                                &&(wr_reg_id[4:0] == { gie, `CPU_PC_REG}))
834
                        pf_pc <= i_dbg_data;
835
                else if (dcd_ce)
836
                        pf_pc <= pf_pc + 1;
837
 
838
        initial new_pc = 1'b1;
839
        always @(posedge i_clk)
840 18 dgisselq
                if ((i_rst)||(i_clear_pf_cache))
841 2 dgisselq
                        new_pc <= 1'b1;
842
                else if (w_switch_to_interrupt)
843
                        new_pc <= 1'b1;
844
                else if (w_release_from_interrupt)
845
                        new_pc <= 1'b1;
846
                else if ((wr_reg_ce)&&(wr_reg_id[4] == gie)&&(wr_write_pc))
847
                        new_pc <= 1'b1;
848
                else if ((i_halt)&&(i_dbg_we)
849
                                &&(wr_reg_id[4:0] == { gie, `CPU_PC_REG}))
850
                        new_pc <= 1'b1;
851
                else
852
                        new_pc <= 1'b0;
853
 
854
        //
855
        // The debug interface
856
        always @(posedge i_clk)
857
                begin
858
                        o_dbg_reg <= regset[i_dbg_reg];
859
                        if (i_dbg_reg[3:0] == `CPU_PC_REG)
860
                                o_dbg_reg <= (i_dbg_reg[4])?upc:ipc;
861
                        else if (i_dbg_reg[3:0] == `CPU_CC_REG)
862
                                o_dbg_reg <= { 25'h00, step, gie, sleep,
863
                                        ((i_dbg_reg[4])?flags:iflags) };
864
                end
865
        always @(posedge i_clk)
866 18 dgisselq
                o_dbg_cc <= { break_en, step, gie, sleep };
867
 
868
        always @(posedge i_clk)
869 2 dgisselq
                o_dbg_stall <= (~i_halt)||(pf_cyc)||(mem_cyc)||(mem_busy)
870
                        ||((~opvalid)&&(~i_rst))
871
                        ||((~dcdvalid)&&(~i_rst));
872
 
873
        //
874
        //
875
        // Produce accounting outputs: Account for any CPU stalls, so we can
876
        // later evaluate how well we are doing.
877
        //
878
        //
879 9 dgisselq
        assign  o_op_stall = (master_ce)&&((~opvalid)||(op_stall));
880
        assign  o_pf_stall = (master_ce)&&(~pf_valid);
881
        assign  o_i_count  = alu_pc_valid;
882 2 dgisselq
endmodule

powered by: WebSVN 2.1.0

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