SCD Graphics documentation?

Discussion in 'Technical Discussion' started by Pyrochrome, Oct 18, 2021.

  1. Pyrochrome

    Pyrochrome

    Member
    4
    0
    1
    We all know the Sega Cd can perform raster effects like rotation and scaling, but is there any documentation on how exactly this is achieved on a hardware level? The only info I can find is some barebones research on an unofficial FAQ. Anybody have concrete info I could use, or a sample of code that demonstrates these capabilities? I'd love to give Sega Cd homebrew a try and this by far is the biggest roadblock.
     
  2. Pobert-Eii

    Pobert-Eii

    Member
    73
    75
    18
  3. Clownacy

    Clownacy

    Tech Member
    901
    193
    43
    • Informative Informative x 1
    • List
  4. Ralakimus

    Ralakimus

    I am CRUSHING YOUR HEAD!!!! Tech Member
    673
    288
    63
    Another bump, but I can explain how it works. Most of what happens is dealt with on the Sub CPU side, as it's a CD hardware thing, which the Genesis doesn't really have direct access to (Genesis can only really see Word RAM on the CD side).

    So, yes, indeed, the Sega CD is capable of performing scaling and rotation effects on a hardware level. If it's set to "2M" mode (where the entirety of the CD's 2 Megabit Word RAM is given to either the Genesis or CD side instead of being split evenly between them like in "1M/1M" mode), then the graphics chip is set to do that. In "1M/1M" mode, it does something entirely different, where it basically can convert a bitmap into tile data, or split up pixels into individual bytes, but that's not what I'm gonna be talking about here. The "hardware/scaling" process goes like this...

    First, you start off with a "stamp bank" and a "stamp map". This is what makes up the source image that you want to apply the transformations to. Stamps can either be 16x16 or 32x32, and are formatted just like Genesis sprite tiles. The stamp map is just a tilemap made up of those stamps. The map can either be 256x256px in size, or 4096x4096px.

    To transform this source image, you need to provide a "trace table". What is contained in that is a list of "starts" and "deltas" for every line in the final rendered image (called the "image buffer"). Basically, first, you describe the size of the final render, and then for each line of pixels in the render, it'll grab a set of starting coordinates, grab a pixel from there in the source image and plot it in the render's line, and it'll continually add those deltas to those starting coordinates and repeatedly grab more pixels for the line it's on.

    The process is summed up here:
    [​IMG]

    So, let's get into the gritty details on how to go about this. Let's start with the registers. They are all 16 bit wide.

    0xFF8058 controls the size of stamps and the stamp map, and also whether to wrap the map if it goes out of bounds when rendering or to just plot blank pixels. Bit 0 controls the wrapping, bit 1 controls the size of a stamp (0 = 16x16px, 1 = 32x32px), and bit 2 controls the size of the stamp map (0 = 256x256px, 1 = 4096x4096px).

    [​IMG]

    We'll talk about the GRON bit in a bit.

    0xFF805A controls the location of the stamp map in Word RAM. The value you put in is the offset relative to the start of Word RAM divided by 4. What the raw location needs to be a multiple of depends on the sizes you have set:
    • 16x16px stamps, 256x256px stamp map: Multiples of 0x200
    • 32x32px stamps, 256x256px stamp map: Multiples of 0x80
    • 16x16px stamps, 4096x4096px stamp map: Multiples of 0x20000
    • 32x32px stamps, 4096x4096px stamp map: Multiples of 0x8000
    [​IMG]

    0xFF805C controls the height of the image buffer in tiles, minus 1. The maximum height is 32 tiles.

    [​IMG]

    0xFF805E controls the location of the image buffer in Word RAM. The same relative offset and division by 4 rules apply. The raw location must be a multiple of 0x20.

    [​IMG]

    0xFF8060 controls the image buffer offset. Basically, you can offset the output render up to 7 pixels on each axis. Bits 0-2 control the horizontal offset and bits 3-5 control the vertical offset.

    [​IMG]

    0xFF8062 controls the image buffer's width in pixels. The maximum width is 511 pixels.

    0xFF8064 controls the image buffer's height in pixels. Note that the rendering process actually decrements this as it renders each line, so don't forget to reset it when beginning a new render. The maximum height is 255 pixels.

    [​IMG]

    The final register, 0xFF8066, controls the location of the trace table. Same relative offset and division by 4 rules as the other 2 address registers. The raw location must be a multiple of 8. Setting this begins the rendering process.

    [​IMG]

    Also, a brief shout out to the memory mode register (0xFF8002), which has a couple of bits for controlling a little about how the output image is rendered in terms of how it deals with previously rendered data leftover in memory. Bits 3-4 control that:
    • %00 - Writes over the leftover data, regardless of blank pixels.
    • %01 - Skips any blank pixel already written, effectively rendering "under" the previous data.
    • %10 - Prohibited
    • %11 - Any non-blank pixel writes over the leftover data, effectively rendering "over" the previous data.
    [​IMG]

    So, now let's talk about formats.

    Like I mentioned before, stamps are basically formatted exactly how the Genesis formats 8x8 tiles for sprites (arranged vertically, then horizontally), so we can mark that off.

    A stamp map is relatively simple: it's just a set of stamp IDs and flags, arranged horizontally, then vertically. A stamp ID and its flags are formatted as so:
    • Bits 0-10 are the ID. A stamp ID is calculated by taking a stamp's location relative to the start of Word RAM and dividing it by 0x80. With 32x32px stamps, bits 0 and 1 are actually ignored, since those go into locations that are partially within stamps, so for 32x32px stamps, IDs are multiples of 4.
    • Bits 13-14 control the rotation of the stamp. %00 = 0 degrees, %01 = 90 degrees, %10 = 180 degrees, and %11 = 270 degrees.
    • Bit 15 is the horizontal flip flag. Note: this is applied first before rotation is.
    [​IMG]

    [​IMG]

    And now, the trace table. Like, I've mentioned before, it's a list of "starts" and "deltas" for each line to be rendered in the image buffer. A start coordinate is formatted in unsigned 13.3 fixed point, while a delta value is formatted in signed 5.11 fixed point.

    [​IMG]

    So, to basically set it up, you set up your stamps, stamp map, and trace table in Word RAM, and to render, you set up the registers, and let it do it's thing. Your rendered image will be in the image buffer. How can you tell when a render is done? There are 2 methods:
    • Set up interrupt level 1. It gets triggered upon the end of a render.
    • Check the GRON bit (bit 15) in the stamp data size register (0xFF8058). It's clear when there's no rendering going on, and it's set when there is.
    Once that's all done, you can then transfer the image buffer data to VRAM on the Genesis side. It is possible to DMA directly from Word RAM, but due to an issue where it's delayed by a cycle, you'll have to add 2 to the DMA source address, and I believe you also have to manually write the first word or longword of data manually. You can also just copy it from Word RAM over to Genesis RAM and then DMA from there.

    The tiles generated in the image buffer are arranged vertically, then horizontally, so when you set up the tilemap for them, keep that in mind.

    You'd also probably want to set up a double buffer in VRAM to prevent issues. Basically, set up 2 areas in VRAM to store the rendered graphics, and for each buffer, have a tilemap set up. When rendering, just load to a buffer and swap, and you won't be having to deal with any tearing or any weird issues that pop up from not double buffering.

    I hope this helps out some. I also have an example repository that renders a 3D plane. Though, you might wanna check out this older commit, where I just do a basic 2D image scale.
     
    • Like Like x 2
    • Informative Informative x 2
    • List
  5. Pobert-Eii

    Pobert-Eii

    Member
    73
    75
    18
    This... is a godsend. Thank you! I've been meaning to take a look at the Sega CD for a couple of years now, so this will be super helpful.