summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/bus/nes/aladdin.c
blob: ea5e3a763d3bc69d81ddb57d7a250cec411a4b07 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/***********************************************************************************************************


 NES/Famicom cartridge emulation for Camerica/Codemasters Aladdin Deck Enhancer pass-through cart

 Copyright MESS Team.
 Visit http://mamedev.org for licensing and usage restrictions.


 Here we emulate the following PCBs

 * Camerica ALGNV11 [mapper 71]
 * Camerica ALGQV11 [mapper 232]

 ***********************************************************************************************************/


#include "emu.h"
#include "aladdin.h"


#ifdef NES_PCB_DEBUG
#define VERBOSE 1
#else
#define VERBOSE 0
#endif

#define LOG_MMC(x) do { if (VERBOSE) logerror x; } while (0)



//----------------------------------
//
//  Aladdin Cartslot implementation
//
//----------------------------------

//-------------------------------------------------
//  sub-cart interface
//-------------------------------------------------

aladdin_cart_interface::aladdin_cart_interface(const machine_config &mconfig, device_t &device)
						: device_slot_card_interface(mconfig, device),
							m_rom(NULL),
							m_rom_size(0),
							m_rom_mask(0xff)
{
}

aladdin_cart_interface::~aladdin_cart_interface()
{
}

READ8_MEMBER(aladdin_cart_interface::read)
{
	if (offset < 0x4000)
		return m_rom[(m_lobank * 0x4000) + (offset & 0x3fff)];
	else
		return m_rom[(m_hibank * 0x4000) + (offset & 0x3fff)];
}

//-------------------------------------------------
//  sub-cart slot device
//-------------------------------------------------

const device_type NES_ALADDIN_SLOT = &device_creator<nes_aladdin_slot_device>;

nes_aladdin_slot_device::nes_aladdin_slot_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock) :
						device_t(mconfig, NES_ALADDIN_SLOT, "NES Aladdin Deck Enhancer Cartridge Slot", tag, owner, clock, "nes_ade_slot", __FILE__),
						device_image_interface(mconfig, *this),
						device_slot_interface(mconfig, *this)
{
}

nes_aladdin_slot_device::~nes_aladdin_slot_device()
{
}


void nes_aladdin_slot_device::device_start()
{
	m_cart = dynamic_cast<aladdin_cart_interface *>(get_card_device());
}

READ8_MEMBER(nes_aladdin_slot_device::read)
{
	if (m_cart)
		return m_cart->read(space, offset, mem_mask);

	return 0xff;
}

// 128K for Dizzy The Adventurer, 256K for the others
bool nes_aladdin_slot_device::call_load()
{
	if (m_cart)
	{
		UINT8 *ROM = m_cart->get_cart_base();
		UINT32 size = 0;

		if (!ROM)
			return IMAGE_INIT_FAIL;

		if (software_entry() == NULL)
		{
			if (length() != 0x20010 && length() != 0x40010)
				return IMAGE_INIT_FAIL;

			UINT8 temp[0x40010];
			size = length() - 0x10;
			fread(&temp, length());
			memcpy(ROM, temp + 0x10, size);

			// double check that iNES files are really mapper 71 or 232
			{
				UINT8 mapper = (temp[6] & 0xf0) >> 4;
				mapper |= temp[7] & 0xf0;
				if (mapper != 71 && mapper != 232)
					return IMAGE_INIT_FAIL;
			}
		}
		else
		{
			if (get_software_region_length("rom") != 0x20000 && get_software_region_length("rom") != 0x40000)
				return IMAGE_INIT_FAIL;

			size = get_software_region_length("rom");
			memcpy(ROM, get_software_region("rom"), size);
		}

		m_cart->set_cart_size(size);
	}

	return IMAGE_INIT_PASS;
}


bool nes_aladdin_slot_device::call_softlist_load(software_list_device &swlist, const char *swname, const rom_entry *start_entry)
{
	load_software_part_region(*this, swlist, swname, start_entry );
	return TRUE;
}

void nes_aladdin_slot_device::get_default_card_software(astring &result)
{
	if (open_image_file(mconfig().options()))
	{
		const char *slot_string = "algn";
		UINT32 len = core_fsize(m_file);
		dynamic_buffer rom(len);
		UINT8 mapper;

		core_fread(m_file, rom, len);

		mapper = (rom[6] & 0xf0) >> 4;
		mapper |= rom[7] & 0xf0;

//      if (mapper == 71)
//          slot_string = "algn";
		if (mapper == 232)
			slot_string = "algq";

		clear();

		result.cpy(slot_string);
	}
	else
		software_get_default_slot(result, "algn");
}


//----------------------------------
//
//  Aladdin Minicart implementation
//
//----------------------------------

ROM_START( ade_rom )
	ROM_REGION(0x40000, "aderom", ROMREGION_ERASEFF)
ROM_END

const device_type NES_ALGN_ROM = &device_creator<nes_algn_rom_device>;
const device_type NES_ALGQ_ROM = &device_creator<nes_algq_rom_device>;

