Share your repls and programming experiences

← Back to all posts
An emulated 6502 CPU in C++!
JuanR4140

Hey!

About this repl..

This repl contains an emulated 6502 CPU, along with a hard-coded program that demonstrates some instructions! This isn't complete by a long shot, but it has enough instructions that making some programs is definitely possible.

What it contains in detail..

The program first defines u32, a Byte, and a Word. It then makes 2 variables from structs 'memory' and 'CPU'.
It fills values in memory, and executes it. cpu.execute(cycles, memory) calls the execute function in CPU and uses the cycles and memory to step through memory and perform the instructions, decreasing the cycles. (cycles have to be passed in manually, which makes it a pain to write programs sometimes)

Info about the hard-coded program..

In memory location FFFC, we pass in a JMP instruction to jump to a different location in memory, and pass in 0xA in FFFD to tell it exactly where to jump. Then in 0xA location, we call INC to increase the 0x0 memory location by 1, then call LDA to load the accumulator with memory in 0x0. In memory location 0xE, we call CMP to compare the memory in 0xF (0x3) with the accumulator. Since it's the first time, running this, and the accumulator has only 1, it'll set the negative flag and continue onwards. In instruction 0x10, we call BEQ, which branches to a new memory location if the Zero flag is set. This flag is not set however, and program continues execution. The next instruction JMP, tells us to jump to location 0xA. Does this sound familiar? Yep. This is basically a loop. It does this 2 times, until the third time when BEQ does actually change the location since the Zero flag does get set because of the CMP. We skip 8 memory locations ahead to 0x19 where we call LDX to set the X register to 10, and call LDY to set the Y register to 10, and then the program ends.

Ending notes

Whoo, that was a lot. I hope you found interest in this and hopefully you learnt something new! Any thoughts? :-)

Voters
Theboys619
MarcusWeinberger
SudhanshuMishra
PixiGem
RohilPatel
MasonMarquez
Warhawk947
AmoghTheCool
JuanR4140
Comments
hotnewtop
RohilPatel

Crazy

JuanR4140
SqueezyS

@JuanR4140 insane

MinerProMan

@SqueezyS wacky

PixiGem

epik!