download ksystem.c
Language: C
Copyright: (C) 1986 TAITO CORPORATION */
LOC: 1030
Project Info
Raine
Server: SourceForge
Type: cvs
...e\raine\raine\source\games\
   ainferno.c
   ainferno.h
   aquajack.c
   aquajack.h
   arabianm.c
   arabianm.h
   arkanoid.c
   arkretrn.c
   arkretrn.h
   armedf.c
   armedf.h
   ashura.c
   ashura.h
   asuka.c
   asuka.h
   biomtoy.c
   biomtoy.h
   bloodbro.c
   bloodbro.h
   bonzeadv.c
   bonzeadv.h
   bshark.c
   bshark.h
   bsystem.c
   bsystem.h
   bubblem.c
   bubl_mcu.c
   bublbobl.c
   bublbobl.h
   bubsymph.c
   bubsymph.h
   cabal.c
   cabal.h
   cadash.c
   cadash.h
   cameltry.c
   cameltry.h
   cave.c
   cave.h
   chaknpop.c
   chaknpop.h
   chasehq.c
   chasehq.h
   chasehq2.c
   chasehq2.h
   cleofort.c
   cleofort.h
   contcirc.c
   contcirc.h
   cps1.c
   cps1.h
   cps1drv.c
   crimec.c
   crimec.h
   ctribebl.c
   ctribebl.h
   cupfinal.c
   cupfinal.h
   dangarb.c
   dangarb.h
   darius.c
   darius.h
   darius2d.c
   darius2d.h
   dariusg.c
   deadconx.c
   deadconx.h
   default.c
   default.h
   dinorex.c
   dinorex.h
   dleague.c
   dleague.h
   dondokod.c
   dondokod.h
   doubled3.c
   doubled3.h
   driftout.c
   driftout.h
   driver.c
   eaction2.c
   eaction2.h
   earthjkr.c
   earthjkr.h
   exzisus.c
   exzisus.h
   f3demo.c
   f3demo.h
   f3system.c
   f3system.h
   fchamp.c
   fchamp.h
   finalb.c
   finalb.h
   flstory.c
   flstory.h
   galpanic.c
   gameinc.h
   games.c
   games.h
   gcpball.c
   gcpball.h
   gekridan.c
   gekridan.h
   gridseek.c
   gridseek.h
   growl.c
   growl.h
   gunbird.c
   gunfront.c
   gunfront.h
   gunlock.c
   gunlock.h
   heavyunt.c
   heavyunt.h
   hitice.c
   hitice.h
   hthero95.c
   hthero95.h
   hyperpcb.c
   intcup94.c
   intcup94.h
   kaiserkn.c
   kaiserkn.h
   kick_mcu.c
   kiki_mcu.c
   konamigx.c
   konamigx.h
   koshien.c
   koshien.h
   ksystem.c
   ksystem.h
   ktiger2.c
   ktiger2.h
   landmakr.c
   landmakr.h
   lightbr.c
   lightbr.h
   liquidk.c
   liquidk.h
   lsystem.c
   lsystem.h
   macrossp.c
   masterw.c
   masterw.h
   mcatadv.c
   megab.c
   megab.h
   megasy32.c
   megasy32.h
   megasys1.c
   megasys1.h
   metalb.c
   metalb.h
   mjnquest.c
   mjnquest.h
   nichisnd.c
   nichisnd.h
   ninjak.c
   ninjak.h
   ninjaw.c
   ninjaw.h
   nmk.c
   opwolf.c
   opwolf.h
   opwolf3.c
   opwolf3.h
   othunder.c
   othunder.h
   pbobble2.c
   pbobble2.h
   pbobble3.c
   pbobble3.h
   pbobble4.c
   pbobble4.h
   pengo.c
   pirates.c
   popnpop.c
   popnpop.h
   psyforce.c
   psyforce.h
   puchicar.c
   puchicar.h
   pulirula.c
   pulirula.h
   quizgame.c
   quizgame.h
   rainbow.c
   rainbow.h
   rambo3.c
   rambo3.h
   rastan.c
   rastan.h
   rastan2.c
   rastan2.h
   recordbr.c
   recordbr.h
   ridefght.c
   ridefght.h
   ringrage.c
   ringrage.h
   robokid.c
   robokid.h
   seta.c
   seta.h
   setax1.c
   setax1.h
   silentd.c
   silentd.h
   silkworm.c
   silkworm.h
   solfigtr.c
   solfigtr.h
   spacegun.c
   spacegun.h
   spcinv95.c
   spcinv95.h
   ssi.c
   ssi.h
   superchs.c
   superchs.h
   syvalion.c
   syvalion.h
   taitosnd.c
   taitosnd.h
   tc002obj.c
   tc002obj.h
   tc003vcu.c
   tc003vcu.h
   tc004vcu.c
   tc004vcu.h
   tc005rot.c
   tc005rot.h
   tc006vcu.c
   tc006vcu.h
   tc100scn.c
   tc100scn.h
   tc101scn.c
   tc110pcr.c
   tc110pcr.h
   tc140syt.c
   tc140syt.h
   tc150rod.c
   tc150rod.h
   tc180vcu.c
   tc180vcu.h
   tc200obj.c
   tc200obj.h
   tc220ioc.c
   tc220ioc.h
   tchnosnd.c
   tchnosnd.h
   tecmosys.c
   tecmosys.h
   terrac.c
   terrac.h
   terraf.c
   terraf.h
   tetrist.c
   tetrist.h
   thundfox.c
   thundfox.h
   toaplan1.c
   toaplan1.h
   toaplan2.c
   toaplan2.h
   toki.c
   toki.h
   topland.c
   topland.h
   topspeed.c
   topspeed.h
   trstars.c
   trstars.h
   twinqix.c
   twinqix.h
   undrfire.c
   undrfire.h
   upl.c
   upl.h
   viofight.c
   viofight.h
   volfied.c
   volfied.h
   warriorb.c
   warriorb.h
   wgp.c
   wgp.h
   wrestlef.c
   wrestlef.h
   wwfsstar.c
   wwfsstar.h
   xsystem1.c
   xsystem1.h
   xsystem2.c
   xsystem2.h
   zerozone.c
   zerozone.h

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
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
/******************************************************************************/
/*                                                                            */
/*                 TAITO K-SYSTEM (C) 1986 TAITO CORPORATION                  */
/*                                                                            */
/*                           Z80+Z80+YM2203+M68705                            */
/*                  NOTE: K-SYSTEM is not the official name.                  */
/*                                                                            */
/******************************************************************************/

#include "gameinc.h"
#include "ksystem.h"
#include "tc220ioc.h"
#include "taitosnd.h"
#include "2203intf.h"
#include "decode.h"
#include "sasound.h"		// sample support routines
#include "emumain.h"
#include "blit.h" // clear_game_screen

/*

Z80 x 2

M68705 x 1

YM2203 x 1

TC0010VCU x 1

*/

/*

changes/kayamon:
15/2/00:
   - fixed Kick&Run at last, now it works properly =)
   - undid antiriad's amazing F018 bullshit ;-)
     now it works again, assuming MZ80 has been fixed.
     NOTE: it's what we in the trade technically call a 'WATCHDOG'... :P
   - renamed kickrun to kicknrun for general MAME compatibilty
30/8/99:
   - added Knight Boy, dunno if it was worth it though.

   - NOTE: MCU is tested for KKK, seems to be 100%, so any problems are
           most likely due to the rest of the hardware instead.
   - rewrote video h/w, based on Bubble Bobble.
   - *** KICK&RUN not tested at all!!!!!!!!!!!
         Not one bit. DIPs are wrong, etc
         (probably doesn't work at all) ***
   - MEXICO86 needs adding here too...
   - had a go at that SOLID_MASK type stuff, hope it's right.
   - added some more DIPs and stuff.
   - overclocked main CPU, to avoid MZ80 IFF1 bug.
     NOTE: I think the latest MZ80 fixes this itself.
   - renamed driver to K-SYSTEM.
   - added proper MCU emulation (hah, yeah right).
   - fiddled DIPs/inputs about to work.

changes/antiriad:

 - redid the speed hack, so they actually work ;). also hooked up F018 as
   an interrupt request. it's probably some kind of vsync/waitvbl request.
 - increased the cpu slices, because the main cpu was spending a lot of
   time waiting for the sub cpu. i also put the main cpu back to 8mhz, I
   can't see any problem yet, probably the use of F018 now prevents the
   bug showing up (mz80 3.0 will fix iff1 bugs anyway).
 - bank switching without memcpy :).
 - fiddle with the gfx but didn't fix anything (monster priority is wrong,
   if you render everything in reverse, the monsters are right, but not
   much else).

*/

static struct DIR_INFO kick_and_run_dirs[] =
{
   { "kick_and_run", },
   { "kicknrun", },
   { NULL, },
};

static struct ROM_INFO kick_and_run_roms[] =
{
   {   "a87-02.bin", 0x00008000, 0x64f1a85f, 0, 0, 0, },
   {   "a87-03.bin", 0x00010000, 0xf42e8a88, 0, 0, 0, },
   {   "a87-04.bin", 0x00008000, 0x8b438d20, 0, 0, 0, },
   {   "a87-05.bin", 0x00010000, 0x4eee3a8a, 0, 0, 0, },
   {   "a87-06.bin", 0x00008000, 0x1625b587, 0, 0, 0, },
   {   "a87-07.bin", 0x00010000, 0x6cb6ebfe, 0, 0, 0, },
   {   "a87-08.bin", 0x00010000, 0x715e1b04, 0, 0, 0, },
   {   "a87-10.bin", 0x00000100, 0xbe6eb1f0, 0, 0, 0, },
   {   "a87-11.bin", 0x00000100, 0x14f6c28d, 0, 0, 0, },
   {   "a87-12.bin", 0x00000100, 0x3e953444, 0, 0, 0, },
   {     "68_h.bin", 0x00000800, 0xff92f816, 0, 0, 0, },
   {           NULL,          0,          0, 0, 0, 0, },
};

static struct INPUT_INFO kick_and_run_inputs[] =
{
   { KB_DEF_COIN1,        MSG_COIN1,               0x020005, 0x01, BIT_ACTIVE_0 },
   { KB_DEF_COIN2,        MSG_COIN2,               0x020005, 0x02, BIT_ACTIVE_0 },
   { KB_DEF_TILT,         MSG_TILT,                0x020000, 0x04, BIT_ACTIVE_0 },
   { KB_DEF_SERVICE,      MSG_SERVICE,             0x020000, 0x01, BIT_ACTIVE_0 },

   { KB_DEF_P1_START,     MSG_P1_START,            0x020000, 0x08, BIT_ACTIVE_0 },
   { KB_DEF_P1_UP,        MSG_P1_UP,               0x020001, 0x01, BIT_ACTIVE_0 },
   { KB_DEF_P1_DOWN,      MSG_P1_DOWN,             0x020001, 0x02, BIT_ACTIVE_0 },
   { KB_DEF_P1_LEFT,      MSG_P1_LEFT,             0x020001, 0x04, BIT_ACTIVE_0 },
   { KB_DEF_P1_RIGHT,     MSG_P1_RIGHT,            0x020001, 0x08, BIT_ACTIVE_0 },
   { KB_DEF_P1_B1,        MSG_P1_B1,               0x020001, 0x10, BIT_ACTIVE_0 },
   { KB_DEF_P1_B2,        MSG_P1_B2,               0x020001, 0x20, BIT_ACTIVE_0 },

   { KB_DEF_P2_START,     MSG_P2_START,            0x020000, 0x10, BIT_ACTIVE_0 },
   { KB_DEF_P2_UP,        MSG_P2_UP,               0x020002, 0x01, BIT_ACTIVE_0 },
   { KB_DEF_P2_DOWN,      MSG_P2_DOWN,             0x020002, 0x02, BIT_ACTIVE_0 },
   { KB_DEF_P2_LEFT,      MSG_P2_LEFT,             0x020002, 0x04, BIT_ACTIVE_0 },
   { KB_DEF_P2_RIGHT,     MSG_P2_RIGHT,            0x020002, 0x08, BIT_ACTIVE_0 },
   { KB_DEF_P2_B1,        MSG_P2_B1,               0x020002, 0x10, BIT_ACTIVE_0 },
   { KB_DEF_P2_B2,        MSG_P2_B2,               0x020002, 0x20, BIT_ACTIVE_0 },

   { 0,                   NULL,                    0,        0,    0            },
};

static struct DSW_DATA dsw_data_kick_and_run_0[] =
{
   { MSG_DSWA_BIT1,           0x01, 0x02 },
   { MSG_OFF,                 0x01, 0x00 },
   { MSG_ON,                  0x00, 0x00 },
   { MSG_DSWA_BIT2,           0x02, 0x02 },
   { MSG_OFF,                 0x02, 0x00 },
   { MSG_ON,                  0x00, 0x00 },
   { MSG_DSWA_BIT3,           0x04, 0x02 },
   { MSG_OFF,                 0x04, 0x00 },
   { MSG_ON,                  0x00, 0x00 },
   { MSG_DSWA_BIT4,           0x08, 0x02 },
   { MSG_OFF,                 0x08, 0x00 },
   { MSG_ON,                  0x00, 0x00 },
   { MSG_DSWA_BIT5,           0x10, 0x02 },
   { MSG_OFF,                 0x10, 0x00 },
   { MSG_ON,                  0x00, 0x00 },
   { MSG_DSWA_BIT6,           0x20, 0x02 },
   { MSG_OFF,                 0x20, 0x00 },
   { MSG_ON,                  0x00, 0x00 },
   { MSG_DSWA_BIT7,           0x40, 0x02 },
   { MSG_OFF,                 0x40, 0x00 },
   { MSG_ON,                  0x00, 0x00 },
   { MSG_DSWA_BIT8,           0x80, 0x02 },
   { MSG_OFF,                 0x80, 0x00 },
   { MSG_ON,                  0x00, 0x00 },
   { NULL,                    0,    0,   },
};

static struct DSW_INFO kick_and_run_dsw[] =
{
   { 0x020003, 0xFF, dsw_data_kick_and_run_0 },
   { 0x020004, 0xFF, dsw_data_default_1 },
   { 0,        0,    NULL,      },
};

static struct VIDEO_INFO kiki_kai_kai_video =
{
   DrawKikiKaiKai,
   256,
   224,
   8,
   VIDEO_ROTATE_90 |
   VIDEO_ROTATABLE,
};

static struct VIDEO_INFO kick_and_run_video =
{
   DrawKickRun,
   256,
   224,
   8,
   VIDEO_ROTATABLE,
};

static struct YM2203interface ym2203_interface =
{
   1,
   4000000,
   { 0x00ff20c0 },
   {0},
   {0},
   {0},
   {0},
   {NULL}
};

static struct SOUND_INFO kiki_kai_kai_sound[] =
{
   { SOUND_YM2203,  &ym2203_interface,    },
   { 0,             NULL,                 },
};

GAME( kick_and_run ,
   kick_and_run_dirs,
   kick_and_run_roms,
   kick_and_run_inputs,
   kick_and_run_dsw,
   NULL,

   LoadKickRun,
   ClearKSystem,
   &kick_and_run_video,
   ExecuteKSystemFrame,
   "kicknrun",
   "Kick and Run",
   NULL,
   COMPANY_ID_TAITO,
   "A87",
   1986,
   kiki_kai_kai_sound,
   GAME_SPORTS
);

static struct DIR_INFO kiki_kai_kai_dirs[] =
{
   { "kiki_kai_kai", },
   { "kikikai", },
   { "kikikaik", },
   { NULL, },
};

static struct ROM_INFO kiki_kai_kai_roms[] =
{
   {   "a85-08.rom", 0x00000100, 0xd15f61a8, 0, 0, 0, },
   {   "a85-09.rom", 0x00000100, 0xb931c94d, 0, 0, 0, },
   {   "a85-10.rom", 0x00000100, 0x8fc3fa86, 0, 0, 0, },
   {   "a85-11.rom", 0x00008000, 0xcc3539db, 0, 0, 0, },
   {   "a85-12.rom", 0x00010000, 0x91e58067, 0, 0, 0, },
   {   "a85-13.rom", 0x00010000, 0x3eeaf878, 0, 0, 0, },
   {   "a85-14.rom", 0x00010000, 0xa9df0453, 0, 0, 0, },
   {   "a85-15.rom", 0x00010000, 0xaebc8c32, 0, 0, 0, },
   {   "a85-16.rom", 0x00010000, 0x4094d750, 0, 0, 0, },
   {   "a85-17.rom", 0x00010000, 0xc141d5ab, 0, 0, 0, },
   {   "knightb.uc", 0x00000800, 0x3cc2bbe4, 0, 0, 0, },
   {           NULL,          0,          0, 0, 0, 0, },
};

static struct INPUT_INFO kiki_kai_kai_inputs[] =
{
   { KB_DEF_COIN1,        MSG_COIN1,               0x020005, 0x01, BIT_ACTIVE_0 },
   { KB_DEF_COIN2,        MSG_COIN2,               0x020005, 0x02, BIT_ACTIVE_0 },
   { KB_DEF_TILT,         MSG_TILT,                0x020000, 0x04, BIT_ACTIVE_0 },
   { KB_DEF_SERVICE,      MSG_SERVICE,             0x020000, 0x01, BIT_ACTIVE_0 },

   { KB_DEF_P1_START,     MSG_P1_START,            0x020000, 0x08, BIT_ACTIVE_0 },
   { KB_DEF_P1_UP,        MSG_P1_UP,               0x020001, 0x01, BIT_ACTIVE_0 },
   { KB_DEF_P1_DOWN,      MSG_P1_DOWN,             0x020001, 0x02, BIT_ACTIVE_0 },
   { KB_DEF_P1_LEFT,      MSG_P1_LEFT,             0x020001, 0x04, BIT_ACTIVE_0 },
   { KB_DEF_P1_RIGHT,     MSG_P1_RIGHT,            0x020001, 0x08, BIT_ACTIVE_0 },
   { KB_DEF_P1_B1,        MSG_P1_B1,               0x020001, 0x10, BIT_ACTIVE_0 },
   { KB_DEF_P1_B2,        MSG_P1_B2,               0x020001, 0x20, BIT_ACTIVE_0 },

   { KB_DEF_P2_START,     MSG_P2_START,            0x020000, 0x10, BIT_ACTIVE_0 },
   { KB_DEF_P2_UP,        MSG_P2_UP,               0x020002, 0x01, BIT_ACTIVE_0 },
   { KB_DEF_P2_DOWN,      MSG_P2_DOWN,             0x020002, 0x02, BIT_ACTIVE_0 },
   { KB_DEF_P2_LEFT,      MSG_P2_LEFT,             0x020002, 0x04, BIT_ACTIVE_0 },
   { KB_DEF_P2_RIGHT,     MSG_P2_RIGHT,            0x020002, 0x08, BIT_ACTIVE_0 },
   { KB_DEF_P2_B1,        MSG_P2_B1,               0x020002, 0x10, BIT_ACTIVE_0 },
   { KB_DEF_P2_B2,        MSG_P2_B2,               0x020002, 0x20, BIT_ACTIVE_0 },

   { 0,                   NULL,                    0,        0,    0            },
};

static struct DSW_DATA dsw_data_kiki_kai_kai_0[] =
{
   { "Cabinet",               0x01, 0x02 },
   { "Upright",               0x01, 0x00 },
   { "Table",                 0x00, 0x00 },
   { MSG_DSWA_BIT2,           0x02, 0x02 },
   { MSG_OFF,                 0x02, 0x00 },
   { MSG_ON,                  0x00, 0x00 },
   { MSG_TEST_MODE,           0x04, 0x02 },
   { MSG_OFF,                 0x04, 0x00 },
   { MSG_ON,                  0x00, 0x00 },
   { MSG_DEMO_SOUND,          0x08, 0x02 },
   { MSG_ON,                  0x08, 0x00 },
   { MSG_OFF,                 0x00, 0x00 },
   { MSG_COIN1,               0x30, 0x04 },
   { MSG_1COIN_1PLAY,         0x30, 0x00 },
   { MSG_1COIN_2PLAY,         0x20, 0x00 },
   { MSG_2COIN_1PLAY,         0x10, 0x00 },
   { MSG_2COIN_3PLAY,         0x00, 0x00 },
   { MSG_COIN2,               0xC0, 0x04 },
   { MSG_1COIN_1PLAY,         0xC0, 0x00 },
   { MSG_1COIN_2PLAY,         0x80, 0x00 },
   { MSG_2COIN_1PLAY,         0x40, 0x00 },
   { MSG_2COIN_3PLAY,         0x00, 0x00 },
   { NULL,                    0,    0,   },
};

static struct DSW_DATA dsw_data_kiki_kai_kai_1[] =
{
   { MSG_DIFFICULTY,          0x03, 0x04 },
   { MSG_NORMAL,              0x03, 0x00 },
   { MSG_EASY,                0x02, 0x00 },
   { MSG_HARD,                0x01, 0x00 },
   { MSG_HARDEST,             0x00, 0x00 },
   { "Extra Life",            0x0C, 0x04 },
   { "70k 150k",              0x0C, 0x00 },
   { "70k 200k",              0x08, 0x00 },
   { "100k 300k",             0x04, 0x00 },
   { "50k 100k",              0x00, 0x00 },
   { "Lives",                 0x30, 0x04 },
   { "3",                     0x30, 0x00 },
   { "4",                     0x20, 0x00 },
   { "5",                     0x10, 0x00 },
   { "2",                     0x00, 0x00 },
   { "Unknown",		      0x40, 0x02 },
   { MSG_OFF,                 0x40, 0x00 },
   { MSG_ON,                  0x00, 0x00 },
   { "Number Match",          0x80, 0x02 },
   { MSG_OFF,                 0x80, 0x00 },
   { MSG_ON,                  0x00, 0x00 },
   { NULL,                    0,    0,   },
};

static struct DSW_INFO kiki_kai_kai_dsw[] =
{
   { 0x020003, 0xFF, dsw_data_kiki_kai_kai_0 },
   { 0x020004, 0xFF, dsw_data_kiki_kai_kai_1 },
   { 0,        0,    NULL,      },
};

GAME( kiki_kai_kai ,
   kiki_kai_kai_dirs,
   kiki_kai_kai_roms,
   kiki_kai_kai_inputs,
   kiki_kai_kai_dsw,
   NULL,

   LoadKikiKaiKai,
   ClearKSystem,
   &kiki_kai_kai_video,
   ExecuteKSystemFrame,
   "kikikai",
   "Kiki Kai Kai",
   NULL,
   COMPANY_ID_TAITO,
   "A85",
   1986,
   kiki_kai_kai_sound,
   GAME_PLATFORM
);

static struct DIR_INFO knight_boy_dirs[] =
{
   { "knight_boy", },
   { "knightb", },
   { ROMOF("kikikai"), },
   { CLONEOF("kikikai"), },
   { NULL, },
};

static struct ROM_INFO knight_boy_roms[] =
{
   {  "a85-10.rom", 0x00000100, 0x8fc3fa86, 0, 0, 0, },
   {  "a85-09.rom", 0x00000100, 0xb931c94d, 0, 0, 0, },
   {  "a85-08.rom", 0x00000100, 0xd15f61a8, 0, 0, 0, },
   {  "a85-11.rom", 0x00008000, 0xcc3539db, 0, 0, 0, },
   {  "a85-12.rom", 0x00010000, 0x91e58067, 0, 0, 0, },
   {   "knightb.b", 0x00010000, 0x63ad7df3, 0, 0, 0, },
   {  "a85-14.rom", 0x00010000, 0xa9df0453, 0, 0, 0, },
   {   "knightb.d", 0x00010000, 0x53ecdb3f, 0, 0, 0, },
   {  "a85-16.rom", 0x00010000, 0x4094d750, 0, 0, 0, },
   {  "a85-17.rom", 0x00010000, 0xc141d5ab, 0, 0, 0, },
   {  "knightb.uc", 0x00000800, 0x3cc2bbe4, 0, 0, 0, },
   {           NULL,          0,          0, 0, 0, 0, },
};

GAME( knight_boy ,
   knight_boy_dirs,
   knight_boy_roms,
   kiki_kai_kai_inputs,
   kiki_kai_kai_dsw,
   NULL,

   LoadKnightBoy,
   ClearKSystem,
   &kiki_kai_kai_video,
   ExecuteKSystemFrame,
   "knightb",
   "Knight Boy",
   NULL,
   COMPANY_ID_BOOTLEG,
   NULL,
   1986,
   kiki_kai_kai_sound,
   GAME_PLATFORM
);

static int romset;

static UINT8 *RAM2;
static UINT8 *ROM2;
static UINT8 *RAM_INPUT;
static UINT8 *RAM_COLOUR;
static UINT8 *RAM_MCU;

static UINT8 *GFX_BG0_SOLID;

/*

TAITO K-SYSTEM
--------------

0000-7FFF BASE ROM
8000-BFFF BANK ROM
D800-EFFF RAM
F000-F000 BANK SWITCH
F008-F008 MCU ENABLE?
F010-F010 INPUT READ
F018-F018 WATCHDOG
C000-FFFF <misc>

C500-CCFF SPRITE TILE RAM
CD00-D4FF BG0 RAM
D500-D501 BG0 SCROLL X
D502-D503 BG0 SCROLL Y

---

0000-7FFF BASE ROM
8000-9FFF COMMUNICATE RAM
C000-C001 YM2203

Supported romsets:

0 - B36 - Kiki Kai Kai          - 1986
1 - A87 - Kick and Run          - 1985

- Hardware info courtesy of Carl/DSC [Antiriad].

Todo:

- Graphics glitches: Shadows under monsters look like they might be in the
                     wrong priority... hmm..
- Player 2 inputs don't work in KKK?? (work in test mode, not in game).
  Could be do to with writes to 0xffff...
  Works in 'Table' mode, but using P1 inputs.
- I think this H/W has a system to connect 2 boards together...

*/

/******************************************************************************/
/* K-SYSTEM Z80 ROM BANKING                                                   */
/******************************************************************************/

static UINT8 Z80Bank, gfx_bank;

static UINT8 bank_map[8] =
{
   4,5,6,7,2,3,0,1,
};

