summaryrefslogtreecommitdiffstatshomepage
path: root/src/mame/video/ninjakid.c
blob: fa620866c157b5a191d0ea841ccc14b188646f70 (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
#include "driver.h"
#include "sound/ay8910.h"

static tilemap *bg_tilemap, *fg_tilemap;
static UINT8 flipscreen;
static UINT8 ninjakun_xscroll,ninjakun_yscroll;

static UINT8 ninjakun_io_8000_ctrl[4];
// static UINT8 old_scroll;

/*******************************************************************************
 Tilemap Callbacks
*******************************************************************************/

static TILE_GET_INFO( get_fg_tile_info ){
	UINT32 tile_number = videoram[tile_index] & 0xFF;
	UINT8 attr  = videoram[tile_index+0x400];
	tile_number += (attr & 0x20) << 3; /* bank */
	SET_TILE_INFO(
			0,
			tile_number,
			(attr&0xf),
			0);
}

static TILE_GET_INFO( get_bg_tile_info ){
	UINT32 tile_number = videoram[tile_index+0x800] & 0xFF;
	UINT8 attr  = videoram[tile_index+0xc00];
	tile_number += (attr & 0xC0) << 2; /* bank */
	SET_TILE_INFO(
			1,
			tile_number,
			(attr&0xf),
			0);
}

WRITE8_HANDLER( ninjakid_fg_videoram_w ){
	videoram[offset] = data;
	tilemap_mark_tile_dirty(fg_tilemap,offset&0x3ff);
}

WRITE8_HANDLER( ninjakid_bg_videoram_w ){

	int y = (offset + ((ninjakun_yscroll & 0xf8) << 2) ) & 0x3e0;
	int x = (offset + (ninjakun_xscroll >> 3) ) & 0x1f;
	int offs = x+y+(offset & 0x400);

	videoram[0x800+offs] = data;
	tilemap_mark_tile_dirty(bg_tilemap,x+y);
}

READ8_HANDLER( ninjakid_bg_videoram_r )
{
	int y = (offset + ((ninjakun_yscroll & 0xf8) << 2) ) & 0x3e0;
	int x = (offset + (ninjakun_xscroll >> 3) ) & 0x1f;
	int offs = x+y+(offset & 0x400);

	return videoram[0x800+offs];
}

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

WRITE8_HANDLER( ninjakun_flipscreen_w ){
	flipscreen = data?(TILEMAP_FLIPX|TILEMAP_FLIPY):0;
	tilemap_set_flip( ALL_TILEMAPS,flipscreen );
}

READ8_HANDLER( ninjakun_io_8000_r ){
	switch( offset ){
	case 0: /* control */
		return AY8910_read_port_0_r( 0 );

	case 1: /* input read */
		switch( ninjakun_io_8000_ctrl[0] ){
		case 0xf:
			return readinputport(4);
		case 0xe:
			return readinputport(3);
		default:
			return ninjakun_io_8000_ctrl[1];
		}
		break;

	case 2: /* control */
		return AY8910_read_port_1_r( 0 );

	case 3: /* data */
		return ninjakun_io_8000_ctrl[3];
	}

//  logerror("PC=%04x; RAM[0x800%d]\n",activecpu_get_pc(),offset);
	return 0xFF;
}

/* static void handle_scrolly( UINT8 new_scroll ){ */

/*  HACK!!!
**
**  New rows are always written at fixed locations above and below the background
**  tilemaps, rather than at the logical screen boundaries with respect to scrolling.
**
**  I don't know how this is handled by the actual NinjaKun hardware, but the
**  following is a crude approximation, yielding a playable game.
*/

/*
    int old_row = old_scroll/8;
    int new_row = new_scroll/8;
    int i;
    if( new_scroll!=old_scroll ){
        tilemap_set_scrolly( bg_tilemap, 0, new_scroll&7 );

        if ((new_row == ((old_row - 1) & 0xff)) || ((!old_row) && (new_row == 0x1f)))
        {
            for( i=0x400-0x21; i>=0; i-- ){
                ninjakid_bg_videoram_w( i+0x20, videoram[0x800+i] );
            }
        }
        else if ((new_row == ((old_row + 1) & 0xff)) || ((old_row == 0x1f) && (!new_row)))
        {
            for( i=0x20; i<0x400; i++ ){
                ninjakid_bg_videoram_w( i-0x20, videoram[0x800+i] );
            }
        }

        old_scroll = new_scroll;
    }
}
*/


WRITE8_HANDLER( ninjakun_io_8000_w ){
	switch( offset ){
	case 0x0: /* control#1 */
		ninjakun_io_8000_ctrl[0] = data;
		AY8910_control_port_0_w( 0, data );
		break;

	case 0x1: /* data#1 */
		ninjakun_io_8000_ctrl[1] = data;
		switch( ninjakun_io_8000_ctrl[0] ){
		default:
			AY8910_write_port_0_w( 0,data );
			break;
		}
		break;

	case 0x2: /* control#2 */
		ninjakun_io_8000_ctrl[2] = data;
		AY8910_control_port_1_w( 0, data );
		break;

	case 0x3: /* data#2 */
		ninjakun_io_8000_ctrl[3] = data;
		switch( ninjakun_io_8000_ctrl[2] ){
		case 0xf:
				tilemap_set_scrolly( bg_tilemap, 0, data );
				ninjakun_yscroll = data;
			break;
		case 0xe:
			if (flipscreen == 0)
				tilemap_set_scrollx( bg_tilemap, 0, data-7 );
			else
				tilemap_set_scrollx( bg_tilemap, 0, data );
				ninjakun_xscroll = data;
			break;
		default:
			AY8910_write_port_1_w( 0,data );
		}
		break;
	}
}

WRITE8_HANDLER( ninjakun_paletteram_w )
{
	int i;

	paletteram_BBGGRRII_w(offset,data);

	if (offset > 15)
		return;

	if (offset != 1)
	{
		for (i=0; i<16; i++)
		{
			paletteram_BBGGRRII_w(0x200+offset+i*16,data);
		}
	}
	paletteram_BBGGRRII_w(0x200+offset*16+1,data);
}

/*******************************************************************************
 Video Hardware Functions
********************************************************************************
 vh_start / vh_refresh
*******************************************************************************/

VIDEO_START( ninjakid ){
    fg_tilemap = tilemap_create( get_fg_tile_info,tilemap_scan_rows,TILEMAP_TYPE_PEN,8,8,32,32 );
	bg_tilemap = tilemap_create( get_bg_tile_info,tilemap_scan_rows,TILEMAP_TYPE_PEN,8,8,32,32 );
	tilemap_set_transparent_pen( fg_tilemap,0 );

	/* Save State Support */

	state_save_register_global_array(ninjakun_io_8000_ctrl);
	state_save_register_global(flipscreen);
//  state_save_register_global(old_scroll);
}

static void draw_sprites(running_machine *machine, mame_bitmap *bitmap, const rectangle *cliprect ){
	const UINT8 *source = spriteram;
	const UINT8 *finish = source+0x800;

	const gfx_element *gfx = machine->gfx[2];

	while( source<finish ){
		int tile_number = source[0];
		int sx = source[1];
		int sy = source[2];
		int attr = source[3];
		int flipx = attr&0x10;
		int flipy = attr&0x20;
		int color = attr&0x0f;

		if( flipscreen ){
			sx = 240-sx;
			sy = 240-sy;
			flipx = !flipx;
			flipy = !flipy;
		}

		drawgfx(
			bitmap,
			gfx,
			tile_number,
			color,
			flipx,flipy,
			sx,sy,
			cliprect,
			TRANSPARENCY_PEN,0
		);
		if (sx>240)
			drawgfx(
				bitmap,
				gfx,
				tile_number,
				color,
				flipx,flipy,
				sx-256,sy,
				cliprect,
				TRANSPARENCY_PEN,0
			);

		source+=0x20;
	}
}

VIDEO_UPDATE( ninjakid )
{
	int offs,chr,col,px,py,x,y;

	tilemap_draw( bitmap,cliprect,bg_tilemap,0,0 );
	tilemap_draw( bitmap,cliprect,fg_tilemap,0,0 );
	draw_sprites( machine, bitmap,cliprect );

	for (y=4; y<28; y++)
	{
		for (x=0; x<32; x++)
		{
			offs = y*32+x;
			chr = videoram[offs];
			col = videoram[offs + 0x400];
			chr +=  (col & 0x20) << 3;

			if ((col & 0x10) == 0)
			{

				if (flipscreen==0)
				{
					px = 8*x;
					py = 8*y;
				}
				else
				{
					px = 248-8*x;
					py = 248-8*y;
				}

				drawgfx(bitmap,machine->gfx[0],
					chr,
					col & 0x0f,
					flipscreen,flipscreen,
					px,py,
					cliprect,TRANSPARENCY_PEN,0);
			}
		}
	}


	return 0;
}