summaryrefslogtreecommitdiffstatshomepage
path: root/src/mame/drivers/snookr10.cpp
blob: 800c3d3baf5bc5724c388e2e68e15577bfefe817 (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
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
// license:BSD-3-Clause
// copyright-holders:Roberto Fresca
/**********************************************************************************

  SNOOKER 10 / SANDII'
  Driver by Roberto Fresca.


  Games running on this hardware:

  * Snooker 10 (Ver 1.11),       Sandii', 1998.
  * Apple 10 (Ver 1.21),         Sandii', 1998.
  * Ten Balls (Ver 1.05),        unknown, 1997.
  * Crystals Colours (Ver 1.02), JCD srl, 1998.
  * Crystals Colours (Ver 1.01), JCD srl, 1998.


***********************************************************************************

  The hardware is generally composed by:

  CPU:    1x 65SC02 at 2MHz.

  Sound:  1x AD-65 or U6295 (OKI6295 compatible) at 1MHz, pin7 HIGH.
          1x LM358N.
          1x TDA2003.

  HD-PLD: 2x AMD MACH231-15-JC/1-18JI/1  or...
          2x XILINX XC9572-PQ100ASJ-15C  or...
          2x Lattice ispLSI1024-60LJ for earlier revisions.

  RAM:    1x 76C88AL-15 or U6264, SRAM 8Kx8.
  NVRAM:  1x 76C88AL-15 or HT6116-70, SRAM 8Kx8 (battery backed).
  ROMs:   4x 27C256 (3x 27C256 for earlier revisions).
          1x 27C020 for ADPCM samples.

  PROMs:  1x 82S147, AM27S29, or similar. (512 bytes).

  Clock:  1x Crystal: 16MHz.

  Other:  1x 28x2 edge connector.
          1x 15-pins connector.
          1x 7 pins connector (JTAG?) (on Crystals Colours boards).
          1x trimmer (volume).
          1x 8 DIP switches.
          1x 3.6 Volt, 55-80 mAh battery.


***************************************************************************************

  All supported games have been coded using some italian C.M.C games as point to start,
  changing hardware accesses, program logics, graphics, plus protection and some I/O
  through the 2x high density PLDs.

  Color palettes are normally stored in format GGBBBRRR inside a bipolar color PROM
  (old hardware), or repeated 64 times inside a regular 27c256 ROM (new hardware).

  - bits -
  7654 3210
  ---- -xxx   Red component.
  --xx x---   Blue component.
  xx-- ----   Green component.

  Same as Funworld video hardware, this one was designed to manage 4096 tiles with a
  size of 8x4 pixels each. Also support 4bpp graphics and the palette limitation is
  8 bits for color codes (256 x 16 colors). It means the hardware was designed for more
  elaborated graphics than these games...

  Color PROMs from current games are 512 bytes lenght, but they only use the first 256 bytes.

  The sound is composed by 4-bit ADPCM samples. All the supported games have the same sound ROM.
  All the sounds/samples were ripped from the Gottlieb pinball 'Cue Ball Wizard'(1992).


  ***** Game Notes *****

  To initialize NVRAM (for all games), keep pressed ESTATISTICA (key '9') + MANAGEMENT (key '0'),
  then press RESET (key F3), and finally release both (ESTATISTICA + MANAGEMENT) keys.

  Enter ESTATISTICA (key '9'), to enter the stats mode. Press PLAY/CANCELLA (key 'N')
  for 5 seconds to reset all values to zero. Press START (key '1') to exit the mode.

  Enter MANAGEMENT (key '0'), to enter the management mode. Press PLAY/CANCELLA (key 'N')
  to access the PROGRAMAZZIONE (program mode), where you can change the game parameters.
  Press START (key '1') to exit both modes.

  To clear credits (and stats), just re-initialize the NVRAM.

  Only for the new hardware revision (snookr10 & apple10), pressing STOP 1 (key 'Z') into the
  stats mode, make the hidden Input Test mode to appear. Press RESET (F3) to exit the mode.


  --- Super Game ---

  If you have some points accumulated and need to grab the tokens/tickets, you must to play
  a bonus game called SUPER GAME to get the points out. To enter the bonus game, you must
  press STOP5 in the attract mode. The payout system is through this game.

  5 themed items will be shown (apples, balls, etc... depending of the game).
  The joker will start to move from item to item quickly, but decreasing the speed gradually.
  To beat the game, you need to push the start button in the exact moment when the joker is
  located exactly in the center of the screen (item 3).

  Depending of the DIP switches settings, you can grab the prize manually pressing the SCARICA
  (payout) button, and then TICKET or HOPPER buttons. Press TICKET button to print a 100 points
  ticket. Press HOPPER button to get tokens x10 points.

  You have 1 attempt for each 100 earned points. If you lose the game, you lose the points.


  NOTE: Bit 7 of input port 0x3004 is tied to bit 7 of input port 0x3003 (DIP switch 1).
        This allow to use the PAYOUT button to trigger the Supper Game instead of STOP 5.


***************************************************************************************

  Encryption schemes...
  ---------------------

  * Apple 10

  - Tile matrix and color data (video & color RAM) are totally encrypted/scrambled.

  You can see the following table, where 'Normal tile #' is the tile number called
  to be drawn, and 'Scrambled tile #' is the physical tile position in the matrix:

  Normal | Scrambled
  tile # |  tile #
  -------+----------
   0x00  |   0x00   \
   0x01  |   0x80    |
   0x02  |   0x40    | Big "0"
   0x03  |   0xC0    |
   0x04  |   0x20    |
   0x05  |   0xA0   /
  -------+----------
   0x06  |   0x60   \
   0x07  |   0xE0    |
   0x08  |   0x10    | Big "1"
   0x09  |   0x90    |
   0x0A  |   0x50    |
   0x0B  |   0xD0   /
  -------+----------
   0x0C  |   0x30   \
   0x0D  |   0xB0    |
   0x0E  |   0x70    | Big "2"
   0x0F  |   0xF0    |
   0x10  |   0x08    |
   0x11  |   0x88   /
  -------+----------
   0x12  |   0x48   \
   0x13  |   0xC8    |
   0x14  |   0x28    | Big "3"
   0x15  |   0xA8    |
   0x16  |   0x68    |
   0x17  |   0xE8   /
  -------+----------
   0x18  |   0x18   \
   0x19  |   0x98    |
   0x1A  |   0x58    | Big "4"
   0x1B  |   0xD8    |
   0x1C  |   0x38    |
   0x1D  |   0xB8   /
  -------+----------
   0x1E  |   0x78   \
   0x1F  |   0xF8    |
   0x20  |   0x04    | Big "5"
   0x21  |   0x84    |
   0x22  |   0x44    |
   0x23  |   0xC4   /
  -------+----------
   0x24  |   0x24   \
   0x25  |   0xA4    |
   0x26  |   0x64    | Big "6"
   0x27  |   0xE4    |
   0x28  |   0x14    |
   0x29  |   0x94   /
  -------+----------
   0x2A  |   0x54   \
   0x2B  |   0xD4    |
   0x2C  |   0x34    | Big "7"
   0x2D  |   0xB4    |
   0x2E  |   0x74    |
   0x2F  |   0xF4   /
  -------+----------

  So we extract the following decryption table:

  0 <-> 0;  1 <-> 8;  2 <-> 4;  3 <-> C
  4 <-> 2;  5 <-> A;  6 <-> 6;  7 <-> E
  8 <-> 1;  9 <-> 9;  A <-> 5;  B <-> D
  C <-> 3;  D <-> B;  E <-> 7;  F <-> F

  ...and then swap nibbles.

  Also note that the values are inverted/mirrored bits of the original ones.

  0x01 (0001) <-> 0x08 (1000)
  0x02 (0010) <-> 0x04 (0100)
  0x03 (0011) <-> 0x0C (1100)
  0x04 (0100) <-> 0x04 (0010)
  0x05 (0101) <-> 0x0A (1010)
  ...and so on.

  To properly decrypt the thing 'on the fly' as the hardware does, I applied a bitswap into TILE_GET_INFO.
  This method rearrange the tile number for each tile called to be drawn.

  The final algorithm is:
                                                               digit #3
                                                               +-------+ swapped digits 1 & 2
                                                               |       |  +-------+------+
    tile_offset = bitswap<16>((tile_offset & 0xfff),15,14,13,12, 8,9,10,11, 0,1,2,3, 4,5,6,7)
                                                               | | |  |   | | | | || | | |
                                                               inverted   inverted|inverted
                                                               bitorder   bitorder|bitorder
   Colors are scrambled in the following way:

    Normal  | Scrambled
    offset  |  offset
  ----------+-----------
  0x00-0x0F | 0x00-0x0F
  0x10-0x1F | 0x80-0x8F
  0x20-0x2F | 0x40-0x4F
  0x30-0x3F | 0xC0-0xCF
  0x40-0x4F | 0x20-0x2F
  0x50-0x5F | 0xA0-0xAF
  0x60-0x6F | 0x60-0x6F
  0x70-0x7F | 0xE0-0xEF
  ----------+-----------
  0x80-0x8F | 0x10-0x1F
  0x90-0x9F | 0x90-0x9F
  0xA0-0xAF | 0x50-0x5F
  0xB0-0xBF | 0xD0-0xDF
  0xC0-0xCF | 0x30-0x3F
  0xD0-0xDF | 0xB0-0xBF
  0xE0-0xEF | 0x70-0x7F
  0xF0-0xFF | 0xF0-0xFF

  And each one of these segments are internally scrambled too.

  So, the algorithm to properly decrypt the color codes is very different here:

                                      1st nibble
                                  inverted bitorder
                                       | | | |
    color_index = bitswap<8>(color_index,4,5,6,7,2,3,0,1)
                                               <-> <->
                                              2nd nibble
                                            swappeed pairs

  Scary, huh?... ;-)


-----------------------------------------------------------------------------------

  * Crystals Color

  - Tile matrix and color data (video & color RAM) are totally encrypted/scrambled.

  You can see the following table, where 'Normal tile #' is the tile number called
  to be drawn, and 'Scrambled tile #' is the physical tile position in the matrix:

  Normal | Scrambled
  tile # |  tile #
  -------+-----------
   0x00  |   0x000   \
   0x01  |   0x800    |
   0x02  |   0x100    | Big "0"
   0x03  |   0x900    |
   0x04  |   0x004    |
   0x05  |   0x804   /
  -------+-----------
   0x06  |   0x104   \
   0x07  |   0x904    |
   0x08  |   0x008    | Big "1"
   0x09  |   0x808    |
   0x0A  |   0x108    |
   0x0B  |   0x908   /
  -------+-----------
   0x0C  |   0x00C   \
   0x0D  |   0x80C    |
   0x0E  |   0x10C    | Big "2"
   0x0F  |   0x90C    |
   0x10  |   0x010    |
   0x11  |   0x810   /
  -------+-----------
   0x12  |   0x110   \
   0x13  |   0x910    |
   0x14  |   0x014    | Big "3"
   0x15  |   0x814    |
   0x16  |   0x114    |
   0x17  |   0x914   /
  -------+-----------
   0x18  |   0x018   \
   0x19  |   0x818    |
   0x1A  |   0x118    | Big "4"
   0x1B  |   0x918    |
   0x1C  |   0x01C    |
   0x1D  |   0x81C   /
  -------+-----------
   0x1E  |   0x11C   \
   0x1F  |   0x91C    |
   0x20  |   0x200    | Big "5"
   0x21  |   0xA00    |
   0x22  |   0x300    |
   0x23  |   0xB00   /
  -------+-----------
   0x24  |   0x204   \
   0x25  |   0xA04    |
   0x26  |   0x304    | Big "6"
   0x27  |   0xB04    |
   0x28  |   0x208    |
   0x29  |   0xA08   /
  -------+-----------
   0x2A  |   0x308   \
   0x2B  |   0xB08    |
   0x2C  |   0x20C    | Big "7"
   0x2D  |   0xA0C    |
   0x2E  |   0x30C    |
   0x2F  |   0xB0C   /
  -------+-----------


  Will see some binary patterns...

  TILE    NORMAL POS     SCRAMBLED POS
  -----+---------------+---------------
   00:  0000 0000 0000 : 0000 0000 0000
   01:  0000 0000 0001 : 1000 0000 0000
   02:  0000 0000 0010 : 0001 0000 0000
   03:  0000 0000 0011 : 1001 0000 0000
   04:  0000 0000 0100 : 0000 0000 0100
   05:  0000 0000 0101 : 1000 0000 0100
   06:  0000 0000 0110 : 0001 0000 0100
   07:  0000 0000 0111 : 1001 0000 0100

   08:  0000 0000 1000 : 0000 0000 1000
   09:  0000 0000 1001 : 1000 0000 1000
   0A:  0000 0000 1010 : 0001 0000 1000
   0B:  0000 0000 1011 : 1001 0000 1000
   0C:  0000 0000 1100 : 0000 0000 1100
   0D:  0000 0000 1101 : 1000 0000 1100
   0E:  0000 0000 1110 : 0001 0000 1100
   0F:  0000 0000 1111 : 1001 0000 1100

   10:  0000 0001 0000 : 0000 0001 0000
   11:  0000 0001 0001 : 1000 0001 0000
   12:  0000 0001 0010 : 0001 0001 0000
   13:  0000 0001 0011 : 1001 0001 0000
   14:  0000 0001 0100 : 0000 0001 0100
   15:  0000 0001 0101 : 1000 0001 0100
   16:  0000 0001 0110 : 0001 0001 0100
   17:  0000 0001 0111 : 1001 0001 0100

   18:  0000 0001 1000 : 0000 0001 1000
   19:  0000 0001 1001 : 1000 0001 1000
   1A:  0000 0001 1010 : 0001 0001 1000
   1B:  0000 0001 1011 : 1001 0001 1000
   1C:  0000 0001 1100 : 0000 0001 1100
   1D:  0000 0001 1101 : 1000 0001 1100
   1E:  0000 0001 1110 : 0001 0001 1100
   1F:  0000 0001 1111 : 1001 0001 1100

   20:  0000 0010 0000 : 0010 0000 0000
   21:  0000 0010 0001 : 1010 0000 0000
   22:  0000 0010 0010 : 0011 0000 0000
   23:  0000 0010 0011 : 1011 0000 0000
   24:  0000 0010 0100 : 0010 0000 0100
   25:  0000 0010 0101 : 1010 0000 0100
   26:  0000 0010 0110 : 0011 0000 0100
   27:  0000 0010 0111 : 1011 0000 0100

   28:  0000 0010 1000 : 0010 0000 1000
   29:  0000 0010 1001 : 1010 0000 1000
   2A:  0000 0010 1010 : 0011 0000 1000
   2B:  0000 0010 1011 : 1011 0000 1000
   2C:  0000 0010 1100 : 0010 0000 1100
   2D:  0000 0010 1101 : 1010 0000 1100
   2E:  0000 0010 1110 : 0011 0000 1100
   2F:  0000 0010 1111 : 1011 0000 1100

   40:  0000 0100 0000 : 0000 0100 0000
   80:  0000 1000 0000 : 0000 1000 0000
   C0:  0000 1100 0000 : 0000 1100 0000
  100:  0001 0000 0000 : 0000 0000 0010


  We have enough data to arrange the following bitswap:

  FEDCBA9876543210 -> normal tile index.
      ||||||||||||
      |||||||||||+--> B -
      ||||||||||+---> 8   \
      |||||||||+----> 2    |
      ||||||||+-----> 3    |
      |||||||+------> 4    |
      ||||||+-------> 9    |> FEDC0A517694328B -> encrypted tile index.
      |||||+--------> 6    |
      ||||+---------> 7    |
      |||+----------> 1    |
      ||+-----------> 5    |
      |+------------> A   /
      +-------------> 0 -


  - Color RAM: Colors are scrambled in the following way:

    Normal  | Scrambled
    offset  |  offset
  ----------+-----------
  0x00-0x1F | 0x00-0x1F
  0x20-0x3F | 0x40-0x5F
  0x40-0x5F | 0x20-0x3F
  0x60-0x7F | 0x60-0x7F
  ----------+-----------
  0x80-0x9F | 0x80-0x9F
  0xA0-0xBF | 0xC0-0xDF
  0xC0-0xDF | 0xA0-0xBF
  0xE0-0xFF | 0xE0-0xFF


  The color codes are swapped in blocks of 32 bytes. 0x20<->0x40 and 0xA0<->0xC0,
  giving us the following binary representation:

      Normal     | Scrambled
      offset     | offset
  ---------------+-----------
   00: 0000 0000 : 0000 0000
   20: 0010 0000 : 0100 0000 <-- changed
   40: 0100 0000 : 0010 0000 <-- changed
   60: 0110 0000 : 0110 0000
   80: 1000 0000 : 1000 0000
   A0: 1010 0000 : 1100 0000 <-- changed
   C0: 1100 0000 : 1010 0000 <-- changed
   E0: 1110 0000 : 1110 0000


  So, the algorithm to properly decrypt the color codes is the following one:

    color_index = bitswap<8>(color_index,7,5,6,4,3,2,1,0)
                                         | |
                                       swapped

  Happy end here :)


***********************************************************************************

  * MEMORY MAPS *
  ---------------

  (Old hardware)

  $0000 - $07FF   NVRAM (battery backed 6116) ;R/W, all registers and settings.
  $1000 - $1000   OKI6295        ;R/W, sound.
  $4000 - $4000   Input Port 0   ;R
  $4001 - $4001   Input Port 1   ;R
  $4002 - $4002   Input Port 2   ;R
  $4003 - $4003   Input Port 3   ;R  , DIP switches.
  $5000 - $5000   Output Port 0  ;  W, lamps & counters.
  $5001 - $5001   Output Port 1  ;  W, lamps.
  $6000 - $6FFF   Video RAM
  $7000 - $7FFF   Color RAM
  $8000 - $FFFF   ROM Space


  (New hardware)

  $0000 - $07FF   NVRAM (battery backed 6116) ;R/W, all registers and settings.
  $1000 - $1000   OKI6295        ;R/W, sound.
  $3000 - $3000   Input Port 0   ;R
  $3001 - $3001   Input Port 1   ;R
  $3002 - $3002   Input Port 2   ;R
  $3003 - $3003   Input Port 3   ;R  , DIP switches.
  $3004 - $3004   Input Port 4   ;R  , bit 7 in parallel with DIP switch 1.
  $5000 - $5000   Output Port 0  ;  W, lamps & counters.
  $5001 - $5001   Output Port 1  ;  W, lamps.
  $6000 - $6FFF   Video RAM
  $7000 - $7FFF   Color RAM
  $8000 - $FFFF   ROM Space


  (Crystals hardware)

  $0000 - $07FF   NVRAM (battery backed 6116) ;R/W, all registers and settings.
  $1000 - $1000   Output Port 0  ;  W, lamps & counters.
  $1001 - $1001   Output Port 1  ;  W, lamps.
  $2000 - $2008   Special Input  ;R  , special ports meant for data channels...
  $3000 - $3000   Input Port 0   ;R
  $3001 - $3001   Input Port 1   ;R
  $3002 - $3002   Input Port 2   ;R
  $3003 - $3003   Input Port 3   ;R  , DIP switches.
  $5000 - $5000   OKI6295        ;R/W, sound.
  $6000 - $6FFF   Video RAM
  $7000 - $7FFF   Color RAM
  $8000 - $FFFF   ROM Space


***********************************************************************************

  *** Driver Updates ***


  [2016/03/30]

  - Added Crystals Colours (v1.02)
  - Set the new set as parent.
  - Added proper default NVRAM.
  - Added technical notes.


  [2013/05/12]

  Crystals Colours:
  - Decrypted the tile matrix / video RAM.
  - Wrote proper memory map and inputs from the scratch.
  - Bypassed the program data channels protection.
  - Decrypted the colors / color RAM.
  - Added button-lamps support.
  - Created a default NVRAM.
  - Promoted to working state.
  - Added technical and encryption notes...

  Snooker 10:
  - Fixed NVRAM size.
  - Added default NVRAM.

  Apple 10:
  - Fixed NVRAM size.
  - Added default NVRAM.

  Ten Balls:
  - Fixed NVRAM size.
  - Added default NVRAM.


  [2013/05/04]

  - Added Crystals Colours (Ver 1.01).


  [2008/10/09]

  - Reworked the button-lamps matrix system.
  - Documented both output ports.
  - Connected coin in, key in and payout counters.
  - Improved the lamps layout to be more realistic.
  - Updated technical notes.


  [2008/06/09]

  - Fixed the tilemap size.
  - Fixed the screen size.
  - Updated technical notes.


  [2008/05/29]

  - Switched the color system to RESNET calculations.
  - Hooked the infamous bit7 of Input Port 0x3004 in parallel to DIP switch 1.
    This allow to use the PAYOUT button to trigger the Super Game instead of STOP 5.
  - Demultiplexed lamps matrix.
  - Added lamps support, but is still imperfect.
  - Updated technical notes.


  [2008/05/22]

  - Confirmed the CPU clock after some PCB measurements.
  - Changed the SND clock to 1MHz to match the PCB measurement.
  - Corrected the internal OKI6295 frequency turning the pin 7 state to HIGH.
  - Inputs: Eliminated all pulse limitations.
    Affected buttons have a rattled sound in the real thing too.
  - Updated technical notes.


  [2008/04/28]

  - Created a new machine driver for tenballs due to different memory map.
  - Worked all the input ports from the scratch.
  - Fixed the sound ROM_REGION.
  - Added the oki6295 emulation to all games.
  - Hooked output ports.
  - Documented and calculated all bits related to lamps.
  - Adjusted palette lenght to 256 colors.
  - Totally decrypted the apple10 color matrix. Now colors are perfect.
  - Created a new machine driver for apple10 due to encryption.
  - Reverse engineering the code to complete the DIP switches.
  - Added diplocations to DIP switches.
  - Promoted snookr10, apple10 and tenballs to 'WORKING' state.
  - Added game instructions & notes.
  - Updated encryption & technical notes.


  [2008/04/24]

  - Decrypted the apple10 tile matrix.
  - Partially decrypted the apple10 color codes.
  - Added encryption notes.
  - Updated technical notes.


  [2008/04/18]

  - Initial release. Support for snookr10, apple10 and tenballs.
  - Added technical/general notes.


  *** TO DO ***

  - Nothing... :)

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


#include "emu.h"
#include "includes/snookr10.h"

#include "cpu/m6502/m65sc02.h"
#include "machine/nvram.h"
#include "sound/okim6295.h"
#include "screen.h"
#include "speaker.h"

#include "snookr10.lh"


#define MASTER_CLOCK    XTAL(16'000'000)

/**********************
* Read/Write Handlers *
*   - Input Ports -   *
**********************/

READ8_MEMBER(snookr10_state::dsw_port_1_r)
{
/*
   --------------------------------
    PORT 0x3004 ;INPUT PORT 4
   --------------------------------
    BIT 0 =
    BIT 1 =
    BIT 2 =
    BIT 3 =
    BIT 4 =
    BIT 5 =
    BIT 6 =
    BIT 7 = Complement of DS1, bit 7
   ---------------------------------
*/
return ioport("SW1")->read();
}


/**********************
* Read/Write Handlers *
*  - Output Ports -   *
**********************/

/*  Lamps are multiplexed using a 6 bit matrix.
    The first 4 bits are from Port A, and the
    remaining 2 are from Port B.

    LAMPS components:

    START  = bit5
    CANCEL = bit2
    STOP1  = bit0
    STOP2  = bit1
    STOP3  = bit0
    STOP4  = bit3
    STOP5  = bit4
*/

WRITE8_MEMBER(snookr10_state::output_port_0_w)
{
/*
   ----------------------------
    PORT 0x5000 ;OUTPUT PORT A
   ----------------------------
    BIT 0 = Coin counter.
    BIT 1 = Lamps matrix, m_bit0.
    BIT 2 = Payout x10.
    BIT 3 = Lamps matrix, m_bit1.
    BIT 4 = Key in.
    BIT 5 = Lamps matrix, m_bit2.
    BIT 6 =
    BIT 7 = Lamps matrix, m_bit3.
   ----------------------------
*/
	m_outportl = data;

	m_bit0 = (data >> 1) & 1;
	m_bit1 = (data >> 3) & 1;
	m_bit2 = (data >> 5) & 1;
	m_bit3 = (data >> 7) & 1;
	m_bit4 = m_outporth & 1;
	m_bit5 = (m_outporth >> 1) & 1;

	output().set_lamp_value(0, m_bit5);   /* Lamp 0 - START  */
	output().set_lamp_value(1, m_bit2);   /* Lamp 1 - CANCEL */
	output().set_lamp_value(2, m_bit0);   /* Lamp 2 - STOP1  */
	output().set_lamp_value(3, m_bit1);   /* Lamp 3 - STOP2  */
	output().set_lamp_value(4, m_bit0);   /* Lamp 4 - STOP3  */
	output().set_lamp_value(5, m_bit3);   /* Lamp 5 - STOP4  */
	output().set_lamp_value(6, m_bit4);   /* Lamp 6 - STOP5  */

	machine().bookkeeping().coin_counter_w(0, data & 0x01);  /* Coin in */
	machine().bookkeeping().coin_counter_w(1, data & 0x10);  /* Key in */
	machine().bookkeeping().coin_counter_w(2, data & 0x04);  /* Payout x10 */

//  logerror("high: %04x - low: %X \n", m_outporth, m_outportl);
//  popmessage("written : %02X", data);
}

WRITE8_MEMBER(snookr10_state::output_port_1_w)
{
/*
   ----------------------------
    PORT 0x5001 ;OUTPUT PORT B
   ----------------------------
    BIT 0 = Lamps matrix, m_bit4
    BIT 1 = Lamps matrix, m_bit5
    BIT 2 =
    BIT 3 =
    BIT 4 =
    BIT 5 =
    BIT 6 =
    BIT 7 =
   ----------------------------
*/
	m_outporth = data << 8;

	m_bit0 = (m_outportl >> 1) & 1;
	m_bit1 = (m_outportl >> 3) & 1;
	m_bit2 = (m_outportl >> 5) & 1;
	m_bit3 = (m_outportl >> 7) & 1;
	m_bit4 = data & 1;
	m_bit5 = (data >> 1) & 1;

	output().set_lamp_value(0, m_bit5);   /* Lamp 0 - START  */
	output().set_lamp_value(1, m_bit2);   /* Lamp 1 - CANCEL */
	output().set_lamp_value(2, m_bit0);   /* Lamp 2 - STOP1  */
	output().set_lamp_value(3, m_bit1);   /* Lamp 3 - STOP2  */
	output().set_lamp_value(4, m_bit0);   /* Lamp 4 - STOP3  */
	output().set_lamp_value(5, m_bit3);   /* Lamp 5 - STOP4  */
	output().set_lamp_value(6, m_bit4);   /* Lamp 6 - STOP5  */
}


READ8_MEMBER(snookr10_state::port2000_8_r)
{
/*
    Ports 2000-2008 expect data, otherwise the program trigger
    a sort of service-text mode, sets 300000 points and starts
    to decrease the amount one by one, triggering a sound each
    time the amount is decremented.
*/
	return 0xff;
}

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

void snookr10_state::snookr10_map(address_map &map)
{
	map(0x0000, 0x07ff).ram().share("nvram");   /* battery backed 6116 */
	map(0x1000, 0x1000).rw("oki", FUNC(okim6295_device::read), FUNC(okim6295_device::write));
	map(0x3000, 0x3000).portr("IN0");        /* IN0 */
	map(0x3001, 0x3001).portr("IN1");        /* IN1 */
	map(0x3002, 0x3002).portr("IN2");        /* IN2 */
	map(0x3003, 0x3003).portr("SW1");        /* DS1 */
	map(0x3004, 0x3004).r(this, FUNC(snookr10_state::dsw_port_1_r));      /* complement of DS1, bit 7 */
	map(0x5000, 0x5000).w(this, FUNC(snookr10_state::output_port_0_w));  /* OUT0 */
	map(0x5001, 0x5001).w(this, FUNC(snookr10_state::output_port_1_w));  /* OUT1 */
	map(0x6000, 0x6fff).ram().w(this, FUNC(snookr10_state::snookr10_videoram_w)).share("videoram");
	map(0x7000, 0x7fff).ram().w(this, FUNC(snookr10_state::snookr10_colorram_w)).share("colorram");
	map(0x8000, 0xffff).rom();
}

void snookr10_state::tenballs_map(address_map &map)
{
	map(0x0000, 0x07ff).ram().share("nvram");   /* battery backed 6116 */
	map(0x1000, 0x1000).rw("oki", FUNC(okim6295_device::read), FUNC(okim6295_device::write));
	map(0x4000, 0x4000).portr("IN0");        /* IN0 */
	map(0x4001, 0x4001).portr("IN1");        /* IN1 */
	map(0x4002, 0x4002).portr("IN2");        /* IN2 */
	map(0x4003, 0x4003).portr("SW1");        /* DS1 */
	map(0x5000, 0x5000).w(this, FUNC(snookr10_state::output_port_0_w));  /* OUT0 */
	map(0x5001, 0x5001).w(this, FUNC(snookr10_state::output_port_1_w));  /* OUT1 */
	map(0x6000, 0x6fff).ram().w(this, FUNC(snookr10_state::snookr10_videoram_w)).share("videoram");
	map(0x7000, 0x7fff).ram().w(this, FUNC(snookr10_state::snookr10_colorram_w)).share("colorram");
	map(0x8000, 0xffff).rom();
}

void snookr10_state::crystalc_map(address_map &map)
{
	map(0x0000, 0x07ff).ram().share("nvram");   /* battery backed 6116 */
	map(0x1000, 0x1000).w(this, FUNC(snookr10_state::output_port_0_w));  /* OUT0 */
	map(0x1001, 0x1001).w(this, FUNC(snookr10_state::output_port_1_w));  /* OUT1 */
	map(0x2000, 0x2008).r(this, FUNC(snookr10_state::port2000_8_r));      /* unknown... protection or data channels? */
	map(0x3000, 0x3000).portr("IN0");        /* IN0 */
	map(0x3001, 0x3001).portr("IN1");        /* IN1 */
	map(0x3002, 0x3002).portr("IN2");        /* IN2 */
	map(0x3003, 0x3003).portr("SW1");        /* DS1 */
	map(0x5000, 0x5000).rw("oki", FUNC(okim6295_device::read), FUNC(okim6295_device::write));
	map(0x6000, 0x6fff).ram().w(this, FUNC(snookr10_state::snookr10_videoram_w)).share("videoram");
	map(0x7000, 0x7fff).ram().w(this, FUNC(snookr10_state::snookr10_colorram_w)).share("colorram");
	map(0x8000, 0xffff).rom();
}


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

/*  Eliminated all PORT_IMPULSE limitations.
    All Hold & Cancel buttons have a rattle sound in the real PCB. */

static INPUT_PORTS_START( snookr10 )
	PORT_START("IN0")
	PORT_BIT( 0x01, IP_ACTIVE_LOW, IPT_SERVICE ) PORT_NAME("Remote x100") PORT_CODE(KEYCODE_Q)
	PORT_BIT( 0x02, IP_ACTIVE_LOW, IPT_POKER_HOLD1 ) PORT_NAME("Stop 1")    /* Input Test in stats mode */
	PORT_BIT( 0x04, IP_ACTIVE_LOW, IPT_POKER_CANCEL ) PORT_NAME("Cancella (Cancel) / Play / Bet")
	PORT_BIT( 0x08, IP_ACTIVE_LOW, IPT_START1 ) PORT_NAME("Start (Deal) / Raddoppio (Double-Up)")
	PORT_BIT( 0x10, IP_ACTIVE_LOW, IPT_POKER_HOLD5 ) PORT_NAME("Stop 5 / Risk (Half Gamble) / Super Game")
	PORT_BIT( 0x20, IP_ACTIVE_LOW, IPT_GAMBLE_BOOK ) PORT_NAME("Estatistica (Stats)")
	PORT_BIT( 0x40, IP_ACTIVE_LOW, IPT_GAMBLE_SERVICE ) PORT_NAME("Management")
	PORT_BIT( 0x80, IP_ACTIVE_LOW, IPT_POKER_HOLD4 ) PORT_NAME("Stop 4 / Alta (High)")

	PORT_START("IN1")
	PORT_BIT( 0x01, IP_ACTIVE_LOW, IPT_POKER_HOLD2 ) PORT_NAME("Stop 2 / Bassa (Low)")
	PORT_BIT( 0x02, IP_ACTIVE_LOW, IPT_POKER_HOLD3 ) PORT_NAME("Stop 3")
	PORT_BIT( 0x04, IP_ACTIVE_LOW, IPT_OTHER ) PORT_NAME("Ticket") PORT_CODE(KEYCODE_T)
	PORT_BIT( 0x08, IP_ACTIVE_LOW, IPT_OTHER ) PORT_NAME("Hopper")
	PORT_BIT( 0x10, IP_ACTIVE_LOW, IPT_UNKNOWN )
	PORT_BIT( 0x20, IP_ACTIVE_LOW, IPT_COIN2 )
	PORT_BIT( 0x40, IP_ACTIVE_LOW, IPT_UNKNOWN )
	PORT_BIT( 0x80, IP_ACTIVE_LOW, IPT_GAMBLE_PAYOUT ) PORT_NAME("Scarica (Payout)")

	PORT_START("IN2")
	PORT_BIT( 0x01, IP_ACTIVE_LOW, IPT_COIN1 )
	PORT_BIT( 0x02, IP_ACTIVE_LOW, IPT_UNKNOWN )
	PORT_BIT( 0x04, IP_ACTIVE_LOW, IPT_UNKNOWN )
	PORT_BIT( 0x08, IP_ACTIVE_LOW, IPT_UNKNOWN )
	PORT_BIT( 0x10, IP_ACTIVE_LOW, IPT_UNKNOWN )
	PORT_BIT( 0x20, IP_ACTIVE_LOW, IPT_UNKNOWN )
	PORT_BIT( 0x40, IP_ACTIVE_LOW, IPT_UNKNOWN )
	PORT_BIT( 0x80, IP_ACTIVE_LOW, IPT_UNKNOWN )

	PORT_START("SW1")
	PORT_DIPNAME( 0x03, 0x00, "Pool Value" )                PORT_DIPLOCATION("SW1:7,8")
	PORT_DIPSETTING(    0x03, "100" )
	PORT_DIPSETTING(    0x02, "200" )
	PORT_DIPSETTING(    0x01, "500" )
	PORT_DIPSETTING(    0x00, "1000" )
	PORT_DIPNAME( 0x0c, 0x00, DEF_STR( Coinage ) )          PORT_DIPLOCATION("SW1:5,6")
	PORT_DIPSETTING(    0x0c, DEF_STR( 1C_1C ) )
	PORT_DIPSETTING(    0x08, DEF_STR( 1C_2C ) )
	PORT_DIPSETTING(    0x04, DEF_STR( 1C_5C ) )
	PORT_DIPSETTING(    0x00, "1 Coin / 10 Credits" )
	PORT_DIPNAME( 0x10, 0x10, "Super Game Settings" )       PORT_DIPLOCATION("SW1:4")
	PORT_DIPSETTING(    0x10, "Play to Payout" )
	PORT_DIPSETTING(    0x00, "Direct Payout" )
	PORT_DIPNAME( 0x60, 0x60, "Super Game Payment Type" )   PORT_DIPLOCATION("SW1:3,2")
	PORT_DIPSETTING(    0x00, "Manual - User Choice" )
	PORT_DIPSETTING(    0x20, "Manual - Coins" )
	PORT_DIPSETTING(    0x40, "Manual - Tickets" )
	PORT_DIPSETTING(    0x60, "Automatic" )
	PORT_DIPNAME( 0x80, 0x80, "Super Game Button" )         PORT_DIPLOCATION("SW1:1")
	PORT_DIPSETTING(    0x00, "PAYOUT button" )
	PORT_DIPSETTING(    0x80, "STOP 5 button" )
INPUT_PORTS_END

/*  Eliminated all PORT_IMPULSE limitations.
    All Hold & Cancel buttons have a rattle sound in the real PCB. */

static INPUT_PORTS_START( apple10 )
	PORT_INCLUDE( snookr10 )

	PORT_MODIFY("SW1")
	PORT_DIPNAME( 0x03, 0x00, "Pool Value" )                PORT_DIPLOCATION("SW1:7,8")
	PORT_DIPSETTING(    0x03, "100" )
	PORT_DIPSETTING(    0x02, "200" )
	PORT_DIPSETTING(    0x01, "500" )
	PORT_DIPSETTING(    0x00, "1000" )
	PORT_DIPNAME( 0x0c, 0x00, DEF_STR( Coinage ) )          PORT_DIPLOCATION("SW1:5,6")
	PORT_DIPSETTING(    0x0c, DEF_STR( 1C_1C ) )
	PORT_DIPSETTING(    0x08, DEF_STR( 1C_2C ) )
	PORT_DIPSETTING(    0x04, DEF_STR( 1C_5C ) )
	PORT_DIPSETTING(    0x00, "1 Coin / 10 Credits" )
	PORT_DIPNAME( 0x10, 0x10, "Super Game Settings" )       PORT_DIPLOCATION("SW1:4")
	PORT_DIPSETTING(    0x10, "Play to Payout" )
	PORT_DIPSETTING(    0x00, "Direct Payout" )
	PORT_DIPNAME( 0x60, 0x60, "Super Game Payment Type" )   PORT_DIPLOCATION("SW1:3,2")
	PORT_DIPSETTING(    0x00, "Manual - Coins 1" )
	PORT_DIPSETTING(    0x20, "Manual - Coins 2" )
	PORT_DIPSETTING(    0x40, "Disable Payment/Game" )
	PORT_DIPSETTING(    0x60, "Automatic" )
	PORT_DIPNAME( 0x80, 0x80, "Super Game Button" )         PORT_DIPLOCATION("SW1:1")
	PORT_DIPSETTING(    0x00, "PAYOUT button" )
	PORT_DIPSETTING(    0x80, "STOP 5 button" )
INPUT_PORTS_END

/*  Eliminated all PORT_IMPULSE limitations.
    All Hold & Cancel buttons have a rattle sound in the real PCB. */

static INPUT_PORTS_START( tenballs )
	PORT_INCLUDE( snookr10 )

	/* tenballs seems a prototype, most DIP
	   switches seems to do nothing at all.
	*/
	PORT_MODIFY("SW1")
	PORT_DIPNAME( 0x03, 0x00, "Pool Value" )        PORT_DIPLOCATION("SW1:7,8")
	PORT_DIPSETTING(    0x03, "100" )
	PORT_DIPSETTING(    0x02, "200" )
	PORT_DIPSETTING(    0x01, "500" )
	PORT_DIPSETTING(    0x00, "1000" )
	/* coinage is always 1 coin - 10 credits */
	PORT_DIPNAME( 0x04, 0x04, DEF_STR( Unknown ) )  PORT_DIPLOCATION("SW1:6")
	PORT_DIPSETTING(    0x04, DEF_STR( Off ) )
	PORT_DIPSETTING(    0x00, DEF_STR( On ) )
	PORT_DIPNAME( 0x08, 0x08, DEF_STR( Unknown ) )  PORT_DIPLOCATION("SW1:5")
	PORT_DIPSETTING(    0x08, DEF_STR( Off ) )
	PORT_DIPSETTING(    0x00, DEF_STR( On ) )
	/* always play Super Game to payout */
	PORT_DIPNAME( 0x10, 0x10, DEF_STR( Unknown ) )  PORT_DIPLOCATION("SW1:4")
	PORT_DIPSETTING(    0x10, DEF_STR( Off ) )
	PORT_DIPSETTING(    0x00, DEF_STR( On ) )
	/* always manual payout */
	PORT_DIPNAME( 0x20, 0x20, DEF_STR( Unknown ) )  PORT_DIPLOCATION("SW1:3")
	PORT_DIPSETTING(    0x20, DEF_STR( Off ) )
	PORT_DIPSETTING(    0x00, DEF_STR( On ) )
	PORT_DIPNAME( 0x40, 0x40, DEF_STR( Unknown ) )  PORT_DIPLOCATION("SW1:2")
	PORT_DIPSETTING(    0x40, DEF_STR( Off ) )
	PORT_DIPSETTING(    0x00, DEF_STR( On ) )
	/* Super Game always ON */
	PORT_DIPNAME( 0x80, 0x80, DEF_STR( Unknown ) )  PORT_DIPLOCATION("SW1:1")
	PORT_DIPSETTING(    0x80, DEF_STR( Off ) )
	PORT_DIPSETTING(    0x00, DEF_STR( On ) )
INPUT_PORTS_END

static INPUT_PORTS_START( crystalc )
	PORT_START("IN0")
	PORT_BIT( 0x01, IP_ACTIVE_LOW, IPT_SERVICE )        PORT_NAME("Remote x100") PORT_CODE(KEYCODE_Q)
	PORT_BIT( 0x02, IP_ACTIVE_LOW, IPT_POKER_HOLD1 )    PORT_NAME("Stop 1")
	PORT_BIT( 0x04, IP_ACTIVE_LOW, IPT_POKER_CANCEL )   PORT_NAME("Cancella (Cancel) / Play / Bet")
	PORT_BIT( 0x08, IP_ACTIVE_LOW, IPT_START1 )         PORT_NAME("Start (Deal) / Raddoppio (Double-Up)")
	PORT_BIT( 0x10, IP_ACTIVE_LOW, IPT_POKER_HOLD5 )    PORT_NAME("Stop 5 / Risk (Half Gamble) / Super Game")
	PORT_BIT( 0x20, IP_ACTIVE_LOW, IPT_GAMBLE_BOOK )    PORT_NAME("Statistica (Stats)")
	PORT_BIT( 0x40, IP_ACTIVE_LOW, IPT_GAMBLE_SERVICE ) PORT_NAME("Management")
	PORT_BIT( 0x80, IP_ACTIVE_LOW, IPT_POKER_HOLD4 )    PORT_NAME("Stop 4 / Alta (High)")

	PORT_START("IN1")
	PORT_BIT( 0x01, IP_ACTIVE_LOW, IPT_POKER_HOLD2 ) PORT_NAME("Stop 2 / Bassa (Low)")
	PORT_BIT( 0x02, IP_ACTIVE_LOW, IPT_POKER_HOLD3 ) PORT_NAME("Stop 3")
	PORT_BIT( 0x04, IP_ACTIVE_LOW, IPT_OTHER )       PORT_NAME("Ticket") PORT_CODE(KEYCODE_T)
	PORT_BIT( 0x08, IP_ACTIVE_LOW, IPT_OTHER )       PORT_NAME("Hopper") PORT_CODE(KEYCODE_H)
	PORT_BIT( 0x10, IP_ACTIVE_LOW, IPT_UNKNOWN )
	PORT_BIT( 0x20, IP_ACTIVE_LOW, IPT_COIN2 )
	PORT_BIT( 0x40, IP_ACTIVE_LOW, IPT_UNKNOWN )
	PORT_BIT( 0x80, IP_ACTIVE_LOW, IPT_GAMBLE_PAYOUT ) PORT_NAME("Scarica (Payout)")

	PORT_START("IN2")
	PORT_BIT( 0x01, IP_ACTIVE_LOW, IPT_COIN1 )
	PORT_BIT( 0x02, IP_ACTIVE_LOW, IPT_UNKNOWN )
	PORT_BIT( 0x04, IP_ACTIVE_LOW, IPT_UNKNOWN )
	PORT_BIT( 0x08, IP_ACTIVE_LOW, IPT_UNKNOWN )
	PORT_BIT( 0x10, IP_ACTIVE_LOW, IPT_UNKNOWN )    // SG vs real value in paytable.
	PORT_BIT( 0x20, IP_ACTIVE_LOW, IPT_UNKNOWN )
	PORT_BIT( 0x40, IP_ACTIVE_LOW, IPT_UNKNOWN )
	PORT_BIT( 0x80, IP_ACTIVE_LOW, IPT_UNKNOWN )

	PORT_START("SW1")
	PORT_DIPNAME( 0x03, 0x00, "Bull Value" )        PORT_DIPLOCATION("SW1:7,8")
	PORT_DIPSETTING(    0x03, "100" )
	PORT_DIPSETTING(    0x02, "200" )
	PORT_DIPSETTING(    0x01, "500" )
	PORT_DIPSETTING(    0x00, "1000" )
	PORT_DIPNAME( 0x04, 0x04, DEF_STR( Unknown ) )  PORT_DIPLOCATION("SW1:6")
	PORT_DIPSETTING(    0x04, DEF_STR( Off ) )
	PORT_DIPSETTING(    0x00, DEF_STR( On ) )
	PORT_DIPNAME( 0x08, 0x08, DEF_STR( Unknown ) )  PORT_DIPLOCATION("SW1:5")
	PORT_DIPSETTING(    0x08, DEF_STR( Off ) )
	PORT_DIPSETTING(    0x00, DEF_STR( On ) )
	PORT_DIPNAME( 0x10, 0x10, DEF_STR( Unknown ) )  PORT_DIPLOCATION("SW1:4")
	PORT_DIPSETTING(    0x10, DEF_STR( Off ) )
	PORT_DIPSETTING(    0x00, DEF_STR( On ) )
	PORT_DIPNAME( 0x60, 0x60, "Payment Type" )      PORT_DIPLOCATION("SW1:3,2")
	PORT_DIPSETTING(    0x00, "Ticket + Coins" )
	PORT_DIPSETTING(    0x20, "Ticket only" )
	PORT_DIPSETTING(    0x40, "Coins only" )
	PORT_DIPSETTING(    0x60, "Ticket + Coins" )
	PORT_DIPNAME( 0x80, 0x80, DEF_STR( Unknown ) )  PORT_DIPLOCATION("SW1:1")
	PORT_DIPSETTING(    0x00, DEF_STR( Off ) )
	PORT_DIPSETTING(    0x80, DEF_STR( On ) )
INPUT_PORTS_END

static INPUT_PORTS_START( crystalca )
	PORT_INCLUDE( crystalc )

	PORT_MODIFY("SW1")
	PORT_DIPNAME( 0x03, 0x00, "Pool Value" )        PORT_DIPLOCATION("SW1:7,8")
	PORT_DIPSETTING(    0x03, "100" )
	PORT_DIPSETTING(    0x02, "200" )
	PORT_DIPSETTING(    0x01, "500" )
	PORT_DIPSETTING(    0x00, "1000" )
INPUT_PORTS_END


/*************************
*    Graphics Layouts    *
*************************/

static const gfx_layout charlayout =
{
	4,8,
	RGN_FRAC(1,2),
	4,
	{ RGN_FRAC(0,2), RGN_FRAC(0,2) + 4, RGN_FRAC(1,2), RGN_FRAC(1,2) + 4 },
	{ 0, 1, 2, 3 },
	{ 0*8, 1*8, 2*8, 3*8, 4*8, 5*8, 6*8, 7*8 },
	8*4*2
};


/******************************
* Graphics Decode Information *
******************************/

static GFXDECODE_START( snookr10 )
	GFXDECODE_ENTRY( "gfx1", 0x0000, charlayout, 0, 16 )
GFXDECODE_END


/**************************
*     Machine Drivers     *
**************************/

MACHINE_CONFIG_START(snookr10_state::snookr10)

	/* basic machine hardware */
	MCFG_DEVICE_ADD("maincpu", M65SC02, MASTER_CLOCK/8)    /* 2 MHz (1.999 MHz measured) */
	MCFG_DEVICE_PROGRAM_MAP(snookr10_map)

	MCFG_NVRAM_ADD_0FILL("nvram")

	/* video hardware */

	MCFG_SCREEN_ADD("screen", RASTER)
	MCFG_SCREEN_REFRESH_RATE(60)
	MCFG_SCREEN_VBLANK_TIME(ATTOSECONDS_IN_USEC(0))
	MCFG_SCREEN_SIZE(96*4, 30*8)
	MCFG_SCREEN_VISIBLE_AREA(0*4, 96*4-1, 0*8, 30*8-1)
	MCFG_SCREEN_UPDATE_DRIVER(snookr10_state, screen_update_snookr10)
	MCFG_SCREEN_PALETTE("palette")
	MCFG_SCREEN_VBLANK_CALLBACK(INPUTLINE("maincpu", INPUT_LINE_NMI))

	MCFG_GFXDECODE_ADD("gfxdecode", "palette", snookr10)
	MCFG_PALETTE_ADD("palette", 256)
	MCFG_PALETTE_INIT_OWNER(snookr10_state, snookr10)

	/* sound hardware */
	MCFG_SPEAKER_STANDARD_MONO("mono")
	MCFG_OKIM6295_ADD("oki", MASTER_CLOCK/16, PIN7_HIGH)   /* 1 MHz (995.5 kHz measured); pin7 checked HIGH on PCB */
	MCFG_SOUND_ROUTE(ALL_OUTPUTS, "mono", 1.8)

MACHINE_CONFIG_END

MACHINE_CONFIG_START(snookr10_state::apple10)
	snookr10(config);

	/* basic machine hardware */
	MCFG_DEVICE_MODIFY("maincpu")

	/* video hardware */
	MCFG_PALETTE_MODIFY("palette")
	MCFG_PALETTE_INIT_OWNER(snookr10_state, apple10)
	MCFG_VIDEO_START_OVERRIDE(snookr10_state, apple10)

MACHINE_CONFIG_END

MACHINE_CONFIG_START(snookr10_state::tenballs)
	snookr10(config);

	/* basic machine hardware */
	MCFG_DEVICE_MODIFY("maincpu")
	MCFG_DEVICE_PROGRAM_MAP(tenballs_map)

MACHINE_CONFIG_END

MACHINE_CONFIG_START(snookr10_state::crystalc)
	snookr10(config);

	/* basic machine hardware */
	MCFG_DEVICE_MODIFY("maincpu")
	MCFG_DEVICE_PROGRAM_MAP(crystalc_map)

	MCFG_PALETTE_MODIFY("palette")
	MCFG_PALETTE_INIT_OWNER(snookr10_state, crystalc)
	MCFG_VIDEO_START_OVERRIDE(snookr10_state, crystalc)

MACHINE_CONFIG_END


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

ROM_START( snookr10 )
	ROM_REGION( 0x10000, "maincpu", 0 )
	ROM_LOAD( "1.u2", 0x8000, 0x8000, CRC(216ccb2d) SHA1(d86270cd03a08f6fd3e7b327b8173f66da28e5e8) )

	ROM_REGION( 0x10000, "gfx1", 0 )
	ROM_LOAD( "2.u22", 0x0000, 0x8000, CRC(a70d9c48) SHA1(3fa90190323526553866662afda4dbe1c94abeff) )
	ROM_LOAD( "3.u25", 0x8000, 0x8000, CRC(3009faaa) SHA1(d1cda455b270cb9afa65b9701735a3a1f2a48df2) )

	ROM_REGION( 0x40000, "oki", 0 ) /* ADPCM samples */
	ROM_LOAD( "4.u18", 0x00000, 0x40000, CRC(17090d56) SHA1(3a4c247f96c80f8cf4c1389b273880c5ea6fc39d) )

	ROM_REGION( 0x0800, "nvram", 0 )    /* default NVRAM */
	ROM_LOAD( "snooker_10_nvram.bin", 0x0000, 0x0800, CRC(d383363b) SHA1(88f0d9e53b0bd276d01cefd817f0edcd753322dc) )

	/* this should be changed because the palette is stored in a normal ROM instead of a color PROM */
	ROM_REGION( 0x8000, "proms", 0 )
	ROM_LOAD( "5.u27", 0x0000, 0x8000, CRC(f3d7d640) SHA1(f78060f4603e316fa3c2ec4ba6d7edf261cf6d8a) )
ROM_END

ROM_START( apple10 )
	ROM_REGION( 0x10000, "maincpu", 0 )
	ROM_LOAD( "1.u2", 0x8000, 0x8000, CRC(7d538566) SHA1(2e805157010c366ab1f2313a2bedb071c1dde733) )

	ROM_REGION( 0x10000, "gfx1", 0 )
	ROM_LOAD( "2.u22", 0x0000, 0x8000, CRC(42b016f4) SHA1(59d1b77f8cb706a3878813111c6a71514c413784) )
	ROM_LOAD( "3.u25", 0x8000, 0x8000, CRC(afc535dc) SHA1(ed2d65f3154c6d80b7b22bfef1f30232e4496128) )

	ROM_REGION( 0x40000, "oki", 0 ) /* ADPCM samples */
	ROM_LOAD( "4.u18", 0x00000, 0x40000, CRC(17090d56) SHA1(3a4c247f96c80f8cf4c1389b273880c5ea6fc39d) )

	ROM_REGION( 0x0800, "nvram", 0 )    /* default NVRAM */
	ROM_LOAD( "apple_10_nvram.bin", 0x0000, 0x0800, CRC(00069b55) SHA1(378ca25dc3173252548bc27e80909e1b2b4d58db) )

	/* this should be changed because the palette is stored in a normal ROM instead of a color PROM */
	ROM_REGION( 0x8000, "proms", 0 )
	ROM_LOAD( "5.u27", 0x0000, 0x8000, CRC(3510d705) SHA1(2190c8199d29bf89e3007eb771cc6b0e2b58f6cd) )
ROM_END

ROM_START( tenballs )
	ROM_REGION( 0x10000, "maincpu", 0 )
	ROM_LOAD( "4.u2", 0x8000, 0x8000, CRC(2f334862) SHA1(61d57995451b6bc7de23900c460c3e073993899c) )

	ROM_REGION( 0x10000, "gfx1", 0 )
	ROM_LOAD( "3.u16", 0x0000, 0x8000, CRC(9eb88a08) SHA1(ab52924103e2b14c598a21c3d77b053da37a0212) )
	ROM_LOAD( "2.u15", 0x8000, 0x8000, CRC(a5091583) SHA1(c0775d9b77cb634d3702b6c08cdf73c867b6169a) )

	ROM_REGION( 0x40000, "oki", 0 ) /* ADPCM samples */
	ROM_LOAD( "1.u28", 0x00000, 0x40000, CRC(17090d56) SHA1(3a4c247f96c80f8cf4c1389b273880c5ea6fc39d) )

	ROM_REGION( 0x0800, "nvram", 0 )    /* default NVRAM */
	ROM_LOAD( "ten_balls_nvram.bin", 0x0000, 0x0800, CRC(42a5803f) SHA1(2c8c9ec0f26a947cf9cfa2e91e9127725becdef5) )

	ROM_REGION( 0x0200, "proms", 0 )
	ROM_LOAD( "82s147.u17", 0x0000, 0x0200, CRC(20234dcc) SHA1(197937bbec0201888467e250bdba49e39aa4204a) )
ROM_END

/*
  Crystals Colours

  Ver 1.01
  The top left bonus is called "Pool"

  Ver 1.02
  The top left bonus is called "Bull"
  Minor differences against Ver 1.01
  (see code at 0x9da0 onward...)

  PROMs and ADPCM are identicals.

  Coinage seems to be fixed to:
  - Coin A: 10 credits per coin.
  - Coin B: 10 credits per coin.
  - Key In: 100 credits per coin.

*/
ROM_START( crystalc )
	ROM_REGION( 0x10000, "maincpu", 0 )
	ROM_LOAD( "crystals_colours_1__,1.02.u2", 0x8000, 0x8000, CRC(a803d857) SHA1(8eb34f6b4f820776459d92f9c589794bec7547ea) )

	ROM_REGION( 0x10000, "gfx1", 0 )
	ROM_LOAD( "crystals_colours_2_l.425.u16", 0x0000, 0x8000, CRC(834aba76) SHA1(86c1f282c11a4bb058ab702c237b76a620b99cc7) )
	ROM_LOAD( "crystals_colours_3_l.425.u15", 0x8000, 0x8000, CRC(7f14f66a) SHA1(daba9a687b8985edfb61e21ba010544f97523bdc) )

	ROM_REGION( 0x40000, "oki", 0 ) /* ADPCM samples */
	ROM_LOAD( "crystals_colours_4.u28", 0x00000, 0x40000, CRC(ecc6b575) SHA1(f6032e89b30aebeab9ad721608277430084256bc) )

	ROM_REGION( 0x0800, "nvram", 0 )    /* default NVRAM */
	ROM_LOAD( "crystals_colours_1.02_nvram.bin", 0x0000, 0x0800, CRC(20456301) SHA1(224da8fd8ea0997741bd5a7d51c8ca7f264d2302) )

	ROM_REGION( 0x0200, "proms", 0 )
	ROM_LOAD( "am27s29pc.u17", 0x0000, 0x0200, CRC(1af53eab) SHA1(1cac837aed75a77a0b93a294b07f2a8de6fcffef) )
ROM_END

ROM_START( crystalca )
	ROM_REGION( 0x10000, "maincpu", 0 )
	ROM_LOAD( "crystals_colours_1__,1.01.u2", 0x8000, 0x8000, CRC(ee7a6f62) SHA1(bb3f438ca563eeb4b365d344066b10b9aedb77bb) )

	ROM_REGION( 0x10000, "gfx1", 0 )
	ROM_LOAD( "crystals_colours_2.u16", 0x0000, 0x8000, CRC(527c07f6) SHA1(53ceb1d0267e1d76fa1a9325c9a1d2e8e034958d) )
	ROM_LOAD( "crystals_colours_3.u15", 0x8000, 0x8000, CRC(e1003ab7) SHA1(a78bb2e1dc9d578d6a38072e2087f382cffa9f99) )

	ROM_REGION( 0x40000, "oki", 0 ) /* ADPCM samples */
	ROM_LOAD( "crystals_colours_4.u28", 0x00000, 0x40000, CRC(ecc6b575) SHA1(f6032e89b30aebeab9ad721608277430084256bc) )

	ROM_REGION( 0x0800, "nvram", 0 )    /* default NVRAM */
	ROM_LOAD( "crystals_colours_1.01_nvram.bin", 0x0000, 0x0800, CRC(907d8828) SHA1(ffd302996bee81277c2280fc212d910e0801d81d) )

	ROM_REGION( 0x0200, "proms", 0 )
	ROM_LOAD( "am27s29pc.u17", 0x0000, 0x0200, CRC(1af53eab) SHA1(1cac837aed75a77a0b93a294b07f2a8de6fcffef) )
ROM_END


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

/*     YEAR  NAME       PARENT    MACHINE   INPUT      STATE           INIT ROT   COMPANY      FULLNAME                       FLAGS   LAYOUT */
GAMEL( 1998, snookr10,  0,        snookr10, snookr10,  snookr10_state, 0,   ROT0, "Sandii'",   "Snooker 10 (Ver 1.11)",       0,      layout_snookr10 )
GAMEL( 1998, apple10,   0,        apple10,  apple10,   snookr10_state, 0,   ROT0, "Sandii'",   "Apple 10 (Ver 1.21)",         0,      layout_snookr10 )
GAMEL( 1997, tenballs,  snookr10, tenballs, tenballs,  snookr10_state, 0,   ROT0, "<unknown>", "Ten Balls (Ver 1.05)",        0,      layout_snookr10 )
GAMEL( 1998, crystalc,  0,        crystalc, crystalc,  snookr10_state, 0,   ROT0, "JCD srl",   "Crystals Colours (Ver 1.02)", 0,      layout_snookr10 )
GAMEL( 1998, crystalca, crystalc, crystalc, crystalca, snookr10_state, 0,   ROT0, "JCD srl",   "Crystals Colours (Ver 1.01)", 0,      layout_snookr10 )