void KSystemNewBankWrite(UINT16 offset, UINT8 data)
{
   if(bank_map[data&7] != Z80Bank){
      Z80Bank = bank_map[data&7];
      z80_set_bank(1,Z80Bank);
   }
   gfx_bank = (data & 0x20) >> 5;
}

static void init_bank_rom(UINT8 *src, UINT8 *dst)
{
  z80_init_banks_area_ofs(1,src,0x20000,0x8000,0x4000,2);
}

/******************************************************************************/
/* K-SYSTEM COMMON RAM                                                        */
/******************************************************************************/

#if 0
// Unused functions !
static void KSystemCommonRAMWrite(UINT16 offset, UINT8 data)
{
   RAM[0xC000+(offset&0x1FFF)] = data;
}

static UINT8 KSystemCommonRAMRead(UINT16 offset)
{
   return RAM[0xC000+(offset&0x1FFF)];
}
#endif

/******************************************************************************/
/* K-SYSTEM MCU                                                               */
/******************************************************************************/

static int mcu_enable;
static UINT8 mcu_addr, mcu_latch, mcu_old_data;

static void KSystemMCUWrite(UINT16 offset, UINT8 data)
{
   offset&=0xFF;

#ifdef RAINE_DEBUG
   //print_debug("MCU Write: %02x:%02x [%04x]\n", offset, data, z80pc);
#endif

   RAM[offset+0xE800] = data;
}

static UINT8 KSystemMCURead(UINT16 offset)
{
   offset&=0xFF;

#ifdef RAINE_DEBUG
   //print_debug("MCU Read: %02x:?? [%04x]\n", offset, z80pc);
#endif

   return RAM[offset+0xE800];
}

void KikiKaiKai_mcu(int bih_count);
void KikiKaiKai_mcu_reset(void);
void KickRun_mcu(int bih_count);
void KickRun_mcu_reset(void);

UINT8 KSystem_MCU_RDMEM(int a)
{
   //print_debug("MCU RDMEM(0x%x)=%x\n", a, RAM_MCU[a]);
   if (a == 0x02) /* Port C - COIN,TILT,etc */
   {
      return RAM_INPUT[5];
   }
   return RAM_MCU[a];
}

void KSystem_MCU_WRMEM(int a, UINT8 v)
{
   UINT8 rising,falling;

   //print_debug("MCU WRMEM(0x%x,0x%x)\n", a, v);
   RAM_MCU[a] = v;

   if (a == 0x01) /* I/O control */
   {
      rising = (v ^ mcu_old_data) & v; /* only the rising-edge triggers these */
      falling = (v ^ mcu_old_data) & mcu_old_data; /* only the falling-edge triggers these */
      mcu_old_data = v;

      if (falling & 0x01) RAM_MCU[0x00] = mcu_latch;
      if (rising & 0x02) mcu_addr = RAM_MCU[0x00];
      if (falling & 0x08)
      {
         if (v & 0x10) /* read */
         {
            if (v & 4) /* System RAM */
            {
               mcu_latch = RAM[0xe800 + mcu_addr];
            } else {
               mcu_latch = RAM_INPUT[mcu_addr+1];
            }
         } else {    /* write */
            if (v & 4) /* System RAM */
            {
               RAM[0xe800 + mcu_addr] = RAM_MCU[0x00];
            } else {
               print_debug("INPUT WRITE!!?! (%x)\n", mcu_addr);
            }
         }
      }
      //if (falling & 0x20) mz80int(0xff);
   }
}


static void KSystemF008Write(UINT16 offset, UINT8 data)
{
   print_debug("MCU control: %04x:%02x [%04x]\n", offset, data, z80pc);

   // Just a hack for the MCU

   if(data == 0x1e)
      mcu_enable = 1;
   else
      mcu_enable = 0;
}

/******************************************************************************/
/* K-SYSTEM INPUT RAM                                                         */
/******************************************************************************/

static void KSystemInputWrite(UINT16 offset, UINT8 data)
{
   print_debug("Input Write: %04x:%02x [%04x]\n", offset, data, z80pc);
}

static UINT8 KSystemInputRead(UINT16 offset)
{
   return RAM_INPUT[0];
}

/******************************************************************************/
/* K-SYSTEM YM2203 AND DSW ACCESS                                             */
/******************************************************************************/

static UINT8 ksys_ym2203_reg;

static UINT8 KSystemYM2203Read(UINT16 offset)
{
   //print_debug("YM2203 RB:%04x/?? [%04x]\n", offset, z80pc);

   if((offset&1)==0){
      return 0; //YM2203_status_port_0_r(0);
   }
   else{
      switch(ksys_ym2203_reg){
         case 0x0e: return get_dsw(0);
         case 0x0f: return get_dsw(1);
         default:   return 0; //return YM2203_read_port_0_r(0);
      }
   }
}

static void KSystemYM2203Write(UINT16 offset, UINT8 data)
{
   //print_debug("YM2203 WB:%04x/%02x [%04x]\n", offset, data, z80pc);

   if((offset&1)==0){
      ksys_ym2203_reg = data;
   }
   else{
      YM2203_control_port_0_w(0,ksys_ym2203_reg);
      YM2203_write_port_0_w(1,data);
   }
}

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

static void DrawNibble0(UINT8 *out, UINT32 plane, UINT8 c)
{
   int count, t;

   count = 4;
      do {
         t = c & 1;
         *out = t << plane;
         out++;
         c >>= 1;
      } while(--count);
}

static void DrawNibble(UINT8 *out, UINT32 plane, UINT8 c)
{
   int count, t;

   count = 4;
      do {
         t = c & 1;
         *out |= t << plane;
         out++;
         c >>= 1;
      } while(--count);
}

