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.

78 lines
2.6 KiB

  1. # test_uart.py
  2. import logging
  3. import random
  4. import cocotb
  5. import pprint
  6. from collections import defaultdict
  7. from Sram import SramRead, SramWrite, SramMonitor
  8. from cocotb.clock import Clock
  9. from cocotb.triggers import FallingEdge, RisingEdge, Timer, ReadOnly
  10. from cocotbext.wishbone.driver import WishboneMaster, WBOp
  11. # Reset coroutine
  12. async def reset_dut(reset_n, duration_ns):
  13. reset_n.value = 1
  14. await Timer(duration_ns, units="ns")
  15. reset_n.value = 0
  16. @cocotb.test()
  17. async def test_wishbone(dut):
  18. """ First simple test """
  19. clkedge = RisingEdge(dut.wbclk_i)
  20. # Connect reset
  21. reset = dut.wbrst_i
  22. # Create empty SRAM memory
  23. memory = defaultdict()
  24. mem_read = SramRead(dut.wbclk_i, dut.localren_o,
  25. dut.localadress_o, dut.localdata_i, memory);
  26. mem_write = SramWrite(dut.wbclk_i, dut.localwen_o,
  27. dut.localadress_o, dut.localdata_o, memory);
  28. sram_monitor = SramMonitor(dut.wbclk_i, dut.localwen_o, dut.localren_o,
  29. dut.localadress_o, dut.localdata_i, dut.localdata_o);
  30. wbmaster = WishboneMaster(dut, "", dut.wbclk_i,
  31. width=16, # size of data bus
  32. timeout=10, # in clock cycle number
  33. signals_dict={"cyc": "wbcyc_i",
  34. "stb": "wbstb_i",
  35. "we": "wbwe_i",
  36. "adr": "wbadr_i",
  37. "datwr":"wbdat_i",
  38. "datrd":"wbdat_o",
  39. "ack": "wback_o" })
  40. # Drive input defaults (setimmediatevalue to avoid x asserts)
  41. dut.wbcyc_i.setimmediatevalue(0)
  42. dut.wbstb_i.setimmediatevalue(0)
  43. dut.wbwe_i.setimmediatevalue(0)
  44. dut.wbadr_i.setimmediatevalue(0)
  45. dut.wbdat_i.setimmediatevalue(0)
  46. clock = Clock(dut.wbclk_i, 10, units="ns") # Create a 10 ns period clock
  47. cocotb.start_soon(clock.start()) # Start the clock
  48. # Execution will block until reset_dut has completed
  49. dut._log.info("Hold reset")
  50. await reset_dut(reset, 100)
  51. dut._log.info("Released reset")
  52. # Test 10 Wishbone transmissions
  53. for i in range(10):
  54. await clkedge
  55. adr = random.randint(0, 255)
  56. data = random.randint(0, 2**16-1)
  57. await wbmaster.send_cycle([WBOp(adr=adr, dat=data)])
  58. rec = await wbmaster.send_cycle([WBOp(adr=adr)])
  59. # Example to print transactions collected by SRAM monitor
  60. with open('results/sram_transactions.log', 'w', encoding='utf-8') as f:
  61. f.write((f"{'Time':7}{'Type':7}{'Adr':11}{'Data'}\n"))
  62. for k, v in sram_monitor.transactions.items():
  63. f.write((f"{k:7}{v['type']:7}{v['adr']:11}{v['data']} \n"))