Ruby Programming

Summarized using AI

Megaruby - mruby/c on Sega Mega Drive

Yuji Yokoo • February 16, 2023 • Melbourne, Australia

The video presentation titled "Megaruby - mruby/c on Sega Mega Drive" by Yuji Yokoo discusses the integration of Ruby code with the classic Sega Mega Drive platform using mruby/c. The speaker highlights his journey and the associated challenges he encountered while developing Ruby applications for this retro console, which was originally released in 1988. \n\nKey Points Discussed:\n- Introduction to Mega Drive and Presenter: Yuji Yokoo explains his background, the context of the presentation at RubyConf AU 2023, and presents basic information about the Sega Mega Drive, including its hardware specs such as the Motorola 68000 CPU.\n- Understanding mruby and mruby/c: The speaker differentiates mruby from other Ruby implementations, noting that mruby is designed for embedded environments and is particularly suited for platforms with limited resources, like the Mega Drive. The mruby/c version is highlighted for its compact nature and emphasis on memory efficiency, which is crucial for the 64KB RAM limitation of the Mega Drive.\n- Development Kit - SGDK: Yooji introduces SGDK (Sega Genesis Development Kit), detailing its features which ease the application development process by providing a C API and memory management tools.\n- Challenges in Development: The speaker discusses the significant challenges of developing on the Mega Drive, particularly emphasizing memory optimization techniques due to the system's limited RAM. He shares the experience of utilizing emulators for debugging.\n- Real-world Applications and Demos: The presentation showcases ‘Mega Present Dash Plus,’ a Ruby-based application tailored for the Mega Drive. It goes on to provide a brief demo of a simple game featuring graphics and sprites. \n- Conclusion and Future Work: Yuji expresses his intent to improve the developer experience and his ongoing commitment to enhancing his projects using mruby/c. He also mentions the creation of a webpage featuring the resources used in the development process.\n\nThe main takeaway is the exciting potential of developing modern applications using legacy hardware by leveraging contemporary programming languages like Ruby, even in 2023. The enthusiasm for continuing the vibrant development scene around the Mega Drive is evident, as is the possibility for open-source collaboration and innovation.

Megaruby - mruby/c on Sega Mega Drive
Yuji Yokoo • February 16, 2023 • Melbourne, Australia

With mruby/c, it's now possible to run Ruby code on Sega Mega Drive!

I will show you how to get started, what makes Sega Mega Drive a great platform for mruby/c, and some of the challenges I faced.

This presentation will be delivered on a Mega Drive unit.

RubyConf AU 2023

