Complete Adder Example
Here is the complete adder example code used in Half Adder, Full Adder, and 4-bit Adder:
adder.vir
mod HalfAdder {
incoming a : Bit
incoming b : Bit
// sum is true when one of a and b is true (but not both)
outgoing sum : Bit dependson a, b {
it := a ^^ b
}
// carry is true when a and b are both true
outgoing carry_out : Bit dependson a, b {
it := a && b
}
}
mod FullAdder {
incoming a : Bit
incoming b : Bit
incoming carry_in : Bit
outgoing sum : Bit dependson a, b, carry_in {
it := halfadder_1.sum
}
outgoing carry_out : Bit dependson a, b, carry_in {
it := halfadder_0.carry_out || halfadder_1.carry_out
}
// first, sum a + b
mod halfadder_0 of HalfAdder {
it.a := a
it.b := b
}
// then, sum the result with carry_in
mod halfadder_1 of HalfAdder {
it.a := halfadder_0.sum
it.b := carry_in
}
}
pub mod Adder4 {
incoming a : Word[4]
incoming b : Word[4]
incoming carry_in : Bit
outgoing sum : Word[4] dependson a, b, carry_in
outgoing carry_out : Bit dependson a, b, carry_in
mod fulladder_0 of FullAdder {
it.a := a[0]
it.b := b[0]
it.carry_in := carry_in
}
mod fulladder_1 of FullAdder {
it.a := a[1]
it.b := b[1]
it.carry_in := fulladder_0.carry_out
}
mod fulladder_2 of FullAdder {
it.a := a[2]
it.b := b[2]
it.carry_in := fulladder_1.carry_out
}
mod fulladder_3 of FullAdder {
it.a := a[3]
it.b := b[3]
it.carry_in := fulladder_2.carry_out
carry_out := it.carry_out
}
sum := word(
fulladder_3.sum,
fulladder_2.sum,
fulladder_1.sum,
fulladder_0.sum,
)
}