Search
lxdream.org :: lxdream/src/pvr2/tacore.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/pvr2/tacore.c
changeset 199:4433d9c1638c
prev198:627220aa0e3b
next200:c3bdcd373b6d
author nkeynes
date Sun Aug 06 03:50:36 2006 +0000 (13 years ago)
permissions -rw-r--r--
last change Handle NAN vertexes "correctly" (TA treats them as +/- INF)
view annotate diff log raw
     1 /**
     2  * $Id: tacore.c,v 1.5 2006-08-06 03:50:36 nkeynes Exp $
     3  *
     4  * PVR2 Tile Accelerator implementation
     5  *
     6  * Copyright (c) 2005 Nathan Keynes.
     7  *
     8  * This program is free software; you can redistribute it and/or modify
     9  * it under the terms of the GNU General Public License as published by
    10  * the Free Software Foundation; either version 2 of the License, or
    11  * (at your option) any later version.
    12  *
    13  * This program is distributed in the hope that it will be useful,
    14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    16  * GNU General Public License for more details.
    17  */
    18 #include "pvr2.h"
    19 #include "asic.h"
    21 #define STATE_IDLE                 0
    22 #define STATE_IN_LIST              1
    23 #define STATE_IN_POLYGON           2
    24 #define STATE_EXPECT_POLY_BLOCK2   3
    25 #define STATE_EXPECT_VERTEX_BLOCK2 4
    26 #define STATE_ERROR                5
    27 #define STATE_EXPECT_END_VERTEX_BLOCK2 7
    29 #define TA_CMD(i) ( (i) >> 29 )
    30 #define TA_CMD_END_LIST 0
    31 #define TA_CMD_CLIP 1
    32 #define TA_CMD_POLYGON_CONTEXT 4
    33 #define TA_CMD_SPRITE_CONTEXT 5
    34 #define TA_CMD_VERTEX 7
    36 #define TA_LIST_NONE -1
    37 #define TA_LIST_OPAQUE 0
    38 #define TA_LIST_OPAQUE_MOD 1
    39 #define TA_LIST_TRANS 2
    40 #define TA_LIST_TRANS_MOD 3
    41 #define TA_LIST_PUNCH_OUT 4
    42 #define TA_IS_MODIFIER_LIST(list) (list == TA_LIST_OPAQUE_MOD || list == TA_LIST_TRANS_MOD)
    44 #define TA_GROW_UP 0
    45 #define TA_GROW_DOWN 1
    47 #define TA_VERTEX_NONE                        -1
    48 #define TA_VERTEX_PACKED                      0x00
    49 #define TA_VERTEX_TEX_PACKED                  0x08
    50 #define TA_VERTEX_TEX_SPEC_PACKED             0x0C
    51 #define TA_VERTEX_TEX_UV16_PACKED             0x09
    52 #define TA_VERTEX_TEX_UV16_SPEC_PACKED        0x0D
    53 #define TA_VERTEX_FLOAT                       0x10
    54 #define TA_VERTEX_TEX_FLOAT                   0x18
    55 #define TA_VERTEX_TEX_SPEC_FLOAT              0x1C
    56 #define TA_VERTEX_TEX_UV16_FLOAT              0x19
    57 #define TA_VERTEX_TEX_UV16_SPEC_FLOAT         0x1D
    58 #define TA_VERTEX_INTENSITY                   0x20
    59 #define TA_VERTEX_TEX_INTENSITY               0x28
    60 #define TA_VERTEX_TEX_SPEC_INTENSITY          0x2C
    61 #define TA_VERTEX_TEX_UV16_INTENSITY          0x29
    62 #define TA_VERTEX_TEX_UV16_SPEC_INTENSITY     0x2D
    63 #define TA_VERTEX_PACKED_MOD                  0x40
    64 #define TA_VERTEX_TEX_PACKED_MOD              0x48
    65 #define TA_VERTEX_TEX_SPEC_PACKED_MOD         0x4C
    66 #define TA_VERTEX_TEX_UV16_PACKED_MOD         0x49
    67 #define TA_VERTEX_TEX_UV16_SPEC_PACKED_MOD    0x4D
    68 #define TA_VERTEX_INTENSITY_MOD               0x60
    69 #define TA_VERTEX_TEX_INTENSITY_MOD           0x68
    70 #define TA_VERTEX_TEX_SPEC_INTENSITY_MOD      0x6C
    71 #define TA_VERTEX_TEX_UV16_INTENSITY_MOD      0x69
    72 #define TA_VERTEX_TEX_UV16_SPEC_INTENSITY_MOD 0x6D
    73 #define TA_VERTEX_SPRITE                      0x80
    74 #define TA_VERTEX_TEX_SPRITE                  0x88
    75 #define TA_VERTEX_MOD_VOLUME                  0x81
    77 #define TA_IS_NORMAL_POLY() (ta_status.current_vertex_type < TA_VERTEX_SPRITE)
    79 static int strip_lengths[4] = {3,4,6,8}; /* in vertexes */
    80 #define TA_POLYCMD_LISTTYPE(i) ( ((i) >> 24) & 0x0F )
    81 #define TA_POLYCMD_USELENGTH(i) ( i & 0x00800000 )
    82 #define TA_POLYCMD_LENGTH(i)  strip_lengths[((i >> 18) & 0x03)]
    83 #define TA_POLYCMD_CLIP(i)  ((i>>16)&0x03)
    84 #define TA_POLYCMD_COLOURFMT(i)  (i & 0x00000030)
    85 #define TA_POLYCMD_COLOURFMT_ARGB32 0x00000000
    86 #define TA_POLYCMD_COLOURFMT_FLOAT 0x00000010
    87 #define TA_POLYCMD_COLOURFMT_INTENSITY 0x00000020
    88 #define TA_POLYCMD_COLOURFMT_LASTINT 0x00000030
    90 #define TA_POLYCMD_MODIFIED 0x00000080
    91 #define TA_POLYCMD_FULLMOD  0x00000040
    92 #define TA_POLYCMD_TEXTURED 0x00000008
    93 #define TA_POLYCMD_SPECULAR 0x00000004
    94 #define TA_POLYCMD_SHADED 0x00000002
    95 #define TA_POLYCMD_UV16 0x00000001
    97 #define TA_POLYCMD_IS_SPECULAR(i) ((i & 0x0000000C)==0x0000000C) /* Only applies to textured polys */
    98 #define TA_POLYCMD_IS_FULLMOD(i) ((i & 0x000000C0)==0x000000C0)
   101 #define TA_IS_END_VERTEX(i) (i & 0x10000000)
   103 /** Note these are not the IEEE 754 definitions - the TA treats NANs
   104  * as if they were INFs of the appropriate sign.
   105  */
   106 #define TA_IS_INF(f) (((*((uint32_t *)&f)) & 0xFF800000) == 0x7F800000)
   107 #define TA_IS_NINF(f) (((*((uint32_t *)&f)) & 0xFF800000) == 0xFF800000)
   109 #define MIN3( x1, x2, x3 ) ( (x1)<(x2)? ((x1)<(x3)?(x1):(x3)) : ((x2)<(x3)?(x2):(x3)) )
   110 #define MAX3( x1, x2, x3 ) ( (x1)>(x2)? ((x1)>(x3)?(x1):(x3)) : ((x2)>(x3)?(x2):(x3)) )
   112 #define TILESLOT( x, y ) (ta_status.current_tile_matrix + (ta_status.current_tile_size * (y * ta_status.width+ x) << 2))
   114 extern char *video_base;
   115 #define PVRRAM(addr) (*(uint32_t *)(video_base + ((addr)&PVR2_RAM_MASK)))
   117 struct pvr2_ta_vertex {
   118     float x,y,z;
   119     uint32_t detail[8]; /* 0-8 detail words */
   120 };
   122 struct tile_bounds {
   123     int x1, y1, x2, y2;
   124 };
   126 struct pvr2_ta_status {
   127     int state;
   128     int width, height; /* Tile resolution, ie 20x15 */
   129     int tilelist_dir; /* Growth direction of the tilelist, 0 = up, 1 = down */
   130     uint32_t tilelist_size; /* Size of the tilelist segments */
   131     uint32_t tilelist_start; /* Initial address of the tilelist */
   132     int current_vertex_type;
   133     gboolean accept_vertexes;
   134     int vertex_count; /* index of last start-vertex seen, or -1 if no vertexes 
   135 			 * are present
   136 			 */
   137     int max_vertex;     /* Maximum number of vertexes in the current polygon (3/4/6/8) */
   138     int current_list_type;
   139     uint32_t current_tile_matrix; /* Memory location of the first tile for the current list. */
   140     uint32_t current_tile_size; /* Size of the tile matrix space  in 32-bit words (0/8/16/32)*/
   141     uint32_t intensity1, intensity2;
   142     struct tile_bounds clip; 
   143     /**
   144      * Current working object
   145      */
   146     int poly_context_size;
   147     int poly_vertex_size;
   148     int poly_parity;
   149     uint32_t poly_context[5];
   150     uint32_t poly_pointer;
   151     struct tile_bounds last_triangle_bounds;
   152     struct pvr2_ta_vertex poly_vertex[8];
   153     int debug_output;
   154 };
   156 static struct pvr2_ta_status ta_status;
   158 static int tilematrix_sizes[4] = {0,8,16,32};
   160 /**
   161  * Convenience union - ta data is either 32-bit integer or 32-bit float.
   162  */
   163 union ta_data {
   164     unsigned int i;
   165     float f;
   166 };
   169 void pvr2_ta_reset() {
   170     ta_status.state = STATE_ERROR; /* State not valid until initialized */
   171     ta_status.debug_output = 0;
   172 }
   174 void pvr2_ta_save_state( FILE *f )
   175 {
   176     fwrite( &ta_status, sizeof(ta_status), 1, f );
   177 }
   179 int pvr2_ta_load_state( FILE *f )
   180 {
   181     if( fread( &ta_status, sizeof(ta_status), 1, f ) != 1 )
   182 	return 1;
   183     return 0;
   184 }
   186 void pvr2_ta_init() {
   187     ta_status.state = STATE_IDLE;
   188     ta_status.current_list_type = -1;
   189     ta_status.current_vertex_type = -1;
   190     ta_status.poly_parity = 0;
   191     ta_status.vertex_count = 0;
   192     ta_status.max_vertex = 3;
   193     ta_status.last_triangle_bounds.x1 = -1;
   194     ta_status.accept_vertexes = TRUE;
   195     ta_status.clip.x1 = 0;
   196     ta_status.clip.y1 = 0;
   198     uint32_t size = MMIO_READ( PVR2, TA_TILESIZE );
   199     ta_status.width = (size & 0xFFFF) + 1;
   200     ta_status.height = (size >> 16) + 1;
   201     ta_status.clip.x2 = ta_status.width-1;
   202     ta_status.clip.y2 = ta_status.height-1;
   203     uint32_t control = MMIO_READ( PVR2, TA_TILECFG );
   204     ta_status.tilelist_dir = (control >> 20) & 0x01;
   205     ta_status.tilelist_size = tilematrix_sizes[ (control & 0x03) ];
   206     MMIO_WRITE( PVR2, TA_POLYPOS, MMIO_READ( PVR2, TA_POLYBASE ) );
   207     uint32_t plistpos = MMIO_READ( PVR2, TA_LISTBASE ) >> 2;
   208     if( ta_status.tilelist_dir == TA_GROW_DOWN ) {
   209 	plistpos -= ta_status.tilelist_size;
   210     }
   211     MMIO_WRITE( PVR2, TA_LISTPOS, plistpos );
   212     ta_status.tilelist_start = plistpos;
   213 }
   215 static uint32_t parse_float_colour( float a, float r, float g, float b ) {
   216     return
   217     	(((unsigned int)((256 * CLAMP(a,0.0,1.0))-1))<<24) |
   218     	(((unsigned int)((256 * CLAMP(r,0.0,1.0))-1))<<16) |
   219     	(((unsigned int)((256 * CLAMP(g,0.0,1.0))-1))<<8) |
   220     	(((unsigned int)((256 * CLAMP(b,0.0,1.0))-1)));
   221 }
   223 static uint32_t parse_intensity_colour( uint32_t base, float intensity )
   224 {
   225     unsigned int i = (unsigned int)(256 * CLAMP(intensity, 0.0,1.0));
   227     return
   228 	(((((base & 0xFF) * i) & 0xFF00) |
   229 	  (((base & 0xFF00) * i) & 0xFF0000) |
   230 	  (((base & 0xFF0000) * i) & 0xFF000000)) >> 8) |
   231 	base & 0xFF000000;
   232 }
   234 /**
   235  * Initialize the specified TA list.
   236  */
   237 static void ta_init_list( unsigned int listtype ) {
   238     int config = MMIO_READ( PVR2, TA_TILECFG );
   239     int tile_matrix = MMIO_READ( PVR2, TA_TILEBASE );
   240     int list_end = MMIO_READ( PVR2, TA_LISTEND );
   242     ta_status.current_tile_matrix = tile_matrix;
   244     /* If the list grows down, the end must be < tile matrix start. 
   245      * If it grows up, the end must be > tile matrix start.
   246      * Don't ask me why, it just does...
   247      */
   248     if( ((ta_status.tilelist_dir == TA_GROW_DOWN && list_end <= tile_matrix) ||
   249 	 (ta_status.tilelist_dir == TA_GROW_UP && list_end >= tile_matrix )) &&
   250 	listtype <= TA_LIST_PUNCH_OUT ) {
   251 	int i;
   252 	uint32_t *p;
   253 	for( i=0; i < listtype; i++ ) {
   254 	    int size = tilematrix_sizes[(config & 0x03)] << 2;
   255 	    ta_status.current_tile_matrix += ta_status.width * ta_status.height * size;
   256 	    config >>= 4;
   257 	}
   258 	ta_status.current_tile_size = tilematrix_sizes[(config & 0x03)];
   260 	/* Initialize each tile to 0xF0000000 */
   261 	if( ta_status.current_tile_size != 0 ) {
   262 	    p = (uint32_t *)(video_base + ta_status.current_tile_matrix);
   263 	    for( i=0; i< ta_status.width * ta_status.height; i++ ) {
   264 		*p = 0xF0000000;
   265 		p += ta_status.current_tile_size;
   266 	    }
   267 	}
   268     } else {
   269 	ta_status.current_tile_size = 0;
   270     }
   272     if( tile_matrix == list_end ) {
   273 	ta_status.current_tile_size = 0;
   274     }
   276     ta_status.state = STATE_IN_LIST;
   277     ta_status.current_list_type = listtype;
   278     ta_status.last_triangle_bounds.x1 = -1;
   279 }
   281 static int list_events[5] = {EVENT_PVR_OPAQUE_DONE, EVENT_PVR_OPAQUEMOD_DONE, 
   282 			     EVENT_PVR_TRANS_DONE, EVENT_PVR_TRANSMOD_DONE,
   283 			     EVENT_PVR_PUNCHOUT_DONE };
   285 static void ta_end_list() {
   286     if( ta_status.current_list_type != TA_LIST_NONE ) {
   287 	asic_event( list_events[ta_status.current_list_type] );
   288 	if( ta_status.state == STATE_IN_POLYGON ) {
   289 	    asic_event( EVENT_TA_ERROR );
   290 	    asic_event( EVENT_PVR_BAD_INPUT );
   291 	}
   292 	ta_status.current_list_type = TA_LIST_NONE;
   293 	ta_status.current_vertex_type = -1;
   294 	ta_status.state = STATE_IDLE;
   295     }
   296 }
   298 /**
   299  * Write data out to the polygon buffer.
   300  * If the end-of-buffer is reached, asserts EVENT_PVR_PRIM_ALLOC_FAIL
   301  * @param data to be written
   302  * @param length Number of 32-bit words to write.
   303  * @return number of words actually written
   304  */
   305 static int ta_write_polygon_buffer( uint32_t *data, int length )
   306 {
   307     int rv;
   308     int posn = MMIO_READ( PVR2, TA_POLYPOS );
   309     int end = MMIO_READ( PVR2, TA_POLYEND );
   310     uint32_t *target = (uint32_t *)(video_base + posn);
   311     for( rv=0; rv < length; rv++ ) {
   312 	if( posn == end ) {
   313 	    asic_event( EVENT_PVR_PRIM_ALLOC_FAIL );
   314 	    asic_event( EVENT_TA_ERROR );
   315 	    //	    ta_status.state = STATE_ERROR;
   316 	    break;
   317 	}
   318 	*target++ = *data++;
   319 	posn += 4;
   320     }
   322     MMIO_WRITE( PVR2, TA_POLYPOS, posn );
   323     return rv;
   324 }
   326 #define TA_NO_ALLOC 0xFFFFFFFF
   328 /**
   329  * Allocate a new tile list block from the grow space and update the
   330  * word at reference to be a link to the new block.
   331  */
   332 static uint32_t ta_alloc_tilelist( uint32_t reference ) {
   333     uint32_t posn = MMIO_READ( PVR2, TA_LISTPOS );
   334     uint32_t limit = MMIO_READ( PVR2, TA_LISTEND ) >> 2;
   335     uint32_t newposn;
   336     uint32_t result;
   337     if( ta_status.tilelist_dir == TA_GROW_DOWN ) {
   338 	newposn = posn - ta_status.tilelist_size;
   339 	if( posn == limit ) {
   340 	    PVRRAM(posn<<2) = 0xF0000000;
   341 	    PVRRAM(reference) = 0xE0000000 | (posn<<2);
   342 	    return TA_NO_ALLOC;
   343 	} else if( posn < limit ) {
   344 	    PVRRAM(reference) = 0xE0000000 | (posn<<2);
   345 	    return TA_NO_ALLOC;
   346 	} else if( newposn <= limit ) {
   347 	} else if( newposn <= (limit + ta_status.tilelist_size) ) {
   348 	    asic_event( EVENT_TA_ERROR );
   349 	    asic_event( EVENT_PVR_MATRIX_ALLOC_FAIL );
   350 	    MMIO_WRITE( PVR2, TA_LISTPOS, newposn );
   351 	} else {
   352 	    MMIO_WRITE( PVR2, TA_LISTPOS, newposn );
   353 	}
   354 	PVRRAM(reference) = 0xE0000000 | (posn<<2);
   355 	return posn << 2;
   356     } else {
   357 	newposn = posn + ta_status.tilelist_size;
   358 	if( posn == limit ) {
   359 	    PVRRAM(posn<<2) = 0xF0000000;
   360 	    PVRRAM(reference) = 0xE0000000 | (posn<<2);
   361 	    return TA_NO_ALLOC;
   362 	} else if ( posn > limit ) {
   363 	    PVRRAM(reference) = 0xE0000000 | (posn<<2);
   364 	    return TA_NO_ALLOC;
   365 	} else if( newposn >= limit ) {
   366 	} else if( newposn >= (limit - ta_status.tilelist_size) ) {
   367 	    asic_event( EVENT_TA_ERROR );
   368 	    asic_event( EVENT_PVR_MATRIX_ALLOC_FAIL );
   369 	    MMIO_WRITE( PVR2, TA_LISTPOS, newposn );
   370 	} else {
   371 	    MMIO_WRITE( PVR2, TA_LISTPOS, newposn );
   372 	}	    
   373 	PVRRAM(reference) = 0xE0000000 | (posn<<2);
   374 	return posn << 2;
   375     }
   376 }
   378 /**
   379  * Write a tile entry out to the matrix.
   380  */
   381 static int ta_write_tile_entry( int x, int y, uint32_t tile_entry ) {
   382     uint32_t tile = TILESLOT(x,y);
   383     uint32_t tilestart = tile;
   384     uint32_t value;
   385     uint32_t lasttri = 0;
   386     int i,l;
   388     if( (tile_entry & 0x80000000) && 
   389 	ta_status.last_triangle_bounds.x1 != -1 &&
   390 	ta_status.last_triangle_bounds.x1 <= x &&
   391 	ta_status.last_triangle_bounds.x2 >= x &&
   392 	ta_status.last_triangle_bounds.y1 <= y &&
   393 	ta_status.last_triangle_bounds.y2 >= y ) {
   394 	/* potential for triangle stacking */
   395 	lasttri = tile_entry & 0xE1E00000;
   396     }
   399     if( PVRRAM(tile) == 0xF0000000 ) {
   400 	PVRRAM(tile) = tile_entry;
   401 	PVRRAM(tile+4) = 0xF0000000;
   402 	return;
   403     }
   405     while(1) {
   406 	value = PVRRAM(tile);
   407 	for( i=1; i<ta_status.current_tile_size; i++ ) {
   408 	    tile += 4;
   409 	    uint32_t nextval = PVRRAM(tile);
   410 	    if( nextval == 0xF0000000 ) {
   411 		if( lasttri != 0 && lasttri == (value&0xE1E00000) ) {
   412 		    int count = (value & 0x1E000000) + 0x02000000;
   413 		    if( count < 0x20000000 ) {
   414 			PVRRAM(tile-4) = (value & 0xE1FFFFFF) | count;
   415 			return;
   416 		    }
   417 		}
   418 		if( i < ta_status.current_tile_size-1 ) {
   419 		    PVRRAM(tile) = tile_entry;
   420 		    PVRRAM(tile+4) = 0xF0000000;
   421 		    return;
   422 		}
   423 	    }
   424 	    value = nextval;
   425 	}
   427 	if( value == 0xF0000000 ) {
   428 	    tile = ta_alloc_tilelist(tile);
   429 	    if( tile != TA_NO_ALLOC ) {
   430 		PVRRAM(tile) = tile_entry;
   431 		PVRRAM(tile+4) = 0xF0000000;
   432 	    }
   433 	    return;
   434 	} else if( (value & 0xFF000000) == 0xE0000000 ) {
   435 	    value &= 0x00FFFFFF;
   436 	    if( value == tilestart )
   437 		return 0; /* Loop */
   438 	    tilestart = tile = value;
   439 	} else {
   440 	    /* This should never happen */
   441 	    return 0;
   442 	}
   443     }
   444 }
   446 /**
   447  * Write a completed polygon out to the memory buffers 
   448  * OPTIMIZEME: This is not terribly efficient at the moment.
   449  */
   450 static void ta_commit_polygon( ) {
   451     int i, x, y;
   452     int tx[ta_status.vertex_count], ty[ta_status.vertex_count];
   453     struct tile_bounds triangle_bound[ta_status.vertex_count - 2];
   454     struct tile_bounds polygon_bound;
   455     uint32_t poly_context[5];
   457     if( ta_status.vertex_count < 2 ) {
   458 	return; /* No polygons - ignore */
   459     }
   460     memcpy( poly_context, ta_status.poly_context, ta_status.poly_context_size * 4 );
   462     /* Compute the tile coordinates for each vertex (need to be careful with
   463      * clamping here)
   464      */
   465     for( i=0; i<ta_status.vertex_count; i++ ) {
   466 	if( ta_status.poly_vertex[i].x < 0.0 || TA_IS_NINF(ta_status.poly_vertex[i].x) ) {
   467 	    tx[i] = -1;
   468 	} else if( ta_status.poly_vertex[i].x > (float)INT_MAX || TA_IS_INF(ta_status.poly_vertex[i].x) ) {
   469 	    tx[i] = INT_MAX/32;
   470 	} else {
   471 	    tx[i] = (int)(ta_status.poly_vertex[i].x / 32.0);
   472 	}
   473 	if( ta_status.poly_vertex[i].y < 0.0 || TA_IS_NINF(ta_status.poly_vertex[i].y)) {
   474 	    ty[i] = -1;
   475 	} else if( ta_status.poly_vertex[i].y > (float)INT_MAX || TA_IS_INF(ta_status.poly_vertex[i].y) ) {
   476 	    ty[i] = INT_MAX/32;
   477 	} else {
   478 	    ty[i] = (int)(ta_status.poly_vertex[i].y / 32.0);
   479 	}
   481     }
   483     /* Compute bounding box for each triangle individually, as well
   484      * as the overall polygon.
   485      */
   487     for( i=0; i<ta_status.vertex_count-2; i++ ) {
   488 	triangle_bound[i].x1 = MIN3(tx[i],tx[i+1],tx[i+2]);
   489 	triangle_bound[i].x2 = MAX3(tx[i],tx[i+1],tx[i+2]);
   490 	triangle_bound[i].y1 = MIN3(ty[i],ty[i+1],ty[i+2]);
   491 	triangle_bound[i].y2 = MAX3(ty[i],ty[i+1],ty[i+2]);
   492 	if( i == 0 ) {
   493 	    polygon_bound.x1 = triangle_bound[0].x1;
   494 	    polygon_bound.y1 = triangle_bound[0].y1;
   495 	    polygon_bound.x2 = triangle_bound[0].x2;
   496 	    polygon_bound.y2 = triangle_bound[0].y2;
   497 	} else {
   498 	    polygon_bound.x1 = MIN(polygon_bound.x1, triangle_bound[i].x1);
   499 	    polygon_bound.x2 = MAX(polygon_bound.x2, triangle_bound[i].x2);
   500 	    polygon_bound.y1 = MIN(polygon_bound.y1, triangle_bound[i].y1);
   501 	    polygon_bound.y2 = MAX(polygon_bound.y2, triangle_bound[i].y2);
   502 	}
   503     }
   505     /* If the polygon is actually entirely out of the frustum, clip it entirely */
   506     if( polygon_bound.x2 < 0 || polygon_bound.x1 > ta_status.width ||
   507 	polygon_bound.y2 < 0 || polygon_bound.y1 > ta_status.height ) {
   508 	return;
   509     }
   511     /* Clamp the polygon bounds to the frustum */
   512     if( polygon_bound.x1 < 0 ) polygon_bound.x1 = 0;
   513     if( polygon_bound.x2 >= ta_status.width ) polygon_bound.x2 = ta_status.width-1;
   514     if( polygon_bound.y1 < 0 ) polygon_bound.y1 = 0;
   515     if( polygon_bound.y2 >= ta_status.width ) polygon_bound.y2 = ta_status.height-1;
   517     /* Set the "single tile" flag if it's entirely contained in 1 tile */
   518     if( polygon_bound.x1 == polygon_bound.x2 &&
   519 	polygon_bound.y1 == polygon_bound.y2 ) {
   520 	poly_context[0] |= 0x00200000;
   521     }
   523     /* Ok, we're good to go - write out the polygon first */
   524     uint32_t tile_entry = MMIO_READ( PVR2, TA_POLYPOS ) >> 2 | ta_status.poly_pointer;
   526     int status = ta_write_polygon_buffer( poly_context, ta_status.poly_context_size );
   527     if( status == 0 ) {
   528 	/* No memory available - abort */
   529 	return;
   530     } else {
   531 	for( i=0; i<ta_status.vertex_count && status != 0; i++ ) {
   532 	    status = ta_write_polygon_buffer( (uint32_t *)(&ta_status.poly_vertex[i]), 3 + ta_status.poly_vertex_size );
   533 	}
   534     }
   536     if( ta_status.current_tile_size == 0 ) {
   537 	/* No memory for tile entry, so don't write anything */
   538 	return;
   539     }
   541     /* And now the tile entries. Triangles are different from everything else */
   542     if( ta_status.vertex_count == 3 ) {
   543 	tile_entry |= 0x80000000;
   544 	for( y=polygon_bound.y1; y<=polygon_bound.y2; y++ ) {
   545 	    for( x=polygon_bound.x1; x<=polygon_bound.x2; x++ ) {
   546 		ta_write_tile_entry( x,y,tile_entry );
   547 	    }
   548 	}
   549 	ta_status.last_triangle_bounds.x1 = polygon_bound.x1;
   550 	ta_status.last_triangle_bounds.y1 = polygon_bound.y1;
   551 	ta_status.last_triangle_bounds.x2 = polygon_bound.x2;
   552 	ta_status.last_triangle_bounds.y2 = polygon_bound.y2;
   553     } else if( ta_status.current_vertex_type == TA_VERTEX_SPRITE ||
   554 	       ta_status.current_vertex_type == TA_VERTEX_TEX_SPRITE ) {
   555 	tile_entry |= 0xA0000000;
   556 	for( y=polygon_bound.y1; y<=polygon_bound.y2; y++ ) {
   557 	    for( x=polygon_bound.x1; x<=polygon_bound.x2; x++ ) {
   558 		ta_write_tile_entry( x,y,tile_entry );
   559 	    }
   560 	}
   561 	ta_status.last_triangle_bounds.x1 = polygon_bound.x1;
   562 	ta_status.last_triangle_bounds.y1 = polygon_bound.y1;
   563 	ta_status.last_triangle_bounds.x2 = polygon_bound.x2;
   564 	ta_status.last_triangle_bounds.y2 = polygon_bound.y2;
   565     } else {
   566 	for( y=polygon_bound.y1; y<=polygon_bound.y2; y++ ) {
   567 	    for( x=polygon_bound.x1; x<=polygon_bound.x2; x++ ) {
   568 		uint32_t entry = tile_entry;
   569 		for( i=0; i<ta_status.vertex_count-2; i++ ) {
   570 		    if( triangle_bound[i].x1 <= x && triangle_bound[i].x2 >= x &&
   571 			triangle_bound[i].y1 <= y && triangle_bound[i].y2 >= y ) {
   572 			entry |= (0x40000000>>i);
   573 		    }
   574 		}
   575 		ta_write_tile_entry( x, y, entry );
   576 	    }
   577 	}
   578 	ta_status.last_triangle_bounds.x1 = -1;
   579     }
   580 }
   582 /**
   583  * Variant of ta_split_polygon called when vertex_count == max_vertex, but 
   584  * the client hasn't sent the LAST VERTEX flag. Commit the poly as normal
   585  * first, then start a new poly with the first 2 vertexes taken from the 
   586  * current one.
   587  */
   588 static void ta_split_polygon() {
   589     ta_commit_polygon();
   590     if( TA_IS_NORMAL_POLY() ) { 
   591 	/* This only applies to ordinary polys - Sprites + modifier lists are
   592 	 * handled differently
   593 	 */
   594 	if( ta_status.vertex_count == 3 ) {
   595 	    /* Triangles use an odd/even scheme */
   596 	    if( ta_status.poly_parity == 0 ) {
   597 		memcpy( &ta_status.poly_vertex[0], &ta_status.poly_vertex[2], 
   598 			sizeof(struct pvr2_ta_vertex) );
   599 		ta_status.poly_parity = 1;
   600 	    } else {
   601 		memcpy( &ta_status.poly_vertex[1], &ta_status.poly_vertex[2],
   602 			sizeof(struct pvr2_ta_vertex) );
   603 		ta_status.poly_parity = 0;
   604 	    }
   605 	} else {
   606 	    /* Everything else just uses the last 2 vertexes in order */
   607 	    memcpy( &ta_status.poly_vertex[0], &ta_status.poly_vertex[ta_status.vertex_count-2], 
   608 		    sizeof(struct pvr2_ta_vertex)*2 );
   609 	    ta_status.poly_parity = 0;
   610 	}
   611 	ta_status.vertex_count = 2;
   612     } else {
   613 	ta_status.vertex_count = 0;
   614     }
   615 }
   617 /**
   618  * Parse the polygon context block and setup the internal state to receive
   619  * vertexes.
   620  * @param data 32 bytes of parameter data.
   621  */
   622 static void ta_parse_polygon_context( union ta_data *data ) {
   623     int colourfmt = TA_POLYCMD_COLOURFMT(data[0].i);
   624     if( TA_POLYCMD_USELENGTH(data[0].i) ) {
   625 	ta_status.max_vertex = TA_POLYCMD_LENGTH(data[0].i);
   626     }
   627     ta_status.vertex_count = 0;
   628     ta_status.poly_context[0] = 
   629 	(data[1].i & 0xFC1FFFFF) | ((data[0].i & 0x0B) << 22);
   630     ta_status.poly_context[1] = data[2].i;
   631     ta_status.poly_context[3] = data[4].i;
   632     ta_status.poly_parity = 0;
   633     if( data[0].i & TA_POLYCMD_TEXTURED ) {
   634 	ta_status.current_vertex_type = data[0].i & 0x0D;
   635 	ta_status.poly_context[2] = data[3].i;
   636 	ta_status.poly_context[4] = data[5].i;
   637 	if( data[0].i & TA_POLYCMD_SPECULAR ) {
   638 	    ta_status.poly_context[0] |= 0x01000000;
   639 	    ta_status.poly_vertex_size = 4;
   640 	} else {
   641 	    ta_status.poly_vertex_size = 3;
   642 	}
   643 	if( data[0].i & TA_POLYCMD_UV16 ) {
   644 	    ta_status.poly_vertex_size--;
   645 	}
   646     } else {
   647 	ta_status.current_vertex_type = 0;
   648 	ta_status.poly_vertex_size = 1;
   649 	ta_status.poly_context[2] = 0;
   650 	ta_status.poly_context[4] = 0;
   651     }
   653     ta_status.poly_pointer = (ta_status.poly_vertex_size << 21);
   654     ta_status.poly_context_size = 3;
   655     if( data[0].i & TA_POLYCMD_MODIFIED ) {
   656 	ta_status.poly_pointer |= 0x01000000;
   657 	if( data[0].i & TA_POLYCMD_FULLMOD ) {
   658 	    ta_status.poly_context_size = 5;
   659 	    ta_status.poly_vertex_size <<= 1;
   660 	    ta_status.current_vertex_type |= 0x40;
   661 	    /* Modified/float not supported - behaves as per last intensity */
   662 	    if( colourfmt == TA_POLYCMD_COLOURFMT_FLOAT ) {
   663 		colourfmt = TA_POLYCMD_COLOURFMT_LASTINT;
   664 	    }
   665 	}
   666     }
   668     if( colourfmt == TA_POLYCMD_COLOURFMT_INTENSITY ) {
   669 	if( TA_POLYCMD_IS_FULLMOD(data[0].i) ||
   670 	    TA_POLYCMD_IS_SPECULAR(data[0].i) ) {
   671 	    ta_status.state = STATE_EXPECT_POLY_BLOCK2;
   672 	} else {
   673 	    ta_status.intensity1 = 
   674 		parse_float_colour( data[4].f, data[5].f, data[6].f, data[7].f );
   675 	}
   676     } else if( colourfmt == TA_POLYCMD_COLOURFMT_LASTINT ) {
   677 	colourfmt = TA_POLYCMD_COLOURFMT_INTENSITY;
   678     }
   680     ta_status.current_vertex_type |= colourfmt;
   681 }
   683 /**
   684  * Parse the modifier volume context block and setup the internal state to 
   685  * receive modifier vertexes.
   686  * @param data 32 bytes of parameter data.
   687  */
   688 static void ta_parse_modifier_context( union ta_data *data ) {
   689     ta_status.current_vertex_type = TA_VERTEX_MOD_VOLUME;
   690     ta_status.poly_vertex_size = 0;
   691     ta_status.poly_context_size = 3;
   692     ta_status.poly_context[0] = (data[1].i & 0xFC1FFFFF) |
   693 	((data[0].i & 0x0B)<<22);
   694     if( TA_POLYCMD_IS_SPECULAR(data[0].i) ) {
   695 	ta_status.poly_context[0] |= 0x01000000;
   696     }
   697     ta_status.poly_context[1] = 0;
   698     ta_status.poly_context[2] = 0;
   699     ta_status.vertex_count = 0;
   700     ta_status.max_vertex = 3;
   701     ta_status.poly_pointer = 0;
   702 }
   704 /**
   705  * Parse the sprite context block and setup the internal state to receive
   706  * vertexes.
   707  * @param data 32 bytes of parameter data.
   708  */
   709 static void ta_parse_sprite_context( union ta_data *data ) {
   710     ta_status.poly_context_size = 3;
   711     ta_status.poly_context[0] = (data[1].i & 0xFC1FFFFF) |
   712 	((data[0].i & 0x0B)<<22) | 0x00400000;
   713     if( TA_POLYCMD_IS_SPECULAR(data[0].i) ) {
   714 	ta_status.poly_context[0] |= 0x01000000;
   715     }
   716     ta_status.poly_context[1] = data[2].i;
   717     ta_status.poly_context[2] = data[3].i;
   718     if( data[0].i & TA_POLYCMD_TEXTURED ) {
   719 	ta_status.poly_vertex_size = 2;
   720 	ta_status.poly_vertex[2].detail[1] = data[4].i;
   721 	ta_status.current_vertex_type = TA_VERTEX_TEX_SPRITE;
   722     } else {
   723 	ta_status.poly_vertex_size = 1;
   724 	ta_status.poly_vertex[2].detail[0] = data[4].i;
   725 	ta_status.current_vertex_type = TA_VERTEX_SPRITE;
   726     }
   727     ta_status.vertex_count = 0;
   728     ta_status.max_vertex = 4;
   729     ta_status.poly_pointer = (ta_status.poly_vertex_size << 21);
   730 }
   732 /**
   733  * Copy the last read vertex into all vertexes up to max_vertex. Used for
   734  * Aborted polygons under some circumstances.
   735  */
   736 static void ta_fill_vertexes( ) {
   737     int i;
   738     for( i=ta_status.vertex_count; i<ta_status.max_vertex; i++ ) {
   739 	memcpy( &ta_status.poly_vertex[i], &ta_status.poly_vertex[ta_status.vertex_count-1],
   740 		sizeof( struct pvr2_ta_vertex ) );
   741     }
   742 }
   744 static void ta_parse_vertex( union ta_data *data ) {
   745     struct pvr2_ta_vertex *vertex = &ta_status.poly_vertex[ta_status.vertex_count];
   746     vertex->x = data[1].f;
   747     vertex->y = data[2].f;
   748     vertex->z = data[3].f;
   750     switch( ta_status.current_vertex_type ) {
   751     case TA_VERTEX_PACKED:
   752 	vertex->detail[0] = data[6].i;
   753 	break;
   754     case TA_VERTEX_FLOAT:
   755 	vertex->detail[0] = parse_float_colour( data[4].f, data[5].f, data[6].f, data[7].f );
   756 	break;
   757     case TA_VERTEX_INTENSITY:
   758 	vertex->detail[0] = parse_intensity_colour( ta_status.intensity1, data[6].f );
   759 	break;
   761     case TA_VERTEX_TEX_SPEC_PACKED:
   762 	vertex->detail[3] = data[7].i; /* ARGB */
   763 	/* Fallthrough */
   764     case TA_VERTEX_TEX_PACKED:
   765 	vertex->detail[0] = data[4].i; /* U */
   766 	vertex->detail[1] = data[5].i; /* V */
   767 	vertex->detail[2] = data[6].i; /* ARGB */
   768 	break;
   769     case TA_VERTEX_TEX_UV16_SPEC_PACKED:
   770 	vertex->detail[2] = data[7].i; /* ARGB */
   771 	/* Fallthrough */
   772     case TA_VERTEX_TEX_UV16_PACKED:
   773 	vertex->detail[0] = data[4].i; /* UV */
   774 	vertex->detail[1] = data[6].i; /* ARGB */
   775 	break;
   777     case TA_VERTEX_TEX_FLOAT:
   778     case TA_VERTEX_TEX_SPEC_FLOAT:
   779 	vertex->detail[0] = data[4].i; /* U */
   780 	vertex->detail[1] = data[5].i; /* UV */
   781 	ta_status.state = STATE_EXPECT_VERTEX_BLOCK2;
   782 	break;
   783     case TA_VERTEX_TEX_UV16_FLOAT:
   784     case TA_VERTEX_TEX_UV16_SPEC_FLOAT:
   785 	vertex->detail[0] = data[4].i; /* UV */
   786 	ta_status.state = STATE_EXPECT_VERTEX_BLOCK2;
   787 	break;
   789     case TA_VERTEX_TEX_SPEC_INTENSITY:
   790 	vertex->detail[3] = parse_intensity_colour( ta_status.intensity2, data[7].f );
   791 	/* Fallthrough */
   792     case TA_VERTEX_TEX_INTENSITY:
   793 	vertex->detail[0] = data[4].i; /* U */
   794 	vertex->detail[1] = data[5].i; /* V */
   795 	vertex->detail[2] = parse_intensity_colour( ta_status.intensity1, data[6].f );
   796 	break;
   797     case TA_VERTEX_TEX_UV16_SPEC_INTENSITY:
   798 	vertex->detail[2] = parse_intensity_colour( ta_status.intensity2, data[7].f );
   799 	/* Fallthrough */
   800     case TA_VERTEX_TEX_UV16_INTENSITY:
   801 	vertex->detail[0] = data[4].i; /* UV */
   802 	vertex->detail[1] = parse_intensity_colour( ta_status.intensity1, data[6].f );
   803 	break;
   805     case TA_VERTEX_PACKED_MOD:
   806 	vertex->detail[0] = data[4].i; /* ARGB */
   807 	vertex->detail[1] = data[5].i; /* ARGB */
   808 	break;
   809     case TA_VERTEX_INTENSITY_MOD:
   810 	vertex->detail[0] = parse_intensity_colour( ta_status.intensity1, data[4].f );
   811 	vertex->detail[1] = parse_intensity_colour( ta_status.intensity2, data[5].f );
   812 	break;
   814     case TA_VERTEX_TEX_SPEC_PACKED_MOD:
   815 	vertex->detail[3] = data[7].i; /* ARGB0 */
   816 	/* Fallthrough */
   817     case TA_VERTEX_TEX_PACKED_MOD:
   818 	vertex->detail[0] = data[4].i; /* U0 */
   819 	vertex->detail[1] = data[5].i; /* V0 */
   820 	vertex->detail[2] = data[6].i; /* ARGB0 */
   821 	ta_status.state = STATE_EXPECT_VERTEX_BLOCK2;
   822 	break;
   823     case TA_VERTEX_TEX_UV16_SPEC_PACKED_MOD:
   824 	vertex->detail[2] = data[7].i; /* ARGB0 */
   825 	/* Fallthrough */
   826     case TA_VERTEX_TEX_UV16_PACKED_MOD:
   827 	vertex->detail[0] = data[4].i; /* UV0 */
   828 	vertex->detail[1] = data[6].i; /* ARGB0 */
   829 	ta_status.state = STATE_EXPECT_VERTEX_BLOCK2;
   830 	break;
   832     case TA_VERTEX_TEX_SPEC_INTENSITY_MOD:
   833 	vertex->detail[3] = parse_intensity_colour( ta_status.intensity1, data[7].f );
   834 	/* Fallthrough */
   835     case TA_VERTEX_TEX_INTENSITY_MOD:
   836 	vertex->detail[0] = data[4].i; /* U0 */
   837 	vertex->detail[1] = data[5].i; /* V0 */
   838 	vertex->detail[2] = parse_intensity_colour( ta_status.intensity1, data[6].f );
   839 	ta_status.state = STATE_EXPECT_VERTEX_BLOCK2;
   840 	break;
   841     case TA_VERTEX_TEX_UV16_SPEC_INTENSITY_MOD:
   842 	vertex->detail[2] = parse_intensity_colour( ta_status.intensity1, data[7].f );
   843 	/* Fallthrough */
   844     case TA_VERTEX_TEX_UV16_INTENSITY_MOD:
   845 	vertex->detail[0] = data[4].i; /* UV0 */
   846 	vertex->detail[1] = parse_intensity_colour( ta_status.intensity1, data[6].f );
   847 	ta_status.state = STATE_EXPECT_VERTEX_BLOCK2;
   848 	break;
   850     case TA_VERTEX_SPRITE:
   851     case TA_VERTEX_TEX_SPRITE:
   852     case TA_VERTEX_MOD_VOLUME:
   853 	vertex++;
   854 	vertex->x = data[4].f;
   855 	vertex->y = data[5].f;
   856 	vertex->z = data[6].f;
   857 	vertex++;
   858 	vertex->x = data[7].f;
   859 	ta_status.vertex_count += 2;
   860 	ta_status.state = STATE_EXPECT_VERTEX_BLOCK2;
   861 	break;
   862     }
   863     ta_status.vertex_count++;
   864 }
   866 static void ta_parse_vertex_block2( union ta_data *data ) {
   867     struct pvr2_ta_vertex *vertex = &ta_status.poly_vertex[ta_status.vertex_count-1];
   869     switch( ta_status.current_vertex_type ) {
   870     case TA_VERTEX_TEX_SPEC_FLOAT:
   871 	vertex->detail[3] = parse_float_colour( data[4].f, data[5].f, data[6].f, data[7].f );
   872 	/* Fallthrough */
   873     case TA_VERTEX_TEX_FLOAT:
   874 	vertex->detail[2] = parse_float_colour( data[0].f, data[1].f, data[2].f, data[3].f );
   875 	break;
   876     case TA_VERTEX_TEX_UV16_SPEC_FLOAT:
   877 	vertex->detail[2] = parse_float_colour( data[4].f, data[5].f, data[6].f, data[7].f );
   878 	/* Fallthrough */
   879     case TA_VERTEX_TEX_UV16_FLOAT:
   880 	vertex->detail[1] = parse_float_colour( data[0].f, data[1].f, data[2].f, data[3].f );
   881 	break;
   882     case TA_VERTEX_TEX_PACKED_MOD:
   883 	vertex->detail[3] = data[0].i; /* U1 */
   884 	vertex->detail[4] = data[1].i; /* V1 */
   885 	vertex->detail[5] = data[2].i; /* ARGB1 */
   886 	break;
   887     case TA_VERTEX_TEX_SPEC_PACKED_MOD:
   888 	vertex->detail[4] = data[0].i; /* U1 */
   889 	vertex->detail[5] = data[1].i; /* V1 */
   890 	vertex->detail[6] = data[2].i; /* ARGB1 */
   891 	vertex->detail[7] = data[3].i; /* ARGB1 */
   892 	break;
   893     case TA_VERTEX_TEX_UV16_PACKED_MOD:
   894 	vertex->detail[2] = data[0].i; /* UV1 */
   895 	vertex->detail[3] = data[2].i; /* ARGB1 */
   896 	break;
   897     case TA_VERTEX_TEX_UV16_SPEC_PACKED_MOD:
   898 	vertex->detail[3] = data[0].i; /* UV1 */
   899 	vertex->detail[4] = data[2].i; /* ARGB1 */
   900 	vertex->detail[5] = data[3].i; /* ARGB1 */
   901 	break;
   903     case TA_VERTEX_TEX_INTENSITY_MOD:
   904 	vertex->detail[3] = data[0].i; /* U1 */
   905 	vertex->detail[4] = data[1].i; /* V1 */
   906 	vertex->detail[5] = parse_intensity_colour( ta_status.intensity2, data[2].f ); /* ARGB1 */
   907 	break;
   908     case TA_VERTEX_TEX_SPEC_INTENSITY_MOD:
   909 	vertex->detail[4] = data[0].i; /* U1 */
   910 	vertex->detail[5] = data[1].i; /* V1 */
   911 	vertex->detail[6] = parse_intensity_colour( ta_status.intensity2, data[2].f ); /* ARGB1 */
   912 	vertex->detail[7] = parse_intensity_colour( ta_status.intensity2, data[3].f ); /* ARGB1 */
   913 	break;
   914     case TA_VERTEX_TEX_UV16_INTENSITY_MOD:
   915 	vertex->detail[2] = data[0].i; /* UV1 */
   916 	vertex->detail[3] = parse_intensity_colour( ta_status.intensity2, data[2].f ); /* ARGB1 */
   917 	break;
   918     case TA_VERTEX_TEX_UV16_SPEC_INTENSITY_MOD:
   919 	vertex->detail[3] = data[0].i; /* UV1 */
   920 	vertex->detail[4] = parse_intensity_colour( ta_status.intensity2, data[2].f ); /* ARGB1 */
   921 	vertex->detail[5] = parse_intensity_colour( ta_status.intensity2, data[3].f ); /* ARGB1 */
   922 	break;
   924     case TA_VERTEX_SPRITE:
   925 	vertex->y = data[0].f;
   926 	vertex->z = data[1].f;
   927 	vertex++;
   928 	ta_status.vertex_count++;
   929 	vertex->x = data[2].f;
   930 	vertex->y = data[3].f;
   931 	vertex->z = 0;
   932 	vertex->detail[0] = 0;
   933 	ta_status.poly_vertex[0].detail[0] = 0;
   934 	ta_status.poly_vertex[1].detail[0] = 0;
   935 	break;
   936     case TA_VERTEX_TEX_SPRITE:
   937 	vertex->y = data[0].f;
   938 	vertex->z = data[1].f;
   939 	vertex++;
   940 	ta_status.vertex_count++;
   941 	vertex->x = data[2].f;
   942 	vertex->y = data[3].f;
   943 	vertex->z = 0;
   944 	vertex->detail[0] = 0;
   945 	vertex->detail[1] = 0;
   946 	ta_status.poly_vertex[0].detail[0] = data[5].i;
   947 	ta_status.poly_vertex[0].detail[1] = 0;
   948 	ta_status.poly_vertex[1].detail[0] = data[6].i;
   949 	ta_status.poly_vertex[1].detail[1] = 0;
   950 	ta_status.poly_vertex[2].detail[0] = data[7].i;
   951 	break;
   952     case TA_VERTEX_MOD_VOLUME:
   953 	vertex->y = data[0].f;
   954 	vertex->z = data[1].f;
   955 	break;
   956     }
   957     ta_status.state = STATE_IN_POLYGON;
   958 }
   960 /**
   961  * Process 1 32-byte block of ta data
   962  */
   963 void pvr2_ta_process_block( char *input ) {
   964     union ta_data *data = (union ta_data *)input;
   966     switch( ta_status.state ) {
   967     case STATE_ERROR:
   968 	/* Error raised - stop processing until reset */
   969 	return;
   971     case STATE_EXPECT_POLY_BLOCK2:
   972 	/* This is always a pair of floating-point colours */
   973 	ta_status.intensity1 = 
   974 	    parse_float_colour( data[0].f, data[1].f, data[2].f, data[3].f );
   975 	ta_status.intensity2 =
   976 	    parse_float_colour( data[4].f, data[5].f, data[6].f, data[7].f );
   977 	ta_status.state = STATE_IN_LIST;
   978 	break;
   980     case STATE_EXPECT_VERTEX_BLOCK2:
   981 	ta_parse_vertex_block2( data );
   982 	if( ta_status.vertex_count == ta_status.max_vertex ) {
   983 	    ta_split_polygon();
   984 	}
   985 	break;
   987     case STATE_EXPECT_END_VERTEX_BLOCK2:
   988 	ta_parse_vertex_block2( data );
   989 	ta_commit_polygon();
   990 	ta_status.vertex_count = 0;
   991 	ta_status.poly_parity = 0;
   992 	ta_status.state = STATE_IN_LIST;
   993 	break;
   994     case STATE_IN_LIST:
   995     case STATE_IN_POLYGON:
   996     case STATE_IDLE:
   997 	switch( TA_CMD( data->i ) ) {
   998 	case TA_CMD_END_LIST:
   999 	    ta_end_list();
  1000 	    break;
  1001 	case TA_CMD_CLIP:
  1002 	    if( ta_status.state == STATE_IN_POLYGON ) {
  1003 		asic_event( EVENT_PVR_BAD_INPUT );
  1004 		asic_event( EVENT_TA_ERROR );
  1005 		/* Enter stuffed up mode */
  1007 	    ta_status.clip.x1 = data[4].i & 0x3F;
  1008 	    ta_status.clip.y1 = data[5].i & 0x0F;
  1009 	    ta_status.clip.x2 = data[6].i & 0x3F;
  1010 	    ta_status.clip.y2 = data[7].i & 0x0F;
  1011 	    break;
  1012 	case TA_CMD_POLYGON_CONTEXT:
  1013 	    if( ta_status.state == STATE_IDLE ) {
  1014 		ta_init_list( TA_POLYCMD_LISTTYPE( data->i ) );
  1017 	    if( ta_status.vertex_count != 0 ) {
  1018 		/* Error, and not a very well handled one either */
  1019 		asic_event( EVENT_PVR_BAD_INPUT );
  1020 		asic_event( EVENT_TA_ERROR );
  1021 		ta_status.accept_vertexes = FALSE;
  1022 		ta_fill_vertexes();
  1023 	    } else {
  1024 		if( TA_IS_MODIFIER_LIST( ta_status.current_list_type ) ) {
  1025 		    ta_parse_modifier_context(data);
  1026 		} else {
  1027 		    ta_parse_polygon_context(data);
  1030 	    break;
  1031 	case TA_CMD_SPRITE_CONTEXT:
  1032 	    if( ta_status.state == STATE_IDLE ) {
  1033 		ta_init_list( TA_POLYCMD_LISTTYPE( data->i ) );
  1036 	    if( ta_status.vertex_count != 0 ) {
  1037 		ta_fill_vertexes();
  1038 		ta_commit_polygon();
  1041 	    ta_parse_sprite_context(data);
  1042 	    break;
  1043 	case TA_CMD_VERTEX:
  1044 	    ta_status.state = STATE_IN_POLYGON;
  1045 	    ta_parse_vertex(data);
  1047 	    if( ta_status.state == STATE_EXPECT_VERTEX_BLOCK2 ) {
  1048 		if( TA_IS_END_VERTEX(data[0].i) ) {
  1049 		    ta_status.state = STATE_EXPECT_END_VERTEX_BLOCK2;
  1051 	    } else if( TA_IS_END_VERTEX(data->i) ) {
  1052 		ta_commit_polygon();
  1053 		ta_status.vertex_count = 0;
  1054 		ta_status.poly_parity = 0;
  1055 		ta_status.state = STATE_IN_LIST;
  1056 	    } else if( ta_status.vertex_count == ta_status.max_vertex ) {
  1057 		ta_split_polygon();
  1059 	    break;
  1061 	break;
  1068 /**
  1069  * Write a block of data to the tile accelerator, adding the data to the 
  1070  * current scene. We don't make any particular attempt to interpret the data
  1071  * at this stage, deferring that until render time.
  1073  * Currently copies the data verbatim to the vertex buffer, processing only
  1074  * far enough to generate the correct end-of-list events. Tile buffer is
  1075  * entirely ignored.
  1076  */
  1077 void pvr2_ta_write( char *buf, uint32_t length )
  1079     if( ta_status.debug_output ) {
  1080 	fwrite_dump32( (uint32_t *)buf, length, stderr );
  1083     for( ; length >=32; length -= 32 ) {
  1084 	pvr2_ta_process_block( buf );
  1085 	buf += 32;
.