| 1 | /* reSID interface |
| 2 | * |
| 3 | * much of the code here is from GOATTRACKER, (c) Cadaver |
| 4 | */ |
| 5 | #define PALCLOCKRATE 985248 |
| 6 | #define NTSCCLOCKRATE 1022727 |
| 7 | |
| 8 | #define NUMSIDREGS 0x19 |
| 9 | #define SIDWRITEDELAY 9 // lda $xxxx,x 4 cycles, sta $d400,x 5 cycles |
| 10 | #define SIDWAVEDELAY 4 // and $xxxx,x 4 cycles extra |
| 11 | |
| 12 | #include <stdlib.h> |
| 13 | #include <resid/sid.h> |
| 14 | #include <resid-fp/sidfp.h> |
| 15 | //#include <assert.h> |
| 16 | #include <stdio.h> |
| 17 | |
| 18 | extern "C" { |
| 19 | typedef struct |
| 20 | { |
| 21 | float distortionrate; |
| 22 | float distortionpoint; |
| 23 | float distortioncfthreshold; |
| 24 | float type3baseresistance; |
| 25 | float type3offset; |
| 26 | float type3steepness; |
| 27 | float type3minimumfetresistance; |
| 28 | float type4k; |
| 29 | float type4b; |
| 30 | float voicenonlinearity; |
| 31 | } FILTERPARAMS; |
| 32 | |
| 33 | int clockrate; |
| 34 | int samplerate; |
| 35 | unsigned char sidreg[NUMSIDREGS]; |
| 36 | const unsigned char sidorder[] = { |
| 37 | 0x0e,0x0f,0x14,0x13,0x10,0x11,0x12, |
| 38 | 0x07,0x08,0x0d,0x0c,0x09,0x0a,0x0b, |
| 39 | 0x00,0x01,0x06,0x05,0x02,0x03,0x04, |
| 40 | 0x16,0x17,0x18,0x15 |
| 41 | }; |
| 42 | SID *sid= 0; |
| 43 | SIDFP *sidfp = 0; |
| 44 | int residdelay = 0; |
| 45 | int usefp = 0; |
| 46 | |
| 47 | void sid_close() { |
| 48 | if(sid) delete sid; |
| 49 | if(sidfp) delete sidfp; |
| 50 | } |
| 51 | |
| 52 | void sid_init(int fp, FILTERPARAMS *fparams, int speed, unsigned m, unsigned ntsc, unsigned interpolate, unsigned customclockrate) |
| 53 | { |
| 54 | int c; |
| 55 | |
| 56 | usefp = fp; |
| 57 | |
| 58 | if (ntsc) clockrate = NTSCCLOCKRATE; |
| 59 | else clockrate = PALCLOCKRATE; |
| 60 | |
| 61 | if (customclockrate) |
| 62 | clockrate = customclockrate; |
| 63 | |
| 64 | samplerate = speed; |
| 65 | |
| 66 | if (!sidfp) sidfp = new SIDFP(); |
| 67 | if (!sid) sid = new SID(); |
| 68 | |
| 69 | if(usefp) { |
| 70 | switch(interpolate) |
| 71 | { |
| 72 | case 0: |
| 73 | sidfp->set_sampling_parameters(clockrate, SAMPLE_INTERPOLATE, speed); |
| 74 | break; |
| 75 | default: |
| 76 | sidfp->set_sampling_parameters(clockrate, SAMPLE_RESAMPLE_INTERPOLATE, speed); |
| 77 | break; |
| 78 | } |
| 79 | sidfp->reset(); |
| 80 | for (c = 0; c < NUMSIDREGS; c++) { |
| 81 | sidreg[c] = 0x00; |
| 82 | } |
| 83 | |
| 84 | if (m == 1) |
| 85 | sidfp->set_chip_model(MOS8580); |
| 86 | else |
| 87 | sidfp->set_chip_model(MOS6581); |
| 88 | sidfp->get_filter().set_distortion_properties( |
| 89 | fparams->distortionrate, |
| 90 | fparams->distortionpoint, |
| 91 | fparams->distortioncfthreshold); |
| 92 | sidfp->get_filter().set_type3_properties( |
| 93 | fparams->type3baseresistance, |
| 94 | fparams->type3offset, |
| 95 | fparams->type3steepness, |
| 96 | fparams->type3minimumfetresistance); |
| 97 | sidfp->get_filter().set_type4_properties( |
| 98 | fparams->type4k, |
| 99 | fparams->type4b); |
| 100 | sidfp->set_voice_nonlinearity( |
| 101 | fparams->voicenonlinearity); |
| 102 | sidfp->enable_filter(true); |
| 103 | } |
| 104 | else { |
| 105 | switch(interpolate) |
| 106 | { |
| 107 | case 0: |
| 108 | sid->set_sampling_parameters(clockrate, SAMPLE_FAST, speed, 20000); |
| 109 | break; |
| 110 | default: |
| 111 | sid->set_sampling_parameters(clockrate, SAMPLE_INTERPOLATE, speed, 20000); |
| 112 | break; |
| 113 | } |
| 114 | sid->reset(); |
| 115 | for (c = 0; c < NUMSIDREGS; c++) { |
| 116 | sidreg[c] = 0x00; |
| 117 | } |
| 118 | |
| 119 | if (m == 1) { |
| 120 | sid->set_chip_model(MOS8580); |
| 121 | } |
| 122 | else { |
| 123 | sid->set_chip_model(MOS6581); |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | unsigned char sid_getorder(unsigned char index) { |
| 129 | return sidorder[index]; |
| 130 | } |
| 131 | |
| 132 | int sid_fillbuffer(short *ptr, int samples, const int cyc) { |
| 133 | int os = samples; |
| 134 | int rc = cyc / 3; // NUMVOICE |
| 135 | int badline = rand() % NUMSIDREGS; |
| 136 | int tdelta; |
| 137 | int tdelta2; |
| 138 | int result; |
| 139 | int total = 0; |
| 140 | int c; |
| 141 | |
| 142 | tdelta = clockrate * samples / samplerate; |
| 143 | |
| 144 | for (c = 0; c < NUMSIDREGS; c++) { |
| 145 | unsigned char o = sid_getorder(c); |
| 146 | |
| 147 | // Extra delay per music routine iteration |
| 148 | if (cyc > 0 && |
| 149 | ((c == 0) || (c == 7) || (c == 14))) { |
| 150 | tdelta2 = rc; |
| 151 | if(usefp) |
| 152 | result = sidfp->clock(tdelta2, ptr, samples); |
| 153 | else |
| 154 | result = sid->clock(tdelta2, ptr, samples); |
| 155 | total += result; |
| 156 | ptr += result; |
| 157 | samples -= result; |
| 158 | tdelta -= rc; |
| 159 | } |
| 160 | |
| 161 | // Possible random badline delay once per writing |
| 162 | /* |
| 163 | if ((badline == c) && (residdelay)) { |
| 164 | tdelta2 = residdelay; |
| 165 | result = sid->clock(tdelta2, ptr, samples); |
| 166 | total += result; |
| 167 | ptr += result; |
| 168 | samples -= result; |
| 169 | tdelta -= residdelay; |
| 170 | } |
| 171 | */ |
| 172 | if(usefp) |
| 173 | sidfp->write(o, sidreg[o]); |
| 174 | else |
| 175 | sid->write(o, sidreg[o]); |
| 176 | |
| 177 | tdelta2 = SIDWRITEDELAY; |
| 178 | if(usefp) |
| 179 | result = sidfp->clock(tdelta2, ptr, samples); |
| 180 | else |
| 181 | result = sid->clock(tdelta2, ptr, samples); |
| 182 | total += result; |
| 183 | ptr += result; |
| 184 | samples -= result; |
| 185 | tdelta -= SIDWRITEDELAY; |
| 186 | } |
| 187 | |
| 188 | if(usefp) |
| 189 | result = sidfp->clock(tdelta, ptr, samples); |
| 190 | else |
| 191 | result = sid->clock(tdelta, ptr, samples); |
| 192 | |
| 193 | total += result; |
| 194 | // assert(total <= os); |
| 195 | return total; |
| 196 | } |
| 197 | } |