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

Subversion Repositories amber

[/] [amber/] [trunk/] [hw/] [vlog/] [amber23/] [a23_decode.v] - Blame information for rev 88

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

Line No. Rev Author Line
1 2 csantifort
//////////////////////////////////////////////////////////////////
2
//                                                              //
3
//  Decode stage of Amber 2 Core                                //
4
//                                                              //
5
//  This file is part of the Amber project                      //
6
//  http://www.opencores.org/project,amber                      //
7
//                                                              //
8
//  Description                                                 //
9
//  This module is the most complex part of the Amber core      //
10
//  It decodes and sequences all instructions and handles all   //
11
//  interrupts                                                  //
12
//                                                              //
13
//  Author(s):                                                  //
14
//      - Conor Santifort, csantifort.amber@gmail.com           //
15
//                                                              //
16
//////////////////////////////////////////////////////////////////
17
//                                                              //
18
// Copyright (C) 2010 Authors and OPENCORES.ORG                 //
19
//                                                              //
20
// This source file may be used and distributed without         //
21
// restriction provided that this copyright statement is not    //
22
// removed from the file and that any derivative work contains  //
23
// the original copyright notice and the associated disclaimer. //
24
//                                                              //
25
// This source file is free software; you can redistribute it   //
26
// and/or modify it under the terms of the GNU Lesser General   //
27
// Public License as published by the Free Software Foundation; //
28
// either version 2.1 of the License, or (at your option) any   //
29
// later version.                                               //
30
//                                                              //
31
// This source is distributed in the hope that it will be       //
32
// useful, but WITHOUT ANY WARRANTY; without even the implied   //
33
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
34
// PURPOSE.  See the GNU Lesser General Public License for more //
35
// details.                                                     //
36
//                                                              //
37
// You should have received a copy of the GNU Lesser General    //
38
// Public License along with this source; if not, download it   //
39
// from http://www.opencores.org/lgpl.shtml                     //
40
//                                                              //
41
//////////////////////////////////////////////////////////////////
42 82 csantifort
`include "global_defines.vh"
43 2 csantifort
 
44 15 csantifort
module a23_decode
45 2 csantifort
(
46
input                       i_clk,
47
input       [31:0]          i_read_data,
48
input                       i_fetch_stall,                  // stall all stages of the cpu at the same time
49
input                       i_irq,                          // interrupt request
50
input                       i_firq,                         // Fast interrupt request
51
input                       i_dabt,                         // data abort interrupt request
52
input                       i_iabt,                         // instruction pre-fetch abort flag
53
input                       i_adex,                         // Address Exception
54
input       [31:0]          i_execute_address,              // Registered address output by execute stage
55
                                                            // 2 LSBs of read address used for calculating 
56
                                                            // shift in LDRB ops
57
input       [7:0]           i_abt_status,                   // Abort status
58
input       [31:0]          i_execute_status_bits,          // current status bits values in execute stage
59
input                       i_multiply_done,                // multiply unit is nearly done
60
 
61
 
62
// --------------------------------------------------
63
// Control signals to execute stage
64
// --------------------------------------------------
65
output reg  [31:0]          o_read_data = 1'd0,
66
output reg  [4:0]           o_read_data_alignment = 1'd0,  // 2 LSBs of read address used for calculating shift in LDRB ops
67
 
68
output reg  [31:0]          o_imm32 = 'd0,
69
output reg  [4:0]           o_imm_shift_amount = 'd0,
70
output reg                  o_shift_imm_zero = 'd0,
71
output reg  [3:0]           o_condition = 4'he,             // 4'he = al
72
output reg                  o_exclusive_exec = 'd0,         // exclusive access request ( swap instruction )
73
output reg                  o_data_access_exec = 'd0,       // high means the memory access is a read 
74
                                                            // read or write, low for instruction
75
output reg  [1:0]           o_status_bits_mode = 2'b11,     // SVC
76
output reg                  o_status_bits_irq_mask = 1'd1,
77
output reg                  o_status_bits_firq_mask = 1'd1,
78
 
79
output reg  [3:0]           o_rm_sel = 'd0,
80
output reg  [3:0]           o_rds_sel = 'd0,
81
output reg  [3:0]           o_rn_sel = 'd0,
82 71 csantifort
output      [3:0]           o_rm_sel_nxt,
83
output      [3:0]           o_rds_sel_nxt,
84
output      [3:0]           o_rn_sel_nxt,
85 2 csantifort
output reg  [1:0]           o_barrel_shift_amount_sel = 'd0,
86
output reg  [1:0]           o_barrel_shift_data_sel = 'd0,
87
output reg  [1:0]           o_barrel_shift_function = 'd0,
88
output reg  [8:0]           o_alu_function = 'd0,
89 83 csantifort
output reg                  o_use_carry_in = 'd0,
90 2 csantifort
output reg  [1:0]           o_multiply_function = 'd0,
91
output reg  [2:0]           o_interrupt_vector_sel = 'd0,
92
output reg  [3:0]           o_address_sel = 4'd2,
93
output reg  [1:0]           o_pc_sel = 2'd2,
94
output reg  [1:0]           o_byte_enable_sel = 'd0,        // byte, halfword or word write
95
output reg  [2:0]           o_status_bits_sel = 'd0,
96
output reg  [2:0]           o_reg_write_sel,
97
output reg                  o_user_mode_regs_load,
98
output reg                  o_user_mode_regs_store_nxt,
99
output reg                  o_firq_not_user_mode,
100
 
101
output reg                  o_write_data_wen = 'd0,
102
output reg                  o_base_address_wen = 'd0,       // save LDM base address register
103
                                                            // in case of data abort
104
output reg                  o_pc_wen = 1'd1,
105
output reg  [14:0]          o_reg_bank_wen = 'd0,
106 71 csantifort
output reg  [3:0]           o_reg_bank_wsel = 'd0,
107 2 csantifort
output reg                  o_status_bits_flags_wen = 'd0,
108
output reg                  o_status_bits_mode_wen = 'd0,
109
output reg                  o_status_bits_irq_mask_wen = 'd0,
110
output reg                  o_status_bits_firq_mask_wen = 'd0,
111
 
112
// --------------------------------------------------
113
// Co-Processor interface
114
// --------------------------------------------------
115
output reg  [2:0]           o_copro_opcode1 = 'd0,
116
output reg  [2:0]           o_copro_opcode2 = 'd0,
117
output reg  [3:0]           o_copro_crn = 'd0,
118
output reg  [3:0]           o_copro_crm = 'd0,
119
output reg  [3:0]           o_copro_num = 'd0,
120
output reg  [1:0]           o_copro_operation = 'd0, // 0 = no operation, 
121
                                                     // 1 = Move to Amber Core Register from Coprocessor
122
                                                     // 2 = Move to Coprocessor from Amber Core Register
123
output reg                  o_copro_write_data_wen = 'd0,
124
output                      o_iabt_trigger,
125
output      [31:0]          o_iabt_address,
126
output      [7:0]           o_iabt_status,
127
output                      o_dabt_trigger,
128
output      [31:0]          o_dabt_address,
129
output      [7:0]           o_dabt_status
130
 
131
 
132
);
133
 
134 82 csantifort
`include "a23_localparams.vh"
135
`include "a23_functions.vh"
136 2 csantifort
 
137
localparam [4:0] RST_WAIT1      = 5'd0,
138
                 RST_WAIT2      = 5'd1,
139
                 INT_WAIT1      = 5'd2,
140
                 INT_WAIT2      = 5'd3,
141
                 EXECUTE        = 5'd4,
142
                 PRE_FETCH_EXEC = 5'd5,  // Execute the Pre-Fetched Instruction
143
                 MEM_WAIT1      = 5'd6,  // conditionally decode current instruction, in case
144
                                         // previous instruction does not execute in S2
145
                 MEM_WAIT2      = 5'd7,
146
                 PC_STALL1      = 5'd8,  // Program Counter altered
147
                                         // conditionally decude current instruction, in case
148
                                         // previous instruction does not execute in S2
149
                 PC_STALL2      = 5'd9,
150
                 MTRANS_EXEC1   = 5'd10,
151
                 MTRANS_EXEC2   = 5'd11,
152
                 MTRANS_EXEC3   = 5'd12,
153
                 MTRANS_EXEC3B  = 5'd13,
154
                 MTRANS_EXEC4   = 5'd14,
155
                 MTRANS5_ABORT  = 5'd15,
156
                 MULT_PROC1     = 5'd16,  // first cycle, save pre fetch instruction
157
                 MULT_PROC2     = 5'd17,  // do multiplication
158
                 MULT_STORE     = 5'd19,  // save RdLo
159
                 MULT_ACCUMU    = 5'd20,  // Accumulate add lower 32 bits
160
                 SWAP_WRITE     = 5'd22,
161
                 SWAP_WAIT1     = 5'd23,
162
                 SWAP_WAIT2     = 5'd24,
163
                 COPRO_WAIT     = 5'd25;
164
 
165
 
166
// ========================================================
167
// Internal signals
168
// ========================================================
169
wire    [31:0]         instruction;
170
wire                   instruction_iabt;        // abort flag, follows the instruction
171
wire                   instruction_adex;        // address exception flag, follows the instruction
172
wire    [31:0]         instruction_address;     // instruction virtual address, follows 
173
                                                // the instruction
174
wire    [7:0]          instruction_iabt_status; // abort status, follows the instruction
175
wire    [1:0]          instruction_sel;
176 87 csantifort
reg     [3:0]          itype;
177 2 csantifort
wire    [3:0]          opcode;
178
wire    [7:0]          imm8;
179
wire    [31:0]         offset12;
180
wire    [31:0]         offset24;
181
wire    [4:0]          shift_imm;
182
 
183
wire                   opcode_compare;
184
wire                   mem_op;
185
wire                   load_op;
186
wire                   store_op;
187
wire                   write_pc;
188
wire                   immediate_shifter_operand;
189
wire                   rds_use_rs;
190
wire                   branch;
191
wire                   mem_op_pre_indexed;
192
wire                   mem_op_post_indexed;
193
 
194
// Flop inputs
195
wire    [31:0]         imm32_nxt;
196
wire    [4:0]          imm_shift_amount_nxt;
197
wire                   shift_imm_zero_nxt;
198
wire    [3:0]          condition_nxt;
199
reg                    exclusive_exec_nxt;
200
reg                    data_access_exec_nxt;
201 88 csantifort
wire                   shift_extend;
202 2 csantifort
 
203
reg     [1:0]          barrel_shift_function_nxt;
204
wire    [8:0]          alu_function_nxt;
205 83 csantifort
reg                    use_carry_in_nxt;
206 2 csantifort
reg     [1:0]          multiply_function_nxt;
207
reg     [1:0]          status_bits_mode_nxt;
208
reg                    status_bits_irq_mask_nxt;
209
reg                    status_bits_firq_mask_nxt;
210
 
211
reg     [1:0]          barrel_shift_amount_sel_nxt;
212
reg     [1:0]          barrel_shift_data_sel_nxt;
213
reg     [3:0]          address_sel_nxt;
214
reg     [1:0]          pc_sel_nxt;
215
reg     [1:0]          byte_enable_sel_nxt;
216
reg     [2:0]          status_bits_sel_nxt;
217
reg     [2:0]          reg_write_sel_nxt;
218
reg                    user_mode_regs_load_nxt;
219
wire                   firq_not_user_mode_nxt;
220
 
221
// ALU Function signals
222
reg                    alu_swap_sel_nxt;
223
reg                    alu_not_sel_nxt;
224
reg     [1:0]          alu_cin_sel_nxt;
225
reg                    alu_cout_sel_nxt;
226
reg     [3:0]          alu_out_sel_nxt;
227
 
228
reg                    write_data_wen_nxt;
229
reg                    copro_write_data_wen_nxt;
230
reg                    base_address_wen_nxt;
231
reg                    pc_wen_nxt;
232 71 csantifort
reg     [3:0]          reg_bank_wsel_nxt;
233 2 csantifort
reg                    status_bits_flags_wen_nxt;
234
reg                    status_bits_mode_wen_nxt;
235
reg                    status_bits_irq_mask_wen_nxt;
236
reg                    status_bits_firq_mask_wen_nxt;
237
 
238
reg                    saved_current_instruction_wen;   // saved load instruction
239
reg                    pre_fetch_instruction_wen;       // pre-fetch instruction
240
 
241
reg     [4:0]          control_state = RST_WAIT1;
242
reg     [4:0]          control_state_nxt;
243
 
244
 
245
wire                   dabt;
246
reg                    dabt_reg = 'd0;
247
reg                    dabt_reg_d1;
248
reg                    iabt_reg = 'd0;
249
reg                    adex_reg = 'd0;
250
reg     [31:0]         abt_address_reg = 'd0;
251
reg     [7:0]          abt_status_reg = 'd0;
252
reg     [31:0]         saved_current_instruction = 'd0;
253
reg                    saved_current_instruction_iabt = 'd0;          // access abort flag
254
reg                    saved_current_instruction_adex = 'd0;          // address exception
255
reg     [31:0]         saved_current_instruction_address = 'd0;       // virtual address of abort instruction
256
reg     [7:0]          saved_current_instruction_iabt_status = 'd0;   // status of abort instruction
257
reg     [31:0]         pre_fetch_instruction = 'd0;
258
reg                    pre_fetch_instruction_iabt = 'd0;              // access abort flag
259
reg                    pre_fetch_instruction_adex = 'd0;              // address exception
260
reg     [31:0]         pre_fetch_instruction_address = 'd0;           // virtual address of abort instruction
261
reg     [7:0]          pre_fetch_instruction_iabt_status = 'd0;       // status of abort instruction
262
 
263
wire                   instruction_valid;
264
wire                   instruction_execute;
265
 
266
reg     [3:0]          mtrans_reg;              // the current register being accessed as part of STM/LDM
267
reg     [3:0]          mtrans_reg_d1 = 'd0;     // delayed by 1 period
268
reg     [3:0]          mtrans_reg_d2 = 'd0;     // delayed by 2 periods
269
reg     [31:0]         mtrans_instruction_nxt;
270
 
271
wire   [31:0]          mtrans_base_reg_change;
272
wire   [4:0]           mtrans_num_registers;
273
wire                   use_saved_current_instruction;
274
wire                   use_pre_fetch_instruction;
275
wire                   interrupt;
276
wire   [1:0]           interrupt_mode;
277
wire   [2:0]           next_interrupt;
278
reg                    irq = 'd0;
279
reg                    firq = 'd0;
280
wire                   firq_request;
281
wire                   irq_request;
282
wire                   swi_request;
283
wire                   und_request;
284
wire                   dabt_request;
285
reg    [1:0]           copro_operation_nxt;
286
reg                    mtrans_r15 = 'd0;
287
reg                    mtrans_r15_nxt;
288
reg                    restore_base_address = 'd0;
289
reg                    restore_base_address_nxt;
290
 
291
wire                   regop_set_flags;
292
 
293
 
294
// ========================================================
295
// Instruction Abort and Data Abort outputs
296
// ========================================================
297
 
298
assign o_iabt_trigger     = instruction_iabt && o_status_bits_mode == SVC && control_state == INT_WAIT1;
299
assign o_iabt_address     = instruction_address;
300
assign o_iabt_status      = instruction_iabt_status;
301
 
302
assign o_dabt_trigger     = dabt_reg && !dabt_reg_d1;
303
assign o_dabt_address     = abt_address_reg;
304
assign o_dabt_status      = abt_status_reg;
305
 
306
 
307
// ========================================================
308
// Instruction Decode
309
// ========================================================
310
 
311
// for instructions that take more than one cycle
312
// the instruction is saved in the 'saved_mem_instruction'
313
// register and then that register is used for the rest of
314
// the execution of the instruction.
315
// But if the instruction does not execute because of the
316
// condition, then need to select the next instruction to
317
// decode
318
assign use_saved_current_instruction =  instruction_execute &&
319
                          ( control_state == MEM_WAIT1     ||
320
                            control_state == MEM_WAIT2     ||
321
                            control_state == MTRANS_EXEC1  ||
322
                            control_state == MTRANS_EXEC2  ||
323
                            control_state == MTRANS_EXEC3  ||
324
                            control_state == MTRANS_EXEC3B ||
325
                            control_state == MTRANS_EXEC4  ||
326
                            control_state == MTRANS5_ABORT ||
327
                            control_state == MULT_PROC1    ||
328
                            control_state == MULT_PROC2    ||
329
                            control_state == MULT_ACCUMU   ||
330
                            control_state == MULT_STORE    ||
331
                            control_state == INT_WAIT1     ||
332
                            control_state == INT_WAIT2     ||
333
                            control_state == SWAP_WRITE    ||
334
                            control_state == SWAP_WAIT1    ||
335
                            control_state == SWAP_WAIT2    ||
336
                            control_state == COPRO_WAIT     );
337
 
338
assign use_pre_fetch_instruction = control_state == PRE_FETCH_EXEC;
339
 
340
 
341
assign instruction_sel  =         use_saved_current_instruction  ? 2'd1 :  // saved_current_instruction 
342
                                  use_pre_fetch_instruction      ? 2'd2 :  // pre_fetch_instruction     
343
                                                                   2'd0 ;  // o_read_data               
344
 
345
assign instruction      =         instruction_sel == 2'd0 ? o_read_data               :
346
                                  instruction_sel == 2'd1 ? saved_current_instruction :
347
                                                            pre_fetch_instruction     ;
348
 
349
// abort flag
350
assign instruction_iabt =         instruction_sel == 2'd0 ? iabt_reg                       :
351
                                  instruction_sel == 2'd1 ? saved_current_instruction_iabt :
352
                                                            pre_fetch_instruction_iabt     ;
353
 
354
assign instruction_address =      instruction_sel == 2'd0 ? abt_address_reg                   :
355
                                  instruction_sel == 2'd1 ? saved_current_instruction_address :
356
                                                            pre_fetch_instruction_address     ;
357
 
358
assign instruction_iabt_status =  instruction_sel == 2'd0 ? abt_status_reg                        :
359
                                  instruction_sel == 2'd1 ? saved_current_instruction_iabt_status :
360
                                                            pre_fetch_instruction_iabt_status     ;
361
 
362
// instruction address exception
363
assign instruction_adex =         instruction_sel == 2'd0 ? adex_reg                       :
364
                                  instruction_sel == 2'd1 ? saved_current_instruction_adex :
365
                                                            pre_fetch_instruction_adex     ;
366
 
367
// Instruction Decode - Order is important!
368
always @*
369
    casez ({instruction[27:20], instruction[7:4]})
370 87 csantifort
        12'b00010?001001 : itype = SWAP;
371
        12'b000000??1001 : itype = MULT;
372
        12'b00?????????? : itype = REGOP;
373
        12'b01?????????? : itype = TRANS;
374
        12'b100????????? : itype = MTRANS;
375
        12'b101????????? : itype = BRANCH;
376
        12'b110????????? : itype = CODTRANS;
377
        12'b1110???????0 : itype = COREGOP;
378
        12'b1110???????1 : itype = CORTRANS;
379
        default:           itype = SWI;
380 2 csantifort
    endcase
381
 
382
 
383
// ========================================================
384
// Fixed fields within the instruction
385
// ========================================================
386
 
387
assign opcode        = instruction[24:21];
388
assign condition_nxt = instruction[31:28];
389
 
390 71 csantifort
assign o_rm_sel_nxt    = instruction[3:0];
391 2 csantifort
 
392 87 csantifort
assign o_rn_sel_nxt    = branch  ? 4'd15              : // Use PC to calculate branch destination
393
                                   instruction[19:16] ;
394 2 csantifort
 
395 71 csantifort
assign o_rds_sel_nxt   = control_state == SWAP_WRITE  ? instruction[3:0]   : // Rm gets written out to memory
396 87 csantifort
                         itype == MTRANS              ? mtrans_reg         :
397
                         branch                       ? 4'd15              : // Update the PC
398
                         rds_use_rs                   ? instruction[11:8]  :
399
                                                        instruction[15:12] ;
400 2 csantifort
 
401
 
402
assign shift_imm     = instruction[11:7];
403 87 csantifort
 
404
// this is used for RRX
405
assign shift_extend  = !instruction[25] && !instruction[4] && !(|instruction[11:7]) && instruction[6:5] == 2'b11;
406
 
407 2 csantifort
assign offset12      = { 20'h0, instruction[11:0]};
408
assign offset24      = {{6{instruction[23]}}, instruction[23:0], 2'd0 }; // sign extend
409
assign imm8          = instruction[7:0];
410
 
411
assign immediate_shifter_operand = instruction[25];
412 87 csantifort
assign rds_use_rs                = (itype == REGOP && !instruction[25] && instruction[4]) ||
413
                                   (itype == MULT &&
414 2 csantifort
                                    (control_state == MULT_PROC1  ||
415
                                     control_state == MULT_PROC2  ||
416
                                     instruction_valid && !interrupt )) ;
417 87 csantifort
assign branch                    = itype == BRANCH;
418 2 csantifort
assign opcode_compare =
419
            opcode == CMP ||
420
            opcode == CMN ||
421
            opcode == TEQ ||
422
            opcode == TST ;
423
 
424
 
425 87 csantifort
assign mem_op               = itype == TRANS;
426 2 csantifort
assign load_op              = mem_op && instruction[20];
427
assign store_op             = mem_op && !instruction[20];
428
assign write_pc             = pc_wen_nxt && pc_sel_nxt != 2'd0;
429 87 csantifort
assign regop_set_flags      = itype == REGOP && instruction[20];
430 2 csantifort
 
431
assign mem_op_pre_indexed   =  instruction[24] && instruction[21];
432
assign mem_op_post_indexed  = !instruction[24];
433
 
434
assign imm32_nxt            =  // add 0 to Rm
435 87 csantifort
                               itype == MULT               ? {  32'd0                      } :
436 2 csantifort
 
437
                               // 4 x number of registers
438 87 csantifort
                               itype == MTRANS             ? {  mtrans_base_reg_change     } :
439
                               itype == BRANCH             ? {  offset24                   } :
440
                               itype == TRANS              ? {  offset12                   } :
441 2 csantifort
                               instruction[11:8] == 4'h0  ? {            24'h0, imm8[7:0] } :
442
                               instruction[11:8] == 4'h1  ? { imm8[1:0], 24'h0, imm8[7:2] } :
443
                               instruction[11:8] == 4'h2  ? { imm8[3:0], 24'h0, imm8[7:4] } :
444
                               instruction[11:8] == 4'h3  ? { imm8[5:0], 24'h0, imm8[7:6] } :
445
                               instruction[11:8] == 4'h4  ? { imm8[7:0], 24'h0            } :
446
                               instruction[11:8] == 4'h5  ? { 2'h0,  imm8[7:0], 22'h0     } :
447
                               instruction[11:8] == 4'h6  ? { 4'h0,  imm8[7:0], 20'h0     } :
448
                               instruction[11:8] == 4'h7  ? { 6'h0,  imm8[7:0], 18'h0     } :
449
                               instruction[11:8] == 4'h8  ? { 8'h0,  imm8[7:0], 16'h0     } :
450
                               instruction[11:8] == 4'h9  ? { 10'h0, imm8[7:0], 14'h0     } :
451
                               instruction[11:8] == 4'ha  ? { 12'h0, imm8[7:0], 12'h0     } :
452
                               instruction[11:8] == 4'hb  ? { 14'h0, imm8[7:0], 10'h0     } :
453
                               instruction[11:8] == 4'hc  ? { 16'h0, imm8[7:0], 8'h0      } :
454
                               instruction[11:8] == 4'hd  ? { 18'h0, imm8[7:0], 6'h0      } :
455
                               instruction[11:8] == 4'he  ? { 20'h0, imm8[7:0], 4'h0      } :
456
                                                            { 22'h0, imm8[7:0], 2'h0      } ;
457
 
458
 
459
assign imm_shift_amount_nxt = shift_imm ;
460
 
461
       // This signal is encoded in the decode stage because 
462
       // it is on the critical path in the execute stage
463
assign shift_imm_zero_nxt   = imm_shift_amount_nxt == 5'd0 &&       // immediate amount = 0
464
                              barrel_shift_amount_sel_nxt == 2'd2;  // shift immediate amount
465
 
466
assign alu_function_nxt     = { alu_swap_sel_nxt,
467
                                alu_not_sel_nxt,
468
                                alu_cin_sel_nxt,
469
                                alu_cout_sel_nxt,
470
                                alu_out_sel_nxt  };
471
 
472
 
473
// ========================================================
474
// MTRANS Operations
475
// ========================================================
476
 
477
   // Bit 15 = r15
478
   // Bit 0  = R0
479
   // In LDM and STM instructions R0 is loaded or stored first 
480
always @*
481
    casez (instruction[15:0])
482
    16'b???????????????1 : mtrans_reg = 4'h0 ;
483
    16'b??????????????10 : mtrans_reg = 4'h1 ;
484
    16'b?????????????100 : mtrans_reg = 4'h2 ;
485
    16'b????????????1000 : mtrans_reg = 4'h3 ;
486
    16'b???????????10000 : mtrans_reg = 4'h4 ;
487
    16'b??????????100000 : mtrans_reg = 4'h5 ;
488
    16'b?????????1000000 : mtrans_reg = 4'h6 ;
489
    16'b????????10000000 : mtrans_reg = 4'h7 ;
490
    16'b???????100000000 : mtrans_reg = 4'h8 ;
491
    16'b??????1000000000 : mtrans_reg = 4'h9 ;
492
    16'b?????10000000000 : mtrans_reg = 4'ha ;
493
    16'b????100000000000 : mtrans_reg = 4'hb ;
494
    16'b???1000000000000 : mtrans_reg = 4'hc ;
495
    16'b??10000000000000 : mtrans_reg = 4'hd ;
496
    16'b?100000000000000 : mtrans_reg = 4'he ;
497
    default              : mtrans_reg = 4'hf ;
498
    endcase
499
 
500 87 csantifort
 
501 2 csantifort
always @*
502
    casez (instruction[15:0])
503
    16'b???????????????1 : mtrans_instruction_nxt = {instruction[31:16], instruction[15: 1],  1'd0};
504
    16'b??????????????10 : mtrans_instruction_nxt = {instruction[31:16], instruction[15: 2],  2'd0};
505
    16'b?????????????100 : mtrans_instruction_nxt = {instruction[31:16], instruction[15: 3],  3'd0};
506
    16'b????????????1000 : mtrans_instruction_nxt = {instruction[31:16], instruction[15: 4],  4'd0};
507
    16'b???????????10000 : mtrans_instruction_nxt = {instruction[31:16], instruction[15: 5],  5'd0};
508
    16'b??????????100000 : mtrans_instruction_nxt = {instruction[31:16], instruction[15: 6],  6'd0};
509
    16'b?????????1000000 : mtrans_instruction_nxt = {instruction[31:16], instruction[15: 7],  7'd0};
510
    16'b????????10000000 : mtrans_instruction_nxt = {instruction[31:16], instruction[15: 8],  8'd0};
511
    16'b???????100000000 : mtrans_instruction_nxt = {instruction[31:16], instruction[15: 9],  9'd0};
512
    16'b??????1000000000 : mtrans_instruction_nxt = {instruction[31:16], instruction[15:10], 10'd0};
513
    16'b?????10000000000 : mtrans_instruction_nxt = {instruction[31:16], instruction[15:11], 11'd0};
514
    16'b????100000000000 : mtrans_instruction_nxt = {instruction[31:16], instruction[15:12], 12'd0};
515
    16'b???1000000000000 : mtrans_instruction_nxt = {instruction[31:16], instruction[15:13], 13'd0};
516
    16'b??10000000000000 : mtrans_instruction_nxt = {instruction[31:16], instruction[15:14], 14'd0};
517
    16'b?100000000000000 : mtrans_instruction_nxt = {instruction[31:16], instruction[15   ], 15'd0};
518
    default              : mtrans_instruction_nxt = {instruction[31:16],                     16'd0};
519
    endcase
520
 
521
 
522
// number of registers to be stored
523
assign mtrans_num_registers =   {4'd0, instruction[15]} +
524
                                {4'd0, instruction[14]} +
525
                                {4'd0, instruction[13]} +
526
                                {4'd0, instruction[12]} +
527
                                {4'd0, instruction[11]} +
528
                                {4'd0, instruction[10]} +
529
                                {4'd0, instruction[ 9]} +
530
                                {4'd0, instruction[ 8]} +
531
                                {4'd0, instruction[ 7]} +
532
                                {4'd0, instruction[ 6]} +
533
                                {4'd0, instruction[ 5]} +
534
                                {4'd0, instruction[ 4]} +
535
                                {4'd0, instruction[ 3]} +
536
                                {4'd0, instruction[ 2]} +
537
                                {4'd0, instruction[ 1]} +
538
                                {4'd0, instruction[ 0]} ;
539
 
540
// 4 x number of registers to be stored
541
assign mtrans_base_reg_change = {25'd0, mtrans_num_registers, 2'd0};
542
 
543
// ========================================================
544
// Interrupts
545
// ========================================================
546
 
547
assign firq_request = firq && !i_execute_status_bits[26];
548
assign irq_request  = irq  && !i_execute_status_bits[27];
549 87 csantifort
assign swi_request  = itype == SWI;
550 2 csantifort
assign dabt_request = dabt_reg;
551
 
552
// copro15 and copro13 only supports reg trans opcodes
553
// all other opcodes involving co-processors cause an 
554
// undefined instrution interrupt
555 87 csantifort
assign und_request  =   itype == CODTRANS ||
556
                        itype == COREGOP  ||
557
                      ( itype == CORTRANS && instruction[11:8] != 4'd15 );
558 2 csantifort
 
559
 
560
  // in order of priority !!                 
561
  // Highest 
562
  // 1 Reset
563
  // 2 Data Abort (including data TLB miss)
564
  // 3 FIRQ
565
  // 4 IRQ
566
  // 5 Prefetch Abort (including prefetch TLB miss)
567
  // 6 Undefined instruction, SWI
568
  // Lowest                        
569
assign next_interrupt = dabt_request     ? 3'd1 :  // Data Abort
570
                        firq_request     ? 3'd2 :  // FIRQ
571
                        irq_request      ? 3'd3 :  // IRQ
572
                        instruction_adex ? 3'd4 :  // Address Exception 
573
                        instruction_iabt ? 3'd5 :  // PreFetch Abort, only triggered 
574
                                                   // if the instruction is used
575
                        und_request      ? 3'd6 :  // Undefined Instruction
576
                        swi_request      ? 3'd7 :  // SWI
577
                                           3'd0 ;  // none             
578
 
579
        // SWI and undefined instructions do not cause an interrupt in the decode
580
        // stage. They only trigger interrupts if they arfe executed, so the
581
        // interrupt is triggered if the execute condition is met in the execute stage
582
assign interrupt      = next_interrupt != 3'd0 &&
583
                        next_interrupt != 3'd7 &&  // SWI
584
                        next_interrupt != 3'd6 ;   // undefined interrupt
585
 
586
 
587
assign interrupt_mode = next_interrupt == 3'd2 ? FIRQ :
588
                        next_interrupt == 3'd3 ? IRQ  :
589
                        next_interrupt == 3'd4 ? SVC  :
590
                        next_interrupt == 3'd5 ? SVC  :
591
                        next_interrupt == 3'd6 ? SVC  :
592
                        next_interrupt == 3'd7 ? SVC  :
593
                        next_interrupt == 3'd1 ? SVC  :
594
                                                 USR  ;
595
 
596
 
597
 
598
 
599
// ========================================================
600
// Generate control signals
601
// ========================================================
602
always @*
603
    begin
604
    // default mode
605
    status_bits_mode_nxt            = i_execute_status_bits[1:0];   // change to mode in execute stage get reflected
606
                                                                    // back to this stage automatically
607
    status_bits_irq_mask_nxt        = o_status_bits_irq_mask;
608
    status_bits_firq_mask_nxt       = o_status_bits_firq_mask;
609
    exclusive_exec_nxt              = 1'd0;
610
    data_access_exec_nxt            = 1'd0;
611
    copro_operation_nxt             = 'd0;
612
 
613
    // Save an instruction to use later
614
    saved_current_instruction_wen   = 1'd0;
615
    pre_fetch_instruction_wen       = 1'd0;
616
    mtrans_r15_nxt                  = mtrans_r15;
617
    restore_base_address_nxt        = restore_base_address;
618
 
619
    // default Mux Select values
620
    barrel_shift_amount_sel_nxt     = 'd0;  // don't shift the input
621
    barrel_shift_data_sel_nxt       = 'd0;  // immediate value
622
    barrel_shift_function_nxt       = 'd0;
623 83 csantifort
    use_carry_in_nxt                = 'd0;
624 2 csantifort
    multiply_function_nxt           = 'd0;
625
    address_sel_nxt                 = 'd0;
626
    pc_sel_nxt                      = 'd0;
627
    byte_enable_sel_nxt             = 'd0;
628
    status_bits_sel_nxt             = 'd0;
629
    reg_write_sel_nxt               = 'd0;
630 71 csantifort
    user_mode_regs_load_nxt         = 'd0;
631 2 csantifort
    o_user_mode_regs_store_nxt      = 'd0;
632
 
633
    // ALU Muxes
634
    alu_swap_sel_nxt                = 'd0;
635
    alu_not_sel_nxt                 = 'd0;
636
    alu_cin_sel_nxt                 = 'd0;
637
    alu_cout_sel_nxt                = 'd0;
638
    alu_out_sel_nxt                 = 'd0;
639
 
640
    // default Flop Write Enable values
641
    write_data_wen_nxt              = 'd0;
642
    copro_write_data_wen_nxt        = 'd0;
643
    base_address_wen_nxt            = 'd0;
644
    pc_wen_nxt                      = 'd1;
645 71 csantifort
    reg_bank_wsel_nxt               = 'hF;  // Don't select any
646 2 csantifort
    status_bits_flags_wen_nxt       = 'd0;
647
    status_bits_mode_wen_nxt        = 'd0;
648
    status_bits_irq_mask_wen_nxt    = 'd0;
649
    status_bits_firq_mask_wen_nxt   = 'd0;
650
 
651
    if ( instruction_valid && !interrupt )
652
        begin
653 87 csantifort
        if ( itype == REGOP )
654 2 csantifort
            begin
655
            if ( !opcode_compare )
656
                begin
657
                // Check is the load destination is the PC
658
                if (instruction[15:12]  == 4'd15)
659
                    begin
660
                    pc_sel_nxt      = 2'd1; // alu_out
661
                    address_sel_nxt = 4'd1; // alu_out
662
                    end
663
                else
664 71 csantifort
                    reg_bank_wsel_nxt = instruction[15:12];
665 2 csantifort
                end
666
 
667
            if ( !immediate_shifter_operand )
668
                barrel_shift_function_nxt  = instruction[6:5];
669
 
670
            if ( !immediate_shifter_operand )
671
                barrel_shift_data_sel_nxt = 2'd2; // Shift value from Rm register
672
 
673
            if ( !immediate_shifter_operand && instruction[4] )
674
                barrel_shift_amount_sel_nxt = 2'd1; // Shift amount from Rs registter
675
 
676
            if ( !immediate_shifter_operand && !instruction[4] )
677
                barrel_shift_amount_sel_nxt = 2'd2; // Shift immediate amount 
678 82 csantifort
 
679
            // regops that do not change the overflow flag
680
            if ( opcode == AND || opcode == EOR || opcode == TST || opcode == TEQ ||
681
                 opcode == ORR || opcode == MOV || opcode == BIC || opcode == MVN )
682
                status_bits_sel_nxt = 3'd5;
683
 
684 2 csantifort
            if ( opcode == ADD || opcode == CMN )   // CMN is just like an ADD
685
                begin
686
                alu_out_sel_nxt  = 4'd1; // Add
687 87 csantifort
                use_carry_in_nxt = shift_extend;
688 2 csantifort
                end
689
 
690
            if ( opcode == ADC ) // Add with Carry
691
                begin
692
                alu_out_sel_nxt  = 4'd1; // Add
693
                alu_cin_sel_nxt  = 2'd2; // carry in from status_bits
694 87 csantifort
                use_carry_in_nxt = shift_extend;
695 2 csantifort
                end
696
 
697
            if ( opcode == SUB || opcode == CMP ) // Subtract
698
                begin
699
                alu_out_sel_nxt  = 4'd1; // Add
700
                alu_cin_sel_nxt  = 2'd1; // cin = 1
701
                alu_not_sel_nxt  = 1'd1; // invert B
702
                end
703
 
704
            // SBC (Subtract with Carry) subtracts the value of its 
705
            // second operand and the value of NOT(Carry flag) from
706
            // the value of its first operand.
707
            //  Rd = Rn - shifter_operand - NOT(C Flag)
708
            if ( opcode == SBC ) // Subtract with Carry
709
                begin
710
                alu_out_sel_nxt  = 4'd1; // Add
711
                alu_cin_sel_nxt  = 2'd2; // carry in from status_bits
712
                alu_not_sel_nxt  = 1'd1; // invert B
713 83 csantifort
                use_carry_in_nxt = 1'd1;
714 2 csantifort
                end
715
 
716
            if ( opcode == RSB ) // Reverse Subtract
717
                begin
718
                alu_out_sel_nxt  = 4'd1; // Add
719
                alu_cin_sel_nxt  = 2'd1; // cin = 1
720
                alu_not_sel_nxt  = 1'd1; // invert B
721
                alu_swap_sel_nxt = 1'd1; // swap A and B
722
                end
723
 
724
            if ( opcode == RSC ) // Reverse Subtract with carry
725
                begin
726
                alu_out_sel_nxt  = 4'd1; // Add
727
                alu_cin_sel_nxt  = 2'd2; // carry in from status_bits
728
                alu_not_sel_nxt  = 1'd1; // invert B
729
                alu_swap_sel_nxt = 1'd1; // swap A and B
730 83 csantifort
                use_carry_in_nxt = 1'd1;
731 2 csantifort
                end
732
 
733
            if ( opcode == AND || opcode == TST ) // Logical AND, Test  (using AND operator)
734
                begin
735
                alu_out_sel_nxt  = 4'd8;  // AND
736
                alu_cout_sel_nxt = 1'd1;  // i_barrel_shift_carry
737
                end
738
 
739
            if ( opcode == EOR || opcode == TEQ ) // Logical Exclusive OR, Test Equivalence (using EOR operator)
740
                begin
741 87 csantifort
                alu_out_sel_nxt  = 4'd6; // XOR
742 2 csantifort
                alu_cout_sel_nxt = 1'd1; // i_barrel_shift_carry
743 87 csantifort
                use_carry_in_nxt = 1'd1;
744 2 csantifort
                end
745
 
746
            if ( opcode == ORR )
747
                begin
748
                alu_out_sel_nxt  = 4'd7; // OR
749 87 csantifort
                alu_cout_sel_nxt = 1'd1; // i_barrel_shift_carry
750
                use_carry_in_nxt = 1'd1;
751 2 csantifort
                end
752
 
753
            if ( opcode == BIC ) // Bit Clear (using AND & NOT operators)
754
                begin
755
                alu_out_sel_nxt  = 4'd8;  // AND
756
                alu_not_sel_nxt  = 1'd1;  // invert B
757
                alu_cout_sel_nxt = 1'd1;  // i_barrel_shift_carry
758 87 csantifort
                use_carry_in_nxt = 1'd1;
759 2 csantifort
                end
760
 
761
            if ( opcode == MOV ) // Move
762
                begin
763
                alu_cout_sel_nxt = 1'd1;  // i_barrel_shift_carry
764 87 csantifort
                use_carry_in_nxt = 1'd1;
765 2 csantifort
                end
766
 
767
            if ( opcode == MVN ) // Move NOT
768
                begin
769
                alu_not_sel_nxt  = 1'd1; // invert B
770 87 csantifort
                alu_cout_sel_nxt = 1'd1; // i_barrel_shift_carry
771
                use_carry_in_nxt = 1'd1;
772 2 csantifort
                end
773
            end
774
 
775
        // Load & Store instructions
776
        if ( mem_op )
777
            begin
778
            saved_current_instruction_wen   = 1'd1; // Save the memory access instruction to refer back to later
779
            pc_wen_nxt                      = 1'd0; // hold current PC value
780
            data_access_exec_nxt            = 1'd1; // indicate that its a data read or write, 
781
                                                    // rather than an instruction fetch
782
            alu_out_sel_nxt                 = 4'd1; // Add
783
 
784
            if ( !instruction[23] )  // U: Subtract offset
785
                begin
786
                alu_cin_sel_nxt  = 2'd1; // cin = 1
787
                alu_not_sel_nxt  = 1'd1; // invert B
788
                end
789
 
790
            if ( store_op )
791
                begin
792
                write_data_wen_nxt = 1'd1;
793 87 csantifort
                if ( itype == TRANS && instruction[22] )
794 2 csantifort
                    byte_enable_sel_nxt = 2'd1;         // Save byte
795
                end
796
 
797
                // need to update the register holding the address ?
798
                // This is Rn bits [19:16]
799
            if ( mem_op_pre_indexed || mem_op_post_indexed )
800
                begin
801
                // Check is the load destination is the PC
802 71 csantifort
                if ( o_rn_sel_nxt  == 4'd15 )
803 2 csantifort
                    pc_sel_nxt = 2'd1;
804
                else
805 71 csantifort
                    reg_bank_wsel_nxt = o_rn_sel_nxt;
806 2 csantifort
                end
807
 
808
                // if post-indexed, then use Rn rather than ALU output, as address
809
            if ( mem_op_post_indexed )
810
               address_sel_nxt = 4'd4; // Rn
811
            else
812
               address_sel_nxt = 4'd1; // alu out
813
 
814 87 csantifort
            if ( instruction[25] && itype ==  TRANS )
815 2 csantifort
                barrel_shift_data_sel_nxt = 2'd2; // Shift value from Rm register
816
 
817 87 csantifort
            if ( itype == TRANS && instruction[25] && shift_imm != 5'd0 )
818 2 csantifort
                begin
819
                barrel_shift_function_nxt   = instruction[6:5];
820
                barrel_shift_amount_sel_nxt = 2'd2; // imm_shift_amount
821
                end
822
            end
823
 
824 87 csantifort
        if ( itype == BRANCH )
825 2 csantifort
            begin
826
            pc_sel_nxt      = 2'd1; // alu_out
827
            address_sel_nxt = 4'd1; // alu_out
828
            alu_out_sel_nxt = 4'd1; // Add
829
 
830
            if ( instruction[24] ) // Link
831
                begin
832 71 csantifort
                reg_bank_wsel_nxt  = 4'd14;  // Save PC to LR
833 2 csantifort
                reg_write_sel_nxt = 3'd1;            // pc - 32'd4
834
                end
835
            end
836
 
837 87 csantifort
        if ( itype == MTRANS )
838 2 csantifort
            begin
839
            saved_current_instruction_wen   = 1'd1; // Save the memory access instruction to refer back to later
840
            pc_wen_nxt                      = 1'd0; // hold current PC value
841
            data_access_exec_nxt            = 1'd1; // indicate that its a data read or write, 
842
                                                    // rather than an instruction fetch
843
            alu_out_sel_nxt                 = 4'd1; // Add
844
            mtrans_r15_nxt                  = instruction[15];  // load or save r15 ?
845
            base_address_wen_nxt            = 1'd1; // Save the value of the register used for the base address,
846
                                                    // in case of a data abort, and need to restore the value        
847
 
848
            // The spec says -
849
            // If the instruction would have overwritten the base with data 
850
            // (that is, it has the base in the transfer list), the overwriting is prevented.
851
            // This is true even when the abort occurs after the base word gets loaded
852
            restore_base_address_nxt        = instruction[20] &&
853
                                                (instruction[15:0] & (1'd1 << instruction[19:16]));
854
 
855
            // Increment or Decrement
856
            if ( instruction[23] ) // increment
857
                begin
858
                if ( instruction[24] )    // increment before
859
                    address_sel_nxt = 4'd7; // Rn + 4
860
                else
861
                    address_sel_nxt = 4'd4; // Rn
862
                end
863
            else // decrement
864
                begin
865
                alu_cin_sel_nxt  = 2'd1; // cin = 1
866
                alu_not_sel_nxt  = 1'd1; // invert B
867
                if ( !instruction[24] )    // decrement after
868
                    address_sel_nxt  = 4'd6; // alu out + 4
869
                else
870
                    address_sel_nxt  = 4'd1; // alu out
871
                end
872
 
873
            // Load or store ?
874
            if ( !instruction[20] )  // Store
875
                write_data_wen_nxt = 1'd1;
876
 
877
            // LDM: load into user mode registers, when in priviledged mode  
878 82 csantifort
            // Don't use mtrans_r15 here because its not loaded yet   
879 87 csantifort
            //if ( {instruction[22],instruction[20],instruction[15]} == 3'b110 )
880
            if ( {instruction[22:20],instruction[15]} == 4'b1010 )
881 2 csantifort
                user_mode_regs_load_nxt = 1'd1;
882 87 csantifort
 
883 2 csantifort
            // SDM: store the user mode registers, when in priviledged mode     
884 87 csantifort
            //if ( {instruction[22],instruction[20]} == 3'b10 )  
885
            if ( {instruction[22:20]} == 3'b100 )
886 2 csantifort
                o_user_mode_regs_store_nxt = 1'd1;
887
 
888
            // update the base register ?
889
            if ( instruction[21] )  // the W bit
890 71 csantifort
                reg_bank_wsel_nxt  = o_rn_sel_nxt;
891 2 csantifort
            end
892
 
893
 
894 87 csantifort
        if ( itype == MULT )
895 2 csantifort
            begin
896
            multiply_function_nxt[0]        = 1'd1; // set enable
897
                                                    // some bits can be changed just below
898
            saved_current_instruction_wen   = 1'd1; // Save the Multiply instruction to 
899
                                                    // refer back to later
900
            pc_wen_nxt                      = 1'd0; // hold current PC value
901
 
902
            if ( instruction[21] )
903
                multiply_function_nxt[1]    = 1'd1; // accumulate
904
            end
905
 
906
 
907
        // swp - do read part first
908 87 csantifort
        if ( itype == SWAP )
909 2 csantifort
            begin
910
            saved_current_instruction_wen   = 1'd1; // Save the memory access instruction to refer back to later
911
            pc_wen_nxt                      = 1'd0; // hold current PC value
912
            data_access_exec_nxt            = 1'd1; // indicate that its a data read or write, 
913
                                                    // rather than an instruction fetch
914
            barrel_shift_data_sel_nxt       = 2'd2; // Shift value from Rm register
915
            address_sel_nxt                 = 4'd4; // Rn
916
            exclusive_exec_nxt              = 1'd1; // signal an exclusive access
917
            end
918
 
919
 
920
        // mcr & mrc - takes two cycles
921 87 csantifort
        if ( itype == CORTRANS && !und_request )
922 2 csantifort
            begin
923
            saved_current_instruction_wen   = 1'd1; // Save the memory access instruction to refer back to later
924
            pc_wen_nxt                      = 1'd0; // hold current PC value
925
            address_sel_nxt                 = 4'd3; // pc  (not pc + 4)
926
 
927
            if ( instruction[20] ) // MRC
928
                copro_operation_nxt         = 2'd1;  // Register transfer from Co-Processor
929
            else // MCR
930
                begin
931
                 // Don't enable operation to Co-Processor until next period
932
                 // So it gets the Rd value from the execution stage at the same time
933
                copro_operation_nxt      = 2'd0;
934
                copro_write_data_wen_nxt = 1'd1;  // Rd register value to co-processor
935
                end
936
            end
937
 
938
 
939 87 csantifort
        if ( itype == SWI || und_request )
940 2 csantifort
            begin
941
            // save address of next instruction to Supervisor Mode LR
942
            reg_write_sel_nxt               = 3'd1;            // pc -4
943 71 csantifort
            reg_bank_wsel_nxt               = 4'd14;  // LR
944 2 csantifort
 
945
            address_sel_nxt                 = 4'd2;            // interrupt_vector
946
            pc_sel_nxt                      = 2'd2;            // interrupt_vector
947
 
948
            status_bits_mode_nxt            = interrupt_mode;  // e.g. Supervisor mode
949
            status_bits_mode_wen_nxt        = 1'd1;
950
 
951
            // disable normal interrupts
952
            status_bits_irq_mask_nxt        = 1'd1;
953
            status_bits_irq_mask_wen_nxt    = 1'd1;
954
            end
955
 
956
 
957
        if ( regop_set_flags )
958
            begin
959
            status_bits_flags_wen_nxt = 1'd1;
960
 
961
            // If <Rd> is r15, the ALU output is copied to the Status Bits. 
962
            // Not allowed to use r15 for mul or lma instructions           
963
            if ( instruction[15:12] == 4'd15 )
964
                begin
965
                status_bits_sel_nxt       = 3'd1; // alu out
966
 
967
                // Priviledged mode? Then also update the other status bits
968
                if ( i_execute_status_bits[1:0] != USR )
969
                    begin
970
                    status_bits_mode_wen_nxt      = 1'd1;
971
                    status_bits_irq_mask_wen_nxt  = 1'd1;
972
                    status_bits_firq_mask_wen_nxt = 1'd1;
973
                    end
974
                end
975
            end
976
 
977
        end
978
 
979
    // Handle asynchronous interrupts.
980
    // interrupts are processed only during execution states
981
    // multicycle instructions must complete before the interrupt starts
982
    // SWI, Address Exception and Undefined Instruction interrupts are only executed if the
983
    // instruction that causes the interrupt is conditionally executed so
984
    // its not handled here
985
    if ( instruction_valid && interrupt &&  next_interrupt != 3'd6 )
986
        begin
987
        // Save the interrupt causing instruction to refer back to later
988
        // This also saves the instruction abort vma and status, in the case of an
989
        // instruction abort interrupt
990
        saved_current_instruction_wen   = 1'd1;
991
 
992
        // save address of next instruction to Supervisor Mode LR
993
        // Address Exception ?
994
        if ( next_interrupt == 3'd4 )
995
            reg_write_sel_nxt               = 3'd7;            // pc
996
        else
997
            reg_write_sel_nxt               = 3'd1;            // pc -4
998
 
999 71 csantifort
        reg_bank_wsel_nxt               = 4'd14;           // LR
1000 2 csantifort
 
1001
        address_sel_nxt                 = 4'd2;            // interrupt_vector
1002
        pc_sel_nxt                      = 2'd2;            // interrupt_vector
1003
 
1004
        status_bits_mode_nxt            = interrupt_mode;  // e.g. Supervisor mode
1005
        status_bits_mode_wen_nxt        = 1'd1;
1006
 
1007
        // disable normal interrupts
1008
        status_bits_irq_mask_nxt        = 1'd1;
1009
        status_bits_irq_mask_wen_nxt    = 1'd1;
1010
 
1011
        // disable fast interrupts
1012
        if ( next_interrupt == 3'd2 ) // FIRQ
1013
            begin
1014
            status_bits_firq_mask_nxt        = 1'd1;
1015
            status_bits_firq_mask_wen_nxt    = 1'd1;
1016
            end
1017
        end
1018
 
1019
 
1020
    // previous instruction was either ldr or sdr
1021
    // if it is currently executing in the execute stage do the following    
1022
    if ( control_state == MEM_WAIT1 )
1023
        begin
1024
        // Save the next instruction to execute later
1025
        // Do this even if this instruction does not execute because of Condition
1026
        pre_fetch_instruction_wen   = 1'd1;
1027
 
1028
        if ( instruction_execute ) // conditional execution state
1029
            begin
1030
            address_sel_nxt             = 4'd3; // pc  (not pc + 4)
1031
            pc_wen_nxt                  = 1'd0; // hold current PC value
1032
            end
1033
        end
1034
 
1035
 
1036
    // completion of load operation        
1037
    if ( control_state == MEM_WAIT2 && load_op )
1038
        begin
1039
        barrel_shift_data_sel_nxt   = 2'd1;  // load word from memory
1040
        barrel_shift_amount_sel_nxt = 2'd3;  // shift by address[1:0] x 8
1041
 
1042
        // shift needed
1043
        if ( i_execute_address[1:0] != 2'd0 )
1044
            barrel_shift_function_nxt = ROR;
1045
 
1046
        // load a byte            
1047 87 csantifort
        if ( itype == TRANS && instruction[22] )
1048 2 csantifort
            alu_out_sel_nxt             = 4'd3;  // zero_extend8
1049
 
1050
        if ( !dabt )  // dont load data there is an abort on the data read
1051
            begin
1052
            // Check if the load destination is the PC
1053
            if (instruction[15:12]  == 4'd15)
1054
                begin
1055
                pc_sel_nxt      = 2'd1; // alu_out
1056
                address_sel_nxt = 4'd1; // alu_out
1057
                end
1058
            else
1059 71 csantifort
                reg_bank_wsel_nxt = instruction[15:12];
1060 2 csantifort
            end
1061
        end
1062
 
1063
 
1064
    // second cycle of multiple load or store
1065
    if ( control_state == MTRANS_EXEC1 )
1066
        begin
1067
        // Save the next instruction to execute later
1068
        // Do this even if this instruction does not execute because of Condition
1069
        pre_fetch_instruction_wen   = 1'd1;
1070
 
1071
        if ( instruction_execute ) // conditional execution state
1072
            begin
1073
            address_sel_nxt             = 4'd5;  // o_address
1074
            pc_wen_nxt                  = 1'd0;  // hold current PC value
1075
            data_access_exec_nxt        = 1'd1;  // indicate that its a data read or write, 
1076
                                                 // rather than an instruction fetch
1077
 
1078
            if ( !instruction[20] ) // Store
1079
                write_data_wen_nxt = 1'd1;
1080
 
1081
            // LDM: load into user mode registers, when in priviledged mode     
1082 87 csantifort
            //if ( {instruction[22],instruction[20],mtrans_r15} == 3'b110 )
1083
            if ( {instruction[22:20],mtrans_r15} == 4'b1010 )
1084 2 csantifort
                user_mode_regs_load_nxt = 1'd1;
1085
 
1086
            // SDM: store the user mode registers, when in priviledged mode     
1087 87 csantifort
            //if ( {instruction[22],instruction[20]} == 2'b10 )  
1088
            if ( {instruction[22:20]} == 3'b100 )
1089 2 csantifort
                o_user_mode_regs_store_nxt = 1'd1;
1090
            end
1091
        end
1092
 
1093
 
1094
        // third cycle of multiple load or store
1095
    if ( control_state == MTRANS_EXEC2 )
1096
        begin
1097
        address_sel_nxt             = 4'd5;  // o_address
1098
        pc_wen_nxt                  = 1'd0;  // hold current PC value
1099
        data_access_exec_nxt        = 1'd1;  // indicate that its a data read or write, 
1100
                                             // rather than an instruction fetch
1101
        barrel_shift_data_sel_nxt   = 2'd1;  // load word from memory
1102
 
1103
        // Load or Store
1104
        if ( instruction[20] ) // Load
1105
            begin
1106
            // Can never be loading the PC in this state, as the PC is always
1107
            // the last register in the set to be loaded
1108
            if ( !dabt )
1109 71 csantifort
                reg_bank_wsel_nxt = mtrans_reg_d2;
1110 2 csantifort
            end
1111
        else // Store
1112
            write_data_wen_nxt = 1'd1;
1113
 
1114
        // LDM: load into user mode registers, when in priviledged mode     
1115 82 csantifort
        if ( {instruction[22],instruction[20],mtrans_r15} == 3'b110 )
1116 2 csantifort
            user_mode_regs_load_nxt = 1'd1;
1117
 
1118
        // SDM: store the user mode registers, when in priviledged mode     
1119 82 csantifort
        if ( {instruction[22],instruction[20]} == 2'b10 )
1120 2 csantifort
            o_user_mode_regs_store_nxt = 1'd1;
1121
        end
1122
 
1123
 
1124
        // second or fourth cycle of multiple load or store
1125
    if ( control_state == MTRANS_EXEC3 && instruction_execute )
1126
        begin
1127
        address_sel_nxt             = 4'd3; // pc  (not pc + 4)
1128
        pc_wen_nxt                  = 1'd0;  // hold current PC value
1129
        barrel_shift_data_sel_nxt   = 2'd1;  // load word from memory
1130
 
1131
        // Can never be loading the PC in this state, as the PC is always
1132
        // the last register in the set to be loaded
1133
        if ( instruction[20] && !dabt ) // Load
1134 71 csantifort
            reg_bank_wsel_nxt = mtrans_reg_d2;
1135 2 csantifort
 
1136
        // LDM: load into user mode registers, when in priviledged mode     
1137 82 csantifort
        if ( {instruction[22],instruction[20],mtrans_r15} == 3'b110 )
1138 2 csantifort
            user_mode_regs_load_nxt = 1'd1;
1139
 
1140
        // SDM: store the user mode registers, when in priviledged mode     
1141 82 csantifort
        //if ( {instruction[22:20]} == 3'b100 )  
1142
        if ( {instruction[22],instruction[20]} == 2'b10 )
1143 2 csantifort
            o_user_mode_regs_store_nxt = 1'd1;
1144
       end
1145
 
1146
    // state is used for LMD/STM of a single register
1147
    if ( control_state == MTRANS_EXEC3B && instruction_execute )
1148
        begin
1149
        // Save the next instruction to execute later
1150
        // Do this even if this instruction does not execute because of Condition
1151
        pre_fetch_instruction_wen   = 1'd1;
1152
 
1153
        address_sel_nxt             = 4'd3;  // pc  (not pc + 4)
1154
        pc_wen_nxt                  = 1'd0;  // hold current PC value
1155
 
1156
        // LDM: load into user mode registers, when in priviledged mode     
1157 82 csantifort
        if ( {instruction[22],instruction[20],mtrans_r15} == 3'b110 )
1158 2 csantifort
            user_mode_regs_load_nxt = 1'd1;
1159
 
1160
        // SDM: store the user mode registers, when in priviledged mode     
1161 82 csantifort
        if ( {instruction[22],instruction[20]} == 2'b10 )
1162 2 csantifort
            o_user_mode_regs_store_nxt = 1'd1;
1163
        end
1164
 
1165
    if ( control_state == MTRANS_EXEC4 )
1166
        begin
1167
        barrel_shift_data_sel_nxt   = 2'd1;  // load word from memory
1168
 
1169
        if ( instruction[20] ) // Load
1170
            begin
1171
            if (!dabt) // dont overwrite registers or status if theres a data abort
1172
                begin
1173
                if ( mtrans_reg_d2 == 4'd15 ) // load new value into PC
1174
                    begin
1175
                    address_sel_nxt = 4'd1; // alu_out - read instructions using new PC value
1176
                    pc_sel_nxt      = 2'd1; // alu_out
1177
                    pc_wen_nxt      = 1'd1; // write PC
1178
 
1179
                    // ldm with S bit and pc: the Status bits are updated
1180
                    // Node this must be done only at the end
1181
                    // so the register set is the set in the mode before it
1182
                    // gets changed. 
1183
                    if ( instruction[22] )
1184
                         begin
1185
                         status_bits_sel_nxt           = 3'd1; // alu out
1186
                         status_bits_flags_wen_nxt     = 1'd1;
1187
 
1188
                         // Can't change the mode or mask bits in User mode
1189
                         if ( i_execute_status_bits[1:0] != USR )
1190
                            begin
1191
                            status_bits_mode_wen_nxt      = 1'd1;
1192
                            status_bits_irq_mask_wen_nxt  = 1'd1;
1193
                            status_bits_firq_mask_wen_nxt = 1'd1;
1194
                            end
1195
                         end
1196
                    end
1197
                else
1198
                    begin
1199 71 csantifort
                    reg_bank_wsel_nxt = mtrans_reg_d2;
1200 2 csantifort
                    end
1201
                end
1202
            end
1203
 
1204
           // we have a data abort interrupt
1205
        if ( dabt )
1206
            begin
1207
            pc_wen_nxt = 1'd0;  // hold current PC value
1208
            end
1209
 
1210
        // LDM: load into user mode registers, when in priviledged mode     
1211 82 csantifort
        if ( {instruction[22],instruction[20],mtrans_r15} == 3'b110 )
1212 2 csantifort
            user_mode_regs_load_nxt = 1'd1;
1213
 
1214
        // SDM: store the user mode registers, when in priviledged mode     
1215 82 csantifort
        if ( {instruction[22],instruction[20]} == 2'b10 )
1216 2 csantifort
            o_user_mode_regs_store_nxt = 1'd1;
1217
        end
1218
 
1219
 
1220
    // state is for when a data abort interrupt is triggered during an LDM
1221
    if ( control_state == MTRANS5_ABORT )
1222
        begin
1223
        // Restore the Base Address, if the base register is included in the
1224
        // list of registers being loaded
1225
        if (restore_base_address) // LDM with base address in register list
1226
            begin
1227
            reg_write_sel_nxt = 3'd6;                        // write base_register
1228 71 csantifort
            reg_bank_wsel_nxt  = instruction[19:16];         // to Rn
1229 2 csantifort
            end
1230
        end
1231
 
1232
 
1233
        // Multiply or Multiply-Accumulate
1234
    if ( control_state == MULT_PROC1 && instruction_execute )
1235
        begin
1236
        // Save the next instruction to execute later
1237
        // Do this even if this instruction does not execute because of Condition
1238
        pre_fetch_instruction_wen   = 1'd1;
1239
        pc_wen_nxt                  = 1'd0;  // hold current PC value
1240
        multiply_function_nxt       = o_multiply_function;
1241
        end
1242
 
1243
 
1244
        // Multiply or Multiply-Accumulate
1245
        // Do multiplication
1246
        // Wait for done or accumulate signal
1247
    if ( control_state == MULT_PROC2 )
1248
        begin
1249
        // Save the next instruction to execute later
1250
        // Do this even if this instruction does not execute because of Condition
1251
        pc_wen_nxt              = 1'd0;  // hold current PC value
1252
        address_sel_nxt         = 4'd3;  // pc  (not pc + 4)
1253
        multiply_function_nxt   = o_multiply_function;
1254
        end
1255
 
1256
 
1257
    // Save RdLo
1258
    // always last cycle of all multiply or multiply accumulate operations
1259
    if ( control_state == MULT_STORE )
1260
        begin
1261
        reg_write_sel_nxt     = 3'd2; // multiply_out
1262
        multiply_function_nxt = o_multiply_function;
1263
 
1264 87 csantifort
        if ( itype == MULT ) // 32-bit
1265 71 csantifort
            reg_bank_wsel_nxt      = instruction[19:16]; // Rd
1266 2 csantifort
        else  // 64-bit / Long
1267 71 csantifort
            reg_bank_wsel_nxt      = instruction[15:12]; // RdLo
1268 2 csantifort
 
1269
        if ( instruction[20] )  // the 'S' bit
1270
            begin
1271
            status_bits_sel_nxt       = 3'd4; // { multiply_flags, status_bits_flags[1:0] } 
1272
            status_bits_flags_wen_nxt = 1'd1;
1273
            end
1274
        end
1275
 
1276
        // Add lower 32 bits to multiplication product
1277
    if ( control_state == MULT_ACCUMU )
1278
        begin
1279
        multiply_function_nxt = o_multiply_function;
1280
        pc_wen_nxt            = 1'd0;  // hold current PC value
1281
        address_sel_nxt       = 4'd3;  // pc  (not pc + 4)
1282
        end
1283
 
1284
    // swp - do write request in 2nd cycle
1285
    if ( control_state == SWAP_WRITE && instruction_execute )
1286
        begin
1287
        barrel_shift_data_sel_nxt       = 2'd2; // Shift value from Rm register
1288
        address_sel_nxt                 = 4'd4; // Rn
1289
        write_data_wen_nxt              = 1'd1;
1290
        data_access_exec_nxt            = 1'd1; // indicate that its a data read or write, 
1291
                                                // rather than an instruction fetch
1292
 
1293
        if ( instruction[22] )
1294
            byte_enable_sel_nxt = 2'd1;         // Save byte
1295
 
1296
        if ( instruction_execute )                         // conditional execution state
1297
            pc_wen_nxt                  = 1'd0; // hold current PC value
1298
 
1299
        // Save the next instruction to execute later
1300
        // Do this even if this instruction does not execute because of Condition
1301
        pre_fetch_instruction_wen   = 1'd1;
1302
 
1303
        end
1304
 
1305
 
1306
    // swp - receive read response in 3rd cycle
1307
    if ( control_state == SWAP_WAIT1 )
1308
        begin
1309
        barrel_shift_data_sel_nxt   = 2'd1;  // load word from memory
1310
        barrel_shift_amount_sel_nxt = 2'd3;  // shift by address[1:0] x 8
1311
 
1312
        // shift needed
1313
        if ( i_execute_address[1:0] != 2'd0 )
1314
            barrel_shift_function_nxt = ROR;
1315
 
1316
        if ( instruction_execute ) // conditional execution state
1317
            begin
1318
            address_sel_nxt             = 4'd3; // pc  (not pc + 4)
1319
            pc_wen_nxt                  = 1'd0; // hold current PC value
1320
            end
1321
 
1322
        // load a byte            
1323
        if ( instruction[22] )
1324
            alu_out_sel_nxt = 4'd3;  // zero_extend8
1325
 
1326
        if ( !dabt )
1327
            begin
1328
            // Check is the load destination is the PC
1329
            if ( instruction[15:12]  == 4'd15 )
1330
                begin
1331
                pc_sel_nxt      = 2'd1; // alu_out
1332
                address_sel_nxt = 4'd1; // alu_out
1333
                end
1334
            else
1335 71 csantifort
                reg_bank_wsel_nxt = instruction[15:12];
1336 2 csantifort
            end
1337
        end
1338
 
1339
    // 1 cycle delay for Co-Processor Register access
1340
    if ( control_state == COPRO_WAIT && instruction_execute )
1341
        begin
1342
        pre_fetch_instruction_wen = 1'd1;
1343
 
1344
        if ( instruction[20] ) // mrc instruction
1345
            begin
1346
            // Check is the load destination is the PC
1347
            if ( instruction[15:12]  == 4'd15 )
1348
                begin
1349
                // If r15 is specified for <Rd>, the condition code flags are 
1350
                // updated instead of a general-purpose register.
1351
                status_bits_sel_nxt           = 3'd3;  // i_copro_data
1352
                status_bits_flags_wen_nxt     = 1'd1;
1353
 
1354
                // Can't change these in USR mode
1355
                if ( i_execute_status_bits[1:0] != USR )
1356
                   begin
1357
                   status_bits_mode_wen_nxt      = 1'd1;
1358
                   status_bits_irq_mask_wen_nxt  = 1'd1;
1359
                   status_bits_firq_mask_wen_nxt = 1'd1;
1360
                   end
1361
                end
1362
            else
1363 71 csantifort
                reg_bank_wsel_nxt = instruction[15:12];
1364 2 csantifort
 
1365
            reg_write_sel_nxt = 3'd5;     // i_copro_data
1366
            end
1367
        else // mcr instruction
1368
            begin
1369
            copro_operation_nxt      = 2'd2;  // Register transfer to Co-Processor 
1370
            end
1371
        end
1372
 
1373
 
1374
    // Have just changed the status_bits mode but this
1375
    // creates a 1 cycle gap with the old mode
1376
    // coming back from execute into instruction_decode
1377
    // So squash that old mode value during this
1378
    // cycle of the interrupt transition    
1379
    if ( control_state == INT_WAIT1 )
1380
        status_bits_mode_nxt            = o_status_bits_mode;   // Supervisor mode
1381
 
1382
    end
1383
 
1384
 
1385
// Speed up the long path from u_decode/o_read_data to u_register_bank/r8_firq
1386
// This pre-encodes the firq_s3 signal thats used in u_register_bank
1387
assign firq_not_user_mode_nxt = !user_mode_regs_load_nxt && status_bits_mode_nxt == FIRQ;
1388
 
1389
 
1390
// ========================================================
1391
// Next State Logic
1392
// ========================================================
1393
 
1394
// this replicates the current value of the execute signal in the execute stage
1395
assign instruction_execute = conditional_execute ( o_condition, i_execute_status_bits[31:28] );
1396
 
1397
assign instruction_valid = (control_state == EXECUTE || control_state == PRE_FETCH_EXEC) ||
1398
                     // when last instruction was multi-cycle instruction but did not execute
1399
                     // because condition was false then act like you're in the execute state
1400
                    (!instruction_execute && (control_state == PC_STALL1    ||
1401
                                              control_state == MEM_WAIT1    ||
1402
                                              control_state == COPRO_WAIT   ||
1403
                                              control_state == SWAP_WRITE   ||
1404
                                              control_state == MULT_PROC1   ||
1405
                                              control_state == MTRANS_EXEC1 ||
1406
                                              control_state == MTRANS_EXEC3 ||
1407
                                              control_state == MTRANS_EXEC3B  ) );
1408
 
1409
 
1410
 always @*
1411
    begin
1412
    // default is to hold the current state
1413
    control_state_nxt = control_state;
1414
 
1415
    // Note: The order is important here
1416
    if ( control_state == RST_WAIT1 )     control_state_nxt = RST_WAIT2;
1417
    if ( control_state == RST_WAIT2 )     control_state_nxt = EXECUTE;
1418
    if ( control_state == INT_WAIT1 )     control_state_nxt = INT_WAIT2;
1419
    if ( control_state == INT_WAIT2 )     control_state_nxt = EXECUTE;
1420
    if ( control_state == COPRO_WAIT )    control_state_nxt = PRE_FETCH_EXEC;
1421
    if ( control_state == PC_STALL1 )     control_state_nxt = PC_STALL2;
1422
    if ( control_state == PC_STALL2 )     control_state_nxt = EXECUTE;
1423
    if ( control_state == SWAP_WRITE )    control_state_nxt = SWAP_WAIT1;
1424
    if ( control_state == SWAP_WAIT1 )    control_state_nxt = SWAP_WAIT2;
1425
    if ( control_state == MULT_STORE )    control_state_nxt = PRE_FETCH_EXEC;
1426
    if ( control_state == MTRANS5_ABORT ) control_state_nxt = PRE_FETCH_EXEC;
1427
 
1428
    if ( control_state == MEM_WAIT1 )
1429
        control_state_nxt = MEM_WAIT2;
1430
 
1431
    if ( control_state == MEM_WAIT2   ||
1432
        control_state == SWAP_WAIT2    )
1433
        begin
1434
        if ( write_pc ) // writing to the PC!! 
1435
            control_state_nxt = PC_STALL1;
1436
        else
1437
            control_state_nxt = PRE_FETCH_EXEC;
1438
        end
1439
 
1440
    if ( control_state == MTRANS_EXEC1 )
1441
        begin
1442
        if (mtrans_instruction_nxt[15:0] != 16'd0)
1443
            control_state_nxt = MTRANS_EXEC2;
1444
        else   // if the register list holds a single register 
1445
            control_state_nxt = MTRANS_EXEC3;
1446
        end
1447
 
1448
        // Stay in State MTRANS_EXEC2 until the full list of registers to
1449
        // load or store has been processed
1450
    if ( control_state == MTRANS_EXEC2 && mtrans_num_registers == 5'd1 )
1451
        control_state_nxt = MTRANS_EXEC3;
1452
 
1453
    if ( control_state == MTRANS_EXEC3 )     control_state_nxt = MTRANS_EXEC4;
1454
 
1455
    if ( control_state == MTRANS_EXEC3B )    control_state_nxt = MTRANS_EXEC4;
1456
 
1457
    if ( control_state == MTRANS_EXEC4  )
1458
        begin
1459
        if ( dabt ) // data abort
1460
            control_state_nxt = MTRANS5_ABORT;
1461
        else if (write_pc) // writing to the PC!! 
1462
            control_state_nxt = PC_STALL1;
1463
        else
1464
            control_state_nxt = PRE_FETCH_EXEC;
1465
        end
1466
 
1467
    if ( control_state == MULT_PROC1 )
1468
        begin
1469
        if (!instruction_execute)
1470
            control_state_nxt = PRE_FETCH_EXEC;
1471
        else
1472
            control_state_nxt = MULT_PROC2;
1473
        end
1474
 
1475
    if ( control_state == MULT_PROC2 )
1476
        begin
1477
        if ( i_multiply_done )
1478
            if      ( o_multiply_function[1] )  // Accumulate ?
1479
                control_state_nxt = MULT_ACCUMU;
1480
            else
1481
                control_state_nxt = MULT_STORE;
1482
        end
1483
 
1484
 
1485
    if ( control_state == MULT_ACCUMU )
1486
        begin
1487
        control_state_nxt = MULT_STORE;
1488
        end
1489
 
1490
 
1491
    // This should come at the end, so that conditional execution works
1492
    // correctly
1493
    if ( instruction_valid )
1494
        begin
1495
        // default is to stay in execute state, or to move into this
1496
        // state from a conditional execute state
1497
        control_state_nxt = EXECUTE;
1498
 
1499
        if ( mem_op )  // load or store word or byte
1500
             control_state_nxt = MEM_WAIT1;
1501
        if ( write_pc )
1502
             control_state_nxt = PC_STALL1;
1503 87 csantifort
        if ( itype == MTRANS )
1504 2 csantifort
            begin
1505
            if ( mtrans_num_registers != 5'd0 )
1506
                begin
1507
                // check for LDM/STM of a single register
1508
                if ( mtrans_num_registers == 5'd1 )
1509
                    control_state_nxt = MTRANS_EXEC3B;
1510
                else
1511
                    control_state_nxt = MTRANS_EXEC1;
1512
                end
1513
            else
1514
                control_state_nxt = MTRANS_EXEC3;
1515
            end
1516
 
1517 87 csantifort
        if ( itype == MULT )
1518 2 csantifort
                control_state_nxt = MULT_PROC1;
1519
 
1520 87 csantifort
        if ( itype == SWAP )
1521 2 csantifort
                control_state_nxt = SWAP_WRITE;
1522
 
1523 87 csantifort
        if ( itype == CORTRANS && !und_request )
1524 2 csantifort
                control_state_nxt = COPRO_WAIT;
1525
 
1526
         // interrupt overrides everything else so its last       
1527
        if ( interrupt )
1528
                control_state_nxt = INT_WAIT1;
1529
        end
1530
    end
1531
 
1532
 
1533
// ========================================================
1534
// Register Update
1535
// ========================================================
1536
always @ ( posedge i_clk )
1537
    if (!i_fetch_stall)
1538
        begin
1539
        o_read_data                 <= i_read_data;
1540
        o_read_data_alignment       <= {i_execute_address[1:0], 3'd0};
1541
        abt_address_reg             <= i_execute_address;
1542
        iabt_reg                    <= i_iabt;
1543
        adex_reg                    <= i_adex;
1544
        abt_status_reg              <= i_abt_status;
1545
        o_status_bits_mode          <= status_bits_mode_nxt;
1546
        o_status_bits_irq_mask      <= status_bits_irq_mask_nxt;
1547
        o_status_bits_firq_mask     <= status_bits_firq_mask_nxt;
1548
        o_imm32                     <= imm32_nxt;
1549
        o_imm_shift_amount          <= imm_shift_amount_nxt;
1550
        o_shift_imm_zero            <= shift_imm_zero_nxt;
1551
 
1552
                                        // when have an interrupt, execute the interrupt operation
1553
                                        // unconditionally in the execute stage
1554
                                        // ensures that status_bits register gets updated correctly
1555
                                        // Likewise when in middle of multi-cycle instructions
1556
                                        // execute them unconditionally
1557
        o_condition                 <= instruction_valid && !interrupt ? condition_nxt : AL;
1558
        o_exclusive_exec            <= exclusive_exec_nxt;
1559
        o_data_access_exec          <= data_access_exec_nxt;
1560
 
1561 71 csantifort
        o_rm_sel                    <= o_rm_sel_nxt;
1562
        o_rds_sel                   <= o_rds_sel_nxt;
1563
        o_rn_sel                    <= o_rn_sel_nxt;
1564 2 csantifort
        o_barrel_shift_amount_sel   <= barrel_shift_amount_sel_nxt;
1565
        o_barrel_shift_data_sel     <= barrel_shift_data_sel_nxt;
1566
        o_barrel_shift_function     <= barrel_shift_function_nxt;
1567
        o_alu_function              <= alu_function_nxt;
1568 83 csantifort
        o_use_carry_in              <= use_carry_in_nxt;
1569 2 csantifort
        o_multiply_function         <= multiply_function_nxt;
1570
        o_interrupt_vector_sel      <= next_interrupt;
1571
        o_address_sel               <= address_sel_nxt;
1572
        o_pc_sel                    <= pc_sel_nxt;
1573
        o_byte_enable_sel           <= byte_enable_sel_nxt;
1574
        o_status_bits_sel           <= status_bits_sel_nxt;
1575
        o_reg_write_sel             <= reg_write_sel_nxt;
1576
        o_user_mode_regs_load       <= user_mode_regs_load_nxt;
1577
        o_firq_not_user_mode        <= firq_not_user_mode_nxt;
1578
        o_write_data_wen            <= write_data_wen_nxt;
1579
        o_base_address_wen          <= base_address_wen_nxt;
1580
        o_pc_wen                    <= pc_wen_nxt;
1581 71 csantifort
        o_reg_bank_wsel             <= reg_bank_wsel_nxt;
1582
        o_reg_bank_wen              <= decode ( reg_bank_wsel_nxt );
1583 2 csantifort
        o_status_bits_flags_wen     <= status_bits_flags_wen_nxt;
1584
        o_status_bits_mode_wen      <= status_bits_mode_wen_nxt;
1585
        o_status_bits_irq_mask_wen  <= status_bits_irq_mask_wen_nxt;
1586
        o_status_bits_firq_mask_wen <= status_bits_firq_mask_wen_nxt;
1587
 
1588
        o_copro_opcode1             <= instruction[23:21];
1589
        o_copro_opcode2             <= instruction[7:5];
1590
        o_copro_crn                 <= instruction[19:16];
1591
        o_copro_crm                 <= instruction[3:0];
1592
        o_copro_num                 <= instruction[11:8];
1593
        o_copro_operation           <= copro_operation_nxt;
1594
        o_copro_write_data_wen      <= copro_write_data_wen_nxt;
1595
        mtrans_r15                  <= mtrans_r15_nxt;
1596
        restore_base_address        <= restore_base_address_nxt;
1597
        control_state               <= control_state_nxt;
1598
        mtrans_reg_d1               <= mtrans_reg;
1599
        mtrans_reg_d2               <= mtrans_reg_d1;
1600
        end
1601
 
1602
 
1603
 
1604
always @ ( posedge i_clk )
1605
    if ( !i_fetch_stall )
1606
        begin
1607
        // sometimes this is a pre-fetch instruction
1608
        // e.g. two ldr instructions in a row. The second ldr will be saved
1609
        // to the pre-fetch instruction register
1610
        // then when its decoded, a copy is saved to the saved_current_instruction
1611
        // register
1612 87 csantifort
        if      (itype == MTRANS)
1613 2 csantifort
            begin
1614
            saved_current_instruction              <= mtrans_instruction_nxt;
1615
            saved_current_instruction_iabt         <= instruction_iabt;
1616
            saved_current_instruction_adex         <= instruction_adex;
1617
            saved_current_instruction_address      <= instruction_address;
1618
            saved_current_instruction_iabt_status  <= instruction_iabt_status;
1619
            end
1620
        else if (saved_current_instruction_wen)
1621
            begin
1622
            saved_current_instruction              <= instruction;
1623
            saved_current_instruction_iabt         <= instruction_iabt;
1624
            saved_current_instruction_adex         <= instruction_adex;
1625
            saved_current_instruction_address      <= instruction_address;
1626
            saved_current_instruction_iabt_status  <= instruction_iabt_status;
1627
            end
1628
 
1629
        if      (pre_fetch_instruction_wen)
1630
            begin
1631
            pre_fetch_instruction                  <= o_read_data;
1632
            pre_fetch_instruction_iabt             <= iabt_reg;
1633
            pre_fetch_instruction_adex             <= adex_reg;
1634
            pre_fetch_instruction_address          <= abt_address_reg;
1635
            pre_fetch_instruction_iabt_status      <= abt_status_reg;
1636
            end
1637
        end
1638
 
1639
 
1640
 
1641
always @ ( posedge i_clk )
1642
    if ( !i_fetch_stall )
1643
        begin
1644
        irq   <= i_irq;
1645
        firq  <= i_firq;
1646
 
1647
        if ( control_state == INT_WAIT1 && o_status_bits_mode == SVC )
1648
            begin
1649
            dabt_reg  <= 1'd0;
1650
            end
1651
        else
1652
            begin
1653
            dabt_reg  <= dabt_reg || i_dabt;
1654
            end
1655
 
1656
        dabt_reg_d1  <= dabt_reg;
1657
        end
1658
 
1659
assign dabt = dabt_reg || i_dabt;
1660
 
1661
 
1662
// ========================================================
1663
// Decompiler for debugging core - not synthesizable
1664
// ========================================================
1665
//synopsys translate_off
1666
 
1667 82 csantifort
`include "debug_functions.vh"
1668 2 csantifort
 
1669 15 csantifort
a23_decompile  u_decompile (
1670 2 csantifort
    .i_clk                      ( i_clk                            ),
1671
    .i_fetch_stall              ( i_fetch_stall                    ),
1672
    .i_instruction              ( instruction                      ),
1673
    .i_instruction_valid        ( instruction_valid                ),
1674
    .i_instruction_execute      ( instruction_execute              ),
1675
    .i_instruction_address      ( instruction_address              ),
1676
    .i_interrupt                ( {3{interrupt}} & next_interrupt  ),
1677
    .i_interrupt_state          ( control_state == INT_WAIT2       ),
1678
    .i_instruction_undefined    ( und_request                      ),
1679
    .i_pc_sel                   ( o_pc_sel                         ),
1680
    .i_pc_wen                   ( o_pc_wen                         )
1681
);
1682
 
1683
 
1684
wire    [(15*8)-1:0]    xCONTROL_STATE;
1685
wire    [(15*8)-1:0]    xMODE;
1686
 
1687
assign xCONTROL_STATE        =
1688
                               control_state == RST_WAIT1      ? "RST_WAIT1"      :
1689
                               control_state == RST_WAIT2      ? "RST_WAIT2"      :
1690
 
1691
 
1692
                               control_state == INT_WAIT1      ? "INT_WAIT1"      :
1693
                               control_state == INT_WAIT2      ? "INT_WAIT2"      :
1694
                               control_state == EXECUTE        ? "EXECUTE"        :
1695
                               control_state == PRE_FETCH_EXEC ? "PRE_FETCH_EXEC" :
1696
                               control_state == MEM_WAIT1      ? "MEM_WAIT1"      :
1697
                               control_state == MEM_WAIT2      ? "MEM_WAIT2"      :
1698
                               control_state == PC_STALL1      ? "PC_STALL1"      :
1699
                               control_state == PC_STALL2      ? "PC_STALL2"      :
1700
                               control_state == MTRANS_EXEC1   ? "MTRANS_EXEC1"   :
1701
                               control_state == MTRANS_EXEC2   ? "MTRANS_EXEC2"   :
1702
                               control_state == MTRANS_EXEC3   ? "MTRANS_EXEC3"   :
1703
                               control_state == MTRANS_EXEC3B  ? "MTRANS_EXEC3B"  :
1704
                               control_state == MTRANS_EXEC4   ? "MTRANS_EXEC4"   :
1705
                               control_state == MTRANS5_ABORT  ? "MTRANS5_ABORT"  :
1706
                               control_state == MULT_PROC1     ? "MULT_PROC1"     :
1707
                               control_state == MULT_PROC2     ? "MULT_PROC2"     :
1708
                               control_state == MULT_STORE     ? "MULT_STORE"     :
1709
                               control_state == MULT_ACCUMU    ? "MULT_ACCUMU"    :
1710
                               control_state == SWAP_WRITE     ? "SWAP_WRITE"     :
1711
                               control_state == SWAP_WAIT1     ? "SWAP_WAIT1"     :
1712
                               control_state == SWAP_WAIT2     ? "SWAP_WAIT2"     :
1713
                               control_state == COPRO_WAIT     ? "COPRO_WAIT"     :
1714
                                                                 "UNKNOWN "       ;
1715
 
1716
assign xMODE  = mode_name ( o_status_bits_mode );
1717
 
1718
always @( posedge i_clk )
1719
    if (control_state == EXECUTE && ((instruction[0] === 1'bx) || (instruction[31] === 1'bx)))
1720
        begin
1721
        `TB_ERROR_MESSAGE
1722
        $display("Instruction with x's =%08h", instruction);
1723
        end
1724
//synopsys translate_on
1725
 
1726
endmodule
1727
 
1728
 

powered by: WebSVN 2.1.0

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