forked from vgmrips/vgmtools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
opl_23.c
568 lines (523 loc) · 11.2 KB
/
opl_23.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
// opl_23.c - VGM Compressor
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <zlib.h>
#include "stdtype.h"
#include "stdbool.h"
#include "VGMFile.h"
#include "vgm_lib.h"
#include "common.h"
static bool OpenVGMFile(const char* FileName);
static void WriteVGMFile(const char* FileName);
static void CompressVGMData(void);
VGM_HEADER VGMHead;
UINT32 VGMDataLen;
UINT8* VGMData;
UINT32 VGMPos;
INT32 VGMSmplPos;
//UINT8* DstData;
//UINT32 DstDataLen;
char FileBase[0x100];
#define CONV_3toDUAL2 0x00
#define CONV_DUAL2to3 0x01
#define CONV_DUAL2to2 0x02
UINT8 ConvMode;
#define OPL3_LEFT 0x10
#define OPL3_RIGHT 0x20
UINT8 OPL2ChipMap[0x02] = {0x01, 0x00};
int main(int argc, char* argv[])
{
int argbase;
int ErrVal;
char FileName[0x100];
bool NeedProcessing;
UINT32 NewClk_OPL2;
UINT32 NewClk_OPL3;
int OPLout;
printf("OPL 2<->3 Converter\n-------------------\n");
ErrVal = 0;
argbase = 1;
ConvMode = 0xFF;
while(argbase < argc && argv[argbase][0] == '-')
{
if (! stricmp(argv[argbase], "-3d2"))
{
ConvMode = CONV_3toDUAL2;
argbase ++;
}
else if (! stricmp(argv[argbase], "-d23"))
{
ConvMode = CONV_DUAL2to3;
argbase ++;
}
else if (! stricmp(argv[argbase], "-d22"))
{
ConvMode = CONV_DUAL2to2;
argbase ++;
}
else
{
break;
}
}
if (argc < argbase + 1 || ConvMode == 0xFF)
{
printf("Usage: opl_23 -mode input.vgm [output.vgm]\n");
printf("Modes:\n");
printf(" 3d2 - OPL3 -> Dual OPL2\n");
printf(" d23 - Dual OPL2 -> OPL3\n");
printf(" d22 - Dual OPL2 -> Single OPL2\n");
return 0;
}
printf("File Name:\t");
strcpy(FileName, argv[argbase + 0]);
printf("%s\n", FileName);
if (! strlen(FileName))
return 0;
if (! OpenVGMFile(FileName))
{
printf("Error opening the file!\n");
ErrVal = 1;
goto EndProgram;
}
printf("\n");
OPLout = 0;
NeedProcessing = false;
switch(ConvMode)
{
case CONV_3toDUAL2:
if (VGMHead.lngHzYMF262)
{
if (VGMHead.lngHzYMF262 & 0x40000000)
{
printf("Sorry, but I can't work with Dual OPL3!\n");
}
else
{
NeedProcessing = true;
NewClk_OPL2 = 0xC0000000 | (VGMHead.lngHzYMF262 / 4);
NewClk_OPL3 = 0x00;
}
}
OPLout = 2;
break;
case CONV_DUAL2to3:
if (VGMHead.lngHzYM3812)
{
if (~VGMHead.lngHzYM3812 & 0xC0000000)
{
printf("Sorry, but currently I need panned DualOPL2!\n");
}
else
{
NeedProcessing = true;
NewClk_OPL2 = 0x00;
NewClk_OPL3 = (VGMHead.lngHzYM3812 & ~0xC0000000) * 4;
}
}
OPLout = 3;
break;
case CONV_DUAL2to2:
if (VGMHead.lngHzYM3812)
{
if (~VGMHead.lngHzYM3812 & 0x40000000)
{
printf("Sorry, but I need DualOPL2!\n");
}
else
{
NeedProcessing = true;
NewClk_OPL2 = VGMHead.lngHzYM3812 & ~0xC0000000;
NewClk_OPL3 = VGMHead.lngHzYMF262;
}
}
OPLout = 2;
break;
}
if (NeedProcessing)
{
memcpy(&VGMData[0x50], &NewClk_OPL2, 0x04);
memcpy(&VGMData[0x5C], &NewClk_OPL3, 0x04);
CompressVGMData();
if (argc > argbase + 1)
strcpy(FileName, argv[argbase + 1]);
else
strcpy(FileName, "");
if (FileName[0] == '\0')
sprintf(FileName, "%s_opl%u.vgm", FileBase, OPLout);
WriteVGMFile(FileName);
}
free(VGMData);
//free(DstData);
EndProgram:
DblClickWait(argv[0]);
return ErrVal;
}
static bool OpenVGMFile(const char* FileName)
{
gzFile hFile;
UINT32 CurPos;
UINT32 TempLng;
char* TempPnt;
hFile = gzopen(FileName, "rb");
if (hFile == NULL)
return false;
gzseek(hFile, 0x00, SEEK_SET);
gzread(hFile, &TempLng, 0x04);
if (TempLng != FCC_VGM)
goto OpenErr;
gzseek(hFile, 0x00, SEEK_SET);
gzread(hFile, &VGMHead, sizeof(VGM_HEADER));
ZLIB_SEEKBUG_CHECK(VGMHead);
// Header preperations
if (VGMHead.lngVersion < 0x00000101)
{
VGMHead.lngRate = 0;
}
if (VGMHead.lngVersion < 0x00000110)
{
VGMHead.shtPSG_Feedback = 0x0000;
VGMHead.bytPSG_SRWidth = 0x00;
VGMHead.lngHzYM2612 = VGMHead.lngHzYM2413;
VGMHead.lngHzYM2151 = VGMHead.lngHzYM2413;
}
if (VGMHead.lngVersion < 0x00000150)
{
VGMHead.lngDataOffset = 0x00000000;
}
if (VGMHead.lngVersion < 0x00000151)
{
VGMHead.lngHzSPCM = 0x0000;
VGMHead.lngSPCMIntf = 0x00000000;
// all others are zeroed by memset
}
// relative -> absolute addresses
VGMHead.lngEOFOffset += 0x00000004;
if (VGMHead.lngGD3Offset)
VGMHead.lngGD3Offset += 0x00000014;
if (VGMHead.lngLoopOffset)
VGMHead.lngLoopOffset += 0x0000001C;
if (! VGMHead.lngDataOffset)
VGMHead.lngDataOffset = 0x0000000C;
VGMHead.lngDataOffset += 0x00000034;
CurPos = VGMHead.lngDataOffset;
if (VGMHead.lngVersion < 0x00000150)
CurPos = 0x40;
TempLng = sizeof(VGM_HEADER);
if (TempLng > CurPos)
TempLng -= CurPos;
else
TempLng = 0x00;
memset((UINT8*)&VGMHead + CurPos, 0x00, TempLng);
// Read Data
VGMDataLen = VGMHead.lngEOFOffset;
VGMData = (UINT8*)malloc(VGMDataLen);
if (VGMData == NULL)
goto OpenErr;
gzseek(hFile, 0x00, SEEK_SET);
gzread(hFile, VGMData, VGMDataLen);
gzclose(hFile);
strcpy(FileBase, FileName);
TempPnt = strrchr(FileBase, '.');
if (TempPnt != NULL)
*TempPnt = 0x00;
return true;
OpenErr:
gzclose(hFile);
return false;
}
static void WriteVGMFile(const char* FileName)
{
FILE* hFile;
hFile = fopen(FileName, "wb");
//fwrite(DstData, 0x01, DstDataLen, hFile);
fwrite(VGMData, 0x01, VGMDataLen, hFile);
fclose(hFile);
printf("File written.\n");
return;
}
static void CompressVGMData(void)
{
UINT8 ChipID;
UINT8 Command;
UINT8 TempByt;
UINT16 TempSht;
UINT32 TempLng;
//UINT32 ROMSize;
//UINT32 DataStart;
//UINT32 DataLen;
UINT32 CmdLen;
bool StopVGM;
UINT8* VGMPnt;
UINT32 VGMPosN;
bool UsePosN;
bool SkipCmdCopy;
UINT16 LastOPLCmd;
UINT32 LoopPosN;
UsePosN = false;
if (ConvMode == CONV_DUAL2to2)
UsePosN = true;
//DstData = (UINT8*)malloc(VGMDataLen + 0x100);
VGMPos = VGMHead.lngDataOffset;
VGMPosN = VGMPos;
//DstPos = VGMHead.lngDataOffset;
VGMSmplPos = 0;
//memcpy(DstData, VGMData, VGMPos); // Copy Header
StopVGM = false;
SkipCmdCopy = false;
LastOPLCmd = 0xFFFF;
while(VGMPos < VGMHead.lngEOFOffset)
{
if (VGMPos == VGMHead.lngLoopOffset)
LoopPosN = VGMPosN;
CmdLen = 0x00;
Command = VGMData[VGMPos + 0x00];
if (Command >= 0x70 && Command <= 0x8F)
{
switch(Command & 0xF0)
{
case 0x70:
TempSht = (Command & 0x0F) + 0x01;
VGMSmplPos += TempSht;
break;
case 0x80:
TempSht = Command & 0x0F;
VGMSmplPos += TempSht;
break;
}
CmdLen = 0x01;
}
else
{
VGMPnt = &VGMData[VGMPos];
// Cheat Mode (to use 2 instances of 1 chip)
ChipID = 0x00;
switch(Command)
{
case 0xAA:
if (VGMHead.lngHzYM3812 & 0x40000000)
{
Command -= 0x50;
ChipID = 0x01;
}
break;
case 0xAB:
if (VGMHead.lngHzYM3526 & 0x40000000)
{
Command -= 0x50;
ChipID = 0x01;
}
break;
case 0xAC:
if (VGMHead.lngHzY8950 & 0x40000000)
{
Command -= 0x50;
ChipID = 0x01;
}
break;
case 0xAE:
case 0xAF:
if (VGMHead.lngHzYMF262 & 0x40000000)
{
Command -= 0x50;
ChipID = 0x01;
}
break;
}
switch(Command)
{
case 0x66: // End Of File
CmdLen = 0x01;
StopVGM = true;
break;
case 0x62: // 1/60s delay
TempSht = 735;
VGMSmplPos += TempSht;
CmdLen = 0x01;
break;
case 0x63: // 1/50s delay
TempSht = 882;
VGMSmplPos += TempSht;
CmdLen = 0x01;
break;
case 0x61: // xx Sample Delay
memcpy(&TempSht, &VGMPnt[0x01], 0x02);
VGMSmplPos += TempSht;
CmdLen = 0x03;
break;
case 0x5A: // YM3812 write
switch(ConvMode)
{
case CONV_DUAL2to3:
switch(VGMPnt[0x01] & 0xF0)
{
case 0xC0:
if (ChipID == 0x00)
VGMPnt[0x02] |= OPL3_LEFT;
else if (ChipID == 0x01)
VGMPnt[0x02] |= OPL3_RIGHT;
break;
}
if (VGMPnt[0x01] == 0x05)
{
VGMPnt[0x00] = 0x5F;
VGMPnt[0x02] |= 0x01;
}
else if (VGMPnt[0x01] < 0x20)
VGMPnt[0x00] = 0x5E;
else
VGMPnt[0x00] = 0x5E + OPL2ChipMap[ChipID];
break;
case CONV_DUAL2to2:
TempSht = (VGMPnt[0x01] << 8) | (VGMPnt[0x02] << 0);
if (! ChipID)
{
LastOPLCmd = TempSht;
}
else
{
if (LastOPLCmd != TempSht)
printf("Warning at Pos 0x%04X: different OPL writes (%04X != %04X)\n",
VGMPos, LastOPLCmd, TempSht);
SkipCmdCopy = true;
}
break;
}
CmdLen = 0x03;
break;
case 0x5E: // YMF262 write port 0
case 0x5F: // YMF262 write port 1
TempByt = Command & 0x01;
if (ConvMode == CONV_3toDUAL2)
{
switch(VGMPnt[0x01] & 0xF0)
{
case 0x00:
if (! TempByt && VGMPnt[0x01] == 0x01)
VGMPnt[0x02] |= 0x20; // enforce Wave Forms
break;
case 0xC0:
if (TempByt == OPL2ChipMap[0x00] && (VGMPnt[0x02] & OPL3_RIGHT))
printf("Warning! Stereo mismatch at 0x%06X!\n", VGMPos);
else if (TempByt == OPL2ChipMap[0x01] && (VGMPnt[0x02] & OPL3_LEFT))
printf("Warning! Stereo mismatch at 0x%06X!\n", VGMPos);
break;
case 0xE0:
case 0xF0:
if (VGMPnt[0x02] & 0x04)
printf("Warning! Waveform %02d used at 0x%06X!\n", VGMPnt[0x02], VGMPos);
break;
}
if (VGMPnt[0x01] < 0x20)
VGMPnt[0x00] = TempByt ? 0xAA : 0x5A;
else
VGMPnt[0x00] = OPL2ChipMap[TempByt] ? 0xAA : 0x5A;
}
CmdLen = 0x03;
break;
case 0x50: // SN76496 write
CmdLen = 0x02;
break;
case 0x67: // PCM Data Stream
memcpy(&TempLng, &VGMPnt[0x03], 0x04);
ChipID = (TempLng & 0x80000000) >> 31;
TempLng &= 0x7FFFFFFF;
CmdLen = 0x07 + TempLng;
break;
case 0xE0: // Seek to PCM Data Bank Pos
memcpy(&TempLng, &VGMPnt[0x01], 0x04);
CmdLen = 0x05;
break;
case 0x4F: // GG Stereo
CmdLen = 0x02;
break;
case 0x68: // PCM RAM write
CmdLen = 0x0C;
break;
case 0x90: // DAC Ctrl: Setup Chip
CmdLen = 0x05;
break;
case 0x91: // DAC Ctrl: Set Data
CmdLen = 0x05;
break;
case 0x92: // DAC Ctrl: Set Freq
CmdLen = 0x06;
break;
case 0x93: // DAC Ctrl: Play from Start Pos
CmdLen = 0x0B;
break;
case 0x94: // DAC Ctrl: Stop immediately
CmdLen = 0x02;
break;
case 0x95: // DAC Ctrl: Play Block (small)
CmdLen = 0x05;
break;
default:
switch(Command & 0xF0)
{
case 0x30:
case 0x40:
CmdLen = 0x02;
break;
case 0x50:
case 0xA0:
case 0xB0:
CmdLen = 0x03;
break;
case 0xC0:
case 0xD0:
CmdLen = 0x04;
break;
case 0xE0:
case 0xF0:
CmdLen = 0x05;
break;
default:
printf("Unknown Command: %X\n", Command);
CmdLen = 0x01;
//StopVGM = true;
break;
}
break;
}
}
if (UsePosN)
{
if (SkipCmdCopy)
{
SkipCmdCopy = false;
}
else
{
memcpy(&VGMData[VGMPosN], &VGMData[VGMPos], CmdLen);
VGMPosN += CmdLen;
}
}
VGMPos += CmdLen;
if (StopVGM)
break;
}
printf("\t\t\t\t\t\t\t\t\r");
if (UsePosN)
{
if (LoopPosN)
{
LoopPosN -= 0x1C;
memcpy(&VGMData[0x1C], &LoopPosN, 0x04);
}
if (VGMPos < VGMHead.lngEOFOffset)
{
TempLng = VGMHead.lngEOFOffset - VGMPos;
memmove(&VGMData[VGMPosN], &VGMData[VGMPos], TempLng);
}
if (VGMHead.lngGD3Offset)
{
TempLng = VGMHead.lngGD3Offset - 0x14;
TempLng -= (VGMPos - VGMPosN);
memcpy(&VGMData[0x14], &TempLng, 0x04);
}
}
return;
}