summaryrefslogtreecommitdiffstatshomepage
path: root/src/mame/drivers/leapfrog_iquest.cpp
blob: 069b97ac0f0121c3a3925c852d4fcb4e9e9319e9 (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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
// license:BSD-3-Clause
// copyright-holders:David Haywood
/******************************************************************************

   Leapfrog IQuest

   has LCD display

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

#include "emu.h"

#include "cpu/mcs51/mcs51.h"

#include "bus/generic/slot.h"
#include "bus/generic/carts.h"
#include "machine/bankdev.h"

#include "screen.h"
#include "softlist_dev.h"
#include "speaker.h"
#include "screen.h"

class leapfrog_iquest_state : public driver_device
{
public:
	leapfrog_iquest_state(const machine_config &mconfig, device_type type, const char *tag)
		: driver_device(mconfig, type, tag)
		, m_cart(*this, "cartslot")
		, m_screen(*this, "screen")
		, m_maincpu(*this, "maincpu")
		, m_rombank(*this, "rombank")
		, m_cart_region(nullptr)
	{ }

	void leapfrog_iquest(machine_config &config);

protected:
	void leapfrog_base(machine_config &config);

	uint32_t screen_update(screen_device &screen, bitmap_rgb32 &bitmap, const rectangle &cliprect);

	required_device<generic_slot_device> m_cart;
	required_device<screen_device> m_screen;

	DECLARE_WRITE_LINE_MEMBER(rx_line_hack);

	DECLARE_DEVICE_IMAGE_LOAD_MEMBER(cart_load);

private:
	virtual void machine_start() override;
	virtual void machine_reset() override;

	void prog_map(address_map &map);
	void ext_map(address_map &map);

	required_device<mcs51_cpu_device> m_maincpu;

	void rom_map(address_map &map);

	required_device<address_map_bank_device> m_rombank;
	memory_region *m_cart_region;

	uint8_t m_lowerbank[2];
	uint8_t m_upperbank[2];

	uint8_t lowerbank_r(offs_t offset);
	uint8_t upperbank_r(offs_t offset);
	void lowerbank_w(offs_t offset, uint8_t data);
	void upperbank_w(offs_t offset, uint8_t data);

	uint8_t m_iobank[2];

	uint8_t iobank_r(offs_t offset);
	void iobank_w(offs_t offset, uint8_t data);


	uint8_t lowerwindow_r(offs_t offset);
	uint8_t upperwindow_r(offs_t offset);

	uint8_t iowindow_r(offs_t offset);
	void iowindow_w(offs_t offset, uint8_t data);

	uint8_t unk_ff80_r();
	void unk_ff80_w(uint8_t data);
	uint8_t m_ff80;

	uint8_t unk_fc00_r();
	uint8_t unk_fc01_r(offs_t offset);

	uint8_t unk_fc2f_r();
	uint8_t unk_fc3f_r();
	void unk_fc3f_w(uint8_t data);

	void unk_fc22_w(uint8_t data);

	uint8_t unk_fce5_r();
	void unk_fce5_w(uint8_t data);

	uint8_t m_fce5;

	uint8_t unk_ff00_01_r(offs_t offset);

	void unk_ff81_84_w(offs_t offset, uint8_t data);
	uint8_t m_ff81_84[4];

	uint8_t unk_ff91_93_r(offs_t offset);
	void unk_ff91_93_w(offs_t offset, uint8_t data);
	uint8_t m_ff91_93[3];

	uint8_t unk_ffa8_r();
	void unk_ffa8_w(uint8_t data);
	uint8_t m_ffa8;

	void unk_ffa9_w(uint8_t data);

	void tx(uint8_t data);
	uint8_t rx();


	uint8_t port0_r();
	void port0_w(u8 data);
	uint8_t port1_r();
	void port1_w(u8 data);
	uint8_t port2_r();
	void port2_w(u8 data);
	uint8_t port3_r();
	void port3_w(u8 data);
};


class leapfrog_turboextreme_state : public leapfrog_iquest_state
{
public:
	leapfrog_turboextreme_state(const machine_config &mconfig, device_type type, const char *tag)
		: leapfrog_iquest_state(mconfig, type, tag)
	{ }

	void leapfrog_turboex(machine_config &config);
};

class leapfrog_turbotwistmath_state : public leapfrog_iquest_state
{
public:
	leapfrog_turbotwistmath_state(const machine_config &mconfig, device_type type, const char *tag)
		: leapfrog_iquest_state(mconfig, type, tag)
	{ }

	void leapfrog_turbotwistmath(machine_config &config);
};

class leapfrog_turbotwistvocabulator_state : public leapfrog_iquest_state
{
public:
	leapfrog_turbotwistvocabulator_state(const machine_config &mconfig, device_type type, const char *tag)
		: leapfrog_iquest_state(mconfig, type, tag)
	{ }

	void leapfrog_turbotwistvocabulator(machine_config &config);
};

class leapfrog_turbotwistspelling_state : public leapfrog_iquest_state
{
public:
	leapfrog_turbotwistspelling_state(const machine_config &mconfig, device_type type, const char *tag)
		: leapfrog_iquest_state(mconfig, type, tag)
	{ }

	void leapfrog_turbotwistspelling(machine_config &config);
};


class leapfrog_turbotwistbrainquest_state : public leapfrog_iquest_state
{
public:
	leapfrog_turbotwistbrainquest_state(const machine_config &mconfig, device_type type, const char *tag)
		: leapfrog_iquest_state(mconfig, type, tag)
	{ }

	void leapfrog_turbotwistbrainquest(machine_config &config);
};


void leapfrog_iquest_state::machine_start()
{
	// if there's a cart, override the standard mapping
	if (m_cart && m_cart->exists())
	{
		m_cart_region = memregion(std::string(m_cart->tag()) + GENERIC_ROM_REGION_TAG);
	}
}

void leapfrog_iquest_state::machine_reset()
{
	m_lowerbank[0] = m_lowerbank[1] = 0x00;
	m_upperbank[0] = m_upperbank[1] = 0x00;
	m_iobank[0] = m_iobank[1] = 0x00;

	m_rombank->set_bank(0);
	m_ff80 = 0x00;

	m_fce5 = 0x00;

	m_ff91_93[0] = m_ff91_93[1] = m_ff91_93[2] = 0x00;
}


uint8_t leapfrog_iquest_state::lowerbank_r(offs_t offset)
{
	return m_lowerbank[offset];
}

uint8_t leapfrog_iquest_state::upperbank_r(offs_t offset)
{
	return m_upperbank[offset];
}


void leapfrog_iquest_state::lowerbank_w(offs_t offset, uint8_t data)
{
	m_lowerbank[offset] = data;
}

void leapfrog_iquest_state::upperbank_w(offs_t offset, uint8_t data)
{
	m_upperbank[offset] = data;
}

uint8_t leapfrog_iquest_state::iobank_r(offs_t offset)
{
	return m_iobank[offset];
}


void leapfrog_iquest_state::iobank_w(offs_t offset, uint8_t data)
{
	//printf("iobank_w %d, %02x\n", offset, data);
	m_iobank[offset] = data;
}


void leapfrog_iquest_state::rom_map(address_map &map)
{
	map(0x00000000, 0x003fffff).rom().region("maincpu", 0);
	map(0x00400000, 0x007fffff).ram();
}

uint8_t leapfrog_iquest_state::lowerwindow_r(offs_t offset)
{
	uint32_t bank = ((m_lowerbank[0] << 8) | (m_lowerbank[1])) * 0x8000;
	return m_rombank->read8(bank + offset);
}

uint8_t leapfrog_iquest_state::upperwindow_r(offs_t offset)
{
	uint32_t bank = ((m_upperbank[0] << 8) | (m_upperbank[1])) * 0x8000;
	return m_rombank->read8(bank + offset);
}

uint8_t leapfrog_iquest_state::iowindow_r(offs_t offset)
{
	uint32_t bank = ((m_iobank[0] << 8) | (m_iobank[1])) * 0x8000;
	return m_rombank->read8(bank + offset);
}

void leapfrog_iquest_state::iowindow_w(offs_t offset, uint8_t data)
{
	uint32_t bank = ((m_iobank[0] << 8) | (m_iobank[1])) * 0x8000;
	m_rombank->write8(bank + offset, data);
}


void leapfrog_iquest_state::prog_map(address_map &map)
{
	map(0x0000, 0x7fff).r(FUNC(leapfrog_iquest_state::lowerwindow_r));
	map(0x8000, 0xffff).r(FUNC(leapfrog_iquest_state::upperwindow_r));
}

uint8_t leapfrog_iquest_state::unk_ff80_r()
{
	logerror("%s: unk_ff80_r\n", machine().describe_context());
	return m_ff80;
}

void leapfrog_iquest_state::unk_ff80_w(uint8_t data)
{
	// must return what is written for some startup tests
	logerror("%s: m_ff80 %02x\n", machine().describe_context(), data);
	m_ff80 = data;
}

uint8_t leapfrog_iquest_state::unk_fc00_r()
{
	logerror("%s: unk_fc00_r\n", machine().describe_context());
	return 0x00;// machine().rand();
}

uint8_t leapfrog_iquest_state::unk_fc01_r(offs_t offset)
{
	logerror("%s: unk_fc01_r %d\n", machine().describe_context(), offset);
	return 0xff;// machine().rand();
}

uint8_t leapfrog_iquest_state::unk_fc2f_r()
{
	logerror("%s: unk_fc2f_r\n", machine().describe_context());
	return 0x00;// machine().rand();
}

uint8_t leapfrog_iquest_state::unk_fc3f_r()
{
	logerror("%s: unk_fc3f_r\n", machine().describe_context());
	return 0x00;// machine().rand();
}

void leapfrog_iquest_state::unk_fc3f_w(uint8_t data)
{
	logerror("%s: unk_fc3f_w %02x\n", machine().describe_context(), data);
}

void leapfrog_iquest_state::unk_fc22_w(uint8_t data)
{
	logerror("%s: unk_fc22_w %02x\n", machine().describe_context(), data);
}

uint8_t leapfrog_iquest_state::unk_ff00_01_r(offs_t offset)
{
	// read around the time of fc22 writes
	logerror("%s: unk_ff00_01_r %d\n", machine().describe_context(), offset);
	return 0x00;
}

uint8_t leapfrog_iquest_state::unk_fce5_r()
{
	logerror("%s: unk_fce5_r\n", machine().describe_context());
	return 0x00;// m_fce5;// machine().rand();
}

void leapfrog_iquest_state::unk_fce5_w(uint8_t data)
{
	// repeated read/write pattern on this address
	logerror("%s: unk_fce5_w %02x\n", machine().describe_context(), data);
	m_fce5 = data;
}

/*

in all cases ff91-ff93 writes appear to be an address in the current main space
which is 0x10000-0x17fff, 0x10000-0x17fff in ROM at the time of writing
or for the later writes  0x10000-0x17fff, 0x20000-0x27fff  (the b448 is from 0x23448 for example)

each of the blocks pointed to is preceded by a 0x00 byte? (maybe 0x00 is a terminator for previous block?)

the blocks being pointed at during startup are debug text, c style strings for 'printf' functions, complete with
formatting characters while the ff81 to ff84 area contains the parameters.

it seems likely this is just a RAM area and these are temporary storage for the debug print function,
either for futher processing and output to an actual debug console, or shared with another device (ff80, which is
possibly the start of this RAM area, is written after putting these pointers in RAM)

*/

uint8_t leapfrog_iquest_state::unk_ff91_93_r(offs_t offset)
{
	logerror("%s: unk_ff91_93_r %d\n", machine().describe_context(), offset);
	return 0x00;// m_ff91_93[offset];
}

void leapfrog_iquest_state::unk_ff91_93_w(offs_t offset, uint8_t data)
{
	// these 3 values are written together
	m_ff91_93[offset] = data;

	// form is ff then 2 other values, these are pointers into the main space it seems

	if (offset == 2)
	{
		logerror("%s: write to ff91 to ff93 region %02x %02x %02x (current banks are %08x %08x %08x)\n", machine().describe_context(), m_ff91_93[0], m_ff91_93[1], m_ff91_93[2], ((m_lowerbank[0] << 8) | (m_lowerbank[1])) * 0x8000, ((m_upperbank[0] << 8) | (m_upperbank[1])) * 0x8000, ((m_iobank[0] << 8) | (m_iobank[1])) * 0x8000);

		uint16_t pointer = (m_ff91_93[1] << 8) | (m_ff91_93[2]);

		address_space& spc = m_maincpu->space(AS_PROGRAM);
		char readdat = 0x00;

		std::string textout;
		do
		{
			readdat = spc.read_byte(pointer++);
			textout.append(1, readdat);
		} while (readdat != 0x00);
		logerror("%s: DEBUG MESSAGE: %s", machine().describe_context(), textout);
		logerror("\n");
	}
}

void leapfrog_iquest_state::unk_ff81_84_w(offs_t offset, uint8_t data)
{
	// these 4 values are written together, with FFA8 before and FFA9 after
	// form is usually 00 00 then 2 used values, maybe coordinates?
	// used in conjunction with unk_ff91_93_w writes above
	m_ff81_84[offset] = data;

	if (offset == 3)
	{
		logerror("%s: write to ff81 to ff84 region %02x %02x %02x %02x\n", machine().describe_context(), m_ff81_84[0], m_ff81_84[1], m_ff81_84[2], m_ff81_84[3]);

		uint16_t pointer = (m_ff81_84[2] << 8) | (m_ff81_84[3]);

		if (pointer != 0x00)
		{
			address_space& spc = m_maincpu->space(AS_PROGRAM);
			char readdat = 0x00;

			std::string textout;
			do
			{
				readdat = spc.read_byte(pointer++);
				textout.append(1, readdat);
			} while (readdat != 0x00);

			//logerror("%s: %s", machine().describe_context(), textout);
			//logerror("\n");
		}
	}
}

uint8_t leapfrog_iquest_state::unk_ffa8_r()
{
	logerror("%s: read from ffa8 ----------- POSSIBLE END OF DEBUG TEXT OPERATION?\n", machine().describe_context());
	return 0x00;
}

void leapfrog_iquest_state::unk_ffa8_w(uint8_t data)
{
	logerror("%s: write to ffa8 %02x ----------- POSSIBLE START OF DEBUG TEXT OPERATION?\n", machine().describe_context(), data);
	m_ffa8 = data;
}

void leapfrog_iquest_state::unk_ffa9_w(uint8_t data)
{
	logerror("%s: write to ffa9 %02x ----------- POSSIBLE TRIGGER DEBUG TEXT OPERATION?? (current banks are %08x %08x %08x)\n", machine().describe_context(), data, ((m_lowerbank[0] << 8) | (m_lowerbank[1])) * 0x8000, ((m_upperbank[0] << 8) | (m_upperbank[1])) * 0x8000, ((m_iobank[0] << 8) | (m_iobank[1])) * 0x8000);
}




void leapfrog_iquest_state::ext_map(address_map &map)
{
	map(0x0000, 0x7fff).rw(FUNC(leapfrog_iquest_state::iowindow_r), FUNC(leapfrog_iquest_state::iowindow_w)); // assume this accesses the same space, with different bank register

	map(0xc260, 0xc52f).ram(); // = clears 0x2d0 bytes (90*64 / 8) display buffer?
	map(0xc530, 0xc7ff).ram(); // = clears 0x2d0 bytes (90*64 / 8) display buffer?

	//map(0xf001, 0xf056).ram(); // written as a block
	map(0xf000, 0xf5ff).ram(); // ? 0xf400 - 0xf427 written as a block, other areas uncertain, might be more registers in here as there are reads too

	map(0xfc00, 0xfc00).r(FUNC(leapfrog_iquest_state::unk_fc00_r));
	map(0xfc01, 0xfc04).r(FUNC(leapfrog_iquest_state::unk_fc01_r));

	map(0xfc06, 0xfc07).rw(FUNC(leapfrog_iquest_state::lowerbank_r), FUNC(leapfrog_iquest_state::lowerbank_w)); // ROM / RAM window in main space at 0000-7fff
	map(0xfc08, 0xfc09).rw(FUNC(leapfrog_iquest_state::upperbank_r), FUNC(leapfrog_iquest_state::upperbank_w)); // ROM / RAM window in main space at 8000-ffff
	map(0xfc0a, 0xfc0b).rw(FUNC(leapfrog_iquest_state::iobank_r), FUNC(leapfrog_iquest_state::iobank_w)); // ROM / RAM window in ext space at 0000-7fff

	map(0xfc22, 0xfc22).w(FUNC(leapfrog_iquest_state::unk_fc22_w));

	map(0xfc2f, 0xfc2f).r(FUNC(leapfrog_iquest_state::unk_fc2f_r));
	map(0xfc3f, 0xfc3f).rw(FUNC(leapfrog_iquest_state::unk_fc3f_r), FUNC(leapfrog_iquest_state::unk_fc3f_w));

	map(0xfce5, 0xfce5).rw(FUNC(leapfrog_iquest_state::unk_fce5_r), FUNC(leapfrog_iquest_state::unk_fce5_w));

	map(0xff00, 0xff01).r(FUNC(leapfrog_iquest_state::unk_ff00_01_r));

	// it seems more likely that this is just RAM, as that after setting pointers in RAM, they're used to construct
	// strings to transmit over the serial.
	// however, mapping this area as RAM instead results in the program stalling much earlier, waiting for $24.3 to
	// be cleared.
	// 017658: 20 23 fd  jb    $24.3,$17658
	//
	//The only realistic place for this to be cleared is deep in the interrupt handler for
	// Serial Receive/Transmit
	// 010023: 02 76 9e  ljmp  $769E
	// (which eventually can reach)
	// 016991: c2 23     clr   $24.3
	// however I'm uncertain how to get the driver to trigger this at all.
	if (0)
	{
		map(0xff80, 0xffff).ram();
	}
	else
	{
		map(0xff80, 0xff80).rw(FUNC(leapfrog_iquest_state::unk_ff80_r), FUNC(leapfrog_iquest_state::unk_ff80_w));

		map(0xff81, 0xff84).w(FUNC(leapfrog_iquest_state::unk_ff81_84_w));

		map(0xff91, 0xff93).rw(FUNC(leapfrog_iquest_state::unk_ff91_93_r), FUNC(leapfrog_iquest_state::unk_ff91_93_w));

		map(0xffa8, 0xffa8).rw(FUNC(leapfrog_iquest_state::unk_ffa8_r), FUNC(leapfrog_iquest_state::unk_ffa8_w));
		map(0xffa9, 0xffa9).w(FUNC(leapfrog_iquest_state::unk_ffa9_w));
	}
}

DEVICE_IMAGE_LOAD_MEMBER(leapfrog_iquest_state::cart_load)
{
	uint32_t size = m_cart->common_get_size("rom");

	m_cart->rom_alloc(size, GENERIC_ROM16_WIDTH, ENDIANNESS_LITTLE);
	m_cart->common_load_rom(m_cart->get_rom_base(), size, "rom");

	return image_init_result::PASS;
}

static INPUT_PORTS_START( leapfrog_iquest )
INPUT_PORTS_END

uint32_t leapfrog_iquest_state::screen_update(screen_device &screen, bitmap_rgb32 &bitmap, const rectangle &cliprect)
{
	return 0;
}

// never triggered?
void leapfrog_iquest_state::tx(uint8_t data)
{
	logerror("%s: transmitting %02x\n", machine().describe_context().c_str(), data);
}

// never triggered?
uint8_t leapfrog_iquest_state::rx()
{
	logerror("%s: receiving\n", machine().describe_context().c_str());
	return machine().rand();
}


// doesn't help?
WRITE_LINE_MEMBER(leapfrog_iquest_state::rx_line_hack)
{
	/*
	m_maincpu->set_input_line(MCS51_RX_LINE, ASSERT_LINE);
	m_maincpu->set_input_line(MCS51_RX_LINE, CLEAR_LINE);
	*/

	if (0)
	{
		// HACK: force past the wait loop if we're treating ff80 - ffff as RAM
		address_space& spc = m_maincpu->space(AS_DATA);
		uint8_t readdat = spc.read_byte(0x24);
		readdat &= ~0x08;
		spc.write_byte(0x24, readdat);
	}
}

uint8_t leapfrog_iquest_state::port0_r()
{
	logerror("%s: port0_r\n", machine().describe_context());
	return 0x00;
}

uint8_t leapfrog_iquest_state::port1_r()
{
	logerror("%s: port1_r\n", machine().describe_context());
	return 0x00;
}

uint8_t leapfrog_iquest_state::port2_r()
{
	logerror("%s: port2_r\n", machine().describe_context());
	return 0x00;
}

uint8_t leapfrog_iquest_state::port3_r()
{
	logerror("%s: port3_r\n", machine().describe_context());
	return 0x00;
}

void leapfrog_iquest_state::port0_w(u8 data)
{
	logerror("%s: port0_w %02x\n", machine().describe_context(), data);
}

void leapfrog_iquest_state::port1_w(u8 data)
{
	logerror("%s: port1_w %02x\n", machine().describe_context(), data);
}

void leapfrog_iquest_state::port2_w(u8 data)
{
	logerror("%s: port2_w %02x\n", machine().describe_context(), data);
}

void leapfrog_iquest_state::port3_w(u8 data)
{
	logerror("%s: port3_w %02x\n", machine().describe_context(), data);
}



void leapfrog_iquest_state::leapfrog_base(machine_config &config)
{
	// seems to have an IRQ vector at 002b, which would suggest it's an 8052 or similar, rather than plain 8031?
	//I8052(config, m_maincpu, 96000000/10); // unknown clock
	I8032(config, m_maincpu, 96000000/10); // unknown clock
	m_maincpu->set_addrmap(AS_PROGRAM, &leapfrog_iquest_state::prog_map);
	m_maincpu->set_addrmap(AS_IO, &leapfrog_iquest_state::ext_map);
	m_maincpu->serial_tx_cb().set(FUNC(leapfrog_iquest_state::tx));
	m_maincpu->serial_rx_cb().set(FUNC(leapfrog_iquest_state::rx));
	m_maincpu->port_in_cb<0>().set(FUNC(leapfrog_iquest_state::port0_r));
	m_maincpu->port_out_cb<0>().set(FUNC(leapfrog_iquest_state::port0_w));
	m_maincpu->port_in_cb<1>().set(FUNC(leapfrog_iquest_state::port1_r));
	m_maincpu->port_out_cb<1>().set(FUNC(leapfrog_iquest_state::port1_w));
	m_maincpu->port_in_cb<2>().set(FUNC(leapfrog_iquest_state::port2_r));
	m_maincpu->port_out_cb<2>().set(FUNC(leapfrog_iquest_state::port2_w));
	m_maincpu->port_in_cb<3>().set(FUNC(leapfrog_iquest_state::port3_r));
	m_maincpu->port_out_cb<3>().set(FUNC(leapfrog_iquest_state::port3_w));

	ADDRESS_MAP_BANK(config, "rombank").set_map(&leapfrog_iquest_state::rom_map).set_options(ENDIANNESS_LITTLE, 8, 31, 0x80000000);
}

void leapfrog_iquest_state::leapfrog_iquest(machine_config &config)
{
	leapfrog_iquest_state::leapfrog_base(config);

	SCREEN(config, m_screen, SCREEN_TYPE_RASTER);
	m_screen->set_refresh_hz(60);
	m_screen->set_vblank_time(ATTOSECONDS_IN_USEC(10));
	m_screen->set_size(90, 64);
	m_screen->set_visarea(0, 90-1, 0, 64-1);
	m_screen->set_screen_update(FUNC(leapfrog_iquest_state::screen_update));
	m_screen->screen_vblank().set(FUNC(leapfrog_iquest_state::rx_line_hack));

	GENERIC_CARTSLOT(config, m_cart, generic_plain_slot, "leapfrog_iquest_cart");
	m_cart->set_width(GENERIC_ROM16_WIDTH);
	m_cart->set_device_load(FUNC(leapfrog_iquest_state::cart_load));

	SOFTWARE_LIST(config, "cart_list").set_original("leapfrog_iquest_cart");
}

void leapfrog_turboextreme_state::leapfrog_turboex(machine_config &config)
{
	leapfrog_iquest_state::leapfrog_base(config);

	SCREEN(config, m_screen, SCREEN_TYPE_RASTER);
	m_screen->set_refresh_hz(60);
	m_screen->set_vblank_time(ATTOSECONDS_IN_USEC(10));
	m_screen->set_size(64, 32); // unknown resolution, lower than iquest
	m_screen->set_visarea(0, 64-1, 0, 32-1);
	m_screen->set_screen_update(FUNC(leapfrog_turboextreme_state::screen_update));
	m_screen->screen_vblank().set(FUNC(leapfrog_turboextreme_state::rx_line_hack));

	GENERIC_CARTSLOT(config, m_cart, generic_plain_slot, "leapfrog_turboextreme_cart");
	m_cart->set_width(GENERIC_ROM16_WIDTH);
	m_cart->set_device_load(FUNC(leapfrog_turboextreme_state::cart_load));

	SOFTWARE_LIST(config, "cart_list").set_original("turboextreme_cart");
}

void leapfrog_turbotwistmath_state::leapfrog_turbotwistmath(machine_config &config)
{
	leapfrog_iquest_state::leapfrog_base(config);

	SCREEN(config, m_screen, SCREEN_TYPE_RASTER);
	m_screen->set_refresh_hz(60);
	m_screen->set_vblank_time(ATTOSECONDS_IN_USEC(10));
	m_screen->set_size(64, 8); // unknown resolution, single row display
	m_screen->set_visarea(0, 64-1, 0, 8-1);
	m_screen->set_screen_update(FUNC(leapfrog_turbotwistmath_state::screen_update));
	m_screen->screen_vblank().set(FUNC(leapfrog_turbotwistmath_state::rx_line_hack));

	GENERIC_CARTSLOT(config, m_cart, generic_plain_slot, "leapfrog_turbotwistmath_cart");
	m_cart->set_width(GENERIC_ROM16_WIDTH);
	m_cart->set_device_load(FUNC(leapfrog_turbotwistmath_state::cart_load));

	SOFTWARE_LIST(config, "cart_list").set_original("ttwist_math_cart");
}

void leapfrog_turbotwistspelling_state::leapfrog_turbotwistspelling(machine_config &config)
{
	leapfrog_iquest_state::leapfrog_base(config);

	SCREEN(config, m_screen, SCREEN_TYPE_RASTER);
	m_screen->set_refresh_hz(60);
	m_screen->set_vblank_time(ATTOSECONDS_IN_USEC(10));
	m_screen->set_size(64, 8); // unknown resolution, single row display
	m_screen->set_visarea(0, 64-1, 0, 8-1);
	m_screen->set_screen_update(FUNC(leapfrog_turbotwistspelling_state::screen_update));
	m_screen->screen_vblank().set(FUNC(leapfrog_turbotwistspelling_state::rx_line_hack));

	GENERIC_CARTSLOT(config, m_cart, generic_plain_slot, "leapfrog_turbotwistspelling_cart");
	m_cart->set_width(GENERIC_ROM16_WIDTH);
	m_cart->set_device_load(FUNC(leapfrog_turbotwistspelling_state::cart_load));

	SOFTWARE_LIST(config, "cart_list").set_original("ttwist_spelling_cart");
}

void leapfrog_turbotwistvocabulator_state::leapfrog_turbotwistvocabulator(machine_config &config)
{
	leapfrog_iquest_state::leapfrog_base(config);

	SCREEN(config, m_screen, SCREEN_TYPE_RASTER);
	m_screen->set_refresh_hz(60);
	m_screen->set_vblank_time(ATTOSECONDS_IN_USEC(10));
	m_screen->set_size(64, 8); // unknown resolution, single row display
	m_screen->set_visarea(0, 64-1, 0, 8-1);
	m_screen->set_screen_update(FUNC(leapfrog_turbotwistvocabulator_state::screen_update));
	m_screen->screen_vblank().set(FUNC(leapfrog_turbotwistvocabulator_state::rx_line_hack));

	GENERIC_CARTSLOT(config, m_cart, generic_plain_slot, "leapfrog_turbotwistvocabulator_cart");
	m_cart->set_width(GENERIC_ROM16_WIDTH);
	m_cart->set_device_load(FUNC(leapfrog_turbotwistvocabulator_state::cart_load));

	SOFTWARE_LIST(config, "cart_list").set_original("ttwist_vocabulator_cart");
}


void leapfrog_turbotwistbrainquest_state::leapfrog_turbotwistbrainquest(machine_config &config)
{
	leapfrog_iquest_state::leapfrog_base(config);

	SCREEN(config, m_screen, SCREEN_TYPE_RASTER);
	m_screen->set_refresh_hz(60);
	m_screen->set_vblank_time(ATTOSECONDS_IN_USEC(10));
	m_screen->set_size(64, 8); // unknown resolution, single row display
	m_screen->set_visarea(0, 64-1, 0, 8-1);
	m_screen->set_screen_update(FUNC(leapfrog_turbotwistbrainquest_state::screen_update));
	m_screen->screen_vblank().set(FUNC(leapfrog_turbotwistbrainquest_state::rx_line_hack));

	GENERIC_CARTSLOT(config, m_cart, generic_plain_slot, "leapfrog_turbotwistbrainquest_cart");
	m_cart->set_width(GENERIC_ROM16_WIDTH);
	m_cart->set_device_load(FUNC(leapfrog_turbotwistbrainquest_state::cart_load));

	SOFTWARE_LIST(config, "cart_list").set_original("ttwist_brainquest_cart");
}

ROM_START( iquest )
	ROM_REGION( 0x400000, "maincpu", ROMREGION_ERASEFF )
	ROM_LOAD( "iquest.bin", 0x000000, 0x400000, CRC(f785dc4e) SHA1(ec002c18df536737334fe6b7db0e7342bad7b66b))

	// there is also a 39vf512 flash containing user data
ROM_END

ROM_START( turboex )
	ROM_REGION( 0x400000, "maincpu", ROMREGION_ERASEFF )
	ROM_LOAD( "turbotwistextreme.bin", 0x000000, 0x400000, CRC(d7cabcff) SHA1(7813811c0518aba017f7a79fd477be5ed9fa7529))
ROM_END

ROM_START( ttwistm )
	ROM_REGION( 0x400000, "maincpu", ROMREGION_ERASEFF )
	ROM_LOAD( "turbotwistmath.bin", 0x000000, 0x200000, CRC(a21d3723) SHA1(d0ae245621d7bc92bdf9fd683908690db6e25133))
ROM_END

ROM_START( ttwistsp ) // PCB marks ROM glob as 8M
	ROM_REGION( 0x400000, "maincpu", ROMREGION_ERASEFF )
	ROM_LOAD( "ttspelling.bin", 0x000000, 0x100000, CRC(09715a8e) SHA1(5d7eb7b714b95012aeb03c37dd0b71a1c025bdda))
ROM_END

ROM_START( ttwistvc ) // PCB marks ROM glob as 8M
	ROM_REGION( 0x400000, "maincpu", ROMREGION_ERASEFF )
	ROM_LOAD( "vocabulator.bin", 0x000000, 0x100000, CRC(71f9c4c9) SHA1(8cafb42bd56c7db99949781e42b6ad4991ee2246))
ROM_END

ROM_START( ttwistbq )
	ROM_REGION( 0x400000, "maincpu", ROMREGION_ERASEFF )
	ROM_LOAD( "turbotwistbrainquest.bin", 0x000000, 0x200000, CRC(b184a517) SHA1(181975da58b3d117a389c54ac025b6a9b24342b2))
ROM_END

ROM_START( ttwistfb ) // PCB marks ROM glob as 16M
	ROM_REGION( 0x400000, "maincpu", ROMREGION_ERASEFF )
	ROM_LOAD( "factblaster.bin", 0x000000, 0x200000, CRC(8efd63f5) SHA1(2cbd1299006ad9743d846e774afe7a134ba0fce3))
ROM_END



//    year, name,        parent,    compat, machine,                         input,            class,                                init,       company,    fullname,                         flags
// it is unknown if the versions of IQuest without 4.0 on the case have different system ROM
CONS( 2004, iquest,      0,         0,      leapfrog_iquest,                 leapfrog_iquest,  leapfrog_iquest_state,                empty_init, "LeapFrog", "IQuest 4.0 (US)",                MACHINE_IS_SKELETON )

CONS( 2004, turboex,     0,         0,      leapfrog_turboex,                leapfrog_iquest,  leapfrog_turboextreme_state,          empty_init, "LeapFrog", "Turbo Extreme (US)",             MACHINE_IS_SKELETON )

// from a silver unit with orange lettering (there are different case styles, it is unknown if the software changed)
CONS( 2002, ttwistm,     0,         0,      leapfrog_turbotwistmath,         leapfrog_iquest,  leapfrog_turbotwistmath_state,        empty_init, "LeapFrog", "Turbo Twist Math (US)",          MACHINE_IS_SKELETON )

// Brain Quest / Fact Blaster are compatible with the same cartridges
CONS( 200?, ttwistfb,    0,         0,      leapfrog_turbotwistbrainquest,   leapfrog_iquest,  leapfrog_turbotwistbrainquest_state,  empty_init, "LeapFrog", "Turbo Twist Fact Blaster (US)",  MACHINE_IS_SKELETON )
CONS( 2002, ttwistbq,    0,         0,      leapfrog_turbotwistbrainquest,   leapfrog_iquest,  leapfrog_turbotwistbrainquest_state,  empty_init, "LeapFrog", "Turbo Twist Brain Quest (US)",   MACHINE_IS_SKELETON )

// from a green unit with blue edges
CONS( 2000, ttwistsp,    0,         0,      leapfrog_turbotwistspelling,     leapfrog_iquest,  leapfrog_turbotwistspelling_state,    empty_init, "LeapFrog", "Turbo Twist Spelling (US)",      MACHINE_IS_SKELETON )

CONS( 2001, ttwistvc,    0,         0,      leapfrog_turbotwistvocabulator,  leapfrog_iquest,  leapfrog_turbotwistvocabulator_state, empty_init, "LeapFrog", "Turbo Twist Vocabulator (US)",   MACHINE_IS_SKELETON )

// Undumped units

// These have 2 digit 7-seg style displays, it is unknown if they are on related hardware
// Twist & Shout Addition
// Twist & Shout Subtraction
// Twist & Shout Multiplication
// Twist & Shout Division

// This appears to have a 4 digit 7-seg style display, unknown if it is on related hardware
// Twist & Shout Phonics