annotate vgmsplit.c @ 848:7068a9db6dd0

Wrote a buggy tool for splitting VGM files by channel
author Michael Pavone <pavone@retrodev.com>
date Sun, 01 Nov 2015 12:55:08 -0800
parents
children 215b5dabbc20
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
848
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1 /*
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
2 Copyright 2015 Michael Pavone
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
3 This file is part of BlastEm.
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
4 BlastEm is free software distributed under the terms of the GNU General Public License version 3 or greater. See COPYING for full license text.
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
5 */
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
6 #include "ym2612.h"
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
7 #include "vgm.h"
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
8 #include <stdint.h>
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
9 #include <stdio.h>
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
10 #include <stdlib.h>
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
11 #include <string.h>
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
12
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
13 #define OUT_CHANNELS 10
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
14 #define DAC_CHANNEL 5
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
15 #define PSG_BASE 6
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
16 #define SAMPLE_THRESHOLD 100
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
17
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
18 int main(int argc, char ** argv)
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
19 {
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
20 data_block *blocks = NULL;
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
21 data_block *seek_block = NULL;
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
22 uint32_t seek_offset;
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
23 uint32_t block_offset;
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
24
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
25
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
26 FILE * f = fopen(argv[1], "rb");
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
27 vgm_header header;
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
28 size_t bytes = fread(&header, 1, sizeof(header), f);
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
29 if (bytes != sizeof(header)) {
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
30 fputs("Error reading file\n", stderr);
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
31 exit(1);
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
32 }
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
33 if (header.version < 0x150 || !header.data_offset) {
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
34 header.data_offset = 0xC;
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
35 }
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
36 fseek(f, header.data_offset + 0x34, SEEK_SET);
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
37 uint32_t data_size = header.eof_offset + 4 - (header.data_offset + 0x34);
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
38 uint8_t * data = malloc(data_size);
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
39 data_size = fread(data, 1, data_size, f);
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
40 fclose(f);
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
41 uint8_t *buffers[OUT_CHANNELS];
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
42 uint8_t *out_pos[OUT_CHANNELS];
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
43 uint8_t has_real_data[OUT_CHANNELS];
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
44
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
45 buffers[0] = malloc(data_size * OUT_CHANNELS);
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
46 out_pos[0] = buffers[0];
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
47 has_real_data[0] = 0;
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
48 for (int i = 1; i < OUT_CHANNELS; i++)
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
49 {
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
50 buffers[i] = buffers[i-1] + data_size;
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
51 out_pos[i] = buffers[i];
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
52 has_real_data[i] = 0;
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
53 }
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
54
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
55 uint8_t * end = data + data_size;
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
56 uint8_t * cur = data;
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
57 uint32_t current_cycle = 0;
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
58 uint8_t psg_latch = 0;
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
59 uint8_t param,reg;
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
60 uint8_t channel;
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
61 uint32_t sample_count = 0;
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
62 uint8_t last_cmd;
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
63 while (cur < end) {
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
64 uint8_t cmd = *(cur++);
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
65 switch(cmd)
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
66 {
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
67 case CMD_PSG_STEREO:
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
68 //ignore for now
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
69 cur++;
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
70 break;
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
71 case CMD_PSG:
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
72 param = *(cur++);
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
73 if (param & 0x80) {
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
74 psg_latch = param;
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
75 channel = param >> 5 & 3;
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
76 } else {
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
77 channel = psg_latch >> 5 & 3;
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
78 }
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
79 *(out_pos[PSG_BASE+channel]++) = cmd;
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
80 *(out_pos[PSG_BASE+channel]++) = param;
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
81 has_real_data[PSG_BASE+channel] = 1;
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
82 break;
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
83 case CMD_YM2612_0:
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
84 reg = *(cur++);
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
85 param = *(cur++);
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
86 if (reg < REG_KEY_ONOFF) {
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
87 for (int i = 0; i < 6; i++)
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
88 {
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
89 *(out_pos[i]++) = cmd;
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
90 *(out_pos[i]++) = reg;
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
91 *(out_pos[i]++) = param;
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
92 }
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
93 break;
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
94 } else if(reg == REG_DAC || reg == REG_DAC_ENABLE) {
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
95 if (reg == REG_DAC) {
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
96 sample_count++;
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
97 }
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
98 channel = DAC_CHANNEL;
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
99 } else if(reg == REG_KEY_ONOFF) {
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
100 channel = param & 7;
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
101 if (channel > 2) {
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
102 channel--;
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
103 }
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
104 if (param & 0xF0) {
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
105 has_real_data[channel] = 1;
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
106 }
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
107 } else if (reg >= REG_FNUM_LOW_CH3 && reg < REG_ALG_FEEDBACK) {
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
108 channel = 2;
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
109 } else {
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
110 channel = 255;
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
111 }
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
112 case CMD_YM2612_1:
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
113 if (cmd == CMD_YM2612_1) {
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
114 reg = *(cur++);
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
115 param = *(cur++);
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
116 channel = 255;
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
117 }
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
118 if (channel >= PSG_BASE) {
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
119 if (reg >= REG_DETUNE_MULT && reg < REG_FNUM_LOW) {
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
120 channel = (cmd == CMD_YM2612_0 ? 0 : 3) + (reg & 0xC >> 2);
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
121 } else if ((reg >= REG_FNUM_LOW && reg < REG_FNUM_LOW_CH3) || (reg >= REG_ALG_FEEDBACK && reg < 0xC0)) {
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
122 channel = (cmd == CMD_YM2612_0 ? 0 : 3) + (reg & 0x3);
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
123 } else {
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
124 fprintf(stderr, "WARNING: Skipping nrecognized write to register %X on part %d\n", reg, (cmd == CMD_YM2612_0 ? 1 : 2));
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
125 }
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
126 }
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
127 if (channel < PSG_BASE) {
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
128 *(out_pos[channel]++) = cmd;
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
129 *(out_pos[channel]++) = reg;
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
130 *(out_pos[channel]++) = param;
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
131 }
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
132 break;
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
133 case CMD_WAIT: {
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
134 reg = *(cur++);
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
135 param = *(cur++);
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
136 for (int i = 0; i < OUT_CHANNELS; i++)
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
137 {
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
138 *(out_pos[i]++) = cmd;
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
139 *(out_pos[i]++) = reg;
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
140 *(out_pos[i]++) = param;
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
141 }
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
142 break;
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
143 }
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
144 case CMD_WAIT_60:
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
145 case CMD_WAIT_50:
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
146 case CMD_END:
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
147 for (int i = 0; i < OUT_CHANNELS; i++)
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
148 {
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
149 *(out_pos[i]++) = cmd;
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
150 }
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
151 cur = end;
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
152 break;
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
153 case CMD_DATA: {
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
154 uint8_t * start = cur - 1;
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
155 cur++; //skip compat command
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
156 uint8_t data_type = *(cur++);
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
157 uint32_t data_size = *(cur++);
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
158 data_size |= *(cur++) << 8;
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
159 data_size |= *(cur++) << 16;
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
160 data_size |= *(cur++) << 24;
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
161 if (cur + data_size > end) {
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
162 data_size = end - cur;
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
163 }
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
164 cur += data_size;
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
165 if (data_type == DATA_YM2612_PCM) {
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
166 memcpy(out_pos[DAC_CHANNEL], start, cur-start);
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
167 out_pos[DAC_CHANNEL] += cur-start;
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
168 } else {
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
169 fprintf(stderr, "WARNING: Skipping data block with unrecognized type %X\n", data_type);
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
170 }
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
171 break;
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
172 }
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
173 case CMD_DATA_SEEK: {
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
174 memcpy(out_pos[DAC_CHANNEL], cur-1, 5);
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
175 out_pos[DAC_CHANNEL] += 5;
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
176 cur += 4;
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
177 break;
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
178 }
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
179
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
180 default:
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
181 if (cmd >= CMD_WAIT_SHORT && cmd < (CMD_WAIT_SHORT + 0x10)) {
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
182 for (int i = 0; i < OUT_CHANNELS; i++)
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
183 {
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
184 *(out_pos[i]++) = cmd;
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
185 }
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
186 } else if (cmd >= CMD_YM2612_DAC && cmd < CMD_DAC_STREAM_SETUP) {
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
187 *(out_pos[DAC_CHANNEL]++) = cmd;
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
188 sample_count++;
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
189 } else {
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
190 fprintf(stderr, "unimplemented command: %X at offset %X, last valid command was %X\n", cmd, (unsigned int)(cur - data - 1), last_cmd);
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
191 exit(1);
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
192 }
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
193 }
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
194 last_cmd = cmd;
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
195 }
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
196 if (sample_count > SAMPLE_THRESHOLD) {
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
197 has_real_data[DAC_CHANNEL] = 1;
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
198 }
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
199 for (int i = 0; i < OUT_CHANNELS; i++)
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
200 {
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
201 if (has_real_data[i]) {
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
202 char fname[11];
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
203 sprintf(fname, i < PSG_BASE ? "ym_%d.vgm" : "psg_%d.vgm", i < PSG_BASE ? i : i - PSG_BASE);
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
204 f = fopen(fname, "wb");
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
205 if (!f) {
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
206 fprintf(stderr, "Failed to open %s for writing\n", fname);
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
207 exit(1);
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
208 }
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
209 data_size = out_pos[i] - buffers[i];
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
210 header.eof_offset = (header.data_offset + 0x34) + data_size - 4;
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
211 fwrite(&header, 1, sizeof(header), f);
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
212 fseek(f, header.data_offset + 0x34, SEEK_SET);
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
213 fwrite(buffers[i], 1, data_size, f);
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
214 fclose(f);
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
215 }
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
216 }
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
217 return 0;
7068a9db6dd0 Wrote a buggy tool for splitting VGM files by channel
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
218 }