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

Subversion Repositories amber

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

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

powered by: WebSVN 2.1.0

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