Tag

Showing blog posts sorted under the tag: pio

Building a GD-ROM Optical Drive Emulator for Sega Dreamcast

EmeraldGD prototype installed in a Dreamcast.

The PIO (programmable input/output) hardawre on the RP2040 and RP2350 chips has been a game changer in a lot of applications. After getting familiar with using PIO to interface with the Dreamcast's controller port, I started experimented talking directly to the console through its GD-ROM interface.

The idea came from hearing about another project, PicoIDE, an IDE emulator for retro computers using the RP2350. I had also read that the Dreamcast interfaced with the GD-ROM via ATA, and so thought the RP2350 could probably be used here too.

So, for the past 6 months or so, I've been working on a Dreamcast ODE called EmeraldGD that also uses the RP2350 chip. It reads GDI and CDI disc images, handles the Dreamcast's normal ATA timings in both PIO and multiword-DMA modes, and supports CD-DA for games that use digital audio.

It's not quite a performant as other ODE solutions. This is due to using SPI to read data from the SD card, which can be pretty slow, and not being very efficient fetching data from the SD card. The ATA interface itself runs at full speed. So, there's still room for improvement.

I also tried to make it compatible with the latest version of OpenMenu, at least the simplest case of choosing a game and launching it works. OpenMenu and GD MENU Card Manager are both super convenient for managing games.


Even though the Dreamcast's GD-ROM connector is somewhat proprietary, it basically just brings the ATA and CD-DA (CD digital audio) interfaces together on one plug.

So, I harvested a GD-ROM connector from an old GDEMU board and built a little breakout board to collect traces from a GDEMU and to connect my RP2350 dev board to the Dreamcast. A cheap 16 channel 200MHz analyzer was just fast enough to capture ATA activity. 16 channels is not enough for the whole bus but it worked out all right since I usually only cared about a subset of the bus at any one time.

RP2350 dev board connected to a Dreamcast.

ATA

The Dreamcast uses standard parallel ATA to talk to the GD-ROM drive, which is quite an old standard by now. There's a lot of high-level descriptions of ATA online but finding really detailed stuff was weirdly difficult.

I did manage to find a nice document here that describes the ATA and ATAPI spec and was quite helpful to get a decent understanding. Claude (AI I know) was also really helpful in generating timing diagrams for different ATA operations. Comparing live traces to the documentation also helped a ton.

At a really high level, ATA is basically just a protocol for a host to read from and write to registers of a device. Several lines of the bus are used to setup a read or write, then the data lines get filled with required data. The data lines get sampled, and the whole process starts again.

For a read, when the Dreamcast pulls DIOR low, the drive needs to put the value of the requested register on the data lines as soon as it can. For a write, the Dreamcast pulls DIOW low then the drive just samples the data lines and writes that value into the chosen register.

A sample ATA trace showing a register write, followed by a read.

The above trace (captured from a Dreamcast talking to EmeraldGD) shows two register operations: a read followed by a write, as indicated by first DIOR then DIOW going low.

The write in this trace is actually quite special, it is writing the value of 0xA0 to the Command register. This is part of the ATAPI interface and kicks off a Packet Command. When the drive receives this command code, it gets ready to receive a payload of data for a more complicated command like reading sector data from the disc, or getting the disc's table of contents.

On the Dreamcast, register reads and writes are typically 100ns. To put that into perspective, I set the clock speed of the RP2350 to around 200MHz, which means each clock is about 5ns, so there are only 20 clocks to get data on the lines. Way too fast to bitbang, but the RP's PIO hardware makes this possible.

Using PIO to Talk ATA

To be able to interface with the Dreamcast via ATA, the RP2350 needs to basically just do two things: sample data when a register write is given, and get the correct data on the data lines when a read is detected. Since it is the host that drives the read/write signal lines, these are the timing-critical pieces that the PIO blocks need to take care of.

Dealing with writes was pretty simple, I created a small program that just waits on the DIOW line to go low, then high, then sample the bus into a buffer. The CPU deals with writes on its own time. Pretty much every write I encountered from the Dreamcast was in preparation for a packet command, so as long as the write buffer is flushed before dealing with any commands, everything seemed to work well.

Read requests were the much trickier task. There's absolutely no way that a PIO program would be able to get data from the CPU within the 100ns window, remember that's only 20 clock cycles! The solution I found was to use a new feature of the RP2350 chip that allows it to access arbitrary entries in its RX FIFO.

Each PIO state machine has 4 32-bit entries in its RX FIFO (enough for 8 16-bit registers) which are usually used for sampling data, but it's also possible to use them in a sort of 'register' mode where the 4 entries can be accessed arbitrarily via the mov instruction.

So, I get the address of the requested register from the address lines, then grab it from the RX FIFO using: mov osr rxfifo[y]. I still update the registers on the CPU side, but the PIO program can always respond to reads without needed to go to the CPU.

