Home
JAQForum Ver 20.06
Log In or Join  
Active Topics
Local Time 09:30 24 Sep 2020 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : WIP: CMM2 NES Emulator

     Page 1 of 4    
Author Message
Atomizer_Zero
Senior Member

Joined: 04/07/2020
Location: United Kingdom
Posts: 134
Posted: 06:15pm 29 Jul 2020
Copy link to clipboard 
Print this post

I've been holding off on sharing any details about this current project. I wanted to at least get something displaying on the screen first..

So, some backstory. A youtuber called JavidX9 did an amazing video series on programming a NES Emulator in C++. I followed that some time ago, and essentially ported it to Java. It wasn't an easy task, as many of the conveniences of C++ don't translate at all/well to Java. Either way, it works great in Java (without sound...).

So, 3 or 4 weeks ago, I started porting that code to MMBasic (first with the win/dos version, testing things out), and now on the CMM2.

It's finally "running". It SLOW AS ALL HECK! of course. takes about 10 minutes to actually show anything, and of course, barely anything actually works, but as far as I can tell so far, the cpu is pretty much in fully working order (minus unofficial opcodes..)



On the left is the serial output, displaying debug info and on the right is the video output captured via OBS. The rom being run is "nestest.nes", which is designed for tesing the cpu, especially when the ppu (the graphics processor) isn't working properly; which is the case right now. The display is actually more of a hack to actually get it to draw... more work needs to be done.

The code is currently very much unoptimized, as you would imagine, so I know it can get faster with time.

I will continue to update this thread as progress is made.
Edited 2020-07-30 04:18 by Atomizer_Zero
 
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 883
Posted: 06:26pm 29 Jul 2020
Copy link to clipboard 
Print this post

I know I could google, but what is a "NES"?
 
Atomizer_Zero
Senior Member

Joined: 04/07/2020
Location: United Kingdom
Posts: 134
Posted: 06:29pm 29 Jul 2020
Copy link to clipboard 
Print this post

Nintendo Entertainment System. The original Nintendo console, which had Super Mario Brothers, Legend of Zelda, Donkey Kong, etc. etc.
 
thwill

Guru

Joined: 16/09/2019
Location: United Kingdom
Posts: 710
Posted: 07:40pm 29 Jul 2020
Copy link to clipboard 
Print this post

That is completely amazing    

How many instructions per second do you think the emulated 6502 is managing?

Tom
Edited 2020-07-30 05:43 by thwill
CMM2 Welcome Tape, Z-MIM: Zork on the CMM2
 
Decoy
Regular Member

Joined: 02/08/2019
Location: Denmark
Posts: 96
Posted: 07:58pm 29 Jul 2020
Copy link to clipboard 
Print this post

That is awesome!! Great, great project!

By the way, I love the "What is a NES?" - You can't make something like that up :)

.
 
Atomizer_Zero
Senior Member

Joined: 04/07/2020
Location: United Kingdom
Posts: 134
Posted: 08:00pm 29 Jul 2020
Copy link to clipboard 
Print this post

Thanks :D

It's hard to say... depending on the instruction, it is probably about 20 to 40 instructions a second. But it's also running the ppu too, which takes a lot of time as well. It's performing around 125 cycles per second.
It's obviously completely unplayable at these speeds. in fact, it's so slow, you can watch the screen update one scanline at a time lol.
 
Atomizer_Zero
Senior Member

Joined: 04/07/2020
Location: United Kingdom
Posts: 134
Posted: 08:06pm 29 Jul 2020
Copy link to clipboard 
Print this post

We have hit the Donkey Kong title screen! This is great news, as it's proof of the cpu working as intended. It takes around 15 minutes to get there... but it does get there lol. Still, the PPU isn't working correctly. Sprites aren't loading yet. Lots more work to do.


 
mkopack73
Senior Member

Joined: 03/07/2020
Location: United States
Posts: 178
Posted: 09:54pm 29 Jul 2020
Copy link to clipboard 
Print this post

Time to start optimizing some things as CSubs!  But that’s awesome!!! If at all possible if you can keep the 6502 portion segmented then it means other older 8bit 6502 based systems (Vic20, c64, apple2, Atari 400/800) could also be developed.
 
berighteous
Senior Member

Joined: 18/07/2020
Location: United States
Posts: 110
Posted: 10:08pm 29 Jul 2020
Copy link to clipboard 
Print this post