void LoadKikiKaiKai(void)
{
   int ta,tb,tc,td;
   UINT8 *TMP;

   romset=0;
   mcu_enable=0;

   RAMSize=0x10000+0x10000+0x10+0x200+0x800;

   if(!(ROM2=AllocateMem(0x8000))) return;

   if(!(RAM=AllocateMem(RAMSize))) return;

   RAM2       = RAM+0x10000;
   RAM_INPUT  = RAM+0x10000+0x10000;
   RAM_COLOUR = RAM+0x10000+0x10000+0x10;
   RAM_MCU    = RAM+0x10000+0x10000+0x10+0x200;

   if(!(TMP=AllocateMem(0x20000))) return;

   if(!load_rom("a85-17.rom",TMP+0x00000,0x10000)) return;	// Z80 MAIN ROM
   if(!load_rom("a85-16.rom",TMP+0x10000,0x10000)) return;	// Z80 MAIN ROM

   // Fix Checksum

   TMP[0x485a]=0xC9;  // RET

   // Skip Idle Z80

   TMP[0x0503]=0x28;  // JR Z,#0508
   TMP[0x0504]=0x03;  //

   TMP[0x0505]=0xD3;  // OUTA (AAh)
   TMP[0x0506]=0xAA;  //

   SetStopZ80BMode2(0x04FF);

   init_bank_rom(TMP,ROM);

   memset(RAM+0x00000, 0x00, RAMSize);
   memcpy(RAM, TMP, 0x8000+0x4000);

   AddZ80BROMBase(RAM, 0x0038, 0x0066);

   AddZ80BReadByte(0x0000, 0xBFFF, NULL,                    NULL);         // Z80 ROM/BANK ROM
   AddZ80BReadByte(0xC000, 0xE7FF, NULL,                    RAM+0xC000);   // RAM
   AddZ80BReadByte(0xE800, 0xE8FF, KSystemMCURead,          NULL);         // MCU COMM AREA
   AddZ80BReadByte(0xE900, 0xEFFF, NULL,                    RAM+0xE900);   // RAM
   AddZ80BReadByte(0xF010, 0xF010, KSystemInputRead,        NULL);         // INPUT
   AddZ80BReadByte(0x0000, 0xFFFF, DefBadReadZ80,           NULL);         // <bad reads>
   AddZ80BReadByte(-1, -1, NULL, NULL);

   AddZ80BWriteByte(0xC000, 0xE7FF, NULL,                   RAM+0xC000);   // RAM
   AddZ80BWriteByte(0xE800, 0xE8FF, KSystemMCUWrite,        NULL);         // MCU COMM AREA
   AddZ80BWriteByte(0xE900, 0xEFFF, NULL,                   RAM+0xE900);   // RAM
   AddZ80BWriteByte(0xF000, 0xF000, KSystemNewBankWrite,    NULL);         // ROM BANK
   AddZ80BWriteByte(0xF008, 0xF008, KSystemF008Write,       NULL);         // MCU??
   AddZ80BWriteByte(0xF010, 0xF010, KSystemInputWrite,      NULL);         // INPUT
   AddZ80BWriteByte(0xF018, 0xF018, NULL,                   RAM+0xF018);   // WATCHDOG
   AddZ80BWriteByte(0x0000, 0xFFFF, DefBadWriteZ80,         NULL);         // <bad writes>
   AddZ80BWriteByte(-1, -1, NULL, NULL);

   AddZ80BReadPort(0x00, 0xFF, DefBadReadZ80,			NULL);	// <bad reads>
   AddZ80BReadPort(-1, -1, NULL, NULL);

   AddZ80BWritePort(0xAA, 0xAA, StopZ80BMode2,			NULL);	// Trap Idle Z80
   AddZ80BWritePort(0x00, 0xFF, DefBadWriteZ80,			NULL);	// <bad reads>
   AddZ80BWritePort(-1, -1, NULL, NULL);

   AddZ80BInit();

   FreeMem(TMP);

   if(!load_rom("a85-11.rom",ROM2,0x8000)) return;		// Z80 SUB ROM

   // Skip Idle Z80
   // -------------

   ROM2[0x007F]=0x00;  // NOP
   ROM2[0x0080]=0x00;  // NOP

   ROM2[0x00BE]=0xC3;  // JP 00A4
   ROM2[0x00BF]=0xA4;  //
   ROM2[0x00C0]=0x00;  //

   ROM2[0x009C]=0x1A;  //
   ROM2[0x009D]=0x32;  //
   ROM2[0x009E]=0xFD;  //
   ROM2[0x009F]=0x9F;  //
   ROM2[0x00A0]=0x36;  //
   ROM2[0x00A1]=0x0F;  //
   ROM2[0x00A2]=0x18;  //
   ROM2[0x00A3]=0x10;  //

   ROM2[0x00A4]=0xD3;  // OUTA (AAh)
   ROM2[0x00A5]=0xAA;  //
   ROM2[0x00A6]=0x7E;  //
   ROM2[0x00A7]=0xFE;  //
   ROM2[0x00A8]=0xFF;  //
   ROM2[0x00A9]=0x28;  //
   ROM2[0x00AA]=0xF9;  //
   ROM2[0x00AB]=0xC3;  //
   ROM2[0x00AC]=0xC3;  //
   ROM2[0x00AD]=0x00;  //

   SetStopZ80CMode2(0x00A6);

   memset(RAM2+0x00000, 0x00, 0x10000);
   memcpy(RAM2, ROM2, 0x8000);

   AddZ80CROMBase(RAM2, 0x0038, 0x0066);

   AddZ80CReadByte(0x0000, 0x7FFF, NULL,                    RAM2+0x0000);   // Z80 ROM
   AddZ80CReadByte(0x8000, 0x9FFF, NULL,                    RAM +0xC000);   // COMMON RAM
   AddZ80CReadByte(0xC000, 0xC001, KSystemYM2203Read,       NULL);          // YM2203; INPUT
   AddZ80CReadByte(0xA800, 0xC7FF, NULL,                    RAM2+0xA800);   // RAM?
   AddZ80CReadByte(0x0000, 0xFFFF, DefBadReadZ80,           NULL);          // <bad reads>
   AddZ80CReadByte(-1, -1, NULL, NULL);

   AddZ80CWriteByte(0x8000, 0x9FFF, NULL,                   RAM +0xC000);   // COMMON RAM
   AddZ80CWriteByte(0xC000, 0xC001, KSystemYM2203Write,     NULL);          // YM2203; INPUT
   AddZ80CWriteByte(0xA800, 0xC7FF, NULL,                   RAM2+0xA800);   // RAM?
   AddZ80CWriteByte(0x0000, 0xFFFF, DefBadWriteZ80,         NULL);          // <bad writes>
   AddZ80CWriteByte(-1, -1, NULL, NULL);

   AddZ80CReadPort(0x00, 0xFF, DefBadReadZ80,			NULL);	// <bad reads>
   AddZ80CReadPort(-1, -1, NULL, NULL);

   AddZ80CWritePort(0xAA, 0xAA, StopZ80CMode2,			NULL);	// Trap Idle Z80
   AddZ80CWritePort(0x00, 0xFF, DefBadWriteZ80,			NULL);	// <bad reads>
   AddZ80CWritePort(-1, -1, NULL, NULL);

   AddZ80CInit();

   /* MCU ROM */
   if(!load_rom("knightb.uc",RAM_MCU,0x800)) return;

   if(!(GFX=AllocateMem(0x080000))) return;
   if(!(TMP=AllocateMem(0x040000))) return;

   tb=0;
   if(!load_rom("a85-15.rom", TMP+0x00000, 0x10000)) return;
   if(!load_rom("a85-14.rom", TMP+0x10000, 0x10000)) return;
   if(!load_rom("a85-13.rom", TMP+0x20000, 0x10000)) return;
   if(!load_rom("a85-12.rom", TMP+0x30000, 0x10000)) return;
   for(ta=0;ta<0x20000;ta+=2,tb+=8){
      DrawNibble0(GFX+tb+0, 0, (UINT8) (TMP[ta+0x00000]&15) );
      DrawNibble (GFX+tb+0, 1, (UINT8) (TMP[ta+0x00000]>>4) );
      DrawNibble (GFX+tb+0, 2, (UINT8) (TMP[ta+0x20000]&15) );
      DrawNibble (GFX+tb+0, 3, (UINT8) (TMP[ta+0x20000]>>4) );
      DrawNibble0(GFX+tb+4, 0, (UINT8) (TMP[ta+0x00001]&15) );
      DrawNibble (GFX+tb+4, 1, (UINT8) (TMP[ta+0x00001]>>4) );
      DrawNibble (GFX+tb+4, 2, (UINT8) (TMP[ta+0x20001]&15) );
      DrawNibble (GFX+tb+4, 3, (UINT8) (TMP[ta+0x20001]>>4) );
   }

   tb=0;
   if(!load_rom("a85-08.rom", TMP+0x00000, 0x80)) return;
   if(!load_rom("a85-10.rom", TMP+0x00080, 0x80)) return;
   if(!load_rom("a85-09.rom", TMP+0x00100, 0x80)) return;
   for(ta=0;ta<0x80;ta++,tb+=2){
      td  = (ta&0x70)|(0x0F-(ta&0x0F));
      tc  = (TMP[td+0x000]&0x0F)<<8;
      tc |= (TMP[td+0x080]&0x0F)<<4;
      tc |= (TMP[td+0x100]&0x0F)<<0;
      WriteWord(&RAM_COLOUR[tb+0x000],tc);
      WriteWord(&RAM_COLOUR[tb+0x100],tc);
   }

   FreeMem(TMP);

   GFX_BG0_SOLID = make_solid_mask_8x8(GFX, 0x2000);
}

