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

Subversion Repositories zipcpu

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

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

Line No. Rev Author Line
1 69 dgisselq
///////////////////////////////////////////////////////////////////////////////
2
//
3
// Filename:    idecode.v
4
//
5
// Project:     Zip CPU -- a small, lightweight, RISC CPU soft core
6
//
7
// Purpose:     This RTL file specifies how instructions are to be decoded
8
//              into their underlying meanings.  This is specifically a version
9
//      designed to support a "Next Generation", or "Version 2" instruction
10
//      set as (currently) activated by the OPT_NEW_INSTRUCTION_SET option
11
//      in cpudefs.v.
12
//
13
//      I expect to (eventually) retire the old instruction set, at which point
14
//      this will become the default instruction set decoder.
15
//
16
//
17
// Creator:     Dan Gisselquist, Ph.D.
18
//              Gisselquist Technology, LLC
19
//
20
///////////////////////////////////////////////////////////////////////////////
21
//
22
// Copyright (C) 2015, Gisselquist Technology, LLC
23
//
24
// This program is free software (firmware): you can redistribute it and/or
25
// modify it under the terms of  the GNU General Public License as published
26
// by the Free Software Foundation, either version 3 of the License, or (at
27
// your option) any later version.
28
//
29
// This program is distributed in the hope that it will be useful, but WITHOUT
30
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
31
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
32
// for more details.
33
//
34
// License:     GPL, v3, as defined and found on www.gnu.org,
35
//              http://www.gnu.org/licenses/gpl.html
36
//
37
//
38
///////////////////////////////////////////////////////////////////////////////
39
//
40
//
41
//
42
`define CPU_CC_REG      4'he
43
`define CPU_PC_REG      4'hf
44
//
45
`include "cpudefs.v"
46
//
47
//
48
//
49
module  idecode(i_clk, i_rst, i_ce, i_stalled,
50
                i_instruction, i_gie, i_pc, i_pf_valid,
51
                        i_illegal,
52
                o_phase, o_illegal,
53
                o_pc, o_gie,
54
                o_dcdR, o_dcdA, o_dcdB, o_I, o_zI,
55
                o_cond, o_wF,
56
                o_op, o_ALU, o_M, o_DV, o_FP, o_break, o_lock,
57
                o_wR, o_rA, o_rB,
58 105 dgisselq
                o_early_branch, o_branch_pc, o_ljmp,
59 71 dgisselq
                o_pipe
60 69 dgisselq
                );
61
        parameter       ADDRESS_WIDTH=24, IMPLEMENT_MPY=1, EARLY_BRANCHING=1,
62
                        IMPLEMENT_DIVIDE=1, IMPLEMENT_FPU=0, AW = ADDRESS_WIDTH;
63
        input                   i_clk, i_rst, i_ce, i_stalled;
64
        input   [31:0]           i_instruction;
65
        input                   i_gie;
66
        input   [(AW-1):0]       i_pc;
67
        input                   i_pf_valid, i_illegal;
68
        output  wire            o_phase;
69
        output  reg             o_illegal;
70
        output  reg     [(AW-1):0]       o_pc;
71
        output  reg             o_gie;
72
        output  reg     [6:0]    o_dcdR, o_dcdA, o_dcdB;
73
        output  wire    [31:0]   o_I;
74
        output  reg             o_zI;
75
        output  reg     [3:0]    o_cond;
76
        output  reg             o_wF;
77
        output  reg     [3:0]    o_op;
78 130 dgisselq
        output  reg             o_ALU, o_M, o_DV, o_FP, o_break;
79
        output  wire            o_lock;
80 69 dgisselq
        output  reg             o_wR, o_rA, o_rB;
81
        output  wire            o_early_branch;
82
        output  wire    [(AW-1):0]       o_branch_pc;
83 105 dgisselq
        output  wire            o_ljmp;
84 138 dgisselq
        output  wire            o_pipe;
85 69 dgisselq
 
86
        wire    dcdA_stall, dcdB_stall, dcdF_stall;
87
        wire                    o_dcd_early_branch;
88
        wire    [(AW-1):0]       o_dcd_branch_pc;
89
        reg     o_dcdI, o_dcdIz;
90 130 dgisselq
`ifdef  OPT_PIPELINED
91 138 dgisselq
        reg     r_lock, r_pipe;
