summaryrefslogtreecommitdiff
path: root/hw/kdrive/mga/mgadraw.c
blob: 04cd44fc9f4668712eb79ce76a2d4767b4bf17bd (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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
 * $Id$
 *
 * Copyright © 2003 Anders Carlsson
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the name of Anders Carlsson not be used in
 * advertising or publicity pertaining to distribution of the software without
 * specific, written prior permission.  Anders Carlsson makes no
 * representations about the suitability of this software for any purpose.  It
 * is provided "as is" without express or implied warranty.
 *
 * ANDERS CARLSSON DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 * EVENT SHALL ANDERS CARLSSON BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
 */
/* $Header$ */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "mga.h"

CARD32 mgaRop[16] = {
    /* GXclear        */  MGA_ATYPE_RPL  | 0x00000000,	/* 0 */
    /* GXand          */  MGA_ATYPE_RSTR | 0x00080000,	/* src AND dst */
    /* GXandReverse   */  MGA_ATYPE_RSTR | 0x00040000,	/* src AND NOT dst */
    /* GXcopy         */  MGA_ATYPE_RSTR | 0x000c0000,	/* src */
    /* GXandInverted  */  MGA_ATYPE_RSTR | 0x00020000,	/* NOT src AND dst */
    /* GXnoop         */  MGA_ATYPE_RSTR | 0x000a0000,	/* dst */
    /* GXxor          */  MGA_ATYPE_RSTR | 0x00060000,	/* src XOR dst */
    /* GXor           */  MGA_ATYPE_RSTR | 0x000e0000,	/* src OR dst */
    /* GXnor          */  MGA_ATYPE_RSTR | 0x00010000,	/* NOT src AND NOT dst */
    /* GXequiv        */  MGA_ATYPE_RSTR | 0x00090000,	/* NOT src XOR dst */
    /* GXinvert       */  MGA_ATYPE_RSTR | 0x00050000,	/* NOT dst */
    /* GXorReverse    */  MGA_ATYPE_RSTR | 0x000d0000,	/* src OR NOT dst */
    /* GXcopyInverted */  MGA_ATYPE_RPL  | 0x00030000,	/* NOT src */
    /* GXorInverted   */  MGA_ATYPE_RSTR | 0x000b0000,	/* NOT src OR dst */
    /* GXnand         */  MGA_ATYPE_RSTR | 0x00070000,	/* NOT src OR NOT dst */
    /* GXset          */  MGA_ATYPE_RPL  | 0x000f0000	/* 1 */
};

static VOL8 *mmio;
int fifo_size;
int pitch, src_pitch;
int dir;

static void
mgaWaitAvail (int n)
{
    if (fifo_size < n) {
      while ((fifo_size = MGA_IN32 (mmio, MGA_REG_FIFOSTATUS) & 0xff) < n)
	;
    }
    
    fifo_size -= n;
}

static void
mgaWaitIdle (void)
{
    while (MGA_IN32 (mmio, MGA_REG_STATUS) & 0x10000);
}

static Bool
mgaSetup (ScreenPtr pScreen, int wait)
{
  KdScreenPriv (pScreen);
  mgaScreenInfo (pScreenPriv);
  mgaCardInfo (pScreenPriv);

  fifo_size = 0;
  mmio = mgac->reg_base;
  pitch = mgas->pitch;
  
  if (!mmio)
    return FALSE;

  mgaWaitAvail (wait + 4);
  MGA_OUT32 (mmio, MGA_REG_MACCESS, mgas->pw);
  MGA_OUT32 (mmio, MGA_REG_CXBNDRY, 0xffff0000);
  MGA_OUT32 (mmio, MGA_REG_YTOP, 0x00000000);
  MGA_OUT32 (mmio, MGA_REG_YBOT, 0x007fffff);

  return TRUE;
}

static Bool
mgaPrepareSolid (PixmapPtr pPixmap, int alu, Pixel pm, Pixel fg)
{
    KdScreenPriv(pPixmap->drawable.pScreen);
    int cmd;
    int dst_org;

    cmd = MGA_OPCOD_TRAP | MGA_DWGCTL_SOLID | MGA_DWGCTL_ARZERO | MGA_DWGCTL_SGNZERO |
	MGA_DWGCTL_SHIFTZERO | mgaRop[alu];

    dst_org = (int)pPixmap->devPrivate.ptr - (int)pScreenPriv->screen->memory_base;
    
    mgaSetup (pPixmap->drawable.pScreen, 5);

    MGA_OUT32 (mmio, MGA_REG_DSTORG, dst_org);
    MGA_OUT32 (mmio, MGA_REG_PITCH, pPixmap->devKind / (pPixmap->drawable.bitsPerPixel >> 3));
    MGA_OUT32 (mmio, MGA_REG_DWGCTL, cmd);
    MGA_OUT32 (mmio, MGA_REG_FCOL, fg);
    MGA_OUT32 (mmio, MGA_REG_PLNWT, pm);
    
    return TRUE;
}

static void
mgaSolid (int x1, int y1, int x2, int y2)
{
    mgaWaitAvail (2);

    MGA_OUT32 (mmio, MGA_REG_FXBNDRY, (x2 << 16) | (x1 & 0xffff));
    MGA_OUT32 (mmio, MGA_REG_YDSTLEN | MGA_REG_EXEC, (y1 << 16) | (y2 - y1));
}

static void
mgaDoneSolid (void)
{
}

#define BLIT_LEFT	1
#define BLIT_UP		4

static Bool
mgaPrepareCopy (PixmapPtr pSrcPixmap, PixmapPtr pDstPixmap,
		int dx, int dy, int alu, Pixel pm)
{
    KdScreenPriv(pSrcPixmap->drawable.pScreen);

    int cmd;

    cmd = MGA_OPCOD_BITBLT | MGA_DWGCTL_BFCOL | MGA_DWGCTL_SHIFTZERO | mgaRop[alu];

    dir = 0;

    if (dy < 0)
	dir |= BLIT_UP;
    if (dx < 0)
	dir |= BLIT_LEFT;

    mgaSetup (pSrcPixmap->drawable.pScreen, 7);

    MGA_OUT32 (mmio, MGA_REG_SRCORG, ((int)pSrcPixmap->devPrivate.ptr - (int)pScreenPriv->screen->memory_base));
    MGA_OUT32 (mmio, MGA_REG_DSTORG, ((int)pDstPixmap->devPrivate.ptr - (int)pScreenPriv->screen->memory_base));
    MGA_OUT32 (mmio, MGA_REG_PITCH, pDstPixmap->devKind / (pDstPixmap->drawable.bitsPerPixel >> 3));
    src_pitch = pSrcPixmap->devKind / (pSrcPixmap->drawable.bitsPerPixel >> 3);
	
    MGA_OUT32 (mmio, MGA_REG_DWGCTL, cmd);
    MGA_OUT32 (mmio, MGA_REG_SGN, dir);
    MGA_OUT32 (mmio, MGA_REG_PLNWT, pm);
    MGA_OUT32 (mmio, MGA_REG_AR5, src_pitch * (dy < 0 ? -1 : 1) );

    return TRUE;
}

static void
mgaCopy (int srcX, int srcY, int dstX, int dstY, int w, int h)
{
    int start, end;
    
    if (dir & BLIT_UP)
    {
	srcY += h - 1;
	dstY += h - 1;
    }

    w--;
    start = end = srcY * src_pitch + srcX;

    if (dir & BLIT_LEFT)
	start += w;
    else
	end += w;

    mgaWaitAvail (4);
    MGA_OUT32 (mmio, MGA_REG_AR0, end);
    MGA_OUT32 (mmio, MGA_REG_AR3, start);
    MGA_OUT32 (mmio, MGA_REG_FXBNDRY, ((dstX + w) << 16) | (dstX & 0xffff));
    MGA_OUT32 (mmio, MGA_REG_YDSTLEN | MGA_REG_EXEC, (dstY << 16) | h);
}

static void
mgaDoneCopy (void)
{
}

KaaScreenInfoRec mgaKaa = {
    mgaPrepareSolid,
    mgaSolid,
    mgaDoneSolid,

    mgaPrepareCopy,
    mgaCopy,
    mgaDoneCopy,

    192, /* Offscreen byte alignment */
    64, /* Offset pitch */
    KAA_OFFSCREEN_PIXMAPS, /* Flags */
};

Bool
mgaDrawInit (ScreenPtr pScreen)
{
    if (!kaaDrawInit (pScreen, &mgaKaa))
	return FALSE;

    return TRUE;
}

void
mgaDrawEnable (ScreenPtr pScreen)
{
    KdScreenPriv (pScreen);
    mgaScreenInfo (pScreenPriv);

    mgas->pitch = pScreenPriv->screen->width;

    switch (pScreenPriv->screen->fb[0].depth) {
    case 8:
      mgas->pw = MGA_PW8;
      break;
    case 16:
      mgas->pw = MGA_PW16;
      break;
    case 24:
      mgas->pw = MGA_PW24;
      break;
    case 32:
      mgas->pw = MGA_PW32;
      break;
    default:
      FatalError ("unsupported pixel format");
    }

    KdMarkSync (pScreen);
}

void
mgaDrawDisable (ScreenPtr pScreen)
{
}

void
mgaDrawFini (ScreenPtr pScreen)
{
}

void
mgaDrawSync (ScreenPtr pScreen)
{
    KdScreenPriv (pScreen);
    mgaCardInfo (pScreenPriv);

    mmio = mgac->reg_base;

    mgaWaitIdle ();
}