series: My Series Name
I am doing a course at my school where we are supposed to work on 6502 emulator. I always wanted to do low level code, and everything seems super interesting!!. Lets have a look at what I learned in the first two weeks.
1. The 6502 CPU Overview
The 6502 CPU is historically significant because it powered early computers like the Apple II, Commodore 64, and even the original Nintendo Entertainment System. Unlike modern processors with vast instruction sets and high complexity, the 6502 has a very limited set of instructions, making it ideal for learning assembly language.
2. Memory Interaction
The 6502 CPU can directly address up to 64KB of memory using 16-bit addresses.The CPU interacts with memory through various addressing modes:
Immediate Addressing: The value is directly included in the instruction. For example, LDA #$01 loads the number 1 directly into the accumulator.
Zero-Page Addressing: Accesses the first 256 bytes of memory, which can be done more quickly. For instance, LDA $00 fetches a value from memory location $00.
Absolute Addressing: Uses a full 16-bit address. For example, LDA $0200 will load the value from memory location $0200.
Indexed Addressing (with X or Y): This allows us to add the value of the X or Y register to a base address, making it useful for iterating over arrays or working with data tables. I think of them as variables
3. Programming the 6502
Building a Simple Program
We began writing basic programs for the 6502 using an emulator. The first task was to load values into the accumulator, and store them in memory. Here’s an example of a simple operation:
LDA #$01 ; Load the value 1 into the accumulator
STA $0200 ; Store the value from the accumulator
This simple code loads the value 1 into the accumulator and stores it in memory location $0200, which could represent a part of the screen, a variable in a game, or something else, depending on the system configuration.
There is more that can be done like branching and loop. Lets talk about that in my next blog.
4. What I Took Away from the Lecture
This lecture opened my eyes to how low-level programming works. Assembly language is much closer to how the CPU operates than high-level languages like Python or JavaScript. The experience of coding in assembly on the 6502 made me appreciate the fine grained control you have over the CPU and how even a single instruction or addressing mode choice can significantly affect performance.
5. Moving Forward
I plan to continue optimizing my understanding of assembly by reading more about different CPU architectures and instruction sets. It’ll also be interesting to see how the lessons from this simple 6502 processor scale up to modern CPUs, which are far more complex but still governed by the same fundamental principles.