PSIONICS FILE - SYSCALLS
========================
System calls
Last modified 1994-01-15
========================

The Psion System 3 operating system offers a large number of system calls.

A system call is described by two byte values: the Fn and Sub values. A call
takes up to 5 word parameters (called BX, CX, DX, SI, and DI) and one byte
parameter (AX), and returns 6 word results (AX, BX, CX, DX, SI, and DI) and
one byte result (FL).

There are two OPL keywords for making system calls: CALL and OS. They access
the same system calls, and differ only in the way in which the parameters are
passed and the results returned.

CALL takes 1 to 6 arguments. The first is (Fn + 256 * Sub) while the remainder
(all optional) are BX to DI in that order; AL is set to an undefined value.
Only the word result AX is returned (as the result of the CALL keyword); the
other results cannot be accessed.

OS takes two or three arguments; if there is no third, it is taken to be
identical to the second. The first argument is Fn, while the second and third
are each the address of a 12 byte buffer, holding the parameters and results
respectively:
  Offset  0 (word): Sub * 256 + AL parameter, or AX result
  Offset  2 (word): BX
  Offset  4 (word): CX
  Offset  6 (word): DX
  Offset  8 (word): SI
  Offset 10 (word): DI

The byte result FL is returned as the result of the keyword. In many cases
only bit 0 of FL is significant; this bit is described as CF.
of OS is the value of FL returned. Usually only the bottom bit of FL (called
CF) is significant.

In many cases, a system call will set CF to indicate an error, and place an
error code in AX. In this case only the low byte is defined; to get the error
code, evaluate ((AX AND $FF) - $100). If CF is clear, AX will be zero.

When a parameter or result is described as a cstr, a qstr, a buffer, or any
other construct requiring more than 2 bytes, the actual parameter or result is
the address of the construct.


The system calls are described in a standard form. The first line gives the
Fn and Sub values, plus the meaning of AL if specified. The next six lines
show the meaning of AX, BX, CX, DX, SI, and DI, and the last line, if present,
shows the meaning of FL or CF. In each case except the last, an arrow (->)
separates the value passed from that returned (AX has no value passed). If
there is no arrow, the returned value is unspecified. A double arrow (=>)
indicates that the value is unchanged by the call. A completely blank entry
indicates that all known examples of the call do not specify this argument at
all; calls via CALL set the argument to 0 or omit it, and calls via OS leave
the array element in its initial state (zero).

For example, consider the following (non-existent) system call:
Fn $12 Sub $34
    AX: -> magic cookie table entry zero
    BX: table entry number =>
    CX: -> magic cookie entry BX
    DX: addend -> CX out + DX in
    SI:
    DI:
    CF: if set, call failed and CX and DX are unspecified
Returns entries from the magic cookie table.

This system call could be used either by:
    entry0% = CALL ($3412, entry%, 0, addend%)
which only returns magic cookie table entry zero, or by:
    LOCAL regs%(6)
    regs%(1)=$3400
    regs%(2)=entryno%
    regs%(4)=addend%
    failed%=1 AND OS ($12, ADDR(regs%()))
    entry0%=regs%(1)
    IF NOT failed
        REM regs%(2) will still equal entryno%
        entry%=regs%(3)
        REM regs%(4) will equal entry%+addend%
    ENDIF
which returns much more information.


System calls
------------

Fn $80 Sub $00
    AX: -> free memory
    BX:
    CX:
    DX:
    SI:
    DI:
    CF: clear if successful
Returns the amount of free system memory, in units of 16 bytes.


Fn $81 Sub $00
    AX: -> allocated block
    BX:
    CX: number of bytes to allocate
    DX:
    SI:
    DI:
    CF: clear if successful
Allocates a block of memory.


Fn $81 Sub $03
    AX:
    BX: 0
    CX: block to free
    DX:
    SI:
    DI:
    FL:
Frees a previously allocated block.


Fn $83 Sub $00
    AX:
    BX: number of message slots + 256 * extra message data length
    CX:
    DX:
    SI:
    DI:
    FL:
Initialize the message system so that the current process can receive
inter-process messages. The call reserves the indicated number of message
slots and the indicated extra message data space. [It is not clear whether
this space is global or per message, nor where the slots are or how to access
them. This call has only actually been seen with BX = 1.]


Fn $83 Sub $01
    AX:
    BX: address of message slot reference word
    CX:
    DX:
    SI:
    DI: address of status word
    FL:
Request that inter-process messages to this process be delivered. When a
message is delivered, the status word is set to zero and the message slot
reference word is set to the message slot into which the message has been
delivered. [I have no information on what the reference word actually is, nor
how the slot is freed for re-use; the only example of IPC does not use either
of these values.]


Fn $86 Sub $09
    AX:
    BX: process ID
    CX:
    DX:
    SI:
    DI:
    FL:
Sends an IOSIGNAL to a process.


Fn $87 Sub $01
    AX: -> error code, zero means success
    BX: (cstr) program name
    CX: (qstr) program information
    DX: 0
    SI: 0
    DI: address of word to be filled with process ID of new process
    CF: clear if successful
Starts a new process and places it in "suspended" state. The program
information is a qstr consisting of the following catenated together (note
that all these elements except the first are cstrs):
(1) the character 'C' (create new file) or 'O' (open existing file);
(2) (cstr) the application name, with the first character uppercased and the
    rest lowercased;
(3) (cstr) one of:
    (a) the normal extension for filenames, including the leading dot;
    (b) the normal extension with dot, followed by a space and aliasing
        information;
    (c) an empty string
(4) (cstr) the data file path passed as the initial argument;
(5) (cstr) the application path (may be omitted if not required).

A binary application may be used to operate on a standard file type. Examples
include:
    Program                  Application  Extension
                             [item 2]     [item 3]
    "ROM::DATA.APP"          "Data"       ".DBF"
    "ROM::WORD.APP"          "Word"       ".WRD"
    "ROM::WORD.APP"          "Program"    ".OPL"
    "LOC::C:\APP\COMMS.APP"  "Comms"      ".SCO"

In each of these cases there is no aliasing information or application path.
An example program information qstr is "Data~.DBF~LOC::M:\DBF\MYDATA.DBF~"
(tilde indicates a zero byte).

The word processor may also be used as a program editor, by making item 2
its name ("Program") and item 3 an extension (usually ".OPL") followed by the
necessary aliasing information: "OROPO", where the first O is the last letter
of the name of the translator "ROM::SYS$PRGO" (and can be changed if other
translators are available), the R indicates that the translator can execute
resulting code [I don't know what value to use if it can't], and OPO is the
extension of translated files. An example program information qstr is
"Program~.OPL OROPO~LOC::M:\OPL\MYPROG.OPL~"

Finally, a translator may be used to execute a translated file: in the case
of the translator "ROM::SYS$PRGO", it can execute either OPO or OPA files.
The application name is given in the APP ... ENDA clause for OPA files, and
is "RunOpl" for OPO files; there is no extension or aliasing information.
For OPA files, the data file is passed to the application, and the application
path shows which application is actually run. For OPO files, the data file
should name the OPO file itself, and there is no application path. Examples
are "RunOpl~~M:\OPO\MYPROG.OPO~" and "MyApp~~M:\DAT\MYDATA~M:\APP\MYAPP.OPA~".


Fn $87 Sub $08
    AX: -> error code, zero means success
    BX: (cstr) filename
    CX: 16 byte buffer
    DX: 0
    SI: 0
    DI: 0
    CF: clear if successful
The buffer is filled in with information about the file:
  Offset  0 (word): [unknown, always seems to be 2, called "ver"]
  Offset  2 (word): attributes
  Offset  4 (long): file size in bytes
  Offset  8 (long): time last modified
  Offset 12 (long): [unknown, called "sp"]

The attribute bits have the following meanings (question marks indicate that
the bit name was specified but the meaning is unknown):
    Bit  0: file is not ReadOnly
    Bit  1: file is Hidden
    Bit  2: file is a System file
    Bit  3: file is a Volume name
    Bit  4: file is a Directory
    Bit  5: file is Modified
  ? Bit  8: file is a readable file
  ? Bit  9: file is an executable file
  ? Bit 10: file is a byte stream file
  ? Bit 11: file is a text file


Fn $87 Sub $09
    AX: -> zero indicates success
    BX: (cstr) filename
    CX: mask of attributes to be changed (only bits $27 should be used)
    DX: 0
    SI: 0
    DI: new values for attributes to be altered
    FL:
The attributes indicated in the mask of the specified file are altered to
the new values; all other attributes are left unchanged.


Fn $87 Sub $0A
    AX: -> error code, zero indicates success
    BX: (cstr) device name (such as "A:" or "LOC::A:")
    CX: 64 byte buffer
    DX:
    SI:
    DI:
    CF: clear if device exists
The buffer is filled in with information about the device:
  Offset  2 (byte): device type
    0 = unknown
    1 = floppy
    2 = hard disc
    3 = flash
    4 = ram
    5 = rom
    6 = write-protected
  Offset  3 (byte): device properties:
    Bit 3: formattable device
    Bit 4: dual density device
    Bit 5: internal device
    Bit 6: dynamically sizeable device
    Bit 7: compressible (worth compressing database files)
  Offset  6 (long): total space on the device (may be wrong for M:)
  Offset 10 (long): free space on the device
  Offset 14 (cstr): volume name


Fn $88 Sub $00
    AX: -> process ID of the current process
    BX:
    CX:
    DX:
    SI:
    DI:
    FL:
Returns the process ID of the current process.


Fn $88 Sub $01
    AX: -> process ID
    BX: (cstr) pattern
    CX:
    DX:
    SI:
    DI:
    FL:
Returns the process ID of a process whose name matches the pattern. [This is
probably related to Fn $88 Sub $0B in some way, but I'm not sure why there
are two different calls. This one is only ever seen used with a pattern like
"*.$06".]


Fn $88 Sub $06
    AX:
    BX: process ID
    CX:
    DX:
    SI:
    DI:
    FL:
Take a process out of the suspended state and start it executing.


Fn $88 Sub $0B
    AX: -> process ID
    BX: 0 or process ID
    CX:
    DX:
    SI: name buffer =>
    DI: (cstr) pattern =>
    CF: clear if successful, set if no more processes
Obtains process IDs for processes whose name matches the pattern ("?" and "*"
wildcards have their Unix meaning, and case is ignored). BX should be zero to
return the first matching process, or the process ID returned by a previous
call to return subsequent matching processes. Process are returned in *task*
ID order. The name buffer is filled with a cstr giving the process name
(maximum 19 characters + terminating zero).


Fn $88 Sub $0D
    AX:
    BX: process ID
    CX:
    DX:
    SI:
    DI:
    FL:
Terminates the indicated process.


Fn $88 Sub $0E
    AX:
    BX: message type (?)
    CX:
    DX:
    SI:
    DI:
    FL:
Requests that, when the process is about to be terminated by Fn $88 Sub $0D,
the process should not be terminated but instead be sent an inter-process
message. [The message is presumably of the indicated type; this call has
only been seen with the type set to 1.]


Fn $89 Sub $02
    AX: -> high half of system clock
    BX: -> low half of system clock
    CX:
    DX:
    SI:
    DI:
    FL:
Reads the system clock (an abstime).


Fn $89 Sub $03
    AX:
    BX:
    CX: high half of new system clock
    DX: low half of new system clock
    SI:
    DI:
    FL:
Sets the system clock to the given abstime.


Fn $89 Sub $04
    AX:
    BX: 0
    CX: high half of time
    DX: low half of time
    SI: 0
    DI: 8 byte buffer
    FL:
Splits an abstime into a day number and an interval, placed in the buffer as
follows:
  Offset  0 (long): day number
  Offset  4 (long): interval


Fn $8B Sub $01
    AX: -> machine type information
    BX: 
    CX:
    DX:
    SI:
    DI:
    FL:
On my Series 3, the low byte of the machine type information is $05 and the
high byte is 1. On a Series 3A, the low byte is $0B.


Fn $8B Sub $05
    AX:
    BX: 40 byte buffer
    CX:
    DX:
    SI:
    DI:
    FL:
Fills the buffer with country-specific data:
  Offset  0 (word): country code (e.g. UK is 44) of locale
  Offset  2 (word): current offset from GMT in minutes (+ is ahead)
  Offset  4 (byte): date format (0 = MDY, 1 = DMY, 2 = YMD)
  Offset  5 (byte): time format (0 = am-pm, 1 = 24 hour)
  Offset  6 (byte): currency symbol position (0 = before, 1 = after)
  Offset  7 (byte): currency symbol separated with space (0 = yes, 1 = no)
  Offset  8 (byte): currency decimal places
  Offset  9 (byte): currency negative format (0 = minus, 1 = brackets)
  Offset 10 (byte): currency separate thousands
                    (0 = no, 1 = yes, 4 = 10000 and over)
  Offset 11 (byte): thousands separator
  Offset 12 (byte): decimal separator
  Offset 13 (byte): date separator
  Offset 14 (byte): time separator
  Offset 15 to  23: zero terminated currency symbol
  Offset 24 (byte): start of week (0 = Mon, 1 = Tue, ... 6 = Sun)
  Offset 25 (byte): summer times group
                    (1 = Home, 2 = European, 4 = Northern, 8 = Southern)
  Offset 26 (byte): clock type (0 = analogue, 1 = digital)
  Offset 27 (byte): number of letters in day abbreviation (0 to 6)
  Offset 28 (byte): number of letters in month abbreviation (0 to 255)
  Offset 29 (byte): workdays (the set bits indicate the workdays)
    Bit 0: Monday
    Bit 1: Tuesday
    Bit 2: Wednesday
    Bit 3: Thursday
    Bit 4: Friday
    Bit 5: Saturday
    Bit 6: Sunday
    Bit 7: always zero
  Offset 30 (byte): units (0 = inches, 1 = centimetres)


Fn $8B Sub $07
    AX:
    BX: 0
    CX: number of bytes to fetch
    DX:
    SI: offset of first byte in kernel workspace
    DI: buffer to be filled in
    FL:
Copy a number of bytes from the kernel workspace to the current process.
The values available include:
  Offset 1052 (word): increments every 1/32 second, but is not synchronized
                      to the real time clock (it drifts)
  Offset 1054 (word): always $FFFF so far
  Offset 1056 (word): delay in 1/32 seconds until current time next changes
  Offset 1058 (long): current abstime


Fn $8B Sub $13
    AX:
    BX:
    CX:
    DX:
    SI:
    DI:
    FL:
[Various code calls this 'GenMarkNonActive'. I don't know what it does, but it
is something to do with auto-off. I believe it works something like this. A
process is idle if it is waiting for a signal (either directly using IOWAIT,
or indirectly using IOWAITSTAT or IOW). Auto-off operates if every process has
been idle for the requisite time, with the timer being restarted every time a
process receives a signal. However, processes that call GenMarkNonActive are
omitted from this logic; they can thus receive frequent signals without
preventing auto-off from working. This hypothesis needs further testing.]


Fn $8B Sub $17
    AX: -> auto-off time in seconds, or $FFFF if disabled
    BX:
    CX:
    DX:
    SI:
    DI:
    FL:
Returns the auto-off time.


Fn $8B Sub $18
    AX:
    BX: auto-off time in seconds, or $FFFF to disable
    CX:
    DX:
    SI:
    DI:
    FL:
Sets the auto-off time.


Fn $8B Sub $1B
    AX: -> current locale code
    BX:
    CX:
    DX:
    SI:
    DI:
    FL:
Returns the current locale code. Locale codes are listed in the Psionics
file LOCALES.


Fn $8B Sub $1E
    AX:
    BX: address of a 20 byte buffer
    CX:
    DX:
    SI:
    DI:
    FL:
Sets the country-specific data to that in the buffer. Since there is no way to
set only part of the data, use Fn $8B Sub $05 to fill the buffer, alter the
contents, then make this call.


Fn $8B Sub $24
    AX:
    BX: search position -> new search position
    CX:
    DX: length of pattern to search for =>
    SI: qstr with maximum length of 255 =>
    DI: address of pattern to search for =>
    CF: set if no more, clear if variable found
Looks for environment variables whose names match the pattern (names should
be uppercase, and wildcards "?" and "*" are available). The first variable is
found using search position 0, and subsequent variables by using the search
position returned by the previous call. If the call succeeds, the variable name
will have been placed in the qstr.

The environment variable names include 10 which refer to the 10 calculator
memories. These variables have names of the form "M$0AAA" to "M$9JJJ", and the
calculator memory names are then "AAA" to "JJJ" (so if memory 6 is actually
called "M6", the variable will be "M$6M6). These calculator memories can be
accessed by the OPL expression 'EVAL "AAA"' etc. These are the only variables
with names beginning "M$" followed by a digit.


Fn $8B Sub $2A
    AX: -> rom version number
    BX:
    CX:
    DX:
    SI:
    DI:
    FL:
Returns the rom version number. $180F represents version "1.80F"


Fn $8D Sub $19
    AX:
    BX: processing state
    CX: process ID
    DX:
    SI:
    DI:
    FL:
Moves the indicated process to the indicated processing state. State 0 is the
foreground process; state 100 is a background process. [I don't know what any
other states are.]


Fn $8D Sub $5F
    AX:
    BX: 1
    CX: offset
    DX: 0
    SI: 0
    DI: 0
    FL:
Modifies the effect of an immediately following call to GLOADBIT with one
argument. It indicates that the picture starts at the indicated offset of
the file rather than at the start.


Fn $8D Sub $99
    AX:
    BX: processing state
    CX: 0
    DX:
    SI:
    DI:
    FL:
Equivalent to Fn $8D Sub $19 applied to the current process.


Fn $8D Sub $C5
    AX: -> zero means success
    BX: 26
    CX: $404
    DX:
    SI:
    DI:
    FL:
[Still trying to find out more about this.]
    [[All to do with capturing keys]]
    [[BX=26 CX=$404 does something]]
    [[BX=$123 CX=$800 apparently captures the HELP key]]
    [[BX=k CX=0 captures key code k, so does it with CX=$800]]
    [[BX=$2003 CX=0 captures power-off !]]


Fn $8D Sub $C6
    AX: -> zero means success
    BX: ?
    CX: ?
    DX:
    SI:
    DI:
    FL:
[Still trying to find out more about this. It probably cancels Sub $C5.]


Fn $8D Sub $C7
    AX:
    BX: 100
    CX:
    DX:
    SI:
    DI:
    FL:
Makes the current pprocess a background "system modal" process.


Fn $8D Sub $C8
    AX:
    BX: 0
    CX:
    DX:
    SI:
    DI:
    FL:
Cancels the system modality of the current process and moves it to the
foreground.


Fn $8D Sub $DA
    AX:
    BX: process ID
    CX: (cstr) message
    DX: 1 (message length ?)
    SI:
    DI:
    FL:
Sends the message to the indicated process; if the process uses GETEVENT, it
will receive event $404 and can get the message with GETCMD$. [This has only
been tested with the message "X".]


Fn $8E Sub $11
    AX:
    BX: 6 byte buffer
    CX:
    DX:
    SI:
    DI:
    FL:
Gets information about the power supply. The buffer is filled with the
following data:
  Offset  0 (word): main battery voltage in mV [but I have seen 1800 both
                    with no battery and with slightly low battery, and 2056
                    with new batteries]; [allegedly external supply voltage
                    when on]
  Offset  2 (word): backup battery voltage in mV [but I have seen 2300 with
                    no battery and 2556 with one fitted];
  Offset  4 (word): positive if external power is available, zero if not
                    available, and negative if the detector is disabled
                    because an SSD door is open.


Fn $8E Sub $1C
    AX:
    BX: 8 byte buffer
    CX:
    DX:
    SI:
    DI:
    FL:
Gets information about the power supply. The buffer is filled with the
following data:
  Offset  0 (word): main battery good voltage threshold in mV
  Offset  2 (word): backup battery good voltage threshold in mV
  Offset  4 (word): main battery nominal maximum voltage in mV
  Offset  6 (word): backup battery nominal maximum voltage in mV


Fn $91 Sub $00
    AX:
    BX: process ID
    CX: number of bytes to copy
    DX: 0
    SI: remote address of first byte to copy
    DI: local address of first byte to copy
    FL:
Copy a number of bytes from the indicated process to the current process.


Fn $92 Sub $00
    AX:
    BX: process ID
    CX: number of bytes to copy
    DX: 0
    SI: local address of first byte to copy
    DI: remote address of first byte to copy
    FL:
Copies a number of bytes from the current process to the indicated process.


Fn $A1 Sub $00
    AX:
    BX: 0
    CX: length to be copied
    DX: 0
    SI: address of "from" buffer
    DI: address of "to" buffer
    FL:
Copies a number of bytes from one buffer to another. The effect if the copied
areas of the buffers overlap is unspecified.


Fn $A9 Sub $00 (case sensitive)
Fn $AA Sub $00 (case insensitive)
    AX: -> offset of sub-string within buffer
    BX: length of sub-string
    CX: size of buffer
    DX:
    SI: buffer to be searched
    DI: sub-string
    CF: clear if sub-string found, set it not
Searches a buffer for a contained sub-string, and returns the offset of the
first occurence. The search can be case sensitive or case insensitive.


Fn $AC Sub $00
    AX:
    BX:
    CX:
    DX:
    SI: cstr
    DI: buffer large enough to hold the cstr
    FL:
Copies the cstr into the buffer.


Fn $B9 Sub $00
    AX: -> length of the string (excluding the zero)
    BX: 0
    CX: 0
    DX: 0
    SI: 0
    DI: cstr
    FL:
Returns the length of a cstr.
-- 
Clive D.W. Feather     | Santa Cruz Operation    | If you lie to the compiler,
clive@sco.com          | Croxley Centre          | it will get its revenge.
Phone: +44 923 816 344 | Hatters Lane, Watford   |   - Henry Spencer
Fax:   +44 923 817 688 | WD1 8YN, United Kingdom |


