Skip to content

Instantly share code, notes, and snippets.

@WideWord
Created July 24, 2012 19:48
Show Gist options
  • Save WideWord/3172206 to your computer and use it in GitHub Desktop.
Save WideWord/3172206 to your computer and use it in GitHub Desktop.
set pc, kernel_start
:clock
dat 0xffff
:get_hardware; () -> void
hwn i
.loop
sub i, 1
hwq i
ife a, 0xb402
set [clock], i
ifn i, 0
set pc, loop
set pc, pop
:kernel_start
ias interrupt_handler
jsr get_hardware
ife [clock], 0xffff
sub pc, 1
; setup clock
set a, 0
set b, 1
hwi [clock] ; 60 times per second
set a, main
jsr process_create
set b, [current_pid]
mul b, 3
add b, process_table
set sp, [b+1]
set ex, 0
set a, clock_interrupt
jsr interrupt_address_alloc
set b, a
set a, 2
hwi [clock] ; clock interrupt
set pc, main
:main
sub pc, 1
:interrupt_address
dat 0, 0, 0, 0, 0, 0, 0, 0
dat 0, 0, 0, 0, 0, 0, 0, 0
dat 0, 0, 0, 0, 0, 0, 0, 0
dat 0, 0, 0, 0, 0, 0, 0, 0
dat 0, 0, 0, 0, 0, 0, 0, 0
dat 0, 0, 0, 0, 0, 0, 0, 0
dat 0, 0, 0, 0, 0, 0, 0, 0
dat 0, 0, 0, 0, 0, 0, 0, 0
:interrupt_address_alloc; (ptr) -> num
set b, 1
set c, interrupt_address
add c, 1
.loop
ife [c], 0
set pc, return
add b, 1
add c, 1
ife b, 64
set pc, fail
set pc, loop
.return
set [c], a
set a, b
set pc, pop
.fail
set a, 0xffff
set pc, pop
:interrupt_address_free; (num) -> void
set b, interrupt_address
add b, a
set [b], 0
set pc, pop
:interrupt_handler; don't call this
add a, interrupt_address
set push, b
set push, c
set push, x
set push, y
set push, z
set push, j
set push, i
jsr [a]
:interrupt_handler_return
set i, pop
set j, pop
set z, pop
set y, pop
set x, pop
set c, pop
set b, pop
rfi 0
:process_table
;isproc sp ex
dat 0, 0, 0
dat 0, 0, 0
dat 0, 0, 0
dat 0, 0, 0
dat 0, 0, 0
dat 0, 0, 0
dat 0, 0, 0
dat 0, 0, 0
:process_create; (ptr to code) -> pid
set [mutex], 1 ; work with process table need mutex lock
set b, process_table
set c, 0
.loop
ife [b], 0
set pc, .return
add b, 3
add c, 1
ife c, 8
set pc, fail
set pc, loop
.return
set [b], 1
set x, c
mul x, 0x400 ; 1024 words stack size for every process
add x, 0xe000
add x, 0x390
set [b+1], x
set [b+2], 0
set [x+9], a ; pointer to code in stack
set [x], interrupt_handler_return ; need address to return to interrupt_handler
set a, c
set [mutex], 0
set pc, pop
.fail
set a, 0xffff
set [mutex], 0
set pc, pop
:mutex
dat 0
:current_pid
dat 0
:clock_interrupt
ifn [mutex], 0
set pc, pop
set b, current_pid
mul b, 3
add b, process_table
set [b+2], ex
set [b+1], sp
.next_pid
add [current_pid], 1
ife [current_pid], 8
set [current_pid], 0
set b, [current_pid]
mul b, 3
add b, process_table
ife [b], 0
set pc, .next_pid
set ex, [b+2]
set sp, [b+1]
set pc, pop
:exit_process
set [mutex], 1
set [current_pid], 0
set [mutex], 0
sub pc, 1
:main
set a, process1
jsr process_create
set a, process2
jsr process_create
jsr process_exit; exit from main process
:process1
set [mutex], 1; lock
sub [data], 1
set [mutex], 0; unlock
set pc, process1; loop
:process2
set [mutex], 1;lock
add [data], 1
set [mutex], 0;unlock
set pc, process2; loop
:data
dat 0x5000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment