Yuji Yokoo

Developing your Dreamcast apps and games with mruby

What would you make, if you can run your Ruby code on Dreamcast?

Well, now you can! I have been working on my development setup for running mruby code on Dreamcast. I would like to show you what I have developed so far and how you can get started. I would also like to tell you why development on Dreamcast is a great idea and share a few things I learned along the way.

RubyKaigi Takeout 2020

00:00:01.120 Hi, thank you for watching. I'm Yuji, and I'd like to tell you a little bit about Dreamcast and mruby. This is developing your Dreamcast apps and games with mruby, so let's get started.
00:00:06.879 First of all, this is a table of contents. I'll tell you a little bit about myself. Then I'll tell you about the application I'm running right now on my Dreamcast. I'll introduce you to Dreamcast, mruby, and Callistios. After that, I'll show you some of the development steps involved in creating your Dreamcast games or applications. I'll also share a bit of code for this presentation application and explain how to run and test your game or application, as well as how to get it onto your Dreamcast unit. I would like to close with a little demo at the end.
00:00:29.599 So, this is me. My name is Yuji Yokoo. I work as a web application developer in Adelaide, Australia, and I’m originally from Tokyo, Japan. I love Ruby.
00:00:38.799 Now, let me present the application I'm currently demonstrating. Hopefully, you can see the screen output. I've developed this application and called it Dream Present. It is a presentation app that runs on Dreamcast and has been written in mruby. This whole demonstration is live, so it might crash at any point. Fortunately, this is a recorded presentation, so if it crashes, I can simply restart it, and you wouldn't know, even if I had to restart it multiple times.
00:01:02.400 But seriously, it doesn't crash that often, just sometimes. Now, let's talk about what Dreamcast is. This right here is a photo of my Dreamcast unit, and I also have one running right next to me. Dreamcast is a video game console from Sega, and I’d like to call it the best video game console. It was released in November 1998, running a Hitachi SH-4 processor at 200 megahertz, and it has 16 megabytes of RAM.
00:01:21.600 For a modern desktop PC, you might have around 16 gigabytes of RAM if you're doing serious development. In comparison, Dreamcast has about 0.1% of the RAM, making it an extremely limited environment by today's standards.
00:01:28.680 Now, let me share an important question: Is Ruby dead? This question has been discussed many times, and I have nothing new to add. Instead, the real question is: Is Dreamcast dead? My simple answer is no. I will explain why this is the case. You might think that Dreamcast is a dead platform because Sega no longer produces Dreamcast units, and all existing units are around 20 years old. Additionally, peripherals are hard to find and becoming quite expensive. You have to look for them in second-hand markets, and they are getting rarer.
00:01:55.200 There is no more official software distribution. Just like Sega doesn't produce any more Dreamcast consoles, they do not distribute software. Because of these reasons, you might think it's a dead platform. However, I would argue it is still well and truly alive due to a strong indie homebrew culture surrounding it.
00:02:21.600 Many individuals are creating games and applications for Dreamcast today, and there are still indie game development studios making brand new Dreamcast games. There are also several good open-source tools available for Dreamcast, which can help you get started with developing your own Dreamcast software by gathering freely available resources from the internet. Alongside this, there's a wealth of information available that is open to anyone.
00:02:55.920 Moreover, Dreamcast does not require any hardware or firmware modifications to run your custom code. As long as you can write a specifically formatted CD onto a CDR, Dreamcast can boot that code. This does depend on the hardware revision, so if you're going to buy a Dreamcast for this purpose, I recommend looking up how to identify the models that can do this. Most Dreamcast units can run custom code, but some late revision consoles cannot.
00:03:10.480 Now, let's talk about mruby. Since this is RubyKaigi, many people may already be familiar with this section, but I want to touch briefly on this topic. Mruby is a lightweight ruby implementation, which you might know from Ruby MRI. It mostly implements Ruby 1.9 and the ISO standard but is not strictly limited to those. It's excellent for embedding and integrates well with C.
00:03:36.280 Let’s move on to Callistios, also known as Chaos. It is a BSD-licensed development system for Dreamcast. It bundles a whole bunch of tools like cross compilers, linkers, libraries, and everything else you need to get started with developing your own Dreamcast software.
00:04:05.200 I’d like to show you the steps involved in creating Dreamcast software, from producing the binary to creating a CD image. When you write your application or game code in C, you cross-compile it. Cross-compilation refers to the process of compiling for another platform. Your machine probably has an Intel processor, and your compiler typically produces a binary for the same platform. However, you want to produce something that can run on Dreamcast, so you need a cross-compiler that can create an SH4 binary, which comes with Chaos.
00:04:41.519 After the cross-compilation, you get the SH4 object file. Then, you link against the Chaos library to get the executable file. If you can get this to the Dreamcast console, which typically involves producing a bootable disk image. You can write this image onto a CDR, which is portable in your console. This is what happens when you use mruby alongside C.
00:05:02.720 You use mrbc to compile mruby, producing bytecode that is compatible with C. You then combine the bytecode with your C code. This involves the same cross-compilation process, resulting in an SH4 object file again. You then link against both the Chaos library and the mruby library to create an executable file. Finally, you perform the same process to produce the bootable CD image. I have created a Docker image that bundles all the required tools; if you're interested in this process, I highly recommend trying the Docker image—it’s quite handy.
00:05:34.560 Next, I'd like to show you a bit of code for this presentation application. Here's the entry point of the program. You don’t have to read this, but just know that it initializes and kicks off the mruby code. This is the compiled mruby bytecode. I cannot read it, and even if some can, let's move on. Here’s a C function that is callable from mruby; again, you don't have to read this, but the important aspect is that this function accepts an mruby parameter, unwraps it, processes it, and wraps it again in an mruby value to return.
00:06:04.880 This is the C code that attaches the function defined on the previous slide as a method to an mruby module. Finally, this code calls that function. Most of this presentation application is written in Ruby, and now that you know how to create a binary that can run on Dreamcast, you might be wondering how to ensure it runs correctly.
00:06:36.240 You ultimately want to run this on the Dreamcast console, but emulators are extremely handy for testing. Everything compiles on your machine when using an emulator, and you don't need to use your Dreamcast unit while checking functionality. Emulators typically provide console output, which is great for debugging via printf methods, though that is not available when booting directly from a CD.
00:07:04.520 If you use a network loader instead, you will have access to console output. With an emulator, this feature is provided by default. Here’s a screenshot of me running an old version of this presentation through an emulator; you can see a whole bunch of information printed to the console, which is extremely handy. However, it's important to note that emulators don’t operate the same way as actual consoles, so it's crucial to test on the hardware as well.
00:07:30.240 Now, you might be wondering how to get that executable onto your Dreamcast unit. There are several ways to do this, and I will list a few methods I am familiar with. You can use CDRs, an SD card reader for Dreamcast, a network adapter called broadband adapters, and a serial cable called coders cables.
00:07:53.680 All require booting from CDRs. When writing CDRs, you need to boot from that disk. If you use SD, broadband adapters, or coders cables, you still need to boot the loader programs from the CDR. Each method comes with its own challenges; for instance, serial cables can be quite expensive, and each change requires a new disk. While you can reuse an SD card, you still need to unplug it, write new contents, and reinsert it, which can be time-consuming.
00:08:25.600 Broadband adapters are great but generally expensive, and as far as I understand, you can no longer purchase serial cables from suppliers. In fact, I built one myself last year. I obtained an SD card connector that plugs into the serial port, modified it by doing some soldering, and while it worked fine, I didn't find the soldering job to be particularly neat.
00:09:07.680 Here’s what the finished cable looked like in the end—it didn't work well initially, so I spent days debugging until I finally managed to fix it. Ultimately, I felt uncomfortable with the modifications and opted to buy a broadband adapter instead.
00:09:43.360 Now, that’s it! You have all the knowledge required to create your own Dreamcast games and applications and successfully run them on your unit. Although there are some details I've skipped, you have the overview and can fill in the gaps. Of course, many of you might not have a Dreamcast unit yet, but that’s something you can obtain.
00:10:02.360 Here’s a list of future directions I hope to pursue. Admittedly, I haven't been very productive this year—after all, it is 2020. My major goal is to enhance usability for others so that they can use this information to create something exciting.
00:10:55.440 I'd like to express my gratitude to a multitude of people, particularly to RubyKaigi for giving me a chance to share this with everyone, especially after the original event was canceled. Thank you very much!
00:11:21.520 Now, I want to share some links: Dream Present, the application itself, the example game I will demonstrate, the development Docker image, and the coders cable construction information for those interested.
00:11:52.960 Please feel free to reach out if you have questions, either via Twitter or any other means you know how to contact me.
00:12:07.200 So let’s see if my blog puzzle game works! I am replacing the disk with the game disk.
00:12:21.440 This is the Dreamcast boot screen with a blue swirl in the Power region. Each region has a different color, and this Sega logo is always displayed, with no way to skip it; many people include disclaimers that it is not licensed by Sega.
00:12:40.960 Here’s my blog puzzle game developed using Ruby along with a bit of C. I am running it now, and that’s the presentation—all my points have been made! Thank you very much for your attention and for watching.