summaryrefslogtreecommitdiffstatshomepage
path: root/src/mame/video/terracre.cpp
blob: d2ee3a13cdb744a69babc85315bb55ca90fe400b (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
// license:BSD-3-Clause
// copyright-holders:Carlos A. Lozano
/***************************************************************************

  video.c

  Functions to emulate the video hardware of the machine.

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

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


TILE_GET_INFO_MEMBER(terracre_state::get_bg_tile_info)
{
	/* xxxx.----.----.----
	 * ----.xx--.----.----
	 * ----.--xx.xxxx.xxxx */
	unsigned data = m_bg_videoram[tile_index];
	unsigned color = data>>11;
	SET_TILE_INFO_MEMBER(1,data&0x3ff,color,0 );
}

TILE_GET_INFO_MEMBER(terracre_state::get_fg_tile_info)
{
	unsigned data = m_fg_videoram[tile_index];
	SET_TILE_INFO_MEMBER(0,data&0xff,0,0 );
}

void terracre_state::draw_sprites(bitmap_ind16 &bitmap, const rectangle &cliprect )
{
	const uint8_t *spritepalettebank = memregion("user1")->base();
	gfx_element *pGfx = m_gfxdecode->gfx(2);
	const uint16_t *pSource = m_spriteram->buffer();
	int flip = flip_screen();
	int transparent_pen;

	if( pGfx->elements() > 0x200 )
	{ /* HORE HORE Kid */
		transparent_pen = 0xf;
	}
	else
	{
		transparent_pen = 0x0;
	}
	for( int i=0; i<0x200; i+=8 )
	{
		int tile = pSource[1]&0xff;
		int attrs = pSource[2];
		int flipx = attrs&0x04;
		int flipy = attrs&0x08;
		int color = (attrs&0xf0)>>4;
		int sx = (pSource[3] & 0xff) - 0x80 + 256 * (attrs & 1);
		int sy = 240 - (pSource[0] & 0xff);

		if( transparent_pen )
		{
			int bank;

			if( attrs&0x02 ) tile |= 0x200;
			if( attrs&0x10 ) tile |= 0x100;

			bank = (tile&0xfc)>>1;
			if( tile&0x200 ) bank |= 0x80;
			if( tile&0x100 ) bank |= 0x01;

			color &= 0xe;
			color += 16*(spritepalettebank[bank]&0xf);
		}
		else
		{
			if( attrs&0x02 ) tile|= 0x100;
			color += 16 * (spritepalettebank[(tile>>1)&0xff] & 0x0f);
		}

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

		pGfx->transpen(
			bitmap,cliprect,tile, color,flipx,flipy,sx,sy,transparent_pen );

		pSource += 4;
	}
}

PALETTE_INIT_MEMBER(terracre_state, terracre)
{
	const uint8_t *color_prom = memregion("proms")->base();
	int i;

	/* create a lookup table for the palette */
	for (i = 0; i < 0x100; i++)
	{
		int r = pal4bit(color_prom[i + 0x000]);
		int g = pal4bit(color_prom[i + 0x100]);
		int b = pal4bit(color_prom[i + 0x200]);

		palette.set_indirect_color(i, rgb_t(r, g, b));
	}

	/* color_prom now points to the beginning of the lookup table */
	color_prom += 0x300;

	/* characters use colors 0-0x0f */
	for (i = 0; i < 0x10; i++)
		palette.set_pen_indirect(i, i);

	/* background tiles use colors 0xc0-0xff in four banks */
	/* the bottom two bits of the color code select the palette bank for */
	/* pens 0-7; the top two bits for pens 8-0x0f. */
	for (i = 0; i < 0x100; i++)
	{
		uint8_t ctabentry;

		if (i & 0x08)
			ctabentry = 0xc0 | (i & 0x0f) | ((i & 0xc0) >> 2);
		else
			ctabentry = 0xc0 | (i & 0x0f) | ((i & 0x30) >> 0);

		palette.set_pen_indirect(0x10 + i, ctabentry);
	}

	/* sprites use colors 128-191 in four banks */
	/* The lookup table tells which colors to pick from the selected bank */
	/* the bank is selected by another PROM and depends on the top 8 bits of */
	/* the sprite code. The PROM selects the bank *separately* for pens 0-7 and */
	/* 8-15 (like for tiles). */
	for (i = 0; i < 0x1000; i++)
	{
		uint8_t ctabentry;
		int i_swapped = ((i & 0x0f) << 8) | ((i & 0xff0) >> 4);

		if (i & 0x80)
			ctabentry = 0x80 | ((i & 0x0c) << 2) | (color_prom[i >> 4] & 0x0f);
		else
			ctabentry = 0x80 | ((i & 0x03) << 4) | (color_prom[i >> 4] & 0x0f);

		palette.set_pen_indirect(0x110 + i_swapped, ctabentry);
	}
}

WRITE16_MEMBER(terracre_state::amazon_background_w)
{
	COMBINE_DATA( &m_bg_videoram[offset] );
	m_background->mark_tile_dirty(offset );
}

WRITE16_MEMBER(terracre_state::amazon_foreground_w)
{
	COMBINE_DATA( &m_fg_videoram[offset] );
	m_foreground->mark_tile_dirty(offset );
}

WRITE16_MEMBER(terracre_state::amazon_flipscreen_w)
{
	if( ACCESSING_BITS_0_7 )
	{
		machine().bookkeeping().coin_counter_w(0, data&0x01 );
		machine().bookkeeping().coin_counter_w(1, (data&0x02)>>1 );
		flip_screen_set(data&0x04);
	}
}

WRITE16_MEMBER(terracre_state::amazon_scrolly_w)
{
	COMBINE_DATA(&m_yscroll);
	m_background->set_scrolly(0,m_yscroll);
}

WRITE16_MEMBER(terracre_state::amazon_scrollx_w)
{
	COMBINE_DATA(&m_xscroll);
	m_background->set_scrollx(0,m_xscroll);
}

void terracre_state::video_start()
{
	m_background = &machine().tilemap().create(*m_gfxdecode, tilemap_get_info_delegate(FUNC(terracre_state::get_bg_tile_info),this),TILEMAP_SCAN_COLS,16,16,64,32);
	m_foreground = &machine().tilemap().create(*m_gfxdecode, tilemap_get_info_delegate(FUNC(terracre_state::get_fg_tile_info),this),TILEMAP_SCAN_COLS,8,8,64,32);
	m_foreground->set_transparent_pen(0xf);

	/* register for saving */
	save_item(NAME(m_xscroll));
	save_item(NAME(m_yscroll));
}

uint32_t terracre_state::screen_update_amazon(screen_device &screen, bitmap_ind16 &bitmap, const rectangle &cliprect)
{
	if( m_xscroll&0x2000 )
		bitmap.fill(m_palette->black_pen(), cliprect );
	else
		m_background->draw(screen, bitmap, cliprect, 0, 0 );

	draw_sprites(bitmap,cliprect );
	m_foreground->draw(screen, bitmap, cliprect, 0, 0 );
	return 0;
}