fox32rom: Rewrite event system

This commit is contained in:
Lua MacDougall 2022-04-16 14:59:19 -07:00 committed by Ry
parent 0c889859de
commit 3871ae4810
6 changed files with 142 additions and 163 deletions

263
event.asm
View File

@ -1,18 +1,12 @@
; event system routines ; event system routines
const EVENT_QUEUE_TOP: 0x01FFFFFE
const EVENT_QUEUE_BOTTOM: 0x01FFFBFE ; top - 0x400 (32 events * (4 bytes * (1 type + 7 parameters)))
const EVENT_QUEUE_INDEX: 0x01FFFFFF ; byte
const EVENT_QUEUE_SIZE: 32 ; 32 events
const EVENT_SIZE_BYTES: 32 ; 32 bytes per event
const EVENT_SIZE_WORDS: 8 ; 8 words per event
; event types ; event types
const MOUSE_CLICK_EVENT_TYPE: 0x00000000 const EVENT_TYPE_MOUSE_CLICK: 0x00000000
const MENU_BAR_CLICK_EVENT_TYPE: 0x00000001 const EVENT_TYPE_MOUSE_RELEASE: 0x00000001
const MENU_UPDATE_EVENT_TYPE: 0x00000002 const EVENT_TYPE_MENU_BAR_CLICK: 0x00000002
const MENU_CLICK_EVENT_TYPE: 0x00000003 const EVENT_TYPE_MENU_UPDATE: 0x00000003
const EMPTY_EVENT_TYPE: 0xFFFFFFFF const EVENT_TYPE_MENU_CLICK: 0x00000004
const EVENT_TYPE_EMPTY: 0xFFFFFFFF
; block until an event is available ; block until an event is available
; inputs: ; inputs:
@ -20,18 +14,7 @@ const EMPTY_EVENT_TYPE: 0xFFFFFFFF
; outputs: ; outputs:
; r0: event type ; r0: event type
; r1-r7: event parameters ; r1-r7: event parameters
wait_for_event: wait_for_event: jmp event_wait
ise
halt
; if the event queue index doesn't equal zero, then at least one event is available
cmp.8 [EVENT_QUEUE_INDEX], 0
ifz jmp wait_for_event
; an event is available in the event queue, remove it from the queue and return it
call get_next_event
ret
; add an event to the event queue ; add an event to the event queue
; inputs: ; inputs:
@ -39,25 +22,128 @@ wait_for_event:
; r1-r7: event parameters ; r1-r7: event parameters
; outputs: ; outputs:
; none ; none
new_event: new_event: jmp event_new
; ensure there is enough space left for another event
cmp.8 [EVENT_QUEUE_INDEX], EVENT_QUEUE_SIZE
ifz ret
; get the next event and remove it from the event queue
; inputs:
; none
; outputs:
; r0: event type
; r1-r7: event parameters
get_next_event: jmp event_next
; implementation
const EVENT_SIZE: 32
const EVENT_TEMP: 0x01FFFBDA
const EVENT_QUEUE_POINTER: 0x01FFFBFA
const EVENT_QUEUE_BOTTOM: 0x01FFFBFE
event_wait:
call event__init
event_wait_0:
ise
halt
cmp [EVENT_QUEUE_POINTER], EVENT_QUEUE_BOTTOM
ifz jmp event_wait_0
jmp event_next_0
event_next:
call event__init
cmp [EVENT_QUEUE_POINTER], EVENT_QUEUE_BOTTOM
ifz jmp event__empty
event_next_0:
icl
push r8 push r8
push r9 push r9
; point to the current event queue index
mov r8, EVENT_QUEUE_BOTTOM mov r8, EVENT_QUEUE_BOTTOM
movz.8 r9, [EVENT_QUEUE_INDEX] call event__load
mul r9, EVENT_SIZE_BYTES mov r8, EVENT_TEMP
add r8, r9 call event__store
; copy the event type mov r9, EVENT_QUEUE_BOTTOM
event_next_1:
add r9, EVENT_SIZE
cmp [EVENT_QUEUE_POINTER], r9
ifz jmp event_next_2
mov r8, r9
call event__load
mov r8, r9
sub r8, EVENT_SIZE
call event__store
jmp event_next_1
event_next_2:
mov r8, EVENT_TEMP
call event__load
sub [EVENT_QUEUE_POINTER], EVENT_SIZE
pop r9
pop r8
ise
ret
event_new:
call event__init
push r8
mov r8, [EVENT_QUEUE_POINTER]
call event__store
mov [EVENT_QUEUE_POINTER], r8
pop r8
ret
event__init:
cmp [EVENT_QUEUE_POINTER], 0
ifz mov [EVENT_QUEUE_POINTER], EVENT_QUEUE_BOTTOM
ret
event__empty:
mov r0, EVENT_TYPE_EMPTY
mov r1, 0
mov r2, 0
mov r3, 0
mov r4, 0
mov r5, 0
mov r6, 0
mov r7, 0
ret
event__load:
mov r0, [r8]
add r8, 4
mov r1, [r8]
add r8, 4
mov r2, [r8]
add r8, 4
mov r3, [r8]
add r8, 4
mov r4, [r8]
add r8, 4
mov r5, [r8]
add r8, 4
mov r6, [r8]
add r8, 4
mov r7, [r8]
add r8, 4
ret
event__store:
mov [r8], r0 mov [r8], r0
add r8, 4 add r8, 4
; copy the event parameters
mov [r8], r1 mov [r8], r1
add r8, 4 add r8, 4
mov [r8], r2 mov [r8], r2
@ -72,111 +158,4 @@ new_event:
add r8, 4 add r8, 4
mov [r8], r7 mov [r8], r7
add r8, 4 add r8, 4
; increment the index
inc.8 [EVENT_QUEUE_INDEX]
pop r9
pop r8
ret
; get the next event and remove it from the event queue
; inputs:
; none
; outputs:
; r0: event type
; r1-r7: event parameters
get_next_event:
; if the event queue index equals zero, then the queue is empty
cmp.8 [EVENT_QUEUE_INDEX], 0
ifz jmp get_next_event_empty
icl
push r8
; point to the bottom of the event queue
mov r8, EVENT_QUEUE_BOTTOM
; copy the event type
mov r0, [r8]
add r8, 4
; copy the event parameters
mov r1, [r8]
add r8, 4
mov r2, [r8]
add r8, 4
mov r3, [r8]
add r8, 4
mov r4, [r8]
add r8, 4
mov r5, [r8]
add r8, 4
mov r6, [r8]
add r8, 4
mov r7, [r8]
; shift all events down by one
call shift_events
; decrement the index
dec.8 [EVENT_QUEUE_INDEX]
pop r8
ise
ret
get_next_event_empty:
mov r0, EMPTY_EVENT_TYPE
mov r1, 0
mov r2, 0
mov r3, 0
mov r4, 0
mov r5, 0
mov r6, 0
mov r7, 0
ret
; shift all events down by one, overwriting the zero'th event
; inputs:
; none
; outputs:
; none
shift_events:
push r0
push r1
push r2
push r3
push r4
; for (int i = 0; i < (event_queue_index - 1); i++) {
; event_queue[i] = event_queue[i + 1];
; }
movz.8 r31, [EVENT_QUEUE_INDEX]
mov r3, 0 ; i
; source pointer: event_queue[i + 1]
mov r0, EVENT_QUEUE_BOTTOM
mov r4, r3
inc r4
mul r4, EVENT_SIZE_WORDS
add r0, r4
; destination pointer: event_queue[i]
mov r1, EVENT_QUEUE_BOTTOM
mov r4, r3
mul r4, EVENT_SIZE_WORDS
add r1, r4
; size: event_size_words
mov r2, EVENT_SIZE_WORDS
call copy_memory_words
pop r4
pop r3
pop r2
pop r1
pop r0
ret ret

