Option _Explicit

Do Until _ScreenExists: Loop
_Title "Logic Circuits"

Screen _NewImage(800, 600, 32)

Type Wire
    Identity As _Unsigned Long ' Uniquely identifies a connection between two components. (1000-x)
    Pointer As Integer ' Downstream component
    Value As Integer ' Voltage
End Type

Type Junction3
    Species As String ' Type of component
    Identity As _Unsigned Long ' Uniquely identifies component. (1-999)
    A As Wire
    B As Wire
    C As Wire ' Optional, set to 0 if not in use.
End Type

Type Button ' Generalized input
    Species As String ' Type of button
    Identity As _Unsigned Long
    Pointer As Integer ' Connected component
    Value As Integer ' Voltage
End Type

Type Light ' Generalized output
    Species As String ' Type of light
    Identity As _Unsigned Long
    Pointer As Integer ' Connected component
    Value As Integer ' Voltage
End Type

Dim Shared Component(999) As Junction3
Dim Shared Inputs(9) As Button
Dim Shared Outputs(9) As Light
Dim Shared StateHistory(3) As String

Dim Shared ExampleCircuitNumber As Integer
Dim Shared ExampleCircuitName As String

' '''''''''' '''''''''' '''''''''' '''''''''' '''''''''' ''''''''''

ExampleCircuitNumber = 1

Call Initialize
Call LoadCircuit

' Main Loop

Do

    Call RefreshInput

    Cls
    Call PrintCircuit
    Call PrintInput
    Call PrintOutput
    Call PrintStateHistory
    Call DrawGlyph
    Call DrawControls
    _Display


    Call UpdateState

    _Limit 60
Loop

End

' Example circuits

