Skip to main content

Microsoft Silverlight

Answered Question Silverlight NES Emulator with soundsRSS Feed

(0)

Tuska
Tuska

Member

Member

21 points

7 Posts

Silverlight NES Emulator with sounds

 I am currently writing a remake of popular java NES emulator vNES in silverlight.And since vNES is GNU lisenced, so is my implementation. If you want to see source code, please ask. (The games are not included).

 

All games should work but I'm having a bit of trouble with CPU timing and sounds. Sounds are almost perfect, only slight echoing can be heard now and then.

Main problem is the CPU timing, I haven't found a good way how to time CPU in silverlight accurately. (C# version works)

I have tried DateTime.UtcNow.Ticks, Environment.TickCount, using dispatcher timer and composition rendering but none of them seem to be accurate and stable enough for timing. With slow computers the emulator runs too slow and with fast computers it's probably too fast. I have timed emu to run with my computer which is Core 2 Duo E8600.

Future things to do is to add 2nd player support, loading and saving state, game genie and buy more nintendo games.

 

And without further ado: Silverlight nes emulator with super mario 3 (if you see only white screen, refresh..)

Controls are for player one:

Keys for moving

A = Space

B = Ctrl

Start = Enter

Select = Shift

prujohn
prujohn

Contributor

Contributor

3579 points

704 Posts

Re: Silverlight NES Emulator with sounds

Well done!

davidezordan
davidezo...

Contributor

Contributor

5690 points

875 Posts

Silverlight MVP

Re: Silverlight NES Emulator with sounds

Hi Tuska,

cool implementation, well done! Smile

Thanks, Davide

Silverlight MVP

Blog Twitter Silverlight Experts

neopocott
neopocott

Member

Member

368 points

82 Posts

Answered Question

Re: Silverlight NES Emulator with sounds

Hi Tuska,

I would be very interested to have a look at the source code especially for the sound implementation and to give advices if necessary.

Regarding your timing issues, here is the trick I used for my Silverlight emulators (Z80 arcade game and PC emulator) and Quake in Silverlight to achieve perfect and smooth display.

The most accurate loop is using Composition Target rendering which is refreshed every 60Hz. This said you just need to change the MaxFramerate to the current framerate of the NES console.

With this you are sure that CompositionTarget rendering is called for each frame to display. The most important point is now the following, all time-consuming like the CPU core, the sprites and rendering must be handled in the CompositionTarget rendering.

If you do it in another thread, you will slow down the display.

So to ensure perfect timing, you need to determine the exact number of CPU cycles to burn per frame.

Here is my "perfect" loop for my Z80 arcade game:

private const int CPU_CYCLES = 8000000 / 60; // CPU is running at 8Mhz and framerate is 60 fps
 
void Page_CompositionTarget_Rendering(object sender, EventArgs e)
{
    // Burn cpu cycles for one single frame.
    driver_cpu.Execute(CPU_CYCLES);
 
    // Update the video screen for the current frame.
    updateVideo();
 
    // Update sound.
    updateSound();
}

Hope that helps.

Julien Frelat
Microsoft Client App Dev MVP
Quake in Silverlight - Twitter - Blog (French)

Tuska
Tuska

Member

Member

21 points

7 Posts

Re: Silverlight NES Emulator with sounds

CompositionTarget rendering cycle helped indeed.

I have updated the emulator, timing should be okay as long as framerate stays at 60.

Sounds are almost fixed too, only slight noise can be heard now and then. One option was to ignore loop:

protected override void GetSampleAsync(MediaStreamType mediaStreamType)
{

...

}

Without using GetSampleAsync sounds were perfect but browser hung up randomly. And by using above loop, no hang ups but sounds are Almost perfect.

And I have added saving / loading option, press S to save state and L to load it.

Qbus
Qbus

Member

Member

607 points

269 Posts

Re: Re: Silverlight NES Emulator with sounds

Looks great! Keep up the good work :)

--------------------------
Please mark the post as answered if this answers your question
http://www.laumania.net

neopocott
neopocott

Member

Member

368 points

82 Posts

Re: Silverlight NES Emulator with sounds

Do not forget to mark my message as the answer if I could help you. It also helps other people on the forums to directly track answers to common problems they could have.

What is interesting with the CompositionTargetRendering is by default framerate for the Silverlight UI thread is 60 fps. Of course, it is possible to force this framerate when initializing your Silverlight application. Also if you play with the MaxFramerate option, you can also test the current speed of your emulation engine.

You could then add a FPS counter for the emulator. If you like I can share code on this.

Regarding sound, the best approach is to use the GetSampleAsync routine from MediaStreamSource. You ensure the sound stream is constantly feed with some data. Also this does not slow down the UI Thread.

I'm curious about the sound noise you get, could you share the code on this?

Julien Frelat
Microsoft Client App Dev MVP
Quake in Silverlight - Twitter - Blog (French)
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities