]> 4ch.mooo.com Git - 16.git/blob - src/lib/vgmsnd/vgmSnd.c
bakapi modex16 test fun: allow video mode choice from command line.
[16.git] / src / lib / vgmsnd / vgmSnd.c
1 // vgmSndDrv.c - VGM Sound Driver for OPL2\r
2 // Valley Bell, 2015-07-27\r
3 \r
4 // Note: This uses quite a few optimizations that assume that your\r
5 //       machine is Little Endian.\r
6 \r
7 #include <stdio.h>\r
8 #include <stdlib.h>\r
9 #include <string.h>\r
10 \r
11 #include "src/lib/vgmsnd/common.h"\r
12 #include "src/lib/vgmsnd/vgmSnd.h"\r
13 \r
14 \r
15 #define QUICK_READ\r
16 \r
17 \r
18 \r
19 #define FCC_VGM 0x206D6756      // 'Vgm '\r
20 #define FCC_GD3 0x20336447      // 'Gd3 '\r
21 \r
22 typedef struct _vgm_file_header_base\r
23 {\r
24         dword fccVGM;                   // 00\r
25         dword/*32*/ lngEOFOffset;       // 04\r
26         dword/*32*/ lngVersion;         // 08\r
27         dword/*32*/ lngSkip1[2];                // 0C\r
28         dword/*32*/ lngGD3Offset;       // 14\r
29         dword/*32*/ lngTotalSamples;    // 18\r
30         dword/*32*/ lngLoopOffset;      // 1C\r
31         dword/*32*/ lngLoopSamples;     // 20\r
32         dword/*32*/ lngRate;                    // 24\r
33         dword/*32*/ lngSkip2[3];                // 28\r
34         dword/*32*/ lngDataOffset;      // 34\r
35         dword/*32*/ lngSkip3[2];                // 38\r
36 } VGM_BASE_HDR;\r
37 \r
38 #define PBMODE_MUSIC    0x00\r
39 #define PBMODE_SFX              0x01\r
40 typedef struct _vgm_playback\r
41 {\r
42         UINT8 pbMode;\r
43         UINT8 vgmEnd;   // 00 - running, 01 - finished, FF - not loaded\r
44         UINT16 curLoopCnt;\r
45         dword/*32*/ vgmPos;\r
46         dword/*32*/ vgmSmplPos;\r
47         dword/*32*/ pbSmplPos;\r
48         VGM_FILE* file;\r
49 \r
50         // oplChnMask:\r
51         //      Music: mask of channels used/overridden by SFX\r
52         //      SFX:   ID of channel used by SFX (all commands are forces to it)\r
53         UINT16 oplChnMask;\r
54         UINT8* oplRegCache;\r
55         UINT8 workRAM[0x04];\r
56 } VGM_PBK;\r
57 \r
58 \r
59 \r
60 INLINE UINT16 ReadLE16(const UINT8* buffer)\r
61 {\r
62 #ifdef QUICK_READ\r
63         return *(UINT16*)buffer;\r
64 #else\r
65         return (buffer[0x00] << 0) | (buffer[0x01] << 8);\r
66 #endif\r
67 }\r
68 \r
69 INLINE dword/*32*/ ReadLE32(const UINT8* buffer)\r
70 {\r
71 #ifdef QUICK_READ\r
72         return *(dword/*32*/*)buffer;\r
73 #else\r
74         return  (buffer[0x00] <<  0) | (buffer[0x01] <<  8) |\r
75                         (buffer[0x02] << 16) | (buffer[0x03] << 24);\r
76 #endif\r
77 }\r
78 \r
79 \r
80 // Function Prototypes\r
81 //UINT8 OpenVGMFile(const char* FileName, VGM_FILE* vgmFile);\r
82 //void FreeVGMFile(VGM_FILE* vgmFile);\r
83 \r
84 static boolean DoVgmLoop(VGM_PBK* vgmPlay);\r
85 static void UpdateVGM(VGM_PBK* vgmPlay, UINT16 Samples);\r
86 \r
87 //void InitEngine(void);\r
88 //void DeinitEngine(void);\r
89 \r
90 //UINT8 PlayMusic(VGM_FILE* vgmFile);\r
91 //UINT8 PlaySFX(VGM_FILE* vgmFile, UINT8 sfxChnID);\r
92 //UINT8 StopMusic(void);\r
93 //UINT8 StopSFX(UINT8 sfxChnID);        // Note: sfxChnID == 0xFF -> stop all SFX\r
94 //UINT8 PauseMusic(void);\r
95 //UINT8 ResumeMusic(void);\r
96 static void StartPlayback(VGM_PBK* vgmPb);\r
97 static void StopPlayback(VGM_PBK* vgmPb);\r
98 \r
99 static void ym2413_write(VGM_PBK* vgmPb, UINT8 reg, UINT8 data);\r
100 static void ym3812_write(VGM_PBK* vgmPb, UINT8 reg, UINT8 data);\r
101 static void ym3512_write(VGM_PBK* vgmPb, UINT8 reg, UINT8 data);\r
102 static void ymf262_write(VGM_PBK* vgmPb, UINT8 port, UINT8 reg, UINT8 data);\r
103 \r
104 //void UpdateSoundEngine(void);\r
105 \r
106 \r
107 // Functions that must be supplied by external library\r
108 extern void OPL2_Write(UINT8 reg, UINT8 data);\r
109 extern UINT8 OPL2_ReadStatus(void);\r
110 \r
111 \r
112 \r
113 \r
114 #define SFX_CHN_COUNT   6\r
115 \r
116 #define TIMER1_RATE             7       // 256-7 = 248; (3579545/72/4) / 248 = 50.12 Hz ~ 50 Hz\r
117 #define VGM_UPD_RATE    880     // 880 samples (44.1 KHz) = 50.11 Hz\r
118 \r
119 static VGM_PBK vgmPbMusic;\r
120 static VGM_PBK vgmPbSFX[SFX_CHN_COUNT];\r
121 \r
122 static UINT8 oplRegs_Music[0x100];\r
123 static UINT8 oplRegs_SFX[SFX_CHN_COUNT][0x0D];  // 20 23 40 43 60 63 80 83 E0 E3 C0 A0 B0\r
124 \r
125 static const UINT8 SFX_REGS[0x0D] =\r
126 {       0x20, 0x23, 0x40, 0x43, 0x60, 0x63, 0x80, 0x83,\r
127         0xE0, 0xE3, 0xC0, 0xA0, 0xB0};\r
128 static const UINT8 SFX_REGS_REV[0x10] = // 20/30 -> 0, 40/50 -> 2, ...\r
129 {       0xFF, 0xFF, 0x00, 0x00, 0x02, 0x02, 0x04, 0x04,\r
130         0x06, 0x06, 0x0B, 0x0C, 0x0A, 0xFF, 0x08, 0x08};\r
131 static const UINT8 CHN_OPMASK[0x09] =\r
132 {       0x00, 0x01, 0x02, 0x08, 0x09, 0x0A, 0x10, 0x11, 0x12};\r
133 static const UINT8 CHN_OPMASK_REV[0x20] =\r
134 {       0x00, 0x01, 0x02, 0x80, 0x81, 0x82, 0xFF, 0xFF,\r
135         0x03, 0x04, 0x05, 0x83, 0x84, 0x85, 0xFF, 0xFF,\r
136         0x06, 0x07, 0x08, 0x86, 0x87, 0x88, 0xFF, 0xFF,\r
137         0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};\r
138 \r
139 \r
140 UINT8 OpenVGMFile(const char* FileName, VGM_FILE* vgmFile)\r
141 {\r
142         size_t hdrSize;\r
143         size_t readEl;  // 'elements' read from file\r
144         size_t bytesToRead;\r
145         VGM_BASE_HDR vgmBaseHdr;\r
146         FILE* hFile;\r
147         dword/*32*/ CurPos;\r
148 \r
149         memset(vgmFile, 0x00, sizeof(VGM_FILE));\r
150 \r
151         hFile = fopen(FileName, "rb");\r
152         if (hFile == NULL)\r
153                 return 0xFF;\r
154 \r
155         hdrSize = sizeof(VGM_BASE_HDR);\r
156         readEl = fread(&vgmBaseHdr, hdrSize, 0x01, hFile);\r
157         if (readEl == 0)\r
158         {\r
159                 fclose(hFile);\r
160                 return 0xFE;    // read error\r
161         }\r
162         if (vgmBaseHdr.fccVGM != FCC_VGM)\r
163         {\r
164                 fclose(hFile);\r
165                 return 0x80;    // bad signature\r
166         }\r
167         if (vgmBaseHdr.lngVersion < 0x0150)\r
168         {\r
169                 fclose(hFile);\r
170                 return 0x81;    // We don't support VGM v1.10 and earlier\r
171         }\r
172 \r
173         vgmFile->dataLen = vgmBaseHdr.lngEOFOffset + 0x04;\r
174         vgmFile->data = (UINT8*)malloc(vgmFile->dataLen);\r
175         if (vgmFile->data == NULL)\r
176         {\r
177                 fclose(hFile);\r
178                 return 0xF0;    // malloc error\r
179         }\r
180         memcpy(vgmFile->data, &vgmBaseHdr, hdrSize);\r
181         bytesToRead = vgmFile->dataLen - hdrSize;\r
182         readEl = fread(vgmFile->data + hdrSize, 0x01, bytesToRead, hFile);\r
183         if (readEl < bytesToRead)\r
184         {\r
185                 //fclose(hFile);\r
186                 //return 0xFE;  // read error\r
187                 vgmFile->dataLen = hdrSize + readEl;\r
188         }\r
189 \r
190         fclose(hFile);\r
191 \r
192         memcpy(&vgmFile->header, vgmFile->data, sizeof(VGM_HEADER));\r
193 \r
194         // relative -> absolute addresses\r
195         vgmFile->header.lngEOFOffset += 0x04;\r
196         if (vgmFile->header.lngGD3Offset)\r
197                 vgmFile->header.lngGD3Offset += 0x14;\r
198         if (vgmFile->header.lngLoopOffset)\r
199                 vgmFile->header.lngLoopOffset += 0x1C;\r
200         if (! vgmFile->header.lngDataOffset)\r
201                 vgmFile->header.lngDataOffset = 0x0C;\r
202         vgmFile->header.lngDataOffset += 0x34;\r
203 \r
204         CurPos = vgmFile->header.lngDataOffset;\r
205         if (vgmFile->header.lngVersion < 0x0150)\r
206                 CurPos = 0x40;\r
207         hdrSize = sizeof(VGM_HEADER);\r
208         if (hdrSize > CurPos)\r
209                 memset((UINT8*)&vgmFile->header + CurPos, 0x00, hdrSize - CurPos);\r
210 \r
211         fclose(hFile);\r
212         return 0x00;\r
213 }\r
214 \r
215 void FreeVGMFile(VGM_FILE* vgmFile)\r
216 {\r
217         free(vgmFile->data);    vgmFile->data = NULL;\r
218         vgmFile->dataLen = 0;\r
219 \r
220         return;\r
221 }\r
222 \r
223 \r
224 static boolean DoVgmLoop(VGM_PBK* vgmPlay)\r
225 {\r
226         const VGM_HEADER* vgmHdr = &vgmPlay->file->header;\r
227 \r
228         if (! vgmHdr->lngLoopOffset)\r
229                 return false;\r
230 \r
231         vgmPlay->curLoopCnt ++;\r
232 \r
233         vgmPlay->vgmPos = vgmHdr->lngLoopOffset;\r
234         vgmPlay->vgmSmplPos -= vgmHdr->lngLoopSamples;\r
235         vgmPlay->pbSmplPos -= vgmHdr->lngLoopSamples;\r
236 \r
237         return true;\r
238 }\r
239 \r
240 static void UpdateVGM(VGM_PBK* vgmPlay, UINT16 Samples)\r
241 {\r
242         const dword/*32*/ vgmLen = vgmPlay->file->dataLen;\r
243         const UINT8* vgmData = vgmPlay->file->data;\r
244         const UINT8* VGMPnt;\r
245         dword/*32*/ VGMPos;\r
246         dword/*32*/ VGMSmplPos;\r
247         UINT8 Command;\r
248         UINT8 blockType;\r
249         dword/*32*/ blockLen;\r
250 \r
251         vgmPlay->pbSmplPos += Samples;\r
252         VGMPos = vgmPlay->vgmPos;\r
253         VGMSmplPos = vgmPlay->vgmSmplPos;\r
254         while(VGMSmplPos < vgmPlay->pbSmplPos && ! vgmPlay->vgmEnd)\r
255         {\r
256                 VGMPnt = &vgmData[VGMPos];\r
257                 Command = VGMPnt[0x00];\r
258                 switch(Command & 0xF0)\r
259                 {\r
260                 case 0x70:      // small delay (1-16 samples)\r
261                         VGMSmplPos += (Command & 0x0F) + 0x01;\r
262                         VGMPos += 0x01;\r
263                         break;\r
264                 case 0x80:      // DAC write + small delay (0-15 samples)\r
265                         VGMSmplPos += (Command & 0x0F);\r
266                         VGMPos += 0x01;\r
267                         break;\r
268                 case 0x60:\r
269                         switch(Command)\r
270                         {\r
271                         case 0x66:      // End Of File\r
272                                 vgmPlay->vgmPos = VGMPos;\r
273                                 vgmPlay->vgmSmplPos = VGMSmplPos;\r
274                                 if (! DoVgmLoop(vgmPlay))\r
275                                         vgmPlay->vgmEnd = 0x01;\r
276                                 VGMPos = vgmPlay->vgmPos;\r
277                                 VGMSmplPos = vgmPlay->vgmSmplPos;\r
278                                 break;\r
279                         case 0x62:      // 1/60s delay\r
280                                 VGMSmplPos += 735;\r
281                                 VGMPos += 0x01;\r
282                                 break;\r
283                         case 0x63:      // 1/50s delay\r
284                                 VGMSmplPos += 882;\r
285                                 VGMPos += 0x01;\r
286                                 break;\r
287                         case 0x61:      // xx Sample Delay\r
288                                 VGMSmplPos += ReadLE16(&VGMPnt[0x01]);\r
289                                 VGMPos += 0x03;\r
290                                 break;\r
291                         case 0x67:      // Data Block (PCM Data Stream)\r
292                                 blockType = VGMPnt[0x02];\r
293                                 blockLen = ReadLE32(&VGMPnt[0x03]);\r
294                                 blockLen &= 0x7FFFFFFF;\r
295                                 VGMPos += 0x07 + blockLen;\r
296                                 break;\r
297                         case 0x68:      // PCM RAM write\r
298                                 VGMPos += 0x0C;\r
299                                 break;\r
300                         default:\r
301                                 vgmPlay->vgmEnd = 0x01;\r
302                                 break;\r
303                         }\r
304                         break;\r
305                 case 0x50:\r
306                         if (Command == 0x50)\r
307                         {\r
308                                 VGMPos += 0x02; // SN76496 write\r
309                                 break;\r
310                         }\r
311                         switch(Command)\r
312                         {\r
313                         case 0x51:      // YM2413 write\r
314                                 ym2413_write(vgmPlay, VGMPnt[0x01], VGMPnt[0x02]);\r
315                                 break;\r
316                         case 0x5A:      // YM3812 write\r
317                                 ym3812_write(vgmPlay, VGMPnt[0x01], VGMPnt[0x02]);\r
318                                 break;\r
319                         case 0x5B:      // YM3526 write\r
320                         case 0x5C:      // Y8950 write\r
321                                 ym3512_write(vgmPlay, VGMPnt[0x01], VGMPnt[0x02]);\r
322                                 break;\r
323                         case 0x5E:      // YMF262 write, port 0\r
324                         case 0x5F:      // YMF262 write, port 1\r
325                                 ymf262_write(vgmPlay, Command & 0x01, VGMPnt[0x01], VGMPnt[0x02]);\r
326                                 break;\r
327                         }\r
328                         VGMPos += 0x03;\r
329                         break;\r
330                 case 0x30:\r
331                         VGMPos += 0x02;\r
332                         break;\r
333                 case 0x40:\r
334                 case 0xA0:\r
335                 case 0xB0:\r
336                         VGMPos += 0x03;\r
337                         break;\r
338                 case 0xC0:\r
339                 case 0xD0:\r
340                         VGMPos += 0x04;\r
341                         break;\r
342                 case 0xE0:\r
343                 case 0xF0:\r
344                         VGMPos += 0x05;\r
345                         break;\r
346                 case 0x90:\r
347                         switch(Command)\r
348                         {\r
349                         case 0x90:      // DAC Ctrl: Setup Chip\r
350                                 VGMPos += 0x05;\r
351                                 break;\r
352                         case 0x91:      // DAC Ctrl: Set Data\r
353                                 VGMPos += 0x05;\r
354                                 break;\r
355                         case 0x92:      // DAC Ctrl: Set Freq\r
356                                 VGMPos += 0x06;\r
357                                 break;\r
358                         case 0x93:      // DAC Ctrl: Play from Start Pos\r
359                                 VGMPos += 0x0B;\r
360                                 break;\r
361                         case 0x94:      // DAC Ctrl: Stop immediately\r
362                                 VGMPos += 0x02;\r
363                                 break;\r
364                         case 0x95:      // DAC Ctrl: Play Block (small)\r
365                                 VGMPos += 0x05;\r
366                                 break;\r
367                         default:\r
368                                 vgmPlay->vgmEnd = 0x01;\r
369                                 break;\r
370                         }\r
371                         break;\r
372                 default:\r
373                         vgmPlay->vgmEnd = 0x01;\r
374                         return;\r
375                 }\r
376 \r
377                 if (VGMPos >= vgmLen)\r
378                         vgmPlay->vgmEnd = 0x01;\r
379         }\r
380         vgmPlay->vgmPos = VGMPos;\r
381         vgmPlay->vgmSmplPos = VGMSmplPos;\r
382         if (vgmPlay->vgmEnd)\r
383                 StopPlayback(vgmPlay);\r
384 \r
385         return;\r
386 }\r
387 \r
388 \r
389 \r
390 \r
391 void InitEngine(void)\r
392 {\r
393         UINT8 curSFX;\r
394         UINT8 curReg;\r
395 \r
396         memset(oplRegs_Music, 0x00, 0x100);\r
397         memset(&vgmPbMusic, 0x00, sizeof(VGM_PBK));\r
398         vgmPbMusic.pbMode = PBMODE_MUSIC;\r
399         vgmPbMusic.vgmEnd = 0xFF;\r
400         vgmPbMusic.oplChnMask = 0x0000;\r
401         vgmPbMusic.oplRegCache = oplRegs_Music;\r
402 \r
403         for (curSFX = 0; curSFX < SFX_CHN_COUNT; curSFX ++)\r
404         {\r
405                 memset(&oplRegs_SFX[curSFX], 0x00, sizeof(VGM_PBK));\r
406                 memset(&vgmPbSFX[curSFX], 0x00, sizeof(VGM_PBK));\r
407                 vgmPbSFX[curSFX].pbMode = PBMODE_SFX;\r
408                 vgmPbSFX[curSFX].vgmEnd = 0xFF;\r
409                 vgmPbSFX[curSFX].oplChnMask = curSFX;\r
410                 vgmPbSFX[curSFX].oplRegCache = oplRegs_SFX[curSFX];\r
411         }\r
412 \r
413         // reset OPL2 chip\r
414         curReg = 0x00;\r
415         do\r
416         {\r
417                 curReg --;\r
418                 OPL2_Write(curReg, 0x00);\r
419         } while(curReg > 0x20);\r
420 \r
421         OPL2_Write(0x02, TIMER1_RATE);  // set Timer 1 Period\r
422         OPL2_Write(0x04, 0x01); // Timer 1 on/unmasked, Timer 2 off\r
423         OPL2_Write(0x04, 0x80); // Reset Timer/IRQ Status Flags\r
424 \r
425         OPL2_Write(0x01, 0x20); // Waveform Select: Enable\r
426 \r
427         return;\r
428 }\r
429 \r
430 void DeinitEngine(void)\r
431 {\r
432         UINT8 curSFX;\r
433 \r
434         StopPlayback(&vgmPbMusic);\r
435         for (curSFX = 0; curSFX < SFX_CHN_COUNT; curSFX ++)\r
436                 StopPlayback(&vgmPbSFX[curSFX]);\r
437 \r
438         OPL2_Write(0x04, 0x00); // disable all timers\r
439 \r
440         return;\r
441 }\r
442 \r
443 \r
444 UINT8 PlayMusic(VGM_FILE* vgmFile)\r
445 {\r
446         VGM_PBK* vgmPb = &vgmPbMusic;\r
447 \r
448         if (! vgmPb->vgmEnd)\r
449                 StopPlayback(vgmPb);\r
450 \r
451         vgmPb->file = vgmFile;\r
452 \r
453         StartPlayback(vgmPb);\r
454 \r
455         return 0x00;\r
456 }\r
457 \r
458 UINT8 PlaySFX(VGM_FILE* vgmFile, UINT8 sfxChnID)\r
459 {\r
460         VGM_PBK* vgmPb;\r
461 \r
462         if (sfxChnID >= SFX_CHN_COUNT)\r
463                 return 0xFF;\r
464 \r
465         vgmPb = &vgmPbSFX[sfxChnID];\r
466 \r
467         if (! vgmPb->vgmEnd)\r
468                 StopPlayback(vgmPb);\r
469 \r
470         vgmPb->file = vgmFile;\r
471 \r
472         StartPlayback(vgmPb);\r
473 \r
474         return 0x00;\r
475 }\r
476 \r
477 UINT8 StopMusic(void)\r
478 {\r
479         StopPlayback(&vgmPbMusic);\r
480         return 0x00;\r
481 }\r
482 \r
483 UINT8 StopSFX(UINT8 sfxChnID)\r
484 {\r
485         if (sfxChnID == 0xFF)\r
486         {\r
487                 for (sfxChnID = 0; sfxChnID < SFX_CHN_COUNT; sfxChnID ++)\r
488                         StopPlayback(&vgmPbSFX[sfxChnID]);\r
489                 return 0x00;\r
490         }\r
491 \r
492         if (sfxChnID >= SFX_CHN_COUNT)\r
493                 return 0xFF;\r
494 \r
495         StopPlayback(&vgmPbSFX[sfxChnID]);\r
496         return 0x00;\r
497 }\r
498 \r
499 UINT8 PauseMusic(void)\r
500 {\r
501         if (vgmPbMusic.vgmEnd == 0xFF)\r
502                 return 0xFF;    // not playing\r
503         if (vgmPbMusic.vgmEnd == 0x01)\r
504                 return 0x80;    // finished playing already\r
505         if (vgmPbMusic.vgmEnd == 0x02)\r
506                 return 0x01;    // is already paused\r
507 \r
508         StopPlayback(&vgmPbMusic);\r
509         vgmPbMusic.vgmEnd = 0x02;\r
510 \r
511         return 0x00;\r
512 }\r
513 \r
514 UINT8 ResumeMusic(void)\r
515 {\r
516         if (vgmPbMusic.vgmEnd == 0xFF)\r
517                 return 0xFF;    // not playing\r
518         if (vgmPbMusic.vgmEnd == 0x01)\r
519                 return 0x80;    // finished playing already\r
520         if (! (vgmPbMusic.vgmEnd & 0x02))\r
521                 return 0x01;    // is not paused\r
522 \r
523         vgmPbMusic.vgmEnd &= ~0x02;\r
524 \r
525         return 0x00;\r
526 }\r
527 \r
528 static void StartPlayback(VGM_PBK* vgmPb)\r
529 {\r
530         if (vgmPb->file == NULL || vgmPb->file->data == NULL ||\r
531                 vgmPb->file->header.fccVGM != FCC_VGM\r
532 )\r
533         {\r
534                 vgmPb->vgmEnd = 0xFF;\r
535                 return;\r
536         }\r
537 \r
538         vgmPb->vgmEnd = 0x00;   // set to 'running'\r
539         vgmPb->vgmPos = vgmPb->file->header.lngDataOffset;\r
540         vgmPb->vgmSmplPos = 0;\r
541         vgmPb->pbSmplPos = 0;\r
542         vgmPb->curLoopCnt = 0;\r
543         memset(vgmPb->workRAM, 0x00, 0x04);\r
544 \r
545         if (vgmPb->pbMode == PBMODE_SFX)\r
546         {\r
547                 UINT8 curReg;\r
548 \r
549                 curReg = 0xB0 | vgmPb->oplChnMask;\r
550                 if (oplRegs_Music[curReg] & 0x20)\r
551                         OPL2_Write(curReg, oplRegs_Music[curReg] & ~0x20);      // send Key Off\r
552 \r
553                 vgmPbMusic.oplChnMask |= (1 << vgmPb->oplChnMask);      // mask out music channel\r
554         }\r
555 \r
556         return;\r
557 }\r
558 \r
559 static void StopPlayback(VGM_PBK* vgmPb)\r
560 {\r
561         if (vgmPb->vgmEnd & 0x80)\r
562                 return;\r
563 \r
564         if (vgmPb->pbMode == PBMODE_MUSIC)\r
565         {\r
566                 UINT8 curReg;\r
567                 UINT16 chnMask;\r
568 \r
569                 chnMask = 0x0001;\r
570                 for (curReg = 0xB0; curReg < 0xB9; curReg ++, chnMask <<= 1)\r
571                 {\r
572                         if (vgmPb->oplChnMask & chnMask)\r
573                                 continue;       // keep channels used by SFX untouched\r
574                         if (vgmPb->oplRegCache[curReg] & 0x20)\r
575                         {\r
576                                 vgmPb->oplRegCache[curReg] &= ~0x20;\r
577                                 OPL2_Write(curReg, vgmPb->oplRegCache[curReg]); // send Key Off\r
578                         }\r
579                 }\r
580                 curReg = 0xBD;  // rhythm register\r
581                 if (vgmPb->oplRegCache[curReg] & 0x1F)\r
582                 {\r
583                         vgmPb->oplRegCache[curReg] &= ~0x1F;\r
584                         OPL2_Write(curReg, vgmPb->oplRegCache[curReg]); // send Key Off\r
585                 }\r
586 \r
587                 vgmPb->vgmEnd = 0x01;\r
588         }\r
589         else //if (vgmPb->pbMode == PBMODE_SFX)\r
590         {\r
591                 UINT8 regID;\r
592                 UINT8 curReg;\r
593                 UINT8 opMask;\r
594 \r
595                 curReg = 0xB0 | vgmPb->oplChnMask;\r
596                 if (vgmPb->oplRegCache[0x0C] & 0x20)\r
597                 {\r
598                         vgmPb->oplRegCache[0x0C] &= ~0x20;\r
599                         OPL2_Write(curReg, vgmPb->oplRegCache[0x0C]);   // send Key Off\r
600                 }\r
601 \r
602                 vgmPb->vgmEnd = 0x01;\r
603 \r
604                 if (! vgmPbMusic.vgmEnd)        // if (music is playing)\r
605                 {\r
606                         opMask = CHN_OPMASK[vgmPb->oplChnMask];\r
607                         for (regID = 0x00; regID < 0x0A; regID ++)\r
608                         {\r
609                                 curReg = SFX_REGS[regID] + opMask;\r
610                                 OPL2_Write(curReg, oplRegs_Music[curReg]);      // restore Music register\r
611                         }\r
612                         for (; regID < 0x0D; regID ++)\r
613                         {\r
614                                 curReg = SFX_REGS[regID] | vgmPb->oplChnMask;\r
615                                 OPL2_Write(curReg, oplRegs_Music[curReg]);      // restore Music register\r
616                         }\r
617 \r
618                         vgmPbMusic.oplChnMask &= ~(1 << vgmPb->oplChnMask);\r
619                 }\r
620         }\r
621 \r
622         return;\r
623 }\r
624 \r
625 \r
626 \r
627 static void OPL_CachedWrite(VGM_PBK* vgmPb, UINT8 reg, UINT8 data)\r
628 {\r
629         UINT8 regChn;\r
630         UINT8 ramOfs;\r
631 \r
632         if (vgmPb->pbMode == PBMODE_MUSIC)\r
633         {\r
634                 if (reg == 0x01)\r
635                         data |= 0x20;   // enforce "Waveform Select Enable" bit\r
636                 vgmPb->oplRegCache[reg] = data;\r
637 \r
638                 ramOfs = SFX_REGS_REV[reg >> 4];\r
639                 if (ramOfs < 0x0A)      // Operator 20/40/60/80/E0\r
640                 {\r
641                         regChn = CHN_OPMASK_REV[reg & 0x1F] & 0x7F;\r
642                         if (vgmPb->oplChnMask & (1 << regChn))\r
643                                 return; // channel overridden by SFX - return\r
644                 }\r
645                 else if (ramOfs < 0x0D) // Operator C0/A0/B0\r
646                 {\r
647                         regChn = reg & 0x0F;\r
648                         if (vgmPb->oplChnMask & (1 << regChn))\r
649                                 return; // channel overridden by SFX - return\r
650                 }\r
651         }\r
652         else //if (vgmPb->pbMode == PBMODE_SFX)\r
653         {\r
654                 if (reg == 0xBD)\r
655                         return; // no rhythm register for SFX\r
656 \r
657                 ramOfs = SFX_REGS_REV[reg >> 4];\r
658                 if (ramOfs == 0xFF)\r
659                         return;\r
660 \r
661                 if (ramOfs < 0x0A)      // Operator 20/40/60/80/E0\r
662                 {\r
663                         regChn = CHN_OPMASK_REV[reg & 0x1F];\r
664                         if (regChn == 0xFF)\r
665                                 return; // ignore writes to invalid channels/operators\r
666                         ramOfs += (regChn & 0x80) >> 7;\r
667                         regChn &= 0x7F;\r
668                         vgmPb->oplRegCache[ramOfs] = data;\r
669 \r
670                         if (regChn != vgmPb->oplChnMask)\r
671                         {\r
672                                 // force command to current channel\r
673                                 reg = SFX_REGS[ramOfs] + CHN_OPMASK[vgmPb->oplChnMask];\r
674                         }\r
675                 }\r
676                 else    // Operator C0/A0/B0\r
677                 {\r
678                         regChn = CHN_OPMASK_REV[reg & 0x0F];\r
679                         if (regChn >= 0x09)\r
680                                 return; // ignore writes to invalid channels\r
681                         vgmPb->oplRegCache[ramOfs] = data;\r
682 \r
683                         reg = (reg & 0xF0) | vgmPb->oplChnMask;\r
684                 }\r
685         }\r
686 \r
687         OPL2_Write(reg, data);\r
688         return;\r
689 }\r
690 \r
691 \r
692 static void ym2413_write(VGM_PBK* vgmPb, UINT8 reg, UINT8 data)\r
693 {\r
694         return; // unsupported for now\r
695 }\r
696 \r
697 static void ym3812_write(VGM_PBK* vgmPb, UINT8 reg, UINT8 data)\r
698 {\r
699         if (reg == 0x01)\r
700         {\r
701                 vgmPb->workRAM[0x00] = data & 0x20;     // store "Wave Select Enable" bit\r
702         }\r
703         else if ((reg & 0xE0) == 0xE0)\r
704         {\r
705                 if (! vgmPb->workRAM[0x00])     // "Wave Select Enable" off?\r
706                         data = 0x00;    // disable waveforms\r
707         }\r
708 \r
709         OPL_CachedWrite(vgmPb, reg, data);\r
710         return;\r
711 }\r
712 \r
713 static void ym3512_write(VGM_PBK* vgmPb, UINT8 reg, UINT8 data)\r
714 {\r
715         if ((reg & 0xE0) == 0xE0)\r
716         {\r
717                 data = 0x00;    // OPL1 has no waveforms\r
718         }\r
719         if (reg >= 0x07 && reg < 0x20)\r
720         {\r
721                 if (reg == 0x08)\r
722                         data &= ~0x0F;  // mask out Y8950 DeltaT data\r
723                 else\r
724                         return; // ignore Y8950 DeltaT writes\r
725         }\r
726 \r
727         OPL_CachedWrite(vgmPb, reg, data);\r
728         return;\r
729 }\r
730 \r
731 static void ymf262_write(VGM_PBK* vgmPb, UINT8 port, UINT8 reg, UINT8 data)\r
732 {\r
733         return; // unsupported for now\r
734 }\r
735 \r
736 \r
737 \r
738 void UpdateSoundEngine(void)\r
739 {\r
740         UINT8 tmrMask;\r
741         UINT8 curSFX;\r
742 \r
743         tmrMask = OPL2_ReadStatus();\r
744         if (! (tmrMask & 0x40))\r
745                 return; // wait for overflow\r
746         OPL2_Write(0x04, 0x80); // Reset Timer/IRQ Status Flags\r
747 \r
748         if (! vgmPbMusic.vgmEnd)\r
749                 UpdateVGM(&vgmPbMusic, VGM_UPD_RATE);\r
750         for (curSFX = 0; curSFX < SFX_CHN_COUNT; curSFX ++)\r
751         {\r
752                 if (! vgmPbSFX[curSFX].vgmEnd)\r
753                         UpdateVGM(&vgmPbSFX[curSFX], VGM_UPD_RATE);\r
754         }\r
755 \r
756         return;\r
757 }\r