Esempio n. 1
0
def test_TYA():
     cpu = CPU()
     memory = Memory()
     cpu.a = 1
     cpu.y = 2
     TYA = OpCodes.all[152]
     TYA.exec(cpu, memory)

     assert cpu.a == 2
     assert cpu.zero == False
     assert cpu.negative == False

     cpu.y = 0
     cpu.a = 1
     cpu.inc_cycle_by(-cpu.cycle)
     TYA.exec(cpu, memory)

     assert cpu.cycle == (TYA.cycles - 1)
     assert cpu.a == 0
     assert cpu.zero == True
     assert cpu.negative == False

     cpu.y = 0b10000001
     cpu.a = 1
     TYA.exec(cpu, memory)

     assert cpu.a == 0b10000001
     assert cpu.zero == False
     print(cpu.negative)
     assert cpu.negative == True
Esempio n. 2
0
def test_TAY():
     cpu = CPU()
     memory = Memory()
     cpu.y = 1
     cpu.a = 2
     TAY = OpCodes.all[168]
     TAY.exec(cpu, memory)

     assert cpu.y == 2
     assert cpu.zero == False
     assert cpu.negative == False

     cpu.a = 0
     cpu.y = 1
     cpu.inc_cycle_by(-cpu.cycle)
     TAY.exec(cpu, memory)

     assert cpu.cycle == (TAY.cycles - 1)
     assert cpu.y == 0
     assert cpu.zero == True
     assert cpu.negative == False

     cpu.a = 0b10000001
     cpu.y = 1
     TAY.exec(cpu, memory)

     assert cpu.y == 0b10000001
     assert cpu.zero == False
     print(cpu.negative)
     assert cpu.negative == True
def test_read_AbsoluteY():
    address_mode = AbsoluteY
    cpu = CPU()
    memory = Memory(rom=[0x32, 0x02, 0x7F, 0x01, 0x00, 0x20],
                    ram=list(map(lambda x: x % 256, range(Memory.ram_size()))))

    cpu.inc_cycle_by(-cpu.cycle)
    cpu.inc_pc_by(-cpu.pc + MemoryPositions.PRG_ROM_START.start)
    cpu.y = 0x0A
    address = address_mode.fetch_address(cpu, memory)
    value = address_mode.read_from(cpu, memory, address)
    assert address == 0x023C
    assert value == (0x023C % 256)

    cpu.inc_cycle_by(-cpu.cycle)
    cpu.y = 0xA1
    address = address_mode.fetch_address(cpu, memory)
    value = address_mode.read_from(cpu, memory, address)

    assert address == 0x0220
    assert value == (0x0220 % 256)

    cpu.inc_cycle_by(-cpu.cycle)
    cpu.y = 0x07
    address = address_mode.fetch_address(cpu, memory)

    assert address == 0x2007
def test_write_AbsoluteY():
    address_mode = AbsoluteY
    cpu = CPU()
    memory = Memory(rom=[0x30, 0x01, 0xFF, 0x02, 0x0A, 0x05],
                    ram=list(map(lambda x: x % 256, range(Memory.ram_size()))))

    cpu.inc_cycle_by(-cpu.cycle)
    cpu.inc_pc_by(-cpu.pc + MemoryPositions.PRG_ROM_START.start)
    cpu.y = 0x25
    address = address_mode.fetch_address(cpu, memory)
    address_mode.write_to(cpu, memory, address, 20)
    assert address == 0x0155
    assert memory.ram[address] == 20

    cpu.inc_cycle_by(-cpu.cycle)
    cpu.y = 0x30
    address = address_mode.fetch_address(cpu, memory)
    address_mode.write_to(cpu, memory, address, 100)
    assert address == 0x032F
    assert memory.ram[address] == 100

    cpu.inc_cycle_by(-cpu.cycle)
    cpu.y = 0x20
    address = address_mode.fetch_address(cpu, memory)
    address_mode.write_to(cpu, memory, address, 67)
    assert address == 0x052A
    assert memory.ram[address] == 67
