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

Subversion Repositories openarty

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

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 50 dgisselq
////////////////////////////////////////////////////////////////////////////////
2 3 dgisselq
//
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 50 dgisselq
////////////////////////////////////////////////////////////////////////////////
21 3 dgisselq
//
22 50 dgisselq
// Copyright (C) 2015-2017, Gisselquist Technology, LLC
23 3 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 50 dgisselq
// You should have received a copy of the GNU General Public License along
35
// with this program.  (It's in the $(ROOT)/doc directory.  Run make with no
36
// target there if the PDF file isn't present.)  If not, see
37
// <http://www.gnu.org/licenses/> for a copy.
38
//
39 3 dgisselq
// License:     GPL, v3, as defined and found on www.gnu.org,
40
//              http://www.gnu.org/licenses/gpl.html
41
//
42
//
43 50 dgisselq
////////////////////////////////////////////////////////////////////////////////
44 3 dgisselq
//
45
//
46
//
47 50 dgisselq
`define CPU_SP_REG      4'hd
48 3 dgisselq
`define CPU_CC_REG      4'he
49
`define CPU_PC_REG      4'hf
50
//
51
`include "cpudefs.v"
52
//
53
//
54
//
55
module  idecode(i_clk, i_rst, i_ce, i_stalled,
56
                i_instruction, i_gie, i_pc, i_pf_valid,
57
                        i_illegal,
58 50 dgisselq
                o_valid,
59 3 dgisselq
                o_phase, o_illegal,
60
                o_pc, o_gie,
61
                o_dcdR, o_dcdA, o_dcdB, o_I, o_zI,
62
                o_cond, o_wF,
63
                o_op, o_ALU, o_M, o_DV, o_FP, o_break, o_lock,
64
                o_wR, o_rA, o_rB,
65
                o_early_branch, o_branch_pc, o_ljmp,
66 50 dgisselq
                o_pipe,
67
                o_sim, o_sim_immv
68 3 dgisselq
                );
69
        parameter       ADDRESS_WIDTH=24, IMPLEMENT_MPY=1, EARLY_BRANCHING=1,
70
                        IMPLEMENT_DIVIDE=1, IMPLEMENT_FPU=0, AW = ADDRESS_WIDTH;
71
        input                   i_clk, i_rst, i_ce, i_stalled;
72
        input   [31:0]           i_instruction;
73
        input                   i_gie;
74
        input   [(AW-1):0]       i_pc;
75
        input                   i_pf_valid, i_illegal;
76 50 dgisselq
        output  wire            o_valid, o_phase;
77 3 dgisselq
        output  reg             o_illegal;
78 50 dgisselq
        output  reg     [AW:0]   o_pc;
79 3 dgisselq
        output  reg             o_gie;
80
        output  reg     [6:0]    o_dcdR, o_dcdA, o_dcdB;
81
        output  wire    [31:0]   o_I;
82
        output  reg             o_zI;
83
        output  reg     [3:0]    o_cond;
84
        output  reg             o_wF;
85
        output  reg     [3:0]    o_op;
86
        output  reg             o_ALU, o_M, o_DV, o_FP, o_break;
87
        output  wire            o_lock;
88
        output  reg             o_wR, o_rA, o_rB;
89
        output  wire            o_early_branch;
90
        output  wire    [(AW-1):0]       o_branch_pc;
91
        output  wire            o_ljmp;
92
        output  wire            o_pipe;
93 50 dgisselq
        output  reg             o_sim           /* verilator public_flat */;
94
        output  reg     [22:0]   o_sim_immv      /* verilator public_flat */;
95 3 dgisselq
 
96
        wire    dcdA_stall, dcdB_stall, dcdF_stall;
97
        wire                    o_dcd_early_branch;
98
        wire    [(AW-1):0]       o_dcd_branch_pc;
99
        reg     o_dcdI, o_dcdIz;
100
`ifdef  OPT_PIPELINED
101
        reg     r_lock;
102
`endif
103
`ifdef  OPT_PIPELINED_BUS_ACCESS
104
        reg     r_pipe;
105
`endif
106
 
107
 
108
        wire    [4:0]    w_op;
109 50 dgisselq
        wire            w_ldi, w_mov, w_cmptst, w_ldilo, w_ALU, w_brev,
110
                        w_noop, w_lock;
