summaryrefslogtreecommitdiffstatshomepage
path: root/src/devices/machine/tc009xlvc.cpp
blob: e48e04fc50150db397099ed43e502dacce4bcda1 (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
// license:LGPL-2.1+
// copyright-holders:Angelo Salese
/***************************************************************************

    TC009xLVC device emulation

    Written by Angelo Salese, based off Taito L implementation

    TODO:
    - non-video stuff needs to be ported there as well

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

#include "emu.h"
#include "machine/tc009xlvc.h"

#include "emupal.h"
#include "screen.h"


DEFINE_DEVICE_TYPE(TC0091LVC, tc0091lvc_device, "tc009xlvc", "Taito TC0091LVC")

void tc0091lvc_device::vram_w(offs_t offset, u8 data)
{
	// TODO : offset 0x0000 - 0x3fff is not used?

	m_vram[offset] = data;
	gfx(2)->mark_dirty(offset / 32);
	if (offset >= 0x8000 && offset < 0x9000) // 0x8000-0x9000 bg 0
	{
		offset -= 0x8000;
		bg_tilemap[0]->mark_tile_dirty(offset/2);
	}
	else if (offset >= 0x9000 && offset < 0xa000) // 0x9000-0xa000 bg 1
	{
		offset -= 0x9000;
		bg_tilemap[1]->mark_tile_dirty(offset/2);
	}
	else if (offset >= 0xa000 && offset < 0xb000) // 0xa000-0xb000 text
	{
		offset -= 0xa000;
		tx_tilemap->mark_tile_dirty(offset/2);
	}
}

void tc0091lvc_device::vregs_w(offs_t offset, u8 data)
{
	if ((offset & 0xfc) == 0)
	{
		bg_tilemap[0]->mark_all_dirty();
		bg_tilemap[1]->mark_all_dirty();
	}

	m_vregs[offset] = data;
}

void tc0091lvc_device::ram_bank_w(offs_t offset, u8 data)
{
	m_ram_bank[offset] = data;
	m_bankdev[offset]->set_bank(m_ram_bank[offset]);
}

static const gfx_layout layout_8x8 =
{
	8, 8,
	RGN_FRAC(1,1),
	4,
	{ 8, 12, 0, 4 },
	{ STEP4(3,-1), STEP4(4*4+3,-1) },
	{ STEP8(0,4*4*2) },
	8*8*4
};

static const gfx_layout layout_16x16 =
{
	16, 16,
	RGN_FRAC(1,1),
	4,
	{ 8, 12, 0, 4 },
	{ STEP4(3,-1), STEP4(4*4+3,-1), STEP4(8*8*4+3,-1), STEP4(8*8*4+4*4+3,-1) },
	{ STEP8(0,4*4*2), STEP8(8*8*4*2,4*4*2) },
	8*8*4*4
};

GFXDECODE_MEMBER(tc0091lvc_device::gfxinfo)
	GFXDECODE_DEVICE("gfx",      0, layout_8x8,   0, 16)
	GFXDECODE_DEVICE("gfx",      0, layout_16x16, 0, 16)
	GFXDECODE_DEVICE_RAM("vram", 0, layout_8x8,   0, 16)
GFXDECODE_END

void tc0091lvc_device::cpu_map(address_map &map)
{
	// 0x0000-0x7fff ROM (0x0000-0x5fff Fixed, 0x6000-0x7fff Bankswitched)
	map(0x0000, 0x5fff).r(FUNC(tc0091lvc_device::rom_r));
	map(0x6000, 0x7fff).lr8(NAME([this] (offs_t offset) { return rom_r((m_rom_bank << 13) | (offset & 0x1fff)); }));

	// 0x8000-0xbfff External mappable area

	// 0xc000-0xfdff RAM banks (Connected in VRAMs, 4KB boundary)
	map(0xc000, 0xcfff).m(m_bankdev[0], FUNC(address_map_bank_device::amap8));
	map(0xd000, 0xdfff).m(m_bankdev[1], FUNC(address_map_bank_device::amap8));
	map(0xe000, 0xefff).m(m_bankdev[2], FUNC(address_map_bank_device::amap8));
	map(0xf000, 0xfdff).m(m_bankdev[3], FUNC(address_map_bank_device::amap8));

	// 0xfe00-0xffff Internal functions
}

void tc0091lvc_device::banked_map(address_map &map)
{
	map(0x010000, 0x01ffff).readonly().share("vram");
	// note, the way tiles are addressed suggests that 0x0000-0x3fff of this might be usable,
	//       but we don't map it anywhere, so the first tiles are always blank at the moment.
	map(0x014000, 0x01ffff).lw8(NAME([this] (offs_t offset, u8 data) { vram_w(offset + 0x4000, data); }));
	map(0x040000, 0x05ffff).ram().share("bitmap_ram");
	map(0x080000, 0x0801ff).ram().w("palette", FUNC(palette_device::write8)).share("palette");
}

tc0091lvc_device::tc0091lvc_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock)
	: device_t(mconfig, TC0091LVC, tag, owner, clock)
	, device_gfx_interface(mconfig, *this, gfxinfo, "palette")
	, m_bankdev(*this, "bankdev_%u", 0U)
	, m_vram(*this, "vram")
	, m_bitmap_ram(*this, "bitmap_ram")
	, m_rom(*this, DEVICE_SELF)
{
}

template<unsigned Offset>
TILE_GET_INFO_MEMBER(tc0091lvc_device::get_tile_info)
{
	const u16 attr = m_vram[Offset + (2 * tile_index) + 1];
	const u16 code = m_vram[Offset + (2 * tile_index)]
			| ((attr & 0x03) << 8)
			| ((m_vregs[(attr & 0xc) >> 2]) << 10);
//          | (state->m_horshoes_gfxbank << 12);

	SET_TILE_INFO_MEMBER(0,
			code,
			(attr & 0xf0) >> 4,
			0);
}

TILE_GET_INFO_MEMBER(tc0091lvc_device::get_tx_tile_info)
{
	const u16 attr = m_vram[0xa000 + (2 * tile_index) + 1];
	const u16 code = m_vram[0xa000 + (2 * tile_index)]
			| ((attr & 0x07) << 8);

	SET_TILE_INFO_MEMBER(2,
			code,
			(attr & 0xf0) >> 4,
			0);
}


void tc0091lvc_device::device_add_mconfig(machine_config &config)
{
	for (int i = 0; i < 4; i++)
	{
		ADDRESS_MAP_BANK(config, m_bankdev[i]).set_map(&tc0091lvc_device::banked_map).set_options(ENDIANNESS_LITTLE, 8, 20, 0x1000);
	}
	PALETTE(config, "palette", palette_device::BLACK).set_format(palette_device::xBGRBBBBGGGGRRRR_bit0, 0x100);
}


void tc0091lvc_device::device_post_load()
{
	for (int i = 0; i < 4; i++)
		m_bankdev[i]->set_bank(m_ram_bank[i]);
}


void tc0091lvc_device::device_start()
{
	std::fill_n(&m_vram[0], m_vram.bytes(), 0);
	std::fill_n(&m_bitmap_ram[0], m_bitmap_ram.bytes(), 0);
	std::fill(std::begin(m_ram_bank), std::end(m_ram_bank), 0);
	for (int i = 0; i < 4; i++)
		m_bankdev[i]->set_bank(m_ram_bank[i]);

	m_rom_bank = 0;

	m_vregs = make_unique_clear<u8[]>(0x100);
	m_sprram_buffer = make_unique_clear<u8[]>(0x400);

	tx_tilemap    = &machine().tilemap().create(*this, tilemap_get_info_delegate(*this, FUNC(tc0091lvc_device::get_tx_tile_info)),      TILEMAP_SCAN_ROWS, 8, 8, 64, 32);
	bg_tilemap[0] = &machine().tilemap().create(*this, tilemap_get_info_delegate(*this, FUNC(tc0091lvc_device::get_tile_info<0x8000>)), TILEMAP_SCAN_ROWS, 8, 8, 64, 32);
	bg_tilemap[1] = &machine().tilemap().create(*this, tilemap_get_info_delegate(*this, FUNC(tc0091lvc_device::get_tile_info<0x9000>)), TILEMAP_SCAN_ROWS, 8, 8, 64, 32);

	tx_tilemap->set_transparent_pen(0);
	bg_tilemap[0]->set_transparent_pen(0);
	bg_tilemap[1]->set_transparent_pen(0);

	tx_tilemap->set_scrolldx(-8, -8);
	bg_tilemap[0]->set_scrolldx(28, -11);
	bg_tilemap[1]->set_scrolldx(38, -21);

	save_item(NAME(m_irq_vector));
	save_item(NAME(m_irq_enable));
	save_item(NAME(m_ram_bank));
	save_item(NAME(m_rom_bank));
	save_pointer(NAME(m_vregs), 0x100);
	save_pointer(NAME(m_sprram_buffer), 0x400);
}


void tc0091lvc_device::draw_sprites(screen_device &screen, bitmap_ind16 &bitmap, const rectangle &cliprect, u8 global_flip)
{
	for (int count = 0; count < 0x3e7; count += 8)
	{
		const u32 code = m_sprram_buffer[count + 0] | (m_sprram_buffer[count + 1] << 8);
		int x = m_sprram_buffer[count + 4] | (m_sprram_buffer[count + 5] << 8);
		if (x >= 320)
			x -= 512;
		int y = m_sprram_buffer[count + 6];
		const u32 col = (m_sprram_buffer[count + 2]) & 0x0f;
		int fx = m_sprram_buffer[count + 3] & 0x1;
		int fy = m_sprram_buffer[count + 3] & 0x2;

		if (global_flip)
		{
			x = 304 - x;
			y = 240 - y;
			fx = !fx;
			fy = !fy;
		}

		gfx(1)->prio_transpen(bitmap, cliprect, code, col, fx, fy, x, y, screen.priority(), (col & 0x08) ? 0xaa : 0x00, 0);
	}
}

u32 tc0091lvc_device::screen_update(screen_device &screen, bitmap_ind16 &bitmap, const rectangle &cliprect)
{
	bitmap.fill(palette().black_pen(), cliprect);

	if ((m_vregs[4] & 0x20) == 0)
		return 0;

	const u8 global_flip = m_vregs[4] & 0x10;

	if ((m_vregs[4] & 0x7) == 7) // 8bpp bitmap enabled
	{
		u32 count = 0;

		for (int y = 0; y < 256; y++)
		{
			for (int x = 0; x < 512; x++)
			{
				const int res_x = (global_flip) ? 320 - x : x;
				const int res_y = (global_flip) ? 256 - y : y;

				if (cliprect.contains(res_x, res_y))
					bitmap.pix16(res_y, res_x) = palette().pen(m_bitmap_ram[count]);

				count++;
			}
		}
	}
	else
	{
		machine().tilemap().set_flip_all(global_flip ? (TILEMAP_FLIPY | TILEMAP_FLIPX) : 0);

		int dx = m_bg_scroll[0][0] | (m_bg_scroll[0][1] << 8);
		if (global_flip) { dx = ((dx & 0xfffc) | ((dx - 3) & 0x0003)) ^ 0xf; dx += 192; }
		int dy = m_bg_scroll[0][2];

		bg_tilemap[0]->set_scrollx(0, -dx);
		bg_tilemap[0]->set_scrolly(0, -dy);

		dx = m_bg_scroll[1][0] | (m_bg_scroll[1][1] << 8);
		if (global_flip) { dx = ((dx & 0xfffc) | ((dx - 3) & 0x0003)) ^ 0xf; dx += 192; }
		dy = m_bg_scroll[1][2];

		bg_tilemap[1]->set_scrollx(0, -dx);
		bg_tilemap[1]->set_scrolly(0, -dy);

		tx_tilemap->set_scrollx(0, (global_flip) ? -192 : 0);

		screen.priority().fill(0, cliprect);
		bg_tilemap[1]->draw(screen, bitmap, cliprect, 0, 0);
		bg_tilemap[0]->draw(screen, bitmap, cliprect, 0, (m_vregs[4] & 0x8) ? 0 : 1);
		draw_sprites(screen, bitmap, cliprect, global_flip);
		tx_tilemap->draw(screen, bitmap, cliprect, 0, 0);
	}
	return 0;
}

void tc0091lvc_device::screen_eof(void)
{
	std::copy_n(&m_vram[0xb000], 0x400, &m_sprram_buffer[0]);
	m_bg_scroll[0][0] = m_sprram_buffer[0x3f4];
	m_bg_scroll[0][1] = m_sprram_buffer[0x3f5];
	m_bg_scroll[0][2] = m_sprram_buffer[0x3f6];

	m_bg_scroll[1][0] = m_sprram_buffer[0x3fc];
	m_bg_scroll[1][1] = m_sprram_buffer[0x3fd];
	m_bg_scroll[1][2] = m_sprram_buffer[0x3fe];
}