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

Subversion Repositories zipcpu

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

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

powered by: WebSVN 2.1.0

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