summaryrefslogtreecommitdiff
path: root/src/mesa/pipe/cell/spu/main.c
blob: 6dbd93f5305bc0a3d62bcdfe5fd606315bcd8010 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
/**************************************************************************
 * 
 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
 * All Rights Reserved.
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sub license, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 * 
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 * 
 **************************************************************************/


/* main() for Cell SPU code */


#include <stdio.h>
#include <assert.h>
#include <libmisc.h>
#include <spu_mfcio.h>

#include "main.h"
#include "tri.h"
#include "pipe/cell/common.h"
#include "pipe/p_defines.h"

/*
helpful headers:
/usr/lib/gcc/spu/4.1.1/include/spu_mfcio.h
/opt/ibm/cell-sdk/prototype/sysroot/usr/include/libmisc.h
*/

static boolean Debug = FALSE;

volatile struct cell_init_info init;

struct framebuffer fb;

uint ctile[TILE_SIZE][TILE_SIZE] ALIGN16_ATTRIB;
ushort ztile[TILE_SIZE][TILE_SIZE] ALIGN16_ATTRIB;

ubyte tile_status[MAX_HEIGHT/TILE_SIZE][MAX_WIDTH/TILE_SIZE] ALIGN16_ATTRIB;
ubyte tile_status_z[MAX_HEIGHT/TILE_SIZE][MAX_WIDTH/TILE_SIZE] ALIGN16_ATTRIB;




void
wait_on_mask(unsigned tagMask)
{
   mfc_write_tag_mask( tagMask );
   /* wait for completion of _any_ DMAs specified by tagMask */
   mfc_read_tag_status_any();
}



void
get_tile(const struct framebuffer *fb, uint tx, uint ty, uint *tile,
         int tag, int zBuf)
{
   const uint offset = ty * fb->width_tiles + tx;
   const uint bytesPerTile = TILE_SIZE * TILE_SIZE * (zBuf ? 2 : 4);
   const ubyte *src = zBuf ? fb->depth_start : fb->color_start;

   src += offset * bytesPerTile;

   assert(tx < fb->width_tiles);
   assert(ty < fb->height_tiles);
   ASSERT_ALIGN16(tile);
   /*
   printf("get_tile:  dest: %p  src: 0x%x  size: %d\n",
          tile, (unsigned int) src, bytesPerTile);
   */
   mfc_get(tile,  /* dest in local memory */
           (unsigned int) src, /* src in main memory */
           bytesPerTile,
           tag,
           0, /* tid */
           0  /* rid */);
}


void
put_tile(const struct framebuffer *fb, uint tx, uint ty, const uint *tile,
         int tag, int zBuf)
{
   const uint offset = ty * fb->width_tiles + tx;
   const uint bytesPerTile = TILE_SIZE * TILE_SIZE * (zBuf ? 2 : 4);
   ubyte *dst = zBuf ? fb->depth_start : fb->color_start;

   dst += offset * bytesPerTile;

   assert(tx < fb->width_tiles);
   assert(ty < fb->height_tiles);
   ASSERT_ALIGN16(tile);
   /*
   printf("put_tile:  src: %p  dst: 0x%x  size: %d\n",
          tile, (unsigned int) dst, bytesPerTile);
   */
   mfc_put((void *) tile,  /* src in local memory */
           (unsigned int) dst,  /* dst in main memory */
           bytesPerTile,
           tag,
           0, /* tid */
           0  /* rid */);
}


void
clear_tile(uint tile[TILE_SIZE][TILE_SIZE], uint value)
{
   uint i, j;
   for (i = 0; i < TILE_SIZE; i++) {
      for (j = 0; j < TILE_SIZE; j++) {
         tile[i][j] = value;
      }
   }
}

void
clear_tile_z(ushort tile[TILE_SIZE][TILE_SIZE], uint value)
{
   uint i, j;
   for (i = 0; i < TILE_SIZE; i++) {
      for (j = 0; j < TILE_SIZE; j++) {
         tile[i][j] = value;
      }
   }
}


