1
0
mirror of https://github.com/myhdl/myhdl.git synced 2024-12-14 07:44:38 +08:00

77 lines
1.6 KiB
Python
Raw Normal View History

2003-02-19 22:35:10 +00:00
from __future__ import generators
from random import randrange
from myhdl import Signal, Simulation, StopSimulation
2005-10-21 09:37:50 +00:00
from myhdl import intbv, delay, posedge, negedge, now, always, instance
2003-02-19 22:35:10 +00:00
ACTIVE_LOW, INACTIVE_HIGH = 0, 1
def Inc(count, enable, clock, reset, n):
""" Incrementer with enable.
count -- output
enable -- control input, increment when 1
clock -- clock input
reset -- asynchronous reset input
n -- counter max value
"""
while 1:
2005-10-21 09:37:50 +00:00
yield clock.posedge, reset.negedge
2003-02-19 22:35:10 +00:00
if reset == ACTIVE_LOW:
count.next = 0
else:
if enable:
count.next = (count + 1) % n
2003-07-07 16:41:33 +00:00
def testbench():
count, enable, clock, reset = [Signal(intbv(0)) for i in range(4)]
2003-02-19 22:35:10 +00:00
2003-07-07 16:41:33 +00:00
INC_1 = Inc(count, enable, clock, reset, n=4)
2003-02-19 22:35:10 +00:00
2005-10-21 15:01:41 +00:00
HALF_PERIOD = delay(10)
@always(HALF_PERIOD)
2003-07-07 16:41:33 +00:00
def clockGen():
2005-10-21 09:37:50 +00:00
clock.next = not clock
2003-02-19 22:35:10 +00:00
2005-10-21 09:37:50 +00:00
@instance
2003-07-07 16:41:33 +00:00
def stimulus():
reset.next = ACTIVE_LOW
2003-02-19 22:35:10 +00:00
yield negedge(clock)
2003-07-07 16:41:33 +00:00
reset.next = INACTIVE_HIGH
for i in range(12):
enable.next = min(1, randrange(3))
yield negedge(clock)
raise StopSimulation
2003-02-19 22:35:10 +00:00
2005-10-21 09:37:50 +00:00
@instance
2003-07-07 16:41:33 +00:00
def monitor():
print "enable count"
yield posedge(reset)
while 1:
yield posedge(clock)
yield delay(1)
print " %s %s" % (enable, count)
2005-10-21 09:37:50 +00:00
return clockGen, stimulus, INC_1, monitor
2003-07-07 16:41:33 +00:00
tb = testbench()
2003-02-19 22:35:10 +00:00
2003-06-30 14:24:23 +00:00
def main():
2003-07-07 16:41:33 +00:00
Simulation(tb).run()
2003-02-19 22:35:10 +00:00
2003-06-30 14:24:23 +00:00
if __name__ == '__main__':
main()
2003-02-19 22:35:10 +00:00