As a big fan of low level programming, and exploring hardware flavors from CPU that were to the world when I was not even in my parents mind, I decided to teach myself 6502 architecture and programming. That being said, I do think that these series of articles are not about anything else than just my passion for learning stuff and exploring older hardware. This article is subject to change and will indeed change, see it a bit more like a notebook, notepad and notations of stuff I explored in my learning journey.

### Setup overall description

To start writing 6502, we need to have some way to write assembly code, an assembler and an emulator.

The pipeline: So, to start writing 6502 assembly code, we need to dissect few steps of the pipeline. In shorts, we will send assembly to the assembler, then the assembler will produce some sorts of binary that will then be readable and executed by the emulator.

So from left to right, we have the overall idea on our code will be assembled into assembly using an assembler, then packed into a rom that we will be able to load into another program called the emulator.

The Code:

This is what we want the processor to execute. This is where we will write the actions that we want our processor to do, in order(we will get into more detailed later). For now, let’s just say that the first step is to write assembly for the CPU of the machine we are writing code for.

Assembly code will be what we send to the assembler, assembly code allow us to talk with the 6502 by indicating what we want the CPU to do. Note that every assembly language got it’s own different flavor, and you should use an assembler that is made to translate the assembly you are coding to the machine on wich you want your code to run.

The Assembler:

The assembler takes in assembly code, and create a *sort-of* executable. I would prefer to use the name rom here, as the output of the DASM assembler will create a file that will then need to be loaded to an emulator to make it run.

https://dasm-assembler.github.io/

The Emulator:

We will use the Stella Emulator, the emulator will allow us to load the *rom* that DASM generated from the assembler. Plus, it also allow us to run the assembly line by line, see the RAM, bus value and so on…

### Hello World 6502

Ok, first thing: Note that what we are doing in here is to simply test the workspace with assembly code, DASM and the emulator. It is the most naive and dumb way to do it, but from what I explored, it helps to actually got our hands dirty at first, then go deeper into how every parts of the puzzle works. But for now, let’s just test step we have described in the last part of the article.

To test our workspace, we need to write an assembly, translate it to machine code using DASM, and load the output of DASM into our emulator.

So the source of our program goes like this :

001.asm

	processor 6502
seg Code ; define ass seg
org $F000 ; origin of rom at$F000
Start: ;
ldx #82 ; Load the x register with decimal value 82
jmp Start ; infinite loop

org \$FFFC
.word Start
.word Start


Let’s assemble it using DASM

dasm 001.asm -f3 -v0 -osimpleXReg.bin


Once running this command to assemble our source code, we now have a “rom” that had been generated from the DASM assembler. The rom, that we will load inside the Emulator is located in the same folder as our assembly.

We can now load simpleXReg.bin into the emulator.

Let’s emulate it using Stella

When opening Stella, you will be prompt with a file system that asks you to select a ROM to run on the emulator. I will then select the simpleXReg bin that was generated from the DASM assembler

Then, if we open debugger mode, we can see that we have indeed the value 52 inside the de X register of the CPU

### Counting in different base

It is trivial for us to count in base 10, If I told you I have 9 apples it means I have 9 apples! While being highly related to the intuition we built while learning basic mathematics, it is also important to remember that what we are dealing with is just a way to represent values. When we say 10, it means we have a +1 unit of 10th. Allowing us to grow to larger number, therefore 99.

This part of the article is to refresh some basic notations. Our brains are wired to count in base 10, everything we count is in base 10. But computers count in base 2! Programmers along the decades of talking to hardware found ways to transfer those ways of seeing numbers into what machine can understand.

[10^1]	[10^0]
0
1
2
3
4
5
6
7
8
9
1       0
1       1
1       2
1       3
1       4
1       5
...

5       0
5       1
5       2
5       3
...
9       0
9       1
...
9       9


So, to store 99 we simply 1 position to be 9 and then crank the 10 (the base) to be 9. It gives us 99

### The 6502 processor

Here is a bit of zooming into the processor we are going to work with. There some few parts we will describe into deeper look, but for now, let’s just see a little diagram of the 6502 CPU

Happy
0 %
0 %
Excited
0 %
Sleepy
0 %
Angry
0 %
Surprise
0 %

5 Star
0%
4 Star
0%
3 Star
0%
2 Star
0%
1 Star
0%