OpenCores
URL https://opencores.org/ocsvn/hf-risc/hf-risc/trunk

Subversion Repositories hf-risc

[/] [hf-risc/] [trunk/] [hf-risc/] [ucore/] [datapath.vhd] - Blame information for rev 17

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

Line No. Rev Author Line
1 13 serginhofr
library ieee;
2
use ieee.std_logic_1164.all;
3
use ieee.std_logic_unsigned.all;
4
use ieee.std_logic_arith.all;
5
 
6
entity datapath is
7
        port (  clock:          in std_logic;
8
                reset:          in std_logic;
9
 
10
                stall:          in std_logic;
11
                busy:           in std_logic;
12
 
13
                irq_vector:     in std_logic_vector(31 downto 0);
14
                irq:            in std_logic;
15
                irq_ack:        out std_logic;
16
 
17
                inst_addr:      out std_logic_vector(31 downto 0);
18
                inst_in:        in std_logic_vector(31 downto 0);
19
 
20
                data_addr:      out std_logic_vector(31 downto 0);
21
                data_in:        in std_logic_vector(31 downto 0);
22
                data_out:       out std_logic_vector(31 downto 0);
23
                data_w:         out std_logic_vector(3 downto 0);
24
                data_access:    out std_logic
25
        );
26
end datapath;
27
 
28
architecture arch_datapath of datapath is
29
-- datapath signals
30
        signal data_in_s, pc, pc_last, pc_plus4, pc_next, result, branch, jump, ext32, ext32b, ext32h, alu_src: std_logic_vector(31 downto 0);
31
        signal opcode, funct: std_logic_vector(5 downto 0);
32
        signal read_reg1, read_reg2, write_reg, rs, rt, rd, target: std_logic_vector(4 downto 0);
33
        signal write_data, read_data1, read_data2: std_logic_vector(31 downto 0);
34
        signal imm: std_logic_vector(15 downto 0);
35 17 serginhofr
        signal wreg, zero, less_than, br_link_ctl, branch_taken, jump_taken, stall_reg: std_logic;
36 13 serginhofr
        signal irq_ack_s, irq_ack_s_dly, bds: std_logic;
37
 
38
-- control signals
39
        signal reg_dst_ctl, reg_write_ctl, alu_src_ctl, reg_to_mem_ctl, mem_to_reg_ctl, mem_to_reg_ctl_dly, signed_imm_ctl, signed_rd_ctl, shift_ctl: std_logic;
40
        signal jump_ctl, mem_read_ctl, mem_write_ctl: std_logic_vector(1 downto 0);
41
        signal branch_ctl: std_logic_vector(2 downto 0);
42
        signal alu_op_ctl: std_logic_vector(3 downto 0);
43
 
44
 
45
        signal reg_dst_ctl_r, reg_write_ctl_r, alu_src_ctl_r, reg_to_mem_ctl_r, mem_to_reg_ctl_r, signed_imm_ctl_r, signed_rd_ctl_r, shift_ctl_r, br_link_ctl_r: std_logic;
46
        signal jump_ctl_r, mem_read_ctl_r, mem_write_ctl_r: std_logic_vector(1 downto 0);
47
        signal branch_ctl_r: std_logic_vector(2 downto 0);
48
        signal alu_op_ctl_r: std_logic_vector(3 downto 0);
49
        signal rs_r, rt_r, rd_r: std_logic_vector(4 downto 0);
50
        signal imm_r: std_logic_vector(15 downto 0);
51
begin
52
 
53
--
54
-- FETCH STAGE
55
--
56
-- 1st stage, instruction memory access, PC update, interrupt acknowledge logic
57
 
58
        -- program counter logic
59
        process(clock, reset, reg_to_mem_ctl_r, mem_to_reg_ctl_r, busy, stall)
60
        begin
61
                if reset = '1' then
62
                        pc <= (others => '0');
63
                        pc_last <= (others => '0');
64
                elsif clock'event and clock = '1' then
65
                        if stall = '0' then
66
                                if busy = '0' then
67
                                        pc <= pc_next;
68
                                        pc_last <= pc;
69
                                else
70 17 serginhofr
                                        if (reg_to_mem_ctl_r = '1' or mem_to_reg_ctl_r = '1') and bds = '0' then
71 13 serginhofr
                                                pc <= pc_last;
72
                                        end if;
73
                                end if;
74
                        end if;
75
                end if;
76
        end process;
77
 
78
        pc_plus4 <=     pc + 4;
79
 
80
        pc_next <=      irq_vector when irq = '1' and irq_ack_s = '1' else
