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

#include "emu.h"
#include "includes/macrossp.h"


/*** SCR A LAYER ***/

WRITE32_MEMBER(macrossp_state::macrossp_scra_videoram_w)
{
	COMBINE_DATA(&m_scra_videoram[offset]);

	m_scra_tilemap->mark_tile_dirty(offset);
}


TILE_GET_INFO_MEMBER(macrossp_state::get_macrossp_scra_tile_info)
{
	UINT32 attr, tileno, color;

	attr = m_scra_videoram[tile_index];
	tileno = attr & 0x0000ffff;

	switch (m_scra_videoregs[0] & 0x00000c00)
	{
		case 0x00000800:
			color = (attr & 0x000e0000) >> 15;
			break;

		case 0x00000400:
			color = (attr & 0x003e0000) >> 17;
			break;

		default:
			color = machine().rand() & 7;
			break;
	}

	SET_TILE_INFO_MEMBER(1, tileno, color, TILE_FLIPYX((attr & 0xc0000000) >> 30));
}

/*** SCR B LAYER ***/

WRITE32_MEMBER(macrossp_state::macrossp_scrb_videoram_w)
{
	COMBINE_DATA(&m_scrb_videoram[offset]);

	m_scrb_tilemap->mark_tile_dirty(offset);
}


TILE_GET_INFO_MEMBER(macrossp_state::get_macrossp_scrb_tile_info)
{
	UINT32 attr, tileno, color;

	attr = m_scrb_videoram[tile_index];
	tileno = attr & 0x0000ffff;

	switch (m_scrb_videoregs[0] & 0x00000c00)
	{
		case 0x00000800:
			color = (attr & 0x000e0000) >> 15;
			break;

		case 0x00000400:
			color = (attr & 0x003e0000) >> 17;
			break;

		default:
			color = machine().rand() & 7;
			break;
	}

	SET_TILE_INFO_MEMBER(2, tileno, color, TILE_FLIPYX((attr & 0xc0000000) >> 30));
}

/*** SCR C LAYER ***/

WRITE32_MEMBER(macrossp_state::macrossp_scrc_videoram_w)
{
	COMBINE_DATA(&m_scrc_videoram[offset]);

	m_scrc_tilemap->mark_tile_dirty(offset);
}


TILE_GET_INFO_MEMBER(macrossp_state::get_macrossp_scrc_tile_info)
{
	UINT32 attr, tileno, color;

	attr = m_scrc_videoram[tile_index];
	tileno = attr & 0x0000ffff;

	switch (m_scrc_videoregs[0] & 0x00000c00)
	{
		case 0x00000800:
			color = (attr & 0x000e0000) >> 15;
			break;

		case 0x00000400:
			color = (attr & 0x003e0000) >> 17;
			break;

		default:
			color = machine().rand() & 7;
			break;
	}

	SET_TILE_INFO_MEMBER(3, tileno, color, TILE_FLIPYX((attr & 0xc0000000) >> 30));
}

/*** TEXT LAYER ***/

WRITE32_MEMBER(macrossp_state::macrossp_text_videoram_w)
{
	COMBINE_DATA(&m_text_videoram[offset]);

	m_text_tilemap->mark_tile_dirty(offset);
}


TILE_GET_INFO_MEMBER(macrossp_state::get_macrossp_text_tile_info)
{
	UINT32 tileno, colour;

	tileno = m_text_videoram[tile_index] & 0x0000ffff;
	colour = (m_text_videoram[tile_index] & 0x00fe0000) >> 17;

	SET_TILE_INFO_MEMBER(4, tileno, colour, 0);
}



/*** VIDEO START / UPDATE ***/

void macrossp_state::video_start()
{
	m_spriteram_old = auto_alloc_array_clear(machine(), UINT32, m_spriteram.bytes() / 4);
	m_spriteram_old2 = auto_alloc_array_clear(machine(), UINT32, m_spriteram.bytes() / 4);

	m_text_tilemap = &machine().tilemap().create(tilemap_get_info_delegate(FUNC(macrossp_state::get_macrossp_text_tile_info),this), TILEMAP_SCAN_ROWS, 16, 16, 64, 64);
	m_scra_tilemap = &machine().tilemap().create(tilemap_get_info_delegate(FUNC(macrossp_state::get_macrossp_scra_tile_info),this), TILEMAP_SCAN_ROWS, 16, 16, 64, 64);
	m_scrb_tilemap = &machine().tilemap().create(tilemap_get_info_delegate(FUNC(macrossp_state::get_macrossp_scrb_tile_info),this), TILEMAP_SCAN_ROWS, 16, 16, 64, 64);
	m_scrc_tilemap = &machine().tilemap().create(tilemap_get_info_delegate(FUNC(macrossp_state::get_macrossp_scrc_tile_info),this), TILEMAP_SCAN_ROWS, 16, 16, 64, 64);

	m_text_tilemap->set_transparent_pen(0);
	m_scra_tilemap->set_transparent_pen(0);
	m_scrb_tilemap->set_transparent_pen(0);
	m_scrc_tilemap->set_transparent_pen(0);

	machine().gfx[0]->set_granularity(64);
	machine().gfx[1]->set_granularity(64);
	machine().gfx[2]->set_granularity(64);
	machine().gfx[3]->set_granularity(64);

	save_pointer(NAME(m_spriteram_old), m_spriteram.bytes() / 4);
	save_pointer(NAME(m_spriteram_old2), m_spriteram.bytes() / 4);
}



static void draw_sprites(running_machine &machine, bitmap_rgb32 &bitmap, const rectangle &cliprect, int priority )
{
	macrossp_state *state = machine.driver_data<macrossp_state>();
	gfx_element *gfx = machine.gfx[0];
	//  UINT32 *source = state->m_spriteram;
	UINT32 *source = state->m_spriteram_old2; /* buffers by two frames */
	UINT32 *finish = source + state->m_spriteram.bytes() / 4;

	while (source < finish)
	{
		/*

		 --hh hhyy yyyy yyyy   CCww wwxx xxxx xxxx

		 ---- --zz zzzz zzzz   ---- --ZZ ZZZZ ZZZZ

		 fFa- pp-- cccc c---   tttt tttt tttt tttt

		 */


		int wide = (source[0] & 0x00003c00) >> 10;
		int high = (source[0] & 0x3c000000) >> 26;

		int xpos = (source[0] & 0x000003ff) >> 0;
		int ypos = (source[0] & 0x03ff0000) >> 16;

		int xzoom = (source[1] & 0x000003ff) >> 0; /* 0x100 is zoom factor of 1.0 */
		int yzoom = (source[1] & 0x03ff0000) >> 16;

		int col;
		int tileno = (source[2] & 0x0000ffff) >> 0;

		int flipx = (source[2] & 0x40000000) >> 30;
		int flipy = (source[2] & 0x80000000) >> 31;

		int alpha = (source[2] & 0x20000000)?0x80:0xff; /* alpha blending enable? */

		int loopno = 0;

		int xcnt, ycnt;
		int xoffset, yoffset;

		int pri = (source[2] & 0x0c000000) >> 26;

		if (pri == priority)
		{
			switch (source[0] & 0x0000c000)
			{
				case 0x00008000:
					col = (source[2] & 0x00380000) >> 17;
					break;

				case 0x00004000:
					col = (source[2] & 0x00f80000) >> 19;
					break;

				default:
					col = machine.rand();
					break;
			}


			if (xpos > 0x1ff) xpos -=0x400;
			if (ypos > 0x1ff) ypos -=0x400;

			if (!flipx)
			{
				if (!flipy)
				{
					/* noxflip, noyflip */
					yoffset = 0; /* I'm doing this so rounding errors are cumulative, still looks a touch crappy when multiple sprites used together */
					for (ycnt = 0; ycnt <= high; ycnt++)
					{
						xoffset = 0;
						for (xcnt = 0; xcnt <= wide; xcnt++)
						{
							drawgfxzoom_alpha(bitmap,cliprect,gfx,tileno+loopno,col,flipx,flipy,xpos+xoffset,ypos+yoffset,xzoom*0x100,yzoom*0x100,0,alpha);

							xoffset += ((xzoom*16 + (1<<7)) >> 8);
							loopno++;
						}
						yoffset += ((yzoom*16 + (1<<7)) >> 8);
					}
				}
				else
				{
					/* noxflip, flipy */
					yoffset = ((high * yzoom * 16) >> 8);
					for (ycnt = high; ycnt >= 0; ycnt--)
					{
						xoffset = 0;
						for (xcnt = 0; xcnt <= wide; xcnt++)
						{
							drawgfxzoom_alpha(bitmap,cliprect,gfx,tileno+loopno,col,flipx,flipy,xpos+xoffset,ypos+yoffset,xzoom*0x100,yzoom*0x100,0,alpha);

							xoffset += ((xzoom * 16 + (1 << 7)) >> 8);
							loopno++;
						}
						yoffset -= ((yzoom * 16 + (1 << 7)) >> 8);
					}
				}
			}
			else
			{
				if (!flipy)
				{
					/* xflip, noyflip */
					yoffset = 0;
					for (ycnt = 0; ycnt <= high; ycnt++)
					{
						xoffset = ((wide*xzoom*16) >> 8);
						for (xcnt = wide; xcnt >= 0; xcnt--)
						{
							drawgfxzoom_alpha(bitmap,cliprect,gfx,tileno+loopno,col,flipx,flipy,xpos+xoffset,ypos+yoffset,xzoom*0x100,yzoom*0x100,0,alpha);

							xoffset -= ((xzoom * 16 + (1 << 7)) >> 8);
							loopno++;
						}
						yoffset += ((yzoom * 16 + (1 << 7)) >> 8);
					}
				}
				else
				{
					/* xflip, yflip */
					yoffset = ((high * yzoom * 16) >> 8);
					for (ycnt = high; ycnt >= 0; ycnt--)
					{
						xoffset = ((wide * xzoom * 16) >> 8);
						for (xcnt = wide; xcnt >=0 ; xcnt--)
						{
							drawgfxzoom_alpha(bitmap,cliprect,gfx,tileno+loopno,col,flipx,flipy,xpos+xoffset,ypos+yoffset,xzoom*0x100,yzoom*0x100,0,alpha);

							xoffset -= ((xzoom * 16 + (1 << 7)) >> 8);
							loopno++;
						}
						yoffset -= ((yzoom * 16 + (1 << 7)) >> 8);
					}
				}
			}
		}
		source += 3;
	}
}


