2020-10-16 02:58:12 +00:00
|
|
|
; C02 module intlib.h02 assembly language subroutines
|
2019-05-27 21:12:10 +00:00
|
|
|
; Requires
|
2020-10-16 02:58:12 +00:00
|
|
|
; external zero page words DSTPTR and SRCPTR
|
|
|
|
; and external locations TEMP0, TEMP1, TEMP2, and TEMP3
|
2019-05-27 21:12:10 +00:00
|
|
|
|
2020-10-06 16:30:20 +00:00
|
|
|
SUBROUTINE INTLIB
|
|
|
|
|
2019-05-27 21:12:10 +00:00
|
|
|
;iabs(n) - Get Integer ABSolute Value
|
|
|
|
;Args: Y,X = Integer to get Absolute Value Of
|
|
|
|
;Sets: TEMP1, TEMP2
|
|
|
|
;Affects: C, N, Z
|
|
|
|
;Returns: A = Absolute Value of Argument
|
|
|
|
IABS: CPY #$80 ;If Negative (High Bit Set)
|
2020-10-06 16:30:20 +00:00
|
|
|
BCC .RETURN ; Carry will Already be Set
|
2019-05-27 21:12:10 +00:00
|
|
|
JSR SAVRXY ; Copy LSB, MSB to TEMP1. TEMP2
|
|
|
|
LDA #0 ; Subtract LSB
|
|
|
|
SBC TEMP1 ; from 0
|
|
|
|
TAX ; and Copy to X Register
|
|
|
|
LDA #0 ; Subtract MSB
|
|
|
|
SBC TEMP2 ; from 0
|
|
|
|
TAY ; and Copy to Y Register
|
2020-10-06 16:30:20 +00:00
|
|
|
.RETURN RTS
|
2019-05-27 21:12:10 +00:00
|
|
|
|
2020-09-08 15:51:30 +00:00
|
|
|
;imax(i) - Get MAXimum of Two Integers
|
|
|
|
;Args: Y,X = Second Integer
|
2020-10-16 02:58:12 +00:00
|
|
|
;Uses: SRCPTR = First Integer
|
2019-05-27 21:12:10 +00:00
|
|
|
;Affects: N,Z,C
|
2020-09-08 15:51:30 +00:00
|
|
|
;Returns: Y,X = Larger of the Two Arguments
|
2020-10-16 02:58:12 +00:00
|
|
|
IMAX: CPY SRCPTR+1 ;If Y < SRCPTR MSB
|
|
|
|
BCC .GETSRC ; Return SRCPTR
|
|
|
|
CPX SRCPTR ;IF X >= SRCPTR LSB
|
2020-10-06 16:30:20 +00:00
|
|
|
BCS .RETURN ; Return Argument
|
2020-10-16 02:58:12 +00:00
|
|
|
.GETSRC JMP GETSRC ;Return Integer in SRCPTR
|
2019-05-27 21:12:10 +00:00
|
|
|
|
2020-09-08 15:51:30 +00:00
|
|
|
;imin(i) - Get MINimum of Two Integers
|
|
|
|
;Args: Y,X = Second Integer
|
2020-10-16 02:58:12 +00:00
|
|
|
;Uses: SRCPTR = First Integer
|
2019-05-27 21:12:10 +00:00
|
|
|
;Affects: N,Z,C
|
2020-09-08 15:51:30 +00:00
|
|
|
;Returns: Y,X = Larger of the Two Arguments
|
2020-10-16 02:58:12 +00:00
|
|
|
IMIN: CPY SRCPTR+1 ;If Y < SRCPTR+1
|
2020-10-06 16:30:20 +00:00
|
|
|
BCC .RETURN ; Return Argument
|
2020-10-16 02:58:12 +00:00
|
|
|
BNE .GETSRC ;If Y > SRCPTR+1 Return SRCPTR
|
|
|
|
CPX SRCPTR ;If X >= SRCPTR
|
|
|
|
BCS .GETSRC ; Return SRCPTR
|
2020-10-06 16:30:20 +00:00
|
|
|
RTS ;Return Argument
|
2019-05-27 21:12:10 +00:00
|
|
|
|
2020-09-08 15:51:30 +00:00
|
|
|
;iaddc(c,i) - Add Byte c to Integer i
|
|
|
|
IADDC: JSR SETSRC ;Save Integer and Clear Y
|
|
|
|
TAX ;Copy Byte to LSB and drop into IADD
|
|
|
|
|
2019-05-27 21:12:10 +00:00
|
|
|
;iadd(d) - ADD Integer d to from Integer g
|
|
|
|
;Args: Y,X = Addend
|
|
|
|
;Requires: setsrc(g) - Augend
|
|
|
|
;Sets: TEMP1,TEMP2 = Addend
|
|
|
|
;Affects: Z,C
|
2020-10-06 16:30:20 +00:00
|
|
|
;Returns: A = Carry
|
|
|
|
; Y,X = Sum
|
2019-05-27 21:12:10 +00:00
|
|
|
; N = Sign of Result
|
|
|
|
IADD: CLC ;Clear Carry for Addition
|
|
|
|
TXA ;Add Addend LSB
|
2020-10-16 02:58:12 +00:00
|
|
|
ADC SRCPTR ;to Augend LSB
|
2019-05-27 21:12:10 +00:00
|
|
|
TAX ;and Copy to X
|
|
|
|
TYA ;Add Addend MSB
|
2020-10-16 02:58:12 +00:00
|
|
|
ADC SRCPTR+1 ;to Augebd MSB
|
2019-05-27 21:12:10 +00:00
|
|
|
TAY ;and Copy to Y
|
|
|
|
LDA #0 ;Set Overflow to 0
|
|
|
|
ROL ; Rotate Carry (Same as Adding it)
|
|
|
|
RTS ; and Return
|
|
|
|
|
|
|
|
;isub(s) - SUBtract Integer s from Integer m
|
|
|
|
;Args: Y,X = Subtrahend
|
|
|
|
;Requires: setsrc(m) - Minuend
|
|
|
|
;Sets: TEMP1,TEMP2 = Subtrahend
|
|
|
|
;Affects: Z,C
|
2020-10-06 16:30:20 +00:00
|
|
|
;Returns: A = Carry
|
|
|
|
; Y,X = Difference
|
2019-05-27 21:12:10 +00:00
|
|
|
; N = Sign of Result
|
|
|
|
ISUB: JSR SAVRXY ;Store Subtrahend in TEMP1,TEMP2
|
|
|
|
SEC ;Set Carry for Subtraction
|
2020-10-16 02:58:12 +00:00
|
|
|
LDA SRCPTR ;Load Minuend LSB
|
2019-05-27 21:12:10 +00:00
|
|
|
SBC TEMP1 ;Subtract Subtrahend LSB
|
|
|
|
TAX ;Copy Difference LSB to X
|
2020-10-16 02:58:12 +00:00
|
|
|
LDA SRCPTR+1 ;Load Minuend MSB
|
2019-05-27 21:12:10 +00:00
|
|
|
SBC TEMP2 ;Subtract Subtrahend MSB
|
|
|
|
TAY ;Copy Difference MSB to Y
|
|
|
|
LDA #0 ;Set Overflow Byte to 0
|
|
|
|
SBC #0 ; Subtract Carry
|
|
|
|
RTS ; and Return
|
|
|
|
|
|
|
|
;imult(m) - MULTiply Two Integers
|
2020-09-08 15:51:30 +00:00
|
|
|
;Args: Y,X - Multiplier
|
2020-10-06 16:30:20 +00:00
|
|
|
;Requires: DSTPTR = Multiplicand
|
2020-09-08 15:51:30 +00:00
|
|
|
;Sets: TEMP0-TEMP3 = 32 Bit Product
|
2020-10-06 16:30:20 +00:00
|
|
|
;Destroys: SRCPTR
|
2020-09-08 15:51:30 +00:00
|
|
|
;Affects: A,C,Z,N
|
2020-10-06 16:30:20 +00:00
|
|
|
;Returns: A,Y,X = 24 Bit Product
|
2020-09-08 15:51:30 +00:00
|
|
|
IMULT: JSR SETSRC ;Save Multiplier
|
2020-10-06 16:30:20 +00:00
|
|
|
STY TEMP0+2 ;Clear Upper Bits of Product
|
|
|
|
STY TEMP0+3
|
2020-09-08 15:51:30 +00:00
|
|
|
LDX #16 ;Rotate Through 16 Bits
|
2020-10-06 16:30:20 +00:00
|
|
|
.MSHFTR LSR SRCPTR+1 ;Divide Multiplier by 2
|
|
|
|
ROR SRCPTR
|
|
|
|
BCC .MROTR ;If Shifted out Bit is 1
|
|
|
|
LDA TEMP0+2 ; Add Multiplicand
|
2020-09-08 15:51:30 +00:00
|
|
|
CLC ; to Upper Half of Product
|
2020-10-06 16:30:20 +00:00
|
|
|
ADC DSTPTR
|
|
|
|
STA TEMP0+2
|
|
|
|
LDA TEMP0+3
|
|
|
|
ADC DSTPTR+1
|
|
|
|
STA TEMP0+3
|
|
|
|
.MROTR ROR TEMP0+3
|
|
|
|
ROR TEMP0+2
|
|
|
|
ROR TEMP0+1
|
2020-09-08 15:51:30 +00:00
|
|
|
ROR TEMP0
|
|
|
|
DEX ;Decrement Counter
|
2020-10-06 16:30:20 +00:00
|
|
|
BNE .MSHFTR ;and Process Next Bit
|
2020-09-08 15:51:30 +00:00
|
|
|
LDX TEMP0
|
2020-10-06 16:30:20 +00:00
|
|
|
LDY TEMP1 ;Return Low 24 Bits of
|
|
|
|
LDA TEMP2 ;Product in A, Y, and X
|
2020-09-08 15:51:30 +00:00
|
|
|
RTS
|
2019-05-27 21:12:10 +00:00
|
|
|
|
2020-10-06 16:30:20 +00:00
|
|
|
;idiv(d) - Integer DIVide
|
2020-09-08 15:51:30 +00:00
|
|
|
;Args: Y,X - Divisor
|
2020-10-06 16:30:20 +00:00
|
|
|
;Requires: DSTPTR = Dividend
|
|
|
|
;Sets: SRCPTR = Divisor
|
|
|
|
; DSTPTR = Quotient
|
2020-09-08 15:51:30 +00:00
|
|
|
; TEMP1,TEMP2 = Remainder
|
|
|
|
;Affects: A,C,Z,N
|
|
|
|
;Returns: Y,X = 16 Bit Quotient
|
2020-10-06 16:30:20 +00:00
|
|
|
IDIV: JSR .IDIV ;Do Division and
|
|
|
|
JMP GETDST ;Return Quotient
|
|
|
|
|
|
|
|
;imod(d) - Integer MODulus
|
|
|
|
;Args: Y,X - Divisor
|
|
|
|
;Requires: DSTPTR = Dividend
|
|
|
|
;Sets: SRCPTR = Divisor
|
|
|
|
; DSTPTR = Quotient
|
|
|
|
; TEMP1,TEMP2 = Remainder
|
|
|
|
;Affects: A,C,Z,N
|
|
|
|
;Returns: Y,X = 16 Bit Remainder
|
|
|
|
IMOD: JSR .IDIV ;Do Division and
|
|
|
|
JMP RESRXY ;Return Remainder
|
|
|
|
|
|
|
|
.IDIV JSR SETSRC ;Save Divisor
|
|
|
|
STY TEMP1
|
|
|
|
STY TEMP1+1
|
2020-09-08 15:51:30 +00:00
|
|
|
LDX #16 ;repeat for each bit: ...
|
2020-10-06 16:30:20 +00:00
|
|
|
.IDLOOP ASL DSTPTR ;dividend lb & hb*2, msb -> Carry
|
|
|
|
ROL DSTPTR+1
|
2020-09-08 15:51:30 +00:00
|
|
|
ROL TEMP1 ;remainder lb & hb * 2 + msb from carry
|
2020-10-06 16:30:20 +00:00
|
|
|
ROL TEMP1+1
|
2020-09-08 15:51:30 +00:00
|
|
|
LDA TEMP1
|
|
|
|
SEC
|
2020-10-06 16:30:20 +00:00
|
|
|
SBC SRCPTR ;subtract divisor to see if it fits in
|
2020-09-08 15:51:30 +00:00
|
|
|
TAY ;lb result -> Y, for we may need it later
|
2020-10-06 16:30:20 +00:00
|
|
|
LDA TEMP1+1
|
|
|
|
SBC SRCPTR+1
|
|
|
|
BCC .IDSKIP ;if carry=0 then divisor didn't fit in yet
|
|
|
|
STA TEMP1+1 ;else save substraction result as new remainder,
|
2020-09-08 15:51:30 +00:00
|
|
|
STY TEMP1
|
2020-10-06 16:30:20 +00:00
|
|
|
INC DSTPTR ;and INCrement result cause divisor fit in 1 times
|
|
|
|
.IDSKIP DEX
|
|
|
|
BNE .IDLOOP
|
2020-09-08 15:51:30 +00:00
|
|
|
RTS
|
2019-05-27 21:12:10 +00:00
|
|
|
|
|
|
|
;ishftl(n,i) - Shift Integer i to the Left n Bits
|
|
|
|
;Sets: TEMP1, TEMP2 = LSB, MSB of Result
|
|
|
|
;Affects: A,Y,N,Z,C
|
2020-10-06 16:30:20 +00:00
|
|
|
;Returns: A = Bits Shifted out of Integer
|
|
|
|
; Y,X = Shifted Integer
|
2019-05-27 21:12:10 +00:00
|
|
|
ISHFTL: JSR SAVRXY ;Save X,Y in TEMP1,TEMP2
|
2020-09-08 15:51:30 +00:00
|
|
|
TAY ;Set Counter to Number of Bits
|
2020-10-06 16:30:20 +00:00
|
|
|
BEQ .RESRXY ;If Zero, Return
|
|
|
|
LDA #0 ;Clear Overflow
|
|
|
|
.LSLOOP ASL TEMP1 ;Shift LSB to Left
|
2019-05-27 21:12:10 +00:00
|
|
|
ROL TEMP2 ;Rotate MSB to Left
|
2020-10-06 16:30:20 +00:00
|
|
|
ROL ;Rotate Carry into A
|
2019-05-27 21:12:10 +00:00
|
|
|
DEY ;Decrement Counter
|
2020-10-06 16:30:20 +00:00
|
|
|
BNE .LSLOOP ; and Loop if Not 0
|
|
|
|
BEQ .RESRXY ;Return Shifted Integer
|
2019-05-27 21:12:10 +00:00
|
|
|
|
|
|
|
;ishftr(n,i) - Shift Integer i to the Right n Bits
|
|
|
|
;Sets: TEMP1, TEMP2 = LSB, MSB of Result
|
|
|
|
;Affects: A,Y,N,Z,C
|
2020-10-06 16:30:20 +00:00
|
|
|
;Returns: A = Bits Shifted out of Integer
|
|
|
|
; Y,X = Shifted Integer
|
2019-05-27 21:12:10 +00:00
|
|
|
ISHFTR: JSR SAVRXY ;Save X,Y in TEMP1,TEMP2
|
2020-10-06 16:30:20 +00:00
|
|
|
TAY ;Set Counter to Number of Bits
|
|
|
|
BEQ .RESRXY ;If Zero, Return
|
|
|
|
LDA #0 ;Clear Overflow
|
|
|
|
.RSLOOP LSR TEMP2 ;Shift MSB to Right
|
|
|
|
ROR TEMP1 ;Rotate LSB to Right
|
|
|
|
ROR ;Rotate Carry into A
|
2019-05-27 21:12:10 +00:00
|
|
|
DEY ;Decrement Counter
|
2020-10-06 16:30:20 +00:00
|
|
|
BNE .RSLOOP ; and Loop if Not 0
|
|
|
|
BEQ .RESRXY ;Load Shifted Integer and Return
|
2019-05-27 21:12:10 +00:00
|
|
|
|
|
|
|
;atoi(&s) - ASCII string TO Integer
|
|
|
|
;Args: Y,X = Address of String to Convert
|
|
|
|
;Sets: TEMP1, TEMP2 = Integer Value
|
|
|
|
;Affects: TEMP0
|
|
|
|
;Returns: A = Number of Digits
|
|
|
|
; Y,X = Integer Value
|
|
|
|
ATOI: JSR SETSRC ;Initialize Source String
|
|
|
|
STY TEMP1 ;Initialize Result
|
|
|
|
STY TEMP2
|
2020-10-16 02:58:12 +00:00
|
|
|
.AILOOP LDA (SRCPTR),Y ;Get Next Character
|
2019-05-27 21:12:10 +00:00
|
|
|
CMP #$30 ;If Less Than '0'
|
2020-10-06 16:30:20 +00:00
|
|
|
BCC .AIDONE ; Exit
|
2019-05-27 21:12:10 +00:00
|
|
|
CMP #$3A ;If Greater Than '9'
|
2020-10-06 16:30:20 +00:00
|
|
|
BCS .AIDONE ; Exit
|
2019-05-27 21:12:10 +00:00
|
|
|
AND #$0F ;Convert to Binary Nybble
|
|
|
|
STA TEMP0 ; and Save It
|
|
|
|
LDA TEMP1 ;Load Result
|
|
|
|
LDX TEMP2
|
|
|
|
ASL TEMP1 ;Multiply by 5 by
|
|
|
|
ROL TEMP2
|
|
|
|
ASL TEMP1 ; Multiplying by 4
|
|
|
|
ROL TEMP2
|
|
|
|
CLC ; And Adding Itself
|
|
|
|
ADC TEMP1
|
|
|
|
STA TEMP1
|
|
|
|
TXA
|
|
|
|
ADC TEMP2
|
|
|
|
STA TEMP2
|
|
|
|
ASL TEMP1 ;Multiply that by 2
|
|
|
|
ROL TEMP2
|
|
|
|
LDA TEMP0 ;Get Saved Nybble
|
|
|
|
CLC ;and Add to Result
|
|
|
|
ADC TEMP1 ;Add Saved Nybble
|
|
|
|
STA TEMP1 ; and Store Result
|
|
|
|
LDA #0
|
|
|
|
ADC TEMP2
|
|
|
|
STA TEMP2
|
|
|
|
INY ;Increment Index
|
2020-10-06 16:30:20 +00:00
|
|
|
BPL .AILOOP ; and Loop
|
|
|
|
.AIDONE TYA ;Return Number of Digits
|
|
|
|
.RESRXY JMP RESRXY ;and Integer Value
|
2019-05-27 21:12:10 +00:00
|
|
|
|
|
|
|
;itoa(n) - Integer TO ASCII string
|
|
|
|
;Args: Y,X = Integer Value to Convert
|
2020-10-16 02:58:12 +00:00
|
|
|
;Uses: DSTPTR = Destination String
|
2020-10-06 16:30:20 +00:00
|
|
|
;Affects: X
|
2019-05-27 21:12:10 +00:00
|
|
|
;Returns: A,Y = Length of String
|
|
|
|
ITOA: JSR CVIBCD ;Convert Integer to Packed BCD
|
|
|
|
LDY #0 ;Initialize Index into String
|
|
|
|
STY TEMP3
|
2020-10-06 16:30:20 +00:00
|
|
|
.ITOAA LDY #4 ;Set Initial Digit Number
|
|
|
|
.IAZERO JSR UPBCDI ;Unpack Digit Y
|
|
|
|
BNE .IASKIP ;If Zero
|
2020-09-08 15:51:30 +00:00
|
|
|
DEY ; Decrement Digit Number
|
2020-10-06 16:30:20 +00:00
|
|
|
BNE .IAZERO ; If Not Zero Loop
|
|
|
|
BEQ .IASKIP ; Else .IDSKIP Unpack
|
|
|
|
.IALOOP JSR UPBCDI ;Unpack Digit #Y
|
|
|
|
.IASKIP TAX ;Save Digit in X
|
2019-05-27 21:12:10 +00:00
|
|
|
TYA ;Push Digit Number into Stack
|
|
|
|
PHA
|
|
|
|
TXA ;and Restore Digit
|
|
|
|
LDY TEMP3 ;Get Index into String
|
|
|
|
ORA #$30 ;Convert Digit to ASCII
|
2020-10-16 02:58:12 +00:00
|
|
|
STA (DSTPTR),Y ;and Store in String
|
2019-05-27 21:12:10 +00:00
|
|
|
INC TEMP3 ;Increment Index into String
|
|
|
|
PLA ;Pull Digit Number off Stack
|
|
|
|
TAY
|
|
|
|
DEY ;Decrement Digit Number
|
2020-10-06 16:30:20 +00:00
|
|
|
BPL .IALOOP ;Loop if >= Zero
|
2019-05-27 21:12:10 +00:00
|
|
|
LDA #0 ;Terminate String
|
2020-10-16 02:58:12 +00:00
|
|
|
STA (DSTPTR),Y
|
2019-05-27 21:12:10 +00:00
|
|
|
TYA ;Return String Length
|
|
|
|
RTS
|
|
|
|
|
|
|
|
;upbcdi() - UnPack digits from BCD Integer
|
|
|
|
; Assumes that TEMP0, TEMP1, and TEMP2
|
|
|
|
; are in consecutive memory locations
|
|
|
|
;Args: Y = Digit Number to Unpack (0-5)
|
|
|
|
;Uses: TEMP0 = Low Byte
|
|
|
|
; TEMP1 = Middle Byte
|
|
|
|
; TEMP2 = High Nybble
|
2020-10-06 16:30:20 +00:00
|
|
|
;Affects: X,N,Z
|
2019-05-27 21:12:10 +00:00
|
|
|
;Returns: A = Unpacked Digit
|
2020-10-06 16:30:20 +00:00
|
|
|
UPBCDI: PHP
|
|
|
|
TYA ;Divide Digit Number by 2,
|
|
|
|
LSR ; Setting Carry
|
|
|
|
TAX ; if Digit Number is Odd
|
|
|
|
LDA TEMP0,X ;Load BCD Byte
|
|
|
|
BCC .UPSKIP ;If Digit Number is Odd
|
|
|
|
LSR ; Shift High Nybble to Low Nybble
|
|
|
|
LSR
|
|
|
|
LSR
|
|
|
|
LSR
|
|
|
|
.UPSKIP PLP
|
|
|
|
AND #$0F ;Strip Off High Nybble
|
2019-05-27 21:12:10 +00:00
|
|
|
RTS
|
|
|
|
|
|
|
|
;cvibcd(int) - ConVert Integer to packed Binary Coded Decimal
|
|
|
|
;Args: Y,X - Integer to Convert
|
|
|
|
;Sets: TEMP0 = Tens and Ones Digit
|
2020-09-08 15:51:30 +00:00
|
|
|
; TEMP1 = Thousands and Hundreds Digit
|
|
|
|
; TEMP2 = Ten-Thousands Digit
|
2020-10-06 16:30:20 +00:00
|
|
|
;Affects: A
|
2019-05-27 21:12:10 +00:00
|
|
|
CVIBCD: LDA #0 ;Clear BCD Bytes
|
|
|
|
STA TEMP0
|
|
|
|
STA TEMP1
|
|
|
|
STA TEMP2
|
|
|
|
PHP ;Save Status Register
|
|
|
|
SEI ;Disable Interrupts
|
|
|
|
SED ;Set Decimal Mode
|
|
|
|
TYA ;Push MSB onto Stack
|
|
|
|
PHA
|
|
|
|
TXA ;Push LSB onto Stack
|
|
|
|
PHA
|
|
|
|
TSX ;Copy Stack Pointer to X
|
|
|
|
LDY #16 ;Process 16 bits of Binary
|
2020-10-06 16:30:20 +00:00
|
|
|
.CVLOOP ASL $101,X ;Shift High Bit Into Carry
|
2019-05-27 21:12:10 +00:00
|
|
|
ROL $102,X
|
|
|
|
LDA TEMP0 ;Add 6 Digit BCD Number Itself
|
|
|
|
ADC TEMP0 ; Effectively Multiplying It by 2
|
|
|
|
STA TEMP0 ; and Adding in the Shifted Out Bit
|
|
|
|
LDA TEMP1
|
|
|
|
ADC TEMP1
|
|
|
|
STA TEMP1
|
|
|
|
LDA TEMP2
|
|
|
|
ADC TEMP2
|
|
|
|
STA TEMP2
|
|
|
|
DEY ;Decrement Counter and
|
2020-10-06 16:30:20 +00:00
|
|
|
BNE .CVLOOP ; Process Next Bit
|
|
|
|
PLA ;Restore X and Y Registers
|
2019-05-27 21:12:10 +00:00
|
|
|
PLA
|
|
|
|
PLP ;Restore Status Register
|
2020-09-08 15:51:30 +00:00
|
|
|
RTS
|
2020-10-06 16:30:20 +00:00
|
|
|
|
|
|
|
ENDSUBROUTINE
|