fetch.vhd 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. library ieee;
  2. use ieee.std_logic_1164.all;
  3. use ieee.numeric_std.all;
  4. use work.core_pkg.all;
  5. use work.op_pkg.all;
  6. use work.mem_pkg.all;
  7. entity fetch is
  8. port (
  9. clk : in std_logic;
  10. res_n : in std_logic;
  11. stall : in std_logic;
  12. flush : in std_logic;
  13. -- to control
  14. mem_busy : out std_logic;
  15. pcsrc : in std_logic;
  16. pc_in : in pc_type;
  17. pc_out : out pc_type := (others => '0');
  18. instr : out instr_type;
  19. -- memory controller interface
  20. mem_out : out mem_out_type;
  21. mem_in : in mem_in_type
  22. );
  23. end entity;
  24. architecture rtl of fetch is
  25. signal pc_out_next : pc_type := (others => '0');
  26. begin
  27. -- concurrent
  28. mem_busy <= mem_in.busy;
  29. mem_out.address(ADDR_WIDTH - 1 downto 0) <= pc_out_next(PC_WIDTH - 1 downto PC_WIDTH - ADDR_WIDTH);
  30. mem_out.rd <= '1';
  31. mem_out.wr <= '0';
  32. mem_out.byteena <= "1111";
  33. mem_out.wrdata <= (others => '0');
  34. instr(31 downto 24) <= NOP_INST(31 downto 24) when flush = '1' else mem_in.rddata(7 downto 0);
  35. instr(23 downto 16) <= NOP_INST(23 downto 16) when flush = '1' else mem_in.rddata(15 downto 8);
  36. instr(15 downto 8) <= NOP_INST(15 downto 8) when flush = '1' else mem_in.rddata(23 downto 16);
  37. instr(7 downto 0) <= NOP_INST(7 downto 0) when flush = '1' else mem_in.rddata(31 downto 24);
  38. -- sequential
  39. sync : process(clk, res_n)
  40. begin
  41. if res_n = '0' then
  42. pc_out <= (others => '0'); --initialize to -4
  43. elsif rising_edge(clk) then
  44. pc_out <= pc_out_next;
  45. end if;
  46. end process;
  47. async : process(all)
  48. begin
  49. pc_out_next <= pc_out;
  50. -- assumes instruction has the same length as mem data
  51. -- only change pc
  52. if stall = '0' and pcsrc = '1' then
  53. -- use pc_in
  54. pc_out_next <= pc_in;
  55. elsif stall = '0' and pcsrc = '0' then
  56. -- use pc_out + 4 (bytes)
  57. pc_out_next <= std_logic_vector(unsigned(pc_out) + x"4");
  58. end if;
  59. end process;
  60. end architecture;