-
Notifications
You must be signed in to change notification settings - Fork 2
/
spcplayer.c
4735 lines (3822 loc) · 114 KB
/
spcplayer.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
/*
* spcplayer.c - An SPC file player
* Copyright (C) 2011 Benjamin Charron <[email protected]>
*
* This program 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.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* spcplayer.c - Benjamin Charron <[email protected]>
* Created : Sat Sep 3 14:53:26 2011
* Revision : $Id$
*/
/*
Assembler notes:
MOV X, A ; Register X = A
MOV Y, #$12 ; Register Y = 0x12 (#$xx == immediate)
MOV Y, $12 ; Register Y = ram[0x12] ($xx == memory offset)
MOV ($12)+Y, A ; Not sure! Maybe ram[ram[0x12] + Y] = A?
*/
#include <stdio.h>
#include <assert.h>
#include <sys/time.h>
#include <errno.h>
#include <arpa/inet.h>
#include <SDL.h>
#include <signal.h>
#include <unistd.h>
#include <limits.h>
#include "opcodes.h"
#include "dsp_registers.h"
#include "ctl_registers.h"
#include "buf.h"
#define CLAMP16(s) { if (s > 32767) s = 32767; else if (s < -32768) s = -32768; }
#define CLAMP15(s) { if (s > 16383) s = 16383; else if (s < -16384) s = -16384; }
// SPC Main clock speed, in Hz
#define MAIN_CLOCK 1024000
// How many samples are output per second. Don't try changing this ;)
#define SAMPLE_RATE 32000
// How many cycles between audio updates
#define AUDIO_SAMPLE_PERIOD (MAIN_CLOCK / SAMPLE_RATE)
// How many samples to fill in each pass. This buffer is the queue from which
// SDL_audio reads from.
#define AUDIO_BUFFER_SIZE 8000
// Don't redefine this, it's just to increase readability :)
#define SPC_NB_VOICES 8
#define SPC_HEADER_LEN 33
#define SPC_TAG_TYPE_OFFSET 0x23
#define SPC_VERSION_OFFSET 0x24
#define SPC_ID_TAG_OFFSET 0x2e
#define SPC_RAM_OFFSET 0x0100
#define SPC_DSP_REGISTERS 128
#define SPC_RAM_SIZE 65536
#define SPC_HEADER_MAGIC "SNES-SPC700 Sound File Data v0.30"
#define SPC_HAS_ID_TAG 26
#define SPC_TAG_SONG_TITLE_LEN 32
#define SPC_TAG_GAME_TITLE_LEN 32
#define SPC_TAG_DUMPER_NAME_LEN 32
#define SPC_TAG_COMMENTS_LEN 32
#define SPC_STACK_BASE 0x0100
#define NO_OPERAND 0
#define SPC_REG_CONTROL 0xF1
#define SPC_REG_TIMER0 0xFA
#define SPC_REG_TIMER1 0xFB
#define SPC_REG_TIMER2 0xFC
#define SPC_REG_COUNTER0 0xFD
#define SPC_REG_COUNTER1 0xFE
#define SPC_REG_COUNTER2 0xFF
// How many cycles before a timer's internal counter is incremented, based on
// 1.024 MHz clock. In other words: the period of the timer, in cpu cycles.
#define SPC_TIMER_CYCLES_8KHZ (MAIN_CLOCK / 8000)
#define SPC_TIMER_CYCLES_64KHZ (MAIN_CLOCK / 64000)
// ADSR Stuff
#define SPC_DSP_ENV_MAX (1 << 11) // Max value of the enveloppe
#define SPC_DSP_MVOLL 0x0C
#define SPC_DSP_MVOLR 0x1C
#define SPC_DSP_KON 0x4C
#define SPC_DSP_KOFF 0x5C
#define SPC_DSP_DIR 0x5D
#define SPC_DSP_FLG 0x6C
#define SPC_DSP_ENDX 0x7C
#define SPC_FLG_MUTE (1 << 6)
#define SPC_FLG_RESET (1 << 7)
// Per-voice registers
#define SPC_DSP_VxVOLL 0x00
#define SPC_DSP_VxVOLR 0x01
#define SPC_DSP_VxPITCHL 0x02
#define SPC_DSP_VxPITCHH 0x03
#define SPC_DSP_VxSCRN 0x04
#define SPC_DSP_VxADSR1 0x05
#define SPC_DSP_VxADSR2 0x06
#define SPC_DSP_VxGAIN 0x07
#define SPC_DSP_VxENVX 0x08
#define SPC_DSP_VxOUTX 0x09
enum error_values {
SUCCESS = 0,
FATAL_ERROR = 1
};
/* Passed to functions that may or not update flags */
#define DONT_ADJUST_FLAGS 0
#define ADJUST_FLAGS 1
enum trace_flags {
TRACE_CPU_JUMPS = 0x01,
TRACE_APU_VOICES = 0x02,
TRACE_REGISTER_WRITES = 0x04,
TRACE_REGISTER_READS = 0x08,
TRACE_CPU_INSTRUCTIONS = 0x10,
TRACE_COUNTERS = 0x20,
TRACE_DSP_OPS = 0x40,
TRACE_TIME_ELAPSED = 0x80,
TRACE_ADSR = 0x100
};
#define TRACE_ALL (TRACE_CPU_JUMPS | TRACE_APU_VOICES | TRACE_REGISTER_WRITES | TRACE_REGISTER_READS | TRACE_CPU_INSTRUCTIONS | TRACE_COUNTERS | TRACE_DSP_OPS | TRACE_TIME_ELAPSED | TRACE_ADSR)
// Bit order: 7 6 5 4 3 2 1 0
// N V P - H - Z C
typedef union spc_flags_u {
struct {
unsigned int c : 1; // Carry
unsigned int z : 1; // Zero
unsigned int i : 1; // Interrupt Enable
unsigned int h : 1; // Half-Carry
unsigned int b : 1; // Break
unsigned int p : 1; // Direct Page
unsigned int v : 1; // Overflow
unsigned int n : 1; // Negative
} f;
Uint8 val;
} spc_flags_t;
typedef struct brr_block_s {
Sint16 samples[32];
int filter;
int loop_flag;
int last_chunk;
int loop_code; // Addressing last_chunk + loop_flag as one 2-bit value.
} brr_block_t;
typedef struct spc_registers_s {
Uint16 pc;
Uint8 a;
Uint8 x;
Uint8 y;
spc_flags_t psw;
Uint8 sp;
Uint8 reserved[2];
} spc_registers_t;
typedef struct spc_timers_s {
unsigned long next_timer[3]; // Next cycle number for this timer to increase
Uint8 timer[3]; // Increments by one every time next_timer == cycle. This is the lower 8-bit counter.
Uint8 counter[3]; // Increments by one every time timer[x] == divisor[x]. This is the upper 4-bit counter.
Uint8 divisor[3]; // How many times timer[x] must increment before we increment counter
} spc_timers_t;
typedef struct id_tag_s {
char song_title[32 + 1];
char game_title[32 + 1];
char dumper[16 + 1];
char comments[32 + 1];
time_t date_dumped;
} id_tag_t;
typedef struct spc_file_s {
char header[SPC_HEADER_LEN + 1];
Uint8 junk[2];
Uint8 tag_type;
Uint8 version_minor;
spc_registers_t registers;
Uint8 ram[SPC_RAM_SIZE];
Uint8 dsp_registers[128];
Uint8 unused[64];
Uint8 extra_ram[64];
id_tag_t id_tag;
} spc_file_t;
enum adsr_phases {
SPC_VOICE_ATTACK,
SPC_VOICE_DECAY,
SPC_VOICE_SUSTAIN,
SPC_VOICE_RELEASE
};
enum output_format {
FMT_NONE,
FMT_RAW,
FMT_WAV
};
typedef struct spc_adsr_s {
unsigned int ar; // attack rate
unsigned int dr; // decay rate
unsigned int sr; // sustain rate
unsigned int sl; // sustain level
unsigned int rr; // release rate
unsigned int use_adsr; // 1 = use ADSR, 0 = Use VxGAIN
int env; // Current volume for this enveloppe
int step; // How much to increment/decrement the enveloppe every 'rate' tick.
enum adsr_phases cur_phase; // Current ADSR phase (A/D/S/R)
unsigned int next_counter; // Next time to modify the enveloppe based on the global samples counter
int gain; // Value of VxGAIN
int gain_mode; // 0:Decrease linear, 1:Decrease Exp, 2:Increase linear, 3:Increase bent
} spc_adsr_t;
/* Represents a voice */
typedef struct spc_voice_s {
int enabled; // 1 if enabled (KON), 0 otherwise
Uint16 cur_addr; // Address of current sample block
int looping; // Whether it's in looping mode
brr_block_t block; // Current block
unsigned int counter; // Current counter, based on number of steps done for this block of 4 BRR samples so far
int prev_brr[2]; // Previous BRR samples, for voice filter
spc_adsr_t adsr;
} spc_voice_t;
typedef struct spc_state_s {
spc_registers_t *regs; // XXX: Why the hell would I make regs a pointer.
spc_timers_t timers;
Uint8 *ram;
Uint8 *dsp_registers;
Uint8 current_dsp_register;
unsigned int sample_counter; // Number of samples played so far
unsigned long cycle;
spc_voice_t voices[8];
int trace;
int profiling;
int *profile_info;
buf_t *audio_buf;
FILE *out_file;
enum output_format output_format;
int audio_dev;
int wav_samples_remaining;
} spc_state_t;
/* Gaussian Interpolation table - straight from no$sns specs */
int INTERP_TABLE[] = {
0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000,
0x001, 0x001, 0x001, 0x001, 0x001, 0x001, 0x001, 0x001, 0x001, 0x001, 0x001, 0x002, 0x002, 0x002, 0x002, 0x002,
0x002, 0x002, 0x003, 0x003, 0x003, 0x003, 0x003, 0x004, 0x004, 0x004, 0x004, 0x004, 0x005, 0x005, 0x005, 0x005,
0x006, 0x006, 0x006, 0x006, 0x007, 0x007, 0x007, 0x008, 0x008, 0x008, 0x009, 0x009, 0x009, 0x00A, 0x00A, 0x00A,
0x00B, 0x00B, 0x00B, 0x00C, 0x00C, 0x00D, 0x00D, 0x00E, 0x00E, 0x00F, 0x00F, 0x00F, 0x010, 0x010, 0x011, 0x011,
0x012, 0x013, 0x013, 0x014, 0x014, 0x015, 0x015, 0x016, 0x017, 0x017, 0x018, 0x018, 0x019, 0x01A, 0x01B, 0x01B,
0x01C, 0x01D, 0x01D, 0x01E, 0x01F, 0x020, 0x020, 0x021, 0x022, 0x023, 0x024, 0x024, 0x025, 0x026, 0x027, 0x028,
0x029, 0x02A, 0x02B, 0x02C, 0x02D, 0x02E, 0x02F, 0x030, 0x031, 0x032, 0x033, 0x034, 0x035, 0x036, 0x037, 0x038,
0x03A, 0x03B, 0x03C, 0x03D, 0x03E, 0x040, 0x041, 0x042, 0x043, 0x045, 0x046, 0x047, 0x049, 0x04A, 0x04C, 0x04D,
0x04E, 0x050, 0x051, 0x053, 0x054, 0x056, 0x057, 0x059, 0x05A, 0x05C, 0x05E, 0x05F, 0x061, 0x063, 0x064, 0x066,
0x068, 0x06A, 0x06B, 0x06D, 0x06F, 0x071, 0x073, 0x075, 0x076, 0x078, 0x07A, 0x07C, 0x07E, 0x080, 0x082, 0x084,
0x086, 0x089, 0x08B, 0x08D, 0x08F, 0x091, 0x093, 0x096, 0x098, 0x09A, 0x09C, 0x09F, 0x0A1, 0x0A3, 0x0A6, 0x0A8,
0x0AB, 0x0AD, 0x0AF, 0x0B2, 0x0B4, 0x0B7, 0x0BA, 0x0BC, 0x0BF, 0x0C1, 0x0C4, 0x0C7, 0x0C9, 0x0CC, 0x0CF, 0x0D2,
0x0D4, 0x0D7, 0x0DA, 0x0DD, 0x0E0, 0x0E3, 0x0E6, 0x0E9, 0x0EC, 0x0EF, 0x0F2, 0x0F5, 0x0F8, 0x0FB, 0x0FE, 0x101,
0x104, 0x107, 0x10B, 0x10E, 0x111, 0x114, 0x118, 0x11B, 0x11E, 0x122, 0x125, 0x129, 0x12C, 0x130, 0x133, 0x137,
0x13A, 0x13E, 0x141, 0x145, 0x148, 0x14C, 0x150, 0x153, 0x157, 0x15B, 0x15F, 0x162, 0x166, 0x16A, 0x16E, 0x172,
0x176, 0x17A, 0x17D, 0x181, 0x185, 0x189, 0x18D, 0x191, 0x195, 0x19A, 0x19E, 0x1A2, 0x1A6, 0x1AA, 0x1AE, 0x1B2,
0x1B7, 0x1BB, 0x1BF, 0x1C3, 0x1C8, 0x1CC, 0x1D0, 0x1D5, 0x1D9, 0x1DD, 0x1E2, 0x1E6, 0x1EB, 0x1EF, 0x1F3, 0x1F8,
0x1FC, 0x201, 0x205, 0x20A, 0x20F, 0x213, 0x218, 0x21C, 0x221, 0x226, 0x22A, 0x22F, 0x233, 0x238, 0x23D, 0x241,
0x246, 0x24B, 0x250, 0x254, 0x259, 0x25E, 0x263, 0x267, 0x26C, 0x271, 0x276, 0x27B, 0x280, 0x284, 0x289, 0x28E,
0x293, 0x298, 0x29D, 0x2A2, 0x2A6, 0x2AB, 0x2B0, 0x2B5, 0x2BA, 0x2BF, 0x2C4, 0x2C9, 0x2CE, 0x2D3, 0x2D8, 0x2DC,
0x2E1, 0x2E6, 0x2EB, 0x2F0, 0x2F5, 0x2FA, 0x2FF, 0x304, 0x309, 0x30E, 0x313, 0x318, 0x31D, 0x322, 0x326, 0x32B,
0x330, 0x335, 0x33A, 0x33F, 0x344, 0x349, 0x34E, 0x353, 0x357, 0x35C, 0x361, 0x366, 0x36B, 0x370, 0x374, 0x379,
0x37E, 0x383, 0x388, 0x38C, 0x391, 0x396, 0x39B, 0x39F, 0x3A4, 0x3A9, 0x3AD, 0x3B2, 0x3B7, 0x3BB, 0x3C0, 0x3C5,
0x3C9, 0x3CE, 0x3D2, 0x3D7, 0x3DC, 0x3E0, 0x3E5, 0x3E9, 0x3ED, 0x3F2, 0x3F6, 0x3FB, 0x3FF, 0x403, 0x408, 0x40C,
0x410, 0x415, 0x419, 0x41D, 0x421, 0x425, 0x42A, 0x42E, 0x432, 0x436, 0x43A, 0x43E, 0x442, 0x446, 0x44A, 0x44E,
0x452, 0x455, 0x459, 0x45D, 0x461, 0x465, 0x468, 0x46C, 0x470, 0x473, 0x477, 0x47A, 0x47E, 0x481, 0x485, 0x488,
0x48C, 0x48F, 0x492, 0x496, 0x499, 0x49C, 0x49F, 0x4A2, 0x4A6, 0x4A9, 0x4AC, 0x4AF, 0x4B2, 0x4B5, 0x4B7, 0x4BA,
0x4BD, 0x4C0, 0x4C3, 0x4C5, 0x4C8, 0x4CB, 0x4CD, 0x4D0, 0x4D2, 0x4D5, 0x4D7, 0x4D9, 0x4DC, 0x4DE, 0x4E0, 0x4E3,
0x4E5, 0x4E7, 0x4E9, 0x4EB, 0x4ED, 0x4EF, 0x4F1, 0x4F3, 0x4F5, 0x4F6, 0x4F8, 0x4FA, 0x4FB, 0x4FD, 0x4FF, 0x500,
0x502, 0x503, 0x504, 0x506, 0x507, 0x508, 0x50A, 0x50B, 0x50C, 0x50D, 0x50E, 0x50F, 0x510, 0x511, 0x511, 0x512,
0x513, 0x514, 0x514, 0x515, 0x516, 0x516, 0x517, 0x517, 0x517, 0x518, 0x518, 0x518, 0x518, 0x518, 0x519, 0x519
};
/* Global variables */
volatile int g_do_break = 1;
int g_break_read_addr = -1;
int g_break_write_addr = -1;
int g_break_exec_addr = -1;
opcode_t *g_opcode_table = NULL;
int dump_instruction(Uint16 pc, Uint8 *ram);
void dump_registers(spc_registers_t *registers);
int execute_next(spc_state_t *state);
spc_file_t *read_spc_file(char *filename);
Uint16 get_direct_page_addr(spc_state_t *state, Uint16 addr);
Uint8 get_direct_page_byte(spc_state_t *state, Uint16 addr);
void adjust_flags(spc_state_t *state, Uint16 val);
Uint8 read_byte(spc_state_t *state, Uint16 addr);
Uint16 read_word(spc_state_t *state, Uint16 addr);
void write_byte(spc_state_t *state, Uint16 addr, Uint8 val);
void write_word(spc_state_t *state, Uint16 addr, Uint16 val);
void enable_timer(spc_state_t *state, int timer);
void clear_timer(spc_state_t *state, int timer);
Uint16 get_sample_addr(spc_state_t *state, int voice_nr, int loop);
brr_block_t *decode_brr_block(spc_voice_t *v, Uint8 *ptr);
void kon_voice(spc_state_t *state, int voice_nr);
void koff_voice(spc_state_t *state, int voice_nr);
void init_voice(spc_state_t *state, int voice_nr);
int get_voice_pitch(spc_state_t *state, int voice_nr);
Sint16 get_next_sample(spc_state_t *state, int voice_nr);
char *flags_str(spc_flags_t flags)
{
static char buf[10];
char *ptr = buf;
*ptr++ = '[';
*ptr++ = flags.f.n ? 'n' : ' ';
*ptr++ = flags.f.v ? 'v' : ' ';
*ptr++ = flags.f.p ? 'p' : ' ';
*ptr++ = flags.f.b ? 'b' : ' ';
*ptr++ = flags.f.h ? 'h' : ' ';
*ptr++ = flags.f.i ? 'i' : ' ';
*ptr++ = flags.f.z ? 'z' : ' ';
*ptr++ = flags.f.c ? 'c' : ' ';
*ptr++ = ']';
*ptr++ = '\0';
return(buf);
}
/* Convert from 16-bit little-endian */
uint16_t le16toh(uint16_t i) {
return(SDL_SwapLE16(i));
}
/* Make a 16-bit value out of two 8-bit ones */
uint16_t make16(uint8_t high, uint8_t low) {
uint16_t offset = ((uint16_t) high << 8) | low;
return(offset);
}
/* Get low byte of a 16-bit word */
uint8_t get_low(uint16_t word)
{
uint8_t low = (word & 0x00FF);
return(low);
}
/* Get high byte of a 16-bit word */
uint8_t get_high(uint16_t word)
{
uint8_t high = (word & 0xFF00) >> 8;
return(high);
}
opcode_t *convert_opcode_table(void) {
opcode_t *table;
table = malloc(sizeof(opcode_t) * 256);
if (NULL == table) {
perror("convert_opcode_table() -> malloc()");
exit(1);
}
memset(table, 0, sizeof(opcode_t) * 256);
for (int x = 0; x < OPCODE_TABLE_LEN; x++) {
int op = OPCODE_TABLE[x].opcode;
table[op].opcode = OPCODE_TABLE[x].opcode;
table[op].name = OPCODE_TABLE[x].name;
table[op].len = OPCODE_TABLE[x].len;
assert(table[op].len > 0);
}
/*
for (int x = 0; x < OPCODE_TABLE_LEN; x++) {
printf("[%02X] %s (%d)\n", x, table[x].name, table[x].len);
}
*/
return(table);
}
opcode_t *get_opcode_by_value(Uint8 opcode) {
return(&g_opcode_table[opcode]);
}
/*
Dump a voice to file. If loop is defined, loops for 32k samples (~1 second of audio at 32kHz)
** This function is all sorts of wrong. Do not use. **
*/
void dump_voice(spc_state_t *state, int voice_nr, char *path) {
FILE *f;
int dealloc = 0;
int done = 0;
int written_samples = 0;
brr_block_t *block;
if (NULL == path) {
path = malloc(1024);
sprintf(path, "sample_%02d", voice_nr);
dealloc = 1;
}
printf("Writing to %s\n", path);
f = fopen(path, "w+");
int addr = get_sample_addr(state, voice_nr, 0);
do {
block = decode_brr_block(&state->voices[voice_nr], &state->ram[addr]);
addr += 9;
for (int brr_nr = 0; brr_nr < 16; brr_nr++) {
Sint16 sample = block->samples[brr_nr];
fprintf(f, "%hd\n", sample);
written_samples++;
printf("sample: %d brr_nr: %d\n", sample, brr_nr);
}
if (block->last_chunk) {
if (block->loop_flag) {
printf("Looping.\n");
addr = get_sample_addr(state, voice_nr, 1);
} else {
done = 1;
}
}
} while(! done && written_samples < 32000 && addr < 65536);
fclose(f);
if (dealloc)
free(path);
}
/* Read the value of a counter. Doing so resets the counter. */
Uint8 read_counter(spc_state_t *state, Uint16 addr) {
Uint8 val;
int counter_nr;
assert(addr >= SPC_REG_COUNTER0 && addr <= SPC_REG_COUNTER2);
counter_nr = addr - SPC_REG_COUNTER0;
val = state->timers.counter[counter_nr];
state->timers.counter[counter_nr] = 0;
return(val);
}
/* Called when a register is being written to */
void dsp_register_write(spc_state_t *state, Uint8 reg, Uint8 val) {
// 128-255 is a mirror I think, but I want to catch ROMs doing this, if any.
assert(reg <= 127);
if (state->trace & (TRACE_REGISTER_WRITES|TRACE_DSP_OPS))
printf("%0.1f $%04X [DSP] Writing %02X into register %02X (%s)\n", (float) state->cycle / (2048 * 1000), state->regs->pc, val, reg, DSP_NAMES[reg % 127]);
state->dsp_registers[reg] = val;
switch(reg) {
case SPC_DSP_KON:
{
for (int x = 0; x < 8; x++) {
Uint8 bit = 1 << x;
if ((val & bit) > 0) {
if (state->trace & TRACE_APU_VOICES)
printf("Enabling voice %d\n", x);
kon_voice(state, x);
}
}
}
break;
case SPC_DSP_KOFF:
{
for (int x = 0; x < 8; x++) {
Uint8 bit = 1 << x;
if ((val & bit) > 0) {
if (state->trace & TRACE_APU_VOICES)
printf("Disabling voice %d\n", x);
koff_voice(state, x);
}
}
}
break;
case SPC_DSP_FLG:
{
if (val & SPC_FLG_RESET) {
if (state->trace & TRACE_APU_VOICES)
printf("Disabling all voices\n");
for (int x = 0; x < 8; x++) {
koff_voice(state, x);
}
}
}
break;
/* Writing to ENDx resets its value */
case SPC_DSP_ENDX:
state->dsp_registers[SPC_DSP_ENDX] = 0;
break;
default:
break;
}
}
/* Handles a byte being written to $00F0-$00FF (registers) */
void register_write(spc_state_t *state, Uint16 addr, Uint8 val) {
assert(addr >= 0xF0 && addr <= 0xFF);
if (state->trace & TRACE_REGISTER_WRITES)
printf("Register write $%04X [%s]\n", addr, CTL_REGISTER_NAMES[addr - 0xF0]);
switch(addr) {
case 0xF0: // Test?
state->ram[addr] = val;
break;
case 0xF1: // Control, AKA SPCCON1, AKA CONTROL
state->ram[addr] = val;
// Start or stop a timer
for (int timer = 0; timer < 3; timer++) {
int bit = 0x01 << timer;
// XXX: Handle the case where timer == 0x00, which is in fact 256.
if (val & bit)
enable_timer(state, timer);
else
clear_timer(state, timer);
}
// XXX: Handles bits 4-5 (PORT0-3)
// XXX: Bit 7 appears to be related to the IPL ROM being ROM or RAM.
break;
case 0xF2: // Register address port, AKA SPCDRGA, AKA DSPADDR
state->current_dsp_register = val;
if (val > 127) {
fprintf(stderr, "Trying to access DSP register %d, but maximum is 127.\n", val);
state->current_dsp_register = val % 127;
}
state->ram[addr] = val;
break;
case 0xF3: // Register data port, AKA SPCDDAT, AKA DSPDATA
dsp_register_write(state, state->current_dsp_register, val);
state->ram[addr] = val;
break;
case 0xF4: // I/O Ports, AKA CPUIO0
case 0xF5: // CPUIO1
case 0xF6: // CPUIO2
case 0xF7: // CPUIO3
// state->ram[addr] = val;
// Ignore writes to CPUIO; they break srb-10 and 11
break;
case 0xF8: // Unknown, AKA AUXIO4
case 0xF9: // AUXIO5
state->ram[addr] = val;
break;
case 0xFA: // Timers, AKA SPCTMLT, AKA T1DIV
case 0xFB: // T1DIV
case 0xFC: // T2DIV
{
int timer = addr - 0xFA;
if (state->trace & TRACE_COUNTERS)
printf("Timer %d new divisor: %d\n", timer, val);
// XXX: It's not clear whether or not the divisor can
// change while a timer is enabled. Docs seem to say
// timer must be stopped before this value can be changed.
// state->timers.divisor[timer] = val;
state->ram[addr] = val;
}
break;
case 0xFD: // Counters, AKA SPCTMCT, AKA TxOUT
case 0xFE: // T1OUT
case 0xFF: // T2OUT
// I don't think these counters can be written to..
// state->ram[addr] = val;
fprintf(stderr, "Illegal write to %02X\n", addr);
break;
default:
fprintf(stderr, "register_write(%04X): HOW THE FUCK DID THIS HAPPEN??\n", addr);
exit(1);
break;
}
}
Uint8 register_read(spc_state_t *state, Uint16 addr) {
Uint8 val;
assert(addr >= 0xF0 && addr <= 0xFF);
if (state->trace & TRACE_REGISTER_READS)
if (addr != 0xFD && addr != 0xF7)
printf("$%04X: Register read $%04X [%s]\n", state->regs->pc, addr, CTL_REGISTER_NAMES[addr - 0xF0]);
switch(addr) {
case 0xF0: // Test?
val = state->ram[addr];
break;
case 0xF1: // Control
val = state->ram[addr];
break;
case 0xF2: // Register stuff: Add
val = state->ram[addr];
break;
case 0xF3: // Register stuff: Data
// val = state->ram[addr];
val = state->dsp_registers[state->current_dsp_register];
break;
case 0xF4: // I/O Ports
case 0xF5:
case 0xF6:
case 0xF7:
val = state->ram[addr];
break;
case 0xF8: // Unknown
case 0xF9:
val = state->ram[addr];
break;
case 0xFA: // Timers, AKA SPCTMLT, AKA T1DIV
case 0xFB: // T1DIV
case 0xFC: // T2DIV
val = state->ram[addr];
break;
case 0xFD: // Counters, AKA SPCTMCT, AKA TxOUT
case 0xFE: // T1OUT
case 0xFF: // T2OUT
val = read_counter(state, addr);
break;
default:
val = state->ram[addr];
break;
}
return(val);
}
/* Write a byte to memory / registers */
void write_byte(spc_state_t *state, Uint16 addr, Uint8 val) {
if (addr == g_break_write_addr) {
printf("$%04X is writing to %04X\n", state->regs->pc, addr);
g_do_break = 1;
}
// Handle registers 0xF0-0xFF
if ((addr & 0xFFF0) == 0x00F0) {
// XXX: Handle register here.
register_write(state, addr, val);
} else {
state->ram[addr] = val;
}
}
void write_word(spc_state_t *state, Uint16 addr, Uint16 val) {
// XXX: Pretty sure this is little-endian
Uint8 l = get_low(val);
Uint8 h = get_high(val);
write_byte(state, addr, l);
write_byte(state, addr + 1, h);
}
/* Read a byte from memory / registers / whatever */
Uint8 read_byte(spc_state_t *state, Uint16 addr) {
Uint8 val;
if (addr == g_break_read_addr) {
printf("$%04X is reading from %04X\n", state->regs->pc, addr);
g_do_break = 1;
}
// Handle registers 0xF0-0xFF
if ((addr & 0xFFF0) == 0x00F0) {
val = register_read(state, addr);
} else
val = state->ram[addr];
return(val);
}
/* Read a word (16-bit) from memory / registers / whatever */
Uint16 read_word(spc_state_t *state, Uint16 addr) {
Uint16 ret;
Uint8 l;
Uint8 h;
l = read_byte(state, addr);
h = read_byte(state, addr + 1);
ret = make16(h, l);
return(ret);
}
/* Get the contents of DSP register X. Does not involve read_byte(). */
Uint8 get_dsp(spc_state_t *state, Uint8 reg) {
Uint8 b;
b = state->dsp_registers[reg];
return(b);
}
/* Get the register 'reg' of DSP Voice 'voice_nr' */
Uint8 get_dsp_voice(spc_state_t *state, int voice_nr, Uint8 reg) {
Uint8 addr;
Uint8 result;
addr = voice_nr * 0x10 + reg;
result = get_dsp(state, addr);
return(result);
}
// Write 'val' to voice 'voice_nr's register 'reg'
void set_dsp_voice(spc_state_t *state, int voice_nr, Uint8 reg, Uint8 val) {
Uint8 addr;
addr = voice_nr * 0x10 + reg;
state->dsp_registers[addr] = val;
}
/* Perform the branch if flag 'flag' is set */
// XXX: Cycles appear to be wrong for flag-only checks like BMI/BPL/etc. Should be 2/4, not 4/6.
int branch_if_flag(spc_state_t *state, int flag, Uint8 operand1) {
int cycles;
if (flag) {
state->regs->pc += (Sint8) operand1 + 2;
if (state->trace & TRACE_CPU_JUMPS)
printf("Jumping to 0x%04X\n", state->regs->pc);
cycles = 6;
} else {
state->regs->pc += 2;
cycles = 4;
}
return(cycles);
}
int branch_if_flag_clear(spc_state_t *state, int flag, Uint8 operand1) {
int ret;
ret = branch_if_flag(state, ! flag, operand1);
return(ret);
}
int branch_if_flag_set(spc_state_t *state, int flag, Uint8 operand1) {
int ret;
ret = branch_if_flag(state, flag, operand1);
return(ret);
}
/*
int do_bcc(spc_state_t *state, Uint8 operand1) {
int cycles = 2;
if (! state->regs->psw.f.c) {
state->regs->pc += (Sint8) operand1 + 2;
printf("Jumping to 0x%04X\n", state->regs->pc);
} else {
state->regs->pc += 2;
cycles = 4;
}
return(cycles);
}
*/
/*
int do_beq(spc_state_t *state, Uint8 operand1)
{
int cycles = 2;
if (state->regs->psw.f.z) {
state->regs->pc += (Sint8) operand1 + 2;
printf("Jumping to 0x%04X\n", state->regs->pc);
} else {
state->regs->pc += 2;
cycles = 4;
}
return(cycles);
}
*/
/* Jump if bit 'bit' of the addr is clear */
int do_bbc(spc_state_t *state, int bit, Uint16 src_addr, Uint8 rel) {
int cycles;
Uint8 val;
Uint8 test;
test = 1 << bit;
val = read_byte(state, src_addr);
// printf("DO_BBC(%d): Jump if %02X (%04X) & %02X == 0\n", bit, val, src_addr, test);
if (val & test) {
cycles = 5;
state->regs->pc += 3;
} else {
state->regs->pc += (Sint8) rel + 3;
if (state->trace & TRACE_CPU_JUMPS)
printf("Jumping to 0x%04X\n", state->regs->pc);
cycles = 7;
}
return(cycles);
}
/* Jump if bit 'bit' of the addr is set */
int do_bbs(spc_state_t *state, int bit, Uint16 src_addr, Uint8 rel) {
int cycles;
Uint8 test;
Uint8 val;
test = 1 << bit;
state->regs->pc += 3;
cycles = 5;
val = read_byte(state, src_addr);
if (val & test) {
state->regs->pc += (Sint8) rel;
if (state->trace & TRACE_CPU_JUMPS)
printf("Jumping to 0x%04X\n", state->regs->pc);
cycles += 2;
}
return(cycles);
}
void instr_or(spc_state_t *state, Uint8 *operand1, Uint8 operand2)
{
printf("OR %02X, %02X\n", *operand1, operand2);
*operand1 = *operand1 | operand2;
state->regs->psw.f.n = (*operand1 & 0x80) > 0;
state->regs->psw.f.z = (*operand1 == 0);
}
Uint8 do_rol(spc_state_t *state, Uint8 val) {
Uint8 new_carry = (val & 0x80) > 0;
assert(new_carry == 0x00 || new_carry == 0x01);
val <<= 1;
val |= state->regs->psw.f.c;
state->regs->psw.f.c = new_carry;
adjust_flags(state, val);
return(val);
}
Uint8 do_pop(spc_state_t *state) {
Uint8 ret;
Uint16 stack_addr;
state->regs->sp++;
stack_addr = SPC_STACK_BASE + state->regs->sp;
ret = state->ram[stack_addr];
return(ret);
}
void do_push(spc_state_t *state, Uint8 val) {
Uint16 stack_addr;
stack_addr = SPC_STACK_BASE + state->regs->sp;
state->ram[stack_addr] = val;
state->regs->sp--;
}
void do_ret(spc_state_t *state) {
Uint16 ret_addr;
Uint8 h, l;
l = do_pop(state);
h = do_pop(state);
ret_addr = make16(h, l);
// printf("Popped address %04X\n", ret_addr);
state->regs->pc = ret_addr;
if (state->trace & TRACE_CPU_JUMPS)
printf("Returning to $%04X\n", state->regs->pc);
}
void do_call(spc_state_t *state, Uint8 operand1, Uint8 operand2) {
Uint16 ret_addr;
Uint16 dest_addr;
ret_addr = state->regs->pc + 3;
if (state->trace & TRACE_CPU_JUMPS)
printf("Pushing return address $%04X on the stack\n", ret_addr);
do_push(state, get_high(ret_addr));
do_push(state, get_low(ret_addr));
dest_addr = make16(operand2, operand1);
state->regs->pc = dest_addr;
if (state->trace & TRACE_CPU_JUMPS)
printf("Jumping to $%04X\n", state->regs->pc);
}
/* Update the flags based on (operand1 - operand2) */
void do_cmp(spc_state_t *state, Uint8 operand1, Uint8 operand2) {
Uint16 result;
result = operand1 - operand2;
// For some reason, Carry is set "when there has been no borrow"..
state->regs->psw.f.c = (operand1 >= operand2);
adjust_flags(state, result & 0xFF);
}
Uint8 do_adc(spc_state_t *state, Uint8 dst, Uint8 operand) {
Uint16 result;
Sint16 sResult;
Uint8 ret;
sResult = (Sint8) dst + (Sint8) operand + state->regs->psw.f.c;
result = dst + operand + state->regs->psw.f.c;
ret = (result & 0x00FF);