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 13

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

powered by: WebSVN 2.1.0

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