Lab 3

This lab was about creating a program for the 6502 Emulator that outputs to both character and graphics screens, accepts user input, and performs arithmetic operations. I decided to write a simple Number Guessing Game. 


Objective

The program generates a random number between 1 and 100, and the player guesses the number. It provides feedback like "Too High" or "Too Low" and indicates correct guesses visuals.


Features

  1. Character Screen:
    • Displays instructions and feedback messages.
  2. Graphics Screen:
    • Changes colors based on the result:
      • Green: Correct guess.
      • Red: Too low.
      • Yellow: Too high.
  3. Operations:
    • Compares the user’s guess with the random number.

Code

Here is the code below;

; Number Guessing Game for 6502 Emulator

; Constants
RANDOM_NUMBER = $42 ; Replace this with a random number generator
CHAR_SCREEN = $0400
GRAPHICS_SCREEN = $2000
GREEN = $1
RED = $2
YELLOW = $3

; Initialize program
    LDX #$00        ; Clear X register
    STX CHAR_SCREEN ; Clear character screen

; Display instructions
    LDA #$20        ; ASCII space
    LDY #$00
DisplayText:
    STA CHAR_SCREEN, X
    INX
    CPX #$50
    BNE DisplayText

    LDA #<Message
    LDY #>Message
    JSR PrintMessage

; Main game loop
StartGame:
    LDA RANDOM_NUMBER
    STA $50          ; Store the random number in memory
    JSR GetInput     ; Get user input
    LDX $51          ; Load user's guess
    LDY $50          ; Load random number
    CPX $50          ; Compare user's guess with random number
    BEQ CorrectGuess ; Branch if equal
    BCC TooLow       ; Branch if less than
    BCS TooHigh      ; Branch if greater than

TooLow:
    LDA #RED
    STA GRAPHICS_SCREEN
    LDA #<LowMessage
    LDY #>LowMessage
    JSR PrintMessage
    JMP StartGame

TooHigh:
    LDA #YELLOW
    STA GRAPHICS_SCREEN
    LDA #<HighMessage
    LDY #>HighMessage
    JSR PrintMessage
    JMP StartGame

CorrectGuess:
    LDA #GREEN
    STA GRAPHICS_SCREEN
    LDA #<WinMessage
    LDY #>WinMessage
    JSR PrintMessage
    JMP StartGame

; Subroutines
PrintMessage:
    LDA (Message), Y
    BEQ DoneMessage
    STA CHAR_SCREEN, X
    INX
    INY
    JMP PrintMessage
DoneMessage:
    RTS

GetInput:
    LDA #$10  ; Simulated input
    STA $51
    RTS

; Messages
Message:
    .asciiz "Guess the number between 1 and 100!"
LowMessage:
    .asciiz "Too Low! Try again."
HighMessage:
    .asciiz "Too High! Try again."
WinMessage:
    .asciiz "You got it!"


Reflection

This lab was a fun challenge, especially building logic with assembly language. While debugging was tough, the experience improved my understanding of how low-level programs interact with hardware. Learning languages like assembly that are so raw to machine language really shows how the interaction happens.

Comments

Popular posts from this blog

Building GCC Part 2

Wrapping Up Project : Stage 3