Search
lxdream.org :: lxdream/src/pvr2/tacore.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/pvr2/tacore.c
changeset 194:7fcecbcd5f01
prev193:31151fcc3cb7
next198:627220aa0e3b
author nkeynes
date Sat Aug 05 00:02:41 2006 +0000 (13 years ago)
permissions -rw-r--r--
last change Add an input error event when terminating a list while there's an active
vertex list.
view annotate diff log raw
     1 /**
     2  * $Id: tacore.c,v 1.3 2006-08-05 00:02:41 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 #define MIN3( x1, x2, x3 ) ( (x1)<(x2)? ((x1)<(x3)?(x1):(x3)) : ((x2)<(x3)?(x2):(x3)) )
   104 #define MAX3( x1, x2, x3 ) ( (x1)>(x2)? ((x1)>(x3)?(x1):(x3)) : ((x2)>(x3)?(x2):(x3)) )
   106 #define TILESLOT( x, y ) (ta_status.current_tile_matrix + (ta_status.current_tile_size * (y * ta_status.width+ x) << 2))
   108 extern char *video_base;
   109 #define PVRRAM(addr) (*(uint32_t *)(video_base + ((addr)&PVR2_RAM_MASK)))
   111 struct pvr2_ta_vertex {
   112     float x,y,z;
   113     uint32_t detail[8]; /* 0-8 detail words */
   114 };
   116 struct tile_bounds {
   117     int x1, y1, x2, y2;
   118 };
   120 struct pvr2_ta_status {
   121     int state;
   122     int width, height; /* Tile resolution, ie 20x15 */
   123     int tilelist_dir; /* Growth direction of the tilelist, 0 = up, 1 = down */
   124     uint32_t tilelist_size; /* Size of the tilelist segments */
   125     uint32_t tilelist_start; /* Initial address of the tilelist */
   126     int current_vertex_type;
   127     gboolean accept_vertexes;
   128     int vertex_count; /* index of last start-vertex seen, or -1 if no vertexes 
   129 			 * are present
   130 			 */
   131     int max_vertex;     /* Maximum number of vertexes in the current polygon (3/4/6/8) */
   132     int current_list_type;
   133     uint32_t current_tile_matrix; /* Memory location of the first tile for the current list. */
   134     uint32_t current_tile_size; /* Size of the tile matrix space  in 32-bit words (0/8/16/32)*/
   135     uint32_t intensity1, intensity2;
   136     /**
   137      * Current working object
   138      */
   139     int poly_context_size;
   140     int poly_vertex_size;
   141     int poly_parity;
   142     uint32_t poly_context[5];
   143     uint32_t poly_pointer;
   144     struct tile_bounds last_triangle_bounds;
   145     struct pvr2_ta_vertex poly_vertex[8];
   146     int debug_output;
   147 };
   149 static struct pvr2_ta_status ta_status;
   151 static int tilematrix_sizes[4] = {0,8,16,32};
   153 /**
   154  * Convenience union - ta data is either 32-bit integer or 32-bit float.
   155  */
   156 union ta_data {
   157     unsigned int i;
   158     float f;
   159 };
   162 void pvr2_ta_reset() {
   163     ta_status.state = STATE_ERROR; /* State not valid until initialized */
   164     ta_status.debug_output = 0;
   165 }
   167 void pvr2_ta_save_state( FILE *f )
   168 {
   169     fwrite( &ta_status, sizeof(ta_status), 1, f );
   170 }
   172 int pvr2_ta_load_state( FILE *f )
   173 {
   174     if( fread( &ta_status, sizeof(ta_status), 1, f ) != 1 )
   175 	return 1;
   176     return 0;
   177 }
   179 void pvr2_ta_init() {
   180     ta_status.state = STATE_IDLE;
   181     ta_status.current_list_type = -1;
   182     ta_status.current_vertex_type = -1;
   183     ta_status.poly_parity = 0;
   184     ta_status.vertex_count = 0;
   185     ta_status.max_vertex = 3;
   186     ta_status.last_triangle_bounds.x1 = -1;
   187     ta_status.accept_vertexes = TRUE;
   189     uint32_t size = MMIO_READ( PVR2, TA_TILESIZE );
   190     ta_status.width = (size & 0xFFFF) + 1;
   191     ta_status.height = (size >> 16) + 1;
   192     uint32_t control = MMIO_READ( PVR2, TA_TILECFG );
   193     ta_status.tilelist_dir = (control >> 20) & 0x01;
   194     ta_status.tilelist_size = tilematrix_sizes[ (control & 0x03) ];
   195     MMIO_WRITE( PVR2, TA_POLYPOS, MMIO_READ( PVR2, TA_POLYBASE ) );
   196     uint32_t plistpos = MMIO_READ( PVR2, TA_LISTBASE ) >> 2;
   197     if( ta_status.tilelist_dir == TA_GROW_DOWN ) {
   198 	plistpos -= ta_status.tilelist_size;
   199     }
   200     MMIO_WRITE( PVR2, TA_LISTPOS, plistpos );
   201     ta_status.tilelist_start = plistpos;
   202 }
   204 static uint32_t parse_float_colour( float a, float r, float g, float b ) {
   205     return
   206     	(((unsigned int)((256 * CLAMP(a,0.0,1.0))-1))<<24) |
   207     	(((unsigned int)((256 * CLAMP(r,0.0,1.0))-1))<<16) |
   208     	(((unsigned int)((256 * CLAMP(g,0.0,1.0))-1))<<8) |
   209     	(((unsigned int)((256 * CLAMP(b,0.0,1.0))-1)));
   210 }
   212 static uint32_t parse_intensity_colour( uint32_t base, float intensity )
   213 {
   214     unsigned int i = (unsigned int)(256 * CLAMP(intensity, 0.0,1.0));
   216     return
   217 	(((((base & 0xFF) * i) & 0xFF00) |
   218 	  (((base & 0xFF00) * i) & 0xFF0000) |
   219 	  (((base & 0xFF0000) * i) & 0xFF000000)) >> 8) |
   220 	base & 0xFF000000;
   221 }
   223 /**
   224  * Initialize the specified TA list.
   225  */
   226 static void ta_init_list( unsigned int listtype ) {
   227     int config = MMIO_READ( PVR2, TA_TILECFG );
   228     int tile_matrix = MMIO_READ( PVR2, TA_TILEBASE );
   229     int list_end = MMIO_READ( PVR2, TA_LISTEND );
   231     ta_status.current_tile_matrix = tile_matrix;
   233     /* If the list grows down, the end must be < tile matrix start. 
   234      * If it grows up, the end must be > tile matrix start.
   235      * Don't ask me why, it just does...
   236      */
   237     if( ((ta_status.tilelist_dir == TA_GROW_DOWN && list_end <= tile_matrix) ||
   238 	 (ta_status.tilelist_dir == TA_GROW_UP && list_end >= tile_matrix )) &&
   239 	listtype <= TA_LIST_PUNCH_OUT ) {
   240 	int i;
   241 	uint32_t *p;
   242 	for( i=0; i < listtype; i++ ) {
   243 	    int size = tilematrix_sizes[(config & 0x03)] << 2;
   244 	    ta_status.current_tile_matrix += ta_status.width * ta_status.height * size;
   245 	    config >>= 4;
   246 	}
   247 	ta_status.current_tile_size = tilematrix_sizes[(config & 0x03)];
   249 	/* Initialize each tile to 0xF0000000 */
   250 	if( ta_status.current_tile_size != 0 ) {
   251 	    p = (uint32_t *)(video_base + ta_status.current_tile_matrix);
   252 	    for( i=0; i< ta_status.width * ta_status.height; i++ ) {
   253 		*p = 0xF0000000;
   254 		p += ta_status.current_tile_size;
   255 	    }
   256 	}
   257     } else {
   258 	ta_status.current_tile_size = 0;
   259     }
   261     if( tile_matrix == list_end ) {
   262 	ta_status.current_tile_size = 0;
   263     }
   265     ta_status.state = STATE_IN_LIST;
   266     ta_status.current_list_type = listtype;
   267     ta_status.last_triangle_bounds.x1 = -1;
   268 }
   270 static int list_events[5] = {EVENT_PVR_OPAQUE_DONE, EVENT_PVR_OPAQUEMOD_DONE, 
   271 			     EVENT_PVR_TRANS_DONE, EVENT_PVR_TRANSMOD_DONE,
   272 			     EVENT_PVR_PUNCHOUT_DONE };
   274 static void ta_end_list() {
   275     if( ta_status.current_list_type != TA_LIST_NONE ) {
   276 	asic_event( list_events[ta_status.current_list_type] );
   277 	if( ta_status.state == STATE_IN_POLYGON ) {
   278 	    asic_event( EVENT_TA_ERROR );
   279 	    asic_event( EVENT_PVR_BAD_INPUT );
   280 	}
   281 	ta_status.current_list_type = TA_LIST_NONE;
   282 	ta_status.current_vertex_type = -1;
   283 	ta_status.state = STATE_IDLE;
   284     }
   285 }
   287 /**
   288  * Write data out to the polygon buffer.
   289  * If the end-of-buffer is reached, asserts EVENT_PVR_PRIM_ALLOC_FAIL
   290  * @param data to be written
   291  * @param length Number of 32-bit words to write.
   292  * @return number of words actually written
   293  */
   294 static int ta_write_polygon_buffer( uint32_t *data, int length )
   295 {
   296     int rv;
   297     int posn = MMIO_READ( PVR2, TA_POLYPOS );
   298     int end = MMIO_READ( PVR2, TA_POLYEND );
   299     uint32_t *target = (uint32_t *)(video_base + posn);
   300     for( rv=0; rv < length; rv++ ) {
   301 	if( posn == end ) {
   302 	    asic_event( EVENT_PVR_PRIM_ALLOC_FAIL );
   303 	    asic_event( EVENT_TA_ERROR );
   304 	    //	    ta_status.state = STATE_ERROR;
   305 	    break;
   306 	}
   307 	*target++ = *data++;
   308 	posn += 4;
   309     }
   311     MMIO_WRITE( PVR2, TA_POLYPOS, posn );
   312     return rv;
   313 }
   315 #define TA_NO_ALLOC 0xFFFFFFFF
   317 /**
   318  * Allocate a new tile list block from the grow space and update the
   319  * word at reference to be a link to the new block.
   320  */
   321 static uint32_t ta_alloc_tilelist( uint32_t reference ) {
   322     uint32_t posn = MMIO_READ( PVR2, TA_LISTPOS );
   323     uint32_t limit = MMIO_READ( PVR2, TA_LISTEND ) >> 2;
   324     uint32_t newposn;
   325     uint32_t result;
   326     if( ta_status.tilelist_dir == TA_GROW_DOWN ) {
   327 	newposn = posn - ta_status.tilelist_size;
   328 	if( posn == limit ) {
   329 	    PVRRAM(posn<<2) = 0xF0000000;
   330 	    PVRRAM(reference) = 0xE0000000 | (posn<<2);
   331 	    return TA_NO_ALLOC;
   332 	} else if( posn < limit ) {
   333 	    PVRRAM(reference) = 0xE0000000 | (posn<<2);
   334 	    return TA_NO_ALLOC;
   335 	} else if( newposn <= limit ) {
   336 	} else if( newposn <= (limit + ta_status.tilelist_size) ) {
   337 	    asic_event( EVENT_TA_ERROR );
   338 	    asic_event( EVENT_PVR_MATRIX_ALLOC_FAIL );
   339 	    MMIO_WRITE( PVR2, TA_LISTPOS, newposn );
   340 	} else {
   341 	    MMIO_WRITE( PVR2, TA_LISTPOS, newposn );
   342 	}
   343 	PVRRAM(reference) = 0xE0000000 | (posn<<2);
   344 	return posn << 2;
   345     } else {
   346 	newposn = posn + ta_status.tilelist_size;
   347 	if( posn == limit ) {
   348 	    PVRRAM(posn<<2) = 0xF0000000;
   349 	    PVRRAM(reference) = 0xE0000000 | (posn<<2);
   350 	    return TA_NO_ALLOC;
   351 	} else if ( posn > limit ) {
   352 	    PVRRAM(reference) = 0xE0000000 | (posn<<2);
   353 	    return TA_NO_ALLOC;
   354 	} else if( newposn >= limit ) {
   355 	} else if( newposn >= (limit - ta_status.tilelist_size) ) {
   356 	    asic_event( EVENT_TA_ERROR );
   357 	    asic_event( EVENT_PVR_MATRIX_ALLOC_FAIL );
   358 	    MMIO_WRITE( PVR2, TA_LISTPOS, newposn );
   359 	} else {
   360 	    MMIO_WRITE( PVR2, TA_LISTPOS, newposn );
   361 	}	    
   362 	PVRRAM(reference) = 0xE0000000 | (posn<<2);
   363 	return posn << 2;
   364     }
   365 }
   367 /**
   368  * Write a tile entry out to the matrix.
   369  */
   370 static int ta_write_tile_entry( int x, int y, uint32_t tile_entry ) {
   371     uint32_t tile = TILESLOT(x,y);
   372     uint32_t tilestart = tile;
   373     uint32_t value;
   374     uint32_t lasttri = 0;
   375     int i,l;
   377     if( (tile_entry & 0x80000000) && 
   378 	ta_status.last_triangle_bounds.x1 != -1 &&
   379 	ta_status.last_triangle_bounds.x1 <= x &&
   380 	ta_status.last_triangle_bounds.x2 >= x &&
   381 	ta_status.last_triangle_bounds.y1 <= y &&
   382 	ta_status.last_triangle_bounds.y2 >= y ) {
   383 	/* potential for triangle stacking */
   384 	lasttri = tile_entry & 0xE1E00000;
   385     }
   388     if( PVRRAM(tile) == 0xF0000000 ) {
   389 	PVRRAM(tile) = tile_entry;
   390 	PVRRAM(tile+4) = 0xF0000000;
   391 	return;
   392     }
   394     while(1) {
   395 	value = PVRRAM(tile);
   396 	for( i=1; i<ta_status.current_tile_size; i++ ) {
   397 	    tile += 4;
   398 	    uint32_t nextval = PVRRAM(tile);
   399 	    if( nextval == 0xF0000000 ) {
   400 		if( lasttri != 0 && lasttri == (value&0xE1E00000) ) {
   401 		    int count = (value & 0x1E000000) + 0x02000000;
   402 		    if( count < 0x20000000 ) {
   403 			PVRRAM(tile-4) = (value & 0xE1FFFFFF) | count;
   404 			return;
   405 		    }
   406 		}
   407 		if( i < ta_status.current_tile_size-1 ) {
   408 		    PVRRAM(tile) = tile_entry;
   409 		    PVRRAM(tile+4) = 0xF0000000;
   410 		    return;
   411 		}
   412 	    }
   413 	    value = nextval;
   414 	}
   416 	if( value == 0xF0000000 ) {
   417 	    tile = ta_alloc_tilelist(tile);
   418 	    if( tile != TA_NO_ALLOC ) {
   419 		PVRRAM(tile) = tile_entry;
   420 		PVRRAM(tile+4) = 0xF0000000;
   421 	    }
   422 	    return;
   423 	} else if( (value & 0xFF000000) == 0xE0000000 ) {
   424 	    value &= 0x00FFFFFF;
   425 	    if( value == tilestart )
   426 		return 0; /* Loop */
   427 	    tilestart = tile = value;
   428 	} else {
   429 	    /* This should never happen */
   430 	    return 0;
   431 	}
   432     }
   433 }
   435 /**
   436  * Write a completed polygon out to the memory buffers 
   437  * OPTIMIZEME: This is not terribly efficient at the moment.
   438  */
   439 static void ta_commit_polygon( ) {
   440     int i, x, y;
   441     int tx[ta_status.vertex_count], ty[ta_status.vertex_count];
   442     struct tile_bounds triangle_bound[ta_status.vertex_count - 2];
   443     struct tile_bounds polygon_bound;
   444     uint32_t poly_context[5];
   446     if( ta_status.vertex_count < 2 ) {
   447 	return; /* No polygons - ignore */
   448     }
   449     memcpy( poly_context, ta_status.poly_context, ta_status.poly_context_size * 4 );
   451     /* Compute the tile coordinates for each vertex (need to be careful with
   452      * clamping here)
   453      */
   454     for( i=0; i<ta_status.vertex_count; i++ ) {
   455 	if( ta_status.poly_vertex[i].x < 0.0 ) {
   456 	    tx[i] = -1;
   457 	} else if( ta_status.poly_vertex[i].x > (float)INT_MAX ) {
   458 	    tx[i] = INT_MAX/32;
   459 	} else {
   460 	    tx[i] = (int)(ta_status.poly_vertex[i].x / 32.0);
   461 	}
   462 	if( ta_status.poly_vertex[i].y < 0.0 ) {
   463 	    ty[i] = -1;
   464 	} else if( ta_status.poly_vertex[i].y > (float)INT_MAX ) {
   465 	    ty[i] = INT_MAX/32;
   466 	} else {
   467 	    ty[i] = (int)(ta_status.poly_vertex[i].y / 32.0);
   468 	}
   470     }
   472     /* Compute bounding box for each triangle individually, as well
   473      * as the overall polygon.
   474      */
   476     for( i=0; i<ta_status.vertex_count-2; i++ ) {
   477 	triangle_bound[i].x1 = MIN3(tx[i],tx[i+1],tx[i+2]);
   478 	triangle_bound[i].x2 = MAX3(tx[i],tx[i+1],tx[i+2]);
   479 	triangle_bound[i].y1 = MIN3(ty[i],ty[i+1],ty[i+2]);
   480 	triangle_bound[i].y2 = MAX3(ty[i],ty[i+1],ty[i+2]);
   481 	if( i == 0 ) {
   482 	    polygon_bound.x1 = triangle_bound[0].x1;
   483 	    polygon_bound.y1 = triangle_bound[0].y1;
   484 	    polygon_bound.x2 = triangle_bound[0].x2;
   485 	    polygon_bound.y2 = triangle_bound[0].y2;
   486 	} else {
   487 	    polygon_bound.x1 = MIN(polygon_bound.x1, triangle_bound[i].x1);
   488 	    polygon_bound.x2 = MAX(polygon_bound.x2, triangle_bound[i].x2);
   489 	    polygon_bound.y1 = MIN(polygon_bound.y1, triangle_bound[i].y1);
   490 	    polygon_bound.y2 = MAX(polygon_bound.y2, triangle_bound[i].y2);
   491 	}
   492     }
   494     /* If the polygon is actually entirely out of the frustum, clip it entirely */
   495     if( polygon_bound.x2 < 0 || polygon_bound.x1 > ta_status.width ||
   496 	polygon_bound.y2 < 0 || polygon_bound.y1 > ta_status.height ) {
   497 	return;
   498     }
   500     /* Clamp the polygon bounds to the frustum */
   501     if( polygon_bound.x1 < 0 ) polygon_bound.x1 = 0;
   502     if( polygon_bound.x2 >= ta_status.width ) polygon_bound.x2 = ta_status.width-1;
   503     if( polygon_bound.y1 < 0 ) polygon_bound.y1 = 0;
   504     if( polygon_bound.y2 >= ta_status.width ) polygon_bound.y2 = ta_status.height-1;
   506     /* Set the "single tile" flag if it's entirely contained in 1 tile */
   507     if( polygon_bound.x1 == polygon_bound.x2 &&
   508 	polygon_bound.y1 == polygon_bound.y2 ) {
   509 	poly_context[0] |= 0x00200000;
   510     }
   512     /* Ok, we're good to go - write out the polygon first */
   513     uint32_t tile_entry = MMIO_READ( PVR2, TA_POLYPOS ) >> 2 | ta_status.poly_pointer;
   515     int status = ta_write_polygon_buffer( poly_context, ta_status.poly_context_size );
   516     if( status == 0 ) {
   517 	/* No memory available - abort */
   518 	return;
   519     } else {
   520 	for( i=0; i<ta_status.vertex_count && status != 0; i++ ) {
   521 	    status = ta_write_polygon_buffer( (uint32_t *)(&ta_status.poly_vertex[i]), 3 + ta_status.poly_vertex_size );
   522 	}
   523     }
   525     if( ta_status.current_tile_size == 0 ) {
   526 	/* No memory for tile entry, so don't write anything */
   527 	return;
   528     }
   530     /* And now the tile entries. Triangles are different from everything else */
   531     if( ta_status.vertex_count == 3 ) {
   532 	tile_entry |= 0x80000000;
   533 	for( y=polygon_bound.y1; y<=polygon_bound.y2; y++ ) {
   534 	    for( x=polygon_bound.x1; x<=polygon_bound.x2; x++ ) {
   535 		ta_write_tile_entry( x,y,tile_entry );
   536 	    }
   537 	}
   538 	ta_status.last_triangle_bounds.x1 = polygon_bound.x1;
   539 	ta_status.last_triangle_bounds.y1 = polygon_bound.y1;
   540 	ta_status.last_triangle_bounds.x2 = polygon_bound.x2;
   541 	ta_status.last_triangle_bounds.y2 = polygon_bound.y2;
   542     } else if( ta_status.current_vertex_type == TA_VERTEX_SPRITE ||
   543 	       ta_status.current_vertex_type == TA_VERTEX_TEX_SPRITE ) {
   544 	tile_entry |= 0xA0000000;
   545 	for( y=polygon_bound.y1; y<=polygon_bound.y2; y++ ) {
   546 	    for( x=polygon_bound.x1; x<=polygon_bound.x2; x++ ) {
   547 		ta_write_tile_entry( x,y,tile_entry );
   548 	    }
   549 	}
   550 	ta_status.last_triangle_bounds.x1 = polygon_bound.x1;
   551 	ta_status.last_triangle_bounds.y1 = polygon_bound.y1;
   552 	ta_status.last_triangle_bounds.x2 = polygon_bound.x2;
   553 	ta_status.last_triangle_bounds.y2 = polygon_bound.y2;
   554     } else {
   555 	for( y=polygon_bound.y1; y<=polygon_bound.y2; y++ ) {
   556 	    for( x=polygon_bound.x1; x<=polygon_bound.x2; x++ ) {
   557 		uint32_t entry = tile_entry;
   558 		for( i=0; i<ta_status.vertex_count-2; i++ ) {
   559 		    if( triangle_bound[i].x1 <= x && triangle_bound[i].x2 >= x &&
   560 			triangle_bound[i].y1 <= y && triangle_bound[i].y2 >= y ) {
   561 			entry |= (0x40000000>>i);
   562 		    }
   563 		}
   564 		ta_write_tile_entry( x, y, entry );
   565 	    }
   566 	}
   567 	ta_status.last_triangle_bounds.x1 = -1;
   568     }
   569 }
   571 /**
   572  * Variant of ta_split_polygon called when vertex_count == max_vertex, but 
   573  * the client hasn't sent the LAST VERTEX flag. Commit the poly as normal
   574  * first, then start a new poly with the first 2 vertexes taken from the 
   575  * current one.
   576  */
   577 static void ta_split_polygon() {
   578     ta_commit_polygon();
   579     if( TA_IS_NORMAL_POLY() ) { 
   580 	/* This only applies to ordinary polys - Sprites + modifier lists are
   581 	 * handled differently
   582 	 */
   583 	if( ta_status.vertex_count == 3 ) {
   584 	    /* Triangles use an odd/even scheme */
   585 	    if( ta_status.poly_parity == 0 ) {
   586 		memcpy( &ta_status.poly_vertex[0], &ta_status.poly_vertex[2], 
   587 			sizeof(struct pvr2_ta_vertex) );
   588 		ta_status.poly_parity = 1;
   589 	    } else {
   590 		memcpy( &ta_status.poly_vertex[1], &ta_status.poly_vertex[2],
   591 			sizeof(struct pvr2_ta_vertex) );
   592 		ta_status.poly_parity = 0;
   593 	    }
   594 	} else {
   595 	    /* Everything else just uses the last 2 vertexes in order */
   596 	    memcpy( &ta_status.poly_vertex[0], &ta_status.poly_vertex[ta_status.vertex_count-2], 
   597 		    sizeof(struct pvr2_ta_vertex)*2 );
   598 	    ta_status.poly_parity = 0;
   599 	}
   600 	ta_status.vertex_count = 2;
   601     } else {
   602 	ta_status.vertex_count = 0;
   603     }
   604 }
   606 /**
   607  * Parse the polygon context block and setup the internal state to receive
   608  * vertexes.
   609  * @param data 32 bytes of parameter data.
   610  */
   611 static void ta_parse_polygon_context( union ta_data *data ) {
   612     int colourfmt = TA_POLYCMD_COLOURFMT(data[0].i);
   613     if( TA_POLYCMD_USELENGTH(data[0].i) ) {
   614 	ta_status.max_vertex = TA_POLYCMD_LENGTH(data[0].i);
   615     }
   616     ta_status.vertex_count = 0;
   617     ta_status.poly_context[0] = 
   618 	(data[1].i & 0xFC1FFFFF) | ((data[0].i & 0x0B) << 22);
   619     ta_status.poly_context[1] = data[2].i;
   620     ta_status.poly_context[3] = data[4].i;
   621     ta_status.poly_parity = 0;
   622     if( data[0].i & TA_POLYCMD_TEXTURED ) {
   623 	ta_status.current_vertex_type = data[0].i & 0x0D;
   624 	ta_status.poly_context[2] = data[3].i;
   625 	ta_status.poly_context[4] = data[5].i;
   626 	if( data[0].i & TA_POLYCMD_SPECULAR ) {
   627 	    ta_status.poly_context[0] |= 0x01000000;
   628 	    ta_status.poly_vertex_size = 4;
   629 	} else {
   630 	    ta_status.poly_vertex_size = 3;
   631 	}
   632 	if( data[0].i & TA_POLYCMD_UV16 ) {
   633 	    ta_status.poly_vertex_size--;
   634 	}
   635     } else {
   636 	ta_status.current_vertex_type = 0;
   637 	ta_status.poly_vertex_size = 1;
   638 	ta_status.poly_context[2] = 0;
   639 	ta_status.poly_context[4] = 0;
   640     }
   642     ta_status.poly_pointer = (ta_status.poly_vertex_size << 21);
   643     ta_status.poly_context_size = 3;
   644     if( data[0].i & TA_POLYCMD_MODIFIED ) {
   645 	ta_status.poly_pointer |= 0x01000000;
   646 	if( data[0].i & TA_POLYCMD_FULLMOD ) {
   647 	    ta_status.poly_context_size = 5;
   648 	    ta_status.poly_vertex_size <<= 1;
   649 	    ta_status.current_vertex_type |= 0x40;
   650 	    /* Modified/float not supported - behaves as per last intensity */
   651 	    if( colourfmt == TA_POLYCMD_COLOURFMT_FLOAT ) {
   652 		colourfmt = TA_POLYCMD_COLOURFMT_LASTINT;
   653 	    }
   654 	}
   655     }
   657     if( colourfmt == TA_POLYCMD_COLOURFMT_INTENSITY ) {
   658 	if( TA_POLYCMD_IS_FULLMOD(data[0].i) ||
   659 	    TA_POLYCMD_IS_SPECULAR(data[0].i) ) {
   660 	    ta_status.state = STATE_EXPECT_POLY_BLOCK2;
   661 	} else {
   662 	    ta_status.intensity1 = 
   663 		parse_float_colour( data[4].f, data[5].f, data[6].f, data[7].f );
   664 	}
   665     } else if( colourfmt == TA_POLYCMD_COLOURFMT_LASTINT ) {
   666 	colourfmt = TA_POLYCMD_COLOURFMT_INTENSITY;
   667     }
   669     ta_status.current_vertex_type |= colourfmt;
   670 }
   672 /**
   673  * Parse the modifier volume context block and setup the internal state to 
   674  * receive modifier vertexes.
   675  * @param data 32 bytes of parameter data.
   676  */
   677 static void ta_parse_modifier_context( union ta_data *data ) {
   678     ta_status.current_vertex_type = TA_VERTEX_MOD_VOLUME;
   679     ta_status.poly_vertex_size = 0;
   680     ta_status.poly_context_size = 3;
   681     ta_status.poly_context[0] = (data[1].i & 0xFC1FFFFF) |
   682 	((data[0].i & 0x0B)<<22);
   683     if( TA_POLYCMD_IS_SPECULAR(data[0].i) ) {
   684 	ta_status.poly_context[0] |= 0x01000000;
   685     }
   686     ta_status.poly_context[1] = 0;
   687     ta_status.poly_context[2] = 0;
   688     ta_status.vertex_count = 0;
   689     ta_status.max_vertex = 3;
   690     ta_status.poly_pointer = 0;
   691 }
   693 /**
   694  * Parse the sprite context block and setup the internal state to receive
   695  * vertexes.
   696  * @param data 32 bytes of parameter data.
   697  */
   698 static void ta_parse_sprite_context( union ta_data *data ) {
   699     ta_status.poly_context_size = 3;
   700     ta_status.poly_context[0] = (data[1].i & 0xFC1FFFFF) |
   701 	((data[0].i & 0x0B)<<22) | 0x00400000;
   702     if( TA_POLYCMD_IS_SPECULAR(data[0].i) ) {
   703 	ta_status.poly_context[0] |= 0x01000000;
   704     }
   705     ta_status.poly_context[1] = data[2].i;
   706     ta_status.poly_context[2] = data[3].i;
   707     if( data[0].i & TA_POLYCMD_TEXTURED ) {
   708 	ta_status.poly_vertex_size = 2;
   709 	ta_status.poly_vertex[2].detail[1] = data[4].i;
   710 	ta_status.current_vertex_type = TA_VERTEX_TEX_SPRITE;
   711     } else {
   712 	ta_status.poly_vertex_size = 1;
   713 	ta_status.poly_vertex[2].detail[0] = data[4].i;
   714 	ta_status.current_vertex_type = TA_VERTEX_SPRITE;
   715     }
   716     ta_status.vertex_count = 0;
   717     ta_status.max_vertex = 4;
   718     ta_status.poly_pointer = (ta_status.poly_vertex_size << 21);
   719 }
   721 /**
   722  * Copy the last read vertex into all vertexes up to max_vertex. Used for
   723  * Aborted polygons under some circumstances.
   724  */
   725 static void ta_fill_vertexes( ) {
   726     int i;
   727     for( i=ta_status.vertex_count; i<ta_status.max_vertex; i++ ) {
   728 	memcpy( &ta_status.poly_vertex[i], &ta_status.poly_vertex[ta_status.vertex_count-1],
   729 		sizeof( struct pvr2_ta_vertex ) );
   730     }
   731 }
   733 static void ta_parse_vertex( union ta_data *data ) {
   734     struct pvr2_ta_vertex *vertex = &ta_status.poly_vertex[ta_status.vertex_count];
   735     vertex->x = data[1].f;
   736     vertex->y = data[2].f;
   737     vertex->z = data[3].f;
   739     switch( ta_status.current_vertex_type ) {
   740     case TA_VERTEX_PACKED:
   741 	vertex->detail[0] = data[6].i;
   742 	break;
   743     case TA_VERTEX_FLOAT:
   744 	vertex->detail[0] = parse_float_colour( data[4].f, data[5].f, data[6].f, data[7].f );
   745 	break;
   746     case TA_VERTEX_INTENSITY:
   747 	vertex->detail[0] = parse_intensity_colour( ta_status.intensity1, data[6].f );
   748 	break;
   750     case TA_VERTEX_TEX_SPEC_PACKED:
   751 	vertex->detail[3] = data[7].i; /* ARGB */
   752 	/* Fallthrough */
   753     case TA_VERTEX_TEX_PACKED:
   754 	vertex->detail[0] = data[4].i; /* U */
   755 	vertex->detail[1] = data[5].i; /* V */
   756 	vertex->detail[2] = data[6].i; /* ARGB */
   757 	break;
   758     case TA_VERTEX_TEX_UV16_SPEC_PACKED:
   759 	vertex->detail[2] = data[7].i; /* ARGB */
   760 	/* Fallthrough */
   761     case TA_VERTEX_TEX_UV16_PACKED:
   762 	vertex->detail[0] = data[4].i; /* UV */
   763 	vertex->detail[1] = data[6].i; /* ARGB */
   764 	break;
   766     case TA_VERTEX_TEX_FLOAT:
   767     case TA_VERTEX_TEX_SPEC_FLOAT:
   768 	vertex->detail[0] = data[4].i; /* U */
   769 	vertex->detail[1] = data[5].i; /* UV */
   770 	ta_status.state = STATE_EXPECT_VERTEX_BLOCK2;
   771 	break;
   772     case TA_VERTEX_TEX_UV16_FLOAT:
   773     case TA_VERTEX_TEX_UV16_SPEC_FLOAT:
   774 	vertex->detail[0] = data[4].i; /* UV */
   775 	ta_status.state = STATE_EXPECT_VERTEX_BLOCK2;
   776 	break;
   778     case TA_VERTEX_TEX_SPEC_INTENSITY:
   779 	vertex->detail[3] = parse_intensity_colour( ta_status.intensity2, data[7].f );
   780 	/* Fallthrough */
   781     case TA_VERTEX_TEX_INTENSITY:
   782 	vertex->detail[0] = data[4].i; /* U */
   783 	vertex->detail[1] = data[5].i; /* V */
   784 	vertex->detail[2] = parse_intensity_colour( ta_status.intensity1, data[6].f );
   785 	break;
   786     case TA_VERTEX_TEX_UV16_SPEC_INTENSITY:
   787 	vertex->detail[2] = parse_intensity_colour( ta_status.intensity2, data[7].f );
   788 	/* Fallthrough */
   789     case TA_VERTEX_TEX_UV16_INTENSITY:
   790 	vertex->detail[0] = data[4].i; /* UV */
   791 	vertex->detail[1] = parse_intensity_colour( ta_status.intensity1, data[6].f );
   792 	break;
   794     case TA_VERTEX_PACKED_MOD:
   795 	vertex->detail[0] = data[4].i; /* ARGB */
   796 	vertex->detail[1] = data[5].i; /* ARGB */
   797 	break;
   798     case TA_VERTEX_INTENSITY_MOD:
   799 	vertex->detail[0] = parse_intensity_colour( ta_status.intensity1, data[4].f );
   800 	vertex->detail[1] = parse_intensity_colour( ta_status.intensity2, data[5].f );
   801 	break;
   803     case TA_VERTEX_TEX_SPEC_PACKED_MOD:
   804 	vertex->detail[3] = data[7].i; /* ARGB0 */
   805 	/* Fallthrough */
   806     case TA_VERTEX_TEX_PACKED_MOD:
   807 	vertex->detail[0] = data[4].i; /* U0 */
   808 	vertex->detail[1] = data[5].i; /* V0 */
   809 	vertex->detail[2] = data[6].i; /* ARGB0 */
   810 	ta_status.state = STATE_EXPECT_VERTEX_BLOCK2;
   811 	break;
   812     case TA_VERTEX_TEX_UV16_SPEC_PACKED_MOD:
   813 	vertex->detail[2] = data[7].i; /* ARGB0 */
   814 	/* Fallthrough */
   815     case TA_VERTEX_TEX_UV16_PACKED_MOD:
   816 	vertex->detail[0] = data[4].i; /* UV0 */
   817 	vertex->detail[1] = data[6].i; /* ARGB0 */
   818 	ta_status.state = STATE_EXPECT_VERTEX_BLOCK2;
   819 	break;
   821     case TA_VERTEX_TEX_SPEC_INTENSITY_MOD:
   822 	vertex->detail[3] = parse_intensity_colour( ta_status.intensity1, data[7].f );
   823 	/* Fallthrough */
   824     case TA_VERTEX_TEX_INTENSITY_MOD:
   825 	vertex->detail[0] = data[4].i; /* U0 */
   826 	vertex->detail[1] = data[5].i; /* V0 */
   827 	vertex->detail[2] = parse_intensity_colour( ta_status.intensity1, data[6].f );
   828 	ta_status.state = STATE_EXPECT_VERTEX_BLOCK2;
   829 	break;
   830     case TA_VERTEX_TEX_UV16_SPEC_INTENSITY_MOD:
   831 	vertex->detail[2] = parse_intensity_colour( ta_status.intensity1, data[7].f );
   832 	/* Fallthrough */
   833     case TA_VERTEX_TEX_UV16_INTENSITY_MOD:
   834 	vertex->detail[0] = data[4].i; /* UV0 */
   835 	vertex->detail[1] = parse_intensity_colour( ta_status.intensity1, data[6].f );
   836 	ta_status.state = STATE_EXPECT_VERTEX_BLOCK2;
   837 	break;
   839     case TA_VERTEX_SPRITE:
   840     case TA_VERTEX_TEX_SPRITE:
   841     case TA_VERTEX_MOD_VOLUME:
   842 	vertex++;
   843 	vertex->x = data[4].f;
   844 	vertex->y = data[5].f;
   845 	vertex->z = data[6].f;
   846 	vertex++;
   847 	vertex->x = data[7].f;
   848 	ta_status.vertex_count += 2;
   849 	ta_status.state = STATE_EXPECT_VERTEX_BLOCK2;
   850 	break;
   851     }
   852     ta_status.vertex_count++;
   853 }
   855 static void ta_parse_vertex_block2( union ta_data *data ) {
   856     struct pvr2_ta_vertex *vertex = &ta_status.poly_vertex[ta_status.vertex_count-1];
   858     switch( ta_status.current_vertex_type ) {
   859     case TA_VERTEX_TEX_SPEC_FLOAT:
   860 	vertex->detail[3] = parse_float_colour( data[4].f, data[5].f, data[6].f, data[7].f );
   861 	/* Fallthrough */
   862     case TA_VERTEX_TEX_FLOAT:
   863 	vertex->detail[2] = parse_float_colour( data[0].f, data[1].f, data[2].f, data[3].f );
   864 	break;
   865     case TA_VERTEX_TEX_UV16_SPEC_FLOAT:
   866 	vertex->detail[2] = parse_float_colour( data[4].f, data[5].f, data[6].f, data[7].f );
   867 	/* Fallthrough */
   868     case TA_VERTEX_TEX_UV16_FLOAT:
   869 	vertex->detail[1] = parse_float_colour( data[0].f, data[1].f, data[2].f, data[3].f );
   870 	break;
   871     case TA_VERTEX_TEX_PACKED_MOD:
   872 	vertex->detail[3] = data[0].i; /* U1 */
   873 	vertex->detail[4] = data[1].i; /* V1 */
   874 	vertex->detail[5] = data[2].i; /* ARGB1 */
   875 	break;
   876     case TA_VERTEX_TEX_SPEC_PACKED_MOD:
   877 	vertex->detail[4] = data[0].i; /* U1 */
   878 	vertex->detail[5] = data[1].i; /* V1 */
   879 	vertex->detail[6] = data[2].i; /* ARGB1 */
   880 	vertex->detail[7] = data[3].i; /* ARGB1 */
   881 	break;
   882     case TA_VERTEX_TEX_UV16_PACKED_MOD:
   883 	vertex->detail[2] = data[0].i; /* UV1 */
   884 	vertex->detail[3] = data[2].i; /* ARGB1 */
   885 	break;
   886     case TA_VERTEX_TEX_UV16_SPEC_PACKED_MOD:
   887 	vertex->detail[3] = data[0].i; /* UV1 */
   888 	vertex->detail[4] = data[2].i; /* ARGB1 */
   889 	vertex->detail[5] = data[3].i; /* ARGB1 */
   890 	break;
   892     case TA_VERTEX_TEX_INTENSITY_MOD:
   893 	vertex->detail[3] = data[0].i; /* U1 */
   894 	vertex->detail[4] = data[1].i; /* V1 */
   895 	vertex->detail[5] = parse_intensity_colour( ta_status.intensity2, data[2].f ); /* ARGB1 */
   896 	break;
   897     case TA_VERTEX_TEX_SPEC_INTENSITY_MOD:
   898 	vertex->detail[4] = data[0].i; /* U1 */
   899 	vertex->detail[5] = data[1].i; /* V1 */
   900 	vertex->detail[6] = parse_intensity_colour( ta_status.intensity2, data[2].f ); /* ARGB1 */
   901 	vertex->detail[7] = parse_intensity_colour( ta_status.intensity2, data[3].f ); /* ARGB1 */
   902 	break;
   903     case TA_VERTEX_TEX_UV16_INTENSITY_MOD:
   904 	vertex->detail[2] = data[0].i; /* UV1 */
   905 	vertex->detail[3] = parse_intensity_colour( ta_status.intensity2, data[2].f ); /* ARGB1 */
   906 	break;
   907     case TA_VERTEX_TEX_UV16_SPEC_INTENSITY_MOD:
   908 	vertex->detail[3] = data[0].i; /* UV1 */
   909 	vertex->detail[4] = parse_intensity_colour( ta_status.intensity2, data[2].f ); /* ARGB1 */
   910 	vertex->detail[5] = parse_intensity_colour( ta_status.intensity2, data[3].f ); /* ARGB1 */
   911 	break;
   913     case TA_VERTEX_SPRITE:
   914 	vertex->y = data[0].f;
   915 	vertex->z = data[1].f;
   916 	vertex++;
   917 	ta_status.vertex_count++;
   918 	vertex->x = data[2].f;
   919 	vertex->y = data[3].f;
   920 	vertex->z = 0;
   921 	vertex->detail[0] = 0;
   922 	ta_status.poly_vertex[0].detail[0] = 0;
   923 	ta_status.poly_vertex[1].detail[0] = 0;
   924 	break;
   925     case TA_VERTEX_TEX_SPRITE:
   926 	vertex->y = data[0].f;
   927 	vertex->z = data[1].f;
   928 	vertex++;
   929 	ta_status.vertex_count++;
   930 	vertex->x = data[2].f;
   931 	vertex->y = data[3].f;
   932 	vertex->z = 0;
   933 	vertex->detail[0] = 0;
   934 	vertex->detail[1] = 0;
   935 	ta_status.poly_vertex[0].detail[0] = data[5].i;
   936 	ta_status.poly_vertex[0].detail[1] = 0;
   937 	ta_status.poly_vertex[1].detail[0] = data[6].i;
   938 	ta_status.poly_vertex[1].detail[1] = 0;
   939 	ta_status.poly_vertex[2].detail[0] = data[7].i;
   940 	break;
   941     case TA_VERTEX_MOD_VOLUME:
   942 	vertex->y = data[0].f;
   943 	vertex->z = data[1].f;
   944 	break;
   945     }
   946     ta_status.state = STATE_IN_POLYGON;
   947 }
   949 /**
   950  * Process 1 32-byte block of ta data
   951  */
   952 void pvr2_ta_process_block( char *input ) {
   953     union ta_data *data = (union ta_data *)input;
   955     switch( ta_status.state ) {
   956     case STATE_ERROR:
   957 	/* Error raised - stop processing until reset */
   958 	return;
   960     case STATE_EXPECT_POLY_BLOCK2:
   961 	/* This is always a pair of floating-point colours */
   962 	ta_status.intensity1 = 
   963 	    parse_float_colour( data[0].f, data[1].f, data[2].f, data[3].f );
   964 	ta_status.intensity2 =
   965 	    parse_float_colour( data[4].f, data[5].f, data[6].f, data[7].f );
   966 	ta_status.state = STATE_IN_LIST;
   967 	break;
   969     case STATE_EXPECT_VERTEX_BLOCK2:
   970 	ta_parse_vertex_block2( data );
   971 	if( ta_status.vertex_count == ta_status.max_vertex ) {
   972 	    ta_split_polygon();
   973 	}
   974 	break;
   976     case STATE_EXPECT_END_VERTEX_BLOCK2:
   977 	ta_parse_vertex_block2( data );
   978 	ta_commit_polygon();
   979 	ta_status.vertex_count = 0;
   980 	ta_status.poly_parity = 0;
   981 	ta_status.state = STATE_IN_LIST;
   982 	break;
   983     case STATE_IN_LIST:
   984     case STATE_IN_POLYGON:
   985     case STATE_IDLE:
   986 	switch( TA_CMD( data->i ) ) {
   987 	case TA_CMD_END_LIST:
   988 	    ta_end_list();
   989 	    break;
   990 	case TA_CMD_CLIP: /* TODO */
   991 	    break;
   992 	case TA_CMD_POLYGON_CONTEXT:
   993 	    if( ta_status.state == STATE_IDLE ) {
   994 		ta_init_list( TA_POLYCMD_LISTTYPE( data->i ) );
   995 	    }
   997 	    if( ta_status.vertex_count != 0 ) {
   998 		/* Error, and not a very well handled one either */
   999 		asic_event( EVENT_PVR_BAD_INPUT );
  1000 		asic_event( EVENT_TA_ERROR );
  1001 		ta_status.accept_vertexes = FALSE;
  1002 		ta_fill_vertexes();
  1003 	    } else {
  1004 		if( TA_IS_MODIFIER_LIST( ta_status.current_list_type ) ) {
  1005 		    ta_parse_modifier_context(data);
  1006 		} else {
  1007 		    ta_parse_polygon_context(data);
  1010 	    break;
  1011 	case TA_CMD_SPRITE_CONTEXT:
  1012 	    if( ta_status.state == STATE_IDLE ) {
  1013 		ta_init_list( TA_POLYCMD_LISTTYPE( data->i ) );
  1016 	    if( ta_status.vertex_count != 0 ) {
  1017 		ta_fill_vertexes();
  1018 		ta_commit_polygon();
  1021 	    ta_parse_sprite_context(data);
  1022 	    break;
  1023 	case TA_CMD_VERTEX:
  1024 	    ta_status.state = STATE_IN_POLYGON;
  1025 	    ta_parse_vertex(data);
  1027 	    if( ta_status.state == STATE_EXPECT_VERTEX_BLOCK2 ) {
  1028 		if( TA_IS_END_VERTEX(data[0].i) ) {
  1029 		    ta_status.state = STATE_EXPECT_END_VERTEX_BLOCK2;
  1031 	    } else if( TA_IS_END_VERTEX(data->i) ) {
  1032 		ta_commit_polygon();
  1033 		ta_status.vertex_count = 0;
  1034 		ta_status.poly_parity = 0;
  1035 		ta_status.state = STATE_IN_LIST;
  1036 	    } else if( ta_status.vertex_count == ta_status.max_vertex ) {
  1037 		ta_split_polygon();
  1039 	    break;
  1041 	break;
  1048 /**
  1049  * Write a block of data to the tile accelerator, adding the data to the 
  1050  * current scene. We don't make any particular attempt to interpret the data
  1051  * at this stage, deferring that until render time.
  1053  * Currently copies the data verbatim to the vertex buffer, processing only
  1054  * far enough to generate the correct end-of-list events. Tile buffer is
  1055  * entirely ignored.
  1056  */
  1057 void pvr2_ta_write( char *buf, uint32_t length )
  1059     if( ta_status.debug_output ) {
  1060 	fwrite_dump32( (uint32_t *)buf, length, stderr );
  1063     for( ; length >=32; length -= 32 ) {
  1064 	pvr2_ta_process_block( buf );
  1065 	buf += 32;
.