Debian Codegraph @

mednafen/vb/vip.cpp

raw symbols SCIP
1 /* Mednafen - Multi-system Emulator
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 */
17
18 #include "vb.h"
19 #include "vip.h"
20 #include <math.h>
21
22 static inline void VIP_DBGMSG(const char *format, ...)
23 {
24 }
25
26 static uint8 FB[2][2][0x6000];
27 static uint16 CHR_RAM[0x8000 / sizeof(uint16)];
28 static uint16 DRAM[0x20000 / sizeof(uint16)];
29
30 // Helper functions for the V810 VIP RAM read/write handlers.
31 // "Memory Array 16 (Write/Read) (16/8)"
32 #define VIP__GETP16(array, address) ( (uint16 *)&((uint8 *)(array))[(address)] )
33
34 #ifdef MSB_FIRST
35 #define VIP__GETP8(array, address) ( &((uint8 *)(array))[(address) ^ 1] )
36 #else
37 #define VIP__GETP8(array, address) ( &((uint8 *)(array))[(address)] )
38 #endif
39
40 static INLINE void VIP_MA16W16(uint16 *array, const uint32 v810_address, const uint16 value)
41 {
42 *(VIP__GETP16(array, v810_address)) = value;
43 }
44
45 static INLINE uint16 VIP_MA16R16(uint16 *array, const uint32 v810_address)
46 {
47 return *(VIP__GETP16(array, v810_address));
48 }
49
50 static INLINE void VIP_MA16W8(uint16 *array, const uint32 v810_address, const uint8 value)
51 {
52 *(VIP__GETP8(array, v810_address)) = value;
53 }
54
55 static INLINE uint8 VIP_MA16R8(uint16 *array, const uint32 v810_address)
56 {
57 return *(VIP__GETP8(array, v810_address));
58 }
59
60
61 #define INT_SCAN_ERR 0x0001
62 #define INT_LFB_END 0x0002
63 #define INT_RFB_END 0x0004
64 #define INT_GAME_START 0x0008
65 #define INT_FRAME_START 0x0010
66
67 #define INT_SB_HIT 0x2000
68 #define INT_XP_END 0x4000
69 #define INT_TIME_ERR 0x8000
70
71 static uint16 InterruptPending;
72 static uint16 InterruptEnable;
73
74 static uint8 BRTA, BRTB, BRTC, REST;
75 static uint8 Repeat;
76
77 static void CopyFBColumnToTarget_Anaglyph(void) NO_INLINE;
78 static void CopyFBColumnToTarget_AnaglyphSlow(void) NO_INLINE;
79 static void CopyFBColumnToTarget_CScope(void) NO_INLINE;
80 static void CopyFBColumnToTarget_SideBySide(void) NO_INLINE;
81 static void CopyFBColumnToTarget_VLI(void) NO_INLINE;
82 static void CopyFBColumnToTarget_HLI(void) NO_INLINE;
83 static void (*CopyFBColumnToTarget)(void) = NULL;
84 static uint32 VB3DMode;
85 static uint32 VB3DReverse;
86 static uint32 VBPrescale;
87 static uint32 VBSBS_Separation;
88 static uint32 HLILUT[256];
89 static uint32 ColorLUT[2][256];
90 static int32 BrightnessCache[4];
91 static uint32 BrightCLUT[2][4];
92
93 static double ColorLUTNoGC[2][256][3];
94 static uint32 AnaSlowColorLUT[256][256];
95
96 // A few settings:
97 static bool InstantDisplayHack;
98 static bool AllowDrawSkip;
99
100 static bool VidSettingsDirty;
101 static bool ParallaxDisabled;
102 static uint32 Anaglyph_Colors[2];
103 static uint32 Default_Color;
104
105 static void MakeColorLUT(const MDFN_PixelFormat &format)
106 {
107 unsigned lr, i, l_b, r_b;
108
109 for(lr = 0; lr < 2; lr++)
110 {
111 for(i = 0; i < 256; i++)
112 {
113 double r, g, b;
114 double r_prime, g_prime, b_prime;
115 r = g = b = (double)i / 255;
116
117 // TODO: Use correct gamma curve, instead of approximation.
118 r_prime = pow(r, 1.0 / 2.2);
119 g_prime = pow(g, 1.0 / 2.2);
120 b_prime = pow(b, 1.0 / 2.2);
121
122 switch(VB3DMode)
123 {
124 case VB3DMODE_ANAGLYPH:
125 r_prime = r_prime * ((Anaglyph_Colors[lr ^ VB3DReverse] >> 16) & 0xFF) / 255;
126 g_prime = g_prime * ((Anaglyph_Colors[lr ^ VB3DReverse] >> 8) & 0xFF) / 255;
127 b_prime = b_prime * ((Anaglyph_Colors[lr ^ VB3DReverse] >> 0) & 0xFF) / 255;
128 break;
129 default:
130 r_prime = r_prime * ((Default_Color >> 16) & 0xFF) / 255;
131 g_prime = g_prime * ((Default_Color >> 8) & 0xFF) / 255;
132 b_prime = b_prime * ((Default_Color >> 0) & 0xFF) / 255;
133 break;
134 }
135 ColorLUTNoGC[lr][i][0] = pow(r_prime, 2.2 / 1.0);
136 ColorLUTNoGC[lr][i][1] = pow(g_prime, 2.2 / 1.0);
137 ColorLUTNoGC[lr][i][2] = pow(b_prime, 2.2 / 1.0);
138
139 ColorLUT[lr][i] = MAKECOLOR((int)(r_prime * 255), (int)(g_prime * 255), (int)(b_prime * 255), 0);
140 }
141 }
142
143 // Anaglyph slow-mode LUT calculation
144 for(l_b = 0; l_b < 256; l_b++)
145 {
146 for(r_b = 0; r_b < 256; r_b++)
147 {
148 double r_prime, g_prime, b_prime;
149 double r = ColorLUTNoGC[0][l_b][0] + ColorLUTNoGC[1][r_b][0];
150 double g = ColorLUTNoGC[0][l_b][1] + ColorLUTNoGC[1][r_b][1];
151 double b = ColorLUTNoGC[0][l_b][2] + ColorLUTNoGC[1][r_b][2];
152
153 if(r > 1.0)
154 r = 1.0;
155 if(g > 1.0)
156 g = 1.0;
157 if(b > 1.0)
158 b = 1.0;
159
160 r_prime = pow(r, 1.0 / 2.2);
161 g_prime = pow(g, 1.0 / 2.2);
162 b_prime = pow(b, 1.0 / 2.2);
163
164 AnaSlowColorLUT[l_b][r_b] = MAKECOLOR(((int)(r_prime * 255)), ((int)(g_prime * 255)), ((int)(b_prime * 255)), 0);
165 }
166 }
167 }
168
169 static void RecalcBrightnessCache(void)
170 {
171 unsigned i, lr;
172 //printf("BRTA: %d, BRTB: %d, BRTC: %d, Rest: %d\n", BRTA, BRTB, BRTC, REST);
173 int32 CumulativeTime = (BRTA + 1 + BRTB + 1 + BRTC + 1 + REST + 1) + 1;
174 int32 MaxTime = 128;
175
176 BrightnessCache[0] = 0;
177 BrightnessCache[1] = 0;
178 BrightnessCache[2] = 0;
179 BrightnessCache[3] = 0;
180
181 for(i = 0; i < Repeat + 1; i++)
182 {
183 int32 btemp[4];
184
185 if((i * CumulativeTime) >= MaxTime)
186 break;
187
188 btemp[1] = (i * CumulativeTime) + BRTA;
189 if(btemp[1] > MaxTime)
190 btemp[1] = MaxTime;
191 btemp[1] -= (i * CumulativeTime);
192 if(btemp[1] < 0)
193 btemp[1] = 0;
194
195
196 btemp[2] = (i * CumulativeTime) + BRTA + 1 + BRTB;
197 if(btemp[2] > MaxTime)
198 btemp[2] = MaxTime;
199 btemp[2] -= (i * CumulativeTime) + BRTA + 1;
200 if(btemp[2] < 0)
201 btemp[2] = 0;
202
203 //btemp[3] = (i * CumulativeTime) + BRTA + 1 + BRTB + 1 + BRTC;
204 //if(btemp[3] > MaxTime)
205 // btemp[3] = MaxTime;
206 //btemp[3] -= (i * CumulativeTime);
207 //if(btemp[3] < 0)
208 // btemp[3] = 0;
209
210 btemp[3] = (i * CumulativeTime) + BRTA + BRTB + BRTC + 1;
211 if(btemp[3] > MaxTime)
212 btemp[3] = MaxTime;
213 btemp[3] -= (i * CumulativeTime) + 1;
214 if(btemp[3] < 0)
215 btemp[3] = 0;
216
217 BrightnessCache[1] += btemp[1];
218 BrightnessCache[2] += btemp[2];
219 BrightnessCache[3] += btemp[3];
220 }
221
222 //printf("BC: %d %d %d %d\n", BrightnessCache[0], BrightnessCache[1], BrightnessCache[2], BrightnessCache[3]);
223
224 for(i = 0; i < 4; i++)
225 BrightnessCache[i] = 255 * BrightnessCache[i] / MaxTime;
226
227 for(lr = 0; lr < 2; lr++)
228 for(i = 0; i < 4; i++)
229 BrightCLUT[lr][i] = ColorLUT[lr][BrightnessCache[i]];
230 }
231
232 static void Recalc3DModeStuff(bool non_rgb_output = false)
233 {
234 switch(VB3DMode)
235 {
236 default:
237 CopyFBColumnToTarget = CopyFBColumnToTarget_Anaglyph;
238 if(((Anaglyph_Colors[0] & 0xFF) && (Anaglyph_Colors[1] & 0xFF)) ||
239 ((Anaglyph_Colors[0] & 0xFF00) && (Anaglyph_Colors[1] & 0xFF00)) ||
240 ((Anaglyph_Colors[0] & 0xFF0000) && (Anaglyph_Colors[1] & 0xFF0000)) ||
241 non_rgb_output)
242 CopyFBColumnToTarget = CopyFBColumnToTarget_AnaglyphSlow;
243 break;
244
245 case VB3DMODE_CSCOPE:
246 CopyFBColumnToTarget = CopyFBColumnToTarget_CScope;
247 break;
248
249 case VB3DMODE_SIDEBYSIDE:
250 CopyFBColumnToTarget = CopyFBColumnToTarget_SideBySide;
251 break;
252
253 case VB3DMODE_VLI:
254 CopyFBColumnToTarget = CopyFBColumnToTarget_VLI;
255 break;
256
257 case VB3DMODE_HLI:
258 CopyFBColumnToTarget = CopyFBColumnToTarget_HLI;
259 break;
260 }
261 RecalcBrightnessCache();
262 }
263
264 void VIP_Set3DMode(uint32 mode, bool reverse, uint32 prescale, uint32 sbs_separation)
265 {
266 uint32_t p;
267
268 VB3DMode = mode;
269 VB3DReverse = reverse ? 1 : 0;
270 VBPrescale = prescale;
271 VBSBS_Separation = sbs_separation;
272
273 VidSettingsDirty = true;
274
275 for(p = 0; p < 256; p++)
276 {
277 unsigned i, ps, shifty;
278 uint8 s[4];
279 uint32 v = 0;
280
281 s[0] = (p >> 0) & 0x3;
282 s[1] = (p >> 2) & 0x3;
283 s[2] = (p >> 4) & 0x3;
284 s[3] = (p >> 6) & 0x3;
285
286 for(i = 0, shifty = 0; i < 4; i++)
287 {
288 for(ps = 0; ps < prescale; ps++)
289 {
290 v |= s[i] << shifty;
291 shifty += 2;
292 }
293 }
294
295 HLILUT[p] = v;
296 }
297 }
298
299 void VIP_SetParallaxDisable(bool disabled)
300 {
301 ParallaxDisabled = disabled;
302 }
303
304 void VIP_SetDefaultColor(uint32 default_color)
305 {
306 Default_Color = default_color;
307
308 VidSettingsDirty = true;
309 }
310
311
312 void VIP_SetAnaglyphColors(uint32 lcolor, uint32 rcolor)
313 {
314 Anaglyph_Colors[0] = lcolor;
315 Anaglyph_Colors[1] = rcolor;
316
317 VidSettingsDirty = true;
318 }
319
320 void VIP_SetInstantDisplayHack(bool val)
321 {
322 InstantDisplayHack = val;
323 }
324
325 void VIP_SetAllowDrawSkip(bool val)
326 {
327 AllowDrawSkip = val;
328 }
329
330
331 static uint16 FRMCYC;
332
333 static uint16 DPCTRL;
334 static bool DisplayActive;
335
336 #define XPCTRL_XP_RST 0x0001
337 #define XPCTRL_XP_EN 0x0002
338 static uint16 XPCTRL;
339 static uint16 SBCMP; // Derived from XPCTRL
340
341 static uint16 SPT[4]; // SPT0~SPT3, 5f848~5f84e
342 static uint16 GPLT[4];
343 static uint8 GPLT_Cache[4][4];
344
345 static INLINE void Recalc_GPLT_Cache(int which)
346 {
347 unsigned i;
348 for(i = 0; i < 4; i++)
349 GPLT_Cache[which][i] = (GPLT[which] >> (i * 2)) & 3;
350 }
351
352 static uint16 JPLT[4];
353 static uint8 JPLT_Cache[4][4];
354
355 static INLINE void Recalc_JPLT_Cache(int which)
356 {
357 unsigned i;
358 for(i = 0; i < 4; i++)
359 JPLT_Cache[which][i] = (JPLT[which] >> (i * 2)) & 3;
360 }
361
362
363 static uint16 BKCOL;
364
365 static int32 CalcNextEvent(void);
366
367 static int32 last_ts;
368
369 static int32 Column;
370 static int32 ColumnCounter;
371
372 static int32 DisplayRegion;
373 static bool DisplayFB;
374
375 static int32 GameFrameCounter;
376
377 static int32 DrawingCounter;
378 static bool DrawingActive;
379 static bool DrawingFB;
380 static uint32 DrawingBlock;
381 static int32 SB_Latch;
382 static int32 SBOUT_InactiveTime;
383
384 //static uint8 CTA_L, CTA_R;
385
386 static void CheckIRQ(void)
387 {
388 VBIRQ_Assert(VBIRQ_SOURCE_VIP, (bool)(InterruptEnable & InterruptPending));
389
390 #if 0
391 printf("%08x\n", InterruptEnable & InterruptPending);
392 if((bool)(InterruptEnable & InterruptPending))
393 puts("IRQ asserted");
394 else
395 puts("IRQ not asserted");
396 #endif
397 }
398
399
400 bool VIP_Init(void)
401 {
402 InstantDisplayHack = false;
403 AllowDrawSkip = false;
404 ParallaxDisabled = false;
405 Anaglyph_Colors[0] = 0xFF0000;
406 Anaglyph_Colors[1] = 0x0000FF;
407 VB3DMode = VB3DMODE_ANAGLYPH;
408 Default_Color = 0xFFFFFF;
409 VB3DReverse = 0;
410 VBPrescale = 1;
411 VBSBS_Separation = 0;
412
413 VidSettingsDirty = true;
414
415 return(true);
416 }
417
418 void VIP_Power(void)
419 {
420 unsigned i;
421
422 Repeat = 0;
423 SB_Latch = 0;
424 SBOUT_InactiveTime = -1;
425 last_ts = 0;
426
427 Column = 0;
428 ColumnCounter = 259;
429
430 DisplayRegion = 0;
431 DisplayFB = 0;
432
433 GameFrameCounter = 0;
434
435 DrawingCounter = 0;
436 DrawingActive = false;
437 DrawingFB = 0;
438 DrawingBlock = 0;
439
440 DPCTRL = 2;
441 DisplayActive = false;
442
443
444
445 memset(FB, 0, 0x6000 * 2 * 2);
446 memset(CHR_RAM, 0, 0x8000);
447 memset(DRAM, 0, 0x20000);
448
449 InterruptPending = 0;
450 InterruptEnable = 0;
451
452 BRTA = 0;
453 BRTB = 0;
454 BRTC = 0;
455 REST = 0;
456
457 FRMCYC = 0;
458
459 XPCTRL = 0;
460 SBCMP = 0;
461
462 for(i = 0; i < 4; i++)
463 {
464 SPT[i] = 0;
465 GPLT[i] = 0;
466 JPLT[i] = 0;
467
468 Recalc_GPLT_Cache(i);
469 Recalc_JPLT_Cache(i);
470 }
471
472 BKCOL = 0;
473 }
474
475 static INLINE uint16 ReadRegister(int32 &timestamp, uint32 A)
476 {
477 uint16_t ret = 0; //0xFFFF;
478
479 if(A & 1)
480 VIP_DBGMSG("Misaligned VIP Read: %08x", A);
481
482 switch(A & 0xFE)
483 {
484 default:
485 VIP_DBGMSG("Unknown VIP register read: %08x", A);
486 break;
487
488 case 0x00:
489 ret = InterruptPending;
490 break;
491
492 case 0x02:
493 ret = InterruptEnable;
494 break;
495
496 case 0x20:
497 //printf("Read DPSTTS at %d\n", timestamp);
498 ret = DPCTRL & 0x702;
499 if((DisplayRegion & 1) && DisplayActive)
500 {
501 unsigned int DPBSY = 1 << ((DisplayRegion >> 1) & 1);
502
503 if(DisplayFB)
504 DPBSY <<= 2;
505
506 ret |= DPBSY << 2;
507 }
508 //if(!(DisplayRegion & 1)) // FIXME? (Had to do it this way for Galactic Pinball...)
509 ret |= 1 << 6;
510 break;
511
512 // Note: Upper bits of BRTA, BRTB, BRTC, and REST(?) are 0 when read(on real hardware)
513 case 0x24:
514 ret = BRTA;
515 break;
516
517 case 0x26:
518 ret = BRTB;
519 break;
520
521 case 0x28:
522 ret = BRTC;
523 break;
524
525 case 0x2A:
526 ret = REST;
527 break;
528
529 case 0x30:
530 ret = 0xFFFF;
531 break;
532
533 case 0x40:
534 ret = XPCTRL & 0x2;
535 if(DrawingActive)
536 {
537 ret |= (1 + DrawingFB) << 2;
538 }
539 if(timestamp < SBOUT_InactiveTime)
540 {
541 ret |= 0x8000;
542 ret |= /*DrawingBlock*/SB_Latch << 8;
543 }
544 break; // XPSTTS, read-only
545
546 case 0x44:
547 ret = 2; // VIP version. 2 is a known valid version, while the validity of other numbers is unknown, so we'll just go with 2.
548 break;
549
550 case 0x48:
551 case 0x4a:
552 case 0x4c:
553 case 0x4e:
554 ret = SPT[(A >> 1) & 3];
555 break;
556
557 case 0x60:
558 case 0x62:
559 case 0x64:
560 case 0x66:
561 ret = GPLT[(A >> 1) & 3];
562 break;
563
564 case 0x68:
565 case 0x6a:
566 case 0x6c:
567 case 0x6e:
568 ret = JPLT[(A >> 1) & 3];
569 break;
570
571 case 0x70:
572 ret = BKCOL;
573 break;
574 }
575
576 return(ret);
577 }
578
579 static INLINE void WriteRegister(int32 &timestamp, uint32 A, uint16 V)
580 {
581 if(A & 1)
582 VIP_DBGMSG("Misaligned VIP Write: %08x %04x", A, V);
583
584 switch(A & 0xFE)
585 {
586 default:
587 VIP_DBGMSG("Unknown VIP register write: %08x %04x", A, V);
588 break;
589 case 0x00:
590 break; // Interrupt pending, read-only
591 case 0x02:
592 InterruptEnable = V & 0xE01F;
593
594 VIP_DBGMSG("Interrupt Enable: %04x", V);
595
596 if(V & 0x2000)
597 VIP_DBGMSG("Warning: VIP SB Hit Interrupt enable: %04x\n", V);
598 CheckIRQ();
599 break;
600 case 0x04:
601 InterruptPending &= ~V;
602 CheckIRQ();
603 break;
604
605 case 0x20:
606 break; // Display control, read-only.
607
608 case 0x22:
609 DPCTRL = V & (0x703); // Display-control, write-only
610 if(V & 1)
611 {
612 DisplayActive = false;
613 InterruptPending &= ~(INT_TIME_ERR | INT_FRAME_START | INT_GAME_START | INT_RFB_END | INT_LFB_END | INT_SCAN_ERR);
614 CheckIRQ();
615 }
616 break;
617
618 case 0x24:
619 BRTA = V & 0xFF; // BRTA
620 RecalcBrightnessCache();
621 break;
622
623 case 0x26:
624 BRTB = V & 0xFF; // BRTB
625 RecalcBrightnessCache();
626 break;
627
628 case 0x28:
629 BRTC = V & 0xFF; // BRTC
630 RecalcBrightnessCache();
631 break;
632
633 case 0x2A:
634 REST = V & 0xFF; // REST
635 RecalcBrightnessCache();
636 break;
637
638 case 0x2E:
639 FRMCYC = V & 0xF; // FRMCYC, write-only?
640 break;
641
642 case 0x30:
643 break; // CTA, read-only(
644
645 case 0x40:
646 break; // XPSTTS, read-only
647
648 case 0x42:
649 XPCTRL = V & 0x0002; // XPCTRL, write-only
650 SBCMP = (V >> 8) & 0x1F;
651
652 if(V & 1)
653 {
654 VIP_DBGMSG("XPRST");
655 DrawingActive = 0;
656 DrawingCounter = 0;
657 InterruptPending &= ~(INT_SB_HIT | INT_XP_END | INT_TIME_ERR);
658 CheckIRQ();
659 }
660 break;
661
662 case 0x44:
663 break; // Version Control, read-only?
664
665 case 0x48:
666 case 0x4a:
667 case 0x4c:
668 case 0x4e:
669 SPT[(A >> 1) & 3] = V & 0x3FF;
670 break;
671
672 case 0x60:
673 case 0x62:
674 case 0x64:
675 case 0x66:
676 GPLT[(A >> 1) & 3] = V & 0xFC;
677 Recalc_GPLT_Cache((A >> 1) & 3);
678 break;
679
680 case 0x68:
681 case 0x6a:
682 case 0x6c:
683 case 0x6e:
684 JPLT[(A >> 1) & 3] = V & 0xFC;
685 Recalc_JPLT_Cache((A >> 1) & 3);
686 break;
687
688 case 0x70:
689 BKCOL = V & 0x3;
690 break;
691 }
692 }
693
694 /* Don't update the VIP state on reads/writes,
695 * the event system will update it with enough precision
696 * as far as VB software cares.
697 */
698
699 uint8 VIP_Read8(int32 &timestamp, uint32 A)
700 {
701 uint8_t ret = 0; //0xFF;
702
703 //VIP_Update(timestamp);
704
705 switch(A >> 16)
706 {
707 case 0x0:
708 case 0x1:
709 if((A & 0x7FFF) >= 0x6000)
710 return VIP_MA16R8(CHR_RAM, (A & 0x1FFF) | ((A >> 2) & 0x6000));
711 return FB[(A >> 15) & 1][(A >> 16) & 1][A & 0x7FFF];
712 case 0x2:
713 case 0x3:
714 return VIP_MA16R8(DRAM, A & 0x1FFFF);
715 case 0x4:
716 case 0x5:
717 if(A >= 0x5E000)
718 return ReadRegister(timestamp, A);
719 break;
720 case 0x6:
721 break;
722
723 case 0x7:
724 if(A >= 0x8000)
725 return VIP_MA16R8(CHR_RAM, A & 0x7FFF);
726 break;
727 default:
728 break;
729 }
730
731 //VB_SetEvent(VB_EVENT_VIP, timestamp + CalcNextEvent());
732 VIP_DBGMSG("Unknown VIP Read: %08x", A);
733
734 return 0;
735 }
736
737 uint16 VIP_Read16(int32 &timestamp, uint32 A)
738 {
739 //VIP_Update(timestamp);
740
741 switch(A >> 16)
742 {
743 case 0x0:
744 case 0x1:
745 if((A & 0x7FFF) >= 0x6000)
746 return VIP_MA16R16(CHR_RAM, (A & 0x1FFF) | ((A >> 2) & 0x6000));
747 return LoadU16_LE((uint16 *)&FB[(A >> 15) & 1][(A >> 16) & 1][A & 0x7FFF]);
748 case 0x2:
749 case 0x3:
750 return VIP_MA16R16(DRAM, A & 0x1FFFF);
751 case 0x4:
752 case 0x5:
753 if(A >= 0x5E000)
754 return ReadRegister(timestamp, A);
755 break;
756 case 0x6:
757 break;
758 case 0x7:
759 if(A >= 0x8000)
760 return VIP_MA16R16(CHR_RAM, A & 0x7FFF);
761 break;
762 default:
763 break;
764 }
765
766 VIP_DBGMSG("Unknown VIP Read: %08x", A);
767
768 //VB_SetEvent(VB_EVENT_VIP, timestamp + CalcNextEvent());
769 return 0;
770 }
771
772 void VIP_Write8(int32 &timestamp, uint32 A, uint8 V)
773 {
774 //VIP_Update(timestamp);
775
776 //if(A >= 0x3DC00 && A < 0x3E000)
777 // printf("%08x %02x\n", A, V);
778
779 switch(A >> 16)
780 {
781 case 0x0:
782 case 0x1:
783 if((A & 0x7FFF) >= 0x6000)
784 VIP_MA16W8(CHR_RAM, (A & 0x1FFF) | ((A >> 2) & 0x6000), V);
785 else
786 FB[(A >> 15) & 1][(A >> 16) & 1][A & 0x7FFF] = V;
787 break;
788
789 case 0x2:
790 case 0x3:
791 VIP_MA16W8(DRAM, A & 0x1FFFF, V);
792 break;
793
794 case 0x4:
795 case 0x5:
796 if(A >= 0x5E000)
797 WriteRegister(timestamp, A, V);
798 else
799 VIP_DBGMSG("Unknown VIP Write: %08x %02x", A, V);
800 break;
801
802 case 0x6:
803 VIP_DBGMSG("Unknown VIP Write: %08x %02x", A, V);
804 break;
805
806 case 0x7:
807 if(A >= 0x8000)
808 VIP_MA16W8(CHR_RAM, A & 0x7FFF, V);
809 else
810 VIP_DBGMSG("Unknown VIP Write: %08x %02x", A, V);
811 break;
812
813 default:
814 VIP_DBGMSG("Unknown VIP Write: %08x %02x", A, V);
815 break;
816 }
817
818 //VB_SetEvent(VB_EVENT_VIP, timestamp + CalcNextEvent());
819 }
820
821 void VIP_Write16(int32 &timestamp, uint32 A, uint16 V)
822 {
823 //VIP_Update(timestamp);
824
825 //if(A >= 0x3DC00 && A < 0x3E000)
826 // printf("%08x %04x\n", A, V);
827
828 switch(A >> 16)
829 {
830 case 0x0:
831 case 0x1:
832 if((A & 0x7FFF) >= 0x6000)
833 VIP_MA16W16(CHR_RAM, (A & 0x1FFF) | ((A >> 2) & 0x6000), V);
834 else
835 StoreU16_LE((uint16 *)&FB[(A >> 15) & 1][(A >> 16) & 1][A & 0x7FFF], V);
836 break;
837
838 case 0x2:
839 case 0x3:
840 VIP_MA16W16(DRAM, A & 0x1FFFF, V);
841 break;
842 case 0x4:
843 case 0x5:
844 if(A >= 0x5E000)
845 WriteRegister(timestamp, A, V);
846 else
847 VIP_DBGMSG("Unknown VIP Write: %08x %04x", A, V);
848 break;
849 case 0x6:
850 VIP_DBGMSG("Unknown VIP Write: %08x %04x", A, V);
851 break;
852 case 0x7:
853 if(A >= 0x8000)
854 VIP_MA16W16(CHR_RAM, A & 0x7FFF, V);
855 else
856 VIP_DBGMSG("Unknown VIP Write: %08x %04x", A, V);
857 break;
858 default:
859 VIP_DBGMSG("Unknown VIP Write: %08x %04x", A, V);
860 break;
861 }
862
863
864 //VB_SetEvent(VB_EVENT_VIP, timestamp + CalcNextEvent());
865 }
866
867 static MDFN_Surface *surface;
868 static bool skip;
869
870 void VIP_StartFrame(EmulateSpecStruct *espec)
871 {
872 if(espec->VideoFormatChanged || VidSettingsDirty)
873 {
874 MakeColorLUT(espec->surface->format);
875 Recalc3DModeStuff(espec->surface->format.colorspace != MDFN_COLORSPACE_RGB);
876
877 VidSettingsDirty = false;
878 }
879
880 espec->DisplayRect.x = 0;
881 espec->DisplayRect.y = 0;
882
883 switch(VB3DMode)
884 {
885 default:
886 espec->DisplayRect.w = 384;
887 espec->DisplayRect.h = 224;
888 break;
889
890 case VB3DMODE_VLI:
891 espec->DisplayRect.w = 768 * VBPrescale;
892 espec->DisplayRect.h = 224;
893 break;
894
895 case VB3DMODE_HLI:
896 espec->DisplayRect.w = 384;
897 espec->DisplayRect.h = 448 * VBPrescale;
898 break;
899
900 case VB3DMODE_CSCOPE:
901 espec->DisplayRect.w = 512;
902 espec->DisplayRect.h = 384;
903 break;
904
905 case VB3DMODE_SIDEBYSIDE:
906 espec->DisplayRect.w = 768 + VBSBS_Separation;
907 espec->DisplayRect.h = 224;
908 break;
909 }
910
911 surface = espec->surface;
912 skip = espec->skip;
913 }
914
915 void VIP_ResetTS(void)
916 {
917 if(SBOUT_InactiveTime >= 0)
918 SBOUT_InactiveTime -= last_ts;
919 last_ts = 0;
920 }
921
922 static int32 CalcNextEvent(void)
923 {
924 return(ColumnCounter);
925 }
926
927 #include "vip_draw.inc"
928
929 static INLINE void CopyFBColumnToTarget_Anaglyph_BASE(const bool DisplayActive_arg, const int lr)
930 {
931 int y, y_sub;
932 const int fb = DisplayFB;
933
934 #if defined(WANT_8BPP)
935 uint8 *target = surface->pixels8 + Column;
936 #elif defined(WANT_16BPP)
937 uint16 *target = surface->pixels16 + Column;
938 #else
939 uint32 *target = surface->pixels + Column;
940 #endif
941 const int32 pitchinpix = surface->pitchinpix;
942 const uint8 *fb_source = &FB[fb][lr][64 * Column];
943
944 for(y = 56; y; y--)
945 {
946 uint32 source_bits = *fb_source;
947
948 for(y_sub = 4; y_sub; y_sub--)
949 {
950 uint32 pixel = BrightCLUT[lr][source_bits & 3];
951
952 if(!DisplayActive_arg)
953 pixel = 0;
954
955 if(lr)
956 *target |= pixel;
957 else
958 *target = pixel;
959
960 source_bits >>= 2;
961 target += pitchinpix;
962 }
963 fb_source++;
964 }
965 }
966
967 static void CopyFBColumnToTarget_Anaglyph(void)
968 {
969 const int lr = (DisplayRegion & 2) >> 1;
970
971 if(!DisplayActive)
972 {
973 if(!lr)
974 CopyFBColumnToTarget_Anaglyph_BASE(0, 0);
975 else
976 CopyFBColumnToTarget_Anaglyph_BASE(0, 1);
977 }
978 else
979 {
980 if(!lr)
981 CopyFBColumnToTarget_Anaglyph_BASE(1, 0);
982 else
983 CopyFBColumnToTarget_Anaglyph_BASE(1, 1);
984 }
985 }
986
987 static uint32 AnaSlowBuf[384][224];
988
989 static INLINE void CopyFBColumnToTarget_AnaglyphSlow_BASE(const bool DisplayActive_arg, const int lr)
990 {
991 const int fb = DisplayFB;
992 const uint8 *fb_source = &FB[fb][lr][64 * Column];
993
994 if(!lr)
995 {
996 uint32 *target = AnaSlowBuf[Column];
997
998 for(int y = 56; y; y--)
999 {
1000 uint32 source_bits = *fb_source;
1001
1002 for(int y_sub = 4; y_sub; y_sub--)
1003 {
1004 uint32 pixel = BrightnessCache[source_bits & 3];
1005
1006 if(!DisplayActive_arg)
1007 pixel = 0;
1008
1009 *target = pixel;
1010 source_bits >>= 2;
1011 target++;
1012 }
1013 fb_source++;
1014 }
1015
1016 }
1017 else
1018 {
1019 uint32 *target = surface->pixels + Column;
1020 const uint32 *left_src = AnaSlowBuf[Column];
1021 const int32 pitch32 = surface->pitch32;
1022
1023 for(int y = 56; y; y--)
1024 {
1025 uint32 source_bits = *fb_source;
1026
1027 for(int y_sub = 4; y_sub; y_sub--)
1028 {
1029 uint32 pixel = AnaSlowColorLUT[*left_src][DisplayActive_arg ? BrightnessCache[source_bits & 3] : 0];
1030
1031 *target = pixel;
1032
1033 source_bits >>= 2;
1034 target += pitch32;
1035 left_src++;
1036 }
1037 fb_source++;
1038 }
1039 }
1040 }
1041
1042 static void CopyFBColumnToTarget_AnaglyphSlow(void)
1043 {
1044 const int lr = (DisplayRegion & 2) >> 1;
1045
1046 if(!DisplayActive)
1047 {
1048 if(!lr)
1049 CopyFBColumnToTarget_AnaglyphSlow_BASE(0, 0);
1050 else
1051 CopyFBColumnToTarget_AnaglyphSlow_BASE(0, 1);
1052 }
1053 else
1054 {
1055 if(!lr)
1056 CopyFBColumnToTarget_AnaglyphSlow_BASE(1, 0);
1057 else
1058 CopyFBColumnToTarget_AnaglyphSlow_BASE(1, 1);
1059 }
1060 }
1061
1062
1063 static void CopyFBColumnToTarget_CScope_BASE(const bool DisplayActive_arg, const int lr, const int dest_lr)
1064 {
1065 int y, y_sub;
1066 const int fb = DisplayFB;
1067 uint32 *target = surface->pixels + (dest_lr ? 512 - 16 - 1 : 16) + (dest_lr ? Column : 383 - Column) * surface->pitch32;
1068 const uint8 *fb_source = &FB[fb][lr][64 * Column];
1069
1070 for(y = 56; y; y--)
1071 {
1072 uint32 source_bits = *fb_source;
1073
1074 for(y_sub = 4; y_sub; y_sub--)
1075 {
1076 if(DisplayActive_arg)
1077 *target = BrightCLUT[lr][source_bits & 3];
1078 else
1079 *target = 0;
1080
1081 source_bits >>= 2;
1082 if(dest_lr)
1083 target--;
1084 else
1085 target++;
1086 }
1087 fb_source++;
1088 }
1089 }
1090
1091 static void CopyFBColumnToTarget_CScope(void)
1092 {
1093 const int lr = (DisplayRegion & 2) >> 1;
1094
1095 if(!DisplayActive)
1096 {
1097 if(!lr)
1098 CopyFBColumnToTarget_CScope_BASE(0, 0, 0 ^ VB3DReverse);
1099 else
1100 CopyFBColumnToTarget_CScope_BASE(0, 1, 1 ^ VB3DReverse);
1101 }
1102 else
1103 {
1104 if(!lr)
1105 CopyFBColumnToTarget_CScope_BASE(1, 0, 0 ^ VB3DReverse);
1106 else
1107 CopyFBColumnToTarget_CScope_BASE(1, 1, 1 ^ VB3DReverse);
1108 }
1109 }
1110
1111 static void CopyFBColumnToTarget_SideBySide_BASE(const bool DisplayActive_arg, const int lr, const int dest_lr)
1112 {
1113 const int fb = DisplayFB;
1114 uint32 *target = surface->pixels + Column + (dest_lr ? (384 + VBSBS_Separation) : 0);
1115 const int32 pitch32 = surface->pitch32;
1116 const uint8 *fb_source = &FB[fb][lr][64 * Column];
1117
1118 for(int y = 56; y; y--)
1119 {
1120 uint32 source_bits = *fb_source;
1121
1122 for(int y_sub = 4; y_sub; y_sub--)
1123 {
1124 if(DisplayActive_arg)
1125 *target = BrightCLUT[lr][source_bits & 3];
1126 else
1127 *target = 0;
1128 source_bits >>= 2;
1129 target += pitch32;
1130 }
1131 fb_source++;
1132 }
1133 }
1134
1135 static void CopyFBColumnToTarget_SideBySide(void)
1136 {
1137 const int lr = (DisplayRegion & 2) >> 1;
1138
1139 if(!DisplayActive)
1140 {
1141 if(!lr)
1142 CopyFBColumnToTarget_SideBySide_BASE(0, 0, 0 ^ VB3DReverse);
1143 else
1144 CopyFBColumnToTarget_SideBySide_BASE(0, 1, 1 ^ VB3DReverse);
1145 }
1146 else
1147 {
1148 if(!lr)
1149 CopyFBColumnToTarget_SideBySide_BASE(1, 0, 0 ^ VB3DReverse);
1150 else
1151 CopyFBColumnToTarget_SideBySide_BASE(1, 1, 1 ^ VB3DReverse);
1152 }
1153 }
1154
1155 static INLINE void CopyFBColumnToTarget_VLI_BASE(const bool DisplayActive_arg, const int lr, const int dest_lr)
1156 {
1157 const int fb = DisplayFB;
1158 uint32 *target = surface->pixels + Column * 2 * VBPrescale + dest_lr;
1159 const int32 pitch32 = surface->pitch32;
1160 const uint8 *fb_source = &FB[fb][lr][64 * Column];
1161
1162 for(int y = 56; y; y--)
1163 {
1164 uint32 source_bits = *fb_source;
1165
1166 for(int y_sub = 4; y_sub; y_sub--)
1167 {
1168 uint32 tv;
1169
1170 if(DisplayActive_arg)
1171 tv = BrightCLUT[0][source_bits & 3];
1172 else
1173 tv = 0;
1174
1175 for(uint32 ps = 0; ps < VBPrescale; ps++)
1176 target[ps * 2] = tv;
1177
1178 source_bits >>= 2;
1179 target += pitch32;
1180 }
1181 fb_source++;
1182 }
1183 }
1184
1185 static void CopyFBColumnToTarget_VLI(void)
1186 {
1187 const int lr = (DisplayRegion & 2) >> 1;
1188
1189 if(!DisplayActive)
1190 {
1191 if(!lr)
1192 CopyFBColumnToTarget_VLI_BASE(0, 0, 0 ^ VB3DReverse);
1193 else
1194 CopyFBColumnToTarget_VLI_BASE(0, 1, 1 ^ VB3DReverse);
1195 }
1196 else
1197 {
1198 if(!lr)
1199 CopyFBColumnToTarget_VLI_BASE(1, 0, 0 ^ VB3DReverse);
1200 else
1201 CopyFBColumnToTarget_VLI_BASE(1, 1, 1 ^ VB3DReverse);
1202 }
1203 }
1204
1205 static INLINE void CopyFBColumnToTarget_HLI_BASE(const bool DisplayActive_arg, const int lr, const int dest_lr)
1206 {
1207 const int fb = DisplayFB;
1208 const int32 pitch32 = surface->pitch32;
1209 uint32 *target = surface->pixels + Column + dest_lr * pitch32;
1210 const uint8 *fb_source = &FB[fb][lr][64 * Column];
1211
1212 if(VBPrescale <= 4)
1213 for(int y = 56; y; y--)
1214 {
1215 uint32 source_bits = HLILUT[*fb_source];
1216
1217 for(int y_sub = 4 * VBPrescale; y_sub; y_sub--)
1218 {
1219 if(DisplayActive_arg)
1220 *target = BrightCLUT[0][source_bits & 3];
1221 else
1222 *target = 0;
1223
1224 target += pitch32 * 2;
1225 source_bits >>= 2;
1226 }
1227 fb_source++;
1228 }
1229 else
1230 for(int y = 56; y; y--)
1231 {
1232 uint32 source_bits = *fb_source;
1233
1234 for(int y_sub = 4; y_sub; y_sub--)
1235 {
1236 for(uint32 ps = 0; ps < VBPrescale; ps++)
1237 {
1238 if(DisplayActive_arg)
1239 *target = BrightCLUT[0][source_bits & 3];
1240 else
1241 *target = 0;
1242
1243 target += pitch32 * 2;
1244 }
1245
1246 source_bits >>= 2;
1247 }
1248 fb_source++;
1249 }
1250 }
1251
1252 static void CopyFBColumnToTarget_HLI(void)
1253 {
1254 const int lr = (DisplayRegion & 2) >> 1;
1255
1256 if(!DisplayActive)
1257 {
1258 if(!lr)
1259 CopyFBColumnToTarget_HLI_BASE(0, 0, 0 ^ VB3DReverse);
1260 else
1261 CopyFBColumnToTarget_HLI_BASE(0, 1, 1 ^ VB3DReverse);
1262 }
1263 else
1264 {
1265 if(!lr)
1266 CopyFBColumnToTarget_HLI_BASE(1, 0, 0 ^ VB3DReverse);
1267 else
1268 CopyFBColumnToTarget_HLI_BASE(1, 1, 1 ^ VB3DReverse);
1269 }
1270 }
1271
1272 v810_timestamp_t MDFN_FASTCALL VIP_Update(const v810_timestamp_t timestamp)
1273 {
1274 int32 clocks = timestamp - last_ts;
1275 int32 running_timestamp = timestamp;
1276
1277 while(clocks > 0)
1278 {
1279 int32 chunk_clocks = clocks;
1280
1281 if(DrawingCounter > 0 && chunk_clocks > DrawingCounter)
1282 chunk_clocks = DrawingCounter;
1283 if(chunk_clocks > ColumnCounter)
1284 chunk_clocks = ColumnCounter;
1285
1286 running_timestamp += chunk_clocks;
1287
1288 if(DrawingCounter > 0)
1289 {
1290 DrawingCounter -= chunk_clocks;
1291 if(DrawingCounter <= 0)
1292 {
1293 MDFN_ALIGN(8) uint8 DrawingBuffers[2][512 * 8]; // Don't decrease this from 512 unless you adjust vip_draw.inc(including areas that draw off-visible >= 384 and >= -7 for speed reasons)
1294
1295 if(skip && InstantDisplayHack && AllowDrawSkip)
1296 {
1297 #if 0
1298 for(int lr = 0; lr < 2; lr++)
1299 {
1300 uint8 *FB_Target = FB[DrawingFB][lr] + DrawingBlock * 2;
1301 for(int x = 0; x < 384; x++)
1302 {
1303 FB_Target[64 * x + 0] = BKCOL;
1304 FB_Target[64 * x + 1] = BKCOL;
1305 }
1306 }
1307 #endif
1308 }
1309 else
1310 {
1311 VIP_DrawBlock(DrawingBlock, DrawingBuffers[0] + 8, DrawingBuffers[1] + 8);
1312
1313 for(int lr = 0; lr < 2; lr++)
1314 {
1315 uint8 *FB_Target = FB[DrawingFB][lr] + DrawingBlock * 2;
1316
1317 for(int x = 0; x < 384; x++)
1318 {
1319 FB_Target[64 * x + 0] = (DrawingBuffers[lr][8 + x + 512 * 0] << 0)
1320 | (DrawingBuffers[lr][8 + x + 512 * 1] << 2)
1321 | (DrawingBuffers[lr][8 + x + 512 * 2] << 4)
1322 | (DrawingBuffers[lr][8 + x + 512 * 3] << 6);
1323
1324 FB_Target[64 * x + 1] = (DrawingBuffers[lr][8 + x + 512 * 4] << 0)
1325 | (DrawingBuffers[lr][8 + x + 512 * 5] << 2)
1326 | (DrawingBuffers[lr][8 + x + 512 * 6] << 4)
1327 | (DrawingBuffers[lr][8 + x + 512 * 7] << 6);
1328
1329 }
1330 }
1331 }
1332
1333 SBOUT_InactiveTime = running_timestamp + 1120;
1334 SB_Latch = DrawingBlock; // Not exactly correct, but probably doesn't matter.
1335
1336 DrawingBlock++;
1337 if(DrawingBlock == 28)
1338 {
1339 DrawingActive = false;
1340
1341 InterruptPending |= INT_XP_END;
1342 CheckIRQ();
1343 }
1344 else
1345 DrawingCounter += 1120 * 4;
1346 }
1347 }
1348
1349 ColumnCounter -= chunk_clocks;
1350 if(ColumnCounter == 0)
1351 {
1352 if(DisplayRegion & 1)
1353 {
1354 if(!(Column & 3))
1355 {
1356 const int lr = (DisplayRegion & 2) >> 1;
1357 uint16 ctdata = VIP_MA16R16(DRAM, 0x1DFFE - ((Column >> 2) * 2) - (lr ? 0 : 0x200));
1358
1359 if((ctdata >> 8) != Repeat)
1360 {
1361 Repeat = ctdata >> 8;
1362 RecalcBrightnessCache();
1363 }
1364 }
1365 if(!skip && !InstantDisplayHack)
1366 CopyFBColumnToTarget();
1367 }
1368
1369 ColumnCounter = 259;
1370 Column++;
1371 if(Column == 384)
1372 {
1373 Column = 0;
1374
1375 if(DisplayActive)
1376 {
1377 if(DisplayRegion & 1) // Did we just finish displaying an active region?
1378 {
1379 if(DisplayRegion & 2) // finished displaying right eye
1380 InterruptPending |= INT_RFB_END;
1381 else // Otherwise, left eye
1382 InterruptPending |= INT_LFB_END;
1383
1384 CheckIRQ();
1385 }
1386 }
1387
1388 DisplayRegion = (DisplayRegion + 1) & 3;
1389
1390 if(DisplayRegion == 0) // New frame start
1391 {
1392 DisplayActive = DPCTRL & 0x2;
1393
1394 if(DisplayActive)
1395 {
1396 InterruptPending |= INT_FRAME_START;
1397 CheckIRQ();
1398 }
1399 GameFrameCounter++;
1400 if(GameFrameCounter > FRMCYC) // New game frame start?
1401 {
1402 InterruptPending |= INT_GAME_START;
1403 CheckIRQ();
1404
1405 if(XPCTRL & XPCTRL_XP_EN)
1406 {
1407 DisplayFB ^= 1;
1408
1409 DrawingBlock = 0;
1410 DrawingActive = true;
1411 DrawingCounter = 1120 * 4;
1412 DrawingFB = DisplayFB ^ 1;
1413 }
1414
1415 GameFrameCounter = 0;
1416 }
1417
1418 if(!skip && InstantDisplayHack)
1419 {
1420 // Ugly kludge, fix in the future.
1421 int32 save_DisplayRegion = DisplayRegion;
1422 int32 save_Column = Column;
1423 uint8 save_Repeat = Repeat;
1424
1425 for(int lr = 0; lr < 2; lr++)
1426 {
1427 DisplayRegion = lr << 1;
1428 for(Column = 0; Column < 384; Column++)
1429 {
1430 if(!(Column & 3))
1431 {
1432 uint16 ctdata = VIP_MA16R16(DRAM, 0x1DFFE - ((Column >> 2) * 2) - (lr ? 0 : 0x200));
1433
1434 if((ctdata >> 8) != Repeat)
1435 {
1436 Repeat = ctdata >> 8;
1437 RecalcBrightnessCache();
1438 }
1439 }
1440
1441 CopyFBColumnToTarget();
1442 }
1443 }
1444 DisplayRegion = save_DisplayRegion;
1445 Column = save_Column;
1446 Repeat = save_Repeat;
1447 RecalcBrightnessCache();
1448 }
1449
1450 VB_ExitLoop();
1451 }
1452 }
1453 }
1454
1455 clocks -= chunk_clocks;
1456 }
1457
1458 last_ts = timestamp;
1459
1460 return(timestamp + CalcNextEvent());
1461 }
1462
1463 int VIP_StateAction(StateMem *sm, int load, int data_only)
1464 {
1465 SFORMAT StateRegs[] =
1466 {
1467 SFARRAY(FB[0][0], 0x6000 * 2 * 2),
1468 SFARRAY16(CHR_RAM, 0x8000 / sizeof(uint16)),
1469 SFARRAY16(DRAM, 0x20000 / sizeof(uint16)),
1470
1471 SFVAR(InterruptPending),
1472 SFVAR(InterruptEnable),
1473
1474 SFVAR(BRTA),
1475 SFVAR(BRTB),
1476 SFVAR(BRTC),
1477 SFVAR(REST),
1478
1479 SFVAR(FRMCYC),
1480 SFVAR(DPCTRL),
1481
1482 SFVAR(DisplayActive),
1483
1484 SFVAR(XPCTRL),
1485 SFVAR(SBCMP),
1486 SFARRAY16(SPT, 4),
1487 SFARRAY16(GPLT, 4), // FIXME
1488 SFARRAY16(JPLT, 4),
1489
1490 SFVAR(BKCOL),
1491
1492 SFVAR(Column),
1493 SFVAR(ColumnCounter),
1494
1495 SFVAR(DisplayRegion),
1496 SFVAR(DisplayFB),
1497
1498 SFVAR(GameFrameCounter),
1499
1500 SFVAR(DrawingCounter),
1501
1502 SFVAR(DrawingActive),
1503 SFVAR(DrawingFB),
1504 SFVAR(DrawingBlock),
1505
1506 SFVAR(SB_Latch),
1507 SFVAR(SBOUT_InactiveTime),
1508
1509 SFVAR(Repeat),
1510 SFEND
1511 };
1512
1513 int ret = MDFNSS_StateAction(sm, load, data_only, StateRegs, "VIP");
1514
1515 if(load)
1516 {
1517 RecalcBrightnessCache();
1518 for(int i = 0; i < 4; i++)
1519 {
1520 Recalc_GPLT_Cache(i);
1521 Recalc_JPLT_Cache(i);
1522 }
1523 }
1524
1525 return(ret);
1526 }
1527
1528 uint32 VIP_GetRegister(const unsigned int id, char *special, const uint32 special_len)
1529 {
1530 switch(id)
1531 {
1532 case VIP_GSREG_IPENDING:
1533 return InterruptPending;
1534 case VIP_GSREG_IENABLE:
1535 return InterruptEnable;
1536 case VIP_GSREG_DPCTRL:
1537 return DPCTRL;
1538 case VIP_GSREG_BRTA:
1539 return BRTA;
1540 case VIP_GSREG_BRTB:
1541 return BRTB;
1542 case VIP_GSREG_BRTC:
1543 return BRTC;
1544 case VIP_GSREG_REST:
1545 return REST;
1546 case VIP_GSREG_FRMCYC:
1547 return FRMCYC;
1548 case VIP_GSREG_XPCTRL:
1549 return XPCTRL | (SBCMP << 8);
1550 case VIP_GSREG_SPT0:
1551 case VIP_GSREG_SPT1:
1552 case VIP_GSREG_SPT2:
1553 case VIP_GSREG_SPT3:
1554 return SPT[id - VIP_GSREG_SPT0];
1555 case VIP_GSREG_GPLT0:
1556 case VIP_GSREG_GPLT1:
1557 case VIP_GSREG_GPLT2:
1558 case VIP_GSREG_GPLT3:
1559 return GPLT[id - VIP_GSREG_GPLT0];
1560 case VIP_GSREG_JPLT0:
1561 case VIP_GSREG_JPLT1:
1562 case VIP_GSREG_JPLT2:
1563 case VIP_GSREG_JPLT3:
1564 return JPLT[id - VIP_GSREG_JPLT0];
1565 case VIP_GSREG_BKCOL:
1566 return BKCOL;
1567 }
1568
1569 return 0xDEADBEEF;
1570 }
1571
1572 void VIP_SetRegister(const unsigned int id, const uint32 value)
1573 {
1574 switch(id)
1575 {
1576 case VIP_GSREG_IPENDING:
1577 InterruptPending = value & 0xE01F;
1578 CheckIRQ();
1579 break;
1580
1581 case VIP_GSREG_IENABLE:
1582 InterruptEnable = value & 0xE01F;
1583 CheckIRQ();
1584 break;
1585
1586 case VIP_GSREG_DPCTRL:
1587 DPCTRL = value & 0x703; // FIXME(Lower bit?)
1588 break;
1589
1590 case VIP_GSREG_BRTA:
1591 BRTA = value & 0xFF;
1592 RecalcBrightnessCache();
1593 break;
1594
1595 case VIP_GSREG_BRTB:
1596 BRTB = value & 0xFF;
1597 RecalcBrightnessCache();
1598 break;
1599
1600 case VIP_GSREG_BRTC:
1601 BRTC = value & 0xFF;
1602 RecalcBrightnessCache();
1603 break;
1604
1605 case VIP_GSREG_REST:
1606 REST = value & 0xFF;
1607 RecalcBrightnessCache();
1608 break;
1609
1610 case VIP_GSREG_FRMCYC:
1611 FRMCYC = value & 0xF;
1612 break;
1613
1614 case VIP_GSREG_XPCTRL:
1615 XPCTRL = value & 0x2;
1616 SBCMP = (value >> 8) & 0x1f;
1617 break;
1618
1619 case VIP_GSREG_SPT0:
1620 case VIP_GSREG_SPT1:
1621 case VIP_GSREG_SPT2:
1622 case VIP_GSREG_SPT3:
1623 SPT[id - VIP_GSREG_SPT0] = value & 0x3FF;
1624 break;
1625
1626 case VIP_GSREG_GPLT0:
1627 case VIP_GSREG_GPLT1:
1628 case VIP_GSREG_GPLT2:
1629 case VIP_GSREG_GPLT3:
1630 GPLT[id - VIP_GSREG_GPLT0] = value & 0xFC;
1631 Recalc_GPLT_Cache(id - VIP_GSREG_GPLT0);
1632 break;
1633
1634 case VIP_GSREG_JPLT0:
1635 case VIP_GSREG_JPLT1:
1636 case VIP_GSREG_JPLT2:
1637 case VIP_GSREG_JPLT3:
1638 JPLT[id - VIP_GSREG_JPLT0] = value & 0xFC;
1639 Recalc_JPLT_Cache(id - VIP_GSREG_JPLT0);
1640 break;
1641
1642 case VIP_GSREG_BKCOL:
1643 BKCOL = value & 0x03;
1644 break;
1645 }
1646 }