MSX-Audio BIOS v1.3b
--------------------

THIS IS A HOBBYIST PROJECT, AND NEITHER ASCII OR MSX ASSOCIATION WERE INVOLVED.
USE THIS SOFTWARE SOLELY AT YOUR OWN RISK. NEITHER ASCII, THE MSX ASSOCIATION
NOR MYSELF ANSWER FOR ANY TYPE OF DAMAGE MADE ON YOUR COMPUTER OR YOUR DATA BY
USING THIS SOFTWARE.


==== A bit of History ====

The MSX-Audio was the original FM extension of the MSX standard. It was
created in late 1984 and meant to be used as part of the MSX2 standard.

It was composed of the Y8950 soundchip (aka OPL1+ADPCM) and the MSX-Audio BIOS.


It's an awesome extension that complements the MSX-BIOS, and provides solutions
for many limitations the MSX standard had until then, like:

- Provides two new timers, independent of the VDP interrupt generation. This
  means that i.e. the music speed can remain constant independently of the
  framerate used on VDP.
- Supports 9 channels of FM sound
- Supports both PCM and ADPCM for playing samples. The samples can be stored
  either on SampleRAM (up to 256KB), SampleROM (up to 256KB), AudioROM, and on
  the MSX VRAM (16KB, 64KB or 128KB supported). All those memory sizes are
  detected on boot.

But unfortunately, history wasn't too kind to this nice standard:

1) The release seems to had been rushed. The BIOS, albeit very well structured,
   was shipped with many bugs
2) Of the three hardware makers that created Y8950-base cartridges (Panasonic,
   Toshiba and Philips), only Panasonic did implement the full standard.
   Philips and Toshiba "forgot" to put the BIOS on their respective Music
   Modules. Toshiba even "forgot" to put some SampleRAM, but the BIOS could had
   worked fine without SampleRAM too.
3) In 1987, Panasonic decided to fork the standard on its own, took the BIOS
   and ported it to work with the then-incompatible YM2413 (aka OPLL) soundchip.
   At least they fixed nearly all the bugs on this version of the BIOS, but
   OTOH it lost a lot of features (all PCM/ADPCM, timers, MIDI and
   music-keyboard features were ripped off). A de-facto standard was created.
4) In 1988, the three MSX2+ makers decided to include the MSX-Music built-in
   on their machines. A lot of software houses decided to adopt the standard
   and leave the MSX-Audio behind.
5) ASCII, probably pressed by both the hardware and software makers, adopted
   the MSX-Music as the standard for the MSX Turbo-R machines, burying the
   MSX-Audio for good.

Being a de-facto standard, the MSX-Music has its problems:

The detection algorithm was only consolidated years later. Older games look for
the PAC2OPLL signature at 4010h, thus they don't detect the internal MSX-Music.
Other games look for either PAC2OPLL or APRLOPLL signatures at 4010h, meaning
that other external MSX-Musics with diferent signatures will not be detected.
The ASCII's oficial algorithm looks for the OPLL signature at 401Ch, and this
is the correct way of detecting it.


==== This Enhanced MSX-Audio BIOS ====

This is an enhancement over the MSX-Audio BIOS, with a lot new features and
fixes, listed below:


v1.3b:
- Small optimizations on RCA_VOICE
- Added an extension function table to the MSX-Music FM-BIOS jump table, at
  4128h. This makes life a lot easier to use the PSG emulation for MSX-Music
  applications. It's structured like this:
  4128h:	db	entries		; Number of function pointer entries on
					; this table
		dw	PSGemuaddr	; Address of the PSGemu routine on
					; this slot (frame-1 or 2)
- Fixed a bug in INIOPL that caused the machine to freeze if the game interrupt 
  handler was located on frame-2
- Fixed a bug that caused the ADPCM playback to be muted on the NMS-1205

v1.3:
- Lots of bugfixes
- Lots of optimizations
- Enhanced Instrument set, from MSX-Music BIOS
- Support for OPL3 soundchips (and OPL4fm, off course).
  - Support for stereo sound
  - The new WaveSelect parameter of OPL2/3/4 soundchips (register E0h) is now
    supported on instruments, on its parameter +21 (1st operator) and +29 (2nd
    operator), allowing enhanced custom instruments
- Brand new SoundChipFish extension. This is a soundchip translation library
  that supports on-the-fly translation of commands between the following
  soundchips:
  - OPLL->OPLn (IOTW, YM2413 to YM3527/Y8950/YM3812/YMF262/YMF278)
  - AY-3-8910 to OPL3 (OPL4fm included, off course)
