⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 os_cpu_a.asm

📁 在ATmega16芯片上移植的ucosii系统
💻 ASM
字号:
;********************************************************************************************************
;                                               uC/OS-II
;                                         The Real-Time Kernel
;
;                          (c) Copyright 1992-2002, Jean J. Labrosse, Weston, FL
;                                          All Rights Reserved
;
;
;                                       80x86/80x88 Specific code
;                                          LARGE MEMORY MODEL
;
;                                       Pacific Hi-Tech C Compiler
;                                       (IBM/PC Compatible Target)
;
; File         : OS_CPU_A.ASM
; By           : Jean J. Labrosse
; Modified by  : Tony Richardson
;********************************************************************************************************

;********************************************************************************************************
;                                    PUBLIC and EXTERNAL REFERENCES
;********************************************************************************************************

            .GLOBL _OSTickISR                     ; Public
            .GLOBL _OSStartHighRdy                ; Public
            .GLOBL _OSCtxSw                       ; Public
            .GLOBL _OSIntCtxSw                    ; Public

            .GLOBL  _OSIntExit                    ; External, Far
            .GLOBL  _OSTimeTick                   ; External, Far
            .GLOBL  _OSTaskSwHook                 ; External, Far

            .GLOBL  _OSIntNesting                 ; External, Byte
            .GLOBL  _OSTickDOSCtr                 ; External, Byte
            .GLOBL  _OSPrioHighRdy                ; External, Byte
            .GLOBL  _OSPrioCur                    ; External, Byte
            .GLOBL  _OSRunning                    ; External, Byte

            .GLOBL  _OSTCBCur                     ; External, Dword
            .GLOBL  _OSTCBHighRdy                 ; External, Dword

            .186
	    .PSECT  _TEXT
;*********************************************************************************************************
;                                          START MULTITASKING
;                                       void OSStartHighRdy(void)
;
; The stack frame is assumed to look as follows:
;
; OSTCBHighRdy->OSTCBStkPtr --> DS                               (Low memory)
;                               ES
;                               DI
;                               SI
;                               BP
;                               SP
;                               BX
;                               DX
;                               CX
;                               AX
;                               OFFSET  of task code address
;                               SEGMENT of task code address
;                               Flags to load in PSW
;                               OFFSET  of task code address
;                               SEGMENT of task code address
;                               OFFSET  of 'pdata'
;                               SEGMENT of 'pdata'               (High memory)
;
; Note : OSStartHighRdy() MUST:
;           a) Call OSTaskSwHook() then,
;           b) Set OSRunning to TRUE,
;           c) Switch to the highest priority task.
;*********************************************************************************************************

_OSStartHighRdy:

            MOV    AX, #SEG _OSTCBHighRdy          ; Reload DS
            MOV    DS, AX                         ;
;
            CALLF   _OSTaskSwHook                 ; Call user defined task switch hook
;
            MOV    AL, #1                         ; OSRunning = TRUE;
            MOV    DS:_OSRunning, AL              ;   (Indicates that multitasking has started)
;
            LES    BX, DS:_OSTCBHighRdy           ; SS:SP = OSTCBHighRdy->OSTCBStkPtr
            MOV    SS, ES:2[BX]                   ;
            MOV    SP, ES:[BX]                    ;
;
            POP    DS                             ; Load task's context
            POP    ES                             ;
            POPA                                  ;
;
            IRET                                  ; Run task

;*********************************************************************************************************
;                                PERFORM A CONTEXT SWITCH (From task level)
;                                           void OSCtxSw(void)
;
; Note(s): 1) Upon entry,
;             OSTCBCur     points to the OS_TCB of the task to suspend
;             OSTCBHighRdy points to the OS_TCB of the task to resume
;
;          2) The stack frame of the task to suspend looks as follows:
;
;                 SP -> OFFSET  of task to suspend    (Low memory)
;                       SEGMENT of task to suspend
;                       PSW     of task to suspend    (High memory)
;
;          3) The stack frame of the task to resume looks as follows:
;
;                 OSTCBHighRdy->OSTCBStkPtr --> DS                               (Low memory)
;                                               ES
;                                               DI
;                                               SI
;                                               BP
;                                               SP
;                                               BX
;                                               DX
;                                               CX
;                                               AX
;                                               OFFSET  of task code address
;                                               SEGMENT of task code address
;                                               Flags to load in PSW             (High memory)
;*********************************************************************************************************

_OSCtxSw:
;
            PUSHA                                  ; Save current task's context
            PUSH   ES                              ;
            PUSH   DS                              ;
;
            MOV    AX, #SEG _OSTCBCur               ; Reload DS in case it was altered
            MOV    DS, AX                          ;
;
            LES    BX, DS:_OSTCBCur                ; OSTCBCur->OSTCBStkPtr = SS:SP
            MOV    ES:2[BX], SS                    ;
            MOV    ES:[BX], SP                     ;
;
            CALLF  _OSTaskSwHook                   ; Call user defined task switch hook
;
            MOV    AX, DS:_OSTCBHighRdy+2          ; OSTCBCur = OSTCBHighRdy
            MOV    DX, DS:_OSTCBHighRdy            ;
            MOV    DS:_OSTCBCur+2, AX              ;
            MOV    DS:_OSTCBCur, DX                ;
;
            MOV    AL, DS:_OSPrioHighRdy           ; OSPrioCur = OSPrioHighRdy
            MOV    DS:_OSPrioCur, AL               ;
;
            LES    BX, DS:_OSTCBHighRdy            ; SS:SP = OSTCBHighRdy->OSTCBStkPtr
            MOV    SS, ES:2[BX]                    ;
            MOV    SP, ES:[BX]                     ;
;
            POP    DS                              ; Load new task's context
            POP    ES                              ;
            POPA                                   ;
;
            IRET                                   ; Return to new task
;

;*********************************************************************************************************
;                                PERFORM A CONTEXT SWITCH (From an ISR)
;                                        void OSIntCtxSw(void)
;
; Note(s): 1) Upon entry,
;             OSTCBCur     points to the OS_TCB of the task to suspend
;             OSTCBHighRdy points to the OS_TCB of the task to resume
;
;          2) The stack frame of the task to suspend looks as follows:
;
;             OSTCBCur->OSTCBStkPtr ------>  DS                              (Low memory)
;                                            ES
;                                            DI
;                                            SI
;                                            BP
;                                            SP
;                                            BX
;                                            DX
;                                            CX
;                                            AX
;                                            OFFSET  of task code address
;                                            SEGMENT of task code address
;                                            Flags to load in PSW            (High memory)
;
;
;          3) The stack frame of the task to resume looks as follows:
;
;             OSTCBHighRdy->OSTCBStkPtr --> DS                               (Low memory)
;                                           ES
;                                           DI
;                                           SI
;                                           BP
;                                           SP
;                                           BX
;                                           DX
;                                           CX
;                                           AX
;                                           OFFSET  of task code address
;                                           SEGMENT of task code address
;                                           Flags to load in PSW             (High memory)
;*********************************************************************************************************

_OSIntCtxSw:
;
            CALLF   _OSTaskSwHook                  ; Call user defined task switch hook
;
            MOV    AX, #SEG _OSTCBCur               ; Reload DS in case it was altered
            MOV    DS, AX                          ;
;
            MOV    AX, DS:_OSTCBHighRdy+2          ; OSTCBCur = OSTCBHighRdy
            MOV    DX, DS:_OSTCBHighRdy            ;
            MOV    DS:_OSTCBCur+2, AX              ;
            MOV    DS:_OSTCBCur, DX                ;
;
            MOV    AL, DS:_OSPrioHighRdy           ; OSPrioCur = OSPrioHighRdy
            MOV    DS:_OSPrioCur, AL      
;
            LES    BX, DS:_OSTCBHighRdy            ; SS:SP = OSTCBHighRdy->OSTCBStkPtr
            MOV    SS, ES:2[BX]                    ;
            MOV    SP, ES:[BX]                     ;
;
            POP    DS                              ; Load new task's context
            POP    ES                              ;
            POPA                                   ;
;
            IRET                                   ; Return to new task
;

;*********************************************************************************************************
;                                            HANDLE TICK ISR
;
; Description: This function is called 199.99 times per second or, 11 times faster than the normal DOS
;              tick rate of 18.20648 Hz.  Thus every 11th time, the normal DOS tick handler is called.
;              This is called chaining.  10 times out of 11, however, the interrupt controller on the PC
;              must be cleared to allow for the next interrupt.
;
; Arguments  : none
;
; Returns    : none
;
; Note(s)    : The following C-like pseudo-code describe the operation being performed in the code below.
;
;              Save all registers on the current task's stack;
;              OSIntNesting++;
;              if (OSIntNesting == 1) {
;                 OSTCBCur->OSTCBStkPtr = SS:SP
;              }
;              OSTickDOSCtr--;
;              if (OSTickDOSCtr == 0) {
;                  OSTickDOSCtr = 11;
;                  INT 81H;               Chain into DOS every 54.925 mS
;                                         (Interrupt will be cleared by DOS)
;              } else {
;                  Send EOI to PIC;       Clear tick interrupt by sending an End-Of-Interrupt to the 8259
;                                         PIC (Priority Interrupt Controller)
;              }
;              OSTimeTick();              Notify uC/OS-II that a tick has occured
;              OSIntExit();               Notify uC/OS-II about end of ISR
;              Restore all registers that were save on the current task's stack;
;              Return from Interrupt;
;*********************************************************************************************************
;
_OSTickISR:
;
            PUSHA                                ; Save interrupted task's context
            PUSH   ES
            PUSH   DS
;
            MOV    AX, #SEG _OSIntNesting         ; Reload DS
            MOV    DS, AX
            INC    DS:_OSIntNesting, BYTE        ; Notify uC/OS-II of ISR
;
            CMP    DS:_OSIntNesting, #1,  BYTE	 ; if (OSIntNesting == 1)
            JNE    _OSTickISR1             
            MOV    AX, #SEG _OSTCBCur             ;     Reload DS
            MOV    DS, AX
            LES    BX, DS:_OSTCBCur              ;     OSTCBCur->OSTCBStkPtr = SS:SP
            MOV    ES:2[BX], SS                  ;
            MOV    ES:[BX], SP                   ;
;
_OSTickISR1:
            MOV    AX, #SEG _OSTickDOSCtr         ; Reload DS
            MOV    DS, AX
            DEC    DS:_OSTickDOSCtr,  BYTE
            CMP    DS:_OSTickDOSCtr, #0, BYTE
            JNE    _OSTickISR2                   ; Every 11 ticks (~199.99 Hz), chain into DOS
;
            MOV    DS:_OSTickDOSCtr, #11, BYTE
            INT    #081H                         ; Chain into DOS's tick ISR
            JMP    _OSTickISR3

_OSTickISR2:
            MOV    AL, #20H                      ; Move EOI code into AL.
            MOV    DX, #20H                      ; Address of 8259 PIC in DX.
            OUT    [DX], AL                      ; Send EOI to PIC if not processing DOS timer.
;
_OSTickISR3:
            CALLF  _OSTimeTick                   ; Process system tick
;
            CALLF  _OSIntExit                    ; Notify uC/OS-II of end of ISR
;
            POP    DS                            ; Restore interrupted task's context
            POP    ES
            POPA
;
            IRET                                 ; Return to interrupted task
;
;
            .END

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -