summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/video/i8275x.c
blob: d0ac9a0c7c91018c0d49b9d1cfae4eda59257a2d (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
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
// license:BSD-3-Clause
// copyright-holders:Curt Coder
/**********************************************************************

    Intel 8275 Programmable CRT Controller emulation

    Copyright MESS Team.
    Visit http://mamedev.org for licensing and usage restrictions.

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

/*

    TODO:

    - double spaced rows

*/

#include "i8275x.h"



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

const int DMA_BURST_SPACING[] = { 0, 7, 15, 23, 31, 39, 47, 55 };

#define DOUBLE_SPACED_ROWS \
	BIT(m_param[REG_SCN1], 7)

#define CHARACTERS_PER_ROW \
	((m_param[REG_SCN1] & 0x7f) + 1)

#define VRTC_ROW_COUNT \
	((m_param[REG_SCN2] >> 5) + 1)

#define CHARACTER_ROWS_PER_FRAME \
	((m_param[REG_SCN2] & 0x3f) + 1)

#define UNDERLINE \
	(m_param[REG_SCN3] >> 4)

#define SCANLINES_PER_ROW \
	((m_param[REG_SCN3] & 0x0f) + 1)

#define OFFSET_LINE_COUNTER \
	BIT(m_param[REG_SCN4], 7)

#define VISIBLE_FIELD_ATTRIBUTE \
	BIT(m_param[REG_SCN4], 6)

#define CURSOR_FORMAT \
	((m_param[REG_SCN4] >> 4) & 0x03)

#define HRTC_COUNT \
	(((m_param[REG_SCN4] & 0x0f) + 1) * 2)

#define DMA_BURST_COUNT \
	(1 << (m_param[REG_DMA] & 0x03))

#define DMA_BURST_SPACE \
	DMA_BURST_SPACING[(m_param[REG_DMA] >> 2) & 0x07]



//**************************************************************************
//  somethign
//**************************************************************************

// device type definition
const device_type I8275x = &device_creator<i8275x_device>;


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

//-------------------------------------------------
//  i8275x_device - constructor
//-------------------------------------------------

i8275x_device::i8275x_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
	: device_t(mconfig, I8275x, "I8275", tag, owner, clock, "i8275x", __FILE__),
		device_video_interface(mconfig, *this),
		m_write_irq(*this),
		m_write_drq(*this),
		m_write_hrtc(*this),
		m_write_vrtc(*this),
		m_display_pixels(NULL),
		m_status(0),
		m_param_idx(0),
		m_param_end(0),
		m_buffer_idx(0),
		m_fifo_next(false),
		m_buffer_dma(0),
		m_lpen(0),
		m_hlgt(0),
		m_vsp(0),
		m_gpa(0),
		m_rvv(0),
		m_lten(0),
		m_scanline(0),
		m_du(false),
		m_cursor_blink(0),
		m_char_blink(0)
{
}


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

void i8275x_device::device_start()
{
	// get the screen device
	m_screen->register_screen_bitmap(m_bitmap);

	// resolve callbacks
	m_write_drq.resolve_safe();
	m_write_irq.resolve_safe();
	m_write_hrtc.resolve_safe();
	m_write_vrtc.resolve_safe();

	// allocate timers
	m_hrtc_on_timer = timer_alloc(TIMER_HRTC_ON);
	m_drq_on_timer = timer_alloc(TIMER_DRQ_ON);
	m_drq_off_timer = timer_alloc(TIMER_DRQ_OFF);
	m_scanline_timer = timer_alloc(TIMER_SCANLINE);

	// state saving
	save_item(NAME(m_status));
	save_item(NAME(m_param));
	save_item(NAME(m_param_idx));
	save_item(NAME(m_param_end));
	save_item(NAME(m_buffer[0]));
	save_item(NAME(m_buffer[1]));
	save_item(NAME(m_buffer_idx));
	save_item(NAME(m_fifo_idx));
	save_item(NAME(m_fifo_next));
	save_item(NAME(m_buffer_dma));
	save_item(NAME(m_lpen));
}


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

void i8275x_device::device_reset()
{
	memset(m_buffer, 0, sizeof(m_buffer));

	m_status &= ~ST_IE;

	m_write_irq(CLEAR_LINE);
}


//-------------------------------------------------
//  device_timer - handle timer events
//-------------------------------------------------

void i8275x_device::device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr)
{
	//int y = m_screen->vpos();
	//int x = m_screen->hpos();
	int rc = m_scanline / SCANLINES_PER_ROW;
	int lc = m_scanline % SCANLINES_PER_ROW;

	switch (id)
	{
	case TIMER_HRTC_ON:
		//logerror("I8275 '%s' y %u x %u HRTC 1\n", tag(), y, x);
		m_write_hrtc(1);
		break;

	case TIMER_DRQ_ON:
		//logerror("I8275 '%s' y %u x %u DRQ 1\n", tag(), y, x);
		m_write_drq(1);
		m_drq_off_timer->adjust(clocks_to_attotime(DMA_BURST_COUNT));
		break;

	case TIMER_DRQ_OFF:
		if (m_buffer_idx == 0)
		{
			m_status |= ST_DU;
			m_du = true;
			//logerror("I8275 '%s' y %u x %u DRQ 0\n", tag(), y, x);
			m_write_drq(0);
		}
		else if (m_buffer_idx == CHARACTERS_PER_ROW)
		{
			//logerror("I8275 '%s' y %u x %u DRQ 0\n", tag(), y, x);
			m_write_drq(0);
		}
		else if (DMA_BURST_SPACE > 0)
		{
			//logerror("I8275 '%s' y %u x %u DRQ 0\n", tag(), y, x);
			m_write_drq(0);
			m_drq_on_timer->adjust(clocks_to_attotime(DMA_BURST_SPACE));
		}
		break;

	case TIMER_SCANLINE:
		if (!(m_status & ST_VE)) break;

		//logerror("I8275 '%s' y %u x %u HRTC 0\n", tag(), y, x);
		m_write_hrtc(0);

		if (m_scanline == 0)
		{
			//logerror("I8275 '%s' y %u x %u VRTC 0\n", tag(), y, x);
			m_write_vrtc(0);
		}
		else if (m_scanline == m_irq_scanline)
		{
			if (m_status & ST_IE)
			{
				//logerror("I8275 '%s' y %u x %u IRQ 1\n", tag(), y, x);
				m_status |= ST_IR;
				m_write_irq(ASSERT_LINE);
			}
		}
		else if (m_scanline == m_vrtc_scanline)
		{
			//logerror("I8275 '%s' y %u x %u VRTC 1\n", tag(), y, x);
			m_write_vrtc(1);

			// reset field attributes
			m_hlgt = 0;
			m_vsp = 0;
			m_gpa = 0;
			m_rvv = 0,
			m_lten = 0;

			m_du = false;

			m_cursor_blink++;
			m_cursor_blink &= 0x1f;

			m_char_blink++;
			m_char_blink &= 0x3f;
		}

		if (lc == 0)
		{
			// swap line buffers
			m_buffer_dma = !m_buffer_dma;
			m_buffer_idx = 0;
			m_fifo_idx = 0;

			if (!m_du && ((m_scanline < m_vrtc_scanline - SCANLINES_PER_ROW) || (m_scanline == m_vrtc_drq_scanline)))
			{
				// start DMA burst
				m_drq_on_timer->adjust(clocks_to_attotime(DMA_BURST_SPACE));
			}
		}

		if (m_scanline < m_vrtc_scanline)
		{
			for (int sx = 0; sx < CHARACTERS_PER_ROW; sx++)
			{
				int m_lineattr = 0;
				int lten = 0;
				int vsp = 0;

				UINT8 data = m_buffer[!m_buffer_dma][sx];

				if (data & 0x80)
				{
					if ((data & 0xc0) == 0x80)
					{
						// field attribute code
						m_hlgt = (data & FAC_H) ? 1 : 0;
						m_vsp = (data & FAC_B) ? 1 : 0;
						m_gpa = (data & FAC_GG) >> 2;
						m_rvv = (data & FAC_R) ? 1 : 0;
						m_lten = (data & FAC_U) ? 1 : 0;

						if (!VISIBLE_FIELD_ATTRIBUTE)
						{
							int fifo_idx = 0;

							data = m_fifo[!m_buffer_dma][fifo_idx];

							fifo_idx++;
							fifo_idx &= 0xf;
						}
						else
						{
							vsp = 1;
						}
					}
					else
					{
						// character attribute code
					}
				}

				if (!vsp && m_vsp)
				{
					vsp = (m_char_blink < 32) ? 1 : 0;
				}

				if ((rc == m_param[REG_CUR_ROW]) && (sx == m_param[REG_CUR_COL]))
				{
					int vis = 1;

					if (!(CURSOR_FORMAT & 0x02))
					{
						vis = (m_cursor_blink < 16) ? 1 : 0;
					}

					if (CURSOR_FORMAT & 0x01)
					{
						lten = (lc == UNDERLINE) ? vis : 0;
					}
					else
					{
						lten = vis;
					}
				}

				if (OFFSET_LINE_COUNTER)
				{
					lc = (lc - 1) & 0x0f;
				}

				if (m_display_pixels)
				m_display_pixels(this, m_bitmap,
					sx * m_hpixels_per_column, // x position on screen of starting point
					m_scanline, // y position on screen
					lc, // current line of char
					(data & 0x7f),  // char code to be displayed
					m_lineattr,  // line attribute code
					lten | m_lten,  // light enable signal
					m_rvv,  // reverse video signal
					vsp, // video suppression
					m_gpa,  // general purpose attribute code
					m_hlgt  // highlight
				);
			}
		}

		m_scanline++;
		m_scanline %= ((CHARACTER_ROWS_PER_FRAME + VRTC_ROW_COUNT) * SCANLINES_PER_ROW);
		break;
	}
}


