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

Subversion Repositories hf-risc

[/] [hf-risc/] [trunk/] [hf-riscv/] [core_rv32i/] [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
                exception:      out std_logic;
17
 
18
                inst_addr:      out std_logic_vector(31 downto 0);
19
                inst_in:        in std_logic_vector(31 downto 0);
20
 
21
                data_addr:      out std_logic_vector(31 downto 0);
22
                data_in:        in std_logic_vector(31 downto 0);
23
                data_out:       out std_logic_vector(31 downto 0);
24
                data_w:         out std_logic_vector(3 downto 0);
25
                data_access:    out std_logic
26
        );
27
end datapath;
28
 
29
architecture arch_datapath of datapath is
30
-- datapath signals
31
        signal inst_in_s, data_in_s, pc, pc_last, pc_last2, pc_plus4, pc_next, result, branch, ext32b, ext32h, alu_src1, alu_src2: std_logic_vector(31 downto 0);
32
        signal ext32: std_logic_vector(31 downto 12);
33
        signal opcode, funct7: std_logic_vector(6 downto 0);
34
        signal funct3: std_logic_vector(2 downto 0);
35
        signal read_reg1, read_reg2, write_reg, rs1, rs2, rd: std_logic_vector(4 downto 0);
36
        signal write_data, read_data1, read_data2: std_logic_vector(31 downto 0);
37
        signal imm_i, imm_s, imm_sb, imm_uj, branch_src1, branch_src2: std_logic_vector(31 downto 0);
38
        signal imm_u: std_logic_vector(31 downto 12);
39 17 serginhofr
        signal wreg, zero, less_than, branch_taken, jump_taken, stall_reg: std_logic;
40 13 serginhofr
        signal irq_ack_s, irq_ack_s_dly, bds: std_logic;
41
 
42
-- control signals
43
        signal reg_write_ctl, alu_src1_ctl, sig_read_ctl, reg_to_mem, mem_to_reg, except: std_logic;
44
        signal jump_ctl, mem_write_ctl, mem_read_ctl: std_logic_vector(1 downto 0);
45
        signal alu_src2_ctl, branch_ctl: std_logic_vector(2 downto 0);
46
        signal alu_op_ctl: std_logic_vector(3 downto 0);
47
 
48
        signal rs1_r, rs2_r, rd_r: std_logic_vector(4 downto 0);
49
        signal imm_i_r, imm_s_r, imm_sb_r, imm_uj_r: std_logic_vector(31 downto 0);
50
        signal imm_u_r: std_logic_vector(31 downto 12);
51
        signal reg_write_ctl_r, alu_src1_ctl_r, sig_read_ctl_r, reg_to_mem_r, mem_to_reg_r, mem_to_reg_r_dly: std_logic;
52
        signal jump_ctl_r, mem_write_ctl_r, mem_read_ctl_r: std_logic_vector(1 downto 0);
53
        signal alu_src2_ctl_r, branch_ctl_r: std_logic_vector(2 downto 0);
54
        signal alu_op_ctl_r: std_logic_vector(3 downto 0);
55
begin
56
 
57
--
58
-- FETCH STAGE
59
--
60
-- 1st stage, instruction memory access, PC update, interrupt acknowledge logic
61
 
62
        -- program counter logic
63
        process(clock, reset, reg_to_mem_r, mem_to_reg_r, busy, stall)
64
        begin
65
                if reset = '1' then
66
                        pc <= (others => '0');
67
                        pc_last <= (others => '0');
68
                        pc_last2 <= (others => '0');
69
                elsif clock'event and clock = '1' then
70
                        if stall = '0' then
71
                                if busy = '0' then
72
                                        pc <= pc_next;
73
                                        pc_last <= pc;
74
                                        pc_last2 <= pc_last;
75
                                else
76 17 serginhofr
                                        if (reg_to_mem_r = '1' or mem_to_reg_r = '1' or except = '1') and bds = '0' then
77 13 serginhofr
                                                pc <= pc_last;
78
                                        end if;
79
                                end if;
80
                        end if;
81
                end if;
82
        end process;
83
 
84
        pc_plus4 <=     pc + 4;
85
 
86
        pc_next <=      irq_vector when (irq = '1' and irq_ack_s = '1') or except = '1' else
87
                        branch when branch_taken = '1' or jump_taken = '1' else
88
                        pc_plus4;
89
 
90
        -- interrupt acknowledge logic
91
        irq_ack_s <= '1' when irq = '1' and
92
                bds = '0' and branch_taken = '0' and jump_taken = '0' and
93
                reg_to_mem_r = '0' and mem_to_reg_r = '0' else '0';
94
 
95
        irq_ack <= irq_ack_s_dly;
96
 
97
        exception <= '1' when except = '1' else '0';
98
 
99
        process(clock, reset, irq, irq_ack_s, mem_to_reg_r, busy, stall)
100
        begin
101
                if reset = '1' then
102
                        irq_ack_s_dly <= '0';
103
                        bds <= '0';
104
                        mem_to_reg_r_dly <= '0';
105
                        stall_reg <= '0';
106
                elsif clock'event and clock = '1' then
107
                        stall_reg <= stall;
108
                        if stall = '0' then
109
                                mem_to_reg_r_dly <= mem_to_reg_r;
110
                                if busy = '0' then
111
                                        irq_ack_s_dly <= irq_ack_s;
112
                                        if branch_taken = '1' or jump_taken = '1' then
113
                                                bds <= '1';
114
                                        else
115
                                                bds <= '0';
116
                                        end if;
117
                                end if;
118
                        end if;
119
                end if;
120
        end process;
121
 
122
--
123
-- DECODE STAGE
124
--
125
-- 2nd stage, instruction decode, control unit operation, pipeline bubble insertion logic on load/store and branches
126
 
127
        -- instruction decode
128
        inst_in_s <= inst_in(7 downto 0) & inst_in(15 downto 8) & inst_in(23 downto 16) & inst_in(31 downto 24);
129
 
130
        opcode <= inst_in_s(6 downto 0);
131
        funct3 <= inst_in_s(14 downto 12);
132
        funct7 <= inst_in_s(31 downto 25);
133
        rd <= inst_in_s(11 downto 7);
134
        rs1 <= inst_in_s(19 downto 15);
135
        rs2 <= inst_in_s(24 downto 20);
136
        imm_i <= ext32(31 downto 12) & inst_in_s(31 downto 20);
137
        imm_s <= ext32(31 downto 12) & inst_in_s(31 downto 25) & inst_in_s(11 downto 7);
138
        imm_sb <= ext32(31 downto 13) & inst_in_s(31) & inst_in_s(7) & inst_in_s(30 downto 25) & inst_in_s(11 downto 8) & '0';
139
        imm_u <= inst_in_s(31 downto 12);
140
        imm_uj <= ext32(31 downto 21) & inst_in_s(31) & inst_in_s(19 downto 12) & inst_in_s(20) & inst_in_s(30 downto 21) & '0';
141
        ext32 <= (others => '1') when inst_in_s(31) = '1' else (others => '0');
142
 
143
        -- control unit
144
        control_hellfire: entity work.control
145
        port map(       opcode => opcode,
146
                        funct3 => funct3,
147
                        funct7 => funct7,
148
                        reg_write => reg_write_ctl,
149
                        alu_src1 => alu_src1_ctl,
150
                        alu_src2 => alu_src2_ctl,
151
                        alu_op => alu_op_ctl,
152
                        jump => jump_ctl,
153
                        branch => branch_ctl,
154
                        mem_write => mem_write_ctl,
155
                        mem_read => mem_read_ctl,
156
                        sig_read => sig_read_ctl
157
        );
158
 
159
        reg_to_mem <= '1' when mem_write_ctl /= "00" else '0';
160
        mem_to_reg <= '1' when mem_read_ctl /= "00" else '0';
161
 
162
        process(clock, reset, irq_ack_s, bds, busy, stall)
163
        begin
164
                if reset = '1' then
165
                        rd_r <= (others => '0');
166
                        rs1_r <= (others => '0');
167
                        rs2_r <= (others => '0');
168
                        imm_i_r <= (others => '0');
169
                        imm_s_r <= (others => '0');
170
                        imm_sb_r <= (others => '0');
171
                        imm_u_r <= (others => '0');
172
                        imm_uj_r <= (others => '0');
173
                        reg_write_ctl_r <= '0';
174
                        alu_src1_ctl_r <= '0';
175
                        alu_src2_ctl_r <= (others => '0');
176
                        alu_op_ctl_r <= (others => '0');
177
                        jump_ctl_r <= (others => '0');
178
                        branch_ctl_r <= (others => '0');
179
                        mem_write_ctl_r <= (others => '0');
180
                        mem_read_ctl_r <= (others => '0');
181
                        sig_read_ctl_r <= '0';
182
                        reg_to_mem_r <= '0';
183
                        mem_to_reg_r <= '0';
184
                elsif clock'event and clock = '1' then
185
                        if stall = '0' then
186
                                if irq_ack_s = '1' then
187
                                        rd_r <= (others => '0');
188
                                        rs1_r <= (others => '0');
189
                                        rs2_r <= (others => '0');
190
                                        imm_i_r <= (others => '0');
191
                                        imm_s_r <= (others => '0');
192
                                        imm_sb_r <= (others => '0');
193
                                        imm_u_r <= (others => '0');
194
                                        imm_uj_r <= (others => '0');
195
                                        reg_write_ctl_r <= '0';
