load/save dialogs
[drnoksnes] / tile_org.cpp
1 /*
2  * Snes9x - Portable Super Nintendo Entertainment System (TM) emulator.
3  *
4  * (c) Copyright 1996 - 2001 Gary Henderson (gary.henderson@ntlworld.com) and
5  *                           Jerremy Koot (jkoot@snes9x.com)
6  *
7  * Super FX C emulator code 
8  * (c) Copyright 1997 - 1999 Ivar (ivar@snes9x.com) and
9  *                           Gary Henderson.
10  * Super FX assembler emulator code (c) Copyright 1998 zsKnight and _Demo_.
11  *
12  * DSP1 emulator code (c) Copyright 1998 Ivar, _Demo_ and Gary Henderson.
13  * C4 asm and some C emulation code (c) Copyright 2000 zsKnight and _Demo_.
14  * C4 C code (c) Copyright 2001 Gary Henderson (gary.henderson@ntlworld.com).
15  *
16  * DOS port code contains the works of other authors. See headers in
17  * individual files.
18  *
19  * Snes9x homepage: http://www.snes9x.com
20  *
21  * Permission to use, copy, modify and distribute Snes9x in both binary and
22  * source form, for non-commercial purposes, is hereby granted without fee,
23  * providing that this license information and copyright notice appear with
24  * all copies and any derived work.
25  *
26  * This software is provided 'as-is', without any express or implied
27  * warranty. In no event shall the authors be held liable for any damages
28  * arising from the use of this software.
29  *
30  * Snes9x is freeware for PERSONAL USE only. Commercial users should
31  * seek permission of the copyright holders first. Commercial use includes
32  * charging money for Snes9x or software derived from Snes9x.
33  *
34  * The copyright holders request that bug fixes and improvements to the code
35  * should be forwarded to them so everyone can benefit from the modifications
36  * in future versions.
37  *
38  * Super NES and Super Nintendo Entertainment System are trademarks of
39  * Nintendo Co., Limited and its subsidiary companies.
40  */
41 #include "snes9x.h"
42
43 #include "memmap.h"
44 #include "ppu.h"
45 #include "cpuexec.h"
46 #include "display.h"
47 #include "gfx.h"
48 #include "apu.h"
49 #include "tile_org.h"
50
51
52 extern uint32 HeadMask [4];
53 extern uint32 TailMask [5];
54
55 uint8 orgConvertTile (uint8 *pCache, uint32 TileAddr)
56 {
57     register uint8 *tp = &Memory.VRAM[TileAddr];
58     uint32 *p = (uint32 *) pCache;
59     uint32 non_zero = 0;
60     uint8 line;
61
62     switch (BG.BitShift)
63     {
64     case 8:
65         for (line = 8; line != 0; line--, tp += 2)
66         {
67             uint32 p1 = 0;
68             uint32 p2 = 0;
69             register uint8 pix;
70
71             if ((pix = *(tp + 0)))
72             {
73                 p1 |= odd_high[0][pix >> 4];
74                 p2 |= odd_low[0][pix & 0xf];
75             }
76             if ((pix = *(tp + 1)))
77             {
78                 p1 |= even_high[0][pix >> 4];
79                 p2 |= even_low[0][pix & 0xf];
80             }
81             if ((pix = *(tp + 16)))
82             {
83                 p1 |= odd_high[1][pix >> 4];
84                 p2 |= odd_low[1][pix & 0xf];
85             }
86             if ((pix = *(tp + 17)))
87             {
88                 p1 |= even_high[1][pix >> 4];
89                 p2 |= even_low[1][pix & 0xf];
90             }
91             if ((pix = *(tp + 32)))
92             {
93                 p1 |= odd_high[2][pix >> 4];
94                 p2 |= odd_low[2][pix & 0xf];
95             }
96             if ((pix = *(tp + 33)))
97             {
98                 p1 |= even_high[2][pix >> 4];
99                 p2 |= even_low[2][pix & 0xf];
100             }
101             if ((pix = *(tp + 48)))
102             {
103                 p1 |= odd_high[3][pix >> 4];
104                 p2 |= odd_low[3][pix & 0xf];
105             }
106             if ((pix = *(tp + 49)))
107             {
108                 p1 |= even_high[3][pix >> 4];
109                 p2 |= even_low[3][pix & 0xf];
110             }
111             *p++ = p1;
112             *p++ = p2;
113             non_zero |= p1 | p2;
114         }
115         break;
116
117     case 4:
118         for (line = 8; line != 0; line--, tp += 2)
119         {
120             uint32 p1 = 0;
121             uint32 p2 = 0;
122             register uint8 pix;
123             if ((pix = *(tp + 0)))
124             {
125                 p1 |= odd_high[0][pix >> 4];
126                 p2 |= odd_low[0][pix & 0xf];
127             }
128             if ((pix = *(tp + 1)))
129             {
130                 p1 |= even_high[0][pix >> 4];
131                 p2 |= even_low[0][pix & 0xf];
132             }
133             if ((pix = *(tp + 16)))
134             {
135                 p1 |= odd_high[1][pix >> 4];
136                 p2 |= odd_low[1][pix & 0xf];
137             }
138             if ((pix = *(tp + 17)))
139             {
140                 p1 |= even_high[1][pix >> 4];
141                 p2 |= even_low[1][pix & 0xf];
142             }
143             *p++ = p1;
144             *p++ = p2;
145             non_zero |= p1 | p2;
146         }
147         break;
148
149     case 2:
150         for (line = 8; line != 0; line--, tp += 2)
151         {
152             uint32 p1 = 0;
153             uint32 p2 = 0;
154             register uint8 pix;
155             if ((pix = *(tp + 0)))
156             {
157                 p1 |= odd_high[0][pix >> 4];
158                 p2 |= odd_low[0][pix & 0xf];
159             }
160             if ((pix = *(tp + 1)))
161             {
162                 p1 |= even_high[0][pix >> 4];
163                 p2 |= even_low[0][pix & 0xf];
164             }
165             *p++ = p1;
166             *p++ = p2;
167             non_zero |= p1 | p2;
168         }
169         break;
170     }
171     return (non_zero ? TRUE : BLANK_TILE);
172 }
173
174
175
176 #define PLOT_PIXEL(screen, pixel) (pixel)
177
178 inline void WRITE_4PIXELS16 (uint32 Offset, uint8 *Pixels)
179 {
180     uint32 Pixel;
181     uint16 *Screen = (uint16 *) GFX.S + Offset;
182     uint8  *Depth = GFX.DB + Offset;
183
184 #define FN(N) \
185     if (GFX.Z1 > Depth [N] && (Pixel = Pixels[N])) \
186     { \
187         Screen [N] = GFX.ScreenColors [Pixel]; \
188         Depth [N] = GFX.Z2; \
189     }
190
191     FN(0)
192     FN(1)
193     FN(2)
194     FN(3)
195 #undef FN
196 }
197
198 inline void WRITE_4PIXELS16_FLIPPED (uint32 Offset, uint8 *Pixels)
199 {
200     uint32 Pixel;
201     uint16 *Screen = (uint16 *) GFX.S + Offset;
202     uint8  *Depth = GFX.DB + Offset;
203
204 #define FN(N) \
205     if (GFX.Z1 > Depth [N] && (Pixel = Pixels[3 - N])) \
206     { \
207         Screen [N] = GFX.ScreenColors [Pixel]; \
208         Depth [N] = GFX.Z2; \
209     }
210
211     FN(0)
212     FN(1)
213     FN(2)
214     FN(3)
215 #undef FN
216 }
217
218
219 inline void WRITE_4PIXELSHI16 (uint32 Offset, uint8 *Pixels)
220 {
221     uint32 Pixel;
222     uint16 *Screen = (uint16 *) GFX.S + Offset;
223     uint8  *Depth = GFX.DB + Offset;
224
225 #define FN(N) \
226     if (GFX.Z1 > Depth [N] && (Pixel = Pixels[2*N])) \
227     { \
228         Screen [N] = GFX.ScreenColors [Pixel]; \
229         Depth [N] = GFX.Z2; \
230     }
231
232     FN(0)
233     FN(1)
234     FN(2)
235     FN(3)
236 #undef FN
237 }
238
239 inline void WRITE_4PIXELSHI16_FLIPPED (uint32 Offset, uint8 *Pixels)
240 {
241     uint32 Pixel;
242     uint16 *Screen = (uint16 *) GFX.S + Offset;
243     uint8  *Depth = GFX.DB + Offset;
244
245 #define FN(N) \
246     if (GFX.Z1 > Depth [N] && (Pixel = Pixels[6 - 2*N])) \
247     { \
248         Screen [N] = GFX.ScreenColors [Pixel]; \
249         Depth [N] = GFX.Z2; \
250     }
251
252     FN(0)
253     FN(1)
254     FN(2)
255     FN(3)
256 #undef FN
257 }
258
259
260 inline void WRITE_4PIXELS16x2 (uint32 Offset, uint8 *Pixels)
261 {
262     uint32 Pixel;
263     uint16 *Screen = (uint16 *) GFX.S + Offset;
264     uint8  *Depth = GFX.DB + Offset;
265
266 #define FN(N) \
267     if (GFX.Z1 > Depth [N * 2] && (Pixel = Pixels[N])) \
268     { \
269         Screen [N * 2] = Screen [N * 2 + 1] = GFX.ScreenColors [Pixel]; \
270         Depth [N * 2] = Depth [N * 2 + 1] = GFX.Z2; \
271     }
272
273     FN(0)
274     FN(1)
275     FN(2)
276     FN(3)
277 #undef FN
278 }
279
280 inline void WRITE_4PIXELS16_FLIPPEDx2 (uint32 Offset, uint8 *Pixels)
281 {
282     uint32 Pixel;
283     uint16 *Screen = (uint16 *) GFX.S + Offset;
284     uint8  *Depth = GFX.DB + Offset;
285
286 #define FN(N) \
287     if (GFX.Z1 > Depth [N * 2] && (Pixel = Pixels[3 - N])) \
288     { \
289         Screen [N * 2] = Screen [N * 2 + 1] = GFX.ScreenColors [Pixel]; \
290         Depth [N * 2] = Depth [N * 2 + 1] = GFX.Z2; \
291     }
292
293     FN(0)
294     FN(1)
295     FN(2)
296     FN(3)
297 #undef FN
298 }
299
300 inline void WRITE_4PIXELS16x2x2 (uint32 Offset, uint8 *Pixels)
301 {
302     uint32 Pixel;
303     uint16 *Screen = (uint16 *) GFX.S + Offset;
304     uint8  *Depth = GFX.DB + Offset;
305
306 #define FN(N) \
307     if (GFX.Z1 > Depth [N * 2] && (Pixel = Pixels[N])) \
308     { \
309         Screen [N * 2] = Screen [N * 2 + 1] = Screen [(GFX.RealPitch >> 1) + N * 2] = \
310             Screen [(GFX.RealPitch >> 1) + N * 2 + 1] = GFX.ScreenColors [Pixel]; \
311         Depth [N * 2] = Depth [N * 2 + 1] = Depth [(GFX.RealPitch >> 1) + N * 2] = \
312             Depth [(GFX.RealPitch >> 1) + N * 2 + 1] = GFX.Z2; \
313     }
314
315     FN(0)
316     FN(1)
317     FN(2)
318     FN(3)
319 #undef FN
320 }
321
322 inline void WRITE_4PIXELS16_FLIPPEDx2x2 (uint32 Offset, uint8 *Pixels)
323 {
324     uint32 Pixel;
325     uint16 *Screen = (uint16 *) GFX.S + Offset;
326     uint8  *Depth = GFX.DB + Offset;
327
328 #define FN(N) \
329     if (GFX.Z1 > Depth [N * 2] && (Pixel = Pixels[3 - N])) \
330     { \
331         Screen [N * 2] = Screen [N * 2 + 1] = Screen [(GFX.RealPitch >> 1) + N * 2] = \
332             Screen [(GFX.RealPitch >> 1) + N * 2 + 1] = GFX.ScreenColors [Pixel]; \
333         Depth [N * 2] = Depth [N * 2 + 1] = Depth [(GFX.RealPitch >> 1) + N * 2] = \
334             Depth [(GFX.RealPitch >> 1) + N * 2 + 1] = GFX.Z2; \
335     }
336
337     FN(0)
338     FN(1)
339     FN(2)
340     FN(3)
341 #undef FN
342 }
343
344 void orgDrawTile16 (uint32 Tile, uint32 Offset, uint32 StartLine,
345                  uint32 LineCount)
346 {
347     TILE_PREAMBLE
348     register uint8 *bp;
349
350     RENDER_TILE(WRITE_4PIXELS16, WRITE_4PIXELS16_FLIPPED, 4)
351 }
352
353 void orgDrawClippedTile16 (uint32 Tile, uint32 Offset,
354                         uint32 StartPixel, uint32 Width,
355                         uint32 StartLine, uint32 LineCount)
356 {
357     TILE_PREAMBLE
358     register uint8 *bp;
359
360     TILE_CLIP_PREAMBLE
361     RENDER_CLIPPED_TILE(WRITE_4PIXELS16, WRITE_4PIXELS16_FLIPPED, 4)
362 }
363
364 void orgDrawTileHi16 (uint32 Tile, uint32 Offset, uint32 StartLine,
365                  uint32 LineCount)
366 {
367     TILE_PREAMBLE
368     register uint8 *bp;
369
370     RENDER_TILEHI(WRITE_4PIXELSHI16, WRITE_4PIXELSHI16_FLIPPED, 4)
371 }
372
373 void orgDrawClippedTileHi16 (uint32 Tile, uint32 Offset,
374                         uint32 StartPixel, uint32 Width,
375                         uint32 StartLine, uint32 LineCount)
376 {
377     TILE_PREAMBLE
378     register uint8 *bp;
379
380     TILE_CLIP_PREAMBLE
381     RENDER_CLIPPED_TILEHI(WRITE_4PIXELSHI16, WRITE_4PIXELSHI16_FLIPPED, 4)
382 }
383
384
385 void orgDrawTile16x2 (uint32 Tile, uint32 Offset, uint32 StartLine,
386                    uint32 LineCount)
387 {
388     TILE_PREAMBLE
389     register uint8 *bp;
390
391     RENDER_TILE(WRITE_4PIXELS16x2, WRITE_4PIXELS16_FLIPPEDx2, 8)
392 }
393
394 void orgDrawClippedTile16x2 (uint32 Tile, uint32 Offset,
395                           uint32 StartPixel, uint32 Width,
396                           uint32 StartLine, uint32 LineCount)
397 {
398     TILE_PREAMBLE
399     register uint8 *bp;
400
401     TILE_CLIP_PREAMBLE
402     RENDER_CLIPPED_TILE(WRITE_4PIXELS16x2, WRITE_4PIXELS16_FLIPPEDx2, 8)
403 }
404
405 void orgDrawTile16x2x2 (uint32 Tile, uint32 Offset, uint32 StartLine,
406                      uint32 LineCount)
407 {
408     TILE_PREAMBLE
409     register uint8 *bp;
410
411     RENDER_TILE(WRITE_4PIXELS16x2x2, WRITE_4PIXELS16_FLIPPEDx2x2, 8)
412 }
413
414 void orgDrawClippedTile16x2x2 (uint32 Tile, uint32 Offset,
415                             uint32 StartPixel, uint32 Width,
416                             uint32 StartLine, uint32 LineCount)
417 {
418     TILE_PREAMBLE
419     register uint8 *bp;
420
421     TILE_CLIP_PREAMBLE
422     RENDER_CLIPPED_TILE(WRITE_4PIXELS16x2x2, WRITE_4PIXELS16_FLIPPEDx2x2, 8)
423 }
424
425 void orgDrawLargePixel16 (uint32 Tile, uint32 Offset,
426                        uint32 StartPixel, uint32 Pixels,
427                        uint32 StartLine, uint32 LineCount)
428 {
429     TILE_PREAMBLE
430
431     register uint16 *sp = (uint16 *) GFX.S + Offset;
432     uint8  *Depth = GFX.DB + Offset;
433     uint16 pixel;
434
435     RENDER_TILE_LARGE (GFX.ScreenColors [pixel], PLOT_PIXEL)
436 }
437
438 inline void WRITE_4PIXELS16_ADD (uint32 Offset, uint8 *Pixels)
439 {
440     uint32 Pixel;
441     uint16 *Screen = (uint16 *) GFX.S + Offset;
442     uint8  *Depth = GFX.ZBuffer + Offset;
443     uint8  *SubDepth = GFX.SubZBuffer + Offset;
444
445 #define FN(N) \
446     if (GFX.Z1 > Depth [N] && (Pixel = Pixels[N])) \
447     { \
448         if (SubDepth [N]) \
449         { \
450             if (SubDepth [N] != 1) \
451                 Screen [N] = COLOR_ADD (GFX.ScreenColors [Pixel], \
452                                         Screen [GFX.Delta + N]); \
453             else \
454                 Screen [N] = COLOR_ADD (GFX.ScreenColors [Pixel], \
455                                         GFX.FixedColour); \
456         } \
457         else \
458             Screen [N] = GFX.ScreenColors [Pixel]; \
459         Depth [N] = GFX.Z2; \
460     }
461
462     FN(0)
463     FN(1)
464     FN(2)
465     FN(3)
466
467 #undef FN
468 }
469
470 inline void WRITE_4PIXELS16_FLIPPED_ADD (uint32 Offset, uint8 *Pixels)
471 {
472     uint32 Pixel;
473     uint16 *Screen = (uint16 *) GFX.S + Offset;
474     uint8  *Depth = GFX.ZBuffer + Offset;
475     uint8  *SubDepth = GFX.SubZBuffer + Offset;
476
477 #define FN(N) \
478     if (GFX.Z1 > Depth [N] && (Pixel = Pixels[3 - N])) \
479     { \
480         if (SubDepth [N]) \
481         { \
482             if (SubDepth [N] != 1) \
483                 Screen [N] = COLOR_ADD (GFX.ScreenColors [Pixel], \
484                                         Screen [GFX.Delta + N]); \
485             else \
486                 Screen [N] = COLOR_ADD (GFX.ScreenColors [Pixel], \
487                                         GFX.FixedColour); \
488         } \
489         else \
490             Screen [N] = GFX.ScreenColors [Pixel]; \
491         Depth [N] = GFX.Z2; \
492     }
493
494     FN(0)
495     FN(1)
496     FN(2)
497     FN(3)
498
499 #undef FN
500 }
501
502 inline void WRITE_4PIXELS16_ADD1_2 (uint32 Offset, uint8 *Pixels)
503 {
504     uint32 Pixel;
505     uint16 *Screen = (uint16 *) GFX.S + Offset;
506     uint8  *Depth = GFX.ZBuffer + Offset;
507     uint8  *SubDepth = GFX.SubZBuffer + Offset;
508
509 #define FN(N) \
510     if (GFX.Z1 > Depth [N] && (Pixel = Pixels[N])) \
511     { \
512         if (SubDepth [N]) \
513         { \
514             if (SubDepth [N] != 1) \
515                 Screen [N] = (uint16) (COLOR_ADD1_2 (GFX.ScreenColors [Pixel], \
516                                                      Screen [GFX.Delta + N])); \
517             else \
518                 Screen [N] = COLOR_ADD (GFX.ScreenColors [Pixel], \
519                                         GFX.FixedColour); \
520         } \
521         else \
522             Screen [N] = GFX.ScreenColors [Pixel]; \
523         Depth [N] = GFX.Z2; \
524     }
525
526     FN(0)
527     FN(1)
528     FN(2)
529     FN(3)
530
531 #undef FN
532 }
533
534 inline void WRITE_4PIXELS16_FLIPPED_ADD1_2 (uint32 Offset, uint8 *Pixels)
535 {
536     uint32 Pixel;
537     uint16 *Screen = (uint16 *) GFX.S + Offset;
538     uint8  *Depth = GFX.ZBuffer + Offset;
539     uint8  *SubDepth = GFX.SubZBuffer + Offset;
540
541 #define FN(N) \
542     if (GFX.Z1 > Depth [N] && (Pixel = Pixels[3 - N])) \
543     { \
544         if (SubDepth [N]) \
545         { \
546             if (SubDepth [N] != 1) \
547                 Screen [N] = (uint16) (COLOR_ADD1_2 (GFX.ScreenColors [Pixel], \
548                                                      Screen [GFX.Delta + N])); \
549             else \
550                 Screen [N] = COLOR_ADD (GFX.ScreenColors [Pixel], \
551                                         GFX.FixedColour); \
552         } \
553         else \
554             Screen [N] = GFX.ScreenColors [Pixel]; \
555         Depth [N] = GFX.Z2; \
556     }
557
558     FN(0)
559     FN(1)
560     FN(2)
561     FN(3)
562
563 #undef FN
564 }
565
566 inline void WRITE_4PIXELS16_SUB (uint32 Offset, uint8 *Pixels)
567 {
568     uint32 Pixel;
569     uint16 *Screen = (uint16 *) GFX.S + Offset;
570     uint8  *Depth = GFX.ZBuffer + Offset;
571     uint8  *SubDepth = GFX.SubZBuffer + Offset;
572
573 #define FN(N) \
574     if (GFX.Z1 > Depth [N] && (Pixel = Pixels[N])) \
575     { \
576         if (SubDepth [N]) \
577         { \
578             if (SubDepth [N] != 1) \
579                 Screen [N] = (uint16) COLOR_SUB (GFX.ScreenColors [Pixel], \
580                                         Screen [GFX.Delta + N]); \
581             else \
582                 Screen [N] = (uint16) COLOR_SUB (GFX.ScreenColors [Pixel], \
583                                         GFX.FixedColour); \
584         } \
585         else \
586             Screen [N] = GFX.ScreenColors [Pixel]; \
587         Depth [N] = GFX.Z2; \
588     }
589
590     FN(0)
591     FN(1)
592     FN(2)
593     FN(3)
594
595 #undef FN
596 }
597
598 inline void WRITE_4PIXELS16_FLIPPED_SUB (uint32 Offset, uint8 *Pixels)
599 {
600     uint32 Pixel;
601     uint16 *Screen = (uint16 *) GFX.S + Offset;
602     uint8  *Depth = GFX.ZBuffer + Offset;
603     uint8  *SubDepth = GFX.SubZBuffer + Offset;
604
605 #define FN(N) \
606     if (GFX.Z1 > Depth [N] && (Pixel = Pixels[3 - N])) \
607     { \
608         if (SubDepth [N]) \
609         { \
610             if (SubDepth [N] != 1) \
611                 Screen [N] = (uint16) COLOR_SUB (GFX.ScreenColors [Pixel], \
612                                         Screen [GFX.Delta + N]); \
613             else \
614                 Screen [N] = (uint16) COLOR_SUB (GFX.ScreenColors [Pixel], \
615                                         GFX.FixedColour); \
616         } \
617         else \
618             Screen [N] = GFX.ScreenColors [Pixel]; \
619         Depth [N] = GFX.Z2; \
620     }
621
622     FN(0)
623     FN(1)
624     FN(2)
625     FN(3)
626
627 #undef FN
628 }
629
630 inline void WRITE_4PIXELS16_SUB1_2 (uint32 Offset, uint8 *Pixels)
631 {
632     uint32 Pixel;
633     uint16 *Screen = (uint16 *) GFX.S + Offset;
634     uint8  *Depth = GFX.ZBuffer + Offset;
635     uint8  *SubDepth = GFX.SubZBuffer + Offset;
636
637 #define FN(N) \
638     if (GFX.Z1 > Depth [N] && (Pixel = Pixels[N])) \
639     { \
640         if (SubDepth [N]) \
641         { \
642             if (SubDepth [N] != 1) \
643                 Screen [N] = (uint16) COLOR_SUB1_2 (GFX.ScreenColors [Pixel], \
644                                            Screen [GFX.Delta + N]); \
645             else \
646                 Screen [N] = (uint16) COLOR_SUB (GFX.ScreenColors [Pixel], \
647                                         GFX.FixedColour); \
648         } \
649         else \
650             Screen [N] = GFX.ScreenColors [Pixel]; \
651         Depth [N] = GFX.Z2; \
652     }
653
654     FN(0)
655     FN(1)
656     FN(2)
657     FN(3)
658
659 #undef FN
660 }
661
662 inline void WRITE_4PIXELS16_FLIPPED_SUB1_2 (uint32 Offset, uint8 *Pixels)
663 {
664     uint32 Pixel;
665     uint16 *Screen = (uint16 *) GFX.S + Offset;
666     uint8  *Depth = GFX.ZBuffer + Offset;
667     uint8  *SubDepth = GFX.SubZBuffer + Offset;
668
669 #define FN(N) \
670     if (GFX.Z1 > Depth [N] && (Pixel = Pixels[3 - N])) \
671     { \
672         if (SubDepth [N]) \
673         { \
674             if (SubDepth [N] != 1) \
675                 Screen [N] = (uint16) COLOR_SUB1_2 (GFX.ScreenColors [Pixel], \
676                                            Screen [GFX.Delta + N]); \
677             else \
678                 Screen [N] = (uint16) COLOR_SUB (GFX.ScreenColors [Pixel], \
679                                         GFX.FixedColour); \
680         } \
681         else \
682             Screen [N] = GFX.ScreenColors [Pixel]; \
683         Depth [N] = GFX.Z2; \
684     }
685
686     FN(0)
687     FN(1)
688     FN(2)
689     FN(3)
690
691 #undef FN
692 }
693
694
695 void orgDrawTile16Add (uint32 Tile, uint32 Offset, uint32 StartLine,
696                     uint32 LineCount)
697 {
698     TILE_PREAMBLE
699     register uint8 *bp;
700
701     RENDER_TILE(WRITE_4PIXELS16_ADD, WRITE_4PIXELS16_FLIPPED_ADD, 4)
702 }
703
704 void orgDrawClippedTile16Add (uint32 Tile, uint32 Offset,
705                            uint32 StartPixel, uint32 Width,
706                            uint32 StartLine, uint32 LineCount)
707 {
708     TILE_PREAMBLE
709     register uint8 *bp;
710
711     TILE_CLIP_PREAMBLE
712     RENDER_CLIPPED_TILE(WRITE_4PIXELS16_ADD, WRITE_4PIXELS16_FLIPPED_ADD, 4)
713 }
714
715 void orgDrawTile16Add1_2 (uint32 Tile, uint32 Offset, uint32 StartLine,
716                        uint32 LineCount)
717 {
718     TILE_PREAMBLE
719     register uint8 *bp;
720
721     RENDER_TILE(WRITE_4PIXELS16_ADD1_2, WRITE_4PIXELS16_FLIPPED_ADD1_2, 4)
722 }
723
724 void orgDrawClippedTile16Add1_2 (uint32 Tile, uint32 Offset,
725                               uint32 StartPixel, uint32 Width,
726                               uint32 StartLine, uint32 LineCount)
727 {
728     TILE_PREAMBLE
729     register uint8 *bp;
730
731     TILE_CLIP_PREAMBLE
732     RENDER_CLIPPED_TILE(WRITE_4PIXELS16_ADD1_2, WRITE_4PIXELS16_FLIPPED_ADD1_2, 4)
733 }
734
735 void orgDrawTile16Sub (uint32 Tile, uint32 Offset, uint32 StartLine,
736                     uint32 LineCount)
737 {
738     TILE_PREAMBLE
739     register uint8 *bp;
740
741     RENDER_TILE(WRITE_4PIXELS16_SUB, WRITE_4PIXELS16_FLIPPED_SUB, 4)
742 }
743
744 void orgDrawClippedTile16Sub (uint32 Tile, uint32 Offset,
745                            uint32 StartPixel, uint32 Width,
746                            uint32 StartLine, uint32 LineCount)
747 {
748     TILE_PREAMBLE
749     register uint8 *bp;
750
751     TILE_CLIP_PREAMBLE
752     RENDER_CLIPPED_TILE(WRITE_4PIXELS16_SUB, WRITE_4PIXELS16_FLIPPED_SUB, 4)
753 }
754
755 void orgDrawTile16Sub1_2 (uint32 Tile, uint32 Offset, uint32 StartLine,
756                        uint32 LineCount)
757 {
758     TILE_PREAMBLE
759     register uint8 *bp;
760
761     RENDER_TILE(WRITE_4PIXELS16_SUB1_2, WRITE_4PIXELS16_FLIPPED_SUB1_2, 4)
762 }
763
764 void orgDrawClippedTile16Sub1_2 (uint32 Tile, uint32 Offset,
765                               uint32 StartPixel, uint32 Width,
766                               uint32 StartLine, uint32 LineCount)
767 {
768     TILE_PREAMBLE
769     register uint8 *bp;
770
771     TILE_CLIP_PREAMBLE
772     RENDER_CLIPPED_TILE(WRITE_4PIXELS16_SUB1_2, WRITE_4PIXELS16_FLIPPED_SUB1_2, 4)
773 }
774
775 inline void WRITE_4PIXELS16_ADDF1_2 (uint32 Offset, uint8 *Pixels)
776 {
777     uint32 Pixel;
778     uint16 *Screen = (uint16 *) GFX.S + Offset;
779     uint8  *Depth = GFX.ZBuffer + Offset;
780     uint8  *SubDepth = GFX.SubZBuffer + Offset;
781
782 #define FN(N) \
783     if (GFX.Z1 > Depth [N] && (Pixel = Pixels[N])) \
784     { \
785         if (SubDepth [N] == 1) \
786             Screen [N] = (uint16) (COLOR_ADD1_2 (GFX.ScreenColors [Pixel], \
787                                                  GFX.FixedColour)); \
788         else \
789             Screen [N] = GFX.ScreenColors [Pixel];\
790         Depth [N] = GFX.Z2; \
791     }
792
793     FN(0)
794     FN(1)
795     FN(2)
796     FN(3)
797
798 #undef FN
799 }
800
801 inline void WRITE_4PIXELS16_FLIPPED_ADDF1_2 (uint32 Offset, uint8 *Pixels)
802 {
803     uint32 Pixel;
804     uint16 *Screen = (uint16 *) GFX.S + Offset;
805     uint8  *Depth = GFX.ZBuffer + Offset;
806     uint8  *SubDepth = GFX.SubZBuffer + Offset;
807
808 #define FN(N) \
809     if (GFX.Z1 > Depth [N] && (Pixel = Pixels[3 - N])) \
810     { \
811         if (SubDepth [N] == 1) \
812             Screen [N] = (uint16) (COLOR_ADD1_2 (GFX.ScreenColors [Pixel], \
813                                                  GFX.FixedColour)); \
814         else \
815             Screen [N] = GFX.ScreenColors [Pixel];\
816         Depth [N] = GFX.Z2; \
817     }
818
819     FN(0)
820     FN(1)
821     FN(2)
822     FN(3)
823
824 #undef FN
825 }
826
827 inline void WRITE_4PIXELS16_SUBF1_2 (uint32 Offset, uint8 *Pixels)
828 {
829     uint32 Pixel;
830     uint16 *Screen = (uint16 *) GFX.S + Offset;
831     uint8  *Depth = GFX.ZBuffer + Offset;
832     uint8  *SubDepth = GFX.SubZBuffer + Offset;
833
834 #define FN(N) \
835     if (GFX.Z1 > Depth [N] && (Pixel = Pixels[N])) \
836     { \
837         if (SubDepth [N] == 1) \
838             Screen [N] = (uint16) COLOR_SUB1_2 (GFX.ScreenColors [Pixel], \
839                                                 GFX.FixedColour); \
840         else \
841             Screen [N] = GFX.ScreenColors [Pixel]; \
842         Depth [N] = GFX.Z2; \
843     }
844
845     FN(0)
846     FN(1)
847     FN(2)
848     FN(3)
849
850 #undef FN
851 }
852
853 inline void WRITE_4PIXELS16_FLIPPED_SUBF1_2 (uint32 Offset, uint8 *Pixels)
854 {
855     uint32 Pixel;
856     uint16 *Screen = (uint16 *) GFX.S + Offset;
857     uint8  *Depth = GFX.ZBuffer + Offset;
858     uint8  *SubDepth = GFX.SubZBuffer + Offset;
859
860 #define FN(N) \
861     if (GFX.Z1 > Depth [N] && (Pixel = Pixels[3 - N])) \
862     { \
863         if (SubDepth [N] == 1) \
864             Screen [N] = (uint16) COLOR_SUB1_2 (GFX.ScreenColors [Pixel], \
865                                                 GFX.FixedColour); \
866         else \
867             Screen [N] = GFX.ScreenColors [Pixel]; \
868         Depth [N] = GFX.Z2; \
869     }
870
871     FN(0)
872     FN(1)
873     FN(2)
874     FN(3)
875
876 #undef FN
877 }
878
879 void orgDrawTile16FixedAdd1_2 (uint32 Tile, uint32 Offset, uint32 StartLine,
880                             uint32 LineCount)
881 {
882     TILE_PREAMBLE
883     register uint8 *bp;
884
885     RENDER_TILE(WRITE_4PIXELS16_ADDF1_2, WRITE_4PIXELS16_FLIPPED_ADDF1_2, 4)
886 }
887
888 void orgDrawClippedTile16FixedAdd1_2 (uint32 Tile, uint32 Offset,
889                                    uint32 StartPixel, uint32 Width,
890                                    uint32 StartLine, uint32 LineCount)
891 {
892     TILE_PREAMBLE
893     register uint8 *bp;
894
895     TILE_CLIP_PREAMBLE
896     RENDER_CLIPPED_TILE(WRITE_4PIXELS16_ADDF1_2, 
897                         WRITE_4PIXELS16_FLIPPED_ADDF1_2, 4)
898 }
899
900 void orgDrawTile16FixedSub1_2 (uint32 Tile, uint32 Offset, uint32 StartLine,
901                             uint32 LineCount)
902 {
903     TILE_PREAMBLE
904     register uint8 *bp;
905
906     RENDER_TILE(WRITE_4PIXELS16_SUBF1_2, WRITE_4PIXELS16_FLIPPED_SUBF1_2, 4)
907 }
908
909 void orgDrawClippedTile16FixedSub1_2 (uint32 Tile, uint32 Offset,
910                                    uint32 StartPixel, uint32 Width,
911                                    uint32 StartLine, uint32 LineCount)
912 {
913     TILE_PREAMBLE
914     register uint8 *bp;
915
916     TILE_CLIP_PREAMBLE
917     RENDER_CLIPPED_TILE(WRITE_4PIXELS16_SUBF1_2, 
918                         WRITE_4PIXELS16_FLIPPED_SUBF1_2, 4)
919 }
920
921 void orgDrawLargePixel16Add (uint32 Tile, uint32 Offset,
922                           uint32 StartPixel, uint32 Pixels,
923                           uint32 StartLine, uint32 LineCount)
924 {
925     TILE_PREAMBLE
926
927     register uint16 *sp = (uint16 *) GFX.S + Offset;
928     uint8  *Depth = GFX.ZBuffer + Offset;
929     uint16 pixel;
930
931 #define LARGE_ADD_PIXEL(s, p) \
932 (Depth [z + GFX.DepthDelta] ? (Depth [z + GFX.DepthDelta] != 1 ? \
933                                COLOR_ADD (p, *(s + GFX.Delta))    : \
934                                COLOR_ADD (p, GFX.FixedColour)) \
935                             : p)
936                               
937     RENDER_TILE_LARGE (GFX.ScreenColors [pixel], LARGE_ADD_PIXEL)
938 }
939
940 void orgDrawLargePixel16Add1_2 (uint32 Tile, uint32 Offset,
941                              uint32 StartPixel, uint32 Pixels,
942                              uint32 StartLine, uint32 LineCount)
943 {
944     TILE_PREAMBLE
945
946     register uint16 *sp = (uint16 *) GFX.S + Offset;
947     uint8  *Depth = GFX.ZBuffer + Offset;
948     uint16 pixel;
949
950 #define LARGE_ADD_PIXEL1_2(s, p) \
951 ((uint16) (Depth [z + GFX.DepthDelta] ? (Depth [z + GFX.DepthDelta] != 1 ? \
952                                COLOR_ADD1_2 (p, *(s + GFX.Delta))    : \
953                                COLOR_ADD (p, GFX.FixedColour)) \
954                             : p))
955                               
956     RENDER_TILE_LARGE (GFX.ScreenColors [pixel], LARGE_ADD_PIXEL1_2)
957 }
958
959 void orgDrawLargePixel16Sub (uint32 Tile, uint32 Offset,
960                           uint32 StartPixel, uint32 Pixels,
961                           uint32 StartLine, uint32 LineCount)
962 {
963     TILE_PREAMBLE
964
965     register uint16 *sp = (uint16 *) GFX.S + Offset;
966     uint8  *Depth = GFX.ZBuffer + Offset;
967     uint16 pixel;
968
969 #define LARGE_SUB_PIXEL(s, p) \
970 (Depth [z + GFX.DepthDelta] ? (Depth [z + GFX.DepthDelta] != 1 ? \
971                                COLOR_SUB (p, *(s + GFX.Delta))    : \
972                                COLOR_SUB (p, GFX.FixedColour)) \
973                             : p)
974                               
975     RENDER_TILE_LARGE (GFX.ScreenColors [pixel], LARGE_SUB_PIXEL)
976 }
977
978 void orgDrawLargePixel16Sub1_2 (uint32 Tile, uint32 Offset,
979                              uint32 StartPixel, uint32 Pixels,
980                              uint32 StartLine, uint32 LineCount)
981 {
982     TILE_PREAMBLE
983
984     register uint16 *sp = (uint16 *) GFX.S + Offset;
985     uint8  *Depth = GFX.ZBuffer + Offset;
986     uint16 pixel;
987
988 #define LARGE_SUB_PIXEL1_2(s, p) \
989 (Depth [z + GFX.DepthDelta] ? (Depth [z + GFX.DepthDelta] != 1 ? \
990                                COLOR_SUB1_2 (p, *(s + GFX.Delta))    : \
991                                COLOR_SUB (p, GFX.FixedColour)) \
992                             : p)
993                               
994     RENDER_TILE_LARGE (GFX.ScreenColors [pixel], LARGE_SUB_PIXEL1_2)
995 }
996