def test_write_ZeroPageY():
    address_mode = ZeroPageY
    cpu = CPU()
    memory = Memory(rom=list(range(256)))

    cpu.inc_cycle_by(-cpu.cycle)
    cpu.inc_pc_by(-cpu.pc + MemoryPositions.PRG_ROM_START.start)
    cpu.y = 5
    address = address_mode.fetch_address(cpu, memory)
    address_mode.write_to(cpu, memory, address, 6)
    assert address == 5
    assert memory.ram[address] == 6
    assert cpu.cycle == 3

    cpu.inc_cycle_by(-cpu.cycle)
    cpu.inc_pc_by(-cpu.pc + MemoryPositions.PRG_ROM_START.start + 20)
    cpu.y = 15
    address = address_mode.fetch_address(cpu, memory)
    address_mode.write_to(cpu, memory, address, 105)

    assert address == 35
    assert memory.ram[address] == 105
    assert cpu.cycle == 3

    cpu.inc_cycle_by(-cpu.cycle)
    cpu.inc_pc_by(-cpu.pc + MemoryPositions.PRG_ROM_START.start + 100)
    cpu.y = 155
    address = address_mode.fetch_address(cpu, memory)
    address_mode.write_to(cpu, memory, address, 143)

    assert address == 255
    assert memory.ram[address] == 143
    assert cpu.cycle == 3

    cpu.inc_cycle_by(-cpu.cycle)
    cpu.inc_pc_by(-cpu.pc + MemoryPositions.PRG_ROM_START.start + 255)
    cpu.y = 0
    address = address_mode.fetch_address(cpu, memory)
    address_mode.write_to(cpu, memory, address, 255)

    assert address == 255
    assert memory.ram[address] == 255
    assert cpu.cycle == 3

    cpu.inc_cycle_by(-cpu.cycle)
    cpu.inc_pc_by(-cpu.pc + MemoryPositions.PRG_ROM_START.start + 255)
    cpu.y = 75
    address = address_mode.fetch_address(cpu, memory)
    address_mode.write_to(cpu, memory, address, 255)

    assert address == 74
    assert memory.ram[address] == 255
    assert cpu.cycle == 3
def test_read_ZeroPageY():
    address_mode = ZeroPageY
    cpu = CPU()
    memory = Memory(rom=list(range(256)), ram=list(reversed(range(256))))

    cpu.inc_cycle_by(-cpu.cycle)
    cpu.inc_pc_by(-cpu.pc + MemoryPositions.PRG_ROM_START.start)
    cpu.y = 1
    address = address_mode.fetch_address(cpu, memory)
    value = address_mode.read_from(cpu, memory, address)
    assert address == 1
    assert value == 254
    assert cpu.cycle == 3

    cpu.inc_cycle_by(-cpu.cycle)
    cpu.inc_pc_by(-cpu.pc + MemoryPositions.PRG_ROM_START.start + 20)
    cpu.y = 10
    address = address_mode.fetch_address(cpu, memory)
    value = address_mode.read_from(cpu, memory, address)

    assert address == 30
    assert value == 225
    assert cpu.cycle == 3

    cpu.inc_cycle_by(-cpu.cycle)
    cpu.inc_pc_by(-cpu.pc + MemoryPositions.PRG_ROM_START.start + 100)
    cpu.y = 5
    address = address_mode.fetch_address(cpu, memory)
    value = address_mode.read_from(cpu, memory, address)

    assert address == 105
    assert value == 150
    assert cpu.cycle == 3

    cpu.inc_cycle_by(-cpu.cycle)
    cpu.inc_pc_by(-cpu.pc + MemoryPositions.PRG_ROM_START.start + 255)
    cpu.y = 0
    address = address_mode.fetch_address(cpu, memory)
    value = address_mode.read_from(cpu, memory, address)

    assert address == 255
    assert value == 0
    assert cpu.cycle == 3

    cpu.inc_cycle_by(-cpu.cycle)
    cpu.inc_pc_by(-cpu.pc + MemoryPositions.PRG_ROM_START.start + 255)
    cpu.y = 5
    address = address_mode.fetch_address(cpu, memory)
    value = address_mode.read_from(cpu, memory, address)

    assert address == 4
    assert value == 251
    assert cpu.cycle == 3