111 3 dgisselq
        wire    [4:0]    w_dcdR, w_dcdB, w_dcdA;
112
        wire            w_dcdR_pc, w_dcdR_cc;
113
        wire            w_dcdA_pc, w_dcdA_cc;
114
        wire            w_dcdB_pc, w_dcdB_cc;
115
        wire    [3:0]    w_cond;
116 50 dgisselq
        wire            w_wF, w_mem, w_sto, w_div, w_fpu;
117 3 dgisselq
        wire            w_wR, w_rA, w_rB, w_wR_n;
118 50 dgisselq
        wire            w_ljmp, w_ljmp_dly, w_cis_ljmp;
119 3 dgisselq
        wire    [31:0]   iword;
120
 
121
 
122 50 dgisselq
`ifdef  OPT_CIS
123
        reg     [15:0]   r_nxt_half;
124 3 dgisselq
        assign  iword = (o_phase)
125 50 dgisselq
                                // set second half as a NOOP ... but really
126 3 dgisselq
                                // shouldn't matter
127 50 dgisselq
                        ? { r_nxt_half[15:0], i_instruction[15:0] }
128 3 dgisselq
                        : i_instruction;
129
`else
130
        assign  iword = { 1'b0, i_instruction[30:0] };
131
`endif
132
 
133
        generate
134
        if (EARLY_BRANCHING != 0)
135 50 dgisselq
        begin
136
`ifdef  OPT_CIS
137
                reg     r_pre_ljmp;
138
                always @(posedge i_clk)
139
                if ((i_rst)||(o_early_branch))
140
                        r_pre_ljmp <= 1'b0;
141
                else if ((i_ce)&&(i_pf_valid))
142
                        r_pre_ljmp <= (!o_phase)&&(i_instruction[31])
143
                                &&(i_instruction[14:0] == 15'h7cf8);
144
                else if (i_ce)
145
                        r_pre_ljmp <= 1'b0;
146
 
147
                assign  w_cis_ljmp = r_pre_ljmp;
148
`else
149
                assign  w_cis_ljmp = 1'b0;
150
`endif
151
                // 0.1111.10010.000.1.1111.000000000...
152
                // 0111.1100.1000.0111.11000....
153 3 dgisselq
                assign  w_ljmp = (iword == 32'h7c87c000);
154 50 dgisselq
        end else begin
155
                assign  w_cis_ljmp = 1'b0;
156 3 dgisselq
                assign  w_ljmp = 1'b0;
157 50 dgisselq
        end
158 3 dgisselq
        endgenerate
159
 
160 50 dgisselq
`ifdef  OPT_CIS
161
`ifdef  VERILATOR
162
        wire    [4:0]    w_cis_op;
163
        always @(iword)
164
                if (!iword[31])
165
                        w_cis_op = w_op;
166
                else case(iword[26:24])
167
                3'h0: w_cis_op = 5'h00;
168
                3'h1: w_cis_op = 5'h01;
169
                3'h2: w_cis_op = 5'h02;
170
                3'h3: w_cis_op = 5'h10;
171
                3'h4: w_cis_op = 5'h12;
172
                3'h5: w_cis_op = 5'h13;
173
                3'h6: w_cis_op = 5'h18;
174
                3'h7: w_cis_op = 5'h0d;
175
                endcase
176
`else
177
        reg     [4:0]    w_cis_op;
178
        always @(iword,w_op)
179
                if (!iword[31])
180
                        w_cis_op <= w_op;
181
                else case(iword[26:24])
182
                3'h0: w_cis_op <= 5'h00;
183
                3'h1: w_cis_op <= 5'h01;
184
                3'h2: w_cis_op <= 5'h02;
185
                3'h3: w_cis_op <= 5'h10;
186
                3'h4: w_cis_op <= 5'h12;
187
                3'h5: w_cis_op <= 5'h13;
188
                3'h6: w_cis_op <= 5'h18;
189
                3'h7: w_cis_op <= 5'h0d;
190
                endcase
191
`endif
192
`else
193
        wire    [4:0]    w_cis_op;
194
        assign  w_cis_op = w_op;
195
`endif
196 3 dgisselq
 
197
        assign  w_op= iword[26:22];
198 50 dgisselq
        assign  w_mov    = (w_cis_op      == 5'h0d);
199
        assign  w_ldi    = (w_cis_op[4:1] == 4'hc);
200
        assign  w_brev   = (w_cis_op      == 5'h8);
201
        assign  w_cmptst = (w_cis_op[4:1] == 4'h8);
202
        assign  w_ldilo  = (w_cis_op[4:0] == 5'h9);
203
        assign  w_ALU    = (!w_cis_op[4]) // anything with [4]==0, but ...
204
                                &&(w_cis_op[3:1] != 3'h7); // not the divide
205 3 dgisselq
 
206 50 dgisselq
 
207
        // w_dcdR (4 LUTs)
208 3 dgisselq
        //
209 50 dgisselq
        // What register will we be placing results into (if at all)?
210
        //
211 3 dgisselq
        // Two parts to the result register: the register set, given for
212 49 dgisselq
        // moves in iword[18] but only for the supervisor, and the other
213 3 dgisselq
        // four bits encoded in the instruction.
214
        //
215 50 dgisselq
        assign  w_dcdR = { ((!iword[31])&&(w_mov)&&(~i_gie))?iword[18]:i_gie,
216 3 dgisselq
                                iword[30:27] };
217
        // 2 LUTs
218
        //
219
        // If the result register is either CC or PC, and this would otherwise
220
        // be a floating point instruction with floating point opcode of 0,
221
        // then this is a NOOP.
222 50 dgisselq
        assign  w_lock   = (!iword[31])&&(w_op[4:0]==5'h1d)&&(
223
                                ((IMPLEMENT_FPU>0)&&(w_dcdR[3:1]==3'h7))
224
                                ||(IMPLEMENT_FPU==0));
225
        assign  w_noop   = (!iword[31])&&(w_op[4:0] == 5'h1f)&&(
226 3 dgisselq
                        ((IMPLEMENT_FPU>0)&&(w_dcdR[3:1] == 3'h7))
227
                        ||(IMPLEMENT_FPU==0));
228
 
229 50 dgisselq
        // dcdB - What register is used in the opB?
230
        //
231
        assign w_dcdB[4] = ((!iword[31])&&(w_mov)&&(~i_gie))?iword[13]:i_gie;
232
        assign w_dcdB[3:0]= (iword[31])
233
                                ? (((!iword[23])&&(iword[26:25]==2'b10))
234
                                        ? `CPU_SP_REG : iword[22:19])
235
                                : iword[17:14];
236 3 dgisselq
 
237
        // 0 LUTs
238 50 dgisselq
        assign  w_dcdA = w_dcdR;        // on ZipCPU, A is always result reg
239 3 dgisselq
        // 2 LUTs, 1 delay each
240
        assign  w_dcdR_pc = (w_dcdR == {i_gie, `CPU_PC_REG});
241
        assign  w_dcdR_cc = (w_dcdR == {i_gie, `CPU_CC_REG});
242
        // 0 LUTs
243
        assign  w_dcdA_pc = w_dcdR_pc;
244
        assign  w_dcdA_cc = w_dcdR_cc;
245
        // 2 LUTs, 1 delays each
246 50 dgisselq
        assign  w_dcdB_pc = (w_rB)&&(w_dcdB[3:0] == `CPU_PC_REG);
247
        assign  w_dcdB_cc = (w_rB)&&(w_dcdB[3:0] == `CPU_CC_REG);
248 3 dgisselq
 
249
        // Under what condition will we execute this
250
        // instruction?  Only the load immediate instruction
251
        // is completely unconditional.
252
        //
253
        // 3+4 LUTs
254 50 dgisselq
        assign  w_cond = ((w_ldi)||(iword[31])) ? 4'h8 :
255
                        { (iword[21:19]==3'h0), iword[21:19] };
256 3 dgisselq
 
257
        // 1 LUT
258 50 dgisselq
        assign  w_mem    = (w_cis_op[4:3] == 2'b10)&&(w_cis_op[2:1] !=2'b00);
259
        assign  w_sto     = (w_mem)&&( w_cis_op[0]);
260 3 dgisselq
        // 1 LUT
261 50 dgisselq
        assign  w_div     = (!iword[31])&&(w_op[4:1] == 4'h7);
262
        // 2 LUTs
263
        assign  w_fpu   = (!iword[31])&&(w_op[4:3] == 2'b11)
264
                                &&(w_dcdR[3:1] != 3'h7)&&(w_op[2:1] != 2'b00);
265
        //
266
        // rA - do we need to read register A?
267
        assign  w_rA = // Floating point reads reg A
268
                        ((w_fpu)&&(w_cis_op[4:1] != 4'hf))
269
                        // Divide's read A
270
                        ||(w_div)
271
                        // ALU ops read A,
272
                        //      except for MOV's and BREV's which don't
273
                        ||((w_ALU)&&(!w_brev)&&(!w_mov))
274
                        // STO's read A
275
                        ||(w_sto)
276
                        // Test/compares
277
                        ||(w_cmptst);
278
        // rB -- do we read a register for operand B?  Specifically, do we
279
        // add the registers value to the immediate to create opB?
280
        assign  w_rB     = (w_mov)
281
                                ||((!iword[31])&&(iword[18])&&(!w_ldi))
282
                                ||(( iword[31])&&(iword[23])&&(!w_ldi))
283
                                // If using compressed instruction sets,
284
                                // we *always* read on memory operands.
285
                                ||(( iword[31])&&(w_mem));
286
        // wR -- will we be writing our result back?
287
        // wR_n = !wR
288
        // 1 LUT: All but STO, NOOP/BREAK/LOCK, and CMP/TST write back to w_dcdR
289
        assign  w_wR_n   = (w_sto)
290
                                ||((!iword[31])&&(w_cis_op[4:3]==2'b11)
291
                                        &&(w_cis_op[2:1]!=2'b00)
292
                                        &&(w_dcdR[3:1]==3'h7))
293 49 dgisselq
                                ||(w_cmptst);
294 3 dgisselq
        assign  w_wR     = ~w_wR_n;
295
        //
296 50 dgisselq
        // wF -- do we write flags when we are done?
297
        //
298 3 dgisselq
        assign  w_wF     = (w_cmptst)
299 50 dgisselq
                        ||((w_cond[3])&&((w_fpu)||(w_div)
300
                                ||((w_ALU)&&(!w_mov)&&(!w_ldilo)&&(!w_brev)
301
                                        &&(w_dcdR[3:1] != 3'h7))));
302 3 dgisselq
 
303
        // Bottom 13 bits: no LUT's
304
        // w_dcd[12: 0] -- no LUTs
305
        // w_dcd[   13] -- 2 LUTs
306
        // w_dcd[17:14] -- (5+i0+i1) = 3 LUTs, 1 delay
307
        // w_dcd[22:18] : 5 LUTs, 1 delay (assuming high bit is o/w determined)
308
        reg     [22:0]   r_I;
309
        wire    [22:0]   w_I, w_fullI;
310
        wire            w_Iz;
311
 
312
        assign  w_fullI = (w_ldi) ? { iword[22:0] } // LDI
313 50 dgisselq
                        // MOVE immediates have one less bit
314
                        :((w_mov) ?{ {(23-13){iword[12]}}, iword[12:0] }
315
                        // Normal Op-B immediate ... 18 or 14 bits
316 3 dgisselq
                        :((~iword[18]) ? { {(23-18){iword[17]}}, iword[17:0] }
317
                        : { {(23-14){iword[13]}}, iword[13:0] }
318
                        ));
319
 
320 50 dgisselq
`ifdef  OPT_CIS
321
        wire    [7:0]    w_halfbits;
322
        assign  w_halfbits = iword[23:16];
323
 
324
        wire    [7:0]    w_halfI;
325
        assign  w_halfI = (iword[26:24]==3'h6) ? w_halfbits[7:0]
326
                        :(w_halfbits[7])?
327
                                { {(6){w_halfbits[2]}}, w_halfbits[1:0]}
328
                                :{ w_halfbits[6], w_halfbits[6:0] };
329
        assign  w_I  = (iword[31])?{{(23-8){w_halfI[7]}}, w_halfI }:w_fullI;
330 3 dgisselq
`else
331
        assign  w_I  = w_fullI;
332
`endif
333
        assign  w_Iz = (w_I == 0);
334
 
335
 
336 50 dgisselq
`ifdef  OPT_CIS
337 3 dgisselq
        //
338
        // The o_phase parameter is special.  It needs to let the software
339
        // following know that it cannot break/interrupt on an o_phase asserted
340
        // instruction, lest the break take place between the first and second
341 50 dgisselq
        // half of a CIS instruction.  To do this, o_phase must be asserted
342 3 dgisselq
        // when the first instruction half is valid, but not asserted on either
343
        // a 32-bit instruction or the second half of a 2x16-bit instruction.
344
        reg     r_phase;
345
        initial r_phase = 1'b0;
346
        always @(posedge i_clk)
347
                if ((i_rst) // When no instruction is in the pipe, phase is zero
348
                        ||(o_early_branch)||(w_ljmp_dly))
349
                        r_phase <= 1'b0;
350
                else if ((i_ce)&&(i_pf_valid))
351 50 dgisselq
                        r_phase <= (o_phase)? 1'b0
352
                                : ((i_instruction[31])&&(i_pf_valid));
353
                else if (i_ce)
354
                        r_phase <= 1'b0;
355 3 dgisselq
        // Phase is '1' on the first instruction of a two-part set
356
        // But, due to the delay in processing, it's '1' when our output is
357
        // valid for that first part, but that'll be the same time we
358
        // are processing the second part ... so it may look to us like a '1'
359
        // on the second half of processing.
360
 
361
        assign  o_phase = r_phase;
362
`else
363
        assign  o_phase = 1'b0;
364
`endif
365
 
366
 
367
        initial o_illegal = 1'b0;
368
        always @(posedge i_clk)
369
                if (i_rst)
370
                        o_illegal <= 1'b0;
371
                else if (i_ce)
372
                begin
373 50 dgisselq
`ifdef  OPT_CIS
374 3 dgisselq
                        o_illegal <= (i_illegal);
375
`else
376
                        o_illegal <= ((i_illegal) || (i_instruction[31]));
377
`endif
378 50 dgisselq
                        if ((IMPLEMENT_MPY==0)&&((w_cis_op[4:1]==4'h5)||(w_cis_op[4:0]==5'h0c)))
379 3 dgisselq
                                o_illegal <= 1'b1;
380
 
381 50 dgisselq
                        if ((IMPLEMENT_DIVIDE==0)&&(w_div))
382 3 dgisselq
                                o_illegal <= 1'b1;
383 50 dgisselq
                        else if ((IMPLEMENT_DIVIDE!=0)&&(w_div)&&(w_dcdR[3:1]==3'h7))
384 3 dgisselq
                                o_illegal <= 1'b1;
385
 
386
 
387 50 dgisselq
                        if ((IMPLEMENT_FPU==0)&&(w_fpu))
388 3 dgisselq
                                o_illegal <= 1'b1;
389
 
390 50 dgisselq
                        if ((w_cis_op[4:3]==2'b11)&&(w_cis_op[2:1]!=2'b00)
391
                                &&(w_dcdR[3:1]==3'h7)
392 3 dgisselq
                                &&(
393 50 dgisselq
                                        (w_cis_op[2:0] != 3'h4)  // BREAK
394 3 dgisselq
`ifdef  OPT_PIPELINED
395 50 dgisselq
                                        &&(w_cis_op[2:0] != 3'h5)        // LOCK
396 3 dgisselq
`endif
397 50 dgisselq
                                        // SIM instructions are always illegal
398
                                        &&(w_cis_op[2:0] != 3'h7)))      // NOOP
399 3 dgisselq
                                o_illegal <= 1'b1;
400
                end
401
 
402
 
403
        always @(posedge i_clk)
404
                if (i_ce)
405
                begin
406 50 dgisselq
`ifdef  OPT_CIS
407
                        if (!o_phase)
408
                                o_gie<= i_gie;
409
 
410
                        if (iword[31])
411 3 dgisselq
                        begin
412 50 dgisselq
                                if (o_phase)
413
                                        o_pc <= o_pc + 1'b1;
414
                                else if (i_pf_valid)
415
                                        o_pc <= { i_pc, 1'b1 };
416
                        end else begin
417
                                // The normal, non-CIS case
418
                                o_pc <= { i_pc + 1'b1, 1'b0 };
419 3 dgisselq
                        end
420
`else
421
                        o_gie<= i_gie;
422 50 dgisselq
                        o_pc <= { i_pc + 1'b1, 1'b0 };
423 3 dgisselq
`endif
424
 
425
                        // Under what condition will we execute this
426
                        // instruction?  Only the load immediate instruction
427
                        // is completely unconditional.
428
                        o_cond <= w_cond;
429
                        // Don't change the flags on conditional instructions,
430
                        // UNLESS: the conditional instruction was a CMP
431
                        // or TST instruction.
432
                        o_wF <= w_wF;
433
 
434
                        // Record what operation/op-code (4-bits) we are doing
435
                        //      Note that LDI magically becomes a MOV
436
                        //      instruction here.  That way it's a pass through
437
                        //      the ALU.  Likewise, the two compare instructions
438
                        //      CMP and TST becomes SUB and AND here as well.
439
                        // We keep only the bottom four bits, since we've
440 50 dgisselq
                        // already done the rest of the decode necessary to
441 3 dgisselq
                        // settle between the other instructions.  For example,
442
                        // o_FP plus these four bits uniquely defines the FP
443
                        // instruction, o_DV plus the bottom of these defines
444
                        // the divide, etc.
445 50 dgisselq
                        o_op <= ((w_ldi)||(w_noop))? 4'hd : w_cis_op[3:0];
446 3 dgisselq
 
447
                        // Default values
448
                        o_dcdR <= { w_dcdR_cc, w_dcdR_pc, w_dcdR};
449
                        o_dcdA <= { w_dcdA_cc, w_dcdA_pc, w_dcdA};
450
                        o_dcdB <= { w_dcdB_cc, w_dcdB_pc, w_dcdB};
451
                        o_wR  <= w_wR;
452
                        o_rA  <= w_rA;
453
                        o_rB  <= w_rB;
454
                        r_I    <= w_I;
455
                        o_zI   <= w_Iz;
456
 
457 50 dgisselq
                        // Turn a NOOP into an ALU operation--subtract in
458 3 dgisselq
                        // particular, although it doesn't really matter as long
459
                        // as it doesn't take longer than one clock.  Note
460
                        // also that this depends upon not setting any registers
461
                        // or flags, which should already be true.
462 50 dgisselq
                        o_ALU  <=  (w_ALU)||(w_ldi)||(w_cmptst)||(w_noop);
463
                        o_M    <=  w_mem;
464
                        o_DV   <=  w_div;
465
                        o_FP   <=  w_fpu;
466
 
467
                        o_break <= (!iword[31])&&(w_op[4:0]==5'h1c)&&(
468 3 dgisselq
                                ((IMPLEMENT_FPU>0)&&(w_dcdR[3:1]==3'h7))
469
                                ||(IMPLEMENT_FPU==0));
470
`ifdef  OPT_PIPELINED
471 50 dgisselq
                        r_lock  <= w_lock;
472 3 dgisselq
`endif
473 50 dgisselq
`ifdef  OPT_CIS
474
                        r_nxt_half <= { iword[31], iword[14:0] };
475 3 dgisselq
`endif
476 50 dgisselq
 
477
`ifdef  VERILATOR
478
                        // Support the SIM instruction(s)
479
                        o_sim <= (!iword[31])&&(w_op[4:1] == 4'hf)
480
                                &&(w_dcdR[3:1] == 3'h7);
481
`else
482
                        o_sim <= 1'b0;
483
`endif
484
                        o_sim_immv <= iword[22:0];
485 3 dgisselq
                end
486
 
487
`ifdef  OPT_PIPELINED
488
        assign  o_lock = r_lock;
489
`else
490
        assign  o_lock = 1'b0;
491
`endif
492
 
493
        generate
494
        if (EARLY_BRANCHING!=0)
495
        begin
496
                reg                     r_early_branch, r_ljmp;
497
                reg     [(AW-1):0]       r_branch_pc;
498
 
499
                initial r_ljmp = 1'b0;
500
                always @(posedge i_clk)
501
                        if (i_rst)
502
                                r_ljmp <= 1'b0;
503 50 dgisselq
`ifdef  OPT_CIS
504
                        else if ((i_ce)&&(o_phase))
505
                                r_ljmp <= w_cis_ljmp;
506
`endif
507 3 dgisselq
                        else if ((i_ce)&&(i_pf_valid))
508
                                r_ljmp <= (w_ljmp);
509
                assign  o_ljmp = r_ljmp;
510
 
511
                always @(posedge i_clk)
512
                if (i_rst)
513
                        r_early_branch <= 1'b0;
514
                else if ((i_ce)&&(i_pf_valid))
515
                begin
516
                        if (r_ljmp)
517
                                // LOD (PC),PC
518
                                r_early_branch <= 1'b1;
519 50 dgisselq
                        else if ((!iword[31])&&(iword[30:27]==`CPU_PC_REG)
520
                                        &&(w_cond[3]))
521 3 dgisselq
                        begin
522 50 dgisselq
                                if ((w_op[4:0]==5'h02)&&(!iword[18]))
523 3 dgisselq
                                        // Add x,PC
524
                                        r_early_branch     <= 1'b1;
525 50 dgisselq
                                else
526 3 dgisselq
                                        r_early_branch     <= 1'b0;
527
                        end else
528
                                r_early_branch <= 1'b0;
529
                end else if (i_ce)
530
                        r_early_branch <= 1'b0;
531
 
532
                always @(posedge i_clk)
533
                        if (i_ce)
534
                        begin
535
                                if (r_ljmp)
536 50 dgisselq
                                        r_branch_pc <= iword[(AW+1):2];
537 3 dgisselq
                                else // Add x,PC
538
                                r_branch_pc <= i_pc
539 50 dgisselq
                                        + {{(AW-15){iword[17]}},iword[16:2]}
540 3 dgisselq
                                        + {{(AW-1){1'b0}},1'b1};
541
                        end
542
 
543
                assign  w_ljmp_dly         = r_ljmp;
544
                assign  o_early_branch     = r_early_branch;
545
                assign  o_branch_pc        = r_branch_pc;
546
        end else begin
547
                assign  w_ljmp_dly         = 1'b0;
548
                assign  o_early_branch = 1'b0;
549
                assign  o_branch_pc = {(AW){1'b0}};
550
                assign  o_ljmp = 1'b0;
551
        end endgenerate
552
 
553
 
554
        // To be a pipeable operation there must be ...
555
        //      1. Two valid adjacent instructions
556
        //      2. Both must be memory operations, of the same time (both lods
557
        //              or both stos)
558
        //      3. Both must use the same register base address
559
        //      4. Both must be to the same address, or the address incremented
560
        //              by one
561
        // Note that we're not using iword here ... there's a lot of logic
562
        // taking place, and it's only valid if the new word is not compressed.
563
        //
564
        reg     r_valid;
565
`ifdef  OPT_PIPELINED_BUS_ACCESS
566
        initial r_pipe = 1'b0;
567
        always @(posedge i_clk)
568
                if (i_ce)
569 50 dgisselq
                        r_pipe <= (r_valid)&&((i_pf_valid)||(o_phase))
570
                                // Both must be memory operations
571
                                &&(w_mem)&&(o_M)
572
                                // Both must be writes, or both stores
573
                                &&(o_op[0] == w_cis_op[0])
574
                                // Both must be register ops
575
                                &&(w_rB)
576
                                // Both must use the same register for B
577
                                &&(w_dcdB[3:0] == o_dcdB[3:0])
578
                                // But ... the result can never be B
579
                                &&((o_op[0])
580
                                        ||(w_dcdB[3:0] != o_dcdA[3:0]))
581
                                // Needs to be to the mode, supervisor or user
582 3 dgisselq
                                &&(i_gie == o_gie)
583 50 dgisselq
                                // Same condition, or no condition before
584 3 dgisselq
                                &&((i_instruction[21:19]==o_cond[2:0])
585
                                        ||(o_cond[2:0] == 3'h0))
586 50 dgisselq
                                // Same immediate
587
                                &&((w_I[13:2]==r_I[13:2])
588
                                        ||({1'b0, w_I[13:2]}==(r_I[13:2]+12'h1)));
589 3 dgisselq
        assign o_pipe = r_pipe;
590
`else
591
        assign o_pipe = 1'b0;
592
`endif
593
 
594
        always @(posedge i_clk)
595
                if (i_rst)
596
                        r_valid <= 1'b0;
597 50 dgisselq
                else if (i_ce)
598
                        r_valid <= ((i_pf_valid)||(o_phase)||(i_illegal))
599
                                        &&(!o_ljmp)&&(!o_early_branch);
600
                else if (!i_stalled)
601 3 dgisselq
                        r_valid <= 1'b0;
602
 
603 50 dgisselq
        assign  o_valid = r_valid;
604
 
605
 
606 3 dgisselq
        assign  o_I = { {(32-22){r_I[22]}}, r_I[21:0] };
607
 
608
endmodule

powered by: WebSVN 2.1.0

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