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

    Art & Magic hardware

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

#include "emu.h"
#include "profiler.h"
#include "cpu/tms34010/tms34010.h"
#include "video/tlc34076.h"
#include "includes/artmagic.h"


#define INSTANT_BLIT		1


/*************************************
 *
 *  Inlines
 *
 *************************************/

INLINE UINT16 *address_to_vram(artmagic_state *state, offs_t *address)
{
	offs_t original = *address;
	*address = TOWORD(original & 0x001fffff);
	if (original >= 0x00000000 && original < 0x001fffff)
		return state->m_vram0;
	else if (original >= 0x00400000 && original < 0x005fffff)
		return state->m_vram1;
	return NULL;
}



/*************************************
 *
 *  Video start
 *
 *************************************/

VIDEO_START( artmagic )
{
	artmagic_state *state = machine.driver_data<artmagic_state>();
	state->m_blitter_base = (UINT16 *)machine.region("gfx1")->base();
	state->m_blitter_mask = machine.region("gfx1")->bytes()/2 - 1;

	state_save_register_global_array(machine, state->m_xor);
	state_save_register_global(machine, state->m_is_stoneball);
	state_save_register_global_array(machine, state->m_blitter_data);
	state_save_register_global(machine, state->m_blitter_page);
}



/*************************************
 *
 *  Shift register transfers
 *
 *************************************/

void artmagic_to_shiftreg(address_space *space, offs_t address, UINT16 *data)
{
	artmagic_state *state = space->machine().driver_data<artmagic_state>();
	UINT16 *vram = address_to_vram(state, &address);
	if (vram)
		memcpy(data, &vram[address], TOBYTE(0x2000));
}


void artmagic_from_shiftreg(address_space *space, offs_t address, UINT16 *data)
{
	artmagic_state *state = space->machine().driver_data<artmagic_state>();
	UINT16 *vram = address_to_vram(state, &address);
	if (vram)
		memcpy(&vram[address], data, TOBYTE(0x2000));
}



/*************************************
 *
 *  Custom blitter
 *
 *************************************/

static void execute_blit(running_machine &machine)
{
	artmagic_state *state = machine.driver_data<artmagic_state>();
	UINT16 *dest = state->m_blitter_page ? state->m_vram0 : state->m_vram1;
	int offset = ((state->m_blitter_data[1] & 0xff) << 16) | state->m_blitter_data[0];
	int color = (state->m_blitter_data[1] >> 4) & 0xf0;
	int x = (INT16)state->m_blitter_data[2];
	int y = (INT16)state->m_blitter_data[3];
	int maskx = state->m_blitter_data[6] & 0xff;
	int masky = state->m_blitter_data[6] >> 8;
	int w = ((state->m_blitter_data[7] & 0xff) + 1) * 4;
	int h = (state->m_blitter_data[7] >> 8) + 1;
	int i, j, sx, sy, last;

#if 0
{
	static UINT32 hit_list[50000];
	static int hit_index;
	static FILE *f;

	logerror("%s:Blit from %06X to (%d,%d) %dx%d -- %04X %04X %04X %04X %04X %04X %04X %04X\n",
				machine.describe_context(), offset, x, y, w, h,
				state->m_blitter_data[0], state->m_blitter_data[1],
				state->m_blitter_data[2], state->m_blitter_data[3],
				state->m_blitter_data[4], state->m_blitter_data[5],
				state->m_blitter_data[6], state->m_blitter_data[7]);

	if (!f) f = fopen("artmagic.log", "w");

	for (i = 0; i < hit_index; i++)
		if (hit_list[i] == offset)
			break;
	if (i == hit_index)
	{
		int tempoffs = offset;
		hit_list[hit_index++] = offset;

		fprintf(f, "----------------------\n"
				   "%s:Blit from %06X to (%d,%d) %dx%d -- %04X %04X %04X %04X %04X %04X %04X %04X\n",
					machine.describe_context(), offset, x, y, w, h,
					state->m_blitter_data[0], state->m_blitter_data[1],
					state->m_blitter_data[2], state->m_blitter_data[3],
					state->m_blitter_data[4], state->m_blitter_data[5],
					state->m_blitter_data[6], state->m_blitter_data[7]);

		fprintf(f, "\t");
		for (i = 0; i < h; i++)
		{
			for (j = 0; j < w; j += 4)
				fprintf(f, "%04X ", state->m_blitter_base[tempoffs++]);
			fprintf(f, "\n\t");
		}
		fprintf(f, "\n\t");
		tempoffs = offset;
		for (i = 0; i < h; i++)
		{
			last = 0;
			if (i == 0)	/* first line */
			{
				/* ultennis, stonebal */
				last ^= (state->m_blitter_data[7] & 0x0001);
				if (state->m_is_stoneball)
					last ^= ((state->m_blitter_data[0] & 0x0020) >> 3);
				else	/* ultennis */
					last ^= ((state->m_blitter_data[0] & 0x0040) >> 4);

				/* cheesech */
				last ^= ((state->m_blitter_data[7] & 0x0400) >> 9);
				last ^= ((state->m_blitter_data[0] & 0x2000) >> 10);
			}
			else	/* following lines */
			{
				int val = state->m_blitter_base[tempoffs];

				/* ultennis, stonebal */
				last ^= 4;
				last ^= ((val & 0x0400) >> 8);
				last ^= ((val & 0x5000) >> 12);

				/* cheesech */
				last ^= 8;
				last ^= ((val & 0x0800) >> 8);
				last ^= ((val & 0xa000) >> 12);
			}

			for (j = 0; j < w; j += 4)
			{
				static const char hex[] = ".123456789ABCDEF";
				int val = state->m_blitter_base[tempoffs++];
				int p1, p2, p3, p4;
				p1 = last = ((val ^ state->m_xor[last]) >>  0) & 0xf;
				p2 = last = ((val ^ state->m_xor[last]) >>  4) & 0xf;
				p3 = last = ((val ^ state->m_xor[last]) >>  8) & 0xf;
				p4 = last = ((val ^ state->m_xor[last]) >> 12) & 0xf;
				fprintf(f, "%c%c%c%c ", hex[p1], hex[p2], hex[p3], hex[p4]);
			}
			fprintf(f, "\n\t");
		}
		fprintf(f, "\n");
	}
}
#endif

	g_profiler.start(PROFILER_VIDEO);

	last = 0;
	sy = y;
	for (i = 0; i < h; i++)
	{
		if ((i & 1) || !((masky << ((i/2) & 7)) & 0x80))
		{
			if (sy >= 0 && sy < 256)
			{
				int tsy = sy * TOWORD(0x2000);
				sx = x;

				/* The first pixel of every line doesn't have a previous pixel
                   to depend on, so it takes the "feed" from other bits.
                   The very first pixel blitted is also treated differently.

                   ultennis/stonebal use a different encryption from cheesech,
                   however the former only need to set bits 0 and 2 of the
                   feed (the others are irrelevant), while the latter only
                   bits 1 and 3, so I can handle both at the same time.
                 */
				last = 0;
				if (i == 0)	/* first line */
				{
					/* ultennis, stonebal */
					last ^= (state->m_blitter_data[7] & 0x0001);
					if (state->m_is_stoneball)
						last ^= ((state->m_blitter_data[0] & 0x0020) >> 3);
					else	/* ultennis */
						last ^= (((state->m_blitter_data[0] + 1) & 0x0040) >> 4);

					/* cheesech */
					last ^= ((state->m_blitter_data[7] & 0x0400) >> 9);
					last ^= ((state->m_blitter_data[0] & 0x2000) >> 10);
				}
				else	/* following lines */
				{
					int val = state->m_blitter_base[offset & state->m_blitter_mask];

					/* ultennis, stonebal */
					last ^= 4;
					last ^= ((val & 0x0400) >> 8);
					last ^= ((val & 0x5000) >> 12);

					/* cheesech */
					last ^= 8;
					last ^= ((val & 0x0800) >> 8);
					last ^= ((val & 0xa000) >> 12);
				}

				for (j = 0; j < w; j += 4)
				{
					UINT16 val = state->m_blitter_base[(offset + j/4) & state->m_blitter_mask];
					if (sx < 508)
					{
						if (h == 1 && state->m_is_stoneball)
							last = ((val) >>  0) & 0xf;
						else
							last = ((val ^ state->m_xor[last]) >>  0) & 0xf;
						if (!((maskx << ((j/2) & 7)) & 0x80))
						{
							if (last && sx >= 0 && sx < 512)
								dest[tsy + sx] = color | (last);
							sx++;
						}

						if (h == 1 && state->m_is_stoneball)
							last = ((val) >>  4) & 0xf;
						else
							last = ((val ^ state->m_xor[last]) >>  4) & 0xf;
						{
							if (last && sx >= 0 && sx < 512)
								dest[tsy + sx] = color | (last);
							sx++;
						}

						if (h == 1 && state->m_is_stoneball)
							last = ((val) >>  8) & 0xf;
						else
							last = ((val ^ state->m_xor[last]) >>  8) & 0xf;
						if (!((maskx << ((j/2) & 7)) & 0x40))
						{
							if (last && sx >= 0 && sx < 512)
								dest[tsy + sx] = color | (last);
							sx++;
						}

						if (h == 1 && state->m_is_stoneball)
							last = ((val) >> 12) & 0xf;
						else
							last = ((val ^ state->m_xor[last]) >> 12) & 0xf;
						{
							if (last && sx >= 0 && sx < 512)
								dest[tsy + sx] = color | (last);
							sx++;
						}
					}
				}
			}
			sy++;
		}
		offset += w/4;
	}

	g_profiler.stop();

#if (!INSTANT_BLIT)
	state->m_blitter_busy_until = machine.time() + attotime::from_nsec(w*h*20);
#endif
}


