Skip to content

Instantly share code, notes, and snippets.

@HTV04
Created July 24, 2023 20:28
Show Gist options
  • Save HTV04/429eba8bb5d8c6b06f6ccfa00ac0c77c to your computer and use it in GitHub Desktop.
Save HTV04/429eba8bb5d8c6b06f6ccfa00ac0c77c to your computer and use it in GitHub Desktop.
MDSDRV ABI Wrapper
// MDSDRV ABI wrapper
// By HTV04
// Reference: https://github.com/superctr/MDSDRV/blob/master/doc/api.md
/*
MIT License
Copyright (c) 2023 HTV04
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, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
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.
*/
#pragma once
// Priority slots
#define MDS_BGM 3
#define MDS_SE1 2
#define MDS_SE2 1
#define MDS_SE3 0
// ABI functions
unsigned int mds_init(void);
void mds_request(unsigned int slot, unsigned int id);
void mds_update(void);
unsigned int mds_command(unsigned int id, ...);
/* Command wrapper functions */
const char *mds_get_version(unsigned short *hexver, unsigned short *len);
/* Command wrapper macros */
#define mds_get_cmd_count() mds_command(0x00)
#define mds_get_sound_count() mds_command(0x01)
#define mds_get_status(priority) mds_command(0x02, priority)
#define mds_get_gtempo() mds_command(0x04)
#define mds_set_gtempo(tempo) mds_command(0x05, tempo)
#define mds_get_gvolume() mds_command(0x06)
#define mds_set_gvolume(volume) mds_command(0x07, volume)
#define mds_write_fm_port0(register, data) mds_command(0x08, ((register & 0xff) << 8) | (data & 0xff))
#define mds_write_fm_port1(register, data) mds_command(0x09, ((register & 0xff) << 8) | (data & 0xff))
#define mds_fade_bgm(target, speed, stop) mds_command(0x0a, ((speed & 0xff) << 8) | ((stop & 1) << 7) | (target & 0x7f))
#define mds_set_pause(slot, state) mds_command(0x0b, slot, state)
#define mds_get_volume(slot) mds_command(0x0c, slot)
#define mds_set_volume(slot, volume) mds_command(0x0d, slot, volume)
#define mds_get_tempo(slot) mds_command(0x0e, slot)
#define mds_set_tempo(slot, tempo) mds_command(0x0f, slot, tempo)
#define mds_get_comm(slot) mds_command(0x10)
#define mds_set_pcmmode(mix, buffer) mds_command(0x11, mix, buffer)
#define mds_get_pcmmode() mds_command(0x12)
; MDSDRV ABI wrapper
; By HTV04
; Reference: https://github.com/superctr/MDSDRV/blob/master/doc/api.md
; MIT License
;
; Copyright (c) 2023 HTV04
;
; 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, subject to the following conditions:
;
; The above copyright notice and this permission notice shall be included in all
; copies or substantial portions of the Software.
;
; 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.
align 2
; Binaries
section .rodata,data
mds_drvbin incbin "lib/mdsdrv.bin"
mds_seqbin incbin "out/mdsseq.bin"
align 32768
mds_pcmbin incbin "out/mdspcm.bin"
align 2
; Work area
section .bss,bss
mds_work dcb.w 512
; Interface
section .text,code
; d0: Success (l)
mds_init::
move.l a2,-(sp)
lea mds_work,a0
lea mds_seqbin,a1
lea mds_pcmbin,a2
jsr mds_drvbin+0
move.l (sp)+,a2
rts
mds_update::
movem.l d2-d7/a2-a6,-(sp)
lea mds_work,a0
jsr mds_drvbin+4
movem.l (sp)+,d2-d7/a2-a6
rts
; Stack: Slot (w), ID (w)
mds_request::
lea mds_work,a0
move.w 10(sp),d0
move.w 6(sp),d1
jsr mds_drvbin+8
rts
; Stack: Command (w), Argument 1 (w), Argument 2 (w)
; a0: Return value, if applicable (l)
; d0: Return value, if applicable (l)
mds_command::
move.l d2,-(sp)
lea mds_work,a0
move.w 6(sp),d0
move.w 10(sp),d1
move.w 14(sp),d2
jsr mds_drvbin+12
move.l (sp)+,d2
rts
; Command wrappers
section .text,code
; Stack: Hex version pointer (l), length pointer (l)
; a0: Version pointer (l)
mds_get_version::
move.l #$03,-(sp)
jsr mds_command
addq.l #4,sp
move.l a0,-(sp)
move.l 8(sp),a0
move.l 12(sp),a1
move.l d0,(a0)
move.l d1,(a1)
move.l (sp)+,a0
rts
@HTV04
Copy link
Author

HTV04 commented Jul 24, 2023

NOTE: This wrapper was written for use with VASM. It may or may not work with other M68K assemblers, but it should be trivial to port over.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment