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

Subversion Repositories zipcpu

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

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

powered by: WebSVN 2.1.0

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