summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/bus/nes/bandai.c
blob: e92b2ad33eda37c0771e792cb6da59697b2be466 (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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/***********************************************************************************************************


 NES/Famicom cartridge emulation for Bandai PCBs

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


 Here we emulate the following PCBs

 * Bandai FCG [mapper 16] (older design, regs are only accessed in 0x6000-0x7fff range)
 * Bandai LZ93D50 [mapper 16] (this extends FCG by solving issues when writing to 0x8000-0xffff)
 * Bandai LZ93D50 + 24C01 EEPROM [mapper 159]
 * Bandai LZ93D50 + 24C02 EEPROM [mapper 16]
 * Bandai Famicom Jump 2 (aka LZ93D50 + SRAM) [mapper 153]
 * Bandai Oeka Kids [mapper 96]

 * Bandai Datach Joint ROM System [mapper 157] is emulated in a separate source file
   to implement also the subslot, but the PCB is basically a Bandai LZ93D50 + 24C02 EEPROM
   pcb with added barcode reader and subslot
 
 * Bandai Karaoke Studio [mapper 188] is emulated in a separate source file
   to implement also the subslot and the mic inputs
 
 
 TODO:
 - investigate why EEPROM does not work
 - add support to the PPU for the code necessary to Oeka Kids games (also needed by UNL-DANCE2000 PCB)
 - check the cause for the flickering in Famicom Jump 2

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


#include "emu.h"
#include "bandai.h"

#include "cpu/m6502/m6502.h"

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

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



//-------------------------------------------------
//  constructor
//-------------------------------------------------

const device_type NES_OEKAKIDS = &device_creator<nes_oekakids_device>;
const device_type NES_FCG = &device_creator<nes_fcg_device>;
const device_type NES_LZ93D50 = &device_creator<nes_lz93d50_device>;
const device_type NES_LZ93D50_24C01 = &device_creator<nes_lz93d50_24c01_device>;
const device_type NES_LZ93D50_24C02 = &device_creator<nes_lz93d50_24c02_device>;
const device_type NES_FJUMP2 = &device_creator<nes_fjump2_device>;


nes_oekakids_device::nes_oekakids_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
					: nes_nrom_device(mconfig, NES_OEKAKIDS, "NES Cart Bandai Oeka Kids PCB", tag, owner, clock, "nes_oeka", __FILE__)
{
}

nes_fcg_device::nes_fcg_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)
					: nes_nrom_device(mconfig, type, name, tag, owner, clock, shortname, source)
{
}

nes_fcg_device::nes_fcg_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
					: nes_nrom_device(mconfig, NES_FCG, "NES Cart Bandai FCG PCB", tag, owner, clock, "nes_fcg", __FILE__)
{
}

nes_lz93d50_device::nes_lz93d50_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)
					: nes_fcg_device(mconfig, type, name, tag, owner, clock, shortname, source)
{
}

nes_lz93d50_device::nes_lz93d50_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
					: nes_fcg_device(mconfig, NES_LZ93D50, "NES Cart Bandai LZ93D50 PCB", tag, owner, clock, "nes_lz93d50", __FILE__)
{
}

nes_lz93d50_24c01_device::nes_lz93d50_24c01_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)
					: nes_lz93d50_device(mconfig, type, name, tag, owner, clock, shortname, source),
						m_i2cmem(*this, "i2cmem")
{
}

nes_lz93d50_24c01_device::nes_lz93d50_24c01_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
					: nes_lz93d50_device(mconfig, NES_LZ93D50_24C01, "NES Cart Bandai LZ93D50 + 24C01 PCB", tag, owner, clock, "nes_lz93d50_ep1", __FILE__),
						m_i2cmem(*this, "i2cmem")
{
}

nes_lz93d50_24c02_device::nes_lz93d50_24c02_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
					: nes_lz93d50_24c01_device(mconfig, NES_LZ93D50_24C02, "NES Cart Bandai LZ93D50 + 24C02 PCB", tag, owner, clock, "nes_lz93d50_ep2", __FILE__)
{
}

nes_fjump2_device::nes_fjump2_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
					: nes_lz93d50_device(mconfig, NES_FJUMP2, "NES Cart Bandai Famicom Jump II PCB", tag, owner, clock, "nes_fjump2", __FILE__)
{
}



void nes_oekakids_device::device_start()
{
	common_start();
	save_item(NAME(m_latch));
	save_item(NAME(m_reg));
}

void nes_oekakids_device::pcb_reset()
{
	prg32(0);
	chr4_0(0, CHRRAM);
	chr4_4(3, CHRRAM);
	set_nt_mirroring(PPU_MIRROR_LOW);
	m_latch = 0;
	m_reg = 0;
}

void nes_fcg_device::device_start()
{
	common_start();
	irq_timer = timer_alloc(TIMER_IRQ);
	irq_timer->adjust(attotime::zero, 0, machine().device<cpu_device>("maincpu")->cycles_to_attotime(1));

	save_item(NAME(m_irq_enable));
	save_item(NAME(m_irq_count));
}

void nes_fcg_device::pcb_reset()
{
	m_chr_source = m_vrom_chunks ? CHRROM : CHRRAM;
	prg16_89ab(0);
	prg16_cdef(m_prg_chunks - 1);
	chr8(0, m_chr_source);

	m_irq_enable = 0;
	m_irq_count = 0;
}

void nes_lz93d50_24c01_device::device_start()
{
	common_start();
	irq_timer = timer_alloc(TIMER_IRQ);
	irq_timer->adjust(attotime::zero, 0, machine().device<cpu_device>("maincpu")->cycles_to_attotime(1));

	save_item(NAME(m_irq_enable));
	save_item(NAME(m_irq_count));
	save_item(NAME(m_i2c_dir));
}

void nes_lz93d50_24c01_device::pcb_reset()
{
	m_chr_source = m_vrom_chunks ? CHRROM : CHRRAM;
	prg16_89ab(0);
	prg16_cdef(m_prg_chunks - 1);
	chr8(0, m_chr_source);

	m_irq_enable = 0;
	m_irq_count = 0;
	m_i2c_dir = 0;
}

void nes_fjump2_device::device_start()
{
	common_start();
	irq_timer = timer_alloc(TIMER_IRQ);
	irq_timer->adjust(attotime::zero, 0, machine().device<cpu_device>("maincpu")->cycles_to_attotime(1));

	save_item(NAME(m_reg));
}

void nes_fjump2_device::pcb_reset()
{
	chr8(0, CHRRAM);
	memset(m_reg, 0, sizeof(m_reg));
	set_prg();
}



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

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

 Bandai Oeka Kids board emulation

 Games: Oeka Kids - Anpanman no Hiragana Daisuki, Oeka
 Kids - Anpanman to Oekaki Shiyou!!

 This board can swap CHR whenever a PPU address line is
 changed and we still do not emulate this.

 iNES: mapper 96

 In MESS: Preliminary Support.

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


WRITE8_MEMBER(nes_oekakids_device::nt_w)
{
	int page = ((offset & 0xc00) >> 10);

#if 0
	if (!(offset & 0x1000) && (offset & 0x3ff) < 0x3c0)
	{
		m_latch = (offset & 0x300) >> 8;
		chr4_0(m_reg | m_latch, CHRRAM);
	}
#endif

	m_nt_access[page][offset & 0x3ff] = data;
}

READ8_MEMBER(nes_oekakids_device::nt_r)
{
	int page = ((offset & 0xc00) >> 10);

#if 0
	if (!(offset & 0x1000) && (offset & 0x3ff) < 0x3c0)
	{
		m_latch = (offset & 0x300) >> 8;
		chr4_0(m_reg | m_latch, CHRRAM);
	}
#endif

	return m_nt_access[page][offset & 0x3ff];
}

void nes_oekakids_device::update_chr()
{
	chr4_0(m_reg | m_latch, CHRRAM);
	chr4_4(m_reg | 0x03, CHRRAM);
}

// this only monitors accesses to $2007 while we would need to monitor accesses to $2006...
void nes_oekakids_device::ppu_latch(offs_t offset)
{
#if 0
	if ((offset & 0x3000) == 0x2000)
	{
		m_latch = (offset & 0x300) >> 8;
		update_chr();
	}
#endif
}

WRITE8_MEMBER(nes_oekakids_device::write_h)
{
	LOG_MMC(("oeka kids write_h, offset: %04x, data: %02x\n", offset, data));

	prg32(data & 0x03);
	m_reg = data & 0x04;
	update_chr();
}

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

 Bandai FCG / LZ93D50 boards emulation

 There are several variants: plain board with or without SRAM,
 board + 24C01 EEPROM, board + 24C02 EEPROM, board + Barcode
 Reader (DATACH).
 We currently only emulate the base hardware.

 Games: Crayon Shin-Chan - Ora to Poi Poi, Dragon Ball Z Gaiden,
 Dragon Ball Z II & III, Rokudenashi Blues, SD Gundam
 Gaiden - KGM2, Dragon Ball Z, Magical Taruruuto-kun, SD Gundam
 Gaiden [with EEPROM], Dragon Ball, Dragon Ball 3, Famicom Jump,
 Famicom Jump II [no EEPROM], Datach Games

 At the moment, we don't support EEPROM I/O

 iNES: mappers 16, 153 (see below), 157 & 159

 In MESS: Supported

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

void nes_fcg_device::device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr)
{
	if (id == TIMER_IRQ)
	{
		if (m_irq_enable)
		{
			// 16bit counter, IRQ fired when the counter goes from 1 to 0
			// after firing, the counter is *not* reloaded, but next clock
			// counter wraps around from 0 to 0xffff
			if (!m_irq_count)
				m_irq_count = 0xffff;
			else
				m_irq_count--;

			if (!m_irq_count)
			{
				machine().device("maincpu")->execute().set_input_line(M6502_IRQ_LINE, ASSERT_LINE);
				m_irq_enable = 0;
			}
		}
	}
}

WRITE8_MEMBER(nes_fcg_device::fcg_write)
{
	LOG_MMC(("lz93d50_write, offset: %04x, data: %02x\n", offset, data));

	switch (offset & 0x0f)
	{
		case 0: case 1: case 2: case 3:
		case 4: case 5: case 6: case 7:
			chr1_x(offset & 0x07, data, m_chr_source);
			break;
		case 8:
			prg16_89ab(data);
			break;
		case 9:
			switch (data & 0x03)
			{
				case 0: set_nt_mirroring(PPU_MIRROR_VERT); break;
				case 1: set_nt_mirroring(PPU_MIRROR_HORZ); break;
				case 2: set_nt_mirroring(PPU_MIRROR_LOW); break;
				case 3: set_nt_mirroring(PPU_MIRROR_HIGH); break;
			}
			break;
		case 0x0a:
			m_irq_enable = data & 0x01;
			machine().device("maincpu")->execute().set_input_line(M6502_IRQ_LINE, CLEAR_LINE);
			break;
		case 0x0b:
			m_irq_count = (m_irq_count & 0xff00) | data;
			break;
		case 0x0c:
			m_irq_count = (m_irq_count & 0x00ff) | (data << 8);
			break;
		default:
			logerror("lz93d50_write uncaught write, offset: %04x, data: %02x\n", offset, data);
			break;
	}
}

WRITE8_MEMBER(nes_fcg_device::write_m)
{
	LOG_MMC(("lz93d50 write_m, offset: %04x, data: %02x\n", offset, data));

	if (!m_battery && !m_prgram)
		fcg_write(space, offset & 0x0f, data, mem_mask);
	else if (m_battery)
		m_battery[offset] = data;
	else
		m_prgram[offset] = data;
}

// FCG board does not access regs in 0x8000-0xffff space!
// only later design lz93d50 (and its variants do)!

WRITE8_MEMBER(nes_lz93d50_24c01_device::write_h)
{
	LOG_MMC(("lz93d50_24c01 write_h, offset: %04x, data: %02x\n", offset, data));

	switch (offset & 0x0f)
	{
		case 0x0d:
			m_i2cmem->write_scl(BIT(data, 5));
			m_i2cmem->write_sda(BIT(data, 6));
			m_i2c_dir = BIT(data, 7);
			break;
		default:
			fcg_write(space, offset & 0x0f, data, mem_mask);
			break;
	}
}

READ8_MEMBER(nes_lz93d50_24c01_device::read_m)
{
	LOG_MMC(("lz93d50 EEPROM read, offset: %04x\n", offset));
	if (m_i2c_dir)
		return (m_i2cmem->read_sda() & 1) << 4;
	else
		return 0;
}

//-------------------------------------------------
//  SERIAL I2C DEVICE
//-------------------------------------------------

MACHINE_CONFIG_FRAGMENT( bandai_i2c_24c01 )
	MCFG_24C01_ADD("i2cmem")
MACHINE_CONFIG_END

machine_config_constructor nes_lz93d50_24c01_device::device_mconfig_additions() const
{
	return MACHINE_CONFIG_NAME( bandai_i2c_24c01 );
}


MACHINE_CONFIG_FRAGMENT( bandai_i2c_24c02 )
	MCFG_24C02_ADD("i2cmem")
MACHINE_CONFIG_END

machine_config_constructor nes_lz93d50_24c02_device::device_mconfig_additions() const
{
	return MACHINE_CONFIG_NAME( bandai_i2c_24c02 );
}


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

 Bandai BANDAI-JUMP2 boards emulation

 This is a variant of LZ93D50, with SRAM and no EEPROM
 The board is only used by Famicom Jump II, which
 has no CHR and 512K of PRG, so it is not completely
 clear if the CHR regs of LZ93D50 (i.e. offset & 0xf < 8)
 would switch also CHR or if they are only used to select
 upper 256K of PRG

 iNES: mappers 153

 In MESS: Supported

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

void nes_fjump2_device::set_prg()
{
	UINT8 prg_base = 0;

	for (int i = 0; i < 4; i++)
		prg_base |= (m_reg[i] << 4);

	prg16_89ab(prg_base | m_reg[4]);
	prg16_cdef(prg_base | 0x0f);
}

READ8_MEMBER(nes_fjump2_device::read_m)
{
	LOG_MMC(("fjump2 read_m, offset: %04x\n", offset));
	return m_battery[offset & (m_battery.count() - 1)];
}

WRITE8_MEMBER(nes_fjump2_device::write_m)
{
	LOG_MMC(("fjump2 write_m, offset: %04x, data: %02x\n", offset, data));
	m_battery[offset & (m_battery.count() - 1)] = data;
}

WRITE8_MEMBER(nes_fjump2_device::write_h)
{
	LOG_MMC(("fjump2 write_h, offset: %04x, data: %02x\n", offset, data));

	switch (offset & 0x0f)
	{
		case 0: case 1: case 2: case 3:
			m_reg[offset & 0x0f] = BIT(data,0);
			set_prg();
			break;
		case 4: case 5: case 6: case 7:
			// these have been verified to be disabled in this board
			break;
		case 8:
			m_reg[4] = data & 0x0f;
			set_prg();
			break;
		default:
			fcg_write(space, offset & 0x0f, data, mem_mask);
			break;
	}
}