-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlibcRSID.h
3993 lines (3709 loc) · 239 KB
/
libcRSID.h
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
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// cRSID lightweight RealSID (integer-only) library-header (with API-calls) by Hermit (Mihaly Horvath)
// Define `CRSID_IMPLEMENTATION` in *one* C/C++ file before including this
// library to create the implementation.
#ifdef __cplusplus
extern "C"
{
#endif
struct cRSID_Output
{
signed int L;
signed int R;
};
struct cRSID_SIDwavOutput
{
signed int NonFilted;
signed int FilterInput;
};
enum cRSID_Specifications
{
CRSID_SIDCOUNT_MAX = 4,
CRSID_CIACOUNT = 2,
CRSID_FILEVERSION_WEBSID = 0x4E
};
enum cRSID_Channels
{
CRSID_CHANNEL_LEFT = 1,
CRSID_CHANNEL_RIGHT = 2,
CRSID_CHANNEL_BOTH = 3
};
enum cRSID_StatusCodes
{
CRSID_STATUS_OK = 0,
CRSID_ERROR_INIT = -1,
CRSID_ERROR_LOAD = -2
};
typedef struct cRSID_SIDheader cRSID_SIDheader;
typedef struct cRSID_C64instance cRSID_C64instance;
typedef struct cRSID_CPUinstance cRSID_CPUinstance;
typedef struct cRSID_SIDinstance cRSID_SIDinstance;
typedef struct cRSID_CIAinstance cRSID_CIAinstance;
typedef struct cRSID_VICinstance cRSID_VICinstance;
typedef struct cRSID_Output cRSID_Output;
typedef struct cRSID_SIDwavOutput cRSID_SIDwavOutput;
struct cRSID_SIDheader
{ // Offset: default/info:
unsigned char
MagicString[4]; //$00 - "PSID" or "RSID" (RSID must provide Reset-circumstances & CIA/VIC-interrupts)
unsigned char VersionH00; //$04
unsigned char Version; //$05 - 1 for PSID v1, 2..4 for PSID v2..4 or RSID v2..4 (3/4 has 2SID/3SID support),
//0x4E for 4SID (WebSID-format)
unsigned char HeaderSizeH00; //$06
unsigned char HeaderSize; //$07 - $76 for v1, $7C for v2..4, with WebSID-format: $7E for 2SID, $80 for 3SID, $82
//for 4SID (depends on number of SIDs)
unsigned char LoadAddressH,
LoadAddressL; //$08 - if 0 it's a PRG and its loadaddress is used (RSID: 0, PRG-loadaddress>=$07E8)
unsigned char InitAddressH, InitAddressL; //$0A - if 0 it's taken from load-address (but should be set) (RSID:
//don't point to ROM, 0 if BASICflag set)
unsigned char PlayAddressH,
PlayAddressL; //$0C - if 0 play-routine-call is set by the initializer (always true for RSID)
unsigned char SubtuneAmountH00; //$0E
unsigned char SubtuneAmount; //$0F - 1..256
unsigned char DefaultSubtuneH00; //$10
unsigned char DefaultSubtune; //$11 - 1..256 (optional, defaults to 1)
unsigned char SubtuneTimeSources[4]; //$12 - 0:Vsync / 1:CIA1 (for PSID) (LSB is subtune1, MSB above 32) ,
//always 0 for RSID
char Title[32]; //$16 - strings are using 1252 codepage
char Author[32]; //$36
char ReleaseInfo[32]; //$56
// SID v2 additions: (if SID2/SID3 model is set to unknown, they're set to the same
// model as SID1)
unsigned char ModelFormatStandardH; //$76 - bit9&8/7&6/5&4: SID3/2/1 model (00:?,01:6581,10:8580,11:both)
//(4SID:bit6=SID1-channel), bit3&2:VideoStandard..
unsigned char ModelFormatStandard; //$77 ..(01:PAL,10:NTSC,11:both),
//bit1:(0:C64,1:PlaySIDsamples/RSID_BASICflag), bit0:(0:builtin-player,1:MUS)
unsigned char RelocStartPage; //$78 - v2NG specific, if 0 the SID doesn't write outside its data-range, if $FF
//there's no place for driver
unsigned char RelocFreePages; //$79 - size of area from RelocStartPage for driver-relocation (RSID: must not
//contain ROM or 0..$3FF)
union {
unsigned char SID2baseAddress; //$7A - (SID2BASE-$d000)/16 //SIDv3-relevant, only $42..$FE values are valid
//($d420..$DFE0), else no SID2
unsigned char
SID2flagsH; //$7A: address of SID2 in WebSID-format too (same format as SID2baseAddress in HVSC format)
};
union {
unsigned char SID3baseAddress; //$7B - (SID3BASE-$d000)/16 //SIDv4-relevant, only $42..$FE values are valid
//($d420..$DFE0), else no SID3
unsigned char SID2flagsL; //$7B: flags for WebSID-format, bit6: output-channel (0(default):left, 1:right,
//?:both?), bit5..4:SIDmodel(00:setting,01:6581,10:8580,11:both)
// my own (implemented in SID-Wizard too) proposal for channel-info: bit7 should
// be 'middle' channel-flag (overriding bit6 left/right)
};
// WebSID-format (with 4 and more SIDs -support) additional fields: for each extra SID there's an 'nSIDflags'
// byte-pair
unsigned char SID3flagsH, SID3flagsL; //$7C,$7D: the same address/flag-layout for SID3 as with SID2
union {
unsigned char SID4flagsH;
unsigned char SID4baseAddress;
}; //$7E
unsigned char SID4flagsL; //$7F: the same address/flag-layout for SID4 as with SID2
//... repeated for more SIDs, and end the list with $00,$00 (this determines the amount of SIDs)
}; // music-program follows right after the header
struct cRSID_CPUinstance
{
cRSID_C64instance *C64; // reference to the containing C64
unsigned int PC;
short int A, SP;
unsigned char X, Y, ST; // STATUS-flags: N V - B D I Z C
unsigned char PrevNMI; // used for NMI leading edge detection
};
struct cRSID_SIDinstance
{
// SID-chip data:
cRSID_C64instance *C64; // reference to the containing C64
unsigned short ChipModel; // values: 8580 / 6581
unsigned char Channel; // 1:left, 2:right, 3:both(middle)
unsigned short BaseAddress; // SID-baseaddress location in C64-memory (IO)
unsigned char *BasePtr; // SID-baseaddress location in host's memory
// ADSR-related:
unsigned char ADSRstate[15];
unsigned short RateCounter[15];
unsigned char EnvelopeCounter[15];
unsigned char ExponentCounter[15];
// Wave-related:
int PhaseAccu[15]; // 28bit precision instead of 24bit
int PrevPhaseAccu[15]; //(integerized ClockRatio fractionals, WebSID has similar solution)
unsigned char SyncSourceMSBrise;
unsigned int RingSourceMSB;
unsigned int NoiseLFSR[15];
unsigned int PrevWavGenOut[15];
unsigned char PrevWavData[15];
// Filter-related:
int PrevLowPass;
int PrevBandPass;
// Output-stage:
int NonFiltedSample;
int FilterInputSample;
int PrevNonFiltedSample;
int PrevFilterInputSample;
signed int PrevVolume; // lowpass-filtered version of Volume-band register
int Output; // not attenuated (range:0..0xFFFFF depending on SID's main-volume)
int Level; // filtered version, good for VU-meter display
};
struct cRSID_CIAinstance
{
cRSID_C64instance *C64; // reference to the containing C64
char ChipModel; // old or new CIA? (have 1 cycle difference in cases)
unsigned short BaseAddress; // CIA-baseaddress location in C64-memory (IO)
unsigned char *BasePtrWR; // CIA-baseaddress location in host's memory for writing
unsigned char *BasePtrRD; // CIA-baseaddress location in host's memory for reading
};
struct cRSID_VICinstance
{
cRSID_C64instance *C64; // reference to the containing C64
char ChipModel; //(timing differences between models?)
unsigned short BaseAddress; // VIC-baseaddress location in C64-memory (IO)
unsigned char *BasePtrWR; // VIC-baseaddress location in host's memory for writing
unsigned char *BasePtrRD; // VIC-baseaddress location in host's memory for reading
unsigned short RasterLines;
unsigned char RasterRowCycles;
unsigned char RowCycleCnt;
};
struct cRSID_C64instance
{
// platform-related:
unsigned short SampleRate;
unsigned int BufferSize;
unsigned char HighQualitySID;
unsigned char SIDchipCount;
unsigned char Stereo;
unsigned char PlaybackSpeed;
unsigned char Paused;
// C64-machine related:
unsigned char VideoStandard; // 0:NTSC, 1:PAL (based on the SID-header field)
unsigned int CPUfrequency;
unsigned short SampleClockRatio; // ratio of CPU-clock and samplerate
unsigned short SelectedSIDmodel;
unsigned char MainVolume;
// SID-file related:
union {
cRSID_SIDheader *SIDheader;
char *SIDfileData;
};
unsigned short Attenuation;
char RealSIDmode;
char PSIDdigiMode;
unsigned char SubTune;
unsigned short LoadAddress;
unsigned short InitAddress;
unsigned short PlayAddress;
unsigned short EndAddress;
char TimerSource; // for current subtune, 0:VIC, 1:CIA (as in SID-header)
// PSID-playback related:
unsigned char SoundStarted;
// char CIAisSet; //for dynamic CIA setting from player-routine (RealSID substitution)
int FrameCycles;
int FrameCycleCnt; // this is a substitution in PSID-mode for CIA/VIC counters
short PrevRasterLine;
short SampleCycleCnt;
short OverSampleCycleCnt;
short TenthSecondCnt;
unsigned short SecondCnt;
short PlayTime;
char Finished;
char Returned;
unsigned char IRQ; // collected IRQ line from devices
unsigned char NMI; // collected NMI line from devices
// Hardware-elements:
cRSID_CPUinstance CPU;
cRSID_SIDinstance SID[CRSID_SIDCOUNT_MAX + 1];
cRSID_CIAinstance CIA[CRSID_CIACOUNT + 1];
cRSID_VICinstance VIC;
// Overlapping system memories, which one is read/written in an address region depends on CPU-port
// bankselect-bits) Address $00 and $01 - data-direction and data-register of port built into CPU (used as
// bank-selection) (overriding RAM on C64)
unsigned char RAMbank[0x10100]; //$0000..$FFFF RAM (and RAM under IO/ROM/CPUport)
unsigned char IObankWR[0x10100]; //$D000..$DFFF IO-RAM (registers) to write (VIC/SID/CIA/ColorRAM/IOexpansion)
unsigned char
IObankRD[0x10100]; //$D000..$DFFF IO-RAM (registers) to read from (VIC/SID/CIA/ColorRAM/IOexpansion)
unsigned char
ROMbanks[0x10100]; //$1000..$1FFF/$9000..$9FFF (CHARGEN), $A000..$BFFF (BASIC), $E000..$FFFF (KERNAL)
};
extern cRSID_C64instance
cRSID_C64; // the only global object (for faster & simpler access than with struct-pointers, in some places)
cRSID_C64instance *cRSID_init(unsigned short samplerate); // init emulation objects and sound
cRSID_SIDheader *cRSID_processSIDfile(cRSID_C64instance *C64, unsigned char *filedata,
int filesize); // in host/file.c, copy SID-data to C64 memory
void cRSID_initSIDtune(cRSID_C64instance *C64, cRSID_SIDheader *SIDheader, char subtune); // init tune/subtune
void cRSID_initC64(cRSID_C64instance *C64); // hard-reset
void cRSID_generateSound(cRSID_C64instance *C64, unsigned char *buf, unsigned short len);
void cRSID_generateFloat(cRSID_C64instance *C64, float *buf, unsigned short len);
#ifdef __cplusplus
}
#endif
#ifdef CRSID_IMPLEMENTATION
#ifdef __cplusplus
extern "C"
{
#endif
#if defined _MSC_VER || (defined __SIZEOF_FLOAT__ && __SIZEOF_FLOAT__ == 4)
#include <stdint.h>
#endif
#include <stdlib.h>
// Emulation of C64 memories and memory bus (PLA & MUXes)
static inline unsigned char *cRSID_getMemReadPtr(unsigned short address)
{
// cRSID_C64instance* const C64 = &cRSID_C64; //for faster (?) operation we use a global object as memory
if (address < 0xA000)
return &cRSID_C64.RAMbank[address];
else if (0xD000 <= address && address < 0xE000 && (cRSID_C64.RAMbank[1] & 3))
{
if (0xD400 <= address && address < 0xD419)
return &cRSID_C64.IObankWR[address]; // emulate bitfading aka SID-read of last written reg (e.g. Lift Off
// ROR $D400,x)
return &cRSID_C64.IObankRD[address];
}
else if ((address < 0xC000 && (cRSID_C64.RAMbank[1] & 3) == 3) || (0xE000 <= address && (cRSID_C64.RAMbank[1] & 2)))
return &cRSID_C64.ROMbanks[address];
return &cRSID_C64.RAMbank[address];
}
static inline unsigned char *cRSID_getMemReadPtrC64(cRSID_C64instance *C64, unsigned short address)
{
if (address < 0xA000)
return &C64->RAMbank[address];
else if (0xD000 <= address && address < 0xE000 && (C64->RAMbank[1] & 3))
{
if (0xD400 <= address && address < 0xD419)
return &cRSID_C64.IObankWR[address]; // emulate peculiar SID-read (e.g. Lift Off)
return &C64->IObankRD[address];
}
else if ((address < 0xC000 && (C64->RAMbank[1] & 3) == 3) || (0xE000 <= address && (C64->RAMbank[1] & 2)))
return &C64->ROMbanks[address];
return &C64->RAMbank[address];
}
static inline unsigned char *cRSID_getMemWritePtr(unsigned short address)
{
// cRSID_C64instance* const C64 = &cRSID_C64; //for faster (?) operation we use a global object as memory
if (address < 0xD000 || 0xE000 <= address)
return &cRSID_C64.RAMbank[address];
else if (cRSID_C64.RAMbank[1] & 3)
{ // handle SID-mirrors! (CJ in the USA workaround (writing above $d420, except SID2/SID3))
if (0xD420 <= address && address < 0xD800)
{ // CIA/VIC mirrors needed?
if (!(cRSID_C64.PSIDdigiMode && 0xD418 <= address && address < 0xD500) &&
!(cRSID_C64.SID[2].BaseAddress <= address && address < cRSID_C64.SID[2].BaseAddress + 0x20) &&
!(cRSID_C64.SID[3].BaseAddress <= address && address < cRSID_C64.SID[3].BaseAddress + 0x20) &&
!(cRSID_C64.SID[4].BaseAddress <= address && address < cRSID_C64.SID[4].BaseAddress + 0x20))
{
return &cRSID_C64.IObankWR[0xD400 +
(address & 0x1F)]; // write to $D400..D41F if not in SID2/SID3 address-space
}
else
return &cRSID_C64.IObankWR[address];
}
else
return &cRSID_C64.IObankWR[address];
}
return &cRSID_C64.RAMbank[address];
}
static inline unsigned char *cRSID_getMemWritePtrC64(cRSID_C64instance *C64, unsigned short address)
{
if (address < 0xD000 || 0xE000 <= address)
return &C64->RAMbank[address];
else if (C64->RAMbank[1] & 3)
{ // handle SID-mirrors! (CJ in the USA workaround (writing above $d420, except SID2/SID3/PSIDdigi))
if (0xD420 <= address && address < 0xD800)
{ // CIA/VIC mirrors needed?
if (!(cRSID_C64.PSIDdigiMode && 0xD418 <= address && address < 0xD500) &&
!(C64->SID[2].BaseAddress <= address && address < C64->SID[2].BaseAddress + 0x20) &&
!(C64->SID[3].BaseAddress <= address && address < C64->SID[3].BaseAddress + 0x20) &&
!(C64->SID[4].BaseAddress <= address && address < C64->SID[4].BaseAddress + 0x20))
{
return &C64->IObankWR[0xD400 +
(address & 0x1F)]; // write to $D400..D41F if not in SID2/SID3 address-space
}
else
return &C64->IObankWR[address];
}
else
return &C64->IObankWR[address];
}
return &C64->RAMbank[address];
}
/*static inline unsigned char cRSID_readMem(unsigned short address)
{
return *cRSID_getMemReadPtr(address);
}*/
static inline unsigned char cRSID_readMemC64(cRSID_C64instance *C64, unsigned short address)
{
return *cRSID_getMemReadPtrC64(C64, address);
}
/*static inline void cRSID_writeMem(unsigned short address, unsigned char data)
{
*cRSID_getMemWritePtr(address) = data;
}*/
static inline void cRSID_writeMemC64(cRSID_C64instance *C64, unsigned short address, unsigned char data)
{
*cRSID_getMemWritePtrC64(C64, address) = data;
}
static void cRSID_setROMcontent(cRSID_C64instance *C64)
{ // fill KERNAL/BASIC-ROM areas with content needed for SID-playback
int i;
static const unsigned char ROM_IRQreturnCode[9] = {0xAD, 0x0D, 0xDC, 0x68, 0xA8,
0x68, 0xAA, 0x68, 0x40}; // CIA1-acknowledge IRQ-return
static const unsigned char ROM_NMIstartCode[5] = {0x78, 0x6c, 0x18, 0x03, 0x40}; // SEI and jmp($0318)
static const unsigned char ROM_IRQBRKstartCode[19] = {
// Full IRQ-return (handling BRK with the same RAM vector as IRQ)
0x48, 0x8A, 0x48, 0x98, 0x48, 0xBA, 0xBD, 0x04, 0x01, 0x29,
0x10, 0xEA, 0xEA, 0xEA, 0xEA, 0xEA, 0x6C, 0x14, 0x03};
for (i = 0xA000; i < 0x10000; ++i)
C64->ROMbanks[i] = 0x60; // RTS (at least return if some unsupported call is made to ROM)
// for (i=0; i<sizeof(KERNAL); ++i) C64->ROMbanks[0xE000+i] = KERNAL[i];
for (i = 0xEA31; i < 0xEA7E; ++i)
C64->ROMbanks[i] = 0xEA; // NOP (full IRQ-return leading to simple IRQ-return without other tasks)
for (i = 0; i < 9; ++i)
C64->ROMbanks[0xEA7E + i] = ROM_IRQreturnCode[i];
for (i = 0; i < 4; ++i)
C64->ROMbanks[0xFE43 + i] = ROM_NMIstartCode[i];
for (i = 0; i < 19; ++i)
C64->ROMbanks[0xFF48 + i] = ROM_IRQBRKstartCode[i];
C64->ROMbanks[0xFFFB] = 0xFE;
C64->ROMbanks[0xFFFA] = 0x43; // ROM NMI-vector
C64->ROMbanks[0xFFFF] = 0xFF;
C64->ROMbanks[0xFFFE] = 0x48; // ROM IRQ-vector
// copy KERNAL & BASIC ROM contents into the RAM under them? (So PSIDs that don't select bank correctly will work
// better.)
for (i = 0xA000; i < 0x10000; ++i)
C64->RAMbank[i] = C64->ROMbanks[i];
}
static void cRSID_initMem(cRSID_C64instance *C64)
{ // set default values that normally KERNEL ensures after startup/reset (only SID-playback related)
static int i;
// data required by both PSID and RSID (according to HVSC SID_file_format.txt):
cRSID_writeMemC64(C64, 0x02A6, C64->VideoStandard); //$02A6 should be pre-set to: 0:NTSC / 1:PAL
cRSID_writeMemC64(C64, 0x0001, 0x37); // initialize bank-reg. (ROM-banks and IO enabled)
// if (C64->ROMbanks[0xE000]==0) { //wasn't a KERNAL-ROM loaded? (e.g. PSID)
cRSID_writeMemC64(C64, 0x00CB, 0x40); // Some tunes might check for keypress here (e.g. Master Blaster Intro)
// if(C64->RealSIDmode) {
cRSID_writeMemC64(C64, 0x0315, 0xEA);
cRSID_writeMemC64(C64, 0x0314, 0x31); // IRQ
cRSID_writeMemC64(C64, 0x0319, 0xEA /*0xFE*/);
cRSID_writeMemC64(C64, 0x0318, 0x81 /*0x47*/); // NMI
//}
for (i = 0xD000; i < 0xD7FF; ++i)
C64->IObankRD[i] = C64->IObankWR[i] = 0; // initialize the whole IO area for a known base-state
if (C64->RealSIDmode)
{
C64->IObankWR[0xD012] = 0x37;
C64->IObankWR[0xD011] = 0x8B;
} // else C64->IObankWR[0xD012] = 0;
// C64->IObankWR[0xD019] = 0; //PSID: rasterrow: any value <= $FF, IRQ:enable later if there is VIC-timingsource
C64->IObankRD[0xDC00] = 0x10;
C64->IObankRD[0xDC01] = 0xFF; // Imitate CIA1 keyboard/joy port, some tunes check if buttons are not pressed
if (C64->VideoStandard)
{
C64->IObankWR[0xDC04] = 0x24;
C64->IObankWR[0xDC05] = 0x40;
} // initialize CIAs
else
{
C64->IObankWR[0xDC04] = 0x95;
C64->IObankWR[0xDC05] = 0x42;
}
if (C64->RealSIDmode)
C64->IObankWR[0xDC0D] =
0x81; // Reset-default, but for PSID CIA1 TimerA IRQ should be enabled anyway if SID is CIA-timed
C64->IObankWR[0xDC0E] = 0x01; // some tunes (and PSID doc) expect already running CIA (Reset-default)
C64->IObankWR[0xDC0F] = 0x00; // All counters other than CIA1 TimerA should be disabled and set to 0xFF for PSID:
C64->IObankWR[0xDD00] = C64->IObankRD[0xDD00] = 0x03; // VICbank-selector default
C64->IObankWR[0xDD04] = C64->IObankWR[0xDD05] = 0xFF; // C64->IObankWR[0xDD0E] = C64->IObank[0xDD0F] = 0x00;
//}
}
// cRSID CPU-emulation
static inline void cRSID_writeCIAIRQmask(cRSID_CIAinstance *CIA, unsigned char value)
{
if (value & 0x80)
CIA->BasePtrWR[0xD] |= (value & 0x1F);
else
CIA->BasePtrWR[0xD] &= ~(value & 0x1F);
}
static inline void cRSID_acknowledgeCIAIRQ(cRSID_CIAinstance *CIA)
{
CIA->BasePtrRD[0xD] = 0x00; // reading a CIA interrupt-register clears its read-part and IRQ-flag
}
static inline void cRSID_acknowledgeVICrasterIRQ(cRSID_VICinstance *VIC)
{
enum VICregisters
{
INTERRUPT = 0x19
};
enum InterruptBitVal
{
VIC_IRQ = 0x80,
RASTERROW_MATCH_IRQ = 0x01
};
// An 1 is to be written into the IRQ-flag (bit0) of $d019 to clear it and deassert IRQ signal
// if (VIC->BasePtrWR[INTERRUPT] & RASTERROW_MATCH_IRQ) { //acknowledge raster-interrupt by writing to $d019 bit0?
// But oftentimes INC/LSR/etc. RMW commands are used to acknowledge VIC IRQ, they work on real
// CPU because it writes the unmodified original value itself to memory before writing the modified there
VIC->BasePtrWR[INTERRUPT] &= ~RASTERROW_MATCH_IRQ; // prepare for next acknowledge-detection
VIC->BasePtrRD[INTERRUPT] &= ~(VIC_IRQ | RASTERROW_MATCH_IRQ); // remove IRQ flag and state
//}
}
enum StatusFlagBitValues
{
N = 0x80,
V = 0x40,
B = 0x10,
D = 0x08,
I = 0x04,
Z = 0x02,
C = 0x01
};
static const unsigned char FlagSwitches[] = {0x01, 0x21, 0x04, 0x24, 0x00, 0x40, 0x08, 0x28},
BranchFlags[] = {0x80, 0x40, 0x01, 0x02};
static cRSID_C64instance *const C64 = &cRSID_C64;
static char Cycles, SamePage;
static unsigned char IR, ST, X, Y;
static short int A, SP, T;
static unsigned int PC, Addr, PrevPC;
static void loadReg(cRSID_CPUinstance *CPU)
{
PC = CPU->PC;
SP = CPU->SP;
ST = CPU->ST;
A = CPU->A;
X = CPU->X;
Y = CPU->Y;
}
static void storeReg(cRSID_CPUinstance *CPU)
{
CPU->PC = PC;
CPU->SP = SP;
CPU->ST = ST;
CPU->A = A;
CPU->X = X;
CPU->Y = Y;
}
static unsigned char rd(unsigned short address)
{
static unsigned char value;
value = *cRSID_getMemReadPtr(address);
if (C64->RealSIDmode)
{
if (C64->RAMbank[1] & 3)
{
if (address == 0xDC0D)
{
cRSID_acknowledgeCIAIRQ(&C64->CIA[1]);
}
else if (address == 0xDD0D)
{
cRSID_acknowledgeCIAIRQ(&C64->CIA[2]);
}
}
}
return value;
}
static void wr(unsigned short address, unsigned char data)
{
*cRSID_getMemWritePtr(address) = data;
if (C64->RealSIDmode && (C64->RAMbank[1] & 3))
{
// if(data&1) { //only writing 1 to $d019 bit0 would acknowledge, not any value (but RMW instructions write
// $d019 back before mod.)
if (address == 0xD019)
{
cRSID_acknowledgeVICrasterIRQ(&C64->VIC);
}
//}
}
}
static void wr2(unsigned short address, unsigned char data)
{ // PSID-hack specific memory-write
static int Tmp;
*cRSID_getMemWritePtr(address) = data;
if (C64->RAMbank[1] & 3)
{
if (C64->RealSIDmode)
{
if (address == 0xDC0D)
cRSID_writeCIAIRQmask(&C64->CIA[1], data);
else if (address == 0xDD0D)
cRSID_writeCIAIRQmask(&C64->CIA[2], data);
else if (address == 0xDD0C)
C64->IObankRD[address] = data; // mirror WR to RD (e.g. Wonderland_XIII_tune_1.sid)
else if (address == 0xD019 && data & 1)
{ // only writing 1 to $d019 bit0 would acknowledge
cRSID_acknowledgeVICrasterIRQ(&C64->VIC);
}
}
else
{
switch (address)
{
case 0xDC05:
case 0xDC04:
if (C64->TimerSource)
{ // dynamic CIA-setting (Galway/Rubicon workaround)
C64->FrameCycles =
((C64->IObankWR[0xDC04] + (C64->IObankWR[0xDC05] << 8))); //<< 4) / C64->SampleClockRatio;
}
break;
case 0xDC08:
C64->IObankRD[0xDC08] = data;
break; // refresh TOD-clock
case 0xDC09:
C64->IObankRD[0xDC09] = data;
break; // refresh TOD-clock
case 0xD012: // dynamic VIC IRQ-rasterline setting (Microprose Soccer V1 workaround)
if (C64->PrevRasterLine >= 0)
{ // was $d012 set before? (or set only once?)
if (C64->IObankWR[0xD012] != C64->PrevRasterLine)
{
Tmp = C64->IObankWR[0xD012] - C64->PrevRasterLine;
if (Tmp < 0)
Tmp += C64->VIC.RasterLines;
C64->FrameCycleCnt = C64->FrameCycles - Tmp * C64->VIC.RasterRowCycles;
}
}
C64->PrevRasterLine = C64->IObankWR[0xD012];
break;
}
}
}
}
static void addrModeImmediate(void)
{
++PC;
Addr = PC;
Cycles = 2;
} // imm.
static void addrModeZeropage(void)
{
++PC;
Addr = rd(PC);
Cycles = 3;
} // zp
static void addrModeAbsolute(void)
{
++PC;
Addr = rd(PC);
++PC;
Addr += rd(PC) << 8;
Cycles = 4;
} // abs
static void addrModeZeropageXindexed(void)
{
++PC;
Addr = (rd(PC) + X) & 0xFF;
Cycles = 4;
} // zp,x (with zeropage-wraparound of 6502)
static void addrModeZeropageYindexed(void)
{
++PC;
Addr = (rd(PC) + Y) & 0xFF;
Cycles = 4;
} // zp,y (with zeropage-wraparound of 6502)
static void addrModeXindexed(void)
{ // abs,x (only STA is 5 cycles, others are 4 if page not crossed, RMW:7)
++PC;
Addr = rd(PC) + X;
++PC;
SamePage = (Addr <= 0xFF);
Addr += rd(PC) << 8;
Cycles = 5;
}
static void addrModeYindexed(void)
{ // abs,y (only STA is 5 cycles, others are 4 if page not crossed, RMW:7)
++PC;
Addr = rd(PC) + Y;
++PC;
SamePage = (Addr <= 0xFF);
Addr += rd(PC) << 8;
Cycles = 5;
}
static void addrModeIndirectYindexed(void)
{ // (zp),y (only STA is 6 cycles, others are 5 if page not crossed, RMW:8)
++PC;
Addr = rd(rd(PC)) + Y;
SamePage = (Addr <= 0xFF);
Addr += rd((rd(PC) + 1) & 0xFF) << 8;
Cycles = 6;
}
static void addrModeXindexedIndirect(void)
{ // (zp,x)
++PC;
Addr = (rd(rd(PC) + X) & 0xFF) + ((rd(rd(PC) + X + 1) & 0xFF) << 8);
Cycles = 6;
}
static void clrC(void)
{
ST &= ~C;
} // clear Carry-flag
static void setC(unsigned char expr)
{
ST &= ~C;
ST |= (expr != 0);
} // set Carry-flag if expression is not zero
static void clrNZC(void)
{
ST &= ~(N | Z | C);
} // clear flags
static void clrNVZC(void)
{
ST &= ~(N | V | Z | C);
} // clear flags
static void setNZbyA(void)
{
ST &= ~(N | Z);
ST |= ((!A) << 1) | (A & N);
} // set Negative-flag and Zero-flag based on result in Accumulator
static void setNZbyT(void)
{
T &= 0xFF;
ST &= ~(N | Z);
ST |= ((!T) << 1) | (T & N);
}
static void setNZbyX(void)
{
ST &= ~(N | Z);
ST |= ((!X) << 1) | (X & N);
} // set Negative-flag and Zero-flag based on result in X-register
static void setNZbyY(void)
{
ST &= ~(N | Z);
ST |= ((!Y) << 1) | (Y & N);
} // set Negative-flag and Zero-flag based on result in Y-register
static void setNZbyM(void)
{
ST &= ~(N | Z);
ST |= ((!rd(Addr)) << 1) | (rd(Addr) & N);
} // set Negative-flag and Zero-flag based on result at Memory-Address
static void setNZCbyAdd(void)
{
ST &= ~(N | Z | C);
ST |= (A & N) | (A > 255);
A &= 0xFF;
ST |= (!A) << 1;
} // after increase/addition
static void setVbyAdd(unsigned char M)
{
ST &= ~V;
ST |= ((~(T ^ M)) & (T ^ A) & N) >> 1;
} // calculate V-flag from A and T (previous A) and input2 (Memory)
static void setNZCbySub(signed short *obj)
{
ST &= ~(N | Z | C);
ST |= (*obj & N) | (*obj >= 0);
*obj &= 0xFF;
ST |= ((!(*obj)) << 1);
}
static void push(unsigned char value)
{
C64->RAMbank[0x100 + SP] = value;
--SP;
SP &= 0xFF;
} // push a value to stack
static unsigned char pop(void)
{
++SP;
SP &= 0xFF;
return C64->RAMbank[0x100 + SP];
} // pop a value from stack
static void cRSID_initCPU(cRSID_CPUinstance *CPU, unsigned short mempos)
{
CPU->PC = mempos;
CPU->A = 0;
CPU->X = 0;
CPU->Y = 0;
CPU->ST = 0x04;
CPU->SP = 0xFF;
CPU->PrevNMI = 0;
}
static unsigned char cRSID_emulateCPU(void)
{ // the CPU emulation for SID/PRG playback (ToDo: CIA/VIC-IRQ/NMI/RESET vectors, BCD-mode)
IR = ST = X = Y = 0;
A = SP = T = 0;
PC = Addr = PrevPC = 0;
loadReg(&C64->CPU);
PrevPC = PC;
IR = rd(PC);
Cycles = 2;
SamePage = 0; //'Cycles': ensure smallest 6510 runtime (for implied/register instructions)
if (IR & 1)
{ // nybble2: 1/5/9/D:accu.instructions, 3/7/B/F:illegal opcodes
switch ((IR & 0x1F) >> 1)
{ // value-forming to cause jump-table //PC wraparound not handled inside to save codespace
case 0:
case 1:
addrModeXindexedIndirect();
break; //(zp,x)
case 2:
case 3:
addrModeZeropage();
break;
case 4:
case 5:
addrModeImmediate();
break;
case 6:
case 7:
addrModeAbsolute();
break;
case 8:
case 9:
addrModeIndirectYindexed();
break; //(zp),y (5..6 cycles, 8 for R-M-W)
case 0xA:
addrModeZeropageXindexed();
break; // zp,x
case 0xB:
if ((IR & 0xC0) != 0x80)
addrModeZeropageXindexed(); // zp,x for illegal opcodes
else
addrModeZeropageYindexed(); // zp,y for LAX/SAX illegal opcodes
break;
case 0xC:
case 0xD:
addrModeYindexed();
break;
case 0xE:
addrModeXindexed();
break;
case 0xF:
if ((IR & 0xC0) != 0x80)
addrModeXindexed(); // abs,x for illegal opcodes
else
addrModeYindexed(); // abs,y for LAX/SAX illegal opcodes
break;
}
Addr &= 0xFFFF;
switch ((IR & 0xE0) >> 5)
{ // value-forming to cause gapless case-values and faster jump-table creation from switch-case
case 0:
if ((IR & 0x1F) != 0xB)
{ // ORA / SLO(ASO)=ASL+ORA
if ((IR & 3) == 3)
{
clrNZC();
setC(rd(Addr) >= N);
wr(Addr, rd(Addr) << 1);
Cycles += 2;
} // for SLO
else
Cycles -= SamePage;
A |= rd(Addr);
setNZbyA(); // ORA
}
else
{
A &= rd(Addr);
setNZbyA();
setC(A >= N);
} // ANC (AND+Carry=bit7)
break;
case 1:
if ((IR & 0x1F) != 0xB)
{ // AND / RLA (ROL+AND)
if ((IR & 3) == 3)
{ // for RLA
T = (rd(Addr) << 1) + (ST & C);
clrNZC();
setC(T > 255);
T &= 0xFF;
wr(Addr, T);
Cycles += 2;
}
else
Cycles -= SamePage;
A &= rd(Addr);
setNZbyA(); // AND
}
else
{
A &= rd(Addr);
setNZbyA();
setC(A >= N);
} // ANC (AND+Carry=bit7)
break;
case 2:
if ((IR & 0x1F) != 0xB)
{ // EOR / SRE(LSE)=LSR+EOR
if ((IR & 3) == 3)
{
clrNZC();
setC(rd(Addr) & 1);
wr(Addr, rd(Addr) >> 1);
Cycles += 2;
} // for SRE
else
Cycles -= SamePage;
A ^= rd(Addr);
setNZbyA(); // EOR
}
else
{
A &= rd(Addr);
setC(A & 1);
A >>= 1;
A &= 0xFF;
setNZbyA();
} // ALR(ASR)=(AND+LSR)
break;
case 3:
if ((IR & 0x1F) != 0xB)
{ // RRA (ROR+ADC) / ADC
if ((IR & 3) == 3)
{ // for RRA
T = (rd(Addr) >> 1) + ((ST & C) << 7);
clrNZC();
setC(T & 1);
wr(Addr, T);
Cycles += 2;
}
else
Cycles -= SamePage;
T = A;
A += rd(Addr) + (ST & C);
if ((ST & D) && (A & 0xF) > 9)
{
A += 0x10;
A &= 0xF0;
} // BCD?
setNZCbyAdd();
setVbyAdd(rd(Addr)); // ADC
}
else
{ // ARR (AND+ROR, bit0 not going to C, but C and bit7 get exchanged.)
A &= rd(Addr);
T += rd(Addr) + (ST & C);
clrNVZC();
setC(T > 255);
setVbyAdd(rd(Addr)); // V-flag set by intermediate ADC mechanism: (A&mem)+mem
T = A;
A = (A >> 1) + ((ST & C) << 7);
setC(T >= N);
setNZbyA();
}
break;
case 4:
if ((IR & 0x1F) == 0xB)
{
A = X & rd(Addr);
setNZbyA();
} // XAA (TXA+AND), highly unstable on real 6502!
else if ((IR & 0x1F) == 0x1B)
{
SP = A & X;
wr(Addr, SP & ((Addr >> 8) + 1));
} // TAS(SHS) (SP=A&X, mem=S&H} - unstable on real 6502
else
{
wr2(Addr, A & (((IR & 3) == 3) ? X : 0xFF));
} // STA / SAX (at times same as AHX/SHX/SHY) (illegal)
break;
case 5:
if ((IR & 0x1F) != 0x1B)
{
A = rd(Addr);
if ((IR & 3) == 3)
X = A;
} // LDA / LAX (illegal, used by my 1 rasterline player) (LAX #imm is unstable on C64)
else
{
A = X = SP = rd(Addr) & SP;
} // LAS(LAR)
setNZbyA();
Cycles -= SamePage;
break;
case 6:
if ((IR & 0x1F) != 0xB)
{ // CMP / DCP(DEC+CMP)
if ((IR & 3) == 3)
{
wr(Addr, rd(Addr) - 1);
Cycles += 2;
} // DCP
else
Cycles -= SamePage;
T = A - rd(Addr);
}
else
{
X = T = (A & X) - rd(Addr);
} // SBX(AXS) //SBX (AXS) (CMP+DEX at the same time)
setNZCbySub(&T);
break;