mem_pkg.vhd 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. library ieee;
  2. use ieee.std_logic_1164.all;
  3. package mem_pkg is
  4. -- width of a dataword
  5. constant DATA_WIDTH_BITS : integer := 5;
  6. constant DATA_WIDTH : integer := 2**DATA_WIDTH_BITS;
  7. constant BYTE_WIDTH : integer := 8;
  8. constant BYTES_PER_WORD : integer := (DATA_WIDTH+BYTE_WIDTH-1)/BYTE_WIDTH;
  9. -- bits to address memory
  10. -- NOTE: these are word addresses
  11. constant ADDR_WIDTH : integer := 14;
  12. constant BYTEEN_WIDTH : integer := DATA_WIDTH/BYTE_WIDTH;
  13. subtype mem_address_type is std_logic_vector(ADDR_WIDTH-1 downto 0);
  14. subtype mem_data_type is std_logic_vector(DATA_WIDTH-1 downto 0);
  15. subtype mem_byteena_type is std_logic_vector(BYTEEN_WIDTH-1 downto 0);
  16. -- NOTE: memory direction (in, out) is seen from RISC-V.
  17. type mem_out_type is
  18. record
  19. address : mem_address_type;
  20. rd, wr : std_logic;
  21. byteena : mem_byteena_type;
  22. wrdata : mem_data_type;
  23. end record;
  24. constant MEM_OUT_NOP : mem_out_type := (
  25. address => (others => '0'),
  26. rd => '0',
  27. wr => '0',
  28. byteena => (others => '1'),
  29. wrdata => (others => '0')
  30. );
  31. type mem_in_type is
  32. record
  33. busy : std_logic;
  34. rddata : mem_data_type;
  35. end record;
  36. constant MEM_IN_NOP : mem_in_type := (
  37. busy => '0',
  38. rddata => (others => '0')
  39. );
  40. subtype mem_out_range_type is natural range DATA_WIDTH + ADDR_WIDTH + 1 + 1 + BYTEEN_WIDTH - 1 downto 0;
  41. subtype mem_in_range_type is natural range DATA_WIDTH + 1 - 1 downto 0;
  42. pure function to_std_logic_vector(i : mem_out_type) return std_logic_vector;
  43. pure function to_std_logic_vector(i : mem_in_type) return std_logic_vector;
  44. pure function to_mem_out_type(i : std_logic_vector(mem_out_range_type)) return mem_out_type;
  45. pure function to_mem_in_type(i : std_logic_vector(mem_in_range_type)) return mem_in_type;
  46. end package;
  47. package body mem_pkg is
  48. pure function to_std_logic_vector(i : mem_out_type) return std_logic_vector is
  49. begin
  50. return i.address & i.wrdata & i.wr & i.rd & i.byteena;
  51. end function;
  52. pure function to_mem_out_type(i : std_logic_vector(mem_out_range_type)) return mem_out_type is
  53. variable ofs : natural := 0;
  54. variable ret : mem_out_type;
  55. begin
  56. ret.byteena := i(ofs + BYTEEN_WIDTH - 1 downto ofs);
  57. ofs := ofs + BYTEEN_WIDTH;
  58. ret.rd := i(ofs);
  59. ofs := ofs + 1;
  60. ret.wr := i(ofs);
  61. ofs := ofs + 1;
  62. ret.wrdata := i(ofs + DATA_WIDTH - 1 downto ofs);
  63. ofs := ofs + DATA_WIDTH;
  64. ret.address := i(ofs + ADDR_WIDTH - 1 downto ofs);
  65. return ret;
  66. end function;
  67. pure function to_std_logic_vector(i : mem_in_type) return std_logic_vector is
  68. begin
  69. return i.busy & i.rddata;
  70. end function;
  71. pure function to_mem_in_type(i : std_logic_vector(mem_in_range_type)) return mem_in_type is
  72. begin
  73. return mem_in_type'(
  74. rddata => i(31 downto 0),
  75. busy => i(32)
  76. );
  77. end function;
  78. end package body;