- Software compatibility with the MSX-Music BIOS. Yes, you read it right: all
  games/tools that use the MSX-Music BIOS INIOPL/WRTOPL functions will work
  on this BIOS, as the BIOS will use the built-in SoundChipFish library to
  translate OPLL commands to OPLx commands on-the-fly!
  This means the following MSX-Music games will work just fine on any device
  containing the MSX-Audio BIOS v1.3, and will even be played in stereo on
  OPL3/4 cartridges. Check the list of compatible games ahead on this readme.
- This new version is bigger now requires a 64KB ROM. The Philips Music-Module
  upgrade board already supports this. It's just a matter of installing a
  27C512 EPROM and jumping the board accordingly. For the Toshiba Music-Module,
  a small mod of the upgrade will be required. The upgrade-guide was updated
  to reflect this, just check it. 
- Because of the bigger ROM requirement, the Panasonic FS-CA1 isn't supported
  anymore. If anyone ever releases a guide on how to upgrade this cartridge to
  support this version of the BIOS, then it may be supported again on future
  versions (only if openMSX emulate the upgraded FS-CA1).
- The dual-OPL support was removed, because it was slow, buggy and didn't add
  new FM channels to be used. But many of the routines were fixed, allowing
  real dual-OPL 18 channel support on future.
- OPL2 (YM3812) is now supported as well. But no binary was released because
  there's no real cartridge or emulation that contains this soundchip currently.
  (and for home-built cartridges it's much better to use an OPL3 anyway).
- New soundchip detection routines that follows strictly the Yamaha's algorithm.
  It now properly detects and supports any of the OPL soundchips, on the
  following I/O ports configurations:
  - I/O ports C0h-C1h or C2h-C3h for OPL1/OPL2 soundchips
  - I/O ports C0h-C3h or C4h-C7h for OPL3/OPL4 soundchips
- MSX-Music style transpose/temper tables are now supported. When a CALL MUSIC
  is issued, the MSX-Audio will now lower the RAM top to allocate RAM for the
  necessary structures to mimic the MSX-Music in-RAM structures, giving greater
  compatibility to the BASIC FM musics that make use of POKEs. 
- Other MSX-Music extensions connected to the same machine will be disabled
  once this BIOS receives a CALL AUDIO or a CALL MUSIC command, providing a way
  to solve the conflict on machines with an internal MSX-Music on a lower slot
  (like Panasonic MSX2+/TR
  and CIEL Expert-Turbo)
- Fixed a bug that reported an the rhythm channel incorrectly as an ADPCM
  channel
- Moved many initialization routines to the frame-2, to release space for the
  new routines on frame-1.
- 0066h will now call HNMI, to allow real-hardware debugging using the NMI.
- Turbo Z80 machines without a system-timer are now fully supported, up to
  7MHz.

v1.2:
- Many optimizations/bugfixes
- Support for the E6h system-timer (aka Turbo-R timer). Now you'll be able to use the MSX-Audio in R800 mode, just like the MSX-Music
- Support for the NMS-1205 and FS-CA1 proprietary mute-switches, and
  for the standard SP-OFF mute-switch used on the HX-MU900.
- A more elegant work-around than Panasonic did to "fix" their mistake on the
  MusicKeyboard-port pinout of the FS-CA1
- NMS-1160 Music Keyboard support
- Music Keyboard auto-detection between the HX-MU901 (49 keys) and NMS-1160
  (61 keys). The auto-detection routine will discover the MusicKeyboard type
  and change the keymap on-the-fly accordingly. To use it:
  - On a 49 keys keyboard: just press the last key.
  - On a 61 keys keyboard: just slide your finger on at least 5 consecutive
    white keys in any direction.
- Preliminary OPL3 support. All MusicKeyboard and ADPCM related commands were
  block, just like the MSX-Music.
- It now supports the CALL MUSIC command, so it isn't necessary to replace
  CALL MUSIC by CALL AUDIO on BASIC musics anymore.

==== Known Issues ====
- ADPCM recording requires the mute switch to work. This still have to be fixed
  on the NMS-1205, and will require a mod to work on the HX-MU901
- The HX-MU901 will also require a mod to fix its ADPCM LPF filter. It works
  fine without this mod, so don't worry.
- The MSX-Music Keyboard keymatrix is incorrect and needs a rollback to the
  v1.0 code

==== Special Thanks ====

- Zeilemaker, for the impressive msxsyssrc project, for disassembling and
  commenting the original MSX-Audio BIOS
- Wouter, for creating the Boosted_audio extension for openMSX, and for working
  so hard on the emulator
- openMSX team, for their excellent emulator/debugger
- Dioniso, for digitizing the MSX-Datapack
- Takamichi and Sander, for their help with the translation of the MSX-Audio
  and MSX-BIOS chapters of the MSX-Datapack
- Parn, for supplying me with a Ton of BASIC FM musics to test the BIOS
- Sturaro, for both the Philips Music Module upgrade board, and the OPL3v3
  cartridge.
- Leonard, for reporting the ADPCM playback bug on the NMS-1205

==== List of Compatible Games ====

- MSX-Audio BIOS mode
 - Compile
   - Disk Station 0 to 32
   - Disk Station Deluxe 1 to 3
   - Golvellius 2
   - Gorby's Pipeline
   - Lulba 2
   - Madou Monogatari
   - Megaz Monkey Do
   - Puyo Puyo
   - RuneMaster 1
   - RuneMaster 2
   - The Adventure of Randar I
   - The Adventure of Randar II
   - The Adventure of Randar III
   - USA Jong
 - Namco
   - Xevious
 - Pack in Video
   - Labyrinth
 - Telenet
   - Columns
 - ASCII
   - Penguin Kun Wars 2
   - Dante2
   - Lübeck
   - Sea Sardine
   - Fleet Commander 2 (requires the patch to fix a race-condition that the
     game had on its initialization routines)
   - Gladius
 - Games with patches by FRS
   - FireHawk-HDD
   - MonMon Monster

- Games with bugs on MSX-Audio support
  - Xevious (requires the game to be run on the second cartridge connector to
    allow MSX-Audio to be detected)
  - Puyo Puyo: Freezes on boot on many machines. The bug is on obtaining the RAM slot on 02E3h causes 032Fh to call EXTBIO with incorrect slotid=0
  - Gorby's Pipeline: freezes on boot on many machines. There's a bug on
    obtaining the RAM slot that results in a call to EXTBIO with incorrect
    slotid=0.
  - Madou Monogatari: freezes on boot on many machines. There's a bug is on
    obtaining the RAM slot that results in a call to EXTBIO with incorrect
    slotid=0.
  - Warau Salesman: freezes on boot on many machines. There's a bug is on
    obtaining the RAM slot that results in a call to EXTBIO with incorrect
    slotid=0.
  - Rune Master 3: Notes play out of tune
  - Green Crystal: Freezes on boot. Incorrect EXTBIO handling to detect the
    Audio BIOS.

- Games without MSX-Audio support, but for some reason try to detect it and
  then select the MSX-Music replayer. This means that if the machine doesn't
  have an internal MSX-Music, no FM sound will be produced.
  - Aleste-2
  - Aleste Gaiden
  - Randar III
  - Pink Sox 3-8

Note: Compile always used an incorrect method for detecting the MSX-Audio.
      They should have used the EXTBIO function-1 to obtain the number of
      available OPL chips.

==== FAQ ====

Q: Can this BIOS be run on a MegaFlashROM/similar device?
A: No, it requires a special configuration of ROM+RAM on the same slot, so a
   a MegaROM emulation don't fit. You can install it by these two ways:
   1) Upgrade your MSX-Audio cartridge to support the ROM internally, using
      the recipes available on the Net.
   2) You can opt to modify a 32KB game cartridge with the following recipe:
      a) Upgrade it to contain 64KB of ROM and 8KB of RAM
      b) The memory mapping must be as follows:
         0000h-2FFFh: ROM
         3000h-3FFFh: lower 4KB of the RAM
         4000h-6FFFh: ROM
         7000h-7FFFh: lower 4KB of the RAM (mirror of 3000h-3FFFh)
         8000h-AFFFh: ROM
         B000h-BFFFh: upper 4KB of the RAM
         C000h-EFFFh: ROM
         F000h-FFFFh: upper 4KB of the RAM (mirror of B000h-BFFFh)
      This game-cartridge approach can be used for users who don't want to
      upgrade their OPL1/OPL3/OPL4 cartriges internally.