//-------------------------------------------------
//  read -
//-------------------------------------------------

READ8_MEMBER( i8275x_device::read )
{
	UINT8 data = 0;

	if (offset & 0x01)
	{
		data = m_status;

		if (m_status & ST_IR)
		{
			//logerror("I8275 '%s' IRQ 0\n", tag());
			m_write_irq(CLEAR_LINE);
		}

		m_status &= ~(ST_IR | ST_LP | ST_IC | ST_DU | ST_FO);
	}
	else
	{
		data = m_param[m_param_idx];
		m_param_idx++;

		if (m_param_idx > m_param_end)
		{
			m_status |= ST_IC;
		}
	}

	return data;
}


//-------------------------------------------------
//  write -
//-------------------------------------------------

WRITE8_MEMBER( i8275x_device::write )
{
	if (offset & 0x01)
	{
		logerror("I8275 '%s' Command %02x\n", tag(), data);

		switch (data >> 5)
		{
		case CMD_RESET:
			logerror("I8275 '%s' Reset\n", tag());

			m_status &= ~ST_IE;
			logerror("I8275 '%s' IRQ 0\n", tag());
			m_write_irq(CLEAR_LINE);

			m_param_idx = REG_SCN1;
			m_param_end = REG_SCN4;
			break;

		case CMD_START_DISPLAY:
			{
				m_param[REG_DMA] = data;
				logerror("I8275 '%s' Start Display %u %u\n", tag(), DMA_BURST_COUNT, DMA_BURST_SPACE);
				m_status |= (ST_IE | ST_VE);
			}
			break;

		case CMD_STOP_DISPLAY:
			logerror("I8275 '%s' Stop Display\n", tag());
			m_status &= ~ST_VE;
			break;

		case CMD_READ_LIGHT_PEN:
			logerror("I8275 '%s' Read Light Pen\n", tag());
			m_param_idx = REG_LPEN_COL;
			m_param_end = REG_LPEN_ROW;
			break;

		case CMD_LOAD_CURSOR:
			logerror("I8275 '%s' Load Cursor\n", tag());
			m_param_idx = REG_CUR_COL;
			m_param_end = REG_CUR_ROW;
			break;

		case CMD_ENABLE_INTERRUPT:
			logerror("I8275 '%s' Enable Interrupt\n", tag());
			m_status |= ST_IE;
			break;

		case CMD_DISABLE_INTERRUPT:
			logerror("I8275 '%s' Disable Interrupt\n", tag());
			m_status &= ~ST_IE;
			break;

		case CMD_PRESET_COUNTERS:
			logerror("I8275 '%s' Preset Counters\n", tag());
			m_scanline = 0;
			break;
		}
	}
	else
	{
		logerror("I8275 '%s' Parameter %02x\n", tag(), data);

		m_param[m_param_idx] = data;

		if (m_param_idx == REG_SCN4)
		{
			recompute_parameters();
		}

		m_param_idx++;
	}
}


