-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVICII.cpp
More file actions
931 lines (792 loc) · 26.8 KB
/
Copy pathVICII.cpp
File metadata and controls
931 lines (792 loc) · 26.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
#include "VICII.h"
#include <cstring> // memset/memcpy if used
#include <cmath> // if abs/math is used in the implementation
/*
I'll call this VIC-II code about 90% complete for now.
There are a few timing issues left to debug, and I want to architect
real sprite DMA fetches rather than the current simplified model.
And Mode specific resolutions with different window and framebuffer sizes
Next Full Release:
TODO: VICII.h - Mode-specific constants for visible heights (roughly 232 NTSC / 284 PAL)
VICII class - Dynamic frame buffer allocation or separate buffers
WindowCode.cpp - Texture recreation on mode switch, window sizing per mode
Aspect ratio handling - PAL and NTSC have different pixel aspect ratios and different visible line counts
*/
// =============================================================================
// VIC-II Debug Logging (enable with #define VIC_DEBUG before including)
// =============================================================================
#ifdef VIC_DEBUG
#define VIC_LOG_SPRITE(spr, fmt, ...) \
if (debug_sprites && (debug_sprite_num < 0 || debug_sprite_num == spr)) \
LOG_DEBUG("[VIC SPR%d] " fmt, spr, ##__VA_ARGS__)
#define VIC_LOG_RASTER(fmt, ...) \
if (debug_raster) LOG_DEBUG("[VIC RAST] " fmt, ##__VA_ARGS__)
#else
#define VIC_LOG_SPRITE(spr, fmt, ...) ((void)0)
#define VIC_LOG_RASTER(fmt, ...) ((void)0)
#endif
const uint32_t C64_PALETTE_PAL[16] = {
0xFF000000, // 0 Black
0xFFFFFFFF, // 1 White
0xFF68372B, // 2 Red
0xFF70A4B2, // 3 Cyan
0xFF6F3D86, // 4 Purple
0xFF588D43, // 5 Green
0xFF352879, // 6 Blue
0xFFB8C76F, // 7 Yellow
0xFF6F4F25, // 8 Orange
0xFF433900, // 9 Brown
0xFF9A6759, // 10 Light Red
0xFF444444, // 11 Dark Grey
0xFF6C6C6C, // 12 Grey
0xFF9AD284, // 13 Light Green
0xFF6C5EB5, // 14 Light Blue
0xFF959595 // 15 Light Grey
};
const uint32_t C64_PALETTE_NTSC[16] = {
0xFF000000, // 0 Black
0xFFFFFFFF, // 1 White
0xFF7C352B, // 2 Red
0xFF5AA6B1, // 3 Cyan
0xFF694185, // 4 Purple
0xFF5D8643, // 5 Green
0xFF212E78, // 6 Blue
0xFFCFBE6F, // 7 Yellow
0xFF894A26, // 8 Orange
0xFF5B3300, // 9 Brown
0xFFAF6459, // 10 Light Red
0xFF434343, // 11 Dark Grey
0xFF6B6B6B, // 12 Grey
0xFFA0CB84, // 13 Light Green
0xFF5665B3, // 14 Light Blue
0xFF959595 // 15 Light Grey
};
// Default to NTSC palette since your Bus defaults to NTSC timing.
const uint32_t* C64_PALETTE = C64_PALETTE_NTSC;
// -----------------------------------------------------------------------------
// C64_SetActivePalette
// Selects which palette C64_PALETTE points to.
// -----------------------------------------------------------------------------
void C64_SetActivePalette(bool pal) {
C64_PALETTE = pal ? C64_PALETTE_PAL : C64_PALETTE_NTSC;
}
// =============================================================================
// VIC-II Implementation
// =============================================================================
VIC_II::VIC_II() {
ram = nullptr; char_rom = nullptr; color_ram = nullptr;
set_pal(false);
reset();
}
void VIC_II::set_memory(uint8_t* ram_ptr, uint8_t* char_rom_ptr, uint8_t* color_ram_ptr) {
ram = ram_ptr; char_rom = char_rom_ptr; color_ram = color_ram_ptr;
}
void VIC_II::set_pal(bool pal) {
is_pal = pal;
cycles_per_line = pal ? 63 : 65;
lines_per_frame = pal ? 312 : 263;
}
void VIC_II::set_vic_bank(uint16_t bank) {
vic_bank_addr = bank;
}
void VIC_II::reset() {
std::memset(regs, 0, sizeof(regs));
std::memset(frame_buffer, 0, sizeof(frame_buffer));
std::memset(sprites, 0, sizeof(sprites));
std::memset(line_buffer, 0, sizeof(line_buffer));
// NOTE: Do NOT reset x/y_output_offset_pixels here.
// They are configured by the Bus/System during init.
raster_x = 0;
raster_y = 0;
raster_irq = 0;
// Registers defaults
regs[0x19] = 0x70; // IRR
regs[0x1A] = 0xF0; // IMR
vc = 0; vc_base = 0; rc = 0; vmli = 0;
vic_bank_addr = 0x0000;
bad_line = false;
bad_line_enable = false;
display_state = false;
idle_state = true;
ba_low = false;
// Scanline 0 is in the VBLANK/Border area, so the frame must start with border enabled.
main_border_ff = true;
vertical_border_ff = true;
c_data = 0;
g_data = 0;
gfx_shift = 0;
irq_raster = false;
irq_sprite_sprite = false;
irq_sprite_data = false;
irq_lightpen = false;
}
// -----------------------------------------------------------------------------
// Register Access
// -----------------------------------------------------------------------------
uint8_t VIC_II::read(uint8_t reg) {
reg &= 0x3F;
if (reg == RASTER) return raster_y & 0xFF;
if (reg == CR1) return (regs[CR1] & 0x7F) | ((raster_y & 0x100) >> 1);
// Collision clear on read
if (reg == SSCOL) { uint8_t t = regs[SSCOL]; regs[SSCOL] = 0; return t; }
if (reg == SDCOL) { uint8_t t = regs[SDCOL]; regs[SDCOL] = 0; return t; }
if (reg >= 0x2F && reg <= 0x3F) return 0xFF;
return regs[reg];
}
void VIC_II::write(uint8_t reg, uint8_t data)
{
reg &= 0x3F;
if (reg == RASTER) {
// Low 8 bits of raster IRQ compare
raster_irq = (raster_irq & 0x100) | data;
return;
}
if (reg == CR1) {
regs[CR1] = data;
// Bit 7 of CR1 is raster IRQ MSB (RASTER9) mirror.
if (data & 0x80) raster_irq |= 0x100;
else raster_irq &= 0x0FF;
return;
}
if (reg == IRR) {
// Acknowledge interrupts
if (data & 0x01) irq_raster = false;
if (data & 0x02) irq_sprite_data = false;
if (data & 0x04) irq_sprite_sprite = false;
if (data & 0x08) irq_lightpen = false;
update_interrupts();
return;
}
if (reg == IMR) {
regs[IMR] = data & 0x0F;
update_interrupts();
return;
}
if (reg < 0x30) {
regs[reg] = data;
// Any sprite-related register write should immediately sync our sprite units.
if ((reg <= SP7Y) || (reg == MSIGX) || (reg == SPENA) || (reg == SPYEX) || (reg == SPDP) ||
(reg == SPMC) || (reg == SPXEX) || (reg >= SP0C && reg <= SP7C)) {
sync_sprites_from_regs();
}
}
}
void VIC_II::update_interrupts() {
uint8_t active = 0;
if (irq_raster) active |= 0x01;
if (irq_sprite_data) active |= 0x02;
if (irq_sprite_sprite) active |= 0x04;
if (irq_lightpen) active |= 0x08;
regs[IRR] = active | 0x70;
if (active & (regs[IMR] & 0x0F)) {
regs[IRR] |= 0x80;
if (irq_callback) irq_callback(true);
}
else {
if (irq_callback) irq_callback(false);
}
}
// -----------------------------------------------------------------------------
// Core Cycle Execution
// -----------------------------------------------------------------------------
bool VIC_II::execute_cycle()
{
bool frame_complete = false;
// -------------------------------------------------------------------------
// Start-of-line: Latch registers and update state
// -------------------------------------------------------------------------
if (raster_x == 0)
{
// 1. Latch sprite configuration
uint8_t ena = regs[SPENA];
uint8_t xmsb = regs[MSIGX];
uint8_t yexp = regs[SPYEX];
uint8_t xexp = regs[SPXEX];
uint8_t mcm = regs[SPMC];
uint8_t prio = regs[SPDP];
for (int s = 0; s < 8; ++s)
{
sprites[s].enable = (ena & (1 << s)) != 0;
uint16_t sx = regs[SP0X + s * 2];
if (xmsb & (1 << s)) sx |= 0x100;
sprites[s].x = sx;
sprites[s].y = regs[SP0Y + s * 2];
sprites[s].y_expand = (yexp & (1 << s)) != 0;
sprites[s].x_expand = (xexp & (1 << s)) != 0;
sprites[s].multicolor = (mcm & (1 << s)) != 0;
sprites[s].priority = (prio & (1 << s)) != 0;
sprites[s].color = regs[SP0C + s] & 0x0F;
if (!sprites[s].y_expand) sprites[s].expand_ff = true;
if (s <= 2) sprites[s].tile = sprites[s].tile_next;
}
// 2. Check Bad Line Condition (occurs at start of line)
const bool den = (regs[CR1] & 0x10) != 0;
const bool in_badline_window = (raster_y >= 0x30 && raster_y <= 0xF7);
const bool yscroll_match = ((raster_y & 7) == (regs[CR1] & 7));
bad_line = den && in_badline_window && yscroll_match;
// 3. State Transition: Idle -> Display
// The transition occurs as soon as there is a Bad Line Condition.
if (bad_line) {
display_state = true;
idle_state = false;
}
}
// -------------------------------------------------------------------------
// Raster IRQ compare
// -------------------------------------------------------------------------
if (raster_x == 1) {
if (raster_y == raster_irq) {
irq_raster = true;
update_interrupts();
}
}
// -------------------------------------------------------------------------
// Border Logic
// -------------------------------------------------------------------------
check_border_horizontal();
// -------------------------------------------------------------------------
// BA Signal Logic (Stuns CPU on Bad Lines)
// -------------------------------------------------------------------------
ba_low = false;
if (bad_line) {
if (raster_x >= 12 && raster_x <= 54) ba_low = true;
}
// -------------------------------------------------------------------------
// Cycle 14: VC reset/update
// -------------------------------------------------------------------------
if (raster_x == 14) {
vc = vc_base;
vmli = 0;
if (bad_line) {
rc = 0;
idle_state = false; // Ensure sequencer is active
}
}
// -------------------------------------------------------------------------
// Sprite Rule 7 (Cycle 16)
// -------------------------------------------------------------------------
if (raster_x == 15)
{
for (int s = 0; s < 8; ++s) {
if (!sprites[s].dma) continue;
if (sprites[s].expand_ff) {
sprites[s].mc_base = sprites[s].mc;
if ((sprites[s].mc_base & 0x3F) == 63) {
sprites[s].dma = false;
sprites[s].display = false;
}
}
}
}
// -------------------------------------------------------------------------
// Sprite Rules 2 & 3 (Cycle 55/56)
// -------------------------------------------------------------------------
if (raster_x == 54 || raster_x == 55)
{
uint8_t ry = (uint8_t)(raster_y & 0xFF);
for (int s = 0; s < 8; ++s) {
if (!sprites[s].enable) continue;
if (!sprites[s].dma && (sprites[s].y == ry)) {
sprites[s].dma = true;
sprites[s].mc_base = 0;
sprites[s].mc = 0;
sprites[s].pattern_data = 0;
sprites[s].shift_register = 0;
sprites[s].expand_ff = true;
sprites[s].display = true;
}
}
if (raster_x == 55) {
for (int s = 0; s < 8; ++s) {
if (sprites[s].dma && sprites[s].y_expand) {
sprites[s].expand_ff = !sprites[s].expand_ff;
}
if (!sprites[s].y_expand) {
sprites[s].expand_ff = true;
}
}
}
}
// -------------------------------------------------------------------------
// Graphics Fetch (Cycles 16-55)
// -------------------------------------------------------------------------
if (raster_x >= 16 && raster_x <= 55)
{
// Only fetch/increment if in display state.
// This prevents VC from incrementing incorrectly during the border/idle lines.
if (display_state) {
fetch_matrix();
fetch_graphics();
}
else {
// In a cycle-exact emulation we might fetch garbage here,
// but for rendering purposes doing nothing is sufficient
// as draw_pixel handles the idle state logic.
}
}
// -------------------------------------------------------------------------
// Sprite Fetch Schedule
// -------------------------------------------------------------------------
if (raster_x == 0) { fetch_sprite(0, 1); }
if (raster_x == 1) { fetch_sprite(1, 1); }
if (raster_x == 2) { fetch_sprite(2, 1); fetch_sprite(3, 0); }
if (raster_x == 3) { fetch_sprite(3, 1); }
if (raster_x == 4) { fetch_sprite(4, 0); }
if (raster_x == 5) { fetch_sprite(4, 1); }
if (raster_x == 6) { fetch_sprite(5, 0); }
if (raster_x == 7) { fetch_sprite(5, 1); }
if (raster_x == 8) { fetch_sprite(6, 0); }
if (raster_x == 9) { fetch_sprite(6, 1); }
if (raster_x == 10) { fetch_sprite(7, 0); }
if (raster_x == 11) { fetch_sprite(7, 1); }
if (raster_x == 59) { fetch_sprite(0, 0); }
if (raster_x == 60) { fetch_sprite(1, 0); }
if (raster_x == 61) { fetch_sprite(2, 0); }
// -------------------------------------------------------------------------
// Render Pixel
// -------------------------------------------------------------------------
draw_pixel();
// -------------------------------------------------------------------------
// Cycle 58: End of Line State Update
// -------------------------------------------------------------------------
if (raster_x == 58) {
if (display_state) {
if (rc == 7) {
vc_base = vc;
// Transition to Idle if this wasn't a Bad Line
if (!bad_line) {
display_state = false;
idle_state = true;
}
}
rc = (rc + 1) & 7;
}
}
// -------------------------------------------------------------------------
// End of Line
// -------------------------------------------------------------------------
raster_x++;
if (raster_x >= cycles_per_line) {
raster_x = 0;
raster_y++;
check_border_vertical();
if (raster_y >= lines_per_frame) {
// ---------------------------------------------------------------------
// Fill any unused buffer lines with border color
// This is needed for NTSC where the frame has fewer lines than the
// buffer height. Without this, unused lines remain black.
// ---------------------------------------------------------------------
int last_rendered_y = (lines_per_frame - 1) - y_output_offset_pixels;
if (last_rendered_y < C64_SCREEN_HEIGHT - 1) {
uint32_t border_color = C64_PALETTE[regs[EC] & 0x0F];
for (int fill_y = last_rendered_y + 1; fill_y < C64_SCREEN_HEIGHT; ++fill_y) {
for (int fill_x = 0; fill_x < C64_SCREEN_WIDTH; ++fill_x) {
frame_buffer[fill_y * C64_SCREEN_WIDTH + fill_x] = border_color;
}
}
}
raster_y = 0;
vc_base = 0;
rc = 0;
vmli = 0;
idle_state = true;
frame_complete = true;
// Reset state for new frame
bad_line = false;
display_state = false;
}
}
return frame_complete;
}
// -----------------------------------------------------------------------------
// Pipeline Operations
// -----------------------------------------------------------------------------
uint8_t VIC_II::read_byte(uint16_t addr) {
uint16_t full_addr = (vic_bank_addr + addr) & 0xFFFF;
// Char ROM phantom mapping (Banks 0/2 at 0x1000-0x1FFF)
if (char_rom && (vic_bank_addr & 0x7000) == 0 && (addr & 0x7000) == 0x1000) {
return char_rom[addr & 0x0FFF];
}
return ram[full_addr];
}
void VIC_II::fetch_matrix() {
// C-Access: Fetch Character Pointer / Color
// Occurs every cycle in display range, but data is only LATCHED if bad_line is active.
// If not bad line, we use buffered data from `line_buffer`.
if (bad_line) {
uint16_t screen_base = ((regs[MEMPTR] & 0xF0) << 6); // x64 -> x1024
uint16_t addr = screen_base + vc;
uint8_t char_code = read_byte(addr);
uint8_t color = color_ram[addr & 0x03FF] & 0x0F;
// Latch into line buffer
line_buffer[vmli] = (uint16_t(color) << 8) | char_code;
}
// Load internal register from buffer (Bad line or not)
c_data = line_buffer[vmli];
}
void VIC_II::fetch_graphics() {
// G-Access: Fetch Bitmap / Font pixels
uint8_t char_code = c_data & 0xFF;
uint16_t addr = 0;
bool bmm = regs[CR1] & 0x20;
bool ecm = regs[CR1] & 0x40;
if (bmm) {
// Bitmap Mode
uint16_t bitmap_base = ((regs[MEMPTR] & 0x08) << 10);
addr = bitmap_base + (vc * 8) + rc;
}
else {
// Text Mode
uint16_t char_base = ((regs[MEMPTR] & 0x0E) << 10);
if (ecm) {
// ECM uses char code bits to select background color, strips them from addr
addr = char_base + ((char_code & 0x3F) * 8) + rc;
}
else {
addr = char_base + (char_code * 8) + rc;
}
}
g_data = read_byte(addr);
// Advance pointers
vc++;
vmli = (vmli + 1) & 0x3F;
}
void VIC_II::fetch_sprite(int num, int step)
{
// step 0: p-access (pointer fetch)
// step 1: s-access (3 bytes) using MCBASE semantics
//
// IMPORTANT:
// - mc_base is the persistent "row base" counter (0..63)
// - mc is the "next base" value (mc_base + 3) after this line's fetch
// - cycle 16 rule (your raster_x==15 block) commits mc_base = mc when expand_ff is set
// which naturally implements Y-expand (only advances every other line)
if (step == 0)
{
uint16_t screen_base = ((regs[MEMPTR] & 0xF0) << 6);
uint16_t ptr_addr = screen_base + 0x3F8 + num;
uint8_t mp = read_byte(ptr_addr);
// Sprites 0-2: pointer fetch is late; latch to tile_next and copy at raster_x==0 next line
if (num <= 2) {
sprites[num].tile_next = mp;
}
else {
sprites[num].tile = mp;
}
return;
}
// step 1: only fetch sprite data while DMA is active
if (!sprites[num].dma)
return;
// Use MCBASE as the address base for this raster line.
uint8_t base = (uint8_t)(sprites[num].mc_base & 0x3F);
uint16_t addr = uint16_t(sprites[num].tile) * 64u + base;
uint8_t b0 = read_byte(addr + 0);
uint8_t b1 = read_byte(addr + 1);
uint8_t b2 = read_byte(addr + 2);
uint32_t data = (uint32_t(b0) << 16) | (uint32_t(b1) << 8) | uint32_t(b2);
sprites[num].pattern_data = data;
sprites[num].shift_register = data;
// Compute "next MC" for rule 7 commit at cycle 16.
// (Do NOT write mc_base here - that's what expand_ff gating controls.)
sprites[num].mc = (uint8_t)((base + 3) & 0x3F);
}
void VIC_II::sync_sprites_from_regs()
{
const uint8_t en = regs[SPENA];
const uint8_t xexp = regs[SPXEX];
const uint8_t yexp = regs[SPYEX];
const uint8_t mcm = regs[SPMC];
const uint8_t pri = regs[SPDP];
const uint8_t msbx = regs[MSIGX];
for (int i = 0; i < 8; ++i) {
sprites[i].enable = (en >> i) & 1;
sprites[i].x_expand = (xexp >> i) & 1;
sprites[i].y_expand = (yexp >> i) & 1;
sprites[i].multicolor = (mcm >> i) & 1;
sprites[i].priority = (pri >> i) & 1;
uint16_t xlo = regs[SP0X + i * 2];
uint16_t xhi = ((msbx >> i) & 1) ? 0x100 : 0x000;
sprites[i].x = xlo | xhi;
sprites[i].y = regs[SP0Y + i * 2];
sprites[i].color = regs[SP0C + i] & 0x0F;
// Per Bauer: if Y expand bit is cleared, advance-line FF is set.
if (!sprites[i].y_expand) {
sprites[i].expand_ff = true;
}
// If disabled, force off.
if (!sprites[i].enable) {
sprites[i].dma = false;
sprites[i].display = false;
sprites[i].pattern_data = 0;
sprites[i].mc = 0;
sprites[i].mc_base = 0;
}
}
}
// -----------------------------------------------------------------------------
// Pixel Generation & Output
// -----------------------------------------------------------------------------
uint32_t VIC_II::get_pixel_color(uint8_t pix, uint16_t c_val, uint8_t bg_src)
{
// pix:
// - Standard modes: 0 or 1
// - Multicolor modes: 0..3
//
// c_val layout:
// low 8 = screen byte (char code in text mode, color nibbles in bitmap modes)
// high 4 = color RAM nibble (0..15)
const bool mcm = (regs[CR2] & 0x10) != 0; // global multicolor
const bool bmm = (regs[CR1] & 0x20) != 0; // bitmap mode
const bool ecm = (regs[CR1] & 0x40) != 0; // extended color (text mode)
const uint8_t screen_byte = (uint8_t)(c_val & 0xFF);
const uint8_t color_nib = (uint8_t)((c_val >> 8) & 0x0F);
uint8_t color_idx = regs[B0C] & 0x0F;
if (!bmm) {
// Text mode
if (ecm) {
// ECM: background chosen by char code bits 6-7
if (pix) {
color_idx = color_nib;
}
else {
const uint8_t sel = (screen_byte >> 6) & 3;
if (sel == 0) color_idx = regs[B0C] & 0x0F;
if (sel == 1) color_idx = regs[B1C] & 0x0F;
if (sel == 2) color_idx = regs[B2C] & 0x0F;
if (sel == 3) color_idx = regs[B3C] & 0x0F;
}
}
else if (mcm && (color_nib & 0x08)) {
// Multicolor text (only when color RAM bit 3 is set)
// 00 = B0, 01 = B1, 10 = B2, 11 = color_nib[2:0]
const uint8_t v = pix & 3;
if (v == 0) color_idx = regs[B0C] & 0x0F;
if (v == 1) color_idx = regs[B1C] & 0x0F;
if (v == 2) color_idx = regs[B2C] & 0x0F;
if (v == 3) color_idx = color_nib & 0x07;
}
else {
// Standard text
color_idx = pix ? color_nib : (regs[B0C] & 0x0F);
// If global MCM is set but this char is hires, the priority logic treats "1" as foreground.
// Color selection remains hires here.
}
}
else {
// Bitmap mode
// screen_byte provides two nibbles in hires bitmap, or two colors in multicolor bitmap.
const uint8_t hi = (screen_byte >> 4) & 0x0F;
const uint8_t lo = (screen_byte >> 0) & 0x0F;
if (mcm) {
// Multicolor bitmap:
// 00 = B0, 01 = hi, 10 = lo, 11 = color RAM nibble
const uint8_t v = pix & 3;
if (v == 0) color_idx = regs[B0C] & 0x0F;
if (v == 1) color_idx = hi;
if (v == 2) color_idx = lo;
if (v == 3) color_idx = color_nib;
}
else {
// Hires bitmap: 0 = lo, 1 = hi
color_idx = pix ? hi : lo;
}
}
return C64_PALETTE[color_idx & 0x0F];
}
void VIC_II::draw_pixel()
{
// History buffers to handle X-Scroll crossing character boundaries.
static uint8_t last_g_data = 0;
static uint16_t last_c_data = 0;
// Reset history at start of line to prevent wrapping artifacts
if (raster_x == 0) {
last_g_data = 0;
last_c_data = 0;
}
const int base_x = (raster_x * 8);
const int raw_y = raster_y;
// Apply Vertical Output Offset for centering
const int y = raw_y - y_output_offset_pixels;
// Clip Y to the defined screen height
if (y < 0 || y >= C64_SCREEN_HEIGHT) {
// Keep history updated even if clipped
last_g_data = (!idle_state) ? g_data : 0;
last_c_data = c_data;
return;
}
const bool mcm_global = (regs[CR2] & 0x10) != 0;
const int x_scroll = regs[CR2] & 7;
for (int p = 0; p < 8; ++p)
{
// VIC-space X (Internal coordinate)
const int vic_x = base_x + p;
// Output-space X (Screen coordinate)
// FIXED: 'x' is now static relative to the beam.
const int x = vic_x - x_output_offset_pixels;
// Clip X to the defined screen width
if (x < 0 || x >= C64_SCREEN_WIDTH)
continue;
uint32_t final_color = C64_PALETTE[regs[EC] & 0x0F];
const bool is_border = main_border_ff || vertical_border_ff;
if (!is_border)
{
// -----------------------------------------------------------------
// 1) Background graphics (text/bitmap)
// -----------------------------------------------------------------
uint8_t gfx_pix = 0;
bool gfx_is_fg = false;
// SCROLL LOGIC: Data Shifting
// effective_p is the bit index adjusted for scroll delay.
// If negative, we fetch bits from the PREVIOUS cycle (last_g_data).
int effective_p = p - x_scroll;
uint8_t data_byte = 0;
uint16_t color_word = 0;
if (effective_p < 0) {
data_byte = last_g_data;
color_word = last_c_data;
// Wrap index: -1 becomes 7, -2 becomes 6...
effective_p += 8;
}
else {
data_byte = (!idle_state) ? g_data : 0;
color_word = c_data;
}
if (!mcm_global)
{
// Hires: Simple bit extraction (MSB first)
gfx_pix = (data_byte >> (7 - effective_p)) & 1;
gfx_is_fg = (gfx_pix != 0);
}
else
{
// Multicolor: 2-bit pixels (Pairs: 76, 54, 32, 10)
const bool bmm = (regs[CR1] & 0x20) != 0;
const bool ecm = (regs[CR1] & 0x40) != 0;
const uint8_t color_nib = (uint8_t)((color_word >> 8) & 0x0F);
// Check MCM capability for the specific character being drawn
bool is_mcm_char = bmm || (!ecm && (color_nib & 0x08));
if (is_mcm_char) {
// Extract the 2-bit pair corresponding to effective_p.
// effective_p: 0,1 -> Pair 3. 2,3 -> Pair 2. etc.
int shift = ((7 - effective_p) >> 1) * 2;
gfx_pix = (data_byte >> shift) & 3;
gfx_is_fg = (gfx_pix != 0);
}
else {
// Hires char in MCM mode
gfx_pix = (data_byte >> (7 - effective_p)) & 1;
gfx_is_fg = (gfx_pix != 0);
if (gfx_pix == 1) gfx_pix = 2; // Map Hires '1' to FG color logic
}
}
const uint32_t gfx_color = get_pixel_color(gfx_pix, color_word, 0);
// -----------------------------------------------------------------
// 2) Sprites
// -----------------------------------------------------------------
// Sprites use absolute coordinates (vic_x) and are NOT affected by x_scroll directly.
uint8_t top_sprite_color = 0;
bool have_sprite_pixel = false;
bool top_sprite_behind_fg = false;
uint8_t sprite_hit_mask = 0;
for (int s = 0; s < 8; ++s)
{
if (!sprites[s].enable || !sprites[s].display) continue;
// Sprite X alignment correction
const int spr_x = (int)sprites[s].x + 104;
int sx = vic_x - spr_x;
if (sprites[s].x_expand) {
if (sx < 0 || sx >= 48) continue;
sx >>= 1;
}
else {
if (sx < 0 || sx >= 24) continue;
}
uint8_t spr_pix = 0;
if (!sprites[s].multicolor) {
spr_pix = (sprites[s].pattern_data >> (23 - sx)) & 1;
}
else {
int pair = sx >> 1;
spr_pix = (sprites[s].pattern_data >> (22 - pair * 2)) & 3;
}
if (spr_pix == 0) continue;
sprite_hit_mask |= (1 << s);
// Collision with visible foreground graphics
if (gfx_is_fg) {
regs[SDCOL] |= (uint8_t)(1 << s);
irq_sprite_data = true;
}
if (!have_sprite_pixel) {
have_sprite_pixel = true;
top_sprite_behind_fg = sprites[s].priority;
if (!sprites[s].multicolor) {
top_sprite_color = sprites[s].color & 0x0F;
}
else {
if (spr_pix == 1) top_sprite_color = regs[SPMC0] & 0x0F;
else if (spr_pix == 2) top_sprite_color = sprites[s].color & 0x0F;
else top_sprite_color = regs[SPMC1] & 0x0F;
}
}
}
if ((sprite_hit_mask & (sprite_hit_mask - 1)) != 0) {
regs[SSCOL] |= sprite_hit_mask;
irq_sprite_sprite = true;
}
if (irq_sprite_data || irq_sprite_sprite) update_interrupts();
// -----------------------------------------------------------------
// 3) Final mix
// -----------------------------------------------------------------
if (have_sprite_pixel) {
if (!top_sprite_behind_fg || !gfx_is_fg)
final_color = C64_PALETTE[top_sprite_color & 0x0F];
else
final_color = gfx_color;
}
else {
final_color = gfx_color;
}
}
frame_buffer[y * C64_SCREEN_WIDTH + x] = final_color;
}
// Update history for next cycle
last_g_data = (!idle_state) ? g_data : 0;
last_c_data = c_data;
}
// -----------------------------------------------------------------------------
// Border Logic
// -----------------------------------------------------------------------------
void VIC_II::check_border_horizontal()
{
// CSEL (CR2 bit 3): 0 = 38 columns, 1 = 40 columns
const bool csel = (regs[CR2] & 0x08) != 0;
// Bauer/chips-style PAL tick positions:
// 40 columns: open at tick 16, close at tick 56
// 38 columns: open at tick 17, close at tick 55
const int left_tick = csel ? 16 : 17;
const int right_tick = csel ? 56 : 55;
if (raster_x == left_tick)
{
// Only open the main border if vertical border is not active.
if (!vertical_border_ff)
main_border_ff = false;
}
if (raster_x == right_tick)
{
// Close border
main_border_ff = true;
}
}
void VIC_II::check_border_vertical() {
bool rsel = regs[CR1] & 0x08; // 0=24row, 1=25row
int top_cmp = rsel ? 51 : 55;
int bot_cmp = rsel ? 251 : 247;
//int top_cmp = rsel ? 40 : 44;
//int bot_cmp = rsel ? 239 : 235;
if (raster_y == top_cmp) vertical_border_ff = false;
// CHANGED: Use >= to strictly enforce border closure at the bottom
if (raster_y >= bot_cmp) vertical_border_ff = true;
// If Vertical border is active, main border is active
if (vertical_border_ff) main_border_ff = true;
}