Skip to content

Instantly share code, notes, and snippets.

@SergioFLS
Last active November 26, 2023 05:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SergioFLS/22cfd7b7fc8d986b8f3164c6d067e305 to your computer and use it in GitHub Desktop.
Save SergioFLS/22cfd7b7fc8d986b8f3164c6d067e305 to your computer and use it in GitHub Desktop.
converter of AY-3-8910 .PSG files to .VGM format
# aypsg2vgm.py: converter of AY-3-8910 .PSG files to .VGM format
#
# Copyright (c) 2023 SergioFLS
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import argparse
import struct
def open_rb(filename: str):
return open(filename, 'rb')
def open_wb(filename: str):
return open(filename, 'wb')
def write_vgm(f, p, rate: int, ay_type: int):
f.write(b'Vgm ')
eof_pointer = f.tell()
f.write(bytes([0x00, 0x20, 0x5C, 0x4D])) # we don't know what the final output will be, so FFFFFFFF is EOF
f.write(struct.pack('<I', 0x00000151)) # 1.51
f.write(bytes([0,0,0,0] * 3)) # No SN7, no YM2413, no GD3
samples_pointer = f.tell()
f.write(struct.pack('<I', 65535*100)) # Total samples (to be done later)
f.write(bytes([0,0,0,0] * 2)) # No loop for now
f.write(bytes([50,0,0,0])) # 50Hz
f.write(bytes([0,0,0,0])) # no sn7
f.write(bytes([0,0,0,0])) # no YM2612
f.write(bytes([0,0,0,0])) # no YM2151
f.write(bytes([0x4c,0,0,0])) # offset (to be done later)
f.write(bytes([0,0,0,0] * 15)) # we don't use those chips
f.write(struct.pack('<I', rate)) # finally! AY8910 clock rate (ZX Spectrum clock for now)
f.write(struct.pack('<B', ay_type)) # AY-3-8912
f.write(b'\x01') # Single out
f.write(b'\x00\x00') # no more misc flags
f.write(b'\x00\x00\x00') # no volume stuff
f.write(b'\x00') # hhhhhhhhhh
# oh god here we go
sample_num = 0
assert(p.read(4) == b'PSG\x1a')
p.read(0x0C) # ignore the padding
while True:
data = p.read(1)
if data == b'': break
data = data[0]
if data == 0xFE:
wait = (p.read(1)[0] * 4) * 882
for i in range(wait // 0xFFFF):
f.write(b'\x61' + struct.pack('<H', 0xFFFF))
f.write(b'\x61' + struct.pack('<H', wait % 0xFFFF))
sample_num += wait
elif data == 0xFF:
f.write(b'\x63')
sample_num += 882
else:
f.write(b'\xa0' + struct.pack('<B', data) + struct.pack('<B', p.read(1)[0]))
print(hex(sample_num))
f.write(b'\x66') # end of file
eof = f.tell()
f.seek(eof_pointer)
f.write(struct.pack('<I', eof)) # now we know where is EOF
f.seek(samples_pointer)
f.write(struct.pack('<I', sample_num)) # and samples
f.seek(eof) # go back to EOF just in case
if __name__ == '__main__':
arg_parser = argparse.ArgumentParser(description='Converts AY-3-8910 .PSG files to .VGM format.')
arg_parser.add_argument('input_psg', type=open_rb, help='PSG input file name')
arg_parser.add_argument('output_vgm', type=open_wb, help='output VGM file name')
arg_parser.add_argument('-r', '--rate', type=int, default=1773400, help='AY8910 clock rate in Hz (default is %(default)s)')
arg_parser.add_argument('-t', '--ay-type', type=int, default=1, help='type of AY chip being emulated (per VGM spec; default is %(default)s)')
args = arg_parser.parse_args()
write_vgm(args.output_vgm, args.input_psg, args.rate, args.ay_type)
https://sourceforge.net/p/fuse-emulator/mailman/fuse-emulator-devel/thread/20031107231936.D14180%40philos.lan.philosys.de/
https://web.archive.org/web/20010728140044/http://www14.brinkster.com/maxheadroom/ay/formats_epsg.html:
Byte 0FFh is the marker of the beginning interrupt. If there are some following bytes in range 0-15, then it is the number of the AY register, and the next byte is the value of it.
Further next pair of byte follows, first byte of is the register number and second byte is again the register value. And so on, continuing until the end of file is reached or the byte 0FFh (next interrupt), or byte 0FEh is found. The Byte following the 0FEh marker will be multiplied by four and is the number of interrupts without AY-output.
For example sequence FE 01 FF is equivalent to sequence FF FF FF FF FF.
If in the PSG-file you will find a register number in range 16-253, then you can ignore this and the following next byte. This is an output to other MSX devices.
https://web.archive.org/web/1/http://bulba.at.kz/PSGFormat.rar:
This is log file produced by X128 (ZX Spectrum emulator) and fMSX (MSX
emulator). Every access to the AY 3-891x (Yamaha's soundchip) is stored in this
file. It's much like RAW format for adlib.
format1: [0]=db 'PSG',1ah ... file identifier
[4]=db data ... AY data
data: command or parameter command==0-15,byte ... next byte is data for
this AY register
command==0fdh ... eom (end of music)
command==0feh,byte ... multiple eoi
command==0ffh ... eoi (end of interrupt,
current data will be
send to ay-emulator)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment