summaryrefslogtreecommitdiffstatshomepage
path: root/src/mame/video/tmap038.cpp
blob: 38259e030e8ac56ff2964ddb6c88d823cc425f92 (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
// license:BSD-3-Clause
// copyright-holders:Luca Elia, Paul Priest, David Haywood
/* 038 Tilemap generator manufactured by NEC */

/*
    [ Scrolling Layers ]

        Each 038 chip generates 1 layer. Up to 4 chips are used
        (4 layers)

        Layer Size:             512 x 512
        Tiles:                  8 x 8 & 16 x 16.

        There are 2 tilemaps in memory, one per tiles dimension.
        A bit decides which one gets displayed.
        The tiles depth varies with games, from 16 to 256 colors.

        A per layer row-scroll / row-select effect can be enabled:

        a different scroll value is fetched (from tile RAM) for each
        scan line, and a different tilemap line for each scan line



***************************************************************************

                        Callbacks for the TileMap code

                              [ Tiles Format ]

    Offset:     Bits:                   Value:

    0.w         fe-- ---- ---- ---      Priority
                --dc ba98 ---- ----     Color
                ---- ---- 7654 3210

    2.w                                 Code


    When a row-scroll / row-select effect is enabled, the scroll values are
    fetched starting from tile RAM + $1000, 4 bytes per scan line:

    Offset:     Value:

    0.w         Tilemap line to display
    2.w         X Scroll value

***************************************************************************

***************************************************************************

                            Layers Registers


        Offset:     Bits:                   Value:

        0.w         f--- ---- ---- ----     0 = Layer Flip X
                    -e-- ---- ---- ----     Activate Row-scroll
                    --d- ---- ---- ----
                    ---c ba9- ---- ----
                    ---- ---8 7654 3210     Scroll X

        2.w         f--- ---- ---- ----     0 = Layer Flip Y
                    -e-- ---- ---- ----     Activate Row-select
                    --d- ---- ---- ----     0 = 8x8 tiles, 1 = 16x16 tiles
                    ---c ba9- ---- ----
                    ---- ---8 7654 3210     Scroll Y

        4.w         fedc ba98 765- ----
                    ---- ---- ---4 ----     Layer Disable
                    ---- ---- ---- 3210     Varies*

        *color bank for mcatadv or Layer-Layer priority for cave

There are more!

***************************************************************************

TODO:
    de-fragmentation and merge drawing behavior into tmap038.cpp

***************************************************************************

*/

#include "emu.h"
#include "tmap038.h"

void tilemap038_device::vram_map(address_map &map)
{
	map(0x0000, 0x0fff).rw(FUNC(tilemap038_device::vram_16x16_r), FUNC(tilemap038_device::vram_16x16_w)).share("vram_16x16");
	map(0x1000, 0x17ff).rw(FUNC(tilemap038_device::lineram_r), FUNC(tilemap038_device::lineram_w)).share("lineram");
	map(0x1800, 0x3fff).ram().share("scratchpad"); // scratchpad?
	map(0x4000, 0x7fff).rw(FUNC(tilemap038_device::vram_8x8_r), FUNC(tilemap038_device::vram_8x8_w)).share("vram_8x8");
}

void tilemap038_device::vram_writeonly_map(address_map &map)
{
	map(0x0000, 0x0fff).w(FUNC(tilemap038_device::vram_16x16_w)).share("vram_16x16");
	map(0x1000, 0x17ff).w(FUNC(tilemap038_device::lineram_w)).share("lineram");
	map(0x1800, 0x3fff).writeonly().share("scratchpad"); // scratchpad?
	map(0x4000, 0x7fff).w(FUNC(tilemap038_device::vram_8x8_w)).share("vram_8x8");
}

void tilemap038_device::vram_16x16_map(address_map &map)
{
	map(0x0000, 0x0fff).rw(FUNC(tilemap038_device::vram_16x16_r), FUNC(tilemap038_device::vram_16x16_w)).share("vram_16x16");
	map(0x1000, 0x17ff).rw(FUNC(tilemap038_device::lineram_r), FUNC(tilemap038_device::lineram_w)).share("lineram");
	map(0x1800, 0x3fff).ram().share("scratchpad"); // scratchpad?
}

void tilemap038_device::vram_16x16_writeonly_map(address_map &map)
{
	map(0x0000, 0x0fff).w(FUNC(tilemap038_device::vram_16x16_w)).share("vram_16x16");
	map(0x1000, 0x17ff).w(FUNC(tilemap038_device::lineram_w)).share("lineram");
	map(0x1800, 0x3fff).writeonly().share("scratchpad"); // scratchpad?
}

/*  Some games, that only ever use the 8x8 tiles and no line scroll,
    use mirror ram. For example in donpachi, writes to 400000-403fff
    and 408000-407fff both go to the 8x8 tilemap ram. Use this function
    in this cases. Note that the get_tile_info function looks in the
    4000-7fff range for tiles, so we have to write the data there. */
void tilemap038_device::vram_8x8_map(address_map &map)
{
	map(0x0000, 0x3fff).rw(FUNC(tilemap038_device::vram_8x8_r), FUNC(tilemap038_device::vram_8x8_w)).share("vram_8x8");
}

DEFINE_DEVICE_TYPE(TMAP038, tilemap038_device, "tmap038", "038 Tilemap generator")

tilemap038_device::tilemap038_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock)
	: device_t(mconfig, TMAP038, tag, owner, clock)
	, m_vram_8x8(*this, "vram_8x8")
	, m_vram_16x16(*this, "vram_16x16")
	, m_lineram(*this, "lineram")
	, m_vregs(nullptr)
	, m_tiledim(false)
	, m_gfxdecode(*this, finder_base::DUMMY_TAG)
	, m_gfxno(0)
	, m_038_cb(*this)
{
}

TILE_GET_INFO_MEMBER(tilemap038_device::get_tile_info)
{
	u32 tile, code = 0, color = 0, pri = 0;

	if (tile_is_16x16())
	{
		tile  = (tile_index % (512 / 8)) / 2 + ((tile_index / (512 / 8)) / 2) * (512 / 16);
		tile  = (m_vram_16x16 != nullptr) ? ((m_vram_16x16[tile * 2] << 16) + m_vram_16x16[(tile * 2) + 1]) : 0;

		color = (tile & 0x3f000000) >> (32 - 8);
		pri   = (tile & 0xc0000000) >> (32 - 2);
		code  = (tile & 0x0000ffff) * 4;

		code ^= tile_index & 1;
		code ^= ((tile_index / (512 / 8)) & 1) * 2;

		if (!m_038_cb.isnull())
			m_038_cb(true, color, pri, code);
	}
	else if (tile_is_8x8())
	{
		tile  = (m_vram_8x8 != nullptr) ? ((m_vram_8x8[tile_index * 2] << 16) + m_vram_8x8[(tile_index * 2) + 1]) : 0;

		color = (tile & 0x3f000000) >> (32 - 8);
		pri   = (tile & 0xc0000000) >> (32 - 2);
		code  = (tile & 0x0003ffff);

		if (!m_038_cb.isnull())
			m_038_cb(false, color, pri, code);
	}

	SET_TILE_INFO_MEMBER(m_gfxno, code, color, 0);
	tileinfo.category = pri;
}


void tilemap038_device::device_start()
{
	m_038_cb.resolve();
	m_vregs = make_unique_clear<u16[]>(0x6/2);

	if (m_vram_16x16 == nullptr && m_vram_8x8 == nullptr)
		fatalerror("Tilemap 038 %s: VRAM not found",this->tag());

	m_tmap = &machine().tilemap().create(
			*m_gfxdecode,
			tilemap_get_info_delegate(*this, FUNC(tilemap038_device::get_tile_info)),
			TILEMAP_SCAN_ROWS,
			8,8, 512 / 8,512 / 8);

	m_tmap->set_transparent_pen(0);

	set_scroll_rows(1);  // Initialize scroll rows
	set_scroll_cols(1);  // Initialize scroll cols

	save_item(NAME(m_tiledim));
	save_pointer(NAME(m_vregs), 0x6/2);
}

void tilemap038_device::device_reset()
{
}

u16 tilemap038_device::vram_8x8_r(offs_t offset)
{
	return m_vram_8x8[offset];
}

void tilemap038_device::vram_8x8_w(offs_t offset, u16 data, u16 mem_mask)
{
	COMBINE_DATA(&m_vram_8x8[offset]);
	if (tile_is_8x8())
		m_tmap->mark_tile_dirty(offset >> 1);
}

u16 tilemap038_device::vram_16x16_r(offs_t offset)
{
	return m_vram_16x16[offset];
}

void tilemap038_device::vram_16x16_w(offs_t offset, u16 data, u16 mem_mask)
{
	COMBINE_DATA(&m_vram_16x16[offset]);
	if (tile_is_16x16())
	{
		offset >>= 1;
		offset = (offset % (512 / 16)) * 2 + (offset / (512 / 16)) * (512 / 8) * 2;
		m_tmap->mark_tile_dirty(offset + 0);
		m_tmap->mark_tile_dirty(offset + 1);
		m_tmap->mark_tile_dirty(offset + 0 + 512 / 8);
		m_tmap->mark_tile_dirty(offset + 1 + 512 / 8);
	}
}

void tilemap038_device::prepare()
{
	// refresh tile size
	if (m_vram_8x8 != nullptr && m_vram_16x16 != nullptr)
	{
		const bool new_tiledim = BIT(m_vregs[1], 13);
		if (m_tiledim != new_tiledim)
		{
			m_tmap->mark_all_dirty();
			m_tiledim = new_tiledim;
		}
	}
}

void tilemap038_device::draw(screen_device &screen, bitmap_ind16 &bitmap, const rectangle &cliprect, u32 flags, u8 pri, u8 pri_mask) { draw_common(screen, bitmap, cliprect, flags, pri, pri_mask); }
void tilemap038_device::draw(screen_device &screen, bitmap_rgb32 &bitmap, const rectangle &cliprect, u32 flags, u8 pri, u8 pri_mask) { draw_common(screen, bitmap, cliprect, flags, pri, pri_mask); }

template<class _BitmapClass>
void tilemap038_device::draw_common(screen_device &screen, _BitmapClass &bitmap, const rectangle &cliprect, u32 flags, u8 pri, u8 pri_mask)
{
	m_tmap->draw(screen, bitmap, cliprect, flags, pri, pri_mask);
}