~ubuntu-branches/ubuntu/precise/mupen64plus/precise

« back to all changes in this revision

Viewing changes to glide64/ucode02.h

  • Committer: Bazaar Package Importer
  • Author(s): Sven Eckelmann
  • Date: 2009-09-08 22:17:00 UTC
  • Revision ID: james.westby@ubuntu.com-20090908221700-yela0ckgc1xwiqtn
Tags: upstream-1.5+dfsg1
ImportĀ upstreamĀ versionĀ 1.5+dfsg1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
* Glide64 - Glide video plugin for Nintendo 64 emulators.
 
3
* Copyright (c) 2002  Dave2001
 
4
*
 
5
* This program is free software; you can redistribute it and/or modify
 
6
* it under the terms of the GNU General Public License as published by
 
7
* the Free Software Foundation; either version 2 of the License, or
 
8
* any later version.
 
9
*
 
10
* This program is distributed in the hope that it will be useful,
 
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
* GNU General Public License for more details.
 
14
*
 
15
* You should have received a copy of the GNU General Public
 
16
* License along with this program; if not, write to the Free
 
17
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 
 
18
* Boston, MA  02110-1301, USA
 
19
*/
 
20
 
 
21
//****************************************************************
 
22
//
 
23
// Glide64 - Glide Plugin for Nintendo 64 emulators (tested mostly with Project64)
 
24
// Project started on December 29th, 2001
 
25
//
 
26
// To modify Glide64:
 
27
// * Write your name and (optional)email, commented by your work, so I know who did it, and so that you can find which parts you modified when it comes time to send it to me.
 
28
// * Do NOT send me the whole project or file that you modified.  Take out your modified code sections, and tell me where to put them.  If people sent the whole thing, I would have many different versions, but no idea how to combine them all.
 
29
//
 
30
// Official Glide64 development channel: #Glide64 on EFnet
 
31
//
 
32
// Original author: Dave2001 (Dave2999@hotmail.com)
 
33
// Other authors: Gonetz, Gugaman
 
34
//
 
35
//****************************************************************
 
36
 
 
37
static void calc_point_light (VERTEX *v, float * vpos)
 
38
{
 
39
  float light_intensity = 0.0f;
 
40
  register float color[3] = {rdp.light[rdp.num_lights].r, rdp.light[rdp.num_lights].g, rdp.light[rdp.num_lights].b};
 
41
  for (DWORD l=0; l<rdp.num_lights; l++)
 
42
  {
 
43
    if (rdp.light[l].nonblack)
 
44
    {
 
45
      float lvec[3] = {rdp.light[l].x, rdp.light[l].y, rdp.light[l].z};
 
46
            lvec[0] -= vpos[0];
 
47
        lvec[1] -= vpos[1];
 
48
        lvec[2] -= vpos[2];
 
49
        float light_len2 = lvec[0]*lvec[0] + lvec[1]*lvec[1] + lvec[2]*lvec[2];
 
50
        float light_len = sqrtf(light_len2);
 
51
#ifdef EXTREME_LOGGING
 
52
        FRDP ("calc_point_light: len: %f, len2: %f\n", light_len, light_len2);
 
53
#endif
 
54
        float at = rdp.light[l].ca + light_len/65535.0f*rdp.light[l].la + light_len2/65535.0f*rdp.light[l].qa;
 
55
        if (at > 0.0f) 
 
56
          light_intensity = 1/at;//DotProduct (lvec, nvec) / (light_len * normal_len * at);
 
57
        else
 
58
          light_intensity = 0.0f;
 
59
    }
 
60
    else
 
61
    {
 
62
      light_intensity = 0.0f;
 
63
    }
 
64
    if (light_intensity > 0.0f)
 
65
    {
 
66
      color[0] += rdp.light[l].r * light_intensity;
 
67
      color[1] += rdp.light[l].g * light_intensity;
 
68
      color[2] += rdp.light[l].b * light_intensity;
 
69
    }
 
70
  }
 
71
  if (color[0] > 1.0f) color[0] = 1.0f;
 
72
  if (color[1] > 1.0f) color[1] = 1.0f;
 
73
  if (color[2] > 1.0f) color[2] = 1.0f;
 
74
  
 
75
  v->r = (BYTE)(color[0]*255.0f);
 
76
  v->g = (BYTE)(color[1]*255.0f);
 
77
  v->b = (BYTE)(color[2]*255.0f);
 
78
}
 
79
 
 
80
static void uc2_vertex ()
 
