Four bit adder: Difference between revisions

Content added Content deleted
(→‎{{header|MyHDL}}: syntax highlithing)
Line 1,214:
To interpret and run this code you will need a recent copy of Python, and the MyHDL library from myhdl.org. Both examples integrate test code, and export Verilog and VHDL for hardware synthesis.
 
Verbose Code - With integrated Test & Demo
 
<lang python>
#!/usr/bin/env python
 
""" http://rosettacode.org/wiki/Four_bit_adder
Demonstrate theoretical four bit adder simulation
using And, Or & Invert primitives
sumVar = 0
2011-05-10 jc
"""
 
from myhdl import always_comb, ConcatSignal, delay, intbv, Signal, \
Simulation, toVerilog, toVHDL
sumVar += 2**i
from random import randrange
 
 
""" define set of primitives
Line 1,245 ⟶ 1,254:
z.next = a or b
return logic
 
 
""" build components using defined primitive set
Line 1,282 ⟶ 1,292:
fullAdder_2 = fullAdder(c[3],sl[2], c[2],ina(2),inb(2))
fullAdder_3 = fullAdder(co, sl[3], c[3],ina(3),inb(3))
# create an internal bus for the output list
sc = ConcatSignal(*reversed(sl)) # create internal bus for output list
@always_comb
def logic_list2intbvlist2intbv():
sum4.next = sc # assign internal bus to actual output
sumVar = 0
 
for i in range(4):
return halfAdder_0, fullAdder_1, fullAdder_2, fullAdder_3, logic_list2intbvlist2intbv
if sl[i] <> 0:
 
sumVar += 2**i
sum4.next = sumVar
return halfAdder_0,fullAdder_1,fullAdder_2,fullAdder_3, logic_list2intbv
 
""" define signals and code for testing
Line 1,295 ⟶ 1,305:
t_co, t_s, t_a, t_b, dbug = [Signal(bool(0)) for i in range(5)]
ina4, inb4, sum4 = [Signal(intbv(0)[4:]) for i in range(3)]
sum4 = Signal(intbv(0)[4:])
 
def test():
Line 1,306 ⟶ 1,315:
assert t_co * 16 + sum4 == ina4 + inb4
print
 
 
""" instantiate components and run test