Skip to content

Commit 4c15908

Browse files
committed
Fixing incorrect copy with relocation #7
1 parent fd05947 commit 4c15908

2 files changed

Lines changed: 23 additions & 16 deletions

File tree

lib/mem-global.asm

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,23 +59,24 @@
5959
* @brief This macro copies a block of memory from one location to another using page relocation.
6060
*
6161
* @details This macro also handles the page relocation of the memory block during the copy operation.
62-
* It's slower than @sa c128lib_CopyFast but it uses much less memory, especially for large memory blocks copy.
62+
* It's slower than @sa CopyFast but it uses much less memory, especially for large memory blocks copy.
6363
*
6464
* @param[in] source The starting address of the memory block to be copied.
6565
* @param[in] destination The starting address of the location where the memory block will be copied to.
66-
* @param[in] count The number of bytes to be copied.
6766
*
68-
* @remark Registers .A, .X, and .Y will be modified.
67+
* @remark Registers .A and .X will be modified.
6968
* @remark Flags N and Z will be affected.
7069
* @remark Zeropage location $fe will be used.
70+
* @remark During copy, interrupts are disabled.
7171
*
72-
* @note Usage: c128lib_CopyWithRelocation($C000, $C100, 256) // Copies 256 bytes from memory location $C000 to $C100 with relocation
73-
* @note Minimum length is set to 5 because it's not convenient to use this
74-
* macro for lower values.
72+
* @note Usage: c128lib_CopyWithRelocation($C000, $C100) // Copies 256 bytes from memory location $C000 to $C100 with relocation
73+
* @note The number of bytes that can be copied is always 256.
74+
* @note Source and destination less significant byte should be $00. Any
75+
* different value will be ignored.
7576
*
7677
* @since 1.0.0
7778
*/
78-
.macro @c128lib_CopyWithRelocation(source, destination, count) { CopyWithRelocation(source, destination, count) }
79+
.macro @c128lib_CopyWithRelocation(source, destination) { CopyWithRelocation(source, destination) }
7980

8081
/**
8182
* @brief This macro fills memory with a specified value.

lib/mem.asm

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,36 +77,42 @@
7777
*
7878
* @param[in] source The starting address of the memory block to be copied.
7979
* @param[in] destination The starting address of the location where the memory block will be copied to.
80-
* @param[in] count The number of bytes to be copied.
8180
*
82-
* @remark Registers .A, .X, and .Y will be modified.
81+
* @remark Registers .A and .X will be modified.
8382
* @remark Flags N and Z will be affected.
8483
* @remark Zeropage location $fe will be used.
84+
* @remark During copy, interrupts are disabled.
8585
*
86-
* @note Usage: CopyWithRelocation($C000, $C100, 256) // Copies 256 bytes from memory location $C000 to $C100 with relocation
86+
* @note Usage: CopyWithRelocation($C000, $C100) // Copies 256 bytes from memory location $C000 to $C100 with relocation
87+
* @note The number of bytes that can be copied is always 256.
88+
* @note Source and destination less significant byte should be $00. Any
89+
* different value will be ignored.
8790
* @note Use c128lib_CopyWithRelocation in mem-global.asm
8891
*
8992
* @since 1.0.0
9093
*/
91-
.macro CopyWithRelocation(source, destination, count) {
94+
.macro CopyWithRelocation(source, destination) {
9295
.label TEMP = $fe
9396
sei
97+
ldx #>source // Preparing self mod code
9498
stx !+ + 2
95-
tsx
99+
tsx // Save current stack pointer
96100
stx TEMP
97-
sty Mmu.PAGE1_PAGE_POINTER
101+
ldx #>destination
102+
stx Mmu.PAGE1_PAGE_POINTER
98103

99104
ldx #0
100105
txs
101-
!: lda Mmu.LOAD_CONFIGURATION,x
106+
!: lda $FF00,x // Placeholder for self mod code
102107
pha
103108
dex
104109
bne !-
105110

106-
ldx #1
111+
// .X contains 0 because bne didn't jump to the start of the loop
112+
inx // Setting back stack page to 1
107113
stx Mmu.PAGE1_PAGE_POINTER
108114
ldx TEMP
109-
txs
115+
txs // Setting stack pointer to previous value
110116
cli
111117
rts
112118
}

0 commit comments

Comments
 (0)