81
                        branch when branch_taken = '1' else
82
                        jump when jump_taken = '1' else
83
                        pc_plus4;
84
 
85
        -- interrupt acknowledge logic
86
        irq_ack_s <= '1' when irq = '1' and
87
                bds = '0' and branch_taken = '0' and jump_taken = '0' and
88
                reg_to_mem_ctl_r = '0' and mem_to_reg_ctl_r = '0' else '0';
89
 
90
        irq_ack <= irq_ack_s_dly;
91
 
92
        process(clock, reset, irq, irq_ack_s, mem_to_reg_ctl_r, busy, stall)
93
        begin
94
                if reset = '1' then
95
                        irq_ack_s_dly <= '0';
96
                        bds <= '0';
97
                        mem_to_reg_ctl_dly <= '0';
98
                        stall_reg <= '0';
99
                elsif clock'event and clock = '1' then
100
                        stall_reg <= stall;
101
                        if stall = '0' then
102
                                mem_to_reg_ctl_dly <= mem_to_reg_ctl_r;
103
                                if busy = '0' then
104
                                        irq_ack_s_dly <= irq_ack_s;
105
                                        if branch_taken = '1' or jump_taken = '1' then
106
                                                bds <= '1';
107
                                        else
108
                                                bds <= '0';
109
                                        end if;
110
                                end if;
111
                        end if;
112
                end if;
113
        end process;
114
 
115
--
116
-- DECODE STAGE
117
--
118
-- 2nd stage, instruction decode, control unit operation, pipeline bubble insertion logic on load/store and 2nd branch delay slot
119
 
120
        -- instruction decode
121
        opcode <= inst_in(31 downto 26);
122
        rs <= inst_in(25 downto 21);
123
        rt <= inst_in(20 downto 16);
124
        rd <= "11111" when br_link_ctl = '1' else inst_in(15 downto 11);                                        -- FIXME: this will not work for the 'jalr rd, rs' format
125
        funct <= inst_in(5 downto 0);
126
        imm <= inst_in(15 downto 0);
127
 
128
        -- control unit
129
        control_hellfire: entity work.control
130
        port map(       opcode => opcode,
131
                        funct => funct,
132
                        rtx => rt,
133
                        reg_dst => reg_dst_ctl,
134
                        reg_write => reg_write_ctl,
135
                        alu_src => alu_src_ctl,
136
                        alu_op => alu_op_ctl,
137
                        jump => jump_ctl,
138
                        branch => branch_ctl,
139
                        br_link => br_link_ctl,
140
                        reg_to_mem => reg_to_mem_ctl,
141
                        mem_to_reg => mem_to_reg_ctl,
142
                        signed_imm => signed_imm_ctl,
143
                        mem_write => mem_write_ctl,
144
                        mem_read => mem_read_ctl,
145
                        signed_rd => signed_rd_ctl,
146
                        shift => shift_ctl
147
        );
148
 
149
        process(clock, reset, busy, stall)
150
        begin
151
                if reset = '1' then
152
                        rs_r <= (others => '0');
153
                        rt_r <= (others => '0');
154
                        rd_r <= (others => '0');
155
                        imm_r <= (others => '0');
156
                        reg_dst_ctl_r <= '0';
157
                        reg_write_ctl_r <= '0';
158
                        alu_src_ctl_r <= '0';
159
                        alu_op_ctl_r <= (others => '0');
160
                        jump_ctl_r <= (others => '0');
161
                        branch_ctl_r <= (others => '0');
162
                        br_link_ctl_r <= '0';
163
                        reg_to_mem_ctl_r <= '0';
164
                        mem_to_reg_ctl_r <= '0';
165
                        signed_imm_ctl_r <= '0';
166
                        mem_write_ctl_r <= "00";
167
                        mem_read_ctl_r <= "00";
168
                        signed_rd_ctl_r <= '0';
169
                        shift_ctl_r <= '0';
170
                elsif clock'event and clock = '1' then
171
                        if stall = '0' then
172
                                if irq_ack_s = '1' then
173
                                        rs_r <= (others => '0');
174
                                        rt_r <= (others => '0');
175
                                        rd_r <= (others => '0');
176
                                        imm_r <= (others => '0');
177
                                        reg_dst_ctl_r <= '0';
178
                                        reg_write_ctl_r <= '0';
179
                                        alu_src_ctl_r <= '0';
180
                                        alu_op_ctl_r <= (others => '0');
181
                                        jump_ctl_r <= (others => '0');
182
                                        branch_ctl_r <= (others => '0');
