summaryrefslogtreecommitdiffstatshomepage
path: root/src/mess/video/vtvideo.c
blob: f1d7f1583398ee69662b7aa0adcca58cfc881e05 (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
/**********************************************************************

    DEC VT Terminal video emulation
    [ DC012 and DC011 emulation ]

    01/05/2009 Initial implementation [Miodrag Milanovic]

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

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

#include "emu.h"
#include "vtvideo.h"

/***************************************************************************
    PARAMETERS
***************************************************************************/

#define	VERBOSE			0

#define	LOG(x)		do { if (VERBOSE) logerror x; } while (0)

/***************************************************************************
    TYPE DEFINITIONS
***************************************************************************/

struct vt_video_t
{
	devcb_resolved_read8		in_ram_func;
	devcb_resolved_write8		clear_video_interrupt;

	screen_device *screen;	/* screen */
	UINT8 *gfx;		/* content of char rom */

	int lba7;

	// dc012 attributes
	UINT8 scroll_latch;
	UINT8 blink_flip_flop;
	UINT8 reverse_field;
	UINT8 basic_attribute;
	// dc011 attributes
	UINT8 columns;
	UINT8 height;
	UINT8 skip_lines;
	UINT8 frequency;
	UINT8 interlaced;
};

/***************************************************************************
    INLINE FUNCTIONS
***************************************************************************/

INLINE vt_video_t *get_safe_token(device_t *device)
{
	assert(device != NULL);
	assert(device->type() == VT100_VIDEO);

	return (vt_video_t *)downcast<vt100_video_device *>(device)->token();
}

INLINE const vt_video_interface *get_interface(device_t *device)
{
	assert(device != NULL);
	assert(device->type() == VT100_VIDEO);

	return (const vt_video_interface *) device->static_config();
}

/***************************************************************************
    IMPLEMENTATION
***************************************************************************/

static void vt_video_recompute_parameters(device_t *device)
{
	vt_video_t *vt = get_safe_token(device);
	int horiz_pix_total = 0;
	int vert_pix_total = 0;
	rectangle visarea;

	horiz_pix_total = vt->columns * 10;
	vert_pix_total  = 25 * 10;

	visarea.set(0, horiz_pix_total - 1, 0, vert_pix_total - 1);

	vt->screen->configure(horiz_pix_total, vert_pix_total, visarea,
				vt->screen->frame_period().attoseconds);
}
READ8_DEVICE_HANDLER( vt_video_lba7_r )
{
	vt_video_t *vt = get_safe_token(device);
	return vt->lba7;
}


WRITE8_DEVICE_HANDLER( vt_video_dc012_w )
{
	vt_video_t *vt = get_safe_token(device);

	if ((data & 0x08)==0) {
		if ((data & 0x04)==0) {
			// set lower part scroll
			vt->scroll_latch = (vt->scroll_latch & 0x0c) | (data & 0x03);
		} else {
			// set higher part scroll
			vt->scroll_latch = (vt->scroll_latch & 0x03) | ((data & 0x03) << 2);
		}
	} else {
		switch( data & 0x0f) {
			case 0x08:
				// toggle blink flip flop
				vt->blink_flip_flop = (vt->blink_flip_flop==0) ? 1 : 0;
				break;
			case 0x09:
				// clear vertical frequency interrupt;
				vt->clear_video_interrupt(0, 0);
				break;
			case 0x0A:
				// set reverse field on
				vt->reverse_field = 1;
				break;
			case 0x0B:
				// set reverse field off
				vt->reverse_field = 0;
				break;
			case 0x0C:
				// set basic attribute to underline
				vt->basic_attribute = 0;
				vt->blink_flip_flop = 0;
				break;
			case 0x0D:
				// set basic attribute to reverse video
				vt->basic_attribute = 1;
				vt->blink_flip_flop = 0;
				break;
			case 0x0E:
			case 0x0F:
				// reserved for future specification
				vt->blink_flip_flop = 0;
				break;
		}
	}
}


WRITE8_DEVICE_HANDLER( vt_video_dc011_w )
{
	vt_video_t *vt = get_safe_token(device);
	if (BIT(data,5)==0) {
		UINT8 col = vt->columns;
		if (BIT(data,4)==0) {
			vt->columns = 80;
		} else {
			vt->columns = 132;
		}
		if (col!=vt->columns) {
			vt_video_recompute_parameters(device);
		}
		vt->interlaced = 1;
	} else {
		if (BIT(data,4)==0) {
			vt->frequency = 60;
			vt->skip_lines = 2;
		} else {
			vt->frequency = 50;
			vt->skip_lines = 5;
		}
		vt->interlaced = 0;
	}
}

WRITE8_DEVICE_HANDLER( vt_video_brightness_w )
{
	//palette_set_color_rgb(space.machine(), 1, data, data, data);
}

static void vt_video_display_char(device_t *device,bitmap_ind16 &bitmap, UINT8 code,
	int x, int y,UINT8 scroll_region,UINT8 display_type)
{
	UINT8 line=0;
	int i,b,bit=0,prevbit,invert=0,j;
	int double_width = (display_type==2) ? 1 : 0;
	vt_video_t *vt = get_safe_token(device);

	for (i = 0; i < 10; i++)
	{

		switch(display_type) {
			case 0 : // bottom half, double height
					 j = (i >> 1)+5; break;
			case 1 : // top half, double height
					 j = (i >> 1); break;
			case 2 : // double width
			case 3 : // normal
					 j = i;	break;
			default : j = 0; break;
		}
		// modify line since that is how it is stored in rom
		if (j==0) j=15; else j=j-1;

		line = vt->gfx[(code & 0x7f)*16 + j];
		if (vt->basic_attribute==1) {
			if ((code & 0x80)==0x80)
				invert = 1;
			else
				invert = 0;
		}

		for (b = 0; b < 8; b++)
		{
			prevbit = bit;
			bit = (((line << b) & 0x80) ? 1 : 0);
			if (double_width) {
				bitmap.pix16(y*10+i, x*20+b*2)   =  (bit|prevbit)^invert;
				bitmap.pix16(y*10+i, x*20+b*2+1) =  bit^invert;
			} else {
				bitmap.pix16(y*10+i, x*10+b) =  (bit|prevbit)^invert;
			}
		}
		prevbit = bit;
		// char interleave is filled with last bit
		if (double_width) {
			bitmap.pix16(y*10+i, x*20+16) =  (bit|prevbit)^invert;
			bitmap.pix16(y*10+i, x*20+17) =  bit^invert;
			bitmap.pix16(y*10+i, x*20+18) =  bit^invert;
			bitmap.pix16(y*10+i, x*20+19) =  bit^invert;
		} else {
			bitmap.pix16(y*10+i, x*10+8) =  (bit|prevbit)^invert;
			bitmap.pix16(y*10+i, x*10+9) =  bit^invert;
		}
	}
}

void vt_video_update(device_t *device, bitmap_ind16 &bitmap, const rectangle &cliprect)
{
	vt_video_t *vt = get_safe_token(device);

	UINT16 addr = 0;
	int line = 0;
	int xpos = 0;
	int ypos = 0;
	UINT8 code;
	int x = 0;
	UINT8 scroll_region = 1; // binary 1
	UINT8 display_type = 3;  // binary 11
	UINT16 temp =0;

	if (vt->in_ram_func(0) !=0x7f) return;

	while(line < (vt->height + vt->skip_lines)) {
		code =  vt->in_ram_func(addr + xpos);
		if (code == 0x7f) {
			// end of line, fill empty till end of line
			if (line >= vt->skip_lines) {
				for(x = xpos; x < ((display_type==2) ? (vt->columns / 2) : vt->columns); x++ )
				{
					vt_video_display_char(device,bitmap,code,x,ypos,scroll_region,display_type);
				}
			}
			// move to new data
			temp = vt->in_ram_func(addr+xpos+1)*256 + vt->in_ram_func(addr+xpos+2);
			addr = (temp) & 0x1fff;
			// if A12 is 1 then it is 0x2000 block, if 0 then 0x4000 (AVO)
			if (addr & 0x1000) addr &= 0xfff; else addr |= 0x2000;
			scroll_region = (temp >> 15) & 1;
			display_type  = (temp >> 13) & 3;
			if (line >= vt->skip_lines) {
				ypos++;
			}
			xpos=0;
			line++;
		} else {
			// display regular char
			if (line >= vt->skip_lines) {
				vt_video_display_char(device,bitmap,code,xpos,ypos,scroll_region,display_type);
			}
			xpos++;
			if (xpos > vt->columns) {
				line++;
				xpos=0;
			}
		}
	}

}

static void rainbow_video_display_char(device_t *device,bitmap_ind16 &bitmap, UINT8 code,
	int x, int y,UINT8 scroll_region,UINT8 display_type)
{
	UINT8 line=0;
	int i,b,bit=0,j;
	int double_width = (display_type==2) ? 1 : 0;
	vt_video_t *vt = get_safe_token(device);

	for (i = 0; i < 10; i++)
	{

		switch(display_type) {
			case 0 : // bottom half, double height
					 j = (i >> 1)+5; break;
			case 1 : // top half, double height
					 j = (i >> 1); break;
			case 2 : // double width
			case 3 : // normal
					 j = i;	break;
			default : j = 0; break;
		}
		// modify line since that is how it is stored in rom
		if (j==0) j=15; else j=j-1;

		line = vt->gfx[code*16 + j];
		if (vt->basic_attribute==1) {
			if ((code & 0x80)==0x80) {
				line = line ^ 0xff;
			}
		}

		for (b = 0; b < 8; b++)
		{
			bit = ((line << b) & 0x80) ? 1 : 0;
			if (double_width) {
				bitmap.pix16(y*10+i, x*20+b*2)   =  bit;
				bitmap.pix16(y*10+i, x*20+b*2+1) =  bit;
			} else {
				bitmap.pix16(y*10+i, x*10+b) =  bit;
			}
		}
		// char interleave is filled with last bit
		if (double_width) {
			bitmap.pix16(y*10+i, x*20+16) =  bit;
			bitmap.pix16(y*10+i, x*20+17) =  bit;
			bitmap.pix16(y*10+i, x*20+18) =  bit;
			bitmap.pix16(y*10+i, x*20+19) =  bit;
		} else {
			bitmap.pix16(y*10+i, x*10+8) =  bit;
			bitmap.pix16(y*10+i, x*10+9) =  bit;
		}
	}
}
void rainbow_video_update(device_t *device, bitmap_ind16 &bitmap, const rectangle &cliprect)
{
	vt_video_t *vt = get_safe_token(device);

	UINT16 addr = 0;
	UINT16 attr_addr = 0;
	int line = 0;
	int xpos = 0;
	int ypos = 0;
	UINT8 code;
	int x = 0;
	UINT8 scroll_region = 1; // binary 1
	UINT8 display_type = 3;  // binary 11
	UINT16 temp =0;

	while(line < (vt->height + vt->skip_lines)) {
		code =  vt->in_ram_func(addr + xpos);
		if (code == 0xff) {
			// end of line, fill empty till end of line
			if (line >= vt->skip_lines) {
				for(x = xpos; x < ((display_type==2) ? (vt->columns / 2) : vt->columns); x++ )
				{
					rainbow_video_display_char(device,bitmap,code,x,ypos,scroll_region,display_type);
				}
			}
			// move to new data
			temp = vt->in_ram_func(addr+xpos+2)*256 + vt->in_ram_func(addr+xpos+1);
			addr = (temp) & 0x0fff;
			attr_addr = ((temp) & 0x1fff) - 2;
			// if A12 is 1 then it is 0x2000 block, if 0 then 0x4000 (AVO)
			if (temp & 0x1000) attr_addr &= 0xfff; else attr_addr |= 0x1000;
			temp = vt->in_ram_func(attr_addr);
			scroll_region = (temp) & 1;
			display_type  = (temp>> 1) & 3;
			if (line >= vt->skip_lines) {
				ypos++;
			}
			xpos=0;
			line++;
		} else {
			// display regular char
			if (line >= vt->skip_lines) {
				rainbow_video_display_char(device,bitmap,code,xpos,ypos,scroll_region,display_type);
			}
			xpos++;
			if (xpos > vt->columns) {
				line++;
				xpos=0;
			}
		}
	}

}
/*-------------------------------------------------
    DEVICE_START( vt_video )
-------------------------------------------------*/
static TIMER_CALLBACK(lba7_change)
{
	device_t *device = (device_t *)ptr;
	vt_video_t *vt = get_safe_token(device);

	vt->lba7 = (vt->lba7) ? 0 : 1;
}

static DEVICE_START( vt_video )
{
	vt_video_t *vt = get_safe_token(device);
	const vt_video_interface *intf = get_interface(device);

	/* resolve callbacks */
	vt->in_ram_func.resolve(intf->in_ram_func, *device);
	vt->clear_video_interrupt.resolve(intf->clear_video_interrupt, *device);

	/* get the screen device */
	vt->screen = device->machine().device<screen_device>(intf->screen_tag);
	assert(vt->screen != NULL);

	vt->gfx = device->machine().root_device().memregion(intf->char_rom_region_tag)->base();
	assert(vt->gfx != NULL);

	// LBA7 is scan line frequency update
	device->machine().scheduler().timer_pulse(attotime::from_nsec(31778), FUNC(lba7_change), 0, (void *) device);
}


/*-------------------------------------------------
    DEVICE_RESET( vt_video )
-------------------------------------------------*/

static DEVICE_RESET( vt_video )
{
	vt_video_t *vt = get_safe_token(device);
	palette_set_color_rgb(device->machine(), 0, 0x00, 0x00, 0x00); // black
	palette_set_color_rgb(device->machine(), 1, 0xff, 0xff, 0xff); // white

	vt->height = 25;
	vt->lba7 = 0;

	vt->scroll_latch = 0;
	vt->blink_flip_flop = 0;
	vt->reverse_field = 0;
	vt->basic_attribute = 0;

	vt->columns = 80;
	vt->frequency = 60;
	vt->interlaced = 1;
	vt->skip_lines = 2; // for 60Hz
}

const device_type VT100_VIDEO = &device_creator<vt100_video_device>;

vt100_video_device::vt100_video_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
	: device_t(mconfig, VT100_VIDEO, "VT100 Video", tag, owner, clock)
{
	m_token = global_alloc_clear(vt_video_t);
}

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

void vt100_video_device::device_config_complete()
{
}

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

void vt100_video_device::device_start()
{
	DEVICE_START_NAME( vt_video )(this);
}

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

void vt100_video_device::device_reset()
{
	DEVICE_RESET_NAME( vt_video )(this);
}