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.

139 lines
4.2 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. SimTestP : process is
  35. variable v_time : time;
  36. begin
  37. wait until s_clk = '1';
  38. v_time := now;
  39. wait_cycles(s_clk, 10);
  40. assert (now - v_time) = C_PERIOD * 20
  41. severity failure;
  42. s_tests_done(0) <= true;
  43. report "INFO: wait_cycles() procedure tests finished successfully";
  44. wait;
  45. end process SimTestP;
  46. -- Unit test of spi master procedure, checks all combinations
  47. -- of cpol & cpha against spi slave procedure
  48. SpiMasterP : process is
  49. variable v_send_data : std_logic_vector(C_DATA_WIDTH-1 downto 0) := (others => '0');
  50. variable v_receive_data : std_logic_vector(C_DATA_WIDTH-1 downto 0) := (others => '0');
  51. variable v_queue_data : std_logic_vector(C_DATA_WIDTH-1 downto 0) := (others => '0');
  52. variable v_random : RandomPType;
  53. begin
  54. v_random.InitSeed(v_random'instance_name);
  55. for direction in 0 to 1 loop
  56. for mode in 0 to 3 loop
  57. for i in 0 to 255 loop
  58. v_send_data := v_random.RandSlv(C_DATA_WIDTH);
  59. sv_mosi_queue.push(v_send_data);
  60. spi_master (data_in => v_send_data,
  61. data_out => v_receive_data,
  62. sclk => s_sclk,
  63. ste => s_ste,
  64. mosi => s_mosi,
  65. miso => s_miso,
  66. dir => direction,
  67. cpol => mode / 2,
  68. cpha => mode mod 2,
  69. period => 1 us
  70. );
  71. sv_miso_queue.pop(v_queue_data);
  72. assert_equal(v_receive_data, v_queue_data);
  73. end loop;
  74. end loop;
  75. end loop;
  76. wait;
  77. end process SpiMasterP;
  78. -- Unit test of spi slave procedure, checks all combinations
  79. -- of cpol & cpha against spi master procedure
  80. SpiSlaveP : process is
  81. variable v_send_data : std_logic_vector(C_DATA_WIDTH-1 downto 0) := (others => '0');
  82. variable v_receive_data : std_logic_vector(C_DATA_WIDTH-1 downto 0) := (others => '0');
  83. variable v_queue_data : std_logic_vector(C_DATA_WIDTH-1 downto 0) := (others => '0');
  84. variable v_random : RandomPType;
  85. begin
  86. v_random.InitSeed(v_random'instance_name);
  87. for direction in 0 to 1 loop
  88. for mode in 0 to 3 loop
  89. for i in 0 to 255 loop
  90. v_send_data := v_random.RandSlv(C_DATA_WIDTH);
  91. sv_miso_queue.push(v_send_data);
  92. spi_slave (data_in => v_send_data,
  93. data_out => v_receive_data,
  94. sclk => s_sclk,
  95. ste => s_ste,
  96. mosi => s_mosi,
  97. miso => s_miso,
  98. dir => direction,
  99. cpol => mode / 2,
  100. cpha => mode mod 2
  101. );
  102. sv_mosi_queue.pop(v_queue_data);
  103. assert_equal(v_receive_data, v_queue_data);
  104. end loop;
  105. end loop;
  106. end loop;
  107. report "INFO: All tests of valid spi_master() & spi_slave() combinations finished successfully";
  108. s_tests_done(1) <= true;
  109. wait;
  110. end process SpiSlaveP;
  111. end architecture sim;