Q: I don't know how to build an cartridge to use this BIOS, and I really want
   to have it!!!
A: There are some alternatives for that:
   - Purchade a DalSoRi v2. This OPL4 cartridge has the MSX-Audio BIOS built-in.
   - For the Philips Music Module, both Maluf and SuperSonics and sell upgrade
     kits that will enhance it to have both the BIOS and 256KB of SampleRAM.
     Unfortunately that design was still based on EPROM, so updates will
     require an EPROM burner. We hope that new batches of the board do replace
     the EPROM with Flash, for easier updates.
   - The new OPL3 cartridge from MSXpro already contains the BIOS built-in,
     and on Flash already!
   - For existing OPL4 cartridges (MoonSound/ShockWave/DalSoRi v1), you can ask
     hardware makers to release a piggy-back cartridge or an internal upgrade
     kit containing the BIOS on Flash.
   - Ask your favorite hardware maker to build new OPL4 batches with the
     BIOS built-in on Flash.

Q: Which emulators do support this BIOS?
A: OpenMSX already supports it, but you need to update hardwareconfig.xml
   file for the Boosted_audio and OPL3Cartridge2 extensions for the new
   checksum. 

Q: Will this BIOS fix the sound pitch caused by my homebrew turbo kit?
A: For the FM sound, the OPL3/OPL4 cartridges are known to be immune to changes
   on the CPU clock. This happens because those cartridges are well designed
   and include their own crystal inside for the sound reference.
   For the PSG sound, the BIOS will not *fix* the sound distortion on the real
   sense of the word, but a nice workaround is provided if you use an OPL3/OPL4
   cartridge on your machine: The games that have been upgraded to support the
   SoundChipFish extensions will have their PSG sound will be redirected to the
   OPL3/OPL4 soundchip, fixing the problem. Please note that:
   - Unmodified games will not have this benefict
   - An OPL3 or OPL4 soundchip is required, so Y8950-based devices will not
     have this extension. (This means that the Philips Music Module is out).
   - The PSG will still be affected on MSX-BASIC, as it still plays directly
     to the PSG soundchip. Future revisions of the BIOS may change this.
   - Please note that for the turbofixing the FM pitch on the Philips
     Music-Module there's a DIY mod available on the Net. All that is necessary
     is to install a crystal inside the cartridge just like the OPL3/OPL4
     cartridges have.

