Javascript NES emulator 1 2 »
Users browsing this thread: 1 Guest(s)

I'm attempting to make a NES/6502 emulator in JS.

Screenshot of it in action: (would gif, but gyazo didn't feel like working today)
[Image: b4175de18675e35165263b29c79bca23.png]

Debugging stuff:
[Image: 0f4db3d055b99934a66a4aad4559b381.png]

Links: Emulator
Last updated: [Image: NES_lastupdate.php]

Key bindings for controller 1:
Select: Q
Start: E
DPAD: WASD
M: A
N: B

XBOX360 controller bindings:
Select: Back
Start: Start
DPAD: DPAD/Left analog
A: A
B: X

Current features:
6502 core with all 151 official opcodes implemented
Tracker for 6502 registers, zpage/stack memory and NES I/O registers
Built in 6502 dis/assembler (click code in the disassembler to replace it)
PC breakpoints (click an address in the disassembler)

Todo features:
Scrolling & sprite 0 hit
Mappers
General purpose memory editor
Memory r/w breakpoints
Memory scanning
pAPU (sound)
Input automation
Multiplayer support (online too?)
(This post was last modified: 03-06-2015, 07:57 PM by shygoo. Edit Reason: (updated April 13) )

Awesome project!  Last year, I wrote a NES emulator in C and it was one of the most rewarding things I've worked on.  For testing, I highly recommend running instr_test-v4.  It has tests for all the opcodes (there is even one that tests the illegal/unofficial opcodes) and prints a report to both the PPU and to $6000 which is extremely helpful for testing before you have the PPU written.  Note: mapper 1 is required for the all/official NES files, but anything in the rom_singles can be run without a mapper.

Keep posting your updates here.  I'd like to see how your JS emulator progresses.

So I've made a bit of progress. It now loads roms into cpu/ppu memory (quite lazily at the moment). I also added a bunch of stuff to my debugging interface and the 6502 is almost done; just gotta add a few more ops and tweak the ones I got lazy on. I have yet to start on mappers and the other processors.

Screenshot:
[Image: 7a5267d2ed6b97a1568c4fdc16536e1c.png]
Woot, executes the first 8 instructions in Super Mario Brothers before it hangs due to the lack of a PPU Big Grin

Edit: little hotfix, the PC is now set correctly on boot instead of just going to 0x8000
(This post was last modified: 15-04-2015, 04:08 PM by shygoo.)

Wow, you're making quick progress!  I really like the debugger you've put together.

Hope you don't mind, but I reviewed some of the more tricky instructions and have some corrections:

CMP/CPX/CPY
The flags can be set according to this (even used your !! trick):
setZN(reg - operand); // uint8 subtraction, not sure how JS handles it
SR.C = !!(reg >= operand);

ADC
In my opinion, the flags for this instruction are the most difficult to get right
SR.C: set if addition would result in 9th bit set.  I cheated and did 16-bit math and set: SR.C = (u16 & 0x100)
ST.V: set if result would be > 127 or < -128. I cheated and did 16-bit signed math and set: SR.V=(s16 > 127 || s16 < -128)
SBC's flags are also tricky to get right

DEC (C6,D6,CE,DE), INX/INY/DEX/DEY
These need to set N/Z flags (you have comments on some, but not all)

ASL ac
SR.C should be set before AC is shifted
ASL abs/zpg
These all need to be reviewed, using the correct memory index and Z/N should be set after shifting

Vectors / BRK
The vector table is at 0xFFFA.  You've got the reset one down, but still need to keep track of NMI and BRK.  The NMI is the non-maskable interrupt which the PPU will generate on VBLANK once it's turned on and BRK instructions will go to the BRK vector.
NMI:   0xFFFA
RESET: 0xFFFC
BRK:   0xFFFE

If you'd like, I can review all the instructions when you think you have them all implemented.

I would also recommend against using SMB1 ROM for testing since it is actually one of the more difficult games to emulate.  It has made the list of Tricky-to-emulate games.  I would suggest starting with one of the NROM games that doesn't use Sprite 0 hit and minimal scrolling such as Balloon Fight or Golf.  Note that these are 16K PRG-ROM (only 1 bank) and thus will need to be loaded at both 0x8000 and 0xC000 (usually only the latter is needed) to get the vector table at the end.

I applied some fixes to the 6502 ops (thanks queueRAM), added some test code for processing pattern tables and today I added this much needed dis/assembler:
[Image: 34fce97390d15d8447e2ec9733bfafda.png]

(16-04-2015, 12:46 AM)queueRAM Wrote: I would also recommend against using SMB1 ROM for testing since it is actually one of the more difficult games to emulate.  It has made the list of Tricky-to-emulate games.  I would suggest starting with one of the NROM games that doesn't use Sprite 0 hit and minimal scrolling such as Balloon Fight or Golf.  Note that these are 16K PRG-ROM (only 1 bank) and thus will need to be loaded at both 0x8000 and 0xC000 (usually only the latter is needed) to get the vector table at the end.

I would agree to this to an extant. I think it's a good idea to try running a ROM like Super Mario Bros. because if the emulator he's making can work perfectly with games like Super Mario Bros, Zelda, etc. it would be easier to run everything else.
Watch Pablo's Corner Today!

6502 emulation is running accurately now and is practically done besides for a few register r/w events (thanks again queueRAM for a ton of help debugging), and I'm just getting started on the ppu. Also note that the file has been moved here http://file.s.gp/NES.php so I can have a build date/signature at the top automatically.
(This post was last modified: 28-04-2015, 06:26 PM by shygoo. Edit Reason: i cant grammar )

Haven't bumped in a while, here's what's been added:
Mostly finished clock cycle counting & NMI
Sprite rendering (colorless at the moment)
Roms can now be streamed in:
[Image: NEZFUlO.png](<- I'll be happy to upload any suggestions)

I should have colors and background rendering as soon as I get the ppu i/o registers right

Frame timing is good now (games won't run faster than they're supposed to)
Sprites are now colored
Backgrounds are now rendered (attribute tables aren't being processed yet, so the colors aren't right)
Proper aspect ratio is maintained when fullscreen
XBOX controller support added

Color updates aren't completely optimized yet so slow-downs are to be expected with games that use color-changing sprites.

Backgrounds are now rendering awesomely colored
Scrolling is partially implemented
The tile caching method is infinitely times better than it was before (no more slowdowns)
Added a scanline/tv noise effect for feels
Server related thing: now local copies of the emulator should be able to stream roms from the host I'm using
Chrome requires some extra parameters for this "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --allow-file-access-from-files --disable-web-security  --disable-web-security

I'm thinking TAS and memory scanning features would be relatively easy to add so that will definitely be something for the future. I'm also thinking multiplayer support over http might be possible using some sort of ajax & byte serving setup with some game-specific ram synchronization rules but I'm a bit doubtful that it'll be fast enough to work properly. WebSocket setup and I'll probably give it a shot at some point.
(This post was last modified: 20-05-2015, 11:29 PM by shygoo.)

Javascript NES emulator 1 2 »
Users browsing this thread: 1 Guest(s)