View File

@ -59,8 +59,8 @@ compare_memory_bytes: jmp [0xF0046008]
compare_memory_words: jmp [0xF004600C] compare_memory_words: jmp [0xF004600C]
; event types ; event types
const MOUSE_CLICK_EVENT_TYPE: 0x00000000 const EVENT_TYPE_MOUSE_CLICK: 0x00000000
const MENU_BAR_CLICK_EVENT_TYPE: 0x00000001 const EVENT_TYPE_MENU_BAR_CLICK: 0x00000001
const MENU_UPDATE_EVENT_TYPE: 0x00000002 const EVENT_TYPE_MENU_UPDATE: 0x00000002
const MENU_CLICK_EVENT_TYPE: 0x00000003 const EVENT_TYPE_MENU_CLICK: 0x00000003
const EMPTY_EVENT_TYPE: 0xFFFFFFFF const EVENT_TYPE_EMPTY: 0xFFFFFFFF

View File

@ -74,20 +74,20 @@ event_loop:
call get_next_event call get_next_event
; was the mouse clicked? ; was the mouse clicked?
cmp r0, MOUSE_CLICK_EVENT_TYPE cmp r0, EVENT_TYPE_MOUSE_CLICK
;ifz call mouse_click_event ;ifz call mouse_click_event
; did the user click the menu bar? ; did the user click the menu bar?
cmp r0, MENU_BAR_CLICK_EVENT_TYPE cmp r0, EVENT_TYPE_MENU_BAR_CLICK
ifz mov r0, menu_items_root ifz mov r0, menu_items_root
ifz call menu_bar_click_event ifz call menu_bar_click_event
; is the user in a menu? ; is the user in a menu?
cmp r0, MENU_UPDATE_EVENT_TYPE cmp r0, EVENT_TYPE_MENU_UPDATE
ifz call menu_update_event ifz call menu_update_event
; did the user click a menu item? ; did the user click a menu item?
cmp r0, MENU_CLICK_EVENT_TYPE cmp r0, EVENT_TYPE_MENU_CLICK
ifz call menu_click_event ifz call menu_click_event
; check if a disk is inserted as disk 0 ; check if a disk is inserted as disk 0

View File

@ -173,7 +173,7 @@ close_menu:
; update the currently open menu ; update the currently open menu
; detects mouse movements over the menu and handles clicks ; detects mouse movements over the menu and handles clicks
; this should only be called if a menu_update_event_type event is received ; this should only be called if a event_type_menu_update event is received
; inputs: ; inputs:
; *** these inputs should already be in the required registers from the event parameters *** ; *** these inputs should already be in the required registers from the event parameters ***
; r1: pointer to menu bar root struct ; r1: pointer to menu bar root struct
@ -242,13 +242,13 @@ menu_update_event_clicked:
mov r5, 0 mov r5, 0
mov r6, 0 mov r6, 0
mov r7, 0 mov r7, 0
mov r0, MENU_CLICK_EVENT_TYPE mov r0, EVENT_TYPE_MENU_CLICK
call new_event call new_event
mov r0, r1 mov r0, r1
call close_menu call close_menu
jmp menu_update_event_end_no_add jmp menu_update_event_end_no_add
menu_update_event_end_add: menu_update_event_end_add:
; readd the menu_update_event_type event to the event queue ; readd the event_type_menu_update event to the event queue
mov r1, r8 ; event parameter 0: pointer to menu bar root struct mov r1, r8 ; event parameter 0: pointer to menu bar root struct
mov r2, r9 ; event parameter 1: selected root menu item mov r2, r9 ; event parameter 1: selected root menu item
mov r3, r10 ; event parameter 2: hovering menu item (or 0xFFFFFFFF for none) mov r3, r10 ; event parameter 2: hovering menu item (or 0xFFFFFFFF for none)
@ -256,7 +256,7 @@ menu_update_event_end_add:
mov r5, 0 mov r5, 0
mov r6, 0 mov r6, 0
mov r7, 0 mov r7, 0
mov r0, MENU_UPDATE_EVENT_TYPE mov r0, EVENT_TYPE_MENU_UPDATE
call new_event call new_event
menu_update_event_end_no_add: menu_update_event_end_no_add:
pop r9 pop r9

View File

@ -218,7 +218,7 @@ menu_bar_click_event_found_item:
call draw_menu_bar_root_items call draw_menu_bar_root_items
call draw_menu_items call draw_menu_items
; add a menu_update_event_type event to the event queue ; add a event_type_menu_update event to the event queue
mov r1, r0 ; event parameter 0: pointer to menu bar root struct mov r1, r0 ; event parameter 0: pointer to menu bar root struct
mov r2, r30 ; event parameter 1: selected root menu item mov r2, r30 ; event parameter 1: selected root menu item
mov r3, 0xFFFFFFFF ; event parameter 2: hovering menu item (or 0xFFFFFFFF for none) mov r3, 0xFFFFFFFF ; event parameter 2: hovering menu item (or 0xFFFFFFFF for none)
@ -226,7 +226,7 @@ menu_bar_click_event_found_item:
mov r5, 0 mov r5, 0
mov r6, 0 mov r6, 0
mov r7, 0 mov r7, 0
mov r0, MENU_UPDATE_EVENT_TYPE mov r0, EVENT_TYPE_MENU_UPDATE
call new_event call new_event
menu_bar_click_event_end: menu_bar_click_event_end:
pop r31 pop r31

View File

@ -25,7 +25,7 @@ get_mouse_button:
ret ret
; updates the cursor position and adds a mouse_click_event_type event to the event queue if the mouse button was clicked ; updates the cursor position and adds a event_type_mouse_click event to the event queue if the mouse button was clicked
; this should only be called by system_vsync_handler ; this should only be called by system_vsync_handler
mouse_update: mouse_update:
push r0 push r0
@ -84,7 +84,7 @@ mouse_update_no_menu:
mov r5, 0 mov r5, 0
mov r6, 0 mov r6, 0
mov r7, 0 mov r7, 0
mov r0, MOUSE_CLICK_EVENT_TYPE ; set event type to mouse type mov r0, EVENT_TYPE_MOUSE_CLICK ; set event type to mouse type
call new_event call new_event
jmp mouse_update_end jmp mouse_update_end
mouse_update_menu_was_clicked: mouse_update_menu_was_clicked:
@ -95,7 +95,7 @@ mouse_update_menu_was_clicked:
mov r5, 0 mov r5, 0
mov r6, 0 mov r6, 0
mov r7, 0 mov r7, 0
mov r0, MENU_BAR_CLICK_EVENT_TYPE ; set event type to menu bar click type mov r0, EVENT_TYPE_MENU_BAR_CLICK ; set event type to menu bar click type
call new_event call new_event
mouse_update_end: mouse_update_end:
pop r7 pop r7