183
                                        br_link_ctl_r <= '0';
184
                                        reg_to_mem_ctl_r <= '0';
185
                                        mem_to_reg_ctl_r <= '0';
186
                                        signed_imm_ctl_r <= '0';
187
                                        mem_write_ctl_r <= "00";
188
                                        mem_read_ctl_r <= "00";
189
                                        signed_rd_ctl_r <= '0';
190
                                        shift_ctl_r <= '0';
191
                                else
192
                                        if busy = '0' then
193 17 serginhofr
                                                if reg_to_mem_ctl_r = '1' or mem_to_reg_ctl_r = '1' or bds = '1' then
194 13 serginhofr
                                                        rs_r <= (others => '0');
195
                                                        rt_r <= (others => '0');
196
                                                        rd_r <= (others => '0');
197
                                                        imm_r <= (others => '0');
198
                                                        reg_dst_ctl_r <= '0';
199
                                                        reg_write_ctl_r <= '0';
200
                                                        alu_src_ctl_r <= '0';
201
                                                        alu_op_ctl_r <= (others => '0');
202
                                                        jump_ctl_r <= (others => '0');
203
                                                        branch_ctl_r <= (others => '0');
204
                                                        br_link_ctl_r <= '0';
205
                                                        reg_to_mem_ctl_r <= '0';
206
                                                        mem_to_reg_ctl_r <= '0';
207
                                                        signed_imm_ctl_r <= '0';
208
                                                        mem_write_ctl_r <= "00";
209
                                                        mem_read_ctl_r <= "00";
210
                                                        signed_rd_ctl_r <= '0';
211
                                                        shift_ctl_r <= '0';
212
                                                else
213
                                                        rs_r <= rs;
214
                                                        rt_r <= rt;
215
                                                        rd_r <= rd;
216
                                                        imm_r <= imm;
217
                                                        reg_dst_ctl_r <= reg_dst_ctl;
218
                                                        reg_write_ctl_r <= reg_write_ctl;
219
                                                        alu_src_ctl_r <= alu_src_ctl;
220
                                                        alu_op_ctl_r <= alu_op_ctl;
221
                                                        jump_ctl_r <= jump_ctl;
222
                                                        branch_ctl_r <= branch_ctl;
223
                                                        br_link_ctl_r <= br_link_ctl;
224
                                                        reg_to_mem_ctl_r <= reg_to_mem_ctl;
225
                                                        mem_to_reg_ctl_r <= mem_to_reg_ctl;
226
                                                        signed_imm_ctl_r <= signed_imm_ctl;
227
                                                        mem_write_ctl_r <= mem_write_ctl;
228
                                                        mem_read_ctl_r <= mem_read_ctl;
229
                                                        signed_rd_ctl_r <= signed_rd_ctl;
230
                                                        shift_ctl_r <= shift_ctl;
231
                                                end if;
232
                                        end if;
233
                                end if;
234
                        end if;
235
                end if;
236
        end process;
237
 
238
--
239
-- EXECUTE STAGE
240
--
241
 
242
-- 3rd stage (a) register file access (read)
243
        -- the register file
244
        register_bank: entity work.reg_bank
245
        port map(       clock => clock,
246
                        read_reg1 => read_reg1,
247
                        read_reg2 => read_reg2,
248
                        write_reg => write_reg,
249
                        wreg => wreg,
250
                        write_data => write_data,
251
                        read_data1 => read_data1,
252
                        read_data2 => read_data2
253
        );
254
 
255
        -- register file read/write selection and write enable
256
        read_reg1 <= rs_r when shift_ctl_r = '0' else rt_r;                                              -- source for shifts or normal operations
257
        read_reg2 <= "00000" when branch_ctl_r > "010" else                                             -- source for branch and link (for zero operations)
258
                        rs_r when shift_ctl_r = '1' else rt_r;                                          -- source for register based shifts or normal operations
259
        write_reg <= target when mem_to_reg_ctl_r = '0' else rt_r;
260
        ext32 <= x"0000" & imm_r when (imm_r(15) = '0' or signed_imm_ctl_r = '0') else x"ffff" & imm_r;
261
        target <= rt_r when reg_dst_ctl_r = '0' else rd_r;                                               -- target register selection
262
        wreg <= (reg_write_ctl_r or mem_to_reg_ctl_dly) and not busy and not stall_reg;                 -- enable the register bank for write back also
263
 
264
-- 3rd stage (b) ALU operation
265
        alu: entity work.alu