static void draw_layer( running_machine &machine, bitmap_rgb32 &bitmap, const rectangle &cliprect, int layer )
{
	macrossp_state *state = machine.driver_data<macrossp_state>();
	tilemap_t *tm;
	UINT32 *vr;

	switch (layer)
	{
		case 0:
		default:
			tm = state->m_scra_tilemap;
			vr = state->m_scra_videoregs;
			break;

		case 1:
			tm = state->m_scrb_tilemap;
			vr = state->m_scrb_videoregs;
			break;

		case 2:
			tm = state->m_scrc_tilemap;
			vr = state->m_scrc_videoregs;
			break;
	}

	if ((vr[2] & 0xf0000000) == 0xe0000000) /* zoom enable (guess, surely wrong) */
	{
		int startx, starty, inc;

		startx = (vr[1] & 0x0000ffff) << 16;
		starty = (vr[1] & 0xffff0000) >> 0;
		inc = (vr[2] & 0x00ff0000) >> 6,

		/* WRONG! */
		/* scroll register contain position relative to the center of the screen, so adjust */
		startx -= (368/2) * inc;
		starty -= (240/2) * inc;

		tm->draw_roz(bitmap, cliprect,
				startx,starty,inc,0,0,inc,
				1,  /* wraparound */
				0,0);
	}
	else
	{
		tm->set_scrollx(0, ((vr[0] & 0x000003ff) >> 0 ) );
		tm->set_scrolly(0, ((vr[0] & 0x03ff0000) >> 16) );
		tm->draw(bitmap, cliprect, 0, 0);
	}
}

/* useful function to sort the three tile layers by priority order */
static void sortlayers(int *layer,int *pri)
{
#define SWAP(a,b) \
	if (pri[a] >= pri[b]) \
	{ \
		int t; \
		t = pri[a]; pri[a] = pri[b]; pri[b] = t; \
		t = layer[a]; layer[a] = layer[b]; layer[b] = t; \
	}

	SWAP(0,1)
	SWAP(0,2)
	SWAP(1,2)
}

UINT32 macrossp_state::screen_update_macrossp(screen_device &screen, bitmap_rgb32 &bitmap, const rectangle &cliprect)
{
	int layers[3],layerpri[3];

	bitmap.fill(get_black_pen(machine()), cliprect);

	layers[0] = 0;
	layerpri[0] = (m_scra_videoregs[0] & 0x0000c000) >> 14;
	layers[1] = 1;
	layerpri[1] = (m_scrb_videoregs[0] & 0x0000c000) >> 14;
	layers[2] = 2;
	layerpri[2] = (m_scrc_videoregs[0] & 0x0000c000) >> 14;

	sortlayers(layers, layerpri);

	draw_layer(machine(), bitmap, cliprect, layers[0]);
	draw_sprites(machine(), bitmap, cliprect, 0);
	draw_layer(machine(), bitmap, cliprect, layers[1]);
	draw_sprites(machine(), bitmap, cliprect, 1);
	draw_layer(machine(), bitmap, cliprect, layers[2]);
	draw_sprites(machine(), bitmap, cliprect, 2);
	draw_sprites(machine(), bitmap, cliprect, 3);
	m_text_tilemap->draw(bitmap, cliprect, 0, 0);

#if 0
popmessage  ("scra - %08x %08x %08x\nscrb - %08x %08x %08x\nscrc - %08x %08x %08x",
m_scra_videoregs[0]&0xffff33ff, // yyyyxxxx
m_scra_videoregs[1], // ??? more scrolling?
m_scra_videoregs[2], // 08 - 0b

m_scrb_videoregs[0]&0xffff33ff, // 00 - 03
m_scrb_videoregs[1], // 04 - 07
m_scrb_videoregs[2], // 08 - 0b

m_scrc_videoregs[0]&0xffff33ff, // 00 - 03
m_scrc_videoregs[1], // 04 - 07
m_scrc_videoregs[2]);// 08 - 0b
#endif
	return 0;
}

