Esempio n. 1
0
def ALUMemHelper(f, opcode: bytes, cpu: CPU.CPU):
    #find the addressing mode of this operation. The addressing mode for the ALU operations are stored in bit 2, 3 and 4.
    code = (opcode & 0b11100) >> 2

    #use the helper function to retrieve the byte from memory
    print("Counter: " + str(cpu.PC))
    print(cpu.instructions[0:100])
    nextbytes = cpu.instructions[cpu.PC + 1:cpu.PC + 1 + 2]
    try:
        print("Next bytes: " + str(nextbytes[0]) + " : " + str(nextbytes[1]))
    except:
        print("Next bytes: " + str(nextbytes[0]))

    print("code: " + str(code))
    val = getMemArray[code](cpu, nextbytes)
    print("Memory address to be accessed: " + str(val[0]))

    cpu.PC += val[1]

    if (code == 2):
        val = cpu.instructions[val[0]:val[0] + 1]
    else:
        val = cpu.ram.read_bytes(val[0], 1)

    print("Val retrieved from memory: " + str(val))

    #perform the ALU operation on the retrieved byte
    result = f(cpu.A, val[0])
    print("Result: " + str(result))

    #TODO: not sure of the exact functionality of the carry bit. Should it be cleared if no carry occurs in bit 7?

    #set cpu flags
    if not (opcode >> 7 == 1):
        fresult = result % 256
        carry = result > fresult
        cpu.P = (cpu.P & 0b01111100) | carry | (
            (fresult == 0) << 1) | (fresult & 0b10000000)
        result = fresult

    #save result to accumulator
    cpu.A = result