File: mac_mode.c

package info (click to toggle)
glide 2002.04.10ds1-16
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 59,540 kB
  • sloc: ansic: 290,125; asm: 23,305; sh: 8,089; pascal: 3,854; makefile: 1,276; perl: 73
file content (483 lines) | stat: -rw-r--r-- 11,492 bytes parent folder | download | duplicates (8)
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
/*
** THIS SOFTWARE IS SUBJECT TO COPYRIGHT PROTECTION AND IS OFFERED ONL
** PURSUANT TO THE 3DFX GLIDE GENERAL PUBLIC LICENSE. THERE IS NO RIGH
** TO USE THE GLIDE TRADEMARK WITHOUT PRIOR WRITTEN PERMISSION OF 3DF
** INTERACTIVE, INC. A COPY OF THIS LICENSE MAY BE OBTAINED FROM THE
** DISTRIBUTOR OR BY CONTACTING 3DFX INTERACTIVE INC(info@3dfx.com).
** THIS PROGRAM IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
** EXPRESSED OR IMPLIED. SEE THE 3DFX GLIDE GENERAL PUBLIC LICENSE FOR 
** FULL TEXT OF THE NON-WARRANTY PROVISIONS. 
**
** USE, DUPLICATION OR DISCLOSURE BY THE GOVERNMENT IS SUBJECT T
** RESTRICTIONS AS SET FORTH IN SUBDIVISION (C)(1)(II) OF THE RIGHTS I
** TECHNICAL DATA AND COMPUTER SOFTWARE CLAUSE AT DFARS 252.227-7013
** AND/OR IN SIMILAR OR SUCCESSOR CLAUSES IN THE FAR, DOD OR NASA FA
** SUPPLEMENT. UNPUBLISHED RIGHTS RESERVED UNDER THE COPYRIGHT LAWS O
** THE UNITED STATES. 
**
** COPYRIGHT 3DFX INTERACTIVE, INC. 1999, ALL RIGHTS RESERVE
*/

#ifdef DCON
#undef DCON
#endif

#define DCON 0

#include <3dfx.h>
#include <gdebug.h>

#include <h3cinit.h>
#include <minihwc.h>

#include "setmode.h"

#define __MACERRORS_
#include <Devices.h>
#include <Displays.h>
#include <GraphicsPriv.h>
#include <GraphicsPrivHwc.h>
#include <DriverServices.h>
#include <hdwr_res_mgr.h>
#include <hrm_fifo.h>
#include <hrm_mode.h>
#include <hrm_mem.h>
#include <DCon.h>
#include <AGP.h>
#include <CodeFragments.h>

hrmGetVersionInfoPtr _hrmGetVersionInfo;
hrmSetExclusiveModePtr _hrmSetExclusiveMode;
hrmSetVideoModePtr _hrmSetVideoMode;
hrmReleaseExclusiveModePtr _hrmReleaseExclusiveMode;    
hrmAllocateBlockPtr _hrmAllocateBlock;
hrmFreeBlockPtr _hrmFreeBlock;
hrmAllocWinContextPtr _hrmAllocWinContext;
hrmFreeWinContextPtr _hrmFreeWinContext;
hrmExecuteWinFifoPtr _hrmExecuteWinFifo;
hrmGetDeviceConfigPtr _hrmGetDeviceConfig;
hrmGetAGPInfoPtr _hrmGetAGPInfo;
hrmWriteConfigRegisterPtr _hrmWriteConfigRegister;
hrmReadConfigRegisterPtr _hrmReadConfigRegister;
hrmGetSlaveRegsPtr _hrmGetSlaveRegs;
hrmSLIAAPtr _hrmSLIAA;

#if GLIDE_BYPASS_DISPLAY_MANAGER
static VDSwitchInfoRec oldModeSwitchInfo;
#endif

static  WindowPtr glideWin;

/* Information used to restore video upon forceful exit */
static  hrmBoard_t         *video_board;
static 	FxBool videoNeedsToBeRestored;
static void CleanupAGP(void);
static AGPAddressPair fifoAddress;
static FxU32 fifoAddressValid;

FxBool 
setVideoMode( void* hwnd, 
			  int 	xRes, int yRes, 
			  int   pixelSize,
			  int 	refresh, 
			  void* hmon ) 
{
  dprintf("hrmSetExclusiveMode(%08lx)\n",hmon);
  
  if(_hrmSetExclusiveMode((hrmBoard_t *)hmon)) {
    if(noErr == _hrmSetVideoMode((hrmBoard_t *)hmon, xRes, yRes, refresh)) {
      videoNeedsToBeRestored = FXTRUE;
      video_board = (hrmBoard_t *)hmon;
      return FXTRUE;
    }
    _hrmReleaseExclusiveMode((hrmBoard_t *)hmon);
    videoNeedsToBeRestored = FXFALSE;
    video_board = 0;
  }
  return FXFALSE;
}

void resetVideo(void)
{
  if(videoNeedsToBeRestored)
  {
    /* Just in case something bad happened, don't call a bogus function. */
    if(_hrmReleaseExclusiveMode) {
        dprintf("calling hrmReleaseExclusiveMode(%08lx)\n",video_board);
	    _hrmReleaseExclusiveMode(video_board);
	    dprintf("hrmReleaseExclusiveMode returned\n");
	    videoNeedsToBeRestored = FXFALSE;
	    video_board = 0;
	}
  }
  #if 0
  if(fifoAddressValid) {
    CleanupAGP();
  }
  #endif
}


hrmBoard_t * hwcBoardInfo2hrmBoard( hwcBoardInfo *bInfo )
{
  hrmBoard_t * hrmBoard;
  hrmBoardInfo_t hrmBInfo;
  short i = 0;
  
  while ( (hrmBoard = hrmGetTargetAtIndex(i++)) != 0 )
  {
    hrmGetTargetBoardInfoExt( hrmBoard, &hrmBInfo);

    if ( bInfo->pciInfo.pciBaseAddr[0] == hrmBInfo.pciBaseAddr[0] )
      break;
  }
  
  return hrmBoard;
}


#if 0
OSErr GetAGPInfo(FxU32 *agpLAddr, FxU32 *agpPAddr, FxU32 *agpSize)
{
  OSErr err;
  
  /* Make sure AGP is around. */
  if((long)AGPNewMemory == (long)kUnresolvedCFragSymbolAddress) {
    return -1;
  }
  
  if(!fifoAddressValid) {
    err = AGPNewMemory(&fifoAddress, 1024*1024, false);
    dprintf("AGPNewMemory returned: %d\n",err);
    if(err != noErr) {
      return -1;
    }
    err = AGPCommitMemory(&fifoAddress, false);
    dprintf("AGPCommitMemory returned: %d\n",err);
    if(err != noErr) {
      AGPDisposeMemory(&fifoAddress);
      return -1;
    }
    fifoAddressValid = 1;
  }
  
  *agpLAddr = (FxU32)fifoAddress.systemLogicalAddress;
  *agpPAddr = (FxU32)fifoAddress.agpLogicalAddress;
  *agpSize = 1024*1024;
  
  dprintf("agpLAddr: %08lx agpPAddr: %08lx\n",*agpLAddr,*agpPAddr);
     
  return 0;
}

static void CleanupAGP(void)
{
  dprintf("CleanupAGP: %d\n",fifoAddressValid);
  if(fifoAddressValid) {
    dprintf("Cleaning up AGP memory\n");
    AGPDecommitMemory(&fifoAddress);
    AGPDisposeMemory(&fifoAddress);
    fifoAddressValid = 0;
  }
}
#endif

#if PCI_BUMP_N_GRIND    
static FxU32 saveCopybackAddr, saveCopybackLength;
static FxU32 cacheSettingsNeedToBeRestored = FXFALSE;

void enableCopyBackCache(FxU32 addr, FxU32 length)
{
    OSErr err;
	saveCopybackAddr = addr;
	saveCopybackLength = length;
	FlushProcessorCache(kCurrentAddressSpaceID, (LogicalAddress)addr, length);
	err = SetProcessorCacheMode(kCurrentAddressSpaceID, (const void *)addr, length, kProcessorCacheModeCopyBack);
	cacheSettingsNeedToBeRestored = FXTRUE;
}

void restoreCacheSettings(void)
{
	if(cacheSettingsNeedToBeRestored)
	{
		FlushProcessorCache(kCurrentAddressSpaceID, (LogicalAddress)saveCopybackAddr, 
			saveCopybackLength);
		SetProcessorCacheMode(kCurrentAddressSpaceID, 
			(const void *)saveCopybackAddr,
			saveCopybackLength,
			kProcessorCacheModeInhibited);
		cacheSettingsNeedToBeRestored = FXFALSE;
	}
}
#endif

#if defined(__MWERKS__) || defined(__MRC__)
/* MW linker generated cleanup stub */

extern void resetVideo(void);
extern void gdxSurfaceInit();
extern void gdxSurfaceShutdown();

extern pascal OSErr myInitialize(const CFragInitBlock *	initBlock)
{
  OSErr err;
  err = __initialize(initBlock);
  gdxSurfaceInit();
  return err;
}

extern pascal void myCodeFragCleanup(void)
{
	/* Restore display mode if it didn't get done already. */
#if GLIDE3	
	gdxSurfaceShutdown();
#endif	
    dprintf("myCodeFragCleanup\n");
#if PCI_BUMP_N_GRIND    
	restoreCacheSettings();
#endif	
	resetVideo();
	/* CleanupAGP(); */
	__terminate();
}
#else /* !__MWERKS__ */
#error "Need CFM cleanup proc for this compiler"
#endif /* !__MWERKS__ */


extern FxU32 swizzleRead32(volatile FxU32 *s);
extern FxU16 swizzleRead16(volatile FxU16 *s);
extern void  swizzleWrite32(volatile FxU32 *d, FxU32 s);
extern void  swizzleWrite16(volatile FxU16 *d, FxU16 s);
extern void  swizzleWriteF(volatile FxU32 *d, FxFloat s);
extern void  swizzleWriteLinear8(volatile FxU32 *d, FxU32 s);

FxU32 (*__swizzleRead32)(volatile FxU32 *s);
FxU16 (*__swizzleRead16)(volatile FxU16 *s);
void (*__swizzleWrite32)(volatile FxU32 *d, FxU32 s);
void (*__swizzleWrite16)(volatile FxU16 *d, FxU16 s);
void (*__swizzleWriteF)(volatile FxU32 *d, FxFloat s);
void (*__swizzleWriteLinear8)(volatile FxU32 *d, FxU32 s);

FxU32 __lfbBase = 0;
FxU32 __regBase = 0;

/* 8-bit desktop mode, LFB accesses are not swizzled, so */
/* we have to swizzle everything ourselves */
FxU32 __swizzleRead32_8(volatile FxU32 *s)
{
	return __lwbrx((void *)s, 0);
}

FxU16 __swizzleRead16_8(volatile FxU16 *s)
{
	return __lhbrx((void *)s, 0);
}

void  __swizzleWrite32_8(volatile FxU32 *d, FxU32 s)
{
	__stwbrx(s,(void *)d, 0);
}

void  __swizzleWrite16_8(volatile FxU16 *d, FxU16 s)
{
	__sthbrx(s,(void *)d, 0);
}

void  __swizzleWriteF_8(volatile FxU32 *d, FxFloat s)
{
	__stwbrx(*(FxU32 *)&s,(void *)d,0);
}

void  __swizzleWriteLinear8_8(volatile FxU32 *d, FxU32 s)
{
	*d = s;
}

/* 16-bit desktop mode.  The hardware effectively does 
    byte swizzles within 16-bit words. */

/* For 32-bit acceses, we have to word swap */
FxU32 __swizzleRead32_16(volatile FxU32 *s)
{
	FxU32 temp = *s;
	return (temp >> 16) | (temp << 16);
}

/* For 16-bit acceses, the hardware will byte swap for us */
FxU16 __swizzleRead16_16(volatile FxU16 *s)
{
	return *s;
}

/* Swap words */
void  __swizzleWrite32_16(volatile FxU32 *d, FxU32 s)
{
	FxU32 temp = (s >> 16) | (s << 16);
	*d = temp;
}

/* Pass through */
void  __swizzleWrite16_16(volatile FxU16 *d, FxU16 s)
{
	*d = s;
}

/* Swap words */
void  __swizzleWriteF_16(volatile FxU32 *d, FxFloat s)
{
	FxU32 temp = *(FxU32 *)&s;
	temp = (temp >> 16) | (temp << 16);
	*d = temp;
}

/* Ewww. Swap bytes within word */
void  __swizzleWriteLinear8_16(volatile FxU32 *d, FxU32 s)
{
	FxU32 temp = *(FxU32 *)&s;
	temp = (temp >> 16) | (temp << 16);
	__stwbrx(temp,(void *)d, 0);
}