//-------------------------------------------------
//  dack_w -
//-------------------------------------------------

WRITE8_MEMBER( i8275x_device::dack_w )
{
	//logerror("DACK write %02x %u\n", data, m_buffer_idx);

	if (m_fifo_next)
	{
		if (m_fifo_idx == 16)
		{
			m_fifo_idx = 0;
			m_status |= ST_FO;
		}

		m_fifo[m_buffer_dma][m_fifo_idx++] = data;

		m_fifo_next = false;
	}
	else
	{
		m_buffer[m_buffer_dma][m_buffer_idx++] = data;

		if (!VISIBLE_FIELD_ATTRIBUTE && ((data & 0xc0) == 0x80))
		{
			m_fifo_next = true;
		}

		if (m_buffer_idx == CHARACTERS_PER_ROW)
		{
			m_drq_off_timer->adjust(attotime::zero);
		}
	}
}


//-------------------------------------------------
//  lpen_w -
//-------------------------------------------------

WRITE_LINE_MEMBER( i8275x_device::lpen_w )
{
	if (!m_lpen && state)
	{
		m_param[REG_LPEN_COL] = m_screen->hpos() / m_hpixels_per_column;
		m_param[REG_LPEN_ROW] = m_screen->vpos() / SCANLINES_PER_ROW;

		m_status |= ST_LP;
	}

	m_lpen = state;
}


