summaryrefslogtreecommitdiffstatshomepage
path: root/src/devices/video/mr9735.cpp
blob: 0529cc8349431ad84495edbc71975ce51b1dd99e (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
// license:BSD-3-Clause
// copyright-holders:Nigel Barnes
/**********************************************************************

    MR9735 Teletext/Viewdata 625-line Video Generator

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

#include "emu.h"
#include "mr9735.h"


//**************************************************************************
//  DEVICE DEFINITIONS
//**************************************************************************

DEFINE_DEVICE_TYPE(MR9735_002, mr9735_002_device, "mr9735_002", "MR9735-002 Teletext/Viewdata Video Generator")


//-------------------------------------------------
//  rom_region - device-specific ROM region
//-------------------------------------------------

ROM_START( mr9735_002 )
	ROM_REGION( 0x500, "chargen", 0 )
	ROM_LOAD("mr9735-002", 0x0140, 0x03c0, BAD_DUMP CRC(201490f3) SHA1(6c8daba70374e5aa3a6402f24cdc5f8677d58a0f)) // assumed to be the same as SAA5050
ROM_END

const tiny_rom_entry *mr9735_002_device::device_rom_region() const
{
	return ROM_NAME( mr9735_002 );
}


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

//-------------------------------------------------
//  mr9735_002_device - constructor
//-------------------------------------------------

mr9735_002_device::mr9735_002_device(const machine_config &mconfig, device_type type, const char *tag, device_t *owner, uint32_t clock)
	: device_t(mconfig, type, tag, owner, clock)
	, m_char_rom(*this, "chargen")
	, m_read_data(*this, 0)
	, m_frame_count(0)
	, m_interlace(false)
{
}

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


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

void mr9735_002_device::device_start()
{
	save_item(NAME(m_held_char));
	save_item(NAME(m_ra));
	save_item(NAME(m_bg));
	save_item(NAME(m_fg));
	save_item(NAME(m_prev_col));
	save_item(NAME(m_graphics));
	save_item(NAME(m_separated));
	save_item(NAME(m_flash));
	save_item(NAME(m_boxed));
	save_item(NAME(m_dbl_height));
	save_item(NAME(m_dbl_height_bottom_row));
	save_item(NAME(m_dbl_height_prev_row));
	save_item(NAME(m_hold_char));
	save_item(NAME(m_frame_count));
}


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

void mr9735_002_device::device_reset()
{
	m_ra = 0;
	m_dbl_height = false;
	m_dbl_height_bottom_row = false;

	if (m_interlace)
		m_char_rows = 20;
	else
		m_char_rows = 10;
}


//-------------------------------------------------
//  process_control_character -
//-------------------------------------------------

void mr9735_002_device::process_control_character(uint8_t data)
{
	switch (data)
	{
		case ALPHA_RED:
		case ALPHA_GREEN:
		case ALPHA_YELLOW:
		case ALPHA_BLUE:
		case ALPHA_MAGENTA:
		case ALPHA_CYAN:
		case ALPHA_WHITE:
			m_graphics = false;
			m_fg = data & 0x07;
			set_next_chartype();
			break;

		case FLASH:
			m_flash = true;
			break;

		case STEADY:
			m_flash = false;
			break;

		case END_BOX:
		case START_BOX:
			// TODO
			break;

		case NORMAL_HEIGHT:
		case DOUBLE_HEIGHT:
			m_dbl_height = !!(data & 1);
			if (m_dbl_height) m_dbl_height_prev_row = true;
			break;

		case GRAPHICS_RED:
		case GRAPHICS_GREEN:
		case GRAPHICS_YELLOW:
		case GRAPHICS_BLUE:
		case GRAPHICS_MAGENTA:
		case GRAPHICS_CYAN:
		case GRAPHICS_WHITE:
			m_graphics = true;
			m_fg = data & 0x07;
			set_next_chartype();
			break;

		case CONCEAL_DISPLAY:
			m_fg = m_prev_col = m_bg;
			break;

		case CONTIGUOUS_GFX:
			m_separated = false;
			set_next_chartype();
			break;

		case SEPARATED_GFX:
			m_separated = true;
			set_next_chartype();
			break;

		case BLACK_BACKGROUND:
			m_bg = 0;
			break;

		case NEW_BACKGROUND:
			m_bg = m_fg;
			break;

		case HOLD_GRAPHICS:
			m_hold_char = true;
			break;

		case RELEASE_GRAPHICS:
			m_hold_char = false;
			break;
	}
}


void mr9735_002_device::set_next_chartype()
{
	if (m_graphics)
	{
		if (m_separated)
			m_next_chartype = SEPARATED;
		else
			m_next_chartype = CONTIGUOUS;
	}
	else
	{
		m_next_chartype = ALPHANUMERIC;
	}
};


//-------------------------------------------------
//  get_gfx_data - graphics generator
//-------------------------------------------------

uint16_t mr9735_002_device::get_gfx_data(uint8_t data, offs_t row, bool separated)
{
	uint16_t c = 0;

	if (m_interlace)
		row >>= 1;

	switch (row)
	{
	case 0: case 1:
		c += (data & 0x01) ? 0x38 : 0; // bit 1 top left
		c += (data & 0x02) ? 0x07 : 0; // bit 2 top right
		if (separated) c &= 0x36;
		break;
	case 2:
		if (separated) break;
		c += (data & 0x01) ? 0x38 : 0; // bit 1 top left
		c += (data & 0x02) ? 0x07 : 0; // bit 2 top right
		break;
	case 3: case 4: case 5:
		c += (data & 0x04) ? 0x38 : 0; // bit 3 middle left
		c += (data & 0x08) ? 0x07 : 0; // bit 4 middle right
		if (separated) c &= 0x36;
		break;
	case 6:
		if (separated) break;
		c += (data & 0x04) ? 0x38 : 0; // bit 3 middle left
		c += (data & 0x08) ? 0x07 : 0; // bit 4 middle right
		break;
	case 7: case 8:
		c += (data & 0x10) ? 0x38 : 0; // bit 5 bottom left
		c += (data & 0x40) ? 0x07 : 0; // bit 7 bottom right
		if (separated) c &= 0x36;
		break;
	case 9:
		if (separated) break;
		c += (data & 0x10) ? 0x38 : 0; // bit 5 bottom left
		c += (data & 0x40) ? 0x07 : 0; // bit 7 bottom right
		break;
	}

	if (m_interlace)
		c = ((c & 0x01) * 0x03) + ((c & 0x02) * 0x06) + ((c & 0x04) * 0x0c) + ((c & 0x08) * 0x18) + ((c & 0x10) * 0x30) + ((c & 0x20) * 0x60);

	return c;
}


//-------------------------------------------------
//  get_rom_data - read rom
//-------------------------------------------------

uint16_t mr9735_002_device::get_rom_data(uint8_t data, offs_t row)
{
	uint16_t c;

	if (row < 0 || row >= m_char_rows)
	{
		c = 0;
	}
	else
	{
		if (m_interlace)
			row >>= 1;

		c = m_char_rom[(data * 10) + row];

		if (m_interlace)
			c = ((c & 0x01) * 0x03) + ((c & 0x02) * 0x06) + ((c & 0x04) * 0x0c) + ((c & 0x08) * 0x18) + ((c & 0x10) * 0x30);
	}

	return c;
}


//-------------------------------------------------
//  character_rounding
//-------------------------------------------------

uint16_t mr9735_002_device::character_rounding(uint16_t a, uint16_t b)
{
	return a | ((a >> 1) & b & ~(b >> 1)) | ((a << 1) & b & ~(b << 1));
}


//-------------------------------------------------
//  get_character_data -
//-------------------------------------------------

uint16_t mr9735_002_device::get_character_data(uint8_t data)
{
	bool const dbl_height_prev = m_dbl_height;
	bool const flash_prev      = m_flash;
	bool const graphics_prev   = m_graphics;
	bool const hold_char_prev  = m_hold_char;

	m_prev_col = m_fg;
	m_curr_chartype = m_next_chartype;

	if (data < 0x20)
	{
		process_control_character(data);
		if (graphics_prev && (hold_char_prev || m_hold_char) && m_dbl_height == dbl_height_prev)
		{
			data = m_held_char;
			if (data >= 0x40 && data < 0x60) data = 0x20;
			m_curr_chartype = m_held_chartype;
		}
		else
		{
			m_held_char = 0x20;
			data = 0x20;
		}
	}
	else if (m_graphics)
	{
		if (data & 0x20)
		{
			m_held_char = data;
			m_held_chartype = m_curr_chartype;
		}
	}
	else
	{
		m_held_char = 0x20;
	}

	offs_t ra = m_ra;
	if (dbl_height_prev)
	{
		ra >>= 1;
		if (m_dbl_height_bottom_row)
		{
			ra += m_interlace ? 10 : 5;
		}
	}

	if (flash_prev && !(m_frame_count & 0x30))
		data = 0x20;

	if (m_dbl_height_bottom_row && !m_dbl_height)
		data = 0x20;

	uint16_t char_data;

	if (m_curr_chartype == ALPHANUMERIC || !BIT(data, 5))
	{
		if (m_interlace)
			char_data = character_rounding(get_rom_data(data, ra), get_rom_data(data, ra + ((ra & 1) ? 1 : -1)));
		else
			char_data = get_rom_data(data, ra);
	}
	else
	{
		char_data = get_gfx_data(data, ra, (m_curr_chartype == SEPARATED));
	}
	return char_data;
}


//-------------------------------------------------
//  lfb_w - line flyback
//-------------------------------------------------

void mr9735_002_device::lfb_w(int state)
{
	if (state)
	{
		if (!m_ra)
		{
			if (m_dbl_height_bottom_row)
				m_dbl_height_bottom_row = false;
			else
				m_dbl_height_bottom_row = m_dbl_height_prev_row;
		}

		m_fg = 7;
		m_bg = 0;
		m_graphics = false;
		m_separated = false;
		m_flash = false;
		m_boxed = false;
		m_hold_char = false;
		m_held_char = 0x20;
		m_next_chartype = ALPHANUMERIC;
		m_held_chartype = ALPHANUMERIC;
		m_dbl_height = false;
		m_dbl_height_prev_row = false;
	}
}


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

uint32_t mr9735_002_device::screen_update(screen_device &screen, bitmap_rgb32 &bitmap, const rectangle &cliprect)
{
	if (m_frame_count++ > 50)
		m_frame_count = 0;

	for (int y = 0; y < 24 * m_char_rows; y++)
	{
		int sy = y / m_char_rows;
		int x = 0;

		m_ra = y % m_char_rows;

		lfb_w(1);
		lfb_w(0);

		int ssy = m_dbl_height_bottom_row ? sy - 1 : sy;
		offs_t video_ram_addr = ssy * NUM_COLS;

		for (int sx = 0; sx < NUM_COLS; sx++)
		{
			uint8_t code = ~m_read_data(video_ram_addr++);

			uint16_t char_data = get_character_data(code & 0x7f);

			for (int bit = (m_interlace ? 11 : 5); bit >= 0; bit--)
			{
				int color = BIT(char_data, bit) ? m_prev_col : m_bg;

				if (BIT(code, 7)) color ^= 0x07;

				int r = BIT(color, 0) * 0xff;
				int g = BIT(color, 1) * 0xff;
				int b = BIT(color, 2) * 0xff;

				bitmap.pix(y, x++) = rgb_t(r, g, b);
			}
		}
	}

	return 0;
}