92 130 dgisselq
`endif
93 69 dgisselq
 
94
 
95
        wire    [4:0]    w_op;
96 138 dgisselq
        wire            w_ldi, w_mov, w_cmptst, w_ldilo, w_ALU, w_brev, w_noop;
97 69 dgisselq
        wire    [4:0]    w_dcdR, w_dcdB, w_dcdA;
98
        wire            w_dcdR_pc, w_dcdR_cc;
99
        wire            w_dcdA_pc, w_dcdA_cc;
100
        wire            w_dcdB_pc, w_dcdB_cc;
101
        wire    [3:0]    w_cond;
102
        wire            w_wF, w_dcdM, w_dcdDV, w_dcdFP;
103
        wire            w_wR, w_rA, w_rB, w_wR_n;
104 105 dgisselq
        wire            w_ljmp;
105 118 dgisselq
        wire    [31:0]   iword;
106 69 dgisselq
 
107
 
108
`ifdef  OPT_VLIW
109
        reg     [16:0]   r_nxt_half;
110
        assign  iword = (o_phase)
111
                                // set second half as a NOOP ... but really 
112
                                // shouldn't matter
113
                        ? { r_nxt_half[16:7], 1'b0, r_nxt_half[6:0], 5'b11000, 3'h7, 6'h00 }
114
                        : i_instruction;
115
`else
116
        assign  iword = { 1'b0, i_instruction[30:0] };
117
`endif
118
 
119 118 dgisselq
        generate
120
        if (EARLY_BRANCHING != 0)