81
{
 
82
  // This is special, not handled in update(), but here
 
83
  // * Matrix Pre-multiplication idea by Gonetz (Gonetz@ngs.ru)
 
84
  if (rdp.update & UPDATE_MULT_MAT)
 
85
  {
 
86
    rdp.update ^= UPDATE_MULT_MAT;
 
87
    MulMatrices(rdp.model, rdp.proj, rdp.combined);
 
88
  }
 
89
  if (rdp.update & UPDATE_LIGHTS)
 
90
  {
 
91
    rdp.update ^= UPDATE_LIGHTS;
 
92
    
 
93
    // Calculate light vectors
 
94
    for (DWORD l=0; l<rdp.num_lights; l++)
 
95
    {
 
96
      InverseTransformVector(&rdp.light[l].dir_x, rdp.light_vector[l], rdp.model);
 
97
      NormalizeVector (rdp.light_vector[l]);
 
98
    }
 
99
  }
 
100
  
 
101
  DWORD addr = segoffset(rdp.cmd1);
 
102
  int v0, i, n;
 
103
  float x, y, z;
 
104
  
 
105
  rdp.vn = n = (rdp.cmd0 >> 12) & 0xFF;
 
106
  rdp.v0 = v0 = ((rdp.cmd0 >> 1) & 0x7F) - n;
 
107
  
 
108
  FRDP ("uc2:vertex n: %d, v0: %d, from: %08lx\n", n, v0, addr);
 
109
  
 
110
  if (v0 < 0)
 
111
  {
 
112
    RDP_E ("** ERROR: uc2:vertex v0 < 0\n");
 
113
    RDP ("** ERROR: uc2:vertex v0 < 0\n");
 
114
    return;
 
115
  }
 
116
  
 
117
  DWORD geom_mode = rdp.geom_mode;
 
118
  if (settings.fzero && (rdp.geom_mode & 0x40000))
 
119
  {
 
120
    if (((short*)gfx.RDRAM)[(((addr) >> 1) + 4)^1] || ((short*)gfx.RDRAM)[(((addr) >> 1) + 5)^1])
 
121
      rdp.geom_mode ^= 0x40000;
 
122
  }
 
123
  
 
124
  for (i=0; i < (n<<4); i+=16)
 
125
  {
 
126
    VERTEX *v = &rdp.vtx[v0 + (i>>4)];
 
127
    x   = (float)((short*)gfx.RDRAM)[(((addr+i) >> 1) + 0)^1];
 
128
    y   = (float)((short*)gfx.RDRAM)[(((addr+i) >> 1) + 1)^1];
 
129
    z   = (float)((short*)gfx.RDRAM)[(((addr+i) >> 1) + 2)^1];
 
130
    v->flags  = ((WORD*)gfx.RDRAM)[(((addr+i) >> 1) + 3)^1];
 
131
    v->ou   = (float)((short*)gfx.RDRAM)[(((addr+i) >> 1) + 4)^1] * rdp.tiles[rdp.cur_tile].s_scale;
 
132
    v->ov   = (float)((short*)gfx.RDRAM)[(((addr+i) >> 1) + 5)^1] * rdp.tiles[rdp.cur_tile].t_scale;
 
133
    v->a    = ((BYTE*)gfx.RDRAM)[(addr+i + 15)^3];
 
134
    
 
135
#ifdef EXTREME_LOGGING
 
136
    FRDP ("before: v%d - x: %f, y: %f, z: %f, flags: %04lx, ou: %f, ov: %f\n", i>>4, x, y, z, v->flags, v->ou, v->ov);
 
137
#endif
 
138
    
 
139
    v->x = x*rdp.combined[0][0] + y*rdp.combined[1][0] + z*rdp.combined[2][0] + rdp.combined[3][0];
 
140
    v->y = x*rdp.combined[0][1] + y*rdp.combined[1][1] + z*rdp.combined[2][1] + rdp.combined[3][1];
 
141
    v->z = x*rdp.combined[0][2] + y*rdp.combined[1][2] + z*rdp.combined[2][2] + rdp.combined[3][2];
 
142
    v->w = x*rdp.combined[0][3] + y*rdp.combined[1][3] + z*rdp.combined[2][3] + rdp.combined[3][3];
 
143
    
 
144
    v->oow = 1.0f / v->w;
 
145
    v->x_w = v->x * v->oow;
 
146
    v->y_w = v->y * v->oow;
 
147
    v->z_w = v->z * v->oow;
 
148
    CalculateFog (v);
 
149
    
 
150
#ifdef EXTREME_LOGGING
 
151
    FRDP ("v%d - x: %f, y: %f, z: %f, w: %f, u: %f, v: %f, f: %f, z_w: %f, a: 0x%02lx\n", i>>4, v->x, v->y, v->z, v->w, v->ou, v->ov, v->f, v->z_w, v->a);
 
152
#endif
 
153
    
 
154
    v->uv_calculated = 0xFFFFFFFF;
 
155
    v->screen_translated = 0;
 
156
    v->shade_mods_allowed = 1;
 
157
    v->uv_fixed = 0;
 
158
    
 
159
    v->scr_off = 0;
 
160
    if (v->x < -v->w) v->scr_off |= 1;
 
161
    if (v->x > v->w) v->scr_off |= 2;
 
162
    if (v->y < -v->w) v->scr_off |= 4;
 
163
    if (v->y > v->w) v->scr_off |= 8;
 
164
    if (v->w < 0.1f) v->scr_off |= 16;
 
165
     
 
166
    if (rdp.geom_mode & 0x00020000)
 
167
    {
 
168
      v->vec[0] = ((char*)gfx.RDRAM)[(addr+i + 12)^3];
 
169
      v->vec[1] = ((char*)gfx.RDRAM)[(addr+i + 13)^3];
 
170
      v->vec[2] = ((char*)gfx.RDRAM)[(addr+i + 14)^3];
 
171
      //      FRDP("Calc light. x: %f, y: %f z: %f\n", v->vec[0], v->vec[1], v->vec[2]);
 
172
      //      if (!(rdp.geom_mode & 0x800000))
 
173
      {
 
174
        if (rdp.geom_mode & 0x80000) 
 
175
        {
 
176
        calc_linear (v);
 
177
#ifdef EXTREME_LOGGING
 
178
    FRDP ("calc linear: v%d - u: %f, v: %f\n", i>>4, v->ou, v->ov);
 
179
#endif
 
180
        }
 
181
        else if (rdp.geom_mode & 0x40000) 
 
182
        {
 
183
        calc_sphere (v);
 
184
#ifdef EXTREME_LOGGING
 
185
    FRDP ("calc sphere: v%d - u: %f, v: %f\n", i>>4, v->ou, v->ov);
 
186
#endif
 
187
        }
 
188
      }
 
189
      if (rdp.geom_mode & 0x00400000)
 
190
      {
 
191
        float tmpvec[3] = {x, y, z};
 
192
        calc_point_light (v, tmpvec);
 
193
      }
 
194
      else
 
195
      {
 
196
        NormalizeVector (v->vec);
 
197
        calc_light (v);
 
198
      }
 
199
    }
 
200
    else
 
201
    {
 
202
      v->r = ((BYTE*)gfx.RDRAM)[(addr+i + 12)^3];
 
203
      v->g = ((BYTE*)gfx.RDRAM)[(addr+i + 13)^3];
 
204
      v->b = ((BYTE*)gfx.RDRAM)[(addr+i + 14)^3];
 
205
    }
 
206
  }
 
207
  rdp.geom_mode = geom_mode;
 
208
}
 
209
 
 
210
static void uc2_modifyvtx ()
 
211
{
 
212
  BYTE where = (BYTE)((rdp.cmd0 >> 16) & 0xFF);
 
213
  WORD vtx = (WORD)((rdp.cmd0 >> 1) & 0xFFFF);
 
214
  
 
215
  FRDP ("uc2:modifyvtx: vtx: %d, where: 0x%02lx, val: %08lx - ", vtx, where, rdp.cmd1);
 
216
  uc0_modifyvtx(where, vtx, rdp.cmd1);
 
217
}
 
218
 
 
219
static void uc2_culldl ()
 
220
{
 
221
  WORD vStart = (WORD)(rdp.cmd0 & 0xFFFF) >> 1;
 
222
  WORD vEnd = (WORD)(rdp.cmd1 & 0xFFFF) >> 1;
 
223
  DWORD cond = 0;
 
224
  FRDP ("uc2:culldl start: %d, end: %d\n", vStart, vEnd);
 
225
 
 
226
  if (vEnd < vStart) return;
 
227
  for (WORD i=vStart; i<=vEnd; i++)
 
228
  {
 
229
  /*    
 
230
  VERTEX v = &rdp.vtx[i];
 
231
  // Check if completely off the screen (quick frustrum clipping for 90 FOV)
 
232
  if (v->x >= -v->w)
 
233
  cond |= 0x01;
 
234
  if (v->x <= v->w)
 
235
  cond |= 0x02;
 
236
  if (v->y >= -v->w)
 
237
  cond |= 0x04;
 
238
  if (v->y <= v->w)
 
239
  cond |= 0x08;
 
240
  if (v->w >= 0.1f)
 
241
  cond |= 0x10;
 
242
  
 
243
    if (cond == 0x1F)
 
244
    return;
 
245
    //*/
 
246
    
 
247
#ifdef EXTREME_LOGGING
 
248
    FRDP (" v[%d] = (%02f, %02f, %02f, 0x%02lx)\n", i, rdp.vtx[i].x, rdp.vtx[i].y, rdp.vtx[i].w, rdp.vtx[i].scr_off);
 
249
#endif
 
250
    
 
251
    cond |= (~rdp.vtx[i].scr_off) & 0x1F;
 
252
    if (cond == 0x1F)
 
253
      return;
 
254
  }
 
255
  
 
256
  RDP (" - ");  // specify that the enddl is not a real command
 
257
  uc0_enddl ();
 
258
}
 
259
 
 
260
void uc6_obj_loadtxtr ();
 
261
 
 
262
static void uc2_tri1()
 
263
{
 
264
  if (rdp.skip_drawing)
 
265
  {
 
266
    RDP("uc2:tri1. skipped\n");
 
267
    return;
 
268
  }
 
269
  if ((rdp.cmd0 & 0x00FFFFFF) == 0x17)
 
270
  {
 
271
    uc6_obj_loadtxtr ();
 
272
    return;
 
273
  }
 
274
  
 
275
  FRDP("uc1:tri1 #%d - %d, %d, %d\n", rdp.tri_n,
 
276
    ((rdp.cmd0 >> 17) & 0x7F),
 
277
    ((rdp.cmd0 >> 9) & 0x7F),
 
278
    ((rdp.cmd0 >> 1) & 0x7F));
 
279
  
 
280
  VERTEX *v[3] = {
 
281
    &rdp.vtx[(rdp.cmd0 >> 17) & 0x7F],
 
282
      &rdp.vtx[(rdp.cmd0 >> 9) & 0x7F],
 
283
      &rdp.vtx[(rdp.cmd0 >> 1) & 0x7F]
 
284
  };
 
285
  
 
286
  if (cull_tri(v))
 
287
    rdp.tri_n ++;
 
288
  else
 
289
  {
 
290
    update ();
 
291
    
 
292
    DrawTri (v);
 
293
    rdp.tri_n ++;
 
294
  }
 
295
}
 
296
 
 
297
void uc6_obj_ldtx_sprite ();
 
298
void uc6_obj_ldtx_rect ();
 
299
 
 
300
static void uc2_quad ()
 
