| 1 |
#include "dsid.h" |
| 2 |
#include <stdio.h> |
| 3 |
#include <math.h> // INFINITY |
| 4 |
#include <stdlib.h> |
| 5 |
#include <string.h> // memset, memcpy |
| 6 |
|
| 7 |
#define SID_OUT_SCALE (0x10000 * 3 * 16) |
| 8 |
|
| 9 |
// CONTROL |
| 10 |
#define GAT 0x01 |
| 11 |
#define SYN 0x02 |
| 12 |
#define RNG 0x04 |
| 13 |
#define TST 0x08 |
| 14 |
#define TRI 0x10 |
| 15 |
#define SAW 0x20 |
| 16 |
#define PUL 0x40 |
| 17 |
#define NOI 0x80 |
| 18 |
|
| 19 |
#define _HZ 0x10 |
| 20 |
#define DECSUS 0x40 |
| 21 |
#define ATK 0x80 |
| 22 |
|
| 23 |
// filter mode (high) |
| 24 |
#define LP 0x10 |
| 25 |
#define BP 0x20 |
| 26 |
#define HP 0x40 |
| 27 |
#define OFF3 0x80 |
| 28 |
|
| 29 |
#define waveforms_add_sample(_id,_s) \ |
| 30 |
sid->lastOut[_id]=(_s); |
| 31 |
|
| 32 |
const int Aexp[256] = { |
| 33 |
1, 30, 30, 30, 30, 30, 30, 16, 16, 16, 16, 16, 16, 16, 16, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, |
| 34 |
8, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, |
| 35 |
4, 4, 4, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 36 |
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 37 |
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 38 |
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 39 |
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 40 |
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 41 |
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 42 |
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; |
| 43 |
|
| 44 |
double cmbWF(int chn, int *wfa, int index, int differ6581, struct SID_globals *g) { |
| 45 |
if (differ6581 && g->model == 6581) |
| 46 |
index &= 0x7FF; |
| 47 |
|
| 48 |
return wfa[index]; |
| 49 |
} |
| 50 |
|
| 51 |
void cCmbWF(int *wfa, double bitmul, double bstr, double trh) { |
| 52 |
for (int i = 0; i < 4096; i++) { |
| 53 |
wfa[i] = 0; |
| 54 |
for (int j = 0; j < 12; j++) { |
| 55 |
double blvl = 0; |
| 56 |
for (int k = 0; k < 12; k++) { |
| 57 |
blvl += (bitmul / pow(bstr, abs(k - j))) * (((i >> k) & 1) - 0.5); |
| 58 |
} |
| 59 |
wfa[i] += (blvl >= trh) ? pow(2, j) : 0; |
| 60 |
} |
| 61 |
wfa[i] *= 12; |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
void dSID_init(struct SID_chip* sid, double clockRate, double samplingRate, int model, unsigned char init_wf) { |
| 66 |
if (model == 6581) { |
| 67 |
sid->g.model = 6581; |
| 68 |
} else { |
| 69 |
sid->g.model = 8580; |
| 70 |
} |
| 71 |
|
| 72 |
memset(sid->M,0,MemLen); |
| 73 |
memset(sid->SIDct, 0, sizeof(sid->SIDct)); |
| 74 |
for (int i = 0; i < 3; i++) { |
| 75 |
for (int j = 0; j < 3; j++) { |
| 76 |
sid->SIDct[i].ch[j].Ast = _HZ; |
| 77 |
sid->SIDct[i].ch[j].nLFSR = 0x7FFFF8; |
| 78 |
sid->SIDct[i].ch[j].prevwfout = 0; |
| 79 |
} |
| 80 |
sid->SIDct[i].ch[0].FSW = 1; |
| 81 |
sid->SIDct[i].ch[1].FSW = 2; |
| 82 |
sid->SIDct[i].ch[2].FSW = 4; |
| 83 |
} |
| 84 |
|
| 85 |
sid->g.ctfr = -2.0 * 3.14 * (12500.0 / 256.0) / samplingRate, |
| 86 |
sid->g.ctf_ratio_6581 = -2.0 * 3.14 * (samplingRate / 44100.0) * (20000.0 / 256.0) / samplingRate; |
| 87 |
sid->g.ckr = clockRate / samplingRate; |
| 88 |
|
| 89 |
const double bAprd[16] = {9, 32 * 1, 63 * 1, 95 * 1, 149 * 1, 220 * 1, |
| 90 |
267 * 1, 313 * 1, 392 * 1, 977 * 1, 1954 * 1, 3126 * 1, |
| 91 |
3907 * 1, 11720 * 1, 19532 * 1, 31251 * 1}; |
| 92 |
const int bAstp[16] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; |
| 93 |
memcpy(&sid->g.Aprd, &bAprd, sizeof(bAprd)); |
| 94 |
memcpy(&sid->g.Astp, &bAstp, sizeof(bAstp)); |
| 95 |
if (init_wf) { |
| 96 |
cCmbWF(sid->g.trsaw, 0.8, 2.4, 0.64); |
| 97 |
cCmbWF(sid->g.pusaw, 1.4, 1.9, 0.68); |
| 98 |
cCmbWF(sid->g.Pulsetrsaw, 0.8, 2.5, 0.64); |
| 99 |
|
| 100 |
for (int i = 0; i < 2048; i++) { |
| 101 |
double ctf = (double) i / 8.0 + 0.2; |
| 102 |
if (model == 8580) { |
| 103 |
ctf = 1 - exp(ctf * sid->g.ctfr); |
| 104 |
} else { |
| 105 |
if (ctf < 24) { |
| 106 |
ctf = 2.0 * sin(771.78 / samplingRate); |
| 107 |
} else { |
| 108 |
ctf = (44100.0 / samplingRate) - 1.263 * (44100.0 / samplingRate) * exp(ctf * sid->g.ctf_ratio_6581); |
| 109 |
} |
| 110 |
} |
| 111 |
sid->g.ctf_table[i] = ctf; |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
double prd0 = sid->g.ckr > 9 ? sid->g.ckr : 9; |
| 116 |
sid->g.Aprd[0] = prd0; |
| 117 |
sid->g.Astp[0] = ceil(prd0 / 9); |
| 118 |
|
| 119 |
for (int i=0; i<3; i++) { |
| 120 |
sid->fakeplp[i]=0; |
| 121 |
sid->fakepbp[i]=0; |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
double dSID_render(struct SID_chip* sid) { |
| 126 |
double flin = 0, output = 0; |
| 127 |
double wfout = 0; |
| 128 |
double step = 0; |
| 129 |
for (int chn = 0; chn < 3; chn++) { |
| 130 |
struct SIDVOICE *voic = &((struct SIDMEM *) (sid->M))->v[chn]; |
| 131 |
double pgt = (sid->SIDct->ch[chn].Ast & GAT); |
| 132 |
uint8_t ctrl = voic->control; |
| 133 |
uint8_t wf = ctrl & 0xF0; |
| 134 |
uint8_t test = ctrl & TST; |
| 135 |
uint8_t SR = voic->susres; |
| 136 |
double tmp = 0; |
| 137 |
if (pgt != (ctrl & GAT)) { |
| 138 |
if (pgt) { |
| 139 |
sid->SIDct->ch[chn].Ast &= 0xFF - (GAT | ATK | DECSUS); |
| 140 |
} else { |
| 141 |
sid->SIDct->ch[chn].Ast = (GAT | ATK | DECSUS); |
| 142 |
if ((SR & 0xF) > (sid->SIDct->ch[chn].pSR & 0xF)) |
| 143 |
tmp = 1; |
| 144 |
} |
| 145 |
} |
| 146 |
sid->SIDct->ch[chn].pSR = SR; |
| 147 |
sid->SIDct->ch[chn].rcnt += sid->g.ckr; |
| 148 |
if (sid->SIDct->ch[chn].rcnt >= 0x8000) |
| 149 |
sid->SIDct->ch[chn].rcnt -= 0x8000; |
| 150 |
|
| 151 |
double prd; |
| 152 |
|
| 153 |
if (sid->SIDct->ch[chn].Ast & ATK) { |
| 154 |
step = voic->attack; |
| 155 |
prd = sid->g.Aprd[(int) step]; |
| 156 |
} else if (sid->SIDct->ch[chn].Ast & DECSUS) { |
| 157 |
step = voic->decay; |
| 158 |
prd = sid->g.Aprd[(int) step]; |
| 159 |
} else { |
| 160 |
step = SR & 0xF; |
| 161 |
prd = sid->g.Aprd[(int) step]; |
| 162 |
} |
| 163 |
step = sid->g.Astp[(int) step]; |
| 164 |
if (sid->SIDct->ch[chn].rcnt >= prd && sid->SIDct->ch[chn].rcnt < prd + sid->g.ckr && |
| 165 |
tmp == 0) { |
| 166 |
sid->SIDct->ch[chn].rcnt -= prd; |
| 167 |
if ((sid->SIDct->ch[chn].Ast & ATK) || |
| 168 |
++sid->SIDct->ch[chn].expcnt == Aexp[(int) sid->SIDct->ch[chn].envcnt]) { |
| 169 |
if (!(sid->SIDct->ch[chn].Ast & _HZ)) { |
| 170 |
if (sid->SIDct->ch[chn].Ast & ATK) { |
| 171 |
sid->SIDct->ch[chn].envcnt += step; |
| 172 |
if (sid->SIDct->ch[chn].envcnt >= 0xFF) { |
| 173 |
sid->SIDct->ch[chn].envcnt = 0xFF; |
| 174 |
sid->SIDct->ch[chn].Ast &= 0xFF - ATK; |
| 175 |
} |
| 176 |
} else if (!(sid->SIDct->ch[chn].Ast & DECSUS) || |
| 177 |
sid->SIDct->ch[chn].envcnt > (SR >> 4) + (SR & 0xF0)) { |
| 178 |
sid->SIDct->ch[chn].envcnt -= step; |
| 179 |
if (sid->SIDct->ch[chn].envcnt <= 0 && |
| 180 |
sid->SIDct->ch[chn].envcnt + step != 0) { |
| 181 |
sid->SIDct->ch[chn].envcnt = 0; |
| 182 |
sid->SIDct->ch[chn].Ast |= _HZ; |
| 183 |
} |
| 184 |
} |
| 185 |
} |
| 186 |
sid->SIDct->ch[chn].expcnt = 0; |
| 187 |
} else { |
| 188 |
} |
| 189 |
} |
| 190 |
sid->SIDct->ch[chn].envcnt = (int) sid->SIDct->ch[chn].envcnt & 0xFF; |
| 191 |
double aAdd = (voic->freq_low + voic->freq_high * 256) * sid->g.ckr; |
| 192 |
if (test || ((ctrl & SYN) && sid->SIDct->sMSBrise)) { |
| 193 |
sid->SIDct->ch[chn].pacc = 0; |
| 194 |
} else { |
| 195 |
sid->SIDct->ch[chn].pacc += aAdd; |
| 196 |
if (sid->SIDct->ch[chn].pacc > 0xFFFFFF) |
| 197 |
sid->SIDct->ch[chn].pacc -= 0x1000000; |
| 198 |
} |
| 199 |
double MSB = (int) sid->SIDct->ch[chn].pacc & 0x800000; |
| 200 |
sid->SIDct->sMSBrise = (MSB > ((int) sid->SIDct->ch[chn].pracc & 0x800000)) ? 1 : 0; |
| 201 |
|
| 202 |
if (wf & NOI) { |
| 203 |
tmp = sid->SIDct->ch[chn].nLFSR; |
| 204 |
if ((((int) sid->SIDct->ch[chn].pacc & 0x100000) != |
| 205 |
((int) sid->SIDct->ch[chn].pracc & 0x100000)) || |
| 206 |
aAdd >= 0x100000) { |
| 207 |
step = ((int) tmp & 0x400000) ^ (((int) tmp & 0x20000) << 5); |
| 208 |
tmp = (((int) tmp << 1) + (step > 0 || test)) & 0x7FFFFF; |
| 209 |
sid->SIDct->ch[chn].nLFSR = tmp; |
| 210 |
} |
| 211 |
wfout = (wf & 0x70) ? 0 |
| 212 |
: (((int) tmp & 0x100000) >> 5) + (((int) tmp & 0x40000) >> 4) + |
| 213 |
(((int) tmp & 0x4000) >> 1) + (((int) tmp & 0x800) << 1) + |
| 214 |
(((int) tmp & 0x200) << 2) + (((int) tmp & 0x20) << 5) + |
| 215 |
(((int) tmp & 0x04) << 7) + (((int) tmp & 0x01) << 8); |
| 216 |
} else if (wf & PUL) { |
| 217 |
double pw = (voic->pw_low + (voic->pw_high) * 256) * 16; |
| 218 |
tmp = (int) aAdd >> 9; |
| 219 |
if (0 < pw && pw < tmp) |
| 220 |
pw = tmp; |
| 221 |
tmp = (int) tmp ^ 0xFFFF; |
| 222 |
if (pw > tmp) |
| 223 |
pw = tmp; |
| 224 |
tmp = (int) sid->SIDct->ch[chn].pacc >> 8; |
| 225 |
if (wf == PUL) { |
| 226 |
int lel = ((int) aAdd >> 16); |
| 227 |
if (lel > 0) { |
| 228 |
step = 256.0 / (double) lel; |
| 229 |
} else { |
| 230 |
step = INFINITY; |
| 231 |
} |
| 232 |
if (test) |
| 233 |
wfout = 0xFFFF; |
| 234 |
else if (tmp < pw) { |
| 235 |
double lim = (0xFFFF - pw) * step; |
| 236 |
if (lim > 0xFFFF) |
| 237 |
lim = 0xFFFF; |
| 238 |
wfout = lim - (pw - tmp) * step; |
| 239 |
if (wfout < 0) |
| 240 |
wfout = 0; |
| 241 |
} else { |
| 242 |
double lim = pw * step; |
| 243 |
if (lim > 0xFFFF) |
| 244 |
lim = 0xFFFF; |
| 245 |
wfout = (0xFFFF - tmp) * step - lim; |
| 246 |
if (wfout >= 0) |
| 247 |
wfout = 0xFFFF; |
| 248 |
wfout = (int) wfout & 0xFFFF; |
| 249 |
} |
| 250 |
} else { |
| 251 |
wfout = (tmp >= pw || test) ? 0xFFFF : 0; |
| 252 |
if (wf & TRI) { |
| 253 |
if (wf & SAW) { |
| 254 |
wfout = |
| 255 |
(wfout) ? cmbWF(chn, sid->g.Pulsetrsaw, (int) tmp >> 4, 1, &sid->g) : 0; |
| 256 |
} else { |
| 257 |
tmp = (int) sid->SIDct->ch[chn].pacc ^ (ctrl & RNG ? sid->SIDct->sMSB : 0); |
| 258 |
wfout = |
| 259 |
(wfout) |
| 260 |
? cmbWF(chn, sid->g.pusaw, |
| 261 |
((int) tmp ^ ((int) tmp & 0x800000 ? 0xFFFFFF : 0)) >> 11, |
| 262 |
0, &sid->g) |
| 263 |
: 0; |
| 264 |
} |
| 265 |
} else if (wf & SAW) |
| 266 |
wfout = (wfout) ? cmbWF(chn, sid->g.pusaw, (int) tmp >> 4, 1, &sid->g) : 0; |
| 267 |
} |
| 268 |
} else if (wf & SAW) { |
| 269 |
wfout = (int) sid->SIDct->ch[chn].pacc >> 8; |
| 270 |
if (wf & TRI) |
| 271 |
wfout = cmbWF(chn, sid->g.trsaw, (int) wfout >> 4, 1, &sid->g); |
| 272 |
else { |
| 273 |
step = aAdd / 0x1200000; |
| 274 |
wfout += wfout * step; |
| 275 |
if (wfout > 0xFFFF) |
| 276 |
wfout = 0xFFFF - (wfout - 0x10000) / step; |
| 277 |
} |
| 278 |
} else if (wf & TRI) { |
| 279 |
tmp = (int) sid->SIDct->ch[chn].pacc ^ (ctrl & RNG ? sid->SIDct->sMSB : 0); |
| 280 |
wfout = ((int) tmp ^ ((int) tmp & 0x800000 ? 0xFFFFFF : 0)) >> 7; |
| 281 |
} |
| 282 |
if (wf) |
| 283 |
sid->SIDct->ch[chn].prevwfout = wfout; |
| 284 |
else { |
| 285 |
wfout = sid->SIDct->ch[chn].prevwfout; |
| 286 |
} |
| 287 |
sid->SIDct->ch[chn].pracc = sid->SIDct->ch[chn].pacc; |
| 288 |
sid->SIDct->sMSB = MSB; |
| 289 |
// double preflin = flin; |
| 290 |
if ((sid->mute_mask & (1 << chn))) { |
| 291 |
if (sid->M[0x17] & sid->SIDct->ch[chn].FSW) { |
| 292 |
double chnout = (wfout - 0x8000) * (sid->SIDct->ch[chn].envcnt / 256); |
| 293 |
flin += chnout; |
| 294 |
|
| 295 |
// fake filter for solo waveform ahead |
| 296 |
// mostly copypasted from below |
| 297 |
double fakeflin = chnout; |
| 298 |
double fakeflout = 0; |
| 299 |
double ctf = sid->g.ctf_table[((sid->M[0x15]&7)|(sid->M[0x16]<<3))&0x7ff]; |
| 300 |
double reso; |
| 301 |
if (sid->g.model == 8580) { |
| 302 |
reso = pow(2, ((double) (4 - (double) (sid->M[0x17] >> 4)) / 8)); |
| 303 |
} else { |
| 304 |
reso = (sid->M[0x17] > 0x5F) ? 8.0 / (double) (sid->M[0x17] >> 4) : 1.41; |
| 305 |
} |
| 306 |
double tmp = fakeflin + sid->fakepbp[chn] * reso + sid->fakeplp[chn]; |
| 307 |
if (sid->M[0x18] & HP) |
| 308 |
fakeflout -= tmp; |
| 309 |
tmp = sid->fakepbp[chn] - tmp * ctf; |
| 310 |
sid->fakepbp[chn] = tmp; |
| 311 |
if (sid->M[0x18] & BP) |
| 312 |
fakeflout -= tmp; |
| 313 |
tmp = sid->fakeplp[chn] + tmp * ctf; |
| 314 |
sid->fakeplp[chn] = tmp; |
| 315 |
if (sid->M[0x18] & LP) |
| 316 |
fakeflout += tmp; |
| 317 |
|
| 318 |
double wf_out = (fakeflout / SID_OUT_SCALE) * (sid->M[0x18] & 0xF) * 65535; |
| 319 |
waveforms_add_sample(chn, wf_out); |
| 320 |
} else if ((chn % 3) != 2 || !(sid->M[0x18] & OFF3)) { |
| 321 |
double chnout = (wfout - 0x8000) * (sid->SIDct->ch[chn].envcnt / 256); |
| 322 |
output += chnout; |
| 323 |
|
| 324 |
double wf_out = (chnout / SID_OUT_SCALE) * (sid->M[0x18] & 0xF) * 65535; |
| 325 |
waveforms_add_sample(chn, wf_out); |
| 326 |
} |
| 327 |
} else { |
| 328 |
waveforms_add_sample(chn, 0); |
| 329 |
} |
| 330 |
} |
| 331 |
int M1 = 0; |
| 332 |
if (M1 & 3) |
| 333 |
sid->M[0x1B] = (int) wfout >> 8; |
| 334 |
sid->M[0x1C] = sid->SIDct->ch[2].envcnt; |
| 335 |
|
| 336 |
double ctf = sid->g.ctf_table[((sid->M[0x15]&7)|(sid->M[0x16]<<3))&0x7ff]; |
| 337 |
double reso; |
| 338 |
if (sid->g.model == 8580) { |
| 339 |
reso = pow(2, ((double) (4 - (double) (sid->M[0x17] >> 4)) / 8)); |
| 340 |
} else { |
| 341 |
reso = (sid->M[0x17] > 0x5F) ? 8.0 / (double) (sid->M[0x17] >> 4) : 1.41; |
| 342 |
} |
| 343 |
|
| 344 |
double tmp = flin + sid->SIDct->pbp * reso + sid->SIDct->plp; |
| 345 |
if (sid->M[0x18] & HP) |
| 346 |
output -= tmp; |
| 347 |
tmp = sid->SIDct->pbp - tmp * ctf; |
| 348 |
sid->SIDct->pbp = tmp; |
| 349 |
if (sid->M[0x18] & BP) |
| 350 |
output -= tmp; |
| 351 |
tmp = sid->SIDct->plp + tmp * ctf; |
| 352 |
sid->SIDct->plp = tmp; |
| 353 |
if (sid->M[0x18] & LP) |
| 354 |
output += tmp; |
| 355 |
|
| 356 |
// tildearrow: 6581 bias for software PCM |
| 357 |
if (sid->g.model==6581) { |
| 358 |
return (0.03 + (output / SID_OUT_SCALE)) * (sid->M[0x18] & 0xF); |
| 359 |
} |
| 360 |
return (output / SID_OUT_SCALE) * (sid->M[0x18] & 0xF); |
| 361 |
} |
| 362 |
|
| 363 |
void dSID_setMuteMask(struct SID_chip* sid, int mute_mask) { |
| 364 |
sid->mute_mask = mute_mask; |
| 365 |
} |
| 366 |
|
| 367 |
float dSID_getVolume(struct SID_chip* sid, int channel) { |
| 368 |
if ((sid->M[0x18] & 0xF) == 0) |
| 369 |
return 0; |
| 370 |
return sid->SIDct[0].ch[channel].envcnt / 256.0f; |
| 371 |
} |
| 372 |
|
| 373 |
void dSID_write(struct SID_chip* sid, unsigned char addr, unsigned char val) { |
| 374 |
sid->M[addr&0x1f]=val; |
| 375 |
} |