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

Subversion Repositories ion

[/] [ion/] [trunk/] [vhdl/] [mips_cpu.vhdl] - Blame information for rev 35

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

Line No. Rev Author Line
1 2 ja_rd
--------------------------------------------------------------------------------
2
-- ion_cpu.vhdl -- MIPS-I(tm) compatible CPU core
3
--------------------------------------------------------------------------------
4
-- project:       ION (http://www.opencores.org/project,ion_cpu)
5
-- author:        Jose A. Ruiz (ja_rd@hotmail.com)
6
-- created:       Jan/11/2011
7 30 ja_rd
-- last modified: Jan/31/2011 (ja_rd@hotmail.com)
8 2 ja_rd
--------------------------------------------------------------------------------
9
-- Software placed into the public domain by the author. Use under the terms of
10
-- the GPL.
11
-- Software 'as is' without warranty.  Author liable for nothing.
12
--
13
--------------------------------------------------------------------------------
14
--### MIPS-I things not implemented
15 28 ja_rd
--  # Invalid instruction trapping:
16 30 ja_rd
--      * invalid opcodes do trap but the logic that prevents bad opcodes from
17
--        having side affects has not been tested yet.
18 2 ja_rd
--  # Kernel/user status
19 30 ja_rd
--  # RTE instruction (or ERET)
20 2 ja_rd
--  # Most of the CP0 registers and of course all of the CP1
21
--  # External interrupts
22
--
23
--### Things implemented but not tested
24
--  # Memory pause input
25
--
26
--### Things with provisional implementation
27
-- 
28
-- 1.- Load interlocks: the pipeline is stalled for every load instruction, even
29
--     if the target register is not used in the following instruction. So that
30
--     every load takes two cycles.
31
--     The interlock logic should check register indices.
32
--
33
--------------------------------------------------------------------------------
34
 
35
library ieee;
36
use ieee.std_logic_1164.all;
37
use ieee.std_logic_arith.all;
38
use ieee.std_logic_unsigned.all;
39
use work.mips_pkg.all;
40
 
41
entity mips_cpu is
42
    generic(
43
        XILINX_REGBANK  : string  := "distributed" -- {distributed|block}
44
    );
45
    port(
46
        clk             : in std_logic;
47
        reset           : in std_logic;
48
        interrupt       : in std_logic;
49
 
50
        data_rd_addr    : out std_logic_vector(31 downto 0);
51
        data_rd         : in std_logic_vector(31 downto 0);
52
        data_rd_vma     : out std_logic;
53
 
54
        code_rd_addr    : out std_logic_vector(31 downto 2);
55
        code_rd         : in std_logic_vector(31 downto 0);
56
        code_rd_vma     : out std_logic;
57
 
58
        data_wr_addr    : out std_logic_vector(31 downto 2);
59
        byte_we         : out std_logic_vector(3 downto 0);
60
        data_wr         : out std_logic_vector(31 downto 0);
61 28 ja_rd
 
62 2 ja_rd
        mem_wait        : in std_logic
63
    );
64
end; --entity mips_cpu
65
 
66
architecture rtl of mips_cpu is
67
 
68
--------------------------------------------------------------------------------
69
-- Pipeline stage 0
70
 
71
signal p0_pc_reg :          t_pc;
72 8 ja_rd
signal p0_pc_restart :      t_pc;
73 2 ja_rd
signal p0_pc_incremented :  t_pc;
74
signal p0_pc_jump :         t_pc;
75
signal p0_pc_branch :       t_pc;
76
signal p0_pc_target :       t_pc;
77
signal p0_pc_next :         t_pc;
78
signal p0_rs_num :          t_regnum;
79
signal p0_rt_num :          t_regnum;
80
signal p0_jump_cond_value : std_logic;
81
signal p0_rbank_rs_hazard : std_logic;
82
signal p0_rbank_rt_hazard : std_logic;
83 28 ja_rd
signal p0_uses_rs1 :        std_logic;
84
signal p0_uses_rs2 :        std_logic;
85 2 ja_rd
 
86 28 ja_rd
signal p1_rs1_hazard :      std_logic;
87
signal p1_rs2_hazard :      std_logic;
88
 
89 2 ja_rd
--------------------------------------------------------------------------------
90
-- Pipeline stage 1
91
 
92
 
93
signal p1_rbank :           t_rbank := (others => X"00000000");
94
 
95
-- IMPORTANT: This attribute is used by Xilinx tools to select how to implement
96
-- the register bank. If we don't use it, by default XST would infer 2 BRAMs for
97
-- the 1024-bit 3-port reg bank, which you probably don't want.
98
-- This can take the values {distributed|block}.
99
attribute ram_style :       string;
100 30 ja_rd
attribute ram_style of p1_rbank : signal is XILINX_REGBANK;
101 2 ja_rd
 
102
signal p1_rs, p1_rt :       t_word;
103
signal p1_rs_rbank :        t_word;
104
signal p1_rt_rbank :        t_word;
105
signal p1_rbank_forward :   t_word;
106
signal p1_rd_num :          t_regnum;
107 28 ja_rd
signal p1_c0_rs_num :       t_regnum;
108 2 ja_rd
signal p1_rbank_wr_addr :   t_regnum;
109
signal p1_rbank_we :        std_logic;
110
signal p1_rbank_wr_data :   t_word;
111
signal p1_alu_inp1 :        t_word;
112
signal p1_alu_inp2 :        t_word;
113
signal p1_alu_outp :        t_word;
114
-- ALU control inputs (shortened name for brevity in expressions)
115
signal p1_ac :              t_alu_control;
116
-- ALU flag outputs (comparison results)
117
signal p1_alu_flags :       t_alu_flags;
118
-- immediate data, sign- or zero-extended as required by IR
119
signal p1_data_imm :        t_word;
120
signal p1_branch_offset :   t_pc;
121
signal p1_branch_offset_sex:std_logic_vector(31 downto 18);
122
signal p1_rbank_rs_hazard : std_logic;
123
signal p1_rbank_rt_hazard : std_logic;
124
signal p1_jump_type_set0 :  std_logic_vector(1 downto 0);
125
signal p1_jump_type_set1 :  std_logic_vector(1 downto 0);
126
signal p1_ir_reg :          std_logic_vector(31 downto 0);
127
signal p1_ir_op :           std_logic_vector(31 downto 26);
128
signal p1_ir_fn :           std_logic_vector(5 downto 0);
129
signal p1_op_special :      std_logic;
130
signal p1_exception :       std_logic;
131
signal p1_do_reg_jump :     std_logic;
132
signal p1_do_zero_ext_imm : std_logic;
133
signal p1_set_cp0 :         std_logic;
134
signal p1_get_cp0 :         std_logic;
135
signal p1_alu_op2_sel :     std_logic_vector(1 downto 0);
136
signal p1_alu_op2_sel_set0: std_logic_vector(1 downto 0);
137
signal p1_alu_op2_sel_set1: std_logic_vector(1 downto 0);
138
signal p1_do_load :         std_logic;
139
signal p1_do_store :        std_logic;
140
signal p1_store_size :      std_logic_vector(1 downto 0);
141
signal p1_we_control :      std_logic_vector(5 downto 0);
142
signal p1_load_alu :        std_logic;
143
signal p1_load_alu_set0 :   std_logic;
144
signal p1_load_alu_set1 :   std_logic;
145
signal p1_ld_upper_hword :  std_logic;
146
signal p1_ld_upper_byte :   std_logic;
147
signal p1_ld_unsigned :     std_logic;
148
signal p1_jump_type :       std_logic_vector(1 downto 0);
149
signal p1_link :            std_logic;
150
signal p1_jump_cond_sel :   std_logic_vector(2 downto 0);
151
signal p1_data_addr :       t_addr;
152
signal p1_data_offset :     t_addr;
153
 
154 12 ja_rd
signal p1_muldiv_result :   t_word;
155
signal p1_muldiv_func :     t_mult_function;
156
signal p1_muldiv_running :  std_logic;
157
signal p1_muldiv_started :  std_logic;
158
signal p1_muldiv_stall :    std_logic;
159
 
160 23 ja_rd
signal p1_unknown_opcode :  std_logic;
161 12 ja_rd
 
162 2 ja_rd
--------------------------------------------------------------------------------
163
-- Pipeline stage 2
164
 
165 12 ja_rd
signal p2_muldiv_started :  std_logic;
166 2 ja_rd
signal p2_exception :       std_logic;
167
signal p2_rd_addr :         std_logic_vector(1 downto 0);
168
signal p2_rd_mux_control :  std_logic_vector(3 downto 0);
169
signal p2_load_target :     t_regnum;
170
signal p2_do_load :         std_logic;
171
signal p2_ld_upper_hword :  std_logic;
172
signal p2_ld_upper_byte :   std_logic;
173
signal p2_ld_unsigned :     std_logic;
174
signal p2_wback_mux_sel :   std_logic_vector(1 downto 0);
175
signal p2_data_word_rd :    t_word;
176
signal p2_data_word_ext :   std_logic;
177
 
178
--------------------------------------------------------------------------------
179
-- Global control signals 
180
 
181
signal load_interlock :     std_logic;
182
signal stall_pipeline :     std_logic;
183
-- pipeline is stalled for any reason
184
signal pipeline_stalled :   std_logic;
185
-- pipeline is stalled because of a load instruction interlock
186
signal pipeline_interlocked:std_logic;
187
 
188
--------------------------------------------------------------------------------
189
-- CP0 registers and signals
190
 
191
-- CP0[12]: status register 
192
signal cp0_status :         std_logic_vector(1 downto 0);
193
-- Output of CP0 register bank (only a few regs are implemented)
194
signal cp0_reg_read :       t_word;
195
-- CP0[14]: EPC register (PC value saved at exceptions)
196
signal cp0_epc :            t_pc;
197 28 ja_rd
-- CP0[13]: 'Cause' register (cause and attributes of exception)
198
signal cp0_cause :          t_word;
199
signal cp0_in_delay_slot :  std_logic;
200
signal cp0_cause_bd :       std_logic;
201
signal cp0_cause_ce :       std_logic_vector(1 downto 0);
202
signal cp0_cause_exc_code : std_logic_vector(4 downto 0);
203 2 ja_rd
 
204
begin
205
 
206
--##############################################################################
207
-- Register bank & datapath
208
 
209
-- Register indices are 'decoded' out of the instruction word BEFORE loading IR
210
p0_rs_num <= std_logic_vector(code_rd(25 downto 21));
211
with p1_ir_reg(31 downto 26) select p1_rd_num <=
212
    p1_ir_reg(15 downto 11)    when "000000",
213
    p1_ir_reg(20 downto 16)    when others;
214
 
215
p0_rt_num <= std_logic_vector(code_rd(20 downto 16)); -- also called rs2 in the docs
216
 
217
--------------------------------------------------------------------------------
218
-- Data input shifter & masker (LB,LBU,LH,LHU,LW)
219
 
220
p2_rd_mux_control <= p2_ld_upper_hword & p2_ld_upper_byte & p2_rd_addr;
221
 
222
-- Extension for unused bits will be zero or the sign (bit 7 or bit 15)
223 35 ja_rd
p2_data_word_ext <= '0'          when p2_ld_unsigned='1' else
224 2 ja_rd
                    data_rd(15)  when p2_ld_upper_byte='1' else
225
                    data_rd(7)   when p2_rd_addr="11" else
226
                    data_rd(15)  when p2_rd_addr="10" else
227
                    data_rd(23);
228
 
229
-- byte 0 may come from any of the 4 bytes of the input word
230
with p2_rd_mux_control select p2_data_word_rd(7 downto 0) <=
231
    data_rd(31 downto 24)        when "0000",
232
    data_rd(23 downto 16)        when "0001",
233
    data_rd(23 downto 16)        when "0100",
234
    data_rd(15 downto  8)        when "0010",
235
    data_rd( 7 downto  0)        when others;
236
 
237
-- byte 1 may come from input bytes 1 or 3 or may be extended for LB, LBU
238
with p2_rd_mux_control select p2_data_word_rd(15 downto 8) <=
239
    data_rd(31 downto 24)        when "0100",
240
    data_rd(15 downto  8)        when "0110",
241
    data_rd(15 downto  8)        when "1100",
242
    data_rd(15 downto  8)        when "1101",
243
    data_rd(15 downto  8)        when "1110",
244
    data_rd(15 downto  8)        when "1111",
245
    (others => p2_data_word_ext) when others;
246
 
247
-- bytes 2,3 come straight from input or are extended for LH,LHU
248
with p2_ld_upper_hword select p2_data_word_rd(31 downto 16) <=
249
    (others => p2_data_word_ext)    when '0',
250
    data_rd(31 downto 16)            when others;
251
 
252
-- Select which data is to be written back to the reg bank and where
253
p1_rbank_wr_addr <= p1_rd_num   when p2_do_load='0' and p1_link='0' else
254
                    "11111"     when p2_do_load='0' and p1_link='1' else
255
                    p2_load_target;
256
 
257
p2_wback_mux_sel <=
258
    "00" when p2_do_load='0' and p1_get_cp0='0' and p1_link='0' else
259
    "01" when p2_do_load='1' and p1_get_cp0='0' and p1_link='0' else
260
    "10" when p2_do_load='0' and p1_get_cp0='1' and p1_link='0' else
261
    "11";
262
 
263
with (p2_wback_mux_sel) select p1_rbank_wr_data <=
264
    p1_alu_outp                when "00",
265
    p2_data_word_rd            when "01",
266
    p0_pc_incremented & "00"   when "11",
267
    cp0_reg_read               when others;
268
 
269
p1_rbank_we <= '1' when (p2_do_load='1' or p1_load_alu='1' or
270
                        p1_link='1' or p1_get_cp0='1') and
271 35 ja_rd
                        -- If target register is $zero, ignore write
272 2 ja_rd
                        p1_rbank_wr_addr/="00000" and
273 35 ja_rd
                        -- if the cache controller keeps the cpu stopped, do
274
                        -- not writeback
275
                        mem_wait='0' and
276 2 ja_rd
                        -- on exception, abort next instruction (by preventing 
277
                        -- regbank writeback).
278
                        p2_exception='0'
279
                else '0';
280
 
281
-- Register bank as triple-port RAM. Should synth to 2 BRAMs unless you use
282
-- synth attributes to prevent it (see 'ram_style' attribute above) or your
283
-- FPGA has 3-port BRAMS, or has none.
284
synchronous_reg_bank:
285
process(clk)
286
begin
287
    if clk'event and clk='1' then
288
        if p1_rbank_we='1' and
289
           (pipeline_stalled='0' or pipeline_interlocked='1') then -- @note1
290
            p1_rbank(conv_integer(p1_rbank_wr_addr)) <= p1_rbank_wr_data;
291
        end if;
292
        p1_rt_rbank <= p1_rbank(conv_integer(p0_rt_num));
293
        p1_rs_rbank <= p1_rbank(conv_integer(p0_rs_num));
294
    end if;
295
end process synchronous_reg_bank;
296
 
297
-- Register writeback data in case it needs to be forwarded.
298
data_forward_register:
299
process(clk)
300
begin
301
    if clk'event and clk='1' then
302
        if p1_rbank_we='1' then -- no need to check for stall cycles
303
            p1_rbank_forward <= p1_rbank_wr_data;
304
        end if;
305
    end if;
306
end process data_forward_register;
307
 
308
-- Bypass sync RAM if we're reading and writing to the same address. This saves
309
-- 1 stall cycle and fixes the data hazard.
310
p0_rbank_rs_hazard <= '1' when p1_rbank_wr_addr=p0_rs_num and p1_rbank_we='1'
311
                      else '0';
312
p0_rbank_rt_hazard <= '1' when p1_rbank_wr_addr=p0_rt_num and p1_rbank_we='1'
313
                      else '0';
314
 
315
p1_rs <= p1_rs_rbank when p1_rbank_rs_hazard='0' else p1_rbank_forward;
316
p1_rt <= p1_rt_rbank when p1_rbank_rt_hazard='0' else p1_rbank_forward;
317
 
318
-- Zero extension/Sign extension for instruction immediate data
319
p1_data_imm(15 downto 0)  <= p1_ir_reg(15 downto 0);
320
 
321
with p1_do_zero_ext_imm select p1_data_imm(31 downto 16) <=
322
    (others => '0')             when '1',
323
    (others => p1_ir_reg(15))   when others;
324
 
325
 
326
--------------------------------------------------------------------------------
327
-- ALU & ALU input multiplexors
328
 
329
p1_alu_inp1 <= p1_rs;
330
 
331
with p1_alu_op2_sel select p1_alu_inp2 <=
332 12 ja_rd
    p1_data_imm         when "11",
333 23 ja_rd
    p1_muldiv_result    when "01",
334
    --p1_muldiv_result    when "10", -- FIXME mux input wasted!
335 12 ja_rd
    p1_rt               when others;
336 2 ja_rd
 
337
alu_inst : entity work.mips_alu
338
    port map (
339
        clk             => clk,
340
        reset           => reset,
341
        ac              => p1_ac,
342
        flags           => p1_alu_flags,
343
 
344
        inp1            => p1_alu_inp1,
345
        inp2            => p1_alu_inp2,
346
        outp            => p1_alu_outp
347
    );
348
 
349
 
350
--------------------------------------------------------------------------------
351
-- Mul/Div block interface
352
 
353 12 ja_rd
-- Compute the mdiv block function word. If p1_muldiv_func has any value other
354
-- than MULT_NOTHING a new mdiv operation will start, truncating whatever other
355
-- operation that may have been in course.
356
-- So we encode here the function to be performed and make sure the value stays
357
-- there for only one cycle (the first ALU cycle of the mul/div instruction).
358 2 ja_rd
 
359 12 ja_rd
-- This will be '1' for all mul/div operations other than NOP...
360
p1_muldiv_func(3) <= '1' when p1_op_special='1' and
361
                              p1_ir_fn(5 downto 4)="01" and
362
                              -- ...but only if the mdiv is not already running
363
                              p2_muldiv_started = '0' and
364
                              p1_muldiv_running ='0'
365
                      else '0';
366 2 ja_rd
 
367 12 ja_rd
-- When bit(3) is zero, the rest are zeroed too. Otherwise, they come from IR
368
p1_muldiv_func(2 downto 0) <=
369
    p1_ir_fn(3) & p1_ir_fn(1 downto 0) when p1_muldiv_func(3)='1'
370
    else "000";
371 2 ja_rd
 
372 12 ja_rd
mult_div: entity work.mips_mult
373
    port map (
374
        a           => p1_rs,
375
        b           => p1_rt,
376
        c_mult      => p1_muldiv_result,
377
        pause_out   => p1_muldiv_running,
378
        mult_func   => p1_muldiv_func,
379
        clk         => clk,
380
        reset_in    => reset
381
    );
382
 
383
-- Active only for the 1st ALU cycle of any mul/div instruction
384
p1_muldiv_started <= '1' when p1_op_special='1' and
385
                              p1_ir_fn(5 downto 3)="011" and
386
                              -- 
387
                              p1_muldiv_running='0'
388
                      else '0';
389
 
390
-- Stall the pipeline to enable mdiv operation completion.
391
-- We need p2_muldiv_started to distinguish the cycle before p1_muldiv_running
392
-- is asserted and the cycle after it deasserts.
393
-- Otherwise we would reexecute the same muldiv endlessly instruction after 
394
-- deassertion of p1_muldiv_running, since the IR was stalled and still contains 
395
-- the mul opcode...
396
p1_muldiv_stall <= '1' when
397
        -- Active for the cycle immediately before p1_muldiv_running asserts
398
        -- and NOT for the cycle after it deasserts
399
        (p1_muldiv_started='1' and p2_muldiv_started='0') or
400
        -- Active until operation is complete
401
        p1_muldiv_running = '1'
402
        else '0';
403
 
404
 
405 2 ja_rd
--##############################################################################
406
-- PC register and branch logic
407
 
408
-- p0_pc_reg will not be incremented on stall cycles
409
p0_pc_incremented <= p0_pc_reg + (not stall_pipeline);
410
 
411
-- main pc mux: jump or continue
412
p0_pc_next <=
413
    p0_pc_target when
414
        -- We jump on jump instructions whose condition is met...
415 6 ja_rd
        ((p1_jump_type(1)='1' and p0_jump_cond_value='1' and
416
        -- ...except we abort any jump that follows the victim of an exception
417
          p2_exception='0') or
418
        -- We jump on exceptions too...
419 2 ja_rd
        p1_exception='1')
420 6 ja_rd
        -- ... but we only jump at all if the pipeline is not stalled
421 2 ja_rd
        and stall_pipeline='0'
422
    else p0_pc_incremented;
423
 
424
pc_register:
425
process(clk)
426
begin
427
    if clk'event and clk='1' then
428
        if reset='1' then
429
            -- reset to 0xffffffff so that 1st fetch addr is 0x00000000
430
            p0_pc_reg <= (others => '1');
431
        else
432
            -- p0_pc_reg holds the same value as external sync ram addr register
433
            p0_pc_reg <= p0_pc_next;
434 8 ja_rd
            -- p0_pc_restart = addr saved to EPC on interrupts (@note2)
435 28 ja_rd
            -- It's the addr of the instruction triggering the exception,
436
            -- except when the triggering instruction is in a delay slot. In 
437
            -- that case, this is the previous jump instruction address.
438
            -- I.e. all as per the mips-1 specs.
439 8 ja_rd
            if (p1_jump_type="00" or p0_jump_cond_value='0') then
440
                p0_pc_restart <= p0_pc_reg;
441 28 ja_rd
                -- remember if we are in a delay slot, in case there's a trap
442
                cp0_in_delay_slot <= '0'; -- NOT in a delay slot
443
            else
444
                cp0_in_delay_slot <= '1'; -- in a delay slot
445 8 ja_rd
            end if;
446 2 ja_rd
        end if;
447
    end if;
448
end process pc_register;
449
 
450 35 ja_rd
-- FIXME we should not output the lowest 2 bits
451 2 ja_rd
data_rd_addr <= p1_data_addr(31 downto 0);
452
 
453
-- FIXME these two need to pushed behind a register, they are glitch-prone
454
data_rd_vma <= p1_do_load and not pipeline_stalled; -- FIXME register
455 28 ja_rd
code_rd_vma <= not stall_pipeline; -- FIXME register
456 2 ja_rd
 
457
code_rd_addr <= p0_pc_next;
458
 
459
data_wr_addr <= p1_data_addr(31 downto 2);
460
 
461
-- compute target of J/JR instructions
462
p0_pc_jump <=   p1_rs(31 downto 2) when p1_do_reg_jump='1' else
463
                p0_pc_reg(31 downto 28) & p1_ir_reg(25 downto 0);
464
 
465
-- compute target of relative branch instructions
466
p1_branch_offset_sex <= (others => p1_ir_reg(15));
467
p1_branch_offset <= p1_branch_offset_sex & p1_ir_reg(15 downto 0);
468
-- p0_pc_reg is the addr of the instruction in delay slot
469
p0_pc_branch <= p0_pc_reg + p1_branch_offset;
470
 
471
-- decide which jump target is to be used
472
p0_pc_target <= X"0000003"&"11"     when p1_exception='1' else
473
             p0_pc_jump             when p1_jump_type(0)='1' else
474
             p0_pc_branch;
475
 
476
 
477
--##############################################################################
478
-- Instruction decoding and IR
479
 
480
instruction_register:
481
process(clk)
482
begin
483
    if clk'event and clk='1' then
484
        if reset='1' then
485
            p1_ir_reg <= (others => '0');
486
        elsif stall_pipeline='0' then
487
            p1_ir_reg <= code_rd;
488
        end if;
489
    end if;
490
end process instruction_register;
491
 
492
-- 'Extract' main fields from IR, for convenience
493
p1_ir_op <= p1_ir_reg(31 downto 26);
494
p1_ir_fn <= p1_ir_reg(5 downto 0);
495
 
496
-- Decode jump type, if any, for instructions with op/=0
497
with p1_ir_op select p1_jump_type_set0 <=
498
    -- FIXME weed out invalid instructions
499
    "10" when "000001", -- BLTZ, BGEZ, BLTZAL, BGTZAL
500
    "11" when "000010", -- J
501
    "11" when "000011", -- JAL
502
    "10" when "000100", -- BEQ
503
    "10" when "000101", -- BNE
504
    "10" when "000110", -- BLEZ
505
    "10" when "000111", -- BGTZ
506
    "00" when others;   -- no jump
507
 
508
-- Decode jump type, if any, for instructions with op=0
509
p1_jump_type_set1 <= "11" when p1_op_special='1' and
510
                               p1_ir_reg(5 downto 1)="00100"
511
                     else "00";
512
 
513
-- Decode jump type for the instruction in IR (composite of two formats)
514
p1_jump_type <= p1_jump_type_set0 or p1_jump_type_set1;
515
 
516
p1_link <= '1' when (p1_ir_op="000000" and p1_ir_reg(5 downto 0)="001001") or
517
                    (p1_ir_op="000001" and p1_ir_reg(20)='1') or
518
                    (p1_ir_op="000011")
519
           else '0';
520
 
521
-- Decode jump condition: encode a mux control signal from IR...
522
p1_jump_cond_sel <=
523
    "001" when p1_ir_op="000001" and p1_ir_reg(16)='0' else --   op1 < 0   BLTZ*
524
    "101" when p1_ir_op="000001" and p1_ir_reg(16)='1' else -- !(op1 < 0) BNLTZ*
525
    "010" when p1_ir_op="000100" else                       --   op1 == op2  BEQ
526
    "110" when p1_ir_op="000101" else                       -- !(op1 == op2) BNE
527
    "011" when p1_ir_op="000110" else                       --   op1 <= 0   BLEZ
528
    "111" when p1_ir_op="000111" else                       -- !(op1 <= 0)  BGTZ
529
    "000";                                                  -- always
530
 
531
-- ... and use mux control signal to select the condition value
532
with p1_jump_cond_sel select p0_jump_cond_value <=
533
        p1_alu_flags.inp1_lt_zero       when "001",
534
    not p1_alu_flags.inp1_lt_zero       when "101",
535
        p1_alu_flags.inp1_eq_inp2       when "010",
536
    not p1_alu_flags.inp1_eq_inp2       when "110",
537
        (p1_alu_flags.inp1_lt_inp2 or
538
         p1_alu_flags.inp1_eq_inp2)     when "011",
539
    not (p1_alu_flags.inp1_lt_inp2 or
540
         p1_alu_flags.inp1_eq_inp2)     when "111",
541
    '1'                                 when others;
542
 
543
-- Decode instructions that launch exceptions
544 23 ja_rd
p1_exception <= '1' when
545
    (p1_op_special='1' and p1_ir_reg(5 downto 1)="00110") or
546
    p1_unknown_opcode='1'
547
    else '0';
548 2 ja_rd
 
549
-- Decode MTC0/MFC0 instructions
550
p1_set_cp0 <= '1' when p1_ir_reg(31 downto 21)="01000000100" else '0';
551
p1_get_cp0 <= '1' when p1_ir_reg(31 downto 21)="01000000000" else '0';
552
 
553
-- FIXME elaborate and explain this
554
 
555
p1_op_special <= '1' when p1_ir_op="000000" else '0';
556
 
557
p1_do_reg_jump <= '1' when p1_op_special='1' and p1_ir_fn(5 downto 1)="00100" else '0';
558
p1_do_zero_ext_imm <= '1' when (p1_ir_op(31 downto 28)="0011") else '0';
559
 
560
-- Decode input data mux control (LW, LH, LB, LBU, LHU) and load enable
561 30 ja_rd
p1_do_load <= '1' when
562
    p1_ir_op(31 downto 29)="100" and
563
    p1_ir_op(28 downto 26)/="010" and -- LWL
564
    p1_ir_op(28 downto 26)/="110" and -- LWR
565
    p1_ir_op(28 downto 26)/="111" and -- LWR
566
    p2_exception='0'  -- abort load if previous instruction triggered trap
567
    else '0';
568 2 ja_rd
 
569
p1_load_alu_set0 <= '1'
570
    when p1_op_special='1' and
571
        ((p1_ir_op(31 downto 29)="000" and p1_ir_op(27 downto 26)="00") or
572
         (p1_ir_op(31 downto 29)="000" and p1_ir_op(27 downto 26)="10") or
573
         (p1_ir_op(31 downto 29)="000" and p1_ir_op(27 downto 26)="11") or
574
         (p1_ir_op(31 downto 29)="000" and p1_ir_op(27 downto 26)="00") or
575
         (p1_ir_op(31 downto 28)="0100" and p1_ir_op(27 downto 26)="00") or
576
         (p1_ir_op(31 downto 28)="0100" and p1_ir_op(27 downto 26)="10") or
577
         (p1_ir_op(31 downto 28)="1000") or
578
         (p1_ir_op(31 downto 28)="1001") or
579
         (p1_ir_op(31 downto 28)="1010" and p1_ir_op(27 downto 26)="10") or
580
         (p1_ir_op(31 downto 28)="1010" and p1_ir_op(27 downto 26)="11") or
581
         (p1_ir_op(31 downto 28)="0010" and p1_ir_op(27 downto 26)="01"))
582
    else '0';
583
 
584
with p1_ir_op select p1_load_alu_set1 <=
585 28 ja_rd
    '1' when "001000",  -- addi
586
    '1' when "001001",  -- addiu
587
    '1' when "001010",  -- slti
588
    '1' when "001011",  -- sltiu
589
    '1' when "001100",  -- andi
590
    '1' when "001101",  -- ori
591
    '1' when "001110",  -- xori
592
    '1' when "001111",  -- lui
593 2 ja_rd
    '0' when others;
594 28 ja_rd
p1_load_alu <= (p1_load_alu_set0 or p1_load_alu_set1) and
595
                not p1_unknown_opcode;
596 2 ja_rd
 
597
p1_ld_upper_hword <= p1_ir_op(27); -- use input upper hword vs. sign extend/zero
598
p1_ld_upper_byte <= p1_ir_op(26);  -- use input upper byte vs. sign extend/zero
599
p1_ld_unsigned <= p1_ir_op(28);    -- sign extend vs. zero extend
600
 
601
-- ALU input-2 selection: use external data for 2x opcodes (loads)
602
p1_alu_op2_sel_set0 <=
603
    "11" when    p1_ir_op(31 downto 30)="10" or p1_ir_op(29)='1' else
604
    "00";
605
 
606
-- ALU input-2 selection: use registers Hi and Lo for MFHI, MFLO
607 22 ja_rd
p1_alu_op2_sel_set1 <=
608
    "01" when p1_op_special='1' and (p1_ir_fn="010000" or p1_ir_fn="010010")
609
    else "00";
610 2 ja_rd
 
611
-- ALU input-2 final selection
612
p1_alu_op2_sel <= p1_alu_op2_sel_set0 or p1_alu_op2_sel_set1;
613
 
614
-- Decode store operations
615 30 ja_rd
p1_do_store <= '1' when
616
    p1_ir_op(31 downto 29)="101" and
617
    (p1_ir_op(28 downto 26)="000" or -- SB
618
     p1_ir_op(28 downto 26)="001" or -- SH
619
     p1_ir_op(28 downto 26)="011") and -- SWH
620 28 ja_rd
    p2_exception='0'    -- abort when previous instruction triggered exception
621
    else '0';
622 2 ja_rd
p1_store_size <= p1_ir_op(27 downto 26);
623
 
624
 
625 28 ja_rd
-- Extract source and destination C0 register indices
626
p1_c0_rs_num <= p1_ir_reg(15 downto 11);
627 2 ja_rd
 
628
-- Decode ALU control dignals
629
 
630
p1_ac.use_slt <= '1' when (p1_ir_op="000001" and p1_ir_reg(20 downto 17)="01000") or
631
                        (p1_ir_op="000000" and p1_ir_reg(5 downto 1)="10101") or
632
                        p1_ir_op="001010" or p1_ir_op="001011"
633
               else '0';
634
p1_ac.arith_unsigned <= p1_ac.use_slt and p1_ir_reg(0);
635
 
636
p1_ac.use_logic(0) <= '1' when (p1_op_special='1' and p1_ir_fn(5 downto 3)/="000") or
637
                    -- all immediate arith and logic
638
                    p1_ir_op(31 downto 29)="001"
639
                 else '0';
640
p1_ac.use_logic(1) <= '1' when (p1_op_special='1' and p1_ir_fn="100111") else '0';
641
 
642
p1_ac.use_arith <= '1' when p1_ir_op(31 downto 28)="0010" or
643
                            (p1_op_special='1' and
644
                                (p1_ir_fn(5 downto 2)="1000" or
645
                                p1_ir_fn(5 downto 2)="1010"))
646
                 else '0';
647
 
648
-- selection of 2nd internal alu operand: {i2, /i2, i2<<16, 0x0}
649
p1_ac.neg_sel(1)<= '1' when p1_ir_op(29 downto 26) = "1111" else '0';
650
p1_ac.neg_sel(0)<= '1' when   p1_ir_op="001010" or
651
                            p1_ir_op="001011" or
652
                            p1_ir_op(31 downto 28)="0001" or
653
                            (p1_op_special='1' and
654
                                (p1_ir_fn="100010" or
655
                                 p1_ir_fn="100011" or
656
                                 p1_ir_fn(5 downto 2)="1010"))
657
                 else '0';
658
p1_ac.cy_in <= p1_ac.neg_sel(0);
659
 
660
p1_ac.shift_sel <= p1_ir_fn(1 downto 0);
661
 
662
p1_ac.logic_sel <= "00" when (p1_op_special='1' and p1_ir_fn="100100") else
663
                 "01" when (p1_op_special='1' and p1_ir_fn="100101") else
664
                 "10" when (p1_op_special='1' and p1_ir_fn="100110") else
665
                 "01" when (p1_op_special='1' and p1_ir_fn="100111") else
666
                 "00" when (p1_ir_op="001100") else
667
                 "01" when (p1_ir_op="001101") else
668
                 "10" when (p1_ir_op="001110") else
669
                 "11";
670
 
671
p1_ac.shift_amount <= p1_ir_reg(10 downto 6) when p1_ir_fn(2)='0' else p1_rs(4 downto 0);
672
 
673 23 ja_rd
 
674 2 ja_rd
--------------------------------------------------------------------------------
675 23 ja_rd
-- Decoding of unimplemented and privileged instructions
676 2 ja_rd
 
677 23 ja_rd
-- Unimplemented instructions include:
678
--  1.- All instructions above architecture MIPS-I except:
679
--      1.1.- eret
680
--  2.- Unaligned stores and loads (LWL,LWR,SWL,SWR)
681
--  3.- All CP0 instructions other than mfc0 and mtc0
682
--  4.- All CPi instructions
683
--  5.- All cache instructions
684
-- For the time being, we'll decode them all together.
685
 
686
-- FIXME: some of these should trap but others should just NOP (e.g. EHB)
687
 
688
p1_unknown_opcode <= '1' when
689
    -- decode by 'opcode' field
690
    p1_ir_op(31 downto 29)="011" or
691
    p1_ir_op(31 downto 29)="110" or
692
    p1_ir_op(31 downto 29)="111" or
693
    (p1_ir_op(31 downto 29)="010" and p1_ir_op(28 downto 26)/="000") or
694
    p1_ir_op="101111" or    -- CACHE
695
    p1_ir_op="100010" or    -- LWL
696
    p1_ir_op="100110" or    -- LWR
697
    p1_ir_op="101010" or    -- SWL
698
    p1_ir_op="101110" or    -- SWR
699
    p1_ir_op="100111" or
700
    p1_ir_op="101100" or
701
    p1_ir_op="101101" or
702
    -- decode instructions in the 'special' opcode group
703
    (p1_ir_op="000000" and
704
                (p1_ir_fn(5 downto 4)="11" or
705
                 p1_ir_fn="000001" or
706
                 p1_ir_fn="000101" or
707
                 p1_ir_fn="001010" or
708
                 p1_ir_fn="001011" or
709
                 p1_ir_fn="001110" or
710
                 p1_ir_fn(5 downto 2)="0101" or
711
                 p1_ir_fn(5 downto 2)="0111" or
712
                 p1_ir_fn(5 downto 2)="1011")) or
713
    -- decode instructions in the 'regimm' opcode group
714
    (p1_ir_op="000001" and
715
                (p1_ir_reg(20 downto 16)/="00000" and -- BLTZ is valid
716
                 p1_ir_reg(20 downto 16)/="00001" and -- BGEZ is valid
717
                 p1_ir_reg(20 downto 16)/="10000" and -- BLTZAL is valid 
718
                 p1_ir_reg(20 downto 16)/="10001")) -- BGEZAL is valid
719
 
720
    else '0';
721
 
722
--------------------------------------------------------------------------------
723
 
724 2 ja_rd
-- Stage 1 pipeline register. Involved in ALU control.
725
pipeline_stage1_register:
726
process(clk)
727
begin
728
    if clk'event and clk='1' then
729
        if reset='1' then
730
            p1_rbank_rs_hazard <= '0';
731
            p1_rbank_rt_hazard <= '0';
732
        elsif stall_pipeline='0' then
733
            p1_rbank_rs_hazard <= p0_rbank_rs_hazard;
734
            p1_rbank_rt_hazard <= p0_rbank_rt_hazard;
735
        end if;
736
    end if;
737
end process pipeline_stage1_register;
738
 
739 12 ja_rd
pipeline_stage1_register2:
740
process(clk)
741
begin
742
    if clk'event and clk='1' then
743
        if reset='1' then
744
            p2_muldiv_started <= '0';
745
        else
746
            p2_muldiv_started <= p1_muldiv_running;
747
        end if;
748
    end if;
749
end process pipeline_stage1_register2;
750 6 ja_rd
 
751 12 ja_rd
 
752 6 ja_rd
-- Stage 2 pipeline register. Split in two for convenience.
753 2 ja_rd
-- This register deals with two kinds of stalls:
754
-- * When the pipeline stalls because of a load interlock, this register is 
755
--   allowed to update so that the load operation can complete while the rest of
756
--   the pipeline is frozen.
757
-- * When the stall is caused by any other reason, this register freezes with 
758
--   the rest of the machine.
759 6 ja_rd
 
760
-- Part of stage 2 register that controls load operation
761
pipeline_stage2_register_load_control:
762 2 ja_rd
process(clk)
763
begin
764
    if clk'event and clk='1' then
765 6 ja_rd
        -- Clear load control, effectively preventing load, at reset or if
766
        -- the previous instruction raised an exception.
767
        if reset='1' or p2_exception='1' then
768 2 ja_rd
            p2_do_load <= '0';
769
            p2_ld_upper_hword <= '0';
770
            p2_ld_upper_byte <= '0';
771
            p2_ld_unsigned <= '0';
772
            p2_load_target <= "00000";
773 6 ja_rd
 
774
        -- Load signals from previous stage only if there is no pipeline stall
775
        -- unless the stall is caused by interlock (@note1).
776
        elsif (stall_pipeline='0' or load_interlock='1') then
777
            -- Disable reg bank writeback if pipeline is stalled; this prevents
778
            -- duplicate writes in case the stall is a mem_wait.
779 2 ja_rd
            if pipeline_stalled='0' then
780
                p2_do_load <= p1_do_load;
781
            else
782
                p2_do_load <= '0';
783
            end if;
784
            p2_load_target <= p1_rd_num;
785
            p2_ld_upper_hword <= p1_ld_upper_hword;
786
            p2_ld_upper_byte <= p1_ld_upper_byte;
787
            p2_ld_unsigned <= p1_ld_unsigned;
788 6 ja_rd
        end if;
789
    end if;
790
end process pipeline_stage2_register_load_control;
791
 
792
-- All the rest of the stage 2 register
793
pipeline_stage2_register_others:
794
process(clk)
795
begin
796
    if clk'event and clk='1' then
797
        if reset='1' then
798
            p2_exception <= '0';
799
 
800
        -- Load signals from previous stage only if there is no pipeline stall
801
        -- unless the stall is caused by interlock (@note1).
802
        elsif (stall_pipeline='0' or load_interlock='1') then
803 2 ja_rd
            p2_rd_addr <= p1_data_addr(1 downto 0);
804 6 ja_rd
            p2_exception <= p1_exception;
805 2 ja_rd
        end if;
806
    end if;
807 6 ja_rd
end process pipeline_stage2_register_others;
808 2 ja_rd
 
809
--------------------------------------------------------------------------------
810
 
811
-- FIXME stall when needed: mem pause, mdiv pause and load interlock
812
 
813
pipeline_stall_registers:
814
process(clk)
815
begin
816
    if clk'event and clk='1' then
817
        if reset='1' then
818
            pipeline_stalled <= '0';
819
            pipeline_interlocked <= '0';
820
        else
821
            if stall_pipeline='1' then
822
                pipeline_stalled <= '1';
823
            else
824
                pipeline_stalled <= '0';
825
            end if;
826 35 ja_rd
 
827
            -- stalls caused by mem_wait and load_interlock are independent and
828
            -- must not overlap; so when mem_wait='1' the cache stall takes
829
            -- precedence and the loa interlock must wait.
830
            if mem_wait='0' then
831
                if load_interlock='1' then
832
                    pipeline_interlocked <= '1';
833
                else
834
                    pipeline_interlocked <= '0';
835
                end if;
836 2 ja_rd
            end if;
837
        end if;
838
    end if;
839
end process pipeline_stall_registers;
840
 
841
-- FIXME make sure this combinational will not have bad glitches
842 12 ja_rd
stall_pipeline <= mem_wait or load_interlock or p1_muldiv_stall;
843 2 ja_rd
 
844
 
845
-- FIXME load interlock should happen only if the instruction following 
846
-- the load actually uses the load target register. Something like this:
847
-- (p1_do_load='1' and (p1_rd_num=p0_rs_num or p1_rd_num=p0_rt_num))
848 28 ja_rd
load_interlock <= '1' when
849
    p1_do_load='1' and      -- this is a load instruction
850
    pipeline_stalled='0' and -- not already stalled (i.e. assert for 1 cycle)
851
    (p1_rs1_hazard='1' or p1_rs2_hazard='1')
852
 
853
    else '0';
854 2 ja_rd
 
855 28 ja_rd
p1_rs1_hazard <= '1'; --'1' when p0_uses_rs1='1' and p1_rd_num=p0_rs_num else '0';
856
p1_rs2_hazard <= '1'; --'1' when p0_uses_rs2='1' and p1_rd_num=p0_rt_num else '0';
857
 
858
with p1_ir_op select p0_uses_rs1 <=
859
    '0' when "000010",
860
    '0' when "000011",
861
    '0' when "001111",
862
    '0' when "001000",
863
    '1' when others;
864
 
865
with p1_ir_op select p0_uses_rs2 <=
866
    '1' when "000000",
867
    '1' when "000100",
868
    '1' when "000101",
869
    '1' when "000110",
870
    '1' when "000111",
871
    '1' when "010000",
872
    '1' when "101000",
873
    '1' when "101001",
874
    '1' when "101010",
875
    '1' when "101011",
876
    '1' when "101110",
877
    '0' when others;
878
 
879
 
880 2 ja_rd
--------------------------------------------------------------------------------
881
 
882
p1_data_offset(31 downto 16) <= (others => p1_data_imm(15));
883
p1_data_offset(15 downto 0) <= p1_data_imm(15 downto 0);
884
 
885
p1_data_addr <= p1_rs + p1_data_offset;
886
 
887
--------------------------------------------------------------------------------
888
 
889
-- byte_we is a function of the write size and alignment
890
-- size = {00=1,01=2,11=4}; we 3 is MSB, 0 is LSB; big endian => 00 is msb
891
p1_we_control <= pipeline_stalled & p1_do_store & p1_store_size & p1_data_addr(1 downto 0);
892
 
893
with p1_we_control select byte_we <=
894
    "1000"  when "010000",    -- SB %0
895
    "0100"  when "010001",    -- SB %1
896
    "0010"  when "010010",    -- SB %2
897
    "0001"  when "010011",    -- SB %3
898
    "1100"  when "010100",    -- SH %0
899
    "0011"  when "010110",    -- SH %2
900
    "1111"  when "011100",    -- SW %4
901
    "0000"  when others; -- all other combinations are spurious so don't write
902
 
903
-- Data to be stored always comes straight from the reg bank, but it needs to 
904
-- be shifted so that the LSB is aligned to the write address:
905
 
906
data_wr(7 downto 0) <= p1_rt(7 downto 0);
907
 
908
with p1_we_control select data_wr(15 downto 8) <=
909
    p1_rt( 7 downto  0) when "010010",  -- SB %2
910
    p1_rt(15 downto  8) when others;
911
 
912
with p1_we_control select data_wr(23 downto 16) <=
913
    p1_rt( 7 downto  0) when "010001",  -- SB %1
914
    p1_rt( 7 downto  0) when "010100",  -- SH %0
915
    p1_rt(23 downto 16) when others;
916
 
917
with p1_we_control select data_wr(31 downto 24) <=
918
    p1_rt( 7 downto  0) when "010000",  -- SB %0
919
    p1_rt(15 downto  8) when "010100",  -- SH %0
920
    p1_rt(31 downto 24) when others;
921
 
922
 
923
--##############################################################################
924
-- CP0 (what little is implemented of it)
925
 
926
process(clk)
927
begin
928
    if clk'event and clk='1' then
929
        if reset='1' then
930
            -- "10" => mode=kernel; ints=disabled
931
            cp0_status <= "10";
932 28 ja_rd
            cp0_cause_exc_code <= "00000";
933
            cp0_cause_bd <= '0';
934 2 ja_rd
        else
935
            -- no need to check for stall cycles when loading these
936
            if p1_set_cp0='1' then
937
                -- FIXME check for CP0 reg index
938
                cp0_status <= p1_rs(cp0_status'high downto 0);
939
            end if;
940
            if p1_exception='1' then
941 8 ja_rd
                cp0_epc <= p0_pc_restart;
942 28 ja_rd
 
943
                if p1_unknown_opcode='1' then
944
                    cp0_cause_exc_code <= "01010"; -- bad opcode
945
                else
946
                    if p1_ir_fn(0)='0' then
947
                        cp0_cause_exc_code <= "01000"; -- syscall
948
                    else
949
                        cp0_cause_exc_code <= "01001"; -- break
950
                    end if;
951
                end if;
952
 
953
                cp0_cause_bd <= cp0_in_delay_slot;
954 2 ja_rd
            end if;
955
        end if;
956
    end if;
957
end process;
958
 
959 28 ja_rd
cp0_cause_ce <= "00"; -- FIXME CP* traps merged with unimplemented opcode traps
960
cp0_cause <= cp0_cause_bd & '0' & cp0_cause_ce &
961
             X"00000" & "000" &
962
             cp0_cause_exc_code;
963
 
964 2 ja_rd
-- FIXME the mux should mask to zero for any unused reg index
965 28 ja_rd
with p1_c0_rs_num select cp0_reg_read <=
966
    X"0000000" & "00" & cp0_status  when "01100",
967
    cp0_cause                       when "01101",
968
    cp0_epc & "00"                  when others;
969 2 ja_rd
 
970 28 ja_rd
 
971 2 ja_rd
end architecture rtl;
972
 
973
--------------------------------------------------------------------------------
974
-- Implementation notes
975
--------------------------------------------------------------------------------
976
-- @note1 : 
977
--
978
-- This is the meaning of these two signals:
979
-- pipeline_stalled & pipeline_interlocked =>
980
--  "00" => normal state
981
--  "01" => normal state (makes for easier decoding)
982
--  "10" => all stages of pipeline stalled, including rbank
983
--  "11" => all stages of pipeline stalled, except reg bank write port
984
-- 
985
-- Just to clarify, 'stage X stalled' here means that the registers named 
986
-- pX_* don't load.
987
--
988
-- The register bank WE is enabled when the pipeline is not stalled and when 
989
-- it is stalled because of a load interlock; so that in case of interlock the
990
-- load operation can complete while the rest of the pipeline is frozen.
991
--------------------------------------------------------------------------------

powered by: WebSVN 2.1.0

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