This is awesome!  Certainly a "look what I did!" triumph when you get it done.  Take a time lapse video on it booting and running a game.  Freaking great.
 
Atomizer_Zero
Senior Member

Joined: 04/07/2020
Location: United Kingdom
Posts: 134
Posted: 10:14pm 29 Jul 2020
Copy link to clipboard 
Print this post

I'm not even sure where to begin with csubs lol. I can imagine it would speed things up though.

At the moment, the MainProgram, CPU, PPU, BUS, Mappers, PPURegisters, KeyInput and Cartridge are all seperate, although they all link in some way. One thing I will do, once its working well enough, is try to simplify the logic, which would probably involve combining stuff together. But dont worry, this wont happen until the emulator can actually run a game properly.

A handful of Mapper 0 games are reaching the title screen (Donkey Kong, Bomberman, Pinball, etc.), which gives me confidence that the cpu is good. I'm going to link the keyinput into the loop and see if controls are working... then I need to heavily debug the ppu, as drawing to the screen right now is basically forcing colour to draw from the cpuram info.
 
Atomizer_Zero
Senior Member

Joined: 04/07/2020
Location: United Kingdom
Posts: 134
Posted: 10:17pm 29 Jul 2020
Copy link to clipboard 
Print this post

  berighteous said  This is awesome!  Certainly a "look what I did!" triumph when you get it done.  Take a time lapse video on it booting and running a game.  Freaking great.


lol I'll do a video when I feel happy about the ppu rendering properly. Soontm
 
thwill

Guru

Joined: 16/09/2019
Location: United Kingdom
Posts: 710
Posted: 11:30am 30 Jul 2020
Copy link to clipboard 
Print this post

  Atomizer_Zero said  It's hard to say... depending on the instruction, it is probably about 20 to 40 instructions a second. But it's also running the ppu too, which takes a lot of time as well. It's performing around 125 cycles per second.
It's obviously completely unplayable at these speeds. in fact, it's so slow, you can watch the screen update one scanline at a time lol.


I think without extensive use of CSubs you are going to struggle to get to anything playable. The actual hardware runs at 1-2 MHz doesn't it?

Z-MIM which is pure MMBasic and is also effectively an emulator can only run at 1000 instructions per-second. Its instruction set is slightly more complex to decode than 6502, but you're still looking at needing to be 1000 times faster than I managed.

Good luck

Regards,

Tom
CMM2 Welcome Tape, Z-MIM: Zork on the CMM2
 
Atomizer_Zero
Senior Member

Joined: 04/07/2020
Location: United Kingdom
Posts: 134
Posted: 12:43pm 30 Jul 2020
Copy link to clipboard 
Print this post

I might request collaboration at some point lol.

The NES cpu is 1.79MHz (for NTSC). It executes around 1,790,000 instructions per second, which is blazingly fast. If CMM2 can do around 300,000 lines of code a second... I'd need to make sure a whole frame happens within that time frame. That's not going to be easy...

I followed the thread on making a csub, using the example code, and it worked. So I at least know how to do it now. I'm not entirely sure how I'm going to implement that in this yet though. One thing that's detrimental to the speed of execution is how verbose the code is. It's somewhat easy to read and see what's going on (which is good for understanding) but does slow it down.

Currently, I'm fighting with the PPU. For some reason, the palette table isn't returning the colour information when I need it to. If I don't read from the ppu, and just pass the colour table directly, I end up with the screenshots I posted before. Fun to see it works, but obviously, the colours aren't correct.
 
Atomizer_Zero
Senior Member

Joined: 04/07/2020
Location: United Kingdom
Posts: 134
Posted: 03:28pm 30 Jul 2020
Copy link to clipboard 
Print this post



DONE IT! PPU working, finally.
 
Atomizer_Zero
Senior Member

Joined: 04/07/2020
Location: United Kingdom
Posts: 134
Posted: 03:53pm 30 Jul 2020
Copy link to clipboard 
Print this post

Not all good news unfortunately...


this is better, but the palette selection isn't working. And neither are the sprites... back to the debugging lol
 
vegipete

Guru

Joined: 29/01/2013
Location: Canada
Posts: 369
Posted: 04:12pm 30 Jul 2020
Copy link to clipboard 
Print this post

  Atomizer_Zero said  The NES cpu is 1.79MHz (for NTSC). It executes around 1,790,000 instructions per second, which is blazingly fast