def test_read_IndirectY():
    address_mode = IndirectY
    cpu = CPU()
    memory = Memory(rom=[0x00, 0x02, 0x7F, 0x01, 0xFF, 0x07],
                    ram=list(map(lambda x: x % 256, range(Memory.ram_size()))))

    cpu.inc_cycle_by(-cpu.cycle)
    cpu.inc_pc_by(-cpu.pc + MemoryPositions.PRG_ROM_START.start)
    cpu.y = 4
    address = address_mode.fetch_address(cpu, memory)
    value = address_mode.read_from(cpu, memory, address)
    assert address == 0x0104
    assert value == address % 256

    cpu.inc_cycle_by(-cpu.cycle)
    cpu.y = 0xFF
    address = address_mode.fetch_address(cpu, memory)
    value = address_mode.read_from(cpu, memory, address)
    assert address == 0x0401
    assert value == address % 256
def test_write_IndirectY():
    address_mode = IndirectY
    cpu = CPU()
    memory = Memory(rom=[0x00, 0x02, 0x7F, 0x01, 0xFF, 0x07],
                    ram=list(map(lambda x: x % 256, range(Memory.ram_size()))))

    cpu.inc_cycle_by(-cpu.cycle)
    cpu.inc_pc_by(-cpu.pc + MemoryPositions.PRG_ROM_START.start)
    cpu.y = 4
    address = address_mode.fetch_address(cpu, memory)
    address_mode.write_to(cpu, memory, address, 9)
    assert address == 0x0104
    assert memory.ram[address] == 9

    cpu.inc_cycle_by(-cpu.cycle)
    cpu.y = 0xFF
    address = address_mode.fetch_address(cpu, memory)
    address_mode.write_to(cpu, memory, address, 50)
    assert address == 0x0401
    assert memory.ram[address] == 50
def test_DEY_negative():
    instruction = OpCodes.all[0x88]
    cpu = CPU()
    memory = Memory()
    cpu.inc_cycle_by(-cpu.cycle)
    cpu.y = 0
    instruction.exec(cpu, memory)

    assert cpu.cycle == (instruction.cycles - 1)
    assert cpu.y == 255
    assert cpu.zero == False
    assert cpu.negative == True

    cpu.inc_cycle_by(-cpu.cycle)
    cpu.y = -1
    instruction.exec(cpu, memory)

    assert cpu.cycle == (instruction.cycles - 1)
    assert cpu.y == 254
    assert cpu.zero == False
    assert cpu.negative == True
def test_DEY_within_bounds():
    instruction = OpCodes.all[0x88]
    cpu = CPU()
    memory = Memory()
    cpu.inc_cycle_by(-cpu.cycle)
    cpu.y = 7
    instruction.exec(cpu, memory)

    assert cpu.cycle == (instruction.cycles - 1)
    assert cpu.y == 6
    assert cpu.zero == False
    assert cpu.negative == False
def test_INY_zero():
    instruction = OpCodes.all[0xC8]
    cpu = CPU()
    memory = Memory()
    cpu.inc_cycle_by(-cpu.cycle)
    cpu.y = -1
    instruction.exec(cpu, memory)

    assert cpu.cycle == (instruction.cycles - 1)
    assert cpu.y == 0
    assert cpu.zero == True
    assert cpu.negative == False

    cpu.inc_cycle_by(-cpu.cycle)
    cpu.y = 255
    instruction.exec(cpu, memory)

    assert cpu.cycle == (instruction.cycles - 1)
    assert cpu.y == 0
    assert cpu.zero == True
    assert cpu.negative == False