Created
September 3, 2024 08:26
-
-
Save umutbasal/b4cb154fde61fc6c3a7a9692c8905873 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
package main | |
import "fmt" | |
type AND map[bool]map[bool]bool | |
const ( | |
TRUE = true | |
FALSE = false | |
) | |
var ANDTable = AND{ | |
TRUE: map[bool]bool{ | |
TRUE: true, | |
FALSE: false, | |
}, | |
FALSE: map[bool]bool{ | |
TRUE: false, | |
FALSE: false, | |
}, | |
} | |
func and(a, b bool) bool { | |
return ANDTable[a][b] | |
} | |
type NOT map[bool]bool | |
var NOTTable = NOT{ | |
TRUE: false, | |
FALSE: true, | |
} | |
func not(a bool) bool { | |
return NOTTable[a] | |
} | |
func nand(a, b bool) bool { | |
return not(and(a, b)) | |
} | |
func or(a, b bool) bool { | |
return nand(not(a), not(b)) | |
} | |
func nor(a, b bool) bool { | |
return not(or(a, b)) | |
} | |
func xor(a, b bool) bool { | |
return and(or(a, b), nand(a, b)) | |
} | |
func xnor(a, b bool) bool { | |
return not(xor(a, b)) | |
} | |
type Register [4]bool // 4-bit register | |
func (r Register) String() string { | |
b := 0 | |
for i := 0; i < 4; i++ { | |
if r[i] { | |
b |= 1 << (3 - i) | |
} | |
} | |
return fmt.Sprintf("%d", b) | |
} | |
var ( | |
R0, R1, R2 Register | |
) | |
func loadRegister(reg *Register, value [4]bool) { | |
reg[0] = value[0] | |
reg[1] = value[1] | |
reg[2] = value[2] | |
reg[3] = value[3] | |
} | |
func halfAdder(a, b bool) (sum, carry bool) { | |
sum = xor(a, b) | |
carry = and(a, b) | |
return | |
} | |
func fullAdder(a, b, carryIn bool) (sum, carryOut bool) { | |
sum1, carry1 := halfAdder(a, b) | |
sum, carry2 := halfAdder(sum1, carryIn) | |
carryOut = or(carry1, carry2) | |
return | |
} | |
func add4Bit(a, b Register) (sum Register, carryOut bool) { | |
var carryIn bool | |
sum[3], carryIn = fullAdder(a[3], b[3], carryIn) | |
sum[2], carryIn = fullAdder(a[2], b[2], carryIn) | |
sum[1], carryIn = fullAdder(a[1], b[1], carryIn) | |
sum[0], carryIn = fullAdder(a[0], b[0], carryIn) | |
carryOut = carryIn | |
return | |
} | |
type Instruction func() | |
var instructionSet = map[string]Instruction{ | |
"LOAD_R0": func() { | |
loadRegister(&R0, Register{false, false, true, false}) // 0101 (5 in decimal) | |
}, | |
"LOAD_R1": func() { | |
loadRegister(&R1, Register{false, true, true, true}) // 1101 (13 in decimal) | |
}, | |
"ADD": func() { | |
R2, _ = add4Bit(R0, R1) // Add R0 and R1, store result in R2 | |
}, | |
"STORE": func() { | |
fmt.Printf("R0: %v\n", R0) // Print R0 value | |
fmt.Printf("R1: %v\n", R1) // Print R1 value | |
fmt.Printf("R2 (Sum): %v\n", R2) // Print result in R2 | |
fmt.Printf("R2 (Sum): %s\n", R2) // Print result in R2 | |
}, | |
"HALT": func() { | |
fmt.Println("HALT") | |
}, | |
} | |
var PC int // Program Counter | |
var memory = [256]string{} | |
func fetch() string { | |
if PC >= len(memory) { | |
return "HALT" // Safe fallback to avoid out-of-range error | |
} | |
instruction := memory[PC] | |
PC++ | |
return instruction | |
} | |
func execute(instruction string) { | |
if inst, exists := instructionSet[instruction]; exists { | |
inst() | |
} else { | |
fmt.Println("Unknown instruction:", instruction) | |
} | |
} | |
func main() { | |
// Load a simple program into memory | |
memory[0] = "LOAD_R0" | |
memory[1] = "LOAD_R1" | |
memory[2] = "ADD" | |
memory[3] = "STORE" | |
memory[4] = "HALT" | |
// Execute the program | |
for { | |
instruction := fetch() | |
if instruction == "HALT" { | |
execute(instruction) | |
break | |
} | |
execute(instruction) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment