package gba

import (
	"log"

	"github.com/aabalke/guac/config"
	"github.com/aabalke/guac/emu/gba/cart"
)

// ygba
var vsyncAddrMap = map[cart.Header]uint32{

	// these values are not pipeline adjusted (if pipeline is emulated, +8)
	//{Title: "ADVANCEWARS ", GameCode: "AWRE", Version: 0}: 0x80387EC,
	//{Title: "ADVANCEWARS ", GameCode: "AWRE", Version: 1}: 0x8038818,
	//{Title: "DRILL DOZER ", GameCode: "V49E", Version: 0}: 0x80006BA,
	//{Title: "FFTA_USVER. ", GameCode: "AFXE", Version: 0}: 0x8000418,
	//{Title: "KURURIN     ", GameCode: "AKRP", Version: 0}: 0x800041A,
	//{Title: "POKEMON EMER", GameCode: "BPEE", Version: 0}: 0x80008C6,
	//{Title: "POKEMON FIRE", GameCode: "BPRE", Version: 0}: 0x80008AA,
	//{Title: "POKEMON FIRE", GameCode: "BPRE", Version: 1}: 0x80008BE,
	//{Title: "POKEMON LEAF", GameCode: "BPGE", Version: 0}: 0x80008AA,
	//{Title: "POKEMON LEAF", GameCode: "BPGE", Version: 1}: 0x80008BE,

	// these values are pipeline adjusted (if pipeline is not emulated, -8)
	{Title: "DRACULA AGB1", GameCode: "AAME", Version: 0}: 0x080003D2,
	{Title: "MEGAMAN_BN  ", GameCode: "AREE", Version: 0}: 0x08000338,
	{Title: "MEGAMAN_EXE2", GameCode: "AE2E", Version: 0}: 0x08000358,
	{Title: "MEGA_EXE3_WH", GameCode: "A6BE", Version: 0}: 0x0800036C,
	{Title: "MEGA_EXE3_BL", GameCode: "A3XE", Version: 0}: 0x0800036C,
	{Title: "MEGAMANBN4RS", GameCode: "B4WE", Version: 0}: 0x080003A6,
	{Title: "MEGAMANBN4BM", GameCode: "B4BE", Version: 0}: 0x080003A6,
	{Title: "MEGAMAN5_TP_", GameCode: "BRBE", Version: 0}: 0x080003CA,
	{Title: "MEGAMAN5_TC_", GameCode: "BRKE", Version: 0}: 0x080003CA,
	{Title: "MEGAMAN6_GXX", GameCode: "BR5E", Version: 0}: 0x080003DA,
	{Title: "MEGAMAN ZERO", GameCode: "AZCE", Version: 0}: 0x080004EE,
	{Title: "MEGAMANZERO2", GameCode: "A62E", Version: 0}: 0x08000664,
	{Title: "MEGAMANZERO3", GameCode: "BZ3E", Version: 0}: 0x08001A08,
	{Title: "MEGAMANZERO4", GameCode: "B4ZP", Version: 0}: 0x0800090C,
	{Title: "AGB KIRBY DX", GameCode: "A7KE", Version: 0}: 0x08000FAE,
	{Title: "AGB KIRBY DX", GameCode: "A7KP", Version: 0}: 0x08000FAE,
	{Title: "SUPER MARIOA", GameCode: "AMAE", Version: 0}: 0x08001CF2,
	{Title: "SUPER MARIOA", GameCode: "AMZE", Version: 0}: 0x08001CF2,
	{Title: "SUPER MARIOB", GameCode: "AA2E", Version: 0}: 0x08000534,
	{Title: "SUPER MARIOC", GameCode: "A3AE", Version: 0}: 0x08002BA4,
	{Title: "SUPER MARIOD", GameCode: "AX4E", Version: 0}: 0x08000732,
	{Title: "SUPER MARIOD", GameCode: "AX4P", Version: 0}: 0x08000732,
	{Title: "ADVANCEWARS ", GameCode: "AWRE", Version: 0}: 0x0803880A,
	{Title: "POKEMON EMER", GameCode: "BPEE", Version: 0}: 0x080008CE,
	{Title: "POKEMON EMER", GameCode: "BPED", Version: 0}: 0x080008CE,
	{Title: "POKEMON EMER", GameCode: "BPEF", Version: 0}: 0x080008CE,
	{Title: "POKEMON EMER", GameCode: "BPES", Version: 0}: 0x080008CE,
	{Title: "POKEMON EMER", GameCode: "BPEI", Version: 0}: 0x080008CE,
	{Title: "V-RALLY 3   ", GameCode: "AVRP", Version: 0}: 0x080AA920,
	{Title: "MARIOVSDK   ", GameCode: "BM5E", Version: 0}: 0x08033EEC,
	{Title: "POKEMON FIRE", GameCode: "BPRE", Version: 0}: 0x080008C6,
	{Title: "POKEMON FIRE", GameCode: "BPRS", Version: 0}: 0x080008C6,
	{Title: "POKEMON FIRE", GameCode: "BPRD", Version: 0}: 0x080008C6,
	{Title: "POKEMON FIRE", GameCode: "BPRI", Version: 0}: 0x080008C6,
	{Title: "POKEMON FIRE", GameCode: "BPRF", Version: 0}: 0x080008C6,
	{Title: "POKEMON LEAF", GameCode: "BPGE", Version: 0}: 0x080008B2,
	{Title: "POKEMON LEAF", GameCode: "BPGS", Version: 0}: 0x080008B6,
	{Title: "POKEMON LEAF", GameCode: "BPGD", Version: 0}: 0x080008B6,
	{Title: "POKEMON LEAF", GameCode: "BPGI", Version: 0}: 0x080008B6,
	{Title: "POKEMON LEAF", GameCode: "BPGF", Version: 0}: 0x080008B6,
	{Title: "FFTA_USVER. ", GameCode: "AFXE", Version: 0}: 0x0800041E,
	{Title: "GRADIUSGALAX", GameCode: "AGAE", Version: 0}: 0x08013844,
	{Title: "REBELSTAR   ", GameCode: "BRLE", Version: 0}: 0x0800041A,
	{Title: "GOLDEN_SUN_B", GameCode: "AGFE", Version: 0}: 0x08013542,
	{Title: "MARIOGOLFGBA", GameCode: "BMGE", Version: 0}: 0x08014E0A,
	{Title: "MARIOGOLFGBA", GameCode: "BMGP", Version: 0}: 0x08014E0A,
	{Title: "MARIOGOLFGBA", GameCode: "BMGS", Version: 0}: 0x08014E0A,
	{Title: "MARIOGOLFGBA", GameCode: "BMGF", Version: 0}: 0x08014E0A,
	{Title: "MARIOGOLFGBA", GameCode: "BMGI", Version: 0}: 0x08014E0A,
	{Title: "MARIOGOLFGBA", GameCode: "BMGD", Version: 0}: 0x08014E0A,
	{Title: "MARIOGOLFGBA", GameCode: "BMGU", Version: 0}: 0x08014E0A,
	{Title: "ADVANCEWARS2", GameCode: "AW2E", Version: 0}: 0x08036E2A,
	{Title: "BOMSTORYUSA ", GameCode: "ABSE", Version: 0}: 0x08000526,
	{Title: "BROKENSWORD ", GameCode: "ABJE", Version: 0}: 0x08000A26,
	{Title: "DOTC        ", GameCode: "ADHE", Version: 0}: 0x080007EC,
	{Title: "DRILL DOZER ", GameCode: "V49E", Version: 0}: 0x080006C2,
	{Title: "F-ZERO ADVAN", GameCode: "AFZE", Version: 0}: 0x08000C2E,
	{Title: "MEGAMANZERO2", GameCode: "A62E", Version: 0}: 0x08000664,
	{Title: "MEGAMANZERO3", GameCode: "BZ3E", Version: 0}: 0x08001A08,
	{Title: "MEGAMANZERO4", GameCode: "B4ZE", Version: 0}: 0x0800090C,
	{Title: "METAL SLUG  ", GameCode: "BSME", Version: 0}: 0x08000298,
	{Title: "M&M MAGICAL2", GameCode: "AQME", Version: 0}: 0x0801D340,
	{Title: "M&D MAGICAL3", GameCode: "BMQE", Version: 0}: 0x08016064,
	{Title: "PINBALL CHAL", GameCode: "APLP", Version: 0}: 0x080075A6,
	{Title: "PRINCEPERSIA", GameCode: "BPYE", Version: 0}: 0x0808FF3A,
	{Title: "RIVERCRANSOM", GameCode: "BDTE", Version: 0}: 0x0800065A,
	{Title: "PUZZLEFIGHT2", GameCode: "AZ8E", Version: 0}: 0x08002B5E,
	{Title: "YU-GI-OH DDM", GameCode: "AYDE", Version: 0}: 0x0802CC6A,
	{Title: "YU-GI-OH!EDS", GameCode: "AY5E", Version: 0}: 0x08075D96,
	{Title: "YUGIOH DM7  ", GameCode: "AY7E", Version: 0}: 0x08003BD6,
	{Title: "YWCT2004USA ", GameCode: "BYWE", Version: 0}: 0x080831DA,
	{Title: "YUGIOHWWE   ", GameCode: "AYWE", Version: 0}: 0x08089792,
	{Title: "WARIOWAREINC", GameCode: "AZWE", Version: 0}: 0x08000F66,
	{Title: "SPLINTERCELL", GameCode: "AO4E", Version: 0}: 0x0807A0C4,
	{Title: "TOM CLANCY'S", GameCode: "BSLE", Version: 0}: 0x0807785E,
	{Title: "FF4ADVANCE  ", GameCode: "BZ4E", Version: 0}: 0x0800FABE,
	{Title: "DIGIMON BTSP", GameCode: "A8SE", Version: 0}: 0x08011208,
	{Title: "DIGIMON BS2 ", GameCode: "BDSE", Version: 0}: 0x08010EB0,
	{Title: "DISNEY'S DON", GameCode: "ADKE", Version: 0}: 0x08002F30,
	{Title: "FINAL FIGHT ", GameCode: "AFFE", Version: 0}: 0x0800B428,
	{Title: "BATTLECHIPGP", GameCode: "A89E", Version: 0}: 0x08000544,
	{Title: "MONSTERFORCE", GameCode: "AM8E", Version: 0}: 0x08000B00,
	{Title: "MONSRANCHERA", GameCode: "AMFE", Version: 0}: 0x0809F394,
	{Title: "MONSTERRANC2", GameCode: "A2QE", Version: 0}: 0x081C7290,
	{Title: "PINBALL DEAD", GameCode: "APDE", Version: 0}: 0x08000300,
	{Title: "TRINGO      ", GameCode: "BTJE", Version: 0}: 0x080009A4,
	{Title: "VIRTKASPAROV", GameCode: "AVKE", Version: 0}: 0x0800093A,
	{Title: "ADVANCEWARS2", GameCode: "AW2P", Version: 0}: 0x080371BE,
	{Title: "BOOKWORM    ", GameCode: "BKWE", Version: 0}: 0x0800397C,
	{Title: "NIGHTFIRE   ", GameCode: "A7OE", Version: 0}: 0x080031D6,
	{Title: "ASTERIX     ", GameCode: "BLXP", Version: 0}: 0x0846D060,
	{Title: "KIMPOSSIBLE2", GameCode: "BKME", Version: 0}: 0x080008A2,
	{Title: "KIMPOSSIBLE2", GameCode: "BKMP", Version: 0}: 0x080008A2,
	{Title: "AGB KIRBY DX", GameCode: "A7KJ", Version: 0}: 0x08000F92,
	{Title: "SUPER MARIOD", GameCode: "AX4J", Version: 0}: 0x08000732,
	{Title: "POKEMON EMER", GameCode: "BPEJ", Version: 0}: 0x080008CE,
	{Title: "POKEMON FIRE", GameCode: "BPRJ", Version: 0}: 0x080008B2,
	{Title: "MARIOGOLFGBA", GameCode: "BMGJ", Version: 0}: 0x08014E0A,
	{Title: "RHYTHMTENGOK", GameCode: "BRIJ", Version: 0}: 0x080013D4,
	{Title: "OUGONTAIYO_B", GameCode: "AGFJ", Version: 0}: 0x08013542,
	{Title: "MARIOTENNISA", GameCode: "BTMJ", Version: 0}: 0x08013888,
	{Title: "OPEN SEASON ", GameCode: "BOAE", Version: 0}: 0x08066378,
	{Title: "DRACULA AGB1", GameCode: "AAMP", Version: 0}: 0x080003D2,
	{Title: "MEGAMAN_BN  ", GameCode: "AREP", Version: 0}: 0x08000338,
	{Title: "MEGAMAN_EXE2", GameCode: "AE2P", Version: 0}: 0x08000358,
	{Title: "MEGA_EXE3_WH", GameCode: "A6BP", Version: 0}: 0x0800036C,
	{Title: "MEGA_EXE3_BL", GameCode: "A3XP", Version: 0}: 0x0800036C,
	{Title: "MEGAMANBN4RS", GameCode: "B4WP", Version: 0}: 0x080003A6,
	{Title: "MEGAMANBN4BM", GameCode: "B4BP", Version: 0}: 0x080003A6,
	{Title: "MEGAMAN5_TP_", GameCode: "BRBP", Version: 0}: 0x080003CA,
	{Title: "MEGAMAN5_TC_", GameCode: "BRKP", Version: 0}: 0x080003CA,
	{Title: "MEGAMAN6_GXX", GameCode: "BR5P", Version: 0}: 0x080003DA,
	{Title: "MEGAMANZERO2", GameCode: "A62P", Version: 0}: 0x08000664,
	{Title: "MEGAMANZERO3", GameCode: "BZ3P", Version: 0}: 0x08001A08,
	{Title: "MEGAMANZERO4", GameCode: "B4ZP", Version: 0}: 0x0800090C,
	{Title: "SUPER MARIOA", GameCode: "AMAE", Version: 0}: 0x08001CF2,
	{Title: "SUPER MARIOB", GameCode: "AA2P", Version: 0}: 0x08000534,
	{Title: "SUPER MARIOC", GameCode: "A3AP", Version: 0}: 0x08002BA4,
	{Title: "ADVANCEWARS ", GameCode: "AWRP", Version: 0}: 0x0803880A,
	{Title: "MARIOVSDK   ", GameCode: "BM5P", Version: 0}: 0x08033EEC,
	{Title: "FFTA_USVER. ", GameCode: "AFXP", Version: 0}: 0x0800041E,
	{Title: "GRADIUSGALAX", GameCode: "AGAP", Version: 0}: 0x08013844,
	{Title: "REBELSTAR   ", GameCode: "BRLP", Version: 0}: 0x0800041A,
	{Title: "GOLDEN_SUN_B", GameCode: "AGFS", Version: 0}: 0x08013542,
	{Title: "BROKENSWORD ", GameCode: "ABJP", Version: 0}: 0x08000A26,
	{Title: "DOTC        ", GameCode: "ADHP", Version: 0}: 0x080007EC,
	{Title: "METAL SLUG  ", GameCode: "BSMP", Version: 0}: 0x08000298,
	{Title: "M&M MAGICAL2", GameCode: "AQMP", Version: 0}: 0x0801D340,
	{Title: "M&D MAGICAL3", GameCode: "BMQP", Version: 0}: 0x08016064,
	{Title: "PRINCEPERSIA", GameCode: "BPYP", Version: 0}: 0x0808FF3A,
	{Title: "PUZZLEFIGHT2", GameCode: "AZ8P", Version: 0}: 0x08002B5E,
	{Title: "YU-GI-OH DDM", GameCode: "AYDP", Version: 0}: 0x0802CC6A,
	{Title: "YUGIOH DM7  ", GameCode: "AY7P", Version: 0}: 0x08003BD6,
	{Title: "YWCT2004USA ", GameCode: "BYWP", Version: 0}: 0x080831DA,
	{Title: "YUGIOHWWE   ", GameCode: "AYWP", Version: 0}: 0x08089792,
	{Title: "WARIOWAREINC", GameCode: "AZWP", Version: 0}: 0x08000F66,
	{Title: "SPLINTERCELL", GameCode: "AO4P", Version: 0}: 0x0807A0C4,
	{Title: "TOM CLANCY'S", GameCode: "BSLP", Version: 0}: 0x0807785E,
	{Title: "FF4ADVANCE  ", GameCode: "BZ4P", Version: 0}: 0x0800FABE,
	{Title: "DIGIMON BTSP", GameCode: "A8SP", Version: 0}: 0x08011208,
	{Title: "DIGIMON BS2 ", GameCode: "BDSP", Version: 0}: 0x08010EB0,
	{Title: "DISNEY'S DON", GameCode: "ADKP", Version: 0}: 0x08002F30,
	{Title: "FINAL FIGHT ", GameCode: "AFFP", Version: 0}: 0x0800B428,
	{Title: "BATTLECHIPGP", GameCode: "A89P", Version: 0}: 0x08000544,
	{Title: "MONSTERFORCE", GameCode: "AM8P", Version: 0}: 0x08000B00,
	{Title: "PINBALL DEAD", GameCode: "APDP", Version: 0}: 0x08000300,
	{Title: "TRINGO      ", GameCode: "BTJP", Version: 0}: 0x080009A4,
	{Title: "VIRTKASPAROV", GameCode: "AVKP", Version: 0}: 0x0800093A,
}

func (gba *GBA) SetIdleAddr() {
	if !config.Conf.Gba.IdleOptimize {
		return
	}

	v, ok := vsyncAddrMap[*gba.Cartridge.Header]
	if !ok {
		return
	}

	// subtracting 8 since pipeline is not emulated
	gba.vsyncAddr = v - 8
	log.Printf("Idle Loop Address Set %08X\n", v)
}
