1 // license:BSD-3-Clause
2 // copyright-holders:S. Smith,David Haywood,Fabio Priuli
3 
4 #include "emu.h"
5 #include "prot_kof2k3bl.h"
6 
7 DEFINE_DEVICE_TYPE(NG_KOF2K3BL_PROT, kof2k3bl_prot_device, "ng_kof2k3bl_prot", "Neo Geo KoF 2003 Bootleg Protection")
8 
9 
kof2k3bl_prot_device(const machine_config & mconfig,const char * tag,device_t * owner,uint32_t clock)10 kof2k3bl_prot_device::kof2k3bl_prot_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock) :
11 	device_t(mconfig, NG_KOF2K3BL_PROT, tag, owner, clock)
12 {
13 }
14 
15 
device_start()16 void kof2k3bl_prot_device::device_start()
17 {
18 	save_item(NAME(m_cartridge_ram));
19 	save_item(NAME(m_overlay));
20 	save_item(NAME(m_bank_base));
21 }
22 
device_reset()23 void kof2k3bl_prot_device::device_reset()
24 {
25 	memset(m_cartridge_ram, 0x00, 0x2000);
26 	m_overlay = 0;
27 	m_bank_base = 0;
28 }
29 
30 
31 
32 /* The King of Fighters 2003 (bootleg set 1) */
33 
protection_r(offs_t offset)34 uint16_t kof2k3bl_prot_device::protection_r(offs_t offset)
35 {
36 	return m_cartridge_ram[offset];
37 }
38 
overlay_r()39 uint16_t kof2k3bl_prot_device::overlay_r() // hack?
40 {
41 	return m_overlay;
42 }
43 
kof2003_w(offs_t offset,uint16_t data,uint16_t mem_mask)44 void kof2k3bl_prot_device::kof2003_w(offs_t offset, uint16_t data, uint16_t mem_mask)
45 {
46 	data = COMBINE_DATA(&m_cartridge_ram[offset]);
47 	if (offset == 0x1ff0/2 || offset == 0x1ff2/2)
48 	{
49 		uint8_t* cr = (uint8_t *)m_cartridge_ram;
50 		uint8_t prt = cr[BYTE_XOR_LE(0x1ff2)];
51 		m_bank_base = 0x100000 + ((cr[BYTE_XOR_LE(0x1ff3)] << 16) | (cr[BYTE_XOR_LE(0x1ff2)] << 8) | cr[BYTE_XOR_LE(0x1ff1)]);
52 		//uint32_t address = (cr[BYTE_XOR_LE(0x1ff3)] << 16) | (cr[BYTE_XOR_LE(0x1ff2)] << 8) | cr[BYTE_XOR_LE(0x1ff1)];
53 		//m_bankdev->neogeo_set_main_cpu_bank_address(address+0x100000);
54 
55 		cr[BYTE_XOR_LE(0x1ff0)]  = 0xa0;
56 		cr[BYTE_XOR_LE(0x1ff1)] &= 0xfe;
57 		cr[BYTE_XOR_LE(0x1ff3)] &= 0x7f;
58 
59 		m_overlay = (prt & 0x00ff) | (m_overlay & 0xff00);
60 	}
61 }
62 
kof2003p_w(offs_t offset,uint16_t data,uint16_t mem_mask)63 void kof2k3bl_prot_device::kof2003p_w(offs_t offset, uint16_t data, uint16_t mem_mask)
64 {
65 	data = COMBINE_DATA(&m_cartridge_ram[offset]);
66 	if (offset == 0x1ff0/2 || offset == 0x1ff2/2)
67 	{
68 		uint8_t* cr = (uint8_t *)m_cartridge_ram;
69 		uint8_t prt = cr[BYTE_XOR_LE(0x1ff2)];
70 		m_bank_base = 0x100000 + ((cr[BYTE_XOR_LE(0x1ff3)] << 16) | (cr[BYTE_XOR_LE(0x1ff2)] << 8) | cr[BYTE_XOR_LE(0x1ff0)]);
71 		//uint32_t address = (cr[BYTE_XOR_LE(0x1ff3)] << 16) | (cr[BYTE_XOR_LE(0x1ff2)] << 8) | cr[BYTE_XOR_LE(0x1ff0)];
72 		//m_bankdev->neogeo_set_main_cpu_bank_address(address+0x100000);
73 
74 		cr[BYTE_XOR_LE(0x1ff0)] &= 0xfe;
75 		cr[BYTE_XOR_LE(0x1ff3)] &= 0x7f;
76 
77 		m_overlay = (prt & 0x00ff) | (m_overlay & 0xff00);
78 	}
79 }
80 
bl_px_decrypt(uint8_t * cpurom,uint32_t cpurom_size)81 void kof2k3bl_prot_device::bl_px_decrypt(uint8_t* cpurom, uint32_t cpurom_size)
82 {
83 	static const uint8_t sec[] = { 0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 };
84 	int rom_size = 0x800000;
85 	uint8_t *rom = cpurom;
86 	std::vector<uint8_t> buf(rom_size);
87 
88 	memcpy(&buf[0], rom, rom_size);
89 	for (int i = 0; i < rom_size / 0x100000; i++)
90 		memcpy(&rom[i * 0x100000], &buf[sec[i] * 0x100000], 0x100000);
91 }
92 
93 
94 /* The King of Fighters 2004 Plus / Hero (The King of Fighters 2003 bootleg) */
95 
pl_px_decrypt(uint8_t * cpurom,uint32_t cpurom_size)96 void kof2k3bl_prot_device::pl_px_decrypt(uint8_t* cpurom, uint32_t cpurom_size)
97 {
98 	std::vector<uint16_t> tmp(0x100000/2);
99 	uint16_t*rom16 = (uint16_t*)cpurom;
100 
101 	for (int i = 0; i < 0x700000/2; i += 0x100000/2)
102 	{
103 		memcpy(&tmp[0], &rom16[i], 0x100000);
104 		for (int j = 0; j < 0x100000/2; j++)
105 			rom16[i+j] = tmp[bitswap<24>(j,23,22,21,20,19,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18)];
106 	}
107 
108 	/* patched by Altera protection chip on PCB */
109 	rom16[0xf38ac/2] = 0x4e75;
110 
111 	m_overlay = rom16[0x58196 / 2];
112 }
113 
114 
115 /* The King of Fighters 2004 Ultra Plus (The King of Fighters 2003 bootleg) */
116 
upl_px_decrypt(uint8_t * cpurom,uint32_t cpurom_size)117 void kof2k3bl_prot_device::upl_px_decrypt(uint8_t* cpurom, uint32_t cpurom_size)
118 {
119 	uint8_t *src = cpurom;
120 	memmove(src + 0x100000, src, 0x600000);
121 	memmove(src, src + 0x700000, 0x100000);
122 
123 	uint8_t *rom = cpurom + 0xfe000;
124 	uint8_t *buf = cpurom + 0xd0610;
125 	for (int i = 0; i < 0x2000 / 2; i++)
126 	{
127 		int ofst = (i & 0xff00) + bitswap<8>((i & 0x00ff), 7, 6, 0, 4, 3, 2, 1, 5);
128 		memcpy(&rom[i * 2], &buf[ofst * 2], 2);
129 	}
130 
131 	uint16_t* rom16 = (uint16_t*)cpurom;
132 	m_overlay = rom16[0x58196 / 2];
133 }
134