1
0
mirror of https://github.com/RevCurtisP/C02.git synced 2024-11-22 01:31:33 +00:00
C02/include/block.a02

352 lines
13 KiB
Plaintext
Raw Normal View History

2018-07-25 23:00:46 +00:00
;*** Needs to be Tested and Debugged ****
2018-01-28 18:30:49 +00:00
;C02 library block.h02 assembly language subroutines
;Requires External Zero Page Variables
;BLKLO, BLKHI, DSTPTR, SRCPTR
2018-01-28 18:30:49 +00:00
;External Variables
;BLKSLO, BLKSHI, BLKELO, BLKEHI, BLKLEN, TEMP0, TEMP1, TEMP2
;External Routines
2018-07-25 23:00:46 +00:00
;MEMCMP, MEMCPY, MEMSRC, RESDST, SAVDST, SETDST, SETSRC, SETSRD
2018-01-28 18:30:49 +00:00
SUBROUTINE BLOCK
2018-01-28 18:30:49 +00:00
;blkbgn(&b) - Set Block Start Address
;Args: X,Y = Address
;Sets: BLKSLO, BLKSHI = Block Start Address
;Affects: Z, N
2018-07-31 00:03:56 +00:00
BLKBGN: STX BLKSLO ;Save Block Start LSB
STY BLKSHI ;Save Block Start MSB
2018-01-28 18:30:49 +00:00
RTS
;blkend(&b) - Set Block End Address (+1)
;Args: X,Y = Address
;Sets: BLKELO, BLKEHI = Block End Address
;Affects: Z, N
2018-07-31 00:03:56 +00:00
BLKEND: STX BLKELO ;Save Block End LSB
STY BLKEHI ;Save Block End MSB
2018-01-28 18:30:49 +00:00
RTS
;blkrst() - Reset Block Segment Pointer to Start Address
;Uses: BLKSLO, BLKSHI = Block Start Address
;Sets: BLKLO, BLKHI = Block Pointer
;Affects: Z, N
2018-07-31 00:03:56 +00:00
;Returns: X = Block Pointer LSB
; Y = Block Pointer MSB
BLKRST: LDX BLKSLO ;Load X with Block Start LSB
LDY BLKSHI ;Load Y with Block Start MSB
;and fall into BLKPTR
;blkptr() - Set Block Pointer Address
;Args: X,Y = Address LSB, MSN
;Sets: BLKLO, BLKHI = Block Pointer
;Affects: Z, N
;Returns: X = Block Pointer LSB
; Y = Block Pointer MSB
BLKPTR: STX BLKLO ;Store X in Block Pointer LSB
STY BLKHI ;Store Y in Block Pointer MSB
2018-01-28 18:30:49 +00:00
RTS ;Exit Routine
2018-07-31 00:03:56 +00:00
;blkcpe() - Compare Block Pointer to Block End
;Uses: BLKLO, BLKHI = Block Pointer
; BLKELO, BLKEHI = Block End Address
;Affects: X,N
;Returns: C=0,Z=0 if Pointer before End
; C=1,Z=1 if Pointer at End
; C=1,Z=0 if Pointer after End
BLKCPE: LDX BLKHI ;Get Block Pointer MSB
CPX BLKEHI ;Compare to Block End MSB
BCC BLKCPX ;If Pointer Not Less than
BNE BLKCPX ;or Equal to End
LDX BLKLO ; Get Block Pointer LSB
CPX BLKELO ; Compare to Block End LSB
BLKCPX: RTS
2018-01-28 18:30:49 +00:00
;blkset(c) - Set All Bytes in Block to Character c
;Args: c - Value to Set all Bytes to
2018-07-31 00:03:56 +00:00
;Uses: BLKSLO, BLKSHI - Block Start Address
; BLKELO, BLKEHI - Block End Address
;Sets: BLKLO, BLKHI = Start of Block
2018-01-28 18:30:49 +00:00
;Returns: A - Value of Argument n
2018-07-31 00:03:56 +00:00
BLKSET: JSR BLKRST ;Reset Block Pointer
LDY #0 ;Initialize Index
BLKSEL: JSR BLKCPE ;Compare Pointer to Block End
BCS BLKSEX ;If Past End, Exit Routine
STA (BLKLO),Y ;Store Value at Block Pointer Address
INC BLKLO ;Increment Block Pointer LSB
BNE BLKSEL ;If Not End of Page, Loop
INC BLKHI ;Increment Block Pointer MSB
BNE BLKSEL ;If Not End of Memory, Loop
BLKSEX: JMP BLKRST ;Reset Block Pointer and Return
2018-01-28 18:30:49 +00:00
;blkput(n, &m) - Append n Bytes of m to Block
;Args: n = Number of Bytes to Append
; m = Array of Bytes to Append
2018-07-31 00:03:56 +00:00
;Uses: BLKELO, BLKEHI - Block End Address
;Sets: DSTPTR = Pointer to Bytes in Block
; SRCPTR = Pointer to m
2018-01-28 18:30:49 +00:00
; TEMP0 = Number of Bytes to Append
;Updates: BLKLO, BLKHI = Block Pointer
;Returns: A=$FF - Append Successful
; A=$00 - Block Overflow
2018-07-31 00:03:56 +00:00
BLKPUT: JSR MEMSRC ;Set Source Address & Number of Bytes
2018-07-25 23:00:46 +00:00
JSR BLKDST ;Set Destination to Block Pointer
2018-01-28 18:30:49 +00:00
JMP BLKNCP ;Move Block Pointer and Copy
2018-07-31 00:03:56 +00:00
;blkget(n, &m) - Read n Bytes of Current Segment into Array m
;Args: A = Number of Bytes to Read
; Y,X = Address of Array
2018-01-28 18:30:49 +00:00
;Uses: BLKELO, BLKEHI = Block End Address
;Sets: DSTPTR = Address of Array
; SRCPTR = Current Pointer in Block
2018-01-28 18:30:49 +00:00
; TEMP0 = Number of Bytes to Append
2018-07-31 00:03:56 +00:00
;Updates: BLKLO, BLKHI = Next Segment in Block
2018-01-28 18:30:49 +00:00
;Returns: A=$FF - Append Successful
; A=$00 - Block Overflow
2018-07-31 00:03:56 +00:00
BLKGET: JSR SETDST ;Set Destination Address
JSR MEMSRA ;Save Number of Bytes to Copy
JSR BLKSRC ;Set Source Pointer to Block Pointer
BLKNCP: JSR BLKNXT ;Move Block Pointer
BEQ BLKRTS ;If Past End of Block, Return False
BLKCPY: LDY #0 ;Copy Source To Destination
JSR MEMCPL ;
LDA #$FF ;Return TRUE
RTS
2018-01-28 18:30:49 +00:00
;blkmem(n, &m) - Search for n Bytes of m in Block
2018-07-31 00:03:56 +00:00
;Args: n = Number of Bytes to Compare
2018-01-28 18:30:49 +00:00
; m = Array of Bytes to Search For
;Sets: DSTPTR = Pointer to Segment in Block
; SRCPTR = Pointer to m
2018-07-31 00:03:56 +00:00
; TEMP0 = Number of Bytes to Compare
2018-01-28 18:30:49 +00:00
;Returns: A=$FF - Bytes found
; A=$00 - Bytes not found
2018-07-31 00:03:56 +00:00
BLKMEM: JSR MEMSRC ;Initialize Source, Index, and Count
JSR BLKDSS ;Set Destination Pointer To Block Start
LDX #DSTPTR ;Set BLKNXX Pointer to DSTPTR
2018-07-31 00:03:56 +00:00
BLKMEL: LDY #0 ;Initialize Index
JSR MEMCML ;Compare Source to Destination
BEQ BLKTRU ;If Equal, Exit TRUE
JSR BLKNXX ;Move Destination Pointer
BNE BLKMEL ;If Not Past Block End, Loop
BLKFLS: LDA #FALSE ; Return FALSE
2018-01-28 18:30:49 +00:00
RTS
;blkseg(n) - Set Block Segment Length
;Args: n - Segment Length
;Sets: TEMP0 = Segment Size
;Returns: A=$FF - Segment Length Successfully Set
; A=$00 - Error: Segment Length not Set
2018-07-31 00:03:56 +00:00
BLKSEG: STA BLKLEN ;Store Argument in Block Length
ORA #0 ;If Segment Length is Zero
BEQ BLKFLS ; Return False
BLKTRU: LDA #TRUE ;Return TRUE
2018-01-28 18:30:49 +00:00
BLKRTS: RTS
2018-07-31 00:03:56 +00:00
;BLKNXD() - Move Destination Pointer to Next Segment
;Uses: BLKLEN - Block Segment Length
;Updates: DSTPTR, DSTPTR+1 = Block Pointer
2018-07-31 00:03:56 +00:00
;Affects: C,N,Z
;Returns: A=$FF - Destination Pointer Moved
; A=$00 - Error: Pointer Overflow or Length 0
BLKNXD: LDX #DSTPTR ;Set X to Destination Pointer
2018-07-31 00:03:56 +00:00
JMP BLKNXX ;and Move to Next Segment
;blknxx() - Move Pointer to Next Segment
;Args: X = Zero Page Address of Pointer
2018-01-28 18:30:49 +00:00
;Uses: BLKLEN - Block Segment Length
;Updates: DSTPTR, DSTPTR+1 = Block Pointer
2018-01-28 18:30:49 +00:00
;Affects: C,N,Z
;Returns: A=$FF - Destination Pointer Moved
; A=$00 - Error: Pointer Overflow or Length 0
2018-07-31 00:03:56 +00:00
BLKNXX: LDA BLKLEN ;Get Segment Length
BEQ BLKRTS ;If 0 Return False
2018-01-28 18:30:49 +00:00
CLC
2018-07-31 00:03:56 +00:00
ADC 0,X ;Add to Pointer LSB
STA 0,X
LDA 1,X ;Add Carry
ADC #0 ;to Pointer MSB
STA 1,X ;If Destination MSB
CMP BLKEHI ; < Block End MSB
BCC BLKTRU ; Return True
LDA 0,X ;If Destination LSB
CMP BLKELO ; < Block End LSB
BCC BLKTRU ; Return True
BCS BLKFLS ;Return False
2018-01-28 18:30:49 +00:00
2018-07-31 00:03:56 +00:00
;blknxt(n) - Move Block Pointer to Next Segment
2018-01-28 18:30:49 +00:00
;Uses: BLKLEN - Block Segment Length
;Updates: BLKLO, BLKHI = Block Pointer
;Affects: C,N,Z
;Returns: A=$FF - Destination Pointer Moved
; A=$00 - Error: Pointer Overflow or Length 0
2018-07-31 00:03:56 +00:00
BLKNXT: LDA BLKLEN ;Get Segment Length
BEQ BLKRTS ;If 0 Return False
2018-01-28 18:30:49 +00:00
CLC
2018-07-31 00:03:56 +00:00
ADC BLKLO ;Add to Block Pointer LSB
2018-01-28 18:30:49 +00:00
STA BLKLO
2018-07-31 00:03:56 +00:00
LDA BLKHI ;Add Carry
ADC #0 ;to Block Pointer MSB
STA BLKHI ;If Block Pointer MSB
JSR BLKCPE ;Compare Pointer to End
BCC BLKTRU ; Return True
BCS BLKFLS ;Else Return False
2018-01-28 18:30:49 +00:00
;blkprv(n) - Move Block Pointer to Previous Segment
;Uses: BLKLEN - Block Segment Length
;Updates: BLKLO, BLKHI = Block Pointer
;Affects: C,N,Z
;Returns: A=$FF - Destination Pointer Moved
2018-07-31 00:03:56 +00:00
; A=$00 - Error: Pointer Underflow or Length 0
BLKPRV: LDA BLKLEN ;Get Segment Length
BEQ BLKRTS ;If 0 Return False
LDA BLKLO ;From Block Pointer LSB
2018-01-28 18:30:49 +00:00
SEC
2018-07-31 00:03:56 +00:00
SBC BLKLEN ;Subtract Segment Length
2018-01-28 18:30:49 +00:00
STA BLKLO
2018-07-31 00:03:56 +00:00
LDA BLKHI ;Subtract Borrow
SBC #0 ;from Block Pointer MSB
STA BLKHI ;If Block Pointer MSB
CMP BLKEHI ; >= Block Start MSB
BCS BLKTRU ; Exit True
LDA BLKLO ;Else If Destination LSB
BLKPRL: CMP BLKELO ; >= Block End LSB
BCS BLKTRU ; Return True
BCC BLKFLS ;Else Return False
2018-01-28 18:30:49 +00:00
;blksrt(&m) - Sort Block
;Args: m = Array of at least n Bytes for Temporary Storage
;Sets: TEMP1,TEMP2 = Pointer to Array m
;Uses: BLKLO, BLKHI = Block Pointer
; BLKSLO, BLKSHI = Block Start Address
; BLKELO, BLKEHI = Block End Address
; BLKLEN = Block Segment Size
; DSTPTR = Pointer to Segment in Block
; SRCPTR = Pointer to Segment in Block
2018-01-28 18:30:49 +00:00
;Affects: A,Y,C,N,Z
2018-07-31 00:03:56 +00:00
BLKSRT: LDA BLKLEN ;Get Segment Length
BEQ BLKRTS ;If 0 Return
STA TEMP0 ;Else Set Memory Swap Byte Count
JSR BLKRST ;Set Block Pointer to Block Start
BLKSRP: LDY #0 ;If First Byte of
LDA (BLKLO),Y ; Current Segment is Null
BEQ BLKRTS ; Return
JSR BLKSRC ;Copy Block Pointer to Source Pointer
JSR BLKDST ;Copy Block Pointer and Destination Pointer
BLKSRD: JSR BLKNXD ;Move Destination Pointer
BCS BLKSRE ;If Not Past Block End
LDY #0 ;
LDA (DSTPTR),Y ;and Destination String Populated
2018-07-31 00:03:56 +00:00
BEQ BLKSRE ;
JSR STRCML ; Compare Destination String with Source String
BPL BLKSRD ; If Destination < Source
JSR SETSRD ; Set Source Pointer to Destination Pointer
JMP BLKSRD ; Check Next Destination Segment
BLKSRE: LDA SRCPTR+1 ;If Source Pointer
2018-07-31 00:03:56 +00:00
CMP BLKHI ; <> Block Pointer
2018-01-28 18:30:49 +00:00
BNE BLKSRS
LDA SRCPTR
2018-01-28 18:30:49 +00:00
CMP BLKLO
BEQ BLKSRN
2018-07-31 00:03:56 +00:00
BLKSRS: JSR BLKSWL ; Swap Source and Pointer
BLKSRN: JSR BLKNXT ;Move Block Pointer
BNE BLKSRP ;If Not Past End, Check Next Segment
RTS ;Return
2018-01-28 18:30:49 +00:00
2018-07-31 00:03:56 +00:00
;blkstr(&s) - Search for String s in Block and
; Copy Segment to Destination Array if Found
;Args: Y,X = Address of Search String
;Uses: DSTPTR = Address of Destination Array
;Sets: SRCPTR = Pointer to Segment in Block (if found)
2018-07-31 00:03:56 +00:00
; TEMP0 = Index within segment
2018-01-28 18:30:49 +00:00
; TEMP1,TEMP2 = Destination String Pointer
;Affects: Y,C
;Returns: A=$FF,N=1,Z=0 if Found
; A=$00,N=0,Z=1 if Not Found
2018-07-31 00:03:56 +00:00
BLKSTR: JSR SETSRC ;Set Source Pointer to Search String
JSR SAVDST ;Save Destination Pointer
JSR GETSRC
LDA BLKLEN ;Get Segment Length
JSR MEMSRC ;Save Number of Bytes to Copy
JSR SETDSS ;Set Destination Pointer to Search String
LDX BLKSLO ;Set Source Pointer To Block Start
LDY BLKSHI
JSR SETSRC
LDX #SRCPTR ;Set BLKNXX Pointer to SRCPTR
2018-07-31 00:03:56 +00:00
BLKSTL: LDY #0 ;Initialize Index
JSR STRCML ;Compare Source to Destination
BEQ BLKSTS ;If Not Equal
JSR BLKNXX ; Move Source Pointer to Next Segment
BNE BLKSTL ; If Not Past Block End, Loop
JSR RESDST ; Else Restore Destination String and Return
LDA #$00 ; and Return FALSE
RTS ;Else
BLKSTS: JSR RESDST ; Restore Destination Pointer
JMP BLKCPY ; Copy Source to Destination
2018-01-28 18:30:49 +00:00
;blkswp(n, &m) - Swap n Bytes of Array with Block Segment
;Args: n = Number of bytes to swap
; m = Array to swap bytes from
;Sets: TEMP0 = Number of Bytes to Swap
;Uses: BLKLO, BLKHI = Block Pointer
; DSTPTR, DSTPTR+1 = Pointer to Temporary Storage Array
2018-01-28 18:30:49 +00:00
;Affects: A,Y,C,N,Z
2018-07-31 00:03:56 +00:00
BLKSWP: JSR MEMSRC ;Set Source Address and Index
BLKSWL: JSR BLKDST ;Set Destination Pointer to Block Pointer
LDY #0 ;Initialize Index
JMP MEMSWL ;Swap Bytes
2018-01-28 18:30:49 +00:00
2018-07-25 23:00:46 +00:00
;blksrc() - Set Source Pointer to Block Pointer
2018-01-28 18:30:49 +00:00
;Uses: BLKLO,BLKHI = Block Segment Pointer
;Sets: SRCPTR = Source Array Pointer
2018-01-28 18:30:49 +00:00
;Affects: X,N,Z
;Returns: Y = 0
2018-07-25 23:00:46 +00:00
BLKSRC: LDX BLKLO
2018-01-28 18:30:49 +00:00
LDY BLKHI
2018-07-31 00:03:56 +00:00
JMP SETSRC ;Set Source and Return
2018-01-28 18:30:49 +00:00
2018-07-31 00:03:56 +00:00
;blkdst() - Set Destination Pointer to Block Pointer
;Uses: BLKLO,BLKHI = Block Segment Pointer
;Sets: DSTPTR,DSTPTR+1 = Destination Array Pointer
2018-07-31 00:03:56 +00:00
;Affects: N,Z
;Returns: X = Block Pointer LSB
; Y = Block Pointer MSB
BLKDST: LDX BLKLO
LDY BLKHI
JMP SETDST ;Set Destination and Return
;blkdst() - Set Destination Pointer to Block Start
;Uses: BLKSLO,BLKSHI = Block Start Address
;Sets: DSTPTR,DSTPTR+1 = Destination Array Pointer
2018-01-28 18:30:49 +00:00
;Affects: N,Z
2018-07-31 00:03:56 +00:00
;Returns: X = Block Start LSB
; Y = Block Start MSB
BLKDSS: LDX BLKSLO
LDY BLKSHI
JMP SETDST ;Set Destination and Return
;Block Debug Routine
BLKDBG: LDA DSTPTR+1
2018-07-31 00:03:56 +00:00
CMP #$43
BNE BLKDBX
LDA DSTPTR
2018-07-31 00:03:56 +00:00
CMP #$57
BNE BLKDBX
NOP ;Break Point
BLKDBX: RTS
;Print Destination Address
BLKPDA LDA DSTPTR+1 ;Print Destination MSB
2018-07-31 00:03:56 +00:00
JSR PRBYTE
LDA DSTPTR ;Print Destination LSB
2018-07-31 00:03:56 +00:00
JSR PRBYTE
JMP PUTSPC ;Print Space
;Print Destination Address
BLKPSA LDA SRCPTR+1 ;Print Source MSB
2018-07-31 00:03:56 +00:00
JSR PRBYTE
LDA SRCPTR ;Print Source LSB
2018-07-31 00:03:56 +00:00
JSR PRBYTE
JMP PUTSPC ;Print Space
ENDSUBROUTINE