Lösung des Praktikums Systementwurf
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.

test_data_channel.vhd 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. library ieee;
  2. use ieee.std_logic_1164.all;
  3. use ieee.numeric_std.all;
  4. use ieee.float_pkg.all;
  5. library work;
  6. use work.avalon_slave.all;
  7. use work.test_utility.all;
  8. library std;
  9. use std.textio.all;
  10. package test_data_channel_pkg is
  11. procedure is_empty( signal clk : in std_logic;
  12. signal req : out work.avalon_slave.Request;
  13. signal rsp : in work.avalon_slave.Response;
  14. variable res : out boolean );
  15. procedure assert_empty( signal clk : in std_logic;
  16. signal req : out work.avalon_slave.Request;
  17. signal rsp : in work.avalon_slave.Response );
  18. procedure assert_not_empty( signal clk : in std_logic;
  19. signal req : out work.avalon_slave.Request;
  20. signal rsp : in work.avalon_slave.Response );
  21. procedure assert_full( signal clk : in std_logic;
  22. signal req : out work.avalon_slave.Request;
  23. signal rsp : in work.avalon_slave.Response );
  24. procedure assert_not_full( signal clk : in std_logic;
  25. signal req : out work.avalon_slave.Request;
  26. signal rsp : in work.avalon_slave.Response );
  27. procedure assert_level( signal clk : in std_logic;
  28. signal req : out work.avalon_slave.Request;
  29. signal rsp : in work.avalon_slave.Response;
  30. variable level : in std_logic_vector );
  31. procedure assert_config( signal clk : in std_logic;
  32. signal req : out work.avalon_slave.Request;
  33. signal rsp : in work.avalon_slave.Response;
  34. variable config : in std_logic_vector );
  35. procedure write_config( signal clk : in std_logic;
  36. signal req : out work.avalon_slave.Request;
  37. variable config : in std_logic_vector );
  38. procedure write_and_assert_config( signal clk : in std_logic;
  39. signal req : out work.avalon_slave.Request;
  40. signal rsp : in work.avalon_slave.Response;
  41. variable config : in std_logic_vector );
  42. procedure write_clear( signal clk : in std_logic;
  43. signal req : out work.avalon_slave.Request );
  44. procedure write_sw_sink( signal clk : in std_logic;
  45. signal req : out work.avalon_slave.Request;
  46. variable data : in std_logic_vector );
  47. procedure read_sw_source( signal clk : in std_logic;
  48. signal req : out work.avalon_slave.Request;
  49. signal rsp : in work.avalon_slave.Response;
  50. variable data : out std_logic_vector );
  51. procedure assert_read_sw_source_eq( signal clk : in std_logic;
  52. signal req : out work.avalon_slave.Request;
  53. signal rsp : in work.avalon_slave.Response;
  54. variable expected : in std_logic_vector );
  55. procedure write_hw_sink( signal clk : in std_logic;
  56. signal write : out std_logic;
  57. signal writedata : out std_logic_vector;
  58. variable data : in std_logic_vector );
  59. procedure assert_read_hw_source_eq( signal clk : in std_logic;
  60. signal read : out std_logic;
  61. signal readdata : in std_logic_vector;
  62. variable expected : in std_logic_vector );
  63. procedure write_content( signal clk : in std_logic;
  64. signal req : out work.avalon_slave.Request;
  65. signal rsp : in work.avalon_slave.Response );
  66. procedure check_and_write_content( signal clk : in std_logic;
  67. signal req : out work.avalon_slave.Request;
  68. signal rsp : in work.avalon_slave.Response;
  69. constant expected : real_array );
  70. end package test_data_channel_pkg;
  71. package body test_data_channel_pkg is
  72. procedure is_empty( signal clk : in std_logic;
  73. signal req : out work.avalon_slave.Request;
  74. signal rsp : in work.avalon_slave.Response;
  75. variable res : out boolean ) is
  76. variable address : std_logic_vector( 3 downto 0 );
  77. variable data : std_logic_vector( 31 downto 0 );
  78. constant EMPTY : std_logic_vector( 31 downto 0 ) := x"00000001";
  79. begin
  80. wait until falling_edge( clk );
  81. address := std_logic_vector( to_unsigned( 1, address'length ) );
  82. work.test_avalon_slave.read( clk => clk,
  83. address => address,
  84. req => req,
  85. rsp => rsp,
  86. data => data );
  87. res := data = EMPTY;
  88. end procedure is_empty;
  89. procedure assert_empty( signal clk : in std_logic;
  90. signal req : out work.avalon_slave.Request;
  91. signal rsp : in work.avalon_slave.Response ) is
  92. variable address : std_logic_vector( 3 downto 0 );
  93. variable expected_readdata : std_logic_vector( 31 downto 0 ) := x"00000001";
  94. begin
  95. wait until falling_edge( clk );
  96. address := std_logic_vector( to_unsigned( 1, address'length ) );
  97. expected_readdata := x"00000001";
  98. work.test_avalon_slave.assert_readdata_eq( clk => clk,
  99. address => address,
  100. req => req,
  101. rsp => rsp,
  102. expected => expected_readdata,
  103. message => TEST_FAIL & " assert_empty" );
  104. end procedure assert_empty;
  105. procedure assert_not_empty( signal clk : in std_logic;
  106. signal req : out work.avalon_slave.Request;
  107. signal rsp : in work.avalon_slave.Response ) is
  108. variable address : std_logic_vector( 3 downto 0 );
  109. variable expected_readdata : std_logic_vector( 31 downto 0 ) := x"00000001";
  110. begin
  111. wait until falling_edge( clk );
  112. address := std_logic_vector( to_unsigned( 1, address'length ) );
  113. expected_readdata := x"00000000";
  114. work.test_avalon_slave.assert_readdata_eq( clk => clk,
  115. address => address,
  116. req => req,
  117. rsp => rsp,
  118. expected => expected_readdata,
  119. message => TEST_FAIL & " assert_not_empty" );
  120. end procedure assert_not_empty;
  121. procedure assert_full( signal clk : in std_logic;
  122. signal req : out work.avalon_slave.Request;
  123. signal rsp : in work.avalon_slave.Response ) is
  124. variable address : std_logic_vector( 3 downto 0 );
  125. variable expected_readdata : std_logic_vector( 31 downto 0 ) := x"00000001";
  126. begin
  127. wait until falling_edge( clk );
  128. address := std_logic_vector( to_unsigned( 2, address'length ) );
  129. expected_readdata := x"00000001";
  130. work.test_avalon_slave.assert_readdata_eq( clk => clk,
  131. address => address,
  132. req => req,
  133. rsp => rsp,
  134. expected => expected_readdata,
  135. message => TEST_FAIL & " assert_full" );
  136. end procedure assert_full;
  137. procedure assert_not_full( signal clk : in std_logic;
  138. signal req : out work.avalon_slave.Request;
  139. signal rsp : in work.avalon_slave.Response ) is
  140. variable address : std_logic_vector( 3 downto 0 );
  141. variable expected_readdata : std_logic_vector( 31 downto 0 ) := x"00000001";
  142. begin
  143. wait until falling_edge( clk );
  144. address := std_logic_vector( to_unsigned( 2, address'length ) );
  145. expected_readdata := x"00000000";
  146. work.test_avalon_slave.assert_readdata_eq( clk => clk,
  147. address => address,
  148. req => req,
  149. rsp => rsp,
  150. expected => expected_readdata,
  151. message => TEST_FAIL & " assert_not_full" );
  152. end procedure assert_not_full;
  153. procedure assert_level( signal clk : in std_logic;
  154. signal req : out work.avalon_slave.Request;
  155. signal rsp : in work.avalon_slave.Response;
  156. variable level : in std_logic_vector ) is
  157. variable address : std_logic_vector( 3 downto 0 );
  158. begin
  159. wait until falling_edge( clk );
  160. address := std_logic_vector( to_unsigned( 3, address'length ) );
  161. work.test_avalon_slave.assert_readdata_eq( clk => clk,
  162. address => address,
  163. req => req,
  164. rsp => rsp,
  165. expected => level,
  166. message => TEST_FAIL & " assert_level" );
  167. end procedure assert_level;
  168. procedure assert_config( signal clk : in std_logic;
  169. signal req : out work.avalon_slave.Request;
  170. signal rsp : in work.avalon_slave.Response;
  171. variable config : in std_logic_vector ) is
  172. variable address : std_logic_vector( 3 downto 0 );
  173. begin
  174. wait until falling_edge( clk );
  175. address := std_logic_vector( to_unsigned( 0, address'length ) );
  176. work.test_avalon_slave.assert_readdata_eq( clk => clk,
  177. address => address,
  178. req => req,
  179. rsp => rsp,
  180. expected => config,
  181. message => TEST_FAIL & " assert_config" );
  182. end procedure assert_config;
  183. procedure write_config( signal clk : in std_logic;
  184. signal req : out work.avalon_slave.Request;
  185. variable config : in std_logic_vector ) is
  186. variable address : std_logic_vector( 3 downto 0 );
  187. begin
  188. wait until falling_edge( clk );
  189. address := std_logic_vector( to_unsigned( 0, address'length ) );
  190. work.test_avalon_slave.write( clk => clk,
  191. address => address,
  192. req => req,
  193. data => config );
  194. end procedure write_config;
  195. procedure write_and_assert_config( signal clk : in std_logic;
  196. signal req : out work.avalon_slave.Request;
  197. signal rsp : in work.avalon_slave.Response;
  198. variable config : in std_logic_vector ) is
  199. variable address : std_logic_vector( 3 downto 0 );
  200. begin
  201. write_config( clk => clk, req => req, config => config );
  202. assert_config( clk => clk, req => req, rsp => rsp, config => config );
  203. end procedure write_and_assert_config;
  204. procedure write_clear( signal clk : in std_logic;
  205. signal req : out work.avalon_slave.Request ) is
  206. variable address : std_logic_vector( 3 downto 0 );
  207. variable clear : std_logic_vector( 31 downto 0 ) := x"00000001";
  208. begin
  209. wait until falling_edge( clk );
  210. address := std_logic_vector( to_unsigned( 6, address'length ) );
  211. work.test_avalon_slave.write( clk => clk,
  212. address => address,
  213. req => req,
  214. data => clear );
  215. end procedure write_clear;
  216. procedure write_sw_sink( signal clk : in std_logic;
  217. signal req : out work.avalon_slave.Request;
  218. variable data : in std_logic_vector ) is
  219. variable address : std_logic_vector( 3 downto 0 );
  220. begin
  221. wait until falling_edge( clk );
  222. address := std_logic_vector( to_unsigned( 4, address'length ) );
  223. work.test_avalon_slave.write( clk => clk,
  224. address => address,
  225. req => req,
  226. data => data );
  227. end procedure write_sw_sink;
  228. procedure read_sw_source( signal clk : in std_logic;
  229. signal req : out work.avalon_slave.Request;
  230. signal rsp : in work.avalon_slave.Response;
  231. variable data : out std_logic_vector ) is
  232. variable address : std_logic_vector( 3 downto 0 );
  233. begin
  234. wait until falling_edge( clk );
  235. address := std_logic_vector( to_unsigned( 5, address'length ) );
  236. work.test_avalon_slave.read( clk => clk,
  237. address => address,
  238. req => req,
  239. rsp => rsp,
  240. data => data );
  241. end procedure read_sw_source;
  242. procedure assert_read_sw_source_eq( signal clk : in std_logic;
  243. signal req : out work.avalon_slave.Request;
  244. signal rsp : in work.avalon_slave.Response;
  245. variable expected : in std_logic_vector ) is
  246. variable address : std_logic_vector( 3 downto 0 );
  247. begin
  248. wait until falling_edge( clk );
  249. address := std_logic_vector( to_unsigned( 5, address'length ) );
  250. work.test_avalon_slave.assert_readdata_eq( clk => clk,
  251. address => address,
  252. req => req,
  253. rsp => rsp,
  254. expected => expected,
  255. message => TEST_FAIL & " assert_readdata_eq" );
  256. end procedure assert_read_sw_source_eq;
  257. procedure write_hw_sink( signal clk : in std_logic;
  258. signal write : out std_logic;
  259. signal writedata : out std_logic_vector;
  260. variable data : in std_logic_vector ) is
  261. begin
  262. wait until falling_edge( clk );
  263. write <= '1';
  264. writedata <= data;
  265. wait until falling_edge( clk );
  266. write <= '0';
  267. end procedure write_hw_sink;
  268. procedure assert_read_hw_source_eq( signal clk : in std_logic;
  269. signal read : out std_logic;
  270. signal readdata : in std_logic_vector;
  271. variable expected : in std_logic_vector ) is
  272. begin
  273. wait until falling_edge( clk );
  274. assert( readdata = expected )
  275. report TEST_FAIL & " assert_read_hw_source_eq" & LF &
  276. " expected: " & to_string( expected ) & LF &
  277. " actual: " & to_string( readdata ) & LF
  278. severity error;
  279. wait until falling_edge( clk );
  280. read <= '1';
  281. wait until falling_edge( clk );
  282. read <= '0';
  283. end procedure assert_read_hw_source_eq;
  284. procedure write_content( signal clk : in std_logic;
  285. signal req : out work.avalon_slave.Request;
  286. signal rsp : in work.avalon_slave.Response ) is
  287. variable index : integer := 0;
  288. variable empty : boolean;
  289. variable value : std_logic_vector( 31 downto 0 );
  290. variable float_value : float32;
  291. variable real_value : real;
  292. file data_file : text;
  293. begin
  294. std.textio.write( std.textio.OUTPUT, " write_content ... " );
  295. file_open( data_file, "data.py", write_mode );
  296. std.textio.write( data_file, "float_data = [" );
  297. while true loop
  298. is_empty( clk => clk, req => req, rsp => rsp, res => empty );
  299. if ( empty ) then
  300. exit;
  301. end if;
  302. read_sw_source( clk => clk, req => req, rsp => rsp,
  303. data => value );
  304. float_value := to_float( value );
  305. real_value := to_real( float_value );
  306. std.textio.write( data_file, to_string( real_value ) & "," );
  307. index := index + 1;
  308. end loop;
  309. while index < 1024 loop
  310. std.textio.write( data_file, "0.0 ," );
  311. index := index + 1;
  312. end loop;
  313. std.textio.write( data_file, "]" & LF );
  314. file_close( data_file );
  315. std.textio.write( std.textio.OUTPUT, TEST_OK );
  316. end procedure write_content;
  317. procedure check_and_write_content( signal clk : in std_logic;
  318. signal req : out work.avalon_slave.Request;
  319. signal rsp : in work.avalon_slave.Response;
  320. constant expected : real_array ) is
  321. variable expected_readdata : std_logic_vector( 31 downto 0 );
  322. variable index : integer := 0;
  323. variable empty : boolean;
  324. variable value : std_logic_vector( 31 downto 0 );
  325. variable float_value : float32;
  326. variable real_value : real;
  327. variable expected_value : real;
  328. variable abs_err : real := 0.5e-1;
  329. file data_file : text;
  330. begin
  331. std.textio.write( std.textio.OUTPUT, " check_and_write_content ... " );
  332. assert_full( clk => clk, req => req, rsp => rsp );
  333. expected_readdata := std_logic_vector( to_unsigned( 0, expected_readdata'length ) );
  334. assert_level( clk => clk, req => req, rsp => rsp,
  335. level => expected_readdata );
  336. file_open( data_file, "data.py", write_mode );
  337. std.textio.write( data_file, "float_data = [" );
  338. while true loop
  339. is_empty( clk => clk, req => req, rsp => rsp, res => empty );
  340. if ( empty ) then
  341. exit;
  342. end if;
  343. read_sw_source( clk => clk, req => req, rsp => rsp,
  344. data => value );
  345. float_value := to_float( value );
  346. real_value := to_real( float_value );
  347. std.textio.write( data_file, to_string( real_value ) & "," );
  348. expected_value := expected( index );
  349. assert_element_near( real_value, expected_value, abs_err, index );
  350. index := index + 1;
  351. end loop;
  352. std.textio.write( data_file, "]" & LF );
  353. file_close( data_file );
  354. std.textio.write( std.textio.OUTPUT, TEST_OK );
  355. end procedure check_and_write_content;
  356. end package body test_data_channel_pkg;