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

91 lines
1.8 KiB
Python
Raw Normal View History

2003-02-19 22:35:10 +00:00
from random import randrange
2005-12-10 23:12:01 +00:00
from myhdl import *
2003-02-19 22:35:10 +00:00
ACTIVE_LOW, INACTIVE_HIGH = 0, 1
def Inc(count, enable, clock, reset, n):
2005-12-10 23:12:01 +00:00
2003-02-19 22:35:10 +00:00
""" Incrementer with enable.
count -- output
enable -- control input, increment when 1
clock -- clock input
reset -- asynchronous reset input
n -- counter max value
2005-12-10 23:12:01 +00:00
2003-02-19 22:35:10 +00:00
"""
2005-12-10 23:12:01 +00:00
@always(clock.posedge, reset.negedge)
def incLogic():
2003-02-19 22:35:10 +00:00
if reset == ACTIVE_LOW:
count.next = 0
else:
if enable:
count.next = (count + 1) % n
2005-12-10 23:12:01 +00:00
return incLogic
2003-02-19 22:35:10 +00:00
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
2005-12-10 23:12:01 +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
2005-12-27 12:41:51 +00:00
yield clock.negedge
2003-07-07 16:41:33 +00:00
reset.next = INACTIVE_HIGH
for i in range(12):
enable.next = min(1, randrange(3))
2005-12-27 12:41:51 +00:00
yield clock.negedge
2003-07-07 16:41:33 +00:00
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"
2005-12-27 12:41:51 +00:00
yield reset.posedge
2003-07-07 16:41:33 +00:00
while 1:
2005-12-27 12:41:51 +00:00
yield clock.posedge
2003-07-07 16:41:33 +00:00
yield delay(1)
print " %s %s" % (enable, count)
2005-12-10 23:12:01 +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()
# conversion
m = 8
n = 2 ** m
count = Signal(intbv(0)[m:])
enable = Signal(bool(0))
clock, reset = [Signal(bool()) for i in range(2)]
inc_inst = Inc(count, enable, clock, reset, n=n)
inc_inst = toVerilog(Inc, count, enable, clock, reset, n=n)
inc_inst = toVHDL(Inc, count, enable, clock, reset, n=n)
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