85
edits
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
Back in 2014 I was messing around with the idea of converting Gameboy .GBS soundtracks into Amiga .MOD files for a game I was doing. If you don't know what a .GBS file is, it's basically just a Gameboy ROM with z80 code but with the graphics routines and gameplay stripped out, so it's just the sound driver and music data, so in order to play it back you essentially have to emulate the full Gameboy processor and sound chip. At the time I was sort of infatuated with [https://www.lazarus-ide.org/ FreePascal and Lazarus], and wanted to find a Gameboy emulator written in Pascal that I could maybe yank the CPU and sound code from. | Back in 2014 I was messing around with the idea of converting Gameboy .GBS soundtracks into Amiga .MOD files for a game I was doing. If you don't know what a .GBS file is, it's basically just a Gameboy ROM with z80 code but with the graphics routines and gameplay stripped out, so it's just the sound driver and music data, so in order to play it back you essentially have to emulate the full Gameboy processor and sound chip. At the time I was sort of infatuated with [https://www.lazarus-ide.org/ FreePascal and Lazarus], and wanted to find a Gameboy emulator written in Pascal that I could maybe yank the CPU and sound code from. | ||
Somehow after scavenging over a bunch of old forum posts, I found (apparently) the only Gameboy emulator written in Pascal, ever: '''UGE'''. It was written by a guy named Christian Hackbart and released in 2000, and the only available download was from a mirror on [https://www.zophar.net/gb/uge.html Zophar's Domain]. I started it up, loaded a ROM, was met with some ear-piercing noise, and then the emulator crashed. I canned the project pretty shortly after and that was pretty much that. | Somehow after scavenging over a bunch of old forum posts, I found (apparently) the only Gameboy emulator written in Pascal, ever: '''UGE'''. It was written by a guy named Christian Hackbart with Delphi and released in 2000, and the only available download was from a mirror on [https://www.zophar.net/gb/uge.html Zophar's Domain]. I started it up, loaded a ROM, was met with some ear-piercing noise, and then the emulator crashed. I canned the project pretty shortly after and that was pretty much that. | ||
<youtube>xNVBQLn0bWs</youtube> | <youtube>xNVBQLn0bWs</youtube> | ||
Line 35: | Line 35: | ||
frameStart := frameEnd; | frameStart := frameEnd; | ||
</pre> | </pre> | ||
<youtube>GWEYq6YS9rk</youtube> | |||
Bam! Already it's almost playable, and the sound isn't causing crashes. I think it was crashing before because the extreme speed of the emulation was causing a sound buffer overrun or something. | Bam! Already it's almost playable, and the sound isn't causing crashes. I think it was crashing before because the extreme speed of the emulation was causing a sound buffer overrun or something. | ||
Line 41: | Line 43: | ||
The emulator actually does render the screen right-side-up when using DirectDraw, but I can't record that with OBS for some reason. It renders flipped graphics when drawing with GDI-- let's fix that. In <code>dib_out.pas</code> I changed <code>biheight := 144;</code> to <code>biheight := -144;</code> and.... | The emulator actually does render the screen right-side-up when using DirectDraw, but I can't record that with OBS for some reason. It renders flipped graphics when drawing with GDI-- let's fix that. In <code>dib_out.pas</code> I changed <code>biheight := 144;</code> to <code>biheight := -144;</code> and.... | ||
<youtube>lBD4Sgarpr4</youtube> | |||
Nice. We've eliminated the graphics problem, speed problem, and crashing problem with like 10 lines of code and a 1 character change. All that's really left now is the sound... | Nice. We've eliminated the graphics problem, speed problem, and crashing problem with like 10 lines of code and a 1 character change. All that's really left now is the sound... | ||
Line 78: | Line 82: | ||
That's right, the volume value for each channel is being stored as an unsigned byte, so it will ''never'' go below zero! I changed <code>vol</code> to be of type <code>ShortInt</code> and.... | That's right, the volume value for each channel is being stored as an unsigned byte, so it will ''never'' go below zero! I changed <code>vol</code> to be of type <code>ShortInt</code> and.... | ||
<youtube>lnuSWcR_02s</youtube> | |||
Wow, still bad, but a lot better! Another huge gain from an 8 character change! What else can we do? | Wow, still bad, but a lot better! Another huge gain from an 8 character change! What else can we do? | ||
Line 90: | Line 96: | ||
I don't know why, so let's try just commenting it out. | I don't know why, so let's try just commenting it out. | ||
<youtube>7VbfpDI3JL4</youtube> | |||
WTF? Another huge improvement! We're at a big net negative on code written here, yet it just keeps getting better. What else can we do? | WTF? Another huge improvement! We're at a big net negative on code written here, yet it just keeps getting better. What else can we do? | ||
Line 104: | Line 112: | ||
Do you see it? It's accessing <code>snd[3].Bit</code> in the first branch of the <code>if</code>, but <code>snd[2]</code> in the second one. Let's just change it to 2, and... | Do you see it? It's accessing <code>snd[3].Bit</code> in the first branch of the <code>if</code>, but <code>snd[2]</code> in the second one. Let's just change it to 2, and... | ||
<youtube>J9Tuekee7_8</youtube> | |||
Holy shit, it's perfect! And all I did was change like 20 characters! It really strikes me that all Christian Hackbart had to do back in 2000 was do 5 lines of fixes and he would have had a near perfect Gameboy emulator on his hands. Also let me just say that it's super impressive that not only does near 20-year-old code work unmodified on Windows 10, but also that it's compiled by a totally different compiler and totally rewritten GUI toolkit, almost with zero changes. My hat is off to the FreePascal folks. | |||
I' | Now that I'm done heroically fixing this emulator with my insane hacking skills, I'm off to work on the actual tracker now. |