Q: This BIOS does not work if you don't have 256KB of SampleRAM?
A: It works with 0KB of SampleRAM just fine. For Y8950 devices, the upgrades
   always accompany the 256KB of SampleRAM as a matter of practicality: if
   you're going to mod the device, why not upgrade the SampleRAM too?

Q: Does this BIOS support following CALL-commands?
	APPEND MK
	APEEK
	APOKE
	CONT MK
	COPY PCM
	CONVP
	CONVA
	INMK
	KEY ON
	KEY OFF
	LOAD PCM
	MK VOICE
	MK VEL
	MK VOL
	MK TEMPO
	MK STAT
	MK PCM
	REC PCM
	SET PCM
	SAVE PCM
	SYNTHE
	PLAY PCM
	PCM FREQ
	PCM VOL
	PLAY MK
A: On a Y8950 device, those commands are fully supported, as this one is the
   real MSX-Audio BIOS. Those commands were explicitly blocked on MSX-Music
   because it couldn't support them.
   For OPL3/OPL4 cartridges this commands will be blocked just like they were
   on MSX-Music, for the same reason.

Q: I'm a developer, how can I code to use this BIOS?
A: The best source of information is the MSX-DataPack Vol.2.

Q: Does the CSM commands (Composite Sinusoidal Modeling) still work?
A: I don't know for sure, as I don't have any experience with this mode. I
   just leaved this part of the code untouched, if you know how it works, you
   can try it on OPL1 check the results. The MSX-DataPack Vol.2
   seems to explain how this CSM mode works, but it's only in japanese yet.
   For OPL3 and OPL4 this mode is disabled because information regarding this
   feature is conflicting. Those soundchips datasheets don't mention the
   the feature, but many people report that the feature is present yet
   undocumented.

Q: My machine has an internal MSX-Music. Will this BIOS work on it?
Q: On MSX Turbo-R machines, the internal MSX-Music is being detected instead of
   the MSX-Audio BIOS, both by games and by the CALL MUSIC command. What can I
   do?
