diff options
Diffstat (limited to 'lib/rbcodec/codecs/cRSID/libcRSID.h')
-rw-r--r-- | lib/rbcodec/codecs/cRSID/libcRSID.h | 221 |
1 files changed, 221 insertions, 0 deletions
diff --git a/lib/rbcodec/codecs/cRSID/libcRSID.h b/lib/rbcodec/codecs/cRSID/libcRSID.h new file mode 100644 index 0000000000..23c8dbcd28 --- /dev/null +++ b/lib/rbcodec/codecs/cRSID/libcRSID.h | |||
@@ -0,0 +1,221 @@ | |||
1 | // cRSID lightweight RealSID (integer-only) library-header (with API-calls) by Hermit (Mihaly Horvath) | ||
2 | |||
3 | #ifndef LIBCRSID_HEADER | ||
4 | #define LIBCRSID_HEADER //used to prevent double inclusion of this header-file | ||
5 | |||
6 | |||
7 | enum cRSID_Specifications { CRSID_SIDCOUNT_MAX=3, CRSID_CIACOUNT=2 }; | ||
8 | enum cRSID_StatusCodes { CRSID_STATUS_OK=0, CRSID_ERROR_INIT=-1, CRSID_ERROR_LOAD=-2 }; | ||
9 | |||
10 | |||
11 | typedef struct cRSID_SIDheader cRSID_SIDheader; | ||
12 | typedef struct cRSID_C64instance cRSID_C64instance; | ||
13 | typedef struct cRSID_CPUinstance cRSID_CPUinstance; | ||
14 | typedef struct cRSID_SIDinstance cRSID_SIDinstance; | ||
15 | typedef struct cRSID_CIAinstance cRSID_CIAinstance; | ||
16 | typedef struct cRSID_VICinstance cRSID_VICinstance; | ||
17 | |||
18 | |||
19 | cRSID_C64instance cRSID_C64; //the only global object (for faster & simpler access than with struct-pointers, in some places) | ||
20 | |||
21 | |||
22 | // Main API functions (mainly in libcRSID.c) | ||
23 | cRSID_C64instance* cRSID_init (unsigned short samplerate, unsigned short buflen); //init emulation objects and sound | ||
24 | #ifdef CRSID_PLATFORM_PC | ||
25 | char cRSID_playSIDfile (cRSID_C64instance* C64, char* filename, char subtune); //simple single-call SID playback | ||
26 | cRSID_SIDheader* cRSID_loadSIDtune (cRSID_C64instance* C64, char* filename); //load and process SID-filedata to C64 memory | ||
27 | void cRSID_playSIDtune (void); //start/continue playback (enable playing audio-buffer) | ||
28 | void cRSID_pauseSIDtune (void); //pause playback (enable playing audio-buffer) | ||
29 | void cRSID_close (void); //close sound etc. | ||
30 | #endif | ||
31 | cRSID_SIDheader* cRSID_processSIDfile (cRSID_C64instance* C64, unsigned char* filedata, int filesize); //in host/file.c, copy SID-data to C64 memory | ||
32 | void cRSID_initSIDtune (cRSID_C64instance* C64, cRSID_SIDheader* SIDheader, char subtune); //init tune/subtune | ||
33 | static inline signed short cRSID_generateSample (cRSID_C64instance* C64); //in host/audio.c, calculate a single sample | ||
34 | |||
35 | |||
36 | //Internal functions | ||
37 | |||
38 | // C64/C64.c | ||
39 | cRSID_C64instance* cRSID_createC64 (cRSID_C64instance* C64, unsigned short samplerate); | ||
40 | void cRSID_setC64 (cRSID_C64instance* C64); //configure hardware (SIDs) for SID-tune | ||
41 | void cRSID_initC64 (cRSID_C64instance* C64); //hard-reset | ||
42 | int cRSID_emulateC64 (cRSID_C64instance* C64); | ||
43 | static inline short cRSID_playPSIDdigi (cRSID_C64instance* C64); | ||
44 | // C64/MEM.c | ||
45 | static inline unsigned char* cRSID_getMemReadPtr (register unsigned short address); //for global cSID_C64 fast-access | ||
46 | static inline unsigned char* cRSID_getMemReadPtrC64 (cRSID_C64instance* C64, register unsigned short address); //maybe slower | ||
47 | static inline unsigned char* cRSID_getMemWritePtr (register unsigned short address); //for global cSID_C64 fast-access | ||
48 | static inline unsigned char* cRSID_getMemWritePtrC64 (cRSID_C64instance* C64, register unsigned short address); //maybe slower | ||
49 | static inline unsigned char cRSID_readMem (register unsigned short address); //for global cSID_C64 fast-access | ||
50 | static inline unsigned char cRSID_readMemC64 (cRSID_C64instance* C64, register unsigned short address); //maybe slower | ||
51 | static inline void cRSID_writeMem (register unsigned short address, register unsigned char data); //for global cSID_C64 fast-access | ||
52 | static inline void cRSID_writeMemC64 (cRSID_C64instance* C64, register unsigned short address, register unsigned char data); //maybe slower | ||
53 | void cRSID_setROMcontent (cRSID_C64instance* C64); //KERNAL, BASIC | ||
54 | void cRSID_initMem (cRSID_C64instance* C64); | ||
55 | // C64/CPU.c | ||
56 | void cRSID_initCPU (cRSID_CPUinstance* CPU, unsigned short mempos); | ||
57 | unsigned char cRSID_emulateCPU (void); //direct instances inside for hopefully faster operation | ||
58 | static inline char cRSID_handleCPUinterrupts (cRSID_CPUinstance* CPU); | ||
59 | // C64/SID.c | ||
60 | void cRSID_createSIDchip (cRSID_C64instance* C64, cRSID_SIDinstance* SID, unsigned short model, unsigned short baseaddress); | ||
61 | void cRSID_initSIDchip (cRSID_SIDinstance* SID); | ||
62 | void cRSID_emulateADSRs (cRSID_SIDinstance *SID, char cycles); | ||
63 | int cRSID_emulateWaves (cRSID_SIDinstance* SID); | ||
64 | // C64/CIA.c | ||
65 | void cRSID_createCIAchip (cRSID_C64instance* C64, cRSID_CIAinstance* CIA, unsigned short baseaddress); | ||
66 | void cRSID_initCIAchip (cRSID_CIAinstance* CIA); | ||
67 | static inline char cRSID_emulateCIA (cRSID_CIAinstance* CIA, char cycles); | ||
68 | static inline void cRSID_writeCIAIRQmask (cRSID_CIAinstance* CIA, unsigned char value); | ||
69 | static inline void cRSID_acknowledgeCIAIRQ (cRSID_CIAinstance* CIA); | ||
70 | // C64/VIC.c | ||
71 | void cRSID_createVICchip (cRSID_C64instance* C64, cRSID_VICinstance* VIC, unsigned short baseaddress); | ||
72 | void cRSID_initVICchip (cRSID_VICinstance* VIC); | ||
73 | static inline char cRSID_emulateVIC (cRSID_VICinstance* VIC, char cycles); | ||
74 | static inline void cRSID_acknowledgeVICrasterIRQ (cRSID_VICinstance* VIC); | ||
75 | |||
76 | // host/file.c | ||
77 | #ifdef CRSID_PLATFORM_PC | ||
78 | int cRSID_loadSIDfile (unsigned char* SIDfileData, char* filename, int maxlen); //load SID-file to a memory location (and return size) | ||
79 | #endif | ||
80 | // host/audio.c | ||
81 | #ifdef CRSID_PLATFORM_PC | ||
82 | void* cRSID_initSound (cRSID_C64instance* C64, unsigned short samplerate, unsigned short buflen); | ||
83 | void cRSID_startSound (void); | ||
84 | void cRSID_stopSound (void); | ||
85 | void cRSID_closeSound (void); | ||
86 | void cRSID_generateSound (cRSID_C64instance* C64, unsigned char* buf, unsigned short len); | ||
87 | #endif | ||
88 | |||
89 | |||
90 | struct cRSID_SIDheader { //Offset: default/info: | ||
91 | unsigned char MagicString[4]; //$00 - "PSID" or "RSID" (RSID must provide Reset-circumstances & CIA/VIC-interrupts) | ||
92 | unsigned char VersionH00; //$04 | ||
93 | unsigned char Version; //$05 - 1 for PSID v1, 2..4 for PSID v2..4 or RSID v2..4 (3/4 has 2SID/3SID support) | ||
94 | unsigned char HeaderSizeH00; //$06 | ||
95 | unsigned char HeaderSize; //$07 - $76 for v1, $7C for v2..4 | ||
96 | unsigned char LoadAddressH,LoadAddressL; //$08 - if 0 it's a PRG and its loadaddress is used (RSID: 0, PRG-loadaddress>=$07E8) | ||
97 | 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) | ||
98 | unsigned char PlayAddressH,PlayAddressL; //$0C - if 0 play-routine-call is set by the initializer (always true for RSID) | ||
99 | unsigned char SubtuneAmountH00; //$0E | ||
100 | unsigned char SubtuneAmount; //$0F - 1..256 | ||
101 | unsigned char DefaultSubtuneH00; //$10 | ||
102 | unsigned char DefaultSubtune; //$11 - 1..256 (optional, defaults to 1) | ||
103 | unsigned char SubtuneTimeSources[4]; //$12 - 0:Vsync / 1:CIA1 (for PSID) (LSB is subtune1, MSB above 32) , always 0 for RSID | ||
104 | char Title[32]; //$16 - strings are using 1252 codepage | ||
105 | char Author[32]; //$36 | ||
106 | char ReleaseInfo[32]; //$56 | ||
107 | //SID v2 additions: (if SID2/SID3 model is set to unknown, they're set to the same model as SID1) | ||
108 | unsigned char ModelFormatStandardH; //$76 - bit9&8/7&6/5&4: SID3/2/1 model (00:?,01:6581,10:8580,11:both), bit3&2:VideoStandard.. | ||
109 | unsigned char ModelFormatStandard; //$77 ..(01:PAL,10:NTSC,11:both), bit1:(0:C64,1:PlaySIDsamples/RSID_BASICflag), bit0:(0:builtin-player,1:MUS) | ||
110 | 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 | ||
111 | unsigned char RelocFreePages; //$79 - size of area from RelocStartPage for driver-relocation (RSID: must not contain ROM or 0..$3FF) | ||
112 | unsigned char SID2baseAddress; //$7A - (SID2BASE-$d000)/16 //SIDv3-relevant, only $42..$FE values are valid ($d420..$DFE0), else no SID2 | ||
113 | unsigned char SID3baseAddress; //$7B - (SID3BASE-$d000)/16 //SIDv4-relevant, only $42..$FE values are valid ($d420..$DFE0), else no SID3 | ||
114 | }; //music-program follows right after the header | ||
115 | |||
116 | |||
117 | struct cRSID_CPUinstance { | ||
118 | cRSID_C64instance* C64; //reference to the containing C64 | ||
119 | unsigned int PC; | ||
120 | short int A, SP; | ||
121 | unsigned char X, Y, ST; //STATUS-flags: N V - B D I Z C | ||
122 | unsigned char PrevNMI; //used for NMI leading edge detection | ||
123 | }; | ||
124 | |||
125 | |||
126 | struct cRSID_SIDinstance { | ||
127 | //SID-chip data: | ||
128 | cRSID_C64instance* C64; //reference to the containing C64 | ||
129 | unsigned short ChipModel; //values: 8580 / 6581 | ||
130 | unsigned short BaseAddress; //SID-baseaddress location in C64-memory (IO) | ||
131 | unsigned char* BasePtr; //SID-baseaddress location in host's memory | ||
132 | //ADSR-related: | ||
133 | unsigned char ADSRstate[15]; | ||
134 | unsigned short RateCounter[15]; | ||
135 | unsigned char EnvelopeCounter[15]; | ||
136 | unsigned char ExponentCounter[15]; | ||
137 | //Wave-related: | ||
138 | int PhaseAccu[15]; //28bit precision instead of 24bit | ||
139 | int PrevPhaseAccu[15]; //(integerized ClockRatio fractionals, WebSID has similar solution) | ||
140 | unsigned char SyncSourceMSBrise; | ||
141 | unsigned int RingSourceMSB; | ||
142 | unsigned int NoiseLFSR[15]; | ||
143 | unsigned int PrevWavGenOut[15]; | ||
144 | unsigned char PrevWavData[15]; | ||
145 | //Filter-related: | ||
146 | int PrevLowPass; | ||
147 | int PrevBandPass; | ||
148 | //Output-stage: | ||
149 | signed int PrevVolume; //lowpass-filtered version of Volume-band register | ||
150 | }; | ||
151 | |||
152 | |||
153 | struct cRSID_CIAinstance { | ||
154 | cRSID_C64instance* C64; //reference to the containing C64 | ||
155 | char ChipModel; //old or new CIA? (have 1 cycle difference in cases) | ||
156 | unsigned short BaseAddress; //CIA-baseaddress location in C64-memory (IO) | ||
157 | unsigned char* BasePtrWR; //CIA-baseaddress location in host's memory for writing | ||
158 | unsigned char* BasePtrRD; //CIA-baseaddress location in host's memory for reading | ||
159 | }; | ||
160 | |||
161 | |||
162 | struct cRSID_VICinstance { | ||
163 | cRSID_C64instance* C64; //reference to the containing C64 | ||
164 | char ChipModel; //(timing differences between models?) | ||
165 | unsigned short BaseAddress; //VIC-baseaddress location in C64-memory (IO) | ||
166 | unsigned char* BasePtrWR; //VIC-baseaddress location in host's memory for writing | ||
167 | unsigned char* BasePtrRD; //VIC-baseaddress location in host's memory for reading | ||
168 | unsigned short RasterLines; | ||
169 | unsigned char RasterRowCycles; | ||
170 | unsigned char RowCycleCnt; | ||
171 | }; | ||
172 | |||
173 | |||
174 | struct cRSID_C64instance { | ||
175 | //platform-related: | ||
176 | unsigned short SampleRate; | ||
177 | //C64-machine related: | ||
178 | unsigned char VideoStandard; //0:NTSC, 1:PAL (based on the SID-header field) | ||
179 | unsigned int CPUfrequency; | ||
180 | unsigned short SampleClockRatio; //ratio of CPU-clock and samplerate | ||
181 | //SID-file related: | ||
182 | union { | ||
183 | cRSID_SIDheader* SIDheader; | ||
184 | char* SIDfileData; | ||
185 | }; | ||
186 | unsigned short Attenuation; | ||
187 | char RealSIDmode; | ||
188 | char PSIDdigiMode; | ||
189 | unsigned char SubTune; | ||
190 | unsigned short LoadAddress; | ||
191 | unsigned short InitAddress; | ||
192 | unsigned short PlayAddress; | ||
193 | unsigned short EndAddress; | ||
194 | char TimerSource; //for current subtune, 0:VIC, 1:CIA (as in SID-header) | ||
195 | //PSID-playback related: | ||
196 | //char CIAisSet; //for dynamic CIA setting from player-routine (RealSID substitution) | ||
197 | int FrameCycles; | ||
198 | int FrameCycleCnt; //this is a substitution in PSID-mode for CIA/VIC counters | ||
199 | short PrevRasterLine; | ||
200 | short SampleCycleCnt; | ||
201 | short TenthSecondCnt; | ||
202 | char Finished; | ||
203 | char Returned; | ||
204 | unsigned char IRQ; //collected IRQ line from devices | ||
205 | unsigned char NMI; //collected NMI line from devices | ||
206 | |||
207 | //Hardware-elements: | ||
208 | cRSID_CPUinstance CPU; | ||
209 | cRSID_SIDinstance SID[CRSID_SIDCOUNT_MAX+1]; | ||
210 | cRSID_CIAinstance CIA[CRSID_CIACOUNT+1]; | ||
211 | cRSID_VICinstance VIC; | ||
212 | //Overlapping system memories, which one is read/written in an address region depends on CPU-port bankselect-bits) | ||
213 | //Address $00 and $01 - data-direction and data-register of port built into CPU (used as bank-selection) (overriding RAM on C64) | ||
214 | unsigned char RAMbank[0x10100]; //$0000..$FFFF RAM (and RAM under IO/ROM/CPUport) | ||
215 | unsigned char IObankWR[0x10100]; //$D000..$DFFF IO-RAM (registers) to write (VIC/SID/CIA/ColorRAM/IOexpansion) | ||
216 | unsigned char IObankRD[0x10100]; //$D000..$DFFF IO-RAM (registers) to read from (VIC/SID/CIA/ColorRAM/IOexpansion) | ||
217 | unsigned char ROMbanks[0x10100]; //$1000..$1FFF/$9000..$9FFF (CHARGEN), $A000..$BFFF (BASIC), $E000..$FFFF (KERNAL) | ||
218 | }; | ||
219 | |||
220 | |||
221 | #endif //LIBCRSID_HEADER | ||