summaryrefslogtreecommitdiffstatshomepage
path: root/src/mame/video/gp9001.c
blob: f29175c5915c66545a9135dd501a8a8707ce9973 (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
/* GP9001 Video Controller */

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

  Functions to emulate the video hardware of some Toaplan games,
  which use one or more Toaplan L7A0498 GP9001 graphic controllers.

  The simpler hardware of these games use one GP9001 controller.
  
  Next we have games that use two GP9001 controllers, the mixing of
  the VDPs depends on a PAL on the motherboard.
  (mixing handled in toaplan2.c)

  Finally we have games using one GP9001 controller and an additional
  text tile layer, which has highest priority. This text tile layer
  appears to have line-scroll support. Some of these games copy the
  text tile gfx data to RAM from the main CPU ROM, which easily allows
  for effects to be added to the tiles, by manipulating the text tile
  gfx data. The tiles are then dynamically decoded from RAM before
  displaying them.


 To Do / Unknowns
    -  Hack is needed to reset sound CPU and sound chip when machine
        is 'tilted' in Pipi & Bibis. Otherwise sound CPU interferes
        with the main CPU test of shared RAM. You get a 'Sub CPU RAM Error'
    -  What do Scroll registers 0Eh and 0Fh really do ????
    -  Snow Bros 2 sets bit 6 of the sprite X info word during weather
        world map, and bits 4, 5 and 6 of the sprite X info word during
        the Rabbit boss screen - reasons are unknown.
    -  Fourth set of scroll registers have been used for Sprite scroll
        though it may not be correct. For most parts this looks right
        except for Snow Bros 2 when in the rabbit boss screen (all sprites
        jump when big green nasty (which is the foreground layer) comes
        in from the left)
    -  Teki Paki tests video RAM from address 0 past SpriteRAM to $37ff.
        This seems to be a bug in Teki Paki's vram test routine !




 GP9001 Tile RAM format (each tile takes up 32 bits)

  0         1         2         3
  ---- ---- ---- ---- xxxx xxxx xxxx xxxx = Tile number (0 - FFFFh)
  ---- ---- -xxx xxxx ---- ---- ---- ---- = Color (0 - 7Fh)
  ---- ---- ?--- ---- ---- ---- ---- ---- = unknown / unused
  ---- xxxx ---- ---- ---- ---- ---- ---- = Priority (0 - Fh)
  ???? ---- ---- ---- ---- ---- ---- ---- = unknown / unused / possible flips

Sprites are of varying sizes between 8x8 and 128x128 with any variation
in between, in multiples of 8 either way.

Here we draw the first 8x8 part of the sprite, then by using the sprite
dimensions, we draw the rest of the 8x8 parts to produce the complete
sprite.

There seems to be sprite buffering - double buffering actually.

 GP9001 Sprite RAM format (data for each sprite takes up 4 words)

  0
  ---- ----  ---- --xx = top 2 bits of Sprite number
  ---- ----  xxxx xx-- = Color (0 - 3Fh)
  ---- xxxx  ---- ---- = Priority (0 - Fh)
  ---x ----  ---- ---- = Flip X
  --x- ----  ---- ---- = Flip Y
  -x-- ----  ---- ---- = Multi-sprite
  x--- ----  ---- ---- = Show sprite ?

  1
  xxxx xxxx  xxxx xxxx = Sprite number (top two bits in word 0)

  2
  ---- ----  ---- xxxx = Sprite X size (add 1, then multiply by 8)
  ---- ----  -??? ---- = unknown - used in Snow Bros. 2
  xxxx xxxx  x--- ---- = X position

  3
  ---- ----  ---- xxxx = Sprite Y size (add 1, then multiply by 8)
  ---- ----  -??? ---- = unknown / unused
  xxxx xxxx  x--- ---- = Y position





 GP9001 Scroll Registers (hex) :

    00      Background scroll X (X flip off)
    01      Background scroll Y (Y flip off)
    02      Foreground scroll X (X flip off)
    03      Foreground scroll Y (Y flip off)
    04      Top (text) scroll X (X flip off)
    05      Top (text) scroll Y (Y flip off)
    06      Sprites    scroll X (X flip off) ???
    07      Sprites    scroll Y (Y flip off) ???
    0E      ??? Initialise Video controller at startup ???
    0F      Scroll update complete ??? (Not used in Ghox and V-Five)

    80      Background scroll X (X flip on)
    81      Background scroll Y (Y flip on)
    82      Foreground scroll X (X flip on)
    83      Foreground scroll Y (Y flip on)
    84      Top (text) scroll X (X flip on)
    85      Top (text) scroll Y (Y flip on)
    86      Sprites    scroll X (X flip on) ???
    87      Sprites    scroll Y (Y flip on) ???
    8F      Same as 0Fh except flip bit is active


Scroll Register 0E writes (Video controller inits ?) from different games:

Teki-Paki        | Ghox             | Knuckle Bash     | Truxton 2        |
0003, 0002, 4000 | ????, ????, ???? | 0202, 0203, 4200 | 0003, 0002, 4000 |

Dogyuun          | Batsugun         |
0202, 0203, 4200 | 0202, 0203, 4200 |
1202, 1203, 5200 | 1202, 1203, 5200 | <--- Second video controller

Pipi & Bibis     | Fix Eight        | V-Five           | Snow Bros. 2     |
0003, 0002, 4000 | 0202, 0203, 4200 | 0202, 0203, 4200 | 0202, 0203, 4200 |

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


#include "emu.h"
#include "gp9001.h"

/*
 Single VDP mixing priority note:

 Initial thoughts were that 16 levels of priority exist for both sprites and tilemaps, ie GP9001_PRIMASK 0xf
 However the end of level scene rendered on the first VDP in Batsugun strongly suggests otherwise.

 Sprites  have 'priority' bits of 0x0600 (level 0x6) set
 Tilemaps have 'priority' bits of 0x7000 (level 0x7) set

 If a mask of 0xf is used then the tilemaps render above the sprites, which causes the V bonus items near the
 counters to be invisible (in addition to the English character quote text)

 using a mask of 0xe causes both priority levels to be equal, allowing the sprites to render above the tilemap.

 The alternative option of allowing sprites to render a priority level higher than tilemaps breaks at least the
 'Welcome to..' screen in Batrider after selecting your character.

 It is unknown if the current solution breaks anything.  The majority of titles don't make extensive use of the
 priority system.

*/
#define GP9001_PRIMASK (0x000e)


static WRITE16_DEVICE_HANDLER( gp9001_bg_tmap_w )
{
	gp9001vdp_device *vdp = (gp9001vdp_device*)device;
	COMBINE_DATA(&vdp->bg.vram16[offset]);
	tilemap_mark_tile_dirty(vdp->bg.tmap,offset/2);
}

static WRITE16_DEVICE_HANDLER( gp9001_fg_tmap_w )
{
	gp9001vdp_device *vdp = (gp9001vdp_device*)device;
	COMBINE_DATA(&vdp->fg.vram16[offset]);
	tilemap_mark_tile_dirty(vdp->fg.tmap,offset/2);
}

static WRITE16_DEVICE_HANDLER( gp9001_top_tmap_w )
{
	gp9001vdp_device *vdp = (gp9001vdp_device*)device;
	COMBINE_DATA(&vdp->top.vram16[offset]);
	tilemap_mark_tile_dirty(vdp->top.tmap,offset/2);
}

static READ16_DEVICE_HANDLER( gp9001_bg_tmap_r )
{
	gp9001vdp_device *vdp = (gp9001vdp_device*)device;
	return vdp->bg.vram16[offset];
}

static READ16_DEVICE_HANDLER( gp9001_fg_tmap_r )
{
	gp9001vdp_device *vdp = (gp9001vdp_device*)device;
	return vdp->fg.vram16[offset];
}

static READ16_DEVICE_HANDLER( gp9001_top_tmap_r )
{
	gp9001vdp_device *vdp = (gp9001vdp_device*)device;
	return vdp->top.vram16[offset];
}

static READ16_DEVICE_HANDLER( gp9001_spram_r )
{
	gp9001vdp_device *vdp = (gp9001vdp_device*)device;
	return vdp->sp.vram16[offset];
}

static WRITE16_DEVICE_HANDLER( gp9001_spram_w )
{
	gp9001vdp_device *vdp = (gp9001vdp_device*)device;
	COMBINE_DATA(&vdp->sp.vram16[offset]);
}

static ADDRESS_MAP_START( gp9001vdp_map, 0, 16 )
	AM_RANGE(0x0000, 0x0fff) AM_DEVREADWRITE(DEVICE_SELF, gp9001_bg_tmap_r, gp9001_bg_tmap_w)
	AM_RANGE(0x1000, 0x1fff) AM_DEVREADWRITE(DEVICE_SELF, gp9001_fg_tmap_r, gp9001_fg_tmap_w)
	AM_RANGE(0x2000, 0x2fff) AM_DEVREADWRITE(DEVICE_SELF, gp9001_top_tmap_r, gp9001_top_tmap_w)
	AM_RANGE(0x3000, 0x37ff) AM_DEVREADWRITE(DEVICE_SELF, gp9001_spram_r, gp9001_spram_w)
	AM_RANGE(0x3800, 0x3fff) AM_RAM // sprite mirror?
ADDRESS_MAP_END


gp9001vdp_device_config::gp9001vdp_device_config(const machine_config &mconfig, const char *tag, const device_config *owner, UINT32 clock)
	: device_config(mconfig, static_alloc_device_config, "gp9001vdp_", tag, owner, clock),
	  device_config_memory_interface(mconfig, *this),
	  m_space_config("gp9001vdp", ENDIANNESS_BIG, 16,14, 0, NULL, *ADDRESS_MAP_NAME(gp9001vdp_map))
{
}

device_config *gp9001vdp_device_config::static_alloc_device_config(const machine_config &mconfig, const char *tag, const device_config *owner, UINT32 clock)
{
	return global_alloc(gp9001vdp_device_config(mconfig, tag, owner, clock));
}

device_t *gp9001vdp_device_config::alloc_device(running_machine &machine) const
{
	return auto_alloc(&machine, gp9001vdp_device(machine, *this));
}

void gp9001vdp_device_config::static_set_gfx_region(device_config *device, int gfxregion)
{
	gp9001vdp_device_config *vdp = downcast<gp9001vdp_device_config *>(device);
	vdp->m_gfxregion = gfxregion;
}

bool gp9001vdp_device_config::device_validity_check(const game_driver &driver) const
{
	bool error = false;
	return error;
}

const address_space_config *gp9001vdp_device_config::memory_space_config(int spacenum) const
{
	return (spacenum == 0) ? &m_space_config : NULL;
}


gp9001vdp_device::gp9001vdp_device(running_machine &_machine, const gp9001vdp_device_config &config)
	: device_t(_machine, config),
	  device_memory_interface(_machine, config, *this),
	  m_config(config),
	  m_gfxregion(m_config.m_gfxregion)
{
}



static TILE_GET_INFO_DEVICE( get_top0_tile_info )
{
	int color, tile_number, attrib;

	gp9001vdp_device *vdp = (gp9001vdp_device*)device;

	attrib = vdp->top.vram16[2*tile_index];

	tile_number = vdp->top.vram16[2*tile_index+1];

	if (vdp->gp9001_gfxrom_is_banked)
	{
		tile_number = ( vdp->gp9001_gfxrom_bank[(tile_number >> 13) & 7] << 13 ) | ( tile_number & 0x1fff );
	}

	color = attrib & 0x0fff; // 0x0f00 priority, 0x007f colour
	SET_TILE_INFO_DEVICE(
			vdp->tile_region,
			tile_number,
			color,
			0);
	//tileinfo->category = (attrib & 0x0f00) >> 8;
}



static TILE_GET_INFO_DEVICE( get_fg0_tile_info )
{
	int color, tile_number, attrib;

	gp9001vdp_device *vdp = (gp9001vdp_device*)device;

	attrib = vdp->fg.vram16[2*tile_index];

	tile_number = vdp->fg.vram16[2*tile_index+1];


	if (vdp->gp9001_gfxrom_is_banked)
	{
		tile_number = ( vdp->gp9001_gfxrom_bank[(tile_number >> 13) & 7] << 13 ) | ( tile_number & 0x1fff );
	}

	color = attrib & 0x0fff; // 0x0f00 priority, 0x007f colour
	SET_TILE_INFO_DEVICE(
			vdp->tile_region,
			tile_number,
			color,
			0);
	//tileinfo->category = (attrib & 0x0f00) >> 8;
}

static TILE_GET_INFO_DEVICE( get_bg0_tile_info )
{
	int color, tile_number, attrib;
	gp9001vdp_device *vdp = (gp9001vdp_device*)device;

	attrib = vdp->bg.vram16[2*tile_index];

	tile_number = vdp->bg.vram16[2*tile_index+1];

	if (vdp->gp9001_gfxrom_is_banked)
	{
		tile_number = ( vdp->gp9001_gfxrom_bank[(tile_number >> 13) & 7] << 13 ) | ( tile_number & 0x1fff );
	}

	color = attrib & 0x0fff; // 0x0f00 priority, 0x007f colour
	SET_TILE_INFO_DEVICE(
			vdp->tile_region,
			tile_number,
			color,
			0);
	//tileinfo->category = (attrib & 0x0f00) >> 8;
}

void gp9001vdp_device::create_tilemaps(int region)
{
	tile_region = region;

	top.tmap = tilemap_create_device(this, get_top0_tile_info,tilemap_scan_rows,16,16,32,32);
	fg.tmap = tilemap_create_device(this, get_fg0_tile_info,tilemap_scan_rows,16,16,32,32);
	bg.tmap = tilemap_create_device(this, get_bg0_tile_info,tilemap_scan_rows,16,16,32,32);

	tilemap_set_transparent_pen(top.tmap,0);
	tilemap_set_transparent_pen(fg.tmap,0);
	tilemap_set_transparent_pen(bg.tmap,0);
}


void gp9001vdp_device::device_start()
{
	top.vram16 = auto_alloc_array_clear(machine, UINT16, GP9001_TOP_VRAM_SIZE/2);
	fg.vram16 = auto_alloc_array_clear(machine, UINT16, GP9001_FG_VRAM_SIZE/2);
	bg.vram16 = auto_alloc_array_clear(machine, UINT16, GP9001_BG_VRAM_SIZE/2);

	sp.vram16 = auto_alloc_array_clear(machine, UINT16, GP9001_SPRITERAM_SIZE/2);
	sp.vram16_buffer = auto_alloc_array_clear(machine, UINT16, GP9001_SPRITERAM_SIZE/2);

	create_tilemaps(m_gfxregion);

	state_save_register_device_item_pointer(this, 0, sp.vram16, GP9001_SPRITERAM_SIZE/2);
	state_save_register_device_item_pointer(this, 0, sp.vram16_buffer, GP9001_SPRITERAM_SIZE/2);
	state_save_register_device_item_pointer(this, 0, top.vram16, GP9001_TOP_VRAM_SIZE/2);
	state_save_register_device_item_pointer(this, 0, fg.vram16, GP9001_FG_VRAM_SIZE/2);
	state_save_register_device_item_pointer(this, 0, bg.vram16, GP9001_BG_VRAM_SIZE/2);

	state_save_register_device_item(this,0, gp9001_scroll_reg);
	state_save_register_device_item(this,0, gp9001_voffs);
	state_save_register_device_item(this,0, bg.scrollx);
	state_save_register_device_item(this,0, bg.scrolly);
	state_save_register_device_item(this,0, fg.scrollx);
	state_save_register_device_item(this,0, fg.scrolly);
	state_save_register_device_item(this,0, top.scrollx);
	state_save_register_device_item(this,0, top.scrolly);
	state_save_register_device_item(this,0, sp.scrollx);
	state_save_register_device_item(this,0, sp.scrolly);
	state_save_register_device_item(this,0, bg.flip);
	state_save_register_device_item(this,0, fg.flip);
	state_save_register_device_item(this,0, top.flip);
	state_save_register_device_item(this,0, sp.flip);

	gp9001_gfxrom_is_banked = 0;
	gp9001_gfxrom_bank_dirty = 0;
	state_save_register_device_item_array(this,0,gp9001_gfxrom_bank);

	// default layer offsets used by all original games
	bg.extra_xoffset.normal  = -0x1d6;
	bg.extra_xoffset.flipped = -0x229;
	bg.extra_yoffset.normal  = -0x1ef;
	bg.extra_yoffset.flipped = -0x210;

	fg.extra_xoffset.normal  = -0x1d8;
	fg.extra_xoffset.flipped = -0x227;
	fg.extra_yoffset.normal  = -0x1ef;
	fg.extra_yoffset.flipped = -0x210;

	top.extra_xoffset.normal = -0x1da;
	top.extra_xoffset.flipped= -0x225;
	top.extra_yoffset.normal = -0x1ef;
	top.extra_yoffset.flipped= -0x210;

	sp.extra_xoffset.normal  = -0x1cc;
	sp.extra_xoffset.flipped = -0x17b;
	sp.extra_yoffset.normal  = -0x1ef;
	sp.extra_yoffset.flipped = -0x108;

	sp.use_sprite_buffer = 1;
}

void gp9001vdp_device::device_reset()
{
	gp9001_voffs = 0;
	gp9001_scroll_reg = 0;
	bg.scrollx = bg.scrolly = 0;
	fg.scrollx = fg.scrolly = 0;
	top.scrollx = top.scrolly = 0;
	sp.scrollx = sp.scrolly = 0;

	bg.flip = 0;
	fg.flip = 0;
	top.flip = 0;
	sp.flip = 0;

	init_scroll_regs();
}


static void gp9001_voffs_w(gp9001vdp_device *vdp, offs_t offset, UINT16 data, UINT16 mem_mask)
{
	COMBINE_DATA(&vdp->gp9001_voffs);
}

static int gp9001_videoram16_r(gp9001vdp_device *vdp, offs_t offset)
{
	int offs = vdp->gp9001_voffs;
	vdp->gp9001_voffs++;
	return vdp->space()->read_word(offs*2);
}


static void gp9001_videoram16_w(gp9001vdp_device *vdp, offs_t offset, UINT16 data, UINT16 mem_mask)
{
	int offs = vdp->gp9001_voffs;
	vdp->gp9001_voffs++;
	vdp->space()->write_word(offs*2, data, mem_mask);
}

static WRITE16_DEVICE_HANDLER( gp9001_devvoffs_w )
{
	gp9001vdp_device *vdp = (gp9001vdp_device *)device;

	gp9001_voffs_w(vdp, offset, data, mem_mask);
}


static READ16_DEVICE_HANDLER( gp9001_devvideoram16_r )
{
	gp9001vdp_device *vdp = (gp9001vdp_device *)device;
	return gp9001_videoram16_r(vdp, offset);
}

static WRITE16_DEVICE_HANDLER( gp9001_devvideoram16_w )
{
	gp9001vdp_device *vdp = (gp9001vdp_device *)device;
	gp9001_videoram16_w(vdp, offset, data, mem_mask);
}


static READ16_DEVICE_HANDLER( gp9001_vdpstatus_r )
{
	return ((device->machine->primary_screen->vpos() + 15) % 262) >= 245;
}

static WRITE16_DEVICE_HANDLER( gp9001_scroll_reg_select_w )
{
	gp9001vdp_device *vdp = (gp9001vdp_device *)device;

	if (ACCESSING_BITS_0_7)
	{
		vdp->gp9001_scroll_reg = data & 0x8f;
		if (data & 0x70)
			logerror("Hmmm, selecting unknown LSB video control register (%04x)  Video controller %01x  \n",vdp->gp9001_scroll_reg,vdp->tile_region>>1);
	}
	else
	{
		logerror("Hmmm, selecting unknown MSB video control register (%04x)  Video controller %01x  \n",vdp->gp9001_scroll_reg,vdp->tile_region>>1);
	}
}

static void gp9001_set_scrollx_and_flip_reg(gp9001tilemaplayer* layer, UINT16 data, UINT16 mem_mask, int flip)
{
	COMBINE_DATA(&layer->scrollx);

	if (flip)
	{
		layer->flip |= TILEMAP_FLIPX;
		tilemap_set_scrollx(layer->tmap,0,-(layer->scrollx+layer->extra_xoffset.flipped));
	}
	else
	{
		layer->flip &= (~TILEMAP_FLIPX);
		tilemap_set_scrollx(layer->tmap,0,layer->scrollx+layer->extra_xoffset.normal);
	}
	tilemap_set_flip(layer->tmap,layer->flip);
}

static void gp9001_set_scrolly_and_flip_reg(gp9001tilemaplayer* layer, UINT16 data, UINT16 mem_mask, int flip)
{
	COMBINE_DATA(&layer->scrolly);

	if (flip)
	{
		layer->flip |= TILEMAP_FLIPY;
		tilemap_set_scrolly(layer->tmap,0,-(layer->scrolly+layer->extra_yoffset.flipped));

	}
	else
	{
		layer->flip &= (~TILEMAP_FLIPY);
		tilemap_set_scrolly(layer->tmap,0,layer->scrolly+layer->extra_yoffset.normal);
	}

	tilemap_set_flip(layer->tmap,layer->flip);
}

static void gp9001_set_sprite_scrollx_and_flip_reg(gp9001spritelayer* layer, UINT16 data, UINT16 mem_mask, int flip)
{
	if (flip)
	{
		data += layer->extra_xoffset.flipped;
		COMBINE_DATA(&layer->scrollx);
		if (layer->scrollx & 0x8000) layer->scrollx |= 0xfe00;
		else layer->scrollx &= 0x1ff;
		layer->flip |= GP9001_SPRITE_FLIPX;
	}
	else
	{
		data += layer->extra_xoffset.normal;
		COMBINE_DATA(&layer->scrollx);

		if (layer->scrollx & 0x8000) layer->scrollx |= 0xfe00;
		else layer->scrollx &= 0x1ff;
		layer->flip &= (~GP9001_SPRITE_FLIPX);
	}
}

static void gp9001_set_sprite_scrolly_and_flip_reg(gp9001spritelayer* layer, UINT16 data, UINT16 mem_mask, int flip)
{
	if (flip)
	{
		data += layer->extra_yoffset.flipped;
		COMBINE_DATA(&layer->scrolly);
		if (layer->scrolly & 0x8000) layer->scrolly |= 0xfe00;
		else layer->scrolly &= 0x1ff;
		layer->flip |= GP9001_SPRITE_FLIPY;
	}
	else
	{
		data += layer->extra_yoffset.normal;
		COMBINE_DATA(&layer->scrolly);
		if (layer->scrolly & 0x8000) layer->scrolly |= 0xfe00;
		else layer->scrolly &= 0x1ff;
		layer->flip &= (~GP9001_SPRITE_FLIPY);
	}
}

static void gp9001_scroll_reg_data_w(gp9001vdp_device *vdp, offs_t offset, UINT16 data, UINT16 mem_mask)
{

	/************************************************************************/
	/***** layer X and Y flips can be set independently, so emulate it ******/
	/************************************************************************/

	//printf("gp9001_scroll_reg_data_w %04x %04x\n", offset, data);
	
	// writes with 8x set turn on flip for the specified layer / axis
	int flip = vdp->gp9001_scroll_reg & 0x80;

	switch(vdp->gp9001_scroll_reg&0x7f)
	{
		case 0x00: gp9001_set_scrollx_and_flip_reg(&vdp->bg, data, mem_mask, flip); break;
		case 0x01: gp9001_set_scrolly_and_flip_reg(&vdp->bg, data, mem_mask, flip); break;					

		case 0x02: gp9001_set_scrollx_and_flip_reg(&vdp->fg, data, mem_mask, flip); break;
		case 0x03: gp9001_set_scrolly_and_flip_reg(&vdp->fg, data, mem_mask, flip); break;					

		case 0x04: gp9001_set_scrollx_and_flip_reg(&vdp->top,data, mem_mask, flip); break;
		case 0x05: gp9001_set_scrolly_and_flip_reg(&vdp->top,data, mem_mask, flip); break;					

		case 0x06: gp9001_set_sprite_scrollx_and_flip_reg(&vdp->sp, data,mem_mask,flip); break;
		case 0x07: gp9001_set_sprite_scrolly_and_flip_reg(&vdp->sp, data,mem_mask,flip); break;


		case 0x0e:	/******* Initialise video controller register ? *******/
					#if 0 // do we know this works on real hw?
					if ((gp9001_sub_cpu == CPU_2_Z80) && (data == 3))
					{
						/* HACK! When tilted, sound CPU needs to be reset. */
						device_t *ym = vdp->machine->device("ymsnd");

						if (ym && ym->type() == YM3812)
						{
							cputag_set_input_line(vdp->machine, "audiocpu", INPUT_LINE_RESET, PULSE_LINE);
							devtag_reset(vdp->machine, "ymsnd");
						}
					}
					#endif

		case 0x0f:	break;


		default:	logerror("Hmmm, writing %08x to unknown video control register (%08x)  Video controller %01x  !!!\n",data ,vdp->gp9001_scroll_reg,vdp->tile_region>>1);
					break;
	}
}

void gp9001vdp_device::init_scroll_regs()
{
	gp9001_set_scrollx_and_flip_reg(&bg, 0, 0xffff, 0);
	gp9001_set_scrolly_and_flip_reg(&bg, 0, 0xffff, 0);					
	gp9001_set_scrollx_and_flip_reg(&fg, 0, 0xffff, 0);
	gp9001_set_scrolly_and_flip_reg(&fg, 0, 0xffff, 0);					
	gp9001_set_scrollx_and_flip_reg(&top,0, 0xffff, 0);
	gp9001_set_scrolly_and_flip_reg(&top,0, 0xffff, 0);					
	gp9001_set_sprite_scrollx_and_flip_reg(&sp, 0,0xffff,0);
	gp9001_set_sprite_scrolly_and_flip_reg(&sp, 0,0xffff,0);
}


static WRITE16_DEVICE_HANDLER( gp9001_scroll_reg_devvdata_w )
{
	gp9001vdp_device *vdp = (gp9001vdp_device*)device;
	gp9001_scroll_reg_data_w(vdp, offset, data, mem_mask);
}



READ16_DEVICE_HANDLER( gp9001_vdp_r )
{
	switch (offset)
	{
		case 0x04/2:
		case 0x06/2:
			return gp9001_devvideoram16_r(device, offset-0x04/2, mem_mask);

		case 0x0c/2:
			return gp9001_vdpstatus_r(device, offset-0x0c/2, mem_mask);

		default:
			logerror("gp9001_vdp_r: read from unhandled offset %04x\n",offset*2);
	}

	return 0xffff;
}

WRITE16_DEVICE_HANDLER( gp9001_vdp_w )
{
	switch (offset)
	{
		case 0x00/2:
			gp9001_devvoffs_w(device, offset-0x00/2, data, mem_mask);
			break;

		case 0x04/2:
		case 0x06/2:
			gp9001_devvideoram16_w(device, offset-0x04/2, data, mem_mask);
			break;

		case 0x08/2:
			gp9001_scroll_reg_select_w(device, offset-0x08/2, data, mem_mask);
			break;

		case 0x0c/2:
			gp9001_scroll_reg_devvdata_w(device, offset-0x0c/2, data, mem_mask);
			break;

		default:
			logerror("gp9001_vdp_w: write to unhandled offset %04x %04x\n",offset, data);
			break;
	}
}

/* some raizing games have a different layout */
READ16_DEVICE_HANDLER( gp9001_vdp_alt_r )
{
	switch (offset)
	{
		case 0x00/2:
			return gp9001_vdpstatus_r(device, offset-0x0c/2, mem_mask);

		case 0x08/2:
		case 0x0a/2:
			return gp9001_devvideoram16_r(device, offset-0x04/2, mem_mask);


		default:
			logerror("gp9001_vdp_alt_r: read from unhandled offset %04x\n",offset*2);
	}

	return 0xffff;
}

WRITE16_DEVICE_HANDLER( gp9001_vdp_alt_w )
{
	switch (offset)
	{
		case 0x00/2:
			gp9001_scroll_reg_devvdata_w(device, offset-0x0c/2, data, mem_mask);
			break;

		case 0x04/2:
			gp9001_scroll_reg_select_w(device, offset-0x08/2, data, mem_mask);
			break;

		case 0x08/2:
		case 0x0a/2:
			gp9001_devvideoram16_w(device, offset-0x04/2, data, mem_mask);
			break;

		case 0x0c/2:
			gp9001_devvoffs_w(device, offset-0x00/2, data, mem_mask);
			break;

		default:
			logerror("gp9001_vdp_alt_w: write to unhandled offset %04x %04x\n",offset, data);
			break;
	}
}



/***************************************************************************/
/**************** PIPIBIBI bootleg interface into this video driver ********/

WRITE16_DEVICE_HANDLER( pipibibi_bootleg_scroll_w )
{
	gp9001vdp_device *vdp = (gp9001vdp_device *)device;

	if (ACCESSING_BITS_8_15 && ACCESSING_BITS_0_7)
	{
		switch(offset)
		{
			case 0x00:	data -= 0x01f; break;
			case 0x01:	data += 0x1ef; break;
			case 0x02:	data -= 0x01d; break;
			case 0x03:	data += 0x1ef; break;
			case 0x04:	data -= 0x01b; break;
			case 0x05:	data += 0x1ef; break;
			case 0x06:	data += 0x1d4; break;
			case 0x07:	data += 0x1f7; break;
			default:	logerror("PIPIBIBI writing %04x to unknown scroll register %04x",data, offset);
		}

		vdp->gp9001_scroll_reg = offset;
		gp9001_scroll_reg_data_w(vdp, offset, data, mem_mask);
	}
}

READ16_DEVICE_HANDLER( pipibibi_bootleg_videoram16_r )
{
	gp9001vdp_device *vdp = (gp9001vdp_device*)device;

	gp9001_voffs_w(vdp, 0, offset, 0xffff);
	return gp9001_videoram16_r(vdp, 0);
}

WRITE16_DEVICE_HANDLER( pipibibi_bootleg_videoram16_w )
{
	gp9001vdp_device *vdp = (gp9001vdp_device*)device;

	gp9001_voffs_w(vdp, 0, offset, 0xffff);
	gp9001_videoram16_w(vdp, 0, data, mem_mask);
}

READ16_DEVICE_HANDLER( pipibibi_bootleg_spriteram16_r )
{
	gp9001vdp_device *vdp = (gp9001vdp_device*)device;

	gp9001_voffs_w(vdp, 0, (0x1800 + offset), 0);
	return gp9001_videoram16_r(vdp, 0);
}

WRITE16_DEVICE_HANDLER( pipibibi_bootleg_spriteram16_w )
{
	gp9001vdp_device *vdp = (gp9001vdp_device*)device;

	gp9001_voffs_w(vdp, 0, (0x1800 + offset), mem_mask);
	gp9001_videoram16_w(vdp, 0, data, mem_mask);
}

/***************************************************************************
    Sprite Handlers
***************************************************************************/

void gp9001vdp_device::draw_sprites( running_machine *machine, bitmap_t *bitmap, const rectangle *cliprect, const UINT8* primap )
{
	const gfx_element *gfx = machine->gfx[tile_region+1];

	int offs, old_x, old_y;

	UINT16 *source;
	
	if (sp.use_sprite_buffer) source=(UINT16 *)(sp.vram16_buffer);
	else source=(UINT16 *)(sp.vram16);

	old_x = (-(sp.scrollx)) & 0x1ff;
	old_y = (-(sp.scrolly)) & 0x1ff;


	for (offs = 0; offs < (GP9001_SPRITERAM_SIZE/2); offs += 4)
	{
		int attrib, sprite, color, priority, flipx, flipy, sx, sy;
		int sprite_sizex, sprite_sizey, dim_x, dim_y, sx_base, sy_base;
		int bank, sprite_num;
		UINT16 primask = (GP9001_PRIMASK << 8);

		attrib = source[offs];
		priority = primap[((attrib & primask)>>8)]+1;

		if ((attrib & 0x8000))
		{
			if (!gp9001_gfxrom_is_banked)	/* No Sprite select bank switching needed */
			{
				sprite = ((attrib & 3) << 16) | source[offs + 1];	/* 18 bit */
			}
			else		/* Batrider Sprite select bank switching required */
			{
				sprite_num = source[offs + 1] & 0x7fff;
				bank = ((attrib & 3) << 1) | (source[offs + 1] >> 15);
				sprite = (gp9001_gfxrom_bank[bank] << 15 ) | sprite_num;
			}
			color = (attrib >> 2) & 0x3f;

			/***** find out sprite size *****/
			sprite_sizex = ((source[offs + 2] & 0x0f) + 1) * 8;
			sprite_sizey = ((source[offs + 3] & 0x0f) + 1) * 8;

			/***** find position to display sprite *****/
			if (!(attrib & 0x4000))
			{
				sx_base = ((source[offs + 2] >> 7) - (sp.scrollx)) & 0x1ff;
				sy_base = ((source[offs + 3] >> 7) - (sp.scrolly)) & 0x1ff;

			} else {
				sx_base = (old_x + (source[offs + 2] >> 7)) & 0x1ff;
				sy_base = (old_y + (source[offs + 3] >> 7)) & 0x1ff;
			}

			old_x = sx_base;
			old_y = sy_base;

			flipx = attrib & GP9001_SPRITE_FLIPX;
			flipy = attrib & GP9001_SPRITE_FLIPY;

			if (flipx)
			{
				/***** Wrap sprite position around *****/
				sx_base -= 7;
				if (sx_base >= 0x1c0) sx_base -= 0x200;
			}
			else
			{
				if (sx_base >= 0x180) sx_base -= 0x200;
			}

			if (flipy)
			{
				sy_base -= 7;
				if (sy_base >= 0x1c0) sy_base -= 0x200;
			}
			else
			{
				if (sy_base >= 0x180) sy_base -= 0x200;
			}

			/***** Flip the sprite layer in any active X or Y flip *****/
			if (sp.flip)
			{
				if (sp.flip & GP9001_SPRITE_FLIPX)
					sx_base = 320 - sx_base;
				if (sp.flip & GP9001_SPRITE_FLIPY)
					sy_base = 240 - sy_base;
			}

			/***** Cancel flip, if it, and sprite layer flip are active *****/
			flipx = (flipx ^ (sp.flip & GP9001_SPRITE_FLIPX));
			flipy = (flipy ^ (sp.flip & GP9001_SPRITE_FLIPY));

			/***** Draw the complete sprites using the dimension info *****/
			for (dim_y = 0; dim_y < sprite_sizey; dim_y += 8)
			{
				if (flipy) sy = sy_base - dim_y;
				else       sy = sy_base + dim_y;
				for (dim_x = 0; dim_x < sprite_sizex; dim_x += 8)
				{
					if (flipx) sx = sx_base - dim_x;
					else       sx = sx_base + dim_x;

					/*
                    drawgfx_transpen(bitmap,cliprect,gfx,sprite,
                        color,
                        flipx,flipy,
                        sx,sy,0);
                    */
					sprite %= gfx->total_elements;
					color %= gfx->total_colors;

					{
						int yy, xx;
						const pen_t *paldata = &gfx->machine->pens[gfx->color_base + gfx->color_granularity * color];
						const UINT8* srcdata = gfx_element_get_data(gfx, sprite);
						int count = 0;
						int ystart, yend, yinc;
						int xstart, xend, xinc;

						if (flipy)
						{
							ystart = 7;
							yend = -1;
							yinc = -1;
						}
						else
						{
							ystart = 0;
							yend = 8;
							yinc = 1;
						}

						if (flipx)
						{
							xstart = 7;
							xend = -1;
							xinc = -1;
						}
						else
						{
							xstart = 0;
							xend = 8;
							xinc = 1;
						}

						for (yy=ystart;yy!=yend;yy+=yinc)
						{
							int drawyy = yy+sy;


							for (xx=xstart;xx!=xend;xx+=xinc)
							{
								int drawxx = xx+sx;

								if (drawxx>=cliprect->min_x && drawxx<=cliprect->max_x && drawyy>=cliprect->min_y && drawyy<=cliprect->max_y)
								{
									UINT8 pix = srcdata[count];
									UINT16* dstptr = BITMAP_ADDR16(bitmap,drawyy,drawxx);
									UINT8* dstpri = BITMAP_ADDR8(this->custom_priority_bitmap, drawyy, drawxx);

									if (priority >= dstpri[0])
									{
										if (pix&0xf)
										{
											dstptr[0] = paldata[pix];
											dstpri[0] = priority;

										}
									}
								}


								count++;
							}
						}


					}

					sprite++ ;
				}
			}
		}
	}
}


/***************************************************************************
    Draw the game screen in the given bitmap_t.
***************************************************************************/

void gp9001vdp_device::gp9001_draw_custom_tilemap(running_machine* machine, bitmap_t* bitmap, tilemap_t* tilemap, const UINT8* priremap, const UINT8* pri_enable )
{
	int width = machine->primary_screen->width();
	int height = machine->primary_screen->height();
	int y,x;
	bitmap_t *tmb = tilemap_get_pixmap(tilemap);
	UINT16* srcptr;
	UINT16* dstptr;
	UINT8* dstpriptr;

	int scrollx = tilemap_get_scrollx(tilemap, 0);
	int scrolly = tilemap_get_scrolly(tilemap, 0);

	for (y=0;y<height;y++)
	{
		int realy = (y+scrolly)&0x1ff;

		srcptr = BITMAP_ADDR16(tmb, realy, 0);
		dstptr = BITMAP_ADDR16(bitmap, y, 0);
		dstpriptr = BITMAP_ADDR8(this->custom_priority_bitmap, y, 0);

		for (x=0;x<width;x++)
		{
			int realx = (x+scrollx)&0x1ff;

			UINT16 pixdat = srcptr[realx];
			UINT8 pixpri = ((pixdat & (GP9001_PRIMASK<<12))>>12);

			if (pri_enable[pixpri])
			{
				pixpri = priremap[pixpri]+1; // priority of 0 isn't desireable
				pixdat &=0x07ff;

				if (pixdat&0xf)
				{
					if (pixpri >= dstpriptr[x])
					{
						dstptr[x] = pixdat;
						dstpriptr[x] = pixpri;
					}
				}
			}
		}
	}
}


static const UINT8 gp9001_primap1[16] =  { 0x00, 0x04, 0x08, 0x0c, 0x10, 0x14, 0x18, 0x1c, 0x20, 0x24, 0x28, 0x2c, 0x30, 0x34, 0x38, 0x3c };
//static const UINT8 gp9001_sprprimap1[16] =  { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f };
static const UINT8 gp9001_sprprimap1[16] =  { 0x00, 0x04, 0x08, 0x0c, 0x10, 0x14, 0x18, 0x1c, 0x20, 0x24, 0x28, 0x2c, 0x30, 0x34, 0x38, 0x3c };

static const UINT8 batsugun_prienable0[16]={ 1,    1,    1,    1,    1,    1,    1,    1,    1,    1,    1,    1,    1,    1,    1,    1 };

void gp9001vdp_device::gp9001_render_vdp(running_machine* machine, bitmap_t* bitmap, const rectangle* cliprect)
{
	if (gp9001_gfxrom_is_banked && gp9001_gfxrom_bank_dirty)
	{
		tilemap_mark_all_tiles_dirty(bg.tmap);
		tilemap_mark_all_tiles_dirty(fg.tmap);
		gp9001_gfxrom_bank_dirty = 0;
	}

	gp9001_draw_custom_tilemap( machine, bitmap, bg.tmap, gp9001_primap1, batsugun_prienable0);
	gp9001_draw_custom_tilemap( machine, bitmap, fg.tmap, gp9001_primap1, batsugun_prienable0);
	gp9001_draw_custom_tilemap( machine, bitmap, top.tmap, gp9001_primap1, batsugun_prienable0);
	draw_sprites( machine,bitmap,cliprect, gp9001_sprprimap1);
}


void gp9001vdp_device::gp9001_video_eof(void)
{
	/** Shift sprite RAM buffers  ***  Used to fix sprite lag **/
	if (sp.use_sprite_buffer) memcpy(sp.vram16_buffer,sp.vram16,GP9001_SPRITERAM_SIZE);
}