Commits

Cliff Biffle committed 976ddd7

Abstracted the KERNAL's calling convention and user context access through a set of macros. This will make it easier to change the calling convention, which I think I'm about to do.

  • Participants
  • Parent commits dbbe8c4

Comments (0)

Files changed (1)

 
 .equ kvar_vt_autoscroll, 0x20080A00
 
+@ Structure of exception frame, so that we can get to parameters/results.
+.equ F_UR0,  0x00
+.equ F_UR1,  0x04
+.equ F_UR2,  0x08
+.equ F_UR3,  0x0C
+.equ F_UR12, 0x10
+.equ F_UR14, 0x14
+.equ F_UR15, 0x18
+.equ F_UPSR, 0x1C
+
+.equ F_US0,  0x20
+.equ F_US1,  0x24
+.equ F_US2,  0x28
+.equ F_US3,  0x2C
+@ Can add more if we need them.
+
+@ Abstraction on the calling convention:
+.equ UV0,  F_UR0    @ Syscall ID / first result.
+.equ UV1,  F_US0    @ First parameter / second result.
+.equ UV2,  F_US1    @ Second parameter / third result.
+.equ UV3,  F_US2    @ Third parameter / fourth result.
+
+.equ USID, UV0      @ Syscall ID
+.equ UA0,  UV1      @ First argument.
+.equ UA1,  UV2      @ Second argument.
+.equ UA2,  UV3      @ Third argument.
+
+.equ SR0,  UV0      @ First result.
+.equ SR1,  UV1      @ Second result.
+.equ SR2,  UV2      @ Third result.
+.equ SR3,  UV3      @ Fourth result.
+
+
 .section .text.kernal
 
 .globl _start
           svc 0
           b main              @ Invoke program.
 
+@ Global SVC dispatcher, chooses between subsystems based on User's r0[15:8],
+@ which can be found on the Process stack.
 .equ last_sysid, 4
 .globl k_svc_dispatcher
 .thumb_func
 k_svc_dispatcher:
           mrs r12, PSP        @ Load user stack pointer into r12 for use later.
-          ldr r0, [r12]       @ Load the user's r0 back into r0.
-          uxtb r1, r0, ROR #8 @ Extract byte 1 as the subsystem ID.
+          ldr r0, [r12, USID] @ Load the syscall ID into r0.
+          uxtb r1, r0, ROR #8 @ Extract byte 1 as the subsystem ID in r1.
           cmp r1, #last_sysid @ Bounds-check it.
           bhi _halt           @ Explode if it's wrong.
 
           BH_ENTRY k_sd_dispatch        @ Sys 3: SD card
           BH_ENTRY k_bb_dispatch        @ Sys 4: block buffers.
 
+
 @
-@ System
+@ Subsystem 0: System
 @
 .equ last_system_call, 5
 k_system_dispatch:
           @ Thus, we double its value before returning it.
           mov32 r0, LPC_T0TC  @ Address Timer 0's count register.
           ldr r0, [r0]        @ Load the current count.
-          str r0, [r12]       @ Stuff it into user's r0.
+          str r0, [r12, SR0]  @ Save as result.
           bx LR 
 
 @ Syscall 0.2: Read Real-Time Clock.
           adds r0, r1                   @ r0 = seconds in epoch.
 
           @ Write everything back to user r0/stack.
-          str r0, [r12]                 @ Epoch time in user's r0.
+          str r0, [r12, SR0]            @ Epoch time as first result.
           bic r4, #0xFF000000           @ Clear top byte of time (day-of-week).
