Foenix Kernel Documentation

From C256 Foenix Wiki
Jump to navigation Jump to search

Foenix Kernel Documentation

Kernel Essentials

Source Code: https://github.com/Trinity-11/Kernel_FMX

The Foenix Kernel resides in bank $19 (U) or bank $39 (U+/FMX) of the system RAM. It provides for the initialization of the hardware and a certain minimalist level of access to the hardware. Kernel routines are called through a kernel jump table that starts at $00:1000. All kernel routines must be called using the JSL instruction (long, or 24-bit subroutine call), since they all terminate with an RTL. This allows the kernel routines to be called from anywhere in system memory.

Kernel Calls

Name Address Description
BOOT $00:1000 Cold boot routine
PUTC $00:1018 Print a character to the currently selected channel
Register Width Contents
A 8 ASCII code of the character to print
PUTS $00:101C Print a null-terminated ASCII string to the currently selected channel
Register Width Contents
B 8 Bank containing the data to print
X 16 Address within that bank of the first character to print
SETIN $00:1038 Set the current input channel used by the GET subroutines
Register Width Contents
A 8 Input channel to use
  • 0 = Keyboard
  • 1 = COM1 (external serial)
  • 2 = COM2 (internal serial)
SETOUT $00:103C Set the current output channel used by the PUT subroutines
Register Width Contents
A 8 Input channel to use
  • 0 = Screen
  • 1 = COM1 (external serial)
  • 2 = COM2 (internal serial)
GETCHW $00:104C Get a character from the input channel. Waits until data received. A=0 and Carry=1 if no data is waiting
GETCHE $00:1050 Get a character from the input channel and echo to the screen. Wait if data is not ready.
PRINTCR $00:106C Print Carriage Return
PRINTH $00:1078 Print hex value in memory.
Register Width Contents
B 8 Bank containing the data to print
X 16 Address within that bank of the last byte to print
Y 16 Number of bytes to print
PRINTAH $00:1080 Prints hex value in A. Printed value is 2 wide if M flag is 1, 4 wide if M
LOCATE $00:1084 Move the cursor to a new position on the screen.
Register Width Contents
X 16 The column for the cursor
Y 16 The row for the cursor
CSRRIGHT $00:1090 Move the cursor one position to the right
CSRLEFT $00:1094 Move the cursor one position to the left
CSRUP $00:1098 Move the cursor up one row
CSRDOWN $00:109C Move the cursor down one row
CSRHOME $00:10A0 Move the cursor to the upper-left corner
SCROLLUP $00:10A4 Scroll the screen up one line. Creates an empty line at the bottom.
CLRSCREEN $00:10B0 Clear the screen
INITCHLUT $00:10B4 Init character look-up table
INITSUPERIO $00:10B8 Init Super-IO chip
INITKEYBOARD $00:10BC Init keyboard
INITRTC $00:10C0 Init Real-Time Clock
INITCURSOR $00:10C4 Init the Cursors registers
INITFONTSET $00:10C8 Init the Internal FONT Memory
INITGAMMATABLE $00:10CC Init the RGB GAMMA Look Up Table
INITALLLUT $00:10D0 Init the Graphic Engine (Bitmap/Tile/Sprites) LUT
INITVKYTXTMODE $00:10D4 Init the Text Mode @ Reset Time
INITVKYGRPMODE $00:10D8 Init the Basic Registers for the Graphic Mode
F_OPEN $00:10F0 Open a file for reading/writing. DOS_FD_PTR points to a file descriptor for the file.
F_CREATE $00:10F4 Create a new file and write its first cluster. DOS_FD_PTR points to a file descriptor for the file.
F_CLOSE $00:10F8 Close a file (make sure last cluster is written). DOS_FD_PTR points to a file descriptor for the file.
F_WRITE $00:10FC Write the current cluster to the file. DOS_FD_PTR points to a file descriptor for the file.
F_READ $00:1100 Read the next cluster from the file. DOS_FD_PTR points to a file descriptor for the file.
F_DELETE $00:1104 Delete a file / directory. DOS_PATH_BUFF = a buffer containing the full path to the file (NULL terminated).
F_DIROPEN $00:1108 Open a directory and seek the first directory entry. NOTE: currently only works with the root directory.
Variable Direction Contents
DOS_FD_PTR IN Points to a file descriptor for the path to lookup. NOTE: currently unused.
DOS_DIR_PTR OUT Pointer to the first FAT directory entry in that directory.
F_DIRNEXT $00:110C seek to the next directory of an open directory
Variable Direction Contents
DOS_DIR_PTR IN Pointer to the current FAT directory entry.
DOS_DIR_PTR OUT Pointer to the next FAT directory entry.
F_DIRREAD $00:1110 Read the directory entry for the specified file
Variable Direction Contents
DOS_PATH_BUFF IN The buffer containing the path to the file to lookup.
DOS_DIR_PTR OUT Pointer to the FAT directory entry for that file.
F_DIRWRITE $00:1114 Write any changes in the current directory cluster back to the drive
F_LOAD $00:1118 load a binary file into memory, supports multiple file formats
Variable Contents
DOS_FD_PTR Points to a file descriptor for the file to lookup.
DOS_DST_PTR Pointer to the location to load the file (any value > $3F:FFFF to load the file where the file specifies).
F_SAVE $00:111C Save memory to a binary file
Variable Contents
DOS_FD_PTR Points to a file descriptor for the file to create.
DOS_SRC_PTR Address of the first byte to save.
DOS_END_PTR Address of the last byte to save.
CMDBLOCK $00:1120 Send a command to a block device.
Variable Contents
BIOS_DEV The target device
X The number of the command to send. Command numbers are device dependent, but typical examples are: 1 = Turn on spindle motor, 2 = Turn off spindle motor, 3 = Recalibrate.
F_RUN $00:1124 Load and run a binary file from a block device. DOS_RUN_PARAMS is a pointer to the ASCIIZ string specifying the path of the file to execute and any parameters to it.
F_MOUNT $00:1128 Mount the designated block device. BIOS_DEV is the device number to mount.
F_COPY $00:1130 Copy a file. DOS_STR1_PTR is the path of the source file to copy. DOS_STR2_PTR is the path of the destination.
F_ALLOCFD $00:1134 Allocate a file descriptor from the kernel. DOS_FD_PTR will point to the file descriptor allocated, if one is available. There

is a limit of 8 file descriptors that can be allocated at any one time.

F_FREEFD $00:1138 Return a file descriptor to the kernel. DOS_FD_PTR points to the file descriptor to return to the kernel. It must have been

allocated previously through F_ALLOCFD.

DOS Calling Conventions

The DOS file routines use several variables to communicate with the calling program. All DOS routines will return with the carry bit set on success. If the routine was unable to complete successfully or needs to return a negative condition, the carry bit will be clear and DOS_STATUS and BIOS_STATUS variables may contain non-zero status codes describing the nature of the failure.

Variable Address Width (bytes) Purpose
DOS_FD_PTR $00:0340 4 A pointer to a file descriptor structure (defined below).
DOS_PATH_BUFF $00:0400 256 A buffer space for storing a path to a file or directory.
DOS_DIR_PTR $00:0338 4 A pointer to a directory entry. The layout of the entry is identical to that used in the FAT file system.
DOS_DST_PTR $00:0354 4 A pointer to a block of memory to be filled from the disk.
DOS_SRC_PTR $00:0350 4 A pointer to a block of memory to write to disk.
DOS_END_PTR $00:0358 4 A pointer to the last byte of a block of memory.
DOS_STATUS $00:032E 1 A status code for DOS operations. $00 = success
BIOS_STATUS $00:0320 1 A status code for sector level operations. $00 = success

File Descriptor

Most DOS subroutines take a pointer to a file descriptor as an argument. This file descriptor contains several elements needed to manage a file, including the buffer containing the current cluster of the file:

