.module semlib.c .area text ; resource_id -> 3,x 0000 _sem_get:: 0000 3B pshd 0001 34 pshx 0002 B775 tfr s,x 0004 ; // semlib.c 0004 ; #include <912d60.h> 0004 ; #include "kernel.h" 0004 ; #include "semlib.h" 0004 ; 0004 ; 0004 ; 0004 ; 0004 ; // GLOBAL VARIABLES 0004 ; extern unsigned int current; // current task id number 0004 ; 0004 ; // task control block 0004 ; extern struct task_block { 0004 ; unsigned char id; // ID of task 0004 ; enum task_state state; // State 0004 ; unsigned char priority; // Priority 0004 ; unsigned char *heap_ptr; // heap addr while not current task 0004 ; unsigned int heap_size; // heap size 0004 ; unsigned char *frame_ptr; // CCR pointer 0004 ; }; 0004 ; 0004 ; 0004 ; // resource control block 0004 ; extern struct resource_block { 0004 ; unsigned char id; // ID of resource 0004 ; enum resource_state state; // State (busy, free...) 0004 ; unsigned char user; // Current resource user/owner 0004 ; signed char queue[4]; // Tasks waiting on resource 0004 ; unsigned char queue_pos; // Next free spot in queue 0004 ; }; 0004 ; 0004 ; 0004 ; extern struct task_block task[numtasks]; 0004 ; extern struct resource_block resource[numresources]; 0004 ; 0004 ; 0004 ; 0004 ; 0004 ; 0004 ; 0004 ; 0004 ; signed char sem_get(char resource_id) { 0004 ; 0004 ; /* Gives a resource to a requesting task. 0004 ; returns the resource id number (0,1,2,...) if free. 0004 ; otherwise returns -1. 0004 ; 0004 ; At this point, semaphores are a procedural control 0004 ; that the tasks have to follow to avoid resource contention. 0004 ; There is no kernel control over resources yet. */ 0004 ; 0004 ; 0004 ; // critical section 0004 ; INTR_OFF(); 0004 1410 sei 0006 0006 ; 0006 ; 0006 ; // GET SEMAPHORE 0006 ; // give semaphore to task 0006 ; if (resource[resource_id].state == notbusy) { 0006 E603 ldab 3,x 0008 87 clra 0009 59 lsld 000A 59 lsld 000B 59 lsld 000C C30001 addd #_resource+1 000F B7C6 xgdy 0011 E740 tst 0,y 0013 2649 bne L4 0015 ; resource[resource_id].state = busy; 0015 E603 ldab 3,x 0017 87 clra 0018 59 lsld 0019 59 lsld 001A 59 lsld 001B C30001 addd #_resource+1 001E B7C6 xgdy 0020 C601 ldab #1 0022 6B40 stab 0,y 0024 ; resource[resource_id].user = current; 0024 E603 ldab 3,x 0026 87 clra 0027 59 lsld 0028 59 lsld 0029 59 lsld 002A C30002 addd #_resource+2 002D B7C6 xgdy 002F FC0000 ldd _current 0032 6B40 stab 0,y 0034 ; if (task[current].state == waiting) 0034 CC0009 ldd #9 0037 FD0000 ldy _current 003A 13 emul 003B C30001 addd #_task+1 003E B7C6 xgdy 0040 E640 ldab 0,y 0042 C103 cmpb #3 0044 2610 bne L9 0046 ; task[current].state = running; 0046 CC0009 ldd #9 0049 FD0000 ldy _current 004C 13 emul 004D C30001 addd #_task+1 0050 B7C6 xgdy 0052 C602 ldab #2 0054 6B40 stab 0,y 0056 L9: 0056 ; INTR_ON(); 0056 10EF cli 0058 0058 ; return resource_id; 0058 E603 ldab 3,x 005A B714 tfr b,d 005C 2015 bra L3 005E L4: 005E ; } 005E ; // busy 005E ; else { 005E ; task[current].state = waiting; 005E CC0009 ldd #9 0061 FD0000 ldy _current 0064 13 emul 0065 C30001 addd #_task+1 0068 B7C6 xgdy 006A C603 ldab #3 006C 6B40 stab 0,y 006E ; INTR_ON(); 006E 10EF cli 0070 0070 ; return -1; 0070 CCFFFF ldd #-1 0073 L3: 0073 B757 tfr x,s 0075 30 pulx 0076 1B82 leas 2,sp 0078 .dbline 0 ; func end 0078 3D rts 0079 ; resource_id -> 3,x 0079 _sem_give:: 0079 3B pshd 007A 34 pshx 007B B775 tfr s,x 007D ; } 007D ; } 007D ; 007D ; 007D ; 007D ; 007D ; void sem_give(char resource_id) { 007D ; 007D ; /* Takes a resource back from a task. 007D ; will (eventually) pass a message to a waiting task. 007D ; 007D ; At this point, semaphores are a procedural control 007D ; that the tasks have to follow to avoid resource contention. 007D ; There is no kernel control over resources yet. */ 007D ; 007D ; 007D ; // critical section 007D ; INTR_OFF(); 007D 1410 sei 007F 007F ; 007F ; 007F ; // return the resource 007F ; resource[resource_id].state = notbusy; 007F E603 ldab 3,x 0081 87 clra 0082 59 lsld 0083 59 lsld 0084 59 lsld 0085 C30001 addd #_resource+1 0088 B7C6 xgdy 008A 6940 clr 0,y 008C ; 008C ; INTR_ON(); 008C 10EF cli 008E 008E ; 008E ; } 008E L14: 008E B757 tfr x,s 0090 30 pulx 0091 1B82 leas 2,sp 0093 .dbline 0 ; func end 0093 3D rts