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.

133 lines
3.9 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 mode in 0 to 3 loop
  56. for i in 0 to 255 loop
  57. v_send_data := v_random.RandSlv(C_DATA_WIDTH);
  58. sv_mosi_queue.push(v_send_data);
  59. spi_master (data_in => v_send_data,
  60. data_out => v_receive_data,
  61. sclk => s_sclk,
  62. ste => s_ste,
  63. mosi => s_mosi,
  64. miso => s_miso,
  65. cpol => mode / 2,
  66. cpha => mode mod 2,
  67. period => 1 us
  68. );
  69. sv_miso_queue.pop(v_queue_data);
  70. assert_equal(v_receive_data, v_queue_data);
  71. end loop;
  72. end loop;
  73. wait;
  74. end process SpiMasterP;
  75. -- Unit test of spi slave procedure, checks all combinations
  76. -- of cpol & cpha against spi master procedure
  77. SpiSlaveP : process is
  78. variable v_send_data : std_logic_vector(C_DATA_WIDTH-1 downto 0) := (others => '0');
  79. variable v_receive_data : std_logic_vector(C_DATA_WIDTH-1 downto 0) := (others => '0');
  80. variable v_queue_data : std_logic_vector(C_DATA_WIDTH-1 downto 0) := (others => '0');
  81. variable v_random : RandomPType;
  82. begin
  83. v_random.InitSeed(v_random'instance_name);
  84. for mode in 0 to 3 loop
  85. for i in 0 to 255 loop
  86. v_send_data := v_random.RandSlv(C_DATA_WIDTH);
  87. sv_miso_queue.push(v_send_data);
  88. spi_slave (data_in => v_send_data,
  89. data_out => v_receive_data,
  90. sclk => s_sclk,
  91. ste => s_ste,
  92. mosi => s_mosi,
  93. miso => s_miso,
  94. cpol => mode / 2,
  95. cpha => mode mod 2
  96. );
  97. sv_mosi_queue.pop(v_queue_data);
  98. assert_equal(v_receive_data, v_queue_data);
  99. end loop;
  100. end loop;
  101. report "INFO: spi_* procedures tests finished successfully";
  102. s_tests_done(1) <= true;
  103. wait;
  104. end process SpiSlaveP;
  105. end architecture sim;