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

Subversion Repositories arm4u

[/] [arm4u/] [trunk/] [hdl/] [decode.vhd] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 Bregalad
-- This file is part of ARM4U CPU
2
-- 
3
-- This is a creation of the Laboratory of Processor Architecture
4
-- of Ecole Polytechnique Fédérale de Lausanne ( http://lap.epfl.ch )
5
--
6
-- decode.vhd  --  Description of the decode pipeline stage
7
--
8
-- Written By -  Jonathan Masur and Xavier Jimenez (2013)
9
--
10
-- This program is free software; you can redistribute it and/or modify it
11
-- under the terms of the GNU General Public License as published by the
12
-- Free Software Foundation; either version 2, or (at your option) any
13
-- later version.
14
--
15
-- This program is distributed in the hope that it will be useful,
16
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
17
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
-- GNU General Public License for more details.
19
--
20
-- In other words, you are welcome to use, share and improve this program.
21
-- You are forbidden to forbid anyone else to use, share and improve
22
-- what you give them.   Help stamp out software-hoarding!
23
 
24
 
25
library ieee;
26
use ieee.std_logic_1164.all;
27
use ieee.numeric_std.all;
28
use work.arm_types.all;
29
 
30
entity decode is
31
port(
32
        clk : in std_logic;
33
        reset_n : in std_logic;
34
 
35
        fiq : in std_logic;
36
        irq : in std_logic;
37
        flush : in std_logic;
38
        low_flags : in std_logic_vector(5 downto 0);
39
        decode_stage_valid : in std_logic;
40
        inst_cache_miss : in std_logic;
41
        dec_pc_plus_4 : in unsigned(31 downto 0);
42
        dec_pc_plus_8 : in unsigned(31 downto 0);
43
 
44
        -- instruction being decoded
45
        inst_data : in std_logic_vector(31 downto 0);
46
 
47
        -- regiser file adresses
48
        rfile_A_adr : out std_logic_vector(4 downto 0);
49
        rfile_B_adr : out std_logic_vector(4 downto 0);
50
        rfile_C_adr : out std_logic_vector(4 downto 0);
51
 
52
        -- register operands
53
        exe_A_adr : out std_logic_vector(5 downto 0);
54
        exe_B_adr : out std_logic_vector(5 downto 0);
55
        exe_C_adr : out std_logic_vector(5 downto 0);
56
        exe_pc_plus_4 : out unsigned(31 downto 0);
57
        exe_pc_plus_8 : out unsigned(31 downto 0);
58
 
59
        -- output of the latch
60
        exe_stage_valid : out std_logic;
61
        exe_barrelshift_operand : out std_logic;
62
        exe_barrelshift_type : out std_logic_vector(1 downto 0);
63
        exe_literal_shift_amnt : out std_logic_vector(4 downto 0);
64
        exe_literal_data : out std_logic_vector(23 downto 0);
65
        exe_opb_is_literal : out std_logic;
66
        exe_opb_sel : out std_logic;
67
        exe_alu_operation : out ALU_OPERATION;
68
        exe_condition : out std_logic_vector(3 downto 0);
69
        exe_affect_sflags : out std_logic;
70
        exe_data_sel : out std_logic;
71
        exe_rdest_wren : out std_logic;
72
        exe_rdest_adr : out std_logic_vector(4 downto 0);
73
        exe_branch_en : out std_logic;
74
        exe_wb_sel : out std_logic;
75
        exe_mem_ctrl : out MEM_OPERATION;
76
        exe_mem_burstcount : out std_logic_vector(3 downto 0);
77
 
78
        -- 1 if the stage has completed an instruction for this cycle, 0 otherwise
79
        decode_blocked_n : out std_logic;
80
        -- enable signal for latch after the fetch stage
81
        decode_latch_enable : in std_logic
82
);
83
end entity decode;
84
 
85
architecture rtl of decode is
86
 
87
        signal state, next_state : DECODE_FSM;
88
 
89
        signal barrelshift_operand : std_logic;
90
        signal barrelshift_type : std_logic_vector(1 downto 0);
91
        signal literal_shift_amnt : std_logic_vector(4 downto 0);
92
        signal literal_data : std_logic_vector(23 downto 0);
93
        signal opb_is_literal : std_logic;
94
        signal opb_sel : std_logic;
95
        signal alu_operation : ALU_OPERATION;
96
        signal condition : std_logic_vector(3 downto 0);
97
        signal affect_sflags : std_logic;
98
        signal data_sel : std_logic;
99
        signal rdest_wren : std_logic;
100
        signal rdest_adr : std_logic_vector(4 downto 0);
101
        signal branch_en : std_logic;
102
        signal wb_sel : std_logic;
103
        signal mem_ctrl : MEM_OPERATION;
104
        signal mem_burstcount : std_logic_vector(3 downto 0);
105
        signal stage_active : std_logic;
106
 
107
        signal rfA_adr : std_logic_vector(5 downto 0);
108
        signal rfB_adr : std_logic_vector(5 downto 0);
109
        signal rfC_adr : std_logic_vector(5 downto 0);
110
 
111
        signal i, f, reset_l : std_logic;
112
        signal mode : std_logic_vector(3 downto 0);
113
 
114
        signal current_inst, current_inst_l : std_logic_vector(31 downto 0);
115
 
116
        signal ldmstm_cur_bitmask : std_logic_vector(15 downto 0);
117
        signal ldmstm_next_bitmask : std_logic_vector(15 downto 0);
118
        signal ldmstm_current_reg : integer range 0 to 15;
119
 
120
        -- remap register adresses (4 bits) to actual registers (5 adress bits)
121
        function address_remap(adr : std_logic_vector(3 downto 0);
122
                               mode : std_logic_vector(3 downto 0)) return std_logic_vector is
123
        begin
124
                -- source : http://www.heyrick.co.uk/armwiki/The_Status_register
125
                -- highest bit of "mode" is always 1 and is not implemented
126
                case mode is
127
                        when "0000" =>
128
                        --user mode
129
                                case adr is
130
                                        when "0000" => return r0;
131
                                        when "0001" => return r1;
132
                                        when "0010" => return r2;
133
                                        when "0011" => return r3;
134
                                        when "0100" => return r4;
135
                                        when "0101" => return r5;
136
                                        when "0110" => return r6;
137
                                        when "0111" => return r7;
138
                                        when "1000" => return r8;
139
                                        when "1001" => return r9;
140
                                        when "1010" => return r10;
141
                                        when "1011" => return r11;
142
                                        when "1100" => return r12;
143
                                        when "1101" => return r13;
144
                                        when "1110" => return r14;
145
                                        when others => return "-----";
146
                                end case;
147
 
148
                        when "0001" =>
149
                        -- FIQ mode
150
                                case adr is
151
                                        when "0000" => return r0;
152
                                        when "0001" => return r1;
153
                                        when "0010" => return r2;
154
                                        when "0011" => return r3;
155
                                        when "0100" => return r4;
156
                                        when "0101" => return r5;
157
                                        when "0110" => return r6;
158
                                        when "0111" => return r7;
159
                                        when "1000" => return fiq_r8;
160
                                        when "1001" => return fiq_r9;
161
                                        when "1010" => return fiq_r10;
162
                                        when "1011" => return fiq_r11;
163
                                        when "1100" => return fiq_r12;
164
                                        when "1101" => return fiq_r13;
165
                                        when "1110" => return fiq_r14;
166
                                        when others => return "-----";
167
                                end case;
168
 
169
                        when  "0010" =>
170
                        -- IRQ mode
171
                                case adr is
172
                                        when "0000" => return r0;
173
                                        when "0001" => return r1;
174
                                        when "0010" => return r2;
175
                                        when "0011" => return r3;
176
                                        when "0100" => return r4;
177
                                        when "0101" => return r5;
178
                                        when "0110" => return r6;
179
                                        when "0111" => return r7;
180
                                        when "1000" => return r8;
181
                                        when "1001" => return r9;
182
                                        when "1010" => return r10;
183
                                        when "1011" => return r11;
184
                                        when "1100" => return r12;
185
                                        when "1101" => return irq_r13;
186
                                        when "1110" => return irq_r14;
187
                                        when others => return "-----";
188
                                end case;
189
 
190
                        when "0011" =>
191
                        -- Supervisor (software interrupt) mode
192
                                case adr is
193
                                        when "0000" => return r0;
194
                                        when "0001" => return r1;
195
                                        when "0010" => return r2;
196
                                        when "0011" => return r3;
197
                                        when "0100" => return r4;
198
                                        when "0101" => return r5;
199
                                        when "0110" => return r6;
200
                                        when "0111" => return r7;
201
                                        when "1000" => return r8;
202
                                        when "1001" => return r9;
203
                                        when "1010" => return r10;
204
                                        when "1011" => return r11;
205
                                        when "1100" => return r12;
206
                                        when "1101" => return sup_r13;
207
                                        when "1110" => return sup_r14;
208
                                        when others => return "-----";
209
                                end case;
210
 
211
                        when "1011" =>
212
                        -- Undefined instruction mode
213
                                case adr is
214
                                        when "0000" => return r0;
215
                                        when "0001" => return r1;
216
                                        when "0010" => return r2;
217
                                        when "0011" => return r3;
218
                                        when "0100" => return r4;
219
                                        when "0101" => return r5;
220
                                        when "0110" => return r6;
221
                                        when "0111" => return r7;
222
                                        when "1000" => return r8;
223
                                        when "1001" => return r9;
224
                                        when "1010" => return r10;
225
                                        when "1011" => return r11;
226
                                        when "1100" => return r12;
227
                                        when "1101" => return und_r13;
228
                                        when "1110" => return und_r14;
229
                                        when others => return "-----";
230
                                end case;
231
 
232
                        when others => return "-----";
233
                        end case;
234
        end;
235
 
236
        function address_remap_pc(adr : std_logic_vector(3 downto 0);
237
                                  mode : std_logic_vector(3 downto 0)) return std_logic_vector is
238
        begin
239
                -- adr is PC
240
                if adr = "1111"
241
                then
242
                        return "1-----";
243
                else
244
                        return "0" & address_remap(adr, mode);
245
                end if;
246
        end;
247
 
248
        function address_remap_spsr(mode : std_logic_vector(3 downto 0)) return std_logic_vector is
249
        begin
250
                case mode is
251
                        -- FIQ mode
252
                        when "0001" => return fiq_spsr;
253
                        -- IRQ mode
254
                        when "0010" => return irq_spsr;
255
                        -- Supervisor (software interrupt) mode
256
                        when "0011" => return sup_spsr;
257
                        -- Undefined instruction mode
258
                        when "1011" => return und_spsr;
259
                        -- user mode and invalid mode - undefined implementation
260
                        when others => return "-----";
261
                end case;
262
        end;
263
 
264
        function count_set_bits(word : std_logic_vector) return integer is
265
                variable i, res : integer;
266
        begin
267
                res := 0;
268
                for i in 0 to word'length - 1 loop
269
                        if word(i) = '1'
270
                        then
271
                                res := res + 1;
272
                        end if;
273
                end loop;
274
                return res;
275
        end;
276
 
277
        function find_rightmost_bit(word : std_logic_vector(15 downto 0)) return integer is
278
                variable i : integer;
279
        begin
280
                i := 0;
281
                while i < 15 and word(i) = '0' loop
282
                        i := i + 1;
283
                end loop;
284
                return i;
285
        end;
286
 
287
begin
288
        -- separate low flags bits
289
        i <= low_flags(5);
290
        f <= low_flags(4);
291
        mode <= low_flags(3 downto 0);
292
 
293
        stage_active <= '1' when inst_cache_miss = '0' and decode_stage_valid = '1' and decode_latch_enable = '1' else '0';
294
 
295
        -- instruction latch
296
        process(clk) is
297
        begin
298
                if rising_edge(clk)
299
                then
300
                        current_inst_l <= current_inst;
301
                end if;
302
        end process;
303
        current_inst <= inst_data when stage_active = '1' and state = MAIN_STATE else current_inst_l;
304
 
305
        -- output latch
306
        process(clk) is
307
        begin
308
                if rising_edge(clk)
309
                then
310
                        if decode_latch_enable = '1'
311
                        then
312
                                exe_A_adr <= rfA_adr;
313
                                exe_B_adr <= rfB_adr;
314
                                exe_C_adr <= rfC_adr;
315
                                exe_pc_plus_8 <= dec_pc_plus_8;
316
                                exe_pc_plus_4 <= dec_pc_plus_4;
317
 
318
                                exe_stage_valid <= stage_active;
319
                                exe_barrelshift_operand <= barrelshift_operand;
320
                                exe_barrelshift_type <= barrelshift_type;
321
                                exe_literal_shift_amnt <= literal_shift_amnt;
322
                                exe_literal_data <= literal_data;
323
                                exe_opb_is_literal <= opb_is_literal;
324
                                exe_opb_sel <= opb_sel;
325
                                exe_alu_operation <= alu_operation;
326
                                exe_condition <= condition;
327
                                exe_affect_sflags <= affect_sflags;
328
                                exe_data_sel <= data_sel;
329
                                exe_rdest_wren <= rdest_wren;
330
                                exe_rdest_adr <= rdest_adr;
331
                                exe_branch_en <= branch_en;
332
                                exe_wb_sel <= wb_sel;
333
                                exe_mem_ctrl <= mem_ctrl;
334
                                exe_mem_burstcount <= mem_burstcount;
335
 
336
                                ldmstm_cur_bitmask <= ldmstm_next_bitmask;
337
                        end if;
338
                        if flush = '1'
339
                        then
340
                                exe_stage_valid <= '0';
341
                        end if;
342
                end if;
343
        end process;
344
 
345
        fsm : process(clk, reset_n) is
346
        begin
347
                if reset_n = '0'
348
                then
349
                        state <= MAIN_STATE;
350
                elsif rising_edge(clk)
351
                then
352
                        if stage_active = '1'
353
                        then
354
                                state <= next_state;
355
                        end if;
356
                end if;
357
        end process fsm;
358
 
359
        -- reset interrupt happens one cycle after reset_n is deasserted
360
        resetl : process(clk, reset_n) is
361
        begin
362
                if reset_n = '0'
363
                then
364
                        reset_l <= '1';
365
                elsif rising_edge(clk)
366
                then
367
                        if stage_active = '1'
368
                        then
369
                                reset_l <= '0';
370
                        end if;
371
                end if;
372
        end process resetl;
373
 
374
        ldmstm_current_reg <= find_rightmost_bit(current_inst(15 downto 0) and ldmstm_cur_bitmask);
375
 
376
        -- Decoding matrix : please consult the decode matrix spreadsheet for more information about how this works
377
        decode : process(state, flush, current_inst, low_flags, reset_l, irq, fiq, mode, f, i, stage_active, ldmstm_cur_bitmask, ldmstm_current_reg, ldmstm_next_bitmask) is
378
        begin
379
                -- default instruction condition
380
                condition <= current_inst(31 downto 28);
381
 
382
                -- behaviour in case of cache miss (the state should not be changed, and other values are don't care)
383
                next_state <= state;
384
 
385
                -- using PC as a "unused" register is good in the way it prevents forwarding to stall the processor without reason
386
                rfA_adr <= "1-----";
387
                rfB_adr <= "1-----";
388
                rfC_adr <= "1-----";
389
 
390
                rdest_adr <= (others=>'-');
391
                barrelshift_operand <= '-';
392
                barrelshift_type <= (others=>'-');
393
                literal_shift_amnt <= (others=>'-');
394
                literal_data <= (others=>'-');
395
                opb_is_literal <= '-';
396
                opb_sel <= '-';
397
                alu_operation <= ALU_NOP;
398
                affect_sflags <= '-';
399
                rdest_wren <= '-';
400
                data_sel <= '-';
401
                branch_en <= '-';
402
                wb_sel <= '-';
403
                mem_ctrl <= NO_MEM_OP;
404
                mem_burstcount <= "----";
405
                ldmstm_next_bitmask <= (others=>'1');
406
 
407
                if flush = '1'
408
                then
409
                        next_state <= MAIN_STATE;
410
                elsif stage_active = '1'
411
                then
412
                        if state = MAIN_STATE or state = RETURN_FROM_EXCEPTION or state = LDMSTM_TRANSFER
413
                        then
414
                                -- reset interrupt - used to move into user mode
415
                                if reset_l = '1'
416
                                then
417
                                        condition <= "1110";
418
                                        next_state <= RESET_CYCLE2;
419
                                        alu_operation <= ALU_RWF;
420
                                        affect_sflags <= '1';
421
                                        branch_en <= '0';
422
                                        rdest_wren <= '0';
423
                                        mem_ctrl <= NO_MEM_OP;
424
 
425
                                        -- Enter USR mode, clears IRQ and FIQ flags
426
                                        barrelshift_operand <= '0';
427
                                        barrelshift_type <= "00";
428
                                        literal_shift_amnt <= (others => '0');
429
                                        literal_data <= (23 downto 8 => '0') & "00010000";
430
                                        opb_is_literal <= '1';
431
                                        opb_sel <= '0';
432
 
433
                                -- See if a FIQ interrupt is pending
434
                                elsif state = MAIN_STATE and f = '0' and fiq = '1'
435
                                then
436
                                        condition <= "1110";            -- force ALWAYS condition, so that the FIQ is always executed !
437
                                        next_state <= FIQ_CYCLE2;
438
                                        -- read CPSR and save it in fiq_SPSR, and also write new value to CPSR at the same time
439
                                        alu_operation <= ALU_RWF;
440
                                        affect_sflags <= '1';
441
                                        data_sel <= '1';
442
                                        branch_en <= '0';
443
                                        rdest_wren <= '1';
444
                                        wb_sel <= '0';
445
                                        mem_ctrl <= NO_MEM_OP;
446
                                        rdest_adr <= fiq_SPSR;
447
 
448
                                        -- Enter FIQ mode, set FIQ and IRQ flags
449
                                        barrelshift_operand <= '0';
450
                                        barrelshift_type <= "00";
451
                                        literal_shift_amnt <= (others => '0');
452
                                        literal_data <= (23 downto 8 => '0') & "11010001";
453
                                        opb_is_literal <= '1';
454
                                        opb_sel <= '0';
455
 
456
                                -- See if an IRQ interrupt is pending
457
                                elsif state = MAIN_STATE and i = '0' and irq = '1'
458
                                then
459
                                        condition <= "1110";            -- force ALWAYS condition, so that the IRQ is always executed !
460
                                        next_state <= IRQ_CYCLE2;
461
                                        -- read CPSR and save it in fiq_SPSR, and also write new value to CPSR at the same time
462
                                        alu_operation <= ALU_RWF;
463
                                        affect_sflags <= '1';
464
                                        data_sel <= '1';
465
                                        branch_en <= '0';
466
                                        rdest_wren <= '1';
467
                                        wb_sel <= '0';
468
                                        mem_ctrl <= NO_MEM_OP;
469
                                        rdest_adr <= irq_SPSR;
470
 
471
                                        -- Enter IRQ mode, set IRQ flag, clear FIQ flag
472
                                        barrelshift_operand <= '0';
473
                                        barrelshift_type <= "00";
474
                                        literal_shift_amnt <= (others => '0');
475
                                        literal_data <= (23 downto 8 => '0') & "10010010";
476
                                        opb_is_literal <= '1';
477
                                        opb_sel <= '0';
478
 
479
                                -- ** MUL/MLA instructions **
480
                                elsif current_inst(27 downto 22) = "000000" and current_inst(7 downto 4) = "1001"
481
                                then
482
                                        -- single cycle instruction
483
                                        next_state <= MAIN_STATE;
484
 
485
                                        -- ALU adds or does nothing
486
                                        if current_inst(21) = '0'
487
                                        then
488
                                                alu_operation <= ALU_NOP;
489
                                        else
490
                                                alu_operation <= ALU_ADD;
491
                                        end if;
492
 
493
                                        -- use multiplier
494
                                        opb_sel <= '1';
495
                                        affect_sflags <= current_inst(20);
496
                                        data_sel <= '1';
497
                                        branch_en <= '0';
498
                                        rdest_wren <= '1';
499
                                        wb_sel <= '0';
500
                                        -- no memory access
501
                                        mem_ctrl <= NO_MEM_OP;
502
 
503
                                        rdest_adr <= address_remap(current_inst(19 downto 16), mode);
504
                                        rfA_adr <= address_remap_pc(current_inst(15 downto 12), mode);
505
                                        rfB_adr <= address_remap_pc(current_inst(3 downto 0), mode);
506
                                        rfC_adr <= address_remap_pc(current_inst(11 downto 8), mode);
507
 
508
 
509
                                -- ** MSR instruction **
510
                                elsif (current_inst(27 downto 26) = "00" and current_inst(24 downto 23) = "10"
511
                                and current_inst(21 downto 20) = "10" and current_inst(15 downto 12) = "1111")
512
                                and (current_inst(25) = '1' or current_inst(11 downto 4) = X"00")
513
                                then
514
                                        if current_inst(22) = '0'
515
                                        then
516
                                                -- ** write to CPSR **
517
                                                alu_operation <= ALU_RWF;
518
                                                -- wait an additional cycle, so that the decoding of next instruction is done in correct mode
519
                                                next_state <= ONE_LATENCY_CYCLE;
520
                                                -- don't write any register, write to flags
521
                                                affect_sflags <= '1';
522
                                                rdest_wren <= '0';
523
                                        else
524
                                                -- ** write to SPSR **
525
                                                alu_operation <= ALU_NOP;
526
                                                -- no need for a latency cycle, don't write flags, but need for writeback
527
                                                next_state <= MAIN_STATE;
528
                                                affect_sflags <= '0';
529
                                                rdest_wren <= '1';
530
                                        end if;
531
                                        data_sel <= '1';
532
                                        wb_sel <= '0';
533
                                        rdest_adr <= address_remap_spsr(mode);
534
 
535
                                        -- use barrel shifter
536
                                        opb_sel <= '0';
537
                                        branch_en <= '0';
538
                                        -- no memory access
539
                                        mem_ctrl <= NO_MEM_OP;
540
 
541
                                        -- rotate by literal value
542
                                        barrelshift_operand <= '0';
543
                                        if current_inst(25) = '1'
544
                                        then
545
                                                -- rotated constant adressing
546
                                                if current_inst(11 downto 8) = "0000" then
547
                                                        -- no rotation - LSL #0
548
                                                        barrelshift_type <= "00";
549
                                                        literal_shift_amnt <= (others => '0');
550
                                                else
551
                                                        -- use ROR barrelshift to compute rotated constant
552
                                                        literal_shift_amnt <= current_inst(11 downto 8) & '0';
553
                                                        barrelshift_type <= "11";
554
                                                end if;
555
                                                opb_is_literal <= '1';
556
                                                literal_data <= (23 downto 8 => '0') & current_inst(7 downto 0);
557
                                        else
558
                                                -- register adressing, use LSL #0 (i.e. no modification of the register)
559
                                                barrelshift_type <= "00";
560
                                                literal_shift_amnt <= (others => '0');
561
                                                opb_is_literal <= '0';
562
                                        end if;
563
                                        rfB_adr <= address_remap_pc(current_inst(3 downto 0), mode);
564
 
565
                                -- ** MRS instruction **
566
                                elsif current_inst(27 downto 23) = "00010" and current_inst(21 downto 16) = "001111" and current_inst(11 downto 0) = X"000"
567
                                then
568
                                        next_state <= MAIN_STATE;
569
 
570
                                        if current_inst(22) = '0'
571
                                        then
572
                                                -- ** Read CPSR **
573
                                                alu_operation <= ALU_RWF;
574
                                        else
575
                                                -- ** Read SPSR **, use LSL #0 (i.e. no modification of the register)
576
                                                alu_operation <= ALU_NOP;
577
                                        end if;
578
 
579
                                        opb_sel <= '0';
580
                                        rfB_adr <= '0' & address_remap_spsr(mode);
581
                                        barrelshift_operand <= '0';
582
                                        barrelshift_type <= "00";
583
                                        literal_shift_amnt <= (others => '0');
584
                                        opb_is_literal <= '0';
585
 
586
                                        -- don't affect sflags, always write back ALU data
587
                                        affect_sflags <= '0';
588
                                        data_sel <= '1';
589
                                        branch_en <= '0';
590
                                        rdest_wren <= '1';
591
                                        wb_sel <= '0';
592
                                        mem_ctrl <= NO_MEM_OP;
593
                                        rdest_adr <= address_remap(current_inst(15 downto 12), mode);
594
 
595
                                -- ** data processing instructions **
596
                                elsif current_inst(27 downto 26) = "00"
597
                                and ((current_inst(4)='0' or (current_inst(4)='1' and current_inst(7)='0')) or current_inst(25)='1')
598
                                then
599
                                        -- this is (normally) a single cycle instruction
600
                                        next_state <= MAIN_STATE;
601
 
602
                                        case current_inst(24 downto 21) is
603
                                        -- ADC instruction
604
                                        when "0101" => alu_operation <= ALU_ADC;
605
                                        -- ADD instruction
606
                                        when "0100" => alu_operation <= ALU_ADD;
607
                                        -- AND instruction
608
                                        when "0000" => alu_operation <= ALU_AND;
609
                                        -- BIC instruction
610
                                        when "1110" => alu_operation <= ALU_BIC;
611
                                        -- CMN instruction
612
                                        when "1011" => alu_operation <= ALU_ADD;
613
                                        -- CMP instruction
614
                                        when "1010" => alu_operation <= ALU_SUB;
615
                                        -- EOR instruction
616
                                        when "0001" => alu_operation <= ALU_EOR;
617
                                        -- MVN instruction
618
                                        when "1111" => alu_operation <= ALU_NOT;
619
                                        -- ORR instruction
620
                                        when "1100" => alu_operation <= ALU_ORR;
621
                                        -- RSB instruction
622
                                        when "0011" => alu_operation <= ALU_RSB;
623
                                        -- RSC instruction
624
                                        when "0111" => alu_operation <= ALU_RSC;
625
                                        -- SBC instruction
626
                                        when "0110" => alu_operation <= ALU_SBC;
627
                                        -- SUB instruction
628
                                        when "0010" => alu_operation <= ALU_SUB;
629
                                        -- TEQ instruction
630
                                        when "1001" => alu_operation <= ALU_EOR;
631
                                        -- TST instruction
632
                                        when "1000" => alu_operation <= ALU_AND;
633
                                        -- MOV instruction
634
                                        when others => alu_operation <= ALU_NOP;
635
                                        end case;
636
 
637
                                        -- use barrelshifter
638
                                        opb_sel <= '0';
639
 
640
                                        -- CMP, CMN, TEQ and TST instructions always affect sflags, never write enable
641
                                        if current_inst(24 downto 23) = "10"
642
                                        then
643
                                                affect_sflags <= '1';
644
                                                rdest_wren <= '0';
645
                                        else
646
                                                -- other instructions
647
                                                affect_sflags <= current_inst(20);
648
                                                if current_inst(15 downto 12) = "1111"
649
                                                then
650
                                                        rdest_wren <= '0';
651
                                                else
652
                                                        rdest_wren <= '1';
653
                                                end if;
654
                                        end if;
655
 
656
                                        -- use ALU result, writeback from ALU, no memory operation
657
                                        data_sel <= '1';
658
                                        wb_sel <= '0';
659
                                        mem_ctrl <= NO_MEM_OP;
660
 
661
                                        literal_data <= (23 downto 8 => '0') & current_inst(7 downto 0);
662
                                        opb_is_literal <= current_inst(25);
663
 
664
                                        rdest_adr <= address_remap(current_inst(15 downto 12), mode);
665
                                        rfA_adr <= address_remap_pc(current_inst(19 downto 16), mode);
666
                                        rfB_adr <= address_remap_pc(current_inst(3 downto 0), mode);
667
                                        rfC_adr <= address_remap_pc(current_inst(11 downto 8), mode);
668
 
669
                                        -- adressing mode decoding
670
                                        if current_inst(4)='1' and current_inst(25)='0'
671
                                        then
672
                                                barrelshift_operand <= '1' ;
673
                                        else
674
                                                barrelshift_operand <= '0';
675
                                        end if;
676
                                        if current_inst(25) = '0'
677
                                        then
678
                                                -- rotated register adressing
679
                                                barrelshift_type <= current_inst(6 downto 5);
680
                                                literal_shift_amnt <= current_inst(11 downto 7);
681
                                        else
682
                                                if current_inst(11 downto 8) = "0000"
683
                                                then
684
                                                        -- no rotation - LSL #0
685
                                                        barrelshift_type <= "00";
686
                                                        literal_shift_amnt <= (others => '0');
687
                                                else
688
                                                        -- use ROR barrelshift to compute rotated constant
689
                                                        literal_shift_amnt <= current_inst(11 downto 8) & '0';
690
                                                        barrelshift_type <= "11";
691
                                                end if;
692
                                        end if;
693
 
694
                                        -- is rdest = r15 = PC (hidden jump instruction) ?
695
                                        if current_inst(15 downto 12) = "1111"
696
                                        then
697
                                                if current_inst(20) = '0'
698
                                                then
699
                                                        branch_en <= '1';
700
                                                else
701
                                                        -- special 2 cycle return from instruction
702
                                                        if state = MAIN_STATE
703
                                                        then
704
                                                                -- 1st cycle : move the SPSR into CPSR
705
                                                                affect_sflags <= '1';           -- ALWAYS affect sflags
706
 
707
                                                                -- ** Read SPSR **, use LSL #0 (i.e. no modification of the register)
708
                                                                barrelshift_operand <= '0';
709
                                                                barrelshift_type <= "00";
710
                                                                literal_shift_amnt <= (others => '0');
711
                                                                opb_is_literal <= '0';
712
                                                                rfB_adr <= '0' & address_remap_spsr(mode);
713
 
714
                                                                -- write flags and execute the real instruction on next cycle
715
                                                                alu_operation <= ALU_RWF;
716
                                                                branch_en <= '0';
717
                                                                next_state <= RETURN_FROM_EXCEPTION;
718
 
719
                                                        else
720
                                                                -- 2nd cycle of a ***S R15, **** instruction
721
                                                                -- which is used to return from an exeption
722
                                                                branch_en <= '1';
723
                                                                affect_sflags <= '0';    -- do NOT affect sflags, obviously
724
                                                        end if;
725
                                                end if;
726
                                        else
727
                                                branch_en <= '0' ;
728
                                        end if;
729
 
730
                                -- ** branch instructions **
731
                                elsif current_inst(27 downto 25) = "101"
732
                                then
733
                                        next_state <= MAIN_STATE;
734
                                        alu_operation <= ALU_ADD;
735
                                        opb_sel <= '0';
736
                                        affect_sflags <= '0';
737
                                        branch_en <= '1';
738
 
739
                                        data_sel <= '0';
740
                                        if(current_inst(24) = '1')
741
                                        then
742
                                        -- BL, write PC+4 into R14 of current mode
743
                                                rdest_wren <= '1';
744
                                        else
745
                                        -- normal branch
746
                                                rdest_wren <= '0';
747
                                        end if;
748
                                        wb_sel <= '0';
749
                                        rdest_adr <= address_remap("1110", mode);
750
 
751
                                        rfA_adr <= "100000";
752
 
753
                                        -- multiply operand by 4 (LSL #2)
754
                                        barrelshift_operand <= '0';
755
                                        barrelshift_type <= "00";
756
                                        literal_shift_amnt <= "00010";
757
                                        literal_data <= current_inst(23 downto 0);
758
                                        opb_is_literal <= '1';
759
                                        mem_ctrl <= NO_MEM_OP;
760
 
761
                                -- ** LDR(B) instruction **
762
                                elsif current_inst(27 downto 26) = "01" and current_inst(20) = '1'
763
                                and ((current_inst(21) = '0') or (current_inst(21) = '1' and current_inst(24) = '1'))
764
                                then
765
                                        -- if pre or post-indexed adressing with writeback enabled, a second state is needed
766
                                        if current_inst(24) = current_inst(21)
767
                                        then
768
                                                next_state <= LOADSTORE_WRITEBACK;
769
                                        else
770
                                                if current_inst(15 downto 12) = "1111"
771
                                                then            -- insert two bubbles if loading into R15
772
                                                        next_state <= TWO_LATENCY_CYCLES;
773
                                                else
774
                                                        next_state <= MAIN_STATE;
775
                                                end if;
776
                                        end if;
777
 
778
                                        -- add/sub address (common lines for all load/store instructions cycles)
779
                                        if current_inst(23) = '0'
780
                                        then
781
                                                alu_operation <= ALU_SUB;
782
                                        else
783
                                                alu_operation <= ALU_ADD;
784
                                        end if;
785
                                        affect_sflags <= '0';
786
                                        opb_sel <= '0';
787
                                        barrelshift_operand <= '0';
788
                                        data_sel <= '1';
789
 
790
                                        -- branch if loading to R15, else writeback
791
                                        if current_inst(15 downto 12) = "1111"
792
                                        then
793
                                                branch_en <= '1';
794
                                                rdest_wren <= '0';
795
                                        else
796
                                                branch_en <= '0';
797
                                                rdest_wren <= '1';
798
                                        end if;
799
 
800
                                        -- write back from data bus
801
                                        wb_sel <= '1';
802
                                        rdest_adr <= address_remap(current_inst(15 downto 12), mode);
803
 
804
                                        if current_inst(22) = '1'
805
                                        then
806
                                                mem_ctrl <= LOAD_BYTE;
807
                                        else
808
                                                mem_ctrl <= LOAD_WORD;
809
                                        end if;
810
                                        mem_burstcount <= "0001";
811
 
812
                                        rfA_adr <= address_remap_pc(current_inst(19 downto 16), mode);
813
                                        rfB_adr <= address_remap_pc(current_inst(3 downto 0), mode);
814
 
815
                                        -- offset adressing
816
                                        if current_inst(25) = '0'
817
                                        then
818
                                                barrelshift_type <= "00";       -- LSR #00
819
                                                literal_shift_amnt <= (others => '0');
820
                                                literal_data <= (23 downto 12 => '0') & current_inst(11 downto 0);
821
                                                opb_is_literal <= '1';
822
                                        else
823
                                        -- register addressing
824
                                                barrelshift_type <= current_inst(6 downto 5);
825
                                                literal_shift_amnt <= current_inst(11 downto 7);
826
                                                opb_is_literal <= '0';
827
                                        end if;
828
                                        literal_data <= (23 downto 12 => '0') & current_inst(11 downto 0);
829
 
830
                                        if current_inst(24) = '0'
831
                                        then
832
                                                literal_data <= (others => '0');
833
                                                opb_is_literal <= '1';
834
                                                barrelshift_type <= "00";
835
                                        end if;
836
 
837
                                -- ** STR(B) instruction **
838
                                elsif current_inst(27 downto 26) = "01" and current_inst(20) = '0'
839
                                and ((current_inst(21) = '0') or (current_inst(21) = '1' and current_inst(24) = '1'))
840
                                then
841
                                        if current_inst(24) = '0'
842
                                        then
843
                                                next_state <= LOADSTORE_WRITEBACK;
844
                                        else
845
                                                next_state <= MAIN_STATE;
846
                                        end if;
847
 
848
                                        -- pre-indexing : add/sub address
849
                                        if current_inst(23) = '0'
850
                                        then
851
                                                alu_operation <= ALU_SUB;
852
                                        else
853
                                                alu_operation <= ALU_ADD;
854
                                        end if;
855
 
856
                                        affect_sflags <= '0';
857
                                        opb_sel <= '0';
858
                                        barrelshift_operand <= '0';
859
                                        data_sel <= '1';
860
 
861
                                        branch_en <= '0';
862
                                        wb_sel <= '0';
863
                                        rdest_adr <= address_remap(current_inst(19 downto 16), mode);
864
                                        rdest_wren <= current_inst(21);
865
 
866
                                        if current_inst(22) = '1'
867
                                        then
868
                                                mem_ctrl <= STORE_BYTE;
869
                                        else
870
                                                mem_ctrl <= STORE_WORD;
871
                                        end if;
872
                                        mem_burstcount <= "0001";
873
 
874
                                        rfA_adr <= address_remap_pc(current_inst(19 downto 16), mode);
875
                                        rfB_adr <= address_remap_pc(current_inst(3 downto 0), mode);
876
                                        rfC_adr <= address_remap_pc(current_inst(15 downto 12), mode);
877
 
878
                                        -- offset adressing
879
                                        if current_inst(25) = '0'
880
                                        then
881
                                                barrelshift_type <= "00";       -- LSR #00
882
                                                literal_shift_amnt <= (others => '0');
883
                                                opb_is_literal <= '1';
884
                                        else
885
                                        -- register addressing
886
                                                barrelshift_type <= current_inst(6 downto 5);
887
                                                literal_shift_amnt <= current_inst(11 downto 7);
888
                                                opb_is_literal <= '0';
889
                                        end if;
890
                                        literal_data <= (23 downto 12 => '0') & current_inst(11 downto 0);
891
 
892
                                        if current_inst(24) = '0'
893
                                        then
894
                                                literal_data <= (others => '0');
895
                                                opb_is_literal <= '1';
896
                                                barrelshift_type <= "00";
897
                                        end if;
898
 
899
                                -- LDM/STM instruction, procceed to burst transfter start
900
                                elsif current_inst(27 downto 25) = "100" and current_inst(15 downto 0) /= x"0000"
901
                                then
902
                                        -- ldmstm_current_reg := 0;
903
 
904
                                        -- while current_inst(ldmstm_current_reg) = '0' and ldmstm_cur_bitmask(ldmstm_current_reg) = '0' loop
905
                                                -- ldmstm_current_reg := ldmstm_current_reg + 1;
906
                                        -- end loop;                            
907
 
908
                                        -- Compute mask for next cycle, excluding the current register
909
                                        for n in 0 to 15
910
                                        loop
911
                                                if n > ldmstm_current_reg
912
                                                then
913
                                                        ldmstm_next_bitmask(n) <= '1';
914
                                                else
915
                                                        ldmstm_next_bitmask(n) <= '0';
916
                                                end if;
917
                                        end loop;
918
 
919
                                        -- Don't branch by default
920
                                        branch_en <= '0';
921
 
922
                                        -- Check if we are done with the LDM/STM transfer
923
                                        if (ldmstm_next_bitmask and current_inst(15 downto 0)) = x"0000"
924
                                        then
925
                                                -- if we are loading from R15, we should branch
926
                                                if current_inst(20) = '1' and current_inst(15) = '1'
927
                                                then
928
                                                        branch_en <= '1';
929
                                                        -- if the 'S' flag is set, we should move SPSR to CPSR, and do a potential writeback the following cycle
930
                                                        if current_inst(22) = '1'
931
                                                        then
932
                                                                next_state <= LDMSTM_RETURN_FROM_EXCEPTION;
933
                                                        -- 'S' flag is clear, directly do the writeback, then insert a bubble
934
                                                        elsif current_inst(21) = '1'
935
                                                        then
936
                                                                next_state <= LDMSTM_WRITEBACK;
937
                                                        -- Neither of those are true -> insert two bubbles
938
                                                        else
939
                                                                next_state <= TWO_LATENCY_CYCLES;
940
                                                        end if;
941
 
942
                                                -- Is writeback enabled in something that is not STMDB ?
943
                                                elsif current_inst(21) = '1' and (current_inst(24 downto 23) /= "10" or current_inst(20) /= '0')
944
                                                then
945
                                                        -- Yes -> a writeback cycle should follow
946
                                                        next_state <= LDMSTM_WRITEBACK;
947
                                                else
948
                                                        -- No -> continue code execution
949
                                                        next_state <= MAIN_STATE;
950
                                                end if;
951
                                        else
952
                                                next_state <= LDMSTM_TRANSFER;
953
                                        end if;
954
 
955
                                        -- Use SUB, for IA and IB addressing, use ADD
956
                                        if current_inst(23) = '0'
957
                                        then
958
                                                alu_operation <= ALU_SUB;
959
                                        else
960
                                                alu_operation <= ALU_ADD;
961
                                        end if;
962
 
963
                                        -- Use barrelshifter in all cases
964
                                        opb_sel <= '0';
965
                                        affect_sflags <= '0';
966
                                        data_sel <= '1';
967
                                        barrelshift_operand <= '1';
968
 
969
                                        -- If we are loading a register which is not R15, writeback si enabled
970
                                        if (current_inst(20) = '1' and ldmstm_current_reg /= 15)
971
                                        -- If base writeback is enabled and we are in a STMDB instruction, writeback is enabled
972
                                        or (current_inst(24 downto 23) = "10" and current_inst(21 downto 20) = "10" and state = MAIN_STATE)
973
                                        then
974
                                                rdest_wren <= '1';
975
                                        else
976
                                                rdest_wren <= '0';
977
                                        end if;
978
 
979
                                        wb_sel <= current_inst(20);
980
 
981
                                        if current_inst(20) = '1'
982
                                        then
983
                                                -- Load from memory in the case of LDM
984
                                                wb_sel <= '1';
985
 
986
                                                -- if S bit is clear or r15 is in the list (ret. from interrupt), load into current mode registers
987
                                                if current_inst(22) = '0' or current_inst(15) = '1'
988
                                                then
989
                                                        rdest_adr <= address_remap(std_logic_vector(to_unsigned(ldmstm_current_reg, 4) ), mode);
990
                                                else
991
                                                        -- S bit is set and r15 is not in the list, load into user mode registers
992
                                                        rdest_adr <= address_remap(std_logic_vector(to_unsigned(ldmstm_current_reg, 4) ), "0000");
993
                                                end if;
994
 
995
                                                -- Start a burst if this is the 1st cylce, else continue a burst
996
                                                if state = MAIN_STATE
997
                                                then
998
                                                        mem_ctrl <= LOAD_WORD;
999
                                                else
1000
                                                        mem_ctrl <= LOAD_BURST;
1001
                                                end if;
1002
                                        else
1003
                                                -- Write back the address (STM, only actually used in STMDB, as it's the only case where the address is correct)
1004
                                                wb_sel <= '0';
1005
 
1006
                                                rdest_adr <= address_remap(current_inst(19 downto 16), mode);
1007
                                                mem_ctrl <= STORE_WORD;
1008
                                        end if;
1009
 
1010
                                        -- Send the number of transfers that should be done
1011
                                        mem_burstcount <= std_logic_vector(to_unsigned(count_set_bits(current_inst(15 downto 0)) , 4));
1012
 
1013
                                        rfA_adr <= address_remap_pc(current_inst(19 downto 16), mode);
1014
 
1015
                                        if current_inst(22) = '0'
1016
                                        then
1017
                                                rfC_adr <= address_remap_pc(std_logic_vector(to_unsigned(ldmstm_current_reg, 4) ), mode);
1018
                                        else
1019
                                                -- S bit is set, store user mode registers insted of current mode
1020
                                                rfC_adr <= address_remap_pc(std_logic_vector(to_unsigned(ldmstm_current_reg, 4) ), "0000");
1021
                                        end if;
1022
 
1023
                                        -- LSL #0
1024
                                        barrelshift_type <= "00";
1025
                                        barrelshift_operand <= '0';
1026
                                        literal_shift_amnt <= "00010";
1027
 
1028
                                        case current_inst(24 downto 23) is
1029
                                        -- IA
1030
                                        when "01" => literal_data <= x"000000";
1031
                                        -- IB
1032
                                        when "11" => literal_data <= x"000001";
1033
                                        -- DA
1034
                                        when "00" => literal_data <= std_logic_vector(to_signed(count_set_bits(current_inst(15 downto 0)) - 1, 24));
1035
                                        -- DB
1036
                                        when others =>  literal_data <= std_logic_vector(to_signed(count_set_bits(current_inst(15 downto 0)), 24));
1037
                                        end case;
1038
 
1039
                                        opb_is_literal <= '1';
1040
 
1041
                                -- SWI (software interrupt) instruction
1042
                                elsif current_inst(27 downto 24) = "1111"
1043
                                then
1044
                                        next_state <= SWI_CYCLE2;
1045
                                        -- read CPSR and save it in sup_SPSR, and also write new value to CPSR at the same time
1046
                                        alu_operation <= ALU_RWF;
1047
                                        affect_sflags <= '1';
1048
                                        data_sel <= '1';
1049
                                        branch_en <= '0';
1050
                                        rdest_wren <= '1';
1051
                                        wb_sel <= '0';
1052
                                        mem_ctrl <= NO_MEM_OP;
1053
                                        rdest_adr <= sup_SPSR;
1054
 
1055
                                        -- Enter supervisor mode, clears FIQ or IRQ flags
1056
                                        barrelshift_operand <= '0';
1057
                                        barrelshift_type <= "00";
1058
                                        literal_shift_amnt <= (others => '0');
1059
                                        literal_data <= (23 downto 8 => '0') & "00010011";
1060
                                        opb_is_literal <= '1';
1061
                                        opb_sel <= '0';
1062
 
1063
                                -- ** Undefined instruction **
1064
                                else
1065
                                        -- Could not decode the instruction, start an undefined instruction trap interrupt
1066
                                        next_state <= UNDEF_CYCLE2;
1067
                                        -- read CPSR and save it in fiq_SPSR, and also write new value to CPSR at the same time
1068
                                        alu_operation <= ALU_RWF;
1069
                                        affect_sflags <= '1';
1070
                                        data_sel <= '1';
1071
                                        branch_en <= '0';
1072
                                        rdest_wren <= '1';
1073
                                        wb_sel <= '0';
1074
                                        mem_ctrl <= NO_MEM_OP;
1075
                                        rdest_adr <= und_SPSR;
1076
 
1077
                                        -- Enter UNDEF mode, clears FIQ and IRQ flags
1078
                                        barrelshift_operand <= '0';
1079
                                        barrelshift_type <= "00";
1080
                                        literal_shift_amnt <= (others => '0');
1081
                                        literal_data <= (23 downto 8 => '0') & "00011011";
1082
                                        opb_is_literal <= '1';
1083
                                        opb_sel <= '0';
1084
                                end if;
1085
 
1086
                        elsif state = TWO_LATENCY_CYCLES
1087
                        then
1088
                                -- 2 dummy latency cycles which does nothing
1089
                                next_state <= ONE_LATENCY_CYCLE;
1090
                                affect_sflags <= '0';
1091
                                rdest_wren <= '0';
1092
                                branch_en <= '0';
1093
                                mem_ctrl <= NO_MEM_OP;
1094
 
1095
                        elsif state = ONE_LATENCY_CYCLE
1096
                        then
1097
                                -- a dummy latency cycle which do nothing
1098
                                next_state <= MAIN_STATE;
1099
 
1100
                                affect_sflags <= '0';
1101
                                rdest_wren <= '0';
1102
                                branch_en <= '0';
1103
                                mem_ctrl <= NO_MEM_OP;
1104
 
1105
                        -- Rn writeback state after a load or a store
1106
                        elsif state = LOADSTORE_WRITEBACK
1107
                        then
1108
                                -- one more latency cycle needed if loading from PC
1109
                                if current_inst(20) = '1' and current_inst(15 downto 12) = "1111"
1110
                                then
1111
                                        next_state <= ONE_LATENCY_CYCLE;
1112
                                else
1113
                                        next_state <= MAIN_STATE;
1114
                                end if;
1115
 
1116
                                -- add/sub address
1117
                                if current_inst(23) = '1'
1118
                                then
1119
                                        alu_operation <= ALU_ADD;
1120
                                else
1121
                                        alu_operation <= ALU_SUB;
1122
                                end if;
1123
                                affect_sflags <= '0';
1124
                                opb_sel <= '0';
1125
                                barrelshift_operand <= '0';
1126
                                data_sel <= '1';
1127
 
1128
                                branch_en <= '0';
1129
                                rdest_wren <= '1';
1130
                                wb_sel <= '0';
1131
                                rdest_adr <= address_remap(current_inst(19 downto 16), mode);
1132
                                mem_ctrl <= NO_MEM_OP;
1133
 
1134
                                rfA_adr <= address_remap_pc(current_inst(19 downto 16), mode);
1135
                                rfB_adr <= address_remap_pc(current_inst(3 downto 0), mode);
1136
 
1137
                                -- offset adressing
1138
                                if current_inst(25) = '0'
1139
                                then
1140
                                        barrelshift_type <= "00";       -- LSR #00
1141
                                        literal_shift_amnt <= (others => '0');
1142
                                        literal_data <= (23 downto 12 => '0') & current_inst(11 downto 0);
1143
                                        opb_is_literal <= '1';
1144
                                else
1145
                                -- register addressing
1146
                                        barrelshift_type <= current_inst(6 downto 5);
1147
                                        literal_shift_amnt <= current_inst(11 downto 7);
1148
                                        opb_is_literal <= '0';
1149
                                end if;
1150
 
1151
                        elsif state = LDMSTM_WRITEBACK
1152
                        then
1153
                                -- one more latency cycle needed if loading from PC and S flag was clear
1154
                                if current_inst(20) = '1' and current_inst(15) = '1' and current_inst(22) = '0'
1155
                                then
1156
                                        next_state <= ONE_LATENCY_CYCLE;
1157
                                else
1158
                                        next_state <= MAIN_STATE;
1159
                                end if;
1160
 
1161
                                -- add/sub address
1162
                                if current_inst(23) = '1'
1163
                                then
1164
                                        alu_operation <= ALU_ADD;
1165
                                else
1166
                                        alu_operation <= ALU_SUB;
1167
                                end if;
1168
 
1169
                                branch_en <= '0';
1170
                                affect_sflags <= '0';
1171
                                opb_sel <= '0';
1172
                                barrelshift_operand <= '0';
1173
                                data_sel <= '1';
1174
                                rdest_wren <= '1';
1175
                                wb_sel <= '0';
1176
                                rdest_adr <= address_remap(current_inst(19 downto 16), mode);
1177
                                mem_ctrl <= NO_MEM_OP;
1178
                                rfA_adr <= address_remap_pc(current_inst(19 downto 16), mode);
1179
 
1180
                                -- LSL #0
1181
                                barrelshift_type <= "00";
1182
                                literal_shift_amnt <= "00010";
1183
 
1184
                                -- Add the # of register writtens to Rn and write it back
1185
                                literal_data <= std_logic_vector(to_signed(count_set_bits(current_inst(15 downto 0)) , 24));
1186
                                opb_is_literal <= '1';
1187
 
1188
                        -- Cycle that moves SPSR -> CPSR when LDM with R15 in the list and S flag set
1189
                        elsif state = LDMSTM_RETURN_FROM_EXCEPTION
1190
                        then
1191
                                affect_sflags <= '1';           -- ALWAYS affect sflags
1192
 
1193
                                -- ** Read SPSR **, use LSL #0 (i.e. no modification of the register)
1194
                                barrelshift_operand <= '0';
1195
                                barrelshift_type <= "00";
1196
                                literal_shift_amnt <= (others => '0');
1197
                                opb_is_literal <= '0';
1198
                                rfB_adr <= '0' & address_remap_spsr(mode);
1199
 
1200
                                -- write flags and execute the real instruction on next cycle
1201
                                alu_operation <= ALU_RWF;
1202
                                branch_en <= '0';
1203
                                rdest_wren <= '0';
1204
 
1205
                                if current_inst(21) = '1'
1206
                                then
1207
                                        next_state <= LDMSTM_WRITEBACK;         -- if base writeback is enabled do it
1208
                                else
1209
                                        next_state <= ONE_LATENCY_CYCLE;        -- else insert simply a bubble in the pipeline
1210
                                end if;
1211
 
1212
                        -- 2nd cycle of an interrupt, very similar to a BL instruction
1213
                        elsif state = RESET_CYCLE2 or state = UNDEF_CYCLE2 or state = SWI_CYCLE2
1214
                        or state = IRQ_CYCLE2 or state = FIQ_CYCLE2
1215
                        then
1216
                                next_state <= MAIN_STATE;
1217
                                alu_operation <= ALU_NOP;
1218
                                opb_sel <= '0';
1219
                                affect_sflags <= '0';
1220
                                branch_en <= '1';
1221
 
1222
                                -- BL, write PC+4 into R14 of current mode
1223
                                data_sel <= '0';
1224
                                rdest_wren <= '1';
1225
                                wb_sel <= '0';
1226
 
1227
                                barrelshift_operand <= '0';
1228
                                barrelshift_type <= "00";
1229
                                literal_shift_amnt <= (others => '0');
1230
                                -- interrupt vectors
1231
                                case state is
1232
                                when RESET_CYCLE2 => literal_data <= x"000000";
1233
                                                                        rdest_adr <= r14;
1234
                                                                        -- prevents to fetch a useless opcode on next cycle
1235
                                                                        next_state <= ONE_LATENCY_CYCLE;
1236
                                                                        condition <= "1110";            -- always
1237
 
1238
                                when UNDEF_CYCLE2 => literal_data <= x"000004";
1239
                                                                        rdest_adr <= und_r14;
1240
 
1241
                                when SWI_CYCLE2 => literal_data <= x"000008";
1242
                                                                        rdest_adr <= und_r14;
1243
 
1244
                                when IRQ_CYCLE2 => literal_data <= x"000018";
1245
                                                                        rdest_adr <= irq_r14;
1246
                                                                        next_state <= ONE_LATENCY_CYCLE;
1247
                                                                        condition <= "1110";
1248
 
1249
                                when others => literal_data <= x"00001c";               -- FIQ cycle 2
1250
                                                                        rdest_adr <= fiq_r14;
1251
                                                                        next_state <= ONE_LATENCY_CYCLE;
1252
                                                                        condition <= "1110";
1253
                                end case;
1254
                                opb_is_literal <= '1';
1255
                                mem_ctrl <= NO_MEM_OP;
1256
                        end if;
1257
                end if;
1258
        end process decode;
1259
 
1260
        -- send the 4 low bits (adress bits) to the register file adresses
1261
        rfile_A_adr <= rfA_adr(4 downto 0);
1262
        rfile_B_adr <= rfB_adr(4 downto 0);
1263
        rfile_C_adr <= rfC_adr(4 downto 0);
1264
 
1265
        --decode_blocked_n <= '0' when state /= MAIN_STATE or (decode_stage_valid = '1' and inst_cache_miss = '0') else '1';
1266
        decode_blocked_n <= '0' when next_state /= MAIN_STATE or (inst_cache_miss = '1') else '1';
1267
 
1268
end architecture;

powered by: WebSVN 2.1.0

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