Guest User

Untitled

a guest
Jun 9th, 2016
5
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.01 KB | None | 0 0
  1. //MachinimaOS task switcher
  2.  
  3. #include <stdint.h>
  4. #include <stdarg.h>
  5. #include <stddef.h>
  6. #include "modsys.h"
  7. #include "minihal.h"
  8. #include "config.h"
  9. #include "taskswitcher.h"
  10.  
  11. //DEBUGGING ONLY
  12. //#include "kernel.h"
  13. //#include "16c550driver.h"
  14.  
  15.  
  16. ktask_t tasks[MAX_TASKS];
  17.  
  18. uint8_t current_task;
  19. uint8_t is_first_switch;
  20. uint8_t task_switcher_enabled = 0;
  21.  
  22. void taskswitcher_init() {
  23.     uint16_t i;
  24.    
  25.     hal_start_atomic();
  26.    
  27.     for (i = 0; i < MAX_TASKS; i++) {
  28.         tasks[i].sp = NULL;
  29.     }
  30.    
  31.     current_task = 0;
  32.     is_first_switch = 1;
  33.    
  34.     hal_end_atomic();
  35.    
  36.     modsys_register_raw_isr(HAL_INTCTRL_SLOT, &timer_isr);
  37. }
  38.  
  39. void timer_isr() __naked {
  40.     //We may freely destroy any register. All registers have
  41.     //been saved, except for the alternate register bank.
  42.     //We may not push/pop anything.
  43.     __asm
  44.         ;; Interrupt acknowledge (timer)
  45.         LD  B, #HAL_INTCTRL_SLOT
  46.         LD  C, #HAL_INTCTRL_TIMER_ACK_REGISTER
  47.         LD  A, #0
  48.         OUT (C),A
  49.        
  50.         ;; If the task switcher is disabled, cancel switching
  51.         LD  A, (#_task_switcher_enabled)
  52.         OR  A
  53.         RET Z
  54.        
  55.         ;; Get current ktask_t pointer into IY
  56.         LD  H, #0
  57.         LD  A, (#_current_task)
  58.         LD  L, A
  59.         ;; Multiply by 8
  60.         ADD HL,HL
  61.         ADD HL,HL
  62.         ADD HL,HL
  63.         EX  DE,HL
  64.         LD  IY, #_tasks
  65.         ADD IY,DE
  66.        
  67.         ;; If this is the first run of the task switcher
  68.         ;; or the task does not exist anymore, dont save
  69.         ;; the old context
  70.         LD  A,(#_is_first_switch)
  71.         CP  A,#0
  72.         JP  NZ,first_switch
  73.         LD  A, 3 (IY)
  74.         OR  A, 4 (IY)
  75.         JP  Z,first_switch
  76.        
  77.         ;; Save the current page config and stack pointer
  78.         LD  IX, #_currentPage
  79.         LD  A, 1 (IX)
  80.         LD  0 (IY), A
  81.         LD  A, 2 (IX)
  82.         LD  1 (IY), A
  83.         LD  A, 3 (IX)
  84.         LD  2 (IY), A
  85.        
  86.         LD  HL,#0
  87.         ADD HL,SP
  88.         LD  3 (IY), L
  89.         LD  4 (IY), H
  90.        
  91.         ;; Make IY point to the next task and set current_task accordingly
  92.         LD  A,(#_current_task)
  93.         INC A
  94.         LD  (#_current_task),A
  95.        
  96.         LD  BC,#8
  97.         ADD IY,BC
  98.        
  99.        
  100.     first_switch:
  101.        
  102.        
  103.         ;; Find next task
  104.         ;; If there is no task running anymore, shut down the system.
  105.         LD  E,#0
  106.     find_next_task_loop:
  107.         LD  A,(#_current_task)
  108.         CP  A,#MAX_TASKS
  109.         JP  C,find_next_task_loop_noreset
  110.         LD  A,#0
  111.         LD  (#_current_task),A
  112.         LD  IY,#_tasks
  113.         INC E
  114.         LD  A,E
  115.         CP  A,#3
  116.         JP  Z,_shutdown
  117.     find_next_task_loop_noreset:
  118.         LD  A,3 (IY)
  119.         OR  A,4 (IY)
  120.         JP  NZ,found_next_task
  121.         LD  A,(#_current_task)
  122.         INC A
  123.         LD  (#_current_task),A
  124.         LD  BC,#8
  125.         ADD IY,BC
  126.         JP  find_next_task_loop
  127.     found_next_task:
  128.        
  129.         ;; Restore page config
  130.         LD  B,#HAL_MEM_MAPPER_SLOT
  131.         LD  IX,#_currentPage
  132.         LD  A,0 (IY)
  133.         LD  1 (IX), A
  134.         LD  C,#1
  135.         OUT (C),A
  136.         LD  A,1 (IY)
  137.         LD  2 (IX), A
  138.         LD  C,#2
  139.         OUT (C),A
  140.         LD  A,2 (IY)
  141.         LD  3 (IX), A
  142.         LD  C,#3
  143.         OUT (C),A
  144.        
  145.         ;; Restore stack pointer
  146.         LD  L,3 (IY)
  147.         LD  H,4 (IY)
  148.         LD  SP,HL
  149.        
  150.         LD  A,#0
  151.         LD  (#_is_first_switch),A
  152.        
  153.         RET
  154.     __endasm;
  155. }
  156.  
  157. void yield() __naked {
  158.     __asm
  159.         DI
  160.        
  161.         PUSH    AF
  162.         PUSH    BC
  163.         PUSH    DE
  164.         PUSH    HL
  165.         PUSH    IX
  166.         PUSH    IY
  167.        
  168.         CALL    _timer_isr
  169.        
  170.         POP IY
  171.         POP IX
  172.         POP HL
  173.         POP DE
  174.         POP BC
  175.         POP AF
  176.        
  177.         EI
  178.         RET
  179.     __endasm;
  180. }
Advertisement
Add Comment
Please, Sign In to add comment