summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/machine/seibu_cop.c
blob: 7418ea81ee7674af0e9b8b138a24c4a5c7d61889 (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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/***************************************************************************

    Seibu COP protection device

    (this header needs expanding!)

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

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



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

// device type definition
const device_type SEIBU_COP = &device_creator<seibu_cop_device>;


static ADDRESS_MAP_START( seibu_cop_io, AS_0, 16, seibu_cop_device )
	AM_RANGE(0x0428, 0x0429) AM_WRITE(fill_val_lo_w)
	AM_RANGE(0x042a, 0x042b) AM_WRITE(fill_val_hi_w)

	AM_RANGE(0x045a, 0x045b) AM_WRITE(pal_brightness_val_w)
	AM_RANGE(0x045c, 0x045d) AM_WRITE(pal_brightness_mode_w)

	AM_RANGE(0x0474, 0x0475) AM_WRITE(dma_unk_param_w)
	AM_RANGE(0x0476, 0x0477) AM_WRITE(dma_pal_fade_table_w)
	AM_RANGE(0x0478, 0x0479) AM_WRITE(dma_src_w)
	AM_RANGE(0x047a, 0x047b) AM_WRITE(dma_size_w)
	AM_RANGE(0x047c, 0x047d) AM_WRITE(dma_dst_w)
	AM_RANGE(0x047e, 0x047f) AM_READWRITE(dma_trigger_r, dma_trigger_w)
ADDRESS_MAP_END


//**************************************************************************
//  INLINE HELPERS
//**************************************************************************

//-------------------------------------------------
//  readbyte - read a byte at the given address
//-------------------------------------------------

inline UINT16 seibu_cop_device::read_word(offs_t address)
{
	return space().read_word(address);
}

//-------------------------------------------------
//  writebyte - write a byte at the given address
//-------------------------------------------------

inline void seibu_cop_device::write_word(offs_t address, UINT16 data)
{
	space().write_word(address, data);
}


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

//-------------------------------------------------
//  seibu_cop_device - constructor
//-------------------------------------------------

seibu_cop_device::seibu_cop_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
	: device_t(mconfig, SEIBU_COP, "seibu_cop", tag, owner, clock),
		device_memory_interface(mconfig, *this),
		m_space_config("io", ENDIANNESS_LITTLE, 16, 16, 0, NULL, *ADDRESS_MAP_NAME(seibu_cop_io))
{
}


//-------------------------------------------------
//  device_config_complete - perform any
//  operations now that the configuration is
//  complete
//-------------------------------------------------

void seibu_cop_device::device_config_complete()
{
	// inherit a copy of the static data
	const seibu_cop_interface *intf = reinterpret_cast<const seibu_cop_interface *>(static_config());
	if (intf != NULL)
		*static_cast<seibu_cop_interface *>(this) = *intf;

	// or initialize to defaults if none provided
	else
	{
		memset(&m_in_byte_cb,   0, sizeof(m_in_byte_cb));
		memset(&m_in_word_cb,   0, sizeof(m_in_word_cb));
		memset(&m_in_dword_cb,  0, sizeof(m_in_dword_cb));
		memset(&m_out_byte_cb,  0, sizeof(m_out_byte_cb));
		memset(&m_out_word_cb,  0, sizeof(m_out_word_cb));
		memset(&m_out_dword_cb, 0, sizeof(m_out_dword_cb));
	}
}

//-------------------------------------------------
//  device_validity_check - perform validity checks
//  on this device
//-------------------------------------------------

void seibu_cop_device::device_validity_check(validity_checker &valid) const
{
}


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

void seibu_cop_device::device_start()
{
	// resolve callbacks
	m_in_byte_func.resolve(m_in_byte_cb, *this);
	m_in_word_func.resolve(m_in_word_cb, *this);
	m_in_word_func.resolve(m_in_dword_cb, *this);
	m_out_byte_func.resolve(m_out_byte_cb, *this);
	m_out_word_func.resolve(m_out_word_cb, *this);
	m_out_dword_func.resolve(m_out_dword_cb, *this);

}


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

void seibu_cop_device::device_reset()
{
}

//-------------------------------------------------
//  memory_space_config - return a description of
//  any address spaces owned by this device
//-------------------------------------------------

const address_space_config *seibu_cop_device::memory_space_config(address_spacenum spacenum) const
{
	return (spacenum == AS_0) ? &m_space_config : NULL;
}

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

WRITE16_MEMBER(seibu_cop_device::fill_val_lo_w)
{
	COMBINE_DATA(&m_fill_val_lo);
	m_fill_val = (m_fill_val_lo) | (m_fill_val_hi << 16);
}

WRITE16_MEMBER(seibu_cop_device::fill_val_hi_w)
{
	COMBINE_DATA(&m_fill_val_hi);
	m_fill_val = (m_fill_val_lo) | (m_fill_val_hi << 16);
}

WRITE16_MEMBER(seibu_cop_device::pal_brightness_val_w)
{
	COMBINE_DATA(&m_pal_brightness_val);

	/* TODO: add checks for bits 15-6 */
}