196
                                        alu_src1_ctl_r <= '0';
197
                                        alu_src2_ctl_r <= (others => '0');
198
                                        alu_op_ctl_r <= (others => '0');
199
                                        jump_ctl_r <= (others => '0');
200
                                        branch_ctl_r <= (others => '0');
201
                                        mem_write_ctl_r <= (others => '0');
202
                                        mem_read_ctl_r <= (others => '0');
203
                                        sig_read_ctl_r <= '0';
204
                                        reg_to_mem_r <= '0';
205
                                        mem_to_reg_r <= '0';
206
                                else
207
                                        if busy = '0' then
208 17 serginhofr
                                                if (reg_to_mem_r = '1' or mem_to_reg_r = '1' or except = '1' or branch_taken = '1' or jump_taken = '1' or bds = '1') then
209 13 serginhofr
                                                        rd_r <= (others => '0');
210
                                                        rs1_r <= (others => '0');
211
                                                        rs2_r <= (others => '0');
212
                                                        imm_i_r <= (others => '0');
213
                                                        imm_s_r <= (others => '0');
214
                                                        imm_sb_r <= (others => '0');
215
                                                        imm_u_r <= (others => '0');
216
                                                        imm_uj_r <= (others => '0');
217
                                                        reg_write_ctl_r <= '0';
218
                                                        alu_src1_ctl_r <= '0';
219
                                                        alu_src2_ctl_r <= (others => '0');
220
                                                        alu_op_ctl_r <= (others => '0');
221
                                                        jump_ctl_r <= (others => '0');
222
                                                        branch_ctl_r <= (others => '0');
223
                                                        mem_write_ctl_r <= (others => '0');
224
                                                        mem_read_ctl_r <= (others => '0');
225
                                                        sig_read_ctl_r <= '0';
226
                                                        reg_to_mem_r <= '0';
227
                                                        mem_to_reg_r <= '0';
228
                                                else
229
                                                        rd_r <= rd;
230
                                                        rs1_r <= rs1;
231
                                                        rs2_r <= rs2;
232
                                                        imm_i_r <= imm_i;
233
                                                        imm_s_r <= imm_s;
234
                                                        imm_sb_r <= imm_sb;
235
                                                        imm_u_r <= imm_u;
236
                                                        imm_uj_r <= imm_uj;
237
                                                        reg_write_ctl_r <= reg_write_ctl;
238
                                                        alu_src1_ctl_r <= alu_src1_ctl;
239
                                                        alu_src2_ctl_r <= alu_src2_ctl;
240
                                                        alu_op_ctl_r <= alu_op_ctl;
241
                                                        jump_ctl_r <= jump_ctl;
242
                                                        branch_ctl_r <= branch_ctl;
243
                                                        mem_write_ctl_r <= mem_write_ctl;
244
                                                        mem_read_ctl_r <= mem_read_ctl;
245
                                                        sig_read_ctl_r <= sig_read_ctl;
246
                                                        reg_to_mem_r <= reg_to_mem;
247
                                                        mem_to_reg_r <= mem_to_reg;
248
                                                end if;
249
                                        end if;
250
                                end if;
251
                        end if;
252
                end if;
253
        end process;
254
 
255
--
256
-- EXECUTE STAGE
257
--
258
 
259
-- 3rd stage (a) register file access (read)
260
        -- the register file
261
        register_bank: entity work.reg_bank
262
        port map(       clock => clock,
263
                        read_reg1 => read_reg1,
264
                        read_reg2 => read_reg2,
265
                        write_reg => write_reg,
266
                        wreg => wreg,
267
                        write_data => write_data,
268
                        read_data1 => read_data1,
269
                        read_data2 => read_data2
270
        );
271
 
272
        -- register file read/write selection and write enable
273
        read_reg1 <= rs1_r;
274
        read_reg2 <= rs2_r;
275
        write_reg <= rd_r;
276
        wreg <= (reg_write_ctl_r or mem_to_reg_r_dly) and not busy and not stall_reg;
277
 
278
-- 3rd stage (b) ALU operation
279
        alu: entity work.alu
280
        port map(       op1 => alu_src1,
281
                        op2 => alu_src2,
282
                        alu_op => alu_op_ctl_r,
283
                        result => result,
284
                        zero => zero,
285
                        less_than => less_than
286
        );
287
 
288
        alu_src1 <= read_data1 when alu_src1_ctl_r = '0' else pc_last2;
289
        alu_src2 <=     imm_u_r & x"000" when alu_src2_ctl_r = "000" else
290
                        imm_i_r when alu_src2_ctl_r = "001" else
291
                        imm_s_r when alu_src2_ctl_r = "010" else
292
                        pc when alu_src2_ctl_r = "011" else
293
                        x"000000" & "000" & rs2_r when alu_src2_ctl_r = "100" else