266
        port map(       op1 => read_data1,
267
                        op2 => alu_src,
268
                        alu_op => alu_op_ctl_r,
269
                        result => result,
270
                        zero => zero,
271
                        less_than => less_than
272
        );
273
 
274
        alu_src <= read_data2 when alu_src_ctl_r = '0' else ext32;
275
 
276
        branch <= (ext32(29 downto 0) & "00") + pc_last;
277
        jump <= read_data1 when jump_ctl_r = "10" else pc_last(31 downto 28) & rs_r & rt_r & imm_r & "00";
278
 
279
        branch_taken <= '1' when (zero = '1' and branch_ctl_r = "001") or                                               -- BEQ
280
                                (zero = '0' and branch_ctl_r = "010") or                                         -- BNE
281
                                ((zero = '1' or less_than = '1') and branch_ctl_r = "011") or                           -- BLEZ
282
                                ((zero = '0' and less_than = '0') and branch_ctl_r = "100") or                            -- BGTZ
283
                                ((zero = '0' and less_than = '1') and branch_ctl_r = "101") or                           -- BLTZ, BLTZAL
284
                                ((zero = '1' or less_than = '0') and branch_ctl_r = "110")                               -- BGEZ, BGEZAL
285
                                else '0';
286
        jump_taken <= '1' when jump_ctl_r /= "00" else '0';                                                              -- J, JAL, JR, JALR
287
 
288
        inst_addr <= pc;
289
        data_addr <= result; --result(31 downto 2) & "00";
290
        data_access <= '1' when reg_to_mem_ctl_r = '1' or mem_to_reg_ctl_r = '1' else '0';
291
 
292
 
293
-- 3rd stage (c) data memory / write back operation, register file access (write)
294
        -- memory access, store operations
295
        process(mem_write_ctl_r, result, read_data2)
296
        begin
297
                case mem_write_ctl_r is
298
                        when "11" =>                    -- store word
299
                                data_out <= read_data2;
300
                                data_w <= "1111";
301
                        when "01" =>                    -- store byte
302
                                data_out <= read_data2(7 downto 0) & read_data2(7 downto 0) & read_data2(7 downto 0) & read_data2(7 downto 0);
303
                                case result(1 downto 0) is
304
                                        when "11" => data_w <= "0001";
305
                                        when "10" => data_w <= "0010";
306
                                        when "01" => data_w <= "0100";
307
                                        when others => data_w <= "1000";
308
                                end case;
309
                        when "10" =>                    -- store half word
310
                                data_out <= read_data2(15 downto 0) & read_data2(15 downto 0);
311
                                case result(1) is
312
                                        when '1' => data_w <= "0011";
313
                                        when others => data_w <= "1100";
314
                                end case;
315
                        when others =>                  -- WTF??
316
                                data_out <= read_data2;
317
                                data_w <= "0000";
318
                end case;
319
        end process;
320
 
321
        -- memory access, load operations
322
        process(mem_read_ctl_r, result, data_in)
323
        begin
324
                case mem_read_ctl_r is
325
                        when "01" =>                    -- load byte
326
                                case result(1 downto 0) is
327
                                        when "11" => data_in_s <= x"000000" & data_in(7 downto 0);
328
                                        when "10" => data_in_s <= x"000000" & data_in(15 downto 8);
329
                                        when "01" => data_in_s <= x"000000" & data_in(23 downto 16);
330
                                        when others => data_in_s <= x"000000" & data_in(31 downto 24);
331
 
332
                                end case;
333
                        when "10" =>                    -- load half word
334
                                case result(1) is
335
                                        when '1' => data_in_s <= x"0000" & data_in(15 downto 0);
336
                                        when others => data_in_s <= x"0000" & data_in(31 downto 16);
337
                                end case;
338
                        when others =>                  -- load word
339
                                data_in_s <= data_in;
340
                end case;
341
        end process;
342
 
343
        -- write back
344
        ext32b <= x"000000" & data_in_s(7 downto 0) when (data_in_s(7) = '0' or signed_rd_ctl_r = '0') else x"ffffff" & data_in_s(7 downto 0);
345
        ext32h <= x"0000" & data_in_s(15 downto 0) when (data_in_s(15) = '0' or signed_rd_ctl_r = '0') else x"ffff" & data_in_s(15 downto 0);
346
 
347
        write_data <= data_in_s when mem_read_ctl_r = "11" else
348
                        ext32b when mem_read_ctl_r = "01" else
349
                        ext32h when mem_read_ctl_r = "10" else
350
                        pc when br_link_ctl_r = '1' else result;
351
 
352
end arch_datapath;
353
 

powered by: WebSVN 2.1.0

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