nes_algn_rom_device::nes_algn_rom_device(const machine_config &mconfig, device_type type, const char *name, const char *tag, device_t *owner, UINT32 clock, const char *shortname, const char *source)
						: device_t(mconfig, type, name, tag, owner, clock, shortname, source),
							aladdin_cart_interface( mconfig, *this )
{
}

nes_algn_rom_device::nes_algn_rom_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
						: device_t(mconfig, NES_ALGN_ROM, "NES Aladdin Deck Enhancer ALGN ROM", tag, owner, clock, "nes_algn_rom", __FILE__),
							aladdin_cart_interface( mconfig, *this )
{
}

nes_algq_rom_device::nes_algq_rom_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
						: nes_algn_rom_device(mconfig, NES_ALGQ_ROM, "NES Aladdin Deck Enhancer ALGQ ROM", tag, owner, clock, "nes_algq_rom", __FILE__)
{
}

void nes_algn_rom_device::device_start()
{
	m_rom = (UINT8*)memregion("aderom")->base();
	save_item(NAME(m_lobank));
}

void nes_algn_rom_device::device_reset()
{
	m_lobank = 0;
	m_hibank = 0x0f & m_rom_mask;
}

void nes_algq_rom_device::device_start()
{
	m_rom = (UINT8*)memregion("aderom")->base();
	save_item(NAME(m_lobank));
	save_item(NAME(m_hibank));
	save_item(NAME(m_bank_base));
}

void nes_algq_rom_device::device_reset()
{
	m_lobank = 0;
	m_hibank = 3;
	m_bank_base = 0;
}

const rom_entry *nes_algn_rom_device::device_rom_region() const
{
	return ROM_NAME( ade_rom );
}

UINT8 *nes_algn_rom_device::get_cart_base()
{
	return m_rom;
}

void nes_algn_rom_device::write_prg(UINT32 offset, UINT8 data)
{
	// m_hibank is fixed to the last available bank!
	if (offset >= 0x4000)
		m_lobank = data & m_rom_mask;
}

void nes_algq_rom_device::write_prg(UINT32 offset, UINT8 data)
{
	// here hibank & lobank variables are used differently
	// m_bank_base = 64K block
	// m_lobank = 16K page inside the block
	// m_hibank = 3rd page inside the block
	if (offset < 0x4000)
	{
		m_bank_base = ((data >> 3) & 3) << 2;
		m_lobank = m_bank_base | (m_lobank & 3);
		m_hibank = m_bank_base | 3;
	}
	else
		m_lobank = m_bank_base | (data & 3);
}


//-----------------------------------------------
//
//  Camerica/Codemasters Aladdin passthru
//  implementation
//
//-----------------------------------------------

const device_type NES_ALADDIN = &device_creator<nes_aladdin_device>;

nes_aladdin_device::nes_aladdin_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
					: nes_nrom_device(mconfig, NES_ALADDIN, "NES Cart Camerica Aladdin PCB", tag, owner, clock, "nes_aladdin", __FILE__),
					m_subslot(*this, "ade_slot")
{
}


void nes_aladdin_device::device_start()
{
	common_start();
}

void nes_aladdin_device::pcb_reset()
{
	prg32(0xff);
	chr8(0, CHRRAM);
}


/*-------------------------------------------------
 mapper specific handlers
 -------------------------------------------------*/

/*-------------------------------------------------

 Camerica/Codemasters Aladdin Deck Enhancer

 iNES: mapper 71 & 232

 In MESS: Supported (but timing issues in some games)

 -------------------------------------------------*/

READ8_MEMBER(nes_aladdin_device::read_h)
{
	LOG_MMC(("aladdin read_h, offset: %04x\n", offset));
	// this shall be the proper code, but it's a bit slower, so we access directly the subcart below
	//return m_subslot->read(space, offset, mem_mask);

	if (m_subslot->m_cart)
		return m_subslot->m_cart->read(space, offset, mem_mask);
	else    // this is "fake" in the sense that we fill CPU space with 0xff if no Aladdin cart is loaded
		return hi_access_rom(offset);
}

WRITE8_MEMBER(nes_aladdin_device::write_h)
{
	LOG_MMC(("aladdin write_h, offset: %04x, data: %02x\n", offset, data));
	m_subslot->write_prg(offset, data);
}

//-------------------------------------------------
//  CART SLOT
//-------------------------------------------------

static SLOT_INTERFACE_START(ade_cart)
	SLOT_INTERFACE("algn", NES_ALGN_ROM)
	SLOT_INTERFACE("algq", NES_ALGQ_ROM)
SLOT_INTERFACE_END


MACHINE_CONFIG_FRAGMENT( camerica_aladdin )
	MCFG_ALADDIN_MINICART_ADD("ade_slot", ade_cart)
MACHINE_CONFIG_END

machine_config_constructor nes_aladdin_device::device_mconfig_additions() const
{
	return MACHINE_CONFIG_NAME( camerica_aladdin );
}