LogFAQs > #979481248

LurkerFAQs, Active Database ( 12.01.2023-present ), DB1, DB2, DB3, DB4, DB5, DB6, DB7, DB8, DB9, DB10, DB11, DB12, Clear
Topic List
Page List: 1
TopicWriting an NES emulator ama
Yellow
03/21/24 2:27:22 AM
#1:


There are already 40+ NES emulators out there but I want to make one just so I can learn emulation better. Here are some key things that you might not know;

The CPU is the NES. When you provide it power it just starts moving memory around which makes other things happen. You could violently rip out the other parts, and the CPU would keep chugging, probably kind of confused.

The CPU has a number inside it that tells it where to get its next instruction. That number has a max value of 65535, so that's how many "things" it can see. Some of those values lead to nothing, some lead to what's in the RAM, what's in the ROM, and two of those addresses tell it what buttons are being pressed on the controller.

Here's a log of my CPU executing 10 instructions on a ROM called "nestest.nes". I'm able to tell if it's doing things right by comparing it to a similar log that's basically the cheat sheet to the test. You might notice that these are assembly instructions. It runs until it hits an instruction that I haven't added, I have 22/151 instructions. It gets through 72 before hitting one I haven't done yet.

1 C000 4C F5 C5 JMP $C5F5 A:00 X:00 Y:00 P:24 S:FD CYC:7
2 C5F5 A2 00 LDX #$00 A:00 X:00 Y:00 P:24 S:FD CYC:10
3 C5F7 86 00 STX $00 = 00 A:00 X:00 Y:00 P:26 S:FD CYC:12
4 C5F9 86 10 STX $10 = 00 A:00 X:00 Y:00 P:26 S:FD CYC:15
5 C5FB 86 11 STX $11 = 00 A:00 X:00 Y:00 P:26 S:FD CYC:18
6 C5FD 20 2D C7 JSR $C72D A:00 X:00 Y:00 P:26 S:FD CYC:21
7 C72D EA NOP A:00 X:00 Y:00 P:26 S:FB CYC:28
8 C72E 38 SEC A:00 X:00 Y:00 P:26 S:FB CYC:30
9 C72F B0 04 BCS $C733 (?) A:00 X:00 Y:00 P:27 S:FB CYC:32
10 C735 EA NOP A:00 X:00 Y:00 P:27 S:FB CYC:35

What's it doing?

It starts at C000 (hex)
1 JMP: It's jumping to that memory location. Aka, it sets the number it looks for its next instruction to C5F5. Notice how the memory address changes after this.
2 LDX: Load that value into the X register. A register is a byte that the CPU keeps internally so it can access it faster. It has several of these.
3 STX: Store the X register value in that memory location. It's setting those memory addresses to 0.
4 JSR: It's jumping to a location and storing the return address on the stack. Later, when it hits a "RTS" instruction, it will pull this value from the stack and come back to this point in memory.
5: NOP: Just do nothing for a little bit.
6: SEC: The has a status register. It uses all 8 bits of this byte as individual true or false values. In this case, it sets the "carry" flag to 1. This one 0000_000c.
7: BCS: And immediately after that, it checks to see if the carry flag is set. If the carry flag is set, it will jump to another location. And it is, so it will.

---
https://www.youtube.com/watch?v=5C_Wrt6pNSw
... Copied to Clipboard!
Topic List
Page List: 1