60 lines
1.9 KiB
Plaintext
60 lines
1.9 KiB
Plaintext
Module FourBitAdder {
|
|
Flush
|
|
dim not(0 to 1),and(0 to 1, 0 to 1),or(0 to 1, 0 to 1)
|
|
not(0)=1,0
|
|
and(0,0)=0,0,0,1
|
|
or(0,0)=0,1,1,1
|
|
xor=lambda not(),and(),or() (a,b)-> or(and(a, not(b)), and(b, not(a)))
|
|
ha=lambda xor, and() (a,b, &s, &c)->{
|
|
s=xor(a,b)
|
|
c=and(a,b)
|
|
}
|
|
fa=lambda ha, or() (a, b, c0, &s, &c1)->{
|
|
def sa,ca,cb
|
|
call ha(a, c0, &sa, &ca)
|
|
call ha(sa, b, &s,&cb)
|
|
c1=or(ca,cb)
|
|
}
|
|
add4=lambda fa (inpA(), inpB(), &v, &out()) ->{
|
|
dim carry(0 to 4)=0
|
|
carry(0)=v \\ 0 or 1 borrow
|
|
for i=0 to 3
|
|
\\ mm=fa(InpA(i), inpB(i), carry(i), &out(i), &carry(i+1)) ' same as this
|
|
Call fa(InpA(i), inpB(i), carry(i), &out(i), &carry(i+1))
|
|
next
|
|
v=carry(4)
|
|
}
|
|
dim res(0 to 3)=-1, low()
|
|
source=lambda->{
|
|
shift 1, -stack.size ' reverse stack items
|
|
=array([]) ' convert current stack to array, empty current stack
|
|
}
|
|
def v, k, k_low
|
|
Print "First Example 4-bit"
|
|
Print "A", "", 1, 0, 1, 0
|
|
Print "B", "", 1, 0, 0, 1
|
|
call add4(source(1,0,1,0), source(1,0,0,1), &v, &res())
|
|
k=each(res() end to start) ' k is an iterator, now configure to read items in reverse
|
|
Print "A+B",v, k ' print 1 0 0 1 1
|
|
Print "Second Example 4-bit"
|
|
v-=v
|
|
Print "A", "", 0, 1, 1, 0
|
|
Print "B", "", 0, 1, 1, 1
|
|
call add4(source(0,1,1,0), source(0,1,1,1), &v, &res())
|
|
k=each(res() end to start) ' k is an iterator, now configure to read items in reverse
|
|
Print "A+B",v, k ' print 0 1 1 0 1
|
|
Print "Third Example 8-bit"
|
|
v-=v
|
|
Print "A ", "", 1, 0, 0, 0, 0, 1, 1, 0
|
|
Print "B ", "", 1, 1, 1, 1, 1, 1, 1, 1
|
|
call add4(source(0,1,1,0), source(1,1,1,1), &v, &res())
|
|
low()=res() ' a copy of res()
|
|
' v passed to second adder
|
|
dim res(0 to 3)=-1
|
|
call add4(source(1,0,0,0), source(1,1,1,1), &v, &res())
|
|
k_low=each(low() end to start) ' k_low is an iterator, now configure to read items in reverse
|
|
k=each(res() end to start) ' k is an iterator, now configure to read items in reverse
|
|
Print "A+B",v, k, k_low ' print 1 1 0 0 0 0 1 0 1
|
|
}
|
|
FourBitAdder
|