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

Subversion Repositories zipcpu

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

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

powered by: WebSVN 2.1.0

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