Library of reusable VHDL components
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.

147 lines
4.3 KiB

  1. library ieee;
  2. use ieee.std_logic_1164.all;
  3. use ieee.numeric_std.all;
  4. --+ including vhdl 2008 libraries
  5. --+ These lines can be commented out when using
  6. --+ a simulator with built-in VHDL 2008 support
  7. --library ieee_proposed;
  8. -- use ieee_proposed.standard_additions.all;
  9. -- use ieee_proposed.std_logic_1164_additions.all;
  10. -- use ieee_proposed.numeric_std_additions.all;
  11. library osvvm;
  12. use osvvm.RandomPkg.all;
  13. library libvhdl;
  14. use libvhdl.AssertP.all;
  15. use libvhdl.SimP.all;
  16. use libvhdl.QueueP.all;
  17. entity SimT is
  18. end entity SimT;
  19. architecture sim of SimT is
  20. --* testbench global clock period
  21. constant C_PERIOD : time := 5 ns;
  22. --* SPI data transfer data width
  23. constant C_DATA_WIDTH : natural := 8;
  24. signal s_tests_done : boolean_vector(0 to 1) := (others => false);
  25. signal s_clk : std_logic := '0';
  26. signal s_sclk : std_logic;
  27. signal s_ste : std_logic;
  28. signal s_mosi : std_logic;
  29. signal s_miso : std_logic;
  30. shared variable sv_mosi_queue : t_list_queue;
  31. shared variable sv_miso_queue : t_list_queue;
  32. begin
  33. s_clk <= not(s_clk) after C_PERIOD when not(and_reduce(s_tests_done)) else '0';
  34. QueueInitP : process is
  35. begin
  36. sv_mosi_queue.init(32);
  37. sv_miso_queue.init(32);
  38. wait;
  39. end process QueueInitP;
  40. SimTestP : process is
  41. variable v_time : time;
  42. begin
  43. wait until s_clk = '1';
  44. v_time := now;
  45. wait_cycles(s_clk, 10);
  46. assert (now - v_time) = C_PERIOD * 20
  47. severity failure;
  48. s_tests_done(0) <= true;
  49. report "INFO: wait_cycles() procedure tests finished successfully";
  50. wait;
  51. end process SimTestP;
  52. -- Unit test of spi master procedure, checks all combinations
  53. -- of cpol & cpha against spi slave procedure
  54. SpiMasterP : process is
  55. variable v_send_data : std_logic_vector(C_DATA_WIDTH-1 downto 0) := (others => '0');
  56. variable v_receive_data : std_logic_vector(C_DATA_WIDTH-1 downto 0) := (others => '0');
  57. variable v_queue_data : std_logic_vector(C_DATA_WIDTH-1 downto 0) := (others => '0');
  58. variable v_random : RandomPType;
  59. begin
  60. v_random.InitSeed(v_random'instance_name);
  61. for direction in 0 to 1 loop
  62. for mode in 0 to 3 loop
  63. for i in 0 to 255 loop
  64. v_send_data := v_random.RandSlv(C_DATA_WIDTH);
  65. sv_mosi_queue.push(v_send_data);
  66. spi_master (data_in => v_send_data,
  67. data_out => v_receive_data,
  68. sclk => s_sclk,
  69. ste => s_ste,
  70. mosi => s_mosi,
  71. miso => s_miso,
  72. dir => direction,
  73. cpol => mode / 2,
  74. cpha => mode mod 2,
  75. period => 1 us
  76. );
  77. sv_miso_queue.pop(v_queue_data);
  78. assert_equal(v_receive_data, v_queue_data);
  79. end loop;
  80. end loop;
  81. end loop;
  82. wait;
  83. end process SpiMasterP;
  84. -- Unit test of spi slave procedure, checks all combinations
  85. -- of cpol & cpha against spi master procedure
  86. SpiSlaveP : process is
  87. variable v_send_data : std_logic_vector(C_DATA_WIDTH-1 downto 0) := (others => '0');
  88. variable v_receive_data : std_logic_vector(C_DATA_WIDTH-1 downto 0) := (others => '0');
  89. variable v_queue_data : std_logic_vector(C_DATA_WIDTH-1 downto 0) := (others => '0');
  90. variable v_random : RandomPType;
  91. begin
  92. v_random.InitSeed(v_random'instance_name);
  93. for direction in 0 to 1 loop
  94. for mode in 0 to 3 loop
  95. for i in 0 to 255 loop
  96. v_send_data := v_random.RandSlv(C_DATA_WIDTH);
  97. sv_miso_queue.push(v_send_data);
  98. spi_slave (data_in => v_send_data,
  99. data_out => v_receive_data,
  100. sclk => s_sclk,
  101. ste => s_ste,
  102. mosi => s_mosi,
  103. miso => s_miso,
  104. dir => direction,
  105. cpol => mode / 2,
  106. cpha => mode mod 2
  107. );
  108. sv_mosi_queue.pop(v_queue_data);
  109. assert_equal(v_receive_data, v_queue_data);
  110. end loop;
  111. end loop;
  112. end loop;
  113. report "INFO: All tests of valid spi_master() & spi_slave() combinations finished successfully";
  114. s_tests_done(1) <= true;
  115. wait;
  116. end process SpiSlaveP;
  117. end architecture sim;