/**
 * For tiles whose status is TILE_STATUS_CLEAR, write solid-filled
 * tiles back to the main framebuffer.
 */
static void
really_clear_tiles(uint surfaceIndex)
{
   const uint num_tiles = fb.width_tiles * fb.height_tiles;
   uint i, j;

   if (surfaceIndex == 0) {
      for (i = 0; i < TILE_SIZE; i++)
         for (j = 0; j < TILE_SIZE; j++)
            ctile[i][j] = fb.color_clear_value;

      for (i = init.id; i < num_tiles; i += init.num_spus) {
         uint tx = i % fb.width_tiles;
         uint ty = i / fb.width_tiles;
         if (tile_status[ty][tx] == TILE_STATUS_CLEAR) {
            put_tile(&fb, tx, ty, (uint *) ctile, TAG_SURFACE_CLEAR, 0);
         }
      }
   }
   else {
      for (i = 0; i < TILE_SIZE; i++)
         for (j = 0; j < TILE_SIZE; j++)
            ztile[i][j] = fb.depth_clear_value;

      for (i = init.id; i < num_tiles; i += init.num_spus) {
         uint tx = i % fb.width_tiles;
         uint ty = i / fb.width_tiles;
         if (tile_status_z[ty][tx] == TILE_STATUS_CLEAR)
            put_tile(&fb, tx, ty, (uint *) ctile, TAG_SURFACE_CLEAR, 1);
      }
   }

#if 0
   wait_on_mask(1 << TAG_SURFACE_CLEAR);
#endif
}


static void
clear_surface(const struct cell_command_clear_surface *clear)
{
   const uint num_tiles = fb.width_tiles * fb.height_tiles;
   uint i, j;

#define CLEAR_OPT 1
#if CLEAR_OPT
   /* set all tile's status to CLEAR */
   if (clear->surface == 0) {
      memset(tile_status, TILE_STATUS_CLEAR, sizeof(tile_status));
      fb.color_clear_value = clear->value;
   }
   else {
      memset(tile_status_z, TILE_STATUS_CLEAR, sizeof(tile_status_z));
      fb.depth_clear_value = clear->value;
   }
   return;
#endif

   if (clear->surface == 0) {
      for (i = 0; i < TILE_SIZE; i++)
         for (j = 0; j < TILE_SIZE; j++)
            ctile[i][j] = clear->value;
   }
   else {
      for (i = 0; i < TILE_SIZE; i++)
         for (j = 0; j < TILE_SIZE; j++)
            ztile[i][j] = clear->value;
   }

   /*
   printf("SPU: %s num=%d w=%d h=%d\n",
          __FUNCTION__, num_tiles, fb.width_tiles, fb.height_tiles);
   */

   for (i = init.id; i < num_tiles; i += init.num_spus) {
      uint tx = i % fb.width_tiles;
      uint ty = i / fb.width_tiles;
      if (clear->surface == 0)
         put_tile(&fb, tx, ty, (uint *) ctile, TAG_SURFACE_CLEAR, 0);
      else
         put_tile(&fb, tx, ty, (uint *) ztile, TAG_SURFACE_CLEAR, 1);
      /* XXX we don't want this here, but it fixes bad tile results */
   }

#if 0
   wait_on_mask(1 << TAG_SURFACE_CLEAR);
#endif
}


/**
 * Given a rendering command's bounding box (in pixels) compute the
 * location of the corresponding screen tile bounding box.
 */
