import time
import utime

import machine
time.sleep(0.1) # Wait for USB to become ready

ram = [ #16x8 ram

    [0, 0, 0, 0, 0, 0, 0, 0],  # i=0
    #instruction half inst,half memory location
    [1, 1, 0, 0, 0, 1, 1, 1],  # i=1 //ci7,ci6,ci5,ci4,ci3,ci2,ci1,ci0
    [0, 0, 0, 0, 0, 0, 0, 0],  # i=2
    [0, 0, 0, 0, 0, 0, 0, 0],  # i=3
    [0, 0, 0, 0, 0, 0, 0, 0],  # i=4
    [0, 0, 0, 0, 0, 0, 1, 1],  # i=5
    [0, 0, 0, 1, 0, 0, 1, 0],  # i=6
    [0, 0, 0, 0, 0, 0, 0, 0],  # i=7
    #memory of data 8 to 15
    [0, 0, 0, 0, 0, 0, 0, 0],  # i=8
    [0, 0, 0, 0, 0, 0, 0, 0],  # i=9
    [0, 0, 0, 0, 0, 0, 0, 0],  # i=10
    [0, 0, 0, 0, 0, 0, 0, 0],  # i=11
    [0, 0, 0, 0, 0, 0, 1, 1],  # i=12
    [0, 0, 0, 0, 0, 0, 0, 0],  # i=13
    [0, 0, 0, 0, 0, 0, 1, 1],  # i=14
    [0, 0, 0, 1, 0, 0, 1, 0]   # i=15
]

#pin settings
# CLOCK
clock_pin = machine.Pin(0, machine.Pin.OUT)

reset = machine.Pin(1, machine.Pin.OUT)


en = machine.Pin(2, machine.Pin.OUT)
en.value(1)
reset.value(1)
reset.value(0)

#pc
p0 = machine.Pin(10, machine.Pin.IN)
p1 = machine.Pin(11, machine.Pin.IN)
p2 = machine.Pin(12, machine.Pin.IN)
p3 = machine.Pin(13, machine.Pin.IN)




def provide_clockpulse():
    for _ in range(1):
        clock_pin.value(0)
        utime.sleep_ms(500)  # Adjust the delay as needed
        clock_pin.value(1)
        utime.sleep_ms(500)  # Adjust the delay as needed

while True:
    clock_pin.value(0)
    utime.sleep_ms(500)  # Adjust the delay as needed
    clock_pin.value(1)
    utime.sleep_ms(500)  # Adjust the delay as needed

    en.value(0) # now pc is at 1 so 1st insruction ko fetch kro and do execution between these
    pcval=(p3.value() * 8) + (p2.value() * 4) + (p1.value() * 2) + (p0.value() * 1)
    
    #this whole is instruction
    ci7 = ram[pcval][0]
    ci6 = ram[pcval][1]
    ci5 = ram[pcval][2]
    ci4 = ram[pcval][3]

    ci3 = ram[pcval][4]
    ci2 = ram[pcval][5]
    ci1 = ram[pcval][6]
    ci0 = ram[pcval][7]
    
    #instruction in format
    # c7 c6 c5 c4 -->instruction 0000 load,0001 add, 0010 sub
    # c3 c2 c1 c0 -->  memory location of the data jispe instruction chalna

    decoded_inst= (ci7 * 8) + (ci6 * 4) + (ci5 * 2) + (ci4 * 1)


    mem_loc= (ci3 * 8) + (ci2 * 4) + (ci1 * 2) + (ci0 * 1)
    #now fetch 8 se 15 ke bech vala data and store that in some var for now
    d7=ram[mem_loc][0]
    d6=ram[mem_loc][1]
    d5=ram[mem_loc][2]
    d4=ram[mem_loc][3]
    d3=ram[mem_loc][4]
    d2=ram[mem_loc][5]
    d1=ram[mem_loc][6]
    d0=ram[mem_loc][7] #fetched data successfully isko yaha se bahir bhejna

    #recognizing and perfroming instruction

    #OPCODES LOAD=0, ADD=1, SUB=2
    if decoded_inst == 0: #0000
        # LOAD code
        print("LOAD ")

    elif decoded_inst == 1: #0001
        # ADD code
        print("ADD ")

    elif decoded_inst == 2: #0010
        # SUB code
        print("SUB")

    elif decoded_inst == 14: #1110
        # load acc to output register
        print("output instruction")    
        
    else:
        # Handle invalid instruction or other cases
        print("Unknown opcode")


    break



    en.value(1) #instruction ends here

    if p0.value() == 1 and p1.value() == 1 and p2.value() == 1 and p3.value() == 1:
        break

    

BOOTSELLED1239USBRaspberryPiPico©2020RP2-8020/21P64M15.00TTT
SR
SR
SR
SR

ERC Warnings

flop5:S: Input pin not driven
flop6:S: Input pin not driven
flop7:S: Input pin not driven
flop8:S: Input pin not driven
flop1:CLK: Input pin not driven
flop2:CLK: Input pin not driven
flop3:CLK: Input pin not driven
flop4:CLK: Input pin not driven
flop9:CLK: Input pin not driven
flop10:CLK: Input pin not driven
70 additional warning(s) hidden