294
                        read_data2;
295
 
296
        branch_src1 <= read_data1 when jump_ctl_r = "11" else pc_last2;
297
        branch_src2 <= imm_uj_r when jump_ctl_r = "10" else
298
                        imm_i_r when jump_ctl_r = "11" else imm_sb_r;
299
 
300
        branch <= branch_src1 + branch_src2;
301
 
302
        branch_taken <= '1' when (zero = '1' and branch_ctl_r = "001") or                                               -- BEQ
303
                                (zero = '0' and branch_ctl_r = "010") or                                         -- BNE
304
                                (less_than = '1' and branch_ctl_r = "011") or                                           -- BLT
305
                                (less_than = '0' and branch_ctl_r = "100") or                                            -- BGE
306
                                (less_than = '1' and branch_ctl_r = "101") or                                           -- BLTU
307
                                (less_than = '0' and branch_ctl_r = "110")                                               -- BGEU
308
                                else '0';
309
        except <= '1' when branch_ctl_r = "111" else '0';
310
        jump_taken <= '1' when jump_ctl_r /= "00" else '0';
311
 
312
        inst_addr <= pc;
313
        data_addr <= result; --result(31 downto 2) & "00";
314
        data_access <= '1' when reg_to_mem_r = '1' or mem_to_reg_r = '1' else '0';
315
 
316
 
317
-- 3rd stage (c) data memory / write back operation, register file access (write)
318
        -- memory access, store operations
319
        process(mem_write_ctl_r, result, read_data2)
320
        begin
321
                case mem_write_ctl_r is
322
                        when "11" =>                    -- store word
323
                                data_out <= read_data2(7 downto 0) & read_data2(15 downto 8) & read_data2(23 downto 16) & read_data2(31 downto 24);
324
                                data_w <= "1111";
325
                        when "01" =>                    -- store byte
326
                                data_out <= read_data2(7 downto 0) & read_data2(7 downto 0) & read_data2(7 downto 0) & read_data2(7 downto 0);
327
                                case result(1 downto 0) is
328
                                        when "11" => data_w <= "0001";
329
                                        when "10" => data_w <= "0010";
330
                                        when "01" => data_w <= "0100";
331
                                        when others => data_w <= "1000";
332
                                end case;
333
                        when "10" =>                    -- store half word
334
                                data_out <= read_data2(7 downto 0) & read_data2(15 downto 8) & read_data2(7 downto 0) & read_data2(15 downto 8);
335
                                case result(1) is
336
                                        when '1' => data_w <= "0011";
337
                                        when others => data_w <= "1100";
338
                                end case;
339
                        when others =>                  -- WTF??
340
                                data_out <= read_data2(7 downto 0) & read_data2(15 downto 8) & read_data2(23 downto 16) & read_data2(31 downto 24);
341
                                data_w <= "0000";
342
                end case;
343
        end process;
344
 
345
        -- memory access, load operations
346
        process(mem_read_ctl_r, result, data_in)
347
        begin
348
                case mem_read_ctl_r is
349
                        when "01" =>                    -- load byte
350
                                case result(1 downto 0) is
351
                                        when "11" => data_in_s <= x"000000" & data_in(7 downto 0);
352
                                        when "10" => data_in_s <= x"000000" & data_in(15 downto 8);
353
                                        when "01" => data_in_s <= x"000000" & data_in(23 downto 16);
354
                                        when others => data_in_s <= x"000000" & data_in(31 downto 24);
355
 
356
                                end case;
357
                        when "10" =>                    -- load half word
358
                                case result(1) is
359
                                        when '1' => data_in_s <= x"0000" & data_in(7 downto 0) & data_in(15 downto 8);
360
                                        when others => data_in_s <= x"0000" & data_in(23 downto 16) & data_in(31 downto 24);
361
                                end case;
362
                        when others =>                  -- load word
363
                                data_in_s <= data_in(7 downto 0) & data_in(15 downto 8) & data_in(23 downto 16) & data_in(31 downto 24);
364
                end case;
365
        end process;
366
 
367
        -- write back
368
        ext32b <= x"000000" & data_in_s(7 downto 0) when (data_in_s(7) = '0' or sig_read_ctl_r = '0') else x"ffffff" & data_in_s(7 downto 0);
369
        ext32h <= x"0000" & data_in_s(15 downto 0) when (data_in_s(15) = '0' or sig_read_ctl_r = '0') else x"ffff" & data_in_s(15 downto 0);
370
 
371
        write_data <= data_in_s when mem_read_ctl_r = "11" else
372
                        ext32b when mem_read_ctl_r = "01" else
373
                        ext32h when mem_read_ctl_r = "10" else
374
                        pc_last when jump_taken = '1' else result;
375
 
376
end arch_datapath;
377
 

powered by: WebSVN 2.1.0

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