Debian Codegraph @

src/hardware/adlib.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2002-2010 The DOSBox Team
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19 /* $Id: adlib.cpp,v 1.42 2009-11-03 20:17:42 qbix79 Exp $ */
20
21 #include <stdlib.h>
22 #include <string.h>
23 #include <math.h>
24 #include <sys/types.h>
25 #include "adlib.h"
26
27 #include "setup.h"
28 #include "mapper.h"
29 #include "mem.h"
30 #include "dbopl.h"
31
32 namespace OPL2 {
33 #include "opl.cpp"
34
35 struct Handler : public Adlib::Handler {
36 virtual void WriteReg( Bit32u reg, Bit8u val ) {
37 adlib_write(reg,val);
38 }
39 virtual Bit32u WriteAddr( Bit32u port, Bit8u val ) {
40 return val;
41 }
42
43 virtual void Generate( MixerChannel* chan, Bitu samples ) {
44 Bit16s buf[1024];
45 while( samples > 0 ) {
46 Bitu todo = samples > 1024 ? 1024 : samples;
47 samples -= todo;
48 adlib_getsample(buf, todo);
49 chan->AddSamples_m16( todo, buf );
50 }
51 }
52 virtual void Init( Bitu rate ) {
53 adlib_init(rate);
54 }
55 ~Handler() {
56 }
57 };
58 }
59
60 namespace OPL3 {
61 #define OPLTYPE_IS_OPL3
62 #include "opl.cpp"
63
64 struct Handler : public Adlib::Handler {
65 virtual void WriteReg( Bit32u reg, Bit8u val ) {
66 adlib_write(reg,val);
67 }
68 virtual Bit32u WriteAddr( Bit32u port, Bit8u val ) {
69 adlib_write_index(port, val);
70 return opl_index;
71 }
72 virtual void Generate( MixerChannel* chan, Bitu samples ) {
73 Bit16s buf[1024*2];
74 while( samples > 0 ) {
75 Bitu todo = samples > 1024 ? 1024 : samples;
76 samples -= todo;
77 adlib_getsample(buf, todo);
78 chan->AddSamples_s16( todo, buf );
79 }
80 }
81 virtual void Init( Bitu rate ) {
82 adlib_init(rate);
83 }
84 ~Handler() {
85 }
86 };
87 }
88
89 #define RAW_SIZE 1024
90
91
92 /*
93 Main Adlib implementation
94
95 */
96
97 namespace Adlib {
98
99
100 /* Raw DRO capture stuff */
101
102 #ifdef _MSC_VER
103 #pragma pack (1)
104 #endif
105
106 #define HW_OPL2 0
107 #define HW_DUALOPL2 1
108 #define HW_OPL3 2
109
110 struct RawHeader {
111 Bit8u id[8]; /* 0x00, "DBRAWOPL" */
112 Bit16u versionHigh; /* 0x08, size of the data following the m */
113 Bit16u versionLow; /* 0x0a, size of the data following the m */
114 Bit32u commands; /* 0x0c, Bit32u amount of command/data pairs */
115 Bit32u milliseconds; /* 0x10, Bit32u Total milliseconds of data in this chunk */
116 Bit8u hardware; /* 0x14, Bit8u Hardware Type 0=opl2,1=dual-opl2,2=opl3 */
117 Bit8u format; /* 0x15, Bit8u Format 0=cmd/data interleaved, 1 maybe all cdms, followed by all data */
118 Bit8u compression; /* 0x16, Bit8u Compression Type, 0 = No Compression */
119 Bit8u delay256; /* 0x17, Bit8u Delay 1-256 msec command */
120 Bit8u delayShift8; /* 0x18, Bit8u (delay + 1)*256 */
121 Bit8u conversionTableSize; /* 0x191, Bit8u Raw Conversion Table size */
122 } GCC_ATTRIBUTE(packed);
123 #ifdef _MSC_VER
124 #pragma pack()
125 #endif
126 /*
127 The Raw Tables is < 128 and is used to convert raw commands into a full register index
128 When the high bit of a raw command is set it indicates the cmd/data pair is to be sent to the 2nd port
129 After the conversion table the raw data follows immediatly till the end of the chunk
130 */
131
132 //Table to map the opl register to one <127 for dro saving
133 class Capture {
134 //127 entries to go from raw data to registers
135 Bit8u ToReg[127];
136 //How many entries in the ToPort are used
137 Bit8u RawUsed;
138 //256 entries to go from port index to raw data
139 Bit8u ToRaw[256];
140 Bit8u delay256;
141 Bit8u delayShift8;
142 RawHeader header;
143
144 FILE* handle; //File used for writing
145 Bit32u startTicks; //Start used to check total raw length on end
146 Bit32u lastTicks; //Last ticks when last last cmd was added
147 Bit8u buf[1024]; //16 added for delay commands and what not
148 Bit32u bufUsed;
149 Bit8u cmd[2]; //Last cmd's sent to either ports
150 bool doneOpl3;
151 bool doneDualOpl2;
152
153 RegisterCache* cache;
154
155 void MakeEntry( Bit8u reg, Bit8u& raw ) {
156 ToReg[ raw ] = reg;
157 ToRaw[ reg ] = raw;
158 raw++;
159 }
160 void MakeTables( void ) {
161 Bit8u index = 0;
162 memset( ToReg, 0xff, sizeof ( ToReg ) );
163 memset( ToRaw, 0xff, sizeof ( ToRaw ) );
164 //Select the entries that are valid and the index is the mapping to the index entry
165 MakeEntry( 0x01, index ); //0x01: Waveform select
166 MakeEntry( 0x04, index ); //104: Four-Operator Enable
167 MakeEntry( 0x05, index ); //105: OPL3 Mode Enable
168 MakeEntry( 0x08, index ); //08: CSW / NOTE-SEL
169 MakeEntry( 0xbd, index ); //BD: Tremolo Depth / Vibrato Depth / Percussion Mode / BD/SD/TT/CY/HH On
170 //Add the 32 byte range that hold the 18 operators
171 for ( int i = 0 ; i < 24; i++ ) {
172 if ( (i & 7) < 6 ) {
173 MakeEntry(0x20 + i, index ); //20-35: Tremolo / Vibrato / Sustain / KSR / Frequency Multiplication Facto
174 MakeEntry(0x40 + i, index ); //40-55: Key Scale Level / Output Level
175 MakeEntry(0x60 + i, index ); //60-75: Attack Rate / Decay Rate
176 MakeEntry(0x80 + i, index ); //80-95: Sustain Level / Release Rate
177 MakeEntry(0xe0 + i, index ); //E0-F5: Waveform Select
178 }
179 }
180 //Add the 9 byte range that hold the 9 channels
181 for ( int i = 0 ; i < 9; i++ ) {
182 MakeEntry(0xa0 + i, index ); //A0-A8: Frequency Number
183 MakeEntry(0xb0 + i, index ); //B0-B8: Key On / Block Number / F-Number(hi bits)
184 MakeEntry(0xc0 + i, index ); //C0-C8: FeedBack Modulation Factor / Synthesis Type
185 }
186 //Store the amount of bytes the table contains
187 RawUsed = index;
188 // assert( RawUsed <= 127 );
189 delay256 = RawUsed;
190 delayShift8 = RawUsed+1;
191 }
192
193 void ClearBuf( void ) {
194 fwrite( buf, 1, bufUsed, handle );
195 header.commands += bufUsed / 2;
196 bufUsed = 0;
197 }
198 void AddBuf( Bit8u raw, Bit8u val ) {
199 buf[bufUsed++] = raw;
200 buf[bufUsed++] = val;
201 if ( bufUsed >= sizeof( buf ) ) {
202 ClearBuf();
203 }
204 }
205 void AddWrite( Bit32u regFull, Bit8u val ) {
206 Bit8u regMask = regFull & 0xff;
207 /*
208 Do some special checks if we're doing opl3 or dualopl2 commands
209 Although you could pretty much just stick to always doing opl3 on the player side
210 */
211 //Enabling opl3 4op modes will make us go into opl3 mode
212 if ( header.hardware != HW_OPL3 && regFull == 0x104 && val && (*cache)[0x105] ) {
213 header.hardware = HW_OPL3;
214 }
215 //Writing a keyon to a 2nd address enables dual opl2 otherwise
216 //Maybe also check for rhythm
217 if ( header.hardware == HW_OPL2 && regFull >= 0x1b0 && regFull <=0x1b8 && val ) {
218 header.hardware = HW_DUALOPL2;
219 }
220 Bit8u raw = ToRaw[ regMask ];
221 if ( raw == 0xff )
222 return;
223 if ( regFull & 0x100 )
224 raw |= 128;
225 AddBuf( raw, val );
226 }
227 void WriteCache( void ) {
228 Bitu i, val;
229 /* Check the registers to add */
230 for (i=0;i<256;i++) {
231 //Skip the note on entries
232 if (i>=0xb0 && i<=0xb8)
233 continue;
234 val = (*cache)[ i ];
235 if (val) {
236 AddWrite( i, val );
237 }
238 val = (*cache)[ 0x100 + i ];
239 if (val) {
240 AddWrite( 0x100 + i, val );
241 }
242 }
243 }
244 void InitHeader( void ) {
245 memset( &header, 0, sizeof( header ) );
246 memcpy( header.id, "DBRAWOPL", 8 );
247 header.versionLow = 0;
248 header.versionHigh = 2;
249 header.delay256 = delay256;
250 header.delayShift8 = delayShift8;
251 header.conversionTableSize = RawUsed;
252 }
253 void CloseFile( void ) {
254 if ( handle ) {
255 ClearBuf();
256 /* Endianize the header and write it to beginning of the file */
257 var_write( &header.versionHigh, header.versionHigh );
258 var_write( &header.versionLow, header.versionLow );
259 var_write( &header.commands, header.commands );
260 var_write( &header.milliseconds, header.milliseconds );
261 fseek( handle, 0, SEEK_SET );
262 fwrite( &header, 1, sizeof( header ), handle );
263 fclose( handle );
264 handle = 0;
265 }
266 }
267 public:
268 bool DoWrite( Bit32u regFull, Bit8u val ) {
269 Bit8u regMask = regFull & 0xff;
270 //Check the raw index for this register if we actually have to save it
271 if ( handle ) {
272 /*
273 Check if we actually care for this to be logged, else just ignore it
274 */
275 Bit8u raw = ToRaw[ regMask ];
276 if ( raw == 0xff ) {
277 return true;
278 }
279 /* Check if this command will not just replace the same value
280 in a reg that doesn't do anything with it
281 */
282 if ( (*cache)[ regFull ] == val )
283 return true;
284 /* Check how much time has passed */
285 Bitu passed = PIC_Ticks - lastTicks;
286 lastTicks = PIC_Ticks;
287 header.milliseconds += passed;
288
289 //if ( passed > 0 ) LOG_MSG( "Delay %d", passed ) ;
290
291 // If we passed more than 30 seconds since the last command, we'll restart the the capture
292 if ( passed > 30000 ) {
293 CloseFile();
294 goto skipWrite;
295 }
296 while (passed > 0) {
297 if (passed < 257) { //1-256 millisecond delay
298 AddBuf( delay256, passed - 1 );
299 passed = 0;
300 } else {
301 Bitu shift = (passed >> 8);
302 passed -= shift << 8;
303 AddBuf( delayShift8, shift - 1 );
304 }
305 }
306 AddWrite( regFull, val );
307 return true;
308 }
309 skipWrite:
310 //Not yet capturing to a file here
311 //Check for commands that would start capturing, if it's not one of them return
312 if ( !(
313 //note on in any channel
314 ( regMask>=0xb0 && regMask<=0xb8 && (val&0x020) ) ||
315 //Percussion mode enabled and a note on in any percussion instrument
316 ( regMask == 0xbd && ( (val&0x3f) > 0x20 ) )
317 )) {
318 return true;
319 }
320 handle = OpenCaptureFile("Raw Opl",".dro");
321 if (!handle)
322 return false;
323 InitHeader();
324 //Prepare space at start of the file for the header
325 fwrite( &header, 1, sizeof(header), handle );
326 /* write the Raw To Reg table */
327 fwrite( &ToReg, 1, RawUsed, handle );
328 /* Write the cache of last commands */
329 WriteCache( );
330 /* Write the command that triggered this */
331 AddWrite( regFull, val );
332 //Init the timing information for the next commands
333 lastTicks = PIC_Ticks;
334 startTicks = PIC_Ticks;
335 return true;
336 }
337 Capture( RegisterCache* _cache ) {
338 cache = _cache;
339 handle = 0;
340 bufUsed = 0;
341 MakeTables();
342 }
343 ~Capture() {
344 CloseFile();
345 }
346
347 };
348
349 /*
350 Chip
351 */
352
353 bool Chip::Write( Bit32u reg, Bit8u val ) {
354 switch ( reg ) {
355 case 0x02:
356 timer[0].counter = val;
357 return true;
358 case 0x03:
359 timer[1].counter = val;
360 return true;
361 case 0x04:
362 double time;
363 time = PIC_FullIndex();
364 if ( val & 0x80 ) {
365 timer[0].Reset( time );
366 timer[1].Reset( time );
367 } else {
368 timer[0].Update( time );
369 timer[1].Update( time );
370 if ( val & 0x1 ) {
371 timer[0].Start( time, 80 );
372 } else {
373 timer[0].Stop( );
374 }
375 timer[0].masked = (val & 0x40) > 0;
376 if ( timer[0].masked )
377 timer[0].overflow = false;
378 if ( val & 0x2 ) {
379 timer[1].Start( time, 320 );
380 } else {
381 timer[1].Stop( );
382 }
383 timer[1].masked = (val & 0x20) > 0;
384 if ( timer[1].masked )
385 timer[1].overflow = false;
386
387 }
388 return true;
389 }
390 return false;
391 }
392
393
394 Bit8u Chip::Read( ) {
395 double time( PIC_FullIndex() );
396 timer[0].Update( time );
397 timer[1].Update( time );
398 Bit8u ret = 0;
399 //Overflow won't be set if a channel is masked
400 if ( timer[0].overflow ) {
401 ret |= 0x40;
402 ret |= 0x80;
403 }
404 if ( timer[1].overflow ) {
405 ret |= 0x20;
406 ret |= 0x80;
407 }
408 return ret;
409
410 }
411
412 void Module::CacheWrite( Bit32u reg, Bit8u val ) {
413 //capturing?
414 if ( capture ) {
415 capture->DoWrite( reg, val );
416 }
417 //Store it into the cache
418 cache[ reg ] = val;
419 }
420
421 void Module::DualWrite( Bit8u index, Bit8u reg, Bit8u val ) {
422 //Make sure you don't use opl3 features
423 //Don't allow write to disable opl3
424 if ( reg == 5 ) {
425 return;
426 }
427 //Only allow 4 waveforms
428 if ( reg >= 0xE0 ) {
429 val &= 3;
430 }
431 //Write to the timer?
432 if ( chip[index].Write( reg, val ) )
433 return;
434 //Enabling panning
435 if ( reg >= 0xc0 && reg <=0xc8 ) {
436 val &= 0x0f;
437 val |= index ? 0xA0 : 0x50;
438 }
439 Bit32u fullReg = reg + (index ? 0x100 : 0);
440 handler->WriteReg( fullReg, val );
441 CacheWrite( fullReg, val );
442 }
443
444
445 void Module::PortWrite( Bitu port, Bitu val, Bitu iolen ) {
446 //Keep track of last write time
447 lastUsed = PIC_Ticks;
448 //Maybe only enable with a keyon?
449 if ( !mixerChan->enabled ) {
450 mixerChan->Enable(true);
451 }
452 if ( port&1 ) {
453 switch ( mode ) {
454 case MODE_OPL2:
455 case MODE_OPL3:
456 if ( !chip[0].Write( reg.normal, val ) ) {
457 handler->WriteReg( reg.normal, val );
458 CacheWrite( reg.normal, val );
459 }
460 break;
461 case MODE_DUALOPL2:
462 //Not a 0x??8 port, then write to a specific port
463 if ( !(port & 0x8) ) {
464 Bit8u index = ( port & 2 ) >> 1;
465 DualWrite( index, reg.dual[index], val );
466 } else {
467 //Write to both ports
468 DualWrite( 0, reg.dual[0], val );
469 DualWrite( 1, reg.dual[1], val );
470 }
471 break;
472 }
473 } else {
474 //Ask the handler to write the address
475 //Make sure to clip them in the right range
476 switch ( mode ) {
477 case MODE_OPL2:
478 reg.normal = handler->WriteAddr( port, val ) & 0xff;
479 break;
480 case MODE_OPL3:
481 reg.normal = handler->WriteAddr( port, val ) & 0x1ff;
482 break;
483 case MODE_DUALOPL2:
484 //Not a 0x?88 port, when write to a specific side
485 if ( !(port & 0x8) ) {
486 Bit8u index = ( port & 2 ) >> 1;
487 reg.dual[index] = val & 0xff;
488 } else {
489 reg.dual[0] = val & 0xff;
490 reg.dual[1] = val & 0xff;
491 }
492 break;
493 }
494 }
495 }
496
497
498 Bitu Module::PortRead( Bitu port, Bitu iolen ) {
499 switch ( mode ) {
500 case MODE_OPL2:
501 //We allocated 4 ports, so just return -1 for the higher ones
502 if ( !(port & 3 ) ) {
503 //Make sure the low bits are 6 on opl2
504 return chip[0].Read() | 0x6;
505 } else {
506 return 0xff;
507 }
508 case MODE_OPL3:
509 //We allocated 4 ports, so just return -1 for the higher ones
510 if ( !(port & 3 ) ) {
511 return chip[0].Read();
512 } else {
513 return 0xff;
514 }
515 case MODE_DUALOPL2:
516 //Only return for the lower ports
517 if ( port & 1 ) {
518 return 0xff;
519 }
520 //Make sure the low bits are 6 on opl2
521 return chip[ (port >> 1) & 1].Read() | 0x6;
522 }
523 return 0;
524 }
525
526
527 void Module::Init( Mode m ) {
528 mode = m;
529 switch ( mode ) {
530 case MODE_OPL3:
531 case MODE_OPL2:
532 break;
533 case MODE_DUALOPL2:
534 //Setup opl3 mode in the hander
535 handler->WriteReg( 0x105, 1 );
536 //Also set it up in the cache so the capturing will start opl3
537 CacheWrite( 0x105, 1 );
538 break;
539 }
540 }
541
542 }; //namespace
543
544
545
546 static Adlib::Module* module = 0;
547
548 static void OPL_CallBack(Bitu len) {
549 module->handler->Generate( module->mixerChan, len );
550 //Disable the sound generation after 30 seconds of silence
551 if ((PIC_Ticks - module->lastUsed) > 30000) {
552 Bitu i;
553 for (i=0xb0;i<0xb9;i++) if (module->cache[i]&0x20||module->cache[i+0x100]&0x20) break;
554 if (i==0xb9) module->mixerChan->Enable(false);
555 else module->lastUsed = PIC_Ticks;
556 }
557 }
558
559 static Bitu OPL_Read(Bitu port,Bitu iolen) {
560 return module->PortRead( port, iolen );
561 }
562
563 void OPL_Write(Bitu port,Bitu val,Bitu iolen) {
564 module->PortWrite( port, val, iolen );
565 }
566
567 /*
568 Save the current state of the operators as instruments in an reality adlib tracker file
569 */
570 static void SaveRad() {
571 char b[16 * 1024];
572 int w = 0;
573
574 FILE* handle = OpenCaptureFile("RAD Capture",".rad");
575 if ( !handle )
576 return;
577 //Header
578 fwrite( "RAD by REALiTY!!", 1, 16, handle );
579 b[w++] = 0x10; //version
580 b[w++] = 0x06; //default speed and no description
581 //Write 18 instuments for all operators in the cache
582 for ( int i = 0; i < 18; i++ ) {
583 Bit8u* set = module->cache + ( i / 9 ) * 256;
584 Bitu offset = ((i % 9) / 3) * 8 + (i % 3);
585 Bit8u* base = set + offset;
586 b[w++] = 1 + i; //instrument number
587 b[w++] = base[0x23];
588 b[w++] = base[0x20];
589 b[w++] = base[0x43];
590 b[w++] = base[0x40];
591 b[w++] = base[0x63];
592 b[w++] = base[0x60];
593 b[w++] = base[0x83];
594 b[w++] = base[0x80];
595 b[w++] = set[0xc0 + (i % 9)];
596 b[w++] = base[0xe3];
597 b[w++] = base[0xe0];
598 }
599 b[w++] = 0; //instrument 0, no more instruments following
600 b[w++] = 1; //1 pattern following
601 //Zero out the remaing part of the file a bit to make rad happy
602 for ( int i = 0; i < 64; i++ ) {
603 b[w++] = 0;
604 }
605 fwrite( b, 1, w, handle );
606 fclose( handle );
607 };
608
609
610 static void OPL_SaveRawEvent(bool pressed) {
611 if (!pressed)
612 return;
613 // SaveRad();return;
614 /* Check for previously opened wave file */
615 if ( module->capture ) {
616 delete module->capture;
617 module->capture = 0;
618 LOG_MSG("Stopped Raw OPL capturing.");
619 } else {
620 LOG_MSG("Preparing to capture Raw OPL, will start with first note played.");
621 module->capture = new Adlib::Capture( &module->cache );
622 }
623 }
624
625 namespace Adlib {
626
627 Module::Module( Section* configuration ) : Module_base(configuration) {
628 reg.dual[0] = 0;
629 reg.dual[1] = 0;
630 reg.normal = 0;
631 handler = 0;
632 capture = 0;
633
634 Section_prop * section=static_cast<Section_prop *>(configuration);
635 Bitu base = section->Get_hex("sbbase");
636 Bitu rate = section->Get_int("oplrate");
637 //Make sure we can't select lower than 8000 to prevent fixed point issues
638 if ( rate < 8000 )
639 rate = 8000;
640 std::string oplemu( section->Get_string( "oplemu" ) );
641
642 mixerChan = mixerObject.Install(OPL_CallBack,rate,"FM");
643 mixerChan->SetScale( 2.0 );
644 if (oplemu == "fast") {
645 handler = new DBOPL::Handler();
646 } else if (oplemu == "compat") {
647 if ( oplmode == OPL_opl2 ) {
648 handler = new OPL2::Handler();
649 } else {
650 handler = new OPL3::Handler();
651 }
652 } else {
653 handler = new DBOPL::Handler();
654 }
655 handler->Init( rate );
656 bool single = false;
657 switch ( oplmode ) {
658 case OPL_opl2:
659 single = true;
660 Init( Adlib::MODE_OPL2 );
661 break;
662 case OPL_dualopl2:
663 Init( Adlib::MODE_DUALOPL2 );
664 break;
665 case OPL_opl3:
666 Init( Adlib::MODE_OPL3 );
667 break;
668 }
669 //0x388 range
670 WriteHandler[0].Install(0x388,OPL_Write,IO_MB, 4 );
671 ReadHandler[0].Install(0x388,OPL_Read,IO_MB, 4 );
672 //0x220 range
673 if ( !single ) {
674 WriteHandler[1].Install(base,OPL_Write,IO_MB, 4 );
675 ReadHandler[1].Install(base,OPL_Read,IO_MB, 4 );
676 }
677 //0x228 range
678 WriteHandler[2].Install(base+8,OPL_Write,IO_MB, 2);
679 ReadHandler[2].Install(base+8,OPL_Read,IO_MB, 1);
680
681 MAPPER_AddHandler(OPL_SaveRawEvent,MK_f7,MMOD1|MMOD2,"caprawopl","Cap OPL");
682 }
683
684 Module::~Module() {
685 if ( capture ) {
686 delete capture;
687 }
688 if ( handler ) {
689 delete handler;
690 }
691 }
692
693 //Initialize static members
694 OPL_Mode Module::oplmode=OPL_none;
695
696 }; //Adlib Namespace
697
698
699 void OPL_Init(Section* sec,OPL_Mode oplmode) {
700 Adlib::Module::oplmode = oplmode;
701 module = new Adlib::Module( sec );
702 }
703
704 void OPL_ShutDown(Section* sec){
705 delete module;
706 module = 0;
707
708 }