-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbubble.asm
67 lines (51 loc) · 1.51 KB
/
bubble.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
BasicUpstart2(Entry)
#import "./lib/c64.asm"
.label ElementCount = $00fd
.label Swap = $00fe
ElementArray:
.fill 128, random() * 128
__ElementArray:
Entry:
lda #__ElementArray - ElementArray - 1
sta ElementCount // Store len(ElementArray) - 1
lda #$01 // There's no work to be done if len(ElementArray) <= 1
cmp ElementCount
bpl Exit
// Setup zeropage (0x00fb, 0x00fc) to point to ElementArray
lda #<ElementArray // Low byte of address
sta $00fb
lda #>ElementArray // High byte of address
sta $00fc
!Outer:
lda #$00 // Reset swap flag
sta Swap
ldx ElementCount
ldy #$00
!Inner:
lda ($fb), y
iny
cmp ($fb), y // Compare ElementArray[y] to ElementArray[y + 1]
bmi !SkipSwap+ // Swap if ElementArray[y] > ElementArray[y + 1]
pha
lda ($fb), y // Load ElementArray[y + 1]
dey
sta ($fb), y // Set ElementArray[y] := ElementArray[y + 1]
iny
pla
sta ($fb), y // Set ElementArray[y + 1] := ElementArray[y]
WaitForVsync() // Let's waste some cycles (312 * 63?)
stx VIC.BORDER_COLOR
lda #$01
sta Swap // Set swap flag to true
!SkipSwap:
dex
bne !Inner-
lda #$00 // If no swap was performed we're done
cmp Swap
beq Exit
dec ElementCount
bne !Outer-
Exit:
lda #$0e // Reset background color
sta VIC.BORDER_COLOR
rts