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

    Sega G-80 raster hardware

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

#include "driver.h"
#include "segag80r.h"
#include "rescap.h"
#include "video/resnet.h"

UINT8 segag80r_background_pcb;

static double rweights[3], gweights[3], bweights[2];

static UINT8 *dirtychar;
static UINT8 video_control;
static UINT8 video_flip;
static UINT8 vblank_latch;

static tilemap *spaceod_bg_htilemap;
static tilemap *spaceod_bg_vtilemap;
static UINT16 spaceod_hcounter;
static UINT16 spaceod_vcounter;
static UINT8 spaceod_fixed_color;
static UINT8 spaceod_bg_control;
static UINT8 spaceod_bg_detect;
static UINT8 spaceod_bg_detect_tile_color = 1;

static tilemap *bg_tilemap;
static UINT8 bg_enable;
static UINT8 bg_char_bank;
static UINT16 bg_scrollx, bg_scrolly;

static UINT8 pignewt_bg_color_offset;



/*************************************
 *
 *  VBLANK handling
 *
 *************************************/

static TIMER_CALLBACK( vblank_latch_clear )
{
	vblank_latch = 0;
}


static void vblank_latch_set(void)
{
	/* set a timer to mimic the 555 timer that drives the EDGINT signal */
	/* the 555 is run in monostable mode with R=56000 and C=1000pF */
	vblank_latch = 1;
	timer_set(PERIOD_OF_555_MONOSTABLE(CAP_P(1000), RES_K(56)), 0, vblank_latch_clear);

	/* latch the current flip state at the same time */
	video_flip = video_control & 1;
}


INTERRUPT_GEN( segag80r_vblank_start )
{
	vblank_latch_set();

	/* if interrupts are enabled, clock one */
	if (video_control & 0x04)
		irq0_line_hold();
}


INTERRUPT_GEN( sindbadm_vblank_start )
{
	vblank_latch_set();

	/* interrupts appear to always be enabled, but they have a manual */
	/* acknowledge rather than an automatic ack; they are also not masked */
	/* by bit 2 of video_control like a standard G80 */
	irq0_line_assert();
}



/*************************************
 *
 *  Palette conversion
 *
 *************************************/

static void g80_set_palette_entry(int entry, UINT8 data)
{
	int bit0, bit1, bit2;
	int r, g, b;

	/* extract the raw RGB bits */
	r = (data & 0x07) >> 0;
	g = (data & 0x38) >> 3;
	b = (data & 0xc0) >> 6;

	/* red component */
	bit0 = (r >> 0) & 0x01;
	bit1 = (r >> 1) & 0x01;
	bit2 = (r >> 2) & 0x01;
	r = combine_3_weights(rweights, bit0, bit1, bit2);

	/* green component */
	bit0 = (g >> 0) & 0x01;
	bit1 = (g >> 1) & 0x01;
	bit2 = (g >> 2) & 0x01;
	g = combine_3_weights(gweights, bit0, bit1, bit2);

	/* blue component */
	bit0 = (b >> 0) & 0x01;
	bit1 = (b >> 1) & 0x01;
	b = combine_2_weights(bweights, bit0, bit1);

	palette_set_color(Machine, entry, MAKE_RGB(r, g, b));
}


