             0x54,       // LD D, H
             0x55,       // LD D, L
             0x16, 0x03, // LD D, $03
-            
+
             0x58,       // LD E, B
             0x59,       // LD E, C
             0x5A,       // LD E, D

            0x26, 0xD0, // LD H, $D0
            0x6E,       // LD L, (HL)
            0x2E, 0x00, // LD L ,$00
            
            // LD (HL), r
            0x06, 0x01, // LD B, $01
            0x0E, 0x02, // LD C, $02

            0xF0, 0xA1, // LD A, ($A1)
            0xF0, 0xA2, // LD A, ($A2)
        ]

        // Load program into RAM
        for (offset, byte) in program.enumerated() {
            cpu.memory.write(0xC000 + Address(offset), byte)
        }

        // Start program execution from RAM
        cpu.PC = 0xC000

        cpu.step()
        XCTAssertEqual(cpu.A, UInt8(0xA0))
        cpu.step()
        XCTAssertEqual(cpu.B, UInt8(0x01))
        cpu.step()
        XCTAssertEqual(cpu.C, UInt8(0x02))
        cpu.step()
        XCTAssertEqual(cpu.D, UInt8(0x03))
        cpu.step()
        XCTAssertEqual(cpu.E, UInt8(0x04))
        cpu.step()
        XCTAssertEqual(cpu.H, UInt8(0x05))
        cpu.step()
        XCTAssertEqual(cpu.L, UInt8(0x06))

        cpu.step()
        XCTAssertEqual(cpu.A, UInt8(0xA0))
        cpu.step()
        XCTAssertEqual(cpu.A, UInt8(0x01))
        cpu.step()
        XCTAssertEqual(cpu.A, UInt8(0x02))
        cpu.step()
        XCTAssertEqual(cpu.A, UInt8(0x03))
        cpu.step()
        XCTAssertEqual(cpu.A, UInt8(0x04))
        cpu.step()
        XCTAssertEqual(cpu.A, UInt8(0x05))
        cpu.step()
        XCTAssertEqual(cpu.A, UInt8(0x06))

        cpu.step()
        XCTAssertEqual(cpu.B, UInt8(0x01))
        cpu.step()
        XCTAssertEqual(cpu.B, UInt8(0x02))
        cpu.step()
        XCTAssertEqual(cpu.B, UInt8(0x03))
        cpu.step()
        XCTAssertEqual(cpu.B, UInt8(0x04))
        cpu.step()
        XCTAssertEqual(cpu.B, UInt8(0x05))
        cpu.step()
        XCTAssertEqual(cpu.B, UInt8(0x06))
        cpu.step()

        cpu.step()
        XCTAssertEqual(cpu.C, UInt8(0x01))
        cpu.step()
        XCTAssertEqual(cpu.C, UInt8(0x01))
        cpu.step()
        XCTAssertEqual(cpu.C, UInt8(0x03))
        cpu.step()
        XCTAssertEqual(cpu.C, UInt8(0x04))
        cpu.step()
        XCTAssertEqual(cpu.C, UInt8(0x05))
        cpu.step()
        XCTAssertEqual(cpu.C, UInt8(0x06))
        cpu.step()

        cpu.step()
        XCTAssertEqual(cpu.D, UInt8(0x01))
        cpu.step()
        XCTAssertEqual(cpu.D, UInt8(0x02))
        cpu.step()
        XCTAssertEqual(cpu.D, UInt8(0x02))
        cpu.step()
        XCTAssertEqual(cpu.D, UInt8(0x04))
        cpu.step()
        XCTAssertEqual(cpu.D, UInt8(0x05))
        cpu.step()
        XCTAssertEqual(cpu.D, UInt8(0x06))
        cpu.step()

        cpu.step()
        XCTAssertEqual(cpu.E, UInt8(0x01))
        cpu.step()
        XCTAssertEqual(cpu.E, UInt8(0x02))
        cpu.step()
        XCTAssertEqual(cpu.E, UInt8(0x03))
        cpu.step()
        XCTAssertEqual(cpu.E, UInt8(0x03))
        cpu.step()
        XCTAssertEqual(cpu.E, UInt8(0x05))
        cpu.step()
        XCTAssertEqual(cpu.E, UInt8(0x06))
        cpu.step()

        cpu.step()
        XCTAssertEqual(cpu.H, UInt8(0x01))
        cpu.step()
        XCTAssertEqual(cpu.H, UInt8(0x02))
        cpu.step()
        XCTAssertEqual(cpu.H, UInt8(0x03))
        cpu.step()
        XCTAssertEqual(cpu.H, UInt8(0x04))
        cpu.step()
        XCTAssertEqual(cpu.H, UInt8(0x04))
        cpu.step()
        XCTAssertEqual(cpu.H, UInt8(0x06))
        cpu.step()

        cpu.step()
        XCTAssertEqual(cpu.L, UInt8(0x01))
        cpu.step()
        XCTAssertEqual(cpu.L, UInt8(0x02))
        cpu.step()
        XCTAssertEqual(cpu.L, UInt8(0x03))
        cpu.step()
        XCTAssertEqual(cpu.L, UInt8(0x04))
        cpu.step()
        XCTAssertEqual(cpu.L, UInt8(0x05))
        cpu.step()
        XCTAssertEqual(cpu.L, UInt8(0x05))
        cpu.step()

        4.times { cpu.step() }
        XCTAssertEqual(cpu.B, UInt8(0xF0))
        cpu.step()
        XCTAssertEqual(cpu.C, UInt8(0xF0))
        cpu.step()
        XCTAssertEqual(cpu.D, UInt8(0xF0))
        cpu.step()
        XCTAssertEqual(cpu.E, UInt8(0xF0))
        cpu.step()
        XCTAssertEqual(cpu.H, UInt8(0xF0))
        2.times { cpu.step() }
        XCTAssertEqual(cpu.L, UInt8(0xF0))
        cpu.step()

        6.times { cpu.step() }
        XCTAssertEqual(cpu.A, cpu.B)
        2.times{ cpu.step() }
        XCTAssertEqual(cpu.A, cpu.C)
        2.times{ cpu.step() }
        XCTAssertEqual(cpu.A, cpu.D)
        2.times{ cpu.step() }
        XCTAssertEqual(cpu.A, cpu.E)
        2.times{ cpu.step() }
        XCTAssertEqual(cpu.A, cpu.H)
        2.times{ cpu.step() }
        XCTAssertEqual(cpu.A, cpu.L)

        cpu.step()
        XCTAssertEqual(cpu.A, cpu.A)
        cpu.step()
        XCTAssertEqual(cpu.B, cpu.A)
        cpu.step()
        XCTAssertEqual(cpu.C, cpu.A)
        cpu.step()
        XCTAssertEqual(cpu.D, cpu.A)
        cpu.step()
        XCTAssertEqual(cpu.E, cpu.A)
        cpu.step()
        XCTAssertEqual(cpu.H, cpu.A)
        cpu.step()
        XCTAssertEqual(cpu.L, cpu.A)

        10.times { cpu.step() }
        XCTAssertEqual(cpu.A, UInt8(0xF1))
        cpu.step()
        XCTAssertEqual(cpu.A, UInt8(0xF2))
        cpu.step()
        XCTAssertEqual(cpu.A, UInt8(0xF1))
        6.times { cpu.step() }
        XCTAssertEqual(cpu.A, UInt8(0xF0))
        2.times { cpu.step() }
        XCTAssertEqual(cpu.A, UInt8(0xF1))

        8.times { cpu.step() }
        XCTAssertEqual(cpu.A, UInt8(0xAA))
        cpu.step()
        XCTAssertEqual(cpu.A, UInt8(0xBB))
        cpu.step()
        XCTAssertEqual(cpu.A, UInt8(0xCC))
    }

    func test16BitLoads() {
        var cpu = CPU(memory: Memory())
        let program: [UInt8] = [