Sub LoadCircuit
    Select Case ExampleCircuitNumber
        Case 1
            ExampleCircuitName = "On/Off Switch"
            Call NewComponent("SPL", 10, 1001, 2001, 2002, -1, 20, 20)
            Call NewComponent("AND", 20, 2001, 2002, 2005, -1, -1, -1)
            Call NewInput(1, "TOG", 1001, 10)
            Call NewOutput(1, "LED", 2005, 20)
        Case 2
            ' --- Latch remembers first time 1 or 2 is pressed. ---
            ExampleCircuitName = "SR Latch"
            Call NewComponent("NOR", 10, 1001, 1002, 1005, -1, -1, 20)
            Call NewComponent("NOR", 30, 3001, 3002, 3005, -1, -1, 40)
            Call NewComponent("SPL", 20, 1005, 3002, 2005, -1, 30, -1)
            Call NewComponent("SPL", 40, 3005, 1002, 4005, -1, 10, -1)
            Call NewInput(1, "PSH", 1001, 10)
            Call NewInput(2, "PSH", 3001, 30)
            Call NewOutput(1, "LED", 2005, 20)
            Call NewOutput(2, "LED", 4005, 40)
        Case 3
            ' --- Inputs 1 and 3 only have an effect when 2 is being held. ---
            ExampleCircuitName = "SR Latch with Enable"
            Call NewComponent("NOR", 10, 1001, 1002, 1005, -1, -1, 20)
            Call NewComponent("SPL", 20, 1005, 3002, 2005, -1, 30, -1)
            Call NewComponent("NOR", 30, 3001, 3002, 3005, -1, -1, 40)
            Call NewComponent("SPL", 40, 3005, 1002, 4005, -1, 10, -1)
            Call NewComponent("AND", 50, 5001, 5002, 1001, -1, -1, 10)
            Call NewComponent("SPL", 60, 6001, 5002, 7002, -1, 50, 70)
            Call NewComponent("AND", 70, 7001, 7002, 3001, -1, -1, 30)
            Call NewInput(1, "PSH", 5001, 50)
            Call NewInput(2, "PSH", 6001, 60)
            Call NewInput(3, "PSH", 7001, 70)
            Call NewOutput(1, "LED", 2005, 20)
            Call NewOutput(2, "LED", 4005, 40)
        Case 4
            ' --- A pulse drives input 2. ---
            ExampleCircuitName = "SR Latch on Pulse"
            Call NewComponent("NOR", 10, 1001, 1002, 1005, -1, -1, 20)
            Call NewComponent("SPL", 20, 1005, 3002, 2005, -1, 30, -1)
            Call NewComponent("NOR", 30, 3001, 3002, 3005, -1, -1, 40)
            Call NewComponent("SPL", 40, 3005, 1002, 4005, -1, 10, -1)
            Call NewComponent("AND", 50, 5001, 5002, 1001, -1, -1, 10)
            Call NewComponent("SPL", 60, 6001, 5002, 7002, -1, 50, 70)
            Call NewComponent("AND", 70, 7001, 7002, 3001, -1, -1, 30)
            Call NewInput(1, "PSH", 5001, 50)
            Call NewInput(2, "PLS", 6001, 60)
            Call NewInput(3, "PSH", 7001, 70)
            Call NewOutput(1, "LED", 2005, 20)
            Call NewOutput(2, "LED", 4005, 40)
        Case 5
            ExampleCircuitName = "D Latch"
            Call NewComponent("NOR", 1, 1001, 1002, 1005, -1, -1, 2)
            Call NewComponent("SPL", 2, 1005, 3002, 2005, -1, 3, -1)
            Call NewComponent("NOR", 3, 3001, 3002, 3005, -1, -1, 4)
            Call NewComponent("SPL", 4, 3005, 1002, 4005, -1, 1, -1)
            Call NewComponent("AND", 5, 5001, 5002, 1001, -1, -1, 1)
            Call NewComponent("SPL", 6, 6001, 5002, 7002, -1, 5, 7)
            Call NewComponent("AND", 7, 7001, 7002, 3001, -1, -1, 3)
            Call NewComponent("SPL", 8, 8001, 7001, 9001, -1, 7, 9)
            Call NewComponent("INV", 9, 9001, 5001, 0, -1, 5, -1)
            Call NewInput(1, "PSH", 8001, 8)
            Call NewInput(2, "PSH", 6001, 6)
            Call NewOutput(1, "LED", 2005, 2)
            Call NewOutput(2, "LED", 4005, 4)
        Case 6
            ExampleCircuitName = "Half Adder"
            Call NewComponent("XOR", 10, 1001, 1002, 1005, -1, -1, -1)
            Call NewComponent("SPL", 20, 2001, 1001, 3001, -1, 10, 30)
            Call NewComponent("AND", 30, 3001, 3002, 3005, -1, -1, -1)
            Call NewComponent("SPL", 40, 4001, 1002, 3002, -1, 10, 30)
            Call NewInput(1, "PSH", 2001, 20)
            Call NewInput(2, "PSH", 4001, 40)
            Call NewOutput(1, "LED", 1005, 10)
            Call NewOutput(2, "LED", 3005, 30)
        Case 7
            ExampleCircuitName = "Full Adder"
            Call NewComponent("XOR", 10, 1001, 1002, 1005, -1, -1, -1)
            Call NewComponent("SPL", 20, 6005, 1001, 4002, -1, 10, 40)
            Call NewComponent("SPL", 30, 3001, 1002, 4001, -1, 10, 40)
            Call NewComponent("AND", 40, 4001, 4002, 5001, -1, -1, 50)
            Call NewComponent("OR", 50, 5001, 5002, 5005, -1, 10, 30)
            Call NewComponent("XOR", 60, 6001, 6002, 6005, -1, -1, 20)
            Call NewComponent("AND", 70, 7001, 7002, 5002, -1, -1, 50)
            Call NewComponent("SPL", 80, 8001, 6001, 7002, -1, 60, 70)
            Call NewComponent("SPL", 90, 9001, 6002, 7001, -1, 60, 70)
            Call NewInput(1, "PSH", 8001, 80) 'n1
            Call NewInput(2, "PSH", 9001, 90) 'n2
            Call NewInput(3, "PSH", 3001, 30) 'carry in
            Call NewOutput(1, "LED", 5005, 50) 'carry out
            Call NewOutput(2, "LED", 1005, 10) 'sum
        Case 8
            ExampleCircuitName = "2 Bit Adder"
            Call NewComponent("XOR", 1, 1001, 1002, 1005, -1, -1, -1)
            Call NewComponent("SPL", 2, 6005, 1001, 4002, -1, 1, 4)
            Call NewComponent("SPL", 3, 3001, 1002, 4001, -1, 1, 4)
            Call NewComponent("AND", 4, 4001, 4002, 5001, -1, -1, 5)
            Call NewComponent("OR", 5, 5001, 5002, 5005, -1, -1, 13)
            Call NewComponent("XOR", 6, 6001, 6002, 6005, -1, -1, 2)
            Call NewComponent("AND", 7, 7001, 7002, 5002, -1, -1, 5)
            Call NewComponent("SPL", 8, 8001, 6001, 7002, -1, 6, 7)
            Call NewComponent("SPL", 9, 9001, 6002, 7001, -1, 6, 7)
            Call NewComponent("XOR", 11, 11001, 11002, 11005, -1, -1, -1)
            Call NewComponent("SPL", 12, 16005, 11001, 14002, -1, 11, 14)
            Call NewComponent("SPL", 13, 5005, 11002, 14001, -1, 11, 14)
            Call NewComponent("AND", 14, 14001, 14002, 15001, -1, -1, 15)
            Call NewComponent("OR", 15, 15001, 15002, 15005, -1, -1, -1)
            Call NewComponent("XOR", 16, 16001, 16002, 16005, -1, -1, 12)
            Call NewComponent("AND", 17, 17001, 17002, 15002, -1, -1, 15)
            Call NewComponent("SPL", 18, 18001, 16001, 17002, -1, 16, 17)
            Call NewComponent("SPL", 19, 19001, 16002, 17001, -1, 16, 17)
            Call NewInput(9, "PSH", 3001, 3) 'carry in
            Call NewInput(1, "TOG", 8001, 8) 'a0
            Call NewInput(2, "TOG", 9001, 9) 'b0
            Call NewInput(3, "TOG", 18001, 18) 'a1
            Call NewInput(4, "TOG", 19001, 19) 'b1
            Call NewOutput(1, "LED", 15005, 15) 'carry out
            Call NewOutput(2, "LED", 11005, 11) 'sum1
            Call NewOutput(3, "LED", 1005, 1) 'sum0
        Case 9
            ExampleCircuitName = "3 Bit Adder"
            Call NewComponent("XOR", 1, 1001, 1002, 1005, -1, -1, -1)
            Call NewComponent("SPL", 2, 6005, 1001, 4002, -1, 1, 4)
            Call NewComponent("SPL", 3, 3001, 1002, 4001, -1, 1, 4)
            Call NewComponent("AND", 4, 4001, 4002, 5001, -1, -1, 5)
            Call NewComponent("OR", 5, 5001, 5002, 5005, -1, -1, 13)
            Call NewComponent("XOR", 6, 6001, 6002, 6005, -1, -1, 2)
            Call NewComponent("AND", 7, 7001, 7002, 5002, -1, -1, 5)
            Call NewComponent("SPL", 8, 8001, 6001, 7002, -1, 6, 7)
            Call NewComponent("SPL", 9, 9001, 6002, 7001, -1, 6, 7)
            Call NewComponent("XOR", 11, 11001, 11002, 11005, -1, -1, -1)
            Call NewComponent("SPL", 12, 16005, 11001, 14002, -1, 11, 14)
            Call NewComponent("SPL", 13, 5005, 11002, 14001, -1, 11, 14)
            Call NewComponent("AND", 14, 14001, 14002, 15001, -1, -1, 15)
            Call NewComponent("OR", 15, 15001, 15002, 15005, -1, -1, 23)
            Call NewComponent("XOR", 16, 16001, 16002, 16005, -1, -1, 12)
            Call NewComponent("AND", 17, 17001, 17002, 15002, -1, -1, 15)
            Call NewComponent("SPL", 18, 18001, 16001, 17002, -1, 16, 17)
            Call NewComponent("SPL", 19, 19001, 16002, 17001, -1, 16, 17)
            Call NewComponent("XOR", 21, 21001, 21002, 21005, -1, -1, -1)
            Call NewComponent("SPL", 22, 26005, 21001, 24002, -1, 21, 24)
            Call NewComponent("SPL", 23, 15005, 21002, 24001, -1, 21, 24)
            Call NewComponent("AND", 24, 24001, 24002, 25001, -1, -1, 25)
            Call NewComponent("OR", 25, 25001, 25002, 25005, -1, -1, -1)
            Call NewComponent("XOR", 26, 26001, 26002, 26005, -1, -1, 22)
            Call NewComponent("AND", 27, 27001, 27002, 25002, -1, -1, 25)
            Call NewComponent("SPL", 28, 28001, 26001, 27002, -1, 26, 27)
            Call NewComponent("SPL", 29, 29001, 26002, 27001, -1, 26, 27)
            Call NewInput(9, "PSH", 3001, 3) 'carry in
            Call NewInput(1, "TOG", 8001, 8) 'a0
            Call NewInput(2, "TOG", 9001, 9) 'b0
            Call NewInput(3, "TOG", 18001, 18) 'a1
            Call NewInput(4, "TOG", 19001, 19) 'b1
            Call NewInput(5, "TOG", 28001, 28) 'a2
            Call NewInput(6, "TOG", 29001, 29) 'b2
            Call NewOutput(1, "LED", 25005, 25) 'carry out
            Call NewOutput(2, "LED", 21005, 21) 'sum2
            Call NewOutput(3, "LED", 11005, 11) 'sum1
            Call NewOutput(4, "LED", 1005, 1) 'sum0
        Case 10
            ExampleCircuitName = "4 Bit Adder"
            Call NewComponent("XOR", 1, 1001, 1002, 1005, -1, -1, -1)
            Call NewComponent("SPL", 2, 6005, 1001, 4002, -1, 1, 4)
            Call NewComponent("SPL", 3, 3001, 1002, 4001, -1, 1, 4)
            Call NewComponent("AND", 4, 4001, 4002, 5001, -1, -1, 5)
            Call NewComponent("OR", 5, 5001, 5002, 5005, -1, -1, 13)
            Call NewComponent("XOR", 6, 6001, 6002, 6005, -1, -1, 2)
            Call NewComponent("AND", 7, 7001, 7002, 5002, -1, -1, 5)
            Call NewComponent("SPL", 8, 8001, 6001, 7002, -1, 6, 7)
            Call NewComponent("SPL", 9, 9001, 6002, 7001, -1, 6, 7)
            Call NewComponent("XOR", 11, 11001, 11002, 11005, -1, -1, -1)
            Call NewComponent("SPL", 12, 16005, 11001, 14002, -1, 11, 14)
            Call NewComponent("SPL", 13, 5005, 11002, 14001, -1, 11, 14)
            Call NewComponent("AND", 14, 14001, 14002, 15001, -1, -1, 15)
            Call NewComponent("OR", 15, 15001, 15002, 15005, -1, -1, 23)
            Call NewComponent("XOR", 16, 16001, 16002, 16005, -1, -1, 12)
            Call NewComponent("AND", 17, 17001, 17002, 15002, -1, -1, 15)
            Call NewComponent("SPL", 18, 18001, 16001, 17002, -1, 16, 17)
            Call NewComponent("SPL", 19, 19001, 16002, 17001, -1, 16, 17)
            Call NewComponent("XOR", 21, 21001, 21002, 21005, -1, -1, -1)
            Call NewComponent("SPL", 22, 26005, 21001, 24002, -1, 21, 24)
            Call NewComponent("SPL", 23, 15005, 21002, 24001, -1, 21, 24)
            Call NewComponent("AND", 24, 24001, 24002, 25001, -1, -1, 25)
            Call NewComponent("OR", 25, 25001, 25002, 25005, -1, -1, 33)
            Call NewComponent("XOR", 26, 26001, 26002, 26005, -1, -1, 22)
            Call NewComponent("AND", 27, 27001, 27002, 25002, -1, -1, 25)
            Call NewComponent("SPL", 28, 28001, 26001, 27002, -1, 26, 27)
            Call NewComponent("SPL", 29, 29001, 26002, 27001, -1, 26, 27)
            Call NewComponent("XOR", 31, 31001, 31002, 31005, -1, -1, -1)
            Call NewComponent("SPL", 32, 36005, 31001, 34002, -1, 31, 34)
            Call NewComponent("SPL", 33, 25005, 31002, 34001, -1, 31, 34)
            Call NewComponent("AND", 34, 34001, 34002, 35001, -1, -1, 35)
            Call NewComponent("OR", 35, 35001, 35002, 35005, -1, -1, -1)
            Call NewComponent("XOR", 36, 36001, 36002, 36005, -1, -1, 32)
            Call NewComponent("AND", 37, 37001, 37002, 35002, -1, -1, 35)
            Call NewComponent("SPL", 38, 38001, 36001, 37002, -1, 36, 37)
            Call NewComponent("SPL", 39, 39001, 36002, 37001, -1, 36, 37)
            Call NewInput(9, "PSH", 3001, 3) 'carry in
            Call NewInput(1, "TOG", 8001, 8) 'a0
            Call NewInput(2, "TOG", 9001, 9) 'b0
            Call NewInput(3, "TOG", 18001, 18) 'a1
            Call NewInput(4, "TOG", 19001, 19) 'b1
            Call NewInput(5, "TOG", 28001, 28) 'a2
            Call NewInput(6, "TOG", 29001, 29) 'b2
            Call NewInput(7, "TOG", 38001, 38) 'a3
            Call NewInput(8, "TOG", 39001, 39) 'b3
            Call NewOutput(1, "LED", 35005, 35) 'carry out
            Call NewOutput(2, "LED", 31005, 31) 'sum3
            Call NewOutput(3, "LED", 21005, 21) 'sum2
            Call NewOutput(4, "LED", 11005, 11) 'sum1
            Call NewOutput(5, "LED", 1005, 1) 'sum0
        Case 11
            ExampleCircuitName = "Edge Detector"
            Call NewComponent("SPL", 1, 1001, 2002, 3001, -1, 2, 3)
            Call NewComponent("INV", 2, 2002, 4001, 0, -1, 4, -1)
            Call NewComponent("AND", 3, 3001, 3002, 3010, -1, -1, -1)
            Call NewComponent("INV", 4, 4001, 5001, 0, -1, 5, -1) ' pair of extra inverters
            Call NewComponent("INV", 5, 5001, 3002, 0, -1, 3, -1) ' (must be a pair)
            Call NewInput(1, "PSH", 1001, 1)
            Call NewOutput(1, "LED", 3010, 3)
        Case 12
            ExampleCircuitName = "D Flip-Flop"
            Call NewComponent("NOR", 1, 1001, 1002, 1005, -1, -1, 2)
            Call NewComponent("SPL", 2, 1005, 3002, 2005, -1, 3, -1)
            Call NewComponent("NOR", 3, 3001, 3002, 3005, -1, -1, 4)
            Call NewComponent("SPL", 4, 3005, 1002, 4005, -1, 1, -1)
            Call NewComponent("AND", 5, 5001, 5002, 1001, -1, -1, 1)
            Call NewComponent("SPL", 6, 6001, 5002, 7002, -1, 5, 7)
            Call NewComponent("AND", 7, 7001, 7002, 3001, -1, -1, 3)
            Call NewComponent("SPL", 8, 8001, 7001, 9001, -1, 7, 9)
            Call NewComponent("INV", 9, 9001, 5001, 0, -1, 5, -1)
            Call NewComponent("SPL", 11, 11001, 12002, 13001, -1, 12, 13)
            Call NewComponent("INV", 12, 12002, 14001, 0, -1, 14, -1)
            Call NewComponent("AND", 13, 13001, 13002, 6001, -1, -1, 6)
            Call NewComponent("INV", 14, 14001, 15001, 0, -1, 15, -1) ' pair of extra inverters
            Call NewComponent("INV", 15, 15001, 13002, 0, -1, 13, -1) ' (must be a pair)
            Call NewInput(1, "PSH", 8001, 8) ' data
            Call NewInput(2, "PSH", 11001, 11) ' enable
            Call NewOutput(1, "LED", 2005, 2)
            Call NewOutput(2, "LED", 4005, 4) ' often omitted for clarity
        Case 13
            ExampleCircuitName = "1 Bit Counter"
            Call NewComponent("NOR", 1, 1001, 1002, 1005, -1, -1, 2)
            Call NewComponent("SPL", 2, 1005, 3002, 2005, -1, 3, -1)
            Call NewComponent("NOR", 3, 3001, 3002, 3005, -1, -1, 4)
            Call NewComponent("SPL", 4, 3005, 1002, 8001, -1, 1, 8)
            Call NewComponent("AND", 5, 5001, 5002, 1001, -1, -1, 1)
            Call NewComponent("SPL", 6, 6001, 5002, 7002, -1, 5, 7)
            Call NewComponent("AND", 7, 7001, 7002, 3001, -1, -1, 3)
            Call NewComponent("SPL", 8, 8001, 7001, 9001, -1, 7, 9)
            Call NewComponent("INV", 9, 9001, 5001, 0, -1, 5, -1)
            Call NewComponent("SPL", 11, 11001, 12002, 13001, -1, 12, 13)
            Call NewComponent("INV", 12, 12002, 14001, 0, -1, 14, -1)
            Call NewComponent("AND", 13, 13001, 13002, 6001, -1, -1, 6)
            Call NewComponent("INV", 14, 14001, 15001, 0, -1, 15, -1)
            Call NewComponent("INV", 15, 15001, 13002, 0, -1, 13, -1)
            Call NewInput(1, "PSH", 11001, 11)
            Call NewOutput(1, "LED", 8001, 8)
        Case 14
            ExampleCircuitName = "2 Bit Counter"
            Call NewComponent("NOR", 1, 1001, 1002, 1005, -1, -1, 2)
            Call NewComponent("SPL", 2, 1005, 3002, 31001, -1, 3, 31)
            Call NewComponent("NOR", 3, 3001, 3002, 3005, -1, -1, 4)
            Call NewComponent("SPL", 4, 3005, 1002, 8001, -1, 1, 8)
            Call NewComponent("AND", 5, 5001, 5002, 1001, -1, -1, 1)
            Call NewComponent("SPL", 6, 6001, 5002, 7002, -1, 5, 7)
            Call NewComponent("AND", 7, 7001, 7002, 3001, -1, -1, 3)
            Call NewComponent("SPL", 8, 8001, 7001, 9001, -1, 7, 9)
            Call NewComponent("INV", 9, 9001, 5001, 0, -1, 5, -1)
            Call NewComponent("SPL", 11, 11001, 12002, 13001, -1, 12, 13)
            Call NewComponent("INV", 12, 12002, 14001, 0, -1, 14, -1)
            Call NewComponent("AND", 13, 13001, 13002, 6001, -1, -1, 6)
            Call NewComponent("INV", 14, 14001, 15001, 0, -1, 15, -1)
            Call NewComponent("INV", 15, 15001, 13002, 0, -1, 13, -1)
            Call NewComponent("NOR", 21, 21001, 21002, 21005, -1, -1, 22)
            Call NewComponent("SPL", 22, 21005, 23002, 22005, -1, 23, -1)
            Call NewComponent("NOR", 23, 23001, 23002, 23005, -1, -1, 24)
            Call NewComponent("SPL", 24, 23005, 21002, 28001, -1, 21, 28)
            Call NewComponent("AND", 25, 25001, 25002, 21001, -1, -1, 21)
            Call NewComponent("SPL", 26, 26001, 25002, 27002, -1, 25, 27)
            Call NewComponent("AND", 27, 27001, 27002, 23001, -1, -1, 23)
            Call NewComponent("SPL", 28, 28001, 27001, 29001, -1, 27, 29)
            Call NewComponent("INV", 29, 29001, 25001, 0, -1, 25, -1)
            Call NewComponent("SPL", 31, 31001, 32002, 33001, -1, 32, 33)
            Call NewComponent("INV", 32, 32002, 34001, 0, -1, 34, -1)
            Call NewComponent("AND", 33, 33001, 33002, 26001, -1, -1, 26)
            Call NewComponent("INV", 34, 34001, 35001, 0, -1, 35, -1)
            Call NewComponent("INV", 35, 35001, 33002, 0, -1, 33, -1)
            Call NewInput(1, "PSH", 11001, 11)
            Call NewOutput(1, "LED", 8001, 8)
            Call NewOutput(2, "LED", 28001, 28)
        Case 15
            ExampleCircuitName = "3 Bit Counter"
            Call NewComponent("NOR", 1, 1001, 1002, 1005, -1, -1, 2)
            Call NewComponent("SPL", 2, 1005, 3002, 31001, -1, 3, 31)
            Call NewComponent("NOR", 3, 3001, 3002, 3005, -1, -1, 4)
            Call NewComponent("SPL", 4, 3005, 1002, 8001, -1, 1, 8)
            Call NewComponent("AND", 5, 5001, 5002, 1001, -1, -1, 1)
            Call NewComponent("SPL", 6, 6001, 5002, 7002, -1, 5, 7)
            Call NewComponent("AND", 7, 7001, 7002, 3001, -1, -1, 3)
            Call NewComponent("SPL", 8, 8001, 7001, 9001, -1, 7, 9)
            Call NewComponent("INV", 9, 9001, 5001, 0, -1, 5, -1)
            Call NewComponent("SPL", 11, 11001, 12002, 13001, -1, 12, 13)
            Call NewComponent("INV", 12, 12002, 14001, 0, -1, 14, -1)
            Call NewComponent("AND", 13, 13001, 13002, 6001, -1, -1, 6)
            Call NewComponent("INV", 14, 14001, 15001, 0, -1, 15, -1)
            Call NewComponent("INV", 15, 15001, 13002, 0, -1, 13, -1)
            Call NewComponent("NOR", 21, 21001, 21002, 21005, -1, -1, 22)
            Call NewComponent("SPL", 22, 21005, 23002, 51001, -1, 23, 51)
            Call NewComponent("NOR", 23, 23001, 23002, 23005, -1, -1, 24)
            Call NewComponent("SPL", 24, 23005, 21002, 28001, -1, 21, 28)
            Call NewComponent("AND", 25, 25001, 25002, 21001, -1, -1, 21)
            Call NewComponent("SPL", 26, 26001, 25002, 27002, -1, 25, 27)
            Call NewComponent("AND", 27, 27001, 27002, 23001, -1, -1, 23)
            Call NewComponent("SPL", 28, 28001, 27001, 29001, -1, 27, 29)
            Call NewComponent("INV", 29, 29001, 25001, 0, -1, 25, -1)
            Call NewComponent("SPL", 31, 31001, 32002, 33001, -1, 32, 33)
            Call NewComponent("INV", 32, 32002, 34001, 0, -1, 34, -1)
            Call NewComponent("AND", 33, 33001, 33002, 26001, -1, -1, 26)
            Call NewComponent("INV", 34, 34001, 35001, 0, -1, 35, -1)
            Call NewComponent("INV", 35, 35001, 33002, 0, -1, 33, -1)
            Call NewComponent("NOR", 41, 41001, 41002, 41005, -1, -1, 42)
            Call NewComponent("SPL", 42, 41005, 43002, 42005, -1, 43, -1)
            Call NewComponent("NOR", 43, 43001, 43002, 43005, -1, -1, 44)
            Call NewComponent("SPL", 44, 43005, 41002, 48001, -1, 41, 48)
            Call NewComponent("AND", 45, 45001, 45002, 41001, -1, -1, 41)
            Call NewComponent("SPL", 46, 46001, 45002, 47002, -1, 45, 47)
            Call NewComponent("AND", 47, 47001, 47002, 43001, -1, -1, 43)
            Call NewComponent("SPL", 48, 48001, 47001, 49001, -1, 47, 49)
            Call NewComponent("INV", 49, 49001, 45001, 0, -1, 45, -1)
            Call NewComponent("SPL", 51, 51001, 52002, 53001, -1, 52, 53)
            Call NewComponent("INV", 52, 52002, 54001, 0, -1, 54, -1)
            Call NewComponent("AND", 53, 53001, 53002, 46001, -1, -1, 46)
            Call NewComponent("INV", 54, 54001, 55001, 0, -1, 55, -1)
            Call NewComponent("INV", 55, 55001, 53002, 0, -1, 53, -1)
            Call NewInput(1, "PSH", 11001, 11)
            Call NewOutput(1, "LED", 8001, 8)
            Call NewOutput(2, "LED", 28001, 28)
            Call NewOutput(3, "LED", 48001, 48)
        Case 16
            ExampleCircuitName = "4 Bit Counter"
            Call NewComponent("NOR", 1, 1001, 1002, 1005, -1, -1, 2)
            Call NewComponent("SPL", 2, 1005, 3002, 31001, -1, 3, 31)
            Call NewComponent("NOR", 3, 3001, 3002, 3005, -1, -1, 4)
            Call NewComponent("SPL", 4, 3005, 1002, 8001, -1, 1, 8)
            Call NewComponent("AND", 5, 5001, 5002, 1001, -1, -1, 1)
            Call NewComponent("SPL", 6, 6001, 5002, 7002, -1, 5, 7)
            Call NewComponent("AND", 7, 7001, 7002, 3001, -1, -1, 3)
            Call NewComponent("SPL", 8, 8001, 7001, 9001, -1, 7, 9)
            Call NewComponent("INV", 9, 9001, 5001, 0, -1, 5, -1)
            Call NewComponent("SPL", 11, 11001, 12002, 13001, -1, 12, 13)
            Call NewComponent("INV", 12, 12002, 14001, 0, -1, 14, -1)
            Call NewComponent("AND", 13, 13001, 13002, 6001, -1, -1, 6)
            Call NewComponent("INV", 14, 14001, 15001, 0, -1, 15, -1)
            Call NewComponent("INV", 15, 15001, 13002, 0, -1, 13, -1)
            Call NewComponent("NOR", 21, 21001, 21002, 21005, -1, -1, 22)
            Call NewComponent("SPL", 22, 21005, 23002, 51001, -1, 23, 51)
            Call NewComponent("NOR", 23, 23001, 23002, 23005, -1, -1, 24)
            Call NewComponent("SPL", 24, 23005, 21002, 28001, -1, 21, 28)
            Call NewComponent("AND", 25, 25001, 25002, 21001, -1, -1, 21)
            Call NewComponent("SPL", 26, 26001, 25002, 27002, -1, 25, 27)
            Call NewComponent("AND", 27, 27001, 27002, 23001, -1, -1, 23)
            Call NewComponent("SPL", 28, 28001, 27001, 29001, -1, 27, 29)
            Call NewComponent("INV", 29, 29001, 25001, 0, -1, 25, -1)
            Call NewComponent("SPL", 31, 31001, 32002, 33001, -1, 32, 33)
            Call NewComponent("INV", 32, 32002, 34001, 0, -1, 34, -1)
            Call NewComponent("AND", 33, 33001, 33002, 26001, -1, -1, 26)
            Call NewComponent("INV", 34, 34001, 35001, 0, -1, 35, -1)
            Call NewComponent("INV", 35, 35001, 33002, 0, -1, 33, -1)
            Call NewComponent("NOR", 41, 41001, 41002, 41005, -1, -1, 42)
            Call NewComponent("SPL", 42, 41005, 43002, 71001, -1, 43, 71)
            Call NewComponent("NOR", 43, 43001, 43002, 43005, -1, -1, 44)
            Call NewComponent("SPL", 44, 43005, 41002, 48001, -1, 41, 48)
            Call NewComponent("AND", 45, 45001, 45002, 41001, -1, -1, 41)
            Call NewComponent("SPL", 46, 46001, 45002, 47002, -1, 45, 47)
            Call NewComponent("AND", 47, 47001, 47002, 43001, -1, -1, 43)
            Call NewComponent("SPL", 48, 48001, 47001, 49001, -1, 47, 49)
            Call NewComponent("INV", 49, 49001, 45001, 0, -1, 45, -1)
            Call NewComponent("SPL", 51, 51001, 52002, 53001, -1, 52, 53)
            Call NewComponent("INV", 52, 52002, 54001, 0, -1, 54, -1)
            Call NewComponent("AND", 53, 53001, 53002, 46001, -1, -1, 46)
            Call NewComponent("INV", 54, 54001, 55001, 0, -1, 55, -1)
            Call NewComponent("INV", 55, 55001, 53002, 0, -1, 53, -1)
            Call NewComponent("NOR", 61, 61001, 61002, 61005, -1, -1, 62)
            Call NewComponent("SPL", 62, 61005, 63002, 62005, -1, 63, -1)
            Call NewComponent("NOR", 63, 63001, 63002, 63005, -1, -1, 64)
            Call NewComponent("SPL", 64, 63005, 61002, 68001, -1, 61, 68)
            Call NewComponent("AND", 65, 65001, 65002, 61001, -1, -1, 61)
            Call NewComponent("SPL", 66, 66001, 65002, 67002, -1, 65, 67)
            Call NewComponent("AND", 67, 67001, 67002, 63001, -1, -1, 63)
            Call NewComponent("SPL", 68, 68001, 67001, 69001, -1, 67, 69)
            Call NewComponent("INV", 69, 69001, 65001, 0, -1, 65, -1)
            Call NewComponent("SPL", 71, 71001, 72002, 73001, -1, 72, 73)
            Call NewComponent("INV", 72, 72002, 74001, 0, -1, 74, -1)
            Call NewComponent("AND", 73, 73001, 73002, 66001, -1, -1, 66)
            Call NewComponent("INV", 74, 74001, 75001, 0, -1, 75, -1)
            Call NewComponent("INV", 75, 75001, 73002, 0, -1, 73, -1)
            Call NewInput(1, "PLS", 11001, 11)
            Call NewOutput(1, "LED", 8001, 8)
            Call NewOutput(2, "LED", 28001, 28)
            Call NewOutput(3, "LED", 48001, 48)
            Call NewOutput(4, "LED", 68001, 68)
        Case Else
            ExampleCircuitName = ""
    End Select
End Sub

' '''''''''' '''''''''' '''''''''' '''''''''' '''''''''' ''''''''''

' Subs and Functions

Sub Initialize
    Dim k As Integer
    For k = 1 To UBound(Component)
        Component(k).Species = ""
        Component(k).Identity = 0
        Component(k).A.Identity = 0
        Component(k).B.Identity = 0
        Component(k).C.Identity = 0
        Component(k).A.Pointer = 0
        Component(k).B.Pointer = 0
        Component(k).C.Pointer = 0
        Component(k).A.Value = 0
        Component(k).B.Value = 0
        Component(k).C.Value = 0
    Next
    For k = 1 To UBound(Inputs)
        Inputs(k).Species = ""
        Inputs(k).Identity = 0
        Inputs(k).Pointer = 0
        Inputs(k).Value = 0
    Next
    For k = 1 To UBound(Outputs)
        Outputs(k).Species = ""
        Outputs(k).Identity = 0
        Outputs(k).Pointer = 0
        Outputs(k).Value = 0
    Next
    For k = 1 To UBound(StateHistory)
        StateHistory(k) = ""
    Next
End Sub

Sub Ground
    Dim k As Integer
    For k = 1 To UBound(Component)
        Component(k).A.Value = 0
        Component(k).B.Value = 0
        Component(k).C.Value = 0
    Next
    For k = 1 To UBound(Inputs)
        Inputs(k).Value = 0
    Next
    For k = 1 To UBound(Outputs)
        Outputs(k).Value = 0
    Next
