Last active
July 13, 2020 09:37
-
-
Save mukunda-/677984308a4f5e43c7226d1c93a3f691 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// adder.go | |
package main | |
import ( | |
"fmt" | |
"os" | |
"strconv" | |
"github.com/danishm/andornot/gates" | |
) | |
func main() { | |
args := os.Args[1:] | |
if len(args) < 2 { | |
fmt.Println( "Usage: adder.go <summand> <summand>" ) | |
os.Exit( 0 ); | |
} | |
D1, err := strconv.Atoi( args[0] ) | |
if err != nil { | |
fmt.Println( "Please input data source 1!!" ) | |
os.Exit( 1 ); | |
} | |
D2, err := strconv.Atoi( args[1] ) | |
if err != nil { | |
fmt.Println( "Please input data source 2!!" ) | |
os.Exit( 2 ); | |
} | |
output := 0 | |
board := gates.DefaultBoard() | |
xor1 := gates.XOR(board) | |
xor2 := gates.XOR(board) | |
and1 := gates.AND(board) | |
and2 := gates.AND(board) | |
and3 := gates.AND(board) | |
or1 := gates.OR(board) | |
or2 := gates.OR(board) | |
board.Connect( xor1.Out(), xor2.Pin1() ) | |
board.Connect( and1.Out(), or1.Pin1() ) | |
board.Connect( and2.Out(), or1.Pin2() ) | |
board.Connect( or1.Out(), or2.Pin1() ) | |
board.Connect( and3.Out(), or2.Pin2() ) | |
// Clear carry. | |
C := 0 | |
for i := 0; i < 8; i++ { | |
// Data feed. | |
A1 := D1 & 1 | |
A2 := D2 & 1 | |
D1 >>= 1 | |
D2 >>= 1 | |
// Calculate output bit... | |
// A1──┐ 1 | |
// [XOR]───┐ 2 | |
// A2──┘ [XOR]─OUT | |
// C──────────┘ | |
xor1.Pin1() <- A1 | |
xor1.Pin2() <- A2 | |
xor2.Pin2() <- C | |
OUT := <-xor2.Out() | |
output |= OUT << i; | |
// Calculate carry bit... | |
// A1──┐ 1 | |
// [AND]─┐ 1 | |
// A2──┘ [OR]──┐ | |
// A1──┐ 2 │ │ | |
// [AND]─┘ │ 2 | |
// C───┘ [OR]──C | |
// A2──┐ 3 │ | |
// [AND]──────┘ | |
// C───┘ | |
and1.Pin1() <- A1 | |
and1.Pin2() <- A2 | |
and2.Pin1() <- A1 | |
and2.Pin2() <- C | |
and3.Pin1() <- A2 | |
and3.Pin2() <- C | |
C = <- or2.Out() | |
} | |
fmt.Println( output ) | |
board.Stop() | |
os.Exit( 0 ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment