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

📄 misc.mod

📁 该应用软件可以实现大多数单片机的仿真实验
💻 MOD
📖 第 1 页 / 共 2 页
字号:
;**********************************************************************
;* Module    : MISC.MOD
;* Programmer: Tony Papadimitriou
;* Purpose   : Miscellaneous OS-called routines
;* Language  : MC68HC11 (ASM11 v1.83+)
;* Status    : FREEWARE, Copyright (c) 1999 by Tony Papadimitriou
;* Segments  : RAM    : Variables
;*           : ROM    : Code
;*           : SEG9   : OS definitions (this allows adding more functions)
;* History   : 99.10.31 v1.00 Original (copied from 99.10.26 OS11.MOD)
;*           : 99.11.22 v1.01 Added fHexToBin
;**********************************************************************

#ifmain
                    #LISTOFF
                    #INCLUDE  811E2.INC
                    #INCLUDE  COMMON.INC
                    #INCLUDE  OSERRORS.INC
                    #LISTON

                    #SEG9
                    ORG       $FF00
#endif

                    #SEG9
#ifndef OSCommands
OSCommands          equ       *
#endif

fCopyMem            equ       *-OSCommands/2      ;Copy a block of memory
                    dw        ?CopyMem

fBin2Dec            equ       *-OSCommands/2      ;Binary to Decimal conversion
                    dw        ?Bin2Dec

fBin2BCD            equ       *-OSCommands/2      ;Binary to BCD conversion (actually any base)
                    dw        ?Bin2BCD

fSearchTable        equ       *-OSCommands/2      ;Search any data table
                    dw        ?SearchTable

fConvertAD          equ       *-OSCommands/2      ;Convert A/D value to volts
                    dw        ?ConvertAD

fKickCOP            equ       *-OSCommands/2      ;Kick the COP timer to prevent timeout resets
                    dw        KickCOP             ;Not a local label, called either way

fHexToASCII         equ       *-OSCommands/2      ;Convert a hex number to its ASCII equivalent
                    dw        ?HexToASCII

fReverseByte        equ       *-OSCommands/2      ;Reverse a byte's bit order from MSB to LSB or back
                    dw        ?ReverseByte

fGetBootMode        equ       *-OSCommands/2      ;Get the boot mode code
                    dw        ?GetBootMode

fIncBCD             equ       *-OSCommands/2      ;Increment a BCD number
                    dw        ?IncBCD

fDecBCD             equ       *-OSCommands/2      ;Decrement a BCD number
                    dw        ?DecBCD

fHexToBin           equ       *-OSCommands/2      ;Convert a hex number to binary
                    dw        ?HexToBin

#PAGE ;Operating System routines expanded
**********************************************************************
*                 Operating System routines expanded                 *
**********************************************************************
                    #ROM

?Op_Unused          ldb       #errUnused          ;Dummy call for missing opcodes
                    sec                           ;..use your own non-zero error codes
AnRTS               rts                           ;Also, provides a globally accessed RTS

; Purpose: Copy (move) bytes from source to destination memory
; Input  : D holds number of bytes to copy
;        : Y points to start of source memory
;        : X points to start of destination memory
; Note(s): Correctly handles copies (even partially) to EEPROM
?CopyMem            pshy
                    lda       A_,y                ;load parameters
                    ldb       B_,y
                    ldx       X_,y
                    ldy       Y_,y
                    cmpd      #0                  ;any bytes to copy?
                    beq       ?CopyMem.NoError    ;no, get out of here
?CopyMem.Loop       pshd                          ;save counter between loops
#ifdef fWriteEE
                    cpx       #EEPROM             ;check destination for EEPROM
                    blo       ?CopyMem.RAM
                    cpx       #EEPROM_END
                    bhi       ?CopyMem.RAM
     ;?CopyMem.EEPROM
                    lda       ,y                  ;get a byte from source
                    os        fWriteEE            ;write EEPROM byte A to ,X
                    bcs       ?CopyMem.Error      ;on error...
                    bra       ?CopyMem.LoopEnd    ;no error, continue
#endif
?CopyMem.RAM        lda       ,y                  ;get byte from source
                    sta       ,x                  ;put byte to destination
?CopyMem.LoopEnd    inx                           ;bump up both pointers
                    iny
                    puld                          ;get loop counter
                    decd                          ;and count down
                    bne       ?CopyMem.Loop
?CopyMem.NoError    puly
                    clc
                    rts