It runs at 1790000 cycles per second. Instructions range between 2 and 7 cycles each with an average of about 4 cycles per instruction, depending on what you are doing. 447,500 instructions per second is still rather quick for interpreted BASIC.

I've started playing with a 6502 emulator for the CMM2: 1000 _instuctions_ is taking near half a second - at least 100 times slower than a real 1MHz system.
 
Atomizer_Zero
Senior Member

Joined: 04/07/2020
Location: United Kingdom
Posts: 134
Posted: 04:27pm 30 Jul 2020
Copy link to clipboard 
Print this post

Whoops. Thats what I meant lol. sorry. Been looking at numbers for hours and hours at this point.

right now, the ppu is bottlenecking the cpu pretty hard. obviously the ppu has a lot to do. it does ~340 cycles per scanline, with 262 scanlines per frame. so, for 60 frames per second, its 15,720 scanlines, 5,344,800 ppu cycles. So, with the cpu doing 1,790,000 cycles and the ppu doing 5,344,800 cycles... it's a lot to do in a per second.
 
thwill

Guru

Joined: 16/09/2019
Location: United Kingdom
Posts: 710
Posted: 10:12am 31 Jul 2020
Copy link to clipboard 
Print this post

Hi,

  Atomizer_Zero said  I might request collaboration at some point lol.


I, and I'm sure several others will be happy to offer an opinion

  vegipete said  
  Atomizer_Zero said  The NES cpu is 1.79MHz (for NTSC). It executes around 1,790,000 instructions per second, which is blazingly fast
It runs at 1790000 cycles per second. Instructions range between 2 and 7 cycles each with an average of about 4 cycles per instruction, depending on what you are doing. 447,500 instructions per second is still rather quick for interpreted BASIC.

I've started playing with a 6502 emulator for the CMM2: 1000 _instuctions_ is taking near half a second - at least 100 times slower than a real 1MHz system.


Given that (based on limited data and ignoring graphics) the performance of the CMM2 in BASIC is approximately that of 6502 machine code running at 1 MHz there is a fundamental problem achieving a usable emulation in pure BASIC alone ... it looks like trying to implement an emulator for an ~2 MHz 8-bit system on an ~1 MHz 8-bit system

However it looks likely that the NEs can be emulated in native code (people have emulated the ZX Spectrum on less impressive micro-controllers), so that presumably means it can be done with CSubs ... depends on what limitations they ultimately impose and whether you are happy writing your emulator largely in "C" rather than BASIC.

  Quote  One thing that's detrimental to the speed of execution is how verbose the code is. It's somewhat easy to read and see what's going on (which is good for understanding) but does slow it down.


I doubt additional verbosity accounts more more than 10-20%. If your verbosity is comments and whitespace then that shouldn't slow it down, they are crunched out when the program is tokenized and written to the FLASH (or program RAM). For inlining constants and reducing long identifiers you can use my transpiler, though you might want to wait for the next version as I'm implementing automated identifier crunching; currently replacements have to be specified manually.

Note that there are also pre-existing programs (for earlier 'mites) for doing some of what my transpiler performs here:
       http://fruitoftheshed.com/MMBasic.MMBasic-Source-Code-Formatter-part-of-the-original-MMBasic-library.ashx
and here:
       http://fruitoftheshed.com/MMBasic.MMBasic-Source-Compression-Utility-CRUNCH-bas-v2-4-part-of-the-original-MMBasic-library.ashx
.

Regards,

Tom
CMM2 Welcome Tape, Z-MIM: Zork on the CMM2
 
Atomizer_Zero
Senior Member

Joined: 04/07/2020
Location: United Kingdom
Posts: 134
Posted: 03:10pm 31 Jul 2020
Copy link to clipboard 
Print this post

So, since last night, we've been having frequent power cuts. There's a problem with a wire underground apparently.

So, I've had to get a little creative..
Introducing, my portable battery powered cmm2 dev environment lol


 
Shadamus
Newbie

Joined: 26/07/2020
Location: United States
Posts: 17
Posted: 03:25pm 31 Jul 2020
Copy link to clipboard 
Print this post

Way to improvise!

Just need to find a VGA recording device to pull VGA back in through USB and display on laptop for a closed-loop solution.  :)
 
     Page 1 of 4    
Print this page
© JAQ Software 2020