summaryrefslogtreecommitdiffstatshomepage
path: root/src/mame/video/tmnt.c
blob: af381a939fb7830b3111dadec985e0ead2542456 (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
#include "emu.h"
#include "video/konicdev.h"
#include "includes/tmnt.h"

static TILE_GET_INFO( glfgreat_get_roz_tile_info )
{
	tmnt_state *state = machine.driver_data<tmnt_state>();
	UINT8 *rom = machine.region("user1")->base();
	int code;

	tile_index += 0x40000 * state->m_glfgreat_roz_rom_bank;

	code = rom[tile_index + 0x80000] + 256 * rom[tile_index] + 256 * 256 * ((rom[tile_index / 4 + 0x100000] >> (2 * (tile_index & 3))) & 3);

	SET_TILE_INFO(0, code & 0x3fff, code >> 14, 0);
}

static TILE_GET_INFO( prmrsocr_get_roz_tile_info )
{
	UINT8 *rom = machine.region("user1")->base();
	int code = rom[tile_index + 0x20000] + 256 * rom[tile_index];

	SET_TILE_INFO(0, code & 0x1fff, code >> 13, 0);
}



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

  Callbacks for the K052109

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

/* Missing in Action */

void mia_tile_callback( running_machine &machine, int layer, int bank, int *code, int *color, int *flags, int *priority )
{
	tmnt_state *state = machine.driver_data<tmnt_state>();
	*flags = (*color & 0x04) ? TILE_FLIPX : 0;
	if (layer == 0)
	{
		*code |= ((*color & 0x01) << 8);
		*color = state->m_layer_colorbase[layer] + ((*color & 0x80) >> 5) + ((*color & 0x10) >> 1);
	}
	else
	{
		*code |= ((*color & 0x01) << 8) | ((*color & 0x18) << 6) | (bank << 11);
		*color = state->m_layer_colorbase[layer] + ((*color & 0xe0) >> 5);
	}
}

void cuebrick_tile_callback( running_machine &machine, int layer, int bank, int *code, int *color, int *flags, int *priority )
{
	tmnt_state *state = machine.driver_data<tmnt_state>();

	if ((k052109_get_rmrd_line(state->m_k052109) == CLEAR_LINE) && (layer == 0))
	{
		*code |= ((*color & 0x01) << 8);
		*color = state->m_layer_colorbase[layer]  + ((*color & 0x80) >> 5) + ((*color & 0x10) >> 1);
	}
	else
	{
		*code |= ((*color & 0xf) << 8);
		*color = state->m_layer_colorbase[layer] + ((*color & 0xe0) >> 5);
	}
}

void tmnt_tile_callback( running_machine &machine, int layer, int bank, int *code, int *color, int *flags, int *priority )
{
	tmnt_state *state = machine.driver_data<tmnt_state>();
	*code |= ((*color & 0x03) << 8) | ((*color & 0x10) << 6) | ((*color & 0x0c) << 9) | (bank << 13);
	*color = state->m_layer_colorbase[layer] + ((*color & 0xe0) >> 5);
}

void ssbl_tile_callback( running_machine &machine, int layer, int bank, int *code, int *color, int *flags, int *priority )
{
	tmnt_state *state = machine.driver_data<tmnt_state>();
	if (layer == 0)
	{
		*code |= ((*color & 0x03) << 8) | ((*color & 0x10) << 6) | ((*color & 0x0c) << 9) | (bank << 13);
	}
	else
	{
		*code |= ((*color & 0x03) << 8) | ((*color & 0x10) << 6) | ((*color & 0x0c) << 9) | (bank << 13);
//      mame_printf_debug("L%d: bank %d code %x color %x\n", layer, bank, *code, *color);
	}

	*color = state->m_layer_colorbase[layer] + ((*color & 0xe0) >> 5);
}

void blswhstl_tile_callback( running_machine &machine, int layer, int bank, int *code, int *color, int *flags, int *priority )
{
	tmnt_state *state = machine.driver_data<tmnt_state>();

	/* (color & 0x02) is flip y handled internally by the 052109 */
	*code |= ((*color & 0x01) << 8) | ((*color & 0x10) << 5) | ((*color & 0x0c) << 8) | (bank << 12) | state->m_blswhstl_rombank << 14;
	*color = state->m_layer_colorbase[layer] + ((*color & 0xe0) >> 5);
}



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

  Callbacks for the K051960

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

void mia_sprite_callback( running_machine &machine, int *code, int *color, int *priority, int *shadow )
{
	tmnt_state *state = machine.driver_data<tmnt_state>();
	*color = state->m_sprite_colorbase + (*color & 0x0f);
}

void tmnt_sprite_callback( running_machine &machine, int *code, int *color, int *priority, int *shadow )
{
	tmnt_state *state = machine.driver_data<tmnt_state>();
	*code |= (*color & 0x10) << 9;
	*color = state->m_sprite_colorbase + (*color & 0x0f);
}

void punkshot_sprite_callback( running_machine &machine, int *code, int *color, int *priority_mask, int *shadow )
{
	tmnt_state *state = machine.driver_data<tmnt_state>();
	int pri = 0x20 | ((*color & 0x60) >> 2);
	if (pri <= state->m_layerpri[2])
		*priority_mask = 0;
	else if (pri > state->m_layerpri[2] && pri <= state->m_layerpri[1])
		*priority_mask = 0xf0;
	else if (pri > state->m_layerpri[1] && pri <= state->m_layerpri[0])
		*priority_mask = 0xf0 | 0xcc;
	else
		*priority_mask = 0xf0 | 0xcc | 0xaa;

	*code |= (*color & 0x10) << 9;
	*color = state->m_sprite_colorbase + (*color & 0x0f);
}

void thndrx2_sprite_callback( running_machine &machine, int *code, int *color, int *priority_mask, int *shadow )
{
	tmnt_state *state = machine.driver_data<tmnt_state>();
	int pri = 0x20 | ((*color & 0x60) >> 2);
	if (pri <= state->m_layerpri[2])
		*priority_mask = 0;
	else if (pri > state->m_layerpri[2] && pri <= state->m_layerpri[1])
		*priority_mask = 0xf0;
	else if (pri > state->m_layerpri[1] && pri <= state->m_layerpri[0])
		*priority_mask = 0xf0 | 0xcc;
	else
		*priority_mask = 0xf0 | 0xcc | 0xaa;

	*color = state->m_sprite_colorbase + (*color & 0x0f);
}


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

  Callbacks for the K053245

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

void lgtnfght_sprite_callback( running_machine &machine, int *code, int *color, int *priority_mask )
{
	tmnt_state *state = machine.driver_data<tmnt_state>();
	int pri = 0x20 | ((*color & 0x60) >> 2);
	if (pri <= state->m_layerpri[2])
		*priority_mask = 0;
	else if (pri > state->m_layerpri[2] && pri <= state->m_layerpri[1])
		*priority_mask = 0xf0;
	else if (pri > state->m_layerpri[1] && pri <= state->m_layerpri[0])
		*priority_mask = 0xf0 | 0xcc;
	else
		*priority_mask = 0xf0 | 0xcc | 0xaa;

	*color = state->m_sprite_colorbase + (*color & 0x1f);
}

void blswhstl_sprite_callback( running_machine &machine, int *code, int *color, int *priority_mask )
{
	tmnt_state *state = machine.driver_data<tmnt_state>();
#if 0
if (machine.input().code_pressed(KEYCODE_Q) && (*color & 0x20)) *color = rand();
if (machine.input().code_pressed(KEYCODE_W) && (*color & 0x40)) *color = rand();
if (machine.input().code_pressed(KEYCODE_E) && (*color & 0x80)) *color = rand();
#endif
	int pri = 0x20 | ((*color & 0x60) >> 2);
	if (pri <= state->m_layerpri[2])
		*priority_mask = 0;
	else if (pri > state->m_layerpri[2] && pri <= state->m_layerpri[1])
		*priority_mask = 0xf0;
	else if (pri > state->m_layerpri[1] && pri <= state->m_layerpri[0])
		*priority_mask = 0xf0 | 0xcc;
	else
		*priority_mask = 0xf0 | 0xcc | 0xaa;

	*color = state->m_sprite_colorbase + (*color & 0x1f);
}

void prmrsocr_sprite_callback( running_machine &machine, int *code, int *color, int *priority_mask )
{
	tmnt_state *state = machine.driver_data<tmnt_state>();
	int pri = 0x20 | ((*color & 0x60) >> 2);
	if (pri <= state->m_layerpri[2])
		*priority_mask = 0;
	else if (pri > state->m_layerpri[2] && pri <= state->m_layerpri[1])
		*priority_mask = 0xf0;
	else if (pri > state->m_layerpri[1] && pri <= state->m_layerpri[0])
		*priority_mask = 0xf0 | 0xcc;
	else
		*priority_mask = 0xf0 | 0xcc | 0xaa;

	*code |= state->m_prmrsocr_sprite_bank << 14;

	*color = state->m_sprite_colorbase + (*color & 0x1f);
}



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

  Start the video hardware emulation.

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

VIDEO_START( cuebrick )
{
	tmnt_state *state = machine.driver_data<tmnt_state>();
	state->m_layer_colorbase[0] = 0;
	state->m_layer_colorbase[1] = 32;
	state->m_layer_colorbase[2] = 40;
	state->m_sprite_colorbase = 16;
}

VIDEO_START( mia )
{
	tmnt_state *state = machine.driver_data<tmnt_state>();
	state->m_layer_colorbase[0] = 0;
	state->m_layer_colorbase[1] = 32;
	state->m_layer_colorbase[2] = 40;
	state->m_sprite_colorbase = 16;

	state->m_tmnt_priorityflag = 0;
	state->save_item(NAME(state->m_tmnt_priorityflag));
}

VIDEO_START( tmnt )
{
	tmnt_state *state = machine.driver_data<tmnt_state>();
	state->m_layer_colorbase[0] = 0;
	state->m_layer_colorbase[1] = 32;
	state->m_layer_colorbase[2] = 40;
	state->m_sprite_colorbase = 16;

	state->m_tmnt_priorityflag = 0;
	state->save_item(NAME(state->m_tmnt_priorityflag));

	palette_set_shadow_factor(machine,0.75);
}

VIDEO_START( lgtnfght )	/* also tmnt2, ssriders */
{
	tmnt_state *state = machine.driver_data<tmnt_state>();

	k05324x_set_z_rejection(state->m_k053245, 0);

	state->m_dim_c = state->m_dim_v = state->m_lastdim = state->m_lasten = 0;

	state->save_item(NAME(state->m_dim_c));
	state->save_item(NAME(state->m_dim_v));
	state->save_item(NAME(state->m_lastdim));
	state->save_item(NAME(state->m_lasten));
}

VIDEO_START( glfgreat )
{
	tmnt_state *state = machine.driver_data<tmnt_state>();

	state->m_roz_tilemap = tilemap_create(machine, glfgreat_get_roz_tile_info, tilemap_scan_rows, 16, 16, 512, 512);
	tilemap_set_transparent_pen(state->m_roz_tilemap,0);

	state->m_glfgreat_roz_rom_bank = 0;
	state->m_glfgreat_roz_char_bank = 0;
	state->m_glfgreat_roz_rom_mode = 0;
	state->save_item(NAME(state->m_glfgreat_roz_rom_bank));
	state->save_item(NAME(state->m_glfgreat_roz_char_bank));
	state->save_item(NAME(state->m_glfgreat_roz_rom_mode));
}

VIDEO_START( prmrsocr )
{
	tmnt_state *state = machine.driver_data<tmnt_state>();

	state->m_roz_tilemap = tilemap_create(machine, prmrsocr_get_roz_tile_info, tilemap_scan_rows, 16, 16, 512, 256);
	tilemap_set_transparent_pen(state->m_roz_tilemap,0);

	state->m_prmrsocr_sprite_bank = 0;
	state->m_glfgreat_roz_char_bank = 0;
	state->save_item(NAME(state->m_prmrsocr_sprite_bank));
	state->save_item(NAME(state->m_glfgreat_roz_char_bank));
}

VIDEO_START( blswhstl )
{
	tmnt_state *state = machine.driver_data<tmnt_state>();

	state->m_blswhstl_rombank = -1;
	state->save_item(NAME(state->m_blswhstl_rombank));
}


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

  Memory handlers

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

WRITE16_HANDLER( tmnt_paletteram_word_w )
{
	COMBINE_DATA(space->machine().generic.paletteram.u16 + offset);
	offset &= ~1;

	data = (space->machine().generic.paletteram.u16[offset] << 8) | space->machine().generic.paletteram.u16[offset + 1];
	palette_set_color_rgb(space->machine(), offset / 2, pal5bit(data >> 0), pal5bit(data >> 5), pal5bit(data >> 10));
}



WRITE16_HANDLER( tmnt_0a0000_w )
{
	tmnt_state *state = space->machine().driver_data<tmnt_state>();

	if (ACCESSING_BITS_0_7)
	{
		/* bit 0/1 = coin counters */
		coin_counter_w(space->machine(), 0, data & 0x01);
		coin_counter_w(space->machine(), 1, data & 0x02);	/* 2 players version */

		/* bit 3 high then low triggers irq on sound CPU */
		if (state->m_last == 0x08 && (data & 0x08) == 0)
			device_set_input_line_and_vector(state->m_audiocpu, 0, HOLD_LINE, 0xff);

		state->m_last = data & 0x08;

		/* bit 5 = irq enable */
		state->m_irq5_mask = data & 0x20;

		/* bit 7 = enable char ROM reading through the video RAM */
		k052109_set_rmrd_line(state->m_k052109, (data & 0x80) ? ASSERT_LINE : CLEAR_LINE);

		/* other bits unused */
	}
}

WRITE16_HANDLER( punkshot_0a0020_w )
{
	tmnt_state *state = space->machine().driver_data<tmnt_state>();

	if (ACCESSING_BITS_0_7)
	{
		/* bit 0 = coin counter */
		coin_counter_w(space->machine(), 0, data & 0x01);

		/* bit 2 = trigger irq on sound CPU */
		if (state->m_last == 0x04 && (data & 0x04) == 0)
			device_set_input_line_and_vector(state->m_audiocpu, 0, HOLD_LINE, 0xff);

		state->m_last = data & 0x04;

		/* bit 3 = enable char ROM reading through the video RAM */
		k052109_set_rmrd_line(state->m_k052109, (data & 0x08) ? ASSERT_LINE : CLEAR_LINE);
	}
}

WRITE16_HANDLER( lgtnfght_0a0018_w )
{
	tmnt_state *state = space->machine().driver_data<tmnt_state>();

	if (ACCESSING_BITS_0_7)
	{
		/* bit 0,1 = coin counter */
		coin_counter_w(space->machine(), 0, data & 0x01);
		coin_counter_w(space->machine(), 1, data & 0x02);

		/* bit 2 = trigger irq on sound CPU */
		if (state->m_last == 0x00 && (data & 0x04) == 0x04)
			device_set_input_line_and_vector(state->m_audiocpu, 0, HOLD_LINE, 0xff);

		state->m_last = data & 0x04;

		/* bit 3 = enable char ROM reading through the video RAM */
		k052109_set_rmrd_line(state->m_k052109, (data & 0x08) ? ASSERT_LINE : CLEAR_LINE);
	}
}

WRITE16_HANDLER( blswhstl_700300_w )
{
	tmnt_state *state = space->machine().driver_data<tmnt_state>();

	if (ACCESSING_BITS_0_7)
	{
		/* bit 0,1 = coin counter */
		coin_counter_w(space->machine(), 0,data & 0x01);
		coin_counter_w(space->machine(), 1,data & 0x02);

		/* bit 3 = enable char ROM reading through the video RAM */
		k052109_set_rmrd_line(state->m_k052109, (data & 0x08) ? ASSERT_LINE : CLEAR_LINE);

		/* bit 7 = select char ROM bank */
		if (state->m_blswhstl_rombank != ((data & 0x80) >> 7))
		{
			state->m_blswhstl_rombank = (data & 0x80) >> 7;
			tilemap_mark_all_tiles_dirty_all(space->machine());
		}

		/* other bits unknown */
	}
}


READ16_HANDLER( glfgreat_rom_r )
{
	tmnt_state *state = space->machine().driver_data<tmnt_state>();

	if (state->m_glfgreat_roz_rom_mode)
		return space->machine().region("gfx3")->base()[state->m_glfgreat_roz_char_bank * 0x80000 + offset];
	else if (offset < 0x40000)
	{
		UINT8 *usr = space->machine().region("user1")->base();
		return usr[offset + 0x80000 + state->m_glfgreat_roz_rom_bank * 0x40000] + 256 * usr[offset + state->m_glfgreat_roz_rom_bank * 0x40000];
	}
	else
		return space->machine().region("user1")->base()[((offset & 0x3ffff) >> 2) + 0x100000 + state->m_glfgreat_roz_rom_bank * 0x10000];
}

WRITE16_HANDLER( glfgreat_122000_w )
{
	tmnt_state *state = space->machine().driver_data<tmnt_state>();

	if (ACCESSING_BITS_0_7)
	{
		/* bit 0,1 = coin counter */
		coin_counter_w(space->machine(), 0, data & 0x01);
		coin_counter_w(space->machine(), 1, data & 0x02);

		/* bit 4 = enable char ROM reading through the video RAM */
		k052109_set_rmrd_line(state->m_k052109, (data & 0x10) ? ASSERT_LINE : CLEAR_LINE);

		/* bit 5 = 53596 tile rom bank selection */
		if (state->m_glfgreat_roz_rom_bank != (data & 0x20) >> 5)
		{
			state->m_glfgreat_roz_rom_bank = (data & 0x20) >> 5;
			tilemap_mark_all_tiles_dirty(state->m_roz_tilemap);
		}

		/* bit 6,7 = 53596 char bank selection for ROM test */
		state->m_glfgreat_roz_char_bank = (data & 0xc0) >> 6;

		/* other bits unknown */
	}
	if (ACCESSING_BITS_8_15)
	{
		/* bit 8 = 53596 char/rom selection for ROM test */
		state->m_glfgreat_roz_rom_mode = data & 0x100;
	}
}


WRITE16_HANDLER( ssriders_eeprom_w )
{
	tmnt_state *state = space->machine().driver_data<tmnt_state>();

	if (ACCESSING_BITS_0_7)
	{
		/* bit 0 is data */
		/* bit 1 is cs (active low) */
		/* bit 2 is clock (active high) */
		input_port_write(space->machine(), "EEPROMOUT", data, 0xff);

		/* bits 3-4 control palette dimming */
		/* 4 = DIMPOL = when set, negate SHAD */
		/* 3 = DIMMOD = when set, or BRIT with [negated] SHAD */
		state->m_dim_c = data & 0x18;

		/* bit 5 selects sprite ROM for testing in TMNT2 (bits 5-7, actually, according to the schematics) */
		k053244_bankselect(state->m_k053245, ((data & 0x20) >> 5) << 2);
	}
}

WRITE16_HANDLER( ssriders_1c0300_w )
{
	tmnt_state *state = space->machine().driver_data<tmnt_state>();

	if (ACCESSING_BITS_0_7)
	{
		/* bit 0,1 = coin counter */
		coin_counter_w(space->machine(), 0, data & 0x01);
		coin_counter_w(space->machine(), 1, data & 0x02);

		/* bit 3 = enable char ROM reading through the video RAM */
		k052109_set_rmrd_line(state->m_k052109, (data & 0x08) ? ASSERT_LINE : CLEAR_LINE);

		/* bits 4-6 control palette dimming (DIM0-DIM2) */
		state->m_dim_v = (data & 0x70) >> 4;
	}
}

WRITE16_HANDLER( prmrsocr_122000_w )
{
	tmnt_state *state = space->machine().driver_data<tmnt_state>();

	if (ACCESSING_BITS_0_7)
	{
		/* bit 0,1 = coin counter */
		coin_counter_w(space->machine(), 0, data & 0x01);
		coin_counter_w(space->machine(), 1, data & 0x02);

		/* bit 4 = enable char ROM reading through the video RAM */
		k052109_set_rmrd_line(state->m_k052109, (data & 0x10) ? ASSERT_LINE : CLEAR_LINE);

		/* bit 6 = sprite ROM bank */
		state->m_prmrsocr_sprite_bank = (data & 0x40) >> 6;
		k053244_bankselect(state->m_k053245, state->m_prmrsocr_sprite_bank << 2);

		/* bit 7 = 53596 region selector for ROM test */
		state->m_glfgreat_roz_char_bank = (data & 0x80) >> 7;

		/* other bits unknown (unused?) */
	}
}

READ16_HANDLER( prmrsocr_rom_r )
{
	tmnt_state *state = space->machine().driver_data<tmnt_state>();

	if(state->m_glfgreat_roz_char_bank)
		return space->machine().region("gfx3")->base()[offset];
	else
	{
		UINT8 *usr = space->machine().region("user1")->base();
		return 256 * usr[offset] + usr[offset + 0x020000];
	}
}

WRITE16_HANDLER( tmnt_priority_w )
{
	tmnt_state *state = space->machine().driver_data<tmnt_state>();

	if (ACCESSING_BITS_0_7)
	{
		/* bit 2/3 = priority; other bits unused */
		/* bit2 = PRI bit3 = PRI2
              sprite/playfield priority is controlled by these two bits, by bit 3
              of the background tile color code, and by the SHADOW sprite
              attribute bit.
              Priorities are encoded in a PROM (G19 for TMNT). However, in TMNT,
              the PROM only takes into account the PRI and SHADOW bits.
              PRI  Priority
               0   bg fg spr text
               1   bg spr fg text
              The SHADOW bit, when set, torns a sprite into a shadow which makes
              color below it darker (this is done by turning off three resistors
              in parallel with the RGB output).

              Note: the background color (color used when all of the four layers
              are 0) is taken from the *foreground* palette, not the background
              one as would be more intuitive.
        */
		state->m_tmnt_priorityflag = (data & 0x0c) >> 2;
	}
}



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

  Display refresh

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

SCREEN_UPDATE( mia )
{
	tmnt_state *state = screen->machine().driver_data<tmnt_state>();

	k052109_tilemap_update(state->m_k052109);

	k052109_tilemap_draw(state->m_k052109, bitmap, cliprect, 2, TILEMAP_DRAW_OPAQUE,0);
	if ((state->m_tmnt_priorityflag & 1) == 1) k051960_sprites_draw(state->m_k051960, bitmap, cliprect, 0, 0);
	k052109_tilemap_draw(state->m_k052109, bitmap, cliprect, 1, 0, 0);
	if ((state->m_tmnt_priorityflag & 1) == 0) k051960_sprites_draw(state->m_k051960, bitmap, cliprect, 0, 0);
	k052109_tilemap_draw(state->m_k052109, bitmap, cliprect, 0, 0, 0);

	return 0;
}

SCREEN_UPDATE( tmnt )
{
	tmnt_state *state = screen->machine().driver_data<tmnt_state>();

	k052109_tilemap_update(state->m_k052109);

	k052109_tilemap_draw(state->m_k052109, bitmap, cliprect, 2, TILEMAP_DRAW_OPAQUE,0);
	if ((state->m_tmnt_priorityflag & 1) == 1) k051960_sprites_draw(state->m_k051960, bitmap, cliprect, 0, 0);
	k052109_tilemap_draw(state->m_k052109, bitmap, cliprect, 1, 0, 0);
	if ((state->m_tmnt_priorityflag & 1) == 0) k051960_sprites_draw(state->m_k051960, bitmap, cliprect, 0, 0);
	k052109_tilemap_draw(state->m_k052109, bitmap, cliprect, 0, 0, 0);

	return 0;
}


SCREEN_UPDATE( punkshot )
{
	tmnt_state *state = screen->machine().driver_data<tmnt_state>();

	state->m_sprite_colorbase = k053251_get_palette_index(state->m_k053251, K053251_CI1);
	state->m_layer_colorbase[0] = k053251_get_palette_index(state->m_k053251, K053251_CI2);
	state->m_layer_colorbase[1] = k053251_get_palette_index(state->m_k053251, K053251_CI4);
	state->m_layer_colorbase[2] = k053251_get_palette_index(state->m_k053251, K053251_CI3);

	k052109_tilemap_update(state->m_k052109);

	state->m_sorted_layer[0] = 0;
	state->m_layerpri[0] = k053251_get_priority(state->m_k053251, K053251_CI2);
	state->m_sorted_layer[1] = 1;
	state->m_layerpri[1] = k053251_get_priority(state->m_k053251, K053251_CI4);
	state->m_sorted_layer[2] = 2;
	state->m_layerpri[2] = k053251_get_priority(state->m_k053251, K053251_CI3);

	konami_sortlayers3(state->m_sorted_layer, state->m_layerpri);

	bitmap_fill(screen->machine().priority_bitmap, cliprect, 0);
	k052109_tilemap_draw(state->m_k052109, bitmap, cliprect, state->m_sorted_layer[0], TILEMAP_DRAW_OPAQUE, 1);
	k052109_tilemap_draw(state->m_k052109, bitmap, cliprect, state->m_sorted_layer[1], 0, 2);
	k052109_tilemap_draw(state->m_k052109, bitmap, cliprect, state->m_sorted_layer[2], 0, 4);

	k051960_sprites_draw(state->m_k051960, bitmap, cliprect, -1, -1);
	return 0;
}


SCREEN_UPDATE( lgtnfght )
{
	tmnt_state *state = screen->machine().driver_data<tmnt_state>();
	int bg_colorbase;

	bg_colorbase = k053251_get_palette_index(state->m_k053251, K053251_CI0);
	state->m_sprite_colorbase = k053251_get_palette_index(state->m_k053251, K053251_CI1);
	state->m_layer_colorbase[0] = k053251_get_palette_index(state->m_k053251, K053251_CI2);
	state->m_layer_colorbase[1] = k053251_get_palette_index(state->m_k053251, K053251_CI4);
	state->m_layer_colorbase[2] = k053251_get_palette_index(state->m_k053251, K053251_CI3);

	k052109_tilemap_update(state->m_k052109);

	state->m_sorted_layer[0] = 0;
	state->m_layerpri[0] = k053251_get_priority(state->m_k053251, K053251_CI2);
	state->m_sorted_layer[1] = 1;
	state->m_layerpri[1] = k053251_get_priority(state->m_k053251, K053251_CI4);
	state->m_sorted_layer[2] = 2;
	state->m_layerpri[2] = k053251_get_priority(state->m_k053251, K053251_CI3);

	konami_sortlayers3(state->m_sorted_layer, state->m_layerpri);

	bitmap_fill(screen->machine().priority_bitmap, cliprect, 0);
	bitmap_fill(bitmap, cliprect, 16 * bg_colorbase);
	k052109_tilemap_draw(state->m_k052109, bitmap, cliprect, state->m_sorted_layer[0], 0, 1);
	k052109_tilemap_draw(state->m_k052109, bitmap, cliprect, state->m_sorted_layer[1], 0, 2);
	k052109_tilemap_draw(state->m_k052109, bitmap, cliprect, state->m_sorted_layer[2], 0, 4);

	k053245_sprites_draw(state->m_k053245, bitmap, cliprect);
	return 0;
}


READ16_HANDLER( glfgreat_ball_r )
{
	tmnt_state *state = space->machine().driver_data<tmnt_state>();

#ifdef MAME_DEBUG
popmessage("%04x", state->m_glfgreat_pixel);
#endif
	/* if out of the ROZ layer palette range, it's in the water - return 0 */
	if (state->m_glfgreat_pixel < 0x400 || state->m_glfgreat_pixel >= 0x500)
		return 0;
	else
		return state->m_glfgreat_pixel & 0xff;
}

SCREEN_UPDATE( glfgreat )
{
	tmnt_state *state = screen->machine().driver_data<tmnt_state>();
	int bg_colorbase;

	bg_colorbase = k053251_get_palette_index(state->m_k053251, K053251_CI0);
	state->m_sprite_colorbase  = k053251_get_palette_index(state->m_k053251, K053251_CI1);
	state->m_layer_colorbase[0] = k053251_get_palette_index(state->m_k053251, K053251_CI2);
	state->m_layer_colorbase[1] = k053251_get_palette_index(state->m_k053251, K053251_CI3) + 8;	/* weird... */
	state->m_layer_colorbase[2] = k053251_get_palette_index(state->m_k053251, K053251_CI4);

	k052109_tilemap_update(state->m_k052109);

	state->m_sorted_layer[0] = 0;
	state->m_layerpri[0] = k053251_get_priority(state->m_k053251, K053251_CI2);
	state->m_sorted_layer[1] = 1;
	state->m_layerpri[1] = k053251_get_priority(state->m_k053251, K053251_CI3);
	state->m_sorted_layer[2] = 2;
	state->m_layerpri[2] = k053251_get_priority(state->m_k053251, K053251_CI4);

	konami_sortlayers3(state->m_sorted_layer, state->m_layerpri);

	/* not sure about the 053936 priority, but it seems to work */

	bitmap_fill(screen->machine().priority_bitmap, cliprect, 0);
	bitmap_fill(bitmap, cliprect,16 * bg_colorbase);
	k052109_tilemap_draw(state->m_k052109, bitmap, cliprect, state->m_sorted_layer[0], 0, 1);

	if (state->m_layerpri[0] >= 0x30 && state->m_layerpri[1] < 0x30)
	{
		k053936_zoom_draw(state->m_k053936, bitmap, cliprect, state->m_roz_tilemap, 0, 1, 1);
		state->m_glfgreat_pixel = *BITMAP_ADDR16(bitmap, 0x80, 0x105);
	}

	k052109_tilemap_draw(state->m_k052109, bitmap, cliprect, state->m_sorted_layer[1], 0, 2);

	if (state->m_layerpri[1] >= 0x30 && state->m_layerpri[2] < 0x30)
	{
		k053936_zoom_draw(state->m_k053936, bitmap, cliprect, state->m_roz_tilemap, 0, 1, 1);
		state->m_glfgreat_pixel = *BITMAP_ADDR16(bitmap, 0x80, 0x105);
	}

	k052109_tilemap_draw(state->m_k052109, bitmap, cliprect, state->m_sorted_layer[2], 0, 4);

	if (state->m_layerpri[2] >= 0x30)
	{
		k053936_zoom_draw(state->m_k053936, bitmap, cliprect, state->m_roz_tilemap, 0, 1, 1);
		state->m_glfgreat_pixel = *BITMAP_ADDR16(bitmap, 0x80, 0x105);
	}

	k053245_sprites_draw(state->m_k053245, bitmap, cliprect);
	return 0;
}

SCREEN_UPDATE( tmnt2 )
{
	tmnt_state *state = screen->machine().driver_data<tmnt_state>();
	double brt;
	int i, newdim, newen, cb, ce;

	newdim = state->m_dim_v | ((~state->m_dim_c & 0x10) >> 1);
	newen  = (k053251_get_priority(state->m_k053251, 5) && k053251_get_priority(state->m_k053251, 5) != 0x3e);

	if (newdim != state->m_lastdim || newen != state->m_lasten)
	{
		brt = 1.0;
		if (newen)
			brt -= (1.0 - PALETTE_DEFAULT_SHADOW_FACTOR) * newdim / 8;
		state->m_lastdim = newdim;
		state->m_lasten = newen;

		/*
            Only affect the background and sprites, not text layer.
            Instead of dimming each layer we dim the entire palette
            except text colors because palette bases may change
            anytime and there's no guarantee a dimmed color will be
            reset properly.
        */

		// find the text layer's palette range
		cb = state->m_layer_colorbase[state->m_sorted_layer[2]] << 4;
		ce = cb + 128;

		// dim all colors before it
		for (i = 0; i < cb; i++)
			palette_set_pen_contrast(screen->machine(), i, brt);

		// reset all colors in range
		for (i = cb; i < ce; i++)
			palette_set_pen_contrast(screen->machine(), i, 1.0);

		// dim all colors after it
		for (i = ce; i < 2048; i++)
			palette_set_pen_contrast(screen->machine(), i, brt);

		// toggle shadow/highlight
		if (~state->m_dim_c & 0x10)
			palette_set_shadow_mode(screen->machine(), 1);
		else
			palette_set_shadow_mode(screen->machine(), 0);
	}

	SCREEN_UPDATE_CALL(lgtnfght);
	return 0;
}


SCREEN_UPDATE( thndrx2 )
{
	tmnt_state *state = screen->machine().driver_data<tmnt_state>();
	int bg_colorbase;

	bg_colorbase = k053251_get_palette_index(state->m_k053251, K053251_CI0);
	state->m_sprite_colorbase = k053251_get_palette_index(state->m_k053251, K053251_CI1);
	state->m_layer_colorbase[0] = k053251_get_palette_index(state->m_k053251, K053251_CI2);
	state->m_layer_colorbase[1] = k053251_get_palette_index(state->m_k053251, K053251_CI4);
	state->m_layer_colorbase[2] = k053251_get_palette_index(state->m_k053251, K053251_CI3);

	k052109_tilemap_update(state->m_k052109);

	state->m_sorted_layer[0] = 0;
	state->m_layerpri[0] = k053251_get_priority(state->m_k053251, K053251_CI2);
	state->m_sorted_layer[1] = 1;
	state->m_layerpri[1] = k053251_get_priority(state->m_k053251, K053251_CI4);
	state->m_sorted_layer[2] = 2;
	state->m_layerpri[2] = k053251_get_priority(state->m_k053251, K053251_CI3);

	konami_sortlayers3(state->m_sorted_layer, state->m_layerpri);

	bitmap_fill(screen->machine().priority_bitmap, cliprect, 0);
	bitmap_fill(bitmap, cliprect, 16 * bg_colorbase);
	k052109_tilemap_draw(state->m_k052109, bitmap, cliprect, state->m_sorted_layer[0], 0, 1);
	k052109_tilemap_draw(state->m_k052109, bitmap, cliprect, state->m_sorted_layer[1], 0, 2);
	k052109_tilemap_draw(state->m_k052109, bitmap, cliprect, state->m_sorted_layer[2], 0, 4);

	k051960_sprites_draw(state->m_k051960, bitmap, cliprect, -1, -1);
	return 0;
}



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

  Housekeeping

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

SCREEN_EOF( blswhstl )
{
	tmnt_state *state = machine.driver_data<tmnt_state>();
	k053245_clear_buffer(state->m_k053245);
}