# Visual Basic > Games and Graphics Programming > Game Demos >  Nintendo Entertainment System emulator in VB6 [EXE + source]

## miker00lz

*EDIT: Updated to version 0.1.1, added 1x, 2x, 3x scaling selection. Fixed bug where it crashed if you click cancel on the load state dialog.*

*EDIT 2: Updated to version 0.1.3, MMC3 fixed enough so that Super Mario Bros 3 basically runs perfectly. Added battery-backed SRAM emulation. Added netplay support. Not tested over internet, works great on LAN.*

I've been working on a NES emu from scratch (other than some small snippets for DirectX audio and fast image blitting) in VB6 the last couple of weeks, thought I'd share what I have. It's still a work in progress, but a lot of games play fine. It's got auto frame-skipping, save states, and AUTHENTIC Game Genie support! Source and EXE included in attachment. Check the readme.txt if you have any problems or ask in here!  :Big Grin: 

(The GG ROM is legal to include, Galoob made it public domain years ago)

The bad:
 - A lot of games have graphics glitches or don't run.
 - APU has a few issues, most noticeably that I haven't been able to get sweeps to work right so they're disabled for now.
 - It's slow. You'll want an Intel Core-i3/i5/i7 or AMD equivalent to play at full a 60 FPS. High end Pentium 4 or Core 2 Duo will work, but it will skip frames to compensate.

I'll be fixing things over the next few weeks and updating with major improvements here! If it doesn't start or has no audio, start cmd.exe AS ADMINISTRATOR, then run resreg.bat.

*Download: (too large to attach to post)*

Source and EXE in ZIP: http://rubbermallet.org/vbNES-0.1.3.zip
MSI installer, no source: http://rubbermallet.org/vbNES-0.1.3.msi

MSI doesn't require messing with registering any files.

And some screenshots:

----------


## miker00lz

Just FYI if anybody cares, the latest update v0.1.3 above now plays Super Mario Bros 3 pretty much perfectly...

----------


## Jacob Roman

Well I must say your sound is extremely off which I'll get into a bit later since its 3am o.o

But another thing thats easier to solve for ya is that your palette is a way off. Basically for many NES emulators out there, their palette is off too. The NES palette that's been mentioned all over the internet is also extremely off, and more like colors captured after taking photos of a TV using a professional camera, which actually darkens a lot of the colors. And I'm pretty damn picky when it comes to emulating something, especially colors. I understand there is no way to get the exact colors but I have a method that's pretty freaking close, if not exact. When I use my laptop, I have it hooked into my 40 inch HDTV. When I use my real NES, I also have it hooked into the same HDTV. No matter what, the brightness and contrast are the same for both the laptop and NES. So when I make my emulator and run it, I can go back and forth between Video and HD mode (my laptop) to compare colors. Although my so called guessing game in values wasn't precise, it got it pretty close. However, I wasn't satisfied enough. So I recently got an El Gato composite video capture cable. Funny thing is, it was the same exact colors as if I had it on Video mode, only now I can see it off my laptop. Although it was composite and still had a blend of particular colors, I took snapshots and blended the average color of the blurred composite color in photoshop, which then gave me the actual color that matched the captured video! I didn't tell too many people this, but I have been redesigning the emulator on C++ using DirectX, so use this new palette. Play Super Mario Bros and you'll see a difference:


