summaryrefslogtreecommitdiffstats
path: root/src/mame/machine/ataristb.cpp
blob: e1daa535a5db102ed8ac366c50cdd8c9934d1e3d (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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
// license:BSD-3-Clause
// copyright-holders:Curt Coder, Olivier Galibert

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



//**************************************************************************
//  CONSTANTS / MACROS
//**************************************************************************

DEFINE_DEVICE_TYPE(ST_BLITTER, st_blitter_device, "st_blitter", "Atari ST Blitter")

static const int BLITTER_NOPS[16][4] =
{
	{ 1, 1, 1, 1 },
	{ 2, 2, 3, 3 },
	{ 2, 2, 3, 3 },
	{ 1, 1, 2, 2 },
	{ 2, 2, 3, 3 },
	{ 2, 2, 2, 2 },
	{ 2, 2, 3, 3 },
	{ 2, 2, 3, 3 },
	{ 2, 2, 3, 3 },
	{ 2, 2, 3, 3 },
	{ 2, 2, 2, 2 },
	{ 2, 2, 3, 3 },
	{ 1, 1, 2, 2 },
	{ 2, 2, 3, 3 },
	{ 2, 2, 3, 3 },
	{ 1, 1, 1, 1 }
};


//**************************************************************************
//  BLITTER
//**************************************************************************

//-------------------------------------------------
//  blitter_source -
//-------------------------------------------------

void st_blitter_device::blitter_source()
{
	uint16_t data = m_space->read_word(m_src);

	if (m_src_inc_x < 0)
	{
		m_srcbuf = (data << 16) | (m_srcbuf >> 16);
	}
	else
	{
		m_srcbuf = (m_srcbuf << 16) | data;
	}
}


//-------------------------------------------------
//  blitter_hop -
//-------------------------------------------------

uint16_t st_blitter_device::blitter_hop()
{
	uint16_t source = m_srcbuf >> (m_skew & 0x0f);
	uint16_t halftone = m_halftone[m_ctrl & 0x0f];

	if (m_ctrl & CTRL_SMUDGE)
	{
		halftone = m_halftone[source & 0x0f];
	}

	switch (m_hop)
	{
	case 0:
		return 0xffff;
	case 1:
		return halftone;
	case 2:
		return source;
	case 3:
		return source & halftone;
	}

	return 0;
}


//-------------------------------------------------
//  blitter_op -
//-------------------------------------------------

void st_blitter_device::blitter_op(uint16_t s, uint32_t dstaddr, uint16_t mask)
{
	uint16_t d = m_space->read_word(dstaddr);
	uint16_t result = 0;

	if (m_op & 0x08) result = (~s & ~d);
	if (m_op & 0x04) result |= (~s & d);
	if (m_op & 0x02) result |= (s & ~d);
	if (m_op & 0x01) result |= (s & d);

	m_space->write_word(dstaddr, result);
}


//-------------------------------------------------
//  blitter_tick -
//-------------------------------------------------

TIMER_CALLBACK_MEMBER(st_blitter_device::blitter_tick)
{
	do
	{
		if (m_skew & SKEW_FXSR)
		{
			blitter_source();
			m_src += m_src_inc_x;
		}

		blitter_source();
		blitter_op(blitter_hop(), m_dst, m_endmask1);
		m_xcount--;

		while (m_xcount > 0)
		{
			m_src += m_src_inc_x;
			m_dst += m_dst_inc_x;

			if (m_xcount == 1)
			{
				if (!(m_skew & SKEW_NFSR))
				{
					blitter_source();
				}

				blitter_op(blitter_hop(), m_dst, m_endmask3);
			}
			else
			{
				blitter_source();
				blitter_op(blitter_hop(), m_dst, m_endmask2);
			}

			m_xcount--;
		}

		m_src += m_src_inc_y;
		m_dst += m_dst_inc_y;

		if (m_dst_inc_y < 0)
		{
			m_ctrl = (m_ctrl & 0xf0) | (((m_ctrl & 0x0f) - 1) & 0x0f);
		}
		else
		{
			m_ctrl = (m_ctrl & 0xf0) | (((m_ctrl & 0x0f) + 1) & 0x0f);
		}

		m_xcount = m_xcountl;
		m_ycount--;
	}
	while (m_ycount > 0);

	m_ctrl &= 0x7f;

	m_int_callback(0); // active low
}


//-------------------------------------------------
//  halftone_r -
//-------------------------------------------------

uint16_t st_blitter_device::halftone_r(offs_t offset)
{
	return m_halftone[offset];
}


//-------------------------------------------------
//  src_inc_x_r -
//-------------------------------------------------

uint16_t st_blitter_device::src_inc_x_r()
{
	return m_src_inc_x;
}


//-------------------------------------------------
//  src_inc_y_r -
//-------------------------------------------------

uint16_t st_blitter_device::src_inc_y_r()
{
	return m_src_inc_y;
}


//-------------------------------------------------
//  src_r -
//-------------------------------------------------

uint16_t st_blitter_device::src_r(offs_t offset)
{
	switch (offset)
	{
	case 0:
		return (m_src >> 16) & 0xff;
	case 1:
		return m_src & 0xfffe;
	}

	return 0;
}


//-------------------------------------------------
//  end_mask_r -
//-------------------------------------------------

uint16_t st_blitter_device::end_mask_r(offs_t offset)
{
	switch (offset)
	{
	case 0:
		return m_endmask1;
	case 1:
		return m_endmask2;
	case 2:
		return m_endmask3;
	}

	return 0;
}


//-------------------------------------------------
//  dst_inc_x_r -
//-------------------------------------------------

uint16_t st_blitter_device::dst_inc_x_r()
{
	return m_dst_inc_x;
}


//-------------------------------------------------
//  dst_inc_y_r -
//-------------------------------------------------

uint16_t st_blitter_device::dst_inc_y_r()
{
	return m_dst_inc_y;
}


//-------------------------------------------------
//  blitter_dst_r -
//-------------------------------------------------

uint16_t st_blitter_device::dst_r(offs_t offset)
{
	switch (offset)
	{
	case 0:
		return (m_dst >> 16) & 0xff;
	case 1:
		return m_dst & 0xfffe;
	}

	return 0;
}


//-------------------------------------------------
//  count_x_r -
//-------------------------------------------------

uint16_t st_blitter_device::count_x_r()
{
	return m_xcount;
}


//-------------------------------------------------
//  count_y_r -
//-------------------------------------------------

uint16_t st_blitter_device::count_y_r()
{
	return m_ycount;
}


//-------------------------------------------------
//  op_r -
//-------------------------------------------------

uint16_t st_blitter_device::op_r(offs_t offset, uint16_t mem_mask)
{
	if (ACCESSING_BITS_0_7)
	{
		return m_hop;
	}
	else
	{
		return m_op;
	}
}


//-------------------------------------------------
//  ctrl_r -
//-------------------------------------------------

uint16_t st_blitter_device::ctrl_r(offs_t offset, uint16_t mem_mask)
{
	if (ACCESSING_BITS_0_7)
	{
		return m_ctrl;
	}
	else
	{
		return m_skew;
	}
}


//-------------------------------------------------
//  halftone_w -
//-------------------------------------------------

void st_blitter_device::halftone_w(offs_t offset, uint16_t data)
{
	m_halftone[offset] = data;
}


//-------------------------------------------------
//  src_inc_x_w -
//-------------------------------------------------

void st_blitter_device::src_inc_x_w(uint16_t data)
{
	m_src_inc_x = data & 0xfffe;
}


//-------------------------------------------------
//  src_inc_y_w -
//-------------------------------------------------

void st_blitter_device::src_inc_y_w(uint16_t data)
{
	m_src_inc_y = data & 0xfffe;
}


//-------------------------------------------------
//  src_w -
//-------------------------------------------------

void st_blitter_device::src_w(offs_t offset, uint16_t data)
{
	switch (offset)
	{
	case 0:
		m_src = (data & 0xff) | (m_src & 0xfffe);
		break;

	case 1:
		m_src = (m_src & 0xff0000) | (data & 0xfffe);
		break;
	}
}


//-------------------------------------------------
//  end_mask_w -
//-------------------------------------------------

void st_blitter_device::end_mask_w(offs_t offset, uint16_t data)
{
	switch (offset)
	{
	case 0:
		m_endmask1 = data;
		break;

	case 1:
		m_endmask2 = data;
		break;

	case 2:
		m_endmask3 = data;
		break;
	}
}


//-------------------------------------------------
//  dst_inc_x_w -
//-------------------------------------------------

void st_blitter_device::dst_inc_x_w(uint16_t data)
{
	m_dst_inc_x = data & 0xfffe;
}


//-------------------------------------------------
//  dst_inc_y_w -
//-------------------------------------------------

void st_blitter_device::dst_inc_y_w(uint16_t data)
{
	m_dst_inc_y = data & 0xfffe;
}


//-------------------------------------------------
//  dst_w -
//-------------------------------------------------

void st_blitter_device::dst_w(offs_t offset, uint16_t data)
{
	switch (offset)
	{
	case 0:
		m_dst = (data & 0xff) | (m_dst & 0xfffe);
		break;

	case 1:
		m_dst = (m_dst & 0xff0000) | (data & 0xfffe);
		break;
	}
}


//-------------------------------------------------
//  count_x_w -
//-------------------------------------------------

void st_blitter_device::count_x_w(uint16_t data)
{
	m_xcount = data;
}


//-------------------------------------------------
//  count_y_w -
//-------------------------------------------------

void st_blitter_device::count_y_w(uint16_t data)
{
	m_ycount = data;
}


//-------------------------------------------------
//  op_w -
//-------------------------------------------------

void st_blitter_device::op_w(offs_t offset, uint16_t data, uint16_t mem_mask)
{
	if (ACCESSING_BITS_0_7)
	{
		m_hop = (data >> 8) & 0x03;
	}
	else
	{
		m_op = data & 0x0f;
	}
}


//-------------------------------------------------
//  ctrl_w -
//-------------------------------------------------

void st_blitter_device::ctrl_w(offs_t offset, uint16_t data, uint16_t mem_mask)
{
	if (ACCESSING_BITS_0_7)
	{
		m_ctrl = (data >> 8) & 0xef;

		if (!(m_ctrl & CTRL_BUSY))
		{
			if ((data >> 8) & CTRL_BUSY)
			{
				m_int_callback(1);

				int nops = BLITTER_NOPS[m_op][m_hop]; // each NOP takes 4 cycles
				m_blitter_timer->adjust(clocks_to_attotime(4*nops));
			}
		}
	}
	else
	{
		m_skew = data & 0xcf;
	}
}



//**************************************************************************
//  VIDEO
//**************************************************************************

st_blitter_device::st_blitter_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
	: device_t(mconfig, ST_BLITTER, tag, owner, clock)
	, m_space(*this, finder_base::DUMMY_TAG, 0)
	, m_int_callback(*this)
	, m_blitter_timer(nullptr)
{
}


void st_blitter_device::device_resolve_objects()
{
	m_int_callback.resolve_safe();
}


void st_blitter_device::device_start()
{
	m_blitter_timer = timer_alloc(FUNC(st_blitter_device::blitter_tick), this);

	// register for state saving
	save_item(NAME(m_halftone));
	save_item(NAME(m_src_inc_x));
	save_item(NAME(m_src_inc_y));
	save_item(NAME(m_dst_inc_x));
	save_item(NAME(m_dst_inc_y));
	save_item(NAME(m_src));
	save_item(NAME(m_dst));
	save_item(NAME(m_endmask1));
	save_item(NAME(m_endmask2));
	save_item(NAME(m_endmask3));
	save_item(NAME(m_xcount));
	save_item(NAME(m_ycount));
	save_item(NAME(m_xcountl));
	save_item(NAME(m_hop));
	save_item(NAME(m_op));
	save_item(NAME(m_ctrl));
	save_item(NAME(m_skew));
}


void st_blitter_device::device_reset()
{
}