In addition to register access there are also two ways to transfer larger chunks of data from the drive to the Dreamcast. The more basic method is called PIO (unfortunately named the same as PIO on the RP chips). PIO transfers data via standard ATA register reads through the Data register. Basically, the console just reads the Data register a bunch of times until it has all the data it's expecting.

The way I handle this is with a separate PIO program (the RP's PIO this time) that is triggered with an interrupt when the Data register is read. This program is fed with the data from the CPU via the RP's DMA feature so it can push out 16-bits of new data every time the Data register is read.

A sample ATA trace showing a PIO data transfer from RP2350.

The above trace shows the start of a PIO data transfer, with the response data being supplied from the RP2350. A full Data register read cycle takes about 600ns, a theoretical limit of 3.3MB/s, pretty slow.

Another method of transferring data is called DMA (again an unfortunate name clash). Instead of reading data through the Data register, the Dreamcast and the drive do a bit of a handshake using some special-purpose DMA lines, then the Dreamcast just strobes the DIOR line continuously until it gets all the data it wants. This is much faster than PIO data transfers because it skips the whole setup for requesting a specific register.

This is was actually pretty easy to create a PIO program for. I feed a state machine with data using the RP's DMA feature and the state machine will just continuously pump out 16-bits of data from the buffer every time the Dreamcast strobes DIOR while in the DMA state.

A sample ATA trace showing a DMA data transfer from RP2350.

The above trace shows a DMA data transfer, with the response data being supplied from the RP2350. There's no register reads here, just the Dreamcast bringing DMACK (DMA acknowledge) low then strobing the read line. A single read cycle is about 140ns, a theoretical limit of about 14MB/s, or about 4 times faster than a PIO data transfer.

Most Dreamcast games that I tested tend to use DMA more than PIO, especially for really large data transfers. But, homebrew games using KallistiOS, I noticed tend to use PIO more frequently.

The ATA interface is also only one piece of the whole project, but it was the most interesting for me to work through, so I thought it was worth talking about here. There's lots more like: parsing disc image formats, dealing with different ATAPI packet commands, and of course CD-DA which is important for a lot of games. Maybe I will expand on some of that stuff in another blog post.

Compared to working with the Maple bus, the G1/ATA bus was much more forgiving. As long as the timings for register accesses are kept to, the Dreamcast will just kind of dutifully wait for a response to whatever it asked for, or whatever you tell it to expect. So, while it's certainly more complicated than the Maple bus, at least on the wire, but at the same time was a lot more straightforward to deal with in other ways.

Building a Prototype

Along with the firmware, I also designed a prototype board for use on a real Dreamcast. It's mostly using the RP2350 reference design as a base and just wiring up the GPIO to the GD-ROM connector. I'd never designed a board using a bare RP chip before so it was a fun experience. Those GD-ROM connectors also seem to be very hard to find and I ended up harvesting one from another GDEMU clone.

I went with the RP2354B chip because it has 2MB of built-in flash storage so is a bit cheaper than using an external flash chip, and keeps the board a bit smaller. The unused GPIO are also broken out just in case.

KiCAD render and prototype board.

Surprisingly (or not maybe? I just always expect the worst from my PCB designs) the PCBs came back working without any issues that I could see. I'm sure a proper engineer would be able to find quite a few issues with my board.

Anyways, now that I have an interface to the Dreamcast through the G1 bus, I was thinking about what else could be done with it. One idea was about OpenMenu's settings and Serial VMU features. There's no technical reason that those things couldn't be saved on EmeraldGD using a custom command packet. I think it would be super cool to leverage the serial VMU onto the G1 bus instead of a serial adapter, to essentially get unlimited VMU saves without any additional hardware.

Thanks for reading!


Tags:


Using the Steam Controller on the Sega Dreamcast with Raspberry Pi Pico 2

Pi Pico 2 with Steam Controller and dongle next to a Dreamcast.

I've been scratching my head to come up with a project to play around with the Raspberry Pi Pico's PIO (programmable input/output) and eventually had the idea for some sort of converter to use non-Dreamcast peripherals on the Dreamcast. Of course, all good ideas have already been done, in this case not once or twice, but three times as far as I'm aware, and probably more.

All of these projects are based on the RP2040 chip (original Pico), while I wanted to make use of the new RP2350 (Pico 2) chip. In particular, there is a new PIO input masking feature available on the RP2350 which I think makes for a better implementation for reading the Dreamcast's Maple bus. Also, none of the previously-listed projects actually allow me to use by beloved Steam Controller. All this to say that I really wanted to try things on my own.

The Maple Bus

The Maple bus is how the Sega Dreamcast talks to peripherals on the controller port. It's a two-wire protocol where each wire takes turns being the clock and being the data. Essentially when a wire goes low, it signals that there is data on the other wire (a 1 or a 0), it allows a bit of time to read the bit, before the appropriate wire goes high in preparation for another falling edge.

Below is a sample reading of the Dreamcast send a packet to a controller. In this particular case it's Command 0x01 which is a request for information about what the peripheral is capable of. Hopefully you can see that every falling edge represents one bit of data, read from the other wire. I hope to go into more detail about the Maple protocol in a separate blog post.

PulseView reading of the Dreamcast sending a Maple packet.

Reading and Writing with Maple

To actually read and write data to the Maple bus, the RP2350's programmable input/output (PIO) sounded like the perfect solution. PIO on the RP chips are like little tiny co-processors that can run at a fixed clock rate. Each PIO block has 4 state machines, each with a few registers, and access to a shared space of up-to 32 instructions from a very limited set of instructions.

I wrote two PIO programs, one for reading and one for writing, and both got close to the 32 instruction limit so I ended up using two of the three PIO blocks available on the RP2350. With four state machines in each PIO block, it should - in theory - be possible to read and write from up to four controller ports on the Dreamcast. Though I'm only using one at the moment.

To read the Maple bus, I configured the PIO to constantly check the value of the 2 data lines, waiting for the value to change due to a line going high or low. It takes advantage of the fact that the Maple bus only has data available during a falling edge, meaning a valid is read when the voltage on the wires either goes 11->10/01 or 10/01->00. The former is interpreted as a 1 and the latter as a 0. The PIO program then pushes this bit (eventually) back to the main C program where it can be interpreted.

Writing to the Maple bus was a lot more straightforward. A bunch of data is fed to the PIO which clocks out the start-of-packet signal then starts sending the data accordingly, before finishing with the end-of-packet signal when no more data is available in its buffer.

The next two images below show what the whole thing ends up looking like on the wire. The first packet, pictured below, is a request for data coming from the Dreamcast, and the second packet is the requested data being supplied by the Raspberry Pi:

PulseView reading of the Dreamcast sending a Maple packet requesting data from the controller.

The above packet is send by the Dreamcast and has a command code of 0x9, which is a 'Get Condition' command. It also has some included data (the pink bubbles) with a value of 0x00000001 which should be interpreted as 'Controller'. So, it's asking for the current status of the controller.

PulseView reading of the Pico responding to the Dreamcast with data.

This packet is sent by the Pico in response to the Get Condition command. It has a command code of 0x8 which says that this is a response packet with some data attached. The first 4 bytes are again 0x00000001 saying this is about the controller, then the remaining bytes describe which buttons are pressed and the current axis values of the joystick and triggers.

The whole request and response only takes about 125 microseconds or 0.125 milliseconds. But the controller state is requested about every 16.7 milliseconds, or once per frame at 60fps.

Getting Data from the Steam Controller

Getting USB data from the Steam Controller was surprisingly straightforward since the Pico SDK has TinyUSB as a built-in library. So, with that and the hid-controller example from the TinyUSB docs I was able to start getting the raw USB data quite quickly.

To interpret the raw data that was coming from the controller, I referenced Linux's hid-steam driver as it has mappings as to the meaning of specific bits and bytes sent by the Steam Controller.

Then it was just a matter of keeping track of what buttons are pressed, and sending that data to the Dreamcast via the Maple bus whenever it was requested.

The Second Analog Stick

It's not really a secret, but also not widely known that the Dreamcast supports 2 analog sticks. Very few games will even accept input from a second analog source; Quake 3 and Unreal Tournament are two games known to support it. So, to make full use of the Steam Controller, I wanted the right trackpad to emulate the second analog stick.

To do this, I started by sampling touchpad X,Y-coordinates from the Steam Controller to try and get a velocity of finger movement. Just taking a single change in X,Y from every poll of the controller was quite jittery and not a very pleasant experience. By keeping track of the last several readings, I was able to calculate the average velocity which gave much better results. I played around with a few values but found that 10 samples was generally enough for input to feel smooth.

Another component to a nice touchpad experience is inertia from a quick flick. Basically, input should continue in the last know direction for some time whenever a finger is lifted off the touchpad. I did this by taking the last calculated average velocity and feeding it into an easing - exponentially decaying - function. Again, this involved some experimentation to get a good feel. Eventually, I landed on a cubed decay that lasted 400 milliseconds. Putting a finger back on the touchpad will also stop the inertial movement so it really feels like flicking a trackball around.

Closing

This really wouldn't have been possible without the awesome Dreamcast documentation from Marcus Comstedt. I'm no electrical engineer so his explanations of the exact workings of the Maple bus were invaluable. He also happened to write the Maple bus decoder for PulseView which generates the nice little data bubbles under the logic analyzer readouts. This project would have easily taken twice as long without that. I highly recommend checking out his stuff if you are interested in knowing more about the inner workings of the Sega Dreamcast.

There's also a little video showing the controller in action below, playing some Sonic Adventure.


Tags: