import time
import random
LoadPinOfPcMux = machine.Pin(1, machine.Pin.OUT)
EnablePinOfPc = machine.Pin(0, machine.Pin.OUT)
ClockPc = machine.Pin(16, machine.Pin.OUT)
LoadPinOfPcMux.value(1)
EnablePinOfPc.value(1)
Muxinp1 = machine.Pin(2, machine.Pin.OUT)
Muxinp2 = machine.Pin(3, machine.Pin.OUT)
Muxinp3 = machine.Pin(4, machine.Pin.OUT)
Muxinp4 = machine.Pin(5, machine.Pin.OUT)
Muxinp1.value(1)
Muxinp2.value(1)
Muxinp3.value(1)
Muxinp4.value(1)
Muxinp1.value(0)
Muxinp2.value(0)
Muxinp3.value(0)
Muxinp4.value(0)
PcInputPin1 = machine.Pin(13, machine.Pin.IN)
PcInputPin2 = machine.Pin(12, machine.Pin.IN)
PcInputPin4 = machine.Pin(11, machine.Pin.IN)
PcInputPin8 = machine.Pin(10, machine.Pin.IN)
#bottom IR pins
bottomIRPin1= machine.Pin(18,machine.Pin.OUT)
bottomIRPin2= machine.Pin(19,machine.Pin.OUT)
bottomIRPin3= machine.Pin(20,machine.Pin.OUT)
bottomIRPin4= machine.Pin(21,machine.Pin.OUT)
#top IR pins
topIRpin1 = machine.Pin(22,machine.Pin.OUT)
topIRpin2 = machine.Pin(26,machine.Pin.OUT)
topIRpin3 = machine.Pin(27,machine.Pin.OUT)
topIRpin4 = machine.Pin(28,machine.Pin.OUT)
ClockIR= machine.Pin(17, machine.Pin.OUT)
loadIR= machine.Pin(14, machine.Pin.OUT)
opCodepin1= machine.Pin(8, machine.Pin.OUT)
opCodepin2= machine.Pin(9, machine.Pin.OUT)
accumulatorClock= machine.Pin(6, machine.Pin.OUT)
loadAccumulator = machine.Pin(7, machine.Pin.OUT)
#Memory simulation
Memory = [
[1, 1, 1, 1], #15
[1, 1, 1, 0], #14
[1, 1, 0, 1], #13
[1, 1, 0, 0], #12
[1, 0, 1, 1], #11
[1, 0, 1, 0], #10
[1, 0, 0, 1], #9
[1, 0, 0, 0], #8
[0, 1, 1, 1], #7
[0, 1, 1, 0], #6
[0, 1, 0, 1], #5
[0, 1, 0, 0], #4
[0, 0, 1, 1], #3
[0, 0, 1, 0], #2
[0, 0, 0, 1], #1
[0, 0, 0, 0] #0
]
#print ("Memory elements are as follows: ")
#for row in Memory:
# print(row)
ALUAddSubtract = machine.Pin(15, machine.Pin.OUT)
counter = 1
while True:
ClockPc.value(0)
time.sleep(2)
ClockPc.value(1)
time.sleep(2)
PCpin1_val = PcInputPin1.value()
PCpin2_val = PcInputPin2.value()
PCpin4_val = PcInputPin4.value()
PCpin8_val = PcInputPin8.value()
loadIR.value(0)
PC_dec = PCpin1_val*1 + PCpin2_val*2 + PCpin4_val*4 + PCpin8_val*8
if (PC_dec % 4 == 1):
print(f"starting LOOP {counter} ")
opCodepin1.value(0)
opCodepin2.value(1)
if(PC_dec % 4 == 2):
opCodepin1.value(1)
opCodepin2.value(0)
if (PC_dec % 4 == 3):
addSubtract = random.randint(0, 1)
if (addSubtract == 0):
opCodepin1.value(1)
opCodepin2.value(1)
else:
opCodepin1.value(0)
opCodepin2.value(0)
if (PC_dec % 4 == 0):
print("The final value has been saved in the accumulator")
print(f"LOOP {counter} has been completed successfully. ")
counter += 1
continue
opCodepin1_val= opCodepin1.value()
opCodepin2_val= opCodepin2.value()
if (opCodepin1_val==0 and opCodepin2_val==1):
elements = Memory[PC_dec]
var1, var2, var3, var4 = elements
topIRpin1.value(var4)
topIRpin2.value(var3)
topIRpin3.value(var2)
topIRpin4.value(var1)
print ("OPCODE RECEIVED: 01 - data loaded into the top instruction register -")
print (var1,var2,var3,var4)
if (opCodepin1_val==1 and opCodepin2_val==0):
elements = Memory[PC_dec]
var1, var2, var3, var4 = elements
bottomIRPin1.value(var4)
bottomIRPin2.value(var3)
bottomIRPin3.value(var2)
bottomIRPin4.value(var1)
print ("OPCODE RECEIVED: 10 - data loaded into the bottom instruction register -")
print (var1,var2,var3,var4)
# now if it is 11 we will do addition
if (opCodepin1_val==1 and opCodepin2_val==1):
ALUAddSubtract.value(0)
print ("OPCODE RECEIVED: 11 - DATA IS BEING ADDED")
loadIR.value(1)
ClockIR.value(0)
time.sleep(1)
ClockIR.value(1)
time.sleep(1)
loadAccumulator.value(1)
accumulatorClock.value(0)
time.sleep(1)
accumulatorClock.value(1)
time.sleep(1)
print ("Data has been added successfully. ")
# now if it is 00 we will do subtraction
if (opCodepin1_val==0 and opCodepin2_val==0):
ALUAddSubtract.value(1)
print ("OPCODE RECEIVED: 00 - DATA IS BEING SUBTRACTED")
loadIR.value(1)
ClockIR.value(0)
time.sleep(1)
ClockIR.value(1)
time.sleep(1)
loadAccumulator.value(1)
accumulatorClock.value(0)
time.sleep(1)
accumulatorClock.value(1)
time.sleep(1)
print ("Data has been subtracted successfully. ")
ERC Warnings
flop5:S: Input pin not driven
flop1:S: Input pin not driven
flop2:S: Input pin not driven
flop3:S: Input pin not driven
flop4:S: Input pin not driven
flop4:R: Input pin not driven
flop6:S: Input pin not driven
flop6:R: Input pin not driven
flop7:S: Input pin not driven
flop7:R: Input pin not driven
27 additional warning(s) hidden