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

Subversion Repositories openarty

[/] [openarty/] [trunk/] [rtl/] [cpu/] [idecode.v] - Blame information for rev 49

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

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

powered by: WebSVN 2.1.0

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