summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/video/saa5050.c
blob: d84a148ba02b994aed5ace66ece16c9e415c05d3 (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
/***************************************************************************

    saa5050.c

    Functions to emulate the
    SAA5050 - Teletext Character Generator.

    TODO:
    -  Implement BOX and dirtybuffer
    -  Add support for non-English version (SAA505x), possibly merging
       src/mess/video/saa505x.c unsed by bbc.c in MESS
    -  Investigate why supporting code 0 behavior breaks p2000t vscroll
       in MESS (but not supporting breaks malzak title background)
    -  x,y sizes should probably be calculated from the screen parameters
       rather than passed in the device interface

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

#include "driver.h"
#include "video/saa5050.h"

#define SAA5050_DBLHI   0x0001
#define SAA5050_SEPGR   0x0002
#define SAA5050_FLASH   0x0004
#define SAA5050_BOX     0x0008
#define SAA5050_GRAPH   0x0010
#define SAA5050_CONCEAL 0x0020
#define SAA5050_HOLDGR  0x0040

#define SAA5050_BLACK   0
#define SAA5050_WHITE   7


typedef struct _saa5050_state  saa5050_state;
struct _saa5050_state
{
	const device_config *screen;
	int         gfxnum;
	int         x, y;
	int         size;
	int         rev;

	UINT8 *     videoram;
	UINT16      flags;
	UINT8	      forecol;
	UINT8	      backcol;
	UINT8	      prvcol;
	UINT8	      prvchr;
	INT8        frame_count;
};


/*************************************
 *
 *  Inline functions
 *
 *************************************/

INLINE saa5050_state *get_safe_token( const device_config *device )
{
	assert(device != NULL);
	assert(device->token != NULL);
	assert(device->type == SAA5050);

	return (saa5050_state *)device->token;
}

INLINE const saa5050_interface *get_interface( const device_config *device )
{
	assert(device != NULL);
	assert((device->type == SAA5050));
	return (const saa5050_interface *) device->static_config;
}

/*************************************
 *
 *  Graphics definitions
 *
 *************************************/

static const gfx_layout saa5050_charlayout =
{
	6, 10,
	256,
	1,
	{ 0 },
	{ 2, 3, 4, 5, 6, 7 },
	{ 0*8, 1*8, 2*8, 3*8, 4*8,
	  5*8, 6*8, 7*8, 8*8, 9*8 },
	8 * 10
};

static const gfx_layout saa5050_hilayout =
{
	6, 10,
	256,
	1,
	{ 0 },
	{ 2, 3, 4, 5, 6, 7 },
	{ 0*8, 0*8, 1*8, 1*8, 2*8,
	  2*8, 3*8, 3*8, 4*8, 4*8 },
	8 * 10
};

static const gfx_layout saa5050_lolayout =
{
	6, 10,
	256,
	1,
	{ 0 },
	{ 2, 3, 4, 5, 6, 7 },
	{ 5*8, 5*8, 6*8, 6*8, 7*8,
	  7*8, 8*8, 8*8, 9*8, 9*8 },
	8 * 10
};

GFXDECODE_START( saa5050 )
	GFXDECODE_ENTRY( "gfx1", 0x0000, saa5050_charlayout, 0, 64 )
	GFXDECODE_ENTRY( "gfx1", 0x0000, saa5050_hilayout, 0, 64 )
	GFXDECODE_ENTRY( "gfx1", 0x0000, saa5050_lolayout, 0, 64 )
GFXDECODE_END


/*************************************
 *
 *  Palette initialization
 *
 *************************************/

static const UINT8 saa5050_colors[8 * 3] =
{
	0x00, 0x00, 0x00,	/* black */
	0xff, 0x00, 0x00,	/* red */
	0x00, 0xff, 0x00,	/* green */
	0xff, 0xff, 0x00,	/* yellow */
	0x00, 0x00, 0xff,	/* blue */
	0xff, 0x00, 0xff,	/* magenta */
	0x00, 0xff, 0xff,	/* cyan */
	0xff, 0xff, 0xff	/* white */
};

static const UINT16 saa5050_palette[64 * 2] =	/* bgnd, fgnd */
{
	0,1, 0,1, 0,2, 0,3, 0,4, 0,5, 0,6, 0,7,
	1,0, 1,1, 1,2, 1,3, 1,4, 1,5, 1,6, 1,7,
	2,0, 2,1, 2,2, 2,3, 2,4, 2,5, 2,6, 2,7,
	3,0, 3,1, 3,2, 3,3, 3,4, 3,5, 3,6, 3,7,
	4,0, 4,1, 4,2, 4,3, 4,4, 4,5, 4,6, 4,7,
	5,0, 5,1, 5,2, 5,3, 5,4, 5,5, 5,6, 5,7,
	6,0, 6,1, 6,2, 6,3, 6,4, 6,5, 6,6, 6,7,
	7,0, 7,1, 7,2, 7,3, 7,4, 7,5, 7,6, 7,7
};

PALETTE_INIT( saa5050 )
{
	UINT8 i, r, g, b;

	machine->colortable = colortable_alloc(machine, 8);

	for ( i = 0; i < 8; i++ )
	{
		r = saa5050_colors[i * 3];
		g = saa5050_colors[i * 3 + 1];
		b = saa5050_colors[i * 3 + 2];
		colortable_palette_set_color(machine->colortable, i, MAKE_RGB(r, g, b));
	}

	for (i = 0; i < 128; i++)
		colortable_entry_set_value(machine->colortable, i, saa5050_palette[i]);
}

/*************************************
 *
 *  Videoram access handlers
 *
 *************************************/

WRITE8_DEVICE_HANDLER( saa5050_videoram_w )
{
	saa5050_state *saa5050 = get_safe_token(device);
	saa5050->videoram[offset] = data;
}


READ8_DEVICE_HANDLER( saa5050_videoram_r )
{
	saa5050_state *saa5050 = get_safe_token(device);
	return saa5050->videoram[offset];
}

/*************************************
 *
 *  Emulation
 *
 *************************************/


/* this should probably be put at the end of saa5050 update,
but p2000t in MESS does not seem to currently support it.
Hence, we leave it independent for the moment */
void saa5050_frame_advance( const device_config *device )
{
	saa5050_state *saa5050 = get_safe_token(device);

	saa5050->frame_count++;
	if (saa5050->frame_count > 50)
		saa5050->frame_count = 0;
}

void saa5050_update( const device_config *device, bitmap_t *bitmap, const rectangle *cliprect  )
{
	saa5050_state *saa5050 = get_safe_token(device);
	int code, colour;
	int sx, sy, ssy;

	for (sy = 0; sy <= saa5050->y; sy++)
	{
		/* Set start of line state */
		saa5050->flags = 0;
		saa5050->prvchr = 32;
		saa5050->forecol = SAA5050_WHITE;
		saa5050->prvcol = SAA5050_WHITE;
		saa5050->backcol = SAA5050_BLACK;

		/* should we go in reverse order? */
		ssy = saa5050->rev ? saa5050->y - sy : sy;

		for (sx = 0; sx < saa5050->x; sx++)
		{
			int blank = 0;
			code = saa5050->videoram[ssy * saa5050->size + sx];
			if (code < 32)
			{
				switch (code)
				{
				case 0x00:
#ifndef MESS
					blank = 1;  // code 0x00 should not display anything, unless HOLDGR is set
#endif
					break;
				case 0x01: case 0x02: case 0x03: case 0x04:
				case 0x05: case 0x06: case 0x07:
					saa5050->prvcol = saa5050->forecol = code;
					saa5050->flags &= ~(SAA5050_GRAPH | SAA5050_CONCEAL);
					break;
				case 0x11: case 0x12: case 0x13: case 0x14:
				case 0x15: case 0x16: case 0x17:
					saa5050->prvcol = (saa5050->forecol = (code & 0x07));
					saa5050->flags &= ~SAA5050_CONCEAL;
					saa5050->flags |= SAA5050_GRAPH;
					break;
				case 0x08:
					saa5050->flags |= SAA5050_FLASH;
					break;
				case 0x09:
					saa5050->flags &= ~SAA5050_FLASH;
					break;
				case 0x0a:
					saa5050->flags |= SAA5050_BOX;
					break;
				case 0x0b:
					saa5050->flags &= ~SAA5050_BOX;
					break;
				case 0x0c:
					saa5050->flags &= ~SAA5050_DBLHI;
					break;
				case 0x0d:
					saa5050->flags |= SAA5050_DBLHI;
					break;
				case 0x18:
					saa5050->flags |= SAA5050_CONCEAL;
					break;
				case 0x19:
					saa5050->flags |= SAA5050_SEPGR;
					break;
				case 0x1a:
					saa5050->flags &= ~SAA5050_SEPGR;
					break;
				case 0x1c:
					saa5050->backcol = SAA5050_BLACK;
					break;
				case 0x1d:
					saa5050->backcol = saa5050->prvcol;
					break;
				case 0x1e:
					saa5050->flags |= SAA5050_HOLDGR;
					break;
				case 0x1f:
					saa5050->flags &= ~SAA5050_HOLDGR;
					break;
				}

				if (saa5050->flags & SAA5050_HOLDGR)
	  				code = saa5050->prvchr;
				else
					code = 32;
			}

			if (code & 0x80)
				colour = (saa5050->forecol << 3) | saa5050->backcol;
			else
				colour = saa5050->forecol | (saa5050->backcol << 3);

			if (saa5050->flags & SAA5050_CONCEAL)
				code = 32;
			else if ((saa5050->flags & SAA5050_FLASH) && (saa5050->frame_count > 38))
				code = 32;
			else
			{
				saa5050->prvchr = code;
				if ((saa5050->flags & SAA5050_GRAPH) && (code & 0x20))
				{
					code += (code & 0x40) ? 64 : 96;
					if (saa5050->flags & SAA5050_SEPGR)
						code += 64;
				}
			}

			if((blank == 0) || (saa5050->flags & SAA5050_HOLDGR))
			{
				if (saa5050->flags & SAA5050_DBLHI)
				{
					drawgfx_opaque(bitmap, cliprect, saa5050->screen->machine->gfx[saa5050->gfxnum + 1], code, colour, 0, 0, sx * 6, ssy * 10);
					drawgfx_opaque(bitmap, cliprect, saa5050->screen->machine->gfx[saa5050->gfxnum + 2], code, colour, 0, 0, sx * 6, (ssy + 1) * 10);
				}
				else
				{
					drawgfx_opaque(bitmap, cliprect, saa5050->screen->machine->gfx[saa5050->gfxnum + 0], code, colour, 0, 0, sx * 6, ssy * 10);
				}
			}
		}

		if (saa5050->flags & SAA5050_DBLHI)
		{
			sy++;
			saa5050->flags &= ~SAA5050_DBLHI;
		}
	}
}

/*****************************************************************************
    DEVICE INTERFACE
*****************************************************************************/

static DEVICE_START( saa5050 )
{
	saa5050_state *saa5050 = get_safe_token(device);
	const saa5050_interface *intf = get_interface(device);

	saa5050->screen = devtag_get_device(device->machine, intf->screen);
	saa5050->gfxnum = intf->gfxnum;
	saa5050->x = intf->x;
	saa5050->y = intf->y;
	saa5050->size = intf->size;
	saa5050->rev = intf->rev;

	saa5050->videoram = auto_alloc_array(device->machine, UINT8, 0x800);

	state_save_register_device_item_pointer(device, 0, saa5050->videoram, 0x800);
	state_save_register_device_item(device, 0, saa5050->flags);
	state_save_register_device_item(device, 0, saa5050->forecol);
	state_save_register_device_item(device, 0, saa5050->backcol);
	state_save_register_device_item(device, 0, saa5050->prvcol);
	state_save_register_device_item(device, 0, saa5050->prvchr);
	state_save_register_device_item(device, 0, saa5050->frame_count);
}

static DEVICE_RESET( saa5050 )
{
	saa5050_state *saa5050 = get_safe_token(device);

	memset(saa5050->videoram, 0x00, 0x800);

	saa5050->flags = 0;
	saa5050->forecol = SAA5050_WHITE;
	saa5050->backcol = SAA5050_BLACK;
	saa5050->prvcol = SAA5050_WHITE;
	saa5050->prvchr = 32;
	saa5050->frame_count = 0;
}

static const char DEVTEMPLATE_SOURCE[] = __FILE__;

#define DEVTEMPLATE_ID( p, s )	p##saa5050##s
#define DEVTEMPLATE_FEATURES	DT_HAS_START | DT_HAS_RESET
#define DEVTEMPLATE_NAME		"SAA5050"
#define DEVTEMPLATE_FAMILY		"SAA5050 Teletext Character Generator"
#define DEVTEMPLATE_CLASS		DEVICE_CLASS_VIDEO
#include "devtempl.h"