Work in Progress

Gameboy Emulator

13

McPepic 2023-04-25 20:07 (Edited)

I’ve been keeping this a secret for a while.
Now that I have a bit of progress done, I think it’s about time I show it off.

Switch to debug mode to see the register values and clock cycles. I still have a lot more instructions to emulate and debugging to do.

Let me know what you think!

Update: 1st table done except for stop and halt. Token count is really high, have to group together instructions to reuse code
Update: Reorganized table to take up less tokens, started re-commenting the instructions to make them easier to find, also need to refactor and test
Update: Shortened the code a bit, added comments & trace statements to track the value changes and jump instructions. Turned it into a “stepper” program - just tap the screen to run the current instruction; instructions are cycled through
Update: Condensed more logic. Second & third quarter of tables correctly commented and traced with values. Last quarter WIP. Also fixed wrap-around for relative jump instructions
Update: First table cleaned up, added comments, and traced the values to the debug view. Let me know if you see any problems
Update: Both op code tables done! All working (hopefully), fully commented, and traced to the debug screen! There are very likely to be bugs, so I’m going to need to use some test ROMs at some point for debugging. Super exciting, though!
Update: Rewrote decode & execution logic, can now load ROMs, added bank switching. Register values are properly initialized. If anyone has any ideas about how to add halt and stop instructions, let me know
Update: Emulator properly formats and writes to LowRes character memory! For now, the emulator displays character memory on screen. Pictured is character memory modified by a test ROM!
Update: Been a while. Hooked up BG memory, sprite (OAM), and LCDC register. Reformatted debug screen, added timing and working on interrupts.

Gameboy.nx | Open in app
2023-05-25 17:59
Gameboy.nx | Open in app
2023-05-16 15:42
Gameboy.nx | Open in app
2023-05-13 19:17
Gameboy.nx | Open in app
2023-05-06 16:38
Gameboy.nx | Open in app
2023-05-05 15:55
Gameboy.nx | Open in app
2023-05-03 17:19
Gameboy.nx | Open in app
2023-05-02 19:09
Gameboy.nx | Open in app
2023-05-01 17:54
Gameboy.nx | Open in app
2023-04-26 19:17
Gameboy.nx | Open in app
2023-04-25 20:07

Timo 2023-04-25 20:23

I see CPU emulation in the code... Is that the actual Game Boy CPU?
Maybe for graphics it could be possible to "just" map the Game Boy PPU to the LowRes NX graphics. Emulating it completely will be hard :O


McPepic 2023-04-25 20:39

For now, all it does is store the memory in two separate arrays (one array isn't big enough to map all of memory) as well as the hardware registers.

The program then repeatedly reads the next byte in the simulated memory, compares the value against a series of conditions and runs the steps responsible for the given instruction. I'm also keeping track of the clock cycles when I implement timing.

As for the graphics, the screen isn't big enough to fit everything, so I'll probably just cut off the bottom.

The idea is the program will be able to take a Gameboy ROM and produce its own output using the functionality of LowRes (default sprites, tiles, sound channels, etc.)


was8bit 2023-04-26 05:52

Wow!!!


was8bit 2023-04-26 11:15

Hey, if you wanna ROM to test, maybe try TAMAGOTCHI ... the graphic output seems to be just black, white, and 2 shades of gray... and the animation is rather limited... and its light on the text ...


Timo 2023-04-26 14:23

Was8bit, yes, these are the 4 colors of the Game Boy ;)
I think Tetris might be the easiest game to emulate, it doesn‘t rely on special hardware tricks.

McPepic, I guess with a lot of work it could actually be possible to get a simple GB emulator in NX, but for sure it will be veeeery slow. NX itself already has a speed limitation similar to the GB, and with emulation it can only get worse…


was8bit 2023-04-26 14:46

AH, yes, I was getting it confused with gameboy color...

Tetris does sound like an easier option... one "room" and only a small number of graphics for pieces...

Tamagotchi has a small number of static rooms and some text... I thought of it as its animations are limited to 2 or 4 slow frames... maybe it could be used as a second test ;)

A Mario styled game or anything else with scrolling or fast animation would not be advised...


McPepic 2023-04-26 15:22

Random thing I was considering: the Gameboy could only display 40 sprites, but only 10 could be drawn per row. I imagine it will take more processing to limit how many sprites are visible per raster, so would it be better if I just ignore it or simulate the 10 sprite/line limit manually?


Timo 2023-04-26 15:56

Step by step :)
I would try to map all GB PPU access to the direct NX counterparts and start with a very simple game (like Tetris). And then fix what's broken. If nothing is broken, don't fix anything ;)
Don't expect to get full GB compatibility. Be happy about all little things which work :)


G-9 2023-05-07 19:19

damn, opening lowres nx to this :o


McPepic 2023-05-10 19:03

I just realized that all of the timing (clock cycles per raster, VBL, and frame) are exactly the same in LowRes as they are on the Game Boy.


Timo 2023-05-11 06:09

Pure coincidence ;)


was8bit 2023-05-16 16:58

So, can we get a copy of those characters for lowres??


McPepic 2023-05-16 18:09

@was8bit :)

GB Font.nx | Open in app
2023-05-16 18:09

was8bit 2023-05-16 18:26

Yay!!! Thanks so much!! :D


McPepic 2023-05-16 18:29

@was8bit I wasn't sure when the character memory would be filled, so I used the save command every time the emulator wrote to character memory. Not the fastest method, but it worked. Also the first time I've used that command.


was8bit 2023-05-17 02:23

Very cool to have a piece ;)


was8bit 2023-05-17 05:08

Here is my rendition...

Advanced Font Set.nx | Open in app
2023-05-17 05:08

Timo 2023-05-25 20:10

How can I try this??


Timo 2023-05-25 20:12

Ah, just saw the ROM string in the code…


McPepic 2023-05-25 21:25

@Timo
Yeah, I can't post the program with the ROM in it.
I've been testing using a Tetris ROM.


Timo 2023-05-27 09:31

McPepic, what is the expected result with a Tetris ROM? Is something visible already or "just" the CPU working?


McPepic 2023-05-27 15:27

@Timo
Character, background, and sprite data is already hooked up. If it wants to draw something, it should be able to. I haven’t seen it draw anything yet though…

I figure it has something to do with triggering interrupts, which is what I’ve been working on.


Log in to reply.