2020 VCF 6502 Programming
2020 VCF 6502 Programming
Programming
for the Apple II
Stephen A. Edwards
6502
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
VSS RDY 1 IRQ NMI SYNC VCC A0 A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 A11
(OUT)
ADDRESS BUS
X INDEX REGISTER
Y INDEX REGISTER
The 6502 Programmer’s Model
7 0
A ACCUMULATOR
X INDEX REGISTER
Y INDEX REGISTER
15
PCH PCL PROGRAM COUNTER
The 6502 Programmer’s Model
7 0
A ACCUMULATOR
X INDEX REGISTER
Y INDEX REGISTER
15
PCH PCL PROGRAM COUNTER
1 S STACK POINTER
The 6502 Programmer’s Model
7 0
A ACCUMULATOR
X INDEX REGISTER
Y INDEX REGISTER
15
PCH PCL PROGRAM COUNTER
1 S STACK POINTER
N V B D I Z C STATUS REGISTER
Negative Carry
Overflow Zero
Interrupt Disable
Break Decimal Mode
Source: Visual6502.org
Source: Visual6502.org
Source: Visual6502.org
6502 Instruction Encoding
flag v 1 0 0 0 0 Branches
0 op 0 op 0 0 0 Stack instructions
6502 Instruction Encoding
opcode mode 0 1 “Group one” add, compare; most addressing modes
flag v 1 0 0 0 0 Branches
0 op 0 op 0 0 0 Stack instructions
LDA #$42
A9 42
A
Group One Instructions opcode mode 0 1
A9 42 LDA #$42 ;Load Accumulator Immediate
LDA #$42
A9 42
A
Group One Instructions opcode mode 0 1
A9 42 LDA #$42 ;Load Accumulator Immediate
85 42 STA $42 ;Store Accumulator Zero Page
STA $42
42 42
Zero Page
Memory
$42
Group One Instructions opcode mode 0 1
A9 42 LDA #$42 ;Load Accumulator Immediate
85 42 STA $42 ;Store Accumulator Zero Page
STA $42
42 42
Zero Page
Memory
A $42
Group One Instructions opcode mode 0 1
A9 42 LDA #$42 ;Load Accumulator Immediate
85 42 STA $42 ;Store Accumulator Zero Page
75 42 ADC $42,X ;Add with Carry Zero Page Indexed by X
ADC $42,X
$42,X 42
Zero Page
Memory
A +
C
Group One Instructions opcode mode 0 1
A9 42 LDA #$42 ;Load Accumulator Immediate
85 42 STA $42 ;Store Accumulator Zero Page
75 42 ADC $42,X ;Add with Carry Zero Page Indexed by X
ADC $42,X
$42,X 42 + X
Zero Page
Memory
A + $42 + X
C
Group One Instructions opcode mode 0 1
A9 42 LDA #$42 ;Load Accumulator Immediate
85 42 STA $42 ;Store Accumulator Zero Page
75 42 ADC $42,X ;Add with Carry Zero Page Indexed by X
ED 01 42 SBC $4201 ;Subtract w/Carry Absolute
SBC $4201
ED 01 42
Memory
A −
C
Group One Instructions opcode mode 0 1
A9 42 LDA #$42 ;Load Accumulator Immediate
85 42 STA $42 ;Store Accumulator Zero Page
75 42 ADC $42,X ;Add with Carry Zero Page Indexed by X
ED 01 42 SBC $4201 ;Subtract w/Carry Absolute
SBC $4201
ED 01 42
Memory
A − $4201
C
Group One Instructions opcode mode 0 1
A9 42 LDA #$42 ;Load Accumulator Immediate
85 42 STA $42 ;Store Accumulator Zero Page
75 42 ADC $42,X ;Add with Carry Zero Page Indexed by X
ED 01 42 SBC $4201 ;Subtract w/Carry Absolute
DD 05 42 CMP $4205,X ;Compare Absolute Indexed by X
CMP $4205
DD 05 42
Memory
A −
NZC
Group One Instructions opcode mode 0 1
A9 42 LDA #$42 ;Load Accumulator Immediate
85 42 STA $42 ;Store Accumulator Zero Page
75 42 ADC $42,X ;Add with Carry Zero Page Indexed by X
ED 01 42 SBC $4201 ;Subtract w/Carry Absolute
DD 05 42 CMP $4205,X ;Compare Absolute Indexed by X
CMP $4205
DD 05 42 + X
Memory
A − $4205 + X
NZC
Group One Instructions opcode mode 0 1
A9 42 LDA #$42 ;Load Accumulator Immediate
85 42 STA $42 ;Store Accumulator Zero Page
75 42 ADC $42,X ;Add with Carry Zero Page Indexed by X
ED 01 42 SBC $4201 ;Subtract w/Carry Absolute
DD 05 42 CMP $4205,X ;Compare Absolute Indexed by X
39 06 42 AND $4206,Y ;Logical AND Absolute Indexed by Y
AND $4206,Y
39 06 42
Memory
A AND
Group One Instructions opcode mode 0 1
A9 42 LDA #$42 ;Load Accumulator Immediate
85 42 STA $42 ;Store Accumulator Zero Page
75 42 ADC $42,X ;Add with Carry Zero Page Indexed by X
ED 01 42 SBC $4201 ;Subtract w/Carry Absolute
DD 05 42 CMP $4205,X ;Compare Absolute Indexed by X
39 06 42 AND $4206,Y ;Logical AND Absolute Indexed by Y
AND $4206,Y
39 06 42 + Y
Memory
A AND $4206 + Y
Group One Instructions opcode mode 0 1
A9 42 LDA #$42 ;Load Accumulator Immediate
85 42 STA $42 ;Store Accumulator Zero Page
75 42 ADC $42,X ;Add with Carry Zero Page Indexed by X
ED 01 42 SBC $4201 ;Subtract w/Carry Absolute
DD 05 42 CMP $4205,X ;Compare Absolute Indexed by X
39 06 42 AND $4206,Y ;Logical AND Absolute Indexed by Y
11 42 ORA ($42),Y ;Logical OR Indirect Indexed
ORA ($42),Y
11 42
Memory
A OR
Group One Instructions opcode mode 0 1
A9 42 LDA #$42 ;Load Accumulator Immediate
85 42 STA $42 ;Store Accumulator Zero Page
75 42 ADC $42,X ;Add with Carry Zero Page Indexed by X
ED 01 42 SBC $4201 ;Subtract w/Carry Absolute
DD 05 42 CMP $4205,X ;Compare Absolute Indexed by X
39 06 42 AND $4206,Y ;Logical AND Absolute Indexed by Y
11 42 ORA ($42),Y ;Logical OR Indirect Indexed
ORA ($42),Y
11 42
ORA ($42),Y
11 42
Y
Memory Zero Page
Memory
A OR + $42
$43
Group One Instructions opcode mode 0 1
A9 42 LDA #$42 ;Load Accumulator Immediate
85 42 STA $42 ;Store Accumulator Zero Page
75 42 ADC $42,X ;Add with Carry Zero Page Indexed by X
ED 01 42 SBC $4201 ;Subtract w/Carry Absolute
DD 05 42 CMP $4205,X ;Compare Absolute Indexed by X
39 06 42 AND $4206,Y ;Logical AND Absolute Indexed by Y
11 42 ORA ($42),Y ;Logical OR Indirect Indexed
41 42 EOR ($42,X) ;Exclusive OR Indexed Indirect
EOR ($42,X)
41 42
Memory
A XOR
Group One Instructions opcode mode 0 1
A9 42 LDA #$42 ;Load Accumulator Immediate
85 42 STA $42 ;Store Accumulator Zero Page
75 42 ADC $42,X ;Add with Carry Zero Page Indexed by X
ED 01 42 SBC $4201 ;Subtract w/Carry Absolute
DD 05 42 CMP $4205,X ;Compare Absolute Indexed by X
39 06 42 AND $4206,Y ;Logical AND Absolute Indexed by Y
11 42 ORA ($42),Y ;Logical OR Indirect Indexed
41 42 EOR ($42,X) ;Exclusive OR Indexed Indirect
EOR ($42,X)
41 42 + X
EOR ($42,X)
41 42 + X
0
Apple II Zero 1
Page Usage 2
7
Monitor
8
D
Source: comp.sys.apple2 FAQ, Wagner,
Wirth and Lechner E
F
0 1 2 3 4 5 6 7 8 9 A B C D E F
0
Apple II Zero 1
Page Usage 2
6
DOS 3.3 ProDOS
7
Monitor
8
D
Source: comp.sys.apple2 FAQ, Wagner,
Wirth and Lechner E
F
0 1 2 3 4 5 6 7 8 9 A B C D E F
0
Apple II Zero 1
Page Usage 2
6
DOS 3.3 ProDOS
7
Monitor Applesoft
8
D
Source: comp.sys.apple2 FAQ, Wagner,
Wirth and Lechner E
F
Group Two Instructions opcode mode 1 0
A2 42 LDX #$42 ;Load X Immediate
LDX #$42
A2 42
X
Group Two Instructions opcode mode 1 0
A2 42 LDX #$42 ;Load X Immediate
86 42 STX $42 ;Store X Zero Page
STX $42
86 42
Zero Page
Memory
X $42
Group Two Instructions opcode mode 1 0
A2 42 LDX #$42 ;Load X Immediate
86 42 STX $42 ;Store X Zero Page
F6 42 INC $42,X ;Increment Zero Page Indexed by X
INC $42,X
F6 42 + X
Zero Page
Memory
1 + $42 + X
Group Two Instructions opcode mode 1 0
A2 42 LDX #$42 ;Load X Immediate
86 42 STX $42 ;Store X Zero Page
F6 42 INC $42,X ;Increment Zero Page Indexed by X
CE 77 42 DEC $4277 ;Decrement Absolute
DEC $4277
CE 77 42
Memory
−1 + $4277
Group Two Instructions opcode mode 1 0
A2 42 LDX #$42 ;Load X Immediate
86 42 STX $42 ;Store X Zero Page
F6 42 INC $42,X ;Increment Zero Page Indexed by X
CE 77 42 DEC $4277 ;Decrement Absolute
0A ASL ;Arithmetic Shift Left Accumulator
ASL
0A
C 0
Group Two Instructions opcode mode 1 0
A2 42 LDX #$42 ;Load X Immediate
86 42 STX $42 ;Store X Zero Page
F6 42 INC $42,X ;Increment Zero Page Indexed by X
CE 77 42 DEC $4277 ;Decrement Absolute
0A ASL ;Arithmetic Shift Left Accumulator
5E 80 42 LSR $4280,X ;Logical Shift Right Absolute Indexed by X
LSR $4280,X
5E 80 42
0 C
Group Two Instructions opcode mode 1 0
A2 42 LDX #$42 ;Load X Immediate
86 42 STX $42 ;Store X Zero Page
F6 42 INC $42,X ;Increment Zero Page Indexed by X
CE 77 42 DEC $4277 ;Decrement Absolute
0A ASL ;Arithmetic Shift Left Accumulator
5E 80 42 LSR $4280,X ;Logical Shift Right Absolute Indexed by X
2A ROL ;Rotate Left Accumulator
ROL
2A
C
Group Two Instructions opcode mode 1 0
A2 42 LDX #$42 ;Load X Immediate
86 42 STX $42 ;Store X Zero Page
F6 42 INC $42,X ;Increment Zero Page Indexed by X
CE 77 42 DEC $4277 ;Decrement Absolute
0A ASL ;Arithmetic Shift Left Accumulator
5E 80 42 LSR $4280,X ;Logical Shift Right Absolute Indexed by X
2A ROL ;Rotate Left Accumulator
6A ROR ;Rotate Right Accumulator
ROR
6A
C
Other Instructions opcode mode 1 0 0
A0 42 LDY #$42 ;Load Y Immediate
94 42 STY $42,X ;Store Y Zero Page Indexed by X
C4 42 CPY $42 ;Compare Y Zero Page
EC 50 42 CPX $4250 ;Compare X Absolute
Other Instructions opcode mode 1 0 0
A0 42 LDY #$42 ;Load Y Immediate
94 42 STY $42,X ;Store Y Zero Page Indexed by X
C4 42 CPY $42 ;Compare Y Zero Page
EC 50 42 CPX $4250 ;Compare X Absolute
4C 40 42 JMP $4240 ;Jump Absolute
JMP $4240
4C 40 42
PCH PCL
Other Instructions opcode mode 1 0 0
A0 42 LDY #$42 ;Load Y Immediate
94 42 STY $42,X ;Store Y Zero Page Indexed by X
C4 42 CPY $42 ;Compare Y Zero Page
EC 50 42 CPX $4250 ;Compare X Absolute
4C 40 42 JMP $4240 ;Jump Absolute
6C 41 42 JMP ($4241) ;Jump Indirect
JMP $4241
6C 41 42
PCH PCL
Memory
$4241
$4242
Other Instructions opcode mode 1 0 0
A0 42 LDY #$42 ;Load Y Immediate
94 42 STY $42,X ;Store Y Zero Page Indexed by X
C4 42 CPY $42 ;Compare Y Zero Page
EC 50 42 CPX $4250 ;Compare X Absolute
4C 40 42 JMP $4240 ;Jump Absolute
6C 41 42 JMP ($4241) ;Jump Indirect
JMP $4241
6C 41 42
PCH PCL
Memory
$4241
$4242
Other Instructions opcode mode 1 0 0
A0 42 LDY #$42 ;Load Y Immediate
94 42 STY $42,X ;Store Y Zero Page Indexed by X
C4 42 CPY $42 ;Compare Y Zero Page
EC 50 42 CPX $4250 ;Compare X Absolute
4C 40 42 JMP $4240 ;Jump Absolute
6C 41 42 JMP ($4241) ;Jump Indirect
2C 57 42 BIT $4257 ;Test Bits w/ Accum. Absolute
BIT $4257
2C 57 42
Memory
$4257
Other Instructions opcode mode 1 0 0
A0 42 LDY #$42 ;Load Y Immediate
94 42 STY $42,X ;Store Y Zero Page Indexed by X
C4 42 CPY $42 ;Compare Y Zero Page
EC 50 42 CPX $4250 ;Compare X Absolute
4C 40 42 JMP $4240 ;Jump Absolute
6C 41 42 JMP ($4241) ;Jump Indirect
2C 57 42 BIT $4257 ;Test Bits w/ Accum. Absolute
BIT $4257
2C 57 42
Memory
$4257
ZNV
Other Instructions opcode mode 1 0 0
A0 42 LDY #$42 ;Load Y Immediate
94 42 STY $42,X ;Store Y Zero Page Indexed by X
C4 42 CPY $42 ;Compare Y Zero Page
EC 50 42 CPX $4250 ;Compare X Absolute
4C 40 42 JMP $4240 ;Jump Absolute
6C 41 42 JMP ($4241) ;Jump Indirect
2C 57 42 BIT $4257 ;Test Bits w/ Accum. Absolute
BIT $4257
2C 57 42
Memory
A AND $4257
ZNV
Single-Byte Data Instructions 1 op 1 0 xy 0
E8 INX ;Increment X
CA DEX ;Decrement X
C8 INY ;Increment Y
88 DEY ;Decrement Y
AA TAX ;Transfer Accumulator to X
A8 TAY ;Transfer Accumulator to Y
8A TXA ;Transfer X to Accumulator
98 TYA ;Transfer Y to Accumulator
BA TSX ;Transfer Stack Pointer to X
9A TXS ;Transfer X to Stack Pointer
Single-Byte Flag Instructions flag 1 1 0 0 0
18 CLC ;Clear Carry Flag
38 SEC ;Set Carry Flag
58 CLI ;Clear Interrupt Disable
78 SEI ;Set Interrupt Disable
B8 CLV ;Clear Overflow Flag
Set and clear the carry flag; useful with ADC and SBC
Enable and disable interrupts. Useful for critical regions
Clear the overflow flag
Single-Byte Flag Instructions flag 1 1 0 0 0
18 CLC ;Clear Carry Flag
38 SEC ;Set Carry Flag
58 CLI ;Clear Interrupt Disable
78 SEI ;Set Interrupt Disable
B8 CLV ;Clear Overflow Flag
D8 CLD ;Clear Decimal Mode (ADC, SBC perform binary arithmetic)
F8 SED ;Set Decimal Mode (ADC, SBC perform BCD arithemtic)
RTS pops the program counter value off the stack, returning to just after the
JSR that sent it there
Stack Instructions 0 op 0 op 0 0 0
20 ED FD JSR $FDED ;Jump to Subroutine
60 RTS ;Return from Subroutine
40 RTI ;Return from Interrupt
Return from an interrupt by popping the status register and the program
counter from the stack. (Like RTS, but also restores the status register)
Useful in interrupt routines and returning from BRK
Stack Instructions 0 op 0 op 0 0 0
20 ED FD JSR $FDED ;Jump to Subroutine
60 RTS ;Return from Subroutine
40 RTI ;Return from Interrupt
08 PHP ;Push Processor Status
28 PLP ;Pull Processor Status
48 PHA ;Push Accumulator
68 PLA ;Pull Accumulator
24
Apple II Text Mode: 40 × 24, 7 × 8 characters, 1 byte/char
40
However,
24 8 × 3 = 24
128 = 40 × 3 + 8
8 × 128 = 1024
Apple II Text Mode: 40 × 24, 7 × 8 characters, 1 byte/char
128
40 40 40 8
8
Apple II Text Mode: 40 × 24, 7 × 8 characters, 1 byte/char
128
40 40 40 8
8 128 × 4 = 1024 = 1K
Apple II Text Mode: 40 × 24, 7 × 8 characters, 1 byte/char
128
40 40 40 8
8 128 × 4 = 1024 = 1K
48
Apple II Hires: 140 × 192, 6 colors, 8K/screen
140
192
Apple II Hires Addresses: Even more bizarre
Y Hex Binary
0 $2000 1 000 000 0000000
1 $2400 1 001 000 0000000
2 $2800 1 010 000 0000000
3 $2C00 1 011 000 0000000
4 $3000 1 100 000 0000000
5 $3400 1 101 000 0000000
6 $3800 1 110 000 0000000
7 $3C00 1 111 000 0000000
Apple II Hires Addresses: Even more bizarre
Y Hex Binary
0 $2000 1 000 000 0000000
1 $2400 1 001 000 0000000
2 $2800 1 010 000 0000000
3 $2C00 1 011 000 0000000
4 $3000 1 100 000 0000000
5 $3400 1 101 000 0000000
6 $3800 1 110 000 0000000
7 $3C00 1 111 000 0000000
8 $2080 1 000 001 0000000
9 $2480 1 001 001 0000000
10 $2880 1 010 001 0000000
11 $2C80 1 011 001 0000000
12 $3080 1 100 001 0000000
13 $3480 1 101 001 0000000
14 $3880 1 110 001 0000000
15 $3C80 1 111 001 0000000
Apple II Hires Addresses: Even more bizarre
Y Hex Binary
0 $2000 1 000 000 0000000
1 $2400 1 001 000 0000000
2 $2800 1 010 000 0000000
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
3 $2C00 1 011 000 0000000
4 $3000 1 100 000 0000000 0 0 1 y2 y1 y0 y5 y4 y3 0 0 x4 x3 x2 x1 x0
5 $3400 1 101 000 0000000 Rows
6 $3800 1 110 000 0000000
7 $3C00 1 111 000 0000000 0–63 0 0 0 0
8 $2080 1 000 001 0000000
9 $2480 1 001 001 0000000
64–127 0 1 0 1
10 $2880 1 010 001 0000000
11 $2C80 1 011 001 0000000
12 $3080 1 100 001 0000000 128–191 1 0 1 0
13 $3480 1 101 001 0000000
14 $3880 1 110 001 0000000 Woz used just a 4-bit adder
15 $3C80 1 111 001 0000000
16 $2100 1 000 010 0000000
17 $2500 1 001 010 0000000
18 $2900 1 010 010 0000000
19 $2D00 1 011 010 0000000
Apple II Hires Addresses: Even more bizarre
Y Hex Binary
HPOSN STA HGRY
0 $2000 1 000 000 0000000 STX HGRX
STY HGRX+1
1 $2400 1 001 000 0000000 PHA
AND #%11000000
2 $2800 1 010 000 0000000 STA
LSR
GBASL
A
3 $2C00 1 011 000 0000000 LSR
ORA
A
GBASL
STA GBASL
4 $3000 1 100 000 0000000 PLA
STA GBASH
5 $3400 1 101 000 0000000 ASL A
ASL A
6 $3800 1 110 000 0000000 ASL A
ROL GBASH
7 $3C00 1 111 000 0000000 ASL
ROL
A
GBASH
8 $2080 1 000 001 0000000 ASL
ROR
A
GBASL
LDA GBASH
9 $2480 1 001 001 0000000 AND #%00011111
ORA HGRPAGE
10 $2880 1 010 001 0000000 STA GBASH
TXA
11 $2C80 1 011 001 0000000 CPY #0
BEQ HPOSN2
12 $3080 1 100 001 0000000 LDY
ADC
#35
#4
13 $3480 1 101 001 0000000 HPOSN1 INY
HPOSN2 SBC #7
BCS HPOSN1
14 $3880 1 110 001 0000000 STY HGRHORIZ
TAX
15 $3C80 1 111 001 0000000 LDA MSKTBL-249,X
STA HMASK
16 $2100 1 000 010 0000000 TYA
LSR A
17 $2500 1 001 010 0000000 LDA
STA
HGRCOLOR
HCOLOR1
18 $2900 1 010 010 0000000 BCS
RTS
COLORSHIFT
palette pixels
Apple II Hires Colors: 7 pixels/2 bytes
0000000000000000
0000001100000000
palette pixels
Apple II Hires Colors: 7 pixels/2 bytes
0000000000000000
0000001100000000
0000111100000000
palette pixels
Apple II Hires Colors: 7 pixels/2 bytes
0000000000000000
0000001100000000
0000111100000000
0111111100000001
palette pixels
Apple II Hires Colors: 7 pixels/2 bytes
0000000000000000
0000001100000000
0000111100000000
0111111100000001
palette pixels
0111111101111111
Apple II Hires Colors: 7 pixels/2 bytes
0000000000000000 0101010100101010
0000001100000000
0000111100000000
0111111100000001
palette pixels
0111111101111111
Apple II Hires Colors: 7 pixels/2 bytes
0000000000000000 0101010100101010
0000001100000000 0010101001010101
0000111100000000
0111111100000001
palette pixels
0111111101111111
Apple II Hires Colors: 7 pixels/2 bytes
0000000000000000 0101010100101010
0000001100000000 0010101001010101
0000111100000000 0110101000101011
0111111100000001
palette pixels
0111111101111111
Apple II Hires Colors: 7 pixels/2 bytes
0000000000000000 0101010100101010 1101010110101010
0111111100000001
palette pixels
0111111101111111
Preshifted Shapes: 7 versions
BALL0 db %00111100 BALL1 db %00000000
db %01111111 db %00000001
db %01111111 db %00000001
db %01111111 db %00000001
db %01111111 db %00000001
db %01111111 db %00000001
db %01111111 db %00000001
db %00111100 db %00000000
db %01111000 db %00000000
db %01111110 db %00000011
db %01111110 db %00000011
db %01111110 db %00000011
db %01111110 db %00000011
db %01111110 db %00000011
db %01111110 db %00000011
db %01111000 db %00000000
db %01110000 db %00000001
db %01111100 db %00000111
db %01111100 db %00000111
db %01111100 db %00000111
db %01111100 db %00000111
db %01111100 db %00000111
... ...
Bouncing Balls: Clear the screen
A=1 1 0
The Exclusive-OR Trick
A XOR B
The Exclusive-OR Trick
A XOR B
The Exclusive-OR Trick
A XOR B
The Exclusive-OR Trick
A XOR B
The Exclusive-OR Trick
A XOR B
The Exclusive-OR Trick
A XOR B
The Exclusive-OR Trick
A XOR B
The Exclusive-OR Trick
A XOR B
Draw the Ball: XOR Two Bytes
lda (GBASL),y
eor BALL0,x
sta (GBASL),y
iny
lda (GBASL),y
eor BALL1,x
sta (GBASL),y
Draw the Ball: XOR Two Bytes
lda (GBASL),y
eor BALL0,x
sta (GBASL),y
iny
lda (GBASL),y
eor BALL1,x
sta (GBASL),y
Draw the Ball: XOR Two Bytes
inx
txa
and #7
bne xsplot ; Stop at a multiple of 8 bytes
rts
Bouncing Balls: Horizontal Position
BALLXH BALLXL
0 0 c5 c4 c3 c2 c1 c0 0 0 s2 s1 s0 s−1 s−2 s−3
lda BALLXL,x
clc
adc BALLDX,x
bpl nounder
sta BALLXL,x
inc BALLXH,x
ldy BALLXH,x
cpy #COLUMNS-2 ; Hit the right wall?
bne xdone
bouncex sec
lda #0
sbc BALLDX,x
sta BALLDX,x
xdone
Bouncing Balls: Vertical Fall
clc ; Update Y
lda BALLYL,x
adc BALLDYL,x
sta BALLYL,x
lda BALLYH,x
adc BALLDYH,x
sta BALLYH,x