End Sub

Sub NewComponent (TheSpecies As String, TheIdentity As _Unsigned Long, WireA.Id As _Unsigned Long, WireB.Id As _Unsigned Long, WireC.Id As _Unsigned Long, WireA.Po As Integer, WireB.Po As Integer, WireC.Po As Integer)
    Component(TheIdentity).Species = TheSpecies
    Component(TheIdentity).Identity = TheIdentity
    Component(TheIdentity).A.Identity = WireA.Id
    Component(TheIdentity).B.Identity = WireB.Id
    Component(TheIdentity).C.Identity = WireC.Id
    Component(TheIdentity).A.Pointer = WireA.Po
    Component(TheIdentity).B.Pointer = WireB.Po
    Component(TheIdentity).C.Pointer = WireC.Po
End Sub

Sub NewInput (TheIndex As Integer, TheSpecies As String, TheIdentity As _Unsigned Long, ThePointer As Integer)
    Inputs(TheIndex).Species = TheSpecies
    Inputs(TheIndex).Identity = TheIdentity
    Inputs(TheIndex).Pointer = ThePointer
End Sub

Sub NewOutput (TheIndex As Integer, TheSpecies As String, TheIdentity As _Unsigned Long, ThePointer As Integer)
    Outputs(TheIndex).Species = TheSpecies
    Outputs(TheIndex).Identity = TheIdentity
    Outputs(TheIndex).Pointer = ThePointer
End Sub

Sub RefreshInput
    Dim As Integer h, i, j, k, q
    Dim As Double t
    h = _KeyHit

    For i = 48 + 1 To 48 + UBound(Inputs)
        k = i - 48
        Select Case Inputs(k).Species
            Case ""
                ' Unused
            Case "PSH" ' PSHingy button
                If (_KeyDown(i) <> 0) Then
                    q = 1
                Else
                    q = 0
                End If
                j = Inputs(k).Pointer
                If (Component(j).A.Identity = Inputs(k).Identity) Then Component(j).A.Value = q
                If (Component(j).B.Identity = Inputs(k).Identity) Then Component(j).B.Value = q
                If (Component(j).C.Identity = Inputs(k).Identity) Then Component(j).C.Value = q
            Case "TOG" ' Sticky button
                If (h = i) Then
                    j = Inputs(k).Pointer
                    If (Inputs(k).Identity = Component(j).A.Identity) Then
                        q = Component(j).A.Value
                        If (q = 0) Then Component(j).A.Value = 1 Else Component(j).A.Value = 0
                    End If
                    If (Inputs(k).Identity = Component(j).B.Identity) Then
                        q = Component(j).B.Value
                        If (q = 0) Then Component(j).B.Value = 1 Else Component(j).B.Value = 0
                    End If
                    If (Inputs(k).Identity = Component(j).C.Identity) Then
                        q = Component(j).C.Value
                        If (q = 0) Then Component(j).C.Value = 1 Else Component(j).C.Value = 0
                    End If
                End If
            Case "PLS" ' Pulse
                t = Timer
                t = t - Int(t)
                If (t >= .66) Then q = 1 Else q = 0
                If (k <= UBound(Inputs)) Then
                    j = Inputs(k).Pointer
                    If (j <> -1) Then
                        If (Component(j).A.Identity = Inputs(k).Identity) Then Component(j).A.Value = q
                        If (Component(j).B.Identity = Inputs(k).Identity) Then Component(j).B.Value = q
                        If (Component(j).C.Identity = Inputs(k).Identity) Then Component(j).C.Value = q
                    End If
                End If
            Case Else
                Beep
                End
        End Select
    Next

    If (h = 19712) Then ' rightarrow
        ExampleCircuitNumber = ExampleCircuitNumber + 1
        Call Initialize
        Call LoadCircuit
    End If

    If (h = 19200) Then ' leftarrow
        ExampleCircuitNumber = ExampleCircuitNumber - 1
        If (ExampleCircuitNumber < 1) Then ExampleCircuitNumber = 1
        Call Initialize
        Call LoadCircuit
    End If

    If (h = 48) Then ' 0
        Call Ground
    End If

    _KeyClear
End Sub

Sub UpdateState
    Dim As Integer j, k
    Dim As _Unsigned Long g
    For k = 1 To UBound(Component)
        Select Case Component(k).Species
            Case ""
                ' Unused
            Case "AND"
                ' Update self
                Component(k).C.Value = BinaryAND(Component(k).A.Value, Component(k).B.Value)
                ' Update downstream
                g = Component(k).C.Identity
                j = Component(k).C.Pointer
                If (j <> -1) Then
                    If (Component(j).A.Identity = g) Then Component(j).A.Value = Component(k).C.Value
                    If (Component(j).B.Identity = g) Then Component(j).B.Value = Component(k).C.Value
                    If (Component(j).C.Identity = g) Then Component(j).C.Value = Component(k).C.Value
                End If
            Case "INV"
                ' Update self
                Select Case Component(k).A.Value
                    Case 0
                        Component(k).B.Value = 1
                    Case 1
                        Component(k).B.Value = 0
                End Select
                ' Update downstream
                g = Component(k).B.Identity
                j = Component(k).B.Pointer
                If (j <> -1) Then
                    If (Component(j).A.Identity = g) Then Component(j).A.Value = Component(k).B.Value
                    If (Component(j).B.Identity = g) Then Component(j).B.Value = Component(k).B.Value
                    If (Component(j).C.Identity = g) Then Component(j).C.Value = Component(k).B.Value
                End If
            Case "NOR"
                ' Update self
                Component(k).C.Value = BinaryNOR(Component(k).A.Value, Component(k).B.Value)
                ' Update downstream
                g = Component(k).C.Identity
                j = Component(k).C.Pointer
                If (j <> -1) Then
                    If (Component(j).A.Identity = g) Then Component(j).A.Value = Component(k).C.Value
                    If (Component(j).B.Identity = g) Then Component(j).B.Value = Component(k).C.Value
                    If (Component(j).C.Identity = g) Then Component(j).C.Value = Component(k).C.Value
                End If
            Case "OR"
                ' Update self
                Component(k).C.Value = BinaryOR(Component(k).A.Value, Component(k).B.Value)
                ' Update downstream
                g = Component(k).C.Identity
                j = Component(k).C.Pointer
                If (j <> -1) Then
                    If (Component(j).A.Identity = g) Then Component(j).A.Value = Component(k).C.Value
                    If (Component(j).B.Identity = g) Then Component(j).B.Value = Component(k).C.Value
                    If (Component(j).C.Identity = g) Then Component(j).C.Value = Component(k).C.Value
                End If
            Case "SPL"
                ' Update self
                Component(k).B.Value = Component(k).A.Value
                Component(k).C.Value = Component(k).A.Value
                ' Update downstream
                g = Component(k).B.Identity
                j = Component(k).B.Pointer
                If (j <> -1) Then
                    If (Component(j).A.Identity = g) Then Component(j).A.Value = Component(k).B.Value
                    If (Component(j).B.Identity = g) Then Component(j).B.Value = Component(k).B.Value
                    If (Component(j).C.Identity = g) Then Component(j).C.Value = Component(k).B.Value
                End If
                g = Component(k).C.Identity
                j = Component(k).C.Pointer
                If (j <> -1) Then
                    If (Component(j).A.Identity = g) Then Component(j).A.Value = Component(k).C.Value
                    If (Component(j).B.Identity = g) Then Component(j).B.Value = Component(k).C.Value
                    If (Component(j).C.Identity = g) Then Component(j).C.Value = Component(k).C.Value
                End If
            Case "XOR"
                ' Update self
                Component(k).C.Value = BinaryXOR(Component(k).A.Value, Component(k).B.Value)
                ' Update downstream
                g = Component(k).C.Identity
                j = Component(k).C.Pointer
                If (j <> -1) Then
                    If (Component(j).A.Identity = g) Then Component(j).A.Value = Component(k).C.Value
                    If (Component(j).B.Identity = g) Then Component(j).B.Value = Component(k).C.Value
                    If (Component(j).C.Identity = g) Then Component(j).C.Value = Component(k).C.Value
                End If
            Case Else
                Beep
                End
        End Select
    Next
End Sub

Function BinaryAND (a As Integer, b As Integer)
    Dim TheReturn As Integer
    If ((a = 0) And (b = 0)) Then TheReturn = 0
    If ((a = 0) And (b = 1)) Then TheReturn = 0
    If ((a = 1) And (b = 0)) Then TheReturn = 0
    If ((a = 1) And (b = 1)) Then TheReturn = 1
    BinaryAND = TheReturn
End Function

Function BinaryNAND (a As Integer, b As Integer)
    Dim TheReturn As Integer
    If ((a = 0) And (b = 0)) Then TheReturn = 1
    If ((a = 0) And (b = 1)) Then TheReturn = 1
    If ((a = 1) And (b = 0)) Then TheReturn = 1
    If ((a = 1) And (b = 1)) Then TheReturn = 0
    BinaryNAND = TheReturn
End Function

Function BinaryNOR (a As Integer, b As Integer)
    Dim TheReturn As Integer
    If ((a = 0) And (b = 0)) Then TheReturn = 1
    If ((a = 0) And (b = 1)) Then TheReturn = 0
    If ((a = 1) And (b = 0)) Then TheReturn = 0
    If ((a = 1) And (b = 1)) Then TheReturn = 0
    BinaryNOR = TheReturn
End Function

Function BinaryOR (a As Integer, b As Integer)
    Dim TheReturn As Integer
    If ((a = 0) And (b = 0)) Then TheReturn = 0
    If ((a = 0) And (b = 1)) Then TheReturn = 1
    If ((a = 1) And (b = 0)) Then TheReturn = 1
    If ((a = 1) And (b = 1)) Then TheReturn = 1
    BinaryOR = TheReturn
End Function

Function BinaryXOR (a As Integer, b As Integer)
    Dim TheReturn As Integer
    If ((a = 0) And (b = 0)) Then TheReturn = 0
    If ((a = 0) And (b = 1)) Then TheReturn = 1
    If ((a = 1) And (b = 0)) Then TheReturn = 1
    If ((a = 1) And (b = 1)) Then TheReturn = 0
    BinaryXOR = TheReturn
End Function

Sub PrintCircuit
    Dim k, n As Integer
    Print "Example " + _Trim$(Str$(ExampleCircuitNumber)) + ": "; ExampleCircuitName
    Print
    For k = 1 To UBound(Component)
        If (Component(k).Species <> "") Then
            n = n + 1
            Print "["; _Trim$(Str$(k)); "] "; Component(k).Species; Component(k).A.Identity; Component(k).B.Identity; Component(k).C.Identity; Component(k).A.Pointer; Component(k).B.Pointer; Component(k).C.Pointer
        End If
        If (n = 21) Then
            Print "..."
            Exit For
        End If
    Next
    If (n < 21) Then
        For k = n To 21
            Print
        Next
    End If
    Print
End Sub

Sub PrintInput
    Dim k As Integer
    Dim As String InputRep
    InputRep = ""
    Print "Input: "
    For k = 1 To UBound(Inputs)
        If (Inputs(k).Species <> "") Then
            If (Inputs(k).Identity = Component(Inputs(k).Pointer).A.Identity) Then
                If Inputs(k).Species = "PSH" Then
                    InputRep = InputRep + "(" + _Trim$(Str$(Inputs(k).Pointer)) + ":" + _Trim$(Str$(Inputs(k).Identity)) + ")" + _Trim$(Str$(Component(Inputs(k).Pointer).A.Value)) + " "
                End If
                If Inputs(k).Species = "TOG" Then
                    InputRep = InputRep + "[" + _Trim$(Str$(Inputs(k).Pointer)) + ":" + _Trim$(Str$(Inputs(k).Identity)) + "]" + _Trim$(Str$(Component(Inputs(k).Pointer).B.Value)) + " "
                End If
                If Inputs(k).Species = "PLS" Then
                    InputRep = InputRep + "{" + _Trim$(Str$(Inputs(k).Pointer)) + ":" + _Trim$(Str$(Inputs(k).Identity)) + "}" + _Trim$(Str$(Component(Inputs(k).Pointer).C.Value)) + " "
                End If
            End If
            If (Inputs(k).Identity = Component(Inputs(k).Pointer).B.Identity) Then
                If Inputs(k).Species = "PSH" Then
                    InputRep = InputRep + "(" + _Trim$(Str$(Inputs(k).Pointer)) + ":" + _Trim$(Str$(Inputs(k).Identity)) + ")" + _Trim$(Str$(Component(Inputs(k).Pointer).A.Value)) + " "
                End If
                If Inputs(k).Species = "TOG" Then
                    InputRep = InputRep + "[" + _Trim$(Str$(Inputs(k).Pointer)) + ":" + _Trim$(Str$(Inputs(k).Identity)) + "]" + _Trim$(Str$(Component(Inputs(k).Pointer).B.Value)) + " "
                End If
                If Inputs(k).Species = "PLS" Then
                    InputRep = InputRep + "{" + _Trim$(Str$(Inputs(k).Pointer)) + ":" + _Trim$(Str$(Inputs(k).Identity)) + "}" + _Trim$(Str$(Component(Inputs(k).Pointer).C.Value)) + " "
                End If
            End If
            If (Inputs(k).Identity = Component(Inputs(k).Pointer).C.Identity) Then
                If Inputs(k).Species = "PSH" Then
                    InputRep = InputRep + "(" + _Trim$(Str$(Inputs(k).Pointer)) + ":" + _Trim$(Str$(Inputs(k).Identity)) + ")" + _Trim$(Str$(Component(Inputs(k).Pointer).A.Value)) + " "
                End If
                If Inputs(k).Species = "TOG" Then
                    InputRep = InputRep + "[" + _Trim$(Str$(Inputs(k).Pointer)) + ":" + _Trim$(Str$(Inputs(k).Identity)) + "]" + _Trim$(Str$(Component(Inputs(k).Pointer).B.Value)) + " "
                End If
                If Inputs(k).Species = "PLS" Then
                    InputRep = InputRep + "{" + _Trim$(Str$(Inputs(k).Pointer)) + ":" + _Trim$(Str$(Inputs(k).Identity)) + "}" + _Trim$(Str$(Component(Inputs(k).Pointer).C.Value)) + " "
                End If
            End If
        End If
    Next
    If (InputRep <> "") Then
        If (Len(InputRep) >= (1 * _Width) / 8 - 3) Then
            InputRep = Left$(InputRep, (1 * _Width) / 8 - 3) + "..."
        End If
        Print InputRep;
    End If
    Print: Print
End Sub

Sub PrintStateHistory
    Dim k As Integer
    Dim StateRep As String
    StateRep = ""
    For k = 1 To UBound(Component)
        If (Component(k).Species <> "") Then
            StateRep = StateRep + "[" + _Trim$(Str$(k)) + "]" + _Trim$(Str$(Component(k).A.Value)) + _Trim$(Str$(Component(k).B.Value)) + _Trim$(Str$(Component(k).C.Value))
        End If
    Next
    If (StateHistory(1) <> StateRep) Then
        For k = UBound(StateHistory) To 2 Step -1
            StateHistory(k) = StateHistory(k - 1)
        Next
        StateHistory(1) = StateRep
    End If
    Print "State:"
    For k = 1 To UBound(StateHistory)
        StateRep = StateHistory(k)
        If (Len(StateRep) >= (1 * _Width) / 8 - 3) Then
            StateRep = Left$(StateHistory(k), (1 * _Width) / 8 - 3) + "..."
        End If
        Print StateRep
    Next
End Sub

Sub PrintOutput
    Dim k As Integer
    Dim As String OutputRep
    OutputRep = ""
    Print "Output: "
    For k = 1 To UBound(Outputs)
        If (Outputs(k).Species <> "") Then
            If (Outputs(k).Identity = Component(Outputs(k).Pointer).A.Identity) Then
                OutputRep = OutputRep + "(" + _Trim$(Str$(Outputs(k).Pointer)) + ":" + _Trim$(Str$(Outputs(k).Identity)) + ")" + _Trim$(Str$(Component(Outputs(k).Pointer).A.Value)) + " "
            End If
            If (Outputs(k).Identity = Component(Outputs(k).Pointer).B.Identity) Then
                OutputRep = OutputRep + "(" + _Trim$(Str$(Outputs(k).Pointer)) + ":" + _Trim$(Str$(Outputs(k).Identity)) + ")" + _Trim$(Str$(Component(Outputs(k).Pointer).B.Value)) + " "
            End If
            If (Outputs(k).Identity = Component(Outputs(k).Pointer).C.Identity) Then
                OutputRep = OutputRep + "(" + _Trim$(Str$(Outputs(k).Pointer)) + ":" + _Trim$(Str$(Outputs(k).Identity)) + ")" + _Trim$(Str$(Component(Outputs(k).Pointer).C.Value)) + " "
            End If
        End If
    Next
    If (OutputRep <> "") Then
        If (Len(OutputRep) >= (1 * _Width) / 8 - 3) Then
            OutputRep = Left$(OutputRep, (1 * _Width) / 8 - 3) + "..."
        End If
        Print OutputRep;
    End If
    Print: Print
End Sub