static INLINE void
tile_bounding_box(const struct cell_command_render *render,
                  uint *txmin, uint *tymin,
                  uint *box_num_tiles, uint *box_width_tiles)
{
#if 1
   /* Debug: full-window bounding box */
   uint txmax = fb.width_tiles - 1;
   uint tymax = fb.height_tiles - 1;
   *txmin = 0;
   *tymin = 0;
   *box_num_tiles = fb.width_tiles * fb.height_tiles;
   *box_width_tiles = fb.width_tiles;
#else
   uint txmax, tymax, box_height_tiles;

   *txmin = (uint) render->xmin / TILE_SIZE;
   *tymin = (uint) render->ymin / TILE_SIZE;
   txmax = (uint) render->xmax / TILE_SIZE;
   tymax = (uint) render->ymax / TILE_SIZE;
   *box_width_tiles = txmax - *txmin + 1;
   box_height_tiles = tymax - *tymin + 1;
   *box_num_tiles = *box_width_tiles * box_height_tiles;
#endif
#if 0
   printf("Render bounds: %g, %g  ...  %g, %g\n",
          render->xmin, render->ymin, render->xmax, render->ymax);
   printf("Render tiles:  %u, %u .. %u, %u\n", *txmin, *tymin, txmax, tymax);
#endif
}


static void
render(const struct cell_command_render *render)
{
   struct cell_prim_buffer prim_buffer ALIGN16_ATTRIB;
   uint i, j, vertex_bytes;

   /*
   printf("SPU %u: RENDER buffer dst=%p  src=%p  size=%d\n",
          init.id,
          &prim_buffer, render->vertex_data, (int)sizeof(prim_buffer));
   */

   ASSERT_ALIGN16(render->vertex_data);
   ASSERT_ALIGN16(&prim_buffer);

   /* how much vertex data */
   vertex_bytes = render->num_verts * render->num_attribs * 4 * sizeof(float);

   /* get vertex data from main memory */
   mfc_get(&prim_buffer,  /* dest */
           (unsigned int) render->vertex_data,  /* src */
           vertex_bytes,  /* size */
           TAG_VERTEX_BUFFER,
           0, /* tid */
           0  /* rid */);
   wait_on_mask(1 << TAG_VERTEX_BUFFER);

   /* find tiles which intersect the prim bounding box */
   uint txmin, tymin, box_width_tiles, box_num_tiles;
   tile_bounding_box(render, &txmin, &tymin,
                     &box_num_tiles, &box_width_tiles);

   /* make sure any pending clears have completed */
   wait_on_mask(1 << TAG_SURFACE_CLEAR);

   /* loop over tiles */
   for (i = init.id; i < box_num_tiles; i += init.num_spus) {
      const uint tx = txmin + i % box_width_tiles;
      const uint ty = tymin + i / box_width_tiles;

      assert(tx < fb.width_tiles);
      assert(ty < fb.height_tiles);

      /* Start fetching color/z tiles.  We'll wait for completion when
       * we need read/write to them later in triangle rasterization.
       */
      if (fb.depth_format == PIPE_FORMAT_Z16_UNORM) {
         if (tile_status_z[ty][tx] != TILE_STATUS_CLEAR) {
            get_tile(&fb, tx, ty, (uint *) ztile, TAG_READ_TILE_Z, 1);
         }
      }

      if (tile_status[ty][tx] != TILE_STATUS_CLEAR) {
         get_tile(&fb, tx, ty, (uint *) ctile, TAG_READ_TILE_COLOR, 0);
      }

      assert(render->prim_type == PIPE_PRIM_TRIANGLES);

      /* loop over tris */
      for (j = 0; j < render->num_verts; j += 3) {
         struct prim_header prim;

         /*
         printf("  %u: Triangle %g,%g  %g,%g  %g,%g\n",
                init.id,
                prim_buffer.vertex[j*3+0][0][0],
                prim_buffer.vertex[j*3+0][0][1],
                prim_buffer.vertex[j*3+1][0][0],
                prim_buffer.vertex[j*3+1][0][1],
                prim_buffer.vertex[j*3+2][0][0],
                prim_buffer.vertex[j*3+2][0][1]);
         */

         /* pos */
         COPY_4V(prim.v[0].data[0], prim_buffer.vertex[j+0][0]);
         COPY_4V(prim.v[1].data[0], prim_buffer.vertex[j+1][0]);
         COPY_4V(prim.v[2].data[0], prim_buffer.vertex[j+2][0]);

         /* color */
         COPY_4V(prim.v[0].data[1], prim_buffer.vertex[j+0][1]);
         COPY_4V(prim.v[1].data[1], prim_buffer.vertex[j+1][1]);
         COPY_4V(prim.v[2].data[1], prim_buffer.vertex[j+2][1]);

         tri_draw(&prim, tx, ty);
      }

      /* write color/z tiles back to main framebuffer, if dirtied */
      if (tile_status[ty][tx] == TILE_STATUS_DIRTY) {
         put_tile(&fb, tx, ty, (uint *) ctile, TAG_WRITE_TILE_COLOR, 0);
         tile_status[ty][tx] = TILE_STATUS_DEFINED;
      }
      if (fb.depth_format == PIPE_FORMAT_Z16_UNORM) {
         if (tile_status_z[ty][tx] == TILE_STATUS_DIRTY) {
            put_tile(&fb, tx, ty, (uint *) ztile, TAG_WRITE_TILE_Z, 1);
            tile_status_z[ty][tx] = TILE_STATUS_DEFINED;
         }
      }

      /* XXX move these... */
      wait_on_mask(1 << TAG_WRITE_TILE_COLOR);
      if (fb.depth_format == PIPE_FORMAT_Z16_UNORM) {
         wait_on_mask(1 << TAG_WRITE_TILE_Z);
      }
   }
}


