Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //MachinimaOS task switcher
- #include <stdint.h>
- #include <stdarg.h>
- #include <stddef.h>
- #include "modsys.h"
- #include "minihal.h"
- #include "config.h"
- #include "taskswitcher.h"
- //DEBUGGING ONLY
- //#include "kernel.h"
- //#include "16c550driver.h"
- ktask_t tasks[MAX_TASKS];
- uint8_t current_task;
- uint8_t is_first_switch;
- uint8_t task_switcher_enabled = 0;
- void taskswitcher_init() {
- uint16_t i;
- hal_start_atomic();
- for (i = 0; i < MAX_TASKS; i++) {
- tasks[i].sp = NULL;
- }
- current_task = 0;
- is_first_switch = 1;
- hal_end_atomic();
- modsys_register_raw_isr(HAL_INTCTRL_SLOT, &timer_isr);
- }
- void timer_isr() __naked {
- //We may freely destroy any register. All registers have
- //been saved, except for the alternate register bank.
- //We may not push/pop anything.
- __asm
- ;; Interrupt acknowledge (timer)
- LD B, #HAL_INTCTRL_SLOT
- LD C, #HAL_INTCTRL_TIMER_ACK_REGISTER
- LD A, #0
- OUT (C),A
- ;; If the task switcher is disabled, cancel switching
- LD A, (#_task_switcher_enabled)
- OR A
- RET Z
- ;; Get current ktask_t pointer into IY
- LD H, #0
- LD A, (#_current_task)
- LD L, A
- ;; Multiply by 8
- ADD HL,HL
- ADD HL,HL
- ADD HL,HL
- EX DE,HL
- LD IY, #_tasks
- ADD IY,DE
- ;; If this is the first run of the task switcher
- ;; or the task does not exist anymore, dont save
- ;; the old context
- LD A,(#_is_first_switch)
- CP A,#0
- JP NZ,first_switch
- LD A, 3 (IY)
- OR A, 4 (IY)
- JP Z,first_switch
- ;; Save the current page config and stack pointer
- LD IX, #_currentPage
- LD A, 1 (IX)
- LD 0 (IY), A
- LD A, 2 (IX)
- LD 1 (IY), A
- LD A, 3 (IX)
- LD 2 (IY), A
- LD HL,#0
- ADD HL,SP
- LD 3 (IY), L
- LD 4 (IY), H
- ;; Make IY point to the next task and set current_task accordingly
- LD A,(#_current_task)
- INC A
- LD (#_current_task),A
- LD BC,#8
- ADD IY,BC
- first_switch:
- ;; Find next task
- ;; If there is no task running anymore, shut down the system.
- LD E,#0
- find_next_task_loop:
- LD A,(#_current_task)
- CP A,#MAX_TASKS
- JP C,find_next_task_loop_noreset
- LD A,#0
- LD (#_current_task),A
- LD IY,#_tasks
- INC E
- LD A,E
- CP A,#3
- JP Z,_shutdown
- find_next_task_loop_noreset:
- LD A,3 (IY)
- OR A,4 (IY)
- JP NZ,found_next_task
- LD A,(#_current_task)
- INC A
- LD (#_current_task),A
- LD BC,#8
- ADD IY,BC
- JP find_next_task_loop
- found_next_task:
- ;; Restore page config
- LD B,#HAL_MEM_MAPPER_SLOT
- LD IX,#_currentPage
- LD A,0 (IY)
- LD 1 (IX), A
- LD C,#1
- OUT (C),A
- LD A,1 (IY)
- LD 2 (IX), A
- LD C,#2
- OUT (C),A
- LD A,2 (IY)
- LD 3 (IX), A
- LD C,#3
- OUT (C),A
- ;; Restore stack pointer
- LD L,3 (IY)
- LD H,4 (IY)
- LD SP,HL
- LD A,#0
- LD (#_is_first_switch),A
- RET
- __endasm;
- }
- void yield() __naked {
- __asm
- DI
- PUSH AF
- PUSH BC
- PUSH DE
- PUSH HL
- PUSH IX
- PUSH IY
- CALL _timer_isr
- POP IY
- POP IX
- POP HL
- POP DE
- POP BC
- POP AF
- EI
- RET
- __endasm;
- }
Advertisement
Add Comment
Please, Sign In to add comment