?CopyMem.Error      puld
                    puly
                    ldb       #errFailure
                    sec
                    rts

; Purpose: Convert a binary number to decimal
; Input  : D=Unsigned binary number from 0 to 65535 ($FFFF)
; Output : D=First two ASCII digits of result (first char is blank)
;        : X=Second two ASCII digits of result
;        : Y=Third two ASCII digits of result
?Bin2Dec            lda       A_,y
                    ldb       B_,y
                    ldx       #'00'
                    pshx:3                        ;initialize stack result
                    tsx                           ;and put it's pointer in Y
                    inx:2
?Bin2Dec.01.0       subd      #10000              ;figure out tens of thousands
                    bcs       ?Bin2Dec.01
                    inc       1,x                 ;increment tens of thousands
                    bra       ?Bin2Dec.01.0
?Bin2Dec.01         addd      #10000
?Bin2Dec.01.1       subd      #1000               ;figure out thousands
                    bcs       ?Bin2Dec.02
                    inc       2,x                 ;increment thousands
                    bra       ?Bin2Dec.01.1
?Bin2Dec.02         addd      #1000
?Bin2Dec.02.1       subd      #100
                    bcs       ?Bin2Dec.03
                    inc       3,x                 ;increment hundreds
                    bra       ?Bin2Dec.02.1
?Bin2Dec.03         addd      #100
?Bin2Dec.03.1       subb      #10                 ;no need to use D anymore
                    bcs       ?Bin2Dec.04
                    inc       4,x                 ;increment tens
                    bra       ?Bin2Dec.03.1
?Bin2Dec.04         addb      #10+'0'             ;no need to use D anymore
                    stb       5,x                 ;finally, save units
                    puld
                    sta       A_,y
                    stb       B_,y
                    pulx
                    stx       X_,y
                    pulx
                    stx       Y_,y
?Bin2Dec.Exit       clc
                    rts

; Purpose: Convert a binary word value to BCD or other base
; Input  : D holds word value
;        : X points to output buffer
;        : Y holds length of buffer
;        : First byte of buffer holds conversion base
; Note(s): Although user's stack frame is used for temporary storage,
;        : it is left intact.
?Bin2BCD            equ       *
          ; save original stack frame
                    ldx       Y_,y                ;Save stack frame's Y
                    pshx
                    ldx       X_,y                ;Save stack frame's X (pointer to buffer)
                    pshx
          ; check conversion base for validity
                    clra                          ;Get conversion base in D
                    ldb       ,x
                    cmpb      #2                  ;Check base to be 2 to 36
                    blo       ?Bin2BCD.Error
                    cmpb      #36
                    bhi       ?Bin2BCD.Error
          ; make buffer pointer point to end of buffer (last character)
                    xgdx                          ;Now X holds base, D holds buffer pointer
                    addd      Y_,y                ;add length of buffer
                    decd                          ;minus 1 to point to end of buffer
                    std       X_,y                ;and save in temp space

                    lda       A_,y                ;Load D register with word
                    ldb       B_,y                ;..to convert
                    pshx                          ;push needed for first pass of next loop

?Bin2BCD.01         pulx
                    pshx
                    idiv                          ;X should be holding base here
                    jsr       ToDigit

          ; save current digit to buffer and adjust buffer pointer backwards
                    pshx
                    ldx       X_,y                ;Point to current buffer character
                    stb       ,x                  ;Save remainder in output buffer
                    dex                           ;decrement pointer toward start of buffer
                    stx       X_,y                ;and save it
                    pulx

                    xgdx                          ;and use integer result as input for
                    dec       Y_+1,y              ;decrement [low byte] of Y counter
                    bne       ?Bin2BCD.01         ;2nd.last digit? if no, loop back
?Bin2BCD.Exit       pulx:2                        ;adjust stack and restore stack frame's X
                    stx       X_,y
                    pulx                          ;restore stack frame's Y
                    stx       Y_,y
                    clc
                    rts
?Bin2BCD.Error      pulx:2                        ;dummy PULLs to adjust stack
                    ldb       #errBadParm
                    sec
                    rts

; Purpose: Search a data table for some value, and return its index
;        : Table must be terminated with zero (byte or word) depending
;        : on target size
; Input  : B = number of bytes per entry (eg. 5)
;        : X points to beginning of table
;        : Y holds value to search for (if byte, lower 8-bits only)
;        : Carry Clear = Byte Search
;        : Carry Set   = Word Search
; Output : IF FOUND:     X points to matched table entry

⌨️ 快捷键说明

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