static void
render_vbuf(const struct cell_command_render_vbuf *render)
{
   /* we'll DMA into these buffers */
   ubyte vertex_data[CELL_MAX_VBUF_SIZE] ALIGN16_ATTRIB;
   ushort indexes[CELL_MAX_VBUF_INDEXES] ALIGN16_ATTRIB;
   uint i, j, vertex_bytes, index_bytes;

   ASSERT_ALIGN16(render->vertex_data);
   ASSERT_ALIGN16(render->index_data);

   /* how much vertex data */
   vertex_bytes = render->num_verts * render->num_attribs * 4 * sizeof(float);
   index_bytes = render->num_indexes * sizeof(ushort);
   if (index_bytes < 8)
      index_bytes = 8;
   else
      index_bytes = (index_bytes + 15) & ~0xf; /* multiple of 16 */

   /*
   printf("VBUF: indices at %p,  vertices at %p  vertex_bytes %u  ind_bytes %u\n",
          render->index_data, render->vertex_data, vertex_bytes, index_bytes);
   */

   /* get vertex data from main memory */
   mfc_get(vertex_data,  /* dest */
           (unsigned int) render->vertex_data,  /* src */
           vertex_bytes,  /* size */
           TAG_VERTEX_BUFFER,
           0, /* tid */
           0  /* rid */);

   /* get index data from main memory */
   mfc_get(indexes,  /* dest */
           (unsigned int) render->index_data,  /* src */
           index_bytes,
           TAG_INDEX_BUFFER,
           0, /* tid */
           0  /* rid */);

   wait_on_mask(1 << TAG_VERTEX_BUFFER);
   wait_on_mask(1 << TAG_INDEX_BUFFER);

   /* find tiles which intersect the prim bounding box */
   uint txmin, tymin, box_width_tiles, box_num_tiles;
#if 0
   tile_bounding_box(render, &txmin, &tymin,
                     &box_num_tiles, &box_width_tiles);
#else
   txmin = 0;
   tymin = 0;
   box_num_tiles = fb.width_tiles * fb.height_tiles;
   box_width_tiles = fb.width_tiles;
#endif

   /* make sure any pending clears have completed */
   wait_on_mask(1 << TAG_SURFACE_CLEAR);

   /* loop over tiles */
   for (i = init.id; i < box_num_tiles; i += init.num_spus) {
      const uint tx = txmin + i % box_width_tiles;
      const uint ty = tymin + i / box_width_tiles;

      assert(tx < fb.width_tiles);
      assert(ty < fb.height_tiles);

      /* Start fetching color/z tiles.  We'll wait for completion when
       * we need read/write to them later in triangle rasterization.
       */
      if (fb.depth_format == PIPE_FORMAT_Z16_UNORM) {
         if (tile_status_z[ty][tx] != TILE_STATUS_CLEAR) {
            get_tile(&fb, tx, ty, (uint *) ztile, TAG_READ_TILE_Z, 1);
         }
      }

      if (tile_status[ty][tx] != TILE_STATUS_CLEAR) {
         get_tile(&fb, tx, ty, (uint *) ctile, TAG_READ_TILE_COLOR, 0);
      }

      assert(render->prim_type == PIPE_PRIM_TRIANGLES);

      /* loop over tris */
      for (j = 0; j < render->num_indexes; j += 3) {
         struct prim_header prim;
         const float *vbuf = (const float *) vertex_data;
         const float *v0, *v1, *v2;

         v0 = vbuf + indexes[j] * render->num_attribs * 4;
         v1 = vbuf + indexes[j+1] * render->num_attribs * 4;
         v2 = vbuf + indexes[j+2] * render->num_attribs * 4;

         /*
         printf("  %u: Triangle %g,%g  %g,%g  %g,%g\n",
                init.id,
                prim_buffer.vertex[j*3+0][0][0],
                prim_buffer.vertex[j*3+0][0][1],
                prim_buffer.vertex[j*3+1][0][0],
                prim_buffer.vertex[j*3+1][0][1],
                prim_buffer.vertex[j*3+2][0][0],
                prim_buffer.vertex[j*3+2][0][1]);
         */

         /* pos */
         COPY_4V(prim.v[0].data[0], v0);
         COPY_4V(prim.v[1].data[0], v1);
         COPY_4V(prim.v[2].data[0], v2);

         /* color */
         COPY_4V(prim.v[0].data[1], v0 + 4);
         COPY_4V(prim.v[1].data[1], v1 + 4);
         COPY_4V(prim.v[2].data[1], v2 + 4);

         tri_draw(&prim, tx, ty);
      }

      /* write color/z tiles back to main framebuffer, if dirtied */
      if (tile_status[ty][tx] == TILE_STATUS_DIRTY) {
         put_tile(&fb, tx, ty, (uint *) ctile, TAG_WRITE_TILE_COLOR, 0);
         tile_status[ty][tx] = TILE_STATUS_DEFINED;
      }
      if (fb.depth_format == PIPE_FORMAT_Z16_UNORM) {
         if (tile_status_z[ty][tx] == TILE_STATUS_DIRTY) {
            put_tile(&fb, tx, ty, (uint *) ztile, TAG_WRITE_TILE_Z, 1);
            tile_status_z[ty][tx] = TILE_STATUS_DEFINED;
         }
      }

      /* XXX move these... */
      wait_on_mask(1 << TAG_WRITE_TILE_COLOR);
      if (fb.depth_format == PIPE_FORMAT_Z16_UNORM) {
         wait_on_mask(1 << TAG_WRITE_TILE_Z);
      }
   }
}



