summaryrefslogtreecommitdiffstatshomepage
path: root/src/mame/video/rmnimbus.cpp
blob: 940de72d834b688ff119a1f5235fdad99da704da (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
// license:BSD-3-Clause
// copyright-holders:Phill Harvey-Smith, Carl
/*
    video/rmnimbus.c

    Research machines Nimbus.

    2009-11-14, P.Harvey-Smith.

    This is my best guess implementation of the operation of the Nimbus
    video system.

    On the real machine, the Video chip has a block of 64K of memory which is
    completely separate from the main 80186 memory.

    The main CPU write to the video chip via a series of registers in the
    0x0000 to 0x002F reigon, the video chip then manages all video memory
    from there.

    As I cannot find a datasheet for the vide chip marked
    MB61H201 Fujitsu RML 12835 GCV, I have had to determine most of its
    operation by disassembling the Nimbus bios and by writing experemental
    code on the real machine.
*/

#include <functional>

#include "emu.h"
#include "debugger.h"
#include "debug/debugcon.h"
#include "includes/rmnimbus.h"


#define WIDTH_MASK      0x07

#define FG_COLOUR       (m_colours&0x0F)
#define BG_COLOUR       ((m_colours&0xF0)>>4)
#define SELECT_COL(x,c) (IS_80COL ? ((((x) & 1) ? ((c) << 2) : (c)) & 0xC) : (c))
#define FILL_WORD(c)    (((c) << 12) | ((c) << 8) | ((c) << 4) | (c))

#define IS_80COL        (m_mode&0x10)
#define IS_XOR          (m_op&8)

#define DEBUG_TEXT  0x01
#define DEBUG_DB    0x02
#define DEBUG_PIXEL 0x04

#define DEBUG_SET(flags)    ((m_debug_video & (flags))==(flags))

READ16_MEMBER(rmnimbus_state::nimbus_video_io_r)
{
	uint16_t result = 0;

	switch (offset)
	{
		case 0x00:
		case 0x08:
			result = read_pixel_data(m_x, m_y);
			break;
		case 0x02:
		case 0x0A:
			result = read_pixel_data(m_x, ++m_y);
			break;
		case 0x05:
		case 0x0D:
			result = read_pixel_data(++m_x, m_y);
			break;

		case 0x10:
			result = m_yline;
			break;
		case 0x11:
			result = m_op;
			break;
		case 0x12:
			result = m_colours;
			break;
		case 0x13:
			result = m_mode;
			break;
		case 0x14:
			result = m_screen->vpos() % 0xb; // TODO: verify
			break;
		case 0x15:
			result = m_x;
			break;
		case 0x16:
			result = m_y;
			break;
		default:
			logerror("nimbus: unknown video reg read %02x\n", offset);
			break;
	}

	if(DEBUG_SET(DEBUG_TEXT))
		logerror("Nimbus video IOR at %05X from %04X mask=%04X, data=%04X\n",m_maincpu->pc(),(offset*2),mem_mask,result);

	return result;
}

uint8_t rmnimbus_state::get_pixel(uint16_t x, uint16_t y)
{
	uint8_t   result = 0;

	if((x<640) && (y<250))
	{
		if(IS_80COL)
			result=m_video_mem.pix16(y, x) >> 2;
		else
			result=m_video_mem.pix16(y, x*2);
	}

	return result;
}

uint16_t rmnimbus_state::read_pixel_line(uint16_t x, uint16_t y, uint8_t pixels, uint8_t bpp)
{
	uint16_t colour = 0;
	int i;
	x *= pixels;

	if(DEBUG_SET(DEBUG_TEXT | DEBUG_PIXEL))
		logerror("read_pixel_line(x=%d, y=%d, width=%d, bpp=%d)\n",x,y,pixels,bpp);

	for(i = 0; i < pixels - 1; i++)
	{
		colour |= get_pixel(i + x, y);

		if(bpp==1)
			colour=((colour==SELECT_COL(x + i, FG_COLOUR)) ? 1 : 0) << 1;
		else
			colour <<= bpp;
	}
	return colour | get_pixel(x + i, y);
}

uint16_t rmnimbus_state::read_pixel_data(uint16_t x, uint16_t y)
{
	uint16_t  result=0;

	if(DEBUG_SET(DEBUG_TEXT | DEBUG_PIXEL))
		logerror("read_pixel_data(x=%d, y=%d), reg022=%04X\n",x,y,m_op);

	if(IS_80COL)
	{
		switch (m_op & WIDTH_MASK)
		{
			case 0x00   : break;

			case 0x01   : break;

			case 0x02   : break;

			case 0x03   : break;

			case 0x04   :
					result=read_pixel_line(x,y,4,2);
					break;

			case 0x05   : break;

			case 0x06   :
					result=read_pixel_line(x,y,8,2);
					break;

			case 0x07   : break;
		}
	}
	else /* 40 Col */
	{
		switch (m_op & WIDTH_MASK)
		{
			case 0x00   : break;

			case 0x01   : break;

			case 0x02   : break;

			case 0x03   : break;

			case 0x04   : break;

			case 0x05   : break;

			case 0x06   :
					result=read_pixel_line(x,y,4,4);
					break;

			case 0x07   : break;
		}
	}

	return result;
}

/*
    Write to the video registers.

    Incase anyone wonders about the DEBUG_DB statement, this allows me to log which registers
    are being written to and then play them back at the real machine, this has helped greatly
    in figuring out what the video registers do.

*/

WRITE16_MEMBER(rmnimbus_state::nimbus_video_io_w)
{
	uint16_t colours = data;
	if(offset < 0x14)
	{
		if(DEBUG_SET(DEBUG_TEXT))
			logerror("Nimbus video IOW at %05X write of %04X to %04X mask=%04X\n",m_maincpu->pc(),data,(offset*2),mem_mask);

		if(DEBUG_SET(DEBUG_DB))
			logerror("dw %05X,%05X\n",(offset*2),data);
	}

	switch (offset)
	{
		case 0x00:
		case 0x08:
			break;

		case 0x09:
			colours = FILL_WORD(FG_COLOUR);
		case 0x01:
			m_x = data;
			break;

		case 0x02:
		case 0x0A:
			m_y++;
			break;

		case 0x0B:
			colours = FILL_WORD(FG_COLOUR);
		case 0x03:
			m_x = data;
			m_y++;
			break;

		case 0x05:
		case 0x0D:
			m_x++;
			break;

		case 0x0E:
			colours = FILL_WORD(FG_COLOUR);
		case 0x06:
			m_y = data;
			break;

		case 0x0F:
			colours = FILL_WORD(FG_COLOUR);
		case 0x07:
			m_y = data;
			m_x++;
			break;

		case 0x10:
			m_yline = data;
			return;
		case 0x11:
			m_op = data;
			return;
		case 0x12:
			m_colours = data;
			return;
		case 0x13:
			/*
			    bits 0..3 of reg026 contain the border colour.
			    bit 5 contains the 40/80 column (320/640 pixel) flag.
			*/
			m_mode = data;
			return;
		case 0x14:
		case 0x15:
		case 0x16:
		case 0x17:
			change_palette(offset - 0x14, data);
			return;

		default:
			logerror("nimbus: unknown video reg write %02x %04x\n", offset, data);
			return;
	}
	if(offset & 0x08)
		write_pixel_data(m_x, m_y, colours);
}

void rmnimbus_state::set_pixel(uint16_t x, uint16_t y, uint8_t colour)
{
	if(DEBUG_SET(DEBUG_TEXT | DEBUG_PIXEL))
		logerror("set_pixel(x=%d, y=%d, colour=%04X), IS_XOR=%02X\n",x,y,colour,IS_XOR);

	if((x<640) && (y<250))
	{
		if(IS_XOR)
			m_video_mem.pix16(y, x)^=colour;
		else
			m_video_mem.pix16(y, x)=colour;
	}
}

void rmnimbus_state::set_pixel40( uint16_t x, uint16_t y, uint8_t colour)
{
	set_pixel((x*2),y,colour);
	set_pixel((x*2)+1,y,colour);
}

void rmnimbus_state::write_pixel_line(uint16_t x, uint16_t y, uint16_t data, uint8_t pixels, uint8_t bpp)
{
	uint8_t colour;
	uint8_t mask = (1 << bpp) - 1;
	x *= pixels;

	if(DEBUG_SET(DEBUG_TEXT | DEBUG_PIXEL))
		logerror("write_pixel_line(x=%d, y=%d, data=%04X, width=%d, bpp=%d)\n",x,y,data,pixels,bpp);

	for(int i = (pixels - 1); i >= 0; i--)
	{
		if(bpp==1)
			colour = SELECT_COL(x + i, (data & 1) ? FG_COLOUR : BG_COLOUR);
		else if(IS_80COL)
			colour = (data & mask) << 2;
		else
			colour = (data & mask);

		if(IS_80COL)
			set_pixel(x + i,y,colour);
		else
			set_pixel40(x + i,y,colour);

		data >>= bpp;
	}
}

void rmnimbus_state::move_pixel_line(uint16_t x, uint16_t y, uint8_t pixels)
{
	x *= pixels;
	if(DEBUG_SET(DEBUG_TEXT | DEBUG_PIXEL))
		logerror("move_pixel_line(x=%d, y=%d, width=%d)\n",x,y,pixels);

	for(int i = 0; i < pixels; i++)
	{
		if(DEBUG_SET(DEBUG_TEXT | DEBUG_PIXEL))
			logerror("x=%d\n",x + i);
		m_video_mem.pix16(m_yline, x + i) = m_video_mem.pix16(y, x + i);
	}
}



/*
    The values in the bottom 3 bits of reg022 seem to determine the number of bits per pixel
    for following operations.

    The values that I have decoded so far are :

    000 1bpp, foreground and background colours taken from reg024
    001 2bpp, using the first 4 colours of the pallette
    010
    011
    100 4bpp, must be a 16 bit word, of which the upper byte is a mask anded with existing pixels then ored
              with the lower byte containing the pixel data for two pixels.
    101 Move pixel data at x,reg020 to x,y, used for scrolling.
    110 if 40 col
            4bpp, 16 bit word containing the pixel data for 4 pixels.
        else
            2bpp, 16 bit word containing the pixel data for 8 pixels.
    111

    Bit 3 of reg022 is as follows :

    0   pixels are written from supplied colour data
    1   pixels are xor'ed onto the screen
*/

void rmnimbus_state::write_pixel_data(uint16_t x, uint16_t y, uint16_t    data)
{
	if(DEBUG_SET(DEBUG_TEXT | DEBUG_PIXEL))
		logerror("write_pixel_data(x=%d, y=%d, data=%04X), reg022=%04X\n",x,y,data,m_op);

	if(IS_80COL)
	{
		switch (m_op & WIDTH_MASK)
		{
			case 0x00:
				write_pixel_line(x,y,data,16,1);
				break;

			case 0x01:
				write_pixel_line(x,y,data,8,1);
				break;

			case 0x02:
				write_pixel_line(x,y,data,8,1);
				break;

			case 0x03:
				set_pixel(x,y,SELECT_COL(x, FG_COLOUR));
				break;

			case 0x04:
				write_pixel_line(x,y,(((data & 0xFF00)>>8) & (data & 0xFF)) | (~((data & 0xFF00)>>8) & read_pixel_line(x,y,4,2)),4,2);
				break;

			case 0x05:
				move_pixel_line(x,y,16);
				break;

			case 0x06:
				write_pixel_line(x,y,data,8,2);
				break;

			case 0x07:
				set_pixel(x,y,SELECT_COL(x, FG_COLOUR));
				break;
		}
	}
	else /* 40 Col */
	{
		switch (m_op & WIDTH_MASK)
		{
			case 0x00:
				write_pixel_line(x,y,data,8,1);
				break;

			case 0x01:
				write_pixel_line(x,y,data,4,2);
				break;

			case 0x02:
				set_pixel40(x,y,FG_COLOUR);
				break;

			case 0x03:
				set_pixel(x,y,FG_COLOUR);
				break;

			case 0x04:
				write_pixel_line(x,y,(((data & 0xFF00)>>8) & (data & 0xFF)) | (~((data & 0xFF00)>>8) & read_pixel_line(x,y,2,4)),2,4);
				break;

			case 0x05:
				move_pixel_line(x,y,16);
				break;

			case 0x06:
				write_pixel_line(x,y,data,4,4);
				break;

			case 0x07:
				set_pixel(x,y,FG_COLOUR);
				break;
		}
	}
}

void rmnimbus_state::change_palette(uint8_t bank, uint16_t colours)
{
	// loop over changing colours
	for(int colourno = (bank * 4); colourno < ((bank + 1) * 4); colourno++)
	{
		int i = (colours & 8) >> 3;
		m_palette->set_pen_color(colourno, pal2bit((colours & 2) | i), pal2bit(((colours & 4) >> 1) | i), pal2bit(((colours & 1) << 1) | i));

		if(DEBUG_SET(DEBUG_TEXT))
			logerror("set colourno[%02X], colour=%02X\n",colourno, colours & 0xf);
		colours >>= 4;
	}
}

void rmnimbus_state::video_debug(int ref, const std::vector<std::string> &params)
{
	if (params.size() > 0)
	{
		int temp;
		sscanf(params[0].c_str(), "%d", &temp);
		m_debug_video = temp;
	}
	else
	{
		machine().debugger().console().printf("Error usage : nimbus_vid_debug <debuglevel>\n");
		machine().debugger().console().printf("Current debuglevel=%02X\n", m_debug_video);
	}
}

void rmnimbus_state::video_start()
{
	m_debug_video = 0;

	m_screen->register_screen_bitmap(m_video_mem);

	if (machine().debug_flags & DEBUG_FLAG_ENABLED)
	{
		using namespace std::placeholders;
		machine().debugger().console().register_command("nimbus_vid_debug", CMDFLAG_NONE, 0, 0, 1, std::bind(&rmnimbus_state::video_debug, this, _1, _2));
	}
}

void rmnimbus_state::video_reset()
{
	m_mode = 0;
	m_x = 0;
	m_y = 0;
	m_op = 0;
	m_yline = 0;
}

uint32_t rmnimbus_state::screen_update_nimbus(screen_device &screen, bitmap_ind16 &bitmap, const rectangle &cliprect)
{
	copybitmap(bitmap, m_video_mem, 0, 0, 0, 0, cliprect);

	return 0;
}