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