/**
 * Temporary/simple main loop for SPEs: Get a command, execute it, repeat.
 */
static void
main_loop(void)
{
   struct cell_command cmd;
   int exitFlag = 0;

   if (Debug)
      printf("SPU %u: Enter main loop\n", init.id);

   assert((sizeof(struct cell_command) & 0xf) == 0);
   ASSERT_ALIGN16(&cmd);

   while (!exitFlag) {
      unsigned opcode;
      int tag = 0;

      if (Debug)
         printf("SPU %u: Wait for cmd...\n", init.id);

      /* read/wait from mailbox */
      opcode = (unsigned int) spu_read_in_mbox();

      if (Debug)
         printf("SPU %u: got cmd %u\n", init.id, opcode);

      /* command payload */
      mfc_get(&cmd,  /* dest */
              (unsigned int) init.cmd, /* src */
              sizeof(struct cell_command), /* bytes */
              tag,
              0, /* tid */
              0  /* rid */);
      wait_on_mask( 1 << tag );

      switch (opcode) {
      case CELL_CMD_EXIT:
         if (Debug)
            printf("SPU %u: EXIT\n", init.id);
         exitFlag = 1;
         break;
      case CELL_CMD_FRAMEBUFFER:
         if (Debug)
            printf("SPU %u: FRAMEBUFFER: %d x %d at %p, cformat 0x%x  zformat 0x%x\n",
                   init.id,
                   cmd.fb.width,
                   cmd.fb.height,
                   cmd.fb.color_start,
                   cmd.fb.color_format,
                   cmd.fb.depth_format);
         fb.color_start = cmd.fb.color_start;
         fb.depth_start = cmd.fb.depth_start;
         fb.color_format = cmd.fb.color_format;
         fb.depth_format = cmd.fb.depth_format;
         fb.width = cmd.fb.width;
         fb.height = cmd.fb.height;
         fb.width_tiles = (fb.width + TILE_SIZE - 1) / TILE_SIZE;
         fb.height_tiles = (fb.height + TILE_SIZE - 1) / TILE_SIZE;
         /*
         printf("SPU %u: %u x %u tiles\n",
                init.id, fb.width_tiles, fb.height_tiles);
         */
         break;
      case CELL_CMD_CLEAR_SURFACE:
         if (Debug)
            printf("SPU %u: CLEAR SURF %u to 0x%08x\n", init.id,
                   cmd.clear.surface, cmd.clear.value);
         clear_surface(&cmd.clear);
         break;
      case CELL_CMD_RENDER:
         if (Debug)
            printf("SPU %u: RENDER %u verts, prim %u\n",
                   init.id, cmd.render.num_verts, cmd.render.prim_type);
         render(&cmd.render);
         break;
      case CELL_CMD_RENDER_VBUF:
         if (Debug)
            printf("SPU %u: RENDER_VBUF prim %u, indices: %u, nr_vert: %u\n",
                   init.id,
                   cmd.render_vbuf.prim_type,
                   cmd.render_vbuf.num_verts,
                   cmd.render_vbuf.num_indexes);
         render_vbuf(&cmd.render_vbuf);
         break;

      case CELL_CMD_FINISH:
         if (Debug)
            printf("SPU %u: FINISH\n", init.id);
         really_clear_tiles(0);
         /* wait for all outstanding DMAs to finish */
         mfc_write_tag_mask(~0);
         mfc_read_tag_status_all();
         /* send mbox message to PPU */
         spu_write_out_mbox(CELL_CMD_FINISH);
         break;
      default:
         printf("Bad opcode!\n");
      }

   }

   if (Debug)
      printf("SPU %u: Exit main loop\n", init.id);
}



static void
one_time_init(void)
{
   memset(tile_status, TILE_STATUS_DEFINED, sizeof(tile_status));
   memset(tile_status_z, TILE_STATUS_DEFINED, sizeof(tile_status_z));
}


/**
 * SPE entrypoint.
 * Note: example programs declare params as 'unsigned long long' but
 * that doesn't work.
 */
int
main(unsigned long speid, unsigned long argp)
{
   int tag = 0;

   (void) speid;

   one_time_init();

   if (Debug)
      printf("SPU: main() speid=%lu\n", speid);

   mfc_get(&init,  /* dest */
           (unsigned int) argp, /* src */
           sizeof(struct cell_init_info), /* bytes */
           tag,
           0, /* tid */
           0  /* rid */);
   wait_on_mask( 1 << tag );


   main_loop();

   return 0;
}