void macrossp_state::screen_eof_macrossp(screen_device &screen, bool state)
{
	// rising edge
	if (state)
	{
		/* looks like sprites are *two* frames ahead, like nmk16 */
		memcpy(m_spriteram_old2, m_spriteram_old, m_spriteram.bytes());
		memcpy(m_spriteram_old, m_spriteram, m_spriteram.bytes());
	}
}
0, 0x1a0000, 0x3944, 0, 0x1c0000, 0x655a, 0, 0x1d0000, 0xf513, 0, 0x1e0000, 0xb957, 0, 0, 0x37ca, 0, 0, 0xffffffff, 0xffffffff, 0xffffffff } }, { "pstone2", 0, { -2, 0x14db3f4, 0x000000, -2, 0xfbd0179d, 0x010000, -2, 0x9827117, 0x020000, -2, 0x69358f, 0x030000, -2, 0x193954e, 0x040000, -2, 0xba50eb, 0x050000, -2, 0x9f1523, 0x060000, -2, 0xcb7b03, 0x070000, -2, 0x8f712b, 0x080000, -2, 0x120f246, 0x090000, -2, 0xacc9fc, 0x0a0000, -2, 0x4eb319, 0x0b0000, -2, 0x19d0c41, 0x0c0000, -2, 0x1077853, 0x0d0000, -2, 0x100019d, 0x0e0000, -2, 0xfd91596b, 0x0f0000, -2, 0x63bae7, 0x100000, -2, 0x3e3685, 0x110000, -2, 0x6d08a9, 0x120000, -2, 0xfff85c5d, 0x130000, -2, 0x5263bf, 0x140000, -2, 0x396180, 0x150000, -2, 0x73af6c, 0x160000, -2, 0xfffa8a76, 0x170000, -2, 0xc2d9e0, 0x180000, -2, 0x33be72, 0x190000, 0xffffffff, 0xffffffff, 0xffffffff } }, { "toyfight", 0,{ 0x0615, 0, 0x8f058, 0x1999, 0, 0x8ec58, 0x7510, 0, 0x8f458, 0x5736, 0, 0x8e858, 0xffffffff, 0xffffffff, 0xffffffff } }, { "ggx", 0,{ -1, 0x200000, 0x100000, -1, 0x210004, 0x110000, -1, 0x220008, 0x120000, -1, 0x228000, 0x130000, 0x3af9, 0, 0x000000, 0x2288, 0, 0x010000, 0xe5e6, 0, 0x020000, 0xebb0, 0, 0x030000, 0x0228, 0, 0x040000, 0x872c, 0, 0x050000, 0xbba0, 0, 0x060000, 0x772f, 0, 0x070000, 0x2924, 0, 0x080000, 0x3222, 0, 0x090000, 0x7954, 0, 0x0a0000, 0x5acd, 0, 0x0b0000, 0xdd19, 0, 0x0c0000, 0x2428, 0, 0x0d0000, 0x3329, 0, 0x0e0000, 0x2142, 0, 0x0f0000, 0xffffffff, 0xffffffff, 0xffffffff } }, { "crzytaxi", 0,{ 0x0219, 0, 0, 0xffffffff, 0xffffffff, 0xffffffff } }, { "jambo", 0,{ 0x0223, 0, 0, 0xffffffff, 0xffffffff, 0xffffffff } }, { "18wheelr", 0,{ 0x1502, 0, 0, 0xffffffff, 0xffffffff, 0xffffffff } }, }; /*************************************************************************** INLINE FUNCTIONS ***************************************************************************/ /*------------------------------------------------- get_safe_token - makes sure that the passed in device is, in fact, a naomibd device -------------------------------------------------*/ INLINE naomibd_state *get_safe_token(const device_config *device) { assert(device != NULL); assert(device->token != NULL); assert(device->type == NAOMI_BOARD); return (naomibd_state *)device->token; } /************************************* * * Misc. functions * *************************************/ int naomibd_interrupt_callback(const device_config *device, naomibd_interrupt_func callback) { naomibd_config *config = (naomibd_config *)device->inline_config; //naomibd_state *v = get_safe_token(device); config->interrupt = callback; return 0; } int naomibd_get_type(const device_config *device) { naomibd_state *v = get_safe_token(device); return v->type; } /************************************* * * Common initialization * *************************************/ static STATE_POSTLOAD( naomibd_postload ) { //naomibd_state *v = param; } static void init_save_state(const device_config *device) { naomibd_state *v = get_safe_token(device); state_save_register_postload(device->machine, naomibd_postload, v); /* register states */ state_save_register_device_item(device, 0, v->rom_offset); state_save_register_device_item(device, 0, v->rom_offset_flags); state_save_register_device_item(device, 0, v->dma_count); state_save_register_device_item(device, 0, v->dma_offset); state_save_register_device_item(device, 0, v->dma_offset_flags); state_save_register_device_item(device, 0, v->prot_offset); state_save_register_device_item(device, 0, v->prot_key); state_save_register_device_item(device, 0, v->aw_offset); state_save_register_device_item(device, 0, v->aw_file_base); state_save_register_device_item(device, 0, v->aw_file_offset); } /************************************* * * Reset * *************************************/ static void soft_reset(naomibd_state *v) { v->prot_sum = 0; } /************************************* * * Handlers * *************************************/ READ64_DEVICE_HANDLER( naomibd_r ) { naomibd_state *v = get_safe_token(device); UINT8 *ROM = (UINT8 *)v->memory; // AW board is different, shouldn't ever be read if (v->type == AW_ROM_BOARD) { mame_printf_debug("AW_ROM_BOARD read @ %x mask %" I64FMT "x\n", offset, mem_mask); return U64(0xffffffffffffffff); } // ROM_DATA if ((offset == 1) && ACCESSING_BITS_0_15) { UINT64 ret = 0; if (v->rom_offset_flags & NAOMIBD_FLAG_SPECIAL_MODE) { if (v->rom_offset == 0x1fffe) { UINT8 *prot = (UINT8 *)v->protdata; UINT32 byte_offset = v->prot_offset*2; // this is a good time to clear the prot_sum v->prot_sum = 0; if (v->prot_translate == NULL) { #if NAOMIBD_PRINTF_PROTECTION v->prot_pio_count += 2; printf("naomibd: reading protection data, but none was supplied (now %x bytes)\n", v->prot_pio_count); #endif return 0; } #if NAOMIBD_PRINTF_PROTECTION v->prot_pio_count += 2; printf("naomibd: PIO read count %x\n", v->prot_pio_count); #endif if (v->prot_reverse_bytes) { ret = (UINT64)(prot[byte_offset+1] | (prot[byte_offset]<<8)); } else { ret = (UINT64)(prot[byte_offset] | (prot[byte_offset+1]<<8)); } v->prot_offset++; } #if NAOMIBD_PRINTF_PROTECTION else { printf("Bad protection offset read %x\n", v->rom_offset); } #endif } else { ret = (UINT64)(ROM[v->rom_offset] | (ROM[v->rom_offset+1]<<8)); } if (v->rom_offset_flags & NAOMIBD_FLAG_AUTO_ADVANCE) { v->rom_offset += 2; } return ret; } else if ((offset == 7) && ACCESSING_BITS_32_47) { // 5f703c mame_printf_verbose("ROM: read 5f703c\n"); return (UINT64)0xffff << 32; } else if ((offset == 8) && ACCESSING_BITS_0_15) { // 5f7040 mame_printf_verbose("ROM: read 5f7040\n"); return 0; } else if ((offset == 8) && ACCESSING_BITS_32_47) { // 5f7044 mame_printf_verbose("ROM: read 5f7044\n"); return 0; } else if ((offset == 9) && ACCESSING_BITS_0_15) { // 5f7048 mame_printf_verbose("ROM: read 5f7048\n"); return 0; } else if ((offset == 9) && ACCESSING_BITS_32_47) { // 5f704c mame_printf_verbose("ROM: read 5f704c\n"); return (UINT64)1 << 32; } else if ((offset == 15) && ACCESSING_BITS_32_47) // boardid read { UINT64 ret; ret = x76f100_sda_read( device->machine, 0 ) << 15; return ret << 32; } else { //mame_printf_verbose("%s:ROM: read mask %" I64FMT "x @ %x\n", cpuexec_describe_context(machine), mem_mask, offset); } return U64(0xffffffffffffffff); } WRITE64_DEVICE_HANDLER( naomibd_w ) { naomibd_state *v = get_safe_token(device); INT32 i; // AW board if (v->type == AW_ROM_BOARD) { //printf("AW: %" I64FMT "x to ROM board @ %x (mask %" I64FMT "x)\n", data, offset, mem_mask); switch (offset) { case 0: { if(ACCESSING_BITS_0_15) { // EPR_OFFSETL v->aw_offset &= 0xffff0000; v->aw_offset |= (data & 0xffff); v->dma_offset = v->aw_offset*2; //printf("EPR_OFFSETL = %x, dma_offset %x\n", (UINT32)data, v->dma_offset); } else if(ACCESSING_BITS_32_47 || ACCESSING_BITS_32_63) { // EPR_OFFSETH v->aw_offset &= 0xffff; v->aw_offset |= ((data>>16) & 0xffff0000); v->dma_offset = v->aw_offset*2; v->dma_offset_flags = NAOMIBD_FLAG_ADDRESS_SHUFFLE|NAOMIBD_FLAG_AUTO_ADVANCE; // force normal DMA mode //printf("EPR_OFFSETH = %x, dma_offset %x\n", (UINT32)(data>>32), v->dma_offset); } } break; case 1: { if(ACCESSING_BITS_32_47 || ACCESSING_BITS_32_63) { // MPR_RECORD_INDEX //printf("%x to RECORD_INDEX\n", (UINT32)(data>>32)); v->dma_offset = 0x1000000 + (0x40 * (data>>32)); } } break; case 2: { if(ACCESSING_BITS_0_15) { UINT8 *ROM = (UINT8 *)v->memory; UINT32 base; // MPR_FIRST_FILE_INDEX (usually 3) base = data * 64; v->aw_file_base = ROM[0x100000b+base]<<24 | ROM[0x100000a+base]<<16 | ROM[0x1000009+base]<<8 | ROM[0x1000008+base]; v->aw_file_base += 0x1000000; //printf("%x to FIRST_FILE_INDEX, file_base = %x\n", (UINT32)data, v->aw_file_base); } else if(ACCESSING_BITS_32_47 || ACCESSING_BITS_32_63) { // MPR_FILE_OFFSETL v->aw_file_offset &= 0xffff0000; v->aw_file_offset |= (data>>32) & 0xffff; v->dma_offset = v->aw_file_base + (v->aw_file_offset*2); //printf("%x to FILE_OFFSETL, file_offset %x, dma_offset %x\n", (UINT32)(data>>32), v->aw_file_offset, v->dma_offset); } } break; case 3: { if(ACCESSING_BITS_0_15) { // MPR_FILE_OFFSETH v->aw_file_offset &= 0xffff; v->aw_file_offset |= (data & 0xffff)<<16; v->dma_offset = v->aw_file_base + (v->aw_file_offset*2); //printf("%x to FILE_OFFSETH, file_offset %x, dma_offset %x\n", (UINT32)data, v->aw_file_offset, v->dma_offset); } } break; default: logerror("AW: unhandled %" I64FMT "x to ROM board @ %x (mask %" I64FMT "x)\n", data, offset, mem_mask); break; } return; } switch(offset) { case 0: { if(ACCESSING_BITS_0_15) { // ROM_OFFSETH v->rom_offset &= 0xffff; v->rom_offset |= (data & 0x1fff)<<16; v->rom_offset_flags = data >> 12; } if(ACCESSING_BITS_32_47) { // ROM_OFFSETL v->rom_offset &= 0xffff0000; v->rom_offset |= ((data >> 32) & 0xffff); } #if NAOMIBD_PRINTF_PROTECTION printf("PIO: offset to %x\n", v->rom_offset); #endif } break; case 1: { if(ACCESSING_BITS_32_47 || ACCESSING_BITS_32_63) { // DMA_OFFSETH v->dma_offset &= 0xffff; v->dma_offset |= (data >> 16) & 0x1fff0000; v->dma_offset_flags = (data>>28); } if(ACCESSING_BITS_0_15) { // ROM_DATA - used to access registers in the protection chip switch (v->rom_offset) { case 0x1fff8: // offset low v->prot_offset &= 0xffff0000; v->prot_offset |= (UINT32)data; break; case 0x1fffa: // offset high v->prot_offset &= 0xffff; v->prot_offset |= (UINT32)data<<16; break; case 0x1fffc: // decryption key v->prot_key = data; #if NAOMIBD_PRINTF_PROTECTION printf("Protection: set up read @ %x, key %x sum %x (PIO %x DMA %x) [%s]\n", v->prot_offset*2, v->prot_key, v->prot_sum, v->rom_offset, v->dma_offset, cpuexec_describe_context(device->machine)); v->prot_pio_count = 0; #endif // translate address if necessary if (v->prot_translate != NULL) { i = 0; while (v->prot_translate[i+1] != 0xffffffff) { // should we match by key, address, or sum? if (v->prot_translate[i] == -2) // match sum { if (v->prot_translate[i+1] == v->prot_sum) { #if NAOMIBD_PRINTF_PROTECTION printf("Protection: got sum %x, translated to %x\n", v->prot_sum, v->prot_translate[i+2]); #endif v->prot_offset = v->prot_translate[i+2]/2; break; } else { i+= 3; } } else if (v->prot_translate[i] == -1) // match address { if (v->prot_translate[i+1] == (v->prot_offset*2)) { #if NAOMIBD_PRINTF_PROTECTION printf("Protection: got offset %x, translated to %x\n", v->prot_offset, v->prot_translate[i+2]); #endif v->prot_offset = v->prot_translate[i+2]/2; break; } else { i += 3; } } else // match key { if (v->prot_translate[i] == v->prot_key) { #if NAOMIBD_PRINTF_PROTECTION printf("Protection: got key %x, translated to %x\n", v->prot_key, v->prot_translate[i+2]); #endif v->prot_offset = v->prot_translate[i+2]/2; break; } else { i+= 3; } } } } #if NAOMIBD_PRINTF_PROTECTION else { printf("naomibd: protection not handled for this game\n"); } #endif break; case 0x2000000: case 0x2020000: #if NAOMIBD_PRINTF_PROTECTION printf("Protection write %04x to upload\n", (UINT32)(data&0xffff)); #endif v->prot_sum += (INT16)(data&0xffff); break; default: #if NAOMIBD_PRINTF_PROTECTION printf("naomibd: unknown protection write %x @ %x\n", (UINT32)data, v->rom_offset); #endif break; } } } break; case 2: { if(ACCESSING_BITS_0_15) { // DMA_OFFSETL v->dma_offset &= 0xffff0000; v->dma_offset |= (data & 0xffff); } if(ACCESSING_BITS_32_63) { // NAOMI_DMA_COUNT v->dma_count = data >> 32; } } break; case 7: { if(ACCESSING_BITS_32_47) mame_printf_verbose("ROM: write 5f703c\n"); } break; case 8: { if(ACCESSING_BITS_0_15) mame_printf_verbose("ROM: write 5f7040\n"); if(ACCESSING_BITS_32_47) mame_printf_verbose("ROM: write 5f7044\n"); } break; case 9: { if(ACCESSING_BITS_0_15) mame_printf_verbose("ROM: write 5f7048\n"); if(ACCESSING_BITS_32_47) mame_printf_verbose("ROM: write 5f704c\n"); } break; case 15: { if(ACCESSING_BITS_0_15) { running_machine *machine = device->machine; // NAOMI_BOARDID_WRITE x76f100_cs_write(machine, 0, (data >> 2) & 1 ); x76f100_rst_write(machine, 0, (data >> 3) & 1 ); x76f100_scl_write(machine, 0, (data >> 1) & 1 ); x76f100_sda_write(machine, 0, (data >> 0) & 1 ); } } break; default: mame_printf_verbose("%s: ROM: write %" I64FMT "x to %x, mask %" I64FMT "x\n", cpuexec_describe_context(device->machine), data, offset, mem_mask); break; } } /************************************* * * Load rom file from gdrom * *************************************/ #define FILENAME_LENGTH 16 static void load_rom_gdrom(running_machine* machine, naomibd_state *v) { UINT32 result; cdrom_file *gdromfile; UINT8 buffer[2048]; UINT8 *ptr; UINT32 start,size,sectors,dir; int pos,len,a; char name[128]; UINT64 key; memset(name,'\0',128); memcpy(name, v->picdata+33, 7); memcpy(name+7, v->picdata+25, 7); gdromfile = cdrom_open(v->gdromchd); // primary volume descriptor // read frame 0xb06e (frame=sector+150) // dimm board firmware starts straight from this frame result = cdrom_read_data(gdromfile, 0xb06e - 150, buffer, CD_TRACK_MODE1); start=((buffer[0x8c+0] << 0) | (buffer[0x8c+1] << 8) | (buffer[0x8c+2] << 16) | (buffer[0x8c+3] << 24)); // path table result = cdrom_read_data(gdromfile, start, buffer, CD_TRACK_MODE1); start=((buffer[0x2+0] << 0) | (buffer[0x2+1] << 8) | (buffer[0x2+2] << 16) | (buffer[0x2+3] << 24)); dir = start; // directory result = cdrom_read_data(gdromfile, dir, buffer, CD_TRACK_MODE1); // find data of file start = 0; size = 0; printf("Looking for file %s\n", name); for (pos = 0;pos < 2048;pos += buffer[pos]) { a=0; if (!(buffer[pos+25] & 2)) { len=buffer[pos+32]; for (a=0;a < FILENAME_LENGTH;a++) { if ((buffer[pos+33+a] == ';') && (name[a] == 0)) { a=FILENAME_LENGTH+1; break; } if (buffer[pos+33+a] != name[a]) break; if (a == len) { if (name[a] == 0) a = FILENAME_LENGTH+1; else a = FILENAME_LENGTH; } } } if (a == FILENAME_LENGTH+1) { // start sector and size of file start=((buffer[pos+2] << 0) | (buffer[pos+3] << 8) | (buffer[pos+4] << 16) | (buffer[pos+5] << 24)); size =((buffer[pos+10] << 0) | (buffer[pos+11] << 8) | (buffer[pos+12] << 16) | (buffer[pos+13] << 24)); printf("start %08x size %08x\n", start,size); break; } if (buffer[pos] == 0) break; } if ((start != 0) && (size == 0x100)) { // read file result = cdrom_read_data(gdromfile, start, buffer, CD_TRACK_MODE1); // get "rom" file name memset(name,'\0', 128); memcpy(name, buffer+0xc0, FILENAME_LENGTH-1); // directory result = cdrom_read_data(gdromfile, dir, buffer, CD_TRACK_MODE1); // find data of "rom" file start = 0; size = 0; printf("Looking for file %s\n", name); for (pos = 0;pos < 2048;pos += buffer[pos]) { a = 0; if (!(buffer[pos+25] & 2)) { len = buffer[pos+32]; for (a=0;a < FILENAME_LENGTH;a++) { if ((buffer[pos+33+a] == ';') && (name[a] == 0)) { a=FILENAME_LENGTH+1; break; } if (buffer[pos+33+a] != name[a]) break; if (a == len) { if (name[a] == 0) a = (FILENAME_LENGTH+1); else a = FILENAME_LENGTH; } } } if (a == (FILENAME_LENGTH+1)) { // start sector and size of file start=((buffer[pos+2] << 0) | (buffer[pos+3] << 8) | (buffer[pos+4] << 16) | (buffer[pos+5] << 24)); size =((buffer[pos+10] << 0) | (buffer[pos+11] << 8) | (buffer[pos+12] << 16) | (buffer[pos+13] << 24)); printf("start %08x size %08x\n", start,size); break; } if (buffer[pos] == 0) break; } if (start != 0) { // read encrypted data into memory ptr = v->memory; sectors = (size+2047)/2048; while (sectors > 0) { result = cdrom_read_data(gdromfile, start, ptr, CD_TRACK_MODE1); ptr += 2048; start++; sectors--; } } } // get des key key =(((UINT64)v->picdata[0x31] << 56) | ((UINT64)v->picdata[0x32] << 48) | ((UINT64)v->picdata[0x33] << 40) | ((UINT64)v->picdata[0x34] << 32) | ((UINT64)v->picdata[0x35] << 24) | ((UINT64)v->picdata[0x36] << 16) | ((UINT64)v->picdata[0x37] << 8) | ((UINT64)v->picdata[0x29] << 0)); printf("key is %08x%08x\n", (UINT32)((key & 0xffffffff00000000ULL)>>32), (UINT32)(key & 0x00000000ffffffffULL)); // decrypt loaded data naomi_game_decrypt(machine, key, v->memory, size); cdrom_close(gdromfile); } /*************************************************************************** DEVICE INTERFACE ***************************************************************************/ /*------------------------------------------------- device start callback -------------------------------------------------*/ static DEVICE_START( naomibd ) { const naomibd_config *config = (const naomibd_config *)device->inline_config; naomibd_state *v = get_safe_token(device); int i; /* validate some basic stuff */ assert(device->static_config == NULL); assert(device->inline_config != NULL); assert(device->machine != NULL); assert(device->machine->config != NULL); /* validate configuration */ assert(config->type >= ROM_BOARD && config->type < MAX_NAOMIBD_TYPES); /* store a pointer back to the device */ v->device = device; /* find the protection address translation for this game */ v->prot_translate = (UINT32 *)0; #if NAOMIBD_PRINTF_PROTECTION v->prot_pio_count = 0; #endif for (i=0; i<ARRAY_LENGTH(naomibd_translate_tbl); i++) { if (!strcmp(device->machine->gamedrv->name, naomibd_translate_tbl[i].name)) { v->prot_translate = &naomibd_translate_tbl[i].transtbl[0]; v->prot_reverse_bytes = naomibd_translate_tbl[i].reverse_bytes; break; } } /* configure type-specific values */ switch (config->type) { case ROM_BOARD: v->memory = (UINT8 *)memory_region(device->machine, config->regiontag); v->protdata = (UINT8 *)memory_region(device->machine, "naomibd_prot"); break; case AW_ROM_BOARD: v->memory = (UINT8 *)memory_region(device->machine, config->regiontag); break; case DIMM_BOARD: v->memory = (UINT8 *)memory_region(device->machine, config->regiontag); v->gdromchd = get_disk_handle(device->machine, config->gdromregiontag); v->picdata = (UINT8 *)memory_region(device->machine, config->picregiontag); load_rom_gdrom(device->machine, v); break; default: fatalerror("Unsupported plug-in board in naomibd_start!"); break; } /* set the type */ v->index = device_list_index(device->machine->config->devicelist, device->type, device->tag); v->type = config->type; /* initialize some registers */ v->rom_offset = 0; v->rom_offset_flags = 0; v->dma_count = 0; v->dma_offset = 0; v->dma_offset_flags = 0; v->prot_offset = 0; /* do a soft reset to reset everything else */ soft_reset(v); /* register for save states */ init_save_state(device); } /*------------------------------------------------- device exit callback -------------------------------------------------*/ static DEVICE_STOP( naomibd ) { //naomibd_state *v = get_safe_token(device); } /*------------------------------------------------- device reset callback -------------------------------------------------*/ static DEVICE_RESET( naomibd ) { naomibd_state *v = get_safe_token(device); soft_reset(v); } /*------------------------------------------------- device nvram callback -------------------------------------------------*/ static DEVICE_NVRAM( naomibd ) { //naomibd_state *v = get_safe_token(device); static const UINT8 eeprom_romboard[20+48] = { 0x19,0x00,0xaa,0x55,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x69,0x79,0x68,0x6b,0x74,0x6d,0x68,0x6d, 0xa1,0x09,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20, 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20, 0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30 }; UINT8 *games_contents; if (read_or_write) /*eeprom_save(file)*/; else { /*if (file) eeprom_load(file); else*/ games_contents = memory_region(device->machine, "naomibd_eeprom"); if (games_contents) { x76f100_init( device->machine, 0, games_contents ); } else { UINT8 *eeprom = auto_alloc_array_clear(device->machine, UINT8, 0x84); memcpy(eeprom, eeprom_romboard, sizeof(eeprom_romboard)); x76f100_init( device->machine, 0, eeprom ); } } } /*------------------------------------------------- device get info callback -------------------------------------------------*/ DEVICE_GET_INFO( naomibd ) { const naomibd_config *config = (device != NULL) ? (const naomibd_config *)device->inline_config : NULL; switch (state) { /* --- the following bits of info are returned as 64-bit signed integers --- */ case DEVINFO_INT_TOKEN_BYTES: info->i = sizeof(naomibd_state); break; case DEVINFO_INT_INLINE_CONFIG_BYTES: info->i = sizeof(naomibd_config); break; case DEVINFO_INT_CLASS: info->i = DEVICE_CLASS_PERIPHERAL; break; case DEVINFO_INT_DMAOFFSET: #if NAOMIBD_PRINTF_PROTECTION printf("DMA source %08x, flags %x\n", get_safe_token(device)->dma_offset, get_safe_token(device)->dma_offset_flags); #endif // if the flag is cleared that lets the protection chip go, // we need to handle this specially. but not on DIMM boards. if (!(get_safe_token(device)->dma_offset_flags & NAOMIBD_FLAG_ADDRESS_SHUFFLE) && (get_safe_token(device)->type == ROM_BOARD)) { if (!strcmp(device->machine->gamedrv->name, "qmegamis")) { info->i = 0x9000000; break; } else if (!strcmp(device->machine->gamedrv->name, "mvsc2")) { switch (get_safe_token(device)->dma_offset) { case 0x08000000: info->i = 0x8800000; break; case 0x08026440: info->i = 0x8830000; break; case 0x0803bda0: info->i = 0x8850000; break; case 0x0805a560: info->i = 0x8870000; break; case 0x0805b720: info->i = 0x8880000; break; case 0x0808b7e0: info->i = 0x88a0000; break; default: info->i = get_safe_token(device)->dma_offset; break; } return; } else { logerror("Protected DMA not handled for this game (dma_offset %x)\n", get_safe_token(device)->dma_offset); } } info->i = get_safe_token(device)->dma_offset; break; /* --- the following bits of info are returned as pointers --- */ case DEVINFO_PTR_ROM_REGION: info->romregion = NULL; break; case DEVINFO_PTR_MACHINE_CONFIG: info->machine_config = NULL; break; case DEVINFO_PTR_MEMORY: info->p = get_safe_token(device)->memory; break; /* --- the following bits of info are returned as pointers to data or functions --- */ case DEVINFO_FCT_START: info->start = DEVICE_START_NAME(naomibd); break; case DEVINFO_FCT_STOP: info->stop = DEVICE_STOP_NAME(naomibd); break; case DEVINFO_FCT_RESET: info->reset = DEVICE_RESET_NAME(naomibd); break; case DEVINFO_FCT_NVRAM: info->nvram = DEVICE_NVRAM_NAME(naomibd); break; /* --- the following bits of info are returned as NULL-terminated strings --- */ case DEVINFO_STR_NAME: switch (config->type) { default: case ROM_BOARD: strcpy(info->s, "Naomi Rom Board"); break; case AW_ROM_BOARD: strcpy(info->s, "Atomiswave Rom Board"); break; case DIMM_BOARD: strcpy(info->s, "Naomi Dimm Board"); break; } break; case DEVINFO_STR_FAMILY: strcpy(info->s, "Naomi/Atomiswave plug-in board"); break; case DEVINFO_STR_VERSION: strcpy(info->s, "1.1"); break; case DEVINFO_STR_SOURCE_FILE: strcpy(info->s, __FILE__); break; case DEVINFO_STR_CREDITS: strcpy(info->s, "Copyright Nicola Salmoria and the MAME Team"); break; } }