/* 32-bit desktop mode.  The hardware does full byte swapping */
/* within 32-bit longs */

/* Pass through */
FxU32 __swizzleRead32_32(volatile FxU32 *s)
{
	return *s;
}

/* Must do address swizzling, hardware will do the byte swap */
FxU16 __swizzleRead16_32(volatile FxU16 *s)
{
	FxU32 temp = (FxU32)s;
	temp ^= 2;  // Swap word address
	return *(FxU16 *)temp;
}

/* Pass through */
void  __swizzleWrite32_32(volatile FxU32 *d, FxU32 s)
{
	*d = s;
}

/* Swizzle address */
void  __swizzleWrite16_32(volatile FxU16 *d, FxU16 s)
{
	FxU32 temp = (FxU32)d;
	temp ^= 2;
	*(FxU16 *)temp = s;
}

/* Pass through */
void  __swizzleWriteF_32(volatile FxU32 *d, FxFloat s)
{
	*(FxFloat *)d = s;
}

/* Byte swap */
void  __swizzleWriteLinear8_32(volatile FxU32 *d, FxU32 s)
{
	__stwbrx(s,(void *)d, 0);
}

void setLfbSwizzleMode(FxU32 lfbBase, FxU32 regBase, FxU32 swizzleMode)
{
	__lfbBase = lfbBase;
	__regBase = regBase;
	
	switch(swizzleMode)
	{
		case 8:
		{
			__swizzleRead32 = __swizzleRead32_8;
			__swizzleRead16 = __swizzleRead16_8;
			__swizzleWrite32 = __swizzleWrite32_8;
			__swizzleWrite16 = __swizzleWrite16_8;
			__swizzleWriteF = __swizzleWriteF_8;
			__swizzleWriteLinear8 = __swizzleWriteLinear8_8;
		}
		break;
		
		case 16:
		{
			__swizzleRead32 = __swizzleRead32_16;
			__swizzleRead16 = __swizzleRead16_16;
			__swizzleWrite32 = __swizzleWrite32_16;
			__swizzleWrite16 = __swizzleWrite16_16;
			__swizzleWriteF = __swizzleWriteF_16;
			__swizzleWriteLinear8 = __swizzleWriteLinear8_16;
		}
		break;
		
		case 32:
		{
			__swizzleRead32 = __swizzleRead32_32;
			__swizzleRead16 = __swizzleRead16_32;
			__swizzleWrite32 = __swizzleWrite32_32;
			__swizzleWrite16 = __swizzleWrite16_32;
			__swizzleWriteF = __swizzleWriteF_32;
			__swizzleWriteLinear8 = __swizzleWriteLinear8_32;
		}
		break;
	}
}

extern FxU32 swizzleRead32(volatile FxU32 *s)
{
	if((__lfbBase == ((FxU32)s & 0xFE000000)))
	{	
		return __swizzleRead32(s);
	}
	return __lwbrx( (void *)s, 0 );
}

extern FxU16 swizzleRead16(volatile FxU16 *s)
{
	if((__lfbBase == ((FxU32)s & 0xFE000000)))
	{
		return __swizzleRead16(s);
	}
	return __lhbrx( (void *)s, 0 );
}

extern void swizzleWrite32(volatile FxU32 *d, FxU32 s)
{
	if((__lfbBase == ((FxU32)d & 0xFE000000)))
	{
		__swizzleWrite32(d,s);
		return;
	}
	__stwbrx( s, (void *)d, 0);
}

extern void swizzleWrite16(volatile FxU16 *d, FxU16 s)
{
	if((__lfbBase == ((FxU32)d & 0xFE000000)))
	{
		__swizzleWrite16(d,s);
		return;
	}
	__sthbrx(s, (void *)d, 0);
}

extern void swizzleWriteF(volatile FxU32 *d, FxFloat s)
{
	if((__lfbBase == ((FxU32)d & 0xFE000000)))
	{
		__swizzleWriteF(d,s);
		return;
	}
	__stwbrx(*(FxU32 *)&s, (void *)d, 0);
}

extern void  swizzleWriteLinear8(volatile FxU32 *d, FxU32 s)
{
	if((__lfbBase == ((FxU32)d & 0xFE000000)))
	{
		__swizzleWriteLinear8(d,s);
		return;
	}
	*d = s;
}