WRITE16_MEMBER(seibu_cop_device::pal_brightness_mode_w)
{
	COMBINE_DATA(&m_pal_brightness_mode);

	/* TODO: add checks for anything that isn't 4 or 5 */
}

WRITE16_MEMBER(seibu_cop_device::dma_unk_param_w)
{
	/*
	    This sets up a DMA mode of some sort
	        0x0e00: grainbow, cupsoc
	        0x0a00: legionna, godzilla, denjinmk
	        0x0600: heatbrl
	        0x1e00: zeroteam, xsedae
	    raiden2 and raidendx doesn't set this up, this could indicate that this is related to the non-private buffer DMAs
	    (both only uses 0x14 and 0x15 as DMAs afaik)
	*/
	COMBINE_DATA(&m_dma_unk_param);
}

WRITE16_MEMBER(seibu_cop_device::dma_pal_fade_table_w)
{
	COMBINE_DATA(&m_dma_pal_fade_table);
}

WRITE16_MEMBER(seibu_cop_device::dma_src_w)
{
	COMBINE_DATA(&m_dma_src[m_dma_trigger]);
}

WRITE16_MEMBER(seibu_cop_device::dma_size_w)
{
	COMBINE_DATA(&m_dma_size[m_dma_trigger]);
}

WRITE16_MEMBER(seibu_cop_device::dma_dst_w)
{
	COMBINE_DATA(&m_dma_dst[m_dma_trigger]);
}

READ16_MEMBER(seibu_cop_device::dma_trigger_r)
{
	return m_dma_exec_param;
}

WRITE16_MEMBER(seibu_cop_device::dma_trigger_w)
{
	COMBINE_DATA(&m_dma_exec_param);
	m_dma_trigger = m_dma_exec_param & 7;
}

//**************************************************************************
//  READ/WRITE HANDLERS (device to CPU / CPU to device)
//**************************************************************************

READ16_MEMBER( seibu_cop_device::read )
{
	return read_word(offset + (0x400/2));
}

WRITE16_MEMBER( seibu_cop_device::write )
{
	write_word(offset + (0x400/2),data);
}

void seibu_cop_device::normal_dma_transfer(void)
{
	UINT32 src,dst,size,i;

	src = (m_dma_src[m_dma_trigger] << 6);
	dst = (m_dma_dst[m_dma_trigger] << 6);
	size = ((m_dma_size[m_dma_trigger] << 5) - (m_dma_dst[m_dma_trigger] << 6) + 0x20)/2;

	for(i = 0;i < size;i++)
	{
		m_out_word_func(dst, m_in_word_func(src));
		src+=2;
		dst+=2;
	}
}

/* RE from Seibu Cup Soccer bootleg */
const UINT8 seibu_cop_device::fade_table(int v)
{
	int low  = v & 0x001f;
	int high = v & 0x03e0;

	return (low * (high | (high >> 5)) + 0x210) >> 10;
}

