Skip to content

Commit 0451428

Browse files
authored
Merge pull request #11 from c128lib/issue8
Adding CopyWithRelocationWithBank
2 parents 4c15908 + 43242b9 commit 0451428

3 files changed

Lines changed: 98 additions & 6 deletions

File tree

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ retroProject {
1515

1616
viceExecutable = 'x128'
1717

18-
libFromGitHub "c128lib/labels", "0.2.0"
18+
libFromGitHub "c128lib/labels", "0.3.0"
1919
libFromGitHub "c128lib/128spec", "0.7.2"
2020
}

lib/mem-global.asm

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,32 @@
7878
*/
7979
.macro @c128lib_CopyWithRelocation(source, destination) { CopyWithRelocation(source, destination) }
8080

81+
/**
82+
* @brief This macro copies a block of memory from one location to another using page relocation
83+
* with bank selection.
84+
*
85+
* @details This macro also handles the page relocation of the memory block during the copy operation.
86+
* It's slower than @sa CopyFast but it uses much less memory, especially for large memory blocks copy.
87+
* This version allows to specify the bank for source and destination memory locations.
88+
*
89+
* @param[in] source The starting address of the memory block to be copied.
90+
* @param[in] sourceBank Bank of the starting address.
91+
* @param[in] destination The starting address of the location where the memory block will be copied to.
92+
* @param[in] destinationBank Bank of the destination address.
93+
*
94+
* @remark Registers .A and .X will be modified.
95+
* @remark Flags N and Z will be affected.
96+
* @remark Zeropage location $FE will be used.
97+
* @remark During copy, interrupts are disabled.
98+
*
99+
* @note The number of bytes that can be copied is always 256.
100+
* @note Source and destination less significant byte should be $00. Any
101+
* different value will be ignored.
102+
*
103+
* @since 1.2.0
104+
*/
105+
.macro @c128lib_CopyWithRelocationWithBank(source, sourceBank, destination, destinationBank) { CopyWithRelocationWithBank(source, sourceBank, destination, destinationBank) }
106+
81107
/**
82108
* @brief This macro fills memory with a specified value.
83109
*

lib/mem.asm

Lines changed: 71 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#importonce
3333

3434
#import "labels/lib/mmu.asm"
35+
#import "labels/lib/zeropage.asm"
3536

3637
.filenamespace c128lib
3738

@@ -80,7 +81,7 @@
8081
*
8182
* @remark Registers .A and .X will be modified.
8283
* @remark Flags N and Z will be affected.
83-
* @remark Zeropage location $fe will be used.
84+
* @remark Zeropage location $FE will be used.
8485
* @remark During copy, interrupts are disabled.
8586
*
8687
* @note Usage: CopyWithRelocation($C000, $C100) // Copies 256 bytes from memory location $C000 to $C100 with relocation
@@ -92,12 +93,11 @@
9293
* @since 1.0.0
9394
*/
9495
.macro CopyWithRelocation(source, destination) {
95-
.label TEMP = $fe
9696
sei
9797
ldx #>source // Preparing self mod code
9898
stx !+ + 2
9999
tsx // Save current stack pointer
100-
stx TEMP
100+
stx Zeropage.FE
101101
ldx #>destination
102102
stx Mmu.PAGE1_PAGE_POINTER
103103

@@ -111,10 +111,76 @@
111111
// .X contains 0 because bne didn't jump to the start of the loop
112112
inx // Setting back stack page to 1
113113
stx Mmu.PAGE1_PAGE_POINTER
114-
ldx TEMP
114+
ldx Zeropage.FE
115115
txs // Setting stack pointer to previous value
116116
cli
117-
rts
117+
}
118+
119+
/**
120+
* @brief This macro copies a block of memory from one location to another using page relocation
121+
* with bank selection.
122+
*
123+
* @details This macro also handles the page relocation of the memory block during the copy operation.
124+
* It's slower than @sa CopyFast but it uses much less memory, especially for large memory blocks copy.
125+
* This version allows to specify the bank for source and destination memory locations.
126+
*
127+
* @param[in] source The starting address of the memory block to be copied.
128+
* @param[in] sourceBank Bank of the starting address.
129+
* @param[in] destination The starting address of the location where the memory block will be copied to.
130+
* @param[in] destinationBank Bank of the destination address.
131+
*
132+
* @remark Registers .A and .X will be modified.
133+
* @remark Flags N and Z will be affected.
134+
* @remark Zeropage location $FE will be used.
135+
* @remark During copy, interrupts are disabled.
136+
*
137+
* @note The number of bytes that can be copied is always 256.
138+
* @note Source and destination less significant byte should be $00. Any
139+
* different value will be ignored.
140+
* @note Use c128lib_CopyWithRelocationWithBank in mem-global.asm
141+
*
142+
* @since 1.2.0
143+
*/
144+
.macro CopyWithRelocationWithBank(source, sourceBank, destination, destinationBank) {
145+
sei
146+
ldx #>source // Preparing self mod code
147+
stx !+ + 2
148+
tsx // Save current stack pointer
149+
150+
lda Mmu.PAGE0_BLOCK_POINTER
151+
pha
152+
lda sourceBank
153+
sta Mmu.PAGE0_BLOCK_POINTER
154+
155+
lda Mmu.PAGE1_BLOCK_POINTER
156+
pha
157+
lda destinationBank
158+
sta Mmu.PAGE1_BLOCK_POINTER
159+
160+
stx Zeropage.FE
161+
ldx #>destination
162+
stx Mmu.PAGE1_PAGE_POINTER
163+
164+
ldx #0
165+
txs
166+
!: lda $FF00,x // Placeholder for self mod code
167+
pha
168+
dex
169+
bne !-
170+
171+
// .X contains 0 because bne didn't jump to the start of the loop
172+
inx // Setting back stack page to 1
173+
stx Mmu.PAGE1_PAGE_POINTER
174+
ldx Zeropage.FE
175+
txs // Setting stack pointer to previous value
176+
177+
pla
178+
sta Mmu.PAGE1_BLOCK_POINTER
179+
180+
pla
181+
sta Mmu.PAGE0_BLOCK_POINTER
182+
183+
cli
118184
}
119185

120186
/**

0 commit comments

Comments
 (0)