LuaGB: A Gameboy Emulator written in Pure Lua

Show off your games, demos and other (playable) creations.
zeta0134
Prole
Posts: 1
Joined: Tue Apr 11, 2017 1:40 am

LuaGB: A Gameboy Emulator written in Pure Lua

Post by zeta0134 »

So, one day I got really bored in modded Minecraft, and thought it would be neat to try to write a Gameboy emulator in ComputerCraft. That idea fell apart shortly after I started trying to get graphics code working and realized it's not possible to draw a full resolution gameboy in the mod, so I quickly ported what I had to Love2D and kept going.

This is the result, in its first alpha release. It's capable of playing most Gameboy games, includes full Color support (no Super Gameboy though) and, for being written in Lua, is surprisingly performant. I plan to continue improving it over time, but I decided to polish it up and get it out in the wild in some form for greater feed back and critique.

https://github.com/zeta0134/LuaGB

Games are loaded from Love2D's save location, a folder icon in the main interface acts as a shortcut to there for easy file copies. Sound is supported but lags behind a bit due to limitations in Love2D's audio implementation. I understand a planned QueueableAudio type is coming down the line which should make this issue vanish. Saves and Save States are supported, and there's a rather extensive debug feature, press D to activate that if you're feeling adventurous, it'll print out the rest of the keymaps onscreen.

I'm pretty happy with this. Critique, and especially bug reports welcome!

-Zeta
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: LuaGB: A Gameboy Emulator written in Pure Lua

Post by raidho36 »

Performance is good because LÖVE uses LuaJIT - it compiles some (or all) Lua code into native assembly. Not as efficient as C++ but good nonetheless. There are number of general perofrmance guidelines:
  • save reference table to a local variable to access it directly, as opposed to access over a chain of tables
  • in general, use locals over globals - every global goes to special table, and there's table lookup overhead
  • for tables with known and rigid structure, define a FFI C struct
  • instead of discarding a table that needs all its data modified, modify it in place
  • avoid producing new strings such as by concatenation, this is a slow process and each existing unique string makes it slower
  • don't use language functions that are not compiled by LuaJIT, most notably pairs/ipairs
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: LuaGB: A Gameboy Emulator written in Pure Lua

Post by Nixola »

Just nitpicking, but ipairs is compiled. Here's a list of what's compiled and what isn't.
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
User avatar
zorg
Party member
Posts: 3436
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: LuaGB: A Gameboy Emulator written in Pure Lua

Post by zorg »

Nixola wrote: Tue Apr 11, 2017 6:44 am Just nitpicking, but ipairs is compiled. Here's a list of what's compiled and what isn't.
I could have sworn it wasn't compiled before... oh well, this is a positive thing :P
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
Ulydev
Party member
Posts: 445
Joined: Mon Nov 10, 2014 10:46 pm
Location: Paris
Contact:

Re: LuaGB: A Gameboy Emulator written in Pure Lua

Post by Ulydev »

Impressive!
User avatar
Davidobot
Party member
Posts: 1226
Joined: Sat Mar 31, 2012 5:18 am
Location: Oxford, UK
Contact:

Re: LuaGB: A Gameboy Emulator written in Pure Lua

Post by Davidobot »

Awesome work! I saw a post about this on /r/Lua and was wondering when and if you'll post it here! Amazing work! It runs pretty well too!
Don't mind me, but I'll just dissect your code to find its inner-workings.
How did you figure out the emulation process? Are there online docs about it, or did you go through the code of something like GBAboy?
PM me on here or elsewhere if you'd like to discuss porting your game to Nintendo Switch via mazette!
personal page and a raycaster
User avatar
zorg
Party member
Posts: 3436
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: LuaGB: A Gameboy Emulator written in Pure Lua

Post by zorg »

zeta0134 wrote: Tue Apr 11, 2017 3:36 am...
If you don't want to wait until 0.11 gets released, there are already nightlies that are stable with regards to the new audio features (at least i've been making use of them, and the base stuff you'd probably use too are probably not gonna change in the public interface department :))

(Win32 builds here, linux packages exist as well, but i don't use them so don't have a link of those at hand right now)

Also, i implemented a module player that uses queuable sources, maybe you might find the implementation useful. :3
raidho36 wrote: Tue Apr 11, 2017 6:09 am
  • for tables with known and rigid structure, define a FFI C struct
Don't use FFI if you want to release this on handhelds too, since JIT compilation is disabled on android because of bugs, and IOS doesn't allow it... besides, performance won't be that much faster anyway, in general.
Last edited by zorg on Wed Apr 12, 2017 6:48 am, edited 3 times in total.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
Sulunia
Party member
Posts: 203
Joined: Tue Mar 22, 2016 1:10 pm
Location: SRS, Brazil

Re: LuaGB: A Gameboy Emulator written in Pure Lua

Post by Sulunia »

Whoa, this is pretty rad. I remember being halfway through a gameboy CPU interpreter on lua a while back, but I lost it when they formatted my machine without permission. Yea. That happens 'round here.
Serves as lesson to put everything on some code versioning system..
Don't check my github! It contains thousands of lines of spaghetti code in many different languages cool software! :neko:
https://github.com/Sulunia
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: LuaGB: A Gameboy Emulator written in Pure Lua

Post by raidho36 »

zorg wrote: Tue Apr 11, 2017 1:06 pm Don't use FFI if you want to release this on handhelds too, it's disabled on android because of bugs, and IOS doesn't allow it... besides, performance won't be that much faster anyway, in general.
It's not disabled on either Android or iOS, nor on any target platform that LuaJIT supports. The Just In Time compilation is disabled by default on Android and iOS, on Android because its memory allocator conflicts with LuaJIT's memory allocation policy, resulting in slowdown by factor of 5 or thereabouts compared to interpreted Lua, and on iOS due to the App Store policies. On Android however that's a very old problem and both its kernel and LuaJIT have been updating since, so you can try your luck and enable JIT in runtime. You can do the same in iOS but that would be App Store policy violation. No one has to know you do that, though. ;)

As for performance improvement, depending on the calculations you're doing it's anywhere between factor of 2 and 50. But only when JIT is enabled. Otherwise it's a significant slowdown.
uederson
Prole
Posts: 30
Joined: Mon May 23, 2016 7:59 pm
Location: Brazil
Contact:

Re: LuaGB: A Gameboy Emulator written in Pure Lua

Post by uederson »

Great!
I'm here crashing my brain trying to create a little and simple game while someone is creating something so complex like this!
Post Reply

Who is online

Users browsing this forum: No registered users and 40 guests