301
{
 
302
  if (rdp.skip_drawing)
 
303
  {
 
304
    RDP("uc2_quad. skipped\n");
 
305
    return;
 
306
  }
 
307
  if ((rdp.cmd0 & 0x00FFFFFF) == 0x2F)
 
308
  {
 
309
    DWORD command = rdp.cmd0>>24;
 
310
    if (command == 0x6)
 
311
    {
 
312
      uc6_obj_ldtx_sprite ();
 
313
      return;
 
314
    }
 
315
    if (command == 0x7)
 
316
    {
 
317
      uc6_obj_ldtx_rect ();
 
318
      return;
 
319
    }
 
320
  }
 
321
  RDP("uc2:quad");
 
322
 
 
323
  FRDP(" #%d, #%d - %d, %d, %d - %d, %d, %d\n", rdp.tri_n, rdp.tri_n+1,
 
324
    ((rdp.cmd0 >> 17) & 0x7F),
 
325
    ((rdp.cmd0 >> 9) & 0x7F),
 
326
    ((rdp.cmd0 >> 1) & 0x7F),
 
327
    ((rdp.cmd1 >> 17) & 0x7F),
 
328
    ((rdp.cmd1 >> 9) & 0x7F),
 
329
    ((rdp.cmd1 >> 1) & 0x7F));
 
330
 
 
331
  VERTEX *v[6] = {
 
332
    &rdp.vtx[(rdp.cmd0 >> 17) & 0x7F],
 
333
    &rdp.vtx[(rdp.cmd0 >> 9) & 0x7F],
 
334
    &rdp.vtx[(rdp.cmd0 >> 1) & 0x7F],
 
335
    &rdp.vtx[(rdp.cmd1 >> 17) & 0x7F],
 
336
    &rdp.vtx[(rdp.cmd1 >> 9) & 0x7F],
 
337
    &rdp.vtx[(rdp.cmd1 >> 1) & 0x7F]
 
338
  };
 
339
 
 
340
  BOOL updated = 0;
 
341
 
 
342
  if (cull_tri(v))
 
343
    rdp.tri_n ++;
 
344
  else
 
345
  {
 
346
    updated = 1;
 
347
    update ();
 
348
    DrawTri (v);
 
349
    rdp.tri_n ++;
 
350
  }
 
351
 
 
352
  if (cull_tri(v+3))
 
353
    rdp.tri_n ++;
 
354
  else
 
355
  {
 
356
    if (!updated)
 
357
      update ();
 
358
    DrawTri (v+3);
 
359
    rdp.tri_n ++;
 
360
  }
 
361
}
 
362
 
 
363
static void uc6_ldtx_rect_r ();
 
364
 
 
365
static void uc2_line3d ()
 
366
{
 
367
  if ( (rdp.cmd0&0xFF) == 0x2F )
 
368
    uc6_ldtx_rect_r ();
 
369
  else  
 
370
  {
 
371
    FRDP("uc2:line3d #%d, #%d - %d, %d\n", rdp.tri_n, rdp.tri_n+1,
 
372
      (rdp.cmd0 >> 17) & 0x7F,
 
373
      (rdp.cmd0 >> 9) & 0x7F);
 
374
    
 
375
    VERTEX *v[3] = {
 
376
      &rdp.vtx[(rdp.cmd0 >> 17) & 0x7F],
 
377
        &rdp.vtx[(rdp.cmd0 >> 9) & 0x7F],
 
378
        &rdp.vtx[(rdp.cmd0 >> 9) & 0x7F]
 
379
    };
 
380
    WORD width = (WORD)(rdp.cmd0&0xFF) + 1;
 
381
    
 
382
    if (cull_tri(v))
 
383
      rdp.tri_n ++;
 
384
    else
 
385
    {
 
386
      update ();
 
387
      DrawTri (v, width);
 
388
      rdp.tri_n ++;
 
389
    }
 
390
  }
 
391
}
 
392
 
 
393
static void uc2_special3 ()
 
394
{
 
395
  RDP ("uc2:special3\n");
 
396
}
 
397
 
 
398
static void uc2_special2 ()
 
399
{
 
400
  RDP ("uc2:special2\n");
 
401
}
 
402
#ifdef _WIN32
 
403
static void uc2_special1 ()
 
404
{
 
405
  RDP ("uc2:special1\n");
 
406
}
 
407
#endif // _WIN32
 
408
static void uc2_dma_io ()
 
409
{
 
410
  RDP ("uc2:dma_io\n");
 
411
}
 
412
 
 
413
static void uc2_pop_matrix ()
 
414
{
 
415
  FRDP ("uc2:pop_matrix %08lx, %08lx\n", rdp.cmd0, rdp.cmd1);
 
416
  
 
417
  // Just pop the modelview matrix
 
418
  modelview_pop (rdp.cmd1 >> 6);
 
419
}
 
420
 
 
421
static void uc2_geom_mode ()
 
422
{
 
423
  // Switch around some things
 
424
  DWORD clr_mode = (rdp.cmd0 & 0x00DFC9FF) |
 
425
    ((rdp.cmd0 & 0x00000600) << 3) |
 
426
    ((rdp.cmd0 & 0x00200000) >> 12) | 0xFF000000;
 
427
  DWORD set_mode = (rdp.cmd1 & 0xFFDFC9FF) |
 
428
    ((rdp.cmd1 & 0x00000600) << 3) |
 
429
    ((rdp.cmd1 & 0x00200000) >> 12);
 
430
  
 
431
  FRDP("uc2:geom_mode c:%08lx, s:%08lx ", clr_mode, set_mode);
 
432
  
 
433
  rdp.geom_mode &= clr_mode;
 
434
  rdp.geom_mode |= set_mode;
 
435
  
 
436
  FRDP ("result:%08lx\n", rdp.geom_mode);
 
437
  
 
438
  if (rdp.geom_mode & 0x00000001) // Z-Buffer enable
 
439
  {
 
440
    if (!(rdp.flags & ZBUF_ENABLED))
 
441
    {
 
442
      rdp.flags |= ZBUF_ENABLED;
 
443
      rdp.update |= UPDATE_ZBUF_ENABLED;
 
444
    }
 
445
  }
 
446
  else
 
447
  {
 
448
    if ((rdp.flags & ZBUF_ENABLED)) 
 
449
    {
 
450
      if (!settings.flame_corona || (rdp.rm != 0x00504341)) //hack for flame's corona
 
451
        rdp.flags ^= ZBUF_ENABLED;
 
452
      rdp.update |= UPDATE_ZBUF_ENABLED;
 
453
    }
 
454
  }
 
455
  if (rdp.geom_mode & 0x00001000) // Front culling
 
456
  {
 
457
    if (!(rdp.flags & CULL_FRONT))
 
458
    {
 
459
      rdp.flags |= CULL_FRONT;
 
460
      rdp.update |= UPDATE_CULL_MODE;
 
461
    }
 
462
  }
 
463
  else
 
464
  {
 
465
    if (rdp.flags & CULL_FRONT)
 
466
    {
 
467
      rdp.flags ^= CULL_FRONT;
 
468
      rdp.update |= UPDATE_CULL_MODE;
 
469
    }
 
470
  }
 
471
  if (rdp.geom_mode & 0x00002000) // Back culling
 
472
  {
 
473
    if (!(rdp.flags & CULL_BACK))
 
474
    {
 
475
      rdp.flags |= CULL_BACK;
 
476
      rdp.update |= UPDATE_CULL_MODE;
 
477
    }
 
478
  }
 
479
  else
 
480
  {
 
481
    if (rdp.flags & CULL_BACK)
 
482
    {
 
483
      rdp.flags ^= CULL_BACK;
 
484
      rdp.update |= UPDATE_CULL_MODE;
 
485
    }
 
486
  }
 
487
  
 
488
  //Added by Gonetz
 
489
  if (rdp.geom_mode & 0x00010000)      // Fog enable
 
490
  {
 
491
    if (!(rdp.flags & FOG_ENABLED))
 
492
    {
 
493
      rdp.flags |= FOG_ENABLED;
 
494
      rdp.update |= UPDATE_FOG_ENABLED;
 
495
    }
 
496
  }
 
497
  else
 
498
  {
 
499
    if (rdp.flags & FOG_ENABLED)
 
500
    {
 
501
      rdp.flags ^= FOG_ENABLED;
 
502
      rdp.update |= UPDATE_FOG_ENABLED;
 
503
    }
 
504
  }
 
505
}
 
506
 
 
507
void uc6_obj_rectangle_r ();
 
508
 
 
509
static void uc2_matrix ()
 
510
{
 
511
  if (!(rdp.cmd0 & 0x00FFFFFF))
 
512
  {
 
513
     uc6_obj_rectangle_r();
 
514
     return;
 
515
  }
 
516
  RDP ("uc2:matrix\n");
 
517
  
 
518
  DWORD addr = segoffset(rdp.cmd1);
 
519
  BYTE command = (BYTE)((rdp.cmd0 ^ 1) & 0xFF);
 
520
  
 
521
  __declspec( align(16) ) float m[4][4];
 
522
  int x,y;  // matrix index
 
523
  
 
524
  addr >>= 1;
 
525
  
 
526
  for (x=0; x<16; x+=4) { // Adding 4 instead of one, just to remove mult. later
 
527
    for (y=0; y<4; y++) {
 
528
      m[x>>2][y] = (float)(
 
529
        (((__int32)((WORD*)gfx.RDRAM)[(addr+x+y)^1]) << 16) |
 
530
        ((WORD*)gfx.RDRAM)[(addr+x+y+16)^1]
 
531
        ) / 65536.0f;
 
532
    }
 
533
  }
 
534
  
 
535
  switch (command)
 
536
  {
 
537
  case 0: // modelview mul nopush
 
538
    RDP ("modelview mul\n");
 
539
    modelview_mul (m);
 
540
    break;
 
541
    
 
542
  case 1: // modelview mul push
 
543
    RDP ("modelview mul push\n");
 
544
    modelview_mul_push (m);
 
545
    break;
 
546
    
 
547
  case 2: // modelview load nopush
 
548
    RDP ("modelview load\n");
 
549
    modelview_load (m);
 
550
    break;
 
551
    
 
552
  case 3: // modelview load push
 
553
    RDP ("modelview load push\n");
 
554
    modelview_load_push (m);
 
555
    break;
 
556
    
 
557
  case 4: // projection mul nopush
 
558
  case 5: // projection mul push, can't push projection
 
559
    RDP ("projection mul\n");
 
560
    projection_mul (m);
 
561
    break;
 
562
    
 
563
  case 6: // projection load nopush
 
564
  case 7: // projection load push, can't push projection
 
565
    RDP ("projection load\n");
 
566
    projection_load (m);
 
567
    break;
 
568
    
 
569
  default:
 
570
    FRDP_E ("Unknown matrix command, %02lx", command);
 
571
    FRDP ("Unknown matrix command, %02lx", command);
 
572
  }
 
573
  
 
574
#ifdef EXTREME_LOGGING
 
575
  FRDP ("{%f,%f,%f,%f}\n", m[0][0], m[0][1], m[0][2], m[0][3]);
 
576
  FRDP ("{%f,%f,%f,%f}\n", m[1][0], m[1][1], m[1][2], m[1][3]);
 
577
  FRDP ("{%f,%f,%f,%f}\n", m[2][0], m[2][1], m[2][2], m[2][3]);
 
578
  FRDP ("{%f,%f,%f,%f}\n", m[3][0], m[3][1], m[3][2], m[3][3]);
 
579
  FRDP ("\nmodel\n{%f,%f,%f,%f}\n", rdp.model[0][0], rdp.model[0][1], rdp.model[0][2], rdp.model[0][3]);
 
580
  FRDP ("{%f,%f,%f,%f}\n", rdp.model[1][0], rdp.model[1][1], rdp.model[1][2], rdp.model[1][3]);
 
581
  FRDP ("{%f,%f,%f,%f}\n", rdp.model[2][0], rdp.model[2][1], rdp.model[2][2], rdp.model[2][3]);
 
582
  FRDP ("{%f,%f,%f,%f}\n", rdp.model[3][0], rdp.model[3][1], rdp.model[3][2], rdp.model[3][3]);
 
583
  FRDP ("\nproj\n{%f,%f,%f,%f}\n", rdp.proj[0][0], rdp.proj[0][1], rdp.proj[0][2], rdp.proj[0][3]);
 
584
  FRDP ("{%f,%f,%f,%f}\n", rdp.proj[1][0], rdp.proj[1][1], rdp.proj[1][2], rdp.proj[1][3]);
 
585
  FRDP ("{%f,%f,%f,%f}\n", rdp.proj[2][0], rdp.proj[2][1], rdp.proj[2][2], rdp.proj[2][3]);
 
586
  FRDP ("{%f,%f,%f,%f}\n", rdp.proj[3][0], rdp.proj[3][1], rdp.proj[3][2], rdp.proj[3][3]);
 
587
#endif
 
588
}
 
589
 
 
590
static void uc2_moveword ()
 
591
{
 
592
  BYTE index = (BYTE)((rdp.cmd0 >> 16) & 0xFF);
 
593
  WORD offset = (WORD)(rdp.cmd0 & 0xFFFF);
 
594
  DWORD data = rdp.cmd1;
 
595
  
 
596
  FRDP ("uc2:moveword ");
 
597
  
 
598
  switch (index)
 
599
  {
 
600
    // NOTE: right now it's assuming that it sets the integer part first.  This could
 
601
    //  be easily fixed, but only if i had something to test with.
 
602
    
 
603
  case 0x00:  // moveword matrix
 
604
    {
 
605
      // do matrix pre-mult so it's re-updated next time
 
606
      if (rdp.update & UPDATE_MULT_MAT)
 
607
      {
 
608
        rdp.update ^= UPDATE_MULT_MAT;
 
609
        MulMatrices(rdp.model, rdp.proj, rdp.combined);
 
610
      }
 
611
      
 
612
      if (rdp.cmd0 & 0x20)  // fractional part
 
613
      {
 
614
        int index_x = (rdp.cmd0 & 0x1F) >> 1;
 
615
        int index_y = index_x >> 2;
 
616
        index_x &= 3;
 
617
        
 
618
        float fpart = (rdp.cmd1>>16)/65536.0f;
 
619
        rdp.combined[index_y][index_x] = (float)(int)rdp.combined[index_y][index_x];
 
620
        rdp.combined[index_y][index_x] += fpart;
 
621
        
 
622
        fpart = (rdp.cmd1&0xFFFF)/65536.0f;
 
623
        rdp.combined[index_y][index_x+1] = (float)(int)rdp.combined[index_y][index_x+1];
 
624
        rdp.combined[index_y][index_x+1] += fpart;
 
625
      }
 
626
      else
 
627
      {
 
628
        int index_x = (rdp.cmd0 & 0x1F) >> 1;
 
629
        int index_y = index_x >> 2;
 
630
        index_x &= 3;
 
631
        
 
632
        float fpart = (float)fabs(rdp.combined[index_y][index_x] - (int)rdp.combined[index_y][index_x]);
 
633
        rdp.combined[index_y][index_x] = (short)(rdp.cmd1>>16);
 
634
        
 
635
        fpart = (float)fabs(rdp.combined[index_y][index_x+1] - (int)rdp.combined[index_y][index_x+1]);
 
636
        rdp.combined[index_y][index_x+1] = (short)(rdp.cmd1&0xFFFF);
 
637
      }
 
638
      
 
639
      RDP ("matrix\n");
 
640
    }
 
641
    break;
 
642
    
 
643
  case 0x02:
 
644
    rdp.num_lights = data / 24;
 
645
    rdp.update |= UPDATE_LIGHTS;
 
646
    FRDP ("numlights: %d\n", rdp.num_lights);
 
647
    break;
 
648
    
 
649
  case 0x04:
 
650
    FRDP ("mw_clip %08lx, %08lx\n", rdp.cmd0, rdp.cmd1);
 
651
    break;
 
652
    
 
653
  case 0x06:  // moveword SEGMENT
 
654
    {
 
655
      FRDP ("SEGMENT %08lx -> seg%d\n", data, offset >> 2);
 
656
      if ((data&BMASK)<BMASK)
 
657
        rdp.segment[(offset >> 2) & 0xF] = data;
 
658
    }
 
659
    break;
 
660
    
 
661
    
 
662
  case 0x08:
 
663
    {
 
664
      rdp.fog_multiplier = (short)(rdp.cmd1 >> 16);
 
665
      rdp.fog_offset = (short)(rdp.cmd1 & 0x0000FFFF);
 
666
      FRDP ("fog: multiplier: %f, offset: %f\n", rdp.fog_multiplier, rdp.fog_offset);
 
667
    }
 
668
    break;
 
669
    
 
670
  case 0x0a:  // moveword LIGHTCOL
 
671
    {
 
672
      int n = offset / 24;
 
673
      FRDP ("lightcol light:%d, %08lx\n", n, data);
 
674
      
 
675
      rdp.light[n].r = (float)((data >> 24) & 0xFF) / 255.0f;
 
676
      rdp.light[n].g = (float)((data >> 16) & 0xFF) / 255.0f;
 
677
      rdp.light[n].b = (float)((data >> 8) & 0xFF) / 255.0f;
 
678
      rdp.light[n].a = 255;
 
679
    }
 
680
    break;
 
681
    
 
682
  case 0x0c:
 
683
    RDP_E ("uc2:moveword forcemtx - IGNORED\n");
 
684
    RDP ("forcemtx - IGNORED\n");
 
685
    break;
 
686
    
 
687
  case 0x0e:
 
688
    RDP ("perspnorm - IGNORED\n");
 
689
    break;
 
690
    
 
691
  default:
 
692
    FRDP_E("uc2:moveword unknown (index: 0x%08lx, offset 0x%08lx)\n", index, offset);
 
693
    FRDP ("unknown (index: 0x%08lx, offset 0x%08lx)\n", index, offset);
 
694
  }
 
695
}
 
