1 // Kaneko Pandora module
2 // Based on MAME sources by David Haywood, Luca Elia
3 
4 #include "tiles_generic.h"
5 
6 static UINT16 *pandora_temp	= NULL;
7 static UINT8 *pandora_ram	= NULL;
8 static UINT8 *pandora_gfx	= NULL;
9 static INT32 pandora_clear;
10 static INT32 pandora_xoffset;
11 static INT32 pandora_yoffset;
12 static INT32 pandora_color_offset;
13 static INT32 pandora_code_max;
14 INT32 pandora_flipscreen;
15 
pandora_set_clear(INT32 clear)16 void pandora_set_clear(INT32 clear)
17 {
18 #if defined FBA_DEBUG
19 	if (!DebugDev_PandoraInitted) bprintf(PRINT_ERROR, _T("pandora_set_clear called without init\n"));
20 #endif
21 
22 	pandora_clear = clear;
23 }
24 
pandora_update(UINT16 * dest)25 void pandora_update(UINT16 *dest)
26 {
27 #if defined FBA_DEBUG
28 	if (!DebugDev_PandoraInitted) bprintf(PRINT_ERROR, _T("pandora_update called without init\n"));
29 #endif
30 
31 	for (INT32 i = 0; i < nScreenWidth * nScreenHeight; i++) {
32 		if (pandora_temp[i]) {
33 			dest[i] = pandora_temp[i] & 0x3ff;
34 		}
35 	}
36 }
37 
pandora_buffer_sprites()38 void pandora_buffer_sprites()
39 {
40 #if defined FBA_DEBUG
41 	if (!DebugDev_PandoraInitted) bprintf(PRINT_ERROR, _T("pandora_buffer_sprites called without init\n"));
42 #endif
43 
44 	INT32 sx=0, sy=0, x=0, y=0;
45 
46 	if (pandora_clear) memset (pandora_temp, 0, nScreenWidth * nScreenHeight * sizeof(UINT16));
47 
48 	for (INT32 offs = 0; offs < 0x1000; offs += 8)
49 	{
50 		INT32 attr	= pandora_ram[offs+7];
51 		INT32 code	= pandora_ram[offs+6] + ((attr & 0x3f) << 8);
52 		INT32 dy		= pandora_ram[offs+5];
53 		INT32 dx		= pandora_ram[offs+4];
54 		INT32 color	= pandora_ram[offs+3];
55 		INT32 flipy	= attr & 0x40;
56 		INT32 flipx	= attr & 0x80;
57 
58 		if (color & 1) dx |= 0x100;
59 		if (color & 2) dy |= 0x100;
60 
61 		if (color & 4)
62 		{
63 			x += dx;
64 			y += dy;
65 		}
66 		else
67 		{
68 			x = dx;
69 			y = dy;
70 		}
71 
72 		code &= pandora_code_max;
73 
74 		if (pandora_flipscreen)
75 		{
76 			sx = 240 - x;
77 			sy = 240 - y;
78 			flipx = !flipx;
79 			flipy = !flipy;
80 		}
81 		else
82 		{
83 			sx = x;
84 			sy = y;
85 		}
86 
87 		sx = (sx + pandora_xoffset) & 0x1ff;
88 		sy = (sy + pandora_yoffset) & 0x1ff;
89 		if (sx & 0x100) sx -= 0x200;
90 		if (sy & 0x100) sy -= 0x200;
91 
92 		if (sx >= nScreenWidth  || sx < -15) continue;
93 		if (sy >= nScreenHeight || sy < -15) continue;
94 
95 		if (flipy) {
96 			if (flipx) {
97 				Render16x16Tile_Mask_FlipXY_Clip(pandora_temp, code, sx, sy, color >> 4, 4, 0, pandora_color_offset, pandora_gfx);
98 			} else {
99 				Render16x16Tile_Mask_FlipY_Clip(pandora_temp, code, sx, sy, color >> 4, 4, 0, pandora_color_offset, pandora_gfx);
100 			}
101 		} else {
102 			if (flipx) {
103 				Render16x16Tile_Mask_FlipX_Clip(pandora_temp, code, sx, sy, color >> 4, 4, 0, pandora_color_offset, pandora_gfx);
104 			} else {
105 				Render16x16Tile_Mask_Clip(pandora_temp, code, sx, sy, color >> 4, 4, 0, pandora_color_offset, pandora_gfx);
106 			}
107 		}
108 	}
109 }
110 
111 // must be called after GenericTilesInit()
pandora_init(UINT8 * ram,UINT8 * gfx,INT32 gfx_mod,INT32 color_offset,INT32 x,INT32 y)112 void pandora_init(UINT8 *ram, UINT8 *gfx, INT32 gfx_mod, INT32 color_offset, INT32 x, INT32 y)
113 {
114 	DebugDev_PandoraInitted = 1;
115 
116 	pandora_ram	= ram;
117 	pandora_xoffset	= x;
118 	pandora_yoffset	= y;
119 	pandora_gfx	= gfx;
120 	pandora_color_offset	= color_offset;
121 	pandora_code_max = gfx_mod;
122 
123 	if (BurnDrvGetFlags() & BDF_ORIENTATION_VERTICAL) {
124 		BurnDrvGetVisibleSize(&nScreenHeight, &nScreenWidth);
125 	} else {
126 		BurnDrvGetVisibleSize(&nScreenWidth, &nScreenHeight);
127 	}
128 
129 	pandora_temp = (UINT16*)BurnMalloc(nScreenWidth * nScreenHeight * sizeof(UINT16));
130 	pandora_clear = 1;
131 }
132 
pandora_exit()133 void pandora_exit()
134 {
135 #if defined FBA_DEBUG
136 	if (!DebugDev_PandoraInitted) bprintf(PRINT_ERROR, _T("pandora_exit called without init\n"));
137 #endif
138 
139 	BurnFree (pandora_temp);
140 
141 	pandora_ram = pandora_gfx = NULL;
142 
143 	DebugDev_PandoraInitted = 0;
144 }
145