Line 6... |
Line 6... |
entity datapath is
|
entity datapath is
|
port ( clock: in std_logic;
|
port ( clock: in std_logic;
|
reset: in std_logic;
|
reset: in std_logic;
|
|
|
stall: in std_logic;
|
stall: in std_logic;
|
busy: in std_logic;
|
|
|
|
irq_vector: in std_logic_vector(31 downto 0);
|
irq_vector: in std_logic_vector(31 downto 0);
|
irq: in std_logic;
|
irq: in std_logic;
|
irq_ack: out std_logic;
|
irq_ack: out std_logic;
|
exception: out std_logic;
|
exception: out std_logic;
|
|
|
inst_addr: out std_logic_vector(31 downto 0);
|
address: out std_logic_vector(31 downto 0);
|
inst_in: in std_logic_vector(31 downto 0);
|
|
|
|
data_addr: out std_logic_vector(31 downto 0);
|
|
data_in: in std_logic_vector(31 downto 0);
|
data_in: in std_logic_vector(31 downto 0);
|
data_out: out std_logic_vector(31 downto 0);
|
data_out: out std_logic_vector(31 downto 0);
|
data_w: out std_logic_vector(3 downto 0);
|
data_w: out std_logic_vector(3 downto 0);
|
data_access: out std_logic
|
data_access: out std_logic
|
);
|
);
|
Line 34... |
Line 30... |
signal funct3: std_logic_vector(2 downto 0);
|
signal funct3: std_logic_vector(2 downto 0);
|
signal read_reg1, read_reg2, write_reg, rs1, rs2, rd: std_logic_vector(4 downto 0);
|
signal read_reg1, read_reg2, write_reg, rs1, rs2, rd: std_logic_vector(4 downto 0);
|
signal write_data, read_data1, read_data2: std_logic_vector(31 downto 0);
|
signal write_data, read_data1, read_data2: std_logic_vector(31 downto 0);
|
signal imm_i, imm_s, imm_sb, imm_uj, branch_src1, branch_src2: std_logic_vector(31 downto 0);
|
signal imm_i, imm_s, imm_sb, imm_uj, branch_src1, branch_src2: std_logic_vector(31 downto 0);
|
signal imm_u: std_logic_vector(31 downto 12);
|
signal imm_u: std_logic_vector(31 downto 12);
|
signal wreg, zero, less_than, branch_taken, jump_taken, stall_reg: std_logic;
|
signal wreg, zero, less_than, branch_taken, jump_taken, mwait, stall_reg: std_logic;
|
signal irq_ack_s, irq_ack_s_dly, bds: std_logic;
|
signal irq_ack_s, irq_ack_s_dly, bds, data_access_s, data_access_s_dly: std_logic;
|
|
|
-- control signals
|
-- control signals
|
signal reg_write_ctl, alu_src1_ctl, sig_read_ctl, reg_to_mem, mem_to_reg, except: std_logic;
|
signal reg_write_ctl, alu_src1_ctl, sig_read_ctl, reg_to_mem, mem_to_reg, except: std_logic;
|
signal jump_ctl, mem_write_ctl, mem_read_ctl: std_logic_vector(1 downto 0);
|
signal jump_ctl, mem_write_ctl, mem_read_ctl: std_logic_vector(1 downto 0);
|
signal alu_src2_ctl, branch_ctl: std_logic_vector(2 downto 0);
|
signal alu_src2_ctl, branch_ctl: std_logic_vector(2 downto 0);
|
Line 58... |
Line 54... |
-- FETCH STAGE
|
-- FETCH STAGE
|
--
|
--
|
-- 1st stage, instruction memory access, PC update, interrupt acknowledge logic
|
-- 1st stage, instruction memory access, PC update, interrupt acknowledge logic
|
|
|
-- program counter logic
|
-- program counter logic
|
process(clock, reset, reg_to_mem_r, mem_to_reg_r, busy, stall)
|
process(clock, reset, reg_to_mem_r, mem_to_reg_r, mwait, stall)
|
begin
|
begin
|
if reset = '1' then
|
if reset = '1' then
|
pc <= (others => '0');
|
pc <= (others => '0');
|
pc_last <= (others => '0');
|
pc_last <= (others => '0');
|
pc_last2 <= (others => '0');
|
pc_last2 <= (others => '0');
|
elsif clock'event and clock = '1' then
|
elsif clock'event and clock = '1' then
|
if stall = '0' then
|
if stall = '0' then
|
if busy = '0' then
|
if mwait = '0' then
|
pc <= pc_next;
|
pc <= pc_next;
|
pc_last <= pc;
|
pc_last <= pc;
|
pc_last2 <= pc_last;
|
pc_last2 <= pc_last;
|
else
|
else
|
if (reg_to_mem_r = '1' or mem_to_reg_r = '1' or except = '1') and bds = '0' then
|
if (reg_to_mem_r = '1' or mem_to_reg_r = '1' or except = '1') and bds = '0' then
|
Line 94... |
Line 90... |
|
|
irq_ack <= irq_ack_s_dly;
|
irq_ack <= irq_ack_s_dly;
|
|
|
exception <= '1' when except = '1' else '0';
|
exception <= '1' when except = '1' else '0';
|
|
|
process(clock, reset, irq, irq_ack_s, mem_to_reg_r, busy, stall)
|
process(clock, reset, irq, irq_ack_s, mem_to_reg_r, mwait, stall)
|
begin
|
begin
|
if reset = '1' then
|
if reset = '1' then
|
irq_ack_s_dly <= '0';
|
irq_ack_s_dly <= '0';
|
bds <= '0';
|
bds <= '0';
|
mem_to_reg_r_dly <= '0';
|
mem_to_reg_r_dly <= '0';
|
stall_reg <= '0';
|
stall_reg <= '0';
|
|
data_access_s_dly <= '0';
|
elsif clock'event and clock = '1' then
|
elsif clock'event and clock = '1' then
|
stall_reg <= stall;
|
stall_reg <= stall;
|
if stall = '0' then
|
if stall = '0' then
|
mem_to_reg_r_dly <= mem_to_reg_r;
|
mem_to_reg_r_dly <= mem_to_reg_r;
|
if busy = '0' then
|
data_access_s_dly <= data_access_s;
|
|
if mwait = '0' then
|
irq_ack_s_dly <= irq_ack_s;
|
irq_ack_s_dly <= irq_ack_s;
|
if branch_taken = '1' or jump_taken = '1' then
|
if branch_taken = '1' or jump_taken = '1' then
|
bds <= '1';
|
bds <= '1';
|
else
|
else
|
bds <= '0';
|
bds <= '0';
|
Line 123... |
Line 121... |
-- DECODE STAGE
|
-- DECODE STAGE
|
--
|
--
|
-- 2nd stage, instruction decode, control unit operation, pipeline bubble insertion logic on load/store and branches
|
-- 2nd stage, instruction decode, control unit operation, pipeline bubble insertion logic on load/store and branches
|
|
|
-- instruction decode
|
-- instruction decode
|
inst_in_s <= inst_in(7 downto 0) & inst_in(15 downto 8) & inst_in(23 downto 16) & inst_in(31 downto 24);
|
inst_in_s <= data_in(7 downto 0) & data_in(15 downto 8) & data_in(23 downto 16) & data_in(31 downto 24);
|
|
|
opcode <= inst_in_s(6 downto 0);
|
opcode <= inst_in_s(6 downto 0);
|
funct3 <= inst_in_s(14 downto 12);
|
funct3 <= inst_in_s(14 downto 12);
|
funct7 <= inst_in_s(31 downto 25);
|
funct7 <= inst_in_s(31 downto 25);
|
rd <= inst_in_s(11 downto 7);
|
rd <= inst_in_s(11 downto 7);
|
Line 139... |
Line 137... |
imm_u <= inst_in_s(31 downto 12);
|
imm_u <= inst_in_s(31 downto 12);
|
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';
|
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';
|
ext32 <= (others => '1') when inst_in_s(31) = '1' else (others => '0');
|
ext32 <= (others => '1') when inst_in_s(31) = '1' else (others => '0');
|
|
|
-- control unit
|
-- control unit
|
control_hellfire: entity work.control
|
control_unit: entity work.control
|
port map( opcode => opcode,
|
port map( opcode => opcode,
|
funct3 => funct3,
|
funct3 => funct3,
|
funct7 => funct7,
|
funct7 => funct7,
|
reg_write => reg_write_ctl,
|
reg_write => reg_write_ctl,
|
alu_src1 => alu_src1_ctl,
|
alu_src1 => alu_src1_ctl,
|
Line 157... |
Line 155... |
);
|
);
|
|
|
reg_to_mem <= '1' when mem_write_ctl /= "00" else '0';
|
reg_to_mem <= '1' when mem_write_ctl /= "00" else '0';
|
mem_to_reg <= '1' when mem_read_ctl /= "00" else '0';
|
mem_to_reg <= '1' when mem_read_ctl /= "00" else '0';
|
|
|
process(clock, reset, irq_ack_s, bds, busy, stall)
|
process(clock, reset, irq_ack_s, bds, mwait, stall)
|
begin
|
begin
|
if reset = '1' then
|
if reset = '1' then
|
rd_r <= (others => '0');
|
rd_r <= (others => '0');
|
rs1_r <= (others => '0');
|
rs1_r <= (others => '0');
|
rs2_r <= (others => '0');
|
rs2_r <= (others => '0');
|
Line 202... |
Line 200... |
mem_read_ctl_r <= (others => '0');
|
mem_read_ctl_r <= (others => '0');
|
sig_read_ctl_r <= '0';
|
sig_read_ctl_r <= '0';
|
reg_to_mem_r <= '0';
|
reg_to_mem_r <= '0';
|
mem_to_reg_r <= '0';
|
mem_to_reg_r <= '0';
|
else
|
else
|
if busy = '0' then
|
if mwait = '0' then
|
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
|
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
|
rd_r <= (others => '0');
|
rd_r <= (others => '0');
|
rs1_r <= (others => '0');
|
rs1_r <= (others => '0');
|
rs2_r <= (others => '0');
|
rs2_r <= (others => '0');
|
imm_i_r <= (others => '0');
|
imm_i_r <= (others => '0');
|
Line 271... |
Line 269... |
|
|
-- register file read/write selection and write enable
|
-- register file read/write selection and write enable
|
read_reg1 <= rs1_r;
|
read_reg1 <= rs1_r;
|
read_reg2 <= rs2_r;
|
read_reg2 <= rs2_r;
|
write_reg <= rd_r;
|
write_reg <= rd_r;
|
wreg <= (reg_write_ctl_r or mem_to_reg_r_dly) and not busy and not stall_reg;
|
wreg <= (reg_write_ctl_r or mem_to_reg_r_dly) and not mwait and not stall_reg;
|
|
|
-- 3rd stage (b) ALU operation
|
-- 3rd stage (b) ALU operation
|
alu: entity work.alu
|
alu: entity work.alu
|
port map( op1 => alu_src1,
|
port map( op1 => alu_src1,
|
op2 => alu_src2,
|
op2 => alu_src2,
|
Line 307... |
Line 305... |
(less_than = '0' and branch_ctl_r = "110") -- BGEU
|
(less_than = '0' and branch_ctl_r = "110") -- BGEU
|
else '0';
|
else '0';
|
except <= '1' when branch_ctl_r = "111" else '0';
|
except <= '1' when branch_ctl_r = "111" else '0';
|
jump_taken <= '1' when jump_ctl_r /= "00" else '0';
|
jump_taken <= '1' when jump_ctl_r /= "00" else '0';
|
|
|
inst_addr <= pc;
|
address <= result when data_access_s = '1' and mwait = '1' else pc;
|
data_addr <= result; --result(31 downto 2) & "00";
|
data_access_s <= '1' when reg_to_mem_r = '1' or mem_to_reg_r = '1' else '0';
|
data_access <= '1' when reg_to_mem_r = '1' or mem_to_reg_r = '1' else '0';
|
mwait <= '1' when data_access_s = '1' and data_access_s_dly = '0' else '0';
|
|
data_access <= mwait;
|
|
|
|
|
-- 3rd stage (c) data memory / write back operation, register file access (write)
|
-- 3rd stage (c) data memory / write back operation, register file access (write)
|
-- memory access, store operations
|
-- memory access, store operations
|
process(mem_write_ctl_r, result, read_data2)
|
process(mem_write_ctl_r, result, read_data2)
|