@
@ Copyright (c) 2021, Jeffrey Lee
@ All rights reserved.
@
@ Redistribution and use in source and binary forms, with or without
@ modification, are permitted provided that the following conditions are met: 
@     * Redistributions of source code must retain the above copyright
@       notice, this list of conditions and the following disclaimer.
@     * Redistributions in binary form must reproduce the above copyright
@       notice, this list of conditions and the following disclaimer in the
@       documentation and/or other materials provided with the distribution.
@ 
@ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
@ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
@ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
@ ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
@ LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
@ CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
@ SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
@ INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
@ CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
@ ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
@ POSSIBILITY OF SUCH DAMAGE.
@

        .file "osinterfa.s"
        .text

        .global _start

        .global readescapestate
        .global clock

        .global __rt_stkovf_split_big
        .global __rt_stkovf_split_small
        .global __gcc_alloca
        .global __aeabi_unwind_cpp_pr1

alloca_ptr:
_start:
        MOV R10,#0x10000
        ADD R13,R10,#0x10000
        STR R13,alloca_ptr

        MOV R0,#4
        BL  setvoices
        MOV R0,#1
        MOV R1,#0
        SWI 0x40185
        MOV R6,R1
        MOV R0,#4
        ADR R1,voices
voice_loop:
        SWI 0x4018a
        SUBS R0,R0,#1
        ADDNE R1,R1,#20
        BNE voice_loop
        
        MOV R0,#3+(1<<31)
        MOV R1,#32
        SWI 0x58ec0 
        SUB R2,R10,R0
        MOV R0,#3+(1<<31)
        MOV R3,#0
        SWI 0x58ec1
        MOV R4,R0
        MOV R5,R2
        BL main        
        MOV R7,R0
        MOV R0,R5
        MOV R1,R4
        SWI 0x58ec2
        
        MOV R0,#1
        BL  setvoices
        MOV R0,#1
        MOV R1,R6
        SWI 0x40185        

        MOVS R0,R7
        SWINE 0x2b
__rt_stkovf_split_big:
__rt_stkovf_split_small:
__aeabi_unwind_cpp_pr1:
        SWI 0x11

clock:
        SWI 0x42
        MOV PC,LR

readescapestate:
        SWI 0x2C
        MRS R0,CPSR
        AND R0,R0,#1<<29
        MOV PC,LR

__gcc_alloca:
        LDR R3,alloca_ptr
        ADD R3,R3,R0
        SUB R0,R3,R0
        STR R3,alloca_ptr
        MOV PC,LR

setvoices:
        MOV R1,#0
        MOV R2,#0
        MOV R3,#0
        MOV R4,#0
        SWI 0x40140
        MOV PC,LR

voices:
        .string "Percussion-Noise\0\0\0"
        .string "Percussion-Snare\0\0\0"
        .string "Percussion-Medium\0\0"
        .string "Percussion-Soft"

        .end
