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

143 lines
2.8 KiB
Python
Raw Normal View History

2003-03-06 19:10:15 +00:00
import sys
2003-06-30 14:24:23 +00:00
import traceback
2003-03-06 19:10:15 +00:00
2016-03-10 20:27:07 +01:00
import myhdl
2005-12-14 11:38:23 +00:00
from myhdl import *
2003-03-06 19:10:15 +00:00
class Error(Exception):
pass
def fifo(dout, din, re, we, empty, full, clk, maxFilling=sys.maxint):
""" Synchronous fifo model based on a list.
Ports:
dout -- data out
din -- data in
re -- read enable
we -- write enable
empty -- empty indication flag
full -- full indication flag
clk -- clock input
Optional parameter:
maxFilling -- maximum fifo filling, "infinite" by default
"""
memory = []
2005-12-14 11:38:23 +00:00
@always(clk.posedge)
def access():
2003-03-06 19:10:15 +00:00
if we:
memory.insert(0, din.val)
if re:
dout.next = memory.pop()
2003-03-07 09:42:57 +00:00
filling = len(memory)
empty.next = (filling == 0)
full.next = (filling == maxFilling)
2003-03-06 19:10:15 +00:00
2005-12-14 11:38:23 +00:00
return access
2003-03-06 19:10:15 +00:00
def fifo2(dout, din, re, we, empty, full, clk, maxFilling=sys.maxint):
""" Synchronous fifo model based on a list.
Ports:
dout -- data out
din -- data in
re -- read enable
we -- write enable
empty -- empty indication flag
full -- full indication flag
clk -- clock input
Optional parameter:
maxFilling -- maximum fifo filling, "infinite" by default
"""
memory = []
2005-12-14 11:38:23 +00:00
@always(clk.posedge)
def access():
2003-03-06 19:10:15 +00:00
if we:
memory.insert(0, din.val)
if re:
try:
dout.next = memory.pop()
except IndexError:
2003-03-06 22:52:41 +00:00
raise Error, "Underflow -- Read from empty fifo"
2003-03-07 09:42:57 +00:00
filling = len(memory)
empty.next = (filling == 0)
full.next = (filling == maxFilling)
if filling > maxFilling:
2003-03-06 22:52:41 +00:00
raise Error, "Overflow -- Max filling %s exceeded" % maxFilling
2003-03-06 19:10:15 +00:00
2005-12-14 11:38:23 +00:00
return access
2003-03-06 19:10:15 +00:00
dout, din, re, we, empty, full, clk = args = [Signal(0) for i in range(7)]
dut = fifo2(dout, din, re, we, empty, full, clk, maxFilling=3)
def clkGen():
while 1:
yield delay(10)
clk.next = not clk
def read():
2005-12-14 11:38:23 +00:00
yield clk.negedge
2003-03-06 19:10:15 +00:00
re.next = 1
2005-12-14 11:38:23 +00:00
yield clk.posedge
2003-03-06 19:10:15 +00:00
yield delay(1)
re.next = 0
def write(data):
2005-12-14 11:38:23 +00:00
yield clk.negedge
2003-03-06 19:10:15 +00:00
din.next = data
we.next = 1
2005-12-14 11:38:23 +00:00
yield clk.posedge
2003-03-06 19:10:15 +00:00
yield delay(1)
we.next = 0
def report():
print "dout: %s empty: %s full: %s" % (hex(dout), empty, full)
def test():
yield write(0x55)
report()
yield write(0x77)
report()
yield write(0x11)
report()
yield join(write(0x22), read())
report()
yield join(write(0x33), read())
report()
yield read()
report()
yield read()
report()
yield read()
report()
yield read()
report()
yield read()
raise StopSimulation
sim = Simulation(clkGen(), test(), dut)
2003-06-30 14:24:23 +00:00
def main():
try:
sim.run()
except:
traceback.print_exc()
2003-03-06 19:10:15 +00:00
2003-06-30 14:24:23 +00:00
if __name__ == '__main__':
main()