undar-lang/test/window.ul.ir

92 lines
2.6 KiB
Plaintext

global const str screen_namespace = "/dev/screen/0"
global const str mouse_namespace = "/dev/mouse/0"
global const str terminal_namespace = "/dev/term/0"
global const str new_line = "\n"
global const byte WHITE = 255
function main ()
# Open screen
# use load immediate because it is a pointer to a string, not a value
plex screen is $0
plex mouse is $1
str tmp_str is $2
byte color is $3
byte left_down is $4
int mode is $5
nat offset_temp is $6
nat x is $7
nat y is $8
nat width is $9
nat screen_buffer is $10
nat buffer_size is $11
nat pixel_pos is $12
load_address &screen_namespace -> tmp_str
load_immediate 0 -> mode
syscall OPEN tmp_str mode -> screen # openout Plex screen, in namespace, in flags
nat_to_string screen -> tmp_str
call &pln tmp_str
load_offset_32 screen 8 -> width # load width
nat_to_string width -> tmp_str
call &pln tmp_str
load_offset_32 screen 12 -> buffer_size # load size
nat_to_string buffer_size -> tmp_str
call &pln tmp_str
load_immediate 16 -> offset_temp # offset for screen buffer
add_nat screen offset_temp -> screen_buffer
nat_to_string screen_buffer -> tmp_str
call &pln tmp_str
# open mouse
load_address &mouse_namespace -> tmp_str
syscall OPEN tmp_str mode -> mouse # openout Plex mouse, in namespace, in flags
syscall WRITE screen screen_buffer buffer_size # redraw
draw_loop:
# load mouse click data
syscall STAT mouse
load_offset_8 mouse 16 -> left_down # load btn1 pressed
jump_eq_nat &draw_loop left_down mode # mode is 0 which is an alias for false
load_offset_32 mouse 8 -> x # load x
load_offset_32 mouse 12 -> y # load y
# Compute start address: y*width + x
mul_nat y width -> pixel_pos # = y * width
add_nat x pixel_pos -> pixel_pos # += x
add_nat screen_buffer pixel_pos -> pixel_pos # += pixel_offset
load_immediate 4 -> fat_ptr_size # need to add offset for fat pointer size
add_nat pixel_pos fat_ptr_size -> pixel_pos
load_absolute_32 &WHITE -> color
store_absolute_8 pixel_pos color # draw color at screen [x,y]
syscall WRITE screen screen_buffer buffer_size # redraw
jump &draw_loop
exit 0
function pln (str message is $0)
str term is $1
int msg_length is $2
str nl is $3
int nl_length is $4
int mode is $5
load_address &terminal_namespace -> term # get terminal device
load_immediate 0 -> mode
syscall OPEN term mode -> term
strlen message -> msg_length
syscall WRITE term message msg_length
load_address &new_line -> nl
strlen nl -> nl_length
syscall WRITE term nl nl_length
return