summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/bus/wangpc/lvc.c
blob: dc943a70e0d14da02eeb908e3808e216fe99d2c2 (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
// license:BSD-3-Clause
// copyright-holders:Curt Coder
/**********************************************************************

    Wang PC Low-Resolution Video Controller emulation

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

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

/*

    TODO:

    - cursor
    - scroll
    - option bit 1?

*/

#include "lvc.h"



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

#define LOG 0

#define OPTION_ID       0x10

#define MC6845_TAG      "mc6845"
#define SCREEN_TAG      "screen"

#define RAM_SIZE        0x8000

#define OPTION_VRAM     BIT(m_option, 0)
#define OPTION_UNKNOWN  BIT(m_option, 1)
#define OPTION_80_COL   BIT(m_option, 2)
#define OPTION_VSYNC    BIT(m_option, 3)



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

const device_type WANGPC_LVC = &device_creator<wangpc_lvc_device>;


//-------------------------------------------------
//  mc6845_interface crtc_intf
//-------------------------------------------------

void wangpc_lvc_device::crtc_update_row(mc6845_device *device, bitmap_rgb32 &bitmap, const rectangle &cliprect, UINT16 ma, UINT8 ra, UINT16 y, UINT8 x_count, INT8 cursor_x, void *param)
{
	offs_t scroll_y = (((m_scroll >> 8) + 0x15) & 0xff) * 0x80;

	if (OPTION_80_COL)
	{
		for (int column = 0; column < x_count; column++)
		{
			offs_t addr = scroll_y + (m_scroll & 0x3f) + ((ma / 80) * 0x480) + (((ra & 0x0f) << 7) | (column & 0x7f));
			UINT16 data = m_video_ram[addr & 0x7fff];

			for (int bit = 0; bit < 8; bit++)
			{
				int x = (column * 8) + bit;
				int color = (BIT(data, 15) << 1) | BIT(data, 7);

				if (column == cursor_x) color = 0x03;

				bitmap.pix32(y, x) = m_palette[color];

				data <<= 1;
			}
		}
	}
	else
	{
		//offs_t addr = scroll_y + ((m_scroll & 0x3f) << 1) + ((ma / 40) * 0x480) + (((ra & 0x0f) << 7));
		offs_t addr = scroll_y + ((m_scroll & 0x3f) << 1) + (y * 0x80);

		for (int column = 0; column < x_count; column++)
		{
			UINT32 data = (m_video_ram[(addr + 1) & 0x7fff] << 16) | m_video_ram[addr & 0x7fff];

			for (int bit = 0; bit < 8; bit++)
			{
				int x = (column * 8) + bit;
				int color = (BIT(data, 31) << 3) | (BIT(data, 23) << 2) | (BIT(data, 15) << 1) | BIT(data, 7);

				if (column == cursor_x) color = 0x03;

				bitmap.pix32(y, x) = m_palette[color];

				data <<= 1;
			}

			addr += 2;
		}
	}
}

static MC6845_UPDATE_ROW( wangpc_lvc_update_row )
{
	wangpc_lvc_device *lvc = downcast<wangpc_lvc_device *>(device->owner());

	lvc->crtc_update_row(device, bitmap, cliprect, ma, ra, y, x_count, cursor_x, param);
}

WRITE_LINE_MEMBER( wangpc_lvc_device::vsync_w )
{
	if (OPTION_VSYNC && state)
	{
		set_irq(ASSERT_LINE);
	}
}

static MC6845_INTERFACE( crtc_intf )
{
	false,
	0,0,0,0,
	8,
	NULL,
	wangpc_lvc_update_row,
	NULL,
	DEVCB_NULL,
	DEVCB_NULL,
	DEVCB_NULL,
	DEVCB_DEVICE_LINE_MEMBER(DEVICE_SELF_OWNER, wangpc_lvc_device, vsync_w),
	NULL
};


//-------------------------------------------------
//  MACHINE_CONFIG_FRAGMENT( wangpc_lvc )
//-------------------------------------------------

static MACHINE_CONFIG_FRAGMENT( wangpc_lvc )
	MCFG_SCREEN_ADD(SCREEN_TAG, RASTER)
	MCFG_SCREEN_UPDATE_DEVICE(MC6845_TAG, mc6845_device, screen_update)
	MCFG_SCREEN_SIZE(80*8, 25*9)
	MCFG_SCREEN_VISIBLE_AREA(0, 80*8-1, 0, 25*9-1)
	MCFG_SCREEN_VBLANK_TIME(ATTOSECONDS_IN_USEC(2500))
	MCFG_SCREEN_REFRESH_RATE(60)

	MCFG_MC6845_ADD(MC6845_TAG, MC6845_1, SCREEN_TAG, XTAL_14_31818MHz/16, crtc_intf)
MACHINE_CONFIG_END


//-------------------------------------------------
//  machine_config_additions - device-specific
//  machine configurations
//-------------------------------------------------

machine_config_constructor wangpc_lvc_device::device_mconfig_additions() const
{
	return MACHINE_CONFIG_NAME( wangpc_lvc );
}



//**************************************************************************
//  INLINE HELPERS
//**************************************************************************

//-------------------------------------------------
//  set_irq -
//-------------------------------------------------

inline void wangpc_lvc_device::set_irq(int state)
{
	m_irq = state;

	m_bus->irq3_w(m_irq);
}



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

//-------------------------------------------------
//  wangpc_lvc_device - constructor
//-------------------------------------------------

wangpc_lvc_device::wangpc_lvc_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock) :
	device_t(mconfig, WANGPC_LVC, "Wang PC Low Resolution Video Card", tag, owner, clock, "wangpc_lvc", __FILE__),
	device_wangpcbus_card_interface(mconfig, *this),
	m_crtc(*this, MC6845_TAG),
	m_video_ram(*this, "video_ram"),
	m_option(0),
	m_irq(CLEAR_LINE)
{
}


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

