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

One Kilobyte

Code usage by category

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.

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 Set
PIC instructionshort mnemonicargsnotes
bcfbcff, b1 instruction
bsfbsff, b1 instruction
btfscbtcf, b1 instruction
btfssbtsf, b1 instruction
movlwmvlk1 instruction
movwfmvwf1 instruction
movfmvff, d1 instruction
addwfaddf, d1 instruction
andwfandf, d1 instruction
iorwfiorf, d1 instruction
xorwfxorf, d1 instruction
rlcfrlcf, d1 instruction
rrcfrrcf, d1 instruction
gotobrak2 instructions
target address is doubled. Think of them as instruction word addresses instead of byte addresses. File 1 starts at 0x400
callcalk2 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 API

These API are available and potentially useful to user written programs.

Routineaddresscal addressNotes
return_bra0x000016000BUse this to return.
oledDrawFlushLine0x00012c0096write up to 32 characters to 0x1A. Filled with spaces on return.
readKey0x00015c00AEWaits for keypress, returns character in WREG, also in 0x03. Blocks until key is pressed. You probably want to write something better in an ISR.
openFile0x0002d20169Sets up TBLPTR for reading/writing to the given file in WREG
fputc0x000294014AWrites a byte in WREG, flushing pages as needed
flushLastPage0x0002a20151If bit 0 in memory 0x01 is set, you need to call this once to flush the last page, since pages are only flushed by fputc in 64b increments.

Running Programs

Once you've written and assembled an executable, you can run with with r <file> e.g. r 2 to run file 2.

Your program takes over until the computer is reset.

Hardware

OKOS was written on and designed to run on the PIC MCU in the HaD SuperCon badge. The badge exposes a few GPIO, with i2c support. A small bit of interface hardware is required for i2c, and to level shift the 5v PS2 keyboard signals. This was assembled on a home made toner transfer PCB.

Schematic diagram

Notes

Character set

Supports 0-9, a-z, period, semicolon, space, newline, esc, F1, backspace, Keypad 2,8,6

Maps directly to hex, such that 0x0 => '0', 0xa => 'A'.

keyboard

Supports PS2 keyboards, decode scancodes into character set.

filesystemdisplayMCU

Microchip PIC18f25k50

1K Mapfile evidence

Here's the mapfile section info. Note that configuraton fuses are located at 0x300000 and are not actually program flash, the programm usage calculation is flawed! Program memory ends (and includes) address 0x0003fd - 1022 bytes. Also.cinit is a bug/feature of MPLAB that could be removed with a custom linker file.

MPLINK 5.08, LINKER
Linker Map File - Created Sun Dec  4 17:28:46 2016

                                 Section Info
                  Section       Type    Address   Location Size(Bytes)
                ---------  ---------  ---------  ---------  ---------
                 RES_VECT       code   0x000000    program   0x000008
                  HIGHISR       code   0x000008    program   0x000010
                   LOWISR       code   0x000018    program   0x000004
               TABLE_DATA       code   0x00001c    program   0x0000ba
                   .cinit    romdata   0x0000d6    program   0x000002
                MAIN_PROG       code   0x0000d8    program   0x000326
.config_300000_BUILD/DEFAULT/PRODUCTION/MAIN.O    romdata   0x300000    program   0x000001
.config_300001_BUILD/DEFAULT/PRODUCTION/MAIN.O    romdata   0x300001    program   0x000001
.config_300002_BUILD/DEFAULT/PRODUCTION/MAIN.O    romdata   0x300002    program   0x000001
.config_300003_BUILD/DEFAULT/PRODUCTION/MAIN.O    romdata   0x300003    program   0x000001
.config_300005_BUILD/DEFAULT/PRODUCTION/MAIN.O    romdata   0x300005    program   0x000001
.config_300006_BUILD/DEFAULT/PRODUCTION/MAIN.O    romdata   0x300006    program   0x000001
.config_300008_BUILD/DEFAULT/PRODUCTION/MAIN.O    romdata   0x300008    program   0x000001
.config_300009_BUILD/DEFAULT/PRODUCTION/MAIN.O    romdata   0x300009    program   0x000001
.config_30000A_BUILD/DEFAULT/PRODUCTION/MAIN.O    romdata   0x30000a    program   0x000001
.config_30000B_BUILD/DEFAULT/PRODUCTION/MAIN.O    romdata   0x30000b    program   0x000001
.config_30000C_BUILD/DEFAULT/PRODUCTION/MAIN.O    romdata   0x30000c    program   0x000001
.config_30000D_BUILD/DEFAULT/PRODUCTION/MAIN.O    romdata   0x30000d    program   0x000001
               .udata_acs      udata   0x000000       data   0x00003c
                              Program Memory Usage 
                               Start         End      
                           ---------   ---------      
                            0x000000    0x0003fd      
                            0x300000    0x300003      
                            0x300005    0x300006      
                            0x300008    0x30000d      
            1034 out of 33048 program addresses used, program memory utilization is 3%