One Kilobyte Operating System (OKOS)
Tiny OS for 1kB challenge.
Blurring the line between embedded system and general purpose computer. Inspired by all those wizardly tricks that brought computing to the masses on limited hardware.
OKOS is an operating system that allows the user to edit text or assembly source files code, assemble these into executables, and run their programs.
Features
- Runs on a PIC18f25k50, the MCU in the HaD SuperCon Badge!
- Display driver and API
- Supports SSD1306 OLED display with 128x64 pixels over i2c, with the font included that is 32x8 characters.
- Minimal 3x5 tiny font (remember original Apple ][ uppercase only font?)
- Line buffered text drawing api (write whole row at a time)
- Optional lowercase font, with descender support 3x5.5! But it takes 10 more bytes to support :(
- Check out git history for a full ascii compatible font + display driver in less than 400 bytes.
- Filesystem (if you can call it that)
- Actually just chopping 32k flash into 16 * 2k files.
- 15 files available to user numbered 1-F. Can be either text files or programs.
- Keyboard driver and api
- PS2 keyboard interface. You probably want an extended keyboard with a numeric keypad (more on that later).
- Minimal scan code map/table.
- Keyboard gives you repeat for free.
- Very basic command line interface lets you edit files, assemble them, and run programs
- Minimal instruction set assembler for the PIC MCU
- What kind of OS doesn't let you write code?!?
- Minimal, but workable, instruction set support.
- Writes executable machine code into another file. Not interpreted!
- Visual Text Editor
- Hopefully better than edlin
- Run
- User program takes over
- Core API available
- User program interrupts supported (ISR vector depends on what program is running)
- OKOS is just ok because OKOS is just OK.
One Kilobyte
Visual Text Editor
Edit a text file with e <file>
e.g. e 1
to edit the text file 1
.
The current line will have an arrow in the left-side gutter. Arrow keys are not supported, but instead the keypad 8 (up) and 2 (down) keys are used for navigating. You can scroll through the file with these arrow keys.
The cursor is always at the end of the line. You can delete with backspace (including over newlines), or type to add more characters to a line. Pressing enter will append a newline.
Save the file with the F1
key.
Assembler
Assemble a file with a <source> <out>
e.g. a 1 2
assembles source code in file 1
writing the executable to file 2
.
Minimal usable PIC instruction set.
3 character mnemonics to save space.
;
for comments, can be anywhere.
on a line alone to end the file/parsing. REQUIRED
User ISR starts at offset 0x04
or. This gives you enough room to jump away at the start of your program. Boilerplate program with an ISR:
bra 0012 ; jump past ISR
; insert your ISR code here
; ...
bra 000b ; execute a return
; at address 12. insert your code here
. ; EOF
User memory starts at address 0x3A, with the line buffer at 0x1A, anything before that is needed by the core API. If you don't use the API, feel free to reclaim this memory! Note that if you use ISRs, address 0x00 controls which file block the ISR jumps to.
Unfortunately, there wasn't enough room to support labels.
Instruction SetPIC instruction | short mnemonic | args | notes |
---|---|---|---|
bcf | bcf | f, b | 1 instruction |
bsf | bsf | f, b | 1 instruction |
btfsc | btc | f, b | 1 instruction |
btfss | bts | f, b | 1 instruction |
movlw | mvl | k | 1 instruction |
movwf | mvw | f | 1 instruction |
movf | mvf | f, d | 1 instruction |
addwf | add | f, d | 1 instruction |
andwf | and | f, d | 1 instruction |
iorwf | ior | f, d | 1 instruction |
xorwf | xor | f, d | 1 instruction |
rlcf | rlc | f, d | 1 instruction |
rrcf | rrc | f, d | 1 instruction |
goto | bra | k | 2 instructions target address is doubled. Think of them as instruction word addresses instead of byte addresses. File 1 starts at 0x400 |
call | cal | k | 2 instructions. Same addressing as goto |
return | - | - | NOT IMPLEMENTED Use bra b |
NOTE return is not implemented, but a return instruction is placed at address 0x16 (word 0xb), jumping there will effect a return.
Core APIThese API are available and potentially useful to user written programs.
Routine | address | cal address | Notes |
---|---|---|---|
return_bra | 0x000016 | 000B | Use this to return.... |
Nice feat :-)
In my view, assembler and editor could as well be binary programs loaded from the "file" area, which would free up some space for more OS features. Modern operating systems make a distinction between the use cases "development" and "normal use cases". You take a similar approach as Chuck Moore with OKAD and colorForth. Moore even blurred the line between source and binary in some occasions.