diff --git a/build.gradle b/build.gradle index 0e07ec3..86c74d2 100644 --- a/build.gradle +++ b/build.gradle @@ -10,4 +10,6 @@ retroProject { dialect = "KickAssembler" dialectVersion = "5.25" libDirs = ["..", ".ra/deps/c128lib"] + + libFromGitHub "c128lib/chipset", "0.6.1" } diff --git a/sort-global.asm b/sort-global.asm new file mode 100644 index 0000000..11d1e10 --- /dev/null +++ b/sort-global.asm @@ -0,0 +1,5 @@ +#import "sort.asm" +#importonce +.filenamespace c128lib + +.macro @c128lib_BubbleSort(indirizzoArray, dimensioneArray, switchToFastModeWhileRunning) { BubbleSort(indirizzoArray, dimensioneArray, switchToFastModeWhileRunning) } diff --git a/sort.asm b/sort.asm new file mode 100644 index 0000000..7383e0f --- /dev/null +++ b/sort.asm @@ -0,0 +1,72 @@ +/* + * c128lib (c) 2023 + * https://github.com/c128lib/framework + */ + +#import "chipset/lib/vic2.asm" + +#importonce +.filenamespace c128lib + +.namespace Sort { +} + +/* + Sort an array with bubble sort algorithm. + Fast mode can be switched on/off while algorithm + is running. Fast mode can also be enabled before + macro is called. + + Params: + arrayAddress - memory address of array + arraySize - array size + switchToFastModeWhileRunning - if true, fast mode + will be enabled at start and disabled at end. +*/ +.macro BubbleSort(arrayAddress, arraySize, switchToFastModeWhileRunning) { + .if (switchToFastModeWhileRunning == true) { + lda #1 + sta c128lib.Vic2.CLKRATE + } + +start: + ldx #arraySize + +sort_loop: + ldy #0 + +inner_loop: + lda arrayAddress, y + cmp arrayAddress + 1, y + bcc NoSwap + pha + lda arrayAddress + 1, y + sta arrayAddress, y + pla + sta arrayAddress + 1, y + + NoSwap: + iny + cpy #arraySize - 1 + bne inner_loop + dex + bne sort_loop + + .if (switchToFastModeWhileRunning == true) { + dec c128lib.Vic2.CLKRATE + } +} +.assert "BubbleSort($beef, 3, false)", { BubbleSort($beef, 3, false) }, +{ + ldx #3; ldy #0; lda $beef,y; cmp $bef0, y; bcc *+13; pha; + lda $bef0, y; sta $beef, y; pla; sta $bef0, y; iny + cpy #2; bne *-22; dex; bne *-27 +} +.assert "BubbleSort($beef, 3, true)", { BubbleSort($beef, 3, true) }, +{ + lda #1; sta $d030 + ldx #3; ldy #0; lda $beef,y; cmp $bef0, y; bcc *+13; pha; + lda $bef0, y; sta $beef, y; pla; sta $bef0, y; iny + cpy #2; bne *-22; dex; bne *-27; + dec $d030 +}