cpp Code:
void Create_Real_NES_Pallete_NTSC(){    int Current_Color;     //Row 1    Pallete[0] = D3DCOLOR_XRGB(104, 104, 104);    Pallete[1] = D3DCOLOR_XRGB(0, 42, 136);    Pallete[2] = D3DCOLOR_XRGB(20, 18, 167);    Pallete[3] = D3DCOLOR_XRGB(59, 0, 164);    Pallete[4] = D3DCOLOR_XRGB(92, 0, 126);    Pallete[5] = D3DCOLOR_XRGB(110, 0, 64);    Pallete[6] = D3DCOLOR_XRGB(108, 6, 0);    Pallete[7] = D3DCOLOR_XRGB(86, 29, 0); //One of the coin block animations in Super Mario Bros    Pallete[8] = D3DCOLOR_XRGB(51, 53, 0);    Pallete[9] = D3DCOLOR_XRGB(11, 72, 0);    Pallete[10] = D3DCOLOR_XRGB(0, 82, 0);    Pallete[11] = D3DCOLOR_XRGB(0, 79, 8);    Pallete[12] = D3DCOLOR_XRGB(0, 64, 77);    Pallete[13] = D3DCOLOR_XRGB(0, 0, 0);    Pallete[14] = D3DCOLOR_XRGB(0, 0, 0);    Pallete[15] = D3DCOLOR_XRGB(0, 0, 0);     //Row 2    Pallete[16] = D3DCOLOR_XRGB(173, 173, 173);    Pallete[17] = D3DCOLOR_XRGB(4, 62, 210);    // CORRECT 'Blue: Punch Out Ring Floor    Pallete[18] = D3DCOLOR_XRGB(66, 64, 255);    Pallete[19] = D3DCOLOR_XRGB(117, 39, 254);    Pallete[20] = D3DCOLOR_XRGB(160, 26, 204);    Pallete[21] = D3DCOLOR_XRGB(183, 30, 123);    Pallete[22] = D3DCOLOR_XRGB(145, 39, 1);    // CORRECT 'Dark Red: Marios suspenders in Super Mario Bros, Marios hair, mustache, and boots as fire mario.    Pallete[23] = D3DCOLOR_XRGB(113, 61, 0);    // CORRECT 'Brown: Ground Bricks and regular bricks of outdoor worlds in Super Mario Bros. Background of Super Mario Bros Title.    Pallete[24] = D3DCOLOR_XRGB(87, 89, 1);     // CORRECT 'Dark Greenish Yellow 'Marios Shirt    Pallete[25] = D3DCOLOR_XRGB(56, 135, 0);    Pallete[26] = D3DCOLOR_XRGB(0, 121, 0);     // CORRECT 'Dark Green: Hills, turtle shells, pipe, top of flag pole in Super Mario Bros. Green spots on 1-Up.    Pallete[27] = D3DCOLOR_XRGB(0, 143, 50);    Pallete[28] = D3DCOLOR_XRGB(0, 99, 133);    // CORRECT 'Dark Turquoise: Ground and ceiling bricks in dark worlds such as 1-2 of Super Mario Bros 'D3DCOLOR_XRGB(0, 104, 101) '    Pallete[29] = D3DCOLOR_XRGB(0, 0, 0);    Pallete[30] = D3DCOLOR_XRGB(0, 0, 0);    Pallete[31] = D3DCOLOR_XRGB(0, 0, 0);     //Row 3    Pallete[32] = D3DCOLOR_XRGB(255, 254, 255);    Pallete[33] = D3DCOLOR_XRGB(100, 176, 255);    Pallete[34] = D3DCOLOR_XRGB(134, 129, 255); // CORRECT 'Sky Blue: Sky of outdoor worlds in Super Mario Bros    Pallete[35] = D3DCOLOR_XRGB(198, 118, 255);    Pallete[36] = D3DCOLOR_XRGB(243, 106, 255);    Pallete[37] = D3DCOLOR_XRGB(254, 110, 204);    Pallete[38] = D3DCOLOR_XRGB(254, 129, 112);    Pallete[39] = D3DCOLOR_XRGB(221, 141, 23);  // CORRECT 'Peach/Light Yello: Marios skin, Bright color coins, bright color ? blocks in Super Mario Bros. Mushroom main color.    Pallete[40] = D3DCOLOR_XRGB(188, 190, 0);    Pallete[41] = D3DCOLOR_XRGB(127, 200, 0);   // CORRECT 'Green: Pipes in Super Mario Bros. Bright Green Bushes    Pallete[42] = D3DCOLOR_XRGB(92, 228, 48);    Pallete[43] = D3DCOLOR_XRGB(69, 224, 130);    Pallete[44] = D3DCOLOR_XRGB(72, 205, 222);    Pallete[45] = D3DCOLOR_XRGB(79, 79, 79);    Pallete[46] = D3DCOLOR_XRGB(0, 0, 0);    Pallete[47] = D3DCOLOR_XRGB(0, 0, 0);     //Row 4    Pallete[48] = D3DCOLOR_XRGB(255, 255, 255); // CORRECT 'White: Super Mario Bros Text. Clouds    Pallete[49] = D3DCOLOR_XRGB(192, 223, 255);    Pallete[50] = D3DCOLOR_XRGB(211, 210, 255);    Pallete[51] = D3DCOLOR_XRGB(232, 200, 255);    Pallete[52] = D3DCOLOR_XRGB(251, 194, 255);    Pallete[53] = D3DCOLOR_XRGB(254, 196, 234);    Pallete[54] = D3DCOLOR_XRGB(252, 204, 197); // CORRECT 'Pinkish White: Super Mario Bros Title Logo, White in bricks    Pallete[55] = D3DCOLOR_XRGB(253, 219, 175); // CORRECT 'Pinkish White 2: Marios Shirt with Fireflower in Super Mario Bros    Pallete[56] = D3DCOLOR_XRGB(228, 229, 148);    Pallete[57] = D3DCOLOR_XRGB(207, 239, 150);    Pallete[58] = D3DCOLOR_XRGB(189, 244, 171);    Pallete[59] = D3DCOLOR_XRGB(179, 243, 204);    Pallete[60] = D3DCOLOR_XRGB(181, 235, 242);    Pallete[61] = D3DCOLOR_XRGB(184, 184, 184);    Pallete[62] = D3DCOLOR_XRGB(0, 0, 0);    Pallete[63] = D3DCOLOR_XRGB(0, 0, 0);        for (Current_Color = 0; Current_Color <= 63; Current_Color++)    {        Pallete[Current_Color + 64] = Pallete[Current_Color];        Pallete[Current_Color + 128] = Pallete[Current_Color];        Pallete[Current_Color + 192] = Pallete[Current_Color];    }}

And this is how the colors should look. I put both the real NES captured through my El Gato and my emulator side by side along with yours to show you what I mean:

----------


## miker00lz

Good work on the capture. Yeah, my palette choice was a personal preference but I was thinking about making it selectable with the proper palette as the default. The ability to load in a PAL file would be nice too, and easy to add. For the audio, yes it needs some work. Sweeps don't work, never was able to get that right. The tone is also lower than it should be. This is an easy fix, I deliberately sped up sample generation slightly to ensure the buffer is always full when the sound card is ready for more data. Kind of a band-aid hack that I've been meaning to get back to. There's also a clicking issue with some games (for example, play SMB3 then pause it and wait a few seconds). I didn't have that in an older version of the emulator, I need to restore the audio module to an older version. Other than these known issues, I think it's fairly close.

This emulator is largely a port from a C emulator I wrote a few years ago. There are a few bugs that got introduced during the port that I need to work out as well. Micro Machines doesn't play in the VB version, for one. I think it's a sprite zero hit problem.

----------


## Jacob Roman

Well you could take a look at my NES emulator in my signature. The sweeps work there at least  :Wink:

----------


## miker00lz

I'll take a look at your sweeps, thanks. IIRC, there were some issues in your sound too like the SMB bricks smashing. The NES APU is just a tough beast to get right!

----------


## Jacob Roman

Yea I'm not hearing bricks smash, but at least I get some nice beats / punch sounds in Double Dragon. And I literally matched my values with the documentation. Must have missed something.

----------


## miker00lz

EDIT: deleted

----------


## Jacob Roman

Hmmm missed it. If ya have any more NES questions let me know :P

----------

