emulators are cool

I started working hacking together a Game Boy emulator the other day and wanted to see how quickly I can get from absolute zero to something. I have achieved what I set out to do:

classic

I took the absolute minimalist route and wanted something that wasn’t abstracted away like most (good) emulators are these days. Since my goal was understanding, abstraction wasn’t a high priority.

First, I wrote the parser for the ROMs to ensure I handled all of the instructions correctly in the bootrom. When that was looking good, I had to do a bit of refactoring now to actually execute those instructions and modify my state. After about a week or so of digging in, I had the basics working. For now, I’ll be putting this project on hold because other things have captured my attention. The code is available at https://github.com/AugustKarlstedt/gameboyemu but I warn: it isn’t pretty. 🙂

I chose to use CMake for dependency management and I must say, THIS is the way to use C++ these days. Wonderful all around.

A higher-level discussion

Why do we write an emulator that gets compiled into machine code for a specific CPU? E.g. in my case I wrote my emulator in C++ and compiled it into x86 instructions using MVCC. What’s the point? Our CPUs already have instructions to do what these older CPUs did.

Why don’t we use LLVM to simply translate from e.g. Game Boy instruction set to e.g. x86 instruction set?

Luckily a smart dude already has worked on this: https://andrewkelley.me/post/jamulator.html

Going Through Old Source Code

After backing up some files, I remembered that I wanted to go through some of the old programs and code that I’ve written. It would be a fun experiment: seeing how far I’ve come, what I actually spent my time doing as a teenager, and probably get a few laughs. Well, here are some of the interesting bits I found.

The first thing I came across was a C++ program I made for my friend Jose’s 17th birthday using SFML and Bass. It consisted of a single main.cpp file and most likely completed the night before.

Jose Birthday project

I wasn’t very clever in naming projects. Something I called “Google” was actually a really cool start of a physics puzzle game. Worked on sometime in May through July 2011, it has a complete level format based on XML. The player drags a path for a ball to enter the designated “win” zone. It used Lua scripting, Box2d Physics, and SFML for rendering. Again C++.

“Google” game project

(30 minutes later…) wow, ok, I found something that I really enjoyed! It’s called ParticleSim, written in June of 2011. I’m beginning to think that the dates on the files might be incorrect, or I was just quite productive in 2011? Not sure yet. Anyhow, at the time I was trying to replicate another Facepunch forum member’s work that I found really fun. My take on it was a game where the player can place attractors and repellers and then create particles that will interact with them. I started with the math to get that all working, then added a quick feature to make the particles’ size based on the bass of the current song. I got to play around with FMOD, Lua, and Boost all in C++.

ParticleSim demo

That’s all for now! More to come later.

GPH 321 – Completed

In this class we created a ray tracer from the ground up. I by “we” I mean our professor wrote most of the code and we had to fill in certain class functions. It was pretty terrible, but overall the end result was good. Oh and we had to zip our source code and send it in, instead of simply using something like GitHub or Bitbucket. The midterm and final SUCKED, simply because it’s 2015 (well, it was 2014) and written exams for programming finals are absurd. Writing code on paper completely lacks the feel of typing it out using an IDE. Plus, come on, who doesn’t use IntelliSense, Resharper, Visual Assist X, or some kind of code-completion tool? A fool! And that’s exactly what we are when we write code by hand.

Anyhow, here are some screenshots from the course.

First we simply had to get rays working and detecting collisions.

Rays & Intersections
Rays & Intersections

Then we had to ray-trace our first object, a sphere. This is because it’s pretty simple to represent in math.

First Ray-traced Object
First Ray-traced Object

Then we had to add some shading to that sphere. I know,Γö¼├íit isn’t 100% correct. Spot the error!

Phong shading
Phong Shading

Next came shadows. These are awesome.

Ray-traced Shadows
Ray-traced Shadows

Next was reflections. Super easy thanks to how ray-tracing works.

Ray-traced Reflections
Ray-traced Reflections

Then we had to create a checkerboard pattern.

Checkerboard
Checkboard

Finally we were to implement Fresnel equations to properly simulate glass. I didn’t finish this one fully.

Ray-traced Fresnel
Ray-traced Fresnel

And that was the end of the course. This course was online only, which probably had an impact on my view of it. The course notes weren’t that great. I repeatedly found myself Googling things that were phrased in strange ways to figure out how to implement them. And variable names like V, N, n1, n2, etc. used throughout the given code made things unnecessarily complex.

Oh well, it’s over and done with!

Updates!

Obviously I haven’t posted anything in a while. But it’s time for a change – and change is good!

I am now currently in three courses that are at least semi-relevant to this blog.

  • ANI 230 – 3D Design & Modeling
  • CSC 298 – Internship
  • GPH 321 – Computer Graphics Development I

In ANI 230, we’re learning Maya 2015. Starting with low-poly and now (it’s about midterms time) we’re onto animation with keyframes. I’m particularly interested in ray tracing, because we are building our very own in C++ in GPH 321.

As for CSC 298, it basically runs alongside the internship that I already have. Here, I’ve been working with a ton of Java stuff lately interfacing with a USB HID. Through this, I’ve learned to use Maven – let me just say it’s awesome.

Anyhow, I’ll be posting some images of the stuff I’ve been working on soon.

Also, I may move this entire blog to a domain/host I pay for now. Or maybe even use GitHub Pages and Jekyll, those look fun.

Oh and I read a few Android books, great stuff. Loving Android dev.

Finished with GAM 374

Well, I chose to turn in exactly what you see in that last blog post. Everything that was required for the assignment was there and it earned me a 95%.

I am now focusing my efforts into work with a near-full-time schedule of 38 hours a week. I will be posting stuff about happenings relating to that from here on out.

I’m also working on an Android app, so there will be some Java tossed in.

And last but not least, my love for C++ is growing as I discover C++11 features. There’s a little code I wrote to test out the functionality of some of the new stuff if you read on.

Continue reading →