-          str r4, [r12, #32]            @ Store time into SP+0.
+          str r4, [r12, SR1]            @ Wall time as second result.
           add r5, #(2000 << 16)         @ Correct year to Gregorian year.
-          str r5, [r12, #36]            @ Store Gregorian date into SP+4.
+          str r5, [r12, SR2]            @ Gregorian date as third result.
 
           pop {r4, r5, r6, PC}
 
 k_system_set_rtc:
           mov32 r1, LPC_RTCTCG          @ Address base of timer/counter group.
 
-          ldr r0, [r12, #32]  @ Load wall time from user's stack frame.
+          ldr r0, [r12, UA0]  @ Load wall time from first parameter.
 
           ubfx r2, r0, #0, #8 @ Extract seconds into r2.
           str r2, [r1, #LPC_RTCTCG_to_SEC]
           ubfx r2, r0, #16, #8@ Extract hours into r2.
           str r2, [r1, #LPC_RTCTCG_to_HOUR]
 
-          ldr r0, [r12, #36]  @ Load calendar date from user's stack frame.
+          ldr r0, [r12, UA1]  @ Load calendar date from second parameter.
 
           ubfx r2, r0, #16, #16 @ Extract year into r2.
           tst r2, #0b11       @ Set Z for leap year.
 k_serial_recv:
           push {LR}           @ Save EXC_RETURN value.
           bl uart0_recv       @ Read a character into r0.
-          str r0, [r12]       @ Store it into user's r0.
+          str r0, [r12, SR0]  @ Store it as first result.
           pop {PC}            @ Return.
 
 @ Syscall 1.2: Send Byte on Serial Port.
 @  -00  Character to send, padded out to 32 bits.
 @ No outputs.
 k_serial_send:
-          ldr r0, [r12, #32]  @ Recover the user's character from the stack.
+          ldr r0, [r12, UA0]  @ Load character from first argument.
           b uart0_send        @ Tail-call the send function.
 
 @ Syscall 1.0: Initialize Serial Port.
 @  -00  Flag: when 0, autoscroll is disabled.  Otherwise, autoscroll is enabled.
 @ No outputs.
 k_vt_autoscroll:
-          ldr r0, [r12, #32]  @ Load flag from user's stack.
+          ldr r0, [r12, UA0]            @ Load flag from first argument.
           mov32 r1, kvar_vt_autoscroll  @ Address the control variable.
-          strb r0, [r1]       @ Store the flag.
+          strb r0, [r1]                 @ Store the flag.
           bx LR
 
 
 @ N.B. this definition *must* come before the others so that the branch
 @ table consists of only forward branches.
 k_vt_type_cooked:
-          ldr r0, [r12, #32]  @ Load character from user's stack.
+          ldr r0, [r12, UA0]  @ Load character from first argument.
 
           cmp r0, #16         @ Is it a control character?
           bhs k_vt_type_raw   @ If not, type it.
 @  -00  Character to type, padded out to 32 bits.
 @ No outputs.
 k_vt_type_raw:
-          ldr r0, [r12, #32]  @ Load character from user's stack.
+          ldr r0, [r12, UA0]  @ Load character from first argument.
           @ Fall through
 
 .globl _vt_type_r0  @ Proving useful in other places
 @
 @ Out-of-range columns or rows will produce undefined (but safe) behavior.
 k_vt_place_cursor:
-          ldr r0, [r12, #32]  @ Load column number from user's stack.
-          ldr r1, [r12, #36]  @ Load row number from user's stack.
+          ldr r0, [r12, UA0]  @ Load column number from first argument.
+          ldr r1, [r12, UA1]  @ Load row number from second argument.
 
           @ Bound the column: 0..79
           cmp r0, #79
 @  -00  Set bit 0 to hide cursor, clear to show cursor.
 @ No outputs.
 k_vt_cursor_ctl:
-          ldr r0, [r12, #32]  @ Load cursor flag from user's stack.
+          ldr r0, [r12, UA0]  @ Load cursor flag from first argument.
           mov32 r1, vga_base  @ Get VGA driver state base.
           str r0, [r1, #vga_to_state]
           bx LR
           push {LR}
           bl spi_init
           bl sd_init
-          str r0, [r12]       @ Return flag in user's r0.
+          str r0, [r12, SR0]  @ Return flag as first result.
           pop {PC}
 
 k_sd_read:
           push {LR}
-          ldr r1, [r12, #32]  @ Load destination address from user stack.
-          ldr r0, [r12, #36]  @ Load block number from user stack.
+          ldr r1, [r12, UA0]  @ Load destination address from first argument.
+          ldr r0, [r12, UA1]  @ Load block number from second argument.
           bl sd_read_block    @ Do the read.
-          str r0, [r12]       @ Return result in user's r0.
+          str r0, [r12, SR0]  @ Return result as first result.
           pop {PC}
 
 k_sd_write:
           push {LR}
-          ldr r1, [r12, #32]  @ Load source address from user stack.
-          ldr r0, [r12, #36]  @ Load block number from user stack.
+          ldr r1, [r12, UA0]  @ Load source address from first argument.
+          ldr r0, [r12, UA1]  @ Load block number from second argument
           bl sd_write_block   @ Do the write.
-          str r0, [r12]       @ Return result in user's r0.
+          str r0, [r12, SR0]  @ Return result as first result.
           pop {PC}
 
 .equ last_bb_call, 2
 
 k_bb_find:
           push {LR}
-          ldr r0, [r12, #32]  @ Load slot index from stack.
-          ldr r1, [r12, #36]  @ Load block index from stack.
+          ldr r0, [r12, UA0]  @ Load slot index from first argument.
+          ldr r1, [r12, UA1]  @ Load block index from second argument.
           bl bb_free1
-          str r0, [r12, #32]  @ Store success code back.
-          str r1, [r12, #36]  @ Store buffer index back.
+          str r0, [r12, SR1]  @ Store success code back.  TODO: weird
+          str r1, [r12, SR2]  @ Store buffer index back.
           pop {PC}
 
 k_bb_load:
           push {LR}
-          ldr r0, [r12, #32]  @ Load slot index from stack.
-          ldr r1, [r12, #36]  @ Load block index from stack.
+          ldr r0, [r12, UA0]  @ Load slot index from first argument.
+          ldr r1, [r12, UA1]  @ Load block index from second argument.
           bl bb_read
-          str r0, [r12, #32]  @ Store success code back.
-          str r1, [r12, #36]  @ Store buffer address back.
+          str r0, [r12, SR1]  @ Store success code back.  TODO: weird
+          str r1, [r12, SR2]  @ Store buffer address back.
           pop {PC}
 
 _halt:    HALT