23#include "resid-config.h"
317 enum { value = summer_offset<i - 1>::value + ((2 + i - 1) << 16) };
321struct summer_offset<0>
330 enum { value = mixer_offset<i - 1>::value + ((i - 1) << 16) };
334struct mixer_offset<1>
340struct mixer_offset<0>
351 void enable_filter(
bool enable);
352 void adjust_filter_bias(
double dac_bias);
353 void set_chip_model(chip_model model);
354 void set_voice_mask(reg4 mask);
356 void clock(
int voice1,
int voice2,
int voice3);
357 void clock(cycle_count delta_t,
int voice1,
int voice2,
int voice3);
361 void writeFC_LO(reg8);
362 void writeFC_HI(reg8);
363 void writeRES_FILT(reg8);
364 void writeMODE_VOL(reg8);
367 void input(
short sample);
417 int Vddt_Vw_2, Vw_bias;
423 chip_model sid_model;
442 unsigned short opamp_rev[1 << 16];
444 unsigned short summer[summer_offset<5>::value];
445 unsigned short gain[16][1 << 16];
446 unsigned short mixer[mixer_offset<8>::value];
448 unsigned short f0_dac[1 << 11];
451 int solve_gain(opamp_t* opamp,
int n,
int vi_t,
int& x, model_filter_t& mf);
452 int solve_integrate_6581(
int dt,
int vi_t,
int& x,
int& vc, model_filter_t& mf);
455 static unsigned short vcr_kVg[1 << 16];
456 static unsigned short vcr_n_Ids_term[1 << 16];
458 static model_filter_t model_filter[2];
470#if RESID_INLINING || defined(RESID_FILTER_CC)
476void Filter::clock(
int voice1,
int voice2,
int voice3)
478 model_filter_t& f = model_filter[sid_model];
480 v1 = (voice1*f.voice_scale_s14 >> 18) + f.voice_DC;
481 v2 = (voice2*f.voice_scale_s14 >> 18) + f.voice_DC;
482 v3 = (voice3*f.voice_scale_s14 >> 18) + f.voice_DC;
491 offset = summer_offset<0>::value;
495 offset = summer_offset<1>::value;
499 offset = summer_offset<1>::value;
503 offset = summer_offset<2>::value;
507 offset = summer_offset<1>::value;
511 offset = summer_offset<2>::value;
515 offset = summer_offset<2>::value;
519 offset = summer_offset<3>::value;
523 offset = summer_offset<1>::value;
527 offset = summer_offset<2>::value;
531 offset = summer_offset<2>::value;
535 offset = summer_offset<3>::value;
539 offset = summer_offset<2>::value;
543 offset = summer_offset<3>::value;
547 offset = summer_offset<3>::value;
550 Vi = ve + v3 + v2 + v1;
551 offset = summer_offset<4>::value;
556 if (sid_model == 0) {
558 Vlp = solve_integrate_6581(1, Vbp, Vlp_x, Vlp_vc, f);
559 Vbp = solve_integrate_6581(1, Vhp, Vbp_x, Vbp_vc, f);
560 Vhp = f.summer[offset + f.gain[_8_div_Q][Vbp] + Vlp + Vi];
568 int dVbp = w0*(Vhp >> 4) >> 16;
569 int dVlp = w0*(Vbp >> 4) >> 16;
572 Vhp = (Vbp*_1024_div_Q >> 10) - Vlp - Vi;
580void Filter::clock(cycle_count delta_t,
int voice1,
int voice2,
int voice3)
582 model_filter_t& f = model_filter[sid_model];
584 v1 = (voice1*f.voice_scale_s14 >> 18) + f.voice_DC;
585 v2 = (voice2*f.voice_scale_s14 >> 18) + f.voice_DC;
586 v3 = (voice3*f.voice_scale_s14 >> 18) + f.voice_DC;
592 if (unlikely(!enabled)) {
603 offset = summer_offset<0>::value;
607 offset = summer_offset<1>::value;
611 offset = summer_offset<1>::value;
615 offset = summer_offset<2>::value;
619 offset = summer_offset<1>::value;
623 offset = summer_offset<2>::value;
627 offset = summer_offset<2>::value;
631 offset = summer_offset<3>::value;
635 offset = summer_offset<1>::value;
639 offset = summer_offset<2>::value;
643 offset = summer_offset<2>::value;
647 offset = summer_offset<3>::value;
651 offset = summer_offset<2>::value;
655 offset = summer_offset<3>::value;
659 offset = summer_offset<3>::value;
662 Vi = ve + v3 + v2 + v1;
663 offset = summer_offset<4>::value;
669 cycle_count delta_t_flt = 3;
671 if (sid_model == 0) {
674 if (unlikely(delta_t < delta_t_flt)) {
675 delta_t_flt = delta_t;
679 Vlp = solve_integrate_6581(delta_t_flt, Vbp, Vlp_x, Vlp_vc, f);
680 Vbp = solve_integrate_6581(delta_t_flt, Vhp, Vbp_x, Vbp_vc, f);
681 Vhp = f.summer[offset + f.gain[_8_div_Q][Vbp] + Vlp + Vi];
683 delta_t -= delta_t_flt;
689 if (delta_t < delta_t_flt) {
690 delta_t_flt = delta_t;
698 int w0_delta_t = w0*delta_t_flt >> 2;
700 int dVbp = w0_delta_t*(Vhp >> 4) >> 14;
701 int dVlp = w0_delta_t*(Vbp >> 4) >> 14;
704 Vhp = (Vbp*_1024_div_Q >> 10) - Vlp - Vi;
706 delta_t -= delta_t_flt;
716void Filter::input(
short sample)
727 model_filter_t& f = model_filter[sid_model];
728 ve = (sample*f.voice_scale_s14*3 >> 14) + f.mixer[0];
736short Filter::output()
738 model_filter_t& f = model_filter[sid_model];
763 switch (mix & 0x7f) {
766 offset = mixer_offset<0>::value;
770 offset = mixer_offset<1>::value;
774 offset = mixer_offset<1>::value;
778 offset = mixer_offset<2>::value;
782 offset = mixer_offset<1>::value;
786 offset = mixer_offset<2>::value;
790 offset = mixer_offset<2>::value;
794 offset = mixer_offset<3>::value;
798 offset = mixer_offset<1>::value;
802 offset = mixer_offset<2>::value;
806 offset = mixer_offset<2>::value;
810 offset = mixer_offset<3>::value;
814 offset = mixer_offset<2>::value;
818 offset = mixer_offset<3>::value;
822 offset = mixer_offset<3>::value;
825 Vi = ve + v3 + v2 + v1;
826 offset = mixer_offset<4>::value;
830 offset = mixer_offset<1>::value;
834 offset = mixer_offset<2>::value;
838 offset = mixer_offset<2>::value;
842 offset = mixer_offset<3>::value;
846 offset = mixer_offset<2>::value;
850 offset = mixer_offset<3>::value;
854 offset = mixer_offset<3>::value;
857 Vi = Vlp + v3 + v2 + v1;
858 offset = mixer_offset<4>::value;
862 offset = mixer_offset<2>::value;
866 offset = mixer_offset<3>::value;
870 offset = mixer_offset<3>::value;
873 Vi = Vlp + ve + v2 + v1;
874 offset = mixer_offset<4>::value;
878 offset = mixer_offset<3>::value;
881 Vi = Vlp + ve + v3 + v1;
882 offset = mixer_offset<4>::value;
885 Vi = Vlp + ve + v3 + v2;
886 offset = mixer_offset<4>::value;
889 Vi = Vlp + ve + v3 + v2 + v1;
890 offset = mixer_offset<5>::value;
894 offset = mixer_offset<1>::value;
898 offset = mixer_offset<2>::value;
902 offset = mixer_offset<2>::value;
906 offset = mixer_offset<3>::value;
910 offset = mixer_offset<2>::value;
914 offset = mixer_offset<3>::value;
918 offset = mixer_offset<3>::value;
921 Vi = Vbp + v3 + v2 + v1;
922 offset = mixer_offset<4>::value;
926 offset = mixer_offset<2>::value;
930 offset = mixer_offset<3>::value;
934 offset = mixer_offset<3>::value;
937 Vi = Vbp + ve + v2 + v1;
938 offset = mixer_offset<4>::value;
942 offset = mixer_offset<3>::value;
945 Vi = Vbp + ve + v3 + v1;
946 offset = mixer_offset<4>::value;
949 Vi = Vbp + ve + v3 + v2;
950 offset = mixer_offset<4>::value;
953 Vi = Vbp + ve + v3 + v2 + v1;
954 offset = mixer_offset<5>::value;
958 offset = mixer_offset<2>::value;
962 offset = mixer_offset<3>::value;
966 offset = mixer_offset<3>::value;
969 Vi = Vbp + Vlp + v2 + v1;
970 offset = mixer_offset<4>::value;
974 offset = mixer_offset<3>::value;
977 Vi = Vbp + Vlp + v3 + v1;
978 offset = mixer_offset<4>::value;
981 Vi = Vbp + Vlp + v3 + v2;
982 offset = mixer_offset<4>::value;
985 Vi = Vbp + Vlp + v3 + v2 + v1;
986 offset = mixer_offset<5>::value;
990 offset = mixer_offset<3>::value;
993 Vi = Vbp + Vlp + ve + v1;
994 offset = mixer_offset<4>::value;
997 Vi = Vbp + Vlp + ve + v2;
998 offset = mixer_offset<4>::value;
1001 Vi = Vbp + Vlp + ve + v2 + v1;
1002 offset = mixer_offset<5>::value;
1005 Vi = Vbp + Vlp + ve + v3;
1006 offset = mixer_offset<4>::value;
1009 Vi = Vbp + Vlp + ve + v3 + v1;
1010 offset = mixer_offset<5>::value;
1013 Vi = Vbp + Vlp + ve + v3 + v2;
1014 offset = mixer_offset<5>::value;
1017 Vi = Vbp + Vlp + ve + v3 + v2 + v1;
1018 offset = mixer_offset<6>::value;
1022 offset = mixer_offset<1>::value;
1026 offset = mixer_offset<2>::value;
1030 offset = mixer_offset<2>::value;
1034 offset = mixer_offset<3>::value;
1038 offset = mixer_offset<2>::value;
1042 offset = mixer_offset<3>::value;
1046 offset = mixer_offset<3>::value;
1049 Vi = Vhp + v3 + v2 + v1;
1050 offset = mixer_offset<4>::value;
1054 offset = mixer_offset<2>::value;
1058 offset = mixer_offset<3>::value;
1062 offset = mixer_offset<3>::value;
1065 Vi = Vhp + ve + v2 + v1;
1066 offset = mixer_offset<4>::value;
1070 offset = mixer_offset<3>::value;
1073 Vi = Vhp + ve + v3 + v1;
1074 offset = mixer_offset<4>::value;
1077 Vi = Vhp + ve + v3 + v2;
1078 offset = mixer_offset<4>::value;
1081 Vi = Vhp + ve + v3 + v2 + v1;
1082 offset = mixer_offset<5>::value;
1086 offset = mixer_offset<2>::value;
1089 Vi = Vhp + Vlp + v1;
1090 offset = mixer_offset<3>::value;
1093 Vi = Vhp + Vlp + v2;
1094 offset = mixer_offset<3>::value;
1097 Vi = Vhp + Vlp + v2 + v1;
1098 offset = mixer_offset<4>::value;
1101 Vi = Vhp + Vlp + v3;
1102 offset = mixer_offset<3>::value;
1105 Vi = Vhp + Vlp + v3 + v1;
1106 offset = mixer_offset<4>::value;
1109 Vi = Vhp + Vlp + v3 + v2;
1110 offset = mixer_offset<4>::value;
1113 Vi = Vhp + Vlp + v3 + v2 + v1;
1114 offset = mixer_offset<5>::value;
1117 Vi = Vhp + Vlp + ve;
1118 offset = mixer_offset<3>::value;
1121 Vi = Vhp + Vlp + ve + v1;
1122 offset = mixer_offset<4>::value;
1125 Vi = Vhp + Vlp + ve + v2;
1126 offset = mixer_offset<4>::value;
1129 Vi = Vhp + Vlp + ve + v2 + v1;
1130 offset = mixer_offset<5>::value;
1133 Vi = Vhp + Vlp + ve + v3;
1134 offset = mixer_offset<4>::value;
1137 Vi = Vhp + Vlp + ve + v3 + v1;
1138 offset = mixer_offset<5>::value;
1141 Vi = Vhp + Vlp + ve + v3 + v2;
1142 offset = mixer_offset<5>::value;
1145 Vi = Vhp + Vlp + ve + v3 + v2 + v1;
1146 offset = mixer_offset<6>::value;
1150 offset = mixer_offset<2>::value;
1153 Vi = Vhp + Vbp + v1;
1154 offset = mixer_offset<3>::value;
1157 Vi = Vhp + Vbp + v2;
1158 offset = mixer_offset<3>::value;
1161 Vi = Vhp + Vbp + v2 + v1;
1162 offset = mixer_offset<4>::value;
1165 Vi = Vhp + Vbp + v3;
1166 offset = mixer_offset<3>::value;
1169 Vi = Vhp + Vbp + v3 + v1;
1170 offset = mixer_offset<4>::value;
1173 Vi = Vhp + Vbp + v3 + v2;
1174 offset = mixer_offset<4>::value;
1177 Vi = Vhp + Vbp + v3 + v2 + v1;
1178 offset = mixer_offset<5>::value;
1181 Vi = Vhp + Vbp + ve;
1182 offset = mixer_offset<3>::value;
1185 Vi = Vhp + Vbp + ve + v1;
1186 offset = mixer_offset<4>::value;
1189 Vi = Vhp + Vbp + ve + v2;
1190 offset = mixer_offset<4>::value;
1193 Vi = Vhp + Vbp + ve + v2 + v1;
1194 offset = mixer_offset<5>::value;
1197 Vi = Vhp + Vbp + ve + v3;
1198 offset = mixer_offset<4>::value;
1201 Vi = Vhp + Vbp + ve + v3 + v1;
1202 offset = mixer_offset<5>::value;
1205 Vi = Vhp + Vbp + ve + v3 + v2;
1206 offset = mixer_offset<5>::value;
1209 Vi = Vhp + Vbp + ve + v3 + v2 + v1;
1210 offset = mixer_offset<6>::value;
1213 Vi = Vhp + Vbp + Vlp;
1214 offset = mixer_offset<3>::value;
1217 Vi = Vhp + Vbp + Vlp + v1;
1218 offset = mixer_offset<4>::value;
1221 Vi = Vhp + Vbp + Vlp + v2;
1222 offset = mixer_offset<4>::value;
1225 Vi = Vhp + Vbp + Vlp + v2 + v1;
1226 offset = mixer_offset<5>::value;
1229 Vi = Vhp + Vbp + Vlp + v3;
1230 offset = mixer_offset<4>::value;
1233 Vi = Vhp + Vbp + Vlp + v3 + v1;
1234 offset = mixer_offset<5>::value;
1237 Vi = Vhp + Vbp + Vlp + v3 + v2;
1238 offset = mixer_offset<5>::value;
1241 Vi = Vhp + Vbp + Vlp + v3 + v2 + v1;
1242 offset = mixer_offset<6>::value;
1245 Vi = Vhp + Vbp + Vlp + ve;
1246 offset = mixer_offset<4>::value;
1249 Vi = Vhp + Vbp + Vlp + ve + v1;
1250 offset = mixer_offset<5>::value;
1253 Vi = Vhp + Vbp + Vlp + ve + v2;
1254 offset = mixer_offset<5>::value;
1257 Vi = Vhp + Vbp + Vlp + ve + v2 + v1;
1258 offset = mixer_offset<6>::value;
1261 Vi = Vhp + Vbp + Vlp + ve + v3;
1262 offset = mixer_offset<5>::value;
1265 Vi = Vhp + Vbp + Vlp + ve + v3 + v1;
1266 offset = mixer_offset<6>::value;
1269 Vi = Vhp + Vbp + Vlp + ve + v3 + v2;
1270 offset = mixer_offset<6>::value;
1273 Vi = Vhp + Vbp + Vlp + ve + v3 + v2 + v1;
1274 offset = mixer_offset<7>::value;
1279 if (sid_model == 0) {
1280 return (
short)(f.gain[vol][f.mixer[offset + Vi]] - (1 << 15));
1286 int tmp = Vi*(int)vol >> 4;
1287 if (tmp < -32768) tmp = -32768;
1288 if (tmp > 32767) tmp = 32767;
1289 return (
short)tmp; }
1332int Filter::solve_gain(opamp_t* opamp,
int n,
int vi,
int& x, model_filter_t& mf)
1341 int ak = mf.ak, bk = mf.bk;
1343 int a = n + (1 << 7);
1346 if (b_vi < 0) b_vi = 0;
1347 int c = n*int(
unsigned(b_vi)*
unsigned(b_vi) >> 12);
1353 int vx = opamp[x].vx;
1354 int dvx = opamp[x].dvx;
1359 int vo = vx + (x << 1) - (1 << 16);
1360 if (vo >= (1 << 16)) {
1367 if (b_vx < 0) b_vx = 0;
1369 if (b_vo < 0) b_vo = 0;
1371 int f = a*int(
unsigned(b_vx)*
unsigned(b_vx) >> 12) - c - int(
unsigned(b_vo)*
unsigned(b_vo) >> 5);
1373 int df = ((b_vo*(dvx + (1 << 11)) >> 1) - (a*(b_vx*dvx >> 8))) >> 14;
1381 if (unlikely(x == xk)) {
1396 if (unlikely(x <= ak) || unlikely(x >= bk)) {
1399 if (unlikely(x == ak)) {
1528int Filter::solve_integrate_6581(
int dt,
int vi,
int& vx,
int& vc, model_filter_t& mf)
1535 int kVddt = mf.kVddt;
1538 unsigned int Vgst = kVddt - vx;
1539 unsigned int Vgdt = kVddt - vi;
1540 unsigned int Vgdt_2 = Vgdt*Vgdt;
1543 int n_I_snake = mf.n_snake*(int(Vgst*Vgst - Vgdt_2) >> 15);
1547 int kVg = vcr_kVg[(Vddt_Vw_2 + (Vgdt_2 >> 1)) >> 16];
1551 if (Vgs < 0) Vgs = 0;
1553 if (Vgd < 0) Vgd = 0;
1556 int n_I_vcr = int(
unsigned(vcr_n_Ids_term[Vgs] - vcr_n_Ids_term[Vgd]) << 15);
1559 vc -= (n_I_snake + n_I_vcr)*dt;
1572 vx = mf.opamp_rev[(vc >> 15) + (1 << 15)];
1575 return vx + (vc >> 14);