1
0
mirror of https://github.com/myhdl/myhdl.git synced 2024-12-14 07:44:38 +08:00
This commit is contained in:
jand 2006-08-18 21:13:16 +00:00
parent 7257a6b76b
commit 980fb2ab2d
2 changed files with 119 additions and 0 deletions

View File

@ -0,0 +1,79 @@
from myhdl import *
DESCENDING, ASCENDING = False, True
def compare(a1, a2, z1, z2, dir):
@always_comb
def logic():
z1.next = a1
z2.next = a2
if dir == (a1 > a2):
z1.next = a2
z2.next = a1
return logic
def feedthru(a, z):
@always_comb
def logic():
z.next = a
return logic
def bitonicMerge(a, z, dir):
n = len(a)
k = n//2
w = len(a[0])
if n > 1:
t = [Signal(intbv(0)[w:]) for i in range(n)]
comp = [compare(a[i], a[i+k], t[i], t[i+k], dir) for i in range(k)]
loMerge = bitonicMerge(t[:k], z[:k], dir)
hiMerge = bitonicMerge(t[k:], z[k:], dir)
return comp, loMerge, hiMerge
else:
feed = feedthru(a[0], z[0])
return feed
def bitonicSort(a, z, dir):
n = len(a)
k = n//2
w = len(a[0])
if n > 1:
t = [Signal(intbv(0)[w:]) for i in range(n)]
loSort = bitonicSort(a[:k], t[:k], ASCENDING)
hiSort = bitonicSort(a[k:], t[k:], DESCENDING)
merge = bitonicMerge(t, z, dir)
return loSort, hiSort, merge
else:
feed = feedthru(a[0], z[0])
return feed
def Array8Sorter(a0, a1, a2, a3, a4, a5, a6, a7,
z0, z1, z2, z3, z4, z5, z6, z7):
a = [a0, a1, a2, a3, a4, a5, a6, a7]
z = [z0, z1, z2, z3, z4, z5, z6, z7]
sort = bitonicSort(a, z, ASCENDING)
return sort
def Array8Sorter_v(a0, a1, a2, a3, a4, a5, a6, a7,
z0, z1, z2, z3, z4, z5, z6, z7):
toVerilog(Array8Sorter, a0, a1, a2, a3, a4, a5, a6, a7,
z0, z1, z2, z3, z4, z5, z6, z7)
cmd = "cver -q +loadvpi=myhdl_vpi:vpi_compat_bootstrap " + \
"bitonic_ref.v tb_Array8Sorter.v"
return Cosimulation(cmd, **locals())

View File

@ -0,0 +1,40 @@
from random import randrange
from myhdl import *
from bitonic import Array8Sorter, Array8Sorter_v
def bench():
n = 8
w = 4
a0, a1, a2, a3, a4, a5, a6, a7 = inputs = [Signal(intbv(0)[w:]) for i in range(n)]
z0, z1, z2, z3, z4, z5, z6, z7 = outputs = [Signal(intbv(0)[w:]) for i in range(n)]
inst = Array8Sorter_v(a0, a1, a2, a3, a4, a5, a6, a7,
z0, z1, z2, z3, z4, z5, z6, z7)
@instance
def check():
for i in range(100):
data = [randrange(2**w) for i in range(n)]
for i in range(n):
inputs[i].next = data[i]
yield delay(10)
data.sort()
assert data == outputs
return inst, check
def test_bench():
sim = Simulation(bench())
sim.run()
if __name__ == '__main__':
test_bench()