696
 
 
697
void uc6_obj_movemem ();
 
698
 
 
699
static void uc2_movemem ()
 
700
{
 
701
  int idx = rdp.cmd0 & 0xFF;
 
702
  DWORD addr = segoffset(rdp.cmd1);
 
703
  int ofs = (rdp.cmd0 >> 5) & 0x7F8;
 
704
  
 
705
  FRDP ("uc2:movemem ofs:%d ", ofs);
 
706
  
 
707
  switch (idx)
 
708
  {
 
709
  case 0:
 
710
  case 2:
 
711
    uc6_obj_movemem ();
 
712
    break;
 
713
    
 
714
  case 8:   // VIEWPORT
 
715
    {
 
716
      DWORD a = addr >> 1;
 
717
      short scale_x = ((short*)gfx.RDRAM)[(a+0)^1] >> 2;
 
718
      short scale_y = ((short*)gfx.RDRAM)[(a+1)^1] >> 2;
 
719
      short scale_z = ((short*)gfx.RDRAM)[(a+2)^1];
 
720
      short trans_x = ((short*)gfx.RDRAM)[(a+4)^1] >> 2;
 
721
      short trans_y = ((short*)gfx.RDRAM)[(a+5)^1] >> 2;
 
722
      short trans_z = ((short*)gfx.RDRAM)[(a+6)^1];
 
723
      rdp.view_scale[0] = scale_x * rdp.scale_x;
 
724
      rdp.view_scale[1] = -scale_y * rdp.scale_y;
 
725
      rdp.view_scale[2] = 32.0f * scale_z;
 
726
      rdp.view_trans[0] = trans_x * rdp.scale_x;
 
727
      rdp.view_trans[1] = trans_y * rdp.scale_y;
 
728
      rdp.view_trans[2] = 32.0f * trans_z;
 
729
      
 
730
      rdp.update |= UPDATE_VIEWPORT;
 
731
      
 
732
      FRDP ("viewport scale(%d, %d, %d), trans(%d, %d, %d), from:%08lx\n", scale_x, scale_y, scale_z,
 
733
        trans_x, trans_y, trans_z, a);
 
734
    }
 
735
    break;
 
736
    
 
737
  case 10:  // LIGHT
 
738
    {
 
739
      int n = ofs / 24;
 
740
      
 
741
      if (n < 2)
 
742
      {
 
743
        char dir_x = ((char*)gfx.RDRAM)[(addr+8)^3];
 
744
        rdp.lookat[n][0] = (float)(dir_x) / 127.0f;
 
745
        char dir_y = ((char*)gfx.RDRAM)[(addr+9)^3];
 
746
        rdp.lookat[n][1] = (float)(dir_y) / 127.0f;
 
747
        char dir_z = ((char*)gfx.RDRAM)[(addr+10)^3];
 
748
        rdp.lookat[n][2] = (float)(dir_z) / 127.0f;
 
749
        rdp.use_lookat = TRUE;
 
750
        if (n == 1)
 
751
        {
 
752
          if (!dir_x && !dir_y)
 
753
            rdp.use_lookat = FALSE;
 
754
        }
 
755
        FRDP("lookat_%d (%f, %f, %f)\n", n, rdp.lookat[n][0], rdp.lookat[n][1], rdp.lookat[n][2]);
 
756
        return;
 
757
      }
 
758
      n -= 2;
 
759
      if (n > 7) return;
 
760
      
 
761
      // Get the data
 
762
      BYTE col = gfx.RDRAM[(addr+0)^3];
 
763
      rdp.light[n].r = (float)col / 255.0f;
 
764
      rdp.light[n].nonblack = col;
 
765
      col = gfx.RDRAM[(addr+1)^3];
 
766
      rdp.light[n].g = (float)col / 255.0f;
 
767
      rdp.light[n].nonblack += col;
 
768
      col = gfx.RDRAM[(addr+2)^3];
 
769
      rdp.light[n].b = (float)col / 255.0f;
 
770
      rdp.light[n].nonblack += col;
 
771
      rdp.light[n].a = 1.0f;
 
772
      // ** Thanks to Icepir8 for pointing this out **
 
773
      // Lighting must be signed byte instead of byte
 
774
      rdp.light[n].dir_x = (float)(((char*)gfx.RDRAM)[(addr+8)^3]) / 127.0f;
 
775
      rdp.light[n].dir_y = (float)(((char*)gfx.RDRAM)[(addr+9)^3]) / 127.0f;
 
776
      rdp.light[n].dir_z = (float)(((char*)gfx.RDRAM)[(addr+10)^3]) / 127.0f;
 
777
      DWORD a = addr >> 1;
 
778
      rdp.light[n].x = (float)(((short*)gfx.RDRAM)[(a+4)^1]);
 
779
      rdp.light[n].y = (float)(((short*)gfx.RDRAM)[(a+5)^1]);
 
780
      rdp.light[n].z = (float)(((short*)gfx.RDRAM)[(a+6)^1]);
 
781
      rdp.light[n].ca = (float)(gfx.RDRAM[(addr+3)^3]) / 16.0f;
 
782
      rdp.light[n].la = (float)(gfx.RDRAM[(addr+7)^3]);
 
783
      rdp.light[n].qa = (float)(gfx.RDRAM[(addr+14)^3]) / 8.0f;
 
784
#ifdef EXTREME_LOGGING
 
785
      FRDP ("light: n: %d, pos: x: %f, y: %f, z: %f, ca: %f, la:%f, qa: %f\n",
 
786
        n, rdp.light[n].x, rdp.light[n].y, rdp.light[n].z, rdp.light[n].ca, rdp.light[n].la, rdp.light[n].qa);
 
787
#endif
 
788
      FRDP ("light: n: %d, r: %.3f, g: %.3f, b: %.3f. dir: x: %.3f, y: %.3f, z: %.3f\n",
 
789
        n, rdp.light[n].r, rdp.light[n].g, rdp.light[n].b,
 
790
        rdp.light[n].dir_x, rdp.light[n].dir_y, rdp.light[n].dir_z);
 
791
    }
 
792
    break;
 
793
    
 
794
  case 14:  // matrix
 
795
    {
 
796
      // do not update the combined matrix!
 
797
      rdp.update &= ~UPDATE_MULT_MAT;
 
798
      
 
799
      int addr = segoffset(rdp.cmd1);
 
800
      FRDP ("matrix - addr: %08lx\n", addr);
 
801
      
 
802
      addr >>= 1;
 
803
      int x,y;
 
804
      for (x=0; x<16; x+=4) { // Adding 4 instead of one, just to remove mult. later
 
805
        for (y=0; y<4; y++) {
 
806
          rdp.combined[x>>2][y] = (float)(
 
807
            (((__int32)((WORD*)gfx.RDRAM)[(addr+x+y)^1]) << 16) |
 
808
            ((WORD*)gfx.RDRAM)[(addr+x+y+16)^1]
 
809
            ) / 65536.0f;
 
810
        }
 
811
      }
 
812
      
 
813
#ifdef EXTREME_LOGGING
 
814
      FRDP ("{%f,%f,%f,%f}\n", rdp.combined[0][0], rdp.combined[0][1], rdp.combined[0][2], rdp.combined[0][3]);
 
815
      FRDP ("{%f,%f,%f,%f}\n", rdp.combined[1][0], rdp.combined[1][1], rdp.combined[1][2], rdp.combined[1][3]);
 
816
      FRDP ("{%f,%f,%f,%f}\n", rdp.combined[2][0], rdp.combined[2][1], rdp.combined[2][2], rdp.combined[2][3]);
 
817
      FRDP ("{%f,%f,%f,%f}\n", rdp.combined[3][0], rdp.combined[3][1], rdp.combined[3][2], rdp.combined[3][3]);
 
818
#endif
 
819
    }
 
820
    break;
 
821
    
 
822
  default:
 
823
    FRDP ("uc2:matrix unknown (%d)\n", idx);
 
824
    FRDP ("** UNKNOWN %d\n", idx);
 
825
  }
 
826
}
 
827
 
 
828
static void uc2_load_ucode ()
 
829
{
 
830
  RDP ("uc2:load_ucode\n");
 
831
}
 
832
 
 
833
static void uc2_rdphalf_2 ()
 
834
{
 
835
  RDP ("uc2:rdphalf_2\n");
 
836
}
 
837
 
 
838
static void uc2_dlist_cnt ()
 
839
{
 
840
  DWORD addr = segoffset(rdp.cmd1) & BMASK;
 
841
  int count = rdp.cmd0 & 0x000000FF;
 
842
  FRDP ("dl_count - addr: %08lx, count: %d\n", addr, count);
 
843
  if (addr == 0)
 
844
    return;
 
845
  
 
846
  if (rdp.pc_i >= 9) {
 
847
    RDP_E ("** DL stack overflow **\n");
 
848
    RDP ("** DL stack overflow **\n");
 
849
    return;
 
850
  }
 
851
  rdp.pc_i ++;  // go to the next PC in the stack
 
852
  rdp.pc[rdp.pc_i] = addr;  // jump to the address
 
853
  rdp.dl_count = count + 1;
 
854
}
 
855