summaryrefslogtreecommitdiff
path: root/sys/src/9/pc/vgabt485.c
blob: 50dbfa392ee0f5dc240cb0cf5592aba146ad81cf (plain)
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
#include "u.h"
#include "../port/lib.h"
#include "mem.h"
#include "dat.h"
#include "fns.h"
#include "io.h"
#include "../port/error.h"

#define	Image	IMAGE
#include <draw.h>
#include <memdraw.h>
#include <cursor.h>
#include "screen.h"

/*
 * Hardware graphics cursor support for
 * Brooktree Bt485 Monolithic True-Color RAMDAC.
 * Assumes hooked up to an S3 86C928.
 *
 * BUGS:
 *	64x64x2 cursor always used;
 *	no support for interlaced mode.
 */
enum {
	AddrW		= 0x00,		/* Address register; palette/cursor RAM write */
	Palette		= 0x01,		/* 6/8-bit color palette data */
	Pmask		= 0x02,		/* Pixel mask register */
	AddrR		= 0x03,		/* Address register; palette/cursor RAM read */
	ColorW		= 0x04,		/* Address register; cursor/overscan color write */
	Color		= 0x05,		/* Cursor/overscan color data */
	Cmd0		= 0x06,		/* Command register 0 */
	ColorR		= 0x07,		/* Address register; cursor/overscan color read */
	Cmd1		= 0x08,		/* Command register 1 */
	Cmd2		= 0x09,		/* Command register 2 */
	Status		= 0x0A,		/* Status */
	Cmd3		= 0x1A,		/* Command register 3 */
	Cram		= 0x0B,		/* Cursor RAM array data */
	Cxlr		= 0x0C,		/* Cursor x-low register */
	Cxhr		= 0x0D,		/* Cursor x-high register */
	Cylr		= 0x0E,		/* Cursor y-low register */
	Cyhr		= 0x0F,		/* Cursor y-high register */

	Nreg		= 0x10,
};

/*
 * Lower 2-bits of indirect DAC register
 * addressing.
 */
static ushort dacxreg[4] = {
	PaddrW, Pdata, Pixmask, PaddrR
};

static uchar
bt485io(uchar reg)
{
	uchar crt55, cr0;

	crt55 = vgaxi(Crtx, 0x55) & 0xFC;
	if((reg & 0x0F) == Status){
		/*
		 * 1,2: Set indirect addressing for Status or
		 *      Cmd3 - set bit7 of Cr0.
		 */
		vgaxo(Crtx, 0x55, crt55|((Cmd0>>2) & 0x03));
		cr0 = vgai(dacxreg[Cmd0 & 0x03])|0x80;
		vgao(dacxreg[Cmd0 & 0x03], cr0);

		/*
		 * 3,4: Set the index into the Write register,
		 *      index == 0x00 for Status, 0x01 for Cmd3.
		 */
		vgaxo(Crtx, 0x55, crt55|((AddrW>>2) & 0x03));
		vgao(dacxreg[AddrW & 0x03], (reg == Status) ? 0x00: 0x01);

		/*
		 * 5,6: Get the contents of the appropriate
		 *      register at 0x0A.
		 */
	}

	return crt55;
}

static uchar
bt485i(uchar reg)
{
	uchar crt55, r;

	crt55 = bt485io(reg);
	vgaxo(Crtx, 0x55, crt55|((reg>>2) & 0x03));
	r = vgai(dacxreg[reg & 0x03]);
	vgaxo(Crtx, 0x55, crt55);

	return r;
}

static void
bt485o(uchar reg, uchar data)
{
	uchar crt55;

	crt55 = bt485io(reg);
	vgaxo(Crtx, 0x55, crt55|((reg>>2) & 0x03));
	vgao(dacxreg[reg & 0x03], data);
	vgaxo(Crtx, 0x55, crt55);
}

static void
bt485disable(VGAscr*)
{
	uchar r;

	/*
	 * Disable 
	 *	cursor mode 3;
	 *	cursor control enable for Bt485 DAC;
	 *	the hardware cursor external operation mode.
	 */
	r = bt485i(Cmd2) & ~0x03;
	bt485o(Cmd2, r);

	r = vgaxi(Crtx, 0x45) & ~0x20;
	vgaxo(Crtx, 0x45, r);

	r = vgaxi(Crtx, 0x55) & ~0x20;
	vgaxo(Crtx, 0x55, r);
}

static void
bt485enable(VGAscr*)
{
	uchar r;

	/*
	 * Turn cursor off.
	 */
	r = bt485i(Cmd2) & 0xFC;
	bt485o(Cmd2, r);

	/*
	 * Overscan colour,
	 * cursor colour 1 (white),
	 * cursor colour 2, 3 (black).
	 */
	bt485o(ColorW, 0x00);
	bt485o(Color, Pwhite); bt485o(Color, Pwhite); bt485o(Color, Pwhite);

	bt485o(Color, Pwhite); bt485o(Color, Pwhite); bt485o(Color, Pwhite);

	bt485o(Color, Pblack); bt485o(Color, Pblack); bt485o(Color, Pblack);
	bt485o(Color, Pblack); bt485o(Color, Pblack); bt485o(Color, Pblack);

	/*
	 * Finally, enable
	 *	the hardware cursor external operation mode;
	 *	cursor control enable for Bt485 DAC.
	 * The #9GXE cards seem to need the 86C928 Bt485 support
	 * enabled in order to work at all in enhanced mode.
	 */

	r = vgaxi(Crtx, 0x55)|0x20;
	vgaxo(Crtx, 0x55, r);

	r = vgaxi(Crtx, 0x45)|0x20;
	vgaxo(Crtx, 0x45, r);
}

static void
bt485load(VGAscr* scr, Cursor* curs)
{
	uchar r;
	int x, y;

	/*
	 * Turn cursor off;
	 * put cursor into 64x64x2 mode and clear MSBs of address;
	 * clear LSBs of address;
	 */
	r = bt485i(Cmd2) & 0xFC;
	bt485o(Cmd2, r);

	r = (bt485i(Cmd3) & 0xFC)|0x04;
	bt485o(Cmd3, r);

	bt485o(AddrW, 0x00);

	/*
	 * Now load the cursor RAM array, both planes.
	 * The cursor is 16x16, the array 64x64; put
	 * the cursor in the top left. The 0,0 cursor
	 * point is bottom-right, so positioning will
	 * have to take that into account.
	 */
	for(y = 0; y < 64; y++){
		for(x = 0; x < 64/8; x++){
			if(x < 16/8 && y < 16)
				bt485o(Cram, curs->clr[x+y*2]);
			else
				bt485o(Cram, 0x00);
		}
	}
	for(y = 0; y < 64; y++){
		for(x = 0; x < 64/8; x++){
			if(x < 16/8 && y < 16)
				bt485o(Cram, curs->set[x+y*2]);
			else
				bt485o(Cram, 0x00);
		}
	}

	/*
	 * Initialise the cursor hot-point
	 * and enable the cursor.
	 */
	scr->offset.x = 64+curs->offset.x;
	scr->offset.y = 64+curs->offset.y;

	r = (bt485i(Cmd2) & 0xFC)|0x01;
	bt485o(Cmd2, r);
}

static int
bt485move(VGAscr* scr, Point p)
{
	int x, y;

	x = p.x+scr->offset.x;
	y = p.y+scr->offset.y;

	bt485o(Cxlr, x & 0xFF);
	bt485o(Cxhr, (x>>8) & 0x0F);
	bt485o(Cylr, y & 0xFF);
	bt485o(Cyhr, (y>>8) & 0x0F);

	return 0;
}

VGAcur vgabt485cur = {
	"bt485hwgc",

	bt485enable,
	bt485disable,
	bt485load,
	bt485move,
};