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.

295 lines
8.9 KiB

  1. library ieee;
  2. use ieee.std_logic_1164.all;
  3. use ieee.numeric_std.all;
  4. entity vai_reg is
  5. generic (
  6. Formal : boolean := true
  7. );
  8. port (
  9. Reset_n_i : in std_logic;
  10. Clk_i : in std_logic;
  11. -- req
  12. Din_i : in std_logic_vector(7 downto 0);
  13. DinValid_i : in std_logic;
  14. DinStart_i : in std_logic;
  15. DinStop_i : in std_logic;
  16. DinAccept_o : out std_logic;
  17. -- ack
  18. Dout_o : out std_logic_vector(7 downto 0);
  19. DoutValid_o : out std_logic;
  20. DoutStart_o : out std_logic;
  21. DoutStop_o : out std_logic;
  22. DoutAccept_i : in std_logic
  23. );
  24. end entity vai_reg;
  25. architecture rtl of vai_reg is
  26. constant C_READ : std_logic_vector(3 downto 0) := x"0";
  27. constant C_WRITE : std_logic_vector(3 downto 0) := x"1";
  28. type t_fsm_state is (IDLE, GET_HEADER, GET_DATA,
  29. SET_DATA, SEND_HEADER, SEND_DATA, SEND_FOOTER);
  30. signal s_fsm_state : t_fsm_state;
  31. type t_register is array(0 to 7) of std_logic_vector(7 downto 0);
  32. signal s_register : t_register;
  33. signal s_header : std_logic_vector(7 downto 0);
  34. signal s_data : std_logic_vector(7 downto 0);
  35. signal s_error : boolean;
  36. signal s_dout_accepted : boolean;
  37. alias a_addr : std_logic_vector(3 downto 0) is s_header(7 downto 4);
  38. begin
  39. s_dout_accepted <= (DoutValid_o and DoutAccept_i) = '1';
  40. process (Reset_n_i, Clk_i) is
  41. begin
  42. if (Reset_n_i = '0') then
  43. DinAccept_o <= '0';
  44. DoutStart_o <= '0';
  45. DoutStop_o <= '0';
  46. DoutValid_o <= '0';
  47. Dout_o <= (others => '0');
  48. s_header <= (others => '0');
  49. s_data <= (others => '0');
  50. s_register <= (others => (others => '0'));
  51. s_error <= false;
  52. s_fsm_state <= IDLE;
  53. elsif (rising_edge(Clk_i)) then
  54. case s_fsm_state is
  55. when IDLE =>
  56. DinAccept_o <= '0';
  57. DoutStart_o <= '0';
  58. DoutStop_o <= '0';
  59. DoutValid_o <= '0';
  60. Dout_o <= (others => '0');
  61. s_header <= (others => '0');
  62. s_data <= (others => '0');
  63. s_error <= false;
  64. DinAccept_o <= '1';
  65. s_fsm_state <= GET_HEADER;
  66. when GET_HEADER =>
  67. if (DinValid_i = '1' and DinStart_i = '1') then
  68. s_header <= Din_i;
  69. if (Din_i(3 downto 0) = C_READ and DinStop_i = '1') then
  70. DinAccept_o <= '0';
  71. s_fsm_state <= GET_DATA;
  72. elsif (Din_i(3 downto 0) = C_WRITE and DinStop_i = '0') then
  73. s_fsm_state <= SET_DATA;
  74. else
  75. DinAccept_o <= '0';
  76. s_fsm_state <= IDLE;
  77. end if;
  78. end if;
  79. when GET_DATA =>
  80. if (unsigned(a_addr) <= 7) then
  81. -- s_data <= s_register(to_integer(unsigned(a_addr)));
  82. else
  83. s_error <= true;
  84. s_data <= (others => '0');
  85. end if;
  86. s_fsm_state <= SEND_HEADER;
  87. when SET_DATA =>
  88. if (DinValid_i = '1') then
  89. DinAccept_o <= '0';
  90. if (DinStop_i = '1') then
  91. if (unsigned(a_addr) <= 7) then
  92. -- s_register(to_integer(unsigned(a_addr))) <= Din_i;
  93. else
  94. s_error <= true;
  95. end if;
  96. s_fsm_state <= SEND_HEADER;
  97. else
  98. s_fsm_state <= IDLE;
  99. end if;
  100. end if;
  101. when SEND_HEADER =>
  102. DoutValid_o <= '1';
  103. DoutStart_o <= '1';
  104. Dout_o <= s_header;
  105. if (s_dout_accepted) then
  106. DoutValid_o <= '0';
  107. DoutStart_o <= '0';
  108. if (s_header(3 downto 0) = C_WRITE) then
  109. s_fsm_state <= SEND_FOOTER;
  110. else
  111. s_fsm_state <= SEND_DATA;
  112. end if;
  113. end if;
  114. when SEND_DATA =>
  115. DoutValid_o <= '1';
  116. Dout_o <= s_data;
  117. if (s_dout_accepted) then
  118. DoutValid_o <= '0';
  119. s_fsm_state <= SEND_FOOTER;
  120. end if;
  121. when SEND_FOOTER =>
  122. DoutValid_o <= '1';
  123. DoutStop_o <= '1';
  124. Dout_o <= x"01" when s_error else x"00";
  125. if (s_dout_accepted) then
  126. Dout_o <= (others => '0');
  127. DoutValid_o <= '0';
  128. DoutStop_o <= '0';
  129. s_fsm_state <= IDLE;
  130. end if;
  131. when others => null;
  132. end case;
  133. end if;
  134. end process;
  135. FormalG : if Formal generate
  136. signal s_addr : natural range 0 to 15;
  137. type t_cmd is (READ, WRITE, NOP);
  138. signal s_cmd : t_cmd;
  139. signal s_start : std_logic;
  140. signal s_stop : std_logic;
  141. signal s_dout : std_logic_vector(7 downto 0);
  142. begin
  143. -- VHDL helper logic
  144. process is
  145. begin
  146. wait until rising_edge(Clk_i);
  147. s_start <= DoutStart_o;
  148. s_stop <= DoutStop_o;
  149. s_dout <= Dout_o;
  150. if (s_fsm_state = GET_HEADER) then
  151. if (DinValid_i = '1' and DinStart_i = '1') then
  152. s_cmd <= READ when Din_i(3 downto 0) = x"0" else
  153. WRITE when Din_i(3 downto 0) = x"1" else
  154. NOP;
  155. s_addr <= to_integer(unsigned(Din_i(7 downto 4)));
  156. end if;
  157. end if;
  158. end process;
  159. default clock is rising_edge(Clk_i);
  160. INITIAL_RESET : restrict {Reset_n_i = '0'[*2]; Reset_n_i = '1'[+]}[*1];
  161. -- FSM states in valid range
  162. FSM_STATES_VALID : assert always
  163. s_fsm_state = IDLE or s_fsm_state = GET_HEADER or
  164. s_fsm_state = GET_DATA or s_fsm_state = SET_DATA or
  165. s_fsm_state = SEND_HEADER or s_fsm_state = SEND_DATA or
  166. s_fsm_state = SEND_FOOTER;
  167. -- Discard jobs with invalid command
  168. INV_CMD_DISCARD : assert always
  169. s_fsm_state = GET_HEADER and DinValid_i = '1' and DinStart_i = '1' and
  170. Din_i(3 downto 0) /= x"0" and Din_i(3 downto 0) /= x"1"
  171. ->
  172. next s_fsm_state = IDLE;
  173. -- Discard read job with invalid stop flags
  174. READ_INV_FLAGS_DISCARD : assert always
  175. s_fsm_state = GET_HEADER and DinValid_i = '1' and DinStart_i = '1' and
  176. Din_i(3 downto 0) = x"0" and DinStop_i = '0'
  177. ->
  178. next s_fsm_state = IDLE;
  179. -- Discard write job with invalid stop flags
  180. WRITE_INV_FLAGS_DISCARD : assert always
  181. s_fsm_state = GET_HEADER and DinValid_i = '1' and DinStart_i = '1' and
  182. Din_i(3 downto 0) = x"1" and DinStop_i = '1'
  183. ->
  184. next s_fsm_state = IDLE;
  185. -- After a valid job read request,
  186. -- a job read acknowledge has to follow
  187. READ_VALID_ACK : assert always
  188. {s_fsm_state = GET_HEADER and DinValid_i = '1' and DinStart_i = '1' and
  189. Din_i(3 downto 0) = x"0" and DinStop_i = '1'}
  190. |=>
  191. {-- Job ack header cycle
  192. (not DoutValid_o)[*];
  193. (DoutValid_o and DoutStart_o and not DoutAccept_i)[*];
  194. (DoutValid_o and DoutStart_o and DoutAccept_i);
  195. -- Job ack data cycle
  196. (not DoutValid_o)[*];
  197. (DoutValid_o and not DoutStart_o and not DoutStop_o and not DoutAccept_i)[*];
  198. (DoutValid_o and not DoutStart_o and not DoutStop_o and DoutAccept_i);
  199. -- Job ack footer cycle
  200. (not DoutValid_o)[*];
  201. DoutValid_o and DoutStop_o};
  202. -- After a valid job write request,
  203. -- a job read acknowledge has to follow
  204. WRITE_VALID_ACK : assert always
  205. {s_fsm_state = GET_HEADER and DinValid_i = '1' and DinStart_i = '1' and
  206. Din_i(3 downto 0) = x"1" and DinStop_i = '0';
  207. (not DinValid_i)[*];
  208. DinValid_i and DinStop_i}
  209. |=>
  210. {-- Job ack header cycle
  211. (not DoutValid_o)[*];
  212. (DoutValid_o and DoutStart_o and not DoutAccept_i)[*];
  213. (DoutValid_o and DoutStart_o and DoutAccept_i);
  214. -- Job ack footer cycle
  215. (not DoutValid_o)[*];
  216. DoutValid_o and DoutStop_o};
  217. -- Start & stop flag have to be exclusive
  218. NEVER_START_STOP : assert never
  219. DoutStart_o and DoutStop_o;
  220. -- Start & Stop have to be active together with valid
  221. START_STOP_VALID : assert always
  222. DoutStart_o or DoutStop_o -> DoutValid_o;
  223. -- Valid has to be stable until accepted
  224. VALID_STABLE : assert always
  225. DoutValid_o and not DoutAccept_i -> next (DoutValid_o until_ DoutAccept_i);
  226. -- Start has to be stable until accepted
  227. START_STABLE : assert always
  228. DoutValid_o and not DoutAccept_i -> next (DoutStart_o = s_start until_ DoutAccept_i);
  229. -- Stop has to be stable until accepted
  230. STOP_STABLE : assert always
  231. DoutValid_o and not DoutAccept_i -> next (DoutStop_o = s_stop until_ DoutAccept_i);
  232. -- Data has to be stable until accepted
  233. DATA_STABLE : assert always
  234. DoutValid_o and not DoutAccept_i -> next (Dout_o = s_dout until_ DoutAccept_i);
  235. -- READ_DATA : assert always
  236. -- (DoutValid_o and not DoutStart_o and not DoutStop_o) ->
  237. -- (Dout_o = s_register(to_integer(unsigned(a_addr))));
  238. FOOTER_VALID : cover {DoutValid_o = '1' and DoutStop_o = '1' and Dout_o = 8x"0"};
  239. FOOTER_ERR : cover {DoutValid_o = '1' and DoutStop_o = '1' and Dout_o = 8x"1"};
  240. end generate FormalG;
  241. end architecture rtl;