Debian Codegraph @

mednafen/pce_fast/vdc.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 /* VDC and VCE emulation */
19
20 /*
21 "Tonight I hooked up my Turbo Duo(with no games or CDs in it)'s video output to my PC sound card, recorded it,
22 and did a FFT and looked at the spectrum(around the line rate, 15-16KHz), and I also counted the number
23 of samples between the ~60Hz peaks(just to verify that the math shown below is remotely accurate).
24
25 The spectrum peaked at 15734 Hz. 21477272.727272... / 3 / 15734 = 455.00(CPU cycles per scanline)"
26 */
27
28 #include "pce.h"
29 #include "../video.h"
30 #include "vdc.h"
31 #include "huc.h"
32 #include "pcecd.h"
33 #include "../FileStream.h"
34 #include <math.h>
35
36 static uint32 userle; // User layer enable.
37 static uint32 disabled_layer_color;
38
39 static bool unlimited_sprites;
40 static bool correct_aspect;
41
42 #define ULE_BG0 1
43 #define ULE_SPR0 2
44 #define ULE_BG1 4
45 #define ULE_SPR1 8
46
47 static const uint8 bat_width_shift_tab[4] = { 5, 6, 7, 7 };
48 static const uint8 bat_height_mask_tab[2] = { 32 - 1, 64 - 1 };
49
50 static unsigned int VDS;
51 static unsigned int VSW;
52 static unsigned int VDW;
53 static unsigned int VCR;
54 static unsigned int VBlankFL;
55
56 vce_t vce;
57
58 vdc_t *vdc = NULL;
59
60 #define MAKECOLOR_PCE(val) ((((val & 0x038) >> 3) << 13)|(((((val & 0x038) >> 3) & 0x6) << 10) | (((val & 0x1c0) >> 6) << 8) | (((val & 0x1c0) >> 6) << 5) | ((val & 0x007) << 2) | ((val & 0x007) >> 1)))
61
62
63 static INLINE void MDFN_FastU32MemsetM8(uint32_t *array, uint32_t value_32, unsigned int u32len)
64 {
65 uint32_t *ai;
66
67 for(ai = array; ai < array + u32len; ai += 2)
68 {
69 ai[0] = value_32;
70 ai[1] = value_32;
71 }
72 }
73
74 static INLINE void FixPCache(int entry)
75 {
76 if(!(entry & 0xFF))
77 {
78 uint16_t color0 = MAKECOLOR_PCE(vce.color_table[entry & 0x100]);
79 for(int x = 0; x < 16; x++)
80 vce.color_table_cache[entry + (x << 4)] = color0 ;
81 return;
82 }
83
84 if(entry & 0xF)
85 vce.color_table_cache[entry] = MAKECOLOR_PCE(vce.color_table[entry]);
86 }
87
88 static INLINE void FixTileCache(vdc_t *which_vdc, uint16 A)
89 {
90 uint32 charname = (A >> 4);
91 uint32 y = (A & 0x7);
92 uint64 *tc = &which_vdc->bg_tile_cache[charname][y];
93
94 uint32 bitplane01 = which_vdc->VRAM[y + charname * 16];
95 uint32 bitplane23 = which_vdc->VRAM[y+ 8 + charname * 16];
96
97 *tc = 0;
98
99 for(int x = 0; x < 8; x++)
100 {
101 uint32 raw_pixel = ((bitplane01 >> x) & 1);
102 raw_pixel |= ((bitplane01 >> (x + 8)) & 1) << 1;
103 raw_pixel |= ((bitplane23 >> x) & 1) << 2;
104 raw_pixel |= ((bitplane23 >> (x + 8)) & 1) << 3;
105
106 #ifdef MSB_FIRST
107 *tc |= (uint64)raw_pixel << ((x) * 8);
108 #else
109 *tc |= (uint64)raw_pixel << ((7 - x) * 8);
110 #endif
111 }
112 }
113
114 static INLINE void CheckFixSpriteTileCache(vdc_t *which_vdc, uint16 no, uint32 special)
115 {
116 if(special != 0x4 && special != 0x5)
117 special = 0;
118
119 if((special | 0x80) == which_vdc->spr_tile_clean[no])
120 return;
121
122 //printf("Oops: %d, %d, %d\n", no, special | 0x100, which_vdc->spr_tile_clean[no]);
123 if((no * 64) >= VRAM_Size)
124 {
125 //printf("Unmapped: %d\n", no);
126 //VDC_UNDEFINED("Unmapped VRAM sprite tile read");
127 // Unnecessary, since we reset the sprite tile cache to 0 on reset/init anyway.
128 //memset(which_vdc->spr_tile_cache[no], 0x00, 16 * 16 * sizeof(uint16));
129 }
130 else if(special)
131 {
132 for(int y = 0; y < 16; y++)
133 {
134 uint8 *tc = which_vdc->spr_tile_cache[no][y];
135
136 uint32 bitplane0 = which_vdc->VRAM[y + 0x00 + no * 0x40 + ((special & 1) << 5)];
137 uint32 bitplane1 = which_vdc->VRAM[y + 0x10 + no * 0x40 + ((special & 1) << 5)];
138
139 for(int x = 0; x < 16; x++)
140 {
141 uint32 raw_pixel;
142 raw_pixel = ((bitplane0 >> x) & 1) << 0;
143 raw_pixel |= ((bitplane1 >> x) & 1) << 1;
144 tc[x] = raw_pixel;
145 }
146 }
147 }
148 else
149 {
150 for(int y = 0; y < 16; y++)
151 {
152 uint8 *tc = which_vdc->spr_tile_cache[no][y];
153
154 uint32 bitplane0 = which_vdc->VRAM[y + 0x00 + no * 0x40];
155 uint32 bitplane1 = which_vdc->VRAM[y + 0x10 + no * 0x40];
156 uint32 bitplane2 = which_vdc->VRAM[y + 0x20 + no * 0x40];
157 uint32 bitplane3 = which_vdc->VRAM[y + 0x30 + no * 0x40];
158
159 for(int x = 0; x < 16; x++)
160 {
161 uint32 raw_pixel;
162 raw_pixel = ((bitplane0 >> x) & 1) << 0;
163 raw_pixel |= ((bitplane1 >> x) & 1) << 1;
164 raw_pixel |= ((bitplane2 >> x) & 1) << 2;
165 raw_pixel |= ((bitplane3 >> x) & 1) << 3;
166 tc[x] = raw_pixel;
167 }
168 }
169 }
170
171 which_vdc->spr_tile_clean[no] = special | 0x80;
172 }
173
174
175 static INLINE void SetVCECR(uint8 V)
176 {
177 if(((V & 0x80) >> 7) != vce.bw)
178 {
179 vce.bw = V & 0x80;
180 for(int x = 0; x < 512; x++)
181 FixPCache(x);
182 }
183
184 vce.lc263 = (V & 0x04);
185 vce.dot_clock = V & 1;
186 if(V & 2)
187 vce.dot_clock = 2;
188 vce.CR = V;
189 }
190
191 static unsigned int frame_counter;
192 static int32 need_vbi[2] = { 0, 0 };
193 static int32 line_leadin1 = 0;
194 static int32 magical, cyc_tot;
195
196 vpc_t vpc;
197
198 // Some virtual vdc macros to make code simpler to read
199 #define M_vdc_HSW (vdc->HSR & 0x1F) // Horizontal Synchro Width
200 #define M_vdc_HDS ((vdc->HSR >> 8) & 0x7F) // Horizontal Display Start
201 #define M_vdc_HDW (vdc->HDR & 0x7F) // Horizontal Display Width
202 #define M_vdc_HDE ((vdc->HDR >> 8) & 0x7F) // Horizontal Display End
203
204 #define M_vdc_VSW (vdc->VSR & 0x1F) // Vertical synchro width
205 #define M_vdc_VDS ((vdc->VSR >> 8) & 0xFF) // Vertical Display Start
206 #define M_vdc_VDW (vdc->VDR & 0x1FF) // Vertical Display Width(Height? :b)
207 #define M_vdc_VCR (vdc->VCR & 0xFF)
208
209 #define VDCS_CR 0x01 // Sprite #0 collision interrupt occurred
210 #define VDCS_OR 0x02 // sprite overflow "" ""
211 #define VDCS_RR 0x04 // RCR "" ""
212 #define VDCS_DS 0x08 // VRAM to SAT DMA completion interrupt occurred
213 #define VDCS_DV 0x10 // VRAM to VRAM DMA completion interrupt occurred
214 #define VDCS_VD 0x20 // Vertical blank interrupt occurred
215 #define VDCS_BSY 0x40 // VDC is waiting for a CPU access slot during the active display area??
216
217 void VDC_SetPixelFormat(void)
218 {
219 // I know the temptation is there, but don't combine these two loops just
220 // because they loop 512 times ;)
221 for(int x = 0; x < 512; x++)
222 FixPCache(x);
223
224 disabled_layer_color = MAKECOLOR(0x00, 0xFE, 0x00, 0);
225 }
226
227 DECLFR(VCE_Read)
228 {
229 switch(A & 0x7)
230 {
231 case 4:
232 return (vce.color_table[vce.ctaddress & 0x1FF]);
233 case 5:
234 {
235 uint8 ret = vce.color_table[vce.ctaddress & 0x1FF] >> 8;
236 ret &= 1;
237 ret |= 0xFE;
238 vce.ctaddress++;
239 return(ret);
240 }
241 }
242
243 return(0xFF);
244 }
245
246 DECLFW(VCE_Write)
247 {
248 //printf("%04x %02x, %04x\n", A, V, HuCPU.PC);
249 switch(A&0x7)
250 {
251 case 0: SetVCECR(V); break;
252 case 2: vce.ctaddress &= 0x100; vce.ctaddress |= V; break;
253 case 3: vce.ctaddress &= 0x0FF; vce.ctaddress |= (V & 1) << 8; break;
254 case 4: vce.color_table[vce.ctaddress & 0x1FF] &= 0x100;
255 vce.color_table[vce.ctaddress & 0x1FF] |= V;
256 FixPCache(vce.ctaddress & 0x1FF);
257 break;
258 case 5: vce.color_table[vce.ctaddress & 0x1FF] &= 0xFF;
259 vce.color_table[vce.ctaddress & 0x1FF] |= (V & 1) << 8;
260 FixPCache(vce.ctaddress & 0x1FF);
261 vce.ctaddress++;
262 break;
263 }
264 }
265
266
267 void VDC_SetLayerEnableMask(uint64 mask)
268 {
269 userle = mask;
270 }
271
272 DECLFW(VDC_Write_ST)
273 {
274 //printf("WriteST: %04x %02x\n", A, V);
275 VDC_Write(A, V);
276 }
277
278 static void DoDMA(vdc_t *vdc)
279 {
280 // Assuming one cycle for reads, one cycle for write, with DMA?
281 for(int i = 0; i < 455; i++)
282 {
283 if(!vdc->DMAReadWrite)
284 {
285 if(vdc->SOUR >= VRAM_Size)
286 VDC_UNDEFINED("Unmapped VRAM DMA read");
287
288 vdc->DMAReadBuffer = vdc->VRAM[vdc->SOUR];
289 }
290 else
291 {
292 if(vdc->DESR < VRAM_Size)
293 {
294 vdc->VRAM[vdc->DESR] = vdc->DMAReadBuffer;
295 FixTileCache(vdc, vdc->DESR);
296 vdc->spr_tile_clean[vdc->DESR >> 6] = 0;
297 }
298
299 //if(vdc->DCR & 0xC)
300 //printf("Pllal: %02x\n", vdc->DCR);
301
302 vdc->SOUR += (((vdc->DCR & 0x4) >> 1) ^ 2) - 1;
303 vdc->DESR += (((vdc->DCR & 0x8) >> 2) ^ 2) - 1;
304 vdc->LENR--;
305 if(vdc->LENR == 0xFFFF) // DMA is done.
306 {
307 vdc->DMARunning = 0;
308 if(vdc->DCR & 0x02)
309 {
310 vdc->status |= VDCS_DV;
311 HuC6280_IRQBegin(MDFN_IQIRQ1);
312 VDC_DEBUG("DMA IRQ");
313 }
314 break;
315 }
316 }
317 vdc->DMAReadWrite ^= 1;
318 } // for()
319 }
320
321 DECLFW(VDC_Write)
322 {
323 int msb = A & 1;
324 int chip = 0;
325
326 A &= 0x3;
327 //if((A == 0x2 || A == 0x3) && ((vdc->select & 0x1f) >= 0x09) && ((vdc->select & 0x1f) <= 0x13))
328
329 //printf("%04x, %02x: %02x, %d\n", A, vdc->select, V, vdc->display_counter);
330
331 switch(A)
332 {
333 case 0x0: vdc->select = V & 0x1F; break;
334 case 0x2:
335 case 0x3:
336 switch(vdc->select & 0x1F)
337 {
338 case 0x00: REGSETP(vdc->MAWR, V, msb); break;
339 case 0x01: REGSETP(vdc->MARR, V, msb);
340 if(msb)
341 {
342 if(vdc->MARR >= VRAM_Size)
343 VDC_UNDEFINED("Unmapped VRAM VRR(MARR set) read");
344
345 vdc->read_buffer = vdc->VRAM[vdc->MARR];
346 }
347 break;
348 case 0x02: if(!msb) vdc->write_latch = V;
349 else
350 {
351 if(vdc->MAWR < VRAM_Size)
352 {
353 // A hack to fix Crest of Wolf, and maybe others.
354 while(vdc->DMARunning)
355 DoDMA(vdc);
356
357 vdc->VRAM[vdc->MAWR] = (V << 8) | vdc->write_latch;
358 FixTileCache(vdc, vdc->MAWR);
359 vdc->spr_tile_clean[vdc->MAWR >> 6] = 0;
360 }
361 else
362 {
363 //VDC_UNDEFINED("Unmapped VRAM write");
364 //printf("VROOM: %04x, %02x\n", vdc->MAWR, (vdc->CR >> 11) & 0x3);
365 }
366 vdc->MAWR += vram_inc_tab[(vdc->CR >> 11) & 0x3];
367 }
368 break;
369 case 0x05: REGSETP(vdc->CR, V, msb); break;
370 case 0x06: REGSETP(vdc->RCR, V, msb); vdc->RCR &= 0x3FF; break;
371 case 0x07: REGSETP(vdc->BXR, V, msb); vdc->BXR &= 0x3FF; break;
372 case 0x08: REGSETP(vdc->BYR, V, msb); vdc->BYR &= 0x1FF;
373 vdc->BG_YOffset = vdc->BYR; // Set it on LSB and MSB writes(only changing on MSB breaks Youkai Douchuuki)
374 //printf("%04x\n", HuCPU.PC);
375 break;
376 case 0x09: REGSETP(vdc->MWR, V, msb); break;
377 case 0x0a: REGSETP(vdc->HSR, V, msb); break;
378 case 0x0b: REGSETP(vdc->HDR, V, msb); break;
379 case 0x0c: REGSETP(vdc->VSR, V, msb); break;
380 case 0x0d: REGSETP(vdc->VDR, V, msb); break;
381 case 0x0e: REGSETP(vdc->VCR, V, msb); break;
382 case 0x0f: REGSETP(vdc->DCR, V, msb); break;
383 case 0x10: REGSETP(vdc->SOUR, V, msb); break;
384 case 0x11: REGSETP(vdc->DESR, V, msb); break;
385 case 0x12: REGSETP(vdc->LENR, V, msb);
386 if(msb)
387 {
388 vdc->DMARunning = 1;
389 vdc->DMAReadWrite = 0;
390 if(vdc->burst_mode && !(vdc->DCR & 0x02))
391 DoDMA(vdc); // Do one line's worth of DMA transfers
392 // because Cosmic Fantasy 4 is evil
393 // and uses timed writes to the DMA
394 // start register, rather than waiting until
395 // the machine says we're done,
396 // which would require cycle-accurate VDC emulation...like that's
397 // going to happen when I don't even have accurate values
398 // for HuC6280 instruction timings. :b
399 }
400 break;
401 case 0x13: REGSETP(vdc->SATB, V, msb); vdc->SATBPending = 1; break;
402 // default: printf("Oops 2: %04x %02x\n", vdc->select, V);break;
403 }
404 break;
405 }
406 }
407
408
409 // 682 + 8 + 128 = 818.
410 static INLINE void CalcStartEnd(const vdc_t *vdc, uint32 &start, uint32 &end)
411 {
412 //static const unsigned int ClockModeWidths[3] = { 288, 384, 576 };
413 static const unsigned int ClockPixelWidths[3] = { 341, 455, 682 };
414
415 start = (M_vdc_HDS + 1) * 8;
416 // Semi-hack for Asuka 120%
417 if(vce.dot_clock == 1 && M_vdc_HDS == 5 && M_vdc_HDE == 6 && M_vdc_HDW == 43 && M_vdc_HSW == 2)
418 start -= 8;
419 else if(vce.dot_clock == 0 && M_vdc_HDS == 2 && M_vdc_HDE == 3 && M_vdc_HDW == 33 && M_vdc_HSW == 2)
420 start -= 4;
421 // and for Addams Family
422 else if(vce.dot_clock == 1 && M_vdc_HDS == 4 && M_vdc_HDE == 4 && M_vdc_HDW == 43 && M_vdc_HSW == 9)
423 start -= 4;
424 end = start + (M_vdc_HDW + 1) * 8;
425
426 if(start > (ClockPixelWidths[vce.dot_clock]))
427 start = ClockPixelWidths[vce.dot_clock];
428
429 if(end > (ClockPixelWidths[vce.dot_clock]))
430 end = ClockPixelWidths[vce.dot_clock];
431
432 if(start == end) // In case HDS is way off-screen, REMOVE when we confirm the rest of the code won't flip out
433 start = end - 8; // when start == end;
434
435 // For: start - (vdc->BG_XOffset & 7)
436 end += 8;
437 start += 8;
438
439 // For: alignment space when correct_aspect == 0
440 end += 128;
441 start += 128;
442 }
443
444 #define CB_EXL(n) (((n) << 4) | ((n) << 12) | ((n) << 20) | ((n) << 28) | ((n) << 36) | ((n) << 44) | ((n) << 52) | ((n) << 60))
445 static const uint64 cblock_exlut[16] = {
446 CB_EXL(0ULL), CB_EXL(1ULL), CB_EXL(2ULL), CB_EXL(3ULL), CB_EXL(4ULL), CB_EXL(5ULL), CB_EXL(6ULL), CB_EXL(7ULL),
447 CB_EXL(8ULL), CB_EXL(9ULL), CB_EXL(10ULL), CB_EXL(11ULL), CB_EXL(12ULL), CB_EXL(13ULL), CB_EXL(14ULL), CB_EXL(15ULL)
448 };
449
450 static void DrawBG(const vdc_t *vdc, const uint32 count, uint8 *target) NO_INLINE;
451 static void DrawBG(const vdc_t *vdc, const uint32 count, uint8 *target)
452 {
453 int bat_width_shift = bat_width_shift_tab[(vdc->MWR >> 4) & 3];
454 int bat_width_mask = (1U << bat_width_shift) - 1;
455 int bat_height_mask = bat_height_mask_tab[(vdc->MWR >> 6) & 1];
456 uint64 *target64 = (uint64 *)target;
457
458 {
459 int bat_y = ((vdc->BG_YOffset >> 3) & bat_height_mask) << bat_width_shift;
460
461 int bat_boom = (vdc->BG_XOffset >> 3) & bat_width_mask;
462 int line_sub = vdc->BG_YOffset & 7;
463
464 const uint16 *BAT_Base = &vdc->VRAM[bat_y];
465 const uint64 *CG_Base = &vdc->bg_tile_cache[0][line_sub];
466
467 uint64_t cg_mask = 0xFFFFFFFFFFFFFFFFULL;
468
469 if((vdc->MWR & 0x3) == 0x3)
470 cg_mask = (vdc->MWR & 0x80) ? 0xCCCCCCCCCCCCCCCCULL : 0x3333333333333333ULL;
471
472 for(int x = count - 1; x >= 0; x -= 8)
473 {
474 const uint16 bat = BAT_Base[bat_boom];
475 const uint64 color_or = cblock_exlut[bat >> 12];
476
477 *target64 = (CG_Base[(bat & 0xFFF) * 8] & cg_mask) | color_or;
478
479 bat_boom = (bat_boom + 1) & bat_width_mask;
480 target64++;
481 }
482 }
483 }
484
485 #define SPRF_PRIORITY 0x00080
486 #define SPRF_HFLIP 0x00800
487 #define SPRF_VFLIP 0x08000
488 #define SPRF_SPRITE0 0x10000
489
490 static const unsigned int sprite_height_tab[4] = { 16, 32, 64, 64 };
491 static const unsigned int sprite_height_no_mask[4] = { ~0U, ~2U, ~6U, ~6U };
492
493 static INLINE void RebuildSATCache(vdc_t *vdc)
494 {
495 unsigned i;
496 SAT_Cache_t *sat_ptr = (SAT_Cache_t*)vdc->SAT_Cache;
497
498 vdc->SAT_Cache_Valid = 0;
499
500 for(i = 0; i < 64; i++)
501 {
502 uint16 height;
503
504 const uint16 SATR0 = vdc->SAT[i * 4 + 0x0];
505 const uint16 SATR1 = vdc->SAT[i * 4 + 0x1];
506 const uint16 SATR2 = vdc->SAT[i * 4 + 0x2];
507 const uint16 SATR3 = vdc->SAT[i * 4 + 0x3];
508
509
510 int16 y = (int16)(SATR0 & 0x3FF) - 0x40;
511 uint16 x = SATR1 & 0x3FF;
512 uint16 no = (SATR2 >> 1) & 0x3FF;
513 uint16 flags = (SATR3);
514 bool cgmode = SATR2 & 0x1;
515
516 uint32 width = ((flags >> 8) & 1);
517 flags &= ~0x100;
518
519 height = sprite_height_tab[(flags >> 12) & 3];
520 no &= sprite_height_no_mask[(flags >> 12) & 3];
521
522 no = ((no & ~width) | 0) ^ ((flags & SPRF_HFLIP) ? width : 0);
523
524 sat_ptr->y = y;
525 sat_ptr->height = height;
526 sat_ptr->x = x;
527 sat_ptr->no = no;
528 sat_ptr->flags = flags;
529 sat_ptr->cgmode = cgmode;
530
531 sat_ptr++;
532 vdc->SAT_Cache_Valid++;
533
534 if(width)
535 {
536 no = ((no & ~width) | 1) ^ ((flags & SPRF_HFLIP) ? width : 0);
537 x += 16;
538
539 *sat_ptr = *(sat_ptr - 1);
540
541 sat_ptr->no = no;
542 sat_ptr->x = x;
543
544 sat_ptr++;
545 vdc->SAT_Cache_Valid++;
546 }
547 }
548 }
549
550 static INLINE void DoSATDMA(vdc_t *vdc)
551 {
552 unsigned i;
553 if(vdc->SATB > (VRAM_Size - 0x100))
554 VDC_UNDEFINED("Unmapped VRAM SATB DMA read");
555
556 for(i = 0; i < 256; i++)
557 vdc->SAT[i] = vdc->VRAM[(vdc->SATB + i) & 0xFFFF];
558
559 RebuildSATCache(vdc);
560 }
561
562
563 typedef struct
564 {
565 uint32 x;
566 uint32 flags;
567 uint8 palette_index;
568 uint16 no;
569 uint16 sub_y;
570 } SPRLE;
571
572 #define SPR_HPMASK 0x8000 // High priority bit mask
573
574 // DrawSprites will write up to 0x20 units before the start of the pointer it's passed.
575 static void DrawSprites(vdc_t *vdc, const int32 end, uint16 *spr_linebuf) NO_INLINE;
576 static void DrawSprites(vdc_t *vdc, const int32 end, uint16 *spr_linebuf)
577 {
578 int active_sprites = 0;
579 SPRLE SpriteList[64 * 2]; // (see unlimited_sprites option, *2 to accomodate 32-pixel-width sprites ) //16];
580
581 // First, grab the up to 16(or 128 for unlimited_sprites) sprite units(16xWHATEVER; each 32xWHATEVER sprite counts as 2 sprite units when
582 // rendering a scanline) for this scanline.
583 for(int i = 0; i < vdc->SAT_Cache_Valid; i++)
584 {
585 const SAT_Cache_t *SATR = &vdc->SAT_Cache[i];
586
587 int16 y = SATR->y;
588 uint16 x = SATR->x;
589 uint16 no = SATR->no;
590 const uint16 flags = SATR->flags;
591 const uint8 cgmode = SATR->cgmode;
592 const uint16 height = SATR->height;
593 const uint32 palette_index = (flags & 0xF) << 4;
594
595 uint32 y_offset = vdc->RCRCount - y;
596 if(y_offset < height)
597 {
598 if(active_sprites == 16)
599 {
600 if(vdc->CR & 0x2)
601 {
602 vdc->status |= VDCS_OR;
603 HuC6280_IRQBegin(MDFN_IQIRQ1);
604 VDC_DEBUG("Overflow IRQ");
605 }
606 if(!unlimited_sprites)
607 break;
608 }
609
610 if(flags & SPRF_VFLIP)
611 y_offset = height - 1 - y_offset;
612
613 no |= (y_offset & 0x30) >> 3;
614
615 SpriteList[active_sprites].flags = flags;
616
617 //printf("Found: %d %d\n", vdc->RCRCount, x);
618 SpriteList[active_sprites].x = x;
619 SpriteList[active_sprites].palette_index = palette_index;
620
621 SpriteList[active_sprites].no = no;
622 SpriteList[active_sprites].sub_y = (y_offset & 15);
623
624
625 CheckFixSpriteTileCache(vdc, no, (vdc->MWR & 0xC) | cgmode);
626
627 SpriteList[active_sprites].flags |= i ? 0 : SPRF_SPRITE0;
628
629 active_sprites++;
630 }
631 }
632
633 //if(!active_sprites)
634 // return;
635
636 #if 0
637 memset(spr_linebuf, 0, sizeof(uint16) * end);
638 #else
639 MDFN_FastU32MemsetM8((uint32 *)spr_linebuf, 0, ((end + 3) >> 1) & ~1);
640 #endif
641
642 if(!active_sprites)
643 return;
644
645 for(int i = (active_sprites - 1) ; i >= 0; i--)
646 {
647 int32 pos = SpriteList[i].x - 0x20;
648 uint32 prio_or;
649 uint16 *dest_pix;
650
651 if(pos > end)
652 continue;
653
654 dest_pix = &spr_linebuf[pos];
655
656 prio_or = 0x100 | SpriteList[i].palette_index;
657
658 if(SpriteList[i].flags & SPRF_PRIORITY)
659 prio_or |= SPR_HPMASK;
660
661 const uint8 *pix_source = vdc->spr_tile_cache[SpriteList[i].no][SpriteList[i].sub_y];
662 int increment = -1;
663 int32 x_second = 15;
664
665 if(SpriteList[i].flags & SPRF_HFLIP)
666 {
667 increment = 1;
668 x_second = 0;
669 }
670
671 if((SpriteList[i].flags & SPRF_SPRITE0) && (vdc->CR & 0x01))
672 {
673 for(int32 x = 0; x < 16; x++, x_second += increment)
674 {
675 const uint32 raw_pixel = pix_source[x_second];
676 if(raw_pixel)
677 {
678 if(((uint32)pos + x) >= (uint32)end) // Covers negative and overflowing the right side(to prevent spurious sprite hits)
679 continue;
680
681 if(dest_pix[x] & 0x100)
682 {
683 vdc->status |= VDCS_CR;
684 VDC_DEBUG("Sprite hit IRQ");
685 HuC6280_IRQBegin(MDFN_IQIRQ1);
686 }
687 dest_pix[x] = raw_pixel | prio_or;
688 }
689 }
690 } // End sprite0 handling
691 else // No sprite0 hit:
692 {
693 // x must be signed, for "pos + x" to not be promoted to unsigned, which will cause a stack overflow.
694 //
695
696 for(int32 x = 0; x < 16; x++, x_second += increment)
697 {
698 const uint32 raw_pixel = pix_source[x_second];
699 if(raw_pixel)
700 dest_pix[x] = raw_pixel | prio_or;
701 }
702
703 } // End no sprite0 hit
704 }
705 }
706
707 static INLINE void MixBGSPR(const uint32 count_in, const uint8 *bg_linebuf_in, const uint16 *spr_linebuf_in, uint16_t *target_in)
708 {
709 for(unsigned int x = 0; x < count_in; x++)
710 {
711 const uint32 bg_pixel = bg_linebuf_in[x];
712 const uint32 spr_pixel = spr_linebuf_in[x];
713 uint32 pixel = bg_pixel;
714
715 if ((spr_pixel & SPR_HPMASK) || !(bg_pixel & 0x0F))
716 pixel = spr_pixel;
717
718 target_in[x] = vce.color_table_cache[pixel & 0x1FF];
719 }
720 }
721
722 static void MixBGOnly(const uint32 count, const uint8 *bg_linebuf, uint16_t *target)
723 {
724 unsigned x;
725
726 for(x = 0; x < count; x++)
727 target[x] = vce.color_table_cache[bg_linebuf[x]];
728 }
729
730 static void MixSPROnly(const uint32 count, const uint16 *spr_linebuf, uint16_t *target)
731 {
732 unsigned x;
733
734 for(x = 0; x < count; x++)
735 target[x] = vce.color_table_cache[(spr_linebuf[x] | 0x100) & 0x1FF];
736 }
737
738 static void MixNone(const uint32 count, uint16_t *target)
739 {
740 unsigned x;
741 uint32 bg_color = vce.color_table_cache[0x000];
742
743 for(x = 0; x < count; x++)
744 target[x] = bg_color;
745 }
746
747 void DrawOverscan(const vdc_t *vdc, uint16_t *target, const MDFN_Rect *lw, const bool full = true, const int32 vpl = 0, const int32 vpr = 0)
748 {
749 uint32 os_color = vce.color_table_cache[0x100];
750
751 //printf("%d %d\n", lw->x, lw->w);
752 int x = lw->x;
753
754 if(!full)
755 {
756 for(; x < vpl; x++)
757 target[x] = os_color;
758
759 x = vpr;
760 }
761
762 for(; x < lw->x + lw->w; x++)
763 target[x] = os_color;
764 }
765
766 void VDC_RunFrame(EmulateSpecStruct *espec, bool IsHES)
767 {
768 int max_dc = 0;
769 MDFN_Surface *surface = espec->surface;
770 MDFN_Rect *DisplayRect = &espec->DisplayRect;
771 int32 *LineWidths = espec->LineWidths;
772 bool skip = espec->skip || IsHES;
773
774 // x and w should be overwritten in the big loop
775
776 if(!skip)
777 {
778 DisplayRect->x = 0;
779 DisplayRect->w = 256;
780
781 DisplayRect->y = MDFN_GetSettingUI("pce_fast.slstart");
782 DisplayRect->h = MDFN_GetSettingUI("pce_fast.slend") - DisplayRect->y + 1;
783 }
784
785 do
786 {
787 const bool SHOULD_DRAW = (!skip && (int)frame_counter >= (DisplayRect->y + 14) && (int)frame_counter < (DisplayRect->y + DisplayRect->h + 14));
788
789 if(frame_counter == 0)
790 {
791 VDS = M_vdc_VDS;
792 VSW = M_vdc_VSW;
793 VDW = M_vdc_VDW;
794 VCR = M_vdc_VCR;
795 VBlankFL = VDS + VSW + VDW + 1;
796
797 if(VBlankFL > 261)
798 VBlankFL = 261;
799 }
800
801 need_vbi[0] = need_vbi[1] = 0;
802
803 #if 1
804 line_leadin1 = 0;
805
806 magical = M_vdc_HDS + (M_vdc_HDW + 1) + M_vdc_HDE;
807 magical = (magical + 2) & ~1;
808 magical -= M_vdc_HDW + 1;
809 cyc_tot = magical * 8; //ClockPixelWidths[vce.dot_clock] - magical * 8;
810 cyc_tot-=2;
811 switch(vce.dot_clock)
812 {
813 case 0: cyc_tot = 4 * cyc_tot / 3; break;
814 case 1: break;
815 case 2: cyc_tot = 2 * cyc_tot / 3; break;
816 }
817
818 if(cyc_tot < 0) cyc_tot = 0;
819 line_leadin1 = cyc_tot;
820 #endif
821
822 #if 0
823 {
824 int vdc_to_master = 4;
825
826 line_leadin1 = 1365 - ((M_vdc_HDW + 1) * 8 - 4 + 6) * vdc_to_master;
827
828 if(line_leadin1 < 0)
829 {
830 line_leadin1 = 0;
831 puts("Eep");
832 }
833
834 if(M_vdc_HDS > 2)
835 line_leadin1 += 2;
836
837 line_leadin1 = line_leadin1 / 3;
838 }
839
840 if(line_leadin1 < 0)
841 line_leadin1 = 0;
842 else if(line_leadin1 > 400)
843 line_leadin1 = 400;
844 #endif
845
846 //printf("%d\n", line_leadin1);
847 if(max_dc < vce.dot_clock)
848 max_dc = vce.dot_clock;
849
850 if(!skip)
851 {
852 static const int ws[2][3] = {
853 { 341, 341, 682 },
854 { 256, 341, 512 }
855 };
856
857 DisplayRect->x = 0;
858 DisplayRect->w = ws[correct_aspect][vce.dot_clock];
859 }
860
861 int chip = 0;
862 {
863 if(frame_counter == 0)
864 {
865 vdc->display_counter = 0;
866 vdc->burst_mode = !(vdc->CR & 0xC0);
867 }
868
869 if(vdc->display_counter == (VDS + VSW))
870 {
871 vdc->burst_mode = !(vdc->CR & 0xC0);
872 vdc->RCRCount = 0;
873 }
874 int have_free_time = 1;
875
876 if(!vdc->burst_mode && vdc->display_counter >= (VDS + VSW) && vdc->display_counter < (VDS + VSW + VDW + 1))
877 have_free_time = 0;
878
879 if(have_free_time) // We're outside of the active display area. Weehee
880 {
881 if(vdc->DMARunning)
882 DoDMA(vdc);
883 }
884
885 if(vdc->display_counter == VBlankFL)
886 {
887 need_vbi[0] = 1;
888 if(vdc->SATBPending || (vdc->DCR & 0x10))
889 {
890 vdc->SATBPending = 0;
891 vdc->sat_dma_slcounter = 2;
892
893 DoSATDMA(vdc);
894 }
895 }
896 if((int)vdc->RCRCount == ((int)vdc->RCR - 0x40) && (vdc->CR & 0x04))
897 {
898 VDC_DEBUG("RCR IRQ");
899 vdc->status |= VDCS_RR;
900 HuC6280_IRQBegin(MDFN_IQIRQ1);
901 }
902 }
903
904 HuC6280_Run(line_leadin1);
905
906 const bool fc_vrm = (frame_counter >= 14 && frame_counter < (14 + 242));
907
908 chip = 0;
909 {
910 MDFN_ALIGN(8) uint8 bg_linebuf[8 + 1024];
911 MDFN_ALIGN(8) uint16 spr_linebuf[16 + 1024];
912
913 uint16 *target_ptr16 = surface->pixels + (frame_counter - 14) * surface->pitch;
914
915 if(fc_vrm && !skip)
916 LineWidths[frame_counter - 14] = DisplayRect->w;
917
918 if(vdc->burst_mode)
919 {
920 if(fc_vrm && SHOULD_DRAW)
921 {
922 DrawOverscan(vdc, target_ptr16, DisplayRect);
923 }
924 }
925 else if(vdc->display_counter >= (VDS + VSW) && vdc->display_counter < (VDS + VSW + VDW + 1))
926 {
927 if(vdc->display_counter == (VDS + VSW))
928 vdc->BG_YOffset = vdc->BYR;
929 else
930 vdc->BG_YOffset++;
931 vdc->BG_XOffset = vdc->BXR;
932
933 if(fc_vrm)
934 {
935 uint32 start, end;
936
937 CalcStartEnd(vdc, start, end);
938
939 if((vdc->CR & 0x80) && SHOULD_DRAW)
940 {
941 if(userle & (ULE_BG0))
942 DrawBG(vdc, end - start + (vdc->BG_XOffset & 7), bg_linebuf);
943 else
944 memset(bg_linebuf, 0, end - start + (vdc->BG_XOffset & 7));
945 }
946
947 if((vdc->CR & 0x40) && (SHOULD_DRAW || (vdc->CR & 0x03))) // Don't skip sprite drawing if we can generate sprite #0 or sprite overflow IRQs.
948 {
949 if((userle & (ULE_SPR0)) || (vdc->CR & 0x03))
950 DrawSprites(vdc, end - start, spr_linebuf + 0x20);
951
952 if(!(userle & (ULE_SPR0)))
953 memset(spr_linebuf + 0x20, 0, sizeof(uint16) * (end - start));
954 }
955
956 if(SHOULD_DRAW)
957 {
958 static const int xs[2][3] = {
959 { 24 - 43, 38, 96 - 43 * 2 },
960 { 24, 38, 96 }
961 };
962
963 int32 width = end - start;
964 int32 source_offset = 0;
965 int32 target_offset = start - (128 + 8 + xs[correct_aspect][vce.dot_clock]);
966
967 if(target_offset < 0)
968 {
969 width += target_offset;
970 source_offset += 0 - target_offset;
971 target_offset = 0;
972 }
973
974 if((target_offset + width) > DisplayRect->w)
975 width = (int32)DisplayRect->w - target_offset;
976
977 //if(vdc->display_counter == 50)
978 // MDFN_DispMessage("soffset=%d, toffset=%d, width=%d", source_offset, target_offset, width);
979
980 if(width > 0)
981 {
982 //else if(target_ptr16)
983 {
984 switch(vdc->CR & 0xC0)
985 {
986 case 0xC0:
987 MixBGSPR(width, bg_linebuf + (vdc->BG_XOffset & 7) + source_offset, spr_linebuf + 0x20 + source_offset, target_ptr16 + target_offset);
988 break;
989 case 0x80:
990 MixBGOnly(width, bg_linebuf + (vdc->BG_XOffset & 7) + source_offset, target_ptr16 + target_offset);
991 break;
992 case 0x40:
993 MixSPROnly(width, spr_linebuf + 0x20 + source_offset, target_ptr16 + target_offset);
994 break;
995 case 0x00:
996 MixNone(width, target_ptr16 + target_offset);
997 break;
998 }
999 }
1000 }
1001
1002 //else if(target_ptr16)
1003 DrawOverscan(vdc, target_ptr16, DisplayRect, false, target_offset, target_offset + width);
1004 } // end if(SHOULD_DRAW)
1005 }
1006 }
1007 else if(SHOULD_DRAW && fc_vrm) // Hmm, overscan...
1008 {
1009 //else if(target_ptr16)
1010 DrawOverscan(vdc, target_ptr16, DisplayRect);
1011 }
1012 }
1013
1014 if(SHOULD_DRAW && fc_vrm)
1015 {
1016 MDFN_MidLineUpdate(espec, frame_counter - 14);
1017 }
1018
1019 if((vdc->CR & 0x08) && need_vbi[0])
1020 vdc->status |= VDCS_VD;
1021
1022 HuC6280_Run(2);
1023
1024 if(vdc->status & VDCS_VD)
1025 {
1026 VDC_DEBUG("VBlank IRQ");
1027 HuC6280_IRQBegin(MDFN_IQIRQ1);
1028 }
1029
1030 HuC6280_Run(455 - line_leadin1 - 2);
1031
1032 if(PCE_IsCD)
1033 {
1034 PCECD_Run(HuCPU.timestamp * 3);
1035 }
1036
1037 {
1038 vdc->RCRCount++;
1039
1040 //vdc->BG_YOffset = (vdc->BG_YOffset + 1);
1041 vdc->display_counter++;
1042
1043 if(vdc->sat_dma_slcounter)
1044 {
1045 vdc->sat_dma_slcounter--;
1046 if(!vdc->sat_dma_slcounter)
1047 {
1048 if(vdc->DCR & 0x01)
1049 {
1050 VDC_DEBUG("Sprite DMA IRQ");
1051 vdc->status |= VDCS_DS;
1052 HuC6280_IRQBegin(MDFN_IQIRQ1);
1053 }
1054 }
1055 }
1056
1057 if(vdc->display_counter == (VDS + VSW + VDW + VCR + 3))
1058 {
1059 vdc->display_counter = 0;
1060 }
1061 }
1062
1063 frame_counter = (frame_counter + 1) % (vce.lc263 ? 263 : 262);
1064 //printf("%d\n", vce.lc263);
1065 } while(frame_counter != VBlankFL); // big frame loop!
1066
1067 }
1068
1069 void VDC_Reset(void)
1070 {
1071 vdc->read_buffer = 0xFFFF;
1072
1073 vpc.priority[0] = vpc.priority[1] = 0x11;
1074
1075 vdc->HSR = vdc->HDR = vdc->VSR = vdc->VDR = vdc->VCR = 0xFF; // Needed for Body Conquest 2
1076
1077 frame_counter = 0;
1078 }
1079
1080 void VDC_Power(void)
1081 {
1082 memset(vdc, 0, sizeof(vdc_t));
1083 VDC_Reset();
1084 }
1085
1086 void VDC_Init(int sgx)
1087 {
1088 unlimited_sprites = MDFN_GetSettingB("pce_fast.nospritelimit");
1089 correct_aspect = MDFN_GetSettingB("pce_fast.correct_aspect");
1090 userle = ~0;
1091
1092 vdc = (vdc_t *)malloc(sizeof(vdc_t));
1093 }
1094
1095 void VDC_Close(void)
1096 {
1097 if(vdc)
1098 free(vdc);
1099 vdc = NULL;
1100 }
1101
1102 int VDC_StateAction(StateMem *sm, int load, int data_only)
1103 {
1104 SFORMAT VCE_StateRegs[] =
1105 {
1106 SFVARN(vce.CR, "VCECR"),
1107 SFVARN(vce.lc263, "lc263"),
1108 SFVARN(vce.bw, "bw"),
1109 SFVARN(vce.dot_clock, "dot clock"),
1110 SFVARN(vce.ctaddress, "ctaddress"),
1111 SFARRAY16N(vce.color_table, 0x200, "color_table"),
1112 SFEND
1113 };
1114
1115
1116 int ret = MDFNSS_StateAction(sm, load, data_only, VCE_StateRegs, "VCE");
1117
1118 int chip = 0;
1119 {
1120 SFORMAT VDC_StateRegs[] =
1121 {
1122 SFVARN(vdc->display_counter, "display_counter"),
1123 SFVARN(vdc->sat_dma_slcounter, "sat_dma_slcounter"),
1124
1125 SFVARN(vdc->select, "select"),
1126 SFVARN(vdc->MAWR, "MAWR"),
1127 SFVARN(vdc->MARR, "MARR"),
1128 SFVARN(vdc->CR, "CR"),
1129 SFVARN(vdc->RCR, "RCR"),
1130 SFVARN(vdc->BXR, "BXR"),
1131 SFVARN(vdc->BYR, "BYR"),
1132 SFVARN(vdc->MWR, "MWR"),
1133
1134 SFVARN(vdc->HSR, "HSR"),
1135 SFVARN(vdc->HDR, "HDR"),
1136 SFVARN(vdc->VSR, "VSR"),
1137 SFVARN(vdc->VDR, "VDR"),
1138
1139 SFVARN(vdc->VCR, "VCR"),
1140 SFVARN(vdc->DCR, "DCR"),
1141 SFVARN(vdc->SOUR, "SOUR"),
1142 SFVARN(vdc->DESR, "DESR"),
1143 SFVARN(vdc->LENR, "LENR"),
1144 SFVARN(vdc->SATB, "SATB"),
1145
1146 SFVARN(vdc->RCRCount, "RCRCount"),
1147
1148
1149 SFVARN(vdc->read_buffer, "read_buffer"),
1150 SFVARN(vdc->write_latch, "write_latch"),
1151 SFVARN(vdc->status, "status"),
1152
1153 SFARRAY16N(vdc->SAT, 0x100, "SAT"),
1154
1155 SFARRAY16N(vdc->VRAM, VRAM_Size, "VRAM"),
1156 SFVARN(vdc->DMAReadBuffer, "DMAReadBuffer"),
1157 SFVARN(vdc->DMAReadWrite, "DMAReadWrite"),
1158 SFVARN(vdc->DMARunning, "DMARunning"),
1159 SFVARN(vdc->SATBPending, "SATBPending"),
1160 SFVARN(vdc->burst_mode, "burst_mode"),
1161
1162 SFVARN(vdc->BG_YOffset, "BG_YOffset"),
1163 SFVARN(vdc->BG_XOffset, "BG_XOffset"),
1164 SFVAR(frame_counter),
1165 SFVARN(VDS, "VDS_cache"),
1166 SFVARN(VSW, "VSW_cache"),
1167 SFVARN(VDW, "VDW_cache"),
1168 SFVARN(VCR, "VCR_cache"),
1169 SFVARN(VBlankFL, "VBlankFL_cache"),
1170
1171 SFARRAY32(need_vbi, 2),
1172 SFVAR(line_leadin1),
1173 SFVAR(magical),
1174 SFVAR(cyc_tot),
1175 SFEND
1176 };
1177
1178 ret &= MDFNSS_StateAction(sm, load, data_only, VDC_StateRegs, "VDC0");
1179
1180 if(load)
1181 {
1182 for(int x = 0; x < VRAM_Size; x++)
1183 {
1184 FixTileCache(vdc, x);
1185 vdc->spr_tile_clean[x >> 6] = 0;
1186 }
1187 for(int x = 0; x < 512; x++)
1188 FixPCache(x);
1189 RebuildSATCache(vdc);
1190 }
1191
1192 }
1193
1194 return(ret);
1195 }