Skip to content

Commit

Permalink
Adding CopyWithRelocationWithBank
Browse files Browse the repository at this point in the history
  • Loading branch information
intoinside committed Jun 20, 2024
1 parent 4c15908 commit 43242b9
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 6 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ retroProject {

viceExecutable = 'x128'

libFromGitHub "c128lib/labels", "0.2.0"
libFromGitHub "c128lib/labels", "0.3.0"
libFromGitHub "c128lib/128spec", "0.7.2"
}
26 changes: 26 additions & 0 deletions lib/mem-global.asm
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,32 @@
*/
.macro @c128lib_CopyWithRelocation(source, destination) { CopyWithRelocation(source, destination) }

/**
* @brief This macro copies a block of memory from one location to another using page relocation
* with bank selection.
*
* @details This macro also handles the page relocation of the memory block during the copy operation.
* It's slower than @sa CopyFast but it uses much less memory, especially for large memory blocks copy.
* This version allows to specify the bank for source and destination memory locations.
*
* @param[in] source The starting address of the memory block to be copied.
* @param[in] sourceBank Bank of the starting address.
* @param[in] destination The starting address of the location where the memory block will be copied to.
* @param[in] destinationBank Bank of the destination address.
*
* @remark Registers .A and .X will be modified.
* @remark Flags N and Z will be affected.
* @remark Zeropage location $FE will be used.
* @remark During copy, interrupts are disabled.
*
* @note The number of bytes that can be copied is always 256.
* @note Source and destination less significant byte should be $00. Any
* different value will be ignored.
*
* @since 1.2.0
*/
.macro @c128lib_CopyWithRelocationWithBank(source, sourceBank, destination, destinationBank) { CopyWithRelocationWithBank(source, sourceBank, destination, destinationBank) }

/**
* @brief This macro fills memory with a specified value.
*
Expand Down
76 changes: 71 additions & 5 deletions lib/mem.asm
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#importonce

#import "labels/lib/mmu.asm"
#import "labels/lib/zeropage.asm"

.filenamespace c128lib

Expand Down Expand Up @@ -80,7 +81,7 @@
*
* @remark Registers .A and .X will be modified.
* @remark Flags N and Z will be affected.
* @remark Zeropage location $fe will be used.
* @remark Zeropage location $FE will be used.
* @remark During copy, interrupts are disabled.
*
* @note Usage: CopyWithRelocation($C000, $C100) // Copies 256 bytes from memory location $C000 to $C100 with relocation
Expand All @@ -92,12 +93,11 @@
* @since 1.0.0
*/
.macro CopyWithRelocation(source, destination) {
.label TEMP = $fe
sei
ldx #>source // Preparing self mod code
stx !+ + 2
tsx // Save current stack pointer
stx TEMP
stx Zeropage.FE
ldx #>destination
stx Mmu.PAGE1_PAGE_POINTER

Expand All @@ -111,10 +111,76 @@
// .X contains 0 because bne didn't jump to the start of the loop
inx // Setting back stack page to 1
stx Mmu.PAGE1_PAGE_POINTER
ldx TEMP
ldx Zeropage.FE
txs // Setting stack pointer to previous value
cli
rts
}

/**
* @brief This macro copies a block of memory from one location to another using page relocation
* with bank selection.
*
* @details This macro also handles the page relocation of the memory block during the copy operation.
* It's slower than @sa CopyFast but it uses much less memory, especially for large memory blocks copy.
* This version allows to specify the bank for source and destination memory locations.
*
* @param[in] source The starting address of the memory block to be copied.
* @param[in] sourceBank Bank of the starting address.
* @param[in] destination The starting address of the location where the memory block will be copied to.
* @param[in] destinationBank Bank of the destination address.
*
* @remark Registers .A and .X will be modified.
* @remark Flags N and Z will be affected.
* @remark Zeropage location $FE will be used.
* @remark During copy, interrupts are disabled.
*
* @note The number of bytes that can be copied is always 256.
* @note Source and destination less significant byte should be $00. Any
* different value will be ignored.
* @note Use c128lib_CopyWithRelocationWithBank in mem-global.asm
*
* @since 1.2.0
*/
.macro CopyWithRelocationWithBank(source, sourceBank, destination, destinationBank) {
sei
ldx #>source // Preparing self mod code
stx !+ + 2
tsx // Save current stack pointer

lda Mmu.PAGE0_BLOCK_POINTER
pha
lda sourceBank
sta Mmu.PAGE0_BLOCK_POINTER

lda Mmu.PAGE1_BLOCK_POINTER
pha
lda destinationBank
sta Mmu.PAGE1_BLOCK_POINTER

stx Zeropage.FE
ldx #>destination
stx Mmu.PAGE1_PAGE_POINTER
ldx #0
txs
!: lda $FF00,x // Placeholder for self mod code
pha
dex
bne !-

// .X contains 0 because bne didn't jump to the start of the loop
inx // Setting back stack page to 1
stx Mmu.PAGE1_PAGE_POINTER
ldx Zeropage.FE
txs // Setting stack pointer to previous value

pla
sta Mmu.PAGE1_BLOCK_POINTER

pla
sta Mmu.PAGE0_BLOCK_POINTER

cli
}

/**
Expand Down

0 comments on commit 43242b9

Please sign in to comment.