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.

355 lines
10 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. -- Valid stable until accepted
  159. JOB_REQ_VALID_STABLE : assume always
  160. DinValid_i and not DinAccept_o -> next (stable(DinValid_i) until_ DinAccept_o);
  161. -- Start stable until accepted
  162. JOB_REQ_START_STABLE : assume always
  163. DinValid_i and not DinAccept_o -> next (stable(DinStart_i) until_ DinAccept_o);
  164. -- Stop stable until accepted
  165. JOB_REQ_STOP_STABLE : assume always
  166. DinValid_i and not DinAccept_o -> next (stable(DinStop_i) until_ DinAccept_o);
  167. -- Data stable until accepted
  168. JOB_REQ_DIN_STABLE : assume always
  169. DinValid_i and not DinAccept_o -> next (stable(Din_i) until_ DinAccept_o);
  170. -- ASSERTIONS
  171. -- Reset values
  172. AFTER_RESET : assert always
  173. not Reset_n_i
  174. ->
  175. s_fsm_state = IDLE and
  176. not DinAccept_o and
  177. not DoutStart_o and
  178. not DoutStop_o and
  179. not DoutValid_o and
  180. s_register = (0 to 7 => x"00");
  181. -- FSM states in valid range
  182. FSM_STATES_VALID : assert always
  183. s_fsm_state = IDLE or s_fsm_state = GET_HEADER or
  184. s_fsm_state = GET_DATA or s_fsm_state = SET_DATA or
  185. s_fsm_state = SEND_HEADER or s_fsm_state = SEND_DATA or
  186. s_fsm_state = SEND_FOOTER;
  187. -- Discard jobs with invalid command
  188. INV_CMD_DISCARD : assert always
  189. s_fsm_state = GET_HEADER and DinValid_i = '1' and DinStart_i = '1' and
  190. Din_i(3 downto 0) /= x"0" and Din_i(3 downto 0) /= x"1"
  191. ->
  192. next s_fsm_state = IDLE;
  193. -- Discard read job with invalid stop flags
  194. READ_INV_FLAGS_DISCARD : assert always
  195. s_fsm_state = GET_HEADER and DinValid_i = '1' and DinStart_i = '1' and
  196. Din_i(3 downto 0) = x"0" and DinStop_i = '0'
  197. ->
  198. next s_fsm_state = IDLE;
  199. -- Discard write job with invalid stop flags
  200. WRITE_INV_FLAGS_DISCARD : assert always
  201. s_fsm_state = GET_HEADER and DinValid_i = '1' and DinStart_i = '1' and
  202. Din_i(3 downto 0) = x"1" and DinStop_i = '1'
  203. ->
  204. next s_fsm_state = IDLE;
  205. -- After a valid job read request,
  206. -- a job read acknowledge has to follow
  207. READ_VALID_ACK : assert always
  208. {s_fsm_state = GET_HEADER and DinValid_i = '1' and DinStart_i = '1' and
  209. Din_i(3 downto 0) = x"0" and DinStop_i = '1'}
  210. |=>
  211. {-- Job ack header cycle
  212. not DoutValid_o [*];
  213. DoutValid_o and DoutStart_o and not DoutAccept_i [*];
  214. DoutValid_o and DoutStart_o and DoutAccept_i;
  215. -- Job ack data cycle
  216. not DoutValid_o [*];
  217. DoutValid_o and not DoutStart_o and not DoutStop_o and not DoutAccept_i [*];
  218. DoutValid_o and not DoutStart_o and not DoutStop_o and DoutAccept_i;
  219. -- Job ack footer cycle
  220. not DoutValid_o [*];
  221. DoutValid_o and DoutStop_o};
  222. -- After a valid job write request,
  223. -- a job read acknowledge has to follow
  224. WRITE_VALID_ACK : assert always
  225. {s_fsm_state = GET_HEADER and DinValid_i = '1' and DinStart_i = '1' and
  226. Din_i(3 downto 0) = x"1" and DinStop_i = '0';
  227. not DinValid_i [*];
  228. DinValid_i and DinStop_i}
  229. |=>
  230. {-- Job ack header cycle
  231. not DoutValid_o [*];
  232. DoutValid_o and DoutStart_o and not DoutAccept_i [*];
  233. DoutValid_o and DoutStart_o and DoutAccept_i;
  234. -- Job ack footer cycle
  235. not DoutValid_o [*];
  236. DoutValid_o and DoutStop_o};
  237. -- Start & stop flag have to be exclusive
  238. JOB_ACK_NEVER_START_STOP : assert never
  239. DoutStart_o and DoutStop_o;
  240. -- Start & Stop have to be active together with valid
  241. JOB_ACK_START_STOP_VALID : assert always
  242. DoutStart_o or DoutStop_o -> DoutValid_o;
  243. -- Valid has to be stable until accepted
  244. JOB_ACK_VALID_STABLE : assert always
  245. DoutValid_o and not DoutAccept_i -> next (stable(DoutValid_o) until_ DoutAccept_i);
  246. -- Start has to be stable until accepted
  247. JOB_ACK_START_STABLE : assert always
  248. DoutValid_o and not DoutAccept_i -> next (stable(DoutStart_o) until_ DoutAccept_i);
  249. -- Stop has to be stable until accepted
  250. JOB_ACK_STOP_STABLE : assert always
  251. DoutValid_o and not DoutAccept_i -> next (stable(DoutStop_o) until_ DoutAccept_i);
  252. -- Data has to be stable until accepted
  253. JOB_ACK_DOUT_STABLE : assert always
  254. DoutValid_o and not DoutAccept_i -> next (stable(Dout_o) until_ DoutAccept_i);
  255. -- Data from selected address has to be read
  256. READ_DATA : assert always
  257. DoutValid_o and not DoutStart_o and not DoutStop_o and s_addr <= 7
  258. ->
  259. Dout_o = s_register(s_addr);
  260. -- 0 has to be read when invalid address is given
  261. READ_DATA_INV_ADDR : assert always
  262. DoutValid_o and not DoutStart_o and not DoutStop_o and s_addr > 7
  263. ->
  264. Dout_o = x"00";
  265. -- Register has to be written at given address with given data
  266. -- when correct job req write occurs
  267. WRITE_DATA : assert always
  268. -- Job req header cycle
  269. {s_fsm_state = GET_HEADER and DinValid_i = '1' and DinStart_i = '1' and
  270. Din_i(3 downto 0) = x"1" and unsigned(Din_i(7 downto 4)) <= 7 and DinStop_i = '0';
  271. -- Job req data / footer cycle
  272. not DinValid_i [*];
  273. DinValid_i and not DinStart_i and DinStop_i and not DinAccept_o [*];
  274. DinValid_i and not DinStart_i and DinStop_i and DinAccept_o}
  275. |=>
  276. {s_register(s_addr) = prev(Din_i)};
  277. -- FUNCTIONAL COVERAGE
  278. FOOTER_VALID : cover {DoutValid_o = '1' and DoutStop_o = '1' and Dout_o = 8x"0"};
  279. FOOTER_ERR : cover {DoutValid_o = '1' and DoutStop_o = '1' and Dout_o = 8x"1"};
  280. end generate FormalG;
  281. end architecture rtl;