1 // FinalBurn Neo Goori Goori driver module
2 // Based on MAME driver by David Haywood
3 
4 #include "tiles_generic.h"
5 #include "m68000_intf.h"
6 #include "burn_ym2151.h"
7 #include "msm6295.h"
8 #include "eeprom.h"
9 #include "burn_pal.h"
10 
11 static UINT8 *AllMem;
12 static UINT8 *AllRam;
13 static UINT8 *RamEnd;
14 static UINT8 *MemEnd;
15 static UINT8 *Drv68KROM;
16 static UINT8 *DrvGfxROM[2];
17 static UINT8 *DrvSndROM;
18 static UINT8 *Drv68KRAM;
19 static UINT8 *DrvBgRAM;
20 static UINT8 *DrvSprRAM;
21 
22 static UINT8 DrvRecalc;
23 
24 static UINT8 DrvJoy1[16];
25 static UINT8 DrvJoy2[16];
26 static UINT8 DrvJoy3[16];
27 static UINT8 DrvReset;
28 static UINT8 DrvInputs[3];
29 
30 static struct BurnInputInfo GooriInputList[] = {
31 	{"P1 Coin",			BIT_DIGITAL,	DrvJoy3 + 2,	"p1 coin"		},
32 	{"P1 Start",		BIT_DIGITAL,	DrvJoy3 + 0,	"p1 start"		},
33 	{"P1 Up",			BIT_DIGITAL,	DrvJoy1 + 0,	"p1 up"			},
34 	{"P1 Down",			BIT_DIGITAL,	DrvJoy1 + 1,	"p1 down"		},
35 	{"P1 Left",			BIT_DIGITAL,	DrvJoy1 + 2,	"p1 left"		},
36 	{"P1 Right",		BIT_DIGITAL,	DrvJoy1 + 3,	"p1 right"		},
37 	{"P1 Button 1",		BIT_DIGITAL,	DrvJoy1 + 4,	"p1 fire 1"		},
38 
39 	{"P2 Coin",			BIT_DIGITAL,	DrvJoy3 + 3,	"p2 coin"		},
40 	{"P2 Start",		BIT_DIGITAL,	DrvJoy3 + 1,	"p2 start"		},
41 	{"P2 Up",			BIT_DIGITAL,	DrvJoy2 + 0,	"p2 up"			},
42 	{"P2 Down",			BIT_DIGITAL,	DrvJoy2 + 1,	"p2 down"		},
43 	{"P2 Left",			BIT_DIGITAL,	DrvJoy2 + 2,	"p2 left"		},
44 	{"P2 Right",		BIT_DIGITAL,	DrvJoy2 + 3,	"p2 right"		},
45 	{"P2 Button 1",		BIT_DIGITAL,	DrvJoy2 + 4,	"p2 fire 1"		},
46 
47 	{"Reset",			BIT_DIGITAL,	&DrvReset,		"reset"			},
48 	{"Service",			BIT_DIGITAL,	DrvJoy3 + 6,	"service"		},
49 	{"Service Mode",	BIT_DIGITAL,	DrvJoy3 + 5,	"diag"			},
50 };
51 
STDINPUTINFO(Goori)52 STDINPUTINFO(Goori)
53 
54 static void __fastcall goori_write_byte(UINT32 address, UINT8 data)
55 {
56 	switch (address)
57 	{
58 		case 0x300000:
59 		case 0x300002:
60 			BurnYM2151Write((address / 2) & 1, data);
61 		return;
62 
63 		case 0x300004:
64 			MSM6295Write(0, data & 0xff);
65 		return;
66 
67 		case 0x300008:
68 			// ?
69 		return;
70 
71 		case 0x30000f:
72 			EEPROMWriteBit((data & 0x0004) >> 2);
73 			EEPROMSetCSLine((~data & 0x0001) ? EEPROM_ASSERT_LINE : EEPROM_CLEAR_LINE);
74 			EEPROMSetClockLine((data & 0x0002) ? EEPROM_ASSERT_LINE : EEPROM_CLEAR_LINE);
75 		return;
76 	}
77 }
78 
goori_read_byte(UINT32 address)79 static UINT8 __fastcall goori_read_byte(UINT32 address)
80 {
81 	switch (address)
82 	{
83 		case 0x300002:
84 			return BurnYM2151Read();
85 
86 		case 0x300004:
87 			return MSM6295Read(0);
88 
89 		case 0x500000:
90 			return DrvInputs[0];
91 
92 		case 0x500002:
93 			return DrvInputs[1];
94 
95 		case 0x500004:
96 			return (DrvInputs[2] & ~0x80) | ((EEPROMRead()) ? 0x80 : 0x00);
97 	}
98 
99 	return 0;
100 }
101 
tilemap_callback(bg)102 static tilemap_callback( bg )
103 {
104 	TILE_SET_INFO(1, (DrvBgRAM[offs * 4 + 1] << 8) + DrvBgRAM[offs * 4 + 3], 0, 0);
105 }
106 
DrvDoReset()107 static INT32 DrvDoReset()
108 {
109 	memset (AllRam, 0, RamEnd - AllRam);
110 
111 	SekOpen(0);
112 	SekReset();
113 	SekClose();
114 
115 	EEPROMReset();
116 
117 	MSM6295Reset(0);
118 	BurnYM2151Reset();
119 
120 	return 0;
121 }
122 
MemIndex()123 static INT32 MemIndex()
124 {
125 	UINT8 *Next; Next = AllMem;
126 
127 	Drv68KROM		= Next; Next += 0x100000;
128 
129 	DrvGfxROM[0]	= Next; Next += 0x400000;
130 	DrvGfxROM[1]	= Next; Next += 0x400000;
131 
132 	MSM6295ROM		= Next;
133 	DrvSndROM		= Next; Next += 0x040000;
134 
135 	BurnPalette		= (UINT32*)Next; Next += 0x2000 * sizeof(UINT32);
136 
137 	AllRam			= Next;
138 
139 	Drv68KRAM		= Next; Next += 0x010000;
140 	BurnPalRAM		= Next; Next += 0x004000;
141 	DrvBgRAM		= Next; Next += 0x001000;
142 	DrvSprRAM		= Next; Next += 0x002000;
143 
144 	RamEnd			= Next;
145 
146 	MemEnd			= Next;
147 
148 	return 0;
149 }
150 
DrvGfxDecode()151 static INT32 DrvGfxDecode()
152 {
153 	INT32 Planes[8] = { STEP4(8,1), STEP4(0,1) };
154 	INT32 XOffs[16] = { 0,4,16,20,32,36,48,52, 512+0, 512+4, 512+16, 512+20, 512+32, 512+36, 512+48, 512+52 };
155 	INT32 YOffs[16] = { STEP8(0,8*8), STEP8(1024,8*8) };
156 
157 	UINT8 *tmp = (UINT8*)BurnMalloc(0x400000);
158 	if (tmp == NULL) {
159 		return 1;
160 	}
161 
162 	memcpy (tmp, DrvGfxROM[0], 0x400000);
163 
164 	GfxDecode(0x4000, 8, 16, 16, Planes, XOffs, YOffs, 0x800, tmp, DrvGfxROM[0]);
165 
166 	BurnFree (tmp);
167 
168 	return 0;
169 }
170 
DrvInit()171 static INT32 DrvInit()
172 {
173 	BurnAllocMemIndex();
174 
175 	{
176 		INT32 k = 0;
177 		if (BurnLoadRom(Drv68KROM    + 0x000001, k++, 2)) return 1;
178 		if (BurnLoadRom(Drv68KROM    + 0x000000, k++, 2)) return 1;
179 
180 		if (BurnLoadRom(DrvGfxROM[0] + 0x000000, k++, 1)) return 1;
181 		if (BurnLoadRom(DrvGfxROM[0] + 0x200000, k++, 1)) return 1;
182 
183 		if (BurnLoadRom(DrvGfxROM[1] + 0x000000, k++, 1)) return 1;
184 		if (BurnLoadRom(DrvGfxROM[1] + 0x200000, k++, 1)) return 1;
185 
186 		if (BurnLoadRom(DrvSndROM    + 0x000000, k++, 1)) return 1;
187 
188 		DrvGfxDecode();
189 	}
190 
191 	SekInit(0, 0x68000);
192 	SekOpen(0);
193 	SekMapMemory(Drv68KROM,			0x000000, 0x07ffff, MAP_ROM);
194 	SekMapMemory(Drv68KRAM,			0x100000, 0x10ffff, MAP_RAM);
195 	SekMapMemory(DrvBgRAM,			0x400000, 0x400fff, MAP_RAM);
196 	SekMapMemory(BurnPalRAM,		0x600000, 0x603fff, MAP_RAM);
197 	SekMapMemory(DrvSprRAM,			0x700000, 0x701fff, MAP_RAM);
198 	SekSetWriteByteHandler(0,		goori_write_byte);
199 	SekSetReadByteHandler(0,		goori_read_byte);
200 	SekClose();
201 
202 	EEPROMInit(&eeprom_interface_93C46);
203 
204 	BurnYM2151Init(3579545);
205 	BurnYM2151SetRoute(BURN_SND_YM2151_YM2151_ROUTE_1, 0.45, BURN_SND_ROUTE_LEFT);
206 	BurnYM2151SetRoute(BURN_SND_YM2151_YM2151_ROUTE_2, 0.45, BURN_SND_ROUTE_RIGHT);
207 
208 	MSM6295Init(0, 1000000 / MSM6295_PIN7_HIGH, 1);
209 	MSM6295SetRoute(0, 0.47, BURN_SND_ROUTE_BOTH);
210 
211 	GenericTilesInit();
212 	GenericTilemapInit(0, TILEMAP_SCAN_ROWS, bg_map_callback, 16, 16, 32, 32);
213 	GenericTilemapSetGfx(0, DrvGfxROM[0], 8, 16, 16, 0x400000, 0x0000, 0x1f);
214 	GenericTilemapSetGfx(1, DrvGfxROM[1], 8, 16, 16, 0x400000, 0x1f00, 0x1f);
215 	GenericTilemapSetOffsets(TMAP_GLOBAL, 0, -16);
216 
217 	DrvDoReset();
218 
219 	return 0;
220 }
221 
DrvExit()222 static INT32 DrvExit()
223 {
224 	GenericTilesExit();
225 
226 	BurnYM2151Exit();
227 	MSM6295Exit(0);
228 	SekExit();
229 	EEPROMExit();
230 
231 	BurnFreeMemIndex();
232 
233 	MSM6295ROM = NULL;
234 
235 	return 0;
236 }
237 
draw_sprites()238 static void draw_sprites()
239 {
240 	for (INT32 i = 0; i < 0x2000; i += 16)
241 	{
242 		INT32 attr  = DrvSprRAM[i + 0x06];
243 		INT32 code  = DrvSprRAM[i + 0x0c] | (DrvSprRAM[i + 0x0e] << 8);
244 		INT32 sx    = DrvSprRAM[i + 0x08] | ((attr & 1) << 8);
245 		INT32 sy    = DrvSprRAM[i + 0x0a];
246 		INT32 color = attr >> 3;
247 		INT32 flipx = DrvSprRAM[i + 0x0e] & 0x80;
248 
249 		if (sx >= 0x3e0) sx -= 0x400;
250 
251 		DrawGfxMaskTile(0, 0, code, sx, sy - 16, flipx, 0, color, 0xff);
252 	}
253 }
254 
DrvDraw()255 static INT32 DrvDraw()
256 {
257 	if (DrvRecalc) {
258 		BurnPaletteUpdate_xBBBBBGGGGGRRRRR();
259 		DrvRecalc = 1; // force
260 	}
261 
262 	if (~nBurnLayer & 1) BurnTransferClear();
263 
264 	if ( nBurnLayer & 1) GenericTilemapDraw(0, pTransDraw, 0);
265 
266 	if ( nSpriteEnable & 1) draw_sprites();
267 
268 	BurnTransferCopy(BurnPalette);
269 
270 	return 0;
271 }
272 
DrvFrame()273 static INT32 DrvFrame()
274 {
275 	if (DrvReset) {
276 		DrvDoReset();
277 	}
278 
279 	{
280 		memset (DrvInputs, 0xff, sizeof(DrvInputs));
281 
282 		for (INT32 i = 0; i < 8; i++) {
283 			DrvInputs[0] ^= (DrvJoy1[i] & 1) << i;
284 			DrvInputs[1] ^= (DrvJoy2[i] & 1) << i;
285 			DrvInputs[2] ^= (DrvJoy3[i] & 1) << i;
286 		}
287 	}
288 
289 	INT32 nSegment;
290 	INT32 nInterleave = 262;
291 	INT32 nSoundBufferPos = 0;
292 	INT32 nCyclesTotal[1] = { 16000000 / 60 };
293 	INT32 nCyclesDone[1] = { 0 };
294 
295 	SekOpen(0);
296 
297 	for (INT32 i = 0; i < nInterleave; i++)
298 	{
299 		CPU_RUN(0, Sek);
300 		if (i == 239) {
301 			SekSetIRQLine(4, CPU_IRQSTATUS_AUTO);
302 
303 			if (pBurnDraw) {
304 				BurnDrvRedraw();
305 			}
306 		}
307 
308 		if (pBurnSoundOut) {
309 			nSegment = nBurnSoundLen / nInterleave;
310 			BurnYM2151Render(pBurnSoundOut + (nSoundBufferPos << 1), nSegment);
311 			nSoundBufferPos += nSegment;
312 		}
313 	}
314 
315 	if (pBurnSoundOut) {
316 		nSegment = nBurnSoundLen - nSoundBufferPos;
317 		if (nSegment > 0) {
318 			BurnYM2151Render(pBurnSoundOut + (nSoundBufferPos << 1), nSegment);
319 		}
320 		MSM6295Render(0, pBurnSoundOut, nBurnSoundLen);
321 	}
322 
323 	SekClose();
324 
325 	return 0;
326 }
327 
DrvScan(INT32 nAction,INT32 * pnMin)328 static INT32 DrvScan(INT32 nAction, INT32 *pnMin)
329 {
330 	struct BurnArea ba;
331 
332 	if (pnMin != NULL) {
333 		*pnMin = 0x029698;
334 	}
335 
336 	if (nAction & ACB_MEMORY_RAM) {
337 		memset(&ba, 0, sizeof(ba));
338 		ba.Data	  = AllRam;
339 		ba.nLen	  = RamEnd-AllRam;
340 		ba.szName = "All Ram";
341 		BurnAcb(&ba);
342 	}
343 
344 	if (nAction & ACB_DRIVER_DATA) {
345 		SekScan(nAction);
346 
347 		BurnYM2151Scan(nAction, pnMin);
348 		MSM6295Scan(nAction, pnMin);
349 		EEPROMScan(nAction, pnMin);
350 	}
351 
352 	return 0;
353 }
354 
355 
356 // Goori Goori
357 
358 static struct BurnRomInfo gooriRomDesc[] = {
359 	{ "2",						0x040000, 0x82eae7bf, 1 | BRF_PRG | BRF_ESS }, //  0 68K Code
360 	{ "3",						0x040000, 0x39093929, 1 | BRF_PRG | BRF_ESS }, //  1
361 
362 	{ "mx29f1610ml_obj_16m_l",	0x200000, 0xf26451b9, 2 | BRF_GRA },           //  2 Sprites
363 	{ "mx29f1610ml_obj_16m_h",	0x200000, 0x058ceaec, 2 | BRF_GRA },           //  3
364 
365 	{ "mx29f1610ml_scr_16m_l",	0x200000, 0x8603a662, 3 | BRF_GRA },           //  4 Background Tiles
366 	{ "mx29f1610ml_scr_16m_h",	0x200000, 0x4223383e, 3 | BRF_GRA },           //  5
367 
368 	{ "1",						0x040000, 0xc74351b9, 4 | BRF_SND },           //  6 Samples
369 };
370 
371 STD_ROM_PICK(goori)
372 STD_ROM_FN(goori)
373 
374 struct BurnDriver BurnDrvGoori = {
375 	"goori", NULL, NULL, NULL, "1999",
376 	"Goori Goori\0", NULL, "Unico", "Miscellaneous",
377 	NULL, NULL, NULL, NULL,
378 	BDF_GAME_WORKING, 2, HARDWARE_MISC_POST90S, GBF_PUZZLE, 0,
379 	NULL, gooriRomInfo, gooriRomName, NULL, NULL, NULL, NULL, GooriInputInfo, NULL,
380 	DrvInit, DrvExit, DrvFrame, DrvDraw, DrvScan, &DrvRecalc, 0x2000,
381 	384, 224, 4, 3
382 };
383 
384