Previous 15994 Revisions Next

r6174 Friday 23rd October, 2009 at 07:17:03 UTC by Harmony
[N64] Ported numerous fixes over from Mupen64's software rasterizer et al, some games appear considerably improved [Harmony]
[src/mame/includes]n64.h
[src/mame/machine]n64.c
[src/mame/video]n64.c

src/mame/machine/n64.c
r6173r6174
22
33#include "driver.h"
44#include "cpu/mips/mips3.h"
5#include "cpu/mips/mips3com.h"
56#include "streams.h"
67#include "includes/n64.h"
78#include "sound/dmadac.h"
r6173r6174
3839      cputag_set_input_line(machine, "maincpu", INPUT_LINE_IRQ0, CLEAR_LINE);
3940   }
4041}
42UINT8 is64_buffer[0x10000];
4143
44READ32_HANDLER( n64_is64_r )
45{
46    switch(offset)
47    {
48        case 0x0000/4:
49            return 0x49533634;
50
51        case 0x0004/4:
52        case 0x0008/4:
53        case 0x000c/4:
54        case 0x0010/4:
55        case 0x0014/4:
56        case 0x0018/4:
57        case 0x001c/4:
58            return 0;
59
60        default:
61            return ( is64_buffer[(offset << 2) + 0] << 24 ) |
62                   ( is64_buffer[(offset << 2) + 1] << 16 ) |
63                   ( is64_buffer[(offset << 2) + 2] <<  8 ) |
64                   ( is64_buffer[(offset << 2) + 3] <<  0 );
65    }
66}
67
68WRITE32_HANDLER( n64_is64_w )
69{
70    int i = 0;
71
72    switch(offset)
73    {
74        case 0x0014/4:
75            for(i = 0x20; i < (0x20 + data); i++)
76            {
77                printf( "%c", is64_buffer[i] );
78                if(is64_buffer[i] == 0x0a)
79                {
80                    printf( "%c", 0x0d );
81                }
82                is64_buffer[i] = 0;
83            }
84            break;
85
86        default:
87            is64_buffer[(offset << 2) + 0] = (data >> 24) & 0x000000ff;
88            is64_buffer[(offset << 2) + 1] = (data >> 16) & 0x000000ff;
89            is64_buffer[(offset << 2) + 2] = (data >>  8) & 0x000000ff;
90            is64_buffer[(offset << 2) + 3] = (data >>  0) & 0x000000ff;
91            break;
92    }
93}
94
95READ32_HANDLER( n64_open_r )
96{
97    UINT32 retval = (offset << 2) & 0x0000ffff;
98    retval = (retval << 16) | retval;
99    return retval;
100}
101
102WRITE32_HANDLER( n64_open_w )
103{
104    // Do nothing
105}
106
107// RDRAM registers
108static UINT32 rdram_config;
109static UINT32 rdram_device_id;
110static UINT32 rdram_delay;
111static UINT32 rdram_mode;
112static UINT32 rdram_ref_interval;
113static UINT32 rdram_ref_row;
114static UINT32 rdram_ras_interval;
115static UINT32 rdram_min_interval;
116static UINT32 rdram_addr_select;
117static UINT32 rdram_device_manuf;
118
119READ32_HANDLER( n64_rdram_reg_r )
120{
121    switch (offset)
122    {
123        case 0x00/4:            // RDRAM_CONFIG_REG / RDRAM_DEVICE_TYPE_REG
124            return rdram_config;
125
126        case 0x04/4:            // RDRAM_CONFIG_REG / RDRAM_DEVICE_TYPE_REG
127            return rdram_device_id;
128
129        case 0x08/4:            // RDRAM_CONFIG_REG / RDRAM_DEVICE_TYPE_REG
130            return rdram_delay;
131
132        case 0x0c/4:            // RDRAM_CONFIG_REG / RDRAM_DEVICE_TYPE_REG
133            return rdram_mode;
134
135        case 0x10/4:            // RDRAM_CONFIG_REG / RDRAM_DEVICE_TYPE_REG
136            return rdram_ref_interval;
137
138        case 0x14/4:            // RDRAM_CONFIG_REG / RDRAM_DEVICE_TYPE_REG
139            return rdram_ref_row;
140
141        case 0x18/4:            // RDRAM_CONFIG_REG / RDRAM_DEVICE_TYPE_REG
142            return rdram_ras_interval;
143
144        case 0x1c/4:            // RDRAM_CONFIG_REG / RDRAM_DEVICE_TYPE_REG
145            return rdram_min_interval;
146
147        case 0x20/4:            // RDRAM_CONFIG_REG / RDRAM_DEVICE_TYPE_REG
148            return rdram_addr_select;
149
150        case 0x24/4:            // RDRAM_CONFIG_REG / RDRAM_DEVICE_TYPE_REG
151            return rdram_device_manuf;
152
153        default:
154            logerror("rdram_reg_r: %08X, %08X at %08X\n", offset, mem_mask, cpu_get_pc(space->cpu));
155            break;
156    }
157    return 0;
158}
159
160WRITE32_HANDLER( n64_rdram_reg_w )
161{
162    switch (offset)
163    {
164        case 0x00/4:            // RDRAM_CONFIG_REG / RDRAM_DEVICE_TYPE_REG
165            rdram_config = data;
166            break;
167
168        case 0x04/4:            // RDRAM_CONFIG_REG / RDRAM_DEVICE_TYPE_REG
169            rdram_device_id = data;
170            break;
171
172        case 0x08/4:            // RDRAM_CONFIG_REG / RDRAM_DEVICE_TYPE_REG
173            rdram_delay = data;
174            break;
175
176        case 0x0c/4:            // RDRAM_CONFIG_REG / RDRAM_DEVICE_TYPE_REG
177            rdram_mode = data;
178            break;
179
180        case 0x10/4:            // RDRAM_CONFIG_REG / RDRAM_DEVICE_TYPE_REG
181            rdram_ref_interval = data;
182            break;
183
184        case 0x14/4:            // RDRAM_CONFIG_REG / RDRAM_DEVICE_TYPE_REG
185            rdram_ref_row = data;
186            break;
187
188        case 0x18/4:            // RDRAM_CONFIG_REG / RDRAM_DEVICE_TYPE_REG
189            rdram_ras_interval = data;
190            break;
191
192        case 0x1c/4:            // RDRAM_CONFIG_REG / RDRAM_DEVICE_TYPE_REG
193            rdram_min_interval = data;
194            break;
195
196        case 0x20/4:            // RDRAM_CONFIG_REG / RDRAM_DEVICE_TYPE_REG
197            rdram_addr_select = data;
198            break;
199
200        case 0x24/4:            // RDRAM_CONFIG_REG / RDRAM_DEVICE_TYPE_REG
201            rdram_device_manuf = data;
202            break;
203
204        default:
205            logerror("mi_reg_w: %08X, %08X, %08X at %08X\n", data, offset, mem_mask, cpu_get_pc(space->cpu));
206            break;
207    }
208}
209
210// Memory Interface (MI)
211
212UINT32 mi_mode;
213
214#define MI_CLR_INIT             0x0080      /* Bit  7: clear init mode */
215#define MI_SET_INIT             0x0100      /* Bit  8: set init mode */
216#define MI_CLR_EBUS             0x0200      /* Bit  9: clear ebus test */
217#define MI_SET_EBUS             0x0400      /* Bit 10: set ebus test mode */
218#define MI_CLR_DP_INTR          0x0800      /* Bit 11: clear dp interrupt */
219#define MI_CLR_RDRAM            0x1000      /* Bit 12: clear RDRAM reg */
220#define MI_SET_RDRAM            0x2000      /* Bit 13: set RDRAM reg mode */
221#define MI_MODE_INIT            0x0080      /* Bit  7: init mode */
222#define MI_MODE_EBUS            0x0100      /* Bit  8: ebus test mode */
223#define MI_MODE_RDRAM           0x0200      /* Bit  9: RDRAM reg mode */
224
42225READ32_HANDLER( n64_mi_reg_r )
43226{
44227   switch (offset)
45228   {
229        case 0x00/4:            // MI_MODE_REG
230            return mi_mode;
231
46232      case 0x04/4:         // MI_VERSION_REG
47233         return mi_version;
48234
r6173r6174
65251   switch (offset)
66252   {
67253      case 0x00/4:      // MI_INIT_MODE_REG
68         if (data & 0x0800)
254            if (data & MI_CLR_INIT)     mi_mode &= ~MI_MODE_INIT;
255            if (data & MI_SET_INIT)     mi_mode |=  MI_MODE_INIT;
256            if (data & MI_CLR_EBUS)     mi_mode &= ~MI_MODE_EBUS;
257            if (data & MI_SET_EBUS)     mi_mode |=  MI_MODE_EBUS;
258            if (data & MI_CLR_RDRAM)    mi_mode &= ~MI_MODE_RDRAM;
259            if (data & MI_SET_RDRAM)    mi_mode |=  MI_MODE_RDRAM;
260            if (data & MI_CLR_DP_INTR)
69261         {
70262            clear_rcp_interrupt(space->machine, DP_INTERRUPT);
71263         }
r6173r6174
77269
78270      case 0x0c/4:      // MI_INTR_MASK_REG
79271      {
80         if (data & 0x0001) mi_intr_mask &= ~0x1;      // clear SP mask
81         if (data & 0x0002) mi_intr_mask |= 0x1;         // set SP mask
82         if (data & 0x0004) mi_intr_mask &= ~0x2;      // clear SI mask
83         if (data & 0x0008) mi_intr_mask |= 0x2;         // set SI mask
84         if (data & 0x0010) mi_intr_mask &= ~0x4;      // clear AI mask
85         if (data & 0x0020) mi_intr_mask |= 0x4;         // set AI mask
86         if (data & 0x0040) mi_intr_mask &= ~0x8;      // clear VI mask
87         if (data & 0x0080) mi_intr_mask |= 0x8;         // set VI mask
88         if (data & 0x0100) mi_intr_mask &= ~0x10;      // clear PI mask
89         if (data & 0x0200) mi_intr_mask |= 0x10;      // set PI mask
90         if (data & 0x0400) mi_intr_mask &= ~0x20;      // clear DP mask
91         if (data & 0x0800) mi_intr_mask |= 0x20;      // set DP mask
272            if (data & 0x0001)
273            {
274                mi_intr_mask &= ~0x1;      // clear SP mask
275            }
276            if (data & 0x0002)
277            {
278                mi_intr_mask |= 0x1;           // set SP mask
279            }
280            if (data & 0x0004)
281            {
282                mi_intr_mask &= ~0x2;      // clear SI mask
283            }
284            if (data & 0x0008)
285            {
286                mi_intr_mask |= 0x2;           // set SI mask
287            }
288            if (data & 0x0010)
289            {
290                mi_intr_mask &= ~0x4;      // clear AI mask
291            }
292            if (data & 0x0020)
293            {
294                mi_intr_mask |= 0x4;           // set AI mask
295            }
296            if (data & 0x0040)
297            {
298                mi_intr_mask &= ~0x8;      // clear VI mask
299            }
300            if (data & 0x0080)
301            {
302                mi_intr_mask |= 0x8;           // set VI mask
303            }
304            if (data & 0x0100)
305            {
306                mi_intr_mask &= ~0x10;     // clear PI mask
307            }
308            if (data & 0x0200)
309            {
310                mi_intr_mask |= 0x10;      // set PI mask
311            }
312            if (data & 0x0400)
313            {
314                mi_intr_mask &= ~0x20;     // clear DP mask
315            }
316            if (data & 0x0800)
317            {
318                mi_intr_mask |= 0x20;      // set DP mask
319            }
92320         break;
93321      }
94322
r6173r6174
128356   {
129357      //fatalerror("sp_dma (%s): sp_dma_length unaligned %08X\n", cpu ? "RSP" : "R4300i", sp_dma_length);
130358      //sp_dma_length = sp_dma_length & ~3;
131        sp_dma_length = (sp_dma_length + 3) & ~3;
359        sp_dma_length = (sp_dma_length + 7) & ~7;
132360
133361      //sp_dma_length &= ~3;
134362   }
r6173r6174
156384
157385   if ((sp_mem_addr & 0xfff) + (sp_dma_length) > 0x1000)
158386   {
159      fatalerror("sp_dma: dma out of memory area: %08X, %08X\n", sp_mem_addr, sp_dma_length);
387      printf("sp_dma: dma out of memory area: %08X, %08X\n", sp_mem_addr, sp_dma_length);
388      //fatalerror("sp_dma: dma out of memory area: %08X, %08X\n", sp_mem_addr, sp_dma_length);
389      sp_dma_length = 0x1000 - (sp_mem_addr & 0xfff);
160390   }
161391
162392   if (direction == 0)      // RDRAM -> I/DMEM
r6173r6174
242472         return 0;
243473
244474      case 0x1c/4:      // SP_SEMAPHORE_REG
245         return sp_semaphore;
475            if( sp_semaphore )
476            {
477                return 0;
478            }
479            else
480            {
481                sp_semaphore = 1;
482                return 1;
483            }
246484
247485        case 0x20/4:        // DP_CMD_START
248486        case 0x24/4:        // DP_CMD_END
r6173r6174
443681            }
444682
445683         case 0x1c/4:      // SP_SEMAPHORE_REG
446            sp_semaphore = data;
684                sp_semaphore = 0;
447685      //      mame_printf_debug("sp_semaphore = %08X\n", sp_semaphore);
448686            break;
449687
r6173r6174
555793UINT32 n64_vi_width; // This needs to be non-static
556794UINT32 n64_vi_origin;
557795UINT32 n64_vi_control;
796UINT32 n64_vi_blank;
558797static UINT32 n64_vi_burst, n64_vi_vsync,  n64_vi_hsync,  n64_vi_leap,  n64_vi_hstart, n64_vi_vstart;
559798static UINT32 n64_vi_intr,  n64_vi_vburst, n64_vi_xscale, n64_vi_yscale;
560799
800
801static void n64_vi_recalculate_resolution(running_machine *machine)
802{
803    int x_start = (n64_vi_hstart & 0x03ff0000) >> 16;
804    int x_end = n64_vi_hstart & 0x000003ff;
805    int y_start = ((n64_vi_vstart & 0x03ff0000) >> 16) / 2;
806    int y_end = (n64_vi_vstart & 0x000003ff) / 2;
807    int width = ((n64_vi_xscale & 0x00000fff) * (x_end - x_start)) / 0x400;
808    int height = ((n64_vi_yscale & 0x00000fff) * (y_end - y_start)) / 0x400;
809    rectangle visarea = *video_screen_get_visible_area(machine->primary_screen);
810    attoseconds_t period = video_screen_get_frame_period(machine->primary_screen).attoseconds;
811
812    if (width == 0 || height == 0)
813    {
814        n64_vi_blank = 1;
815    }
816    else
817    {
818        n64_vi_blank = 0;
819    }
820
821    if (width == 0)
822        width = 1;
823
824    if (height == 0)
825        height = 1;
826
827    if (width > 640)
828        width = 640;
829
830    if (height > 480)
831        height = 480;
832
833    visarea.max_x = width - 1;
834    visarea.max_y = height - 1;
835    video_screen_configure(machine->primary_screen, width, 525, &visarea, period);
836}
837
561838READ32_HANDLER( n64_vi_reg_r )
562839{
563840   switch (offset)
r6173r6174
615892      case 0x00/4:      // VI_CONTROL_REG
616893            if ((n64_vi_control & 0x40) != (data & 0x40))
617894         {
618            int width = video_screen_get_width(space->machine->primary_screen);
619            rectangle visarea = *video_screen_get_visible_area(space->machine->primary_screen);
620            attoseconds_t period = video_screen_get_frame_period(space->machine->primary_screen).attoseconds;
621
622            visarea.max_y = (data & 0x40) ? 479 : 239;
623            video_screen_configure(space->machine->primary_screen, width, visarea.max_y + 1, &visarea, period);
895                n64_vi_recalculate_resolution(space->machine);
624896         }
625897            n64_vi_control = data;
626898         break;
r6173r6174
632904      case 0x08/4:      // VI_WIDTH_REG
633905            if (n64_vi_width != data && data > 0)
634906         {
635            int height = video_screen_get_height(space->machine->primary_screen);
636            rectangle visarea = *video_screen_get_visible_area(space->machine->primary_screen);
637            attoseconds_t period = video_screen_get_frame_period(space->machine->primary_screen).attoseconds;
638
639            visarea.max_x = data-1;
640            video_screen_configure(space->machine->primary_screen, visarea.max_x + 1, height, &visarea, period);
907                n64_vi_recalculate_resolution(space->machine);
641908         }
642909            n64_vi_width = data;
643910          fb_width = data;
r6173r6174
669936
670937      case 0x24/4:      // VI_H_START_REG
671938            n64_vi_hstart = data;
939            n64_vi_recalculate_resolution(space->machine);
672940         break;
673941
674942      case 0x28/4:      // VI_V_START_REG
675943            n64_vi_vstart = data;
944            n64_vi_recalculate_resolution(space->machine);
676945         break;
677946
678947      case 0x2c/4:      // VI_V_BURST_REG
r6173r6174
681950
682951      case 0x30/4:      // VI_X_SCALE_REG
683952            n64_vi_xscale = data;
953            n64_vi_recalculate_resolution(space->machine);
684954         break;
685955
686956      case 0x34/4:      // VI_Y_SCALE_REG
687957            n64_vi_yscale = data;
958            n64_vi_recalculate_resolution(space->machine);
688959         break;
689960
690961        /*
r6173r6174
9181189
9191190static UINT32 pi_dram_addr, pi_cart_addr;
9201191static UINT32 pi_first_dma = 1;
1192static UINT32 pi_rd_len = 0;
1193static UINT32 pi_wr_len = 0;
1194static UINT32 pi_status = 0;
1195static UINT32 pi_bsd_dom1_lat = 0;
1196static UINT32 pi_bsd_dom1_pwd = 0;
1197static UINT32 pi_bsd_dom1_pgs = 0;
1198static UINT32 pi_bsd_dom1_rls = 0;
1199static UINT32 pi_bsd_dom2_lat = 0;
1200static UINT32 pi_bsd_dom2_pwd = 0;
1201static UINT32 pi_bsd_dom2_pgs = 0;
1202static UINT32 pi_bsd_dom2_rls = 0;
9211203
9221204READ32_HANDLER( n64_pi_reg_r )
9231205{
r6173r6174
9301212         return pi_cart_addr;
9311213
9321214      case 0x10/4:      // PI_STATUS_REG
933         return 0;
1215         return pi_status;
9341216
1217        case 0x14/4:        // PI_BSD_DOM1_LAT
1218            return pi_bsd_dom1_lat;
1219
1220        case 0x18/4:        // PI_BSD_DOM1_PWD
1221            return pi_bsd_dom1_pwd;
1222
1223        case 0x1c/4:        // PI_BSD_DOM1_PGS
1224            return pi_bsd_dom1_pgs;
1225
1226        case 0x20/4:        // PI_BSD_DOM1_RLS
1227            return pi_bsd_dom1_rls;
1228
1229        case 0x24/4:        // PI_BSD_DOM2_LAT
1230            return pi_bsd_dom2_lat;
1231
1232        case 0x28/4:        // PI_BSD_DOM2_PWD
1233            return pi_bsd_dom2_pwd;
1234
1235        case 0x2c/4:        // PI_BSD_DOM2_PGS
1236            return pi_bsd_dom2_pgs;
1237
1238        case 0x30/4:        // PI_BSD_DOM2_RLS
1239            return pi_bsd_dom2_rls;
1240
9351241      default:
9361242         logerror("pi_reg_r: %08X, %08X at %08X\n", offset, mem_mask, cpu_get_pc(space->cpu));
9371243         break;
r6173r6174
9591265      {
9601266         int i;
9611267         UINT32 dma_length = (data + 1);
1268            pi_rd_len = data;
9621269
9631270         /*if (dma_length & 3)
9641271            {
r6173r6174
9861293      {
9871294         int i;
9881295         UINT32 dma_length = (data + 1);
1296            pi_wr_len = data;
9891297
9901298         if (dma_length & 3)
9911299            {
r6173r6174
10311339         break;
10321340      }
10331341
1342        case 0x14/4:        // PI_BSD_DOM1_LAT
1343            pi_bsd_dom1_lat = data;
1344            break;
1345
1346        case 0x18/4:        // PI_BSD_DOM1_PWD
1347            pi_bsd_dom1_pwd = data;
1348            break;
1349
1350        case 0x1c/4:        // PI_BSD_DOM1_PGS
1351            pi_bsd_dom1_pgs = data;
1352            break;
1353
1354        case 0x20/4:        // PI_BSD_DOM1_RLS
1355            pi_bsd_dom1_rls = data;
1356            break;
1357
1358        case 0x24/4:        // PI_BSD_DOM2_LAT
1359            pi_bsd_dom2_lat = data;
1360            break;
1361
1362        case 0x28/4:        // PI_BSD_DOM2_PWD
1363            pi_bsd_dom2_pwd = data;
1364            break;
1365
1366        case 0x2c/4:        // PI_BSD_DOM2_PGS
1367            pi_bsd_dom2_pgs = data;
1368            break;
1369
1370        case 0x30/4:        // PI_BSD_DOM2_RLS
1371            pi_bsd_dom2_rls = data;
1372            break;
1373
10341374      default:
10351375         logerror("pi_reg_w: %08X, %08X, %08X at %08X\n", data, offset, mem_mask, cpu_get_pc(space->cpu));
10361376         break;
r6173r6174
10381378}
10391379
10401380// RDRAM Interface
1381UINT32 ri_mode = 0;
1382UINT32 ri_config = 0;
1383UINT32 ri_current_load = 0;
1384UINT32 ri_select = 0;
1385UINT32 ri_count = 0;
1386UINT32 ri_latency = 0;
1387UINT32 ri_rerror = 0;
1388UINT32 ri_werror = 0;
1389
10411390READ32_HANDLER( n64_ri_reg_r )
10421391{
1392    //printf( "n64_ri_reg_r: 0x%02x/4 (%08x)\n", offset << 2, mem_mask );
10431393   switch (offset)
10441394   {
1395        case 0x00/4: // RI_MODE_REG
1396            return ri_mode;
1397
1398        case 0x04/4: // RI_CONFIG_REG
1399            return ri_config;
1400
1401        case 0x08/4: // RI_CURRENT_LOAD_REG
1402            return ri_current_load;
1403
1404        case 0x0c/4: // RI_SELECT_REG
1405            return ri_select;
1406
1407        case 0x10/4: // RI_COUNT_REG
1408            return ri_count;
1409
1410        case 0x14/4: // RI_LATENCY_REG
1411            return ri_latency;
1412
1413        case 0x18/4: // RI_RERROR_REG
1414            return ri_rerror;
1415
1416        case 0x1c/4: // RI_WERROR_REG
1417            return ri_werror;
1418
10451419      default:
10461420         logerror("ri_reg_r: %08X, %08X at %08X\n", offset, mem_mask, cpu_get_pc(space->cpu));
10471421         break;
r6173r6174
10521426
10531427WRITE32_HANDLER( n64_ri_reg_w )
10541428{
1429    //printf( "n64_ri_reg_w: 0x%02x/4 = %08x (%08x)\n", offset << 2, data, mem_mask );
10551430   switch (offset)
10561431   {
1432        case 0x00/4: // RI_MODE_REG
1433            ri_mode = data;
1434            break;
1435
1436        case 0x04/4: // RI_CONFIG_REG
1437            ri_config = data;
1438            break;
1439
1440        case 0x08/4: // RI_CURRENT_LOAD_REG
1441            ri_current_load = data;
1442            break;
1443
1444        case 0x0c/4: // RI_SELECT_REG
1445            ri_select = data;
1446            break;
1447
1448        case 0x10/4: // RI_COUNT_REG
1449            ri_count = data;
1450            break;
1451
1452        case 0x14/4: // RI_LATENCY_REG
1453            ri_latency = data;
1454            break;
1455
1456        case 0x18/4: // RI_RERROR_REG
1457            ri_rerror = data;
1458            break;
1459
1460        case 0x1c/4: // RI_WERROR_REG
1461            ri_werror = data;
1462            break;
1463
10571464      default:
10581465         logerror("ri_reg_w: %08X, %08X, %08X at %08X\n", data, offset, mem_mask, cpu_get_pc(space->cpu));
10591466         break;
r6173r6174
10651472static UINT8 pif_cmd[0x40];
10661473static UINT32 si_dram_addr = 0;
10671474static UINT32 si_pif_addr = 0;
1475static UINT32 si_pif_addr_rd64b = 0;
1476static UINT32 si_pif_addr_wr64b = 0;
10681477static UINT32 si_status = 0;
10691478
10701479static UINT8 eeprom[512];
r6173r6174
13171726         }
13181727         //mame_printf_debug("\n");
13191728
1320         rdata[0] = 0;
1729         //rdata[0] = 0;
13211730
13221731         return 1;
13231732      }
r6173r6174
14831892{
14841893   switch (offset)
14851894   {
1486      case 0x00/4:      // SI_DRAM_ADDR_REG
1487         return si_dram_addr;
1895      //case 0x00/4:      // SI_DRAM_ADDR_REG
1896         //return si_dram_addr;
14881897
14891898      case 0x18/4:      // SI_STATUS_REG
14901899         return si_status;
r6173r6174
15041913      case 0x04/4:      // SI_PIF_ADDR_RD64B_REG
15051914         // PIF RAM -> RDRAM
15061915         si_pif_addr = data;
1916            si_pif_addr_rd64b = data;
15071917            pif_dma(space->machine, 0);
15081918         break;
15091919
15101920      case 0x10/4:      // SI_PIF_ADDR_WR64B_REG
15111921         // RDRAM -> PIF RAM
15121922         si_pif_addr = data;
1923            si_pif_addr_wr64b = data;
15131924            pif_dma(space->machine, 1);
15141925         break;
15151926
r6173r6174
15291940READ32_HANDLER( n64_pif_ram_r )
15301941{
15311942    /*mame_printf_debug( "pif_ram_r: %08X, %08X = %08X\n", offset << 2, mem_mask, ( ( pif_ram[offset*4+0] << 24 ) | ( pif_ram[offset*4+1] << 16 ) | ( pif_ram[offset*4+2] <<  8 ) | ( pif_ram[offset*4+3] <<  0 ) ) & mem_mask );*/
1532    if( offset == ( 0x24 / 4 ) )
1943    if(!space->debugger_access)
15331944    {
1534        cic_status = 0x00000080;
1535    }
1536    if( offset == ( 0x3C / 4 ) )
1537    {
1538        return cic_status;
1539    }
1945       if( offset == ( 0x24 / 4 ) )
1946       {
1947           cic_status = 0x00000080;
1948       }
1949       if( offset == ( 0x3C / 4 ) )
1950       {
1951           return cic_status;
1952       }
1953   }
15401954    return ( ( pif_ram[offset*4+0] << 24 ) | ( pif_ram[offset*4+1] << 16 ) | ( pif_ram[offset*4+2] <<  8 ) | ( pif_ram[offset*4+3] <<  0 ) ) & mem_mask;
15411955}
15421956
15431957WRITE32_HANDLER( n64_pif_ram_w )
15441958{
15451959    /*mame_printf_debug("pif_ram_w: %08X, %08X, %08X\n", data, offset << 4, mem_mask);*/
1546    /*
15471960    if( mem_mask & 0xff000000 )
15481961    {
15491962        pif_ram[offset*4+0] = ( data >> 24 ) & 0x000000ff;
r6173r6174
15601973    {
15611974        pif_ram[offset*4+3] = ( data >>  0 ) & 0x000000ff;
15621975    }
1563    */
15641976
15651977    signal_rcp_interrupt(space->machine, SI_INTERRUPT);
15661978}
r6173r6174
16462058    if (boot_checksum == U64(0x000000d057e84864))
16472059    {
16482060        // CIC-NUS-6101
1649        mame_printf_debug("CIC-NUS-6101 detected\n");
2061        printf("CIC-NUS-6102 detected\n");
2062        pif_ram[0x24] = 0x00;
2063        pif_ram[0x25] = 0x02;
2064        pif_ram[0x26] = 0x3f;
2065        pif_ram[0x27] = 0x3f;
16502066        // crc_seed = 0x3f;
2067    }
2068    else if (boot_checksum == U64(0x000000cffb830843) || boot_checksum == U64(0x000000d0027fdf31))
2069    {
2070        // CIC-NUS-6103
2071        printf("CIC-NUS-6101 detected\n");
2072        // crc_seed = 0x78;
16512073        pif_ram[0x24] = 0x00;
1652        pif_ram[0x25] = 0x04;
2074        pif_ram[0x25] = 0x06;
16532075        pif_ram[0x26] = 0x3f;
16542076        pif_ram[0x27] = 0x3f;
16552077    }
16562078    else if (boot_checksum == U64(0x000000d6499e376b))
16572079    {
16582080        // CIC-NUS-6103
1659        mame_printf_debug("CIC-NUS-6103 detected\n");
2081        printf("CIC-NUS-6103 detected\n");
16602082        // crc_seed = 0x78;
16612083        pif_ram[0x24] = 0x00;
1662        pif_ram[0x25] = 0x04;
2084        pif_ram[0x25] = 0x02;
16632085        pif_ram[0x26] = 0x78;
1664        pif_ram[0x27] = 0x78;
2086        pif_ram[0x27] = 0x3f;
16652087    }
16662088    else if (boot_checksum == U64(0x0000011a4a1604b6))
16672089    {
16682090        // CIC-NUS-6105
1669        mame_printf_debug("CIC-NUS-6105 detected\n");
2091        printf("CIC-NUS-6105 detected\n");
16702092        // crc_seed = 0x91;
16712093
16722094        // first_rsp = 0;
16732095        pif_ram[0x24] = 0x00;
1674        pif_ram[0x25] = 0x04;
2096        pif_ram[0x25] = 0x02;
16752097        pif_ram[0x26] = 0x91;
1676        pif_ram[0x27] = 0x91;
2098        pif_ram[0x27] = 0x3f;
16772099    }
16782100    else if (boot_checksum == U64(0x000000d6d5de4ba0))
16792101    {
16802102        // CIC-NUS-6106
1681        mame_printf_debug("CIC-NUS-6106 detected\n");
2103        printf("CIC-NUS-6106 detected\n");
16822104        // crc_seed = 0x85;
16832105        pif_ram[0x24] = 0x00;
1684        pif_ram[0x25] = 0x04;
2106        pif_ram[0x25] = 0x02;
16852107        pif_ram[0x26] = 0x85;
1686        pif_ram[0x27] = 0x85;
2108        pif_ram[0x27] = 0x3f;
16872109    }
16882110    else
16892111    {
1690        mame_printf_debug("Unknown BootCode Checksum %08X%08X\n", (UINT32)(boot_checksum>>32),(UINT32)(boot_checksum));
2112        printf("Unknown BootCode Checksum %08X%08X\n", (UINT32)(boot_checksum>>32),(UINT32)(boot_checksum));
16912113    }
1692
1693    /*
1694    // bootcode differs between CIC-chips, so we can use its checksum to detect the CIC-chip
1695    boot_checksum = 0;
1696    for (i=0x40; i < 0x1000; i+=4)
1697    {
1698        boot_checksum += cart[i/4]+i;
1699    }
1700
1701    if (boot_checksum == U64(0x000000d057e84864))
1702    {
1703        // CIC-NUS-6101
1704        mame_printf_debug("CIC-NUS-6101 detected\n");
1705        crc_seed = 0x3f;
1706    }
1707    else if (boot_checksum == U64(0x000000d6499e376b))
1708    {
1709        // CIC-NUS-6103
1710        mame_printf_debug("CIC-NUS-6103 detected\n");
1711        crc_seed = 0x78;
1712    }
1713    else if (boot_checksum == U64(0x0000011a4a1604b6))
1714    {
1715        // CIC-NUS-6105
1716        mame_printf_debug("CIC-NUS-6105 detected\n");
1717        crc_seed = 0x91;
1718
1719        first_rsp = 0;
1720    }
1721    else if (boot_checksum == U64(0x000000d6d5de4ba0))
1722    {
1723        // CIC-NUS-6106
1724        mame_printf_debug("CIC-NUS-6106 detected\n");
1725        crc_seed = 0x85;
1726    }
1727    else
1728    {
1729        mame_printf_debug("Unknown BootCode Checksum %08X%08X\n", (UINT32)(boot_checksum>>32),(UINT32)(boot_checksum));
1730    }
1731
1732    // The PIF Boot ROM is not dumped, the following code simulates it
1733
1734    // clear all registers
1735    for (i=1; i < 32; i++)
1736    {
1737        *pif_rom++ = 0x00000000 | 0 << 21 | 0 << 16 | i << 11 | 0x20;       // ADD ri, r0, r0
1738    }
1739
1740    // R20 <- 0x00000001
1741    *pif_rom++ = 0x34000000 | 20 << 16 | 0x0001;                    // ORI r20, r0, 0x0001
1742
1743    // R22 <- 0x0000003F
1744    *pif_rom++ = 0x34000000 | 22 << 16 | crc_seed;                  // ORI r22, r0, 0x003f
1745
1746    // R29 <- 0xA4001FF0
1747    *pif_rom++ = 0x3c000000 | 29 << 16 | 0xa400;                    // LUI r29, 0xa400
1748    *pif_rom++ = 0x34000000 | 29 << 21 | 29 << 16 | 0x1ff0;         // ORI r29, r29, 0x1ff0
1749
1750    // clear CP0 registers
1751    for (i=0; i < 32; i++)
1752    {
1753        *pif_rom++ = 0x40000000 | 4 << 21 | 0 << 16 | i << 11;      // MTC2 cp0ri, r0
1754    }
1755
1756    // Random <- 0x0000001F
1757    *pif_rom++ = 0x34000000 | 1 << 16 | 0x001f;
1758    *pif_rom++ = 0x40000000 | 4 << 21 | 1 << 16 | 1 << 11;          // MTC2 Random, r1
1759
1760    // Status <- 0x70400004
1761    *pif_rom++ = 0x3c000000 | 1 << 16 | 0x7040;                     // LUI r1, 0x7040
1762    *pif_rom++ = 0x34000000 | 1 << 21 | 1 << 16 | 0x0004;           // ORI r1, r1, 0x0004
1763    *pif_rom++ = 0x40000000 | 4 << 21 | 1 << 16 | 12 << 11;         // MTC2 Status, r1
1764
1765    // PRId <- 0x00000B00
1766    *pif_rom++ = 0x34000000 | 1 << 16 | 0x0b00;                     // ORI r1, r0, 0x0b00
1767    *pif_rom++ = 0x40000000 | 4 << 21 | 1 << 16 | 15 << 11;         // MTC2 PRId, r1
1768
1769    // Config <- 0x0006E463
1770    *pif_rom++ = 0x3c000000 | 1 << 16 | 0x0006;                     // LUI r1, 0x0006
1771    *pif_rom++ = 0x34000000 | 1 << 21 | 1 << 16 | 0xe463;           // ORI r1, r1, 0xe463
1772    *pif_rom++ = 0x40000000 | 4 << 21 | 1 << 16 | 16 << 11;         // MTC2 Config, r1
1773
1774    // (0xa4300004) <- 0x01010101
1775    *pif_rom++ = 0x3c000000 | 1 << 16 | 0x0101;                     // LUI r1, 0x0101
1776    *pif_rom++ = 0x34000000 | 1 << 21 | 1 << 16 | 0x0101;           // ORI r1, r1, 0x0101
1777    *pif_rom++ = 0x3c000000 | 3 << 16 | 0xa430;                     // LUI r3, 0xa430
1778    *pif_rom++ = 0xac000000 | 3 << 21 | 1 << 16 | 0x0004;           // SW r1, 0x0004(r3)
1779
1780    // Copy 0xb0000000...1fff -> 0xa4000000...1fff
1781    *pif_rom++ = 0x34000000 | 3 << 16 | 0x0400;                     // ORI r3, r0, 0x0400
1782    *pif_rom++ = 0x3c000000 | 4 << 16 | 0xb000;                     // LUI r4, 0xb000
1783    *pif_rom++ = 0x3c000000 | 5 << 16 | 0xa400;                     // LUI r5, 0xa400
1784    *pif_rom++ = 0x8c000000 | 4 << 21 | 1 << 16;                    // LW r1, 0x0000(r4)
1785    *pif_rom++ = 0xac000000 | 5 << 21 | 1 << 16;                    // SW r1, 0x0000(r5)
1786    *pif_rom++ = 0x20000000 | 4 << 21 | 4 << 16 | 0x0004;           // ADDI r4, r4, 0x0004
1787    *pif_rom++ = 0x20000000 | 5 << 21 | 5 << 16 | 0x0004;           // ADDI r5, r5, 0x0004
1788    *pif_rom++ = 0x20000000 | 3 << 21 | 3 << 16 | 0xffff;           // ADDI r3, r3, -1
1789    *pif_rom++ = 0x14000000 | 3 << 21 | 0 << 16 | 0xfffa;           // BNE r3, r0, -6
1790    *pif_rom++ = 0x00000000;
1791
1792    *pif_rom++ = 0x34000000 | 3 << 16 | 0x0000;                     // ORI r3, r0, 0x0000
1793    *pif_rom++ = 0x34000000 | 4 << 16 | 0x0000;                     // ORI r4, r0, 0x0000
1794    *pif_rom++ = 0x34000000 | 5 << 16 | 0x0000;                     // ORI r5, r0, 0x0000
1795
1796    // Zelda and DK64 need these
1797    *pif_rom++ = 0x3c000000 | 9 << 16 | 0xa400;
1798    *pif_rom++ = 0x34000000 | 9 << 21 | 9 << 16 | 0x1ff0;
1799    *pif_rom++ = 0x3c000000 | 11 << 16 | 0xa400;
1800    *pif_rom++ = 0x3c000000 | 31 << 16 | 0xffff;
1801    *pif_rom++ = 0x34000000 | 31 << 21 | 31 << 16 | 0xffff;
1802
1803    *pif_rom++ = 0x3c000000 | 1 << 16 | 0xa400;                     // LUI r1, 0xa400
1804    *pif_rom++ = 0x34000000 | 1 << 21 | 1 << 16 | 0x0040;           // ORI r1, r1, 0x0040
1805    *pif_rom++ = 0x00000000 | 1 << 21 | 0x8;                        // JR r1
1806    */
18072114}
src/mame/includes/n64.h
r6173r6174
5858extern UINT32 n64_vi_width;
5959extern UINT32 n64_vi_origin;
6060extern UINT32 n64_vi_control;
61extern UINT32 n64_vi_blank;
6162
6263extern void dp_full_sync(running_machine *machine);
6364extern void signal_rcp_interrupt(running_machine *machine, int interrupt);
r6173r6174
6566
6667
6768/* read/write handlers for the N64 subsystems */
69extern READ32_HANDLER( n64_is64_r );
70extern WRITE32_HANDLER( n64_is64_w );
71extern READ32_HANDLER( n64_open_r );
72extern WRITE32_HANDLER( n64_open_w );
73extern READ32_HANDLER( n64_rdram_reg_r );
74extern WRITE32_HANDLER( n64_rdram_reg_w );
6875extern READ32_HANDLER( n64_mi_reg_r );
6976extern WRITE32_HANDLER( n64_mi_reg_w );
7077extern READ32_HANDLER( n64_sp_reg_r );
src/mame/video/n64.c
r6173r6174
1313static int rdp_cmd_ptr = 0;
1414static int rdp_cmd_cur = 0;
1515
16#define RDP_CVG_SPAN_MAX    1024
17
1618typedef struct
1719{
1820   int lx, rx;
r6173r6174
2426   int b, db;
2527   int a, da;
2628   int z, dz;
29    UINT8 cvg[RDP_CVG_SPAN_MAX];
2730} SPAN;
2831
29static SPAN span[1024];
32UINT8 cvg_mem[8*1048576];
3033
34static SPAN span[4096];
35
3136/*****************************************************************************/
3237
3338#define BYTE_ADDR_XOR      BYTE4_XOR_BE(0)
r6173r6174
110115   int blend_m2a_1;
111116   int blend_m2b_0;
112117   int blend_m2b_1;
118    int tex_edge;
113119   int force_blend;
114120   int alpha_cvg_select;
115121   int cvg_times_alpha;
r6173r6174
137143#define CYCLE_TYPE_COPY         2
138144#define CYCLE_TYPE_FILL         3
139145
146#define SAMPLE_TYPE_1x1         0
147#define SAMPLE_TYPE_2x2         1
148
140149static COMBINE_MODES combine;
141150static OTHER_MODES other_modes;
142151
r6173r6174
181190static UINT8 *blender2a_b[2];
182191static UINT8 *blender2b_a[2];
183192
184
185193static COLOR pixel_color;
186194static COLOR inv_pixel_color;
187195static COLOR blended_pixel_color;
188196static COLOR memory_color;
189197
190
191
192198static UINT32 fill_color;      // packed 16-bit or 32-bit, depending on framebuffer format
193199
194200static UINT16 primitive_z;
r6173r6174
213219static UINT8 *texture_cache;
214220static UINT32 tlut[256];
215221
216#define MASK(S, T)                                                        \
217do                                                                        \
218{                                                                        \
219    if (mask_s != 0)                                                    \
220    {                                                                    \
221        S &= (((UINT32)(0xffffffff) >> (32-mask_s)));                    \
222    }                                                                    \
223    if (mask_t != 0)                                                    \
224    {                                                                    \
225        T &= (((UINT32)(0xffffffff) >> (32-mask_t)));                    \
226    }                                                                    \
227}                                                                        \
222#define MASK(S, T, CYCLE)                                               \
223do                                                                      \
224{                                                                       \
225    if (mask_s[CYCLE] != 0)                                             \
226    {                                                                   \
227        S &= (1 << mask_s[CYCLE]) - 1;                                  \
228    }                                                                   \
229    if (mask_t[CYCLE] != 0)                                             \
230    {                                                                   \
231        T &= (1 << mask_t[CYCLE]) - 1;                                  \
232    }                                                                   \
233}                                                                       \
228234while (0)
229235
230#define CLAMP(S, T)                                          \
231do                                                      \
232{                                                      \
233   if (clamp_s)                                          \
234   {                                                   \
235      if (S < (tsl >> 2)) S = (tsl >> 2);                        \
236      if (S > (tsh >> 2)) S = (tsh >> 2);                        \
237   }                                                   \
238   if (mirror_s)                                          \
239   {                                                   \
240      if (S < (tsl >> 2)) S = abs(S - (tsl >> 2));               \
241      if (S > (tsh >> 2)) S = abs(S - (tsh >> 2));               \
242   }                                                   \
243   if (clamp_t)                                          \
244   {                                                   \
245      if (T < (ttl >> 2)) T = (ttl >> 2);                        \
246      if (T > (tth >> 2)) T = (tth >> 2);                        \
247   }                                                   \
248   if (mirror_t)                                          \
249   {                                                   \
250      if (T < (ttl >> 2)) T = abs(T - (ttl >> 2));               \
251      if (T > (tth >> 2)) T = abs(T - (tth >> 2));               \
252   }                                                   \
253}                                                      \
236#define CLAMP(S, T, CYCLE)                                              \
237do                                                                      \
238{                                                                       \
239    if (mask_s[CYCLE] != 0 && clamp_s[CYCLE])                           \
240    {                                                                   \
241        if (S < (tsl[CYCLE] >> 2)) S = (tsl[CYCLE] >> 2);               \
242        if (S > (tsh[CYCLE] >> 2)) S = (tsh[CYCLE] >> 2);               \
243    }                                                                   \
244    if (mask_s[CYCLE] != 0 && mirror_s[CYCLE])                          \
245    {                                                                   \
246        if (S & (1 << mask_s[CYCLE])) S ^= (1 << mask_s[CYCLE]) - 1;    \
247    }                                                                   \
248    if (mask_t[CYCLE] != 0 && clamp_t[CYCLE])                           \
249    {                                                                   \
250        if (T < (ttl[CYCLE] >> 2)) T = (ttl[CYCLE] >> 2);               \
251        if (T > (tth[CYCLE] >> 2)) T = (tth[CYCLE] >> 2);               \
252    }                                                                   \
253    if (mask_t[CYCLE] != 0 && mirror_t[CYCLE])                          \
254    {                                                                   \
255        if (T & (1 << mask_t[CYCLE])) T ^= (1 << mask_t[CYCLE]) - 1;    \
256    }                                                                   \
257}                                                                       \
254258while (0)
255259
256260/*****************************************************************************/
r6173r6174
292296   blender2b_a[0] = blender2b_a[1] = &pixel_color.r;
293297}
294298
299static void vi_filter_16( INT32 x, INT32 y, UINT16 *out )
300{
301    UINT8 *cvg_buffer = &cvg_mem[n64_vi_origin / 4];
302    UINT16 *frame_buffer = (UINT16*)&rdram[n64_vi_origin / 4];
303    INT32 i_start = x - 2;
304    INT32 i_end = x + 2;
305    INT32 j_start = y - 1;
306    INT32 j_end = y + 1;
307    INT32 r_sum = 0;
308    INT32 g_sum = 0;
309    INT32 b_sum = 0;
310    INT32 covered_count = 0;
311    INT32 i = 0;
312    INT32 j = 0;
313
314    if (cvg_buffer[y*fb_width + x] == 8)
315    {
316        *out = frame_buffer[y*fb_width + x];
317        return;
318    }
319
320    if (i_start < 0)
321    {
322        i_start = 0;
323    }
324    if (j_start < 0)
325    {
326        j_start = 0;
327    }
328    if (i_end >= fb_width)
329    {
330        i_end = fb_width - 1;
331    }
332    if (j_end >= ((n64_vi_control & 0x40) ? 480 : 240))
333    {
334        j_end = (n64_vi_control & 0x40) ? 479 : 239;
335    }
336
337    for (j = j_start; j <= j_end; j++)
338    {
339        for (i = i_start; i <= i_end; i++)
340        {
341            if (cvg_buffer[j*fb_width + x] == 8)
342            {
343                UINT16 color = frame_buffer[j*fb_width + x];
344                covered_count++;
345                r_sum += (color >> 11) & 0x1f;
346                g_sum += (color >>  6) & 0x1f;
347                b_sum += (color >>  1) & 0x1f;
348            }
349        }
350    }
351
352    if (covered_count)
353    {
354        UINT16 fb = frame_buffer[y*fb_width + x];
355        r_sum /= covered_count;
356        g_sum /= covered_count;
357        b_sum /= covered_count;
358        if (r_sum > 0x1f) r_sum = 0x1f;
359        if (g_sum > 0x1f) g_sum = 0x1f;
360        if (b_sum > 0x1f) b_sum = 0x1f;
361        r_sum = (UINT32)(((float)r_sum / 8.0f) * (float)(8 - cvg_buffer[y*fb_width + x]) +
362                         ((float)((fb >> 11) & 0x1f) / 8.0f) * (float)cvg_buffer[y*fb_width + x]);
363        g_sum = (UINT32)(((float)g_sum / 8.0f) * (float)(8 - cvg_buffer[y*fb_width + x]) +
364                         ((float)((fb >>  6) & 0x1f) / 8.0f) * (float)cvg_buffer[y*fb_width + x]);
365        b_sum = (UINT32)(((float)b_sum / 8.0f) * (float)(8 - cvg_buffer[y*fb_width + x]) +
366                         ((float)((fb >>  1) & 0x1f) / 8.0f) * (float)cvg_buffer[y*fb_width + x]);
367        *out = (r_sum << 11) | (g_sum << 6) | (b_sum << 1);
368    }
369    else
370    {
371        *out = frame_buffer[y*fb_width + x];
372    }
373}
374
375static void vi_filter_32( INT32 x, INT32 y, UINT32 *out )
376{
377    UINT8 *cvg_buffer = &cvg_mem[n64_vi_origin / 4];
378    UINT32 *frame_buffer = (UINT32*)&rdram[n64_vi_origin / 4];
379    INT32 i_start = x - 2;
380    INT32 i_end = x + 2;
381    INT32 j_start = y - 1;
382    INT32 j_end = y + 1;
383    UINT32 r_sum = 0;
384    UINT32 g_sum = 0;
385    UINT32 b_sum = 0;
386    INT32 covered_count = 0;
387    INT32 i = 0;
388    INT32 j = 0;
389
390    if (cvg_buffer[y*fb_width + x] == 8)
391    {
392        *out = frame_buffer[y*fb_width + x];
393        return;
394    }
395
396    if (i_start < 0)
397    {
398        i_start = 0;
399    }
400    if (j_start < 0)
401    {
402        j_start = 0;
403    }
404    if (i_end >= fb_width)
405    {
406        i_end = fb_width - 1;
407    }
408    if (j_end >= ((n64_vi_control & 0x40) ? 480 : 240))
409    {
410        j_end = (n64_vi_control & 0x40) ? 479 : 239;
411    }
412
413    for (j = j_start; j <= j_end; j++)
414    {
415        for (i = i_start; i <= i_end; i++)
416        {
417            if (cvg_buffer[j*fb_width + x] == 8)
418            {
419                UINT32 color = frame_buffer[j*fb_width + x];
420                covered_count++;
421                r_sum += (color >> 16) & 0xff;
422                g_sum += (color >>  8) & 0xff;
423                b_sum += (color >>  0) & 0xff;
424            }
425        }
426    }
427
428    if (covered_count)
429    {
430        r_sum /= covered_count;
431        g_sum /= covered_count;
432        b_sum /= covered_count;
433        if (r_sum > 0xff) r_sum = 0xff;
434        if (g_sum > 0xff) g_sum = 0xff;
435        if (b_sum > 0xff) b_sum = 0xff;
436        *out = (r_sum << 24) | (g_sum << 16) | (b_sum << 8) | 0xe0;
437    }
438    else
439    {
440        *out = frame_buffer[y*fb_width + x];
441    }
442}
443
295444VIDEO_UPDATE(n64)
296445{
297446   int i, j;
298447    int height = (n64_vi_control & 0x40) ? 479 : 239;
299448
449    if (n64_vi_blank)
450    {
451        for (j=0; j <height; j++)
452        {
453            UINT32 *d = BITMAP_ADDR32(bitmap, j, 0);
454            for (i=0; i < fb_width; i++)
455            {
456                d[BYTE_XOR_BE(i)] = 0;
457            }
458        }
459        return 0;
460    }
461
300462    switch (n64_vi_control & 0x3)
301463   {
302464      case 0:      // blank/no signal
r6173r6174
309471            UINT16 *frame_buffer = (UINT16*)&rdram[n64_vi_origin / 4];
310472         if (frame_buffer)
311473         {
312            for (j=0; j <height; j++)
474            for (j=0; j <= height; j++)
313475            {
314476               UINT32 *d = BITMAP_ADDR32(bitmap, j, 0);
315477               for (i=0; i < fb_width; i++)
316478               {
317                  UINT16 pix = *frame_buffer++;
318                  int r = ((pix >> 11) & 0x1f) << 3;
319                  int g = ((pix >> 6) & 0x1f) << 3;
320                  int b = ((pix >> 1) & 0x1f) << 3;
479                  //UINT16 pix = *frame_buffer++;
480                        UINT16 pix = 0;
481                        int r, g, b;
482                        vi_filter_16( i, j, &pix );
483                  r = ((pix >> 11) & 0x1f) << 3;
484                  g = ((pix >> 6) & 0x1f) << 3;
485                  b = ((pix >> 1) & 0x1f) << 3;
321486                  d[BYTE_XOR_BE(i)] = (r << 16) | (g << 8) | b;
322487               }
323488            }
r6173r6174
330495            UINT32 *frame_buffer = (UINT32*)&rdram[n64_vi_origin / 4];
331496         if (frame_buffer)
332497         {
333            for (j=0; j < height; j++)
498            for (j=0; j <= height; j++)
334499            {
335500               UINT32 *d = BITMAP_ADDR32(bitmap, j, 0);
336501               for (i=0; i < fb_width; i++)
337502               {
338                  UINT32 pix = *frame_buffer++;
339                  *d++ = pix & 0xffffff;
503                        //UINT32 pix = *frame_buffer++;
504                        UINT32 pix = 0;
505                        vi_filter_32( i, j, &pix );
506                        *d++ = (pix >> 8) & 0x00ffffff;
340507               }
341508            }
342509         }
r6173r6174
468635   COLOR c;
469636   UINT32 r, g, b, a;
470637
471   r = (((int)((UINT8)(*combiner_rgbsub_a_r[cycle]) - (UINT8)(*combiner_rgbsub_b_r[cycle])) *
472      *combiner_rgbmul_r[cycle]) >> 8) + *combiner_rgbadd_r[cycle];
638    r = (((UINT32)((UINT32)(*combiner_rgbsub_a_r[cycle]) - (UINT32)(*combiner_rgbsub_b_r[cycle])) *
639        *combiner_rgbmul_r[cycle]) >> 8);
473640
474   g = (((int)((UINT8)(*combiner_rgbsub_a_g[cycle]) - (UINT8)(*combiner_rgbsub_b_g[cycle])) *
475      *combiner_rgbmul_g[cycle]) >> 8) + *combiner_rgbadd_g[cycle];
641    g = (((UINT32)((UINT32)(*combiner_rgbsub_a_g[cycle]) - (UINT32)(*combiner_rgbsub_b_g[cycle])) *
642        *combiner_rgbmul_g[cycle]) >> 8);
476643
477   b = (((int)((UINT8)(*combiner_rgbsub_a_b[cycle]) - (UINT8)(*combiner_rgbsub_b_b[cycle])) *
478      *combiner_rgbmul_b[cycle]) >> 8) + *combiner_rgbadd_b[cycle];
644    b = (((UINT32)((UINT32)(*combiner_rgbsub_a_b[cycle]) - (UINT32)(*combiner_rgbsub_b_b[cycle])) *
645        *combiner_rgbmul_b[cycle]) >> 8);
479646
480   a = (((int)((UINT8)(*combiner_alphasub_a[cycle]) - (UINT8)(*combiner_alphasub_b[cycle])) *
481      *combiner_alphamul[cycle]) >> 8) + *combiner_alphaadd[cycle];
647    a = (((UINT32)((UINT32)(*combiner_alphasub_a[cycle]) - (UINT32)(*combiner_alphasub_b[cycle])) *
648        *combiner_alphamul[cycle]) >> 8);
482649
483   if (r > 255) r = 255;
484   if (g > 255) g = 255;
485   if (b > 255) b = 255;
486   if (a > 255) a = 255;
650    if (r & 0xffffff00)
651    {
652        if(r & 0x00000080)
653        {
654            r = 0;
655        }
656        else
657        {
658            r = 255;
659        }
660    }
661    if (g & 0xffffff00)
662    {
663        if(g & 0x00000080)
664        {
665            g = 0;
666        }
667        else
668        {
669            g = 255;
670        }
671    }
672    if (b & 0xffffff00)
673    {
674        if(b & 0x00000080)
675        {
676            b = 0;
677        }
678        else
679        {
680            b = 255;
681        }
682    }
683    if (a & 0xffffff00)
684    {
685        if(a & 0x00000080)
686        {
687            a = 0;
688        }
689        else
690        {
691            a = 255;
692        }
693    }
487694
488   c.r = r; c.g = g; c.b = b; c.a = a;
489   return c;
490}
695    c.r = r; c.g = g; c.b = b; c.a = a;
491696
697    r += *combiner_rgbadd_r[cycle];
698    g += *combiner_rgbadd_g[cycle];
699    b += *combiner_rgbadd_b[cycle];
700    a += *combiner_alphaadd[cycle];
492701
702    r &= 0x000000ff;
703    g &= 0x000000ff;
704    b &= 0x000000ff;
705    a &= 0x000000ff;
493706
707    /*
708    if (r > 255) r = 255;
709    if (g > 255) g = 255;
710    if (b > 255) b = 255;
711    if (a > 255) a = 255;
712    */
713
714    c.r = r; c.g = g; c.b = b; c.a = a;
715
716    combined_color.r = r;
717    combined_color.g = g;
718    combined_color.b = b;
719    combined_color.a = a;
720
721    return c;
722}
723
494724INLINE void SET_BLENDER_INPUT(int cycle, int which, UINT8 **input_r, UINT8 **input_g, UINT8 **input_b, UINT8 **input_a, int a, int b)
495725{
496726   switch (a & 0x3)
r6173r6174
568798#define DITHER_RB(val,dith)   ((((val) << 1) - ((val) >> 4) + ((val) >> 7) + (dith)) >> 1)
569799#define DITHER_G(val,dith)   ((((val) << 2) - ((val) >> 4) + ((val) >> 6) + (dith)) >> 2)
570800
571INLINE void BLENDER1_16(UINT16 *fb, COLOR c, int dith)
801INLINE void BLENDER1_16(UINT16 *fb, COLOR c, int dith, UINT8 *new_cvg, UINT8 *old_cvg, int *alpha_reject)
572802{
573   int r, g, b;
803    int r, g, b;
574804
575   pixel_color.r = c.r;   inv_pixel_color.r = 0xff - c.r;
576   pixel_color.g = c.g;   inv_pixel_color.g = 0xff - c.g;
577   pixel_color.b = c.b;   inv_pixel_color.b = 0xff - c.b;
578   pixel_color.a = c.a;   inv_pixel_color.a = 0xff - c.a;
805    if (other_modes.force_blend )
806    {
807        blend_color.r = c.r;
808        blend_color.g = c.g;
809        blend_color.b = c.b;
810    }
579811
812    pixel_color.r = c.r;
813    pixel_color.g = c.g;
814    pixel_color.b = c.b;
815    pixel_color.a = c.a;
816    inv_pixel_color.r = 0xff - *blender1a_r[0];
817    inv_pixel_color.g = 0xff - *blender1a_g[0];
818    inv_pixel_color.b = 0xff - *blender1a_b[0];
819    inv_pixel_color.a = 0xff - *blender1b_a[0];
580820
821    if( other_modes.alpha_cvg_select )
822    {
823        if( other_modes.cvg_times_alpha )
824        {
825            pixel_color.a = (UINT8)( ( (INT32)(c.a) * (INT32)( (*new_cvg) * 0x20 - 1 ) ) >> 8 );
826        }
827        else
828        {
829            pixel_color.a = (UINT8)( (INT32)(*new_cvg) * 0x20 - 1 );
830        }
831        inv_pixel_color.a = 0xff - pixel_color.a;
581832
582   if (other_modes.image_read_en)
583   {
584      UINT16 mem = *fb;
585      memory_color.r = ((mem >> 11) & 0x1f) << 3;
586      memory_color.g = ((mem >>  6) & 0x1f) << 3;
587      memory_color.b = ((mem >>  1) & 0x1f) << 3;
588      memory_color.a = 0;
589   }
833        if( pixel_color.a == 0 )
834        {
835            *alpha_reject = 1;
836            return;
837        }
838    }
590839
591   r = (((int)(*blender1a_r[0]) * (int)(*blender1b_a[0])) >> 8) +
592      (((int)(*blender2a_r[0]) * (int)(*blender2b_a[0])) >> 8);
840    if (other_modes.image_read_en)
841    {
842        UINT16 mem = *fb;
843        memory_color.r = (((mem >> 11) & 0x1f) << 3) | ((mem >> 13) & 0x07);
844        memory_color.g = (((mem >>  6) & 0x1f) << 3) | ((mem >>  8) & 0x07);
845        memory_color.b = (((mem >>  1) & 0x1f) << 3) | ((mem >>  3) & 0x07);
846        memory_color.a = /*(UINT8)((INT32)(*old_cvg) * 0x20 - 1)*/0;
847    }
593848
594   g = (((int)(*blender1a_g[0]) * (int)(*blender1b_a[0])) >> 8) +
595      (((int)(*blender2a_g[0]) * (int)(*blender2b_a[0])) >> 8);
849    if (((int)(*blender1b_a[0]) + (int)(*blender2b_a[0])) > 0)
850    {
851        r = ((int)(*blender1a_r[0]) * (int)(*blender1b_a[0]) +
852             (int)(*blender2a_r[0]) * (int)(*blender2b_a[0])) /
853            ((int)(*blender1b_a[0]) + (int)(*blender2b_a[0]));
596854
597   b = (((int)(*blender1a_b[0]) * (int)(*blender1b_a[0])) >> 8) +
598      (((int)(*blender2a_b[0]) * (int)(*blender2b_a[0])) >> 8);
855        g = ((int)(*blender1a_g[0]) * (int)(*blender1b_a[0]) +
856             (int)(*blender2a_g[0]) * (int)(*blender2b_a[0])) /
857            ((int)(*blender1b_a[0]) + (int)(*blender2b_a[0]));
599858
600   //r = g = b = 0xff;
859        b = ((int)(*blender1a_b[0]) * (int)(*blender1b_a[0]) +
860             (int)(*blender2a_b[0]) * (int)(*blender2b_a[0])) /
861            ((int)(*blender1b_a[0]) + (int)(*blender2b_a[0]));
862    }
863    else
864    {
865        r = g = b = 0;
866    }
601867
602   if (r > 255) r = 255;
603   if (g > 255) g = 255;
604   if (b > 255) b = 255;
868    //r = g = b = 0xff;
605869
606   if (other_modes.rgb_dither_sel != 3)
607   {
608      r = DITHER_RB(r, dith);
609      g = DITHER_G (g, dith);
610      b = DITHER_RB(b, dith);
611   }
870    if (r > 255) r = 255;
871    if (g > 255) g = 255;
872    if (b > 255) b = 255;
612873
613   if (c.a != 0)
614   {
615      *fb = ((r >> 3) << 11) | ((g >> 3) << 6) | ((b >> 3) << 1);
616   }
874    if (other_modes.rgb_dither_sel != 3)
875    {
876        r = DITHER_RB(r, dith);
877        g = DITHER_G (g, dith);
878        b = DITHER_RB(b, dith);
879    }
880
881    *fb = ((r >> 3) << 11) | ((g >> 3) << 6) | ((b >> 3) << 1) | 1;
617882}
618883
619INLINE void BLENDER2_16(UINT16 *fb, COLOR c1, COLOR c2, int dith)
884INLINE void BLENDER2_16(UINT16 *fb, COLOR c1, COLOR c2, int dith, UINT8 *new_cvg, UINT8 *old_cvg, int *alpha_reject)
620885{
621   int r, g, b;
886    int r, g, b;
622887
623   pixel_color.r = c1.r;   inv_pixel_color.r = 0xff - c1.r;
624   pixel_color.g = c1.g;   inv_pixel_color.g = 0xff - c1.g;
625   pixel_color.b = c1.b;   inv_pixel_color.b = 0xff - c1.b;
626   pixel_color.a = c1.a;   inv_pixel_color.a = 0xff - c1.a;
888    if( other_modes.force_blend )
889    {
890        blend_color.r = c2.r;
891        blend_color.g = c2.g;
892        blend_color.b = c2.b;
893    }
627894
628   if (other_modes.image_read_en)
629   {
630      UINT16 mem = *fb;
631      memory_color.r = ((mem >> 11) & 0x1f) << 3;
632      memory_color.g = ((mem >>  6) & 0x1f) << 3;
633      memory_color.b = ((mem >>  1) & 0x1f) << 3;
634      memory_color.a = 0;
635   }
895    pixel_color.r = c2.r;
896    pixel_color.g = c2.g;
897    pixel_color.b = c2.b;
898    pixel_color.a = c2.a;
899    inv_pixel_color.r = 0xff - *blender1a_r[0];
900    inv_pixel_color.g = 0xff - *blender1a_g[0];
901    inv_pixel_color.b = 0xff - *blender1a_b[0];
902    inv_pixel_color.a = 0xff - *blender1b_a[0];
903    *alpha_reject = 0;
636904
637   r = (((int)(*blender1a_r[0]) * (int)(*blender1b_a[0])) >> 8) +
638      (((int)(*blender2a_r[0]) * (int)(*blender2b_a[0])) >> 8);
905    if( other_modes.alpha_cvg_select )
906    {
907        if( other_modes.cvg_times_alpha )
908        {
909            pixel_color.a = (UINT8)( ( (INT32)(c2.a) * (INT32)( (*new_cvg) * 0x20 - 1 ) ) >> 8 );
910        }
911        else
912        {
913            pixel_color.a = (UINT8)( (INT32)(*new_cvg) * 0x20 - 1 );
914        }
915        //inv_pixel_color.a = 0xff - pixel_color.a;
639916
640   g = (((int)(*blender1a_g[0]) * (int)(*blender1b_a[0])) >> 8) +
641      (((int)(*blender2a_g[0]) * (int)(*blender2b_a[0])) >> 8);
917        if( pixel_color.a == 0 )
918        {
919            *alpha_reject = 1;
920            return;
921        }
922    }
642923
643   b = (((int)(*blender1a_b[0]) * (int)(*blender1b_a[0])) >> 8) +
644      (((int)(*blender2a_b[0]) * (int)(*blender2b_a[0])) >> 8);
924    if (other_modes.image_read_en)
925    {
926        UINT16 mem = *fb;
927        memory_color.r = (((mem >> 11) & 0x1f) << 3) | ((mem >> 13) & 0x07);
928        memory_color.g = (((mem >>  6) & 0x1f) << 3) | ((mem >>  8) & 0x07);
929        memory_color.b = (((mem >>  1) & 0x1f) << 3) | ((mem >>  3) & 0x07);
930        memory_color.a = /*(UINT8)((INT32)(*old_cvg) * 0x20 - 1)*/0;
931    }
645932
646   blended_pixel_color.r = r;
647   blended_pixel_color.g = g;
648   blended_pixel_color.b = b;
933    if (((int)(*blender1b_a[0]) + (int)(*blender2b_a[0])) > 0)
934    {
935        r = ((int)(*blender1a_r[0]) * (int)(*blender1b_a[0]) +
936             (int)(*blender2a_r[0]) * (int)(*blender2b_a[0])) /
937            ((int)(*blender1b_a[0]) + (int)(*blender2b_a[0]));
649938
650   pixel_color.r = c2.r;   inv_pixel_color.r = 0xff - c2.r;
651   pixel_color.g = c2.g;   inv_pixel_color.g = 0xff - c2.g;
652   pixel_color.b = c2.b;   inv_pixel_color.b = 0xff - c2.b;
653   pixel_color.a = c2.a;   inv_pixel_color.a = 0xff - c2.a;
939        g = ((int)(*blender1a_g[0]) * (int)(*blender1b_a[0]) +
940             (int)(*blender2a_g[0]) * (int)(*blender2b_a[0])) /
941            ((int)(*blender1b_a[0]) + (int)(*blender2b_a[0]));
654942
655   r = (((int)(*blender1a_r[1]) * (int)(*blender1b_a[1])) >> 8) +
656      (((int)(*blender2a_r[1]) * (int)(*blender2b_a[1])) >> 8);
943        b = ((int)(*blender1a_b[0]) * (int)(*blender1b_a[0]) +
944             (int)(*blender2a_b[0]) * (int)(*blender2b_a[0])) /
945            ((int)(*blender1b_a[0]) + (int)(*blender2b_a[0]));
946    }
947    else
948    {
949        r = g = b = 0;
950    }
657951
658   g = (((int)(*blender1a_g[1]) * (int)(*blender1b_a[1])) >> 8) +
659      (((int)(*blender2a_g[1]) * (int)(*blender2b_a[1])) >> 8);
952    blended_pixel_color.r = r;
953    blended_pixel_color.g = g;
954    blended_pixel_color.b = b;
660955
661   b = (((int)(*blender1a_b[1]) * (int)(*blender1b_a[1])) >> 8) +
662      (((int)(*blender2a_b[1]) * (int)(*blender2b_a[1])) >> 8);
956    pixel_color.r = c2.r;
957    pixel_color.g = c2.g;
958    pixel_color.b = c2.b;
959    pixel_color.a = c2.a;
960    inv_pixel_color.r = 0xff - *blender1a_r[1];
961    inv_pixel_color.g = 0xff - *blender1a_g[1];
962    inv_pixel_color.b = 0xff - *blender1a_b[1];
963    inv_pixel_color.a = 0xff - *blender1b_a[1];
663964
664   r = c1.r;
665   g = c1.g;
666   b = c1.b;
965    if( other_modes.alpha_cvg_select )
966    {
967        if( other_modes.cvg_times_alpha )
968        {
969            pixel_color.a = (UINT8)( ( (INT32)(c2.a) * (INT32)( (*new_cvg) * 0x20 - 1 ) ) >> 8 );
970        }
971        else
972        {
973            pixel_color.a = (UINT8)( (INT32)(*new_cvg) * 0x20 - 1 );
974        }
667975
668   if (r > 255) r = 255;
669   if (g > 255) g = 255;
670   if (b > 255) b = 255;
976        if( pixel_color.a == 0 )
977        {
978            *alpha_reject = 1;
979            return;
980        }
981    }
671982
672   if (other_modes.rgb_dither_sel != 3)
673   {
674      r = DITHER_RB(r, dith);
675      g = DITHER_G (g, dith);
676      b = DITHER_RB(b, dith);
677   }
983    if (((int)(*blender1b_a[1]) + (int)(*blender2b_a[1])) > 0)
984    {
985        r = ((int)(*blender1a_r[1]) * (int)(*blender1b_a[1]) +
986             (int)(*blender2a_r[1]) * (int)(*blender2b_a[1])) /
987            ((int)(*blender1b_a[1]) + (int)(*blender2b_a[1]));
678988
679   {
680      *fb = ((r >> 3) << 11) | ((g >> 3) << 6) | ((b >> 3) << 1);
681   }
989        g = ((int)(*blender1a_g[1]) * (int)(*blender1b_a[1]) +
990             (int)(*blender2a_g[1]) * (int)(*blender2b_a[1])) /
991            ((int)(*blender1b_a[1]) + (int)(*blender2b_a[1]));
992
993        b = ((int)(*blender1a_b[1]) * (int)(*blender1b_a[1]) +
994             (int)(*blender2a_b[1]) * (int)(*blender2b_a[1])) /
995            ((int)(*blender1b_a[1]) + (int)(*blender2b_a[1]));
996    }
997
998    //if (r > 255) r = 255;
999    //if (g > 255) g = 255;
1000    //if (b > 255) b = 255;
1001    if (r > 255) r = 255;
1002    if (g > 255) g = 255;
1003    if (b > 255) b = 255;
1004    if (r < 0)   r = 0;
1005    if (g < 0)   g = 0;
1006    if (b < 0)   b = 0;
1007    //r &= 0x000000ff;
1008    //g &= 0x000000ff;
1009    //b &= 0x000000ff;
1010
1011    if (other_modes.rgb_dither_sel != 3)
1012    {
1013        r = DITHER_RB(r, dith);
1014        g = DITHER_G (g, dith);
1015        b = DITHER_RB(b, dith);
1016    }
1017
1018    *fb = ((r >> 3) << 11) | ((g >> 3) << 6) | ((b >> 3) << 1) | 1;
6821019}
6831020
6841021/*****************************************************************************/
r6173r6174
6861023static void fill_rectangle_16bit(RECTANGLE *rect)
6871024{
6881025   UINT16 *fb = (UINT16*)&rdram[(fb_address / 4)];
1026    UINT8* cvg_buf = (UINT8*)&cvg_mem[fb_address];
6891027   int index, i, j;
6901028   int x1 = rect->xh / 4;
6911029   int x2 = rect->xl / 4;
6921030   int y1 = rect->yh / 4;
6931031   int y2 = rect->yl / 4;
6941032   int clipx1, clipx2, clipy1, clipy2;
1033    int alpha_reject = 0;
1034    COLOR c1, c2;
6951035
6961036   UINT16 fill_color1, fill_color2;
6971037   fill_color1 = (fill_color >> 16) & 0xffff;
6981038   fill_color2 = (fill_color >>  0) & 0xffff;
6991039
1040    c1.r = c1.g = c1.b = c1.a = 0;
1041    c2.r = c2.g = c2.b = c2.a = 0;
1042
7001043   // TODO: clip
7011044   clipx1 = clip.xh / 4;
7021045   clipx2 = clip.xl / 4;
r6173r6174
7281071         index = j * fb_width;
7291072         for (i=x1; i <= x2; i++)
7301073         {
1074                cvg_buf[(index + i) ^ 1] = (i & 1) ? ( ( fill_color1 & 1 ) ? 8 : 1 ) : ( ( fill_color2 & 1 ) ? 8 : 1 );
7311075            fb[(index + i) ^ 1] = (i & 1) ? fill_color1 : fill_color2;
7321076         }
7331077      }
7341078   }
735   else if (other_modes.cycle_type == CYCLE_TYPE_1)
736   {
737      shade_color.r = prim_color.r;
738      shade_color.g = prim_color.g;
739      shade_color.b = prim_color.b;
740      shade_color.a = prim_color.a;
1079    else if (other_modes.cycle_type == CYCLE_TYPE_1)
1080    {
1081        shade_color.r = prim_color.r;
1082        shade_color.g = prim_color.g;
1083        shade_color.b = prim_color.b;
1084        shade_color.a = prim_color.a;
7411085
742      for (j=y1; j <= y2; j++)
743      {
744         index = j * fb_width;
745         for (i=x1; i <= x2; i++)
746         {
747            COLOR c = COLOR_COMBINER(0);
1086        for (j=y1; j <= y2; j++)
1087        {
1088            index = j * fb_width;
1089            for (i=x1; i <= x2; i++)
1090            {
1091                int dith = dither_matrix_4x4[(((j) & 3) << 2) + ((i^WORD_ADDR_XOR) & 3)];
7481092
749            //BLENDER1_16(&fb[(index + i) ^ 1], c);
750            {
751               int dith = dither_matrix_4x4[(((j) & 3) << 2) + ((i^WORD_ADDR_XOR) & 3)];
752               BLENDER1_16(&fb[(index + i) ^ WORD_ADDR_XOR], c, dith);
753            }
754         }
755      }
756   }
757   else if (other_modes.cycle_type == CYCLE_TYPE_2)
758   {
759      shade_color.r = prim_color.r;
760      shade_color.g = prim_color.g;
761      shade_color.b = prim_color.b;
762      shade_color.a = prim_color.a;
1093                c1 = COLOR_COMBINER(0);
7631094
764      for (j=y1; j <= y2; j++)
765      {
766         index = j * fb_width;
767         for (i=x1; i <= x2; i++)
768         {
769            COLOR c1 = COLOR_COMBINER(0);
770            COLOR c2 = COLOR_COMBINER(1);
1095                cvg_buf[(index + i) ^ WORD_ADDR_XOR] = 8; // Wrong
1096                BLENDER1_16(&fb[(index + i) ^ WORD_ADDR_XOR], c1, dith, &cvg_buf[(index + i) ^ WORD_ADDR_XOR], &cvg_buf[(index + i) ^ WORD_ADDR_XOR], &alpha_reject);
1097            }
1098        }
1099    }
1100    else if (other_modes.cycle_type == CYCLE_TYPE_2)
1101    {
1102        shade_color.r = prim_color.r;
1103        shade_color.g = prim_color.g;
1104        shade_color.b = prim_color.b;
1105        shade_color.a = prim_color.a;
7711106
772            //BLENDER1_16(&fb[(index + i) ^ 1], c);
773            {
774               int dith = dither_matrix_4x4[(((j) & 3) << 2) + ((i^WORD_ADDR_XOR) & 3)];
775               BLENDER2_16(&fb[(index + i) ^ WORD_ADDR_XOR], c1, c2, dith);
776            }
777         }
778      }
779   }
780   else
781   {
782      fatalerror("fill_rectangle_16bit: cycle type copy");
783   }
1107        for (j=y1; j <= y2; j++)
1108        {
1109            index = j * fb_width;
1110            for (i=x1; i <= x2; i++)
1111            {
1112                int dith = dither_matrix_4x4[(((j) & 3) << 2) + ((i^WORD_ADDR_XOR) & 3)];
1113
1114                c1 = COLOR_COMBINER(0);
1115                c2 = COLOR_COMBINER(1);
1116
1117                cvg_buf[(index + i) ^ WORD_ADDR_XOR] = 8; // Wrong
1118                BLENDER2_16(&fb[(index + i) ^ WORD_ADDR_XOR], c1, c2, dith, &cvg_buf[(index + i) ^ WORD_ADDR_XOR], &cvg_buf[(index + i) ^ WORD_ADDR_XOR], &alpha_reject);
1119            }
1120        }
1121    }
1122    else
1123    {
1124        fatalerror("fill_rectangle_16bit: cycle type copy");
1125    }
7841126}
7851127
7861128#define XOR_SWAP_BYTE   4
7871129#define XOR_SWAP_WORD   2
7881130#define XOR_SWAP_DWORD   1
7891131
790INLINE void FETCH_TEXEL(COLOR *color, int s, int t, UINT32 twidth, UINT32 tformat, UINT32 tsize, UINT32 tbase)
1132INLINE int taddr_clamp(int addr)
7911133{
1134    return (addr > 0x100000) ? 0 : addr;
1135}
1136
1137INLINE void FETCH_TEXEL(COLOR *color, int s, int t, UINT32 twidth, UINT32 tformat, UINT32 tsize, UINT32 tbase, UINT8 tpal)
1138{
7921139   if (t < 0) t = 0;
7931140   if (s < 0) s = 0;
7941141
r6173r6174
7981145      {
7991146         switch (tsize)
8001147         {
801            case 0:
802               color->r = color->g = color->b = color->a = 0xff;
803               break;
804            case 1:
805               // FIXME?: Extreme-G 2 does this
806               color->r = color->g = color->b = color->a = 0xff;
807               break;
1148                case PIXEL_SIZE_4BIT:
1149                {
1150                    // 4-bit RGBA is a "valid" mode; some games use it, it behaves the same as 4-bit CI
1151                    UINT8 *tc = (UINT8*)texture_cache;
1152                    int taddr = taddr_clamp((tbase + ((t) * twidth) + ((s) / 2)) ^ ((t & 1) ? XOR_SWAP_BYTE : 0));
1153                    UINT8 p = ((s) & 1) ? (tc[taddr ^ BYTE_ADDR_XOR] & 0xf) : (tc[taddr ^ BYTE_ADDR_XOR] >> 4);
1154                    UINT16 c = tlut[(p + (tpal << 4))^ WORD_ADDR_XOR];
1155
1156                    if (other_modes.tlut_type == 0)
1157                    {
1158                        color->r = ((c >> 11) & 0x1f) << 3;
1159                        color->g = ((c >>  6) & 0x1f) << 3;
1160                        color->b = ((c >>  1) & 0x1f) << 3;
1161                        color->a = (c & 1) ? 0xff : 0;
1162                    }
1163                    else
1164                    {
1165                        color->r = color->g = color->b = (c >> 8) & 0xff;
1166                        color->a = c & 0xff;
1167                    }
1168                    break;
1169                }
1170                case PIXEL_SIZE_8BIT:
1171                {
1172                    // 8-bit RGBA is a "valid" mode; some games use it, it behaves the same as 8-bit CI
1173                    UINT8 *tc = (UINT8*)texture_cache;
1174                    int taddr = taddr_clamp((tbase + ((t) * twidth) + ((s))) ^ ((t & 1) ? XOR_SWAP_BYTE : 0));
1175                    UINT8 p = tc[taddr ^ BYTE_ADDR_XOR];
1176                    UINT16 c = tlut[p ^ WORD_ADDR_XOR];
1177
1178                    if (other_modes.tlut_type == 0)
1179                    {
1180                        color->r = ((c >> 11) & 0x1f) << 3;
1181                        color->g = ((c >>  6) & 0x1f) << 3;
1182                        color->b = ((c >>  1) & 0x1f) << 3;
1183                        color->a = (c & 1) ? 0xff : 0;
1184                    }
1185                    else
1186                    {
1187                        color->r = color->g = color->b = (c >> 8) & 0xff;
1188                        color->a = c & 0xff;
1189                    }
1190                    break;
1191                }
8081192            case PIXEL_SIZE_16BIT:
8091193            {
8101194               UINT16 *tc = (UINT16*)texture_cache;
811               int taddr = ((tbase/2) + ((t) * (twidth/2)) + (s))  ^ ((t & 1) ? XOR_SWAP_WORD : 0);
1195                    int taddr = taddr_clamp(((tbase/2) + ((t) * (twidth/2)) + (s))  ^ ((t & 1) ? XOR_SWAP_WORD : 0));
8121196               UINT16 c = tc[taddr ^ WORD_ADDR_XOR];
8131197
8141198               color->r = ((c >> 11) & 0x1f) << 3;
r6173r6174
8201204            case PIXEL_SIZE_32BIT:
8211205            {
8221206               UINT32 *tc = (UINT32*)texture_cache;
823                    int taddr = ((tbase/4) + ((t) * (twidth/2)) + (s)) ^ ((t & 1) ? XOR_SWAP_DWORD : 0);
1207                    int taddr = taddr_clamp(((tbase/4) + ((t) * (twidth/2)) + (s)) ^ ((t & 1) ? XOR_SWAP_DWORD : 0));
8241208               UINT32 c = tc[taddr];
8251209
8261210               color->r = ((c >> 24) & 0xff);
r6173r6174
8431227            case PIXEL_SIZE_4BIT:
8441228            {
8451229               UINT8 *tc = (UINT8*)texture_cache;
846               int taddr = (tbase + ((t) * twidth) + ((s) / 2)) ^ ((t & 1) ? XOR_SWAP_BYTE : 0);
1230                    int taddr = taddr_clamp((tbase + ((t) * twidth) + ((s) / 2)) ^ ((t & 1) ? XOR_SWAP_BYTE : 0));
8471231               UINT8 p = ((s) & 1) ? (tc[taddr ^ BYTE_ADDR_XOR] & 0xf) : (tc[taddr ^ BYTE_ADDR_XOR] >> 4);
8481232               UINT16 c = tlut[p ^ WORD_ADDR_XOR];
8491233
r6173r6174
8641248            case PIXEL_SIZE_8BIT:
8651249            {
8661250               UINT8 *tc = (UINT8*)texture_cache;
867               int taddr = (tbase + ((t) * twidth) + ((s))) ^ ((t & 1) ? XOR_SWAP_BYTE : 0);
1251                    int taddr = taddr_clamp((tbase + ((t) * twidth) + ((s))) ^ ((t & 1) ? XOR_SWAP_BYTE : 0));
8681252               UINT8 p = tc[taddr ^ BYTE_ADDR_XOR];
8691253               UINT16 c = tlut[p ^ WORD_ADDR_XOR];
8701254
r6173r6174
8821266               }
8831267               break;
8841268            }
885            case 2:
886                // FIXME?: Clay Fighter Sculptor's Cut does this..
887               color->r = color->g = color->b = color->a = 0xff;
888               break;
1269                case PIXEL_SIZE_16BIT:
1270                {
1271                    // 16-bit CI is a "valid" mode; some games use it, it behaves the same as 16-bit RGBA
1272                    UINT16 *tc = (UINT16*)texture_cache;
1273                    int taddr = taddr_clamp(((tbase/2) + ((t) * (twidth/2)) + (s))  ^ ((t & 1) ? XOR_SWAP_WORD : 0));
1274                    UINT16 c = tc[taddr ^ WORD_ADDR_XOR];
1275
1276                    color->r = ((c >> 11) & 0x1f) << 3;
1277                    color->g = ((c >>  6) & 0x1f) << 3;
1278                    color->b = ((c >>  1) & 0x1f) << 3;
1279                    color->a = (c & 1) ? 0xff : 0;
1280                    break;
1281                }
8891282            default:
8901283               color->r = color->g = color->b = color->a = 0xff;
8911284               fatalerror("FETCH_TEXEL: unknown CI texture size %d\n", tsize);
r6173r6174
9001293            case PIXEL_SIZE_4BIT:
9011294            {
9021295               UINT8 *tc = (UINT8*)texture_cache;
903               int taddr = (tbase + ((t) * twidth) + ((s) / 2)) ^ ((t & 1) ? XOR_SWAP_BYTE : 0);
1296                    int taddr = taddr_clamp((tbase + ((t) * twidth) + ((s) / 2)) ^ ((t & 1) ? XOR_SWAP_BYTE : 0));
9041297               UINT8 p = ((s) & 1) ? (tc[taddr ^ BYTE_ADDR_XOR] & 0xf) : (tc[taddr ^ BYTE_ADDR_XOR] >> 4);
9051298               UINT8 i = ((p & 0xe) << 4) | ((p & 0xe) << 1) | (p & 0xe >> 2);
9061299
r6173r6174
9131306            case PIXEL_SIZE_8BIT:
9141307            {
9151308               UINT8 *tc = (UINT8*)texture_cache;
916               int taddr = (tbase + ((t) * twidth) + ((s))) ^ ((t & 1) ? XOR_SWAP_BYTE : 0);
1309                    int taddr = taddr_clamp((tbase + ((t) * twidth) + ((s))) ^ ((t & 1) ? XOR_SWAP_BYTE : 0));
9171310               UINT8 p = tc[taddr ^ BYTE_ADDR_XOR];
9181311               UINT8 i = (p >> 4) | (p & 0xf0);
9191312
r6173r6174
9261319            case PIXEL_SIZE_16BIT:
9271320            {
9281321               UINT16 *tc = (UINT16*)texture_cache;
929               int taddr = (tbase + ((t) * (twidth/2)) + (s)) ^ ((t & 1) ? XOR_SWAP_WORD : 0);
1322                    int taddr = taddr_clamp((tbase + ((t) * (twidth/2)) + (s)) ^ ((t & 1) ? XOR_SWAP_WORD : 0));
9301323               UINT16 c = tc[taddr ^ WORD_ADDR_XOR];
9311324               UINT8 i = (c >> 8);
9321325
r6173r6174
9501343            case PIXEL_SIZE_4BIT:
9511344            {
9521345               UINT8 *tc = (UINT8*)texture_cache;
953               int taddr = (tbase + ((t) * twidth) + ((s) / 2)) ^ ((t & 1) ? XOR_SWAP_BYTE : 0);
1346                    int taddr = taddr_clamp((tbase + ((t) * twidth) + ((s) / 2)) ^ ((t & 1) ? XOR_SWAP_BYTE : 0));
9541347               UINT8 c = ((s) & 1) ? (tc[taddr ^ BYTE_ADDR_XOR] & 0xf) : (tc[taddr ^ BYTE_ADDR_XOR] >> 4);
9551348               c |= (c << 4);
9561349
r6173r6174
9631356            case PIXEL_SIZE_8BIT:
9641357            {
9651358               UINT8 *tc = (UINT8*)texture_cache;
966               int taddr = (tbase + ((t) * twidth) + ((s))) ^ ((t & 1) ? XOR_SWAP_BYTE : 0);
1359                    int taddr = taddr_clamp((tbase + ((t) * twidth) + ((s))) ^ ((t & 1) ? XOR_SWAP_BYTE : 0));
9671360               UINT8 c = tc[taddr ^ BYTE_ADDR_XOR];
9681361
9691362               color->r = c;
r6173r6174
9891382}
9901383
9911384#if 1
992#define TEXTURE_PIPELINE(TEX, SSS, SST, NOBILINEAR)                                                               \
993do                                                                                                   \
994{                                                                                                   \
995   if (other_modes.sample_type && !NOBILINEAR)                                                                  \
996   {                                                                                                \
997      COLOR t0, t1, t2, t3;                                                                              \
998      int sss1, sst1, sss2, sst2;                                                                           \
999                                                                                                   \
1000      /* bilinear */                                                                                    \
1001                                                                                                   \
1385#define TEXTURE_PIPELINE(TEX, SSS, SST, NOBILINEAR, CYCLE)                                                                          \
1386do                                                                                                                                  \
1387{                                                                                                                                   \
1388    if (other_modes.sample_type && !NOBILINEAR)                                                                                     \
1389    {                                                                                                                               \
1390        COLOR t0, t1, t2, t3;                                                                                                       \
1391        int sss1, sst1, sss2, sst2;                                                                                                 \
1392                                                                                                                                    \
1393        /* bilinear */                                                                                                              \
1394                                                                                                                                    \
10021395        sss1 = SSS >> 10;                                                                                                           \
10031396        sss2 = sss1 + 1;                                                                                                            \
10041397                                                                                                                                    \
10051398        sst1 = SST >> 10;                                                                                                           \
10061399        sst2 = sst1 + 1;                                                                                                            \
10071400                                                                                                                                    \
1008      CLAMP(sss1, sst1);                                                                                 \
1009      CLAMP(sss2, sst2);                                                                                 \
1010                                                                                                   \
1011        sss1 -= tsl >> 2;                                                                                                           \
1012        sss2 -= tsl >> 2;                                                                                                           \
1401        sss1 -= tsl[CYCLE] >> 2;                                                                                                    \
1402        sss2 -= tsl[CYCLE] >> 2;                                                                                                    \
10131403                                                                                                                                    \
1014        sst1 -= ttl >> 2;                                                                                                           \
1015        sst2 -= ttl >> 2;                                                                                                           \
1404        sst1 -= ttl[CYCLE] >> 2;                                                                                                    \
1405        sst2 -= ttl[CYCLE] >> 2;                                                                                                    \
10161406                                                                                                                                    \
1017        MASK(sss1, sst1);                                                                                                           \
1018        MASK(sss2, sst2);                                                                                                           \
1407        CLAMP(sss1, sst1, CYCLE);                                                                                                   \
1408        CLAMP(sss2, sst2, CYCLE);                                                                                                   \
10191409                                                                                                                                    \
1020      FETCH_TEXEL(&t0, sss1, sst1, twidth, tformat, tsize, tbase);                                                \
1021      FETCH_TEXEL(&t1, sss2, sst1, twidth, tformat, tsize, tbase);                                                \
1022      FETCH_TEXEL(&t2, sss1, sst2, twidth, tformat, tsize, tbase);                                                \
1023      FETCH_TEXEL(&t3, sss2, sst2, twidth, tformat, tsize, tbase);                                                \
1024                                                                                                   \
1025      TEX.r = (((( (t0.r * (0x3ff - (SSS & 0x3ff))) + (t1.r * ((SSS & 0x3ff))) ) >> 10) * (0x3ff - (SST & 0x3ff))) >> 10) +      \
1026                   (((( (t2.r * (0x3ff - (SSS & 0x3ff))) + (t3.r * ((SSS & 0x3ff))) ) >> 10) * ((SST & 0x3ff))) >> 10);      \
1027                                                                                                    \
1028      TEX.g = (((( (t0.g * (0x3ff - (SSS & 0x3ff))) + (t1.g * ((SSS & 0x3ff))) ) >> 10) * (0x3ff - (SST & 0x3ff))) >> 10) +      \
1029                   (((( (t2.g * (0x3ff - (SSS & 0x3ff))) + (t3.g * ((SSS & 0x3ff))) ) >> 10) * ((SST & 0x3ff))) >> 10);      \
1030                                                                                                    \
1031      TEX.b = (((( (t0.b * (0x3ff - (SSS & 0x3ff))) + (t1.b * ((SSS & 0x3ff))) ) >> 10) * (0x3ff - (SST & 0x3ff))) >> 10) +      \
1032                   (((( (t2.b * (0x3ff - (SSS & 0x3ff))) + (t3.b * ((SSS & 0x3ff))) ) >> 10) * ((SST & 0x3ff))) >> 10);      \
1033                                                                                                    \
1034      TEX.a = (((( (t0.a * (0x3ff - (SSS & 0x3ff))) + (t1.a * ((SSS & 0x3ff))) ) >> 10) * (0x3ff - (SST & 0x3ff))) >> 10) +      \
1035                   (((( (t2.a * (0x3ff - (SSS & 0x3ff))) + (t3.a * ((SSS & 0x3ff))) ) >> 10) * ((SST & 0x3ff))) >> 10);      \
1036   }                                                                                                \
1037   else                                                                                             \
1038   {                                                                                                \
1039      int sss1, sst1;                                                                                    \
1410        MASK(sss1, sst1, CYCLE);                                                                                                    \
1411        MASK(sss2, sst2, CYCLE);                                                                                                    \
1412                                                                                                                                    \
1413        FETCH_TEXEL(&t0, sss1, sst1, twidth[CYCLE], tformat[CYCLE], tsize[CYCLE], tbase[CYCLE], tpal[CYCLE]);                       \
1414        FETCH_TEXEL(&t1, sss2, sst1, twidth[CYCLE], tformat[CYCLE], tsize[CYCLE], tbase[CYCLE], tpal[CYCLE]);                       \
1415        FETCH_TEXEL(&t2, sss1, sst2, twidth[CYCLE], tformat[CYCLE], tsize[CYCLE], tbase[CYCLE], tpal[CYCLE]);                       \
1416        FETCH_TEXEL(&t3, sss2, sst2, twidth[CYCLE], tformat[CYCLE], tsize[CYCLE], tbase[CYCLE], tpal[CYCLE]);                       \
1417                                                                                                                                    \
1418        TEX.r = (((( (t0.r * (0x3ff - (SSS & 0x3ff))) + (t1.r * ((SSS & 0x3ff))) ) >> 10) * (0x3ff - (SST & 0x3ff))) >> 10) +       \
1419                         (((( (t2.r * (0x3ff - (SSS & 0x3ff))) + (t3.r * ((SSS & 0x3ff))) ) >> 10) * ((SST & 0x3ff))) >> 10);       \
1420                                                                                                                                    \
1421        TEX.g = (((( (t0.g * (0x3ff - (SSS & 0x3ff))) + (t1.g * ((SSS & 0x3ff))) ) >> 10) * (0x3ff - (SST & 0x3ff))) >> 10) +       \
1422                         (((( (t2.g * (0x3ff - (SSS & 0x3ff))) + (t3.g * ((SSS & 0x3ff))) ) >> 10) * ((SST & 0x3ff))) >> 10);       \
1423                                                                                                                                    \
1424        TEX.b = (((( (t0.b * (0x3ff - (SSS & 0x3ff))) + (t1.b * ((SSS & 0x3ff))) ) >> 10) * (0x3ff - (SST & 0x3ff))) >> 10) +       \
1425                         (((( (t2.b * (0x3ff - (SSS & 0x3ff))) + (t3.b * ((SSS & 0x3ff))) ) >> 10) * ((SST & 0x3ff))) >> 10);       \
1426                                                                                                                                    \
1427        TEX.a = (((( (t0.a * (0x3ff - (SSS & 0x3ff))) + (t1.a * ((SSS & 0x3ff))) ) >> 10) * (0x3ff - (SST & 0x3ff))) >> 10) +       \
1428                         (((( (t2.a * (0x3ff - (SSS & 0x3ff))) + (t3.a * ((SSS & 0x3ff))) ) >> 10) * ((SST & 0x3ff))) >> 10);       \
1429    }                                                                                                                               \
1430    else                                                                                                                            \
1431    {                                                                                                                               \
1432        int sss1, sst1;                                                                                                             \
10401433        sss1 = SSS >> 10;                                                                                                           \
10411434        if ((SSS & 0x3ff) >= 0x200) sss1++;                                                                                         \
10421435                                                                                                                                    \
10431436        sst1 = SST >> 10;                                                                                                           \
10441437        if ((SST & 0x3ff) >= 0x200) sst1++;                                                                                         \
10451438                                                                                                                                    \
1046      CLAMP(sss1, sst1);                                                                                 \
1047                                                                                                   \
1048        sss1 -= tsl >> 2;                                                                                                           \
1049        sst1 -= ttl >> 2;                                                                                                           \
1439        sss1 -= tsl[CYCLE] >> 2;                                                                                                    \
1440        sst1 -= ttl[CYCLE] >> 2;                                                                                                    \
10501441                                                                                                                                    \
1051        MASK(sss1, sst1);                                                                                                           \
1442        CLAMP(sss1, sst1, CYCLE);                                                                                                   \
10521443                                                                                                                                    \
1053      /* point sample */                                                                                 \
1054      FETCH_TEXEL(&TEX, sss1, sst1, twidth, tformat, tsize, tbase);                                                \
1055   }                                                                                                \
1056}                                                                                                   \
1444        MASK(sss1, sst1, CYCLE);                                                                                                    \
1445                                                                                                                                    \
1446        /* point sample */                                                                                                          \
1447        FETCH_TEXEL(&TEX, sss1, sst1, twidth[CYCLE], tformat[CYCLE], tsize[CYCLE], tbase[CYCLE], tpal[CYCLE]);                      \
1448    }                                                                                                                               \
1449}                                                                                                                                   \
10571450while(0)
10581451#endif
10591452
r6173r6174
10611454// TODO: check the correct texel samples and weighting values
10621455
10631456#if 0
1064#define TEXTURE_PIPELINE(TEX, SSS, SST, NOBILINEAR)                                                               \
1065do                                                                                                   \
1066{                                                                                                   \
1067   if (other_modes.sample_type && !NOBILINEAR)                                                                  \
1068   {                                                                                                \
1069      COLOR t0, t1, t2;                                                                                 \
1070      int sss1, sst1, sss2, sst2;                                                                           \
1071                                                                                                   \
1072      /* bilinear */                                                                                    \
1073                                                                                                   \
1074      sss1 = (SSS >> 10) - (tsl >> 2);                                                                     \
1075      sss2 = sss1 + 1;                                                                                 \
1076                                                                                                   \
1077      sst1 = (SST >> 10) - (ttl >> 2);                                                                     \
1078      sst2 = sst1 + 1;                                                                                 \
1079                                                                                                   \
1080      CLAMP(sss1, sst1);                                                                                 \
1081      CLAMP(sss2, sst2);                                                                                 \
1082                                                                                                   \
1083      FETCH_TEXEL(&t0, sss1, sst1, twidth, tformat, tsize, tbase);                                                \
1084      FETCH_TEXEL(&t1, sss2, sst1, twidth, tformat, tsize, tbase);                                                \
1085      FETCH_TEXEL(&t2, sss2, sst2, twidth, tformat, tsize, tbase);                                                \
1086                                                                                                   \
1087      TEX.r = (((( (t0.r * (0x3ff - (SSS & 0x3ff))) + (t1.r * ((SSS & 0x3ff))) ) >> 10) * (0x3ff - (SST & 0x3ff))) >> 10) +      \
1088            (( (t2.r) * ((SST & 0x3ff))) >> 10);                                                            \
1089                                                                                                    \
1090      TEX.g = (((( (t0.g * (0x3ff - (SSS & 0x3ff))) + (t1.g * ((SSS & 0x3ff))) ) >> 10) * (0x3ff - (SST & 0x3ff))) >> 10) +      \
1091            (( (t2.g) * ((SST & 0x3ff))) >> 10);                                                            \
1092                                                                                                    \
1093      TEX.b = (((( (t0.b * (0x3ff - (SSS & 0x3ff))) + (t1.b * ((SSS & 0x3ff))) ) >> 10) * (0x3ff - (SST & 0x3ff))) >> 10) +      \
1094            (( (t2.b) * ((SST & 0x3ff))) >> 10);                                                            \
1095                                                                                                    \
1096      TEX.a = (((( (t0.a * (0x3ff - (SSS & 0x3ff))) + (t1.a * ((SSS & 0x3ff))) ) >> 10) * (0x3ff - (SST & 0x3ff))) >> 10) +      \
1097            (( (t2.a) * ((SST & 0x3ff))) >> 10);                                                            \
1098   }                                                                                                \
1099   else                                                                                             \
1100   {                                                                                                \
1101      int sss1, sst1;                                                                                    \
1102      sss1 = (SSS >> 10) - (tsl >> 2);                                                                     \
1103      if ((SSS & 0x3ff) >= 0x200) sss1++;                                                                     \
1104                                                                                                   \
1105      sst1 = (SST >> 10) - (ttl >> 2);                                                                     \
1106      if ((SST & 0x3ff) >= 0x200) sst1++;                                                                     \
1107                                                                                                   \
1108      CLAMP(sss1, sst1);                                                                                 \
1109                                                                                                   \
1110      /* point sample */                                                                                 \
1111      FETCH_TEXEL(&TEX, sss1, sst1, twidth, tformat, tsize, tbase);                                                \
1112   }                                                                                                \
1113}                                                                                                   \
1457#define TEXTURE_PIPELINE(TEX, SSS, SST, NOBILINEAR)                                                                                 \
1458do                                                                                                                                  \
1459{                                                                                                                                   \
1460    if (other_modes.sample_type && !NOBILINEAR)                                                                                     \
1461    {                                                                                                                               \
1462        COLOR t0, t1, t2;                                                                                                           \
1463        int sss1, sst1, sss2, sst2;                                                                                                 \
1464                                                                                                                                    \
1465        /* bilinear */                                                                                                              \
1466                                                                                                                                    \
1467        sss1 = (SSS >> 10) - (tsl >> 2);                                                                                            \
1468        sss2 = sss1 + 1;                                                                                                            \
1469                                                                                                                                    \
1470        sst1 = (SST >> 10) - (ttl >> 2);                                                                                            \
1471        sst2 = sst1 + 1;                                                                                                            \
1472                                                                                                                                    \
1473        CLAMP(sss1, sst1);                                                                                                          \
1474        CLAMP(sss2, sst2);                                                                                                          \
1475                                                                                                                                    \
1476        FETCH_TEXEL(&t0, sss1, sst1, twidth, tformat, tsize, tbase);                                                                \
1477        FETCH_TEXEL(&t1, sss2, sst1, twidth, tformat, tsize, tbase);                                                                \
1478        FETCH_TEXEL(&t2, sss2, sst2, twidth, tformat, tsize, tbase);                                                                \
1479                                                                                                                                    \
1480        TEX.r = (((( (t0.r * (0x3ff - (SSS & 0x3ff))) + (t1.r * ((SSS & 0x3ff))) ) >> 10) * (0x3ff - (SST & 0x3ff))) >> 10) +       \
1481                (( (t2.r) * ((SST & 0x3ff))) >> 10);                                                                                \
1482                                                                                                                                    \
1483        TEX.g = (((( (t0.g * (0x3ff - (SSS & 0x3ff))) + (t1.g * ((SSS & 0x3ff))) ) >> 10) * (0x3ff - (SST & 0x3ff))) >> 10) +       \
1484                (( (t2.g) * ((SST & 0x3ff))) >> 10);                                                                                \
1485                                                                                                                                    \
1486        TEX.b = (((( (t0.b * (0x3ff - (SSS & 0x3ff))) + (t1.b * ((SSS & 0x3ff))) ) >> 10) * (0x3ff - (SST & 0x3ff))) >> 10) +       \
1487                (( (t2.b) * ((SST & 0x3ff))) >> 10);                                                                                \
1488                                                                                                                                    \
1489        TEX.a = (((( (t0.a * (0x3ff - (SSS & 0x3ff))) + (t1.a * ((SSS & 0x3ff))) ) >> 10) * (0x3ff - (SST & 0x3ff))) >> 10) +       \
1490                (( (t2.a) * ((SST & 0x3ff))) >> 10);                                                                                \
1491    }                                                                                                                               \
1492    else                                                                                                                            \
1493    {                                                                                                                               \
1494        int sss1, sst1;                                                                                                             \
1495        sss1 = (SSS >> 10) - (tsl >> 2);                                                                                            \
1496        if ((SSS & 0x3ff) >= 0x200) sss1++;                                                                                         \
1497                                                                                                                                    \
1498        sst1 = (SST >> 10) - (ttl >> 2);                                                                                            \
1499        if ((SST & 0x3ff) >= 0x200) sst1++;                                                                                         \
1500                                                                                                                                    \
1501        CLAMP(sss1, sst1);                                                                                                          \
1502                                                                                                                                    \
1503        /* point sample */                                                                                                          \
1504        FETCH_TEXEL(&TEX, sss1, sst1, twidth, tformat, tsize, tbase);                                                               \
1505    }                                                                                                                               \
1506}                                                                                                                                   \
11141507while(0)
11151508#endif
11161509
1117#define TEXTURE_PIPELINE1(SSS, SST, NOBILINEAR)            \
1118do                                             \
1119{                                             \
1120   TEXTURE_PIPELINE(texel0_color, SSS, SST, NOBILINEAR);   \
1121}                                             \
1510#define TEXTURE_PIPELINE1(SSS, SST, NOBILINEAR)             \
1511do                                                          \
1512{                                                           \
1513    TEXTURE_PIPELINE(texel0_color, SSS, SST, NOBILINEAR, 0);\
1514}                                                           \
11221515while(0)
11231516
1124#define TEXTURE_PIPELINE2(SSS, SST, NOBILINEAR)            \
1125do                                             \
1126{                                             \
1127   TEXTURE_PIPELINE(texel0_color, SSS, SST, NOBILINEAR);   \
1128   TEXTURE_PIPELINE(texel1_color, SSS, SST, NOBILINEAR);   \
1129}                                             \
1517#define TEXTURE_PIPELINE2(SSS, SST, NOBILINEAR)             \
1518do                                                          \
1519{                                                           \
1520    TEXTURE_PIPELINE(texel0_color, SSS, SST, NOBILINEAR, 0);\
1521    TEXTURE_PIPELINE(texel1_color, SSS, SST, NOBILINEAR, 1);\
1522}                                                           \
11301523while(0)
11311524
1525static int alpha_compare_success(COLOR c1, COLOR c2)
1526{
1527    if( other_modes.alpha_compare_en )
1528    {
1529        if( other_modes.cycle_type == CYCLE_TYPE_1 )
1530        {
1531            if( c1.a > blend_color.a )
1532            {
1533                return 1;
1534            }
1535            else
1536            {
1537                return 0;
1538            }
1539        }
1540        if( other_modes.cycle_type == CYCLE_TYPE_2 )
1541        {
1542            if( c2.a > blend_color.a )
1543            {
1544                return 1;
1545            }
1546            else
1547            {
1548                return 0;
1549            }
1550        }
1551    }
1552    else
1553    {
1554        return 1;
1555    }
1556    return 0;
1557}
11321558
1559/* Does not yet work properly */
1560static void compress_z(UINT32 in, UINT16 *out_z, UINT8 *out_dz)
1561{
1562    UINT32 rounded = (in >> 13) & 0x0003ffff;
1563    UINT16 full_delta = in & 0x0000ffff;
1564    UINT8 exponent = 0;
1565    UINT8 high_delta_bit = 15;
1566    UINT16 mantissa = 0;
1567    UINT32 final_value = 0;
11331568
1569    // Determine exponent and mantissa
1570    for (exponent = 0; exponent < 7; exponent++)
1571    {
1572        if (!(rounded & (0x00020000 >> exponent)))
1573        {
1574            break;
1575        }
1576    }
1577    mantissa = (rounded >> ((exponent != 7) ? (6 - exponent) : 0)) & 0x000007ff;
1578
1579    // Determine the high bit of Z delta
1580    for (high_delta_bit = 15; high_delta_bit > 0; high_delta_bit--)
1581    {
1582        if (full_delta & (1 << high_delta_bit))
1583        {
1584            break;
1585        }
1586    }
1587
1588    // Composite the final value
1589    final_value = (mantissa << 7) | (exponent << 4) | (high_delta_bit & 0x0f);
1590    *out_z = (final_value >> 2) & 0xfffc;
1591    *out_dz = final_value & 0x000f;
1592}
1593
1594/* Does not yet work properly */
1595static void decompress_z(UINT16 in, UINT32 *out)
1596{
1597    UINT16 mantissa = (in >> 5) & 0x07ff;
1598    UINT8 exponent = (in >> 2) & 0x0007;
1599    switch (exponent)
1600    {
1601        case 0:
1602            (*out) = (mantissa << 6);
1603            break;
1604        case 1:
1605            (*out) = 0x20000 | (mantissa << 5);
1606            break;
1607        case 2:
1608            (*out) = 0x30000 | (mantissa << 4);
1609            break;
1610        case 3:
1611            (*out) = 0x38000 | (mantissa << 3);
1612            break;
1613        case 4:
1614            (*out) = 0x3c000 | (mantissa << 2);
1615            break;
1616        case 5:
1617            (*out) = 0x3e000 | (mantissa << 1);
1618            break;
1619        case 6:
1620            (*out) = 0x3f000 | mantissa;
1621            break;
1622        case 7:
1623            (*out) = 0x3f800 | mantissa;
1624            break;
1625    }
1626}
1627
1628static void calculate_coverage( UINT8 *out, UINT8 in, int* ovf, UINT8 *old )
1629{
1630    (*old) = (*out);
1631    if (((*out) + in) > 8)
1632    {
1633        (*ovf) = 1;
1634    }
1635    switch (other_modes.cvg_dest)
1636    {
1637        case 0: // Clamp
1638            if (other_modes.image_read_en)
1639            {
1640                (*out) = (*out) + in;
1641            }
1642            else
1643            {
1644                (*out) = in;
1645            }
1646            if (*out > 8)
1647            {
1648                *out = 8;
1649            }
1650            break;
1651        case 1: // Wrap
1652            if (other_modes.image_read_en)
1653            {
1654                (*out) = ( (*out) - 1 ) + in;
1655            }
1656            else
1657            {
1658                (*out) = in;
1659            }
1660            (*out) &= 0x07;
1661            (*out)++;
1662            break;
1663        case 2: // Zap
1664            (*out) = 8;
1665            break;
1666        case 3: // Save
1667            (*out) = (*out);
1668            break;
1669    }
1670}
1671
11341672static void texture_rectangle_16bit(TEX_RECTANGLE *rect)
11351673{
1136   UINT16 *fb = (UINT16*)&rdram[(fb_address / 4)];
1137   int i, j;
1138   int x1, x2, y1, y2;
1139   int tsl, tsh, ttl, tth;
1140   int s, t;
1141   int twidth, tformat, tsize, tbase;
1142   int clamp_s, clamp_t, mask_s, mask_t, mirror_s, mirror_t;
1143   int clipx1, clipx2, clipy1, clipy2;
1674    UINT16 *fb = (UINT16*)&rdram[(fb_address / 4)];
1675    UINT16 *zb = (UINT16*)&rdram[(zb_address / 4)];
1676    UINT8* cvg_buf = (UINT8*)&cvg_mem[fb_address];
1677    int i, j;
1678    int x1, x2, y1, y2;
1679    int tsl[2], tsh[2], ttl[2], tth[2];
1680    int s, t;
1681    int twidth[2], tformat[2], tsize[2], tbase[2], tpal[2];
1682    int clamp_s[2], clamp_t[2], mask_s[2], mask_t[2], mirror_s[2], mirror_t[2];
1683    int clipx1, clipx2, clipy1, clipy2;
1684    int cycle;
1685    UINT8 cvg_span[RDP_CVG_SPAN_MAX];
1686    int span_index = 0;
1687    int alpha_reject = 0;
1688    COLOR c1, c2;
1689    c1.r = c1.g = c1.b = c1.a = 0;
1690    c2.r = c2.g = c2.b = c2.a = 0;
11441691
1145   x1 = (rect->xh / 4);
1146   x2 = (rect->xl / 4);
1147   y1 = (rect->yh / 4);
1148   y2 = (rect->yl / 4);
1692    x1 = rect->xh;
1693    x2 = rect->xl;
1694    y1 = rect->yh;
1695    y2 = rect->yl;
11491696
1150   if (other_modes.cycle_type == CYCLE_TYPE_FILL || other_modes.cycle_type == CYCLE_TYPE_COPY)
1151   {
1152      rect->dsdx /= 4;
1153      x2 += 1;
1154      y2 += 1;
1155   }
1697    if (other_modes.cycle_type == CYCLE_TYPE_FILL || other_modes.cycle_type == CYCLE_TYPE_COPY)
1698    {
1699        rect->dsdx /= 4;
1700        x2 += 1;
1701        y2 += 1;
1702    }
11561703
1157   clipx1 = clip.xh / 4;
1158   clipx2 = clip.xl / 4;
1159   clipy1 = clip.yh / 4;
1160   clipy2 = clip.yl / 4;
1704    clipx1 = clip.xh;
1705    clipx2 = clip.xl;
1706    clipy1 = clip.yh;
1707    clipy2 = clip.yl;
11611708
1162   // clip
1163   if (x1 < clipx1)
1164   {
1165      x1 = clipx1;
1166   }
1167   if (y1 < clipy1)
1168   {
1169      y1 = clipy1;
1170   }
1171   if (x2 >= clipx2)
1172   {
1173      x2 = clipx2-1;
1174   }
1175   if (y2 >= clipy2)
1176   {
1177      y2 = clipy2-1;
1178   }
1709    // clip
1710    if (x1 < clipx1)
1711    {
1712        x1 = clipx1;
1713    }
1714    if (y1 < clipy1)
1715    {
1716        y1 = clipy1;
1717    }
1718    if (x2 >= clipx2)
1719    {
1720        x2 = clipx2-1;
1721    }
1722    if (y2 >= clipy2)
1723    {
1724        y2 = clipy2-1;
1725    }
11791726
1180   tsl = tile[rect->tilenum].sl;
1181   tsh = tile[rect->tilenum].sh;
1182   ttl = tile[rect->tilenum].tl;
1183   tth = tile[rect->tilenum].th;
1727    for( cycle = 0; cycle < 2; cycle++ )
1728    {
1729        tsl[cycle] = tile[rect->tilenum + cycle].sl;
1730        tsh[cycle] = tile[rect->tilenum + cycle].sh;
1731        ttl[cycle] = tile[rect->tilenum + cycle].tl;
1732        tth[cycle] = tile[rect->tilenum + cycle].th;
11841733
1185   twidth = tile[rect->tilenum].line;
1186   tformat = tile[rect->tilenum].format;
1187   tsize = tile[rect->tilenum].size;
1188   tbase = tile[rect->tilenum].tmem;
1734        twidth[cycle] = tile[rect->tilenum + cycle].line;
1735        tformat[cycle] = tile[rect->tilenum + cycle].format;
1736        tsize[cycle] = tile[rect->tilenum + cycle].size;
1737        tbase[cycle] = tile[rect->tilenum + cycle].tmem;
1738        tpal[cycle] = tile[rect->tilenum + cycle].palette;
11891739
1190    clamp_t = tile[rect->tilenum].ct;
1191    clamp_s = tile[rect->tilenum].cs;
1740        clamp_t[cycle] = tile[rect->tilenum + cycle].ct;
1741        clamp_s[cycle] = tile[rect->tilenum + cycle].cs;
11921742
1193   mirror_t = tile[rect->tilenum].mt;
1194   mirror_s = tile[rect->tilenum].ms;
1195   mask_t = tile[rect->tilenum].mask_t;
1196   mask_s = tile[rect->tilenum].mask_s;
1743        mirror_t[cycle] = tile[rect->tilenum + cycle].mt;
1744        mirror_s[cycle] = tile[rect->tilenum + cycle].ms;
1745        mask_t[cycle] = tile[rect->tilenum + cycle].mask_t;
1746        mask_s[cycle] = tile[rect->tilenum + cycle].mask_s;
1747    }
11971748
1198   t = (int)(rect->t) << 5;
1749    t = (int)(rect->t) << 5;
11991750
1200   if (other_modes.cycle_type == CYCLE_TYPE_1 || other_modes.cycle_type == CYCLE_TYPE_2)
1201   {
1202   }
1751    if (other_modes.cycle_type == CYCLE_TYPE_1)
1752    {
1753        for (j = y1; j < y2; j += ((j == y1) ? (4 - (y1 & 0x0003)) : 4) )
1754        {
1755            int fb_index = (j >> 2) * fb_width;
12031756
1204   if (other_modes.cycle_type == CYCLE_TYPE_1)
1205   {
1206      for (j = y1; j < y2; j++)
1207      {
1208         int fb_index = j * fb_width;
1757            s = (int)(rect->s) << 5;
12091758
1210         s = (int)(rect->s) << 5;
1759           memset( cvg_span, 0, RDP_CVG_SPAN_MAX );
12111760
1212         for (i = x1; i < x2; i++)
1213         {
1214            COLOR c;
1761            for (span_index = j; span_index < ((j &~ 0x0003) + 4) && span_index < y2; span_index++)
1762            {
1763                int span_start = x1;
1764                int span_end = x2;
1765                int span_iter = 0;
1766                if (x1 < 0)
1767                {
1768                    span_start = 0;
1769                }
1770                if (x2 >= (RDP_CVG_SPAN_MAX << 2))
1771                {
1772                    span_end = (RDP_CVG_SPAN_MAX << 2) - 1;
1773                }
1774                //printf( "%d: Span from %d to %d\n", j, span_start, span_end );
1775                for (span_iter=span_start; span_iter < span_end; span_iter++)
1776                {
1777                    if ((span_iter & 1) == (span_index & 1))
1778                    {
1779                        cvg_span[span_iter >> 2]++;
1780                    }
1781                }
1782            }
12151783
1216            //ss = (s >> 10) - (tsl >> 2);
1217            //if ((s & 0x3ff) >= 0x200) ss++;
1218            //st = (t >> 10) - (ttl >> 2);
1219            //if ((t & 0x3ff) >= 0x200) st++;
1784            for (i = x1; i < x2; i += 4)
1785            {
1786                if (i >= 0 && i < (RDP_CVG_SPAN_MAX << 2) && cvg_span[i >> 2] > 0)
1787                {
1788                    TEXTURE_PIPELINE1(s, t, (rect->dsdx == (1 << 10)) && (rect->dtdy == (1 << 10)));
12201789
1221            //CLAMP(ss, st);
1222            //FETCH_TEXEL(&texel0_color, ss, st, twidth, tformat, tsize, tbase);
1790                    c1 = COLOR_COMBINER(0);
12231791
1224            TEXTURE_PIPELINE1(s, t, (rect->dsdx == (1 << 10)) && (rect->dtdy == (1 << 10)));
1792                    if( alpha_compare_success(c1, c2) )
1793                    {
1794                        int dith = dither_matrix_4x4[(((j >> 2) & 3) << 2) + (((i >> 2)^WORD_ADDR_XOR) & 3)];
1795                        int cvg_ovf = 0;
1796                        UINT8 cvg_old = 0;
1797                        calculate_coverage( &cvg_buf[(fb_index + (i >> 2)) ^ WORD_ADDR_XOR], cvg_span[i >> 2], &cvg_ovf, &cvg_old );
1798                        if ((other_modes.color_on_cvg && cvg_ovf) || !other_modes.color_on_cvg)
1799                        {
1800                            BLENDER1_16(&fb[(fb_index + (i >> 2)) ^ WORD_ADDR_XOR], c1, dith, &cvg_span[i >> 2], &cvg_old, &alpha_reject);
12251801
1226            c = COLOR_COMBINER(0);
1802                            if( other_modes.z_update_en && other_modes.z_source_sel && !alpha_reject )
1803                            {
1804                                zb[(fb_index + (i >> 2)) ^ WORD_ADDR_XOR] = primitive_z;
1805                            }
1806                        }
1807                    }
1808                }
12271809
1228            {
1229               int dith = dither_matrix_4x4[(((j) & 3) << 2) + ((i^WORD_ADDR_XOR) & 3)];
1230               BLENDER1_16(&fb[(fb_index + i) ^ WORD_ADDR_XOR], c, dith);
1231            }
1810                s += rect->dsdx;
1811            }
12321812
1233            s += rect->dsdx;
1234         }
1813            t += rect->dtdy;
1814        }
1815    }
1816    else if (other_modes.cycle_type == CYCLE_TYPE_2)
1817    {
1818        for (j = y1; j < y2; j += (j == y1) ? (4 - (y1 & 0x0003)): 4 )
1819        {
1820            int fb_index = (j >> 2) * fb_width;
12351821
1236         t += rect->dtdy;
1237      }
1238   }
1239   else if (other_modes.cycle_type == CYCLE_TYPE_2)
1240   {
1241      for (j = y1; j < y2; j++)
1242      {
1243         int fb_index = j * fb_width;
1822            s = (int)(rect->s) << 5;
12441823
1245         s = (int)(rect->s) << 5;
1824           memset( cvg_span, 0, RDP_CVG_SPAN_MAX );
12461825
1247         for (i = x1; i < x2; i++)
1248         {
1249            COLOR c1, c2;
1826            for (span_index = j; span_index < ((j &~ 0x0003) + 4) && span_index < y2; span_index++)
1827            {
1828                int span_start = x1;
1829                int span_end = x2;
1830                int span_iter = 0;
1831                if (x1 < 0)
1832                {
1833                    span_start = 0;
1834                }
1835                if (x2 >= (RDP_CVG_SPAN_MAX << 2))
1836                {
1837                    span_end = (RDP_CVG_SPAN_MAX << 2) - 1;
1838                }
1839                for (span_iter=span_start; span_iter < span_end; span_iter++)
1840                {
1841                    if ((span_iter & 1) == (span_index & 1))
1842                    {
1843                        cvg_span[span_iter >> 2]++;
1844                    }
1845                }
1846            }
12501847
1251            //ss = (s >> 10) - (tsl >> 2);
1252            //if ((s & 0x3ff) >= 0x200) ss++;
1253            //st = (t >> 10) - (ttl >> 2);
1254            //if ((t & 0x3ff) >= 0x200) st++;
1848            for (i = x1; i < x2; i += 4)
1849            {
1850                if (i >= 0 && i < (RDP_CVG_SPAN_MAX << 2) && cvg_span[i >> 2] > 0)
1851                {
1852                    TEXTURE_PIPELINE2(s, t, (rect->dsdx == (1 << 10)) && (rect->dtdy == (1 << 10)));
12551853
1256            //CLAMP(ss, st);
1257            //FETCH_TEXEL(&texel0_color, ss, st, twidth, tformat, tsize, tbase);
1258            //FETCH_TEXEL(&texel1_color, ss, st, twidth, tformat, tsize, tbase);
1854                    c1 = COLOR_COMBINER(0);
1855                    c2 = COLOR_COMBINER(1);
12591856
1260            TEXTURE_PIPELINE2(s, t, (rect->dsdx == (1 << 10)) && (rect->dtdy == (1 << 10)));
1857                    if( alpha_compare_success(c1, c2) )
1858                    {
1859                        int dith = dither_matrix_4x4[((((j >> 2)) & 3) << 2) + (((i >> 2)^WORD_ADDR_XOR) & 3)];
1860                        int cvg_ovf = 0;
1861                        UINT8 cvg_old = 0;
1862                        calculate_coverage( &cvg_buf[(fb_index + (i >> 2)) ^ WORD_ADDR_XOR], cvg_span[i >> 2], &cvg_ovf, &cvg_old );
1863                        if ((other_modes.color_on_cvg && cvg_ovf) || !other_modes.color_on_cvg)
1864                        {
1865                            BLENDER2_16(&fb[(fb_index + (i >> 2)) ^ WORD_ADDR_XOR], c1, c2, dith, &cvg_span[i >> 2], &cvg_old, &alpha_reject);
12611866
1262            c1 = COLOR_COMBINER(0);
1263            c2 = COLOR_COMBINER(1);
1867                            if( other_modes.z_update_en && other_modes.z_source_sel && !alpha_reject )
1868                            {
1869                                zb[(fb_index + (i >> 2)) ^ WORD_ADDR_XOR] = primitive_z;
1870                            }
1871                        }
1872                    }
1873                }
12641874
1265            //BLENDER2_16(&fb[(fb_index + i) ^ WORD_ADDR_XOR], c1, c2);
1266            {
1267               int dith = dither_matrix_4x4[(((j) & 3) << 2) + ((i^WORD_ADDR_XOR) & 3)];
1268               BLENDER2_16(&fb[(fb_index + i) ^ WORD_ADDR_XOR], c1, c2, dith);
1269            }
1875                s += rect->dsdx;
1876            }
12701877
1271            s += rect->dsdx;
1272         }
1878            t += rect->dtdy;
1879        }
1880    }
1881    else if (other_modes.cycle_type == CYCLE_TYPE_COPY)
1882    {
1883        for (j = y1; j < y2; j += (j == y1) ? (4 - (y1 & 0x0003)): 4 )
1884        {
1885            int fb_index = (j >> 2) * fb_width;
12731886
1274         t += rect->dtdy;
1275      }
1276   }
1277   else if (other_modes.cycle_type == CYCLE_TYPE_COPY)
1278   {
1279      for (j = y1; j < y2; j++)
1280      {
1281         int fb_index = j * fb_width;
1887            s = (int)(rect->s) << 5;
12821888
1283         s = (int)(rect->s) << 5;
1889           memset( cvg_span, 0, RDP_CVG_SPAN_MAX );
12841890
1285         for (i = x1; i < x2; i++)
1286         {
1287         /*
1288                if (clamp_s)
1891            for (span_index = j; span_index < ((j &~ 0x0003) + 4) && span_index < y2; span_index++)
1892            {
1893                int span_start = x1;
1894                int span_end = x2;
1895                int span_iter = 0;
1896                if (x1 < 0)
12891897                {
1290                    if (s < (tsl << 8)) s = (tsl << 8);
1291                    if (s > (tsh << 8)) s = (tsh << 8);
1898                    span_start = 0;
12921899                }
1293                if (clamp_t)
1900                if (x2 >= (RDP_CVG_SPAN_MAX << 2))
12941901                {
1295                    if (t < (ttl << 8)) t = (ttl << 8);
1296                    if (t > (tth << 8)) t = (tth << 8);
1902                    span_end = (RDP_CVG_SPAN_MAX << 2) - 1;
12971903                }
1298                if (mask_s)
1904                for (span_iter=span_start; span_iter < span_end; span_iter++)
12991905                {
1300                    s &= (((UINT32)(0xffffffff) >> (32-mask_s)) << 10) | 0x3ff;
1906                    if ((span_iter & 1) == (span_index & 1))
1907                    {
1908                        cvg_span[span_iter >> 2]++;
1909                    }
13011910                }
1302                if (mask_t)
1303                {
1304                    t &= (((UINT32)(0xffffffff) >> (32-mask_t)) << 10) | 0x3ff;
1305                }
1911            }
13061912
1307                FETCH_TEXEL(&c, (s-tile[rect->tilenum].sl) >> 10, (t-tile[rect->tilenum].tl) >> 10, twidth, tformat, tsize, tbase);
1308            */
1309            //ss = (s >> 10) - (tsl >> 2);
1310            //if ((s & 0x3ff) >= 0x200) ss++;
1311            //st = (t >> 10) - (ttl >> 2);
1312            //if ((t & 0x3ff) >= 0x200) st++;
1913            // MooglyGuy note: Fix this, this is probably completely wrong
13131914
1314            //CLAMP(ss, st);
1315            //FETCH_TEXEL(&c, ss, st, twidth, tformat, tsize, tbase);
1915            for (i = x1; i < x2; i += 4)
1916            {
1917                if (i >= 0 && i < (RDP_CVG_SPAN_MAX << 2) && cvg_span[i >> 2] > 0)
1918                {
1919                    TEXTURE_PIPELINE1(s, t, (rect->dsdx == (1 << 10)) && (rect->dtdy == (1 << 10)));
13161920
1317            TEXTURE_PIPELINE1(s, t, (rect->dsdx == (1 << 10)) && (rect->dtdy == (1 << 10)));
1921                    if( texel0_color.a != 0 )
1922                    {
1923                        int cvg_ovf = 0;
1924                        UINT8 cvg_old = 0;
1925                        calculate_coverage( &cvg_buf[(fb_index + (i >> 2)) ^ WORD_ADDR_XOR], cvg_span[i >> 2], &cvg_ovf, &cvg_old );
1926                        if ((other_modes.color_on_cvg && cvg_ovf) || !other_modes.color_on_cvg)
1927                        {
1928                            fb[(fb_index + (i >> 2)) ^ WORD_ADDR_XOR] = ((texel0_color.r >> 3) << 11) | ((texel0_color.g >> 3) << 6) | ((texel0_color.b >> 3) << 1) | ((cvg_buf[(fb_index + (i >> 2)) ^ WORD_ADDR_XOR] - 1) >> 2);
1929                            if( other_modes.z_update_en && other_modes.z_source_sel )
1930                            {
1931                                zb[(fb_index + (i >> 2)) ^ WORD_ADDR_XOR] = primitive_z;
1932                            }
1933                        }
1934                    }
1935                }
13181936
1319            if (texel0_color.a != 0)
1320            {
1321               fb[(fb_index + i) ^ WORD_ADDR_XOR] = ((texel0_color.r >> 3) << 11) | ((texel0_color.g >> 3) << 6) | ((texel0_color.b >> 3) << 1);
1322            }
1937                s += rect->dsdx;
1938            }
13231939
1324            s += rect->dsdx;
1325         }
1326
1327         t += rect->dtdy;
1328      }
1329   }
1330   else
1331   {
1332      fatalerror("texture_rectangle_16bit: unknown cycle type %d\n", other_modes.cycle_type);
1333   }
1940            t += rect->dtdy;
1941        }
1942    }
1943    else
1944    {
1945        fatalerror("texture_rectangle_16bit: unknown cycle type %d\n", other_modes.cycle_type);
1946    }
13341947}
13351948
13361949/*****************************************************************************/
13371950
1338INLINE void BLENDER1_32(UINT32 *fb, COLOR c)
1951INLINE void BLENDER1_32(UINT32 *fb, COLOR c, UINT8 *new_cvg, UINT8 *old_cvg, int *alpha_reject)
13391952{
1340   int r, g, b;
1953    int r, g, b;
13411954
1342   pixel_color.r = c.r;   inv_pixel_color.r = 0xff - c.r;
1343   pixel_color.g = c.g;   inv_pixel_color.g = 0xff - c.g;
1344   pixel_color.b = c.b;   inv_pixel_color.b = 0xff - c.b;
1345   pixel_color.a = c.a;   inv_pixel_color.a = 0xff - c.a;
1955    if( other_modes.force_blend )
1956    {
1957        blend_color.r = c.r;
1958        blend_color.g = c.g;
1959        blend_color.b = c.b;
1960    }
13461961
1347   if (other_modes.image_read_en)
1348   {
1349      UINT32 mem = *fb;
1350      memory_color.r = ((mem >> 16) & 0xff);
1351      memory_color.g = ((mem >>  8) & 0xff);
1352      memory_color.b = ((mem >>  0) & 0xff);
1353      memory_color.a = 0;
1354   }
1962    pixel_color.r = c.r;
1963    pixel_color.g = c.g;
1964    pixel_color.b = c.b;
1965    pixel_color.a = c.a;
1966    inv_pixel_color.r = 0xff - *blender1a_r[0];
1967    inv_pixel_color.g = 0xff - *blender1a_g[0];
1968    inv_pixel_color.b = 0xff - *blender1a_b[0];
1969    inv_pixel_color.a = 0xff - *blender1b_a[0];
13551970
1356   r = (((int)(*blender1a_r[0]) * (int)(*blender1b_a[0])) >> 8) +
1357      (((int)(*blender2a_r[0]) * (int)(*blender2b_a[0])) >> 8);
1971    if( other_modes.alpha_cvg_select )
1972    {
1973        if( other_modes.cvg_times_alpha )
1974        {
1975            pixel_color.a = (UINT8)( ( (INT32)(c.a) * (INT32)( (*new_cvg) * 0x20 - 1 ) ) >> 8 );
1976        }
1977        else
1978        {
1979            pixel_color.a = (UINT8)( (INT32)(*new_cvg) * 0x20 - 1 );
1980        }
13581981
1359   g = (((int)(*blender1a_g[0]) * (int)(*blender1b_a[0])) >> 8) +
1360      (((int)(*blender2a_g[0]) * (int)(*blender2b_a[0])) >> 8);
1982        if( pixel_color.a == 0 )
1983        {
1984            *alpha_reject = 1;
1985            return;
1986        }
1987    }
13611988
1362   b = (((int)(*blender1a_b[0]) * (int)(*blender1b_a[0])) >> 8) +
1363      (((int)(*blender2a_b[0]) * (int)(*blender2b_a[0])) >> 8);
1989    if (other_modes.image_read_en)
1990    {
1991        UINT32 mem = *fb;
1992        memory_color.r = ((mem >> 24) & 0xff);
1993        memory_color.g = ((mem >> 16) & 0xff);
1994        memory_color.b = ((mem >>  8) & 0xff);
1995        memory_color.a = /*(UINT8)((INT32)(*old_cvg) * 0x20 - 1)*/0;
1996    }
13641997
1365   //r = g = b = 0xff;
1998    if (((int)(*blender1b_a[0]) + (int)(*blender2b_a[0])) > 0)
1999    {
2000        r = ((int)(*blender1a_r[0]) * (int)(*blender1b_a[0]) +
2001             (int)(*blender2a_r[0]) * (int)(*blender2b_a[0])) /
2002            ((int)(*blender1b_a[0]) + (int)(*blender2b_a[0]));
13662003
1367   if (r > 255) r = 255;
1368   if (g > 255) g = 255;
1369   if (b > 255) b = 255;
2004        g = ((int)(*blender1a_g[0]) * (int)(*blender1b_a[0]) +
2005             (int)(*blender2a_g[0]) * (int)(*blender2b_a[0])) /
2006            ((int)(*blender1b_a[0]) + (int)(*blender2b_a[0]));
13702007
1371   *fb = (r << 16) | (g << 8) | b;
2008        b = ((int)(*blender1a_b[0]) * (int)(*blender1b_a[0]) +
2009             (int)(*blender2a_b[0]) * (int)(*blender2b_a[0])) /
2010            ((int)(*blender1b_a[0]) + (int)(*blender2b_a[0]));
2011    }
2012    else
2013    {
2014        r = g = b = 0;
2015    }
2016
2017    //r = g = b = 0xff;
2018
2019    if (r > 255) r = 255;
2020    if (g > 255) g = 255;
2021    if (b > 255) b = 255;
2022
2023    *fb = (r << 24) | (g << 16) | (b << 8) | 0xe0;
13722024}
13732025
1374INLINE void BLENDER2_32(UINT32 *fb, COLOR c1, COLOR c2)
2026INLINE void BLENDER2_32(UINT32 *fb, COLOR c1, COLOR c2, UINT8 *new_cvg, UINT8 *old_cvg, int *alpha_reject)
13752027{
1376   int r, g, b;
2028    int r, g, b;
13772029
1378   pixel_color.r = c1.r;   inv_pixel_color.r = 0xff - c1.r;
1379   pixel_color.g = c1.g;   inv_pixel_color.g = 0xff - c1.g;
1380   pixel_color.b = c1.b;   inv_pixel_color.b = 0xff - c1.b;
1381   pixel_color.a = c1.a;   inv_pixel_color.a = 0xff - c1.a;
2030    if( other_modes.force_blend )
2031    {
2032        blend_color.r = c2.r;
2033        blend_color.g = c2.g;
2034        blend_color.b = c2.b;
2035    }
13822036
1383   if (other_modes.image_read_en)
1384   {
1385      UINT32 mem = *fb;
1386      memory_color.r = ((mem >> 16) & 0xff);
1387      memory_color.g = ((mem >>  8) & 0xff);
1388      memory_color.b = ((mem >>  0) & 0xff);
1389      memory_color.a = 0;
1390   }
2037    pixel_color.r = c2.r;
2038    pixel_color.g = c2.g;
2039    pixel_color.b = c2.b;
2040    pixel_color.a = c2.a;
2041    inv_pixel_color.r = 0xff - *blender1a_r[0];
2042    inv_pixel_color.g = 0xff - *blender1a_g[0];
2043    inv_pixel_color.b = 0xff - *blender1a_b[0];
2044    inv_pixel_color.a = 0xff - *blender1b_a[0];
13912045
1392   r = (((int)(*blender1a_r[0]) * (int)(*blender1b_a[0])) >> 8) +
1393      (((int)(*blender2a_r[0]) * (int)(*blender2b_a[0])) >> 8);
2046    if( other_modes.alpha_cvg_select )
2047    {
2048        if( other_modes.cvg_times_alpha )
2049        {
2050            pixel_color.a = (UINT8)( ( (INT32)(c2.a) * (INT32)( (*new_cvg) * 0x20 - 1 ) ) >> 8 );
2051        }
2052        else
2053        {
2054            pixel_color.a = (UINT8)( (INT32)(*new_cvg) * 0x20 - 1 );
2055        }
13942056
1395   g = (((int)(*blender1a_g[0]) * (int)(*blender1b_a[0])) >> 8) +
1396      (((int)(*blender2a_g[0]) * (int)(*blender2b_a[0])) >> 8);
2057        if( pixel_color.a == 0 )
2058        {
2059            *alpha_reject = 1;
2060            return;
2061        }
2062    }
13972063
1398   b = (((int)(*blender1a_b[0]) * (int)(*blender1b_a[0])) >> 8) +
1399      (((int)(*blender2a_b[0]) * (int)(*blender2b_a[0])) >> 8);
2064    if (other_modes.image_read_en)
2065    {
2066        UINT32 mem = *fb;
2067        memory_color.r = ((mem >> 24) & 0xff);
2068        memory_color.g = ((mem >> 16) & 0xff);
2069        memory_color.b = ((mem >>  8) & 0xff);
2070        memory_color.a = /*(UINT8)((INT32)(*old_cvg) * 0x20 - 1)*/0;
2071    }
14002072
1401   blended_pixel_color.r = r;
1402   blended_pixel_color.g = g;
1403   blended_pixel_color.b = b;
2073    if (((int)(*blender1b_a[0]) + (int)(*blender2b_a[0])) > 0)
2074    {
2075        r = ((int)(*blender1a_r[0]) * (int)(*blender1b_a[0]) +
2076             (int)(*blender2a_r[0]) * (int)(*blender2b_a[0])) /
2077            ((int)(*blender1b_a[0]) + (int)(*blender2b_a[0]));
14042078
1405   pixel_color.r = c2.r;   inv_pixel_color.r = 0xff - c2.r;
1406   pixel_color.g = c2.g;   inv_pixel_color.g = 0xff - c2.g;
1407   pixel_color.b = c2.b;   inv_pixel_color.b = 0xff - c2.b;
1408   pixel_color.a = c2.a;   inv_pixel_color.a = 0xff - c2.a;
2079        g = ((int)(*blender1a_g[0]) * (int)(*blender1b_a[0]) +
2080             (int)(*blender2a_g[0]) * (int)(*blender2b_a[0])) /
2081            ((int)(*blender1b_a[0]) + (int)(*blender2b_a[0]));
14092082
1410   r = (((int)(*blender1a_r[1]) * (int)(*blender1b_a[1])) >> 8) +
1411      (((int)(*blender2a_r[1]) * (int)(*blender2b_a[1])) >> 8);
2083        b = ((int)(*blender1a_b[0]) * (int)(*blender1b_a[0]) +
2084             (int)(*blender2a_b[0]) * (int)(*blender2b_a[0])) /
2085            ((int)(*blender1b_a[0]) + (int)(*blender2b_a[0]));
2086    }
2087    else
2088    {
2089        r = g = b = 0;
2090    }
14122091
1413   g = (((int)(*blender1a_g[1]) * (int)(*blender1b_a[1])) >> 8) +
1414      (((int)(*blender2a_g[1]) * (int)(*blender2b_a[1])) >> 8);
2092    blended_pixel_color.r = r;
2093    blended_pixel_color.g = g;
2094    blended_pixel_color.b = b;
14152095
1416   b = (((int)(*blender1a_b[1]) * (int)(*blender1b_a[1])) >> 8) +
1417      (((int)(*blender2a_b[1]) * (int)(*blender2b_a[1])) >> 8);
2096    pixel_color.r = c2.r;
2097    pixel_color.g = c2.g;
2098    pixel_color.b = c2.b;
2099    pixel_color.a = c2.a;
2100    if( other_modes.alpha_cvg_select )
2101    {
2102        if( other_modes.cvg_times_alpha )
2103        {
2104            pixel_color.a = (UINT8)( ( (INT32)(c2.a) * (INT32)( (*new_cvg) * 0x20 - 1 ) ) >> 8 );
2105        }
2106        else
2107        {
2108            pixel_color.a = (UINT8)( (INT32)(*new_cvg) * 0x20 - 1 );
2109        }
14182110
1419   if (r > 255) r = 255;
1420   if (g > 255) g = 255;
1421   if (b > 255) b = 255;
2111        if( pixel_color.a == 0 )
2112        {
2113            *alpha_reject = 1;
2114            return;
2115        }
2116    }
2117    inv_pixel_color.r = 0xff - *blender1a_r[0];
2118    inv_pixel_color.g = 0xff - *blender1a_g[0];
2119    inv_pixel_color.b = 0xff - *blender1a_b[0];
2120    inv_pixel_color.a = 0xff - *blender1b_a[0];
14222121
1423   *fb = (r << 16) | (g << 8) | b;
2122    if (((int)(*blender1b_a[1]) + (int)(*blender2b_a[1])) > 0)
2123    {
2124        r = ((int)(*blender1a_r[1]) * (int)(*blender1b_a[1]) +
2125             (int)(*blender2a_r[1]) * (int)(*blender2b_a[1])) /
2126            ((int)(*blender1b_a[1]) + (int)(*blender2b_a[1]));
2127
2128        g = ((int)(*blender1a_g[1]) * (int)(*blender1b_a[1]) +
2129             (int)(*blender2a_g[1]) * (int)(*blender2b_a[1])) /
2130            ((int)(*blender1b_a[1]) + (int)(*blender2b_a[1]));
2131
2132        b = ((int)(*blender1a_b[1]) * (int)(*blender1b_a[1]) +
2133             (int)(*blender2a_b[1]) * (int)(*blender2b_a[1])) /
2134            ((int)(*blender1b_a[1]) + (int)(*blender2b_a[1]));
2135    }
2136    else
2137    {
2138        r = g = b = 0;
2139    }
2140
2141    if (r > 255) r = 255;
2142    if (g > 255) g = 255;
2143    if (b > 255) b = 255;
2144
2145    *fb = (r << 24) | (g << 16) | (b << 8) | 0xe0;
14242146}
14252147
14262148static void fill_rectangle_32bit(RECTANGLE *rect)
14272149{
1428   UINT32 *fb = (UINT32*)&rdram[(fb_address / 4)];
1429   int index, i, j;
1430   int x1 = rect->xh / 4;
1431   int x2 = rect->xl / 4;
1432   int y1 = rect->yh / 4;
1433   int y2 = rect->yl / 4;
1434   int clipx1, clipx2, clipy1, clipy2;
2150    UINT32 *fb = (UINT32*)&rdram[(fb_address / 4)];
2151    UINT8* cvg_buf = (UINT8*)&cvg_mem[fb_address];
2152    int index, i, j;
2153    int x1 = rect->xh / 4;
2154    int x2 = rect->xl / 4;
2155    int y1 = rect->yh / 4;
2156    int y2 = rect->yl / 4;
2157    int clipx1, clipx2, clipy1, clipy2;
2158    int alpha_reject = 0;
2159    COLOR c1, c2;
2160    c1.r = c1.g = c1.b = c1.a = 0;
2161    c2.r = c2.g = c2.b = c2.a = 0;
14352162
1436   // TODO: clip
1437   clipx1 = clip.xh / 4;
1438   clipx2 = clip.xl / 4;
1439   clipy1 = clip.yh / 4;
1440   clipy2 = clip.yl / 4;
2163    // TODO: clip
2164    clipx1 = clip.xh / 4;
2165    clipx2 = clip.xl / 4;
2166    clipy1 = clip.yh / 4;
2167    clipy2 = clip.yl / 4;
14412168
1442   // clip
1443   if (x1 < clipx1)
1444   {
1445      x1 = clipx1;
1446   }
1447   if (y1 < clipy1)
1448   {
1449      y1 = clipy1;
1450   }
1451   if (x2 >= clipx2)
1452   {
1453      x2 = clipx2-1;
1454   }
1455   if (y2 >= clipy2)
1456   {
1457      y2 = clipy2-1;
1458   }
2169    // clip
2170    if (x1 < clipx1)
2171    {
2172        x1 = clipx1;
2173    }
2174    if (y1 < clipy1)
2175    {
2176        y1 = clipy1;
2177    }
2178    if (x2 >= clipx2)
2179    {
2180        x2 = clipx2-1;
2181    }
2182    if (y2 >= clipy2)
2183    {
2184        y2 = clipy2-1;
2185    }
14592186
1460   if (other_modes.cycle_type == CYCLE_TYPE_FILL)
1461   {
1462      for (j=y1; j <= y2; j++)
1463      {
1464         index = j * fb_width;
1465         for (i=x1; i <= x2; i++)
1466         {
1467            fb[(index + i) ^ 1] = fill_color;
1468         }
1469      }
1470   }
1471   else if (other_modes.cycle_type == CYCLE_TYPE_1)
1472   {
1473      shade_color.r = prim_color.r;
1474      shade_color.g = prim_color.g;
1475      shade_color.b = prim_color.b;
1476      shade_color.a = prim_color.a;
2187    if (other_modes.cycle_type == CYCLE_TYPE_FILL)
2188    {
2189        for (j=y1; j <= y2; j++)
2190        {
2191            index = j * fb_width;
2192            for (i=x1; i <= x2; i++)
2193            {
2194                cvg_buf[(index + i)] = (fill_color & 0x000000ff) ? 8 : 1; // Wrong
2195                fb[(index + i) ^ 1] = fill_color;
2196            }
2197        }
2198    }
2199    else if (other_modes.cycle_type == CYCLE_TYPE_1)
2200    {
2201        shade_color.r = prim_color.r;
2202        shade_color.g = prim_color.g;
2203        shade_color.b = prim_color.b;
2204        shade_color.a = prim_color.a;
14772205
1478      for (j=y1; j <= y2; j++)
1479      {
1480         index = j * fb_width;
1481         for (i=x1; i <= x2; i++)
1482         {
1483            COLOR c = COLOR_COMBINER(0);
2206        for (j=y1; j <= y2; j++)
2207        {
2208            index = j * fb_width;
2209            for (i=x1; i <= x2; i++)
2210            {
2211                c1 = COLOR_COMBINER(0);
14842212
1485            BLENDER1_32(&fb[(index + i)], c);
1486         }
1487      }
1488   }
1489   else if (other_modes.cycle_type == CYCLE_TYPE_2)
1490   {
1491      shade_color.r = prim_color.r;
1492      shade_color.g = prim_color.g;
1493      shade_color.b = prim_color.b;
1494      shade_color.a = prim_color.a;
2213                cvg_buf[(index + i)] = 8; // Wrong
2214                BLENDER1_32(&fb[(index + i)], c1, &cvg_buf[(index + i)], &cvg_buf[(index + i)], &alpha_reject);
2215            }
2216        }
2217    }
2218    else if (other_modes.cycle_type == CYCLE_TYPE_2)
2219    {
2220        shade_color.r = prim_color.r;
2221        shade_color.g = prim_color.g;
2222        shade_color.b = prim_color.b;
2223        shade_color.a = prim_color.a;
14952224
1496      for (j=y1; j <= y2; j++)
1497      {
1498         index = j * fb_width;
1499         for (i=x1; i <= x2; i++)
1500         {
1501            COLOR c1 = COLOR_COMBINER(0);
1502            COLOR c2 = COLOR_COMBINER(1);
2225        for (j=y1; j <= y2; j++)
2226        {
2227            index = j * fb_width;
2228            for (i=x1; i <= x2; i++)
2229            {
2230                c1 = COLOR_COMBINER(0);
2231                c2 = COLOR_COMBINER(1);
15032232
1504            BLENDER2_32(&fb[(index + i)], c1, c2);
1505         }
1506      }
1507   }
1508   else
1509   {
1510      fatalerror("fill_rectangle_32bit: cycle type copy");
1511   }
2233                cvg_buf[(index + i)] = 8; // Wrong
2234                BLENDER2_32(&fb[(index + i)], c1, c2, &cvg_buf[(index + i)], &cvg_buf[(index + i)], &alpha_reject);
2235            }
2236        }
2237    }
2238    else
2239    {
2240        fatalerror("fill_rectangle_32bit: cycle type copy");
2241    }
15122242}
15132243
15142244static void texture_rectangle_32bit(TEX_RECTANGLE *rect)
15152245{
1516   UINT32 *fb = (UINT32*)&rdram[(fb_address / 4)];
1517   int i, j;
1518   int x1, x2, y1, y2;
1519   int tsl, tsh, ttl, tth;
1520   int s, t;
1521   int twidth, tformat, tsize, tbase;
1522   int clamp_s, clamp_t, mask_s, mask_t, mirror_s, mirror_t;
1523   int clipx1, clipx2, clipy1, clipy2;
2246    UINT32 *fb = (UINT32*)&rdram[(fb_address / 4)];
2247    UINT16 *zb = (UINT16*)&rdram[(zb_address / 4)];
2248    UINT8* cvg_buf = (UINT8*)&cvg_mem[fb_address];
2249    int i, j;
2250    int x1, x2, y1, y2;
2251    int tsl[2], tsh[2], ttl[2], tth[2];
2252    int s, t;
2253    int twidth[2], tformat[2], tsize[2], tbase[2], tpal[2];
2254    int clamp_s[2], clamp_t[2], mask_s[2], mask_t[2], mirror_s[2], mirror_t[2];
2255    int clipx1, clipx2, clipy1, clipy2;
2256    int cycle;
2257    UINT8 cvg_span[RDP_CVG_SPAN_MAX];
2258    int span_index = 0;
2259    int alpha_reject = 0;
2260    COLOR c1, c2;
2261    c1.r = c1.g = c1.b = c1.a = 0;
2262    c2.r = c2.g = c2.b = c2.a = 0;
15242263
1525   x1 = (rect->xh / 4);
1526   x2 = (rect->xl / 4);
1527   y1 = (rect->yh / 4);
1528   y2 = (rect->yl / 4);
2264    x1 = rect->xh;
2265    x2 = rect->xl;
2266    y1 = rect->yh;
2267    y2 = rect->yl;
15292268
1530   if (other_modes.cycle_type == CYCLE_TYPE_FILL || other_modes.cycle_type == CYCLE_TYPE_COPY)
1531   {
1532      rect->dsdx /= 4;
1533      x2 += 1;
1534      y2 += 1;
1535   }
2269    if (other_modes.cycle_type == CYCLE_TYPE_FILL || other_modes.cycle_type == CYCLE_TYPE_COPY)
2270    {
2271        rect->dsdx /= 4;
2272        x2 += 1;
2273        y2 += 1;
2274    }
15362275
1537   clipx1 = clip.xh / 4;
1538   clipx2 = clip.xl / 4;
1539   clipy1 = clip.yh / 4;
1540   clipy2 = clip.yl / 4;
2276    clipx1 = clip.xh;
2277    clipx2 = clip.xl;
2278    clipy1 = clip.yh;
2279    clipy2 = clip.yl;
15412280
1542   // clip
1543   if (x1 < clipx1)
1544   {
1545      x1 = clipx1;
1546   }
1547   if (y1 < clipy1)
1548   {
1549      y1 = clipy1;
1550   }
1551   if (x2 >= clipx2)
1552   {
1553      x2 = clipx2-1;
1554   }
1555   if (y2 >= clipy2)
1556   {
1557      y2 = clipy2-1;
1558   }
2281    // clip
2282    if (x1 < clipx1)
2283    {
2284        x1 = clipx1;
2285    }
2286    if (y1 < clipy1)
2287    {
2288        y1 = clipy1;
2289    }
2290    if (x2 >= clipx2)
2291    {
2292        x2 = clipx2-1;
2293    }
2294    if (y2 >= clipy2)
2295    {
2296        y2 = clipy2-1;
2297    }
15592298
1560   tsl = tile[rect->tilenum].sl;
1561   tsh = tile[rect->tilenum].sh;
1562   ttl = tile[rect->tilenum].tl;
1563   tth = tile[rect->tilenum].th;
2299    for( cycle = 0; cycle < 2; cycle++ )
2300    {
2301        tsl[cycle] = tile[rect->tilenum + cycle].sl;
2302        tsh[cycle] = tile[rect->tilenum + cycle].sh;
2303        ttl[cycle] = tile[rect->tilenum + cycle].tl;
2304        tth[cycle] = tile[rect->tilenum + cycle].th;
15642305
1565   twidth = tile[rect->tilenum].line;
1566   tformat = tile[rect->tilenum].format;
1567   tsize = tile[rect->tilenum].size;
1568   tbase = tile[rect->tilenum].tmem;
2306        twidth[cycle] = tile[rect->tilenum + cycle].line;
2307        tformat[cycle] = tile[rect->tilenum + cycle].format;
2308        tsize[cycle] = tile[rect->tilenum + cycle].size;
2309        tbase[cycle] = tile[rect->tilenum + cycle].tmem;
2310        tpal[cycle] = tile[rect->tilenum + cycle].palette;
15692311
1570    clamp_t = tile[rect->tilenum].ct;
1571    clamp_s = tile[rect->tilenum].cs;
2312        clamp_t[cycle] = tile[rect->tilenum + cycle].ct;
2313        clamp_s[cycle] = tile[rect->tilenum + cycle].cs;
15722314
1573   mirror_t = tile[rect->tilenum].mt;
1574   mirror_s = tile[rect->tilenum].ms;
1575   mask_t = tile[rect->tilenum].mask_t;
1576   mask_s = tile[rect->tilenum].mask_s;
2315        mirror_t[cycle] = tile[rect->tilenum + cycle].mt;
2316        mirror_s[cycle] = tile[rect->tilenum + cycle].ms;
2317        mask_t[cycle] = tile[rect->tilenum + cycle].mask_t;
2318        mask_s[cycle] = tile[rect->tilenum + cycle].mask_s;
2319    }
15772320
1578   t = (int)(rect->t) << 5;
2321    t = (int)(rect->t) << 5;
15792322
1580   if (other_modes.cycle_type == CYCLE_TYPE_1 || other_modes.cycle_type == CYCLE_TYPE_2)
1581   {
1582   }
2323    if (other_modes.cycle_type == CYCLE_TYPE_1)
2324    {
2325        for (j = y1; j < y2; j += ((j == y1) ? (4 - (y1 & 0x0003)) : 4) )
2326        {
2327            int fb_index = (j >> 2) * fb_width;
15832328
1584   if (other_modes.cycle_type == CYCLE_TYPE_1)
1585   {
1586      for (j = y1; j < y2; j++)
1587      {
1588         int fb_index = j * fb_width;
2329            s = (int)(rect->s) << 5;
15892330
1590         s = (int)(rect->s) << 5;
2331           memset( cvg_span, 0, RDP_CVG_SPAN_MAX );
15912332
1592         for (i = x1; i < x2; i++)
1593         {
1594            COLOR c;
2333            for (span_index = j; span_index < ((j &~ 0x0003) + 4) && span_index < y2; span_index++)
2334            {
2335                int span_start = x1;
2336                int span_end = x2;
2337                int span_iter = 0;
2338                if (x1 < 0)
2339                {
2340                    span_start = 0;
2341                }
2342                if (x2 >= (RDP_CVG_SPAN_MAX << 2))
2343                {
2344                    span_end = (RDP_CVG_SPAN_MAX << 2) - 1;
2345                }
2346                //printf( "%d: Span from %d to %d\n", j, span_start, span_end );
2347                for (span_iter=span_start; span_iter < span_end; span_iter++)
2348                {
2349                    if ((span_iter & 1) == (span_index & 1))
2350                    {
2351                        cvg_span[span_iter >> 2]++;
2352                    }
2353                }
2354            }
15952355
1596            //ss = (s >> 10) - (tsl >> 2);
1597            //if ((s & 0x3ff) >= 0x200) ss++;
1598            //st = (t >> 10) - (ttl >> 2);
1599            //if ((t & 0x3ff) >= 0x200) st++;
2356            for (i = x1; i < x2; i += 4)
2357            {
2358                if (i >= 0 && i < (RDP_CVG_SPAN_MAX << 2) && cvg_span[i >> 2] > 0)
2359                {
2360                    TEXTURE_PIPELINE1(s, t, (rect->dsdx == (1 << 10)) && (rect->dtdy == (1 << 10)));
16002361
1601            //CLAMP(ss, st);
1602            //FETCH_TEXEL(&texel0_color, ss, st, twidth, tformat, tsize, tbase);
2362                    c1 = COLOR_COMBINER(0);
16032363
1604            TEXTURE_PIPELINE1(s, t, (rect->dsdx == (1 << 10)) && (rect->dtdy == (1 << 10)));
2364                    if( alpha_compare_success(c1, c2) )
2365                    {
2366                        int cvg_ovf = 0;
2367                        UINT8 cvg_old = 0;
2368                        calculate_coverage( &cvg_buf[(fb_index + (i >> 2)) ^ WORD_ADDR_XOR], cvg_span[i >> 2], &cvg_ovf, &cvg_old );
2369                        if ((other_modes.color_on_cvg && cvg_ovf) || !other_modes.color_on_cvg)
2370                        {
2371                            BLENDER1_32(&fb[(fb_index + (i >> 2))], c1, &cvg_span[i >> 2], &cvg_old, &alpha_reject);
16052372
1606            c = COLOR_COMBINER(0);
2373                            if( other_modes.z_update_en && other_modes.z_source_sel && !alpha_reject )
2374                            {
2375                                zb[(fb_index + (i >> 2)) ^ WORD_ADDR_XOR] = primitive_z;
2376                            }
2377                        }
2378                    }
2379                }
16072380
1608            BLENDER1_32(&fb[(fb_index + i)], c);
2381                s += rect->dsdx;
2382            }
16092383
1610            s += rect->dsdx;
1611         }
2384            t += rect->dtdy;
2385        }
2386    }
2387    else if (other_modes.cycle_type == CYCLE_TYPE_2)
2388    {
2389        for (j = y1; j < y2; j += (j == y1) ? (4 - (y1 & 0x0003)): 4 )
2390        {
2391            int fb_index = (j >> 2) * fb_width;
16122392
1613         t += rect->dtdy;
1614      }
1615   }
1616   else if (other_modes.cycle_type == CYCLE_TYPE_2)
1617   {
1618      for (j = y1; j < y2; j++)
1619      {
1620         int fb_index = j * fb_width;
2393            s = (int)(rect->s) << 5;
16212394
1622         s = (int)(rect->s) << 5;
2395           memset( cvg_span, 0, RDP_CVG_SPAN_MAX );
16232396
1624         for (i = x1; i < x2; i++)
1625         {
1626            COLOR c1, c2;
2397            for (span_index = j; span_index < ((j &~ 0x0003) + 4) && span_index < y2; span_index++)
2398            {
2399                int span_start = x1;
2400                int span_end = x2;
2401                int span_iter = 0;
2402                if (x1 < 0)
2403                {
2404                    span_start = 0;
2405                }
2406                if (x2 >= (RDP_CVG_SPAN_MAX << 2))
2407                {
2408                    span_end = (RDP_CVG_SPAN_MAX << 2) - 1;
2409                }
2410                for (span_iter=span_start; span_iter < span_end; span_iter++)
2411                {
2412                    if ((span_iter & 1) == (span_index & 1))
2413                    {
2414                        cvg_span[span_iter >> 2]++;
2415                    }
2416                }
2417            }
16272418
1628            //ss = (s >> 10) - (tsl >> 2);
1629            //if ((s & 0x3ff) >= 0x200) ss++;
1630            //st = (t >> 10) - (ttl >> 2);
1631            //if ((t & 0x3ff) >= 0x200) st++;
2419            for (i = x1; i < x2; i += 4)
2420            {
2421                if (i >= 0 && i < (RDP_CVG_SPAN_MAX << 2) && cvg_span[i >> 2] > 0)
2422                {
2423                    TEXTURE_PIPELINE2(s, t, (rect->dsdx == (1 << 10)) && (rect->dtdy == (1 << 10)));
16322424
1633            //CLAMP(ss, st);
1634            //FETCH_TEXEL(&texel0_color, ss, st, twidth, tformat, tsize, tbase);
1635            //FETCH_TEXEL(&texel1_color, ss, st, twidth, tformat, tsize, tbase);
2425                    c1 = COLOR_COMBINER(0);
2426                    c2 = COLOR_COMBINER(1);
16362427
1637            TEXTURE_PIPELINE2(s, t, (rect->dsdx == (1 << 10)) && (rect->dtdy == (1 << 10)));
2428                    if( alpha_compare_success(c1, c2) )
2429                    {
2430                        int cvg_ovf = 0;
2431                        UINT8 cvg_old = 0;
2432                        calculate_coverage( &cvg_buf[(fb_index + (i >> 2)) ^ WORD_ADDR_XOR], cvg_span[i >> 2], &cvg_ovf, &cvg_old );
2433                        if ((other_modes.color_on_cvg && cvg_ovf) || !other_modes.color_on_cvg)
2434                        {
2435                            BLENDER2_32(&fb[fb_index + (i >> 2)], c1, c2, &cvg_span[i >> 2], &cvg_old, &alpha_reject);
16382436
1639            c1 = COLOR_COMBINER(0);
1640            c2 = COLOR_COMBINER(1);
2437                            if( other_modes.z_update_en && other_modes.z_source_sel && !alpha_reject )
2438                            {
2439                                zb[(fb_index + (i >> 2)) ^ WORD_ADDR_XOR] = primitive_z;
2440                            }
2441                        }
2442                    }
2443                }
16412444
1642            BLENDER2_32(&fb[(fb_index + i)], c1, c2);
2445                s += rect->dsdx;
2446            }
16432447
1644            s += rect->dsdx;
1645         }
2448            t += rect->dtdy;
2449        }
2450    }
2451    else if (other_modes.cycle_type == CYCLE_TYPE_COPY)
2452    {
2453        for (j = y1; j < y2; j++)
2454        {
2455            int fb_index = j * fb_width;
16462456
1647         t += rect->dtdy;
1648      }
1649   }
1650   else if (other_modes.cycle_type == CYCLE_TYPE_COPY)
1651   {
1652      for (j = y1; j < y2; j++)
1653      {
1654         int fb_index = j * fb_width;
2457            s = (int)(rect->s) << 5;
16552458
1656         s = (int)(rect->s) << 5;
2459           // MooglyGuy note: This is probably completely wrong, fix it
16572460
1658         for (i = x1; i < x2; i++)
1659         {
1660         /*
2461            for (i = x1; i < x2; i++)
2462            {
2463            /*
16612464                if (clamp_s)
16622465                {
16632466                    if (s < (tsl << 8)) s = (tsl << 8);
r6173r6174
16752478                if (mask_t)
16762479                {
16772480                    t &= (((UINT32)(0xffffffff) >> (32-mask_t)) << 10) | 0x3ff;
1678                }
16792481
2482
16802483                FETCH_TEXEL(&c, (s-tile[rect->tilenum].sl) >> 10, (t-tile[rect->tilenum].tl) >> 10, twidth, tformat, tsize, tbase);
16812484            */
1682            //ss = (s >> 10) - (tsl >> 2);
1683            //if ((s & 0x3ff) >= 0x200) ss++;
1684            //st = (t >> 10) - (ttl >> 2);
1685            //if ((t & 0x3ff) >= 0x200) st++;
2485                //ss = (s >> 10) - (tsl >> 2);
2486                //if ((s & 0x3ff) >= 0x200) ss++;
2487                //st = (t >> 10) - (ttl >> 2);
2488                //if ((t & 0x3ff) >= 0x200) st++;
16862489
1687            TEXTURE_PIPELINE1(s, t, (rect->dsdx == (1 << 10)) && (rect->dtdy == (1 << 10)));
2490                TEXTURE_PIPELINE1(s, t, (rect->dsdx == (1 << 10)) && (rect->dtdy == (1 << 10)));
16882491
1689            //CLAMP(ss, st);
1690            //FETCH_TEXEL(&c, ss, st, twidth, tformat, tsize, tbase);
2492                //CLAMP(ss, st);
2493                //FETCH_TEXEL(&c, ss, st, twidth, tformat, tsize, tbase);
16912494
1692            if (texel0_color.a != 0)
1693            {
1694               fb[(fb_index + i) ^ WORD_ADDR_XOR] = (texel0_color.r << 16) | (texel0_color.g << 8) | (texel0_color.b << 0);
1695            }
2495                if (texel0_color.a != 0)
2496                {
2497                    fb[(fb_index + i) ^ WORD_ADDR_XOR] = (texel0_color.r << 24) | (texel0_color.g << 16) | (texel0_color.b << 8) | 0xe0;
2498                }
16962499
1697            s += rect->dsdx;
1698         }
2500                s += rect->dsdx;
2501            }
16992502
1700         t += rect->dtdy;
1701      }
1702   }
1703   else
1704   {
1705      fatalerror("texture_rectangle_32bit: unknown cycle type %d\n", other_modes.cycle_type);
1706   }
2503            t += rect->dtdy;
2504        }
2505    }
2506    else
2507    {
2508        fatalerror("texture_rectangle_32bit: unknown cycle type %d\n", other_modes.cycle_type);
2509    }
17072510}
17082511
17092512
17102513static void render_spans_32(int start, int end, int tilenum, int shade, int texture, int zbuffer, int flip)
17112514{
1712   UINT32 *fb = (UINT32*)&rdram[fb_address / 4];
1713   UINT16 *zb = (UINT16*)&rdram[zb_address / 4];
1714   int i, j;
1715   int tsl, tsh, ttl, tth;
1716   int twidth, tformat, tsize, tbase;
1717   int clamp_s, clamp_t, mask_s, mask_t, mirror_s, mirror_t;
2515    UINT32 *fb = (UINT32*)&rdram[fb_address / 4];
2516    UINT16 *zb = (UINT16*)&rdram[zb_address / 4];
2517    UINT8* cvg_buf = (UINT8*)&cvg_mem[fb_address];
2518    int i, j;
2519    int tsl[2], tsh[2], ttl[2], tth[2];
2520    int twidth[2], tformat[2], tsize[2], tbase[2], tpal[2];
2521    int clamp_s[2], clamp_t[2], mask_s[2], mask_t[2], mirror_s[2], mirror_t[2];
2522    int cycle;
2523    int alpha_reject = 0;
17182524
1719   int clipx1, clipx2, clipy1, clipy2;
2525    int clipx1, clipx2, clipy1, clipy2;
17202526
1721   clipx1 = clip.xh / 4;
1722   clipx2 = clip.xl / 4;
1723   clipy1 = clip.yh / 4;
1724   clipy2 = clip.yl / 4;
2527    clipx1 = clip.xh / 4;
2528    clipx2 = clip.xl / 4;
2529    clipy1 = clip.yh / 4;
2530    clipy2 = clip.yl / 4;
17252531
1726   tsl = tile[tilenum].sl;
1727   tsh = tile[tilenum].sh;
1728   ttl = tile[tilenum].tl;
1729   tth = tile[tilenum].th;
2532    for( cycle = 0; cycle < 2; cycle++ )
2533    {
2534        tsl[cycle] = tile[tilenum + cycle].sl;
2535        tsh[cycle] = tile[tilenum + cycle].sh;
2536        ttl[cycle] = tile[tilenum + cycle].tl;
2537        tth[cycle] = tile[tilenum + cycle].th;
17302538
1731   twidth = tile[tilenum].line;
1732   tformat = tile[tilenum].format;
1733   tsize = tile[tilenum].size;
1734   tbase = tile[tilenum].tmem;
2539        twidth[cycle] = tile[tilenum + cycle].line;
2540        tformat[cycle] = tile[tilenum + cycle].format;
2541        tsize[cycle] = tile[tilenum + cycle].size;
2542        tbase[cycle] = tile[tilenum + cycle].tmem;
2543        tpal[cycle] = tile[tilenum + cycle].palette;
17352544
1736   clamp_t = tile[tilenum].ct;
1737   clamp_s = tile[tilenum].cs;
1738   mirror_t = tile[tilenum].mt;
1739   mirror_s = tile[tilenum].ms;
1740   mask_t = tile[tilenum].mask_t;
1741   mask_s = tile[tilenum].mask_s;
2545        clamp_t[cycle] = tile[tilenum + cycle].ct;
2546        clamp_s[cycle] = tile[tilenum + cycle].cs;
2547        mirror_t[cycle] = tile[tilenum + cycle].mt;
2548        mirror_s[cycle] = tile[tilenum + cycle].ms;
2549        mask_t[cycle] = tile[tilenum + cycle].mask_t;
2550        mask_s[cycle] = tile[tilenum + cycle].mask_s;
2551    }
17422552
1743   if (start < clipy1)
1744   {
1745      start = clipy1;
1746   }
1747   if (start >= clipy2)
1748   {
1749      start = clipy2-1;
1750   }
1751   if (end < clipy1)
1752   {
1753      end = clipy1;
1754   }
1755   if (end >= clipy2)
1756   {
1757      end = clipy2-1;
1758   }
2553    if (start < clipy1)
2554    {
2555        start = clipy1;
2556    }
2557    if (start >= clipy2)
2558    {
2559        start = clipy2-1;
2560    }
2561    if (end < clipy1)
2562    {
2563        end = clipy1;
2564    }
2565    if (end >= clipy2)
2566    {
2567        end = clipy2-1;
2568    }
17592569
1760   for (i=start; i <= end; i++)
1761   {
1762      int xstart = span[i].lx;
1763      int xend = span[i].rx;
1764      int r = span[i].r;
1765      int g = span[i].g;
1766      int b = span[i].b;
1767      int a = span[i].a;
1768      int z = span[i].z;
1769      int s = span[i].s;
1770      int t = span[i].t;
1771      int w = span[i].w;
1772      int dr = span[i].dr;
1773      int dg = span[i].dg;
1774      int db = span[i].db;
1775      int da = span[i].da;
1776      int dz = span[i].dz;
1777      int ds = span[i].ds;
1778      int dt = span[i].dt;
1779      int dw = span[i].dw;
1780      int drinc, dginc, dbinc, dainc, dzinc, dsinc, dtinc, dwinc;
2570    for (i=start; i <= end; i++)
2571    {
2572        int xstart = span[i].lx;
2573        int xend = span[i].rx;
2574        int r = span[i].r;
2575        int g = span[i].g;
2576        int b = span[i].b;
2577        int a = span[i].a;
2578        int z = span[i].z;
2579        int s = span[i].s;
2580        int t = span[i].t;
2581        int w = span[i].w;
2582        int dr = span[i].dr;
2583        int dg = span[i].dg;
2584        int db = span[i].db;
2585        int da = span[i].da;
2586        int dz = span[i].dz;
2587        int ds = span[i].ds;
2588        int dt = span[i].dt;
2589        int dw = span[i].dw;
2590        int drinc, dginc, dbinc, dainc, dzinc, dsinc, dtinc, dwinc;
17812591
1782      int x;
1783      int xinc;
2592        int x;
2593        int xinc;
17842594
1785      int fb_index = fb_width * i;
1786      int length;
2595        int fb_index = fb_width * i;
2596        int length;
17872597
1788      drinc = flip ? (dr) : -dr;
1789      dginc = flip ? (dg) : -dg;
1790      dbinc = flip ? (db) : -db;
1791      dainc = flip ? (da) : -da;
1792      dzinc = flip ? (dz) : -dz;
1793      dsinc = flip ? (ds) : -ds;
1794      dtinc = flip ? (dt) : -dt;
1795      dwinc = flip ? (dw) : -dw;
2598        drinc = flip ? (dr) : -dr;
2599        dginc = flip ? (dg) : -dg;
2600        dbinc = flip ? (db) : -db;
2601        dainc = flip ? (da) : -da;
2602        dzinc = flip ? (dz) : -dz;
2603        dsinc = flip ? (ds) : -ds;
2604        dtinc = flip ? (dt) : -dt;
2605        dwinc = flip ? (dw) : -dw;
17962606
1797      xinc = flip ? 1 : -1;
1798      x = xend;
2607        xinc = flip ? 1 : -1;
2608        x = xend;
17992609
1800      length = flip ? (xstart - xend) : (xend - xstart);
2610        length = flip ? (xstart - xend) : (xend - xstart);
18012611
1802      for (j=0; j <= length; j++)
1803      {
1804         int sr = r >> 16;
1805         int sg = g >> 16;
1806         int sb = b >> 16;
1807         int sa = a >> 16;
1808         int ss = s >> 16;
1809         int st = t >> 16;
1810         int sw = w >> 16;
1811         UINT16 sz = z >> 16;
1812         int oz;
1813         int sss = 0, sst = 0;
2612        for (j=0; j <= length; j++)
2613        {
2614            int sr = r >> 16;
2615            int sg = g >> 16;
2616            int sb = b >> 16;
2617            int sa = a >> 16;
2618            int ss = s >> 16;
2619            int st = t >> 16;
2620            int sw = w >> 16;
2621            UINT16 sz = (other_modes.z_source_sel) ? primitive_z : (z >> 16);
2622            int oz;
2623            int sss = 0, sst = 0;
18142624
1815         if (x >= clipx1 && x < clipx2)
1816         {
1817                //int alpha_out = 0;
1818            COLOR c1, c2;
1819            c1.r = c1.g = c1.b = c1.a = 0;
1820            c2.r = c2.g = c2.b = c2.a = 0;
2625            if (x >= clipx1 && x < clipx2)
2626            {
2627                COLOR c1, c2;
2628                c1.r = c1.g = c1.b = c1.a = 0;
2629                c2.r = c2.g = c2.b = c2.a = 0;
18212630
1822            if ((z & 0xffff) >= 0x8000) sz++;
1823            if ((w & 0xffff) >= 0x8000) sw++;
2631                if ((z & 0xffff) >= 0x8000) sz++;
2632                if ((w & 0xffff) >= 0x8000) sw++;
18242633
1825            if (other_modes.persp_tex_en)
1826            {
1827               if (sw != 0)
1828               {
1829                  sss = (((INT64)(ss) << 20) / sw);
1830                  sst = (((INT64)(st) << 20) / sw);
1831               }
1832            }
1833            else
1834            {
1835               sss = ss;
1836               sst = st;
1837            }
1838
1839            if (sr > 0xff) sr = 0xff;   if (sg > 0xff) sg = 0xff;   if (sb > 0xff) sb = 0xff;
1840
1841            shade_color.r = sr; shade_color.g = sg; shade_color.b = sb; shade_color.a = sa;
1842
1843            if (texture)
1844            {
1845               /*if (other_modes.sample_type)
2634                if (other_modes.persp_tex_en)
2635                {
2636                    if (sw != 0)
18462637                    {
1847                        // bilinear
1848
1849                        sss1 = (sss >> 10) - (tsl >> 2);
1850                        sss2 = sss1 + 1;
1851
1852                        sst1 = (sst >> 10) - (ttl >> 2);
1853                        sst2 = sst1 + 1;
1854
1855                        CLAMP(sss1, sst1);
1856                        CLAMP(sss2, sst2);
1857
1858                        FETCH_TEXEL(&t0, sss1, sst1, twidth, tformat, tsize, tbase);
1859                        FETCH_TEXEL(&t1, sss2, sst1, twidth, tformat, tsize, tbase);
1860                        FETCH_TEXEL(&t2, sss1, sst2, twidth, tformat, tsize, tbase);
1861                        FETCH_TEXEL(&t3, sss2, sst2, twidth, tformat, tsize, tbase);
1862
1863                        texel0_color.r = (((( (t0.r * (0x3ff - (sss & 0x3ff))) + (t1.r * ((sss & 0x3ff))) ) >> 10) * (0x3ff - (sst & 0x3ff))) >> 10) +
1864                                         (((( (t2.r * (0x3ff - (sss & 0x3ff))) + (t3.r * ((sss & 0x3ff))) ) >> 10) * ((sst & 0x3ff))) >> 10);
1865
1866                        texel0_color.g = (((( (t0.g * (0x3ff - (sss & 0x3ff))) + (t1.g * ((sss & 0x3ff))) ) >> 10) * (0x3ff - (sst & 0x3ff))) >> 10) +
1867                                         (((( (t2.g * (0x3ff - (sss & 0x3ff))) + (t3.g * ((sss & 0x3ff))) ) >> 10) * ((sst & 0x3ff))) >> 10);
1868
1869                        texel0_color.b = (((( (t0.b * (0x3ff - (sss & 0x3ff))) + (t1.b * ((sss & 0x3ff))) ) >> 10) * (0x3ff - (sst & 0x3ff))) >> 10) +
1870                                         (((( (t2.b * (0x3ff - (sss & 0x3ff))) + (t3.b * ((sss & 0x3ff))) ) >> 10) * ((sst & 0x3ff))) >> 10);
1871
1872                        texel0_color.a = (((( (t0.a * (0x3ff - (sss & 0x3ff))) + (t1.a * ((sss & 0x3ff))) ) >> 10) * (0x3ff - (sst & 0x3ff))) >> 10) +
1873                                         (((( (t2.a * (0x3ff - (sss & 0x3ff))) + (t3.a * ((sss & 0x3ff))) ) >> 10) * ((sst & 0x3ff))) >> 10);
2638                        sss = (((INT64)(ss) << 20) / sw);
2639                        sst = (((INT64)(st) << 20) / sw);
18742640                    }
1875                    else
1876                    {
1877                        sss1 = (sss >> 10) - (tsl >> 2);
1878                        if ((sss & 0x3ff) >= 0x200) sss1++;
2641                }
2642                else
2643                {
2644                    sss = ss;
2645                    sst = st;
2646                }
18792647
1880                        sst1 = (sst >> 10) - (ttl >> 2);
1881                        if ((sst & 0x3ff) >= 0x200) sst1++;
2648                if (sr > 0xff) sr = 0xff;   if (sg > 0xff) sg = 0xff;   if (sb > 0xff) sb = 0xff;
18822649
1883                CLAMP(sss1, sst1);
2650                shade_color.r = sr; shade_color.g = sg; shade_color.b = sb; shade_color.a = sa;
18842651
1885                        // point sample
1886                        FETCH_TEXEL(&texel0_color, sss1, sst1, twidth, tformat, tsize, tbase);
1887                    }*/
1888
1889               if (other_modes.cycle_type == CYCLE_TYPE_1)
1890               {
1891                  TEXTURE_PIPELINE1(sss, sst, 0);
1892               }
1893               else
1894               {
1895                  TEXTURE_PIPELINE2(sss, sst, 0);
1896               }
1897            }
1898
1899            if (other_modes.cycle_type == CYCLE_TYPE_1)
1900            {
1901               c1 = COLOR_COMBINER(0);
1902                    if( c1.a == 0 )
2652                if (texture)
2653                {
2654                    if (other_modes.cycle_type == CYCLE_TYPE_1)
19032655                    {
1904                        //alpha_out = 1;
2656                        TEXTURE_PIPELINE1(sss, sst, other_modes.sample_type == 0);
19052657                    }
1906            }
1907            else if (other_modes.cycle_type == CYCLE_TYPE_2)
1908            {
1909               c1 = COLOR_COMBINER(0);
1910               c2 = COLOR_COMBINER(1);
1911                    if( c2.a == 0 )
2658                    else
19122659                    {
1913                        //alpha_out = 1;
2660                        TEXTURE_PIPELINE2(sss, sst, other_modes.sample_type == 0);
19142661                    }
1915            }
2662                }
19162663
1917                /* SGINUT 1/6/08: This fixes things in some games and breaks things in others.
1918                                  I believe the pixels should be discarded based on the alpha
1919                                  value stored in memory, but alpha and coverage are stored in
1920                                  anciliary bits in the framebuffer which are not currently
1921                                  emulated by MESS. */
1922                if( /*!alpha_out*/ 1 )
2664                if (other_modes.cycle_type == CYCLE_TYPE_1)
19232665                {
1924                    oz = (UINT16)zb[(fb_index + x) ^ WORD_ADDR_XOR];
1925                    if (zbuffer)
2666                    c1 = COLOR_COMBINER(0);
2667                }
2668                else if (other_modes.cycle_type == CYCLE_TYPE_2)
2669                {
2670                    c1 = COLOR_COMBINER(0);
2671                    c2 = COLOR_COMBINER(1);
2672                }
2673
2674                if( alpha_compare_success(c1, c2) )
2675                {
2676                    oz = (other_modes.z_source_sel) ? primitive_z : (UINT16)zb[(fb_index + x) ^ WORD_ADDR_XOR];
2677                    if (other_modes.z_compare_en && zbuffer)
19262678                    {
1927                        if (sz < oz /*&& c.a != 0*/)
2679                        if (sz < oz)
19282680                        {
1929                            if (other_modes.cycle_type == CYCLE_TYPE_1)
2681                            int cvg_ovf = 0;
2682                            UINT8 cvg_old = 0;
2683                            calculate_coverage( &cvg_buf[(fb_index + x) ^ WORD_ADDR_XOR], span[i].cvg[x], &cvg_ovf, &cvg_old );
2684                            if ((other_modes.color_on_cvg && cvg_ovf) || !other_modes.color_on_cvg)
19302685                            {
1931                                BLENDER1_32(&fb[(fb_index + x)], c1);
1932                            }
1933                            else
1934                            {
1935                                BLENDER2_32(&fb[(fb_index + x)], c1, c2);
1936                            }
2686                                if (other_modes.cycle_type == CYCLE_TYPE_1)
2687                                {
2688                                    BLENDER1_32(&fb[(fb_index + x)], c1, &span[i].cvg[x], &cvg_old, &alpha_reject);
2689                                }
2690                                else
2691                                {
2692                                    BLENDER2_32(&fb[(fb_index + x)], c1, c2, &span[i].cvg[x], &cvg_old, &alpha_reject);
2693                                }
19372694
1938                            if (other_modes.z_compare_en && other_modes.z_update_en)
1939                            {
1940                                zb[(fb_index + x) ^ WORD_ADDR_XOR] = sz;
2695                                if (other_modes.z_update_en && !alpha_reject)
2696                                {
2697                                    zb[(fb_index + x) ^ WORD_ADDR_XOR] = sz;
2698                                }
19412699                            }
19422700                        }
19432701                    }
19442702                    else
19452703                    {
1946                        if (other_modes.cycle_type == CYCLE_TYPE_1)
2704                        int cvg_ovf = 0;
2705                        UINT8 cvg_old = 0;
2706                        calculate_coverage( &cvg_buf[(fb_index + x) ^ WORD_ADDR_XOR], span[i].cvg[x], &cvg_ovf, &cvg_old );
2707                        if ((other_modes.color_on_cvg && cvg_ovf) || !other_modes.color_on_cvg)
19472708                        {
1948                            BLENDER1_32(&fb[(fb_index + x)], c1);
2709                            if (other_modes.cycle_type == CYCLE_TYPE_1)
2710                            {
2711                                BLENDER1_32(&fb[(fb_index + x)], c1, &span[i].cvg[x], &cvg_old, &alpha_reject);
2712                            }
2713                            else
2714                            {
2715                                BLENDER2_32(&fb[(fb_index + x)], c1, c2, &span[i].cvg[x], &cvg_old, &alpha_reject);
2716                            }
19492717                        }
1950                        else
1951                        {
1952                            BLENDER2_32(&fb[(fb_index + x)], c1, c2);
1953                        }
19542718                    }
19552719                }
1956         }
2720            }
19572721
1958         r += drinc;
1959         g += dginc;
1960         b += dbinc;
1961         a += dainc;
1962         z += dzinc;
1963         s += dsinc;
1964         t += dtinc;
1965         w += dwinc;
2722            r += drinc;
2723            g += dginc;
2724            b += dbinc;
2725            a += dainc;
2726            z += dzinc;
2727            s += dsinc;
2728            t += dtinc;
2729            w += dwinc;
19662730
1967         x += xinc;
1968      }
1969   }
2731            x += xinc;
2732        }
2733    }
19702734}
19712735
19722736/*****************************************************************************/
19732737
1974
1975
19762738static void render_spans_16(int start, int end, int tilenum, int shade, int texture, int zbuffer, int flip)
19772739{
1978   UINT16 *fb = (UINT16*)&rdram[fb_address / 4];
1979   UINT16 *zb = (UINT16*)&rdram[zb_address / 4];
1980   int i, j;
1981   int tsl, tsh, ttl, tth;
1982   int twidth, tformat, tsize, tbase;
1983   int clamp_s, clamp_t, mask_s, mask_t, mirror_s, mirror_t;
2740    UINT16 *fb = (UINT16*)&rdram[fb_address / 4];
2741    UINT16 *zb = (UINT16*)&rdram[zb_address / 4];
2742    UINT8* cvg_buf = (UINT8*)&cvg_mem[fb_address];
2743    UINT8* zd_buf = (UINT8*)&cvg_mem[zb_address];
2744    int i, j;
2745    int tsl[2], tsh[2], ttl[2], tth[2];
2746    int twidth[2], tformat[2], tsize[2], tbase[2], tpal[2];
2747    int clamp_s[2], clamp_t[2], mask_s[2], mask_t[2], mirror_s[2], mirror_t[2];
2748    int cycle;
2749    int alpha_reject = 0;
19842750
1985   int clipx1, clipx2, clipy1, clipy2;
2751    int clipx1, clipx2, clipy1, clipy2;
19862752
1987   clipx1 = clip.xh / 4;
1988   clipx2 = clip.xl / 4;
1989   clipy1 = clip.yh / 4;
1990   clipy2 = clip.yl / 4;
2753    clipx1 = clip.xh / 4;
2754    clipx2 = clip.xl / 4;
2755    clipy1 = clip.yh / 4;
2756    clipy2 = clip.yl / 4;
19912757
1992   tsl = tile[tilenum].sl;
1993   tsh = tile[tilenum].sh;
1994   ttl = tile[tilenum].tl;
1995   tth = tile[tilenum].th;
2758    for( cycle = 0; cycle < 2; cycle++ )
2759    {
2760        tsl[cycle] = tile[tilenum + cycle].sl;
2761        tsh[cycle] = tile[tilenum + cycle].sh;
2762        ttl[cycle] = tile[tilenum + cycle].tl;
2763        tth[cycle] = tile[tilenum + cycle].th;
19962764
1997   twidth = tile[tilenum].line;
1998   tformat = tile[tilenum].format;
1999   tsize = tile[tilenum].size;
2000   tbase = tile[tilenum].tmem;
2765        twidth[cycle] = tile[tilenum + cycle].line;
2766        tformat[cycle] = tile[tilenum + cycle].format;
2767        tsize[cycle] = tile[tilenum + cycle].size;
2768        tbase[cycle] = tile[tilenum + cycle].tmem;
2769        tpal[cycle] = tile[tilenum + cycle].palette;
20012770
2002   clamp_t = tile[tilenum].ct;
2003   clamp_s = tile[tilenum].cs;
2004   mirror_t = tile[tilenum].mt;
2005   mirror_s = tile[tilenum].ms;
2006   mask_t = tile[tilenum].mask_t;
2007   mask_s = tile[tilenum].mask_s;
2771        clamp_t[cycle] = tile[tilenum + cycle].ct;
2772        clamp_s[cycle] = tile[tilenum + cycle].cs;
2773        mirror_t[cycle] = tile[tilenum + cycle].mt;
2774        mirror_s[cycle] = tile[tilenum + cycle].ms;
2775        mask_t[cycle] = tile[tilenum + cycle].mask_t;
2776        mask_s[cycle] = tile[tilenum + cycle].mask_s;
2777    }
20082778
2009   if (start < clipy1)
2010   {
2011      start = clipy1;
2012   }
2013   if (start >= clipy2)
2014   {
2015      start = clipy2-1;
2016   }
2017   if (end < clipy1)
2018   {
2019      end = clipy1;
2020   }
2021   if (end >= clipy2)
2022   {
2023      end = clipy2-1;
2024   }
2779    if (start < clipy1)
2780    {
2781        start = clipy1;
2782    }
2783    if (start >= clipy2)
2784    {
2785        start = clipy2-1;
2786    }
2787    if (end < clipy1)
2788    {
2789        end = clipy1;
2790    }
2791    if (end >= clipy2)
2792    {
2793        end = clipy2-1;
2794    }
20252795
2026   for (i=start; i <= end; i++)
2027   {
2028      int xstart = span[i].lx;
2029      int xend = span[i].rx;
2030      int r = span[i].r;
2031      int g = span[i].g;
2032      int b = span[i].b;
2033      int a = span[i].a;
2034      int z = span[i].z;
2035      int s = span[i].s;
2036      int t = span[i].t;
2037      int w = span[i].w;
2038      int dr = span[i].dr;
2039      int dg = span[i].dg;
2040      int db = span[i].db;
2041      int da = span[i].da;
2042      int dz = span[i].dz;
2043      int ds = span[i].ds;
2044      int dt = span[i].dt;
2045      int dw = span[i].dw;
2046      int drinc, dginc, dbinc, dainc, dzinc, dsinc, dtinc, dwinc;
2796    for (i=start; i <= end; i++)
2797    {
2798        int xstart = span[i].lx;
2799        int xend = span[i].rx;
2800        int r = span[i].r;
2801        int g = span[i].g;
2802        int b = span[i].b;
2803        int a = span[i].a;
2804        int z = span[i].z;
2805        int s = span[i].s;
2806        int t = span[i].t;
2807        int w = span[i].w;
2808        int dr = span[i].dr;
2809        int dg = span[i].dg;
2810        int db = span[i].db;
2811        int da = span[i].da;
2812        int dz = span[i].dz;
2813        int ds = span[i].ds;
2814        int dt = span[i].dt;
2815        int dw = span[i].dw;
2816        int drinc, dginc, dbinc, dainc, dzinc, dsinc, dtinc, dwinc;
20472817
2048      int x;
2049      int xinc;
2818        int x;
2819        int xinc;
20502820
2051      int fb_index = fb_width * i;
2052      int length;
2821        int fb_index = fb_width * i;
2822        int length;
20532823
2054      drinc = flip ? (dr) : -dr;
2055      dginc = flip ? (dg) : -dg;
2056      dbinc = flip ? (db) : -db;
2057      dainc = flip ? (da) : -da;
2058      dzinc = flip ? (dz) : -dz;
2059      dsinc = flip ? (ds) : -ds;
2060      dtinc = flip ? (dt) : -dt;
2061      dwinc = flip ? (dw) : -dw;
2824        drinc = flip ? (dr) : -dr;
2825        dginc = flip ? (dg) : -dg;
2826        dbinc = flip ? (db) : -db;
2827        dainc = flip ? (da) : -da;
2828        dzinc = flip ? (dz) : -dz;
2829        dsinc = flip ? (ds) : -ds;
2830        dtinc = flip ? (dt) : -dt;
2831        dwinc = flip ? (dw) : -dw;
20622832
2063      /*if (flip)
2833        xinc = flip ? 1 : -1;
2834        x = xend;
2835
2836        length = flip ? (xstart - xend) : (xend - xstart);
2837
2838        for (j=0; j <= length; j++)
20642839        {
2065            if (xend > clipx2)
2840            int sr = r >> 16;
2841            int sg = g >> 16;
2842            int sb = b >> 16;
2843            int sa = a >> 16;
2844            int ss = s >> 16;
2845            int st = t >> 16;
2846            int sw = w >> 16;
2847            //UINT16 sz;
2848            int sz = (other_modes.z_source_sel) ? (int)((primitive_z << 16) | primitive_delta_z) : (int)z;
2849            UINT32 oz = 0;
2850            int sss = 0, sst = 0;
2851            COLOR c1, c2;
2852            //UINT8 old_delta_z;
2853            c1.r = c1.g = c1.b = c1.a = 0;
2854            c2.r = c2.g = c2.b = c2.a = 0;
2855
2856            /*
2857            if (other_modes.z_source_sel)
20662858            {
2067                int d = xend-clipx2;
2068                r += d * drinc;
2069                g += d * dginc;
2070                b += d * dbinc;
2071                a += d * dainc;
2072                z += d * dzinc;
2073                s += d * dsinc;
2074                t += d * dtinc;
2075                w += d * dwinc;
2076
2077                xend = clipx2;
2859                compress_z((primitive_z << 16) | primitive_delta_z, &sz, &old_delta_z);
20782860            }
2079            if (xstart < clipx1)
2861            else
20802862            {
2081                xstart = clipx1;
2863                compress_z(z, &sz, &old_delta_z);
20822864            }
2083        }
2084        else
2085        {
2086            if (xstart > clipx2)
2087            {
2088                int d = xstart-clipx2;
2089                r += d * drinc;
2090                g += d * dginc;
2091                b += d * dbinc;
2092                a += d * dainc;
2093                z += d * dzinc;
2094                s += d * dsinc;
2095                t += d * dtinc;
2096                w += d * dwinc;
2865            */
20972866
2098                xstart = clipx2;
2099            }
2100            if (xend < clipx1)
2867            if (x >= clipx1 && x < clipx2)
21012868            {
2102                xend = clipx1;
2103            }
2104        }*/
2869                //if ((z & 0xffff) >= 0x8000) sz++;
2870                if ((w & 0xffff) >= 0x8000) sw++;
21052871
2106      xinc = flip ? 1 : -1;
2107      x = xend;
2108
2109      length = flip ? (xstart - xend) : (xend - xstart);
2110
2111      for (j=0; j <= length; j++)
2112      {
2113         int sr = r >> 16;
2114         int sg = g >> 16;
2115         int sb = b >> 16;
2116         int sa = a >> 16;
2117         int ss = s >> 16;
2118         int st = t >> 16;
2119         int sw = w >> 16;
2120         UINT16 sz = z >> 16;
2121         int oz;
2122         int sss = 0, sst = 0;
2123            //int alpha_out = 0;
2124         COLOR c1, c2;
2125         c1.r = c1.g = c1.b = c1.a = 0;
2126         c2.r = c2.g = c2.b = c2.a = 0;
2127
2128         if (x >= clipx1 && x < clipx2)
2129         {
2130            if ((z & 0xffff) >= 0x8000) sz++;
2131            if ((w & 0xffff) >= 0x8000) sw++;
2132
2133            if (other_modes.persp_tex_en)
2134            {
2135               if (sw != 0)
2136               {
2137                  sss = (((INT64)(ss) << 20) / sw);
2138                  sst = (((INT64)(st) << 20) / sw);
2139               }
2140            }
2141            else
2142            {
2143               sss = ss;
2144               sst = st;
2145            }
2146
2147            if (sr > 0xff) sr = 0xff;   if (sg > 0xff) sg = 0xff;   if (sb > 0xff) sb = 0xff;
2148
2149            shade_color.r = sr; shade_color.g = sg; shade_color.b = sb; shade_color.a = sa;
2150
2151            if (texture)
2152            {
2153               /*if (other_modes.sample_type)
2872                if (other_modes.persp_tex_en)
2873                {
2874                    if (sw != 0)
21542875                    {
2155                        // bilinear
2156
2157                        sss1 = (sss >> 10) - (tsl >> 2);
2158                        sss2 = sss1 + 1;
2159
2160                        sst1 = (sst >> 10) - (ttl >> 2);
2161                        sst2 = sst1 + 1;
2162
2163                        CLAMP(sss1, sst1);
2164                        CLAMP(sss2, sst2);
2165
2166                        FETCH_TEXEL(&t0, sss1, sst1, twidth, tformat, tsize, tbase);
2167                        FETCH_TEXEL(&t1, sss2, sst1, twidth, tformat, tsize, tbase);
2168                        FETCH_TEXEL(&t2, sss1, sst2, twidth, tformat, tsize, tbase);
2169                        FETCH_TEXEL(&t3, sss2, sst2, twidth, tformat, tsize, tbase);
2170
2171                        texel0_color.r = (((( (t0.r * (0x3ff - (sss & 0x3ff))) + (t1.r * ((sss & 0x3ff))) ) >> 10) * (0x3ff - (sst & 0x3ff))) >> 10) +
2172                                         (((( (t2.r * (0x3ff - (sss & 0x3ff))) + (t3.r * ((sss & 0x3ff))) ) >> 10) * ((sst & 0x3ff))) >> 10);
2173
2174                        texel0_color.g = (((( (t0.g * (0x3ff - (sss & 0x3ff))) + (t1.g * ((sss & 0x3ff))) ) >> 10) * (0x3ff - (sst & 0x3ff))) >> 10) +
2175                                         (((( (t2.g * (0x3ff - (sss & 0x3ff))) + (t3.g * ((sss & 0x3ff))) ) >> 10) * ((sst & 0x3ff))) >> 10);
2176
2177                        texel0_color.b = (((( (t0.b * (0x3ff - (sss & 0x3ff))) + (t1.b * ((sss & 0x3ff))) ) >> 10) * (0x3ff - (sst & 0x3ff))) >> 10) +
2178                                         (((( (t2.b * (0x3ff - (sss & 0x3ff))) + (t3.b * ((sss & 0x3ff))) ) >> 10) * ((sst & 0x3ff))) >> 10);
2179
2180                        texel0_color.a = (((( (t0.a * (0x3ff - (sss & 0x3ff))) + (t1.a * ((sss & 0x3ff))) ) >> 10) * (0x3ff - (sst & 0x3ff))) >> 10) +
2181                                         (((( (t2.a * (0x3ff - (sss & 0x3ff))) + (t3.a * ((sss & 0x3ff))) ) >> 10) * ((sst & 0x3ff))) >> 10);
2876                        sss = (((INT64)(ss) << 20) / sw);
2877                        sst = (((INT64)(st) << 20) / sw);
21822878                    }
2183                    else
2184                    {
2185                        sss1 = (sss >> 10) - (tsl >> 2);
2186                        if ((sss & 0x3ff) >= 0x200) sss1++;
2879                }
2880                else
2881                {
2882                    sss = ss;
2883                    sst = st;
2884                }
21872885
2188                        sst1 = (sst >> 10) - (ttl >> 2);
2189                        if ((sst & 0x3ff) >= 0x200) sst1++;
2886                if (sr > 0xff) sr = 0xff;   if (sg > 0xff) sg = 0xff;   if (sb > 0xff) sb = 0xff;
21902887
2191                CLAMP(sss1, sst1);
2888                shade_color.r = sr; shade_color.g = sg; shade_color.b = sb; shade_color.a = sa;
21922889
2193                        // point sample
2194                        FETCH_TEXEL(&texel0_color, sss1, sst1, twidth, tformat, tsize, tbase);
2195                    }*/
2196
2197               if (other_modes.cycle_type == CYCLE_TYPE_1)
2198               {
2199                  TEXTURE_PIPELINE1(sss, sst, 0);
2200               }
2201               else
2202               {
2203                  TEXTURE_PIPELINE2(sss, sst, 0);
2204               }
2205            }
2206
2207            if (other_modes.cycle_type == CYCLE_TYPE_1)
2208            {
2209               c1 = COLOR_COMBINER(0);
2210                    if( c1.a == 0 )
2890                if (texture)
2891                {
2892                    if (other_modes.cycle_type == CYCLE_TYPE_1)
22112893                    {
2212                        //alpha_out = 1;
2894                        TEXTURE_PIPELINE1(sss, sst, other_modes.sample_type == 0);
22132895                    }
2214            }
2215            else if (other_modes.cycle_type == CYCLE_TYPE_2)
2216            {
2217               c1 = COLOR_COMBINER(0);
2218               c2 = COLOR_COMBINER(1);
2219                    if( c2.a == 0 )
2896                    else
22202897                    {
2221                        //alpha_out = 1;
2898                        TEXTURE_PIPELINE2(sss, sst, other_modes.sample_type == 0);
22222899                    }
2223            }
2900                }
22242901
2225                /* SGINUT 1/6/08: This fixes things in some games and breaks things in others.
2226                                  I believe the pixels should be discarded based on the alpha
2227                                  value stored in memory, but alpha and coverage are stored in
2228                                  anciliary bits in the framebuffer which are not currently
2229                                  emulated by MESS. */
2230                if( /*!alpha_out*/ 1 )
2902                if (other_modes.cycle_type == CYCLE_TYPE_1)
22312903                {
2232                    oz = (UINT16)zb[(fb_index + x) ^ WORD_ADDR_XOR];
2233                    if (zbuffer)
2904                    c1 = COLOR_COMBINER(0);
2905                }
2906                else if (other_modes.cycle_type == CYCLE_TYPE_2)
2907                {
2908                    c1 = COLOR_COMBINER(0);
2909                    c2 = COLOR_COMBINER(1);
2910                }
2911
2912                if( alpha_compare_success(c1, c2) )
2913                {
2914                    decompress_z(zb[(fb_index + x) ^ WORD_ADDR_XOR], &oz);
2915                    //oz = (UINT16)zb[(fb_index + x) ^ WORD_ADDR_XOR];
2916
2917                    if (other_modes.z_compare_en && zbuffer)
22342918                    {
2235                        if (sz < oz /*&& c.a != 0*/)
2919                        if (((sz >> 13) < oz) || ((other_modes.z_mode == 3) && ((sz >> 13) == oz)))
22362920                        {
2237                            //BLENDER1_16(&fb[(fb_index + x) ^ WORD_ADDR_XOR], c);
2921                            int dith = dither_matrix_4x4[(((j) & 3) << 2) + ((i^WORD_ADDR_XOR) & 3)];
2922                            int cvg_ovf = 0;
2923                            UINT8 cvg_old = 0;
2924                            calculate_coverage( &cvg_buf[(fb_index + x) ^ WORD_ADDR_XOR], span[i].cvg[x], &cvg_ovf, &cvg_old );
2925                            if ((other_modes.color_on_cvg && cvg_ovf) || !other_modes.color_on_cvg)
22382926                            {
2239                                int dith = dither_matrix_4x4[(((j) & 3) << 2) + ((i^WORD_ADDR_XOR) & 3)];
22402927                                if (other_modes.cycle_type == CYCLE_TYPE_1)
22412928                                {
2242                                    BLENDER1_16(&fb[(fb_index + x) ^ WORD_ADDR_XOR], c1, dith);
2929                                    BLENDER1_16(&fb[(fb_index + x) ^ WORD_ADDR_XOR], c1, dith, &span[i].cvg[x], &cvg_old, &alpha_reject);
22432930                                }
22442931                                else
22452932                                {
2246                                    BLENDER2_16(&fb[(fb_index + x) ^ WORD_ADDR_XOR], c1, c2, dith);
2933                                    if ((other_modes.color_on_cvg && cvg_ovf) || !other_modes.color_on_cvg)
2934                                    {
2935                                        BLENDER2_16(&fb[(fb_index + x) ^ WORD_ADDR_XOR], c1, c2, dith, &span[i].cvg[x], &cvg_old, &alpha_reject);
2936                                    }
22472937                                }
2248                            }
22492938
2250                            if (other_modes.z_compare_en && other_modes.z_update_en)
2251                            {
2252                                zb[(fb_index + x) ^ WORD_ADDR_XOR] = sz;
2939                                if (other_modes.z_update_en && !alpha_reject)
2940                                {
2941                                    //zb[(fb_index + x) ^ WORD_ADDR_XOR] = sz;
2942                                    compress_z(sz, &zb[(fb_index + x) ^ WORD_ADDR_XOR], &zd_buf[(fb_index + x) ^ WORD_ADDR_XOR] );
2943                                }
22532944                            }
22542945                        }
22552946                    }
22562947                    else
22572948                    {
2258                        //BLENDER1_16(&fb[(fb_index + x) ^ WORD_ADDR_XOR], c);
2259
2949                        int dith = dither_matrix_4x4[(((j) & 3) << 2) + ((i^WORD_ADDR_XOR) & 3)];
2950                        int cvg_ovf = 0;
2951                        UINT8 cvg_old = 0;
2952                        calculate_coverage( &cvg_buf[(fb_index + x) ^ WORD_ADDR_XOR], span[i].cvg[x], &cvg_ovf, &cvg_old );
2953                        if ((other_modes.color_on_cvg && cvg_ovf) || !other_modes.color_on_cvg)
22602954                        {
2261                            int dith = dither_matrix_4x4[(((j) & 3) << 2) + ((i^WORD_ADDR_XOR) & 3)];
22622955                            if (other_modes.cycle_type == CYCLE_TYPE_1)
22632956                            {
2264                                BLENDER1_16(&fb[(fb_index + x) ^ WORD_ADDR_XOR], c1, dith);
2957                                BLENDER1_16(&fb[(fb_index + x) ^ WORD_ADDR_XOR], c1, dith, &span[i].cvg[x], &cvg_old, &alpha_reject);
22652958                            }
22662959                            else
22672960                            {
2268                                BLENDER2_16(&fb[(fb_index + x) ^ WORD_ADDR_XOR], c1, c2, dith);
2961                                BLENDER2_16(&fb[(fb_index + x) ^ WORD_ADDR_XOR], c1, c2, dith, &span[i].cvg[x], &cvg_old, &alpha_reject);
22692962                            }
22702963                        }
22712964                    }
22722965                }
2273         }
2966            }
22742967
2275         r += drinc;
2276         g += dginc;
2277         b += dbinc;
2278         a += dainc;
2279         z += dzinc;
2280         s += dsinc;
2281         t += dtinc;
2282         w += dwinc;
2968            r += drinc;
2969            g += dginc;
2970            b += dbinc;
2971            a += dainc;
2972            z += dzinc;
2973            s += dsinc;
2974            t += dtinc;
2975            w += dwinc;
22832976
2284         x += xinc;
2285      }
2286   }
2977            x += xinc;
2978        }
2979    }
22872980}
22882981
2289static void triangle(UINT32 w1, UINT32 w2, int shade, int texture, int zbuffer)
2982typedef struct
22902983{
2291   int j;
2292   int xleft, xright, xleft_inc, xright_inc;
2293   int xstart, xend;
2294   int r, g, b, a, z, s, t, w;
2295   //int dr, dg, db, da;
2296   int drdx = 0, dgdx = 0, dbdx = 0, dadx = 0, dzdx = 0, dsdx = 0, dtdx = 0, dwdx = 0;
2297   //int drdy = 0, dgdy = 0, dbdy = 0, dady = 0, dzdy = 0, dsdy = 0, dtdy = 0, dwdy = 0;
2298   int drde = 0, dgde = 0, dbde = 0, dade = 0, dzde = 0, dsde = 0, dtde = 0, dwde = 0;
2299   int tile;
2300   int flip = (w1 & 0x800000) ? 1 : 0;
2984    INT32 xl, xm, xh;
2985    INT32 yl, ym, yh;
2986    INT64 dxldy, dxhdy, dxmdy;
2987    int r, g, b, a, s, t, w, z;
2988    int drdx, dgdx, dbdx, dadx, dsdx, dtdx, dwdx, dzdx;
2989    int drdy, dgdy, dbdy, dady, dsdy, dtdy, dwdy, dzdy;
2990    int drde, dgde, dbde, dade, dsde, dtde, dwde, dzde;
2991    int tile;
2992    int flip;
23012993
2302   INT32 yl, ym, yh;
2303   INT32 xl, xm, xh;
2304   INT64 dxldy, dxhdy, dxmdy;
2305   UINT32 w3, w4, w5, w6, w7, w8;
2994} triangle_data;
23062995
2307   int shade_base = rdp_cmd_cur + 8;
2308   int texture_base = rdp_cmd_cur + 8;
2309   int zbuffer_base = rdp_cmd_cur + 8;
2310   if (shade)
2311   {
2312      texture_base += 16;
2313      zbuffer_base += 16;
2314   }
2315   if (texture)
2316   {
2317      zbuffer_base += 16;
2318   }
2996static void fill_span_buffer_1x1( triangle_data* tri, int* span_start, int* span_end )
2997{
2998    int j;
2999    int cvg_idx;
3000    int xleft, xright, xleft_inc, xright_inc;
3001    int xstart, xend;
23193002
2320   w3 = rdp_cmd_data[rdp_cmd_cur+2];
2321   w4 = rdp_cmd_data[rdp_cmd_cur+3];
2322   w5 = rdp_cmd_data[rdp_cmd_cur+4];
2323   w6 = rdp_cmd_data[rdp_cmd_cur+5];
2324   w7 = rdp_cmd_data[rdp_cmd_cur+6];
2325   w8 = rdp_cmd_data[rdp_cmd_cur+7];
3003    tri->yl >>= 2;
3004    tri->ym >>= 2;
3005    tri->yh >>= 2;
23263006
2327   yl = (w1 & 0x3fff) >> 2;
2328   ym = ((w2 >> 16) & 0x3fff) >> 2;
2329   yh = ((w2 >>  0) & 0x3fff) >> 2;
2330   xl = (INT32)(w3);
2331   xh = (INT32)(w5);
2332   xm = (INT32)(w7);
2333   dxldy = (INT32)(w4);
2334   dxhdy = (INT32)(w6);
2335   dxmdy = (INT32)(w8);
3007    xleft = tri->xm;
3008    xright = tri->xh;
3009    xleft_inc = tri->dxmdy;
3010    xright_inc = tri->dxhdy;
23363011
2337   if (yl & 0x800) yl |= 0xfffff000;
2338   if (ym & 0x800) ym |= 0xfffff000;
2339   if (yh & 0x800) yh |= 0xfffff000;
3012    for(j = tri->yh; j <= tri->yl; j++)
3013    {
3014        if(j == tri->ym)
3015        {
3016            xleft = tri->xl;
3017            xleft_inc = tri->dxldy;
3018            xright_inc = tri->dxhdy;
3019        }
23403020
2341   yl += (w1 >> 1) & 1;
2342   ym += (w2 >> 17) & 1;
2343   yh += (w2 >> 1) & 1;
3021        xstart = xleft >> 16;
3022        xend = xright >> 16;
23443023
2345   tile = (w1 >> 16) & 0x7;
3024        if(j >= 0)
3025        {
3026            span[j].lx = xstart;
3027            span[j].rx = xend;
3028            span[j].s = tri->s;     span[j].ds = tri->dsdx;
3029            span[j].t = tri->t;     span[j].dt = tri->dtdx;
3030            span[j].w = tri->w;     span[j].dw = tri->dwdx;
3031            span[j].r = tri->r;     span[j].dr = tri->drdx;
3032            span[j].g = tri->g;     span[j].dg = tri->dgdx;
3033            span[j].b = tri->b;     span[j].db = tri->dbdx;
3034            span[j].a = tri->a;     span[j].da = tri->dadx;
3035            span[j].z = tri->z;     span[j].dz = tri->dzdx;
3036            if(tri->flip)
3037            {
3038                for(cvg_idx = xend; cvg_idx <= xstart; cvg_idx++)
3039                {
3040                    span[j].cvg[cvg_idx] = 8;
3041                }
3042            }
3043            else
3044            {
3045                for(cvg_idx = xstart; cvg_idx <= xend; cvg_idx++)
3046                {
3047                    span[j].cvg[cvg_idx] = 8;
3048                }
3049            }
3050        }
23463051
2347   r = 0xff;   g = 0xff;   b = 0xff;   a = 0xff;   z = 0;   s = 0;   t = 0;   w = 0;
2348   //dr = 0;       dg = 0;     db = 0;     da = 0;
3052        xleft += xleft_inc;
3053        xright += xright_inc;
3054        tri->s += tri->dsde;
3055        tri->t += tri->dtde;
3056        tri->w += tri->dwde;
3057        tri->r += tri->drde;
3058        tri->g += tri->dgde;
3059        tri->b += tri->dbde;
3060        tri->a += tri->dade;
3061        tri->z += tri->dzde;
3062    }
23493063
2350   if (shade)
2351   {
2352      r    = (rdp_cmd_data[shade_base+0 ] & 0xffff0000) | ((rdp_cmd_data[shade_base+4 ] >> 16) & 0x0000ffff);
2353      g    = ((rdp_cmd_data[shade_base+0 ] << 16) & 0xffff0000) | (rdp_cmd_data[shade_base+4 ] & 0x0000ffff);
2354      b    = (rdp_cmd_data[shade_base+1 ] & 0xffff0000) | ((rdp_cmd_data[shade_base+5 ] >> 16) & 0x0000ffff);
2355      a    = ((rdp_cmd_data[shade_base+1 ] << 16) & 0xffff0000) | (rdp_cmd_data[shade_base+5 ] & 0x0000ffff);
2356      drdx = (rdp_cmd_data[shade_base+2 ] & 0xffff0000) | ((rdp_cmd_data[shade_base+6 ] >> 16) & 0x0000ffff);
2357      dgdx = ((rdp_cmd_data[shade_base+2 ] << 16) & 0xffff0000) | (rdp_cmd_data[shade_base+6 ] & 0x0000ffff);
2358      dbdx = (rdp_cmd_data[shade_base+3 ] & 0xffff0000) | ((rdp_cmd_data[shade_base+7 ] >> 16) & 0x0000ffff);
2359      dadx = ((rdp_cmd_data[shade_base+3 ] << 16) & 0xffff0000) | (rdp_cmd_data[shade_base+7 ] & 0x0000ffff);
2360      drde = (rdp_cmd_data[shade_base+8 ] & 0xffff0000) | ((rdp_cmd_data[shade_base+12] >> 16) & 0x0000ffff);
2361      dgde = ((rdp_cmd_data[shade_base+8 ] << 16) & 0xffff0000) | (rdp_cmd_data[shade_base+12] & 0x0000ffff);
2362      dbde = (rdp_cmd_data[shade_base+9 ] & 0xffff0000) | ((rdp_cmd_data[shade_base+13] >> 16) & 0x0000ffff);
2363      dade = ((rdp_cmd_data[shade_base+9 ] << 16) & 0xffff0000) | (rdp_cmd_data[shade_base+13] & 0x0000ffff);
2364      //drdy = (rdp_cmd_data[shade_base+10] & 0xffff0000) | ((rdp_cmd_data[shade_base+14] >> 16) & 0x0000ffff);
2365      //dgdy = ((rdp_cmd_data[shade_base+10] << 16) & 0xffff0000) | (rdp_cmd_data[shade_base+14] & 0x0000ffff);
2366      //dbdy = (rdp_cmd_data[shade_base+11] & 0xffff0000) | ((rdp_cmd_data[shade_base+15] >> 16) & 0x0000ffff);
2367      //dady = ((rdp_cmd_data[shade_base+11] << 16) & 0xffff0000) | (rdp_cmd_data[shade_base+15] & 0x0000ffff);
2368   }
2369   if (texture)
2370   {
2371      s    = (rdp_cmd_data[texture_base+0 ] & 0xffff0000) | ((rdp_cmd_data[texture_base+4 ] >> 16) & 0x0000ffff);
2372      t    = ((rdp_cmd_data[texture_base+0 ] << 16) & 0xffff0000)   | (rdp_cmd_data[texture_base+4 ] & 0x0000ffff);
2373      w    = (rdp_cmd_data[texture_base+1 ] & 0xffff0000) | ((rdp_cmd_data[texture_base+5 ] >> 16) & 0x0000ffff);
2374      dsdx = (rdp_cmd_data[texture_base+2 ] & 0xffff0000) | ((rdp_cmd_data[texture_base+6 ] >> 16) & 0x0000ffff);
2375      dtdx = ((rdp_cmd_data[texture_base+2 ] << 16) & 0xffff0000)   | (rdp_cmd_data[texture_base+6 ] & 0x0000ffff);
2376      dwdx = (rdp_cmd_data[texture_base+3 ] & 0xffff0000) | ((rdp_cmd_data[texture_base+7 ] >> 16) & 0x0000ffff);
2377      dsde = (rdp_cmd_data[texture_base+8 ] & 0xffff0000) | ((rdp_cmd_data[texture_base+12] >> 16) & 0x0000ffff);
2378      dtde = ((rdp_cmd_data[texture_base+8 ] << 16) & 0xffff0000)   | (rdp_cmd_data[texture_base+12] & 0x0000ffff);
2379      dwde = (rdp_cmd_data[texture_base+9 ] & 0xffff0000) | ((rdp_cmd_data[texture_base+13] >> 16) & 0x0000ffff);
2380      //dsdy = (rdp_cmd_data[texture_base+10] & 0xffff0000) | ((rdp_cmd_data[texture_base+14] >> 16) & 0x0000ffff);
2381      //dtdy = ((rdp_cmd_data[texture_base+10] << 16) & 0xffff0000)   | (rdp_cmd_data[texture_base+14] & 0x0000ffff);
2382      //dwdy = (rdp_cmd_data[texture_base+11] & 0xffff0000) | ((rdp_cmd_data[texture_base+15] >> 16) & 0x0000ffff);
2383   }
2384   if (zbuffer)
2385   {
2386      z    = rdp_cmd_data[zbuffer_base+0];
2387      dzdx = rdp_cmd_data[zbuffer_base+1];
2388      dzde = rdp_cmd_data[zbuffer_base+2];
2389      //dzdy = rdp_cmd_data[zbuffer_base+3];
2390   }
3064    *span_start = tri->yh;
3065    *span_end = tri->yl;
3066}
23913067
2392   xleft = xm;
2393   xright = xh;
2394   xleft_inc = dxmdy;
2395   xright_inc = dxhdy;
3068static void fill_span_buffer_2x2( triangle_data* tri, int* span_start, int* span_end )
3069{
3070    int j;
3071    int xleft, xright, xleft_inc, xright_inc;
3072    int xstart, xend;
3073    int covered_pixels = 0;
23963074
2397   for (j=yh; j < ym; j++)
2398   {
2399      xstart = xleft >> 16;
2400      xend = xright >> 16;
3075    tri->xm += (tri->dxmdy >> 2) * (tri->yh & 0x0003);
3076    tri->xh += (tri->dxhdy >> 2) * (tri->yh & 0x0003);
24013077
2402      if (j >= 0)
2403      {
2404         span[j].lx = xstart;
2405         span[j].rx = xend;
2406         span[j].s = s;      span[j].ds = dsdx;
2407         span[j].t = t;      span[j].dt = dtdx;
2408         span[j].w = w;      span[j].dw = dwdx;
2409         span[j].r = r;      span[j].dr = drdx;
2410         span[j].g = g;      span[j].dg = dgdx;
2411         span[j].b = b;      span[j].db = dbdx;
2412         span[j].a = a;      span[j].da = dadx;
2413         span[j].z = z;      span[j].dz = dzdx;
2414      }
3078    tri->r += (tri->drde >> 2) * (tri->yh & 0x0003);
3079    tri->g += (tri->dgde >> 2) * (tri->yh & 0x0003);
3080    tri->b += (tri->dbde >> 2) * (tri->yh & 0x0003);
3081    tri->a += (tri->dade >> 2) * (tri->yh & 0x0003);
3082    tri->s += (tri->dsde >> 2) * (tri->yh & 0x0003);
3083    tri->t += (tri->dtde >> 2) * (tri->yh & 0x0003);
3084    tri->w += (tri->dwde >> 2) * (tri->yh & 0x0003);
3085    tri->z += (tri->dzde >> 2) * (tri->yh & 0x0003);
24153086
2416      xleft += xleft_inc;
2417      xright += xright_inc;
2418      s += dsde;
2419      t += dtde;
2420      w += dwde;
2421      r += drde;
2422      g += dgde;
2423      b += dbde;
2424      a += dade;
2425      z += dzde;
2426   }
3087    xleft = tri->xm;
3088    xright = tri->xh;
3089    xleft_inc = tri->dxmdy;
3090    xright_inc = tri->dxhdy;
24273091
2428   xleft = xl;
2429   xleft_inc = dxldy;
2430   xright_inc = dxhdy;
3092    for( j = tri->yh; j <= tri->yl; j++ )
3093    {
3094        if (j == tri->ym)
3095        {
3096            xleft = tri->xl;
3097            xleft_inc = tri->dxldy;
3098            xright_inc = tri->dxhdy;
3099        }
24313100
2432   for (j=ym; j <= yl; j++)
2433   {
2434      xstart = xleft >> 16;
2435      xend = xright >> 16;
3101        xstart = xleft >> 14;
3102        xend = xright >> 14;
24363103
2437      if (j >= 0)
2438      {
2439         span[j].lx = xstart;
2440         span[j].rx = xend;
2441         span[j].s = s;      span[j].ds = dsdx;
2442         span[j].t = t;      span[j].dt = dtdx;
2443         span[j].w = w;      span[j].dw = dwdx;
2444         span[j].r = r;      span[j].dr = drdx;
2445         span[j].g = g;      span[j].dg = dgdx;
2446         span[j].b = b;      span[j].db = dbdx;
2447         span[j].a = a;      span[j].da = dadx;
2448         span[j].z = z;      span[j].dz = dzdx;
2449      }
3104        if( j >= 0 && ((j == tri->yh) || ((j & 3) == 0)) )
3105        {
3106            memset( span[j >> 2].cvg, 0, RDP_CVG_SPAN_MAX );
3107            covered_pixels = 0;
3108        }
24503109
2451      xleft += xleft_inc;
2452      xright += xright_inc;
2453      s += dsde;
2454      t += dtde;
2455      w += dwde;
2456      r += drde;
2457      g += dgde;
2458      b += dbde;
2459      a += dade;
2460      z += dzde;
2461   }
3110        if( j >= 0 )
3111        {
3112            if( tri->flip )
3113            {
3114                int cvg_iter = 0;
24623115
2463   switch (fb_size)
2464   {
2465      case PIXEL_SIZE_16BIT:   render_spans_16(yh, yl, tile, shade, texture, zbuffer, flip); break;
2466      case PIXEL_SIZE_32BIT:   render_spans_32(yh, yl, tile, shade, texture, zbuffer, flip); break;
2467   }
3116                if( xend & 0x0003 )
3117                {
3118                    // Round up to the next pixel
3119                    xend &= ~0x0003;
3120                    xend += 4;
3121                }
3122                if( xleft & ((1 << 14) - 1) )
3123                {
3124                    xstart++;
3125                }
3126                for( cvg_iter=xend; cvg_iter < xstart; cvg_iter++ )
3127                {
3128                    if( (cvg_iter < 0) || (cvg_iter >= (RDP_CVG_SPAN_MAX << 2)) )
3129                    {
3130                        continue;
3131                    }
3132                    if( (cvg_iter & 1) == (j & 1) )
3133                    {
3134                        span[j >> 2].cvg[cvg_iter >> 2]++;
3135                        covered_pixels++;
3136                    }
3137                }
3138            }
3139            else
3140            {
3141                int cvg_iter = 0;
3142
3143                if( xstart & 0x0003 )
3144                {
3145                    // Round up to the next pixel
3146                    xstart &= ~0x0003;
3147                    xstart += 4;
3148                }
3149                if( xright & ((1 << 14) - 1) )
3150                {
3151                    xend++;
3152                }
3153                for( cvg_iter = xstart; cvg_iter < xend; cvg_iter++ )
3154                {
3155                    if( cvg_iter < 0 || cvg_iter >= (RDP_CVG_SPAN_MAX << 2) )
3156                    {
3157                        continue;
3158                    }
3159                    if( (cvg_iter & 1) == (j & 1) )
3160                    {
3161                        span[j >> 2].cvg[cvg_iter >> 2]++;
3162                        covered_pixels++;
3163                    }
3164                }
3165            }
3166
3167            if( (j & 3) == 3 )
3168            {
3169                span[j >> 2].s = tri->s;    span[j >> 2].ds = tri->dsdx;
3170                span[j >> 2].t = tri->t;    span[j >> 2].dt = tri->dtdx;
3171                span[j >> 2].w = tri->w;    span[j >> 2].dw = tri->dwdx;
3172                span[j >> 2].r = tri->r;    span[j >> 2].dr = tri->drdx;
3173                span[j >> 2].g = tri->g;    span[j >> 2].dg = tri->dgdx;
3174                span[j >> 2].b = tri->b;    span[j >> 2].db = tri->dbdx;
3175                span[j >> 2].a = tri->a;    span[j >> 2].da = tri->dadx;
3176                span[j >> 2].z = tri->z;    span[j >> 2].dz = tri->dzdx;
3177            }
3178
3179            if( (j & 3) == 3 )
3180            {
3181                int cvg_start = 0;
3182                int cvg_end = 0;
3183                int cvg_iter = 0;
3184
3185                for( cvg_iter = 0; cvg_iter < (RDP_CVG_SPAN_MAX - 1); cvg_iter++ )
3186                {
3187                    if( span[j >> 2].cvg[cvg_iter] == 0 && span[j >> 2].cvg[cvg_iter + 1] > 0 )
3188                    {
3189                        cvg_start = cvg_iter + 1;
3190                    }
3191                    if( span[j >> 2].cvg[cvg_iter] > 0 && span[j >> 2].cvg[cvg_iter + 1] == 0 )
3192                    {
3193                        break;
3194                    }
3195                }
3196                cvg_end = cvg_iter;
3197
3198                span[j >> 2].lx = xstart >> 2;
3199                span[j >> 2].rx = xend >> 2;
3200
3201                if( covered_pixels == 0 )
3202                {
3203                    span[j >> 2].lx = -1;
3204                    span[j >> 2].rx = -1;
3205                }
3206            }
3207        }
3208
3209        xleft += xleft_inc >> 2;
3210        xright += xright_inc >> 2;
3211        tri->s += tri->dsde >> 2;
3212        tri->t += tri->dtde >> 2;
3213        tri->w += tri->dwde >> 2;
3214        tri->r += tri->drde >> 2;
3215        tri->g += tri->dgde >> 2;
3216        tri->b += tri->dbde >> 2;
3217        tri->a += tri->dade >> 2;
3218        tri->z += tri->dzde >> 2;
3219    }
3220
3221    *span_start = tri->yh >> 2;
3222    *span_end = ((tri->yl & 0x0003) == 3) ? (tri->yl >> 2) : ((tri->yl >> 2) - 1);
24683223}
24693224
3225static void triangle(UINT32 w1, UINT32 w2, int shade, int texture, int zbuffer)
3226{
3227    UINT32 w3, w4, w5, w6, w7, w8;
3228
3229    int shade_base = rdp_cmd_cur + 8;
3230    int texture_base = rdp_cmd_cur + 8;
3231    int zbuffer_base = rdp_cmd_cur + 8;
3232
3233    int span_start = 0;
3234    int span_end = 0;
3235
3236    triangle_data tri;
3237
3238    tri.yl = tri.ym = tri.yh = 0;
3239    tri.xl = tri.xm = tri.xh = 0;
3240    tri.dxldy = tri.dxhdy = tri.dxmdy = 0;
3241    tri.r = tri.g = tri.b = tri.a = tri.s = tri.t = tri.w = tri.z = 0;
3242    tri.drdx = tri.dgdx = tri.dbdx = tri.dadx = tri.dsdx = tri.dtdx = tri.dwdx = tri.dzdx = 0;
3243    tri.drdy = tri.dgdy = tri.dbdy = tri.dady = tri.dsdy = tri.dtdy = tri.dwdy = tri.dzdy = 0;
3244    tri.drde = tri.dgde = tri.dbde = tri.dade = tri.dsde = tri.dtde = tri.dwde = tri.dzde = 0;
3245    tri.tile = (w1 >> 16) & 0x7;;
3246    tri.flip = (w1 & 0x800000) ? 1 : 0;
3247
3248    if (shade)
3249    {
3250        texture_base += 16;
3251        zbuffer_base += 16;
3252    }
3253    if (texture)
3254    {
3255        zbuffer_base += 16;
3256    }
3257
3258    w3 = rdp_cmd_data[rdp_cmd_cur+2];
3259    w4 = rdp_cmd_data[rdp_cmd_cur+3];
3260    w5 = rdp_cmd_data[rdp_cmd_cur+4];
3261    w6 = rdp_cmd_data[rdp_cmd_cur+5];
3262    w7 = rdp_cmd_data[rdp_cmd_cur+6];
3263    w8 = rdp_cmd_data[rdp_cmd_cur+7];
3264
3265    tri.yl = (w1 & 0x3fff);
3266    tri.ym = ((w2 >> 16) & 0x3fff);
3267    tri.yh = ((w2 >>  0) & 0x3fff);
3268    tri.xl = (INT32)(w3);
3269    tri.xh = (INT32)(w5);
3270    tri.xm = (INT32)(w7);
3271    tri.dxldy = (INT32)(w4);
3272    tri.dxhdy = (INT32)(w6);
3273    tri.dxmdy = (INT32)(w8);
3274
3275    if (tri.yl & 0x2000) tri.yl |= 0xffffc000;
3276    if (tri.ym & 0x2000) tri.ym |= 0xffffc000;
3277    if (tri.yh & 0x2000) tri.yh |= 0xffffc000;
3278
3279    tri.r = tri.g = tri.b = tri.a = 0xff;
3280    tri.s = tri.t = tri.w = 0;
3281    tri.z = 0;
3282
3283    if (shade)
3284    {
3285        tri.r    = (rdp_cmd_data[shade_base+0 ] & 0xffff0000) | ((rdp_cmd_data[shade_base+4 ] >> 16) & 0x0000ffff);
3286        tri.g    = ((rdp_cmd_data[shade_base+0 ] << 16) & 0xffff0000) | (rdp_cmd_data[shade_base+4 ] & 0x0000ffff);
3287        tri.b    = (rdp_cmd_data[shade_base+1 ] & 0xffff0000) | ((rdp_cmd_data[shade_base+5 ] >> 16) & 0x0000ffff);
3288        tri.a    = ((rdp_cmd_data[shade_base+1 ] << 16) & 0xffff0000) | (rdp_cmd_data[shade_base+5 ] & 0x0000ffff);
3289        tri.drdx = (rdp_cmd_data[shade_base+2 ] & 0xffff0000) | ((rdp_cmd_data[shade_base+6 ] >> 16) & 0x0000ffff);
3290        tri.dgdx = ((rdp_cmd_data[shade_base+2 ] << 16) & 0xffff0000) | (rdp_cmd_data[shade_base+6 ] & 0x0000ffff);
3291        tri.dbdx = (rdp_cmd_data[shade_base+3 ] & 0xffff0000) | ((rdp_cmd_data[shade_base+7 ] >> 16) & 0x0000ffff);
3292        tri.dadx = ((rdp_cmd_data[shade_base+3 ] << 16) & 0xffff0000) | (rdp_cmd_data[shade_base+7 ] & 0x0000ffff);
3293        tri.drde = (rdp_cmd_data[shade_base+8 ] & 0xffff0000) | ((rdp_cmd_data[shade_base+12] >> 16) & 0x0000ffff);
3294        tri.dgde = ((rdp_cmd_data[shade_base+8 ] << 16) & 0xffff0000) | (rdp_cmd_data[shade_base+12] & 0x0000ffff);
3295        tri.dbde = (rdp_cmd_data[shade_base+9 ] & 0xffff0000) | ((rdp_cmd_data[shade_base+13] >> 16) & 0x0000ffff);
3296        tri.dade = ((rdp_cmd_data[shade_base+9 ] << 16) & 0xffff0000) | (rdp_cmd_data[shade_base+13] & 0x0000ffff);
3297        tri.drdy = (rdp_cmd_data[shade_base+10] & 0xffff0000) | ((rdp_cmd_data[shade_base+14] >> 16) & 0x0000ffff);
3298        tri.dgdy = ((rdp_cmd_data[shade_base+10] << 16) & 0xffff0000) | (rdp_cmd_data[shade_base+14] & 0x0000ffff);
3299        tri.dbdy = (rdp_cmd_data[shade_base+11] & 0xffff0000) | ((rdp_cmd_data[shade_base+15] >> 16) & 0x0000ffff);
3300        tri.dady = ((rdp_cmd_data[shade_base+11] << 16) & 0xffff0000) | (rdp_cmd_data[shade_base+15] & 0x0000ffff);
3301    }
3302    if (texture)
3303    {
3304        tri.s    = (rdp_cmd_data[texture_base+0 ] & 0xffff0000) | ((rdp_cmd_data[texture_base+4 ] >> 16) & 0x0000ffff);
3305        tri.t    = ((rdp_cmd_data[texture_base+0 ] << 16) & 0xffff0000) | (rdp_cmd_data[texture_base+4 ] & 0x0000ffff);
3306        tri.w    = (rdp_cmd_data[texture_base+1 ] & 0xffff0000) | ((rdp_cmd_data[texture_base+5 ] >> 16) & 0x0000ffff);
3307        tri.dsdx = (rdp_cmd_data[texture_base+2 ] & 0xffff0000) | ((rdp_cmd_data[texture_base+6 ] >> 16) & 0x0000ffff);
3308        tri.dtdx = ((rdp_cmd_data[texture_base+2 ] << 16) & 0xffff0000) | (rdp_cmd_data[texture_base+6 ] & 0x0000ffff);
3309        tri.dwdx = (rdp_cmd_data[texture_base+3 ] & 0xffff0000) | ((rdp_cmd_data[texture_base+7 ] >> 16) & 0x0000ffff);
3310        tri.dsde = (rdp_cmd_data[texture_base+8 ] & 0xffff0000) | ((rdp_cmd_data[texture_base+12] >> 16) & 0x0000ffff);
3311        tri.dtde = ((rdp_cmd_data[texture_base+8 ] << 16) & 0xffff0000) | (rdp_cmd_data[texture_base+12] & 0x0000ffff);
3312        tri.dwde = (rdp_cmd_data[texture_base+9 ] & 0xffff0000) | ((rdp_cmd_data[texture_base+13] >> 16) & 0x0000ffff);
3313        tri.dsdy = (rdp_cmd_data[texture_base+10] & 0xffff0000) | ((rdp_cmd_data[texture_base+14] >> 16) & 0x0000ffff);
3314        tri.dtdy = ((rdp_cmd_data[texture_base+10] << 16) & 0xffff0000) | (rdp_cmd_data[texture_base+14] & 0x0000ffff);
3315        tri.dwdy = (rdp_cmd_data[texture_base+11] & 0xffff0000) | ((rdp_cmd_data[texture_base+15] >> 16) & 0x0000ffff);
3316    }
3317    if (zbuffer)
3318    {
3319        tri.z    = rdp_cmd_data[zbuffer_base+0];
3320        tri.dzdx = rdp_cmd_data[zbuffer_base+1];
3321        tri.dzde = rdp_cmd_data[zbuffer_base+2];
3322        tri.dzdy = rdp_cmd_data[zbuffer_base+3];
3323    }
3324
3325    if( other_modes.sample_type == SAMPLE_TYPE_1x1 )
3326    {
3327        fill_span_buffer_1x1( &tri, &span_start, &span_end );
3328    }
3329    else
3330    {
3331        fill_span_buffer_2x2( &tri, &span_start, &span_end );
3332    }
3333
3334    switch (fb_size)
3335    {
3336        case PIXEL_SIZE_16BIT:  render_spans_16(span_start, span_end, tri.tile, shade, texture, zbuffer, tri.flip); break;
3337        case PIXEL_SIZE_32BIT:  render_spans_32(span_start, span_end, tri.tile, shade, texture, zbuffer, tri.flip); break;
3338    }
3339}
3340
24703341/*****************************************************************************/
24713342
24723343INLINE UINT32 READ_RDP_DATA(UINT32 address)

Previous 15994 Revisions Next


© 1998-2018 The MESS Team