func (gb *GameBoy) ADC_a_r(ins []uint8) int {
	r := Reg8ID(ins[0] & 0x07)
	aVal := gb.get8Reg(A)
	bVal := gb.get8Reg(r)
	c := gb.get8Reg(F) & uint8(C_FLAG) >> 4
	out := aVal + bVal + c
	gb.set8Reg(A, out)
	if (aVal&0x0f)+(bVal&0x0f) > 0x0f {
		gb.modifyFlag(H_FLAG, SET)
	} else {
		gb.modifyFlag(H_FLAG, CLEAR)
	}
	if (uint16(aVal) + uint16(bVal)) > 0xff {
		gb.modifyFlag(C_FLAG, SET)
	} else {
		gb.modifyFlag(C_FLAG, CLEAR)
	}
	if out == 0 {
		gb.modifyFlag(Z_FLAG, SET)
	} else {
		gb.modifyFlag(Z_FLAG, CLEAR)
	}
	gb.modifyFlag(N_FLAG, CLEAR)
	gb.regs[PC] += uint16(len(ins))
	return 4
}
ADC A, r 1B
package main
import (
	"encoding/binary"
	"math/bits"
)
func (gb *GameBoy) LD_r_r(ins []uint8) int {
	r1 := Reg8ID((ins[0] & 0x38) >> 3)
	r2 := Reg8ID(ins[0] & 0x07)
	gb.set8Reg(r1, gb.get8Reg(r2))
	gb.regs[PC] += uint16(len(ins))
	return 4
}
func (gb *GameBoy) LD_r_n(ins []uint8) int {
	n := ins[1]
	r := Reg8ID(ins[0] >> 3)
	gb.set8Reg(r, n)
	gb.regs[PC] += uint16(len(ins))
	return 8
}
func (gb *GameBoy) LD_r_hl(ins []uint8) int {
	r := Reg8ID((ins[0] & 0x38) >> 3)
	address := gb.get16Reg(HL)
	gb.set8Reg(r, gb.mainMemory.read(address))
	gb.regs[PC] += uint16(len(ins))
	return 8
}
func (gb *GameBoy) LD_hl_r(ins []uint8) int {
	r := Reg8ID(ins[0] & 0x07)
	address := gb.get16Reg(HL)
	gb.mainMemory.write(address, gb.get8Reg(r))
	gb.regs[PC] += uint16(len(ins))
	return 8
}
func (gb *GameBoy) LD_hl_n(ins []uint8) int {
	n := ins[1]
	address := gb.get16Reg(HL)
	gb.mainMemory.write(address, n)
	gb.regs[PC] += uint16(len(ins))
	return 12
}
func (gb *GameBoy) LD_a_bc(ins []uint8) int {
	address := gb.get16Reg(BC)
	gb.set8Reg(A, gb.mainMemory.read(address))
	gb.regs[PC] += uint16(len(ins))
	return 8
}
func (gb *GameBoy) LD_a_de(ins []uint8) int {
	address := gb.get16Reg(DE)
	gb.set8Reg(A, gb.mainMemory.read(address))
	gb.regs[PC] += uint16(len(ins))
	return 8
}
func (gb *GameBoy) LD_a_c(ins []uint8) int {
	address := uint16(gb.get8Reg(C)) + 0xff00
	value := gb.mainMemory.read(address)
	gb.set8Reg(A, value)
	gb.regs[PC] += uint16(len(ins))
	return 8
}
func (gb *GameBoy) LD_c_a(ins []uint8) int {
	value := gb.get8Reg(A)
	address := uint16(gb.get8Reg(C)) + 0xff00
	gb.mainMemory.write(address, value)
	gb.regs[PC] += uint16(len(ins))
	return 8
}
func (gb *GameBoy) LD_a_n(ins []uint8) int {
	address := uint16(ins[1]) + 0xff00
	value := gb.mainMemory.read(address)
	gb.set8Reg(A, value)
	gb.regs[PC] += uint16(len(ins))
	return 12
}
func (gb *GameBoy) LD_n_a(ins []uint8) int {
	address := uint16(ins[1]) + 0xff00
	value := gb.get8Reg(A)
	gb.mainMemory.write(address, value)
	gb.regs[PC] += uint16(len(ins))
	return 12
}
func (gb *GameBoy) LD_a_nn(ins []uint8) int {
	address := binary.LittleEndian.Uint16(ins[1:])
	gb.set8Reg(A, gb.mainMemory.read(address))
	gb.regs[PC] += uint16(len(ins))
	return 16
}
func (gb *GameBoy) LD_bc_a(ins []uint8) int {
	address := gb.get16Reg(BC)
	gb.mainMemory.write(address, gb.get8Reg(A))
	gb.regs[PC] += uint16(len(ins))
	return 8
}
func (gb *GameBoy) LD_de_a(ins []uint8) int {
	address := gb.get16Reg(DE)
	gb.mainMemory.write(address, gb.get8Reg(A))
	gb.regs[PC] += uint16(len(ins))
	return 8
}
func (gb *GameBoy) LD_nn_a(ins []uint8) int {
	address := binary.LittleEndian.Uint16(ins[1:])
	gb.mainMemory.write(address, gb.get8Reg(A))
	gb.regs[PC] += uint16(len(ins))
	return 16
}
func (gb *GameBoy) LDI_a_hl(ins []uint8) int {
	address := gb.get16Reg(HL)
	value := gb.mainMemory.read(address)
	gb.set8Reg(A, value)
	gb.set16Reg(HL, address+0x1)
	gb.regs[PC] += uint16(len(ins))
	return 8
}
func (gb *GameBoy) LDD_a_hl(ins []uint8) int {
	address := gb.get16Reg(HL)
	value := gb.mainMemory.read(address)
	gb.set8Reg(A, value)
	gb.set16Reg(HL, address-0x1)
	gb.regs[PC] += uint16(len(ins))
	return 8
}
func (gb *GameBoy) LDI_hl_a(ins []uint8) int {
	value := gb.get8Reg(A)
	address := gb.get16Reg(HL)
	gb.mainMemory.write(address, value)
	gb.set16Reg(HL, address+0x1)
	gb.regs[PC] += uint16(len(ins))
	return 8
}
func (gb *GameBoy) LDD_hl_a(ins []uint8) int {
	value := gb.get8Reg(A)
	address := gb.get16Reg(HL)
	gb.mainMemory.write(address, value)
	gb.set16Reg(HL, address-0x1)
	gb.regs[PC] += uint16(len(ins))
	return 8
}
func (gb *GameBoy) LD_dd_nn(ins []uint8) int {
	reg := Reg16ID(ins[0] >> 4)
	nn := binary.LittleEndian.Uint16(ins[1:])
	gb.set16Reg(reg, nn)
	gb.regs[PC] += uint16(len(ins))
	return 12
}
func (gb *GameBoy) LD_sp_hl(ins []uint8) int {
	gb.set16Reg(SP, gb.get16Reg(HL))
	gb.regs[PC] += uint16(len(ins))
	return 8
}
func (gb *GameBoy) PUSH_qq(ins []uint8) int {
	reg := Reg16ID((ins[0] >> 4) & 0x3)
	if reg == 0x3 {
		reg = 0x5
	}
	val := gb.get16Reg(reg)
	lowVal := uint8(val)
	highVal := uint8(val >> 8)
	gb.regs[SP] -= 1
	gb.mainMemory.write(gb.get16Reg(SP), highVal)
	gb.regs[SP] -= 1
	gb.mainMemory.write(gb.get16Reg(SP), lowVal)
	gb.regs[PC] += uint16(len(ins))
	return 16
}
func (gb *GameBoy) POP_qq(ins []uint8) int {
	reg := Reg16ID((ins[0] >> 4) & 0x3)
	if reg == 0x3 {
		reg = 0x5
	}
	lowVal := gb.mainMemory.read(gb.get16Reg(SP))
	gb.regs[SP] += 1
	highVal := gb.mainMemory.read(gb.get16Reg(SP))
	gb.regs[SP] += 1
	val := uint16(lowVal) | (uint16(highVal) << 8)
	gb.set16Reg(reg, val)
	gb.regs[PC] += 1
	return 12
}
func (gb *GameBoy) LDHL_sp_e(ins []uint8) int {
	aVal := gb.get16Reg(SP)
	bVal := uint16(int8(ins[1]))
	out := aVal + bVal
	gb.set16Reg(HL, out)
	gb.modifyFlag(Z_FLAG, CLEAR)
	gb.modifyFlag(N_FLAG, CLEAR)
	if (aVal&0x0fff)+(bVal&0x0fff) > 0x0fff {
		gb.modifyFlag(H_FLAG, SET)
	} else {
		gb.modifyFlag(H_FLAG, CLEAR)
	}
	if uint32(aVal)+uint32(bVal) > 0xffff {
		gb.modifyFlag(C_FLAG, SET)
	} else {
		gb.modifyFlag(C_FLAG, CLEAR)
	}
	gb.regs[PC] += uint16(len(ins))
	return 12
}
func (gb *GameBoy) LD_nn_sp(ins []uint8) int {
	sp := gb.get16Reg(SP)
	sp_lb := uint8(sp)
	sp_hb := uint8(sp >> 8)
	address := binary.LittleEndian.Uint16(ins[1:])
	gb.mainMemory.write(address, sp_lb)
	gb.mainMemory.write(address+0x1, sp_hb)
	gb.regs[PC] += uint16(len(ins))
	return 20
}
func (gb *GameBoy) ADD_a_r(ins []uint8) int {
	r := Reg8ID(ins[0] & 0x07)
	aVal := gb.get8Reg(A)
	bVal := gb.get8Reg(r)
	out := aVal + bVal
	gb.set8Reg(A, out)
	if (aVal&0x0f)+(bVal&0x0f) > 0x0f {
		gb.modifyFlag(H_FLAG, SET)
	} else {
		gb.modifyFlag(H_FLAG, CLEAR)
	}
	if out == 0 {
		gb.modifyFlag(Z_FLAG, SET)
	} else {
		gb.modifyFlag(Z_FLAG, CLEAR)
	}
	gb.modifyFlag(N_FLAG, CLEAR)
	if (uint16(aVal) + uint16(bVal)) > 0xff {
		gb.modifyFlag(C_FLAG, SET)
	} else {
		gb.modifyFlag(C_FLAG, CLEAR)
	}
	gb.regs[PC] += uint16(len(ins))
	return 4
}
func (gb *GameBoy) ADD_a_n(ins []uint8) int {
	aVal := gb.get8Reg(A)
	bVal := ins[1]
	out := aVal + bVal
	gb.set8Reg(A, out)
	if (aVal&0x0f)+(bVal&0x0f) > 0x0f {
		gb.modifyFlag(H_FLAG, SET)
	} else {
		gb.modifyFlag(H_FLAG, CLEAR)
	}
	if (uint16(aVal) + uint16(bVal)) > 0xff {
		gb.modifyFlag(C_FLAG, SET)
	} else {
		gb.modifyFlag(C_FLAG, CLEAR)
	}
	if out == 0 {
		gb.modifyFlag(Z_FLAG, SET)
	} else {
		gb.modifyFlag(Z_FLAG, CLEAR)
	}
	gb.modifyFlag(N_FLAG, CLEAR)
	gb.regs[PC] += uint16(len(ins))
	return 8
}
func (gb *GameBoy) ADD_a_hl(ins []uint8) int {
	aVal := gb.get8Reg(A)
	bVal := gb.mainMemory.read(gb.get16Reg(HL))
	out := aVal + bVal
	gb.set8Reg(A, out)
	if (aVal&0x0f)+(bVal&0x0f) > 0x0f {
		gb.modifyFlag(H_FLAG, SET)
	} else {
		gb.modifyFlag(H_FLAG, CLEAR)
	}
	if (uint16(aVal) + uint16(bVal)) > 0xff {
		gb.modifyFlag(C_FLAG, SET)
	} else {
		gb.modifyFlag(C_FLAG, CLEAR)
	}
	if out == 0 {
		gb.modifyFlag(Z_FLAG, SET)
	} else {
		gb.modifyFlag(Z_FLAG, CLEAR)
	}
	gb.modifyFlag(N_FLAG, CLEAR)
	gb.regs[PC] += uint16(len(ins))
	return 8
}