Name Offset Width (bytes) Purpose
STATUS 0 1 A special status byte containing information about the file's state
Name Value Purpose
FD_STAT_READ $01 The file is open for reading
FD_STAT_WRITE $02 The file is open for writing
FD_STAT_OPEN $40 The file is open.
FD_STAT_ERROR $60 There is an error with the file.
FD_STAT_EOF $80 The file is being read and has reach the end of the file.
DEV 1 1 A byte containing the device number for the file (should not be modified by the user program).
PATH 2 4 A pointer to a null-terminated string containing the path to the file. Set by the user program.
CLUSTER 6 4 The ID of the current cluster being read or written. Usually set by DOS.
FIRST_CLUSTER 10 4 The ID of the first cluster of the file. Usually set by DOS.
BUFFER 14 4 Pointer to the buffer containing a cluster's worth of data. Provided by the user program. Used by DOS to read or write the disk.
SIZE 18 4 The size of the file in bytes.
CREATE_DATE 22 2 The creation date in the same format as used by FAT.
CREATE_TIME 24 2 The creation time in the same format as used by FAT.
MODIFIED_DATE 26 2 The modification date in the same format as used by FAT.
MODIFIED_TIME 28 2 The modification time in the same format as used by FAT.

Interrupt Jump Table

The kernel provides a jump table for interrupt handler support. When an interrupt is triggered of any particular type, the kernel interrupt handler will jump to the vector for that interrupt. The vector should point to a subroutine that returns to the caller through an RTL instruction. Each vector entry is four bytes long. The first byte is a JML opcode. The next three bytes are the long (24-bit) address of the interrupt handler routine. Currently, the kernel only supports a few interrupts in this manner. All system interrupts will eventually have an entry in this table.

A program needing to take control of one of these interrupts can replace the 24-bit address of the appropriate jump table entry (with interrupts disabled). It is recommended to save the previous value and restore it before quitting.

Interrupt Address Purpose
VEC_INT00_SOF $00:1700 Start Of Frame (vertical blank)
VEC_INT01_SOL $00:1704 Start Of Line (horizontal blank)
VEC_INT02_TMR0 $00:1708 Timer 0
VEC_INT03_TMR1 $00:170C Timer 1
VEC_INT04_TMR2 $00:1710 Timer 2

Console Special Characters

The console routines support several special control codes for both input and output. When the key on the keyboard is pressed, the associated code is returned by the GET routines. If the code is printed through the PUTC or PUTS, the associated operation will be performed on the screen. Note that this means that any graphics characters associated with those code points will not be displayed by PUTC or PUTS. If those characters must be displayed, they would have to be written to the text memory directly.

Code Name Keyboard Description
$08 BS Backspace Deletes the character to the left of the cursor, pulling characters in from the right.
$09 TAB TAB Moves the cursor right to the next tabulated column (every eight columns).
$0A LF N/A Puts the cursor at column 0 on the next line on the same column.
$0D CR RETURN Creates a newline on the screen. Puts the cursor at column 0 on the next line.
$0F INS Insert Inserts a blank space under the cursor (pushing characters to the right).
$11 UP Up Arrow Moves the cursor up one row.
$1D RIGHT Right Arrow Moves the cursor right one column. May wrap to the next line.
$7F DEL Delete Deletes the character under the cursor, pulling characters in from the right.
$91 DOWN Down Arrow Moves the cursor down one row. May scroll the screen.
$9D LEFT Left Arrow Moves the cursor left one column.

Special Kernel Variables

The kernel keeps track of several variables. Most kernel variables should be left alone by applications, but there are some which an application might want to set or read.

Start Address End Address Name R/W Description
$00:000C $00:000E SCREENBEGIN RO Address of the first character of the text display
$00:000C $00:000E SCREENBEGIN RO Address of the first character of the text display
$00:000F $00:0010 COLS_VISIBLE RW The number of columns currently displayed
$00:0013 $00:0014 LINES_VISIBLE RW The number of rows currently displayed
$00:0017 $00:0018 CURSORPOS RO The address of the text character under the cursor
$00:001A $00:001B CURSORX RO The address of the text character under the cursor
$00:001C $00:001D CURSORY RO The address of the text character under the cursor
$00:001E CURCOLOR RW The foreground and background colors to be used when printing via PUTC and PUTS
$00:001F $00:0020 COLORPOS RO The address of the text color cell under the cursor