Game Development
Megaruby - Running mruby/c programs on Sega Mega Drive

Summarized using AI

Megaruby - Running mruby/c programs on Sega Mega Drive

Yuji Yokoo • September 08, 2022 • Mie, Japan

In this presentation at RubyKaigi 2022, Yuji Yokoo discusses running mruby/c programs on the Sega Mega Drive, detailing his experiences and technical insights related to this retro gaming console and its development. Key points include:

  • Introduction to the Mega Drive: Yuji begins by introducing the Mega Drive, a popular 16-bit video game console released in 1988, and its architecture, emphasizing the Motorola 68,000 CPU, and its significance in gaming history.
  • Overview of M Ruby and M Ruby/C: He explains M Ruby, an embedded version of Ruby that integrates seamlessly with C, and discusses its limited yet powerful implementation for the Mega Drive.
  • Choice of the Mega Drive for Development: Yuji reflects on his nostalgia for the Mega Drive as he had one in high school and highlights its ongoing accessibility due to various new compatible units and extensive available development resources.
  • Introduction to SGDK: The Sega Genesis Development Kit (SGDK) is introduced as a free and open-source toolkit that facilitates Mega Drive game development, encompassing features like resource compilation and memory management.
  • Development Process: He outlines the general process for developing games using C and Ruby, detailing cross-compilation, resource management, and the integration of M Ruby with C functions to create executable ROMs.
  • Graphics and Memory Limitations: Special emphasis is given to the Mega Drive's graphics capabilities, describing its unique tile-based graphics system and memory limitations, necessitating manual optimization strategies.
  • Demo Game Presentation: Yuji showcases a simple demo game developed for the presentation, highlighting the sprites, parallax scrolling, and interaction with M Ruby/C on the Mega Drive.
  • Future Improvements: He concludes by discussing future plans for enhancing stability and automating development processes to overcome existing memory management challenges.

Overall, this engaging presentation combines technical details with personal anecdotes, providing a comprehensive look at programming for the Mega Drive using modern Ruby implementations and showcasing the potential of retro game development with today's tools.

Megaruby - Running mruby/c programs on Sega Mega Drive
Yuji Yokoo • September 08, 2022 • Mie, Japan

RubyKaigi 2022

00:00:07.640 Good morning, everyone! Thank you for being here on Saturday morning, on day three of the conference. I'm really happy that you've made it here to listen to me talk about the Mega Drive.
00:00:11.920 I have an actual Mega Drive unit right here outputting this screen. Unfortunately, I can't lift it to show you the actual unit since it's all hooked up, and I don't want to mess with the settings. Instead, I brought a spare. This Mega Drive was purchased in Australia, and it comes with a special cartridge. It's not exactly the same as the one connected right now, but basically, those special cartridges allow me to boot from the SD card inserted.
00:00:41.280 Let me start this presentation. This might look familiar to you; these are just the starting screens of my presentation. Here, this is the application I wrote and named 'Mega Present'. The graphics may not be great, but that’s not the limitation of the Mega Drive; it's simply a reflection of my artistic abilities.
00:01:17.479 First of all, I'd like to share the table of contents for this presentation. I’ll tell you a little bit about myself and about the Mega Drive, then discuss M Ruby and m/c, explaining why I chose to focus on M Ruby on the Mega Drive. I'll also touch on something called SGDK and share more insights about the Mega Drive. Finally, I’ll show you some example code and hopefully demonstrate a game.
00:02:39.080 So let’s get started. I'm Yuji Yokoo, a programmer based in Adelaide, Australia. I've always been passionate about video gaming. A couple of years ago, I presented on Takeout Edition about the Dreamcast and M Ruby, and this time, I'm back with the Mega Drive and M Ruby. Here is a pixel art version of myself.
00:03:10.280 Now, let’s discuss what the Mega Drive actually is. If you’re younger, you might not be familiar with the Mega Drive at all. The Sega Mega Drive was a video game console released in 1988, often known as the Sega Genesis in some regions. It featured a Motorola 68,000 as its main CPU and was marketed as a 16-bit console. While it's debated how many bits it truly had in terms of architecture, it was widely recognized as a 16-bit console and became very popular, selling over 30 million units worldwide, especially outside of Japan.
00:04:42.400 You might also be familiar with M Ruby; after all, this is RubyKaigi. There were some talks on M Ruby in previous days. M Ruby is another implementation of Ruby, intended for embedded environments, integrating well with C out of the box. It can also interface with other languages and is particularly useful for calls from C.
00:05:28.799 You might be wondering about native extensions used to integrate C into Ruby. While that's true, writing C-callable functions with CRuby isn't straightforward. With M Ruby, however, you can write Ruby functions and call them directly from your C programs. I've mentioned M Ruby and M Ruby/C. M Ruby/C is a more compact version designed as a VM-only project from the Shiman Open Innovation Center and Kushu Institute of Technology. It provides the VM only and is already somewhat more limited than CRuby, and M Ruby/C is even more limited but retains many of Ruby's great features.
00:06:51.720 Now, let me explain why I've been using the Mega Drive for this project. I had a Mega Drive when I was in high school, and I really enjoyed it. That's probably my main reason, but there are other reasons why the Mega Drive makes sense. There are numerous open-source dev kits available, one of which is called SGDK, which is excellent. Additionally, many units of the Mega Drive were sold back in the day, so there are plenty of second-hand ones available today.
00:07:43.680 Another advantage is that there are many newly produced compatible units, some of which feature built-in emulators, while others utilize FPGA technology. You can still buy these new units, which are often of really good quality. Unlike the Dreamcast, which has no compatible hardware and is slowly becoming rare, the Mega Drive still has various hardware options to run your software.
00:08:52.040 Moreover, there are many good emulators available that are extremely useful for development, especially when working on embedded projects. Often, real hardware is needed for testing, which can be challenging. Fortunately, when working on a Mega Drive, you have access to many excellent emulators and a wealth of active projects with a lot of information available. There are also many homebrew developers and indie software companies producing new games for the Mega Drive.
00:09:37.680 So, you might be asking why I chose to use M Ruby SLC on the Mega Drive. This is RubyKaigi, so it makes sense, right? Using M Ruby not only makes me happy but also makes me more productive. Typically, in a situation like this, you'd write your main game logic in Ruby, while the code that interacts with the platform would be in C, alongside the initial loader that loads your M Ruby code.
00:10:17.679 Now, let’s talk about SGDK. SGDK is the Sega Genesis Development Kit — a free and open-source development kit for Genesis or Mega Drive. It provides an API and comes with many tools and features such as cross-compilers and linkers, along with resource compilation capability.
00:10:53.560 SGDK also manages memory and works with tiles, sprites, and palettes specific to Mega Drive. For instance, the text you see on the screen right now uses the default font provided by SGDK. This tool can also control the audio output, and there are many more features available. Additionally, I use something called Gend Dev, which allows me to use SGDK on Linux, since SGDK is primarily for Windows.
00:11:47.320 Let me summarize the general process of using both SGDK and M Ruby/SLC. The SGDK default build expects a main function to produce an executable, from which a ROM image is created that can be booted. M Ruby/C comes with sample programs, but by default, it doesn’t include a main function. To make this work with SGDK, I checked out the entire M Ruby/C source and modified it to compile correctly with SGDK. I also added my main function to enable it to produce an executable. Along the way, I had to aggressively cut out sections that presented build issues, so while some valuable features may be missing, it's functioning well enough for my needs.
00:12:55.680 Let’s dive a little deeper into the Mega Drive, specifically regarding its graphics capabilities, as they are somewhat unusual by today’s standards. It was effective in its time, but many aspects seem odd now. The Mega Drive’s video display processor operates at 13 MHz with a resolution of 320 pixels by 224 or 240, depending on whether you are using NTSC or PAL. These are the standard TV resolutions used during the era of analog signals.
00:13:55.600 The Mega Drive is limited to a palette of 512 colors, but you cannot display all 512 colors simultaneously on the screen. Instead, it limits you to 61 colors at a time. This is because you need to create palettes, each containing 16 colors, from which you can use four. One color in each palette is reserved for transparency, meaning that you end up with just 15 usable colors per palette. Additionally, a default background color is added, bringing the total to 61 colors.
00:15:13.640 The graphics system is tile-based, meaning that each tile on the screen is associated with a palette. Everything visible on the screen is made up of these tiles. For example, the ninjas at the bottom of the screen, which I borrowed from the official RubyKaigi site, are composed of 8x8 pixel tiles. Every character displayed on the screen is also constructed from tiles. The Mega Drive supports two background layers, which can be thought of as foreground and background. Additionally, it utilizes sprites, which are movable graphics. The ninjas, for example, are sprites.
00:16:53.239 This image of me is also made up of tiles, which might give it a horror film appearance. The scrolling feature is straightforward to utilize; the foreground and background move in different directions and at different speeds, demonstrating the hardware capabilities of the Mega Drive.
00:17:49.720 Developing for the Mega Drive presents unique challenges, particularly regarding memory limitations. You’re working with only 64 kilobytes of RAM — not megabytes, but kilobytes. This small amount of memory necessitates careful management, especially when using languages like Ruby that can create numerous temporary copies of strings. These copies can lead to transient memory shortages.
00:18:43.240 To address this, I've invested significant effort into manual optimization. For instance, the text visible on the screen utilizes pre-rendered images rather than the built-in text rendering capabilities of SGDK. This approach can be more memory efficient, given the need to manage ROM data properly. Since the Mega Drive doesn’t have a conventional console output — there’s no built-in print functionality for debugging or logging — I leverage emulators for this purpose.
00:19:50.240 Regarding emulators, I use one called UJK mode, which provides a separate window for console messages and allows me to see what's loaded into VRAM. It even features remote debugging with GDB, a tool I haven't tried yet. Utilizing emulators streamlines the development process, making things significantly easier.
00:21:18.440 Here’s an overview of how to develop a Mega Drive game using C. Start with your C source code and your assets, such as images and sounds. You'll cross-compile the C source into an object file executable for the 68K processor. If your main computer also utilizes the 68K architecture, cross-compiling is unnecessary, but that is rare.
00:22:06.480 Next, you can use a resource compiler to produce compiled resources, bringing them together with your object files and the SGDK library. This extensive assembly allows you to format your ROM so that it can be booted on the actual hardware. Earlier, I mentioned special cartridges capable of reading from SD cards — this means that those ROMs you create can be booted directly.
00:22:58.600 With M Ruby, I checked out the entire source of M Ruby/C, combined it with my C functions, and ensured everything compiled together seamlessly. I then wrote some M Ruby code and utilized the M Ruby bytecode compiler from the standard M Ruby distribution, producing the required compiled bytecode. After that, I cross-compiled it into an object file and repeated the resource compilation process for the ROM file.
00:24:26.680 The SGDK allows for the declaration of resource files, automatically converting them into a workable format. For example, declaring a resource file named 'RubyK.png' allows you to reference it from your C code after including the generated header. The function 'VDP_DrawImageEx' is part of SGDK's capabilities to render images on the screen.
00:25:31.839 Once everything is prepared, you can use plain Ruby to script your game. This is a simplified view of the presentation process. The class defined in C can be accessed from your Ruby code, enabling you to call its methods like any standard Ruby function. Now that you've seen how to write and build your programs, let me show you a demo game I created for this presentation.
00:26:47.440 The demo I've written doesn't serve much purpose besides allowing me to jump over obstacles. There's no scoring or advanced features yet, but this illustrates what can be developed using M Ruby/C on the Mega Drive. Notice the sprites for the ninjas, spikes, and the parallax effect in the background; they all move at different speeds.
00:27:38.160 As for the future plans of this project, it’s not entirely stable and has exhibited unexpected behaviors due to memory limitations. Looking ahead, I want to improve how it handles memory shortages and refine the code quality, which is quite tangled right now.
00:28:25.880 I'd like to focus on enhancing stability, addressing the bugs introduced during modifications, and automating some processes that I've performed manually. Automation would simplify development and improve the functionality.
00:29:02.240 Here are a couple of URLs related to the demo game you'll see shortly. I want to thank the various contributors for creating the software that allowed me to develop this project.
00:29:16.480 Now, let's proceed to the next part of the presentation. I'm looking forward to showing you more.
00:29:35.000 This allows me to choose which ROM I'd like to boot from. The demo game you'll be seeing now is reminiscent of a classic word game that gained popularity in 2022. Let’s see what I can guess! You’ll notice different letters highlight based on their placement within the word.
00:31:40.880 Thank you very much! That's the end of my presentation. If you're interested in M Ruby or would like to discuss anything related to the Mega Drive, feel free to come and talk to me.
00:32:02.720 Thank you!
Explore all talks recorded at RubyKaigi 2022
+33