READ16_HANDLER( artmagic_blitter_r )
{
	artmagic_state *state = space->machine().driver_data<artmagic_state>();
	/*
        bit 1 is a busy flag; loops tightly if clear
        bit 2 is tested in a similar fashion
        bit 4 reflects the page
    */
	UINT16 result = 0xffef | (state->m_blitter_page << 4);
#if (!INSTANT_BLIT)
	if (attotime_compare(space->machine().time(), state->m_blitter_busy_until) < 0)
		result ^= 6;
#endif
	return result;
}


WRITE16_HANDLER( artmagic_blitter_w )
{
	artmagic_state *state = space->machine().driver_data<artmagic_state>();
	COMBINE_DATA(&state->m_blitter_data[offset]);

	/* offset 3 triggers the blit */
	if (offset == 3)
		execute_blit(space->machine());

	/* offset 4 contains the target page */
	else if (offset == 4)
		state->m_blitter_page = (data >> 1) & 1;
}



/*************************************
 *
 *  Video update
 *
 *************************************/

void artmagic_scanline(screen_device &screen, bitmap_t *bitmap, int scanline, const tms34010_display_params *params)
{
	artmagic_state *state = screen.machine().driver_data<artmagic_state>();
	offs_t offset = (params->rowaddr << 12) & 0x7ff000;
	UINT16 *vram = address_to_vram(state, &offset);
	UINT32 *dest = BITMAP_ADDR32(bitmap, scanline, 0);
	const rgb_t *pens = tlc34076_get_pens(screen.machine().device("tlc34076"));
	int coladdr = params->coladdr << 1;
	int x;

	vram += offset;
	for (x = params->heblnk; x < params->hsblnk; x++)
		dest[x] = pens[vram[coladdr++ & 0x1ff] & 0xff];
}