121
                assign  w_ljmp = (iword == 32'h7c87c000);
122
        else
123
                assign  w_ljmp = 1'b0;
124
        endgenerate
125
 
126
 
127 69 dgisselq
        assign  w_op= iword[26:22];
128
        assign  w_mov    = (w_op      == 5'h0f);
129
        assign  w_ldi    = (w_op[4:1] == 4'hb);
130 130 dgisselq
        assign  w_brev   = (w_op      == 5'hc);
131 69 dgisselq
        assign  w_cmptst = (w_op[4:1] == 4'h8);
132 130 dgisselq
        assign  w_ldilo  = (w_op[4:0] == 5'h9);
133 69 dgisselq
        assign  w_ALU    = (~w_op[4]);
134
 
135
        // 4 LUTs
136 138 dgisselq
        //
137
        // Two parts to the result register: the register set, given for
138
        // moves in i_word[18] but only for the supervisor, and the other
139
        // four bits encoded in the instruction.
140
        //
141 69 dgisselq
        assign  w_dcdR = { ((~iword[31])&&(w_mov)&&(~i_gie))?iword[18]:i_gie,
142
                                iword[30:27] };
143 138 dgisselq
        // 2 LUTs
144
        //
145
        // If the result register is either CC or PC, and this would otherwise
146
        // be a floating point instruction with floating point opcode of 0,
147
        // then this is a NOOP.
148
        assign  w_noop   = (w_op[4:0] == 5'h18)&&(w_dcdR[3:1] == 3'h7);
149
 
150 69 dgisselq
        // 4 LUTs
151
        assign  w_dcdB = { ((~iword[31])&&(w_mov)&&(~i_gie))?iword[13]:i_gie,
152
                                iword[17:14] };
153
 
154
        // 0 LUTs
155
        assign  w_dcdA = w_dcdR;
156
        // 2 LUTs, 1 delay each
157 105 dgisselq
        assign  w_dcdR_pc = (w_dcdR == {i_gie, `CPU_PC_REG});
158 69 dgisselq
        assign  w_dcdR_cc = (w_dcdR == {i_gie, `CPU_CC_REG});
159
        // 0 LUTs
160
        assign  w_dcdA_pc = w_dcdR_pc;
161
        assign  w_dcdA_cc = w_dcdR_cc;
162
        // 2 LUTs, 1 delays each
163
        assign  w_dcdB_pc = (w_dcdB[3:0] == `CPU_PC_REG);
164
        assign  w_dcdB_cc = (w_dcdB[3:0] == `CPU_CC_REG);
165
 
166
        // Under what condition will we execute this
167
        // instruction?  Only the load immediate instruction
168
        // is completely unconditional.
169
        //
170
        // 3+4 LUTs
171
        assign  w_cond = (w_ldi) ? 4'h8 :
172
                        (iword[31])?{(iword[20:19]==2'b00),
173
                                        1'b0,iword[20:19]}
174
                        : { (iword[21:19]==3'h0), iword[21:19] };
175
 
176
        // 1 LUT
177
        assign  w_dcdM    = (w_op[4:1] == 4'h9);
178
        // 1 LUT
179
        assign  w_dcdDV   = (w_op[4:1] == 4'ha);
180
        // 1 LUT
181
        assign  w_dcdFP   = (w_op[4:3] == 2'b11)&&(w_dcdR[3:1] != 3'h7);
182
        // 4 LUT's--since it depends upon FP/NOOP condition (vs 1 before)
183
        //      Everything reads A but ... NOOP/BREAK/LOCK, LDI, LOD, MOV
184
        assign  w_rA     = (w_dcdFP)
185
                                // Divide's read A
186
                                ||(w_dcdDV)
187
                                // ALU read's A, unless it's a MOV to A
188
                                // This includes LDIHI/LDILO
189
                                ||((~w_op[4])&&(w_op[3:0]!=4'hf))
190
                                // STO's read A
191
                                ||((w_dcdM)&&(w_op[0]))
192
                                // Test/compares
193
                                ||(w_op[4:1]== 4'h8);
194
        // 1 LUTs -- do we read a register for operand B?  Specifically, do
195
        // we need to stall if the register is not (yet) ready?
196 130 dgisselq
        assign  w_rB     = (w_mov)||((iword[18])&&(~w_ldi));
197 69 dgisselq
        // 1 LUT: All but STO, NOOP/BREAK/LOCK, and CMP/TST write back to w_dcdR
198
        assign  w_wR_n   = ((w_dcdM)&&(w_op[0]))
199
                                ||((w_op[4:3]==2'b11)&&(w_dcdR[3:1]==3'h7))
200
                                ||(w_cmptst);
201
        assign  w_wR     = ~w_wR_n;
202 90 dgisselq
        //
203
        // 1-output bit (5 Opcode bits, 4 out-reg bits, 3 condition bits)
204 69 dgisselq
        //      
205
        //      This'd be 4 LUTs, save that we have the carve out for NOOPs
206 90 dgisselq
        //      and writes to the PC/CC register(s).
207 69 dgisselq
        assign  w_wF     = (w_cmptst)
208
                        ||((w_cond[3])&&((w_dcdFP)||(w_dcdDV)
209 130 dgisselq
                                ||((w_ALU)&&(~w_mov)&&(~w_ldilo)&&(~w_brev)
210 90 dgisselq
                                        &&(iword[30:28] != 3'h7))));
211 69 dgisselq
 
212
        // Bottom 13 bits: no LUT's
213
        // w_dcd[12: 0] -- no LUTs
214
        // w_dcd[   13] -- 2 LUTs
215
        // w_dcd[17:14] -- (5+i0+i1) = 3 LUTs, 1 delay
216
        // w_dcd[22:18] : 5 LUTs, 1 delay (assuming high bit is o/w determined)
217
        reg     [22:0]   r_I;
218
        wire    [22:0]   w_I, w_fullI;
219
        wire            w_Iz;
220
 
221
        assign  w_fullI = (w_ldi) ? { iword[22:0] } // LDI
222
                        :((w_mov) ?{ {(23-13){iword[12]}}, iword[12:0] } // Move
223
                        :((~iword[18]) ? { {(23-18){iword[17]}}, iword[17:0] }
224
                        : { {(23-14){iword[13]}}, iword[13:0] }
225
                        ));
226
 
227
`ifdef  OPT_VLIW
228
        wire    [5:0]    w_halfI;
229
        assign  w_halfI = (w_ldi) ? iword[5:0]
230
                                :((iword[5]) ? 6'h00 : {iword[4],iword[4:0]});
231
        assign  w_I  = (iword[31])? {{(23-6){w_halfI[5]}}, w_halfI }:w_fullI;
232
`else
233
        assign  w_I  = w_fullI;
234
`endif
235
        assign  w_Iz = (w_I == 0);
236
 
237
 
238
`ifdef  OPT_VLIW
239
        //
240
        // The o_phase parameter is special.  It needs to let the software
241
        // following know that it cannot break/interrupt on an o_phase asserted
242
        // instruction, lest the break take place between the first and second
243
        // half of a VLIW instruction.  To do this, o_phase must be asserted
244
        // when the first instruction half is valid, but not asserted on either
245
        // a 32-bit instruction or the second half of a 2x16-bit instruction.
246
        reg     r_phase;
247
        initial r_phase = 1'b0;
248
        always @(posedge i_clk)
249
                if (i_rst) // When no instruction is in the pipe, phase is zero
250
                        r_phase <= 1'b0;
251
                else if (i_ce)
252
                        r_phase <= (o_phase)? 1'b0:(i_instruction[31]);
253
        // Phase is '1' on the first instruction of a two-part set
254
        // But, due to the delay in processing, it's '1' when our output is
255
        // valid for that first part, but that'll be the same time we
256
        // are processing the second part ... so it may look to us like a '1'
257
        // on the second half of processing.
258
 
259
        assign  o_phase = r_phase;
260
`else
261
        assign  o_phase = 1'b0;
262
`endif
263
 
264
 
265 71 dgisselq
        initial o_illegal = 1'b0;
266 69 dgisselq
        always @(posedge i_clk)
267 71 dgisselq
                if (i_rst)
268
                        o_illegal <= 1'b0;
269
                else if (i_ce)
270 69 dgisselq
                begin
271
`ifdef  OPT_VLIW
272 71 dgisselq
                        o_illegal <= (i_illegal);
273 69 dgisselq
`else
274
                        o_illegal <= ((i_illegal) || (i_instruction[31]));
275
`endif
276
                        if ((IMPLEMENT_MPY!=1)&&(w_op[4:1]==4'h5))
277
                                o_illegal <= 1'b1;
278
 
279
                        if ((IMPLEMENT_DIVIDE==0)&&(w_dcdDV))
280
                                o_illegal <= 1'b1;
281
                        else if ((IMPLEMENT_DIVIDE!=0)&&(w_dcdDV)&&(w_dcdR[3:1]==3'h7))
282
                                o_illegal <= 1'b1;
283
 
284
 
285
                        if ((IMPLEMENT_FPU!=0)&&(w_dcdFP)&&(w_dcdR[3:1]==3'h7))
286
                                o_illegal <= 1'b1;
287
                        else if ((IMPLEMENT_FPU==0)&&(w_dcdFP))
288
                                o_illegal <= 1'b1;
289
 
290 71 dgisselq
                        if ((w_op[4:3]==2'b11)&&(w_dcdR[3:1]==3'h7)
291
                                &&(
292 130 dgisselq
                                        (w_op[2:0] != 3'h1)      // BREAK
293
`ifdef  OPT_PIPELINED
294
                                        &&(w_op[2:0] != 3'h2)    // LOCK
295
`endif
296 71 dgisselq
                                        &&(w_op[2:0] != 3'h0)))  // NOOP
297
                                o_illegal <= 1'b1;
298
                end
299
 
300
 
301
        always @(posedge i_clk)
302
                if (i_ce)
303
                begin
304
`ifdef  OPT_VLIW
305
                        if (~o_phase)
306
                        begin
307
                                o_gie<= i_gie;
308
                                // i.e. dcd_pc+1
309
                                o_pc <= i_pc+{{(AW-1){1'b0}},1'b1};
310
                        end
311
`else
312
                        o_gie<= i_gie;
313
                        o_pc <= i_pc+{{(AW-1){1'b0}},1'b1};
314
`endif
315
 
316 69 dgisselq
                        // Under what condition will we execute this
317
                        // instruction?  Only the load immediate instruction
318
                        // is completely unconditional.
319
                        o_cond <= w_cond;
320
                        // Don't change the flags on conditional instructions,
321
                        // UNLESS: the conditional instruction was a CMP
322
                        // or TST instruction.
323
                        o_wF <= w_wF;
324
 
325
                        // Record what operation/op-code (4-bits) we are doing
326
                        //      Note that LDI magically becomes a MOV
327
                        //      instruction here.  That way it's a pass through
328
                        //      the ALU.  Likewise, the two compare instructions
329
                        //      CMP and TST becomes SUB and AND here as well.
330
                        // We keep only the bottom four bits, since we've
331
                        // already done the rest of the decode necessary to 
332
                        // settle between the other instructions.  For example,
333
                        // o_FP plus these four bits uniquely defines the FP
334
                        // instruction, o_DV plus the bottom of these defines
335
                        // the divide, etc.
336 138 dgisselq
                        o_op <= (w_ldi)||(w_noop)? 4'hf:w_op[3:0];
337 69 dgisselq
 
338
                        // Default values
339
                        o_dcdR <= { w_dcdR_cc, w_dcdR_pc, w_dcdR};
340
                        o_dcdA <= { w_dcdA_cc, w_dcdA_pc, w_dcdA};
341
                        o_dcdB <= { w_dcdB_cc, w_dcdB_pc, w_dcdB};
342
                        o_wR  <= w_wR;
343
                        o_rA  <= w_rA;
344
                        o_rB  <= w_rB;
345
                        r_I    <= w_I;
346
                        o_zI   <= w_Iz;
347
 
348 138 dgisselq
                        // Turn a NOOP into an ALU operation--subtract in 
349
                        // particular, although it doesn't really matter as long
350
                        // as it doesn't take longer than one clock.  Note
351
                        // also that this depends upon not setting any registers
352
                        // or flags, which should already be true.
353
                        o_ALU  <=  (w_ALU)||(w_ldi)||(w_cmptst)||(w_noop); // 2 LUT
354 69 dgisselq
                        o_M    <=  w_dcdM;
355
                        o_DV   <=  w_dcdDV;
356
                        o_FP   <=  w_dcdFP;
357
 
358
                        o_break <= (w_op[4:3]==2'b11)&&(w_dcdR[3:1]==3'h7)&&(w_op[2:0]==3'b001);
359 130 dgisselq
`ifdef  OPT_PIPELINED
360
                        r_lock  <= (w_op[4:3]==2'b11)&&(w_dcdR[3:1]==3'h7)&&(w_op[2:0]==3'b010);
361
`endif
362 69 dgisselq
`ifdef  OPT_VLIW
363
                        r_nxt_half <= { iword[31], iword[13:5],
364
                                ((iword[21])? iword[20:19] : 2'h0),
365
                                iword[4:0] };
366
`endif
367
                end
368
 
369 130 dgisselq
`ifdef  OPT_PIPELINED
370
        assign  o_lock = r_lock;
371
`else
372
        assign  o_lock = 1'b0;
373
`endif
374
 
375 69 dgisselq
        generate
376
        if (EARLY_BRANCHING!=0)
377
        begin
378 105 dgisselq
                reg                     r_early_branch, r_ljmp;
379 69 dgisselq
                reg     [(AW-1):0]       r_branch_pc;
380 105 dgisselq
 
381
                initial r_ljmp = 1'b0;
382 69 dgisselq
                always @(posedge i_clk)
383 105 dgisselq
                        if (i_rst)
384
                                r_ljmp <= 1'b0;
385
                        else if ((i_ce)&&(i_pf_valid))
386
                                r_ljmp <= (w_ljmp);
387
                assign  o_ljmp = r_ljmp;
388
 
389
                always @(posedge i_clk)
390
                if (i_rst)
391
                        r_early_branch <= 1'b0;
392
                else if ((i_ce)&&(i_pf_valid))
393 90 dgisselq
                begin
394 105 dgisselq
                        if (r_ljmp)
395
                                // LOD (PC),PC
396
                                r_early_branch <= 1'b1;
397
                        else if ((~iword[31])&&(iword[30:27]==`CPU_PC_REG)&&(w_cond[3]))
398 69 dgisselq
                        begin
399 90 dgisselq
                                if (w_op[4:1] == 4'hb) // LDI to PC
400 105 dgisselq
                                        // LDI x,PC
401 69 dgisselq
                                        r_early_branch     <= 1'b1;
402 105 dgisselq
                                else if ((w_op[4:0]==5'h02)&&(~iword[18]))
403
                                        // Add x,PC
404 69 dgisselq
                                        r_early_branch     <= 1'b1;
405 105 dgisselq
                                else begin
406 69 dgisselq
                                        r_early_branch     <= 1'b0;
407
                                end
408 90 dgisselq
                        end else
409
                                r_early_branch <= 1'b0;
410 105 dgisselq
                end else if (i_ce)
411
                        r_early_branch <= 1'b0;
412
 
413 69 dgisselq
                always @(posedge i_clk)
414
                        if (i_ce)
415
                        begin
416 105 dgisselq
                                if (r_ljmp)
417
                                        r_branch_pc <= iword[(AW-1):0];
418
                                else if (w_op[4:1] == 4'hb) // LDI
419 90 dgisselq
                                        r_branch_pc <= {{(AW-23){iword[22]}},iword[22:0]};
420
                                else // Add x,PC
421
                                r_branch_pc <= i_pc
422 105 dgisselq
                                        + {{(AW-17){iword[17]}},iword[16:0]}
423 90 dgisselq
                                        + {{(AW-1){1'b0}},1'b1};
424 69 dgisselq
                        end
425
 
426
                assign  o_early_branch     = r_early_branch;
427
                assign  o_branch_pc        = r_branch_pc;
428
        end else begin
429
                assign  o_early_branch = 1'b0;
430
                assign  o_branch_pc = {(AW){1'b0}};
431 105 dgisselq
                assign  o_ljmp = 1'b0;
432 69 dgisselq
        end endgenerate
433
 
434 71 dgisselq
 
435
        // To be a pipeable operation there must be ...
436
        //      1. Two valid adjacent instructions
437
        //      2. Both must be memory operations, of the same time (both lods
438
        //              or both stos)
439
        //      3. Both must use the same register base address
440
        //      4. Both must be to the same address, or the address incremented
441
        //              by one
442
        // Note that we're not using iword here ... there's a lot of logic
443
        // taking place, and it's only valid if the new word is not compressed.
444
        //
445
        reg     r_valid;
446 138 dgisselq
`ifdef  OPT_PIPELINED
447
        initial r_pipe = 1'b0;
448 71 dgisselq
        always @(posedge i_clk)
449
                if (i_ce)
450 138 dgisselq
                        r_pipe <= (r_valid)&&(i_pf_valid)&&(~i_instruction[31])
451 71 dgisselq
                                &&(w_dcdM)&&(o_M)&&(o_op[0] ==i_instruction[22])
452
                                &&(i_instruction[17:14] == o_dcdB[3:0])
453 130 dgisselq
                                &&(i_instruction[17:14] != o_dcdA[3:0])
454 71 dgisselq
                                &&(i_gie == o_gie)
455
                                &&((i_instruction[21:19]==o_cond[2:0])
456
                                        ||(o_cond[2:0] == 3'h0))
457
                                &&((i_instruction[13:0]==r_I[13:0])
458
                                        ||({1'b0, i_instruction[13:0]}==(r_I[13:0]+14'h1)));
459 138 dgisselq
        assign o_pipe = r_pipe;
460
`else
461
        assign o_pipe = 1'b0;
462
`endif
463
 
464 71 dgisselq
        always @(posedge i_clk)
465
                if (i_rst)
466
                        r_valid <= 1'b0;
467 105 dgisselq
                else if ((i_ce)&&(o_ljmp))
468
                        r_valid <= 1'b0;
469 71 dgisselq
                else if ((i_ce)&&(i_pf_valid))
470
                        r_valid <= 1'b1;
471
                else if (~i_stalled)
472
                        r_valid <= 1'b0;
473
 
474
 
475 69 dgisselq
        assign  o_I = { {(32-22){r_I[22]}}, r_I[21:0] };
476
 
477
endmodule

powered by: WebSVN 2.1.0

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