home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Libraries / VideoToolbox 94.11.17 / VideoToolboxSources / SetEntriesQuickly.c < prev    next >
Encoding:
Text File  |  1994-11-17  |  42.3 KB  |  1,102 lines  |  [TEXT/KAHL]

  1. /*
  2. SetEntriesQuickly.c
  3.  
  4. SetEntriesQuickly loads your video card’s clut as quickly as possible.
  5.  
  6. SetEntriesQuickly was written by many people, and the “final” result has not
  7. been tested on all the computers and video devices it’s meant to support. The
  8. best test is simply to run TimeVideo, which gives it a thorough workout on all
  9. of your computer’s video devices. Please send the report “TimeVideo results” to
  10. denis_pelli@isr.syr.edu, and I’ll add your test results to “Video bugs” and the
  11. following list.
  12.  
  13. VIDEO DEVICES CERTIFIED by TimeVideo as compatible with SetEntriesQuickly():
  14. 1. Ok:
  15. “Macintosh Display Card 8•24 GC” (.Display_Video_Apple_MDCGC)
  16. Quadra 900 “Macintosh C Built-In Video” (only tested in 1,2,4,8-bit modes)
  17. Quadra 950 “Macintosh G Built-In Video” (only tested in 1,2,4,8,16-bit modes)
  18.  
  19. 2. Ok when loading whole clut at once, but fail read-back test when loading one entry at a time (at least on the Quadra 840av). Bug reported by Ken Alexander <U12940@UICVM.BITNET> and Wei Xie. This problem does not occur on most Macs.
  20. Apple 8•24 "Macintosh Display Card" (.Display_Video_Apple_MDC version 272)
  21.  
  22. 3. Ok, except for visible hash (dynamic black specks) during clut loading:
  23. Apple “Mac II High-Resolution Video Card” (.Display_Video_Apple_HRVC)
  24. Apple “Toby frame buffer card” (.Display_Video_Apple_TFB version 5)
  25. Mac IIci “Macintosh II Built-In Video” (.Display_Video_Apple_RBV1)
  26.  
  27. 4. Don't work (hopefully will be fixed soon):
  28. RasterOps “ProColor 32” (.RasterOps 1.0 32XL Video Driver version 9327)
  29.  
  30. CONTRIBUTORS TO THIS LIST
  31. Ken Alexander <U12940@UICVM.BITNET> (bug in SetEntriesQuickly)
  32. Kyle Cave, cavekr@ctrvax.vanderbilt.edu (Quadra 700)
  33. Wei Xie (bug in SetEntriesQuickly for Apple 8•24 in Quadra 840AV)
  34.  
  35. Some Macintosh video drivers are poorly written; they take too long (more than a
  36. frame time) to load the clut. This is makes it impossible to do clut animation
  37. for temporal modulation etc., for which one needs to be able to reload the clut
  38. on each frame. At one time many of us thought that the limitation was in
  39. hardware, in the RAMDAC, but we were wrong. Raynald Comptois disassembled
  40. several video drivers and wrote his own programs to load the clut quickly, and
  41. his programs manage to do it within a frame. Raynald was kind enough to share
  42. his code with me. I passed it on to Peter Lennie and Bill Haake, who polished
  43. it, making it compatible with the 68040 processor, and added support for more
  44. cards. I polished their work, made the routines self contained, adding a
  45. “device” argument to allow use in Macs that have more than one video device, and
  46. quickly figuring out all the key parameters (mode, pixelSize, DAC size, and clut
  47. size). There are now two alternative front ends: SetEntriesQuickly() for new
  48. users, and macltset() for backward compatibility with programs that used
  49. Raynald’s original routines. This modularity has increased runtime only
  50. slightly, a fraction of a millisecond.
  51.  
  52. New drivers are hard to write, since they must directly address the registers of
  53. the video card, which are unique to each video card and undocumented. So the
  54. author of a driver must disassemble the original video driver and figure things
  55. out on his or her own. A lot of work.
  56.  
  57. SetEntriesQuickly is unlike the standard video drivers in the following ways:
  58. 1. SetEntriesQuickly always takes less than 2 ms to load the whole clut. 
  59. Some video drivers, e.g. for Apple’s 8•24 card, take several frames (>30 ms) to 
  60. finish loading the clut.
  61. 2A. SetEntriesQuickly does not wait for VBL, so a visible glitch may appear on
  62. the current frame, at least on some older video cards (e.g. Hi-Res, Toby, and
  63. Mac IIci). (You can prevent this glitch by calling SetEntriesQuickly only at
  64. blanking time. Use a VBL task or WaitForBlanking().) Newer cards and computers
  65. seem to have dual ported video clut memory so you can write at any point in the
  66. frame cycle without noticeable glitch, though of course you may want to synch
  67. the update for other reasons.
  68. 2B. A flag argument “waitForNextBlanking” is provided, but at present this
  69. option is only supported for the Toby video card.
  70. 3. SetEntriesQuickly ignores the gamma table, yielding the same result as using
  71. the standard video driver with an uncorrected gamma table. E.g. after calling
  72. GDUncorrectedGamma(device).
  73. 4. SetEntriesQuickly() does nothing and returns an error if the arguments
  74. specify loading of an out-of-range index. 
  75. 5. SetEntriesQuickly() does nothing and returns an error if the “count”
  76. specifies zero entries. This is contrary to the (bizarre) Apple convention that
  77. a setEntries control call with a count corresponding to zero entries will result
  78. in loading of entries specified by the “value” field of the ColorSpec.
  79. 6. SetEntriesQuickly does not have immediate access to the video driver’s
  80. private tables. Therefore the first time you call SetEntriesQuickly() for a 
  81. particular device there is an extra delay of about 1 ms while some key
  82. information is ferreted out. That information is cached, so subsequent calls
  83. for the same device will be fast, spending most of their time loading the clut.
  84.  
  85. Two front ends are provided, for compatibility with two distinct traditions:
  86.  
  87. OSErr SetEntriesQuickly(GDHandle device,short start,short count,ColorSpec *table);
  88.  
  89. SetEntriesQuickly() uses the same calling convention as the VideoToolbox routine
  90. GDSetEntries() and, except for adding the GDHandle argument to specify the
  91. device, is also the same as Apple’s SetEntries() Color Manager routine,
  92. documented in Inside Macintosh V-143. Apple specifies special behavior when
  93. count==-1, but we don’t support that here and simply return with an error. I
  94. suggest that new users use SetEntriesQuickly rather that macltset. “start” is
  95. the index of the first clut entry to load, and should be greater than or equal
  96. to zero. “count” is the number of entries to load, minus 1. (Yes, “minus 1”,
  97. that’s Apple’s confusing convention.) “table” is a Colorspec array. (Each ColorSpec
  98. element is a structure consisting of a two-byte “value”, which is not used, and
  99. a 6-byte “rgb”, which, in turn is a structure of three 16-bit unsigned short
  100. ints: red, green, and blue. Apple’s convention is that the MOST SIGNIFICANT BITS
  101. of the 16-bit color values are used. It’s good practice in your programs to
  102. provide full 16-bit values, so that when you upgrade to fancier video cards with
  103. more-than-eight bit DACs your programs will benefit from the extra precision
  104. without needing any change. Returns zero if successful, nonzero if unsuccessful,
  105. i.e. illegal arguments.
  106.  
  107. short macltset(GDHandle device,short start
  108.     ,unsigned short* red,unsigned short* green,unsigned short* blue,short count1);
  109.  
  110. macltset() uses a calling convention established by Raynald Comtois, and
  111. provides backward compatibility with older programs. “red”, “green”, and “blue”
  112. are arrays of 16-bit unsigned short ints, of which the LEAST SIGNIFICANT BITS
  113. are used. “start” is the index of the first entry to change. “count1” is the
  114. number of entries to change (contrary to Apple’s convention).
  115.  
  116. Both front ends use the same general-purpose subroutine: LoadClut(), which in
  117. turn calls the hardware-specific routine appropriate to the particular video 
  118. device being used.
  119.  
  120. The useMostSignificantBits bit of the “flags” argument specifies whether to use 
  121. Apple’s convention (for users of SetEntriesQuickly) or Raynald’s convention 
  122. (for users of macltset).
  123.  
  124. At the moment all the supported video cards have 8-bit DACs, except the RasterOps
  125. ProColor 32, which has 9-bit DACs. If the useMostSignificantBits flag is true
  126. then you don’t need to worry, as the least significant bit of the 9-bit DAC
  127. simply picks up the next lower bit from your numbers, giving you a tad more
  128. precision. However, if useMostSignificantBits flag is false then, in order to
  129. use the full range of the DAC you must make all your numbers twice as big, or --
  130. cludge time! -- set the useOnly8Bits flag, to request that your 8-bit numbers be
  131. multiplied by two, allowing you to use the whole range of the DAC without
  132. changing the rest of your program, but wasting the DAC’s least significant bit
  133. by setting it permanently to zero.
  134.  
  135. SUSPENDING INTERRUPTS. If you wish, the low-level routines will suspend
  136. interrupts while loading the clut. Presumably Raynald had his reasons for
  137. implementing this, so this “feature” is enabled when you use macltset(). Peter
  138. Lennie writes, “The switch to uninterruptable processing during the write is, I
  139. think, out of the original drivers (though I’m not absolutely sure).  I imagine
  140. it’s to avoid display glitches that would result from some higher priority
  141. interrupt suspending a clut rewrite somewhere in the middle.” However, I (dgp)
  142. don’t see any advantage to suspending interrupts, and believe that there is a
  143. significant downside if you are trying to keep track of the VBL interrupts on
  144. several video cards, since suspending interrupts for 1 or 2 ms might be long
  145. enough to miss a whole VBL interval. Thus SetEntriesQuickly disables this
  146. “feature”. However, this is not a philosophical debate. We all agree that
  147. interrupts should be suspended if doing otherwise would occasionally result in a
  148. visible glitch. Does anyone know?
  149.  
  150. OSErr WaitForNextBlanking(GDHandle device);
  151.  
  152. Waits for beginning of next blanking interval. Currently this supports only the
  153. Toby and HiRes cards (Apple’s original video cards, now obsolete).
  154.  
  155. SPEED. SetEntriesQuickly() is self contained. You simply give it the GDHandle of
  156. your video device (as returned, e.g. by GetScreenDevice), and tell it what you
  157. want to do to the clut. In order to do this for you it needs to figure out a
  158. bunch of stuff about your video device. This research takes time; the first time
  159. you call it for a particular device it takes on the order of 1 ms to look up
  160. stuff. However, it saves this info in a cache, for each device, for quick
  161. retrieval on subsequent occasions. The implication is that programs that use
  162. SetEntriesQuickly ought to call it once just for practice (to get the cache
  163. loading over with) before using it in a situation where speed matters.
  164.  
  165. The coding of the LoadClut “driver” routines is a compromise between the needs
  166. of SetEntriesQuickly and macltset, which both use them. I decided not to write
  167. separate clut loading loops for the two cases (use most- vs. least-significant
  168. bits). I believe (but have not tested) that adding a register offset instead of
  169. using an autoincrement instruction incurs essentially no time penalty because
  170. the processor automatically overlaps the execution of such instructions. So I
  171. think that SetEntriesQuickly is running flat out, and don’t see any prospect of
  172. speeding it up significantly. On the other hand, I suspect that fetching the
  173. least significant byte by doing a byte access to an odd address (for macltset)
  174. does slow things down perhaps 30% (though I haven’t timed it) over doing a word
  175. access to an even address, as Raynald had originally coded it. If that speed
  176. loss is unacceptable, then one could insert an if(flags&useMostSignificantBits)
  177. statement into the relevant subroutine and write two separate loops optimized
  178. for the two cases. My guess is that the current compromise will be acceptable to
  179. all users.
  180.  
  181. A NOTE ON SPEED FROM DAVID BRAINARD:
  182. We have been looking pretty closely at video timing.  In our hands,
  183. SetEntriesQuickly does not always succeed in writing the CLUT during the
  184. vertical blank interval.  In particular, with the Apple 8-24 board on a IIfx
  185. running in the 800 by 600 mode at 75 Hz, it is a little too slow.  This causes a
  186. visible glitch at the top of the screen.  Whether it is fast enough is probably
  187. very hardware-configuration dependent, but it might be worth emphasizing in the
  188. documentation that use is caveat emptor.  You give < 2 ms as the write time,
  189. which is roughly correct, but not always fast enough. I may end up writing an
  190. assembler version to see if I can push it by 25%, which is about what I need.
  191. [However, assembly code won't be compatible with the Power PC.-dgp]
  192.  
  193. IMPROVEMENTS:
  194. It is hoped that others will add to the functionality of this routine. Please
  195. share your enhancements with others by sending them to denis_pelli@isr.syr.edu
  196. for inclusion in the VideoToolbox.
  197.  
  198. Those wishing to support new video devices should begin by buying and reading
  199. Apple’s Designing Cards and Drivers, 3rd Ed., Addison-Wesley, and then use the
  200. VideoToolbox utility GetVideoDrivers to copy all your drivers into resource
  201. files, and use ResEdit with CODE editor to peruse them. The ResEdit CODE editor
  202. is a public domain file distributed by:
  203. Ira L. Ruben
  204. Apple Computer, Inc.
  205. 20525 Mariani Ave., MS: 37-A
  206. Cupertino, Ca. 95014
  207. Ira@Apple.Com
  208. ftp://ftp.apple.com/dts/mac/tools/resedit/resedit-2-1-code-editor...
  209.  
  210. By the way, assembly code is hard to write, read, and maintain,
  211. and the speed advantage is negligible, about 10%. I suggest that
  212. all new code be written in C.
  213.  
  214. It is logical that we identify the video card by the card name,
  215. GDCardName(driver), but in fact getting the card name is very slow (1.5 ms)
  216. whereas getting the driver name is fast, GDName(driver), and would be
  217. sufficiently unique for our purposes. (E.g. the Toby and TFB video cards have
  218. the same driver, and our code works for both cards.) 
  219.  
  220. KNOWN BUGS:
  221. Has not been tested on all the video devices that are supposed to be supported.
  222. Please run the demo TimeVideo, and send the results file to denis_pelli@isr.syr.edu
  223.  
  224. Does not work with the RasterOps ProColor 32. Hopefully this will be fixed soon.
  225.  
  226. The Quadra code requires that start==0. (Apparently the same problem occurs when
  227. running the 8•24 card on a Quadra 840AV.) This could probably be figured out and
  228. fixed pretty easily if someone took the time to do so.
  229.  
  230. I recommend using the standard drivers (i.e. GDSetEntries/GDDirectSetEntries)
  231. instead of SetEntriesQuickly for the Toby and High Resolution video cards and
  232. the Mac IIci built-in video. Those standard drivers work fine, whereas for those
  233. devices SetEntriesQuickly produces visible dynamic black specks as it accesses
  234. the clut.
  235.  
  236. None of these routines wait for the vertical blanking interval before loading
  237. the clut. On older video devices--Toby, HiRes, Mac IIci--this results in visible
  238. dynamic black specks on the screen. I (dgp) consider this a bug, but, for most
  239. of these devices I don’t know how to wait for the end of frame, short of setting
  240. up an interrupt. (Just about every video card has a bit that one could monitor,
  241. but its address is usually undocumented.) Newer devices seem to be ok, because of
  242. dual-ported RAMDAC memory. Check this out on your devices by running TimeVideo.
  243.  
  244. HISTORY:
  245. 8/24/92 Original setcardbase and macltset provided by Raynald Comtois
  246. (raco@wjh12.harvard.edu) to Denis Pelli.
  247.  
  248. 10/2/92 Bill Haake added code for the RasterOps ProColor 32, which has 9-bit
  249. DACs and 9-bit entries in the lookup table.
  250.  
  251. 10/1/92 Peter Lennie added code for Quadra internal video.  No provision for
  252. changing the start position in the table, (I couldn't find any relevant
  253. disassembly) so 'start' is ignored, and you should write the whole table.
  254.     
  255. 9/30/92 Bill Haake & Peter Lennie modified the code for the 8x24 card
  256. and the 8x24GC to make it a) work properly in 32-bit mode. b) to fix a bug
  257. (feature?) of the original drivers that prevented the cards running on a Quadra.
  258. The drivers exploit 'byte-smearing' on the 68020 and 68030 (Tech Note 282). This
  259. means that one can move a byte to the lowest byte address of the data register
  260. on the card, when one actually wants to put it at address+3 (!!). The functions
  261. work for all the cards (except toby, which hasn't been tested) and on internal
  262. video in both 24 and 32 bit mode on Quadra 700/950, IIfx or ci running system
  263. 7.0.1.
  264.     
  265. 9/28/92 Peter Lennie added the function findcard.
  266.  
  267. 11/23/92 Denis Pelli (dgp) eliminated all globals because they implicitly
  268. assumed that there is only one video device. All routines now accept a GDHandle
  269. specifying which video device. Simplified the logic of GetCardBase(), minimizing
  270. the dependence on card type.
  271.  
  272. 11/25/92 dgp When USE_MSB is true, all the routines now use the most significant
  273. bits of the 16-bit elements of the user-supplied color tables. When it is false
  274. the least significant bits are used. This is mostly implemented by offseting the
  275. table pointers by one byte and only reading the desired byte. •Generalized
  276. macltset() to work with tables that have an arbitrary element spacing. This
  277. allows it to work with both with Raynald's convention of three arrays of shorts,
  278. and the Apple convention of a ColorSpec array, each element of which consists of
  279. red, green, blue, and value (which is not used). •Added alias "Toby frame buffer
  280. card" for tobycard.
  281.  
  282. 11/27/92 dgp Broke out the code for each card into separate subroutines. This
  283. allows optimal register assignment for each routine, and makes it much easier to read
  284. the THINK C disassembler output. The runtime overhead of loading and unloading
  285. the stack is negligible, and could be eliminated entirely by putting all the
  286. parameters in a structure and passing a pointer to it. •Added a flag,
  287. suspendInterrupts, to make interrupt suspension optional since it may be
  288. undesirable in some applications. (Blocking interrupts for 1 ms could cause you
  289. to miss the interrupt from a video card, especially if you are trying to keep
  290. track of interrupts on several video cards at once.)
  291.  
  292. 11/30/92 dgp Wrote TestCluts, which reads back the clut and checks all
  293. values, and used it to test SetEntriesQuickly() on Quadra 950 internal video,
  294. Mac IIci internal video, hirescard, "Toby frame buffer card", and 8•24 card at
  295. all depths, for both 24- and 32-bit addressing. Toby card was tested on 68020,
  296. 68030, and 68040 processors. •Wrote documentation. •Replaced compile-time constants
  297. USE_MSB and PRO_8BITS by runtime flags passed as arguments. •Added WaitForNextBlanking()
  298. based on code from VideoTFB.c.
  299.  
  300. 12/3/92 dgp Incorporated Peter Lennie's corrections and additions to the comments above.
  301.  
  302. 12/8/92 dgp Added missing "case" to switch in WaitForNextBlanking.
  303.  
  304. 12/13/92 dgp Changed erroneous "&d" to "%d" in a printf. Added some comments to
  305. the documentation above.
  306.             
  307. 12/15/92 dgp Now get mode from device record and leave it in standard form, 
  308. i.e. with the 0x80 bit set, and only strip off that bit when actually necessary,
  309. e.g. in LoadClutMacIIci. 
  310.  
  311. 12/30/92 dgp Make sure routines return zero when there's no error.
  312.  
  313. 2/15/93    dgp Rewrote nonworking LoadClutToby in C, and made it work. Rewrote
  314. nonworking LoadClutx824 in C, and made it work. Fixed sixteenBitMode in
  315. LoadClutQuadra. Use new SwapPriority instead of Get/SetPriority.
  316.  
  317. 2/20/93    dgp    Translated LoadClutGCx824 to C. (It was ignoring the start value.)
  318.  
  319. 3/4/93    dgp    Added macIIsi to list of supported cards, since it uses the same
  320. driver as the Mac IIci. Changed definitions of string types slightly to allow
  321. compilation of this file as a code resource. However, the assembly code
  322. uses more registers than are available to a code resource.
  323.  
  324. 4/13/93    dgp    Removed 68020 requirement by translating an indexed add in LoadMacIIci to
  325. C.
  326.  
  327. 4/17/93 dgp Added support in GetCardBase for old Mac II computers whose ROM's only 
  328. support 24-bit NuBus addressing. 
  329.  
  330. 5/18/93 SetEntriesQuickly now respects the setting of the device's gray/color mode, 
  331. and maps to gray if in the device is in gray mode and pixelSize<=8. Changed prototype
  332. of macltset to specify the red, green, and blue arrays as "unsigned short" instead of 
  333. "short".
  334.  
  335. 7/7/93    dgp    Disabled some global optimizations because THINK C 6 will
  336. crash while compiling if the Radius PowerView is present: "!gopt_induction,!gopt_loop".
  337.  
  338. 7/9/93    dgp check for 32-bit addressing capability.
  339.  
  340. 6/14/94    dgp    can32 is now computed by calling TrapAvailable(_SwapMMUMode), which 
  341. returns correct answer even on Macs with dirty ROMs.
  342.  
  343. 6/14/94    dgp    Added call to SwapMMUMode in LoadClutToby, because it seemed odd not to.
  344. LoadClutMacIIci still doesn't, because the video buffer is in memory.
  345.  
  346. 6/30/94 dgp Updated the documentation.
  347.  
  348. 7/27/94 dgp Moved the list of compatible devices from the "Video synch" document
  349. to here, at the top of the file.
  350.  
  351. 7/29/94 dgp Made use of the asm directive conditional on THINK_C, for compatibility
  352. with other compilers. Changed LoadClutx824GC() to use its C instead of its asm code. 
  353. However, I don't know if the C code has been tested. GetCardType() returns nonzero
  354. only if the card is supported by the compiled code (i.e. ProColor, Quadra, and Mac
  355. IIci and IIsi are recognized only if this file is compiled by THINK C.)
  356. */
  357. #include "VideoToolbox.h"
  358. #include <assert.h>
  359. #define _SwapMMUMode 0xA05D
  360. #define USE_ONLY_8_BITS_IN_MACLTSET 0    // 1 to use RasterOps ProColor32 as an 8-bit DAC.
  361. #if __MWERKS__            // Metrowerks CodeWarrior C compiler
  362.     #pragma extended_errorcheck off
  363. #endif
  364. #if (THINK_C || THINK_CPLUS)
  365.     #pragma options(assign_registers,honor_register,redundant_loads,defer_adjust)
  366.     #pragma options(global_optimizer,!gopt_induction,!gopt_loop,gopt_cse,gopt_coloring)
  367. #endif
  368.  
  369. // These are the five user-callable routines:
  370. OSErr WaitForNextBlanking(GDHandle device);
  371. OSErr SetEntriesQuickly(GDHandle device,short start,short count,ColorSpec *table);
  372. short macltset(GDHandle device,short start
  373.     ,unsigned short* red,unsigned short* green,unsigned short* blue,short count1);
  374. short GetCardType(GDHandle device);
  375. char *GetCardBase(GDHandle device);
  376.  
  377. /*
  378. I suggest keeping the following information private to this file. In principle
  379. you could publish these card types and use them in your programs. However, in
  380. practice, I cannot see any point in doing so. If you need to identify the card
  381. name I suggest you simply use the string returned by GDCardName(device) in
  382. GDVideo.c of the VideoToolbox. (Don't forget to call DisposPtr() when you're
  383. through with the string.) Or use GDName(device), which returns the name of the
  384. card's driver, and is much quicker. If you simply want to know whether your
  385. video card is supported by SetEntriesQuickly.c then you can simply make sure
  386. that GetCardType() returns a nonzero cardType.
  387. */
  388. struct vtype {        /* associates card name and id */
  389.     char name[40];
  390.     short id;
  391. };
  392. enum {                /* card identifiers */
  393.     tobycard = 1,
  394.     hirescard,
  395.     macIIci,
  396.     macIIsi,
  397.     x824card,
  398.     x824GCcard,
  399.     quadra700,
  400.     quadra900,
  401.     quadra950,
  402.     procolor32
  403. };
  404. static struct vtype card[] = {    // card name & id        // Original author:
  405.     {"Toby frame buffer card",                tobycard},    // Raynald Comtois
  406.     {"Display_Video_Apple_TFB",                tobycard},    //     "      "
  407.     {"Mac II High-Resolution Video Card",    hirescard},    // Raynald Comtois
  408.     {"Macintosh Display Card",                x824card},    // Raynald Comtois
  409.     {"Macintosh Display Card 8•24 GC",        x824GCcard},// Raynald Comtois
  410.     #if THINK_C    // the asm directive is supported only by THINK C
  411.         {"Macintosh II Built-In Video",            macIIci},    // Raynald Comtois
  412.         {"Macintosh A Built-In Video",            macIIsi},    //     "      "
  413.         {"Macintosh E Built-In Video",            quadra700},    // Peter Lennie
  414.         {"Macintosh C Built-In Video",            quadra900},    //     "      "
  415.         {"Macintosh G Built-In Video",            quadra950},    //     "      "
  416.         {"ProColor 32",                            procolor32}    // Bill Haake
  417.     #endif
  418. };
  419. static char driverName[][40]=        // Not used at present.
  420. {
  421.     "\p.Display_Video_Apple_TFB"    // Apple “Toby frame buffer card”
  422.     ,"\p.Display_Video_Apple_HRVC"    // Apple “Mac II High-Resolution Video Card”
  423.     ,"\p.Display_Video_Apple_MDC"    // Apple 8•24 “Macintosh Display Card”
  424.     ,"\p.Display_Video_Apple_MDCGC"    // Apple 8•24GC
  425.     ,"\p.Display_Video_Apple_RBV1"    // Mac IIci and IIsi built-in video
  426.     ,"\p.Display_Video_Apple_DAFB"    // Quadra 700, 900, 950 built-in video
  427.     ,"\p.RasterOps 1.0 32XL Video Driver"    // Radius ProColor 32
  428. };
  429. enum {                            // Flags passed to LoadClut().
  430.     suspendInterrupts=1,
  431.     useMostSignificantBits=2,
  432.     useOnly8Bits=4,
  433.     waitForNextBlanking=8
  434. };
  435. enum{quadraNonzeroStart=111};    // value returned as error.
  436.     
  437. short LoadClut(GDHandle device,short start,short count
  438.     ,unsigned short* red,unsigned short* green,unsigned short* blue,long elementSpacing,short flags);
  439. OSErr LoadClutProColor(short start,short count,char *r,char *g,char *b
  440.     ,long elementSpacing,short mode,short pixelSize,short clutSize
  441.     ,char *cardBase,short flags);
  442. OSErr LoadClutQuadra(short start,short count,char *r,char *g,char *b
  443.     ,long elementSpacing,short mode,short pixelSize,short clutSize
  444.     ,char *cardBase,short flags);
  445. OSErr LoadClutMacIIci(short start,short count,char *r,char *g,char *b
  446.     ,long elementSpacing,short mode,short pixelSize,short clutSize
  447.     ,char *cardBase,short flags);
  448. OSErr LoadClutHiRes(short start,short count,char *r,char *g,char *b
  449.     ,long elementSpacing,short mode,short pixelSize,short clutSize
  450.     ,char *cardBase,short flags);
  451. OSErr LoadClutx824(short start,short count,char *r,char *g,char *b
  452.     ,long elementSpacing,short mode,short pixelSize,short clutSize
  453.     ,char *cardBase,short flags);
  454. OSErr LoadClutx824GC(short start,short count,char *r,char *g,char *b
  455.     ,long elementSpacing,short mode,short pixelSize,short clutSize
  456.     ,char *cardBase,short flags);
  457. OSErr LoadClutToby(short start,short count,char *r,char *g,char *b
  458.     ,long elementSpacing,short mode,short pixelSize,short clutSize
  459.     ,char *cardBase,short flags);
  460. /******************************************************************************/
  461. /*
  462. The arguments start, count, and table are the same as for the Color Manager call
  463. SetEntries(), documented in Inside Macintosh V-143. (Except that a count==-1 is
  464. considered illegal here.) Apple's ideosyncratic convention is that "count" is
  465. "zero-based", meaning that it is one less than the number of clut entries that
  466. you want to modify. "count" must be at least zero. Returns zero if successful,
  467. nonzero if unsuccessful, i.e. illegal arguments.
  468. */
  469. OSErr SetEntriesQuickly(GDHandle device,short start,short count,ColorSpec *table)
  470. {
  471.     short flags=useMostSignificantBits;
  472.     //flags+=suspendInterrupts;        // Optional, no
  473.     //flags+=waitForNextBlanking;    // Optional, no
  474.  
  475.     return LoadClut(device,start,count
  476.         ,&table[0].rgb.red,&table[0].rgb.green
  477.         ,&table[0].rgb.blue,sizeof(table[0]),flags);
  478. }
  479. /******************************************************************************/
  480. short macltset(GDHandle device,register short start
  481.     ,unsigned short* red,unsigned short* green,unsigned short* blue,short count1)
  482. {
  483.     short flags=0;
  484.     flags+=suspendInterrupts;        // Optional
  485.     #if USE_ONLY_8_BITS_IN_MACLTSET
  486.         flags+=useOnly8Bits;        // Optional
  487.     #endif
  488.     //flags+=waitForNextBlanking;    // Optional
  489.     
  490.     return LoadClut(device,start,count1-1,red,green,blue,sizeof(red[0]),flags);
  491. }
  492. /******************************************************************************/
  493. /*
  494. The first call to GetCardType for a particular device takes 1.5-3 ms, depending
  495. on your computer's speed, because it takes Apple's Slot Manager a long time to
  496. get the card name. However, GetCardType's answers are cached so subsequent calls
  497. for a previously queried device will be fast <100 µs.
  498. */
  499. short GetCardType(GDHandle device)    // returns card type, if known, or zero if not.
  500. {
  501.     register short i;
  502.     short cardType;
  503.     char *name;
  504.     static GDHandle deviceCache[MAX_SCREENS];
  505.     static short typeCache[MAX_SCREENS];
  506.     
  507.     // Do we already know the answer? Check the cache.
  508.     for(i=0;i<MAX_SCREENS;i++)if(device==deviceCache[i])return typeCache[i];
  509.     // Get card name, see if it's in our list of known cards
  510.     name=GDCardName(device);
  511.     cardType=0;
  512.     for (i=0; i<sizeof(card)/sizeof(card[0]); i++){
  513.         if(strcmp(name,card[i].name)==0){
  514.             cardType=card[i].id;
  515.             break;
  516.         }
  517.     }
  518.     DisposePtr(name);
  519.     // Save answer in cache.
  520.     for(i=0;i<MAX_SCREENS;i++)if(deviceCache[i]==0){
  521.         typeCache[i]=cardType;
  522.         deviceCache[i]=device;
  523.         break;
  524.     }
  525.     return cardType;
  526. }
  527. /******************************************************************************/
  528. long internalVideoBase:0xDD8;    // Undocumented System global
  529.  
  530. char *GetCardBase(GDHandle device)
  531. {
  532.     long cardBase,slot;    /* slot must be declared long */
  533.     short cardType;
  534.     
  535.     slot=GetDeviceSlot(device);
  536.     if(slot==0){
  537.         // Built-in video, not in a NuBus slot.
  538.         // E.g.: macIIci,macIIsi,quadra700,quadra900,quadra950
  539.         #if 1
  540.             // This C is equivalent to Raynald's assembly code below.
  541.             cardBase = *(long *)(internalVideoBase + *(long *)internalVideoBase + 56);
  542.         #else
  543.             asm {
  544.                 move.l 0xDD8,a0        /* get card base address */
  545.                 adda.l (a0),a0
  546.                 move.l 56(a0),a1
  547.                 move.l a1,cardBase
  548.             }
  549.         #endif
  550.     }else{
  551.         // Video card in NuBus slot
  552.         cardType=GetCardType(device);
  553.         switch(cardType){
  554.             case x824GCcard:
  555.                 cardBase = slot<<28;    // a superslot
  556.                 break;
  557.             case tobycard:
  558.             case hirescard:
  559.                 cardBase = (slot<<24) | 0xF0000000;
  560.                 cardBase+= (slot<<20);    // Support old Mac II 24-bit NuBus addressing
  561.                 break;
  562.             case procolor32:             // RasterOps
  563.             case x824card:
  564.             default:
  565.                 cardBase = (slot<<24) | 0xF0000000;
  566.                 break;
  567.         }
  568.     }
  569.     return (char *)cardBase;
  570. }
  571. /******************************************************************************/
  572. short LoadClut(GDHandle device,short start,short count
  573.     ,unsigned short* red,unsigned short* green,unsigned short* blue
  574.     ,long elementSpacing,short flags)
  575. {
  576.     char *cardBase;
  577.     short cardType=0,pixelSize,mode;
  578.     short clutSize;     // entries in the lookup table
  579.     int error,i,j;
  580.     short isGray;
  581.     unsigned short grayTable[256];
  582.     
  583.     if(device==NULL)return 1;
  584.     cardType=GetCardType(device);    // takes 1.7 ms the first time for each device.
  585.     if(cardType==0)return 1;
  586.     cardBase=GetCardBase(device);
  587.     clutSize=GDClutSize(device);
  588.     pixelSize=(**(**device).gdPMap).pixelSize;
  589.     mode=(**device).gdMode;
  590.     
  591.     // Check range.
  592.     if(start>clutSize-1 || start<0 || count+start>clutSize-1 || count<0)return 1;
  593.     
  594.     // We're going to use these RAM addresses in 32-bit mode.
  595.     red = (unsigned short *)StripAddress(red);
  596.     green = (unsigned short *)StripAddress(green);
  597.     blue = (unsigned short *)StripAddress(blue);
  598.  
  599.     if(waitForNextBlanking & flags){
  600.         WaitForNextBlanking(device);
  601.     }
  602.     
  603.     isGray=!TestDeviceAttribute(device,gdDevType);
  604.     if(isGray && pixelSize<=8){
  605.         j=elementSpacing/sizeof(*red);
  606.         for(i=0;i<=count;i++){
  607.             grayTable[i]=*red*0.30+*green*0.59+*blue*0.11;
  608.             red+=j;
  609.             green+=j;
  610.             blue+=j;
  611.         }
  612.         elementSpacing=sizeof(grayTable[0]);
  613.         red=green=blue=grayTable;
  614.     }
  615.     
  616.     // After the above setting up, actually loading 256x3 clut entries takes <2 ms.
  617.     switch (cardType) {
  618.         // I packaged the code for each case into a separate subroutine
  619.         // in order to allow the THINK C compiler to optimize each
  620.         // one independently. An important consideration is that the THINK C 5.04
  621.         // compiler disables most optimizations for any function that includes
  622.         // the "asm" directive anywhere within the function. Thus mixing C and assembly
  623.         // will result in inefficient C. No less important, the THINK C Disassemble
  624.         // command is very handy in writing fast C code, but produces an uncommented 
  625.         // listing, which is much easier to read if the separate routines are each 
  626.         // named subroutines.
  627.         case procolor32:
  628.             error=LoadClutProColor(start,count,(char *)red,(char *)green,(char *)blue,
  629.                 elementSpacing,mode,pixelSize,clutSize,cardBase,flags);
  630.             break;
  631.         case quadra700:
  632.         case quadra900:
  633.         case quadra950:
  634.             error=LoadClutQuadra(start,count,(char *)red,(char *)green,(char *)blue,
  635.                 elementSpacing,mode,pixelSize,clutSize,cardBase,flags);
  636.             break;
  637.         case macIIci:
  638.         case macIIsi:
  639.             error=LoadClutMacIIci(start,count,(char *)red,(char *)green,(char *)blue,
  640.                 elementSpacing,mode,pixelSize,clutSize,cardBase,flags);
  641.             break;
  642.         case hirescard:
  643.             error=LoadClutHiRes(start,count,(char *)red,(char *)green,(char *)blue,
  644.                 elementSpacing,mode,pixelSize,clutSize,cardBase,flags);
  645.             break;
  646.         case x824card:
  647.             error=LoadClutx824(start,count,(char *)red,(char *)green,(char *)blue,
  648.                 elementSpacing,mode,pixelSize,clutSize,cardBase,flags);
  649.             break;
  650.         case x824GCcard:
  651.             error=LoadClutx824GC(start,count,(char *)red,(char *)green,(char *)blue,
  652.                 elementSpacing,mode,pixelSize,clutSize,cardBase,flags);
  653.             break;
  654.         case tobycard:
  655.             error=LoadClutToby(start,count,(char *)red,(char *)green,(char *)blue,
  656.                 elementSpacing,mode,pixelSize,clutSize,cardBase,flags);
  657.             break;
  658.     }
  659.     return error;
  660. }
  661. /******************************************************************************/
  662. OSErr LoadClutProColor(short start,register short count
  663.     ,register char *red,register char *green,register char *blue
  664.     ,register long elementSpacing
  665.     ,short mode,short pixelSize,short clutSize,char *cardBase,short flags)
  666. {
  667. #if THINK_C
  668.     char mmuMode=true32b,priority=7;
  669.     register long bitShift;
  670.     static Boolean can32,firstTime=1;
  671.  
  672.     if(firstTime){
  673.         can32=TrapAvailable(_SwapMMUMode);
  674.         firstTime=0;
  675.     }
  676.     if(useMostSignificantBits & flags){
  677.         bitShift=9;
  678.     }else{
  679.         if(useOnly8Bits & flags) bitShift=1;
  680.         else bitShift=0;
  681.     }
  682.     if(suspendInterrupts & flags)SwapPriority(&priority);
  683.     if(can32)SwapMMUMode(&mmuMode);
  684.     asm {
  685.         move.l cardBase,a1        /* get card base address */
  686.         adda.l    #0xf60000,a1    /* offset to control registers */        
  687.     @9    move.w    start,2(a1)        /* Set the index on the card */
  688.         move.w (red),d1
  689.         add.l elementSpacing,red
  690.         lsl.w bitShift,d1
  691.         move.w d1,14(a1)
  692.         move.w (green),d1
  693.         add.l elementSpacing,green
  694.         lsl.w bitShift,d1
  695.         move.w d1,14(a1)
  696.         move.w (blue),d1
  697.         add.l elementSpacing,blue
  698.         lsl.w bitShift,d1
  699.         move.w d1,14(a1)
  700.         addq.w    #1,start        /* Point to next entry in table */
  701.         dbf count,@9
  702.     }
  703.     if(can32)SwapMMUMode(&mmuMode);
  704.     if(suspendInterrupts & flags)SwapPriority(&priority);
  705.     return 0;
  706. #else
  707.     start;count;red;green;blue;elementSpacing;mode;pixelSize;clutSize;cardBase;flags;    // prevent "unused argument" warning
  708.     return 1;
  709. #endif
  710. }
  711. OSErr LoadClutQuadra(short start,register short count
  712.     ,register char *red,register char *green,register char *blue
  713.     ,register long elementSpacing
  714.     ,short mode,short pixelSize,short clutSize,char *cardBase,short flags)
  715. {
  716. #if THINK_C
  717.     char mmuMode=true32b,priority=7;
  718.     static Boolean can32,firstTime=1;
  719.  
  720.     if(firstTime){
  721.         can32=TrapAvailable(_SwapMMUMode);
  722.         firstTime=0;
  723.     }
  724.     if(start!=0){
  725.         //printf("LoadClutQuadra: start must be zero\n");
  726.         return quadraNonzeroStart;
  727.     }
  728.     if(!(useMostSignificantBits & flags)){
  729.         // Point to less significant byte of word.
  730.         red++;
  731.         green++;
  732.         blue++;
  733.     }
  734.     if(suspendInterrupts & flags)SwapPriority(&priority);
  735.     if(can32)SwapMMUMode(&mmuMode);
  736.     if(mode!=sixteenBitMode)asm{
  737.         move.l cardBase,a1
  738.         lea 0x210(a1), a1
  739.         clr.l -16(a1)
  740.     @4    move.b (red),d1
  741.         add.l elementSpacing,red
  742.         move.l d1,(a1)
  743.         move.b (green),d1
  744.         add.l elementSpacing,green
  745.         move.l d1,(a1)
  746.         move.b (blue),d1
  747.         add.l elementSpacing,blue
  748.         move.l d1,(a1)
  749.         dbf count,@4
  750.     }else asm{
  751.     // In sixteenBitMode the clut addressing is weird.
  752.     // I arrived at the following solution by trial and error.
  753.     // It's a kludge, but is still fast enough. dgp.
  754.         move.l cardBase,a1
  755.         lea 0x210(a1), a1
  756.         clr.l -16(a1)
  757.     @44    move.b (red),d1
  758.         move.l d1,(a1)
  759.         move.b (green),d1
  760.         move.l d1,(a1)
  761.         move.b (blue),d1
  762.         move.l d1,(a1)
  763.  
  764.         move.b (red),d1
  765.         move.l d1,(a1)
  766.         move.b (green),d1
  767.         move.l d1,(a1)
  768.         move.b (blue),d1
  769.         move.l d1,(a1)
  770.  
  771.         move.b (red),d1
  772.         move.l d1,(a1)
  773.         move.b (green),d1
  774.         move.l d1,(a1)
  775.         move.b (blue),d1
  776.         move.l d1,(a1)
  777.  
  778.         move.b (red),d1
  779.         move.l d1,(a1)
  780.         move.b (green),d1
  781.         move.l d1,(a1)
  782.         move.b (blue),d1
  783.         move.l d1,(a1)
  784.  
  785.         move.b (red),d1
  786.         move.l d1,(a1)
  787.         move.b (green),d1
  788.         move.l d1,(a1)
  789.         move.b (blue),d1
  790.         move.l d1,(a1)
  791.  
  792.         move.b (red),d1
  793.         move.l d1,(a1)
  794.         move.b (green),d1
  795.         move.l d1,(a1)
  796.         move.b (blue),d1
  797.         move.l d1,(a1)
  798.  
  799.         move.b (red),d1
  800.         move.l d1,(a1)
  801.         move.b (green),d1
  802.         move.l d1,(a1)
  803.         move.b (blue),d1
  804.         move.l d1,(a1)
  805.  
  806.         move.b (red),d1
  807.         add.l elementSpacing,red
  808.         move.l d1,(a1)
  809.         move.b (green),d1
  810.         add.l elementSpacing,green
  811.         move.l d1,(a1)
  812.         move.b (blue),d1
  813.         add.l elementSpacing,blue
  814.         move.l d1,(a1)
  815.  
  816.         dbf count,@44
  817.     }
  818.     if(can32)SwapMMUMode(&mmuMode);
  819.     if(suspendInterrupts & flags)SwapPriority(&priority);
  820.     return 0;
  821. #else
  822.     start;count;red;green;blue;elementSpacing;mode;pixelSize;clutSize;cardBase;flags;    // prevent "unused argument" warning
  823.     return 1;
  824. #endif
  825. }
  826. OSErr LoadClutMacIIci(register short start,register short count
  827.     ,register char *red,register char *green,register char *blue
  828.     ,register long elementSpacing
  829.     ,short mode,short pixelSize,short clutSize,char *cardBase,short flags)
  830. {
  831. #if THINK_C
  832.     static char realstartindex[] = {0xFE, 0xFC, 0xF0, 0x00,0,0,0};
  833.     char priority=7;
  834.     static Boolean can32,firstTime=1;
  835.  
  836.     if(firstTime){
  837.         can32=TrapAvailable(_SwapMMUMode);
  838.         firstTime=0;
  839.     }
  840.     if(!(useMostSignificantBits & flags)){
  841.         // Point to less significant byte of word.
  842.         red++;
  843.         green++;
  844.         blue++;
  845.     }
  846.     if(suspendInterrupts & flags)SwapPriority(&priority);
  847.     mode&=7;
  848.     start+=realstartindex[mode];
  849.     asm {
  850.         move.w mode,d1
  851.         move.l cardBase,a0        // get card base address
  852.         move.l a0,a1
  853. //        move.b #255,8(a0)        // not necessary
  854.         move.b start,(a0)
  855.         addq.l #4,a1
  856.     @3    move.b (red),d1
  857.         add.l elementSpacing,red
  858.         move.b d1,(a1)
  859.         move.b (green),d1
  860.         add.l elementSpacing,green
  861.         move.b d1,(a1)
  862.         move.b (blue),d1
  863.         add.l elementSpacing,blue
  864.         move.b d1,(a1)
  865.         dbf count,@3
  866.     }
  867.     if(suspendInterrupts & flags)SwapPriority(&priority);
  868.     return 0;
  869. #else
  870.     start;count;red;green;blue;elementSpacing;mode;pixelSize;clutSize;cardBase;flags;    // prevent "unused argument" warning
  871.     return 1;
  872. #endif
  873. }
  874. // High resolution video card
  875. //#define HRVCBase            0x80000
  876. #define HRVCClutAddrReg        0x940E0
  877. #define HRVCClutWDataReg    0x940E4
  878. //#define HRVCClutRDataReg    0x94054
  879. OSErr LoadClutHiRes(short start,register short count
  880.     ,register char *red,register char *green,register char *blue
  881.     ,register long elementSpacing
  882.     ,short mode,short pixelSize,short clutSize,char *cardBase,short flags)
  883. {
  884.     char *bytePtr;
  885.     char mmuMode=true32b,priority=7;
  886.     static Boolean can32,firstTime=1;
  887.  
  888.     mode;pixelSize;    // prevent "unused argument" warning
  889.     if(firstTime){
  890.         can32=TrapAvailable(_SwapMMUMode);
  891.         firstTime=0;
  892.     }
  893.     if(!(useMostSignificantBits & flags)){
  894.         // Point to less significant byte of word.
  895.         red++;
  896.         green++;
  897.         blue++;
  898.     }
  899.     if(suspendInterrupts & flags)SwapPriority(&priority);
  900.     if(can32)SwapMMUMode(&mmuMode);
  901.     red+=count*elementSpacing;
  902.     green+=count*elementSpacing;
  903.     blue+=count*elementSpacing;
  904.     // We'll start with clut entry start+count, and work
  905.     // down from there to clut entry start. The clut address
  906.     // register counts down automatically.
  907.     *(cardBase+HRVCClutAddrReg)=~(clutSize-1-start-count);
  908.     bytePtr=cardBase+HRVCClutWDataReg;
  909.     // This is the key loop. 
  910.     // This C code is only about 10% slower than the original assembly code.
  911.     elementSpacing= -elementSpacing;
  912.     do{
  913.         *bytePtr=~ *red;
  914.         red+=elementSpacing;
  915.         *bytePtr=~ *green;
  916.         green+=elementSpacing;
  917.         *bytePtr=~ *blue;
  918.         blue+=elementSpacing;
  919.     }while(--count>=0);
  920.     if(can32)SwapMMUMode(&mmuMode);
  921.     if(suspendInterrupts & flags)SwapPriority(&priority);
  922.     return 0;
  923. }
  924. // Macintosh display card (8•24)
  925. //#define MDCVideoBase        0xA00
  926. #define MDCClutAddrReg        0x200200
  927. #define MDCClutDataReg        0x200204
  928. OSErr LoadClutx824(short start,register short count
  929.     ,register char *red,register char *green,register char *blue
  930.     ,register long elementSpacing
  931.     ,short mode,short pixelSize,short clutSize,char *cardBase,short flags)
  932. {
  933.     char mmuMode=true32b,priority=7;
  934.     register char *clut;
  935.     char *clutIndex;
  936.     static Boolean can32,firstTime=1;
  937.  
  938.     mode;pixelSize;clutSize;    // prevent "unused argument" warning
  939.     if(firstTime){
  940.         can32=TrapAvailable(_SwapMMUMode);
  941.         firstTime=0;
  942.     }
  943.     if(!(useMostSignificantBits & flags)){
  944.         // Point to less significant byte of word.
  945.         red++;
  946.         green++;
  947.         blue++;
  948.     }
  949.     if(suspendInterrupts & flags)SwapPriority(&priority);
  950.     if(can32)SwapMMUMode(&mmuMode);
  951.     clut=cardBase+MDCClutDataReg+3;
  952.     clutIndex=cardBase+MDCClutAddrReg;
  953.     *clutIndex=start;
  954.     for(;count>=0;count--){
  955.         *clut=*red;
  956.         red+=elementSpacing;
  957.         *clut=*green;
  958.         green+=elementSpacing;
  959.         *clut=*blue;
  960.         blue+=elementSpacing;
  961.     }
  962.     if(can32)SwapMMUMode(&mmuMode);
  963.     if(suspendInterrupts & flags)SwapPriority(&priority);
  964.     return 0;
  965. }
  966. // Macintosh display card 8•24 GC
  967. #define MDCgcClutAddrReg    0x6C00000
  968. #define MDCgcClutDataReg    0x6C00004
  969. OSErr LoadClutx824GC(short start,register short count
  970.     ,register char *red,register char *green,register char *blue
  971.     ,register long elementSpacing
  972.     ,short mode,short pixelSize,short clutSize,char *cardBase,short flags)
  973. {
  974.     char mmuMode=true32b,priority=7;
  975.     register long *clut;
  976.     char *clutIndex;
  977.     static Boolean can32,firstTime=1;
  978.  
  979.     mode;pixelSize;clutSize;    // prevent "unused argument" warning
  980.     if(firstTime){
  981.         can32=TrapAvailable(_SwapMMUMode);
  982.         firstTime=0;
  983.     }
  984.     if(!(useMostSignificantBits & flags)){
  985.         // Point to less significant byte of word.
  986.         red++;
  987.         green++;
  988.         blue++;
  989.     }
  990.     if(suspendInterrupts & flags)SwapPriority(&priority);
  991.     if(can32)SwapMMUMode(&mmuMode);
  992.     clutIndex=cardBase+MDCgcClutAddrReg;
  993.     *clutIndex=start;
  994.     #if 1
  995.         clut=(long *)(cardBase+MDCgcClutDataReg);
  996.         for(;count>=0;count--){
  997.             *clut=(long)(*red)<<24;
  998.             red+=elementSpacing;
  999.             *clut=(long)(*green)<<24;
  1000.             green+=elementSpacing;
  1001.             *clut=(long)(*blue)<<24;
  1002.             blue+=elementSpacing;
  1003.         }
  1004.     #else
  1005.         asm {
  1006.             move.l cardBase,a1
  1007.             add.l #MDCgcClutDataReg,a1
  1008.         @8    move.b (red),d1
  1009.             add.l    elementSpacing,red
  1010.             ror.l    #8,d1
  1011.             move.l d1,(a1)
  1012.             move.b (green),d1
  1013.             add.l    elementSpacing,green
  1014.             ror.l    #8,d1
  1015.             move.l d1,(a1)
  1016.             move.b (blue),d1
  1017.             add.l elementSpacing,blue
  1018.             ror.l    #8,d1
  1019.             move.l d1,(a1)
  1020.             dbf count,@8
  1021.         }
  1022.     #endif
  1023.     if(can32)SwapMMUMode(&mmuMode);
  1024.     if(suspendInterrupts & flags)SwapPriority(&priority);
  1025.     return 0;
  1026. }
  1027. // Toby frame buffer
  1028. //#define    TFBBase            0x80000
  1029. //#define TFBBufMid            0x80008
  1030. //#define TFBBufLow            0x8000C
  1031. //#define    TFBIBase        0x8fffc
  1032. #define TFBClutWDataReg        0x90018
  1033. //#define TFBClutRDataReg    0x90028
  1034. #define TFBClutAddrReg        0x9001C
  1035. #define    TFBReadVSync        0xD0000
  1036. //#define    TFBReadVInt        0xD0004
  1037. //#define    TFBReadIntlc    0xD0008
  1038. //#define    TFBVIntEnable    0xA0000
  1039. //#define    TFBVIntDisable    0xA0004
  1040. OSErr LoadClutToby(short start,register short count
  1041.     ,register char *red,register char *green,register char *blue
  1042.     ,register long elementSpacing
  1043.     ,short mode,short pixelSize,short clutSize,char *cardBase,short flags)
  1044. {
  1045.     register long index;
  1046.     char mmuMode=true32b,priority=7;
  1047.     register char *clut,*clutIndex;
  1048.     short shift;
  1049.     static Boolean can32,firstTime=1;
  1050.  
  1051.     mode;clutSize;    // prevent "unused argument" warning
  1052.     if(firstTime){
  1053.         can32=TrapAvailable(_SwapMMUMode);
  1054.         firstTime=0;
  1055.     }
  1056.     if(!(useMostSignificantBits & flags)){
  1057.         // Point to less significant byte of word.
  1058.         red++;
  1059.         green++;
  1060.         blue++;
  1061.     }
  1062.     index=(count+1)*elementSpacing;
  1063.     red+=index;
  1064.     green+=index;
  1065.     blue+=index;
  1066.     shift=8-pixelSize;
  1067.     index=start+count+1;
  1068.     clut=cardBase+TFBClutWDataReg;
  1069.     clutIndex=cardBase+TFBClutAddrReg;
  1070.     if(suspendInterrupts & flags)SwapPriority(&priority);
  1071.     if(can32)SwapMMUMode(&mmuMode);
  1072.     for(;count>=0;count--,index--){
  1073.         *clutIndex=(index<<shift)-1;
  1074.         red-=elementSpacing;
  1075.         *clut=~*red;
  1076.         green-=elementSpacing;
  1077.         *clut=~*green;
  1078.         blue-=elementSpacing;
  1079.         *clut=~*blue;
  1080.     }
  1081.     if(can32)SwapMMUMode(&mmuMode);
  1082.     if(suspendInterrupts & flags)SwapPriority(&priority);
  1083.     return 0;
  1084. }
  1085.  
  1086. OSErr WaitForNextBlanking(GDHandle device)
  1087. // WaitForNextBlanking waits for the beginning of the next vertical blanking interval.
  1088. // Returns 0 if successful, or 1 if device is not supported.
  1089. {
  1090.     register long *blankingPtr;
  1091.  
  1092.     switch(GetCardType(device)){
  1093.     case tobycard:
  1094.         blankingPtr = (long *) ((char *)GetCardBase(device) + TFBReadVSync);
  1095.         while (*blankingPtr & 1L) ;    // if we're already blanking, wait till end.
  1096.         while (!(*blankingPtr & 1L)) ; // wait until beginning of blanking interval.
  1097.         return 0;
  1098.     default:
  1099.         return 1;
  1100.     }
  1101. }
  1102.