void LoadKnightBoy(void)
{
   int ta,tb,tc,td;
   UINT8 *TMP;

   romset=0;
   mcu_enable=0;

   RAMSize=0x10000+0x10000+0x10+0x200+0x800;

   if(!(ROM2=AllocateMem(0x8000))) return;

   if(!(RAM=AllocateMem(RAMSize))) return;

   RAM2       = RAM+0x10000;
   RAM_INPUT  = RAM+0x10000+0x10000;
   RAM_COLOUR = RAM+0x10000+0x10000+0x10;
   RAM_MCU    = RAM+0x10000+0x10000+0x10+0x200;

   if(!(TMP=AllocateMem(0x20000))) return;

   if(!load_rom("a85-17.rom",TMP+0x00000,0x10000)) return;	// Z80 MAIN ROM
   if(!load_rom("a85-16.rom",TMP+0x10000,0x10000)) return;	// Z80 MAIN ROM

   // Fix Checksum

   TMP[0x485a]=0xC9;  // RET

   // Skip Idle Z80

   TMP[0x0503]=0x28;  // JR Z,#0508
   TMP[0x0504]=0x03;  //

   TMP[0x0505]=0xD3;  // OUTA (AAh)
   TMP[0x0506]=0xAA;  //

   SetStopZ80BMode2(0x04FF);

   init_bank_rom(TMP,ROM);

   memset(RAM+0x00000, 0x00, 0x10000);
   memcpy(RAM, TMP, 0x8000+0x4000);

   AddZ80BROMBase(RAM, 0x0038, 0x0066);

   AddZ80BReadByte(0x0000, 0xBFFF, NULL,                    NULL);         // Z80 ROM/BANK ROM
   AddZ80BReadByte(0xC000, 0xE7FF, NULL,                    RAM+0xC000);   // RAM
   AddZ80BReadByte(0xE800, 0xE8FF, KSystemMCURead,          NULL);         // MCU COMM AREA
   AddZ80BReadByte(0xE900, 0xEFFF, NULL,                    RAM+0xE900);   // RAM
   AddZ80BReadByte(0xF010, 0xF010, KSystemInputRead,        NULL);         // INPUT
   AddZ80BReadByte(0x0000, 0xFFFF, DefBadReadZ80,           NULL);         // <bad reads>
   AddZ80BReadByte(-1, -1, NULL, NULL);

   AddZ80BWriteByte(0xC000, 0xE7FF, NULL,                   RAM+0xC000);   // RAM
   AddZ80BWriteByte(0xE800, 0xE8FF, KSystemMCUWrite,        NULL);         // MCU COMM AREA
   AddZ80BWriteByte(0xE900, 0xEFFF, NULL,                   RAM+0xE900);   // RAM
   AddZ80BWriteByte(0xF000, 0xF000, KSystemNewBankWrite,    NULL);         // ROM BANK
   AddZ80BWriteByte(0xF008, 0xF008, KSystemF008Write,       NULL);         // MCU??
   AddZ80BWriteByte(0xF010, 0xF010, KSystemInputWrite,      NULL);         // INPUT
   AddZ80BWriteByte(0xF018, 0xF018, NULL,                   RAM+0xF018);   // WATCHDOG
   AddZ80BWriteByte(0x0000, 0xFFFF, DefBadWriteZ80,         NULL);         // <bad writes>
   AddZ80BWriteByte(-1, -1, NULL, NULL);

   AddZ80BReadPort(0x00, 0xFF, DefBadReadZ80,			NULL);	// <bad reads>
   AddZ80BReadPort(-1, -1, NULL, NULL);

   AddZ80BWritePort(0xAA, 0xAA, StopZ80BMode2,			NULL);	// Trap Idle Z80
   AddZ80BWritePort(0x00, 0xFF, DefBadWriteZ80,			NULL);	// <bad reads>
   AddZ80BWritePort(-1, -1, NULL, NULL);

   AddZ80BInit();

   FreeMem(TMP);

   if(!load_rom("a85-11.rom",ROM2,0x8000)) return;		// Z80 SUB ROM

   // Skip Idle Z80
   // -------------

   ROM2[0x007F]=0x00;  // NOP
   ROM2[0x0080]=0x00;  // NOP

   ROM2[0x00BE]=0xC3;  // JP 00A4
   ROM2[0x00BF]=0xA4;  //
   ROM2[0x00C0]=0x00;  //

   ROM2[0x009C]=0x1A;  //
   ROM2[0x009D]=0x32;  //
   ROM2[0x009E]=0xFD;  //
   ROM2[0x009F]=0x9F;  //
   ROM2[0x00A0]=0x36;  //
   ROM2[0x00A1]=0x0F;  //
   ROM2[0x00A2]=0x18;  //
   ROM2[0x00A3]=0x10;  //

   ROM2[0x00A4]=0xD3;  // OUTA (AAh)
   ROM2[0x00A5]=0xAA;  //
   ROM2[0x00A6]=0x7E;  //
   ROM2[0x00A7]=0xFE;  //
   ROM2[0x00A8]=0xFF;  //
   ROM2[0x00A9]=0x28;  //
   ROM2[0x00AA]=0xF9;  //
   ROM2[0x00AB]=0xC3;  //
   ROM2[0x00AC]=0xC3;  //
   ROM2[0x00AD]=0x00;  //

   SetStopZ80CMode2(0x00A6);

   memset(RAM2+0x00000, 0x00, 0x10000);
   memcpy(RAM2, ROM2, 0x8000);

   AddZ80CROMBase(RAM2, 0x0038, 0x0066);

   AddZ80CReadByte(0x0000, 0x7FFF, NULL,                    RAM2+0x0000);   // Z80 ROM
   AddZ80CReadByte(0x8000, 0x9FFF, NULL,                    RAM +0xC000);   // COMMON RAM
   AddZ80CReadByte(0xC000, 0xC001, KSystemYM2203Read,       NULL);          // YM2203; INPUT
   AddZ80CReadByte(0xA000, 0xC7FF, NULL,                    RAM2+0xA000);   // RAM?
   AddZ80CReadByte(0x0000, 0xFFFF, DefBadReadZ80,           NULL);          // <bad reads>
   AddZ80CReadByte(-1, -1, NULL, NULL);

   AddZ80CWriteByte(0x8000, 0x9FFF, NULL,                   RAM +0xC000);   // COMMON RAM
   AddZ80CWriteByte(0xC000, 0xC001, KSystemYM2203Write,     NULL);          // YM2203; INPUT
   AddZ80CWriteByte(0xA000, 0xC7FF, NULL,                   RAM2+0xA000);   // RAM?
   AddZ80CWriteByte(0x0000, 0xFFFF, DefBadWriteZ80,         NULL);          // <bad writes>
   AddZ80CWriteByte(-1, -1, NULL, NULL);

   AddZ80CReadPort(0x00, 0xFF, DefBadReadZ80,			NULL);	// <bad reads>
   AddZ80CReadPort(-1, -1, NULL, NULL);

   AddZ80CWritePort(0xAA, 0xAA, StopZ80CMode2,			NULL);	// Trap Idle Z80
   AddZ80CWritePort(0x00, 0xFF, DefBadWriteZ80,			NULL);	// <bad reads>
   AddZ80CWritePort(-1, -1, NULL, NULL);

   AddZ80CInit();

   /* MCU ROM */
   if(!load_rom("knightb.uc",RAM_MCU,0x800)) return;

   if(!(GFX=AllocateMem(0x080000))) return;
   if(!(TMP=AllocateMem(0x040000))) return;

   tb=0;
   if(!load_rom("knightb.d", TMP+0x00000, 0x10000)) return;
   if(!load_rom("a85-14.rom", TMP+0x10000, 0x10000)) return;
   if(!load_rom("knightb.b", TMP+0x20000, 0x10000)) return;
   if(!load_rom("a85-12.rom", TMP+0x30000, 0x10000)) return;
   for(ta=0;ta<0x20000;ta+=2,tb+=8){
      DrawNibble0(GFX+tb+0, 0, (UINT8) (TMP[ta+0x00000]&15) );
      DrawNibble (GFX+tb+0, 1, (UINT8) (TMP[ta+0x00000]>>4) );
      DrawNibble (GFX+tb+0, 2, (UINT8) (TMP[ta+0x20000]&15) );
      DrawNibble (GFX+tb+0, 3, (UINT8) (TMP[ta+0x20000]>>4) );
      DrawNibble0(GFX+tb+4, 0, (UINT8) (TMP[ta+0x00001]&15) );
      DrawNibble (GFX+tb+4, 1, (UINT8) (TMP[ta+0x00001]>>4) );
      DrawNibble (GFX+tb+4, 2, (UINT8) (TMP[ta+0x20001]&15) );
      DrawNibble (GFX+tb+4, 3, (UINT8) (TMP[ta+0x20001]>>4) );
   }

   tb=0;
   if(!load_rom("a85-08.rom", TMP+0x00000, 0x80)) return;
   if(!load_rom("a85-10.rom", TMP+0x00080, 0x80)) return;
   if(!load_rom("a85-09.rom", TMP+0x00100, 0x80)) return;
   for(ta=0;ta<0x80;ta++,tb+=2){
      td  = (ta&0x70)|(0x0F-(ta&0x0F));
      tc  = (TMP[td+0x000]&0x0F)<<8;
      tc |= (TMP[td+0x080]&0x0F)<<4;
      tc |= (TMP[td+0x100]&0x0F)<<0;
      WriteWord(&RAM_COLOUR[tb+0x000],tc);
      WriteWord(&RAM_COLOUR[tb+0x100],tc);
   }

   FreeMem(TMP);

   GFX_BG0_SOLID = make_solid_mask_8x8(GFX, 0x2000);
}

void ClearKSystem(void)
{
#ifdef RAINE_DEBUG
      save_debug("RAM.bin", RAM, RAMSize, 0);
      save_debug("GFX.bin", GFX, 0x080000, 0);
#endif
}

/* this is a patch for Kicn&Run.
 * it hacks it to accept an MCU from Mexico86.
 * If we ever got a proper K&R dump, we could probably remove this patch.
 */
static UINT8 PatchTable[] = {
   0x3E, 0x00, 0x32, 0x4A, 0xE8, 0x32, 0x4B, 0xE8,
   0x32, 0x77, 0xE8, 0x32, 0x54, 0xE8, 0x32, 0x60,
   0xE8, 0x3E, 0x37, 0x32, 0x4C, 0xE8, 0x3E, 0x12,
   0x32, 0x4F, 0xE8, 0x3E, 0x08, 0x32, 0x7B, 0xE8,
   0x3E, 0x80, 0x32, 0x7F, 0xE8, 0x3E, 0x92, 0x32,
   0x00, 0xE8, 0xC9
};

void LoadKickRun(void)
{
   int ta,tb,tc,td;
   UINT8 *TMP;

   romset=1;
   mcu_enable=0;

   RAMSize=0x10000+0x10000+0x10+0x200+0x800;

   if(!(ROM2=AllocateMem(0x8000))) return;

   if(!(RAM=AllocateMem(RAMSize))) return;

   RAM2       = RAM+0x10000;
   RAM_INPUT  = RAM+0x10000+0x10000;
   RAM_COLOUR = RAM+0x10000+0x10000+0x10;
   RAM_MCU    = RAM+0x10000+0x10000+0x10+0x200;

   if(!(TMP=AllocateMem(0x20000))) return;

   if(!load_rom("a87-08.bin",TMP+0x00000,0x10000)) return;  // Z80 MAIN ROM
   if(!load_rom("a87-07.bin",TMP+0x10000,0x10000)) return;  // Z80 MAIN ROM

   /* Mexico86 is a bootleg of Kick&Run. We don't have a MCU dump for K&R, so
    * we will use Mexico86's. However, the Mex86 dump is not quite an 100%
    * copy, so we need to patch Kick&Run to accept it.
    * If we ever got a proper K&R dump, we could probably remove this patch.
    */
   TMP[0x01C8] = 0x92;
   TMP[0x0207] = 0x3E;
   TMP[0x0208] = 0x92;
   TMP[0x0209] = 0x00;
   TMP[0x0223] = 0x18;
   TMP[0x07D1] = 0x18;
   for (ta=0;ta<sizeof(PatchTable);ta++)
   {
      TMP[0x084C+ta] = PatchTable[ta];
   }
   for (ta=0x0877;ta<=0x08b1;ta++) TMP[ta] = 0xff;

   init_bank_rom(TMP,ROM);

   memset(RAM, 0x00, RAMSize);
   memcpy(RAM, TMP, 0x8000+0x4000);

   AddZ80BROMBase(RAM, 0x0038, 0x0066);

   AddZ80BReadByte(0x0000, 0xBFFF, NULL,              NULL);   // Z80 ROM/BANK ROM
   AddZ80BReadByte(0xC000, 0xE7FF, NULL,              RAM+0xC000);   // RAM
   AddZ80BReadByte(0xE800, 0xE8FF, KSystemMCURead,    NULL);	// MCU
   AddZ80BReadByte(0xE900, 0xEFFF, NULL,              RAM+0xE900);   // RAM
   AddZ80BReadByte(0xF010, 0xF010, KSystemInputRead,  NULL);   // INPUT
   AddZ80BReadByte(0xF800, 0xFFFF, NULL,              RAM+0xF800);   // RAM?
   AddZ80BReadByte(0x0000, 0xFFFF, DefBadReadZ80,     NULL);	// <bad reads>
   AddZ80BReadByte(-1, -1, NULL, NULL);

   AddZ80BWriteByte(0xC000, 0xE7FF, NULL,                RAM+0xC000);   // RAM
   AddZ80BWriteByte(0xE800, 0xE8FF, KSystemMCUWrite,     NULL);         // MCU
   AddZ80BWriteByte(0xE900, 0xEFFF, NULL,                RAM+0xE900);   // RAM
   AddZ80BWriteByte(0xF000, 0xF000, KSystemNewBankWrite, NULL);         // ROM BANK
   AddZ80BWriteByte(0xF008, 0xF008, KSystemF008Write,    NULL);         // ?
   AddZ80BWriteByte(0xF010, 0xF010, KSystemInputWrite,   NULL);         // INPUT
   AddZ80BWriteByte(0xF018, 0xF018, NULL,                RAM+0xF018);   // WATCHDOG
   AddZ80BWriteByte(0xF800, 0xFFFF, NULL,                RAM+0xF800);   // RAM?
   AddZ80BWriteByte(0x0000, 0xFFFF, DefBadWriteZ80,      NULL);         // <bad writes>
   AddZ80BWriteByte(-1, -1, NULL, NULL);

   AddZ80BReadPort(0x00, 0xFF, DefBadReadZ80,			NULL);	// <bad reads>
   AddZ80BReadPort(-1, -1, NULL, NULL);

   AddZ80BWritePort(0xAA, 0xAA, StopZ80BMode2,			NULL);	// Trap Idle Z80
   AddZ80BWritePort(0x00, 0xFF, DefBadWriteZ80,			NULL);	// <bad reads>
   AddZ80BWritePort(-1, -1, NULL, NULL);

   AddZ80BInit();

   if(!load_rom("a87-06.bin",ROM2,0x8000)) return;		// Z80 SUB ROM

   // Skip Idle Z80
   // -------------

   ROM2[0x037d]=0xAF;  // XOR A (checksum always succeeds)
   ROM2[0x00d7]=0xD3;  // OUTA (AAh)
   ROM2[0x00d8]=0xAA;  //

   SetStopZ80CMode2(0x00d4);

   memset(RAM2+0x00000, 0x00, 0x10000);
   memcpy(RAM2, ROM2, 0x8000);

   AddZ80CROMBase(RAM2, 0x0038, 0x0066);

   AddZ80CReadByte(0x0000, 0x7FFF, NULL,                    RAM2+0x0000);   // Z80 ROM
   AddZ80CReadByte(0x8000, 0xA7FF, NULL,                    RAM +0xC000);   // COMMON RAM
   AddZ80CReadByte(0xC000, 0xC001, KSystemYM2203Read,       NULL);          // YM2203; INPUT
   AddZ80CReadByte(0xA800, 0xC7FF, NULL,                    RAM2+0xA800);   // RAM?
   AddZ80CReadByte(0x0000, 0xFFFF, DefBadReadZ80,           NULL);          // <bad reads>
   AddZ80CReadByte(-1, -1, NULL, NULL);

   AddZ80CWriteByte(0x8000, 0xA7FF, NULL,                   RAM +0xC000);   // COMMON RAM
   AddZ80CWriteByte(0xC000, 0xC001, KSystemYM2203Write,     NULL);          // YM2203; INPUT
   AddZ80CWriteByte(0xA800, 0xC7FF, NULL,                   RAM2+0xA800);   // RAM?
   AddZ80CWriteByte(0x0000, 0xFFFF, DefBadWriteZ80,         NULL);          // <bad writes>
   AddZ80CWriteByte(-1, -1, NULL, NULL);

   AddZ80CReadPort(0x00, 0xFF, DefBadReadZ80,         NULL);   // <bad reads>
   AddZ80CReadPort(-1, -1, NULL, NULL);

   AddZ80CWritePort(0xAA, 0xAA, StopZ80CMode2,			NULL);	// Trap Idle Z80
   AddZ80CWritePort(0x00, 0xFF, DefBadWriteZ80,			NULL);	// <bad reads>
   AddZ80CWritePort(-1, -1, NULL, NULL);

   AddZ80CInit();

   /* MCU ROM */
   if(!load_rom("68_h.bin",RAM_MCU,0x800)) return;

   if(!(GFX=AllocateMem(0x080000))) return;
   if(!(TMP=AllocateMem(0x040000))) return;

   if(!load_rom("a87-05.bin", TMP+0x00000, 0x10000)) return;
   if(!load_rom("a87-04.bin", TMP+0x10000, 0x08000)) return;
   if(!load_rom("a87-04.bin", TMP+0x18000, 0x08000)) return;
   if(!load_rom("a87-03.bin", TMP+0x20000, 0x10000)) return;
   if(!load_rom("a87-02.bin", TMP+0x30000, 0x08000)) return;
   if(!load_rom("a87-02.bin", TMP+0x38000, 0x08000)) return;

   // much annoyingness. we have to swap the upper/lower halves
   // of the 64K roms (the 32K ones just get repeated) :-(
   for(ta=0;ta<0x8000;ta++)
   {
      tb = TMP[0x00000+ta];
      TMP[0x00000+ta] = TMP[0x08000+ta];
      TMP[0x08000+ta] = tb;
      tb = TMP[0x20000+ta];
      TMP[0x20000+ta] = TMP[0x28000+ta];
      TMP[0x28000+ta] = tb;
   }

   tb=0;
   for(ta=0;ta<0x20000;ta+=2,tb+=8){
      DrawNibble0(GFX+tb+0, 0, (UINT8) (TMP[ta+0x00000]&15) );
      DrawNibble (GFX+tb+0, 1, (UINT8) (TMP[ta+0x00000]>>4) );
      DrawNibble (GFX+tb+0, 2, (UINT8) (TMP[ta+0x20000]&15) );
      DrawNibble (GFX+tb+0, 3, (UINT8) (TMP[ta+0x20000]>>4) );
      DrawNibble0(GFX+tb+4, 0, (UINT8) (TMP[ta+0x00001]&15) );
      DrawNibble (GFX+tb+4, 1, (UINT8) (TMP[ta+0x00001]>>4) );
      DrawNibble (GFX+tb+4, 2, (UINT8) (TMP[ta+0x20001]&15) );
      DrawNibble (GFX+tb+4, 3, (UINT8) (TMP[ta+0x20001]>>4) );
   }

   tb=0;
   if(!load_rom("a87-10.bin", TMP+0x00000, 0x100)) return;
   if(!load_rom("a87-12.bin", TMP+0x00100, 0x100)) return;
   if(!load_rom("a87-11.bin", TMP+0x00200, 0x100)) return;
   for(ta=0;ta<0x100;ta++,tb+=2){
      td  = ta ^ 0x0f;
      tc  = (TMP[td+0x000]&0x0F)<<8;
      tc |= (TMP[td+0x100]&0x0F)<<4;
      tc |= (TMP[td+0x200]&0x0F)<<0;
      WriteWord(&RAM_COLOUR[tb],tc);
   }

   FreeMem(TMP);

   GFX_BG0_SOLID = make_solid_mask_8x8(GFX, 0x2000);
}

void ExecuteKSystemFrame(void)
{
   int ta;

   if (cpu_get_pc(CPU_Z80_1) == 0x0000)
   {
      switch (romset) {
      case 0: KikiKaiKai_mcu_reset(); break;
      case 1: KickRun_mcu_reset(); break;
      }
   } else if (romset && cpu_get_pc(CPU_Z80_1) == 0x3ad3)
     // This is apparently caused by the protection : the z80 waits here
     // to be reseted, probably by the mcu. Since it does not happen in our
     // emulation, we force the reset.
     reset_game_hardware();

   // ---------------------

   if (romset==0)
   {
      for(ta=0;ta<16;ta++)
      {
         // probably actually only 6Mhz or so, but MZ80 bug means that...
         cpu_execute_cycles(CPU_Z80_1, CPU_FRAME_MHz(16,60)/16);  // Main Z80 (60fps)

         if(cpu_get_pc(CPU_Z80_2) != 0x00A6)

            cpu_execute_cycles(CPU_Z80_2, CPU_FRAME_MHz(8,60)/16);  // Sub Z80 (60fps)

      }

      print_debug("Z80PC_MAIN:%04x\n",cpu_get_pc(CPU_Z80_1));

      cpu_interrupt(CPU_Z80_1, 0x38);

      cpu_interrupt(CPU_Z80_2, 0x38);


   }
   else {
      cpu_execute_cycles(CPU_Z80_1, CPU_FRAME_MHz(8,60));   // Main Z80 6MHz (60fps)
      print_debug("Z80PC_MAIN:%04x\n",z80pc);
      cpu_interrupt(CPU_Z80_1, RAM[0xe800]);

      cpu_execute_cycles(CPU_Z80_2, CPU_FRAME_MHz(8,60));   // Sub Z80 4MHz (60fps)
      print_debug("Z80PC_SUB:%04x\n",z80pc);
      cpu_interrupt(CPU_Z80_2, 0x38);
   }

   if (mcu_enable)
   {
      switch (romset) {
      case 0: KikiKaiKai_mcu(1); break;
      case 1: KickRun_mcu(1); break;
      }
   }
}

void DrawKikiKaiKai(void)
{
   int x,y,ta;
   int sx,sy,offs,goffs,gfx_offs,gfx_num,gfx_attr,height,xc,yc;
   UINT8 map;

   // PALETTE

   if (RefreshBuffers)
   {
      for(x=0;x<0x200;x+=2)
      {
         ta = ReadWord(&RAM_COLOUR[x]);
         pal[x >> 1].r = (ta & 0x0F00) >> 6;
         pal[x >> 1].g = (ta & 0x00F0) >> 2;
         pal[x >> 1].b = (ta & 0x000F) << 2;
      }
   }

   // No Solid BG0

   clear_game_screen(0);

   // OBJECT

   sx = 0;
   for (offs=0;offs<0x300;offs+=4)
   {
      /* skip empty sprites */
      if (*(UINT32 *)(&RAM[offs+0xd500]) == 0) continue;

      gfx_num = RAM[0xd500 + offs + 1];
      gfx_attr = RAM[0xd500 + offs + 3];

      if ((gfx_num & 0x80) == 0)      /* 16x16 sprites */
      {
         gfx_offs = ((gfx_num & 0x1f) * 0x80) + ((gfx_num & 0x60) >> 1) + 12;
         height = 2;
      }
      else    /* tilemaps (each sprite is a 16x256 column) */
      {
         gfx_offs = ((gfx_num & 0x3f) * 0x80);
         height = 32;
      }

      if ((gfx_num & 0xc0) == 0xc0)
         sx += 16;
      else
      {
         sx = RAM[0xd500 + offs + 2];
         if (gfx_attr & 0x40) sx -= 256;
      }
      sy = 256 - height*8 - (RAM[0xd500 + offs + 0]);

      for (xc=0;xc<2;xc++)
      {
         goffs = gfx_offs;

         for (yc=0;yc<height;yc++,goffs+=2)
         {
            /* this is different to Bubble Bobble */
            ta = ReadWord(&RAM[0xc000+goffs]) & 0x1FFF;
            if (!GFX_BG0_SOLID[ta]) continue;       // No pixels; skip

            map = (RAM[0xc001 + goffs] >> 1) & 0x70;

            x = (sx + xc * 8) & 0xff;
            y = ((sy + yc * 8) & 0xff) -16;

            /* Kiki Kai Kai appears not to have a X/Y flip (eh?) */
            if ((x > -8) && (y > -8) && (x < 256) && (y < 224))
            {
               if (GFX_BG0_SOLID[ta] == 1)       // Some pixels; trans
               {
                  Draw8x8_Trans_Rot(&GFX[ta<<6], x+8, y+8, map);
               } else {                          // all pixels; solid
                  Draw8x8_Rot(&GFX[ta<<6], x+8, y+8, map);
               }
            }
         }

         gfx_offs += 64;
      }
   }
}

void DrawKickRun(void)
{
   int x,y,ta;
   int sx,sy,offs,goffs,gfx_offs,gfx_num,gfx_attr,height,xc,yc;
   UINT8 map;

   // PALETTE

   if (RefreshBuffers)
   {
      for(x=0;x<0x200;x+=2)
      {
         ta = ReadWord(&RAM_COLOUR[x]);
         pal[x >> 1].r = (ta & 0x0F00) >> 6;
         pal[x >> 1].g = (ta & 0x00F0) >> 2;
         pal[x >> 1].b = (ta & 0x000F) << 2;
      }
   }

   // No Solid BG0

   clear_game_screen(0);

   // OBJECT

   sx = 0;
   for (offs=0;offs<0x4c0;offs+=4)
   {
      // I really give up on this bollocks, I really do.
      // MAME says do this. <sigh>
      if (offs >= 0x300 && offs < 0x480) continue;

      /* skip empty sprites */
      if (*(UINT32 *)(&RAM[offs+0xd500]) == 0) continue;

      gfx_num = RAM[0xd500 + offs + 1];
      gfx_attr = RAM[0xd500 + offs + 3];

      if ((gfx_num & 0x80) == 0)      /* 16x16 sprites */
      {
         gfx_offs = ((gfx_num & 0x1f) * 0x80) + ((gfx_num & 0x60) >> 1) + 12;
         height = 2;
      }
      else    /* tilemaps (each sprite is a 16x256 column) */
      {
         gfx_offs = ((gfx_num & 0x3f) * 0x80);
         height = 32;
      }

      if ((gfx_num & 0xc0) == 0xc0)
         sx += 16;
      else
      {
         sx = RAM[0xd500 + offs + 2];
         // not this either (accoring to MAME at least)?
         //if (gfx_attr & 0x40) sx -= 256;
      }
      sy = 256 - height*8 - (RAM[0xd500 + offs + 0]);

      for (xc=0;xc<2;xc++)
      {
         goffs = gfx_offs;

         for (yc=0;yc<height;yc++,goffs+=2)
         {
            /* this is different to both Bubble Bobble and KKK */

            ta = ReadWord(&RAM[0xc000+goffs]) & 0x07FF;
            ta |= (RAM[0xc001+goffs] & 0x80) << 4;
            ta |= gfx_bank << 12;

            if (!GFX_BG0_SOLID[ta]) continue;       // No pixels; skip

            map = (RAM[0xc001 + goffs] << 1) & 0x70;
            map |= (gfx_attr & 0x02) << 6;

            x = (sx + xc * 8) & 0xff;
            y = ((sy + yc * 8) & 0xff) -16;

            if ((x > -8) && (y > -8) && (x < 256) && (y < 224))
            {
               if (GFX_BG0_SOLID[ta] == 1)       // Some pixels; trans
               {
                  if (RAM[0xc000+goffs+1] & 0x40)
                     Draw8x8_Trans_FlipY_Rot(&GFX[ta<<6], x+8, y+8, map);
                  else
                     Draw8x8_Trans_Rot(&GFX[ta<<6], x+8, y+8, map);
               } else {                          // all pixels; solid
                  if (RAM[0xc000+goffs+1] & 0x40)
                     Draw8x8_FlipY_Rot(&GFX[ta<<6], x+8, y+8, map);
                  else
                     Draw8x8_Rot(&GFX[ta<<6], x+8, y+8, map);
               }
            }
         }

         gfx_offs += 64;
      }
   }
}

About Koders | Resources | Downloads | Support | Black Duck | Submit Project | Terms of Service | DMCA | Privacy Policy | Site Map| Contact Us