;****************************************************************************
;* This source file was written by Acorn Computers Limited. It is part of   *
;* the "cwimp" library for writing applications in C for RISC OS. It may be *
;* used freely in the creation of programs for Archimedes. It should be     *
;* used with Acorn's objasm assembler                                       *
;*                                                                          *
;* No support can be given to programmers using this code and, while we     *
;* believe that it is correct, no correspondence can be entered into        *
;* concerning behaviour or bugs.                                            *
;*                                                                          *
;* Upgrades of this code may or may not appear, and while every effort will *
;* be made to keep such upgrades upwards compatible, no guarantees can be   *
;* given.                                                                   *
;****************************************************************************

;
; Title  : s.swi
; Purpose: provide access to RISC OS SWIs from C
; Version: 0.1
;

; SWI values

WriteC        * &00
WriteS        * &01
Write0        * &02
NewLine       * &03
ReadC         * &04
CLI           * &05
Byte          * &06
Word          * &07
File          * &08
Args          * &09
BGet          * &0A

BPut          * &0B
Multiple      * &0C
Open          * &0D
ReadLine      * &0E
Control       * &0F
GetEnv        * &10
Exit          * &11
SetEnv        * &12
IntOn         * &13
IntOff        * &14
CallBack      * &15
EnterOS       * &16
BreakPt       * &17
BreakCtrl     * &18
UnusedSWI     * &19
UpdateMEMC    * &1A
SetCallBack   * &1B
Mouse         * &1C

Heap          * &1D
Module        * &1E
Claim         * &1F     ; vector handling routines
Release       * &20     ; vector handling routines
ReadUnsigned  * &21     ; Read an unsigned number
GenerateEvent * &22
ReadVarVal    * &23     ; read variable value & type
SetVarVal     * &24     ; set  variable value & type
GSInit        * &25
GSRead        * &26
GSTrans       * &27
BinaryToDecimal   * &28
FSControl         * &29
ChangeDynamicArea * &2A
GenerateError     * &2B
ReadEscapeState   * &2C

WriteI        * &100

UserSWI       * &200


Initialise          *    &000400c0
CreateWindow        *    &000400c1
CreateIcon          *    &000400c2
DeleteWindow        *    &000400c3
DeleteIcon          *    &000400c4
OpenWindow          *    &000400c5
CloseWindow         *    &000400c6
Poll                *    &000400c7
RedrawWindow        *    &000400c8
UpdateWindow        *    &000400c9
GetRectangle        *    &000400ca
GetWindowState      *    &000400cb
GetWindowInfo       *    &000400cc
SetIconState        *    &000400cd
GetIconState        *    &000400ce
GetPointerInfo      *    &000400cf
DragBox             *    &000400d0
ForceRedraw         *    &000400d1
SetCaretPosition    *    &000400d2
GetCaretPosition    *    &000400d3
CreateMenu          *    &000400d4
DecodeMenu          *    &000400d5
WhichIcon           *    &000400d6
SetExtent           *    &000400d7
SetPointerShape     *    &000400d8



SWI_OP        * &ef000000 ; SWIAL  op-code
XOS_MASK      * &00020000 ; mask to make a swi a RISC OS v-error swi

; ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; Register names

r0 RN 0
r1 RN 1
r2 RN 2
r3 RN 3
r4 RN 4
r5 RN 5
r6 RN 6
r7 RN 7
r8 RN 8
r9 RN 9
r10 RN 10
r11 RN 11
r12 RN 12
r13 RN 13
r14 RN 14
r15 RN 15

R0 RN 0
R1 RN 1
R2 RN 2
R3 RN 3
R4 RN 4
R5 RN 5
R6 RN 6
R7 RN 7
R8 RN 8
R9 RN 9
R10 RN 10
R11 RN 11
R12 RN 12
R13 RN 13
R14 RN 14
R15 RN 15
PC RN 15

a1 RN 0
a2 RN 1
a3 RN 2
a4 RN 3
v1 RN 4
v2 RN 5
v3 RN 6
v4 RN 7
v5 RN 8
v6 RN 9
fp RN 10
ip RN 11
sp RN 12
sl RN 13
lk RN 14
pc RN 15


        EXPORT |bbc_get|
        EXPORT |bbc_vdu|
        EXPORT |bbc_vduw|
        EXPORT |os_swi|
        EXPORT |os_swix|


 AREA |C$$code|, CODE, READONLY

|v$codesegment|

bbc_get SWI     ReadC
        MOVS    PC,R14

bbc_vduw
        SWI     WriteC
        MOV     R0,R0,LSR #8
bbc_vdu SWI     WriteC
        MOVS    PC,R14

; In : a1 contains swi number, a2
; points to arm register structure

os_swi
        STMDB   sp!, {v1-v6, lk}

        ORR     a1, a1, #SWI_OP         ; make into swi operation

        ADR     v1, exit_sequence
        LDMIA   v1, {v2,v3}
        STMDB   sp!, {a1,v2,v3}         ; copy SWI and exit onto stack
        MOV     ip, a2
        LDMIA   a2, {r0-r9}             ; load up registers for SWI
        MOV     pc, sp                  ; and jump to the sequence

exit_sequence
        STMIA   ip, {r0-r9}
        LDMIA   sp!, {a1-a3, v1-v6, pc}^ ; a1-a3 just to pop stack

; In : a1 contains swi number, a2
; points to arm register structure

os_swix
        STMDB   sp!, {v1-v6, lk}

        ORR     a1, a1, #SWI_OP         ; make into swi operation
        ORR     a1, a1, #XOS_MASK       ; make a swi of V-error type

        ADR     v1, xexit_sequence
        LDMIA   v1, {v2,v3,v4,v5}
        STMDB   sp!, {a1,v2,v3,v4,v5}   ; copy SWI and exit onto stack
        MOV     ip, a2
        LDMIA   ip, {r0-r9}             ; load up registers for SWI
        MOV     pc, sp                  ; and jump to the sequence

xexit_sequence
        STMIA   ip, {r0-r9}
        MOVVC   a1, #0
        ADD     sp, sp, #12             ; SWI itself, and two words of exit
        LDMIA   sp!, {a2, a3, v1-v6, pc}^
                                        ; a2, a3 are junk
                                        ; note CANNOT move stack past LDM
                                        ; before instruction executes

        END
