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

    Sun Electronics Kangaroo hardware

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

#include "driver.h"
#include "deprecat.h"
#include "kangaroo.h"


UINT8 *kangaroo_video_control;


static void blitter_execute(void);



/*************************************
 *
 *  Video setup
 *
 *************************************/

VIDEO_START( kangaroo )
{
	/* video RAM is accessed 32 bits at a time (two planes, 4bpp each, 4 pixels) */
	videoram32 = auto_malloc(256 * 64 * 4);
	state_save_register_global_pointer(videoram32, 256 * 64);
}



/*************************************
 *
 *  Video RAM accesses
 *
 *************************************/

static void videoram_write(UINT16 offset, UINT8 data, UINT8 mask)
{
	UINT32 expdata, layermask;

	/* data contains 4 2-bit values packed as DCBADCBA; expand these into 4 8-bit values */
	expdata = 0;
	if (data & 0x01) expdata |= 0x00000055;
	if (data & 0x10) expdata |= 0x000000aa;
	if (data & 0x02) expdata |= 0x00005500;
	if (data & 0x20) expdata |= 0x0000aa00;
	if (data & 0x04) expdata |= 0x00550000;
	if (data & 0x40) expdata |= 0x00aa0000;
	if (data & 0x08) expdata |= 0x55000000;
	if (data & 0x80) expdata |= 0xaa000000;

	/* determine which layers are enabled */
	layermask = 0;
	if (mask & 0x08) layermask |= 0x30303030;
	if (mask & 0x04) layermask |= 0xc0c0c0c0;
	if (mask & 0x02) layermask |= 0x03030303;
	if (mask & 0x01) layermask |= 0x0c0c0c0c;

	/* update layers */
	videoram32[offset] = (videoram32[offset] & ~layermask) | (expdata & layermask);
}


WRITE8_HANDLER( kangaroo_videoram_w )
{
	videoram_write(offset, data, kangaroo_video_control[8]);
}



/*************************************
 *
 *  Video control writes
 *
 *************************************/

WRITE8_HANDLER( kangaroo_video_control_w )
{
	kangaroo_video_control[offset] = data;

	switch (offset)
	{
		case 5:	/* blitter start */
			blitter_execute();
			break;

		case 8:	/* bank select */
			memory_set_bank(1, (data & 0x05) ? 0 : 1);
			break;
	}
}



/*************************************
 *
 *  DMA blitter
 *
 *************************************/

static void blitter_execute(void)
{
	UINT32 gfxhalfsize = memory_region_length(REGION_GFX1) / 2;
	const UINT8 *gfxbase = memory_region(REGION_GFX1);
	UINT16 src = kangaroo_video_control[0] + 256 * kangaroo_video_control[1];
	UINT16 dst = kangaroo_video_control[2] + 256 * kangaroo_video_control[3];
	UINT8 height = kangaroo_video_control[5];
	UINT8 width = kangaroo_video_control[4];
	UINT8 mask = kangaroo_video_control[8];
	int x, y;

	/* during DMA operations, the top 2 bits are ORed together, as well as the bottom 2 bits */
	/* adjust the mask to account for this */
	if (mask & 0x0c) mask |= 0x0c;
	if (mask & 0x03) mask |= 0x03;

	/* loop over height, then width */
	for (y = 0; y <= height; y++, dst += 256)
		for (x = 0; x <= width; x++)
		{
			UINT16 effdst = (dst + x) & 0x3fff;
			UINT16 effsrc = src++ & (gfxhalfsize - 1);
			videoram_write(effdst, gfxbase[0*gfxhalfsize + effsrc], mask & 0x05);
			videoram_write(effdst, gfxbase[1*gfxhalfsize + effsrc], mask & 0x0a);
		}
}



/*************************************
 *
 *  Video updater
 *
 *************************************/

VIDEO_UPDATE( kangaroo )
{
	UINT8 scrolly = kangaroo_video_control[6];
	UINT8 scrollx = kangaroo_video_control[7];
	UINT8 maska = (kangaroo_video_control[10] & 0x28) >> 3;
	UINT8 maskb = (kangaroo_video_control[10] & 0x07) >> 0;
	UINT8 xora = (kangaroo_video_control[9] & 0x20) ? 0xff : 0x00;
	UINT8 xorb = (kangaroo_video_control[9] & 0x10) ? 0xff : 0x00;
	UINT8 enaa = (kangaroo_video_control[9] & 0x08);
	UINT8 enab = (kangaroo_video_control[9] & 0x04);
	UINT8 pria = (~kangaroo_video_control[9] & 0x02);
	UINT8 prib = (~kangaroo_video_control[9] & 0x01);
	rgb_t pens[8];
	int x, y;

	/* build up the pens arrays */
	for (x = 0; x < 8; x++)
		pens[x] = MAKE_RGB(pal1bit(x >> 2), pal1bit(x >> 1), pal1bit(x >> 0));

	/* iterate over pixels */
	for (y = cliprect->min_y; y <= cliprect->max_y; y++)
	{
		UINT32 *dest = BITMAP_ADDR32(bitmap, y, 0);

		for (x = cliprect->min_x; x <= cliprect->max_x; x += 2)
		{
			UINT8 effxa = scrollx + ((x / 2) ^ xora);
			UINT8 effya = scrolly + (y ^ xora);
			UINT8 effxb = (x / 2) ^ xorb;
			UINT8 effyb = y ^ xorb;
			UINT8 pixa = (videoram32[effya + 256 * (effxa / 4)] >> (8 * (effxa % 4) + 0)) & 0x0f;
			UINT8 pixb = (videoram32[effyb + 256 * (effxb / 4)] >> (8 * (effxb % 4) + 4)) & 0x0f;
			UINT8 finalpens;

			/* for each layer, contribute bits if (a) enabled, and (b) either has priority or the opposite plane is 0 */
			finalpens = 0;
			if (enaa && (pria || pixb == 0))
				finalpens |= pixa;
			if (enab && (prib || pixa == 0))
				finalpens |= pixb;

			/* store the first of two pixels, which is always full brightness */
			dest[x + 0] = pens[finalpens & 7];

			/* KOS1 alternates at 5MHz, offset from the pixel clock by 1/2 clock */
			/* when 0, it enables the color mask for pixels with Z = 0 */
			finalpens = 0;
			if (enaa && (pria || pixb == 0))
			{
				if (!(pixa & 0x08)) pixa &= maska;
				finalpens |= pixa;
			}
			if (enab && (prib || pixa == 0))
			{
				if (!(pixb & 0x08)) pixb &= maskb;
				finalpens |= pixb;
			}

			/* store the second of two pixels, which is affected by KOS1 and the A/B masks */
			dest[x + 1] = pens[finalpens & 7];
		}
	}

	return 0;
}