static void spaceod_bg_init_palette(running_machine *machine)
{
	static const int resistances[2] = { 1800, 1200 };
	double trweights[2], tgweights[2], tbweights[2];
	int i;

	/* compute the color output resistor weights at startup */
	compute_resistor_weights(0,	255, -1.0,
			2,	resistances, trweights, 220, 0,
			2,	resistances, tgweights, 220, 0,
			2,	resistances, tbweights, 220, 0);

	/* initialize the fixed background palette */
	for (i = 0; i < 64; i++)
	{
		int bit0, bit1;
		int r, g, b;

		/* extract the raw RGB bits */
		r = (i & 0x30) >> 4;
		g = (i & 0x0c) >> 2;
		b = (i & 0x03) >> 0;

		/* red component */
		bit0 = (r >> 0) & 0x01;
		bit1 = (r >> 1) & 0x01;
		r = combine_2_weights(trweights, bit0, bit1);

		/* green component */
		bit0 = (g >> 0) & 0x01;
		bit1 = (g >> 1) & 0x01;
		g = combine_2_weights(tgweights, bit0, bit1);

		/* blue component */
		bit0 = (b >> 0) & 0x01;
		bit1 = (b >> 1) & 0x01;
		b = combine_2_weights(tbweights, bit0, bit1);

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



/*************************************
 *
 *  Tilemap callbacks
 *
 *************************************/

static TILE_GET_INFO( spaceod_get_tile_info )
{
	int code = memory_region(REGION_GFX2)[tile_index + 0x1000 * (spaceod_bg_control >> 6)];
	SET_TILE_INFO(1, code + 0x100 * ((spaceod_bg_control >> 2) & 1), 0, 0);
}


static TILEMAP_MAPPER( spaceod_scan_rows )
{
	/* this works for both horizontal and vertical tilemaps */
	/* which are 4 32x32 sections */
	return (row & 31) * 32 + (col & 31) + ((row >> 5) * 32*32) + ((col >> 5) * 32*32);
}


static TILE_GET_INFO( bg_get_tile_info )
{
	int code = memory_region(REGION_GFX2)[tile_index];
	SET_TILE_INFO(1, code + 0x100 * bg_char_bank, code >> 4, 0);
}



/*************************************
 *
 *  Video startup
 *
 *************************************/

VIDEO_START( segag80r )
{
	static const int rg_resistances[3] = { 4700, 2400, 1200 };
	static const int b_resistances[2] = { 2000, 1000 };

	/* compute the color output resistor weights at startup */
	compute_resistor_weights(0,	255, -1.0,
			3,	rg_resistances, rweights, 220, 0,
			3,	rg_resistances, gweights, 220, 0,
			2,	b_resistances,  bweights, 220, 0);

	/* allocate paletteram */
	dirtychar = auto_malloc(256);
	memset(dirtychar, 1, 256);

	/* allocate paletteram */
	paletteram = auto_malloc(0x80);

	/* initialize the particulars for each type of background PCB */
	switch (segag80r_background_pcb)
	{
		/* nothing to do here */
		case G80_BACKGROUND_NONE:
			break;

		/* create a fixed background palette and two tilemaps, one horizontally scrolling */
		/* and one vertically scrolling */
		case G80_BACKGROUND_SPACEOD:
			spaceod_bg_init_palette(machine);
			spaceod_bg_htilemap = tilemap_create(spaceod_get_tile_info, spaceod_scan_rows, TILEMAP_TYPE_PEN, 8,8, 128,32);
			spaceod_bg_vtilemap = tilemap_create(spaceod_get_tile_info, spaceod_scan_rows, TILEMAP_TYPE_PEN, 8,8, 32,128);
			break;

		/* background tilemap is effectively 1 screen x n screens */
		case G80_BACKGROUND_MONSTERB:
			bg_tilemap = tilemap_create(bg_get_tile_info, tilemap_scan_rows, TILEMAP_TYPE_PEN, 8,8, 32,memory_region_length(REGION_GFX2) / 32);
			break;

		/* background tilemap is effectively 4 screens x n screens */
		case G80_BACKGROUND_PIGNEWT:
		case G80_BACKGROUND_SINDBADM:
			bg_tilemap = tilemap_create(bg_get_tile_info, tilemap_scan_rows, TILEMAP_TYPE_PEN, 8,8, 128,memory_region_length(REGION_GFX2) / 128);
			break;
	}

	/* register for save states */
	state_save_register_global_pointer(paletteram, 0x80);

	state_save_register_global(video_control);
	state_save_register_global(video_flip);
	state_save_register_global(vblank_latch);

	state_save_register_global(spaceod_hcounter);
	state_save_register_global(spaceod_vcounter);
	state_save_register_global(spaceod_fixed_color);
	state_save_register_global(spaceod_bg_control);
	state_save_register_global(spaceod_bg_detect);

	state_save_register_global(bg_enable);
	state_save_register_global(bg_char_bank);
	state_save_register_global(bg_scrollx);
	state_save_register_global(bg_scrolly);

	state_save_register_global(pignewt_bg_color_offset);
}



/*************************************
 *
 *  Video RAM write
 *
 *************************************/

WRITE8_HANDLER( segag80r_videoram_w )
{
	/* accesses to the upper half of VRAM go to paletteram if selected */
	if ((offset & 0x1000) && (video_control & 0x02))
	{
		offset &= 0x3f;
		paletteram[offset] = data;
		g80_set_palette_entry(offset, data);
		return;
	}

	/* all other accesses go to videoram */
	videoram[offset] = data;

	/* track which characters are dirty */
	if (offset & 0x800)
		dirtychar[(offset & 0x7ff) / 8] = TRUE;
}



/*************************************
 *
 *  Video I board port accesses
 *
 *************************************/

READ8_HANDLER( segag80r_video_port_r )
{
	if (offset == 0)
	{
		logerror("%04X:segag80r_video_port_r(%d)\n", activecpu_get_pc(), offset);
		return 0xff;
	}
	else
	{
		/*
            D0 = 555 timer output from U10 (goes to EDGINT as well)
            D1 = current latched FLIP state
            D2 = interrupt enable state
            D3 = n/c
        */
		return (vblank_latch << 0) | (video_flip << 1) | (video_control & 0x04) | 0xf8;
	}
}


WRITE8_HANDLER( segag80r_video_port_w )
{
	if (offset == 0)
	{
		logerror("%04X:segag80r_video_port_w(%d) = %02X\n", activecpu_get_pc(), offset, data);
	}
	else
	{
		/*
            D0 = FLIP (latched at VSYNC)
            D1 = if low, allows writes to the upper 4k of video RAM
               = if high, allows writes to palette RAM
            D2 = interrupt enable
            D3 = n/c (used as flip by many background boards)
        */
		video_control = data;
	}
}



/*************************************
 *
 *  Space Odyssey background borad
 *  port accesses
 *
 *************************************/

READ8_HANDLER( spaceod_back_port_r )
{
	/* force an update to get the current detection value */
	video_screen_update_partial(0, video_screen_get_vpos(0));
	return 0xfe | spaceod_bg_detect;
}


WRITE8_HANDLER( spaceod_back_port_w )
{
	switch (offset & 7)
	{
		/* port 0: latches D0-D7 into LS377 at U39 (SH4)

            d0 = counter direction: controls U/D on LS191 counters
            d1 = horizontal (0) or vertical (1) scrolling
            d2 = character bank (0/1)
            d6 = background ROM select 0
            d7 = background ROM select 1
        */
		case 0:
			if ((spaceod_bg_control ^ data) & 0xc4)
			{
				tilemap_mark_all_tiles_dirty(spaceod_bg_htilemap);
				tilemap_mark_all_tiles_dirty(spaceod_bg_vtilemap);
			}
			spaceod_bg_control = data;
			break;

		/* port 1: loads both H and V counters with 0 */
		case 1:
			spaceod_hcounter = 0;
			spaceod_vcounter = 0;
			break;

		/* port 2: clocks either the H or V counters (based on port 0:d1) */
		/* either up or down (based on port 0:d0) */
		case 2:
			if (!(spaceod_bg_control & 0x02))
			{
				if (!(spaceod_bg_control & 0x01))
					spaceod_hcounter++;
				else
					spaceod_hcounter--;
			}
			else
			{
				if (!(spaceod_bg_control & 0x01))
					spaceod_vcounter++;
				else
					spaceod_vcounter--;
			}
			break;

		/* port 3: clears the background detection flag */
		case 3:
			video_screen_update_partial(0, video_screen_get_vpos(0));
			spaceod_bg_detect = 0;
			break;

		/* port 4: enables (0)/disables (1) the background */
		case 4:
			bg_enable = data & 1;
			break;

		/* port 5: specifies fixed background color */
		/* top two bits are not connected */
		case 5:
			spaceod_fixed_color = data & 0x3f;
			break;

		/* port 6: latches D0-D7 into LS377 at U37 -> CN1-11/12/13/14/15/16/17/18 */
		/* port 7: latches D0-D5 into LS174 at U40 -> CN2-1/2/3/4/5/6 */
		case 6:
		case 7:
			break;
	}
}



/*************************************
 *
 *  Monster Bash background board
 *  accesses
 *
 *************************************/

WRITE8_HANDLER( monsterb_videoram_w )
{
	/* accesses to the the area $f040-$f07f go to background palette if */
	/* the palette access enable bit is set */
	if ((offset & 0x1fc0) == 0x1040 && (video_control & 0x40))
	{
		offs_t paloffs = offset & 0x3f;
		paletteram[paloffs | 0x40] = data;
		g80_set_palette_entry(paloffs | 0x40, data);
		/* note that since the background board is not integrated with the main board */
		/* writes here also write through to regular videoram */
	}

	/* handle everything else */
	segag80r_videoram_w(offset, data);
}


WRITE8_HANDLER( monsterb_back_port_w )
{
	switch (offset & 7)
	{
		/* port 0: not used (looks like latches for C7-C10 = background color) */
		case 0:
			break;

		/* port 1: not used (looks like comparator tile color value for collision detect)  */
		case 1:
			break;

		/* port 2: not connected */
		/* port 3: not connected */
		case 2:
		case 3:
			break;

		/* port 4: main control latch

            d0 = CG0 - BG MSB ROM bank select bit 0
            d1 = CG1 - BG MSB ROM bank select bit 1
            d2 = CG2 - BG LSB ROM bank select bit 0
            d3 = CG3 - BG LSB ROM bank select bit 1
            d4 = SCN0 - background select bit 0
            d5 = SCN1 - background select bit 1
            d6 = SCN2 - background select bit 2
            d7 = BKGEN - background enable
         */
		case 4:
			if ((bg_char_bank ^ data) & 0x0f)
				tilemap_mark_all_tiles_dirty(bg_tilemap);
			bg_char_bank = data & 0x0f;
			bg_scrolly = (data << 4) & 0x700;
			bg_enable = data & 0x80;
			break;

		/* port 5: not connected */
		case 5:
			break;
	}
}



/*************************************
 *
 *  Pig Newton/Monster Bash 2-board
 *  background accesses
 *
 *************************************/

WRITE8_HANDLER( pignewt_videoram_w )
{
	/* accesses to the the area $f040-$f07f go to background palette if */
	/* the palette access enable bit is set */
	if ((offset & 0x1fc0) == 0x1040 && (video_control & 0x02))
	{
		offs_t paloffs = offset & 0x3f;
		paletteram[paloffs | 0x40] = data;
		g80_set_palette_entry(paloffs | 0x40, data);
		return;
	}

	/* handle everything else */
	segag80r_videoram_w(offset, data);
}


WRITE8_HANDLER( pignewt_back_color_w )
{
	/* it is not really known what this does */
	if (offset == 0)
		pignewt_bg_color_offset = data;
	else
		logerror("pignewt_back_color_w(%d) = %02X\n", pignewt_bg_color_offset, data);
}


WRITE8_HANDLER( pignewt_back_port_w )
{
	switch (offset & 7)
	{
		/* port 0: scroll offset low */
		case 0:
			bg_scrollx = (bg_scrollx & 0x300) | data;
			break;

		/* port 1: scroll offset high */
		case 1:
			bg_scrollx = (bg_scrollx & 0x0ff) | ((data << 8) & 0x300);
			bg_enable = data & 0x80;
			break;

		/* port 2: scroll offset low */
		case 2:
			bg_scrolly = (bg_scrolly & 0x300) | data;
			break;

		/* port 3: scroll offset high */
		case 3:
			bg_scrolly = (bg_scrolly & 0x0ff) | ((data << 8) & 0x300);
			break;

		/* port 4: background character bank control

            d0 = CG0 - BG MSB ROM bank select bit 0
            d1 = CG1 - BG MSB ROM bank select bit 1
            d2 = CG2 - BG LSB ROM bank select bit 0
            d3 = CG3 - BG LSB ROM bank select bit 1

            at least, that's the theory; however, monster2 sets this = 0x03 wanting
            bank 1, so I'm assuming that only d3 and d0 really matter
         */
		case 4:
			data = (data & 0x09) | ((data >> 2) & 0x02) | ((data << 2) & 0x04);
			if ((bg_char_bank ^ data) & 0x0f)
				tilemap_mark_all_tiles_dirty(bg_tilemap);
			bg_char_bank = data & 0x0f;
			break;

		/* port 5: not connected? */
		case 5:
			break;
	}
}



/*************************************
 *
 *  Sindbad Mystery background accesses
 *
 *************************************/

WRITE8_HANDLER( sindbadm_videoram_w )
{
	/* accesses to the the area $f000-$f03f go to background palette if */
	/* the palette access enable bit is set */
	if ((offset & 0x1fc0) == 0x1000 && (video_control & 0x02))
	{
		offs_t paloffs = offset & 0x3f;
		paletteram[paloffs | 0x40] = data;
		g80_set_palette_entry(paloffs | 0x40, data);
		return;
	}

	/* handle everything else */
	segag80r_videoram_w(offset, data);
}


WRITE8_HANDLER( sindbadm_back_port_w )
{
	switch (offset & 3)
	{
		/* port 0: irq ack */
		case 0:
			cpunum_set_input_line(0, 0, CLEAR_LINE);
			break;

		/* port 1: background control

            d0 = BG ROM bank select bit 0
            d1 = BG ROM bank select bit 1
            d2 = BG page select (X scroll bit 0)
            d3 = BG page select (X scroll bit 1)
            d4 = BG page select (Y scroll bit 0)
            d5 = BG page select (Y scroll bit 1)
            d6 = BG page select (Y scroll bit 2)
            d7 = BG enable
        */
		case 1:
			bg_enable = data & 0x80;
			bg_scrollx = (data << 6) & 0x300;
			bg_scrolly = (data << 4) & 0x700;
			if ((bg_char_bank ^ data) & 0x03)
				tilemap_mark_all_tiles_dirty(bg_tilemap);
			bg_char_bank = data & 0x03;
			break;
	}
}



/*************************************
 *
 *  Video I videoram rendering
 *
 *************************************/

static void draw_videoram(running_machine *machine, mame_bitmap *bitmap, const rectangle *cliprect, const UINT8 *transparent_pens)
{
	int flipmask = video_flip ? 0x1f : 0x00;
	int x, y;

	/* iterate over the screen and draw visible tiles */
	for (y = cliprect->min_y / 8; y <= cliprect->max_y / 8; y++)
	{
		int effy = video_flip ? 27 - y : y;
		for (x = cliprect->min_x / 8; x <= cliprect->max_x / 8; x++)
		{
			int offs = effy * 32 + (x ^ flipmask);
			UINT8 tile = videoram[offs];

			/* if the tile is dirty, decode it */
			if (dirtychar[tile])
			{
				decodechar(machine->gfx[0], tile, &videoram[0x800], machine->drv->gfxdecodeinfo[0].gfxlayout);
				dirtychar[tile] = 0;
			}

			/* draw the tile */
			drawgfx(bitmap, machine->gfx[0], tile, tile >> 4, video_flip, video_flip, x*8, y*8, cliprect, TRANSPARENCY_PENS, transparent_pens[tile >> 4]);
		}
	}
}



/*************************************
 *
 *  Space Odyssey background rendering
 *
 *************************************/

static void draw_background_spaceod(mame_bitmap *bitmap, const rectangle *cliprect)
{
	mame_bitmap *pixmap = tilemap_get_pixmap(!(spaceod_bg_control & 0x02) ? spaceod_bg_htilemap : spaceod_bg_vtilemap);
	int flipmask = (spaceod_bg_control & 0x01) ? 0xff : 0x00;
	int xoffset = (spaceod_bg_control & 0x02) ? 0x10 : 0x00;
	int xmask = pixmap->width - 1;
	int ymask = pixmap->height - 1;
	int x, y;

	/* The H and V counters on this board are independent of the ones on */
	/* the main board. The H counter starts counting from 0 when EXT BLK */
	/* goes to 0; this coincides with H=0, so that's fine. However, the V */
	/* counter starts counting from 0 when VSYNC=0, which happens at line */
	/* 240, giving us an offset of (262-240) = 22 scanlines. */

	/* now fill in the background wherever there are black pixels */
	for (y = cliprect->min_y; y <= cliprect->max_y; y++)
	{
		int effy = (y + spaceod_vcounter + 22) ^ flipmask;
		UINT16 *src = (UINT16 *)pixmap->base + (effy & ymask) * pixmap->rowpixels;
		UINT16 *dst = (UINT16 *)bitmap->base + y * bitmap->rowpixels;

		/* loop over horizontal pixels */
		for (x = cliprect->min_x; x <= cliprect->max_x; x++)
		{
			int effx = ((x + spaceod_hcounter) ^ flipmask) + xoffset;
			UINT8 fgpix = paletteram[dst[x]];
			UINT8 bgpix = src[effx & xmask] & 0x3f;

			/* the background detect flag is set if:
                - bgpix != 0 AND
                - fgpix != 0 AND
                - the original tile color == DIP switches
            */
			if (bgpix != 0 && fgpix != 0 && (dst[x] >> 2) == spaceod_bg_detect_tile_color)
				spaceod_bg_detect = 1;

			/* the background graphics are only displayed if:
                - fgpix == 0 AND
                - !EXTBLK (not blanked) AND
                - bg_enable == 0
            */
			if (fgpix == 0 && bg_enable == 0)
				dst[x] = bgpix | spaceod_fixed_color | 0x40;
		}
	}
}



/*************************************
 *
 *  Draw a background with only page
 *  granular scrolling
 *
 *************************************/

static void draw_background_page_scroll(mame_bitmap *bitmap, const rectangle *cliprect)
{
	mame_bitmap *pixmap = tilemap_get_pixmap(bg_tilemap);
	int flipmask = (video_control & 0x08) ? 0xff : 0x00;
	int xmask = pixmap->width - 1;
	int ymask = pixmap->height - 1;
	int x, y;

	/* if disabled, draw nothing */
	if (!bg_enable)
	{
		fillbitmap(bitmap, 0, cliprect);
		return;
	}

	/* now fill in the background wherever there are black pixels */
	for (y = cliprect->min_y; y <= cliprect->max_y; y++)
	{
		int effy = bg_scrolly + (((y ^ flipmask) + (flipmask & 0xe0)) & 0xff);
		UINT16 *src = (UINT16 *)pixmap->base + (effy & ymask) * pixmap->rowpixels;
		UINT16 *dst = (UINT16 *)bitmap->base + y * bitmap->rowpixels;

		/* loop over horizontal pixels */
		for (x = cliprect->min_x; x <= cliprect->max_x; x++)
		{
			int effx = bg_scrollx + (x ^ flipmask);
			dst[x] = src[effx & xmask];
		}
	}
}



/*************************************
 *
 *  Draw a background with full pixel
 *  level scrolling
 *
 *************************************/

static void draw_background_full_scroll(mame_bitmap *bitmap, const rectangle *cliprect)
{
	mame_bitmap *pixmap = tilemap_get_pixmap(bg_tilemap);
	int flipmask = (video_control & 0x08) ? 0x3ff : 0x000;
	int xmask = pixmap->width - 1;
	int ymask = pixmap->height - 1;
	int x, y;

	/* if disabled, draw nothing */
	if (!bg_enable)
	{
		fillbitmap(bitmap, 0, cliprect);
		return;
	}

	/* now fill in the background wherever there are black pixels */
	for (y = cliprect->min_y; y <= cliprect->max_y; y++)
	{
		int effy = (y + bg_scrolly) ^ flipmask;
		UINT16 *src = (UINT16 *)pixmap->base + (effy & ymask) * pixmap->rowpixels;
		UINT16 *dst = (UINT16 *)bitmap->base + y * bitmap->rowpixels;

		/* loop over horizontal pixels */
		for (x = cliprect->min_x; x <= cliprect->max_x; x++)
		{
			int effx = (x + bg_scrollx) ^ flipmask;
			dst[x] = src[effx & xmask];
		}
	}
}



/*************************************
 *
 *  Generic video update
 *
 *************************************/

VIDEO_UPDATE( segag80r )
{
	UINT8 transparent_pens[16];

	switch (segag80r_background_pcb)
	{
		/* foreground: opaque */
		/* background: none */
		case G80_BACKGROUND_NONE:
			memset(transparent_pens, 0, 16);
			draw_videoram(machine, bitmap, cliprect, transparent_pens);
			break;

		/* foreground: visible except where black */
		/* background: Space Odyssey special */
		/* we draw the foreground first, then the background to do collision detection */
		case G80_BACKGROUND_SPACEOD:
			memset(transparent_pens, 0, 16);
			draw_videoram(machine, bitmap, cliprect, transparent_pens);
			draw_background_spaceod(bitmap, cliprect);
			break;

		/* foreground: visible except for pen 0 (this disagrees with schematics) */
		/* background: page-granular scrolling */
		case G80_BACKGROUND_MONSTERB:
			memset(transparent_pens, 1, 16);
			draw_background_page_scroll(bitmap, cliprect);
			draw_videoram(machine, bitmap, cliprect, transparent_pens);
			break;

		/* foreground: visible except for pen 0 */
		/* background: full scrolling */
		case G80_BACKGROUND_PIGNEWT:
			memset(transparent_pens, 1, 16);
			draw_background_full_scroll(bitmap, cliprect);
			draw_videoram(machine, bitmap, cliprect, transparent_pens);
			break;

		/* foreground: visible except for pen 0 */
		/* background: page-granular scrolling */
		case G80_BACKGROUND_SINDBADM:
			memset(transparent_pens, 1, 16);
			draw_background_page_scroll(bitmap, cliprect);
			draw_videoram(machine, bitmap, cliprect, transparent_pens);
			break;
	}
	return 0;
}