-
Notifications
You must be signed in to change notification settings - Fork 1
/
mzpokeysnd.c
2379 lines (2063 loc) · 60.4 KB
/
mzpokeysnd.c
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
/*
* mzpokeysnd.c - POKEY sound chip emulation, v1.6
*
* Copyright (C) 2002 Michael Borisov
* Copyright (C) 2002-2005 Atari800 development team (see DOC/CREDITS)
*
* This file is part of the Atari800 emulator project which emulates
* the Atari 400, 800, 800XL, 130XE, and 5200 8-bit computers.
*
* Atari800 is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Atari800 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Atari800; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "config.h"
#include <stdlib.h>
#include <math.h>
#ifdef ASAP /* external project, see http://asap.sf.net */
#include "asap_internal.h"
#else
#include "atari.h"
#endif
#include "mzpokeysnd.h"
#include "pokeysnd.h"
#include "remez.h"
#include "antic.h"
#include "gtia.h"
#ifdef NONLINEAR_MIXING
static const double pokeymix[61] = { /* Nonlinear POKEY mixing array */
0.000000, 5.169146, 10.157015, 15.166247,
20.073793, 24.927443, 29.728237, 34.495266,
39.181262, 43.839780, 48.429508, 52.932530,
57.327319, 61.586304, 65.673220, 69.547672,
73.207846, 76.594474, 79.739231, 82.631161,
85.300361, 87.750638, 90.020656, 92.108334,
94.051256, 95.848478, 97.521287, 99.080719,
100.540674, 101.902750, 103.185339, 104.375596,
105.491149, 106.523735, 107.473511, 108.361458,
109.185669, 109.962251, 110.685574, 111.367150,
112.008476, 112.612760, 113.185603, 113.722735,
114.227904, 114.712206, 115.171007, 115.605730,
116.024396, 116.416097, 116.803169, 117.155108,
117.532921, 117.835494, 118.196180, 118.502785,
118.825177, 119.138170, 119.421378, 119.734493,
120.000000};
#endif
#define SND_FILTER_SIZE 2048
#define NPOKEYS 2
/* M_PI was not defined in MSVC headers */
#ifndef M_PI
# define M_PI 3.141592653589793
#endif
static unsigned int num_cur_pokeys = 0;
/* Filter */
static unsigned sample_rate; /* Hz */
static unsigned pokey_frq; /* Hz - for easier resampling */
static int filter_size;
static double filter_data[SND_FILTER_SIZE];
static unsigned audible_frq;
static const unsigned long pokey_frq_ideal = 1789790; /* Hz - True */
#if 0
static const int filter_size_44 = 1274;
static const int filter_size_44_8 = 884;
static const int filter_size_22 = 1239;
static const int filter_size_22_8 = 893;
static const int filter_size_11 = 1305;
static const int filter_size_11_8 = 937;
static const int filter_size_48 = 898;
static const int filter_size_48_8 = 626;
static const int filter_size_8 = 1322;
static const int filter_size_8_8 = 1214;
#endif
/* Flags and quality */
static int snd_flags = 0;
static int snd_quality = 0;
/* Poly tables */
static unsigned char poly4tbl[15];
static unsigned char poly5tbl[31];
static unsigned char poly17tbl[131071];
static unsigned char poly9tbl[511];
struct stPokeyState;
typedef int (*readout_t)(struct stPokeyState* ps);
typedef void (*event_t)(struct stPokeyState* ps, char p5v, char p4v, char p917v);
#ifdef NONLINEAR_MIXING
/* Change queue event value type */
typedef double qev_t;
#else
typedef unsigned char qev_t;
#endif
/* State variables for single Pokey Chip */
typedef struct stPokeyState
{
/* Poly positions */
unsigned int poly4pos;
unsigned int poly5pos;
unsigned int poly17pos;
unsigned int poly9pos;
/* Change queue */
qev_t ovola;
int qet[1322]; /* maximal length of filter */
qev_t qev[1322];
int qebeg;
int qeend;
/* Main divider (64khz/15khz) */
unsigned char mdivk; /* 28 for 64khz, 114 for 15khz */
/* Main switches */
unsigned char selpoly9;
unsigned char c0_hf;
unsigned char c1_f0;
unsigned char c2_hf;
unsigned char c3_f2;
/* SKCTL for two-tone mode */
int skctl;
/* Main output state */
qev_t outvol_all;
unsigned char forcero; /* Force readout */
/* channel 0 state */
readout_t readout_0;
event_t event_0;
unsigned long c0divpos;
unsigned long c0divstart; /* AUDF0 recalculated */
unsigned long c0divstart_p; /* start value when c1_f0 */
unsigned short c0diva; /* AUDF0 register */
unsigned char c0ctl; /* AUDC0 register */
unsigned char c0t1; /* D - 5bit, Q goes to sw3 */
unsigned char c0t2; /* D - out sw2, Q goes to sw4 and t3 */
unsigned char c0t3; /* D - out t2, q goes to xor */
unsigned char c0sw1; /* in1 - 4bit, in2 - 17bit, out goes to sw2 */
unsigned char c0sw2; /* in1 - /Q t2, in2 - out sw1, out goes to t2 */
unsigned char c0sw3; /* in1 - +5, in2 - Q t1, out goes to C t2 */
unsigned char c0sw4; /* hi-pass sw */
unsigned char c0vo; /* volume only */
#ifndef NONLINEAR_MIXING
unsigned char c0stop; /* channel counter stopped */
#endif
int vol0;
int outvol_0;
/* channel 1 state */
readout_t readout_1;
event_t event_1;
unsigned long c1divpos;
unsigned long c1divstart;
unsigned short c1diva;
unsigned char c1ctl;
unsigned char c1t1;
unsigned char c1t2;
unsigned char c1t3;
unsigned char c1sw1;
unsigned char c1sw2;
unsigned char c1sw3;
unsigned char c1sw4;
unsigned char c1vo;
#ifndef NONLINEAR_MIXING
unsigned char c1stop; /* channel counter stopped */
#endif
int vol1;
int outvol_1;
/* channel 2 state */
readout_t readout_2;
event_t event_2;
unsigned long c2divpos;
unsigned long c2divstart;
unsigned long c2divstart_p; /* start value when c1_f0 */
unsigned short c2diva;
unsigned char c2ctl;
unsigned char c2t1;
unsigned char c2t2;
unsigned char c2sw1;
unsigned char c2sw2;
unsigned char c2sw3;
unsigned char c2vo;
#ifndef NONLINEAR_MIXING
unsigned char c2stop; /* channel counter stopped */
#endif
int vol2;
int outvol_2;
/* channel 3 state */
readout_t readout_3;
event_t event_3;
unsigned long c3divpos;
unsigned long c3divstart;
unsigned short c3diva;
unsigned char c3ctl;
unsigned char c3t1;
unsigned char c3t2;
unsigned char c3sw1;
unsigned char c3sw2;
unsigned char c3sw3;
unsigned char c3vo;
#ifndef NONLINEAR_MIXING
unsigned char c3stop; /* channel counter stopped */
#endif
int vol3;
int outvol_3;
} PokeyState;
PokeyState pokey_states[NPOKEYS];
/* Forward declarations for ResetPokeyState */
static int readout0_normal(PokeyState* ps);
static void event0_pure(PokeyState* ps, char p5v, char p4v, char p917v);
static int readout1_normal(PokeyState* ps);
static void event1_pure(PokeyState* ps, char p5v, char p4v, char p917v);
static int readout2_normal(PokeyState* ps);
static void event2_pure(PokeyState* ps, char p5v, char p4v, char p917v);
static int readout3_normal(PokeyState* ps);
static void event3_pure(PokeyState* ps, char p5v, char p4v, char p917v);
static void ResetPokeyState(PokeyState* ps)
{
/* Poly positions */
ps->poly4pos = 0;
ps->poly5pos = 0;
ps->poly9pos = 0;
ps->poly17pos = 0;
/* Change queue */
ps->ovola = 0;
ps->qebeg = 0;
ps->qeend = 0;
/* Global Pokey controls */
ps->mdivk = 28;
ps->selpoly9 = 0;
ps->c0_hf = 0;
ps->c1_f0 = 0;
ps->c2_hf = 0;
ps->c3_f2 = 0;
/* SKCTL for two-tone mode */
ps->skctl = 0;
ps->outvol_all = 0;
ps->forcero = 0;
/* Channel 0 state */
ps->readout_0 = readout0_normal;
ps->event_0 = event0_pure;
ps->c0divpos = 1000;
ps->c0divstart = 1000;
ps->c0divstart_p = 1000;
ps->c0diva = 255;
ps->c0ctl = 0;
ps->c0t1 = 0;
ps->c0t2 = 0;
ps->c0t3 = 0;
ps->c0sw1 = 0;
ps->c0sw2 = 0;
ps->c0sw3 = 0;
ps->c0sw4 = 0;
ps->c0vo = 1;
#ifndef NONLINEAR_MIXING
ps->c0stop = 1;
#endif
ps->vol0 = 0;
ps->outvol_0 = 0;
/* Channel 1 state */
ps->readout_1 = readout1_normal;
ps->event_1 = event1_pure;
ps->c1divpos = 1000;
ps->c1divstart = 1000;
ps->c1diva = 255;
ps->c1ctl = 0;
ps->c1t1 = 0;
ps->c1t2 = 0;
ps->c1t3 = 0;
ps->c1sw1 = 0;
ps->c1sw2 = 0;
ps->c1sw3 = 0;
ps->c1sw4 = 0;
ps->c1vo = 1;
#ifndef NONLINEAR_MIXING
ps->c1stop = 1;
#endif
ps->vol1 = 0;
ps->outvol_1 = 0;
/* Channel 2 state */
ps->readout_2 = readout2_normal;
ps->event_2 = event2_pure;
ps->c2divpos = 1000;
ps->c2divstart = 1000;
ps->c2divstart_p = 1000;
ps->c2diva = 255;
ps->c2ctl = 0;
ps->c2t1 = 0;
ps->c2t2 = 0;
ps->c2sw1 = 0;
ps->c2sw2 = 0;
ps->c2sw3 = 0;
ps->c2vo = 0;
#ifndef NONLINEAR_MIXING
ps->c2stop = 1;
#endif
ps->vol2 = 0;
ps->outvol_2 = 0;
/* Channel 3 state */
ps->readout_3 = readout3_normal;
ps->event_3 = event3_pure;
ps->c3divpos = 1000;
ps->c3divstart = 1000;
ps->c3diva = 255;
ps->c3ctl = 0;
ps->c3t1 = 0;
ps->c3t2 = 0;
ps->c3sw1 = 0;
ps->c3sw2 = 0;
ps->c3sw3 = 0;
ps->c3vo = 0;
#ifndef NONLINEAR_MIXING
ps->c3stop = 1;
#endif
ps->vol3 = 0;
ps->outvol_3 = 0;
}
static double read_resam_all(PokeyState* ps)
{
int i = ps->qebeg;
qev_t avol,bvol;
double sum;
if(ps->qebeg == ps->qeend)
{
return ps->ovola * filter_data[0]; /* if no events in the queue */
}
avol = ps->ovola;
sum = 0;
/* Separate two loop cases, for wrap-around and without */
if(ps->qeend < ps->qebeg) /* With wrap */
{
while(i<filter_size)
{
bvol = ps->qev[i];
sum += (avol-bvol)*filter_data[ps->qet[i]];
avol = bvol;
++i;
}
i=0;
}
/* without wrap */
while(i<ps->qeend)
{
bvol = ps->qev[i];
sum += (avol-bvol)*filter_data[ps->qet[i]];
avol = bvol;
++i;
}
sum += avol*filter_data[0];
return sum;
}
static void add_change(PokeyState* ps, qev_t a)
{
ps->qev[ps->qeend] = a;
ps->qet[ps->qeend] = 0;
++ps->qeend;
if(ps->qeend >= filter_size)
ps->qeend = 0;
}
static void bump_qe_subticks(PokeyState* ps, int subticks)
{
/* Remove too old events from the queue while bumping */
int i = ps->qebeg;
if(ps->qeend < ps->qebeg) /* Loop with wrap */
{
while(i<filter_size)
{
ps->qet[i] += subticks;
if(ps->qet[i] >= filter_size - 1)
{
ps->ovola = ps->qev[i];
++ps->qebeg;
if(ps->qebeg >= filter_size)
ps->qebeg = 0;
}
++i;
}
i=0;
}
/* loop without wrap */
while(i<ps->qeend)
{
ps->qet[i] += subticks;
if(ps->qet[i] >= filter_size - 1)
{
ps->ovola = ps->qev[i];
++ps->qebeg;
if(ps->qebeg >= filter_size)
ps->qebeg = 0;
}
++i;
}
}
static void build_poly4(void)
{
unsigned char c;
unsigned char i;
unsigned char poly4=1;
for(i=0; i<15; i++)
{
poly4tbl[i] = ~poly4;
c = ((poly4>>2)&1) ^ ((poly4>>3)&1);
poly4 = ((poly4<<1)&15) + c;
}
}
static void build_poly5(void)
{
unsigned char c;
unsigned char i;
unsigned char poly5 = 1;
for(i = 0; i < 31; i++) {
poly5tbl[i] = ~poly5; /* Inversion! Attention! */
c = ((poly5 >> 2) ^ (poly5 >> 4)) & 1;
poly5 = ((poly5 << 1) & 31) + c;
}
}
static void build_poly17(void)
{
unsigned int c;
unsigned int i;
unsigned int poly17 = 1;
for(i = 0; i < 131071; i++) {
poly17tbl[i] = (unsigned char) poly17;
c = ((poly17 >> 11) ^ (poly17 >> 16)) & 1;
poly17 = ((poly17 << 1) & 131071) + c;
}
}
static void build_poly9(void)
{
unsigned int c;
unsigned int i;
unsigned int poly9 = 1;
for(i = 0; i < 511; i++) {
poly9tbl[i] = (unsigned char) poly9;
c = ((poly9 >> 3) ^ (poly9 >> 8)) & 1;
poly9 = ((poly9 << 1) & 511) + c;
}
}
static void advance_polies(PokeyState* ps, unsigned long tacts)
{
ps->poly4pos = (tacts + ps->poly4pos) % 15;
ps->poly5pos = (tacts + ps->poly5pos) % 31;
ps->poly17pos = (tacts + ps->poly17pos) % 131071;
ps->poly9pos = (tacts + ps->poly9pos) % 511;
}
/***********************************
READ OUTPUT 0
************************************/
static int readout0_vo(PokeyState* ps)
{
return ps->vol0;
}
static int readout0_hipass(PokeyState* ps)
{
if(ps->c0t2 ^ ps->c0t3)
return ps->vol0;
else return 0;
}
static int readout0_normal(PokeyState* ps)
{
if(ps->c0t2)
return ps->vol0;
else return 0;
}
/***********************************
READ OUTPUT 1
************************************/
static int readout1_vo(PokeyState* ps)
{
return ps->vol1;
}
static int readout1_hipass(PokeyState* ps)
{
if(ps->c1t2 ^ ps->c1t3)
return ps->vol1;
else return 0;
}
static int readout1_normal(PokeyState* ps)
{
if(ps->c1t2)
return ps->vol1;
else return 0;
}
/***********************************
READ OUTPUT 2
************************************/
static int readout2_vo(PokeyState* ps)
{
return ps->vol2;
}
static int readout2_normal(PokeyState* ps)
{
if(ps->c2t2)
return ps->vol2;
else return 0;
}
/***********************************
READ OUTPUT 3
************************************/
static int readout3_vo(PokeyState* ps)
{
return ps->vol3;
}
static int readout3_normal(PokeyState* ps)
{
if(ps->c3t2)
return ps->vol3;
else return 0;
}
/***********************************
EVENT CHANNEL 0
************************************/
static void event0_pure(PokeyState* ps, char p5v, char p4v, char p917v)
{
ps->c0t2 = !ps->c0t2;
ps->c0t1 = p5v;
}
static void event0_p5(PokeyState* ps, char p5v, char p4v, char p917v)
{
if(ps->c0t1)
ps->c0t2 = !ps->c0t2;
ps->c0t1 = p5v;
}
static void event0_p4(PokeyState* ps, char p5v, char p4v, char p917v)
{
ps->c0t2 = p4v;
ps->c0t1 = p5v;
}
static void event0_p917(PokeyState* ps, char p5v, char p4v, char p917v)
{
ps->c0t2 = p917v;
ps->c0t1 = p5v;
}
static void event0_p4_p5(PokeyState* ps, char p5v, char p4v, char p917v)
{
if(ps->c0t1)
ps->c0t2 = p4v;
ps->c0t1 = p5v;
}
static void event0_p917_p5(PokeyState* ps, char p5v, char p4v, char p917v)
{
if(ps->c0t1)
ps->c0t2 = p917v;
ps->c0t1 = p5v;
}
/***********************************
EVENT CHANNEL 1
************************************/
static void event1_pure(PokeyState* ps, char p5v, char p4v, char p917v)
{
ps->c1t2 = !ps->c1t2;
ps->c1t1 = p5v;
}
static void event1_p5(PokeyState* ps, char p5v, char p4v, char p917v)
{
if(ps->c1t1)
ps->c1t2 = !ps->c1t2;
ps->c1t1 = p5v;
}
static void event1_p4(PokeyState* ps, char p5v, char p4v, char p917v)
{
ps->c1t2 = p4v;
ps->c1t1 = p5v;
}
static void event1_p917(PokeyState* ps, char p5v, char p4v, char p917v)
{
ps->c1t2 = p917v;
ps->c1t1 = p5v;
}
static void event1_p4_p5(PokeyState* ps, char p5v, char p4v, char p917v)
{
if(ps->c1t1)
ps->c1t2 = p4v;
ps->c1t1 = p5v;
}
static void event1_p917_p5(PokeyState* ps, char p5v, char p4v, char p917v)
{
if(ps->c1t1)
ps->c1t2 = p917v;
ps->c1t1 = p5v;
}
/***********************************
EVENT CHANNEL 2
************************************/
static void event2_pure(PokeyState* ps, char p5v, char p4v, char p917v)
{
ps->c2t2 = !ps->c2t2;
ps->c2t1 = p5v;
/* high-pass clock for channel 0 */
ps->c0t3 = ps->c0t2;
}
static void event2_p5(PokeyState* ps, char p5v, char p4v, char p917v)
{
if(ps->c2t1)
ps->c2t2 = !ps->c2t2;
ps->c2t1 = p5v;
/* high-pass clock for channel 0 */
ps->c0t3 = ps->c0t2;
}
static void event2_p4(PokeyState* ps, char p5v, char p4v, char p917v)
{
ps->c2t2 = p4v;
ps->c2t1 = p5v;
/* high-pass clock for channel 0 */
ps->c0t3 = ps->c0t2;
}
static void event2_p917(PokeyState* ps, char p5v, char p4v, char p917v)
{
ps->c2t2 = p917v;
ps->c2t1 = p5v;
/* high-pass clock for channel 0 */
ps->c0t3 = ps->c0t2;
}
static void event2_p4_p5(PokeyState* ps, char p5v, char p4v, char p917v)
{
if(ps->c2t1)
ps->c2t2 = p4v;
ps->c2t1 = p5v;
/* high-pass clock for channel 0 */
ps->c0t3 = ps->c0t2;
}
static void event2_p917_p5(PokeyState* ps, char p5v, char p4v, char p917v)
{
if(ps->c2t1)
ps->c2t2 = p917v;
ps->c2t1 = p5v;
/* high-pass clock for channel 0 */
ps->c0t3 = ps->c0t2;
}
/***********************************
EVENT CHANNEL 3
************************************/
static void event3_pure(PokeyState* ps, char p5v, char p4v, char p917v)
{
ps->c3t2 = !ps->c3t2;
ps->c3t1 = p5v;
/* high-pass clock for channel 1 */
ps->c1t3 = ps->c1t2;
}
static void event3_p5(PokeyState* ps, char p5v, char p4v, char p917v)
{
if(ps->c3t1)
ps->c3t2 = !ps->c3t2;
ps->c3t1 = p5v;
/* high-pass clock for channel 1 */
ps->c1t3 = ps->c1t2;
}
static void event3_p4(PokeyState* ps, char p5v, char p4v, char p917v)
{
ps->c3t2 = p4v;
ps->c3t1 = p5v;
/* high-pass clock for channel 1 */
ps->c1t3 = ps->c1t2;
}
static void event3_p917(PokeyState* ps, char p5v, char p4v, char p917v)
{
ps->c3t2 = p917v;
ps->c3t1 = p5v;
/* high-pass clock for channel 1 */
ps->c1t3 = ps->c1t2;
}
static void event3_p4_p5(PokeyState* ps, char p5v, char p4v, char p917v)
{
if(ps->c3t1)
ps->c3t2 = p4v;
ps->c3t1 = p5v;
/* high-pass clock for channel 1 */
ps->c1t3 = ps->c1t2;
}
static void event3_p917_p5(PokeyState* ps, char p5v, char p4v, char p917v)
{
if(ps->c3t1)
ps->c3t2 = p917v;
ps->c3t1 = p5v;
/* high-pass clock for channel 1 */
ps->c1t3 = ps->c1t2;
}
static void advance_ticks(PokeyState* ps, unsigned long ticks)
{
unsigned long ta,tbe, tbe0, tbe1, tbe2, tbe3;
char p5v,p4v,p917v;
qev_t outvol_new;
unsigned char need0=0;
unsigned char need1=0;
unsigned char need2=0;
unsigned char need3=0;
unsigned char need=0;
if(ps->forcero)
{
ps->forcero = 0;
#ifdef NONLINEAR_MIXING
outvol_new = pokeymix[ps->outvol_0 + ps->outvol_1 + ps->outvol_2 + ps->outvol_3];
#else
outvol_new = ps->outvol_0 + ps->outvol_1 + ps->outvol_2 + ps->outvol_3;
#endif
if(outvol_new != ps->outvol_all)
{
ps->outvol_all = outvol_new;
add_change(ps, outvol_new);
}
}
while(ticks>0)
{
tbe0 = ps->c0divpos;
tbe1 = ps->c1divpos;
tbe2 = ps->c2divpos;
tbe3 = ps->c3divpos;
tbe = ticks+1;
#ifdef NONLINEAR_MIXING
if(tbe0 < tbe)
tbe = tbe0;
if(tbe1 < tbe)
tbe = tbe1;
if(tbe2 < tbe)
tbe = tbe2;
if(tbe3 < tbe)
tbe = tbe3;
#else
if(!ps->c0stop && tbe0 < tbe)
tbe = tbe0;
if(!ps->c1stop && tbe1 < tbe)
tbe = tbe1;
if(!ps->c2stop && tbe2 < tbe)
tbe = tbe2;
if(!ps->c3stop && tbe3 < tbe)
tbe = tbe3;
#endif
if(tbe>ticks)
ta = ticks;
else
{
ta = tbe;
need = 1;
}
ticks -= ta;
#ifdef NONLINEAR_MIXING
ps->c0divpos -= ta;
ps->c1divpos -= ta;
ps->c2divpos -= ta;
ps->c3divpos -= ta;
#else
if(!ps->c0stop) ps->c0divpos -= ta;
if(!ps->c1stop) ps->c1divpos -= ta;
if(!ps->c2stop) ps->c2divpos -= ta;
if(!ps->c3stop) ps->c3divpos -= ta;
#endif
advance_polies(ps,ta);
bump_qe_subticks(ps,ta);
if(need)
{
p5v = poly5tbl[ps->poly5pos] & 1;
p4v = poly4tbl[ps->poly4pos] & 1;
if(ps->selpoly9)
p917v = poly9tbl[ps->poly9pos] & 1;
else
p917v = poly17tbl[ps->poly17pos] & 1;
#ifdef NONLINEAR_MIXING
if(ta == tbe0)
#else
if(!ps->c0stop && ta == tbe0)
#endif
{
ps->event_0(ps,p5v,p4v,p917v);
ps->c0divpos = ps->c0divstart;
need0 = 1;
}
#ifdef NONLINEAR_MIXING
if(ta == tbe1)
#else
if(!ps->c1stop && ta == tbe1)
#endif
{
ps->event_1(ps,p5v,p4v,p917v);
ps->c1divpos = ps->c1divstart;
if(ps->c1_f0)
ps->c0divpos = ps->c0divstart_p;
need1 = 1;
/*two-tone filter*/
/*use if send break is on and two-tone mode is on*/
/*reset channel 1 if channel 2 changed*/
if((ps->skctl & 0x88) == 0x88) {
ps->c0divpos = ps->c0divstart;
/* it doesn't change the output state */
/*need0 = 1;*/
}
}
#ifdef NONLINEAR_MIXING
if(ta == tbe2)
#else
if(!ps->c2stop && ta == tbe2)
#endif
{
ps->event_2(ps,p5v,p4v,p917v);
ps->c2divpos = ps->c2divstart;
need2 = 1;
if(ps->c0sw4)
need0 = 1;
}
#ifdef NONLINEAR_MIXING
if(ta == tbe3)
#else
if(!ps->c3stop && ta == tbe3)
#endif
{
ps->event_3(ps,p5v,p4v,p917v);
ps->c3divpos = ps->c3divstart;
if(ps->c3_f2)
ps->c2divpos = ps->c2divstart_p;
need3 = 1;