Verwendeter Programmcode in Studienarbeit für ESY1B zum Thema "Verifikation mit SystemVerilog und Python"
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.

counter_4bit_tb.py 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import os
  2. import myhdl
  3. from myhdl import *
  4. module = 'counter_4bit'
  5. testbench = 'tb_%s' % module
  6. build_cmd = "iverilog -o %s.vvp %s.v %s.v" % (testbench, module, testbench)
  7. def tb_counter_4bit():
  8. clk = Signal(bool(0))
  9. rst = Signal(bool(0))
  10. updown = Signal(bool(0))
  11. load = Signal(bool(0))
  12. data = Signal(modbv(val=0, min=0, max=15)[4:])
  13. data_out = Signal(modbv(val=0, min=0, max=15)[4:])
  14. # DUT
  15. print(build_cmd)
  16. if os.system(build_cmd):
  17. raise Exception("Error running build command")
  18. dut = Cosimulation(
  19. "vvp -m myhdl %s.vvp -lxt2" % testbench,
  20. clk=clk,
  21. rst=rst,
  22. updown=updown,
  23. load=load,
  24. data=data,
  25. data_out=data_out
  26. )
  27. @always(delay(4))
  28. def clkgen():
  29. clk.next = not clk
  30. @instance
  31. def check():
  32. print("initialize")
  33. clk.next = 0
  34. rst.next = 0
  35. load.next = 0
  36. updown.next = 1
  37. data.next = 0
  38. yield clk.posedge
  39. yield clk.posedge
  40. print("... OK")
  41. print("test 1: reset")
  42. yield clk.negedge
  43. rst.next = 1
  44. yield clk.posedge
  45. yield clk.negedge
  46. rst.next = 0
  47. yield clk.posedge
  48. assert data_out == 0, f"assert failed with data_out {data_out} != 0"
  49. yield clk.posedge
  50. yield clk.posedge
  51. print("... OK")
  52. print("test 2: load")
  53. yield clk.posedge
  54. load.next = 1
  55. data.next = Signal(modbv(13)[4:])
  56. yield clk.negedge
  57. yield clk.posedge
  58. yield clk.negedge
  59. assert data_out == 13, f"assert failed with data_out {data_out} != {13}"
  60. load.next = 0
  61. yield clk.posedge
  62. yield clk.negedge
  63. print("... OK")
  64. print("test 3: inc")
  65. yield clk.negedge
  66. rst.next = 1
  67. yield clk.posedge
  68. yield clk.negedge
  69. rst.next = 0
  70. soll = 0
  71. for i in range(4):
  72. assert data_out == soll, f"assert failed with data_out {int(data_out)} != {i}"
  73. yield clk.posedge
  74. soll += 1
  75. yield clk.negedge
  76. print("... OK")
  77. print("test 4: dec")
  78. soll = 13
  79. yield clk.posedge
  80. updown.next = 0
  81. load.next = 1
  82. data.next = Signal(modbv(soll)[4:])
  83. yield clk.negedge
  84. yield clk.posedge
  85. yield clk.negedge
  86. load.next = 0
  87. for i in range(4):
  88. assert data_out == soll, f"assert failed with data_out {int(data_out)} != {soll}"
  89. yield clk.posedge
  90. soll -= 1
  91. yield clk.negedge
  92. print("... OK")
  93. for _ in range(10):
  94. yield clk.posedge
  95. raise StopSimulation
  96. return instances()
  97. def simulate():
  98. sim = Simulation(tb_counter_4bit())
  99. sim.run()