Writing emulators in Lua

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Writing emulators in Lua

Rena
I'm interested in writing some emulators in Lua. Specifically I want
to write a Game Boy emulator and maybe later some other game consoles
as well. However I've heard the topic discussed before, and people
mentioned that Lua isn't really ideal for emulation (impressively fast
as it is) due to being bytecode and weak-typed, and LuaJIT even isn't
very good at optimizing emulator cores since it can't tell what
they're going to do next. Would I be insane to write an entire
emulator using Lua? Any tips to make it nice and snappy?

PS, I realize Game Boy only runs at 4-8mhz and just about anything
will probably be "good enough" on modern hardware. I want it to be as
fast as possible because A) I might try to emulate more powerful
systems later, and B) I'd like to be able to run the games at insane
speeds. The main reason for that being movie playback: by recording
the initial state and all inputs, and playing them back again, you can
create a "movie" file for a game. Of course the only way to jump to a
specific spot in such a recording is to play through it up to that
point...

--
Sent from my toaster.

Reply | Threaded
Open this post in threaded view
|

Re: Writing emulators in Lua

David Heiko Kolf
HyperHacker wrote:
> Would I be insane to write an entire emulator using Lua?

Somebody already wrote a PC emulator in JavaScript. ;-)
<http://bellard.org/jslinux/>

I don't know the instruction set of a GameBoy, but I guess binary
operations could be quite slow in Lua (conversion double->int->double)
but frequent in the games. Otherwise I guess it should work, though I
did not write an emulator yet.

Reply | Threaded
Open this post in threaded view
|

Re: Writing emulators in Lua

Reuben Thomas-5
On 8 January 2012 21:09, David Kolf <[hidden email]> wrote:
> HyperHacker wrote:
>>
>> Would I be insane to write an entire emulator using Lua?
>
>
> Somebody already wrote a PC emulator in JavaScript. ;-)
> <http://bellard.org/jslinux/>

And with something like Javascript's typed arrays extension for Lua
you could do it in Lua.

--
http://rrt.sc3d.org

Reply | Threaded
Open this post in threaded view
|

Re: Writing emulators in Lua

David Given
On 08/01/12 21:10, Reuben Thomas wrote:
> On 8 January 2012 21:09, David Kolf <[hidden email]> wrote:
[...]
>> Somebody already wrote a PC emulator in JavaScript. ;-)
>> <http://bellard.org/jslinux/>
>
> And with something like Javascript's typed arrays extension for Lua
> you could do it in Lua.

I should point out that most of the magic in Fabrice Bellard's emulator
is that it's *not* emulating x86 code; it's dynamically recompiling it
into Javascript, and then running the Javascript directly. (It builds on
a lot of the same logic as qemu.)

The same trick would work just as well in Lua, and LuaJIT's ffi even
gives the fast byte arrays to make it all efficient.

The massive amounts of work to actually make it all happen is left as an
exercise for the reader...

--
┌─── dg@cowlark.com ───── http://www.cowlark.com ─────

│ "Never attribute to malice what can be adequately explained by
│ stupidity." --- Nick Diamos (Hanlon's Razor)


signature.asc (262 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Writing emulators in Lua

Rena
On Sun, Jan 8, 2012 at 16:31, David Given <[hidden email]> wrote:

> On 08/01/12 21:10, Reuben Thomas wrote:
>> On 8 January 2012 21:09, David Kolf <[hidden email]> wrote:
> [...]
>>> Somebody already wrote a PC emulator in JavaScript. ;-)
>>> <http://bellard.org/jslinux/>
>>
>> And with something like Javascript's typed arrays extension for Lua
>> you could do it in Lua.
>
> I should point out that most of the magic in Fabrice Bellard's emulator
> is that it's *not* emulating x86 code; it's dynamically recompiling it
> into Javascript, and then running the Javascript directly. (It builds on
> a lot of the same logic as qemu.)
>
> The same trick would work just as well in Lua, and LuaJIT's ffi even
> gives the fast byte arrays to make it all efficient.
>
> The massive amounts of work to actually make it all happen is left as an
> exercise for the reader...
>
> --
> ┌─── dg@cowlark.com ───── http://www.cowlark.com ─────
> │
> │ "Never attribute to malice what can be adequately explained by
> │ stupidity." --- Nick Diamos (Hanlon's Razor)
>

mm, I've thought about recompiling into Lua code, but that's something
to experiment later once I get basic interpretation working. I'm
mostly just curious how slow it'll be interpreting in Lua. Of course
memory will be stored in a fast byte array. (I wrote such a module for
plain Lua a while back.)

--
Sent from my toaster.

Reply | Threaded
Open this post in threaded view
|

Re: Writing emulators in Lua

Dimiter 'malkia' Stanev
In reply to this post by Rena
I've found this today, while browsing http://news.ycombinator.com

It's a GameBoy Color emulator in JS (haven't tried it yet)

http://www.grantgalitz.org/gamecenter/index.html
http://news.ycombinator.com/item?id=3446871

http://www.hnsearch.com/search#request/all&q=Gameboy+emulator&start=0



On 1/7/2012 10:54 PM, HyperHacker wrote:

> I'm interested in writing some emulators in Lua. Specifically I want
> to write a Game Boy emulator and maybe later some other game consoles
> as well. However I've heard the topic discussed before, and people
> mentioned that Lua isn't really ideal for emulation (impressively fast
> as it is) due to being bytecode and weak-typed, and LuaJIT even isn't
> very good at optimizing emulator cores since it can't tell what
> they're going to do next. Would I be insane to write an entire
> emulator using Lua? Any tips to make it nice and snappy?
>
> PS, I realize Game Boy only runs at 4-8mhz and just about anything
> will probably be "good enough" on modern hardware. I want it to be as
> fast as possible because A) I might try to emulate more powerful
> systems later, and B) I'd like to be able to run the games at insane
> speeds. The main reason for that being movie playback: by recording
> the initial state and all inputs, and playing them back again, you can
> create a "movie" file for a game. Of course the only way to jump to a
> specific spot in such a recording is to play through it up to that
> point...
>