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

56 lines
1.3 KiB
Python
Raw Normal View History

2016-03-10 20:27:07 +01:00
import myhdl
2008-08-28 21:43:19 +02:00
from myhdl import *
2003-01-23 22:33:19 +00:00
from arith_utils import BEHAVIOR
from PrefixAnd import PrefixAnd
def LeadZeroDet(width, speed, A, Z, architecture=BEHAVIOR):
2003-02-03 21:54:19 +00:00
2003-02-03 19:32:51 +00:00
""" Set output bit to 1 at first non-zero MSB bit in input
width -- bit width
speed -- SLOW, MEDIUM, or FAST performance
A -- input
Z -- output
architecture - BEHAVIOR or STRUCTURE
"""
2003-01-23 22:33:19 +00:00
2008-08-28 21:43:19 +02:00
@instance
2003-01-23 22:33:19 +00:00
def Behavioral():
while 1:
yield A
zv = intbv(0)
for i in downrange(width):
2003-02-15 14:54:40 +00:00
if A[i] == 1:
2003-01-23 22:33:19 +00:00
zv[i] = 1
break
Z.next = zv
def Structural():
2005-10-22 19:19:39 +00:00
PI = Signal(intbv(0))
PO = Signal(intbv(0))
PIT = Signal(intbv(0))
POT = Signal(intbv(0))
2003-01-23 22:33:19 +00:00
prefix = PrefixAnd(width, speed, PIT, POT)
2008-08-28 21:43:19 +02:00
@instance
2003-01-23 22:33:19 +00:00
def logic():
while 1:
yield PI, POT, PO, A
2003-02-15 14:54:40 +00:00
PI.next = ~A
2003-01-23 22:33:19 +00:00
for i in downrange(width):
2003-02-15 14:54:40 +00:00
PIT.next[i] = PI[width-i-1]
PO.next[i] = POT[width-i-1]
Z.next[width-1] = A[width-1]
Z.next[width-1:] = PO[width:1] & A[width-1:]
2008-08-28 21:43:19 +02:00
return [prefix, logic]
2003-01-23 22:33:19 +00:00
if architecture == BEHAVIOR:
2008-08-28 21:43:19 +02:00
return Behavioral
2003-01-23 22:33:19 +00:00
else:
return Structural()