summaryrefslogtreecommitdiffstatshomepage
path: root/src/mame/drivers/splus.cpp
blob: 2266caac22a5788815b194839d19a7564a63feae (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
// license:BSD-3-Clause
// copyright-holders:Jim Stolis
/**********************************************************************************

    S-PLUS (S+)

    Driver by Jim Stolis.

    --- Technical Notes ---

    Name:    S+
    Company: IGT - International Game Technology
    Year:    1994

    Hardware:

    CPU =  INTEL 83c02       ; I8052 compatible
    SND =  AY-3-8912         ; AY8910 compatible

    History:

***********************************************************************************/
#include "emu.h"

#include "cpu/mcs51/mcs51.h"
#include "machine/i2cmem.h"
#include "machine/nvram.h"
#include "sound/ay8910.h"
#include "emupal.h"
#include "screen.h"
#include "speaker.h"

#include "splus.lh"


#define DEBUG_OUTPUT 0

class splus_state : public driver_device
{
public:
	splus_state(const machine_config &mconfig, device_type type, const char *tag)
		: driver_device(mconfig, type, tag)
		, m_cmosl_ram(*this, "cmosl")
		, m_cmosh_ram(*this, "cmosh")
		, m_program_ram(*this, "program_ram")
		, m_reel_ram(*this, "reel_ram")
		, m_i10(*this, "I10")
		, m_i20(*this, "I20")
		, m_i30(*this, "I30")
		, m_sensor(*this, "SENSOR")
		, m_maincpu(*this, "maincpu")
		, m_i2cmem(*this, "i2cmem")
		, m_digits(*this, "digit%u", 0U)
		, m_leds(*this, "s_bnk%u%u", 0U, 0U)
	{
		m_sda_dir = 0;
		m_coin_state = 0;
		m_last_cycles = 0;

		m_bank10 = 0x00;
		m_bank20 = 0x00;
		m_bank30 = 0x00;
		m_bank40 = 0x00;

		m_io_port1 = 0x00;
		m_p1_reels = 0x00;
		m_p1_unknown = 0x00;
	}

	void splus(machine_config &config);

	void init_splus();

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

	DECLARE_WRITE8_MEMBER(splus_p1_w);
	DECLARE_WRITE8_MEMBER(splus_load_pulse_w);
	DECLARE_WRITE8_MEMBER(splus_serial_w);
	DECLARE_WRITE8_MEMBER(splus_7seg_w);
	DECLARE_WRITE8_MEMBER(splus_duart_w);
	DECLARE_READ8_MEMBER(splus_serial_r);
	DECLARE_READ8_MEMBER(splus_m_reel_ram_r);
	DECLARE_READ8_MEMBER(splus_p3_r);
	DECLARE_READ8_MEMBER(splus_duart_r);
	DECLARE_READ8_MEMBER(splus_watchdog_r);
	DECLARE_READ8_MEMBER(splus_registers_r);
	DECLARE_WRITE8_MEMBER(i2c_nvram_w);
	DECLARE_READ8_MEMBER(splus_reel_optics_r);

	void splus_iomap(address_map &map);
	void splus_map(address_map &map);

	// EEPROM States
	int m_sda_dir;

	// Coin-In States
	uint8_t m_coin_state;
	uint64_t m_last_cycles;
	uint64_t m_last_coin_out;
	uint8_t m_coin_out_state;

	uint8_t m_bank10;
	uint8_t m_bank20;
	uint8_t m_bank30;
	uint8_t m_bank40;

	uint8_t m_io_port1;
	uint8_t m_p1_reels;
	uint8_t m_p1_unknown;

	int16_t m_stepper_pos[5];
	uint8_t m_stop_pos[5];

	virtual void machine_start() override
	{
		m_digits.resolve();
		m_leds.resolve();
	}

	// Pointers to External RAM
	required_shared_ptr<uint8_t> m_cmosl_ram;
	required_shared_ptr<uint8_t> m_cmosh_ram;

	// Program and Reel Data
	optional_shared_ptr<uint8_t> m_program_ram;
	required_shared_ptr<uint8_t> m_reel_ram;

	// IO Ports
	required_ioport m_i10;
	required_ioport m_i20;
	required_ioport m_i30;
	required_ioport m_sensor;
	required_device<i80c32_device> m_maincpu;
	required_device<x2404p_device> m_i2cmem;
	output_finder<9> m_digits;
	output_finder<5,8> m_leds;
};

/* Static Variables */
#define MAX_STEPPER         200     // 1.8 Degree Stepper Motor = 200 full-steps per revolution, but 400 when in half-step mode
#define STEPPER_DIVISOR     9.09 //18.18    // To allow for 22 stop positions

static const uint8_t optics[200] = {
	0x07, 0x07, 0x00, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x07, 0x07, 0x00, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x07, 0x07, 0x00, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x07, 0x07, 0x00, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x07, 0x07, 0x00, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07,
	0x07, 0x07, 0x00, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x07, 0x07, 0x00, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x07, 0x07, 0x00, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x07, 0x07, 0x00, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x07, 0x07, 0x00, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x07, 0x07, 0x00, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x00, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07 };

#define MASTER_CLOCK        XTAL(20'000'000)
#define CPU_CLOCK           ((MASTER_CLOCK)/2)      /* divided by 2 - 7474 */
#define SOUND_CLOCK         ((MASTER_CLOCK)/12)


/*****************
* Write Handlers *
******************/

WRITE8_MEMBER(splus_state::splus_p1_w)
{
	// P1.0 = Reel 1 Controller
	// P1.1 = Reel 2 Controller
	// P1.2 = Reel 3 Controller
	// P1.3 = Reel 4 Controller
	// P1.4 = Reel 5 Controller
	// P1.5 = 7-seg display, door
	// P1.6 = 7-seg display, prog
	// P1.7 = Unknown

	m_io_port1 = data;

	// Process Port 1
	if ((data & 0x1f) != 0x00) {
		// Unknown Bit 7
		m_p1_unknown = (~data & 0x80);

		// Stepper Motor Engaged
		if (BIT(m_bank40, 0))
		{
			// Reel Controllers Only
			m_p1_reels = (data & 0x1f);

			// Loop through Reel Controllers
			for (int x = 0; x < 5; x++)
			{
				// Test Reel Controller
				if (BIT(m_p1_reels, x))
				{
					// Forward Direction
					if (BIT(m_bank10, 5))
					{
						m_stepper_pos[x]++;
						if (m_stepper_pos[x] == MAX_STEPPER)
							m_stepper_pos[x] = 0;
					}
					else
					{
						m_stepper_pos[x]--;
						if (m_stepper_pos[x] < 0)
							m_stepper_pos[x] = MAX_STEPPER - 1;
					}
					m_stop_pos[x] = (int)(m_stepper_pos[x] / STEPPER_DIVISOR);
				}
			}
		}
#if DEBUG_OUTPUT
		if ((data & 0x1f) == 0x01)
			osd_printf_info("Steppers %02X-%02X-%02X-%02X-%02X Motor=%02X Dir=%02X reels=%02X unk=%02X\n", m_stop_pos[0],m_stop_pos[1],m_stop_pos[2],m_stop_pos[3],m_stop_pos[4],BIT(m_bank40, 0),BIT(m_bank10, 5),(data & 0x1f), m_p1_unknown);
#endif
	}
}

WRITE8_MEMBER(splus_state::splus_load_pulse_w)
{
//  uint8_t out = 0;
//    out = ((~m_io_port1 & 0xf0)>>4); // Output Bank
}

WRITE8_MEMBER(splus_state::splus_serial_w)
{
	uint8_t out = 0;
	out = ((~m_io_port1 & 0xe0)>>5); // Output Bank

	switch (out)
	{
		case 0x00: // Bank 10
#if DEBUG_OUTPUT
			if (BIT(m_bank10, 0) != BIT(data, 0)) {
				osd_printf_info("Coin Drop Meter =%02X\n",BIT(data, 0);
			}
			if (BIT(m_bank10, 1) != BIT(data, 1)) {
				osd_printf_info("Coin Out Meter =%02X\n",BIT(data, 1);
			}
			if (BIT(m_bank10, 2) != BIT(data, 2)) {
				osd_printf_info("Coin In Meter =%02X\n",BIT(data, 2);
			}
			if (BIT(m_bank10, 3) != BIT(data, 3)) {
				//osd_printf_info("B Switch for SDS =%02X\n",BIT(data, 3);
			}
			if (BIT(m_bank10, 4) != BIT(data, 4)) {
				osd_printf_info("Hopper Drive 2 =%02X\n",BIT(data, 4);
			}
			if (BIT(m_bank10, 5) != BIT(data, 5)) {
				osd_printf_info("Stepper Motor Direction =%02X\n",BIT(data, 5);
			}
			if (BIT(m_bank10, 6) != BIT(data, 6)) {
				osd_printf_info("Mechanical Bell =%02X\n",BIT(data, 6);
			}
			if (BIT(m_bank10, 7) != BIT(data, 7)) {
				osd_printf_info("Cancelled Credits Meter =%02X\n",BIT(data, 7);
			}
#endif
			m_bank10 = data;

			m_leds[1][0] = BIT(data, 0); // Coin Drop Meter
			m_leds[1][1] = BIT(data, 1); // Coin Out Meter
			m_leds[1][2] = BIT(data, 2); // Coin In Meter
			m_leds[1][3] = BIT(data, 3); // B Switch for SDS
			m_leds[1][4] = BIT(data, 4); // Hopper Drive 2
			m_leds[1][5] = BIT(data, 5); // Stepper Motor Direction
			m_leds[1][6] = BIT(data, 6); // Mechanical Bell
			m_leds[1][7] = BIT(data, 7); // Cancelled Credits Meter
			break;
		case 0x01: // Bank 20
#if DEBUG_OUTPUT
			if (BIT(m_bank20, 5) != BIT(data, 5)) {
				osd_printf_info("Games Played Meter =%02X\n",BIT(data, 5);
			}
			if (BIT(m_bank20, 6) != BIT(data, 6)) {
				osd_printf_info("Bill Acceptor Enable =%02X\n",BIT(data, 6);
			}
			if (BIT(m_bank20, 7) != BIT(data, 7)) {
				osd_printf_info("Jackpots Meter =%02X\n",BIT(data, 7);
			}
#endif
			m_bank20 = data;

			m_leds[2][0] = BIT(data, 0); // Payline Lamp 3
			m_leds[2][1] = BIT(data, 1); // Payline Lamp 4
			m_leds[2][2] = BIT(data, 2); // Payline Lamp 5
			m_leds[2][3] = BIT(data, 3); // Payline Lamp 6
			m_leds[2][4] = BIT(data, 4); // Door Optics Transmitter
			m_leds[2][5] = BIT(data, 5); // Games Played Meter
			m_leds[2][6] = BIT(data, 6); // Bill Acceptor Enable
			m_leds[2][7] = BIT(data, 7); // Jackpots Meter
			break;
		case 0x02: // Bank 30
#if DEBUG_OUTPUT
			if (BIT(m_bank30, 2) != BIT(data, 2)) {
				osd_printf_info("Handle Release =%02X\n",BIT(data, 2);
			}
			if (BIT(m_bank30, 3) != BIT(data, 3)) {
				osd_printf_info("Diverter =%02X\n",BIT(data, 3);
			}
			if (BIT(m_bank30, 4) != BIT(data, 4)) {
				osd_printf_info("Coin Lockout =%02X\n",BIT(data, 4);
			}
			if (BIT(m_bank30, 5) != BIT(data, 5)) {
				osd_printf_info("Hopper Drive 1 =%02X\n",BIT(data, 5);
			}
#endif
			m_bank30 = data;

			m_leds[3][0] = BIT(data, 0); // Change Candle Lamp Bottom
			m_leds[3][1] = BIT(data, 1); // Change Candle Lamp Top
			m_leds[3][2] = BIT(data, 2); // Handle Release
			m_leds[3][3] = BIT(data, 3); // Diverter
			m_leds[3][4] = BIT(data, 4); // Coin Lockout
			m_leds[3][5] = BIT(data, 5); // Hopper Drive 1
			m_leds[3][6] = BIT(data, 6); // Payline Lamp 1
			m_leds[3][7] = BIT(data, 7); // Payline Lamp 2
			break;
		case 0x04: // Bank 40
#if DEBUG_OUTPUT
			if (BIT(m_bank40, 0) != BIT(data, 0)) {
				osd_printf_info("Stepper Motor Power Supply =%02X\n",BIT(data, 0);
			}
			if (BIT(m_bank40, 3) != BIT(data, 3)) {
				osd_printf_info("Jackpot/Hand Pay Lamp =%02X\n",BIT(data, 3);
			}
#endif
			m_bank40 = data;

			m_leds[4][0] = BIT(data, 0); // Stepper Motor Power Supply
			m_leds[4][1] = BIT(data, 1); // Insert Coin Lamp
			m_leds[4][2] = BIT(data, 2); // Coin Accepted Lamp
			m_leds[4][3] = BIT(data, 3); // Jackpot/Hand Pay Lamp
			m_leds[4][4] = BIT(data, 4); // Play Max Credits Lamp
			m_leds[4][5] = BIT(data, 5); // Bet One Credit Lamp
			m_leds[4][6] = BIT(data, 6); // Cashout Credit Lamp
			m_leds[4][7] = BIT(data, 7); // Spin Button Lamp
			break;
	}
}

WRITE8_MEMBER(splus_state::splus_7seg_w)
{
	static const uint8_t ls48_map[16] = { 0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7c,0x07,0x7f,0x67,0x58,0x4c,0x62,0x69,0x78,0x00 };

	uint8_t seg;
	uint8_t val;

	seg = ((~data & 0xf0)>>4); // Segment Number
	val = (~data & 0x0f); // Digit Value

	// Need to add ~m_io_port1-1 to seg value
	if (seg < 9 && (m_io_port1 & 0xe0) == 0xe0)
		m_digits[seg] = ls48_map[val];
}

WRITE8_MEMBER(splus_state::splus_duart_w)
{
	// Used for Slot Accounting System Communication
}

WRITE8_MEMBER(splus_state::i2c_nvram_w)
{
	m_i2cmem->write_scl(BIT(data, 2));
	m_sda_dir = BIT(data, 1);
	m_i2cmem->write_sda(BIT(data, 0));
}

/****************
* Read Handlers *
****************/

READ8_MEMBER(splus_state::splus_serial_r)
{
	uint8_t coin_out = 0x00;
	uint8_t coin_optics = 0x00;
	uint8_t door_optics = 0x00;
	uint32_t curr_cycles = m_maincpu->total_cycles();

	uint8_t in = 0x00;
	uint8_t val = 0x00;
	in = ((~m_io_port1 & 0xe0)>>5); // Input Bank

	switch (in)
	{
		case 0x00: // Bank 40
			// Reel #1 - 0=Low State, 1=High State
			// Reel #2 - The state of Reel 1-5 inputs depends upon where each reel has stopped
			// Reel #3
			// Reel #4
			// Reel #5
			// Unknown
			// Unknown
			// Unknown
			val = 0xaa;
			break;
		case 0x01: // Bank 10
			// Test for Coin-In
			if ((m_sensor->read() & 0x01) == 0x01 && m_coin_state == 0) {
				m_coin_state = 1; // Start Coin Cycle
				m_last_cycles = m_maincpu->total_cycles();
#if DEBUG_OUTPUT
				osd_printf_info("coin=%02X\n", m_coin_state);
#endif
			} else {
				/* Process Next Coin Optic State */
				if (curr_cycles - m_last_cycles > 10000 && m_coin_state != 0) {
					m_coin_state++;
					if (m_coin_state > 5)
						m_coin_state = 0;
					m_last_cycles = m_maincpu->total_cycles();
#if DEBUG_OUTPUT
					osd_printf_info("coin=%02X\n", m_coin_state);
#endif
				}
			}

			// Set Coin State
			switch (m_coin_state)
			{
				case 0x00: // No Coin
					coin_optics = 0x00;
					break;
				case 0x01: // Optic A
					coin_optics = 0x01;
					break;
				case 0x02: // Optic AB
					coin_optics = 0x03;
					break;
				case 0x03: // Optic ABC
					coin_optics = 0x07;
					break;
				case 0x04: // Optic BC
					coin_optics = 0x06;
					break;
				case 0x05: // Optic C
					coin_optics = 0x04;
					break;
			}

			// Determine Door Optics
			if ((m_i10->read() & 0x08) == 0x08)
				door_optics = 0x08;
			else
				door_optics = BIT(m_bank20, 4) << 3; // Use Door Optics Transmitter

			// Test if Hopper 1 and Hopper 2 Motors On
			if (BIT(m_bank10, 4) || BIT(m_bank30, 5)) {
				if (m_coin_out_state == 0)
					m_coin_out_state = 3;
			} else {
				m_coin_out_state = 0;
			}

			// Process Coin Out
			if (curr_cycles - m_last_coin_out > 700000 && m_coin_out_state != 0) {
				if (m_coin_out_state != 2) {
					m_coin_out_state = 2; // Coin-Out Off
				} else {
					m_coin_out_state = 3; // Coin-Out On
				}

				m_last_coin_out = m_maincpu->total_cycles();
			}

			// Set Coin Out State
			switch (m_coin_out_state)
			{
				case 0x00: // No Coin-Out
					coin_out = 0x00;
					break;
				case 0x01: // First Coin-Out On
					coin_out = 0x10;
					break;
				case 0x02: // Coin-Out Off
					coin_out = 0x00;
					break;
				case 0x03: // Additional Coin-Out On
					coin_out = 0x10;
					break;
			}

			val = val | coin_optics; // Coin In A B C
			val = val | door_optics; // Door Optics Receiver
			val = val | coin_out; // Hopper Coin OutR
			val = val | 0x00; // Hopper Full
			val = val | (m_i10->read() & 0x40); // Handle/Spin Button
			val = val | (m_i10->read() & 0x80); // Jackpot Reset Key
			break;
		case 0x02: // Bank 20
			val = val | (m_i20->read() & 0x01); // Bet One Credit
			val = val | (m_i20->read() & 0x02); // Play Max Credits
			val = val | (m_i20->read() & 0x04); // Cash Out
			val = val | (m_i20->read() & 0x08); // Change Request
			val = val | 0x00; // Reel Mechanism
			val = val | (m_i20->read() & 0x20); // Self Test Button
			val = val | 0x40; // Card Cage
			val = val | 0x80; // Bill Acceptor
			break;
		case 0x04: // Bank 30
			// Reserved
			val = val | (m_i30->read() & 0x02); // Drop Door
			// Jackpot to Credit Key
			// Reserved
			// Reserved
			// Reserved
			// Reserved
			// Reserved
			break;
	}
	return val;
}

READ8_MEMBER(splus_state::splus_m_reel_ram_r)
{
	return m_reel_ram[offset];
}

READ8_MEMBER(splus_state::splus_p3_r)
{
	return 0xf3; // Ignore Int0 and Int1, or machine will loop forever waiting
}

READ8_MEMBER(splus_state::splus_duart_r)
{
	// Used for Slot Accounting System Communication
	return 0x00;
}

READ8_MEMBER(splus_state::splus_watchdog_r)
{
	return 0x00; // Watchdog
}

READ8_MEMBER(splus_state::splus_registers_r)
{
	return 0xff; // Reset Registers in Real Time Clock
}

READ8_MEMBER(splus_state::splus_reel_optics_r)
{
/*
        Bit 0 = REEL #1
        Bit 1 = REEL #2
        Bit 2 = REEL #3
        Bit 3 = REEL #4
        Bit 4 = REEL #5
        Bit 5 = ???
        Bit 6 = ???
        Bit 7 = I2C EEPROM SDA
*/
	uint8_t reel_optics = 0x00;
	uint8_t sda = 0;

	// Return Reel Positions
	reel_optics = (optics[199-(m_stepper_pos[4])] & 0x10) | (optics[199-(m_stepper_pos[3])] & 0x08) | (optics[199-(m_stepper_pos[2])] & 0x04) | (optics[199-(m_stepper_pos[1])] & 0x02) | (optics[199-(m_stepper_pos[0])] & 0x01);

	if(!m_sda_dir)
	{
		sda = m_i2cmem->read_sda();
	}

	reel_optics = reel_optics | 0x40 | (sda<<7);

	return reel_optics;
}

/**************
* Driver Init *
***************/

void splus_state::init_splus()
{
	uint8_t *reel_data = memregion( "reeldata" )->base();

	// Load Reel Data
	memcpy(m_reel_ram, &reel_data[0x0000], 0x2000);
}


/*************************
* Memory map information *
*************************/

void splus_state::splus_map(address_map &map)
{
	map(0x0000, 0xffff).rom().share("prograram");
}

void splus_state::splus_iomap(address_map &map)
{
	// Serial I/O
	map(0x0000, 0x0000).r(FUNC(splus_state::splus_serial_r)).w(FUNC(splus_state::splus_serial_w));

	// Battery-backed RAM (Lower 4K) 0x1500-0x16ff eeprom staging area
	map(0x1000, 0x1fff).ram().share("cmosl");

	// Watchdog, 7-segment Display
	map(0x2000, 0x2000).rw(FUNC(splus_state::splus_watchdog_r), FUNC(splus_state::splus_7seg_w));

	// DUART
	map(0x3000, 0x300f).rw(FUNC(splus_state::splus_duart_r), FUNC(splus_state::splus_duart_w));

	// Dip Switches, Sound
	map(0x4000, 0x4000).portr("SW1").w("aysnd", FUNC(ay8910_device::address_w));
	map(0x4001, 0x4001).w("aysnd", FUNC(ay8910_device::data_w));

	// Reel Optics, EEPROM
	map(0x5000, 0x5000).r(FUNC(splus_state::splus_reel_optics_r)).w(FUNC(splus_state::i2c_nvram_w));

	// Reset Registers in Realtime Clock, Serial I/O Load Pulse
	map(0x6000, 0x6000).rw(FUNC(splus_state::splus_registers_r), FUNC(splus_state::splus_load_pulse_w));

	// Battery-backed RAM (Upper 4K)
	map(0x7000, 0x7fff).ram().share("cmosh");

	// SSxxxx Reel Chip
	map(0x8000, 0x9fff).r(FUNC(splus_state::splus_m_reel_ram_r)).share("reel_ram");
}

/*************************
*      Input ports       *
*************************/

static INPUT_PORTS_START( splus )
	PORT_START("I10")
	PORT_BIT( 0x08, IP_ACTIVE_HIGH, IPT_UNKNOWN ) PORT_NAME("Door") PORT_CODE(KEYCODE_O) PORT_TOGGLE
	PORT_BIT( 0x40, IP_ACTIVE_LOW, IPT_UNKNOWN ) PORT_NAME("Spin") PORT_CODE(KEYCODE_Q)
	PORT_BIT( 0x80, IP_ACTIVE_LOW, IPT_UNKNOWN ) PORT_NAME("Jackpot Reset") PORT_CODE(KEYCODE_L)

	PORT_START("I20")
	PORT_BIT( 0x01, IP_ACTIVE_LOW, IPT_UNKNOWN ) PORT_NAME("Play Credit") PORT_CODE(KEYCODE_R)
	PORT_BIT( 0x02, IP_ACTIVE_LOW, IPT_UNKNOWN ) PORT_NAME("Max Bet") PORT_CODE(KEYCODE_W)
	PORT_BIT( 0x04, IP_ACTIVE_LOW, IPT_UNKNOWN ) PORT_NAME("Cashout") PORT_CODE(KEYCODE_T)
	PORT_BIT( 0x08, IP_ACTIVE_LOW, IPT_UNKNOWN ) PORT_NAME("Change Request") PORT_CODE(KEYCODE_Y)
	PORT_BIT( 0x20, IP_ACTIVE_LOW, IPT_UNKNOWN ) PORT_NAME("Self Test") PORT_CODE(KEYCODE_K)

	PORT_START("I30")
	PORT_BIT( 0x02, IP_ACTIVE_HIGH, IPT_UNKNOWN ) PORT_NAME("Drop Door") PORT_CODE(KEYCODE_D) PORT_TOGGLE

	PORT_START("SENSOR")
	PORT_BIT( 0x01, IP_ACTIVE_HIGH, IPT_COIN1) PORT_NAME("Coin In") PORT_IMPULSE(1)

	PORT_START("SW1")
	PORT_DIPNAME( 0x01, 0x01, "Hopper Limit 1" )
	PORT_DIPSETTING(    0x01, DEF_STR( Off ) )
	PORT_DIPSETTING(    0x00, DEF_STR( On ) )
	PORT_DIPNAME( 0x02, 0x02, "Hopper Limit 2" )
	PORT_DIPSETTING(    0x02, DEF_STR( Off ) )
	PORT_DIPSETTING(    0x00, DEF_STR( On ) )
	PORT_DIPNAME( 0x04, 0x04, "Sound Generator" )
	PORT_DIPSETTING(    0x04, DEF_STR( Off ) )
	PORT_DIPSETTING(    0x00, DEF_STR( On ) )
	PORT_DIPNAME( 0x08, 0x08, "Game Speed/Bills to Hopper" ) // Either Game Speed or Bills to Hopper depending on game
	PORT_DIPSETTING(    0x08, "Normal/Set in Self Test" )
	PORT_DIPSETTING(    0x00, "Fast/Auto Exchange" )
	PORT_DIPNAME( 0x10, 0x10, "Progressives" )
	PORT_DIPSETTING(    0x10, DEF_STR( None ) )
	PORT_DIPSETTING(    0x00, "Allow" )
	PORT_DIPNAME( 0x20, 0x20, "High/Low Progressives" )
	PORT_DIPSETTING(    0x20, "Single Level Alternating" )
	PORT_DIPSETTING(    0x00, "High/Low" )
	PORT_DIPNAME( 0x40, 0x40, "Double Progressives" )
	PORT_DIPSETTING(    0x40, DEF_STR( Single ) )
	PORT_DIPSETTING(    0x00, "Double" )
	PORT_DIPNAME( 0x80, 0x80, "Link Progressives" )
	PORT_DIPSETTING(    0x80, "Standalone" )
	PORT_DIPSETTING(    0x00, "Link" )
INPUT_PORTS_END


/*************************
*     Machine Driver     *
*************************/

void splus_state::splus(machine_config &config) // basic machine hardware
{
	I80C32(config, m_maincpu, CPU_CLOCK);
	m_maincpu->set_addrmap(AS_PROGRAM, &splus_state::splus_map);
	m_maincpu->set_addrmap(AS_IO, &splus_state::splus_iomap);
	m_maincpu->port_out_cb<1>().set(FUNC(splus_state::splus_p1_w));
	m_maincpu->port_in_cb<3>().set(FUNC(splus_state::splus_p3_r));

	// Fill NVRAM
	NVRAM(config, "cmosl", nvram_device::DEFAULT_ALL_0);
	NVRAM(config, "cmosh", nvram_device::DEFAULT_ALL_0);

	// video hardware (ALL FAKE, NO VIDEO)
	PALETTE(config, "palette", 16*16);

	screen_device &screen(SCREEN(config, "screen", SCREEN_TYPE_RASTER));
	screen.set_refresh_hz(60);
	screen.set_vblank_time(ATTOSECONDS_IN_USEC(0));
	screen.set_screen_update(FUNC(splus_state::screen_update));
	screen.set_size((52+1)*8, (31+1)*8);
	screen.set_visarea(0*8, 40*8-1, 0*8, 25*8-1);
	screen.set_palette("palette");

	X2404P(config, m_i2cmem);

	// sound hardware
	SPEAKER(config, "mono").front_center();

	AY8912(config, "aysnd", SOUND_CLOCK).add_route(ALL_OUTPUTS, "mono", 0.75);
}

/*************************
*        Rom Load        *
*************************/

ROM_START( spset005 ) /* Set Chip, not sure how it works but archive for future use */
	ROM_REGION( 0x10000, "maincpu", 0 )
	ROM_LOAD( "set005.u52",   0x00000, 0x10000, CRC(43b70e2e) SHA1(30d610c988fc7e7e9fd54ff378c4a1fecfe5fffe) ) /* 09/24/93   @ IGT L93-1769 */
	ROM_LOAD( "set017.u52",   0x00000, 0x10000, NO_DUMP ) /* When dumped make a seperate romdef */
	ROM_LOAD( "set020.u52",   0x00000, 0x10000, NO_DUMP ) /* When dumped make a seperate romdef */
ROM_END

ROM_START( spset015 ) /* Set Chip, not sure how it works but archive for future use */
	ROM_REGION( 0x10000, "maincpu", 0 )
	ROM_LOAD( "set015.u52",   0x00000, 0x10000, CRC(01641946) SHA1(81a0632ef96731907b28963293ab12db2073f4a6) )
ROM_END

ROM_START( spset026 ) /* Set Chip, not sure how it works but archive for future use */
	ROM_REGION( 0x10000, "maincpu", 0 )
	ROM_LOAD( "set026.u52",   0x00000, 0x10000, CRC(01641946) SHA1(81a0632ef96731907b28963293ab12db2073f4a6) ) /* 11/06/96   @ IGT NV */
ROM_END

ROM_START( spss4240 ) /* Coral Reef (SS4240) */
	ROM_REGION( 0x10000, "maincpu", 0 )
	ROM_LOAD( "sp1271.u52",   0x00000, 0x10000, CRC(dc164599) SHA1(7114652a733b26cd711dbe4d65dde065ba73619f) )

	ROM_REGION( 0x02000, "reeldata", 0 )
	ROM_LOAD( "ss4240.u53",   0x00000, 0x02000, CRC(c5715b9b) SHA1(8b0ca15b520a5c8e1ebec13e3a1dc304fb40aea0) )
ROM_END

/*************************
*      Game Drivers      *
*************************/

//     YEAR  NAME      PARENT  MACHINE  INPUT  CLASS        INIT        ROT    COMPANY                                FULLNAME                       FLAGS                LAYOUT
GAMEL( 1993, spset005, 0,      splus,   splus, splus_state, init_splus, ROT0,  "IGT - International Game Technology", "S-Plus SET005 Set chip",  MACHINE_NOT_WORKING, layout_splus )
GAMEL( 1993, spset015, 0,      splus,   splus, splus_state, init_splus, ROT0,  "IGT - International Game Technology", "S-Plus SET015 Set chip",  MACHINE_NOT_WORKING, layout_splus )
GAMEL( 1996, spset026, 0,      splus,   splus, splus_state, init_splus, ROT0,  "IGT - International Game Technology", "S-Plus SET026 Set chip",  MACHINE_NOT_WORKING, layout_splus )

GAMEL( 1994, spss4240, 0,      splus,   splus, splus_state, init_splus, ROT0,  "IGT - International Game Technology", "S-Plus (SS4240) Coral Reef",  MACHINE_NOT_WORKING, layout_splus )