00:00:03.140 Hello everyone, my name is Yuji Yokoo, and today I’ll be talking about Mega Ruby on the Sega Mega Drive.
00:00:06.660 Thank you for the introduction, and thanks everyone for your patience. We had a bit of an equipment issue, and that's what you get for trying to connect 1980s equipment to a modern AV system. However, we've managed to get through it. Here, I've got my Sega Mega Drive connected, and the output you see is from a special cartridge that allows you to boot any ROM file stored on your SD card. I have a few ROM images here, and the particular file I’d like to present now is my presentation.
00:00:57.960 Okay, and I suppose this is my first time presenting at RubyConf AU 2023. This may be the first time you see this on a Mega Drive screen, and I would like to thank the organizers and the staff for giving me the chance to talk to so many people about the Sega Mega Drive. It's truly amazing, and I hope many of you are here on your employer's dime, so you can return and report that you’ve learned how to develop software for the Mega Drive.
00:01:21.899 I recently started working with Cipherstash at the end of January. Although this presentation is not related to what they do, they were still generous enough to cover my accommodation, airfare, and sponsor me as a speaker, for which I am grateful. Now, let's dive into my presentation. The title screen of my applications is called 'Mega Present.' I developed it using Ruby along with some C and it's a presentation application tailored specifically for the Mega Drive. After making some modifications last year, I began referring to it as 'Mega Present Dash,' and with additional updates, I'm now calling it 'Mega Present Dash Plus.' Who knows, one day it might evolve into 'Super Mega Present'!
00:02:42.060 Let me share the table of contents with you. I will start by telling you a bit about myself, then explain what the Mega Drive is. I understand some of you might be too young to know about the Mega Drive. After that, I will introduce mruby and mruby/c, which you may not be familiar with, and explain why mruby makes sense for the Mega Drive. I also want to talk about sgdk, the software development kit I have used. Following this, I will provide more information on the Mega Drive, present some examples, including tiny snippets of Ruby code, and ultimately conclude with a demo game.
00:03:19.920 Here’s a pixel art version of me. As I mentioned, I work at Cipherstash, and although they do not develop software for the Mega Drive, there's a possibility that could happen in the future. I am based in Adelaide, Australia, and here are my social accounts if you’d like to contact me.
00:04:00.840 Now, let's start with the Mega Drive. The Sega Mega Drive, also known as Sega Genesis in some parts of the world, is a video game console released in 1988. It features a Motorola 68000 as its main CPU running at 7.6 megahertz. Here I have another Mega Drive unit that I'm not using today; you can see it's proudly labeled as a 16-bit machine. Back in the day, being labeled as 16-bit was a big deal. The Mega Drive features 64 kilobytes of main RAM in addition to 64 kilobytes of video RAM. This is not gigabytes or megabytes. We're talking about kilobytes!
00:04:46.620 Even now, a relatively expensive notebook might include 64 gigabytes of RAM, but we are talking about 64 kilobytes here. Over 30 million units of the Mega Drive were sold, making it quite popular in the '80s and '90s, especially outside of Japan, even though it was produced by a Japanese company, Sega. You might wonder what mruby is. Well, mruby is another implementation of Ruby. There exist several other implementations like JRuby, CRuby, and TruffleRuby. mruby is intended for embedded environments, which allows it to integrate well with C.
00:05:49.500 If you're not aware of the Ruby implementation you're using, you are probably using CRuby. With mruby, you can call C code from Ruby via native extension. However, calling Ruby code from your C code is considerably easier with mruby. Its feature set is somewhat limited as it implements most of the ISO standard, but it does not include dynamic features such as require, eval, or RubyGems, which are typically not available in mruby. Everything is compiled into a binary that runs on your platform. Now, let’s talk about mruby/c (mrbc). The ‘C’ stands for compact, and it is a project developed by the Schumann ID Open Innovation Center and Kyushu Institute of Technology, providing a compact VM for mruby. Unlike the complete mruby distribution, which includes an interactive environment and bytecode compiler, mrbc focuses solely on the VM.
00:07:07.680 It is typically not faster, but it emphasizes memory efficiency. In mruby/c, there is no support for features that require dynamic loading, so every feature you need has to be included in your source tree. Now, you might ask why you should develop software for the Mega Drive in 2023. The primary reason is there are plenty of good open-source development kits available, including one I use called SGDK, which I will explain in detail in a moment. Due to the popularity of the system, second-hand units can be easily found, and there are even new Mega Drive-compatible game systems available. If a Mega Drive unit breaks, you can replace it with a compatible version from another manufacturer, many of which are FPGA-based and considered high quality.
00:08:55.470 The active Mega Drive development scene plays a significant role in my enthusiasm. There are many indie studios that still create games for the platform, and numerous homebrew developers, like myself, contribute to the ecosystem. This brings us to the question of why to use mrbc for development on the Mega Drive. Well, it’s Ruby, and that is a strong reason in itself. Typically, you’ll need to write some C code to load your mruby code, but after that, you can run your Ruby code while interfacing with the platform API as needed. The core logic can predominantly be expressed in Ruby.
00:10:11.760 Next, let me share some details about SGDK, the Sega Genesis Development Kit. It is one of the many open-source development kits available for the Mega Drive. It provides a C API, relieving you from writing everything in assembly code, and it comes with a range of features, including memory management and graphics capabilities for managing tiles and sprites.
00:10:39.540 Furthermore, SGDK includes a default font, meaning the characters you see on the screen come from the SGDK's default font. It does a lot of great things and features a resource compiler that helps consolidate your assets. I use a tool called Gendx, which enables you to run SGDK on Linux. If you look closely, you’ll notice little wombats at the bottom of the screen; those are sprites, which are movable graphical elements.
00:12:04.380 Now, let's talk about Mega Drive graphics. The Mega Drive has a video display processor (VDP) that drives everything displayed on the screen. The VDP runs at about 13 megahertz and supports both NTSC and PAL modes. Many of you may not be familiar with NTSC and PAL, which were the TV signal standards when everyone was using analog televisions. NTSC was primarily used in the USA and Japan, while PAL was more common in Europe and Australia.
00:12:41.340 The Mega Drive can display up to 512 colors, supporting three bits for RGB, but at any one time, it can only use 61 colors on the screen. The colors can’t just appear wherever you want; they are organized into tiles and palettes. Each tile is coupled with a 15 or 16 color palette, with one reserved for transparency. Since there are four tiles in the Mega Drive, the maximum colors are 15 times 4, equating to 60, plus one for transparency gives a total of 61 colors. Importantly, everything you see on the Mega Drive is drawn as a collection of tiles, even large images.
00:14:42.960 Now, here's an example. There’s a map of Australia with Melbourne, where we are, and Adelaide, the best city in Australia. When you see images like this on the Mega Drive, they too are constructed from those tiles. In terms of scrolling, the VDP has hardware-level scrolling features, allowing you to set background images and specify the offset for automatic scrolling of the background.
00:15:29.420 With development on the Sega Mega Drive, numerous challenges arise, the most significant being the 64 kilobytes of RAM. Ruby is a language that inherently performs a lot of memory allocation, and I encountered quite a few memory issues while trying to draw complex screens. This led me to employ a lot of manual optimization techniques, such as pre-rendering all text into image files and using DMA transfer to move data without consuming RAM.
00:17:06.660 Another challenge is that when developing for a video game console, you often lack a console output; if you use printf or puts for debug messages, there is nowhere for them to appear. Fortunately, emulators—like the one I used, called GensKmod—offer a convenient window for displaying console messages, which makes debugging significantly easier. There are also features like remote debugging with GDB, among many helpful tools.
00:18:55.380 Let's overview how you would develop a Mega Drive game using SGDK. You write your C source code and define your resources, such as sound files and image assets. You cross-compile the C source for the Motorola 68k, producing object files and then utilize SGDK’s resource compiler to create the resource file. You put everything together and add SGDK libraries along with ROM headers, ultimately producing a ROM file executable on the Mega Drive.
00:20:14.760 When working with mruby/c and SGDK, I checked out the full mruby source code and added my own C code. I wrote some Ruby source and used mruby’s bytecode compiler to produce bytecode. I then cross-compiled source files into object files to create a functional binary composed of my original Ruby source.
00:22:21.900 For the Mega Drive application itself, here’s a simplified main function. The main loop mainly consists of plain Ruby code! You’ll see that once I handle platform calls, everything else is just plain Ruby. I prefer to write the slides in a text file, defining page content using tags, and the equal sign denotes the start of a new page.
00:23:54.000 Now, with Mega Drive features, the development process, and everything explained, I’d like to show you a little demo game. As you can see, the sky is moving slowly while the ground is moving slightly faster, along with the spikes.
00:24:12.760 The game features sprites representing ninjas and spikes developed as part of last year's RubyConf, which was heavily themed around ninjas. This demo game is simple, but I have a list of features I plan to implement for improvement.
00:25:03.540 I aim to create additional games that are more engaging than what I've just demonstrated. Right now, everything I’ve developed on mruby/c is intermingled, making it challenging to work with; I want to improve the developer experience overall. Additionally, I’ve recently established a webpage for this project, alongside GitHub links for the presentation application and demo game I’m about to share. I’d like to thank everyone who has contributed to the amazing software I used for this project, including mruby, mrbc, SGDK, and GenDev. Without these tools, none of this would have been possible. Now, let’s get into the demo game!
Explore all talks recorded at RubyConf AU 2023
+9