summaryrefslogtreecommitdiffstatshomepage
path: root/src/mame/video/galaxian.c
blob: 279469e4b03ccbde382f1ef048fc6eff89e3a459 (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
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
/***************************************************************************

    Galaxian-derived video hardware

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

    Video timing:

        The master clock is an 18.432MHz crystal. It is divided by 3 by
        a pair of J/K flip-flops to 6.144MHz. This 6MHz signal is used to
        drive most of the video logic. Note that due to the way the
        divide-by-3 circuit is implemented, the duty cycle of the 6MHz
        signal is 66% (i.e., it is high for 2 18MHz clocks and low for 1).
        This is important for accurate stars rendering.


    Horizontal timing:

        H counts from 010000000 (128) to 111111111 (511), giving 384
        total H clocks per scanline

        However, the top bit is inverted to become 256H, so when reading
        the schematics it's really counting from:
            110000000 -> 111111111 (blanking period)
        and then from:
            000000000 -> 011111111 (main portion of screen = 256 pixels)

        HBLANK is a flip-flop clocked by 2H:
          * It is held clear when 256H = 0 (main portion of screen)
          * The D input is connected to !(64H & 32H & 16H & 8H)
          * It is clocked to 1 when H=130
          * It is clocked to 0 when H=250
          * This gives 264 total non-blanked pixels:
              6 additional pixels on the left (H=250..255)
              256 main area pixels (H=256..511)
              2 additional pixels on the right (H=128..129)

        HSYNC is a flip-flop clocked by 16H:
          * It is held clear when 256H = 0 (main portion of screen)
          * The D input is connected to !(!64H & 32H)
          * HSYNC is the /Q output
          * It is clocked to 1 when H=176
          * It is clocked to 0 when H=208


    Vertical timing:

        V counts from 011111000 (248) to 111111111 (511), giving 264
        total V clocks per frame

        IMPORTANT: the V sync chain is clocked by HSYNC. This means
        that for the first 48 H clocks of the blanking period, the
        V counter is one behind. This is important to take into account
        for sprite and missile positioning.

        VBLANK is a flip-flop clocked by 16V:
          * The D input is connected to !(128V & 64V & 32V)
          * It is clocked to 1 when V=496
          * It is clocked to 0 when V=272
          * This gives 224 total non-blanked pixels

        VSYNC is set to !256V:
          * It is set to 1 when V=248
          * It is cleared to 0 when V=256


    Sprites and missiles:

        During the HBLANK period, sprites and missiles are processed.
        Sprites are rendered into the line buffer, which was cleared
        during the visible portion of the previous scanline.

        It takes 8 H clocks to set up a sprite, and 16 to render it
        to the line buffer. The setup clocks are overlapped with the
        rendering clocks. In theory this would result in enough time
        to render 128/16 = 8 sprites. However, the setup does not
        begin until after HBLANK, so there is only enough time to
        render the first 7 1/2 entries.

        Interleaved with the setup for sprites is setup for the
        shell and missile rendering. Shells and missiles are rendered
        realtime during the visible portion of the frame, and are
        effectively color-ORed directly into the final RGB output.
        During the HBLANK setup period, each shell/missile entry is
        compared against the current V position; if an exact match
        is found, the H position is loaded into a dedicated 8-bit
        counter. The counter clocks each pixel during the active video
        period; when it reaches $FC it enables the output until it
        hits zero, at which point it shuts itself off. Because there
        is only one counter for shells and one for missiles, only one
        shell and one missile can be specified per scanline. The last
        matching entry found will always win.

        The difference between shell and missile is that shells
        populate the first 7 entries and are rendered as white,
        whereas missiles populate the final entry and are rendered
        as yellow.

        Here is the detailed sequence of events for sprite and
        missile/shell rendering during the first 24 H clocks of
        HBLANK:

            H=080: HPOSI=objram[40], /VPL latches V+objram[40]
            H=081: HPOSI=objram[40]
            H=082: HPOSI=objram[41], /OBJ DATA L latches picture number, H/V flip
            H=083: HPOSI=objram[41]
            H=084: HPOSI=objram[42], /COL L latches low 3 bits as color
            H=085: HPOSI=objram[42]
            H=086: HPOSI=objram[43]
            H=087: HPOSI=objram[43], /CNTR LD latches X position
            <sprite 0 begins rendering>
            H=088: HPOSI=objram[40], /VPL latches V+objram[40]
            H=089: HPOSI=objram[40]
            H=08A: HPOSI=objram[61]
            H=08B: HPOSI=objram[61], MSLD is latched if Y position matches shell
            H=08C: HPOSI=objram[42]
            H=08D: HPOSI=objram[42]
            H=08E: HPOSI=objram[63]
            H=08F: HPOSI=objram[63], /SLD fires to latch down shell counter value
            H=090: HPOSI=objram[44], /VPL latches V+objram[44]
            H=091: HPOSI=objram[44]
            H=092: HPOSI=objram[45], /OBJ DATA L latches picture number, H/V flip
            H=093: HPOSI=objram[45]
            H=094: HPOSI=objram[46], /COL L latches low 3 bits as color
            H=095: HPOSI=objram[46]
            H=096: HPOSI=objram[47]
            H=097: HPOSI=objram[47], /CNTR LD latches X position
            <sprite 1 begins rendering>

        From this, you can see the object RAM layout looks like:

            objram[40] = vertical position of sprite 0
            objram[41] = picture number and H/V flip of sprite 0
            objram[42] = color of sprite 0
            objram[43] = horizontal position of sprite 0

            objram[61] = vertical position of shell 0
            objram[63] = horizontal count until shell 0 starts rendering

        A vertical match for a sprite is true if ((V + vpos) & 0xf0) == 0xf0.
        A vertical match for a shell/missile is if ((V + vpos) & 0xff) == 0xff.

        Overall, the process for sprites and missiles during HBLANK looks
        like this:

            H=080: begin setup sprite 0
            H=082: begin HBLANK
            H=088: begin render sprite 0; begin setup shell 0
            H=090: begin setup sprite 1
            H=098: begin render sprite 1; begin setup shell 1
            H=0A0: begin setup sprite 2
            H=0A8: begin render sprite 2; begin setup shell 2
            H=0B0: VSYNC increments V counter; subsequent sprites match V+1
            H=0B0: begin setup sprite 3
            H=0B8: begin render sprite 3; begin setup shell 3
            H=0C0: begin setup sprite 4
            H=0C8: begin render sprite 4; begin setup shell 4
            H=0D0: begin setup sprite 5
            H=0D8: begin render sprite 5; begin setup shell 5
            H=0E0: begin setup sprite 6
            H=0E8: begin render sprite 6; begin setup shell 6
            H=0F0: begin setup sprite 7
            H=0F8: begin render sprite 7; begin setup missile
            H=0FA: end HBLANK
            H=100: finish render sprite 7 (only 1/2 way through)



       /VPL: H=xxxxxx000 -> latches sum of V+HPOSI for vertical positioning
     /COL L: H=xxxxxx100 -> latches HPOSI into color register (low 3 bits)
        /LD: H=xxxxxx111 -> shift register load from ROM
  /CNTR CLR: H=0xxxx0111 -> resets line buffer counter to 0
/OBJ DATA L: H=1xxxx0010 -> latches HPOSI into picture number latch
   /CNTR LD: H=1xxxx0111 -> latches HPOSI into line buffer counter (sprite X position)
       /SLD: H=1xxxx1111 -> latches down counter until shell (except when /MLD)
       /MLD: H=1x1111111 -> latches down counter until missile



VRAM addresses:
         addr  video
  VRA7 =  A9   SUM7
  VRA8 =  A8   SUM6
  VRA6 =  A7   SUM5
  VRA5 =  A6   SUM4
  VRA4 =  A5   SUM3
  VRA3 =  A4   128HB
  VRA0 =  A3    64HB
  VRA1 =  A2    32HB
  VRA2 =  A1    16HB
  VRA9 =  A0     8HB

OBJRAM addresses:
         addr 256H=0 256H=1
   RA4 =  A0    4H     2H
   RA7 =  A1    8HB    4H
   RA1 =  A2   16HB   16HB
   RA0 =  A3   32HB   32HB
   RA5 =  A4   64HB   64HB
   RA6 =  A5  128HB  (2H & 8HB)
   RA3 =  A6  256H   256H
   RA2 =  A7    0      0

H=80: 00,00,01,01,02,02,03,03 00,00,21,21,02,02,23,23
H=90: 04,04,05,05,06,06,07,07 04,04,25,25,06,06,27,27
H=A0: 08,08,09,09,0A,0A,0B,0B 08,08,29,29,0A,0A,2B,2B
H=B0: 0C,0C,0D,0D,0E,0E,0F,0F 0C,0C,2D,2D,0E,0E,2F,2F



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

#include "driver.h"
#include "video/resnet.h"
#include "includes/galaxian.h"




/*************************************
 *
 *  Constants
 *
 *************************************/

#define STAR_RNG_PERIOD		((1 << 17) - 1)
#define RGB_MAXIMUM			224



/*************************************
 *
 *  Global variables
 *
 *************************************/

/* rendering callbacks */
galaxian_draw_bullet_func galaxian_draw_bullet_ptr;
galaxian_draw_background_func galaxian_draw_background_ptr;

/* tile/sprite modification callbacks */
galaxian_extend_tile_info_func galaxian_extend_tile_info_ptr;
galaxian_extend_sprite_info_func galaxian_extend_sprite_info_ptr;

/* global tweaks */
UINT8 galaxian_frogger_adjust;
UINT8 galaxian_sfx_tilemap;
UINT8 galaxian_sprite_clip_start;
UINT8 galaxian_sprite_clip_end;



/*************************************
 *
 *  Local variables
 *
 *************************************/

static tilemap *bg_tilemap;

static UINT8 flipscreen_x;
static UINT8 flipscreen_y;

static UINT8 background_enable;
static UINT8 background_red;
static UINT8 background_green;
static UINT8 background_blue;

static UINT32 star_rng_origin;
static UINT32 star_rng_origin_frame;
static rgb_t star_color[64];
static UINT8 *stars;
static UINT8 stars_enabled;
static UINT8 stars_blink_state;

static rgb_t bullet_color[8];

static UINT8 gfxbank[5];



/*************************************
 *
 *  Function prototypes
 *
 *************************************/

static void state_save_register(void);
static TILE_GET_INFO( bg_get_tile_info );

static void sprites_draw(running_machine *machine, bitmap_t *bitmap, const rectangle *cliprect, const UINT8 *spritebase);

static void stars_init(void);
static void stars_update_origin(running_machine *machine);
static void stars_draw_row(bitmap_t *bitmap, int maxx, int y, UINT32 star_offs, UINT8 starmask);

static void bullets_draw(running_machine *machine, bitmap_t *bitmap, const rectangle *cliprect, const UINT8 *base);



/*************************************
 *
 *  Palette setup
 *
 *************************************/

PALETTE_INIT( galaxian )
{
	static const int rgb_resistances[3] = { 1000, 470, 220 };
	double rweights[3], gweights[3], bweights[2];
	int i, minval, midval, maxval;
	UINT8 starmap[4];

	/*
        Sprite/tilemap colors are mapped through a color PROM as follows:

          bit 7 -- 220 ohm resistor  -- BLUE
                -- 470 ohm resistor  -- BLUE
                -- 220 ohm resistor  -- GREEN
                -- 470 ohm resistor  -- GREEN
                -- 1  kohm resistor  -- GREEN
                -- 220 ohm resistor  -- RED
                -- 470 ohm resistor  -- RED
          bit 0 -- 1  kohm resistor  -- RED

        In parallel with these resistors are a pair of 150 ohm and 100 ohm
        resistors on each R,G,B component that are connected to the star
        generator.

        And in parallel with the whole mess are a set of 100 ohm resistors
        on each R,G,B component that are enabled when a shell/missile is
        enabled.

        When computing weights, we use RGB_MAXIMUM as the maximum to give
        headroom for stars and shells/missiles. This is not fully accurate,
        but if we included all possible sources in parallel, the brightness
        of the main game would be very low to allow for all the oversaturation
        of the stars and shells/missiles.
    */
	compute_resistor_weights(0,	RGB_MAXIMUM, -1.0,
			3, &rgb_resistances[0], rweights, 470, 0,
			3, &rgb_resistances[0], gweights, 470, 0,
			2, &rgb_resistances[1], bweights, 470, 0);

	/* decode the palette first */
	for (i = 0; i < memory_region_length(REGION_PROMS); i++)
	{
		UINT8 bit0, bit1, bit2, r, g, b;

		/* red component */
		bit0 = BIT(color_prom[i],0);
		bit1 = BIT(color_prom[i],1);
		bit2 = BIT(color_prom[i],2);
		r = combine_3_weights(rweights, bit0, bit1, bit2);

		/* green component */
		bit0 = BIT(color_prom[i],3);
		bit1 = BIT(color_prom[i],4);
		bit2 = BIT(color_prom[i],5);
		g = combine_3_weights(gweights, bit0, bit1, bit2);

		/* blue component */
		bit0 = BIT(color_prom[i],6);
		bit1 = BIT(color_prom[i],7);
		b = combine_2_weights(bweights, bit0, bit1);

		palette_set_color(machine, i, MAKE_RGB(r,g,b));
	}

	/*
        The maximum sprite/tilemap resistance is ~130 Ohms with all RGB
        outputs enabled (1/(1/1000 + 1/470 + 1/220)). Since we normalized
        to RGB_MAXIMUM, this maps RGB_MAXIMUM -> 130 Ohms.

        The stars are at 150 Ohms for the LSB, and 100 Ohms for the MSB.
        This means the 3 potential values are:

            150 Ohms -> RGB_MAXIMUM * 130 / 150
            100 Ohms -> RGB_MAXIMUM * 130 / 100
             60 Ohms -> RGB_MAXIMUM * 130 / 60

        Since we can't saturate that high, we instead approximate this
        by compressing the values proportionally into the 194->255 range.
    */
	minval = RGB_MAXIMUM * 130 / 150;
	midval = RGB_MAXIMUM * 130 / 100;
	maxval = RGB_MAXIMUM * 130 / 60;

	/* compute the values for each of 4 possible star values */
	starmap[0] = 0;
	starmap[1] = minval;
	starmap[2] = minval + (255 - minval) * (midval - minval) / (maxval - minval);
	starmap[3] = 255;

	/* generate the colors for the stars */
	for (i = 0; i < 64; i++)
	{
		UINT8 bit0, bit1, r, g, b;

		/* bit 5 = red @ 150 Ohm, bit 4 = red @ 100 Ohm */
		bit0 = BIT(i,5);
		bit1 = BIT(i,4);
		r = starmap[(bit1 << 1) | bit0];

		/* bit 3 = green @ 150 Ohm, bit 2 = green @ 100 Ohm */
		bit0 = BIT(i,3);
		bit1 = BIT(i,2);
		g = starmap[(bit1 << 1) | bit0];

		/* bit 1 = blue @ 150 Ohm, bit 0 = blue @ 100 Ohm */
		bit0 = BIT(i,1);
		bit1 = BIT(i,0);
		b = starmap[(bit1 << 1) | bit0];

		/* set the RGB color */
		star_color[i] = MAKE_RGB(r, g, b);
	}

	/* default bullet colors are white for the first 7, and yellow for the last one */
	for (i = 0; i < 7; i++)
		bullet_color[i] = MAKE_RGB(0xff,0xff,0xff);
	bullet_color[7] = MAKE_RGB(0xff,0xff,0x00);
}



/*************************************
 *
 *  Common video init
 *
 *************************************/

VIDEO_START( galaxian )
{
	/* create a tilemap for the background */
	if (!galaxian_sfx_tilemap)
	{
		/* normal galaxian hardware is row-based and individually scrolling columns */
		bg_tilemap = tilemap_create(bg_get_tile_info, tilemap_scan_rows, GALAXIAN_XSCALE*8,8, 32,32);
		tilemap_set_scroll_cols(bg_tilemap, 32);
		tilemap_set_scrolldx(bg_tilemap, 0, -GALAXIAN_XSCALE * 128);
		tilemap_set_scrolldy(bg_tilemap, 0, 8);
	}
	else
	{
		/* sfx hardware is column-based and individually scrolling rows */
		bg_tilemap = tilemap_create(bg_get_tile_info, tilemap_scan_cols, GALAXIAN_XSCALE*8,8, 32,32);
		tilemap_set_scroll_rows(bg_tilemap, 32);
		tilemap_set_scrolldx(bg_tilemap, 0, -GALAXIAN_XSCALE * 128);
		tilemap_set_scrolldy(bg_tilemap, 0, 8);
	}
	tilemap_set_transparent_pen(bg_tilemap, 0);

	/* initialize globals */
	flipscreen_x = 0;
	flipscreen_y = 0;
	background_enable = 0;
	background_blue = 0;
	background_red = 0;
	background_green = 0;

	/* initialize stars */
	stars_init();

	/* register for save states */
	state_save_register();
}


static void state_save_register(void)
{
	state_save_register_global(flipscreen_x);
	state_save_register_global(flipscreen_y);
	state_save_register_global(background_enable);
	state_save_register_global(background_red);
	state_save_register_global(background_green);
	state_save_register_global(background_blue);

	state_save_register_global_array(gfxbank);

	state_save_register_global(stars_enabled);
	state_save_register_global(star_rng_origin);
	state_save_register_global(star_rng_origin_frame);
	state_save_register_global(stars_blink_state);
}



/*************************************
 *
 *  Common video update
 *
 *************************************/

VIDEO_UPDATE( galaxian )
{
	/* draw the background layer (including stars) */
	(*galaxian_draw_background_ptr)(screen->machine, bitmap, cliprect);

	/* draw the tilemap characters over top */
	tilemap_draw(bitmap, cliprect, bg_tilemap, 0, 0);

	/* render the sprites next */
	sprites_draw(screen->machine, bitmap, cliprect, &spriteram[0x40]);

	/* if we have bullets to draw, render them following */
	if (galaxian_draw_bullet_ptr != NULL)
		bullets_draw(screen->machine, bitmap, cliprect, &spriteram[0x60]);

	return 0;
}



/*************************************
 *
 *  Background tilemap
 *
 *************************************/

static TILE_GET_INFO( bg_get_tile_info )
{
	UINT8 x = tile_index & 0x1f;

	UINT16 code = videoram[tile_index];
	UINT8 attrib = spriteram[x*2+1];
	UINT8 color = attrib & 7;

	if (galaxian_extend_tile_info_ptr != NULL)
		(*galaxian_extend_tile_info_ptr)(&code, &color, attrib, x);

	SET_TILE_INFO(0, code, color, 0);
}


WRITE8_HANDLER( galaxian_videoram_w )
{
	/* update any video up to the current scanline */
	video_screen_update_now(machine->primary_screen);

	/* store the data and mark the corresponding tile dirty */
	videoram[offset] = data;
	tilemap_mark_tile_dirty(bg_tilemap, offset);
}


WRITE8_HANDLER( galaxian_objram_w )
{
	/* update any video up to the current scanline */
	video_screen_update_now(machine->primary_screen);

	/* store the data */
	spriteram[offset] = data;

	/* the first $40 bytes affect the tilemap */
	if (offset < 0x40)
	{
		/* even entries control the scroll position */
		if ((offset & 0x01) == 0)
		{
			/* Frogger: top and bottom 4 bits swapped entering the adder */
			if (galaxian_frogger_adjust)
				data = (data >> 4) | (data << 4);
			if (!galaxian_sfx_tilemap)
				tilemap_set_scrolly(bg_tilemap, offset >> 1, data);
			else
				tilemap_set_scrollx(bg_tilemap, offset >> 1, GALAXIAN_XSCALE*data);
		}

		/* odd entries control the color base for the row */
		else
		{
			for (offset >>= 1; offset < 0x0400; offset += 32)
				tilemap_mark_tile_dirty(bg_tilemap, offset);
		}
	}
}



/*************************************
 *
 *  Sprite rendering
 *
 *************************************/

static void sprites_draw(running_machine *machine, bitmap_t *bitmap, const rectangle *cliprect, const UINT8 *spritebase)
{
	rectangle clip = *cliprect;
	int sprnum;

	/* 16 of the 256 pixels of the sprites are hard-clipped at the line buffer */
	/* according to the schematics, it should be the first 16 pixels; however, */
	/* some bootlegs demonstrate that this can be shifted to other positions. */
	clip.min_x = MAX(clip.min_x, galaxian_sprite_clip_start * GALAXIAN_XSCALE);
	clip.max_x = MIN(clip.max_x, (galaxian_sprite_clip_end + 1) * GALAXIAN_XSCALE - 1);

	/* The line buffer is only written if it contains a '0' currently; */
	/* it is cleared during the visible area, and populated during HBLANK */
	/* To simulate this, we render backwards so that lower numbered sprites */
	/* have priority over higher numbered sprites. */
	for (sprnum = 7; sprnum >= 0; sprnum--)
	{
		const UINT8 *base = &spritebase[sprnum * 4];
		/* Frogger: top and bottom 4 bits swapped entering the adder */
		UINT8 base0 = galaxian_frogger_adjust ? ((base[0] >> 4) | (base[0] << 4)) : base[0];
		/* the first three sprites match against y-1 */
		UINT8 sy = 240 - (base0 - (sprnum < 3));
		UINT16 code = base[1] & 0x3f;
		UINT8 flipx = base[1] & 0x40;
		UINT8 flipy = base[1] & 0x80;
		UINT8 color = base[2] & 7;
		UINT8 sx = base[3];

		/* extend the sprite information */
		if (galaxian_extend_sprite_info_ptr != NULL)
			(*galaxian_extend_sprite_info_ptr)(base, &sx, &sy, &flipx, &flipy, &code, &color);

		/* apply flipscreen in X direction */
		if (flipscreen_x)
		{
			sx = 240 - sx;
			flipx = !flipx;
		}

		/* apply flipscreen in Y direction */
		if (flipscreen_y)
		{
			sy = 240 - sy;
			flipy = !flipy;
		}

		/* draw */
		drawgfx(bitmap, machine->gfx[1],
				code, color,
				flipx, flipy,
				GALAXIAN_H0START + GALAXIAN_XSCALE * sx, sy,
				&clip,
				TRANSPARENCY_PEN, 0);
	}
}



/*************************************
 *
 *  Bullets rendering
 *
 *************************************/

static void bullets_draw(running_machine *machine, bitmap_t *bitmap, const rectangle *cliprect, const UINT8 *base)
{
	int y;

	/* iterate over scanlines */
	for (y = cliprect->min_y; y <= cliprect->max_y; y++)
	{
		UINT8 shell = 0xff, missile = 0xff;
		UINT8 effy;
		int which;

		/* the first 3 entries match Y-1 */
		effy = flipscreen_y ? ((y - 1) ^ 255) : (y - 1);
		for (which = 0; which < 3; which++)
			if ((UINT8)(base[which*4+1] + effy) == 0xff)
				shell = which;

		/* remaining entries match Y */
		effy = flipscreen_y ? (y ^ 255) : y;
		for (which = 3; which < 8; which++)
			if ((UINT8)(base[which*4+1] + effy) == 0xff)
			{
				if (which != 7)
					shell = which;
				else
					missile = which;
			}

		/* draw the shell */
		if (shell != 0xff)
			(*galaxian_draw_bullet_ptr)(machine, bitmap, cliprect, shell, 255 - base[shell*4+3], y);
		if (missile != 0xff)
			(*galaxian_draw_bullet_ptr)(machine, bitmap, cliprect, missile, 255 - base[missile*4+3], y);
	}
}



/*************************************
 *
 *  Screen orientation
 *
 *************************************/

WRITE8_HANDLER( galaxian_flip_screen_x_w )
{
	if (flipscreen_x != (data & 0x01))
	{
		video_screen_update_now(machine->primary_screen);

		/* when the direction changes, we count a different number of clocks */
		/* per frame, so we need to reset the origin of the stars to the current */
		/* frame before we flip */
		stars_update_origin(machine);

		flipscreen_x = data & 0x01;
		tilemap_set_flip(bg_tilemap, (flipscreen_x ? TILEMAP_FLIPX : 0) | (flipscreen_y ? TILEMAP_FLIPY : 0));
	}
}

WRITE8_HANDLER( galaxian_flip_screen_y_w )
{
	if (flipscreen_y != (data & 0x01))
	{
		video_screen_update_now(machine->primary_screen);
		flipscreen_y = data & 0x01;
		tilemap_set_flip(bg_tilemap, (flipscreen_x ? TILEMAP_FLIPX : 0) | (flipscreen_y ? TILEMAP_FLIPY : 0));
	}
}

WRITE8_HANDLER( galaxian_flip_screen_xy_w )
{
	galaxian_flip_screen_x_w(machine, offset, data);
	galaxian_flip_screen_y_w(machine, offset, data);
}



/*************************************
 *
 *  Background controls
 *
 *************************************/

WRITE8_HANDLER( galaxian_stars_enable_w )
{
	if ((stars_enabled ^ data) & 0x01)
	video_screen_update_now(machine->primary_screen);
	if (!stars_enabled && (data & 0x01))
	{
		/* on the rising edge of this, the CLR on the shift registers is released */
		/* this resets the "origin" of this frame to 0 minus the number of clocks */
		/* we have counted so far */
		star_rng_origin = STAR_RNG_PERIOD - (video_screen_get_vpos(machine->primary_screen) * 512 + video_screen_get_hpos(machine->primary_screen));
		star_rng_origin_frame = video_screen_get_frame_number(machine->primary_screen);
	}
	stars_enabled = data & 0x01;
}


WRITE8_HANDLER( scramble_background_enable_w )
{
	video_screen_update_now(machine->primary_screen);
	background_enable = data & 0x01;
}


WRITE8_HANDLER( scramble_background_red_w )
{
	video_screen_update_now(machine->primary_screen);
	background_red = data & 0x01;
}


WRITE8_HANDLER( scramble_background_green_w )
{
	video_screen_update_now(machine->primary_screen);
	background_green = data & 0x01;
}


WRITE8_HANDLER( scramble_background_blue_w )
{
	video_screen_update_now(machine->primary_screen);
	background_blue = data & 0x01;
}



/*************************************
 *
 *  Graphics banking
 *
 *************************************/

WRITE8_HANDLER( galaxian_gfxbank_w )
{
	if (gfxbank[offset] != data)
	{
		video_screen_update_now(machine->primary_screen);
		gfxbank[offset] = data;
		tilemap_mark_all_tiles_dirty(bg_tilemap);
	}
}



/*************************************
 *
 *  Star initialization
 *
 *************************************/

static void stars_init(void)
{
	UINT32 shiftreg;
	int i;

	/* reset the blink and enabled states */
	stars_enabled = FALSE;
	stars_blink_state = 0;

	/* precalculate the RNG */
	stars = auto_malloc(STAR_RNG_PERIOD);
	shiftreg = 0;
	for (i = 0; i < STAR_RNG_PERIOD; i++)
	{
		/* stars are enabled if the upper 8 bits are 1 and the low bit is 0 */
		int enabled = ((shiftreg & 0x1fe01) == 0x1fe00);

		/* color comes from the 6 bits below the top 8 bits */
		int color = (~shiftreg & 0x1f8) >> 3;

		/* store the color value in the low 6 bits and the enable in the upper bit */
		stars[i] = color | (enabled << 7);

		/* the LFSR is fed based on the XOR of bit 12 and the inverse of bit 0 */
		shiftreg = (shiftreg >> 1) | ((((shiftreg >> 12) ^ ~shiftreg) & 1) << 16);
	}
}



/*************************************
 *
 *  Adjust the origin of stars
 *
 *************************************/

static void stars_update_origin(running_machine *machine)
{
	int curframe = video_screen_get_frame_number(machine->primary_screen);

	/* only update on a different frame */
	if (curframe != star_rng_origin_frame)
	{
		/* The RNG period is 2^17-1; each frame, the shift register is clocked */
		/* 512*256 = 2^17 times. This means that we clock one extra time each */
		/* frame. However, if we are NOT flipped, there is a pair of D flip-flops */
		/* at 6B which delay the count so that we count 512*256-2 = 2^17-2 times. */
		/* In this case, we only one time less than the period each frame. Both */
		/* of these off-by-one countings produce the horizontal star scrolling. */
		int per_frame_delta = flipscreen_x ? 1 : -1;
		int total_delta = per_frame_delta * (curframe - star_rng_origin_frame);

		/* we can't just use % here because mod of a negative number is undefined */
		while (total_delta < 0)
			total_delta += STAR_RNG_PERIOD;

		/* now that everything is positive, do the mod */
		star_rng_origin = (star_rng_origin + total_delta) % STAR_RNG_PERIOD;
		star_rng_origin_frame = curframe;
	}
}



/*************************************
 *
 *  Star blinking
 *
 *************************************/

TIMER_CALLBACK( galaxian_stars_blink_timer )
{
	stars_blink_state++;
}



/*************************************
 *
 *  Draw a row of stars
 *
 *************************************/

static void stars_draw_row(bitmap_t *bitmap, int maxx, int y, UINT32 star_offs, UINT8 starmask)
{
	int x;

	/* ensure our star offset is valid */
	star_offs %= STAR_RNG_PERIOD;

	/* iterate over the specified number of 6MHz pixels */
	for (x = 0; x < maxx; x++)
	{
		/* stars are suppressed unless V1 ^ H8 == 1 */
		int enable_star = (y ^ (x >> 3)) & 1;
		UINT8 star;

		/*
            The RNG clock is the master clock (18MHz) ANDed with the pixel clock (6MHz).
            The divide-by-3 circuit that produces the pixel clock generates a square wave
            with a 2/3 duty cycle, so the result of the AND generates a clock like this:
                        _   _   _   _   _   _   _   _
              MASTER: _| |_| |_| |_| |_| |_| |_| |_| |
                        _______     _______     ______
              PIXEL:  _|       |___|       |___|
                        _   _       _   _       _   _
              RNG:    _| |_| |_____| |_| |_____| |_| |

            Thus for each pixel, there are 3 master clocks and 2 RNG clocks, and the RNG
            is clocked asymmetrically. To simulate this, we expand the horizontal screen
            size by 3 and handle the first RNG clock with one pixel and the second RNG
            clock with two pixels.
        */

		/* first RNG clock: one pixel */
		star = stars[star_offs++];
		if (star_offs >= STAR_RNG_PERIOD)
			star_offs = 0;
		if (enable_star && (star & 0x80) != 0 && (star & starmask) != 0)
			*BITMAP_ADDR32(bitmap, y, GALAXIAN_XSCALE*x + 0) = star_color[star & 0x3f];

		/* second RNG clock: two pixels */
		star = stars[star_offs++];
		if (star_offs >= STAR_RNG_PERIOD)
			star_offs = 0;
		if (enable_star && (star & 0x80) != 0 && (star & starmask) != 0)
		{
			*BITMAP_ADDR32(bitmap, y, GALAXIAN_XSCALE*x + 1) = star_color[star & 0x3f];
			*BITMAP_ADDR32(bitmap, y, GALAXIAN_XSCALE*x + 2) = star_color[star & 0x3f];
		}
	}
}



/*************************************
 *
 *  Background rendering
 *
 *************************************/

static int flip_and_clip(rectangle *draw, int xstart, int xend, const rectangle *cliprect)
{
	*draw = *cliprect;
	if (!flipscreen_x)
	{
		draw->min_x = xstart * GALAXIAN_XSCALE;
		draw->max_x = xend * GALAXIAN_XSCALE + (GALAXIAN_XSCALE - 1);
	}
	else
	{
		draw->min_x = (xend ^ 255) * GALAXIAN_XSCALE;
		draw->max_x = (xstart ^ 255) * GALAXIAN_XSCALE + (GALAXIAN_XSCALE - 1);
	}
	sect_rect(draw, cliprect);
	return (draw->min_x <= draw->max_x);
}


void galaxian_draw_background(running_machine *machine, bitmap_t *bitmap, const rectangle *cliprect)
{
	/* erase the background to black first */
	fillbitmap(bitmap, RGB_BLACK, cliprect);

	/* update the star origin to the current frame */
	stars_update_origin(machine);

	/* render stars if enabled */
	if (stars_enabled)
	{
		int y;

		/* iterate over scanlines */
		for (y = cliprect->min_y; y <= cliprect->max_y; y++)
		{
			UINT32 star_offs = star_rng_origin + y * 512;
			stars_draw_row(bitmap, 256, y, star_offs, 0xff);
		}
	}
}


void frogger_draw_background(running_machine *machine, bitmap_t *bitmap, const rectangle *cliprect)
{
	rectangle draw;

	/* color split point verified on real machine */
	/* hmmm, according to schematics it is at 128+8; which is right? */
	draw = *cliprect;
	draw.max_x = MIN(draw.max_x, (128+8) * GALAXIAN_XSCALE - 1);
	if (draw.min_x <= draw.max_x)
		fillbitmap(bitmap, MAKE_RGB(0,0,0x47), &draw);

	draw = *cliprect;
	draw.min_x = MAX(draw.min_x, (128+8) * GALAXIAN_XSCALE);
	if (draw.min_x <= draw.max_x)
		fillbitmap(bitmap, RGB_BLACK, &draw);
}


void amidar_draw_background(running_machine *machine, bitmap_t *bitmap, const rectangle *cliprect)
{
	const UINT8 *prom = memory_region(REGION_USER1);
	rectangle draw;
	int x;

	for (x = 0; x < 32; x++)
		if (flip_and_clip(&draw, x * 8, x * 8 + 7, cliprect))
		{
			/*
                The background PROM is connected the following way:

                   bit 0 = 0 enables the blue gun if BCB is asserted
                   bit 1 = 0 enables the red gun if BCR is asserted and
                             the green gun if BCG is asserted
                   bits 2-7 are unconnected

                The background color generator is connected this way:

                    RED   - 270 ohm resistor
                    GREEN - 560 ohm resistor
                    BLUE  - 470 ohm resistor
            */
			UINT8 red = ((~prom[x] & 0x02) && background_red) ? 0x7c : 0x00;
			UINT8 green = ((~prom[x] & 0x02) && background_green) ? 0x3c : 0x00;
			UINT8 blue = ((~prom[x] & 0x01) && background_blue) ? 0x47 : 0x00;
			fillbitmap(bitmap, MAKE_RGB(red, green, blue), &draw);
		}
}


void turtles_draw_background(running_machine *machine, bitmap_t *bitmap, const rectangle *cliprect)
{
	/*
        The background color generator is connected this way:

            RED   - 390 ohm resistor
            GREEN - 470 ohm resistor
            BLUE  - 390 ohm resistor
    */
	fillbitmap(bitmap, MAKE_RGB(background_red * 0x55, background_green * 0x47, background_blue * 0x55), cliprect);
}


void scramble_draw_background(running_machine *machine, bitmap_t *bitmap, const rectangle *cliprect)
{
	/* blue background - 390 ohm resistor */
	fillbitmap(bitmap, background_enable ? MAKE_RGB(0,0,0x56) : RGB_BLACK, cliprect);

	/* update the star origin to the current frame */
	stars_update_origin(machine);

	/* render stars if enabled */
	if (stars_enabled)
	{
		int blink_state = stars_blink_state & 3;
		int y;

		/* iterate over scanlines */
		for (y = cliprect->min_y; y <= cliprect->max_y; y++)
		{
			/* blink state 2 suppressed stars when 2V == 0 */
			if (blink_state != 2 || (y & 2) != 0)
			{
				/* blink states 0 and 1 suppress stars when certain bits of the color == 0 */
				static const UINT8 colormask_table[4] = { 0x20, 0x08, 0xff, 0xff };
				stars_draw_row(bitmap, 256, y, y * 512, colormask_table[blink_state]);
			}
		}
	}
}


void jumpbug_draw_background(running_machine *machine, bitmap_t *bitmap, const rectangle *cliprect)
{
	/* blue background - 390 ohm resistor */
	fillbitmap(bitmap, background_enable ? MAKE_RGB(0,0,0x56) : RGB_BLACK, cliprect);

	/* update the star origin to the current frame */
	stars_update_origin(machine);

	/* render stars if enabled -- same as scramble but nothing in the status area */
	if (stars_enabled)
	{
		int blink_state = stars_blink_state & 3;
		int y;

		/* iterate over scanlines */
		for (y = cliprect->min_y; y <= cliprect->max_y; y++)
		{
			/* blink state 2 suppressed stars when 2V == 0 */
			if (blink_state != 2 || (y & 2) != 0)
			{
				/* blink states 0 and 1 suppress stars when certain bits of the color == 0 */
				static const UINT8 colormask_table[4] = { 0x20, 0x08, 0xff, 0xff };
				stars_draw_row(bitmap, 240, y, y * 512, colormask_table[blink_state]);
			}
		}
	}
}



/*************************************
 *
 *  Bullet rendering
 *
 *************************************/

INLINE void galaxian_draw_pixel(bitmap_t *bitmap, const rectangle *cliprect, int y, int x, rgb_t color)
{
	if (y >= cliprect->min_y && y <= cliprect->max_y)
	{
		x *= GALAXIAN_XSCALE;
		x += GALAXIAN_H0START;
		if (x >= cliprect->min_x && x <= cliprect->max_x)
			*BITMAP_ADDR32(bitmap, y, x) = color;

		x++;
		if (x >= cliprect->min_x && x <= cliprect->max_x)
			*BITMAP_ADDR32(bitmap, y, x) = color;

		x++;
		if (x >= cliprect->min_x && x <= cliprect->max_x)
			*BITMAP_ADDR32(bitmap, y, x) = color;
	}
}


void galaxian_draw_bullet(running_machine *machine, bitmap_t *bitmap, const rectangle *cliprect, int offs, int x, int y)
{
	/*
        Both "shells" and "missiles" begin displaying when the horizontal counter
        reaches $FC, and they stop displaying when it reaches $00, resulting in
        4-pixel-long shots. The first 7 entries are called "shells" and render as
        white; the final entry is called a "missile" and renders as yellow.
    */
	x -= 4;
	galaxian_draw_pixel(bitmap, cliprect, y, x++, bullet_color[offs]);
	galaxian_draw_pixel(bitmap, cliprect, y, x++, bullet_color[offs]);
	galaxian_draw_pixel(bitmap, cliprect, y, x++, bullet_color[offs]);
	galaxian_draw_pixel(bitmap, cliprect, y, x++, bullet_color[offs]);
}


void mshuttle_draw_bullet(running_machine *machine, bitmap_t *bitmap, const rectangle *cliprect, int offs, int x, int y)
{
	/* verified by schematics:
        * both "W" and "Y" bullets are 4 pixels long
        * "W" bullets are enabled when H6 == 0, and are always purple
        * "Y" bullets are enabled when H6 == 1, and vary in color based on H4,H3,H2
    */
	static const rgb_t colors[8] =
	{
		MAKE_RGB(0xff,0xff,0xff),
		MAKE_RGB(0xff,0xff,0x00),
		MAKE_RGB(0x00,0xff,0xff),
		MAKE_RGB(0x00,0xff,0x00),
		MAKE_RGB(0xff,0x00,0xff),
		MAKE_RGB(0xff,0x00,0x00),
		MAKE_RGB(0x00,0x00,0xff),
		MAKE_RGB(0x00,0x00,0x00)
	};
	--x;
	galaxian_draw_pixel(bitmap, cliprect, y, x, ((x & 0x40) == 0) ? colors[(x >> 2) & 7] : MAKE_RGB(0xff,0x00,0xff));
	--x;
	galaxian_draw_pixel(bitmap, cliprect, y, x, ((x & 0x40) == 0) ? colors[(x >> 2) & 7] : MAKE_RGB(0xff,0x00,0xff));
	--x;
	galaxian_draw_pixel(bitmap, cliprect, y, x, ((x & 0x40) == 0) ? colors[(x >> 2) & 7] : MAKE_RGB(0xff,0x00,0xff));
	--x;
	galaxian_draw_pixel(bitmap, cliprect, y, x, ((x & 0x40) == 0) ? colors[(x >> 2) & 7] : MAKE_RGB(0xff,0x00,0xff));
}


void scramble_draw_bullet(running_machine *machine, bitmap_t *bitmap, const rectangle *cliprect, int offs, int x, int y)
{
	/*
        Scramble only has "shells", which begin displaying when the counter
        reaches $FA, and stop displaying one pixel clock layer. All shells are
        rendered as yellow.
    */
	x -= 6;
	galaxian_draw_pixel(bitmap, cliprect, y, x, MAKE_RGB(0xff,0xff,0x00));
}


void theend_draw_bullet(running_machine *machine, bitmap_t *bitmap, const rectangle *cliprect, int offs, int x, int y)
{
	/* Same as galaxian except blue/green are swapped */
	x -= 4;
	galaxian_draw_pixel(bitmap, cliprect, y, x++, MAKE_RGB(RGB_RED(bullet_color[offs]), RGB_BLUE(bullet_color[offs]), RGB_GREEN(bullet_color[offs])));
	galaxian_draw_pixel(bitmap, cliprect, y, x++, MAKE_RGB(RGB_RED(bullet_color[offs]), RGB_BLUE(bullet_color[offs]), RGB_GREEN(bullet_color[offs])));
	galaxian_draw_pixel(bitmap, cliprect, y, x++, MAKE_RGB(RGB_RED(bullet_color[offs]), RGB_BLUE(bullet_color[offs]), RGB_GREEN(bullet_color[offs])));
	galaxian_draw_pixel(bitmap, cliprect, y, x++, MAKE_RGB(RGB_RED(bullet_color[offs]), RGB_BLUE(bullet_color[offs]), RGB_GREEN(bullet_color[offs])));
}



/*************************************
 *
 *  Generic extensions
 *
 *************************************/

void upper_extend_tile_info(UINT16 *code, UINT8 *color, UINT8 attrib, UINT8 x)
{
	/* tiles are in the upper half of a larger ROM */
	*code += 0x100;
}


void upper_extend_sprite_info(const UINT8 *base, UINT8 *sx, UINT8 *sy, UINT8 *flipx, UINT8 *flipy, UINT16 *code, UINT8 *color)
{
	/* sprites are in the upper half of a larger ROM */
	*code += 0x40;
}



/*************************************
 *
 *  Frogger extensions
 *
 *************************************/

void frogger_extend_tile_info(UINT16 *code, UINT8 *color, UINT8 attrib, UINT8 x)
{
	*color = ((*color >> 1) & 0x03) | ((*color << 2) & 0x04);
}

void frogger_extend_sprite_info(const UINT8 *base, UINT8 *sx, UINT8 *sy, UINT8 *flipx, UINT8 *flipy, UINT16 *code, UINT8 *color)
{
	*color = ((*color >> 1) & 0x03) | ((*color << 2) & 0x04);
}



/*************************************
 *
 *  Ghostmuncher Galaxian extensions
 *
 *************************************/

void gmgalax_extend_tile_info(UINT16 *code, UINT8 *color, UINT8 attrib, UINT8 x)
{
	*code |= gfxbank[0] << 9;
//  *color |= gfxbank[0] << 3;
}

void gmgalax_extend_sprite_info(const UINT8 *base, UINT8 *sx, UINT8 *sy, UINT8 *flipx, UINT8 *flipy, UINT16 *code, UINT8 *color)
{
	*code |= (gfxbank[0] << 7) | 0x40;
	*color |= gfxbank[0] << 3;
}



/*************************************
 *
 *  Pisces extensions
 *
 *************************************/

void pisces_extend_tile_info(UINT16 *code, UINT8 *color, UINT8 attrib, UINT8 x)
{
	*code |= gfxbank[0] << 8;
}

void pisces_extend_sprite_info(const UINT8 *base, UINT8 *sx, UINT8 *sy, UINT8 *flipx, UINT8 *flipy, UINT16 *code, UINT8 *color)
{
	*code |= gfxbank[0] << 6;
}



/*************************************
 *
 *  Batman Part 2 extensions
 *
 *************************************/

void batman2_extend_tile_info(UINT16 *code, UINT8 *color, UINT8 attrib, UINT8 x)
{
	if (*code & 0x80)
		*code |= gfxbank[0] << 8;
}



/*************************************
 *
 *  Moon Cresta extensions
 *
 *************************************/

void mooncrst_extend_tile_info(UINT16 *code, UINT8 *color, UINT8 attrib, UINT8 x)
{
	if (gfxbank[2] && (*code & 0xc0) == 0x80)
		*code = (*code & 0x3f) | (gfxbank[0] << 6) | (gfxbank[1] << 7) | 0x0100;
}

void mooncrst_extend_sprite_info(const UINT8 *base, UINT8 *sx, UINT8 *sy, UINT8 *flipx, UINT8 *flipy, UINT16 *code, UINT8 *color)
{
	if (gfxbank[2] && (*code & 0x30) == 0x20)
		*code = (*code & 0x0f) | (gfxbank[1] << 4) | (gfxbank[0] << 5) | 0x40;
}



/*************************************
 *
 *  Moon Quasar extensions
 *
 *************************************/

void moonqsr_extend_tile_info(UINT16 *code, UINT8 *color, UINT8 attrib, UINT8 x)
{
	*code |= (attrib & 0x20) << 3;
}

void moonqsr_extend_sprite_info(const UINT8 *base, UINT8 *sx, UINT8 *sy, UINT8 *flipx, UINT8 *flipy, UINT16 *code, UINT8 *color)
{
	*code |= (base[2] & 0x20) << 1;
}



/*************************************
 *
 *  Moon Shuttle extensions
 *
 *************************************/

void mshuttle_extend_tile_info(UINT16 *code, UINT8 *color, UINT8 attrib, UINT8 x)
{
	*code |= (attrib & 0x30) << 4;
}

void mshuttle_extend_sprite_info(const UINT8 *base, UINT8 *sx, UINT8 *sy, UINT8 *flipx, UINT8 *flipy, UINT16 *code, UINT8 *color)
{
	*code |= (base[2] & 0x30) << 2;
}



/*************************************
 *
 *  Jumpbug extensions
 *
 *************************************/

void jumpbug_extend_tile_info(UINT16 *code, UINT8 *color, UINT8 attrib, UINT8 x)
{
	if ((*code & 0xc0) == 0x80 && (gfxbank[2] & 0x01))
		*code += 128 + (( gfxbank[0] & 0x01) << 6) +
					   (( gfxbank[1] & 0x01) << 7) +
					   ((~gfxbank[4] & 0x01) << 8);
}

void jumpbug_extend_sprite_info(const UINT8 *base, UINT8 *sx, UINT8 *sy, UINT8 *flipx, UINT8 *flipy, UINT16 *code, UINT8 *color)
{
	if ((*code & 0x30) == 0x20 && (gfxbank[2] & 0x01) != 0)
	{
		*code += 32 + (( gfxbank[0] & 0x01) << 4) +
					  (( gfxbank[1] & 0x01) << 5) +
					  ((~gfxbank[4] & 0x01) << 6);
	}
}