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

Subversion Repositories xulalx25soc

[/] [xulalx25soc/] [trunk/] [rtl/] [cpu/] [idecode.v] - Blame information for rev 51

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

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

powered by: WebSVN 2.1.0

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