KERNAL
KERNAL is Commodore's name for the ROM-resident operating system core in its 8-bit home computers; from the original PET of 1977, followed by the extended but strongly related versions used in its successors: the VIC-20, Commodore 64, Plus/4, C16, and C128.
Description
The Commodore 8-bit machines' KERNAL consists of the low-level, close-to-the-hardware OS routines roughly equivalent to the BIOS in IBM PC compatibles as well as higher-level, device-independent I/O functionality, and is user-callable via a jump table whose central part, for reasons of backwards compatibility, remains largely identical throughout the whole 8-bit series. The KERNAL ROM occupies the last 8 KB of the 8-bit CPU's 64 KB address space.The jump table can be modified to point to user-written routines, for example rewriting the screen display routines to display animated graphics or copying the character set into RAM. This use of a jump table was new to small computers at the time.
The Adventure International games published for the VIC-20 on cartridge are an example of software that uses the KERNAL. Because they only use the jump table, the games can be memory dumped to disk, loaded into a Commodore 64, and run without modification.
The KERNAL was initially written for the Commodore PET by John Feagans, who introduced the idea of separating the BASIC routines from the operating system. It was further developed by several people, notably Robert Russell, who added many of the features for the VIC-20 and the C64.
Example
A simple, yet characteristic, example of using the KERNAL is given by the following 6502 assembly language subroutine :CHROUT = $ffd2 ; CHROUT is the address of the character output routine
CR = $0d ; PETSCII code for Carriage Return
;
hello:
ldx #0 ; start with character 0 by loading 0 into the x index register
next:
lda message,x ; load byte from address message+x into the accumulator
beq done ; if the accumulator holds zero, we're done and want to branch out of the loop
jsr CHROUT ; call CHROUT to output char to current output device
inx ; increment x to move to the next character
bne next ; loop back while the last character is not zero
done:
rts ; return from subroutine
;
message:
.byte ""
.byte CR, 0 ; Carriage Return and zero marking end of string
This code stub employs the
CHROUT
routine, whose address is found at address $FFD2
, to send a text string to the default output device.The name
The KERNAL was known as kernel inside of Commodore since the PET days, but in 1980 Robert Russell misspelled the word as kernal in his notebooks. When Commodore technical writers Neil Harris and Andy Finkel collected Russell's notes and used them as the basis for the VIC-20 programmer's manual, the misspelling followed them along and stuck.According to early Commodore myth, and reported by writer/programmer Jim Butterfield among others, the "word" KERNAL is an acronym standing for Keyboard Entry Read, Network, And Link, which in fact makes good sense considering its role. Berkeley Softworks later used it when naming the core routines of its GUI OS for 8-bit home computers: the GEOS KERNAL.
On device-independent I/O
Surprisingly, the KERNAL implemented a device-independent I/O API not entirely dissimilar from that of Unix or Plan-9, which nobody actually exploited, as far as is publicly known. Whereas one could reasonably argue that "everything is a file" in these latter systems, others could easily claim that "everything is a GPIB-device" in the former.Due to limitations with the 6502 architecture at the time, opening an I/O channel requires three system calls. The first typically sets the logical filename through the
SETNAM
system call. The second call, SETLFS
, establishes the GPIB/IEEE-488 "device" address to communicate with. Finally OPEN
is called to perform the actual transaction. The application then used CHKIN
and CHKOUT
system calls to set the application's current input and output channels, respectively. Applications may have any number of concurrently open files. Thereafter, CHRIN
and CHROUT
prove useful for actually conducting input and output, respectively. CLOSE
then closes a channel.Observe that no system call exists to "create" an I/O channel, for devices cannot be created or destroyed dynamically under normal circumstances. Likewise, no means exists for seeking, nor for performing "I/O control" functions such as ioctl in Unix. Indeed, the KERNAL proves much closer to the Plan-9 philosophy here, where an application would open a special "command" channel to the indicated device to conduct such "meta" or "out-of-band" transactions. For example, to delete a file from a disk, the user typically will "open" the resource called
S0:THE-FILE-TO-RMV
on device 8 or 9, channel 15. Per established convention in the Commodore 8-bit world, channel 15 represents the "command channel" for peripherals, relying on message-passing techniques to communicate both commands and results, including exceptional cases. For example, in Commodore BASIC, they might find software not unlike the following:70...
80 REM ROTATE LOGS CURRENTLY OPENED ON LOGICAL CHANNEL #1.
90 CLOSE 1
100 OPEN 15,8,15,"R0:ERROR.1=0:ERROR.0":REM RENAME FILE ERROR.0 TO ERROR.1
110 INPUT# 15,A,B$,C,D:REM READ ERROR CHANNEL
120 CLOSE 15
130 IF A=0 THEN GOTO 200
140 PRINT "ERROR RENAMING LOG FILE:"
150 PRINT " CODE: "+A
160 PRINT " MSG : "+B$
170 END
200 REM CONTINUE PROCESSING HERE, CREATING NEW LOG FILE AS WE GO...
210 OPEN 1,8,1,"0:ERROR.0,S,W"
220...
Device numbers, per established documentation, are restricted to the range . However, this limitation came from the specific adaptation of the IEEE-488 protocol and, in effect, applies only to external peripherals. With all relevant KERNAL system calls vectored, programmers can intercept system calls to implement virtual devices with any address in the range of 32,256). Conceivably, one can load a device driver binary into memory, patch the KERNAL I/O vectors, and from that moment forward, a new device could be addressed. So far, this capability has never been publicly known as utilized, presumably for two reasons: The KERNAL provides no means for dynamically allocating device IDs, and the KERNAL provides no means for loading a relocatable [binary image. Thus, the burden of collisions both in I/O space and in memory space falls upon the user, while platform compatibility across a wide range of machines falls upon the software author. Nonetheless, support software for these functions could easily be implemented if desired.
Logical filename formats tends to depend upon the specific device addressed. The most common device used, of course, is the floppy disk system, which uses a format similar to
MD:NAME,ATTRS
, where M is a flag of sorts, D is the physical disk unit number, NAME
is a resource name up to 16 characters in length, and ATTRS
is an optional comma-separated list of attributes or flags. For example, if the user wants to overwrite a program file called PRGFILE
, they might see a filename like @0:PRGFILE,P
used in conjunction with device 8 or 9. Meanwhile, a filename for the RS-232 driver consists simply of four characters, encoded in binary format.Other devices, such as the keyboard, cassette, the display interface, and printer, require no filenames to function, either assuming reasonable defaults or simply not needing them at all.