Hydra NES Pacman Emulator
Darryl
Posts: 3
In case anyone is interested I am attaching an implementation of an emulator of the original 1980s Nintendo Entertainment System (NES) for the Hydra, running the original Pacman game.
This is an emulation - meaning that every original 6502 instruction is read in and interpreted by the Hydra in real-time. It's not yet perfect but I think it is somewhat amazing that it works at all. This project ran up against all the limitations of the Hydra - only 32 KB of memory to hold the original ROM code, the ROM Pattern Table information, the Name Table information, sprite data, pallettes, scratch memory, etc. In addition there is the constraint of only 512 longs per cog. Inevitably this means that you can't put everything you want in. In particular I wasn't able to include quite all of the original pattern table and unused 6502 instructions are not emulated. The program uses all the available cogs. If I could I would like one more cog to emulate the sound of the original NES. The Hydra appears fast enough at least for this game, but a few more CPU cycles would be nice.
Why was Pacman chosen for this NES emulation project? The answer is that Pacman is a relatively early game which uses only 16KB (out of the 32 KB available on the NES) and only vertical scrolling (no horizontal scrolling) and horizontal mirroring. Therefore it was potentially feasible. Many larger games simply will not be feasible without a major change in approach (such as the use of the Hydra 512K RAM extension card). My objective was to run the original Pacman ROM code. This was nearly achieved. The ROM code was altered slightly for a couple of reasons - one was to change the original pallette of colors (I haven't yet worked out how to do color mapping in real time), another was to take out one KB of unused space to give me just a little more room.
The program seems to run fine. It emulates all the sprites, name tables, scrolling etc. of the original NES, together with the ability to read the gamepad. It also emulates the PPU's non-maskable interrupt which occurs every time the screen is refreshed (during vertical blanking).
One of the nice features of running an emulator is that debugging on the Hydra is made a lot easier. The emulator allows you to step through one instruction at a time, observing changes to each register (including the stack and the status register), together with changes to any memory location you wish to observe. You can add a breakpoint at any instruction.
This code is not perfect. In particular there are some color artifacts of two kinds. First the colors on the title screen are not quite right due to some problems implementing the PPU's attribute table feature. Perhaps someone can figure out what I am doing wrong. The second problem is related to the sprites themselves. Each sprite somehow picks up the color set of another sprite on the same line. This is a subtle error which has proved hard to debug. I am putting the code up as it is. Still, the sprites are mostly the right colors. Also, as noted above, there is no emulation of sound, due to a lack of cogs and a lack of memory space.
This project uses Eric Ball's 6502 emulator which is clever and efficient. But there were a few "errors" in Eric's original code which I spent many hours trying to track down. Hopefully that is now done.
Having put in a lot of effort to get Pacman running I am hoping to move on to test the emulator on some other NES game. I figure that once I have tried this approach on a few different (simple) games I'll be in a better position to know just how much this emulation approach is feasible. Again, the amazing thing here is that it works at all. We are not quite yet in the position of having an NES-on-a-chip, but this comes close.
I am attaching the code as it is. This is my first attempt at distributing code in this way. If there are any concerns about copyright etc. I will, of course, take it down. I am happy to discuss...
Post Edited (Darryl) : 1/11/2010 11:23:50 AM GMT
This is an emulation - meaning that every original 6502 instruction is read in and interpreted by the Hydra in real-time. It's not yet perfect but I think it is somewhat amazing that it works at all. This project ran up against all the limitations of the Hydra - only 32 KB of memory to hold the original ROM code, the ROM Pattern Table information, the Name Table information, sprite data, pallettes, scratch memory, etc. In addition there is the constraint of only 512 longs per cog. Inevitably this means that you can't put everything you want in. In particular I wasn't able to include quite all of the original pattern table and unused 6502 instructions are not emulated. The program uses all the available cogs. If I could I would like one more cog to emulate the sound of the original NES. The Hydra appears fast enough at least for this game, but a few more CPU cycles would be nice.
Why was Pacman chosen for this NES emulation project? The answer is that Pacman is a relatively early game which uses only 16KB (out of the 32 KB available on the NES) and only vertical scrolling (no horizontal scrolling) and horizontal mirroring. Therefore it was potentially feasible. Many larger games simply will not be feasible without a major change in approach (such as the use of the Hydra 512K RAM extension card). My objective was to run the original Pacman ROM code. This was nearly achieved. The ROM code was altered slightly for a couple of reasons - one was to change the original pallette of colors (I haven't yet worked out how to do color mapping in real time), another was to take out one KB of unused space to give me just a little more room.
The program seems to run fine. It emulates all the sprites, name tables, scrolling etc. of the original NES, together with the ability to read the gamepad. It also emulates the PPU's non-maskable interrupt which occurs every time the screen is refreshed (during vertical blanking).
One of the nice features of running an emulator is that debugging on the Hydra is made a lot easier. The emulator allows you to step through one instruction at a time, observing changes to each register (including the stack and the status register), together with changes to any memory location you wish to observe. You can add a breakpoint at any instruction.
This code is not perfect. In particular there are some color artifacts of two kinds. First the colors on the title screen are not quite right due to some problems implementing the PPU's attribute table feature. Perhaps someone can figure out what I am doing wrong. The second problem is related to the sprites themselves. Each sprite somehow picks up the color set of another sprite on the same line. This is a subtle error which has proved hard to debug. I am putting the code up as it is. Still, the sprites are mostly the right colors. Also, as noted above, there is no emulation of sound, due to a lack of cogs and a lack of memory space.
This project uses Eric Ball's 6502 emulator which is clever and efficient. But there were a few "errors" in Eric's original code which I spent many hours trying to track down. Hopefully that is now done.
Having put in a lot of effort to get Pacman running I am hoping to move on to test the emulator on some other NES game. I figure that once I have tried this approach on a few different (simple) games I'll be in a better position to know just how much this emulation approach is feasible. Again, the amazing thing here is that it works at all. We are not quite yet in the position of having an NES-on-a-chip, but this comes close.
I am attaching the code as it is. This is my first attempt at distributing code in this way. If there are any concerns about copyright etc. I will, of course, take it down. I am happy to discuss...
Post Edited (Darryl) : 1/11/2010 11:23:50 AM GMT
Comments
Edit: Darryl, your main Spin is in a repeat loop, you could use that for an audio cog [noparse]:)[/noparse]
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
http://www.propgfx.co.uk/forum/·home of the PropGFX Lite
Post Edited (Baggers) : 1/11/2010 1:59:59 PM GMT
I love being wrong about what a Prop can do, like this!
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Propeller Wiki: Share the coolness!
8x8 color 80 Column NTSC Text Object
Safety Tip: Life is as good as YOU think it is!
Post Edited (potatohead) : 1/11/2010 3:32:04 PM GMT
Would someone post the change to run this on the demoboard? (Video)
Thanks!
OBC
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
New to the Propeller?
Visit the: The Propeller Pages @ Warranty Void.
You'll have to sort out the joypad stuff yourself, I don't know what pins you've got them on.
it was three simple changes.
in NES_Pacman_emulator_010.spin
· _clkmode = xtal1 + pll16x· ' Set clock multiplier to 8x
· _xinfreq = 5_000_000 + 0_000 ' Set to 10Mhz (and add 5000 to fix crystal imperfection of hydra prototype)
and·in NES_tv_017
_pins·················· long··· %001_0101······ 'pins
Hope this helps.
Baggers.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
http://www.propgfx.co.uk/forum/·home of the PropGFX Lite
@Potatohead.. Have you got a chance to do a screenshot? I'm working today.
OBC
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
New to the Propeller?
Visit the: The Propeller Pages @ Warranty Void.
(trust me, I'm itching to run this)
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Propeller Wiki: Share the coolness!
8x8 color 80 Column NTSC Text Object
Safety Tip: Life is as good as YOU think it is!
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Composite NTSC sprite driver: Forum
NTSC & PAL driver templates: ObEx Forum
OnePinTVText driver: ObEx Forum
Maybe something from http://www.pdroms.de/files/nes/Games/?page=1 for the next one (if you are worried about legality).
Also, Bob Rost had written a few demos/games (and had class projects) in NES game dev. Could be that you could use something from there:
http://bobrost.com/nes/resources.php
I wish they would have had classes in that when I was going through university [noparse];)[/noparse]
Post Edited (trodoss) : 1/12/2010 3:13:51 AM GMT
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
http://www.propgfx.co.uk/forum/·home of the PropGFX Lite
·
I started to sweat a little when I saw that... [noparse]:)[/noparse]
OBC
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
New to the Propeller?
Visit the: The Propeller Pages @ Warranty Void.
They are all supposed to be "Public Domain," however, I can't vouch for the content (or sources for said content).
AWESOME work! Some minor screen jitter, and of course that color issue that
was mentioned, but I'm actually playing NES pacman on my propeller! Neato!
OBC
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
New to the Propeller?
Visit the: The Propeller Pages @ Warranty Void.
That must be the most awesome opening post ever.
If you want I can make the APU, it shouldn't be any problem compared to a SID.
Okey, I just read trough your entire post and I realize that fitting the APU isn't possible thanks to memory limitation [noparse]:([/noparse]
Post Edited (Ahle2) : 1/13/2010 9:46:44 AM GMT
Very cool. I'm looking forward to giving this a more detailed look tonight. (hotel room Propeller time again!!)
Running on HYBRID, which is 96Mhz. There is some screen tearing and such. Are there timing dependent elements in there? Probably 80 percent of the frames are solid, the rest vary a bit.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Propeller Wiki: Share the coolness!
8x8 color 80 Column NTSC Text Object
Safety Tip: Life is as good as YOU think it is!
Post Edited (potatohead) : 1/14/2010 10:23:50 AM GMT
That is truly awesome.
Rick
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
PrEditor·- Text Editor
NYC Area Prop Club
·
humanoido
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Propeller Wiki: Share the coolness!
8x8 color 80 Column NTSC Text Object
Safety Tip: Life is as good as YOU think it is!
Us poor saps have been struggling along for two years now to get a Z80 emulator up with CP/M and a text mode display on the Prop. And here you are with a 6502 emulation and full graphics glory.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
For me, the past is not over yet.
I'd like to load this into my Propacade.
OBC
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
New to the Propeller?
Visit the: The Propeller Pages @ Warranty Void.
Would you like to make a post, in particular about the 6502 emulation, to my emulator thread index (see link in my signature).
I think we need a Games index thread too. Anyone up to the challange of maintaining it? - The creator needs to maintain it, because every so often you should update the first post with new additions and links. This is what I do for the Emulator and Tools indexes.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Links to other interesting threads:
· Home of the MultiBladeProps: TriBlade,·RamBlade,·SixBlade, website
· Single Board Computer:·3 Propeller ICs·and a·TriBladeProp board (ZiCog Z80 Emulator)
· Prop Tools under Development or Completed (Index)
· Emulators: CPUs Z80 etc; Micros Altair etc;· Terminals·VT100 etc; (Index) ZiCog (Z80) , MoCog (6809)·
· Prop OS: SphinxOS·, PropDos , PropCmd··· Search the Propeller forums·(uses advanced Google search)
My cruising website is: ·www.bluemagic.biz·· MultiBlade Props: www.cluso.bluemagic.biz
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Quit buying all those fixed voltage regulators, and·get an Adjustable Power Supply·for your projects!· Includes an LED testing terminal!
(no longer new) SD Card Adapter·Now available!· Add extra memory to your next Propeller project with ease!
Yup.. but we haven't heard much out of the author after his inital post..
OBC
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
New to the Propeller?
Visit the: The Propeller Pages @ Warranty Void.
I'll update the PROJECTS: master list tonight.
It includes a list of games at the top.
OBC
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
New to the Propeller?
Visit the: The Propeller Pages @ Warranty Void.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Links to other interesting threads:
· Home of the MultiBladeProps: TriBlade,·RamBlade,·SixBlade, website
· Single Board Computer:·3 Propeller ICs·and a·TriBladeProp board (ZiCog Z80 Emulator)
· Prop Tools under Development or Completed (Index)
· Emulators: CPUs Z80 etc; Micros Altair etc;· Terminals·VT100 etc; (Index) ZiCog (Z80) , MoCog (6809)·
· Prop OS: SphinxOS·, PropDos , PropCmd··· Search the Propeller forums·(uses advanced Google search)
My cruising website is: ·www.bluemagic.biz·· MultiBlade Props: www.cluso.bluemagic.biz
The emulator now works fine on three different NES games - Galaga, Pacman, and Donkey Kong.
These games were all chosen because they use only 16K of the 32K available ROM space on the NES and because they use only horizontal mirroring. It should not be too big a deal to emulate a game with vertical mirroring - that is one of the next tasks.
The color artifacts present in the last version of the emulator have been resolved. The PPU driver also now does 8x16 sprites (necessary for Galaga). Also some remaining problems in the 6502 emulation were corrected. There are still some minor issues with sprites. The Hydra is not quite fast enough to display numbers of sprites on a single line so sometimes sprites drop out.
There is still no emulation of sound (not enough memory space) and currently only one of the game controllers is active.
I have packaged all three games in the attached zip file. The "top level" file is NES_DonkeyKong_emulator_010, NES_Galaga_emulator_010, and NES_Pacman_emulator_010, respectively. However, if you check these files you will see that they are basically the same spin code, but just loading different game cartridge ROM and PPU ROM data.
I have also attached a file which shows my view of the best mapping between Hydra colors and NES colors. I have found that, in order to get the best correspondence between the Hydra and NES colors I need to turn up the "saturation" control on my video-in device to about the 90% level. This makes the colors on the Hydra much more vivid and there is quite a close match between the Hydra and the NES for colors.
In the case of the Donkey Kong game, after you press the start button there is a delay of a couple of seconds before the game starts - during this period the original game plays a short tune, which of course you can't hear in these emulations, so just be a bit patient!
If you want to contact me directly email darryl.biggar@stanfordalumni.org.
Is anyone else experiencing horizontal? (screen rolling down) roll with these games?
I don't have an adjustment for that on the TV.
OBC
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
New to the Propeller?
Visit the: The Propeller Pages @ Warranty Void.
OBC, I'm having the same screen rolling also.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
http://www.propgfx.co.uk/forum/·home of the PropGFX Lite
·
I just changed out the tube in my arcade box and it seems to be a bit more tolerant of the hold.
I can't help but think that we had a similar discussion in another thread about a problem like this,
but I can't find it. (Darn forum software! )
Anyone remember?
OBC
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
New to the Propeller?
Visit the: The Propeller Pages @ Warranty Void.