summaryrefslogtreecommitdiffstatshomepage
path: root/src/devices/video/jangou_blitter.cpp
blob: a866d13777dbff92dbd6b63281e9ce17658da60d (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:Angelo Salese
/***************************************************************************

    Jangou Custom Blitter Chip, codename "???" (name scratched afaik)

    device emulation by Angelo Salese, from original jangou.cpp implementation
     by Angelo Salese, David Haywood and Phil Bennett.

    TODO:
    - BLTFLIP mechanism;
    - clean-ups;

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

#include "emu.h"
#include "jangou_blitter.h"


#define DEBUG_OUT_OF_MASK 0

//**************************************************************************
//  GLOBAL VARIABLES
//**************************************************************************

// device type definition
DEFINE_DEVICE_TYPE(JANGOU_BLITTER, jangou_blitter_device, "jangou_blitter", "Jangou Blitter Custom Chip")


//**************************************************************************
//  LIVE DEVICE
//**************************************************************************

//-------------------------------------------------
//  jangou_blitter_device - constructor
//-------------------------------------------------

jangou_blitter_device::jangou_blitter_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
	: device_t(mconfig, JANGOU_BLITTER, tag, owner, clock)
{
}


void jangou_blitter_device::blit_v1_regs(address_map &map)
{
	map(0x00, 0x00).w(FUNC(jangou_blitter_device::src_lo_address_w));
	map(0x01, 0x01).w(FUNC(jangou_blitter_device::src_md_address_w));
	map(0x02, 0x02).w(FUNC(jangou_blitter_device::x_w));
	map(0x03, 0x03).w(FUNC(jangou_blitter_device::y_w));
	map(0x04, 0x04).w(FUNC(jangou_blitter_device::width_w));
	map(0x05, 0x05).w(FUNC(jangou_blitter_device::height_and_trigger_w));
	map(0x06, 0x06).w(FUNC(jangou_blitter_device::src_hi_address_w));
}

// Sexy Gal and variants (v2) swaps around upper src address
void jangou_blitter_device::blit_v2_regs(address_map &map)
{
	map(0x00, 0x00).w(FUNC(jangou_blitter_device::src_lo_address_w));
	map(0x01, 0x01).w(FUNC(jangou_blitter_device::src_md_address_w));
	map(0x02, 0x02).w(FUNC(jangou_blitter_device::src_hi_address_w));
	map(0x03, 0x03).w(FUNC(jangou_blitter_device::x_w));
	map(0x04, 0x04).w(FUNC(jangou_blitter_device::y_w));
	map(0x05, 0x05).w(FUNC(jangou_blitter_device::width_w));
	map(0x06, 0x06).w(FUNC(jangou_blitter_device::height_and_trigger_w));
}

//-------------------------------------------------
//  device_start - device-specific startup
//-------------------------------------------------

void jangou_blitter_device::device_start()
{
	memory_region *devregion =  machine().root_device().memregion("gfx");
	m_gfxrom = devregion->base();
	if (m_gfxrom == nullptr)
		fatalerror("JANGOU_BLITTER: \"gfx\" memory base not found");
	m_gfxrommask = devregion->bytes()-1;

	save_item(NAME(m_pen_data));
	save_item(NAME(m_x));
	save_item(NAME(m_y));
	save_item(NAME(m_width));
	save_item(NAME(m_height));
	save_item(NAME(m_src_addr));
	save_item(NAME(m_blit_buffer));
}


//-------------------------------------------------
//  device_reset - device-specific reset
//-------------------------------------------------

void jangou_blitter_device::device_reset()
{
	memset(m_pen_data, 0, ARRAY_LENGTH(m_pen_data));
	m_bltflip = false;
}


//**************************************************************************
//  READ/WRITE HANDLERS
//**************************************************************************

// TODO: inline these
uint8_t jangou_blitter_device::gfx_nibble( uint32_t niboffset )
{
	if (niboffset & 1)
		return (m_gfxrom[(niboffset >> 1) & m_gfxrommask] & 0xf0) >> 4;
	else
		return (m_gfxrom[(niboffset >> 1) & m_gfxrommask] & 0x0f);
}

void jangou_blitter_device::plot_gfx_pixel( uint8_t pix, int x, int y )
{
	if (y < 0 || y >= 256)
		return;
	if (x < 0 || x >= 256)
		return;

	if (x & 1)
		m_blit_buffer[(y * 256) + (x >> 1)] = (m_blit_buffer[(y * 256) + (x >> 1)] & 0x0f) | ((pix << 4) & 0xf0);
	else
		m_blit_buffer[(y * 256) + (x >> 1)] = (m_blit_buffer[(y * 256) + (x >> 1)] & 0xf0) | (pix & 0x0f);
}

void jangou_blitter_device::trigger_write(void)
{
	int src, x, y, h, w, flipx;
	int count = 0;
	int xcount, ycount;
#if DEBUG_OUT_OF_MASK
	bool debug_flag;
#endif

	w = (m_width & 0xff) + 1;
	h = (m_height & 0xff) + 1;

	src = m_src_addr & m_gfxrommask;
	if(m_src_addr & ~m_gfxrommask)
	{
		logerror("%s: Warning blit src address = %08x above ROM mask %08x\n",this->tag(),m_src_addr,m_gfxrommask);
#if DEBUG_OUT_OF_MASK
		debug_flag = true;
#endif
	}
#if DEBUG_OUT_OF_MASK
	else
		debug_flag = false;
#endif

	x = (m_x & 0xff);
	y = (m_y & 0xff);

	#if 0
	// bail out if parameters are blantantly invalid (an indication that the host is using protection tho)
	if((x + w) > 256 || (y + h) > 256)
	{
		printf("%d %d %d %d %08x\n",x,y,w,h,src);
//      return;
	}
	#endif

	// lowest bit of src controls flipping / draw direction?
	flipx = (m_src_addr & 1);

	if (!flipx)
		src += (w * h) - 1;
	else
		src -= (w * h) - 1;

	for (ycount = 0; ycount < h; ycount++)
	{
		for(xcount = 0; xcount < w; xcount++)
		{
			int drawx = (x + xcount) & 0xff;
			int drawy = (y + ycount) & 0xff;
			uint8_t dat = gfx_nibble(src + count);
			uint8_t cur_pen = m_pen_data[dat & 0x0f];

#if DEBUG_OUT_OF_MASK
			if(debug_flag == true)
				cur_pen = machine().rand() & 0xf;
#endif
			//dat = cur_pen_lo | (cur_pen_hi << 4);

			if ((cur_pen & 0xff) != 0)
				plot_gfx_pixel(cur_pen, drawx, drawy);

			if (!flipx)
				count--;
			else
				count++;
		}
	}

	//uint32_t new_src = src + count;

	// update source and height after blitter operation
	// TODO: Jangou doesn't agree with this, later HW?
	#if 0
	m_blit_data[0] = new_src & 0xfe;
	m_blit_data[1] = new_src >> 8;
	m_blit_data[5] = 0;
	m_blit_data[6] = new_src >> 16;
	#endif
	m_bltflip = false;
}

void jangou_blitter_device::vregs_w(offs_t offset, uint8_t data)
{
	// bit 5 set by Jangou, left-over?
	m_pen_data[offset] = data & 0x0f;
}

void jangou_blitter_device::bltflip_w(uint8_t data)
{
	// TODO: unsure about how this works, Charles says it swaps the nibble but afaik it's used for CPU tiles in Night Gal Summer/Sexy Gal and they seems fine?
	//       Maybe flipx is actually bltflip for later HW?
	m_bltflip = true;
}

int jangou_blitter_device::status_r()
{
	return false;
}

// data accessors

void jangou_blitter_device::x_w(uint8_t data) { m_x = data; }
void jangou_blitter_device::y_w(uint8_t data) { m_y = data; }
void jangou_blitter_device::src_lo_address_w(uint8_t data)
{
	m_src_addr &= ~0xff;
	m_src_addr |= data & 0xff;
}

void jangou_blitter_device::src_md_address_w(uint8_t data)
{
	m_src_addr &= ~0xff00;
	m_src_addr |= data << 8;
}

void jangou_blitter_device::src_hi_address_w(uint8_t data)
{
	m_src_addr &= ~0xff0000;
	m_src_addr |= data << 16;
}

void jangou_blitter_device::width_w(uint8_t data)
{
	m_width = data;
}

void jangou_blitter_device::height_and_trigger_w(uint8_t data)
{
	m_height = data;
	trigger_write();
}