Lab - 2: Optional Challenges

 

Exploring 6502 Assembly: Mob Programming & Bouncing Ball Experiment

Introduction

I used 6502 Assembly Language to create a bouncing ball simulation for Lab 2 in SPO600. Mob programming, a cooperative method where several people work together on a single coding task, was introduced in this lab. Understanding low-level programming, working with registers and memory, and adding additional features to an already-existing bouncing ball simulation were the objectives.

I'll describe the following in this blog:
✅ The bouncing ball's basic implementation;
✅ The code's operation and output.
✅ Difficulties and Improvements (e.g., randomized bounces, fractional movements).
✅ My thoughts and experiences with Assembly programming.

Base Implementation: The 6502 Assembly's Bouncing Ball
The objective was to construct a simulation of a bouncing ball that moves predictably and reverses course when it strikes the corners of the screen. The code that carries out this behaviour is shown below:

; 6502 Assembly - Basic Bouncing Ball
LDA #$01       ; Load initial X direction (+1)
STA X_Delta    ; Store in X_Delta variable
LDA #$01       ; Load initial Y direction (+1)
STA Y_Delta    ; Store in Y_Delta variable

MainLoop:
    ; Update X position
    LDA Ball_X
    CLC
    ADC X_Delta
    STA Ball_X
    
    ; Check for X boundary
    CMP #MAX_X
    BCC CheckY
    LDA X_Delta
    EOR #$FF
    ADC #$01
    STA X_Delta

CheckY:
    ; Update Y position
    LDA Ball_Y
    CLC
    ADC Y_Delta
    STA Ball_Y
    
    ; Check for Y boundary
    CMP #MAX_Y
    BCC Render
    LDA Y_Delta
    EOR #$FF
    ADC #$01
    STA Y_Delta

Render:
    ; Code to draw the ball at (Ball_X, Ball_Y)
    JSR DrawBall
    JMP MainLoop 
๐Ÿ”นThe ball's position is stored by Ball_X and Ball_Y.
๐Ÿ”น X_Delta and Y_Delta maintain the direction of movement (+1 or -1).
๐Ÿ”นIn every loop iteration, the program updates the position.
๐Ÿ”น Bitwise operations are used to reverse the movement direction when it hits a boundary.

Results: 
The ball bounces off the corners of the screen as it goes diagonally. It adheres to a set and dependable pattern.

Challenges & Enhancements

1. Supporting Arbitrary Integer Deltas

To allow different step sizes (e.g., +2, -2), I modified the delta values:

LDA #$02    ; Set X delta to 2
STA X_Delta
LDA #$02    ; Set Y delta to 2
STA Y_Delta

Effect: The ball moves faster, skipping pixels instead of moving one at a time.


2. Implementing Fractional Movements

To allow movements like 1.5, I introduced fixed-point arithmetic with a 16-bit format:

  • High byte = Integer part
  • Low byte = Fractional part
LDA Ball_X
CLC
ADC X_Delta_High
STA Ball_X
LDA Ball_X_Frac
CLC
ADC X_Delta_Low
STA Ball_X_Frac
BCS CarryAdjust

Effect: The ball moves smoothly instead of being limited to whole-pixel steps.


3. Adding Random Bounce Perturbation

I used the pseudo-random number generator at $00FE to slightly alter the ball’s direction:

LDA $00FE    ; Get random value
AND #$03     ; Mask to small variation
ADC X_Delta  ; Adjust X delta
STA X_Delta

Effect: The bounce direction is slightly randomized, making movement less predictable.


4. Changing Ball Appearance on Bounce

Each time the ball hits a boundary, it switches to a new sprite or color:

LDA Ball_Color
CLC
ADC #$01
STA Ball_Color

Effect: The ball changes color each time it bounces, adding a visual effect.


Reflections on 6502 Assembly

This lab deepened my appreciation for low-level programming. Key takeaways:

Precision Control: Direct memory manipulation is both powerful and tricky.
Efficient Execution: Optimized bitwise operations make programs compact and fast.
Challenging Yet Rewarding: Debugging was difficult, but getting smooth movement felt great!

Overall, this was a fantastic learning experience. I’m excited to explore more Assembly programming and see how it compares to higher-level languages! ๐Ÿš€




Comments

Popular posts from this blog

Project Stage 3: Enhancing GCC Pass for Multiple Function Clone Handling: Progress Update

Lab - 1: Exploring 6502 Assembly

Project Stage 1: Troubleshooting a bit - Coming soon