1
0
mirror of https://github.com/RevCurtisP/C02.git synced 2024-06-08 06:29:32 +00:00

Completed module 'block'

This commit is contained in:
Curtis F Kaylor 2018-07-30 20:03:56 -04:00
parent bbb55e2946
commit 40b51fcb36
4 changed files with 492 additions and 329 deletions

View File

@ -17,6 +17,7 @@ the variable length (at index 8).
Usage: at the beginning of the program use the directives Usage: at the beginning of the program use the directives
#include <stddef.h02>
#include <memory.h02> #include <memory.h02>
#include <string.h02> #include <string.h02>
#include <block.h02> #include <block.h02>
@ -37,74 +38,103 @@ The following application functions are defined:
a block is the byte after the last byte of memory in a block is the byte after the last byte of memory in
the block. the block.
Although a block usually begins on a 256 byte Although a block usually ends on a 256 byte
boundary, this is not required. boundary, this is not required.
Note: Sets variables blkslo and blkshi. Note: Sets variables blkelo and blkehi.
blkseg(n); Block Segment: Set block segment size to n. blkseg(n); Block Segment: Set block segment size to n.
Required before calls any calls that manipulate Required before calls any calls that manipulate
block segments. block segments.
Note: Sets blklen to n. Note: Sets blklen to n.
blkset(c); Block Set: Fill entire block with character c, blkset(c); Block Set: Fills entire block with character c,
leaving block pointer at end of block. leaving block pointer at end of block.
Used to initialize a block before use. Should be called with an argument of 0, to initialize
a block before use.
blkrst(); Block Reset: Set block segment pointer to block blkrst(); Block Reset: Sets block segment pointer to block
begin address. begin address.
This routine is called before populating a block with This routine is called before populating a block with
calls to the blkapd function. calls to the blkput function.
blknxt(); Block Next: Move block pointer forward by the segment blkptr(&d); Block Pointer: Sets the block pointer to the address
of b.
Does not validate that the address is within the
beginning and ending address of the block.
Note: Sets variables blkslo and blkshi.
blknxt(); Block Next: Moves block pointer forward by the segment
length set by a prior blkseg call. length set by a prior blkseg call.
If the block pointer is moved past the end of the If the block pointer is moved past the end of the
block, a value of 0 (false) is returned. Otherwise, block, or the segmebt length is set to 0, a value of
a value of 255 (true) is returned. #TRUE is returned. Otherwise, a value of #FALSE is
returned.
blkput(n ,&m); Block Append: Copy n bytes of array m to block blkprv(); Block Previous: Moves block pointer backward by the
at current pointer location, and moves block pointer segment length set by a prior blkseg call.
If the block pointer is moved past the beginning of
the block, or the segmebt length is set to 0, a value
of #TRUE is returned. Otherwise, a value of #FALSE is
returned.
blkput(n ,&m); Block Put: Copies n bytes of array m to a block at the
current pointer location, and moves block the pointer
forward by the segment length set by a prior blkseg forward by the segment length set by a prior blkseg
call. call.
If the appended bytes would overflow the end of the If the appended bytes would overflow the end of the
block, no bytes are copied and a value of 0 (false) block, no bytes are copied and a value of #FALSE is
is returned. Otherwise, the bytes are copied and returned. Otherwise, the bytes are copied and a value
a value of 255 (true) is returned. of #TRUE is returned.
Note: Sets dstlo and dsthi to the block pointer prior Note: Sets dstlo and dsthi to blklo and blkhi prior
to the copy, updates blkslo and blkshi, then calls to the copy, updates blklo and blkhi, then calls
the memcpy function. the memcpy function.
blkget(n ,&m); Block Get: Copy n bytes from block at current blkget(n ,&m); Block Get: Copies n bytes from a block at the current
pointer location to array m, and moves block pointer pointer location to array m, and moves the pointer
forward by the segment length set by a prior blkseg forward by the segment length set by a prior blkseg
call. call.
If the copied bytes would overflow the end of the If the copied bytes would overflow the end of the
block, no bytes are copied and a value of 0 (false) block, no bytes are copied and a value #FALSE is
is returned. Otherwise, the bytes are copied and returned. Otherwise, the bytes are copied and a value
a value of 255 (true) is returned. of #TRUE is returned.
Note: Sets dstlo and dsthi to the address of m, Note: Sets dstlo and dsthi to the address of m,
srclo and srchi to the block pointer prior to the srclo and srchi to blklo and blkhi prior to the
copy, updates blkslo and blkshi, then calls the copy, updates blkslo and blkshi, then calls the
memcpy function. memcpy function.
blkswp(n); Block Swap: Swaps n bytes of array m with the current
block segment (pointed to by the block pointer. The
block pointer is not changed.
Requires a prior call to the memdst function specifying
an array at least n bytes long, which is used for
temporary storage.
Note: Sets temp0 to n, copies blklo and blkhi to dstlo,
and dsthi, and calls memswp.
blkmem(n ,&m); Block Memory search: Search block for n byte long blkmem(n ,&m); Block Memory search: Search block for n byte long
segment matching array m. segment matching array m.
If a matching segment is found, the value 255 (true) If a matching segment is found, the value #TRUE is
is returned and the destination pointer is set to the returned and the destination pointer is set to the
address of the matching segment, allowing it to be address of the matching segment, allowing it to be
overwritten with a subsequent memcpy call. Otherwise, overwritten with a subsequent memcpy call. Otherwise,
the value 0 (false) is returned. the value #FALSE is returned.
Note: Sets srclo and srchi to the address of m, Note: Sets srclo and srchi to the address of m,
dstlo and dsthi to the address of the matching segment, dstlo and dsthi to the address of the matching segment,
and temp0 to n. Does not change the block pointer. and temp0 to n. Does not change the block pointer.
@ -121,17 +151,6 @@ The following application functions are defined:
segment, temp0 to n, and copies dstlo and dsthi to segment, temp0 to n, and copies dstlo and dsthi to
temp1 and temp2. Does not change the block pointer. temp1 and temp2. Does not change the block pointer.
blkswp(n); Block Swap: Swaps n bytes of array m with the current
block segment (pointed to by the block pointer. The
block pointer is not changed.
Requires a prior call to the memdst function specifying
an array at least n bytes long, which is used for
temporary storage.
Note: Sets temp0 to n, copies blklo and blkhi to dstlo,
and dsthi, and calls memswp.
blksrt(&m); Block Sort: Sort segments in block by initial string, blksrt(&m); Block Sort: Sort segments in block by initial string,
using array m as temporary storage. using array m as temporary storage.
@ -144,6 +163,14 @@ The following application functions are defined:
Note: Uses the selection sort algorithm. Sets temp1 Note: Uses the selection sort algorithm. Sets temp1
and temp2 to the address of array m. and temp2 to the address of array m.
blkdst(); Block Destination: Sets the destination pointer to the
block segment pointer.
This is a utility function called by other functions in
this module.
Note: Sets dstlo and dsthi to blklo and blkhi.
Note: This library expects the following functions to be defined Note: This library expects the following functions to be defined
setdst(&s); Set destination string pointer setdst(&s); Set destination string pointer

View File

@ -12,100 +12,121 @@
;Args: X,Y = Address ;Args: X,Y = Address
;Sets: BLKSLO, BLKSHI = Block Start Address ;Sets: BLKSLO, BLKSHI = Block Start Address
;Affects: Z, N ;Affects: Z, N
BLKBGN: STX BLKSLO ;Save Block Start Low Byte BLKBGN: STX BLKSLO ;Save Block Start LSB
STY BLKSHI ;Save Block Start High Byte STY BLKSHI ;Save Block Start MSB
RTS RTS
;blkend(&b) - Set Block End Address (+1) ;blkend(&b) - Set Block End Address (+1)
;Args: X,Y = Address ;Args: X,Y = Address
;Sets: BLKELO, BLKEHI = Block End Address ;Sets: BLKELO, BLKEHI = Block End Address
;Affects: Z, N ;Affects: Z, N
BLKEND: STX BLKELO ;Save Block End Low Byte BLKEND: STX BLKELO ;Save Block End LSB
STY BLKEHI ;Save Block End High Byte STY BLKEHI ;Save Block End MSB
RTS RTS
;blkrst() - Reset Block Segment Pointer to Start Address ;blkrst() - Reset Block Segment Pointer to Start Address
;Uses: BLKSLO, BLKSHI = Block Start Address ;Uses: BLKSLO, BLKSHI = Block Start Address
;Sets: BLKLO, BLKHI = Block Pointer ;Sets: BLKLO, BLKHI = Block Pointer
;Affects: Z, N ;Affects: Z, N
;Returns: X = Block Pointer Low Byte ;Returns: X = Block Pointer LSB
; Y = Block Pointer High Byte ; Y = Block Pointer MSB
BLKRST: LDX BLKSLO ;Load X with Block Start Low Byte BLKRST: LDX BLKSLO ;Load X with Block Start LSB
LDY BLKSHI ;Load X with Block Start High Byte LDY BLKSHI ;Load Y with Block Start MSB
;Set Block Address ;and fall into BLKPTR
BLKSXY: STX BLKLO ;Store X in Block Pointer Low Byte
STY BLKHI ;Store Y in Block Pointer High Byte ;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
RTS ;Exit Routine RTS ;Exit Routine
;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
;blkset(c) - Set All Bytes in Block to Character c ;blkset(c) - Set All Bytes in Block to Character c
;Args: c - Value to Set all Bytes to ;Args: c - Value to Set all Bytes to
;Uses: BLKSLO, BLKSHI = Block Start Address ;Uses: BLKSLO, BLKSHI - Block Start Address
; BLKELO, BLKEHI = Block End Address ; BLKELO, BLKEHI - Block End Address
; ;Sets: BLKLO, BLKHI = Start of Block
;Returns: A - Value of Argument n ;Returns: A - Value of Argument n
BLKSET: JSR BLKRST ;Reset Block Pointer BLKSET: JSR BLKRST ;Reset Block Pointer
LDY #0 ;Initialize Index LDY #0 ;Initialize Index
BLKSEL: LDX BLKHI ;Get Block Pointer High Byte BLKSEL: JSR BLKCPE ;Compare Pointer to Block End
CPX BLKEHI ;Compare to Block End High Byte BCS BLKSEX ;If Past End, Exit Routine
BNE BLKSES ; If Equal STA (BLKLO),Y ;Store Value at Block Pointer Address
LDX BLKLO ; Get Block Pointer Low Byte INC BLKLO ;Increment Block Pointer LSB
CPX BLKELO ; Compare to Block End Low Byte BNE BLKSEL ;If Not End of Page, Loop
BEQ BLKSEX ; If Equal Exit Routine INC BLKHI ;Increment Block Pointer MSB
BLKSES: STA (BLKLO),Y ;Store Value at Block Pointer Address BNE BLKSEL ;If Not End of Memory, Loop
INC BLKLO ;Increment Block Pointer Low Byte BLKSEX: JMP BLKRST ;Reset Block Pointer and Return
BNE BLKSEL ;If Not End of Page, Loop
INC BLKHI ;Increment Block Pointer High Byte
JMP BLKSEL ;If Not End of Memory, Loop
BLKSEX: RTS ;Return
;blkput(n, &m) - Append n Bytes of m to Block ;blkput(n, &m) - Append n Bytes of m to Block
;Args: n = Number of Bytes to Append ;Args: n = Number of Bytes to Append
; m = Array of Bytes to Append ; m = Array of Bytes to Append
;Uses: BLKELO, BLKEHI = Block End Address ;Uses: BLKELO, BLKEHI - Block End Address
;Sets: DSTLO, DSTHI = Pointer to Bytes in Block ;Sets: DSTLO, DSTHI = Pointer to Bytes in Block
; SRCHI, SRCLO = Pointer to m ; SRCHI, SRCLO = Pointer to m
; TEMP0 = Number of Bytes to Append ; TEMP0 = Number of Bytes to Append
;Updates: BLKLO, BLKHI = Block Pointer ;Updates: BLKLO, BLKHI = Block Pointer
;Returns: A=$FF - Append Successful ;Returns: A=$FF - Append Successful
; A=$00 - Block Overflow ; A=$00 - Block Overflow
BLKPUT: JSR SETSRC ;Save Source Pointer BLKPUT: JSR MEMSRC ;Set Source Address & Number of Bytes
JSR MEMSRA ;Save Number of Bytes to Append
JSR BLKDST ;Set Destination to Block Pointer JSR BLKDST ;Set Destination to Block Pointer
JMP BLKNCP ;Move Block Pointer and Copy JMP BLKNCP ;Move Block Pointer and Copy
;blkget(n, &m) - Read n Bytes of Block into Array m ;blkget(n, &m) - Read n Bytes of Current Segment into Array m
;Args: n = Number of Bytes to Read ;Args: A = Number of Bytes to Read
; m = Array to Read Bytes into ; Y,X = Address of Array
;Uses: BLKELO, BLKEHI = Block End Address ;Uses: BLKELO, BLKEHI = Block End Address
;Sets: DSTLO, DSTHI = Pointer to Bytes in Block ;Sets: DSTLO, DSTHI = Address of Array
; SRCHI, SRCLO = Pointer to m ; SRCHI, SRCLO = Current Pointer in Block
; TEMP0 = Number of Bytes to Append ; TEMP0 = Number of Bytes to Append
;Updates: BLKLO, BLKHI = Block Pointer ;Updates: BLKLO, BLKHI = Next Segment in Block
;Returns: A=$FF - Append Successful ;Returns: A=$FF - Append Successful
; A=$00 - Block Overflow ; A=$00 - Block Overflow
BLKGET: JSR MEMSRC ;Set Source Address & Number of Bytes BLKGET: JSR SETDST ;Set Destination Address
JSR BLKSRC ;Set Source Pointer to Block Pointer JSR MEMSRA ;Save Number of Bytes to Copy
BLKNCP: JSR BLKNXT ;Move Block Pointer JSR BLKSRC ;Set Source Pointer to Block Pointer
BEQ BLKRTS ;If Past End of Block, Return False BLKNCP: JSR BLKNXT ;Move Block Pointer
JMP BLKCPY ;Else Copy Bytes BEQ BLKRTS ;If Past End of Block, Return False
BLKCPY: LDY #0 ;Copy Source To Destination
JSR MEMCPL ;
LDA #$FF ;Return TRUE
RTS
;blkmem(n, &m) - Search for n Bytes of m in Block ;blkmem(n, &m) - Search for n Bytes of m in Block
;Args: n = Segment Size to Search in Bytes ;Args: n = Number of Bytes to Compare
; m = Array of Bytes to Search For ; m = Array of Bytes to Search For
;Sets: DSTLO, DSTHI = Pointer to Segment in Block ;Sets: DSTLO, DSTHI = Pointer to Segment in Block
; SRCHI, SRCLO = Pointer to m ; SRCHI, SRCLO = Pointer to m
; TEMP0 = Number of Bytes to Append ; TEMP0 = Number of Bytes to Compare
;Returns: A=$FF - Bytes found ;Returns: A=$FF - Bytes found
; A=$00 - Bytes not found ; A=$00 - Bytes not found
BLKMEM: JSR MEMSRC ;Initialize Source, Index, and Count BLKMEM: JSR MEMSRC ;Initialize Source, Index, and Count
JSR BLKSRS ;Set Destination Pointer To Block Start JSR BLKDSS ;Set Destination Pointer To Block Start
BLKMEL: LDY #0 ;Initialize Index LDX #DSTLO ;Set BLKNXX Pointer to SRCLO,SRCHI
JSR MEMCML ;Compare Source to Destination BLKMEL: LDY #0 ;Initialize Index
BEQ BLKTRU ;If Equal, Exit TRUE JSR MEMCML ;Compare Source to Destination
JSR BLKDSN ;Move Destination Pointer BEQ BLKTRU ;If Equal, Exit TRUE
BNE BLKMEL ;If Not Past Block End, Loop JSR BLKNXX ;Move Destination Pointer
;Else BNE BLKMEL ;If Not Past Block End, Loop
BLKFLS: LDA #$00 ; Return FALSE BLKFLS: LDA #FALSE ; Return FALSE
RTS RTS
;blkseg(n) - Set Block Segment Length ;blkseg(n) - Set Block Segment Length
@ -113,73 +134,82 @@ BLKFLS: LDA #$00 ; Return FALSE
;Sets: TEMP0 = Segment Size ;Sets: TEMP0 = Segment Size
;Returns: A=$FF - Segment Length Successfully Set ;Returns: A=$FF - Segment Length Successfully Set
; A=$00 - Error: Segment Length not Set ; A=$00 - Error: Segment Length not Set
BLKSEG: STA BLKLEN ;Store Argument in Block Length BLKSEG: STA BLKLEN ;Store Argument in Block Length
ORA #0 ;If Segment Length is Zero ORA #0 ;If Segment Length is Zero
BEQ BLKFLS ; Return False BEQ BLKFLS ; Return False
;Else BLKTRU: LDA #TRUE ;Return TRUE
BLKTRU: LDA #$FF ; Return TRUE
BLKRTS: RTS BLKRTS: RTS
;blkdsn() - Move Destination Pointer to Next Segment ;BLKNXD() - Move Destination Pointer to Next Segment
;Uses: BLKLEN - Block Segment Length ;Uses: BLKLEN - Block Segment Length
;Updates: DSTLO, DSTHI = Block Pointer ;Updates: DSTLO, DSTHI = Block Pointer
;Affects: C,N,Z ;Affects: C,N,Z
;Returns: A=$FF - Destination Pointer Moved ;Returns: A=$FF - Destination Pointer Moved
; A=$00 - Error: Pointer Overflow or Length 0 ; A=$00 - Error: Pointer Overflow or Length 0
BLKDSN: LDA BLKLEN ;Get Segment Length BLKNXD: LDX #DSTLO ;Set X to Destination Pointer
BEQ BLKRTS ;If 0 Return False JMP BLKNXX ;and Move to Next Segment
;blknxx() - Move Pointer to Next Segment
;Args: X = Zero Page Address of Pointer
;Uses: BLKLEN - Block Segment Length
;Updates: DSTLO, DSTHI = Block Pointer
;Affects: C,N,Z
;Returns: A=$FF - Destination Pointer Moved
; A=$00 - Error: Pointer Overflow or Length 0
BLKNXX: LDA BLKLEN ;Get Segment Length
BEQ BLKRTS ;If 0 Return False
CLC CLC
ADC DSTLO ;Add to Destination Low Byte ADC 0,X ;Add to Pointer LSB
STA DSTLO STA 0,X
LDA DSTHI ;Add Carry LDA 1,X ;Add Carry
ADC #0 ;to Destination High Byte ADC #0 ;to Pointer MSB
STA DSTHI ;If Destination High Byte STA 1,X ;If Destination MSB
CMP BLKEHI ; < Block End High Byte CMP BLKEHI ; < Block End MSB
BCC BLKTRU ; Return True BCC BLKTRU ; Return True
LDA DSTLO ;Else LDA 0,X ;If Destination LSB
JMP BLKNXL ; Check Destination Low Byte CMP BLKELO ; < Block End LSB
BCC BLKTRU ; Return True
BCS BLKFLS ;Return False
;blknxt(n) - Move Block Pointer to Next Segment ;blknxt(n) - Move Block Pointer to Next Segment
;Uses: BLKLEN - Block Segment Length ;Uses: BLKLEN - Block Segment Length
;Updates: BLKLO, BLKHI = Block Pointer ;Updates: BLKLO, BLKHI = Block Pointer
;Affects: C,N,Z ;Affects: C,N,Z
;Returns: A=$FF - Destination Pointer Moved ;Returns: A=$FF - Destination Pointer Moved
; A=$00 - Error: Pointer Overflow or Length 0 ; A=$00 - Error: Pointer Overflow or Length 0
BLKNXT: LDA BLKLEN ;Get Segment Length BLKNXT: LDA BLKLEN ;Get Segment Length
BEQ BLKRTS ;If 0 Return False BEQ BLKRTS ;If 0 Return False
CLC CLC
ADC BLKLO ;Add to Block Pointer Low Byte ADC BLKLO ;Add to Block Pointer LSB
STA BLKLO STA BLKLO
LDA BLKHI ;Add Carry LDA BLKHI ;Add Carry
ADC #0 ;to Block Pointer High Byte ADC #0 ;to Block Pointer MSB
STA BLKHI ;If Block Pointer High Byte STA BLKHI ;If Block Pointer MSB
CMP BLKEHI ; < Block End High Byte JSR BLKCPE ;Compare Pointer to End
BCC BLKTRU ; Exit True BCC BLKTRU ; Return True
LDA BLKLO ;Else If Destination Low Byte BCS BLKFLS ;Else Return False
BLKNXL: CMP BLKELO ; < Block End low Byte
BCC BLKTRU ; Return True
BCS BLKFLS ;Else Return False
;blkprv(n) - Move Block Pointer to Previous Segment ;blkprv(n) - Move Block Pointer to Previous Segment
;Uses: BLKLEN - Block Segment Length ;Uses: BLKLEN - Block Segment Length
;Updates: BLKLO, BLKHI = Block Pointer ;Updates: BLKLO, BLKHI = Block Pointer
;Affects: C,N,Z ;Affects: C,N,Z
;Returns: A=$FF - Destination Pointer Moved ;Returns: A=$FF - Destination Pointer Moved
; A=$00 - Error: Pointer Overflow or Length 0 ; A=$00 - Error: Pointer Underflow or Length 0
BLKPRV: LDA BLKLEN ;Get Segment Length BLKPRV: LDA BLKLEN ;Get Segment Length
BEQ BLKRTS ;If 0 Return False BEQ BLKRTS ;If 0 Return False
LDA BLKLO ;From Block Pointer LSB
SEC SEC
SBC BLKLO ;Add to Block Pointer Low Byte SBC BLKLEN ;Subtract Segment Length
STA BLKLO STA BLKLO
LDA BLKHI ;Add Carry LDA BLKHI ;Subtract Borrow
SBC #0 ;to Block Pointer High Byte SBC #0 ;from Block Pointer MSB
STA BLKHI ;If Block Pointer High Byte STA BLKHI ;If Block Pointer MSB
CMP BLKEHI ; >= Block Start High Byte CMP BLKEHI ; >= Block Start MSB
BCS BLKTRU ; Exit True BCS BLKTRU ; Exit True
LDA BLKLO ;Else If Destination Low Byte LDA BLKLO ;Else If Destination LSB
BLKPRL: CMP BLKELO ; >= Block End low Byte BLKPRL: CMP BLKELO ; >= Block End LSB
BCS BLKTRU ; Return True BCS BLKTRU ; Return True
BCC BLKFLS ;Else Return False BCC BLKFLS ;Else Return False
;blksrt(&m) - Sort Block ;blksrt(&m) - Sort Block
;Args: m = Array of at least n Bytes for Temporary Storage ;Args: m = Array of at least n Bytes for Temporary Storage
@ -191,63 +221,66 @@ BLKPRL: CMP BLKELO ; >= Block End low Byte
; DSTLO, DSTHI = Pointer to Segment in Block ; DSTLO, DSTHI = Pointer to Segment in Block
; SRCHI, SRCLO = Pointer to Segment in Block ; SRCHI, SRCLO = Pointer to Segment in Block
;Affects: A,Y,C,N,Z ;Affects: A,Y,C,N,Z
BLKSRT: LDA BLKLEN ;Get Segment Length BLKSRT: LDA BLKLEN ;Get Segment Length
BEQ BLKRTS ;If 0 Return BEQ BLKRTS ;If 0 Return
STA TEMP0 ;Else Set Memory Swap Byte Count STA TEMP0 ;Else Set Memory Swap Byte Count
JSR BLKRST ;Set Block Pointer to Block Start JSR BLKRST ;Set Block Pointer to Block Start
BLKSRP: LDY #0 ;If First Byte of BLKSRP: LDY #0 ;If First Byte of
LDA (BLKLO),Y ; Current Segment is Null LDA (BLKLO),Y ; Current Segment is Null
BEQ BLKRTS ; Return BEQ BLKRTS ; Return
JSR BLKSRC ;Copy Block Pointer to Source Pointer JSR BLKSRC ;Copy Block Pointer to Source Pointer
JSR BLKDST ;Copy Block Pointer and Destination Pointer JSR BLKDST ;Copy Block Pointer and Destination Pointer
BLKSRD: JSR BLKDSN ;Move Destination Pointer BLKSRD: JSR BLKNXD ;Move Destination Pointer
BCS BLKSRC ;If Not Past Block End BCS BLKSRE ;If Not Past Block End
LDY #0 ; LDY #0 ;
LDA (DSTLO),Y ;and Destination String Populated LDA (DSTLO),Y ;and Destination String Populated
BEQ BLKSRC ; BEQ BLKSRE ;
JSR STRCML ; Compare Destination String with Source String JSR STRCML ; Compare Destination String with Source String
BPL BLKSRD ; If Destination < Source BPL BLKSRD ; If Destination < Source
JSR SETSRD ; Set Source Pointer to Destination Pointer JSR SETSRD ; Set Source Pointer to Destination Pointer
JMP BLKSRD ; Check Next Destination Segment JMP BLKSRD ; Check Next Destination Segment
BLKSRC: LDA SRCHI ;If Source Pointer BLKSRE: LDA SRCHI ;If Source Pointer
CMP BLKHI ; <> Block Pointer CMP BLKHI ; <> Block Pointer
BNE BLKSRS BNE BLKSRS
LDA SRCLO LDA SRCLO
CMP BLKLO CMP BLKLO
BEQ BLKSRN BEQ BLKSRN
BLKSRS: JSR BLKSWL ; Swap Source and Pointer BLKSRS: JSR BLKSWL ; Swap Source and Pointer
BLKSRN: JSR BLKNXT ;Move Block Pointer BLKSRN: JSR BLKNXT ;Move Block Pointer
BNE BLKSRP ;If Not Past End, Check Next Segment BNE BLKSRP ;If Not Past End, Check Next Segment
RTS ;Return RTS ;Return
;blkstr(n, &s) - Search for String s in Block and ;blkstr(&s) - Search for String s in Block and
; Copy Segment to Destination Array if Found ; Copy Segment to Destination Array if Found
;Args: n = Segment Size to Search in Bytes ;Args: Y,X = Address of Search String
; s = String ;Uses: DSTLO.DSTHI = Address of Destination Array
;Sets: SRCLO,SRCHI = Pointer to Segment in String (if found) ;Sets: SRCLO,SRCHI = Pointer to Segment in Block (if found)
; TEMP0 = Segment Size ; TEMP0 = Index within segment
; TEMP1,TEMP2 = Destination String Pointer ; TEMP1,TEMP2 = Destination String Pointer
;Affects: Y,C ;Affects: Y,C
;Returns: A=$FF,N=1,Z=0 if Found ;Returns: A=$FF,N=1,Z=0 if Found
; A=$00,N=0,Z=1 if Not Found ; A=$00,N=0,Z=1 if Not Found
BLKSTR: JSR MEMSRC ;Initialize Source, Index, and Count BLKSTR: JSR SETSRC ;Set Source Pointer to Search String
JSR SAVDST ;Save Destination Pointer JSR SAVDST ;Save Destination Pointer
JSR BLKSRS ;Set Destination Pointer To Block Start JSR GETSRC
BLKSTL: LDY #0 ;Initialize Index LDA BLKLEN ;Get Segment Length
JSR STRCML ;Compare Source to Destination JSR MEMSRC ;Save Number of Bytes to Copy
BEQ BLKSTS ;If Not Equal JSR SETDSS ;Set Destination Pointer to Search String
JSR BLKDSN ; Move Destination Pointer LDX BLKSLO ;Set Source Pointer To Block Start
BNE BLKSTL ; If Not Past Block End, Loop LDY BLKSHI
BLKSTF: JSR RESDST ; Else Restore Destination String and Return JSR SETSRC
LDA #$00 ; and Return FALSE LDX #SRCLO ;Set BLKNXX Pointer to SRCLO,SRCHI
RTS ;Else BLKSTL: LDY #0 ;Initialize Index
BLKSTS: JSR SETSRD ; Set Source Pointer to Destination Pointer JSR STRCML ;Compare Source to Destination
JSR RESDST ; Restore Destination String Pointer BEQ BLKSTS ;If Not Equal
BLKCPY: LDY #0 ; Initialize Index JSR BLKNXX ; Move Source Pointer to Next Segment
JSR MEMCPL ; Copy Segment to Destination String BNE BLKSTL ; If Not Past Block End, Loop
LDA #$FF ; Return TRUE JSR RESDST ; Else Restore Destination String and Return
RTS LDA #$00 ; and Return FALSE
RTS ;Else
BLKSTS: JSR RESDST ; Restore Destination Pointer
JMP BLKCPY ; Copy Source to Destination
;blkswp(n, &m) - Swap n Bytes of Array with Block Segment ;blkswp(n, &m) - Swap n Bytes of Array with Block Segment
;Args: n = Number of bytes to swap ;Args: n = Number of bytes to swap
; m = Array to swap bytes from ; m = Array to swap bytes from
@ -255,20 +288,10 @@ BLKCPY: LDY #0 ; Initialize Index
;Uses: BLKLO, BLKHI = Block Pointer ;Uses: BLKLO, BLKHI = Block Pointer
; DSTLO, DSTHI = Pointer to Temporary Storage Array ; DSTLO, DSTHI = Pointer to Temporary Storage Array
;Affects: A,Y,C,N,Z ;Affects: A,Y,C,N,Z
BLKSWP: JSR MEMSRC ;Set Source Address and Index BLKSWP: JSR MEMSRC ;Set Source Address and Index
BLKSWL: JSR BLKDST ;Set Destination Pointer to Block Pointer BLKSWL: JSR BLKDST ;Set Destination Pointer to Block Pointer
LDY #0 ;Initialize Index LDY #0 ;Initialize Index
JMP MEMSWL ;Swap Bytes JMP MEMSWL ;Swap Bytes
;blkdst() - Set Destination Pointer to Block Pointer
;Uses: BLKLO,BLKHI = Block Segment Pointer
;Sets: DSTLO,DSTHI = Destination Array Pointer
;Affects: N,Z
;Returns: X = Block Pointer Low Byte
; Y = Block Pointer High Byte
BLKDST: LDX BLKLO
LDY BLKHI
JMP SETDST ;Set Destination and Return
;blksrc() - Set Source Pointer to Block Pointer ;blksrc() - Set Source Pointer to Block Pointer
;Uses: BLKLO,BLKHI = Block Segment Pointer ;Uses: BLKLO,BLKHI = Block Segment Pointer
@ -277,14 +300,48 @@ BLKDST: LDX BLKLO
;Returns: Y = 0 ;Returns: Y = 0
BLKSRC: LDX BLKLO BLKSRC: LDX BLKLO
LDY BLKHI LDY BLKHI
JMP SETSRC ;Set Source and Return JMP SETSRC ;Set Source and Return
;blksrs() - Set Destination Pointer to Block Start Address ;blkdst() - Set Destination Pointer to Block Pointer
;Uses: BLKSLO, BLKSHI = Block Start Address ;Uses: BLKLO,BLKHI = Block Segment Pointer
;Sets: DSTLO, DSTHI = Block Pointer ;Sets: DSTLO,DSTHI = Destination Array Pointer
;Affects: N,Z ;Affects: N,Z
;Returns: X = Block Pointer Low Byte ;Returns: X = Block Pointer LSB
; Y = Block Pointer High Byte ; Y = Block Pointer MSB
BLKSRS: LDX BLKSLO ;Load Block Start Low Byte BLKDST: LDX BLKLO
LDY BLKSHI ;Load Block Start High Bytes LDY BLKHI
JMP SETDST ;Set Destination and Return JMP SETDST ;Set Destination and Return
;blkdst() - Set Destination Pointer to Block Start
;Uses: BLKSLO,BLKSHI = Block Start Address
;Sets: DSTLO,DSTHI = Destination Array Pointer
;Affects: N,Z
;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 DSTHI
CMP #$43
BNE BLKDBX
LDA DSTLO
CMP #$57
BNE BLKDBX
NOP ;Break Point
BLKDBX: RTS
;Print Destination Address
BLKPDA LDA DSTHI ;Print Destination MSB
JSR PRBYTE
LDA DSTLO ;Print Destination LSB
JSR PRBYTE
JMP PUTSPC ;Print Space
;Print Destination Address
BLKPSA LDA SRCHI ;Print Source MSB
JSR PRBYTE
LDA SRCLO ;Print Source LSB
JSR PRBYTE
JMP PUTSPC ;Print Space

View File

@ -27,6 +27,20 @@ char blkget();
* $00 if not found */ * $00 if not found */
char blkmem(); char blkmem();
/* Move Block Pointer to Next Segment *
* Returns: A=$FF if successful *
* $00 if there was an error */
void blknxt();
/* Set Block Pointer Address *
* Args: &d - Address to Set Pointer to */
char blkptr();
/* Move Block Pointer to Previous Segment *
* Returns: A=$FF if successful *
* $00 if there was an error */
void blkprv();
/* Write Bytes to Block * /* Write Bytes to Block *
* Args: n - Number of bytes to write * * Args: n - Number of bytes to write *
* &m - Array containing bytes to write * * &m - Array containing bytes to write *
@ -37,13 +51,18 @@ char blkput();
/* Set Block Pointer to Block Start */ /* Set Block Pointer to Block Start */
void blkrst(); void blkrst();
/* Set Block Segment Length *
* Args: n - Length of block segment *
* Returns: Fill character */
char blkseg();
/* Fill Block with Character * /* Fill Block with Character *
* Args: c - Character to fill block with * * Args: c - Character to fill block with *
* Returns: Fill character */ * Returns: Fill character */
char blkset(); char blkset();
/* Sort Segments in Block * /* Sort Segments in Block *
* Args: &m - Array containing bytes to swap */ * Args: &m - Temporary storage array */
char blksrt(); char blksrt();
/* Search Block for String * /* Search Block for String *

View File

@ -1,46 +1,208 @@
/******************************************* /*******************************************
* TESTBLK - Test Array Handling Functions * * TESTBLK - Test Array Handling Functions *
*******************************************/ *******************************************/
//todo: Test blknxt() and blkprv() //todo: Test blknxt() and blkprv()
#include <py65.h02> #include <py65.h02>
#include <stdio.h02> #include <stddef.h02>
#include <stdlib.h02> #include <stdlib.h02>
#include <stdio.h02>
#include <stdiox.h02>
#include <string.h02> #include <string.h02>
#include <test.h02>
#include <memory.h02> #include <memory.h02>
#include <block.h02> #include <block.h02>
char TRUE=$FF, FALSE=$00;
char c, i, n, r, z; char c, i, n, r, z;
char savlo,savhi,tmplo,tmphi; //Address Pointer char savlo,savhi,tmplo,tmphi; //Address Pointer
char chkhi,chklo; //Address Check
char number[5]; char number[5];
char numbrs = "zero one two threefour five six seveneightnine ten"; char numbrs = "zero one two threefour five six seveneightnine ten";
char sorted = {8,5,4,9,1,7,6,3,2,0};
char name1 = "Dick", name2 = "Jane"; char name1 = "Dick", name2 = "Jane";
char block[255]; //Array to Use as Block
char temp[127]; char temp[127];
char seglo[9],seghi[9];
char aa,xx,yy;
char seglen = 7; struct ssgmt {
char sgmt[6]; //Structure: [0-5]=Text, [6]=Binary char key[6];
char seg1[6],seg2[6]; char flag;
char value;
};
char pass = " Pass "; struct ssgmt sgmt;
char fail = " Fail "; struct ssgmt seg1;
struct ssgmt seg2;
struct ssgmt tseg;
main:
puts("blkbgn(&4321); ");
blkbgn(&$4321); //Set Block Start
setchk(&$4321); chkadr(blkshi,blkslo);
puts("blkend(&$4567); ");
blkend(&$4567); //Set Block End
setchk(&$4567); chkadr(blkehi,blkelo);
puts("blkseg(@sgmt); ");
blkseg(@sgmt);
printf(blklen,"blklen=%d:");
if (blklen == @sgmt) passln(); else failln();
puts("blkrst(); ");
blkrst();
setchk(&$4321); chkadr(blkhi,blklo);
puts("blknxt(); ");
blknxt();
setchk(&$432A); chkadr(blkhi,blklo);
puts("blkprv(); ");
blkprv();
setchk(&$4321); chkadr(blkhi,blklo);
puts("blkset('@'); ");
blkset('@'); r=chkptr(&$4321);
while (blkget(1,&sgmt)) {if (sgmt<>'@') r=#FALSE;} psflln(r);
newlin();//if (anykey()==#ESCKEY) goto exit;
setdst(&seg1.key); strcpy(&name1);seg1.flag=#TRUE; seg1.value = 1;
puts("seg1=");prtseg(&seg1);
setdst(&seg2.key); strcpy(&name2);seg2.flag=#FALSE;seg2.value = 2;
puts("seg2=");prtseg(&seg2);
setdst(&sgmt);memcpy(@seg2,&seg2);
puts("sgmt=");prtseg(&sgmt);
newlin();
puts("blkput(@seg1,&seg1); ");
blkrst(); blkput(@seg1,&seg1); r=chkptr(&$432A);
memdst(&$4321); r = chkmem(@seg1,&seg1) & r;
psflln(r);
puts(" _blk=");prtseg(&$4321);
newlin();
puts("blkswp(@sgmt,&sgmt); ");
blkrst(); blkswp(@sgmt,&sgmt); r = chkptr(&$4321);
memdst(&$4321); r = chkmem(@seg2,&seg2) & r;
memdst(&sgmt); r = chkmem(@seg1,&seg1) & r;
psflln(r);
puts(" _blk=");prtseg(&$4321);
puts(" sgmt=");prtseg(&sgmt);
anykey();
puts("blkput(@sgmt, &sgmt);\n"); blkset(0); blkrst(); r = #TRUE;
for (i=0; i<10; i++) {
printf(i,"%d: "); nsgmt(i,&sgmt); savlo=blklo;savhi=blkhi;
blkput(@sgmt,&sgmt); seglo[i]=savlo; seghi[i]=savhi;
putadr(*,savhi,savlo); puts(" _blk="); prtseg(*,savhi,savlo);
setdst(*,savhi,savlo); if (memcmp(@sgmt,&sgmt)) r = #FALSE;
}
puts("blkput:"); psflln(r); newlin();
puts("blkget(@temp, &temp);\n"); blkrst(); r = #TRUE;
for (i=0; i<10; i++) {
printf(i,"%d: "); nsgmt(i,&sgmt); memclr(@temp, &temp);
putadr(*,blkhi,blklo); r = blkget(@temp, &temp);
setdst(&temp); if (memcmp(@sgmt, &sgmt)) r = #FALSE;
puts(" temp="); prtseg(&temp);
}
puts("blkget:"); psflln(r); anykey();
puts("blkmem(@sgmt, &sgmt);\n"); r = #TRUE;
for (i=9; i:+; i--) {
printf(i,"%d: "); nsgmt(i,&sgmt);
blkmem(@sgmt,&sgmt);
if (dstlo<>seglo[i]) r = #FALSE;
if (dsthi<>seghi[i]) r = #FALSE;
putadr(*,dsthi,dstlo); puts(" _blk="); prtseg(*,dsthi,dstlo);
}
puts("blkmem:"); psflln(r); newlin();
puts("blkstr(@sgmt, &sgmnt);\n"); r = #TRUE;
for (i=0; i<10; i++) {
printf(i,"%d: "); nsgmt(i,&sgmt); memclr(@temp, &temp);
setdst(&temp); blkstr(&sgmt.key);
if (srclo<>seglo[i]) r = #FALSE;
if (srchi<>seghi[i]) r = #FALSE;
putadr(*,srchi,srclo); puts(" temp="); prtseg(&temp);
if (memcmp(@sgmt, &sgmt)) r = #FALSE;
i++; if (i=9) i=0; //evens then odds
}
puts("blkstr:"); psflln(r); anykey();
puts("blksrt(&temp);\n"); r = #TRUE; blksrt(&temp);blkrst();
for (i=0; i<10; i++) {
printf(i,"%d: "); nsgmt(sorted[i],&sgmt); memclr(@temp, &temp);
putadr(*,blkhi,blklo); r = blkget(@temp, &temp);
setdst(&temp); if (memcmp(@sgmt, &sgmt)) r = #FALSE;
puts(" temp="); prtseg(&temp);
}
puts("blksrt:"); psflln(r); newlin();
putln("Tests complete.");
goto exit;
char chkptr() {
setchk(); //pass through args
putadr(*,blkhi,blklo);
return cmpadr(*,blkhi,blklo);
}
void setchk() {
chklo=X; chkhi=Y;
}
void chkadr(tmphi,tmplo) {
putadr(*,tmphi,tmplo);
cmpadr(*,tmphi,tmplo);
psflln(tmplo & tmphi);
}
char cmpadr(aa,yy,xx) {
xx = (chklo == xx) ? #TRUE : #FALSE;
yy = (chkhi == yy) ? #TRUE : #FALSE;
return xx & yy;
}
void clrseg() {
setdst(); memset(0, @sgmt);
}
void prtseg(aa,yy,xx) {
setdst(&tseg); memcpy(@tseg,yy,xx);
setdst(&tseg.key); printf("key=\"%s\",");
puts("flag=");if (tseg.flag) puts("TRUE"); else puts("FALSE");
printf(tseg.value,",value=%d\n");
}
char chkmem() {
aa = memcmp(); //pass through args
aa = (aa==0) ? #TRUE : #FALSE;
return aa;
}
//Copy Textual Representation of n to temp //Copy Textual Representation of n to temp
void numcpy(n) { void numstr(n,yy,xx) {
strdst(&temp); strdst(&temp); strcut(mult(n,5),&numbrs); //Copy Representation
strcut(mult(n,5), &numbrs); //Copy Representation
temp[5] = 0; //Cut off remainder of String temp[5] = 0; //Cut off remainder of String
z = strchr(' ', &temp); z=strchr(' ', &temp); if (z:+) temp[z]=0; //and terminate at space
if (z:+) //Search for space in string setdst(*,yy,xx); strcpy(&temp);
temp[z] = 0; //and terminate at that point
} }
//Build String Segment in Temp //Build String Segment in Temp
void numtmp(n) { void nsgmt(n,tmphi,tmplo) {
numcpy(n); //Copy Textual Representation to temp[0:5] memclr(@tseg,&tseg);
temp[6] = i; //Set numstr(n,&tseg.key);
tseg.flag = trufls(n);
tseg.value = n;
setdst(*,tmphi,tmplo); memcpy(@tseg,&tseg);
} }
//Print Addresses //Print Addresses
@ -59,105 +221,3 @@ void prbadr() {
prbyte(blkhi);prbyte(blklo);newlin(); prbyte(blkhi);prbyte(blklo);newlin();
} }
main:
puts("blkbgn(&$4000);");
blkbgn(&$4000); //Set Block Start
if (blkshi == $40) putln(&pass); else putln(&fail);
puts("blkend(&$4400);");
blkend(&$4400); //Set Block End
if (blkehi == $44) putln(&pass); else putln(&fail);
puts("blkseg(seglen);");
blkseg(seglen);
if (blklen == seglen) putln(&pass); else putln(&fail);
puts("blkrst(); ");
blkrst();
if (blkhi == $40) putln(&pass); else putln(&fail);
puts("blkset('@'); ");
blkset('@');
r = $FF;
blkrst();
while (blkget(1,&sgmt)) {
if (sgmt <> '@') r = $00;
}
if (r) putln(&pass); else putln(&fail);
setdst(&seg1); strcpy(&name1);seg1[6] = 1;
setdst(&seg2); strcpy(&name2);seg2[6] = 2;
puts("blkswp(seglen, &seg2);");
blkrst();
blkput(seglen, &seg1);
blkrst();
setdst(&temp);
blkswp(seglen, &seg2);
r = $FF;
blkget(seglen, &sgmt);
if (strcmp(&name2)) r = $00;
if (sgmt[6] <> 2) r = $00;
setdst(&seg2);
if (strcmp(&name1)) r = $00;
if (seg2[6] <> 1) r = $00;
if (r) putln(&pass); else putln(&fail);
blkset(0);
puts("blkput(seglen, &temp);");
r = TRUE;
blkrst();
for (i=0; i<10; i++) {
numtmp(i);
savlo=blklo;savhi=blkhi;
blkput(seglen, &temp);
dstlo=savlo;dsthi=savhi;
if (memcmp(seglen,&temp)) r = FALSE;
}
if (blklo <> 70) r = TRUE;
if (r) putln(&pass); else putln(&fail);
puts("blkmem(seglen, &temp);");
r = $FF;
for (i=9; i:+; i--) {
numtmp(i);
r = blkmem(seglen, &temp);
if (mult(seglen,i) <> dstlo) r = 0;
}
if (r) putln(&pass); else putln(&fail);
puts("blkstr(seglen, &temp);");
r = $FF;
for (i=0; i<10; i++) {
numtmp(i);
setdst(&sgmt);
r = blkstr(seglen, &temp);
if (sgmt[6] <> i) r = 0;
}
if (r) putln(&pass); else putln(&fail);
blkrst();
puts("blkget(seglen, &sgmt);");
r = $FF;
for (i=0; i<10; i++) {
numtmp(i);
setdst(&sgmt);
r = blkget(seglen, &sgmt);
if (memcmp(seglen, &temp)) r = 0;
}
if (r) putln(&pass); else putln(&fail);
puts("blksrt(&temp);");
blksrt(&temp);
r = TRUE;
seg1[0] = 0;
blkrst();
while(blkget(seglen, &seg2)) {
if (!seg2[0]) break;
if (strcmp(&seg1)<>1) r = FALSE;
strdst(&seg1);strcpy(&seg2);
}
if (r) putln(&pass); else putln(&fail);
goto exit;