A: This happens because those machines had a design decision that caused this:
   They placed the MSX-Music BIOS on slot 0-3 to force it to be detected before
   any other external MSX-Music, like the FM-PAC. Otherwise when using the R800
   CPU, the sound would become distorted if an FM-PAC was connected to the MSX.
   Other machines with native internal MSX-Music don't have this problem, like
   all Sony MSX2+, all Panasonic MSX2+, and all Sanyo MSX2+ models.
 To work-around it, use the following procedures:
   - For MSX-BASIC, just type CALL AUDIO before running any BASIC musics or
     BASIC software. This will cause the MSX-Audio BIOS to be activated, and it
     will automatically disable any MSX-Music BASIC extensions present on the
     machine (only until the next reboot, off course  :)
   - For games and any other binary software (*.ROM, *.BIN, *.COM), before
     running the said game/software you'll need to run my other tool, OPLLOFF,
     that will try to disable the internal MSX-Music BIOS on your machine.

Q: I'm using a OPL3 or OPL4 cartridge and can't hear any of the PSG channels of
   the music/SFX! Why that happens?
A: OPL3 and OPL4 cartridges prioritize sound quality, so they don't mix the
   internal MSX sound (that has a lot of noise) with their sound output.
   - For games, they have to be patched to use the SoundChipFish extension to
     translate the PSG commands into OPL3 commands, so all channels can be
     heard from the same soundchip. This has the advantage of also solving the
     wrong mixing volume problem present on many machines, and will also fix
     the sound pitch of the PSG notes for machines with homebrew turbo kits.
   - For MSX-BASIC, there's no solution yet. Future versions of the BIOS might
     redirect the PSG channels to the OPL3/OPL4 soundchip

Q: How is this OPL3 and OPL4 support? Can you be more specific?
A: For the MSX-BASIC, up to 9 FM channels are supported, either on mono or
   stereo modes. Support for 18 channels is planned for the future.
   For the SoundChipFish extension, PSG channels can be redirected to the OPL
   chip, resulting in a total of 15 channels being used. Why 6 channels more?
   Because the PSG has 3 tone-channels and 3 noise-channels that are mixed with
   their respective tone-channels. When translated into OPL commands, those
   noise channels must be processed as independent channels.
   The WaveTable of OPL4 isn't supported yet. That will require a lot more work.

==== Known Issues ====

The HX-MU900 version of the BIOS is making some machines freeze on BASIC, but
only on real hardware (don't affect emulated machines). It also doesn't seem to
freeze the Panasonic Turbo-R machines, but probably freezes machines based on
the S1985 and S3527 chipsets, and those based on TTL chips. I don't know the
reason yet, so if you discover what's causing the problem, please send me a
message. Curiously enough the affected MSX models will freeze even if you use
the NMS-1205 BIOS on the HX-MU900, which mean that it may be a hardware issue,
because the NMS-1205 didn't freeze any machine I tried.
Keep in mind that the BIOS only freezes BASIC programs. The OPLL->OPLn
translation works just fine.

==== Future Releases ==== 

As this is a complex software that requires a lot of work/testing/investigation,
future releases will depend entirely on the support by the MSX community:

- Users effectively using the BIOS, and demanding compliance to the MSX
  standard from the MSX developers (software compliant to the MSX-Music BIOS
  will also run fine on this BIOS)
- Hardware makers including the MSX-Audio BIOS on new hardware
  (please use Flash instead of EPROMs!)
- Chapters 7.3 (MSX-Music) and 7.4 (MSX-Audio) of the MSX-Datapack Vol.2 being
  translated, as further improvements will require good sources of
  documentation.

With the above requirements, the following improvements could be tried:
(not in any specific order)

- Further optimizations on the OPLL->OPLn translation
- Commands for setting the stereo pan per channel on the fly, for both the
  MSX-BASIC and the OPLL translation. Currently the pan position is hardcoded
  and only set on boot.
- Support for the OCM YM2413 extra registers that allows the built-in
  instruments to be redefined. This would redefining the instruments before
  running the games just like CoolColors redefines the palette before running
  MSX1 games.
- Implement the rest of the MSX-Music BIOS functions: OPLDRV, MSTART, MSTOP,
  RDDATA, TSTBGM
- 18 channel FM sound support on MSX-Basic (OPL3, OPL4 or Dual-OPL1)
- Play PSG tracks on OPL3/OPL4fm
- Enhance the OPL4 support:
  - Check OPL4 busy flag instead of using delays
  - Play PCM/ADPCM commands using the WaveTable
  - Play#4 for WaveTable music
- SoundChipFish: Translate Y8950 ADPCM commands into OPL4 WaveTable commands
- MIDI support on the Philips Music-Module