void wangpc_lvc_device::device_start()
{
	// allocate memory
	m_video_ram.allocate(RAM_SIZE);

	// state saving
	save_item(NAME(m_option));
	save_item(NAME(m_scroll));
	save_item(NAME(m_irq));
}


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

void wangpc_lvc_device::device_reset()
{
	m_option = 0;

	set_irq(CLEAR_LINE);
}


//-------------------------------------------------
//  wangpcbus_mrdc_r - memory read
//-------------------------------------------------

UINT16 wangpc_lvc_device::wangpcbus_mrdc_r(address_space &space, offs_t offset, UINT16 mem_mask)
{
	UINT16 data = 0xffff;

	if (OPTION_VRAM && (offset >= 0xe0000/2) && (offset < 0xf0000/2))
	{
		offs_t addr = offset & 0x7fff;

		data = m_video_ram[addr];
	}

	return data;
}


//-------------------------------------------------
//  wangpcbus_amwc_w - memory write
//-------------------------------------------------

void wangpc_lvc_device::wangpcbus_amwc_w(address_space &space, offs_t offset, UINT16 mem_mask, UINT16 data)
{
	if (OPTION_VRAM && (offset >= 0xe0000/2) && (offset < 0xf0000/2))
	{
		offs_t addr = offset & 0x7fff;

		m_video_ram[addr] = data;
	}
}


//-------------------------------------------------
//  wangpcbus_iorc_r - I/O read
//-------------------------------------------------

UINT16 wangpc_lvc_device::wangpcbus_iorc_r(address_space &space, offs_t offset, UINT16 mem_mask)
{
	UINT16 data = 0xffff;

	if (sad(offset))
	{
		switch (offset & 0x7f)
		{
		case 0x02/2:
			data = 0xff00 | m_crtc->register_r(space, 0);
			break;

		case 0x30/2:
			data = 0xffe3;
			data |= m_crtc->de_r() << 2;
			data |= m_crtc->vsync_r() << 3;
			data |= m_crtc->hsync_r() << 4;
			break;

		case 0xfe/2:
			data = 0xff00 | (m_irq << 7) | OPTION_ID;
			break;
		}
	}

	return data;
}


//-------------------------------------------------
//  wangpcbus_aiowc_w - I/O write
//-------------------------------------------------

void wangpc_lvc_device::wangpcbus_aiowc_w(address_space &space, offs_t offset, UINT16 mem_mask, UINT16 data)
{
	if (sad(offset))
	{
		switch (offset & 0x7f)
		{
		case 0x00/2:
			if (ACCESSING_BITS_0_7)
			{
				m_crtc->address_w(space, 0, data & 0xff);
			}
			break;

		case 0x02/2:
			if (ACCESSING_BITS_0_7)
			{
				m_crtc->register_w(space, 0, data & 0xff);
			}
			break;

		case 0x10/2:
			if (ACCESSING_BITS_0_7)
			{
				if (LOG) logerror("LVC option %02x\n", data & 0xff);
				m_option = data & 0xff;

				if (OPTION_80_COL)
				{
					m_crtc->set_clock(XTAL_14_31818MHz / 8);
				}
				else
				{
					m_crtc->set_clock(XTAL_14_31818MHz / 16);
				}
			}
			break;

		case 0x20/2:
			if (LOG) logerror("LVC scroll %04x\n", data);
			m_scroll = data;
			break;

		case 0x40/2: case 0x42/2: case 0x44/2: case 0x46/2: case 0x48/2: case 0x4a/2: case 0x4c/2: case 0x4e/2:
		case 0x50/2: case 0x52/2: case 0x55/2: case 0x56/2: case 0x58/2: case 0x5a/2: case 0x5c/2: case 0x5e/2:
			{
				offs_t index = offset & 0x0f;

				int i = BIT(data, 15);
				int r = BIT(data, 11) ? (i ? 0xff : 0x80) : 0;
				int g = BIT(data, 7) ? (i ? 0xff : 0x80) : 0;
				int b = BIT(data, 3) ? (i ? 0xff : 0x80) : 0;

				m_palette[index] = MAKE_RGB(r, g, b);
			}
			break;

		case 0x70/2:
			set_irq(CLEAR_LINE);
			break;
		}
	}
}