Trying to verify Verilog/VHDL designs with formal methods and tools
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.

121 lines
3.4 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. library ieee;
  2. use ieee.std_logic_1164.all;
  3. use ieee.numeric_std.all;
  4. entity alu is
  5. generic (
  6. Width : natural := 8;
  7. Formal : boolean := true
  8. );
  9. port (
  10. Reset_n_i : in std_logic;
  11. Clk_i : in std_logic;
  12. Opc_i : in std_logic_vector(1 downto 0);
  13. DinA_i : in std_logic_vector(Width-1 downto 0);
  14. DinB_i : in std_logic_vector(Width-1 downto 0);
  15. Dout_o : out std_logic_vector(Width-1 downto 0);
  16. OverFlow_o : out std_logic
  17. );
  18. end entity alu;
  19. architecture rtl of alu is
  20. subtype t_opc is std_logic_vector(Opc_i'range);
  21. constant c_add : t_opc := "00";
  22. constant c_sub : t_opc := "01";
  23. constant c_and : t_opc := "10";
  24. constant c_or : t_opc := "11";
  25. begin
  26. process (Reset_n_i, Clk_i) is
  27. variable v_result : std_logic_vector(Width downto 0);
  28. begin
  29. if (Reset_n_i = '0') then
  30. v_result := (others => '0');
  31. Dout_o <= (others => '0');
  32. OverFlow_o <= '0';
  33. elsif (rising_edge(Clk_i)) then
  34. case Opc_i is
  35. when c_add =>
  36. v_result := std_logic_vector(unsigned('0' & DinA_i) +
  37. unsigned('0' & DinB_i));
  38. when c_sub =>
  39. v_result := std_logic_vector(unsigned('0' & DinA_i) -
  40. unsigned('0' & DinB_i));
  41. when c_and => v_result := DinA_i and DinB_i;
  42. when c_or => v_result := DinA_i or DinB_i;
  43. when others => null;
  44. end case;
  45. Dout_o <= v_result(Width-1 downto 0);
  46. OverFlow_o <= v_result(Width);
  47. end if;
  48. end process;
  49. FormalG : if Formal generate
  50. signal s_dina : std_logic_vector(DinA_i'range);
  51. signal s_dinb : std_logic_vector(DinB_i'range);
  52. function max(a, b: std_logic_vector) return unsigned is
  53. begin
  54. if unsigned(a) > unsigned(b) then
  55. return unsigned(a);
  56. else
  57. return unsigned(b);
  58. end if;
  59. end function max;
  60. begin
  61. -- VHDL helper logic
  62. process is
  63. begin
  64. wait until rising_edge(Clk_i);
  65. s_dina <= DinA_i;
  66. s_dinb <= DinB_i;
  67. end process;
  68. default clock is rising_edge(Clk_i);
  69. AFTER_RESET : assert always
  70. not Reset_n_i -> Dout_o = (Dout_o'range => '0') and OverFlow_o = '0';
  71. ADD_OP : assert Reset_n_i and Opc_i = c_add ->
  72. next unsigned(Dout_o) = unsigned(s_dina) + unsigned(s_dinb) abort not Reset_n_i;
  73. SUB_OP : assert Reset_n_i and Opc_i = c_sub ->
  74. next unsigned(Dout_o) = unsigned(s_dina) - unsigned(s_dinb) abort not Reset_n_i;
  75. AND_OP : assert Reset_n_i and Opc_i = c_and ->
  76. next Dout_o = (s_dina and s_dinb) abort not Reset_n_i;
  77. OR_OP : assert Reset_n_i and Opc_i = c_or ->
  78. next Dout_o = (s_dina or s_dinb) abort not Reset_n_i;
  79. OVERFLOW_ADD : assert Reset_n_i and Opc_i = c_add and (unsigned(DinA_i) + unsigned(DinB_i)) < max(DinA_i, DinB_i) ->
  80. next OverFlow_o abort not Reset_n_i;
  81. NOT_OVERFLOW_ADD : assert Reset_n_i and Opc_i = c_add and (unsigned(DinA_i) + unsigned(DinB_i)) >= max(DinA_i, DinB_i) ->
  82. next not OverFlow_o abort not Reset_n_i;
  83. OVERFLOW_SUB : assert Reset_n_i and Opc_i = c_sub and (unsigned(DinA_i) - unsigned(DinB_i)) > unsigned(DinA_i) ->
  84. next OverFlow_o abort not Reset_n_i;
  85. NOT_OVERFLOW_SUB : assert Reset_n_i and Opc_i = c_sub and (unsigned(DinA_i) - unsigned(DinB_i)) <= unsigned(DinA_i) ->
  86. next not OverFlow_o abort not Reset_n_i;
  87. end generate FormalG;
  88. end architecture rtl;