You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

213 lines
6.8 KiB

3 years ago
  1. library ieee;
  2. use ieee.std_logic_1164.all;
  3. use ieee.numeric_std.all;
  4. use ieee.math_real.all;
  5. package risc_v_pkg is
  6. type t_slv_array is array (natural range <>) of std_logic_vector;
  7. subtype t_reg_file is t_slv_array(0 to 31)(31 downto 0);
  8. subtype t_imem is t_slv_array(natural range 0 to 57)(31 downto 0);
  9. subtype t_dmem is t_reg_file;
  10. -- Test program
  11. constant c_imem : t_imem := (
  12. -- (I) ADDI x1, x0, 10101
  13. b"00000001010100000000000010010011",
  14. -- (I) ADDI x2, x0, 111
  15. b"00000000011100000000000100010011",
  16. -- (I) ADDI x3, x0, 111111111100
  17. b"11111111110000000000000110010011",
  18. -- (I) ANDI x5, x1, 1011100
  19. b"00000101110000001111001010010011",
  20. -- (I) XORI x5, x5, 10101
  21. b"00000001010100101100001010010011",
  22. -- (I) ORI x6, x1, 1011100
  23. b"00000101110000001110001100010011",
  24. -- (I) XORI x6, x6, 1011100
  25. b"00000101110000110100001100010011",
  26. -- (I) ADDI x7, x1, 111
  27. b"00000000011100001000001110010011",
  28. -- (I) XORI x7, x7, 11101
  29. b"00000001110100111100001110010011",
  30. -- (I) SLLI x8, x1, 110
  31. b"00000000011000001001010000010011",
  32. -- (I) XORI x8, x8, 10101000001
  33. b"01010100000101000100010000010011",
  34. -- (I) SRLI x9, x1, 10
  35. b"00000000001000001101010010010011",
  36. -- (I) XORI x9, x9, 100
  37. b"00000000010001001100010010010011",
  38. -- (R) AND x10, x1,x2
  39. b"00000000001000001111010100110011",
  40. -- (I) XORI x10, x10, 100
  41. b"00000000010001010100010100010011",
  42. -- (R) OR x11, x1, x2
  43. b"00000000001000001110010110110011",
  44. -- (I) XORI x11, x11, 10110
  45. b"00000001011001011100010110010011",
  46. -- (R) XOR x12, x1, x2
  47. b"00000000001000001100011000110011",
  48. -- (I) XORI x12, x12, 10011
  49. b"00000001001101100100011000010011",
  50. -- (R) ADD x13, x1, x2
  51. b"00000000001000001000011010110011",
  52. -- (I) XORI x13, x13, 11101
  53. b"00000001110101101100011010010011",
  54. -- (R) SUB x14, x1, x2
  55. b"01000000001000001000011100110011",
  56. -- (I) XORI x14, x14, 1111
  57. b"00000000111101110100011100010011",
  58. -- (R) SLL x15, x2, x2
  59. b"00000000001000010001011110110011",
  60. -- (I) XORI x15, x15, 1110000001
  61. b"00111000000101111100011110010011",
  62. -- (R) SRL x16, x1, x2
  63. b"00000000001000001101100000110011",
  64. -- (I) XORI x16, x16, 1
  65. b"00000000000110000100100000010011",
  66. -- (R) SLTU x17, x2, x1
  67. b"00000000000100010011100010110011",
  68. -- (I) XORI x17, x17, 0
  69. b"00000000000010001100100010010011",
  70. -- (I) SLTIU x18, x2, 10101
  71. b"00000001010100010011100100010011",
  72. -- (I) XORI x18, x18, 0
  73. b"00000000000010010100100100010011",
  74. -- (U) LUI x19 ,0
  75. b"00000000000000000000100110110111",
  76. -- (I) XORI x19, x19, 1
  77. b"00000000000110011100100110010011",
  78. -- (I) SRAI x20, x2, 1
  79. b"01000000000100011101101000010011",
  80. -- (I) XORI x20, x20, 111111111111
  81. b"11111111111110100100101000010011",
  82. -- (R) SLT x21, x3, x1
  83. b"00000000000100011010101010110011",
  84. -- (I) XORI x21, x21, 0
  85. b"00000000000010101100101010010011",
  86. -- (I) SLTI x22, x3, 1
  87. b"00000000000100011010101100010011",
  88. -- (I) XORI x22, x22, 0
  89. b"00000000000010110100101100010011",
  90. -- (R) SRA x23, x1, x2
  91. b"01000000001000001101101110110011",
  92. -- (I) XORI x23, x23, 1
  93. b"00000000000110111100101110010011",
  94. -- (U) AUIPC x4, 100
  95. b"00000000000000000100001000010111",
  96. -- (I) SRLI x24, x4, 111
  97. b"00000000011100100101110000010011",
  98. -- (I) XORI x24, x24, 10000000
  99. b"00001000000011000100110000010011",
  100. -- (J) JAL x25, 10
  101. b"00000000010000000000110011101111",
  102. -- (U) AUIPC x4, 0
  103. b"00000000000000000000001000010111",
  104. -- (R) XOR x25, x25, x4
  105. b"00000000010011001100110010110011",
  106. -- (I) XORI x25, x25, 1
  107. b"00000000000111001100110010010011",
  108. -- (I) JALR x26, x4, 10000
  109. b"00000001000000100000110101100111",
  110. -- (R) SUB x26, x26, x4
  111. b"01000000010011010000110100110011",
  112. -- (I) ADDI x26, x26, 111111110001
  113. b"11111111000111010000110100010011",
  114. -- (S) SW x2, x1, 1
  115. b"00000000000100010010000010100011",
  116. -- (I) LW x27, x2, 1
  117. b"00000000000100010010110110000011",
  118. -- (I) XORI x27, x27, 10100
  119. b"00000001010011011100110110010011",
  120. -- (I) ADDI x28, x0, 1
  121. b"00000000000100000000111000010011",
  122. -- (I) ADDI x29, x0, 1
  123. b"00000000000100000000111010010011",
  124. -- (I) ADDI x30, x0, 1
  125. b"00000000000100000000111100010011",
  126. -- (J) JAL x0, 0
  127. b"00000000000000000000000001101111");
  128. function init_reg_file return t_reg_file;
  129. function init_dmem return t_dmem;
  130. function shift_right (data : in std_logic_vector(31 downto 0);
  131. index : in std_logic_vector) return std_logic_vector;
  132. function shift_left (data : in std_logic_vector(31 downto 0);
  133. index : in std_logic_vector) return std_logic_vector;
  134. function shift_arith_right (data : in std_logic_vector(31 downto 0);
  135. index : in std_logic_vector) return std_logic_vector;
  136. function to_std_logic (data : in boolean) return std_logic;
  137. procedure check_equal (a, b : in std_logic_vector; prefix : in string := "");
  138. end package risc_v_pkg;
  139. package body risc_v_pkg is
  140. function init_reg_file return t_reg_file is
  141. variable v_reg_file : t_reg_file;
  142. begin
  143. for i in t_reg_file'range loop
  144. v_reg_file(i) := std_logic_vector(to_unsigned(i, 32));
  145. end loop;
  146. return v_reg_file;
  147. end init_reg_file;
  148. function init_dmem return t_dmem is
  149. variable v_dmem : t_dmem;
  150. begin
  151. for i in t_dmem'range loop
  152. v_dmem(t_dmem'high-i) := std_logic_vector(to_unsigned(i, 32));
  153. end loop;
  154. return v_dmem;
  155. end init_dmem;
  156. function shift_right (data : in std_logic_vector(31 downto 0);
  157. index : in std_logic_vector) return std_logic_vector is
  158. begin
  159. return std_logic_vector(shift_right(unsigned(data),
  160. to_integer(unsigned(index))));
  161. end function shift_right;
  162. function shift_left (data : in std_logic_vector(31 downto 0);
  163. index : in std_logic_vector) return std_logic_vector is
  164. begin
  165. return std_logic_vector(shift_left(unsigned(data),
  166. to_integer(unsigned(index))));
  167. end function shift_left;
  168. function shift_arith_right (data : in std_logic_vector(31 downto 0);
  169. index : in std_logic_vector) return std_logic_vector is
  170. begin
  171. return std_logic_vector(shift_right(signed(data),
  172. to_integer(unsigned(index))));
  173. end function shift_arith_right;
  174. function to_std_logic (data : in boolean) return std_logic is
  175. begin
  176. if data then
  177. return '1';
  178. else
  179. return '0';
  180. end if;
  181. end function to_std_logic;
  182. procedure check_equal (a, b : in std_logic_vector; prefix : in string := "") is
  183. begin
  184. assert a = b
  185. report prefix & "expected 0x" & to_hstring(b) & ", got 0x" & to_hstring(a);
  186. end procedure check_equal;
  187. end package body risc_v_pkg;