void seibu_cop_device::palette_dma_transfer(void)
{
	UINT32 src,dst,size,i;

	/*
	         Apparently all of those are just different DMA channels, brightness effects are done through a RAM table and the pal_brightness_val / mode
	         0x80 is used by Legionnaire
	         0x81 is used by SD Gundam and Godzilla
	         0x82 is used by Zero Team and X Se Dae
	         0x86 is used by Seibu Cup Soccer
	         0x87 is used by Denjin Makai

	        TODO:
	         - Denjin Makai mode 4 is totally guessworked.
	         - SD Gundam doesn't fade colors correctly, it should have the text layer / sprites with normal gradient and the rest dimmed in most cases,
	           presumably bad RAM table or bad algorithm
	*/

	src = (m_dma_src[m_dma_trigger] << 6);
	dst = (m_dma_dst[m_dma_trigger] << 6);
	size = ((m_dma_size[m_dma_trigger] << 5) - (m_dma_dst[m_dma_trigger] << 6) + 0x20)/2;

	//printf("SRC: %08x %08x DST:%08x SIZE:%08x TRIGGER: %08x %02x %02x\n",cop_dma_src[cop_dma_trigger] << 6,cop_dma_fade_table * 0x400,cop_dma_dst[cop_dma_trigger] << 6,cop_dma_size[cop_dma_trigger] << 5,cop_dma_trigger,pal_brightness_val,pal_brightness_mode);

	for(i = 0;i < size;i++)
	{
		UINT16 pal_val;
		int r,g,b;
		int rt,gt,bt;

		if(m_pal_brightness_mode == 5)
		{
			bt = ((m_in_word_func(src + (m_dma_pal_fade_table * 0x400))) & 0x7c00) >> 5;
			bt = fade_table(bt|(m_pal_brightness_val ^ 0));
			b = ((m_in_word_func(src)) & 0x7c00) >> 5;
			b = fade_table(b|(m_pal_brightness_val ^ 0x1f));
			pal_val = ((b + bt) & 0x1f) << 10;
			gt = ((m_in_word_func(src + (m_dma_pal_fade_table * 0x400))) & 0x03e0);
			gt = fade_table(gt|(m_pal_brightness_val ^ 0));
			g = ((m_in_word_func(src)) & 0x03e0);
			g = fade_table(g|(m_pal_brightness_val ^ 0x1f));
			pal_val |= ((g + gt) & 0x1f) << 5;
			rt = ((m_in_word_func(src + (m_dma_pal_fade_table * 0x400))) & 0x001f) << 5;
			rt = fade_table(rt|(m_pal_brightness_val ^ 0));
			r = ((m_in_word_func(src)) & 0x001f) << 5;
			r = fade_table(r|(m_pal_brightness_val ^ 0x1f));
			pal_val |= ((r + rt) & 0x1f);
		}
		else if(m_pal_brightness_mode == 4) //Denjin Makai
		{
			bt =(m_in_word_func(src + (m_dma_pal_fade_table * 0x400)) & 0x7c00) >> 10;
			b = (m_in_word_func(src) & 0x7c00) >> 10;
			gt =(m_in_word_func(src + (m_dma_pal_fade_table * 0x400)) & 0x03e0) >> 5;
			g = (m_in_word_func(src) & 0x03e0) >> 5;
			rt =(m_in_word_func(src + (m_dma_pal_fade_table * 0x400)) & 0x001f) >> 0;
			r = (m_in_word_func(src) & 0x001f) >> 0;

			if(m_pal_brightness_val == 0x10)
				pal_val = bt << 10 | gt << 5 | rt << 0;
			else if(m_pal_brightness_val == 0xff) // TODO: might be the back plane or it still doesn't do any mod, needs PCB tests
				pal_val = 0;
			else
			{
				bt = fade_table(bt<<5|((m_pal_brightness_val*2) ^ 0));
				b =  fade_table(b<<5|((m_pal_brightness_val*2) ^ 0x1f));
				pal_val = ((b + bt) & 0x1f) << 10;
				gt = fade_table(gt<<5|((m_pal_brightness_val*2) ^ 0));
				g =  fade_table(g<<5|((m_pal_brightness_val*2) ^ 0x1f));
				pal_val |= ((g + gt) & 0x1f) << 5;
				rt = fade_table(rt<<5|((m_pal_brightness_val*2) ^ 0));
				r =  fade_table(r<<5|((m_pal_brightness_val*2) ^ 0x1f));
				pal_val |= ((r + rt) & 0x1f);
			}
		}
		else
		{
			printf("Seibu COP: palette DMA used with mode %02x!\n",m_pal_brightness_mode);
			pal_val = m_in_word_func(src);
		}

		m_out_word_func(dst, pal_val);
		src+=2;
		dst+=2;
	}
}

void seibu_cop_device::fill_word_transfer(void)
{
	UINT32 length, address;
	int i;

	//if(cop_dma_dst[cop_dma_trigger] != 0x0000) // Invalid?
	//  return;

	address = (m_dma_src[m_dma_trigger] << 6);
	length = ((m_dma_size[m_dma_trigger]+1) << 4);

	for (i=address;i<address+length;i+=4)
	{
		m_out_dword_func(i, m_fill_val);
	}
}

void seibu_cop_device::fill_dword_transfer(void)
{
	UINT32 length, address;
	int i;
	if(m_dma_dst[m_dma_trigger] != 0x0000) // Invalid? TODO: log & check this
		return;

	address = (m_dma_src[m_dma_trigger] << 6);
	length = (m_dma_size[m_dma_trigger]+1) << 5;

	//printf("%08x %08x\n",address,length);

	for (i=address;i<address+length;i+=4)
	{
		m_out_dword_func(i, m_fill_val);
	}
}

WRITE16_MEMBER( seibu_cop_device::dma_write_trigger_w )
{
	switch(m_dma_exec_param & 0xfff8)
	{
		case 0x008: normal_dma_transfer(); break;
		case 0x010: break; // private buffer copy, TODO
		case 0x080: palette_dma_transfer(); break;
		case 0x110: fill_word_transfer(); break; // Godzilla uses this
		case 0x118: fill_dword_transfer(); break;
		default:
			logerror("Seibu COP: used unemulated DMA type %04x\n",m_dma_exec_param);
	}
}