or =: +.
and =: *.
not =: -.
bitOr =: monad define
('a' ; 'b') =. y.
a or b
)
bitAnd =: monad define
('a' ; 'b') =. y.
a and b
)
bitNot =: not
bitHalfAdder =: monad define
('a' ; 'b') =. y.
bitOr (bitAnd a , bitNot b) , bitAnd (bitNot a) , b
)
bitXor =: bitHalfAdder
wireOutput =: monad define
('pin' ; 'outputs') =. y.
pin from outputs
)
bitAdder =: monad define
('a' ; 'b' ; 'cin') =. y.
t =. bitHalfAdder a , b
g =. bitAnd a , b
p =. bitAnd t , cin
(bitOr g , p) , bitHalfAdder t , cin
)
fourBitAlu =: monad define
('a3' ; 'a2' ; 'a1' ; 'a0' ; 'b3' ; 'b2' ; 'b1' ; 'b0' ; 'sub') =. y.
t0 =. bitAdder a0 , (bitXor b0 , sub) , sub
t1 =. bitAdder a1 , (bitXor b1 , sub) , wireOutput 0 ; t0
t2 =. bitAdder a2 , (bitXor b2 , sub) , wireOutput 0 ; t1
t3 =. bitAdder a3 , (bitXor b3 , sub) , wireOutput 0 ; t2
(wireOutput 0 1 ; t3) , (wireOutput 1 ; t2) , (wireOutput 1 ; t1) , wireOutput 1 ; t0
)
Below we show the sum and difference of _1 and 1 (ignoring the carry) producing
results of 0 0 0 0 and 1 1 1 0 (0 and _2).
fourBitAlu 1 1 1 1 0 0 0 1 0 1 0 0 0 0 fourBitAlu 1 1 1 1 0 0 0 1 1 1 1 1 1 0