Examples of using cocotb for functional verification of VHDL designs with GHDL.
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.

94 lines
2.6 KiB

  1. # test_uart.py
  2. import logging
  3. import random
  4. import cocotb
  5. import wavedrom
  6. from Uart import UartDriver, UartReceiver
  7. from Vai import VaiDriver, VaiReceiver
  8. from cocotb.clock import Clock
  9. from cocotb.triggers import FallingEdge, RisingEdge, Timer, ReadOnly
  10. from cocotb.wavedrom import Wavedrom, trace
  11. # Reset coroutine
  12. async def reset_dut(reset_n, duration_ns):
  13. reset_n.value = 0
  14. await Timer(duration_ns, units="ns")
  15. reset_n.value = 1
  16. def wave2svg(wave, file):
  17. svg = wavedrom.render(wave)
  18. svg.saveas(file)
  19. @cocotb.test()
  20. async def test_uarttx(dut):
  21. """ First simple test """
  22. clkedge = RisingEdge(dut.clk_i)
  23. # Connect reset
  24. reset_n = dut.reset_n_i
  25. # Instantiate VAI driver
  26. vai_driver = VaiDriver(dut.clk_i, dut.data_i, dut.valid_i, dut.accept_o)
  27. # Instantiate UART receiver
  28. uart_receiver = UartReceiver(dut.tx_o, dut.clk_i, 10, 8, True);
  29. # Drive input defaults (setimmediatevalue to avoid x asserts)
  30. dut.data_i.setimmediatevalue(0)
  31. dut.valid_i.setimmediatevalue(0)
  32. clock = Clock(dut.clk_i, 10, units="ns") # Create a 10 ns period clock
  33. cocotb.start_soon(clock.start()) # Start the clock
  34. # Execution will block until reset_dut has completed
  35. dut._log.info("Hold reset")
  36. await reset_dut(reset_n, 100)
  37. dut._log.info("Released reset")
  38. # Test 10 UART transmissions
  39. for i in range(10):
  40. await clkedge
  41. val = random.randint(0, 255)
  42. await vai_driver.send(val)
  43. rec = await uart_receiver.receive();
  44. assert rec == val, "UART sent data was incorrect on the {}th cycle".format(i)
  45. @cocotb.test()
  46. async def test_uartrx(dut):
  47. """ First simple test """
  48. clkedge = RisingEdge(dut.clk_i)
  49. # Connect reset
  50. reset_n = dut.reset_n_i
  51. # Instantiate UART driver
  52. uart_driver = UartDriver(dut.rx_i, dut.clk_i, 10, 8, True);
  53. # Instantiate VAI receiver
  54. vai_receiver = VaiReceiver(dut.clk_i, dut.data_o, dut.valid_o, dut.accept_i)
  55. # Drive input defaults (setimmediatevalue to avoid x asserts)
  56. dut.rx_i.setimmediatevalue(1)
  57. dut.accept_i.setimmediatevalue(0)
  58. clock = Clock(dut.clk_i, 10, units="ns") # Create a 1 us period clock
  59. cocotb.start_soon(clock.start()) # Start the clock
  60. # Execution will block until reset_dut has completed
  61. dut._log.info("Hold reset")
  62. await reset_dut(reset_n, 100)
  63. dut._log.info("Released reset")
  64. # Test 10 UART transmissions
  65. for i in range(10):
  66. await clkedge
  67. val = random.randint(0, 255)
  68. await uart_driver.send(val)
  69. rec = await vai_receiver.receive();
  70. assert rec == val, "UART received data was incorrect on the {}th cycle".format(i)