//-------------------------------------------------
//  screen_update -
//-------------------------------------------------

UINT32 i8275x_device::screen_update(screen_device &screen, bitmap_rgb32 &bitmap, const rectangle &cliprect)
{
	if (!(m_status & ST_VE))
	{
		m_bitmap.fill(RGB_BLACK);
	}

	copybitmap(bitmap, m_bitmap, 0, 0, 0, 0, cliprect);

	return 0;
}


//-------------------------------------------------
//  recompute_parameters -
//-------------------------------------------------

void i8275x_device::recompute_parameters()
{
	int y = m_screen->vpos();

	int horiz_pix_total = (CHARACTERS_PER_ROW + HRTC_COUNT) * m_hpixels_per_column;
	int vert_pix_total = (CHARACTER_ROWS_PER_FRAME + VRTC_ROW_COUNT) * SCANLINES_PER_ROW;
	attoseconds_t refresh = m_screen->frame_period().attoseconds;
	int max_visible_x = (CHARACTERS_PER_ROW * m_hpixels_per_column) - 1;
	int max_visible_y = (CHARACTER_ROWS_PER_FRAME * SCANLINES_PER_ROW) - 1;

	logerror("width %u height %u max_x %u max_y %u refresh %f\n", horiz_pix_total, vert_pix_total, max_visible_x, max_visible_y, 1 / ATTOSECONDS_TO_DOUBLE(refresh));

	rectangle visarea;
	visarea.set(0, max_visible_x, 0, max_visible_y);
	m_screen->configure(horiz_pix_total, vert_pix_total, visarea, refresh);

	int hrtc_on_pos = CHARACTERS_PER_ROW * m_hpixels_per_column;
	m_hrtc_on_timer->adjust(m_screen->time_until_pos(y, hrtc_on_pos), 0, m_screen->scan_period());

	m_irq_scanline = (CHARACTER_ROWS_PER_FRAME - 1) * SCANLINES_PER_ROW;
	m_vrtc_scanline = CHARACTER_ROWS_PER_FRAME * SCANLINES_PER_ROW;
	m_vrtc_drq_scanline = vert_pix_total - SCANLINES_PER_ROW;

	m_scanline_timer->adjust(m_screen->time_until_pos(0, 0), 0, m_screen->scan_period());
}