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.

365 lines
11 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. begin
  140. -- VHDL helper logic
  141. process is
  142. begin
  143. wait until rising_edge(Clk_i);
  144. if (s_fsm_state = GET_HEADER) then
  145. if (DinValid_i = '1' and DinStart_i = '1') then
  146. s_cmd <= READ when Din_i(3 downto 0) = x"0" else
  147. WRITE when Din_i(3 downto 0) = x"1" else
  148. NOP;
  149. s_addr <= to_integer(unsigned(Din_i(7 downto 4)));
  150. end if;
  151. end if;
  152. end process;
  153. default clock is rising_edge(Clk_i);
  154. -- RESTRICTIONS
  155. -- Initial reset
  156. INITIAL_RESET : restrict {not Reset_n_i[*2]; Reset_n_i[+]}[*1];
  157. -- CONSTRAINTS
  158. -- Inputs are low during reset for simplicity
  159. ASSUME_INPUTS_DURING_RESET : assume always
  160. not Reset_n_i ->
  161. not DinValid_i and
  162. not DinStart_i and
  163. not DinStop_i and
  164. not DoutAccept_i;
  165. -- Valid stable until accepted
  166. JOB_REQ_VALID_STABLE : assume always
  167. DinValid_i and not DinAccept_o -> next (stable(DinValid_i) until_ DinAccept_o);
  168. -- Start stable until accepted
  169. JOB_REQ_START_STABLE : assume always
  170. DinValid_i and not DinAccept_o -> next (stable(DinStart_i) until_ DinAccept_o);
  171. -- Stop stable until accepted
  172. JOB_REQ_STOP_STABLE : assume always
  173. DinValid_i and not DinAccept_o -> next (stable(DinStop_i) until_ DinAccept_o);
  174. -- Data stable until accepted
  175. JOB_REQ_DIN_STABLE : assume always
  176. DinValid_i and not DinAccept_o -> next (stable(Din_i) until_ DinAccept_o);
  177. -- ASSERTIONS
  178. -- Asynchronous (unclocked) Reset asserts
  179. AFTER_RESET : process (all) is
  180. begin
  181. if (not Reset_n_i) then
  182. RESET_STATE : assert s_fsm_state = IDLE;
  183. RESET_ACCEPT : assert DinAccept_o = '0';
  184. RESET_START : assert DoutStart_o = '0';
  185. RESET_STOP : assert DoutStop_o = '0';
  186. RESET_VALID : assert DoutValid_o = '0';
  187. RESET_REG : assert s_register = (0 to 7 => x"00");
  188. end if;
  189. end process AFTER_RESET;
  190. -- FSM states in valid range
  191. FSM_STATES_VALID : assert always
  192. s_fsm_state = IDLE or s_fsm_state = GET_HEADER or
  193. s_fsm_state = GET_DATA or s_fsm_state = SET_DATA or
  194. s_fsm_state = SEND_HEADER or s_fsm_state = SEND_DATA or
  195. s_fsm_state = SEND_FOOTER;
  196. -- Discard jobs with invalid command
  197. INV_CMD_DISCARD : assert always
  198. s_fsm_state = GET_HEADER and DinValid_i = '1' and DinStart_i = '1' and
  199. Din_i(3 downto 0) /= x"0" and Din_i(3 downto 0) /= x"1"
  200. ->
  201. next s_fsm_state = IDLE;
  202. -- Discard read job with invalid stop flags
  203. READ_INV_FLAGS_DISCARD : assert always
  204. s_fsm_state = GET_HEADER and DinValid_i = '1' and DinStart_i = '1' and
  205. Din_i(3 downto 0) = x"0" and DinStop_i = '0'
  206. ->
  207. next s_fsm_state = IDLE;
  208. -- Discard write job with invalid stop flags
  209. WRITE_INV_FLAGS_DISCARD : assert always
  210. s_fsm_state = GET_HEADER and DinValid_i = '1' and DinStart_i = '1' and
  211. Din_i(3 downto 0) = x"1" and DinStop_i = '1'
  212. ->
  213. next s_fsm_state = IDLE;
  214. -- After a valid job read request,
  215. -- a job read acknowledge has to follow
  216. READ_VALID_ACK : assert always
  217. {s_fsm_state = GET_HEADER and DinValid_i = '1' and DinStart_i = '1' and
  218. Din_i(3 downto 0) = x"0" and DinStop_i = '1'}
  219. |=>
  220. {-- Job ack header cycle
  221. not DoutValid_o [*];
  222. DoutValid_o and DoutStart_o and not DoutAccept_i [*];
  223. DoutValid_o and DoutStart_o and DoutAccept_i;
  224. -- Job ack data cycle
  225. not DoutValid_o [*];
  226. DoutValid_o and not DoutStart_o and not DoutStop_o and not DoutAccept_i [*];
  227. DoutValid_o and not DoutStart_o and not DoutStop_o and DoutAccept_i;
  228. -- Job ack footer cycle
  229. not DoutValid_o [*];
  230. DoutValid_o and DoutStop_o};
  231. -- After a valid job write request,
  232. -- a job read acknowledge has to follow
  233. WRITE_VALID_ACK : assert always
  234. {s_fsm_state = GET_HEADER and DinValid_i = '1' and DinStart_i = '1' and
  235. Din_i(3 downto 0) = x"1" and DinStop_i = '0';
  236. not DinValid_i [*];
  237. DinValid_i and DinStop_i}
  238. |=>
  239. {-- Job ack header cycle
  240. not DoutValid_o [*];
  241. DoutValid_o and DoutStart_o and not DoutAccept_i [*];
  242. DoutValid_o and DoutStart_o and DoutAccept_i;
  243. -- Job ack footer cycle
  244. not DoutValid_o [*];
  245. DoutValid_o and DoutStop_o};
  246. -- Start & stop flag have to be exclusive
  247. JOB_ACK_NEVER_START_STOP : assert never
  248. DoutStart_o and DoutStop_o;
  249. -- Start & Stop have to be active together with valid
  250. JOB_ACK_START_STOP_VALID : assert always
  251. DoutStart_o or DoutStop_o -> DoutValid_o;
  252. -- Valid has to be stable until accepted
  253. JOB_ACK_VALID_STABLE : assert always
  254. DoutValid_o and not DoutAccept_i -> next (stable(DoutValid_o) until_ DoutAccept_i);
  255. -- Start has to be stable until accepted
  256. JOB_ACK_START_STABLE : assert always
  257. DoutValid_o and not DoutAccept_i -> next (stable(DoutStart_o) until_ DoutAccept_i);
  258. -- Stop has to be stable until accepted
  259. JOB_ACK_STOP_STABLE : assert always
  260. DoutValid_o and not DoutAccept_i -> next (stable(DoutStop_o) until_ DoutAccept_i);
  261. -- Data has to be stable until accepted
  262. JOB_ACK_DOUT_STABLE : assert always
  263. DoutValid_o and not DoutAccept_i -> next (stable(Dout_o) until_ DoutAccept_i);
  264. -- Data from selected address has to be read
  265. READ_DATA : assert always
  266. DoutValid_o and not DoutStart_o and not DoutStop_o and s_addr <= 7
  267. ->
  268. Dout_o = s_register(s_addr);
  269. -- 0 has to be read when invalid address is given
  270. READ_DATA_INV_ADDR : assert always
  271. DoutValid_o and not DoutStart_o and not DoutStop_o and s_addr > 7
  272. ->
  273. Dout_o = x"00";
  274. -- Register has to be written at given address with given data
  275. -- when correct job req write occurs
  276. WRITE_DATA : assert always
  277. -- Job req header cycle
  278. {s_fsm_state = GET_HEADER and DinValid_i = '1' and DinStart_i = '1' and
  279. Din_i(3 downto 0) = x"1" and unsigned(Din_i(7 downto 4)) <= 7 and DinStop_i = '0';
  280. -- Job req data / footer cycle
  281. not DinValid_i [*];
  282. DinValid_i and not DinStart_i and DinStop_i and not DinAccept_o [*];
  283. DinValid_i and not DinStart_i and DinStop_i and DinAccept_o}
  284. |=>
  285. {s_register(s_addr) = prev(Din_i)};
  286. -- FUNCTIONAL COVERAGE
  287. FOOTER_VALID : cover {DoutValid_o = '1' and DoutStop_o = '1' and Dout_o = 8x"0"};
  288. FOOTER_ERR : cover {DoutValid_o = '1' and DoutStop_o = '1' and Dout_o = 8x"1"};
  289. end generate FormalG;
  290. end architecture rtl;