Sub DrawGlyph
    Type Vector
        x As Double
        y As Double
    End Type
    Dim As Integer n, m, k, j
    Dim As _Unsigned Long g, ActiveColor
    Dim Position(UBound(Component)) As Vector
    n = 0
    For k = 1 To UBound(Component)
        If (Component(k).Species <> "") Then n = n + 1
    Next
    m = 0
    For k = 1 To UBound(Component)
        If (Component(k).Species <> "") Then
            m = m + 1
            Position(k).x = _Width / 4 + 120 * Cos(1 * 2 * 3.14 * (m / n))
            Position(k).y = _Height / 4 + 120 * Sin(3 * 2 * 3.14 * (m / n))
        End If
    Next
    Call CPrintstring("Glyph:", _Width / 4 - 8 * (6) / 2, _Height / 4 + 150, _RGB32(200, 200, 200))
    For k = 1 To UBound(Component)
        If (Component(k).Species <> "") Then
            g = Component(k).A.Identity
            j = Component(k).A.Pointer
            If (j <> -1) Then
                If (g = Component(j).A.Identity) Then
                    If (Component(j).A.Value = 1) Then
                        ActiveColor = _RGB32(255, 0, 0)
                    Else
                        ActiveColor = _RGB32(0, 0, 255)
                    End If
                End If
                If (g = Component(j).B.Identity) Then
                    If (Component(j).B.Value = 1) Then
                        ActiveColor = _RGB32(255, 0, 0)
                    Else
                        ActiveColor = _RGB32(0, 0, 255)
                    End If
                End If
                If (g = Component(j).C.Identity) Then
                    If (Component(j).C.Value = 1) Then
                        ActiveColor = _RGB32(255, 0, 0)
                    Else
                        ActiveColor = _RGB32(0, 0, 255)
                    End If
                End If
                Call CLine(Position(k).x, Position(k).y, Position(j).x, Position(j).y, ActiveColor)
            End If
            g = Component(k).B.Identity
            j = Component(k).B.Pointer
            If (j <> -1) Then
                If (g = Component(j).A.Identity) Then
                    If (Component(j).A.Value = 1) Then
                        ActiveColor = _RGB32(255, 0, 0)
                    Else
                        ActiveColor = _RGB32(0, 0, 255)
                    End If
                End If
                If (g = Component(j).B.Identity) Then
                    If (Component(j).B.Value = 1) Then
                        ActiveColor = _RGB32(255, 0, 0)
                    Else
                        ActiveColor = _RGB32(0, 0, 255)
                    End If
                End If
                If (g = Component(j).C.Identity) Then
                    If (Component(j).C.Value = 1) Then
                        ActiveColor = _RGB32(255, 0, 0)
                    Else
                        ActiveColor = _RGB32(0, 0, 255)
                    End If
                End If
                Call CLine(Position(k).x, Position(k).y, Position(j).x, Position(j).y, ActiveColor)
            End If
            g = Component(k).C.Identity
            j = Component(k).C.Pointer
            If (j <> -1) Then
                If (g = Component(j).A.Identity) Then
                    If (Component(j).A.Value = 1) Then
                        ActiveColor = _RGB32(255, 0, 0)
                    Else
                        ActiveColor = _RGB32(0, 0, 255)
                    End If
                End If
                If (g = Component(j).B.Identity) Then
                    If (Component(j).B.Value = 1) Then
                        ActiveColor = _RGB32(255, 0, 0)
                    Else
                        ActiveColor = _RGB32(0, 0, 255)
                    End If
                End If
                If (g = Component(j).C.Identity) Then
                    If (Component(j).C.Value = 1) Then
                        ActiveColor = _RGB32(255, 0, 0)
                    Else
                        ActiveColor = _RGB32(0, 0, 255)
                    End If
                End If
                Call CLine(Position(k).x, Position(k).y, Position(j).x, Position(j).y, ActiveColor)
            End If
        End If
    Next
    For k = 1 To UBound(Inputs)
        If (Inputs(k).Species <> "") Then
            j = Inputs(k).Pointer
            If (Inputs(k).Identity = Component(j).A.Identity) Then n = Component(j).A.Value
            If (Inputs(k).Identity = Component(j).B.Identity) Then n = Component(j).B.Value
            If (Inputs(k).Identity = Component(j).C.Identity) Then n = Component(j).C.Value
            Call CCircle(Position(j).x, Position(j).y, 18, _RGB32(0, 255, 0))
            If (n = 1) Then Call CCircle(Position(j).x, Position(j).y, 14, _RGB32(0, 255, 0))
        End If
    Next
    For k = 1 To UBound(Outputs)
        If (Outputs(k).Species <> "") Then
            j = Outputs(k).Pointer
            If (Outputs(k).Identity = Component(j).A.Identity) Then n = Component(j).A.Value
            If (Outputs(k).Identity = Component(j).B.Identity) Then n = Component(j).B.Value
            If (Outputs(k).Identity = Component(j).C.Identity) Then n = Component(j).C.Value
            Call CCircle(Position(j).x, Position(j).y, 18, _RGB32(255, 255, 0))
            If (n = 1) Then Call CCircle(Position(j).x, Position(j).y, 14, _RGB32(255, 255, 0))
        End If
    Next
    For k = 1 To UBound(Component)
        If (Component(k).Species <> "") Then
            Call CCircle(Position(k).x, Position(k).y, 16, _RGB32(200, 200, 200))
            Call CPrintstring(_Trim$(Str$(k)), Position(k).x - 2, Position(k).y + 6, _RGB32(255, 200, 200))
        End If
    Next
End Sub

Sub DrawControls
    Dim As Integer n, k, j
    Dim As Double x0, y0
    Dim As _Unsigned Long ActiveColor
    Call CPrintstring("Inputs:", _Width / 4 - 8 * (7) / 2, 0, _RGB32(200, 200, 200))
    For k = 1 To UBound(Inputs)
        x0 = _Width / 4 - 120 + k * 25
        y0 = -25
        If (Inputs(k).Species <> "") Then
            ActiveColor = _RGB32(0, 255, 0)
        Else
            ActiveColor = _RGB32(100, 100, 100)
        End If
        j = Inputs(k).Pointer
        If (Inputs(k).Identity = Component(j).A.Identity) Then n = Component(j).A.Value
        If (Inputs(k).Identity = Component(j).B.Identity) Then n = Component(j).B.Value
        If (Inputs(k).Identity = Component(j).C.Identity) Then n = Component(j).C.Value
        Select Case Inputs(k).Species
            Case "PSH"
                Call CCircle(x0, y0, 10, ActiveColor)
                If (n = 1) Then Call CPaint(x0, y0, ActiveColor)
            Case "TOG"
                Call CLineB(x0 - 10, y0 - 10, x0 + 10, y0 + 10, ActiveColor)
                If (n = 1) Then Call CLineBF(x0 - 10, y0 - 10, x0 + 10, y0 + 10, ActiveColor)
            Case "PLS"
                Call CLine(x0 - 10, y0 - 10, x0 + 10, y0 - 10, ActiveColor)
                Call CLine(x0 - 10, y0 - 10, x0 + 0, y0 + 10, ActiveColor)
                Call CLine(x0 + 10, y0 - 10, x0 + 0, y0 + 10, ActiveColor)
                If (n = 1) Then Call CPaint(x0, y0, ActiveColor)
            Case Else
                Call CCircle(x0, y0, 10, ActiveColor)
        End Select
    Next
    Call CPrintstring("Outputs:", _Width / 4 - 8 * (8) / 2, -50, _RGB32(200, 200, 200))
    For k = 1 To UBound(Outputs)
        x0 = _Width / 4 - 120 + k * 25
        y0 = -75
        If (Outputs(k).Species <> "") Then
            ActiveColor = _RGB32(255, 255, 0)
        Else
            ActiveColor = _RGB32(100, 100, 100)
        End If
        j = Outputs(k).Pointer
        If (Component(j).A.Identity = Outputs(k).Identity) Then n = Component(j).A.Value
        If (Component(j).B.Identity = Outputs(k).Identity) Then n = Component(j).B.Value
        If (Component(j).C.Identity = Outputs(k).Identity) Then n = Component(j).C.Value
        Call CLineB(x0 - 10, y0 - 10, x0 + 10, y0 + 10, ActiveColor)
        If (n = 1) Then Call CPaint(x0, y0, ActiveColor)
    Next
End Sub

Sub CCircle (x0 As Double, y0 As Double, rad As Double, shade As _Unsigned Long)
    Circle (_Width / 2 + x0, -y0 + _Height / 2), rad, shade
End Sub

Sub CPaint (x0 As Double, y0 As Double, shade As _Unsigned Long)
    Paint (_Width / 2 + x0, -y0 + _Height / 2), shade
End Sub

Sub CPrintstring (TheString As String, x0 As Double, y0 As Double, shade As _Unsigned Long)
    Color shade
    _PrintString (_Width / 2 + x0, -y0 + _Height / 2), TheString
End Sub

Sub CLine (x0 As Double, y0 As Double, x1 As Double, y1 As Double, shade As _Unsigned Long)
    Line (_Width / 2 + x0, -y0 + _Height / 2)-(_Width / 2 + x1, -y1 + _Height / 2), shade
End Sub

Sub CLineB (x0 As Double, y0 As Double, x1 As Double, y1 As Double, shade As _Unsigned Long)
    Line (_Width / 2 + x0, -y0 + _Height / 2)-(_Width / 2 + x1, -y1 + _Height / 2), shade, B
End Sub

Sub CLineBF (x0 As Double, y0 As Double, x1 As Double, y1 As Double, shade As _Unsigned Long)
    Line (_Width / 2 + x0, -y0 + _Height / 2)-(_Width / 2 + x1, -y1 + _Height / 2), shade, BF
End Sub

