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

  Video Hardware for Championship V'ball by Paul Hampson
  Generally copied from China Gate by Paul Hampson
  "Mainly copied from video of Double Dragon (bootleg) & Double Dragon II"

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

#include "driver.h"
#include "cpu/m6502/m6502.h"

int vb_scrollx_hi=0;
int vb_scrollx_lo=0;
int vb_scrolly_hi=0;

UINT8 *vb_scrolly_lo;
UINT8 *vb_videoram;
UINT8 *vb_attribram;
int vball_gfxset=0;
int vb_bgprombank=0xff;
int vb_spprombank=0xff;

static tilemap *bg_tilemap;
static int scrollx[32];

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

  Callbacks for the TileMap code

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

static TILEMAP_MAPPER( background_scan )
{
	/* logical (col,row) -> memory offset */
	return (col & 0x1f) + ((row & 0x1f) << 5) + ((col & 0x20) << 5) + ((row & 0x20) <<6);
}

static TILE_GET_INFO( get_bg_tile_info )
{
	UINT8 code = vb_videoram[tile_index];
	UINT8 attr = vb_attribram[tile_index];
	SET_TILE_INFO(
			0,
			code + ((attr & 0x1f) << 8) + (vball_gfxset<<8),
			(attr >> 5) & 0x7,
			0);
}


VIDEO_START( vb )
{
	bg_tilemap = tilemap_create(get_bg_tile_info,background_scan,TILEMAP_TYPE_PEN, 8, 8,64,64);

	tilemap_set_scroll_rows(bg_tilemap,32);
}

WRITE8_HANDLER( vb_videoram_w )
{
	vb_videoram[offset] = data;
	tilemap_mark_tile_dirty(bg_tilemap,offset);
}

#ifdef UNUSED_FUNCTION
READ8_HANDLER( vb_attrib_r )
{
	return vb_attribram[offset];
}
#endif

WRITE8_HANDLER( vb_attrib_w )
{
	vb_attribram[offset] = data;
	tilemap_mark_tile_dirty(bg_tilemap,offset);
}

void vb_bgprombank_w( int bank )
{
	int i;
	UINT8* color_prom;

	if (bank==vb_bgprombank) return;

	color_prom = memory_region(REGION_PROMS) + bank*0x80;
	for (i=0;i<128;i++, color_prom++) {
		palette_set_color_rgb(Machine,i,pal4bit(color_prom[0] >> 0),pal4bit(color_prom[0] >> 4),
				       pal4bit(color_prom[0x800] >> 0));
	}
	vb_bgprombank=bank;
}

void vb_spprombank_w( int bank )
{

	int i;
	UINT8* color_prom;

	if (bank==vb_spprombank) return;

	color_prom = memory_region(REGION_PROMS)+0x400 + bank*0x80;
	for (i=128;i<256;i++,color_prom++)	{
		palette_set_color_rgb(Machine,i,pal4bit(color_prom[0] >> 0),pal4bit(color_prom[0] >> 4),
				       pal4bit(color_prom[0x800] >> 0));
	}
	vb_spprombank=bank;
}

void vb_mark_all_dirty( void )
{
	tilemap_mark_all_tiles_dirty(bg_tilemap);
}

#define DRAW_SPRITE( order, sx, sy ) drawgfx( bitmap, gfx, \
					(which+order),color,flipx,flipy,sx,sy, \
					cliprect,TRANSPARENCY_PEN,0);

static void draw_sprites(running_machine *machine, mame_bitmap *bitmap, const rectangle *cliprect)
{
	const gfx_element *gfx = machine->gfx[1];
	UINT8 *src = spriteram;
	int i;

/*  240-Y    S|X|CLR|WCH WHICH    240-X
    xxxxxxxx x|x|xxx|xxx xxxxxxxx xxxxxxxx
*/
	for (i = 0;i < spriteram_size;i += 4)
	{
		int attr = src[i+1];
		int which = src[i+2]+((attr & 0x07)<<8);
		int sx = ((src[i+3] + 8) & 0xff) - 7;
		int sy = 240 - src[i];
		int size = (attr & 0x80) >> 7;
		int color = (attr & 0x38) >> 3;
		int flipx = ~attr & 0x40;
		int flipy = 0;
		int dy = -16;

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

		switch (size)
		{
			case 0: /* normal */
			DRAW_SPRITE(0,sx,sy);
			break;

			case 1: /* double y */
			DRAW_SPRITE(0,sx,sy + dy);
			DRAW_SPRITE(1,sx,sy);
			break;
		}
	}
}

#undef DRAW_SPRITE

VIDEO_UPDATE( vb )
{
	int i;

	tilemap_set_scrolly(bg_tilemap,0,vb_scrolly_hi + *vb_scrolly_lo);

	/*To get linescrolling to work properly, we must ignore the 1st two scroll values, no idea why! -SJE */
	for (i = 2;i < 32;i++) {
		tilemap_set_scrollx(bg_tilemap,i,scrollx[i-2]);
		//logerror("scrollx[%d] = %d\n",i,scrollx[i]);
	}
	tilemap_draw(bitmap,cliprect,bg_tilemap,0,0);
	draw_sprites(machine,bitmap,cliprect);
	return 0;
}


/*I don't really understand what the proper timing of this should be,
  but after TONS of testing, the tilemap individual line scrolling works as long as flip screen is not set -SJE
*/
INTERRUPT_GEN( vball_interrupt )
{
	int line = 31 - cpu_getiloops();
	if (line < 13)
		cpunum_set_input_line(0, M6502_IRQ_LINE, HOLD_LINE);
	else if (line == 13)
		cpunum_set_input_line(0, INPUT_LINE_NMI, PULSE_LINE);
	//save the scroll x register value
	if(line<32) scrollx[31-line] = (vb_scrollx_hi + vb_scrollx_lo+4);
}