emuforge

The VDP

The VDP draws the picture, makes the interrupts and copies memory. Its own memory is not on any processor's bus: everything goes through two ports. While it draws, it has little time for what comes through them. So what a game writes can wait, the 68000 can wait for that, and a line can change while it is drawn.

It is in crates/mdemu-core/src/vdp.rs and vdp/, ported from jgenesis ("the base"). fifo.rs, registers.rs, sprites.rs, render.rs, colors.rs and cramdots.rs below are in vdp/. Performance has the shortcuts.

Memories

MemorySizeWhat it has
VRAM64 KBtiles, tables of tiles (name tables), sprites and horizontal scroll
CRAM64 words of 9 bitsfour palettes of 16 colours
VSRAM40 words of 11 bitsvertical scroll
Sprite table cache4 bytes for each of 80 spriteseach one's line, size and next sprite

The sprite table cache is a copy of the first half of each sprite's 8 bytes. It changes when one of these bytes is written where register 5 says that the table is. If a game moves the table, the cache keeps what it has. In this core, it changes when a word goes into the FIFO, and not when it gets to VRAM, as an effect of one demo needs that.

Register 1 can turn on a mode for 128 KB of VRAM. VRAM still has 64 KB here. Addresses are changed as in a published formula, and each write stores only its low byte. An old mode (mode 4) is not drawn. But while it is on, writes to registers above 10 do nothing, which one game needs.

The ports

AddressReadWrite
$C00000datadata
$C00004statusa register, or half of a command
$C00008the V counter and the H counter
$C0001Cthe debug register

Data and control are at four addresses each, and so are the counters. A byte that is written to the VDP counts as a word that has it twice. The PSG is at $C00011 (Sound).

A word to the control port whose top two bits are 10 sets a register, with its number in bits 12 to 8 and its value in bits 7 to 0. Anything else is the first half of a command, and the next word is the second half. Together they have an address and a code. Its lowest four bits are 0000 to read VRAM and 0001 to write it, 1000 and 0011 for CRAM, and 0100 and 0101 for VSRAM. 1100 reads single bytes of VRAM. Bit 5 of the code starts a DMA, if register 1 allows DMA. After each read or write of data, the address goes up by register 15. A write to a register changes the lower part of the address too. A read of the status or an access to the data port makes the next control word a first one again.

The status has these bits, from 9 to 0: the FIFO is empty, the FIFO is full, V interrupt pending, too many sprites, sprites collided, odd field, V blank, H blank, DMA, and PAL. V blank also reads as 1 while the display is off. Reading the status clears the two about sprites. Bits above 9 are open bus.

The FIFO and its slots

Data words wait in a FIFO with four places (fifo.rs). The VDP can only take one out in a slot, which lasts two dots. Slots for writes come in a line as often as this:

H32 (256 dots)H40 (320 dots)
While a line is drawn1618
In V blank, or with the display off166204
  • A word to VRAM takes two slots. One to CRAM or VSRAM takes one. A word with a code that is not valid takes two, as one game needs. In the 128 KB mode, a word to VRAM takes one.
  • A word cannot leave for the first three slots or so, which demos showed. Slots for the memory's own refresh do not count.
  • When the FIFO is full, the 68000's next write is kept beside it, and the 68000 waits until it is in. The Z80 waits too if it goes to the 68000's bus.
  • A read while the FIFO has something gives its value at once, and then the 68000 waits until the FIFO is empty. If a word for the same address is still on its way, the read gives that word. This is not well tested. The bits that CRAM and VSRAM do not have come from the next place in the FIFO.

If a game turns the display on while the FIFO has something, it only goes on when the FIFO is empty. That is possibly not how the console does it, but it corrects the picture of games that do this right after a DMA.

Three kinds of DMA

Registers 19 and 20 have the length, where 0 stands for 65,536. Registers 21 to 23 have the source and the kind. The address goes up within 128 KB and then starts again there.

Memory to VDP takes the 68000's bus, and gives it back once the last word is in the FIFO. It reads one word in each slot that is not for refresh, as long as the FIFO has a place. Up to four words can still be on their way then. The source is the cartridge or the 68000's memory. Between them there is only open bus, and a 32X or a Mega CD changes what is in the cartridge's place. From a cartridge with an SVP and from the Mega CD's word RAM, each word comes one read late. When the DMA starts, nothing leaves the FIFO for about 8 slots, or 5 for VSRAM. The code calls that a hack, as demos decided both numbers. Each slot for refresh also costs the read after it.

A 68000 instruction can write two words to the control port, and the first can start a DMA. The second waits in the VDP until the DMA is over, and then counts as if it had just been written. Games need this.

A fill waits for a word at the data port. The word is written as usual. Then its high byte goes to one address after another, one in each slot for writes. A word that is written during the fill changes what is filled in. A fill of CRAM or VSRAM takes its value from the next place in the FIFO. That is a fault of the console, and is here too. A copy goes from VRAM to VRAM, with a byte for each two of these slots. The 68000 runs beside both, and the DMA bit of the status says when they are done. The FIFO always goes first.

Counters and interrupts

A line has 342 dots in H32 and 420 in H40. A dot lasts 10 cycles of the master clock in H32. In H40 it lasts 8, and during parts of the horizontal sync 9 or 10. That way, both make 3,420. The H counter is the dot's number, which skips a part and has 9 bits, without the lowest bit. The V counter has 8 bits, and goes back during V blank. Register 0 can hold both at one value.

The H interrupt's counter goes down with each line of the picture and with the frame's last one. If it is 0, it starts again at register 10 instead, and the interrupt is asked for. During the rest of V blank, it is always set to register 10. The V interrupt comes in the first line after the picture, and the Z80's interrupt line is low during that whole line. Both V and H wait until the 68000 acknowledges them, which the console's page explains. External interrupts are not emulated, and neither is their bit in register 11.

A status or counter that the 68000 reads is from slightly later. In the core, all of an instruction's reads are at its start. On the console, such a read is near the end. The core looks ahead by as much time as MOVE, BTST and CMP need before that, and by 8 of the 68000's cycles for other instructions. A pending V interrupt can be seen that way before the 68000 takes it, as several games need.

What a line is made of

From back to front: the backdrop colour, plane B, plane A or the window, and sprites. A dot with colour 0 is transparent. Dots whose tile or sprite has priority come in front of all others, in the same order. Tiles have 8 by 8 dots with 4 bits each.

  • Planes. A word of a name table has priority, a palette (2 bits), a flip in each direction and the tile (11 bits). Planes are 32, 64 or 128 cells wide and high, but their tables end after 8 KB and start again. Without a valid width, they are 32 cells wide and 1 high.
  • The window stands in for plane A from the left or right edge, and in all of the lines from the top or bottom that register 18 says, in steps of 8 lines on the screen. In the second interlaced mode, that is 16 of the lines of both fields, as in ares. The base has 8 there. It does not scroll. Its table is 32 cells wide in H32, and 64 in H40, where one bit of its address is ignored.
  • Horizontal scroll is for the whole picture, for each 8 lines or for each line. With the setting that is not allowed, the values of the first 8 lines repeat.
  • Vertical scroll is for the whole plane or for each column of 16 dots. The VDP draws such columns, and with a horizontal scroll that is not a multiple of 16, a part of one more is at the left. In H40, that one is scrolled by VSRAM $4C AND $4E, for both planes, and in H32 by 0.
  • Sprites are 1 to 4 tiles wide and high. Each one names the next, starting with sprite 0, and sprites that are named earlier are in front of later ones.
LimitH32H40
Sprites in the table6480
Sprites in a line1620
Tiles of sprites in a line3240

With too many sprites or tiles in a line, the rest is left out, and the overflow flag is set. Tiles outside the picture count too. The collision flag is set when two sprites have dots at the same place that are not transparent. A sprite at horizontal position 0 hides the sprites after it in a line. It needs one at another position before it, or a line before that had too many tiles.

If the display is off during a part of H blank, the VDP looks at fewer sprites and gets fewer of their tiles. The core leaves out one sprite for each dot of that time and one tile for each two. That is only close to what the console does.

Shadow and highlight (register 12). All of the line is in shadow, but not where plane A or B has priority, even with a transparent dot. Colour 14 of palette 3 in a sprite is not shown, and makes what is behind it one step brighter. Colour 15 puts what is behind it in shadow. A sprite with priority is shown with its usual colour, and so is colour 14 of the other palettes. The 8 levels of red, green and blue are not evenly spaced (colors.rs). With a 32X, all of them are darker.

When a line is drawn

A line is first drawn all at once, where its picture begins. Other things have their own place in the line. These are the values of the H counter, with all 9 bits, in the order in which they happen:

H32H40What happens
$000$002the line's horizontal scroll is read, with the registers as they are now
$001$002in the first line after the picture: the V interrupt. In H40 it comes just before the scroll
$018$01Athe sprites of the next line are chosen, with a copy of the cache from the last line. Then this line is drawn
$100$142the rest of what those sprites need is read from VRAM
$108$14Afor the slots, V blank begins (in the picture's last line) or ends (in the line before the frame's last one)
$10A$14Athe H interrupt's counter and the V counter move on, and the cache is copied
$121$169most registers are kept (latched) for the next line, and so is the vertical scroll of whole planes

Real hardware does sprites at other times in the line. The core does it in this order so that it knows how long the display was off. The last row is an estimate. It comes within 36 of the 68000's cycles after the H interrupt, and only to take an interrupt, the 68000 needs 54. So the handler is too late for the next line with most registers.

They wait for the line after it. A few changes do not wait. If one comes while a line of the picture is on its way, the rest of that line is drawn again:

ChangeFrom where it counts
Display on or off, and the backdrop colour (registers 1 and 7)this dot
A colour in CRAM, when it leaves the FIFOthis dot
VSRAM, when it leaves the FIFO, with scroll for each columnthis dot
Name tables (registers 2, 3 and 4)the next column whose tiles are not chosen yet

Sprites are not drawn again. A change in the last 16 dots of a line draws nothing again, as the time between the 68000 and the VDP is not exact enough for that. A change in VRAM does not either. So a new tile is seen in the next line, or in what one of the changes above draws again.

Writing a colour also leaves a dot of that colour where the VDP is (cramdots.rs). It is put into the picture when the next line is drawn. Demos draw whole pictures this way, with the display off and a DMA that writes the backdrop colour all the time. To show these, the core gives a few more dots the same colour, or the rest of the line when no more writes follow. The code calls that a hack.

Picture sizes and interlace

A picture has 256 or 320 dots in each of 224 (V28) or 240 (V30) lines. A change between H32 and H40 moves the events above at once. The picture's width changes at the start of V blank or of the frame. Borders are not shown. A picture is complete after its bottom border, or after its last line when that is later. That is line 240 with V30 on NTSC, where the base's is complete at line 232. It is complete once in each frame, also when a game changes between V28 and V30. If a game changes from V30 to V28 in lines 224 to 239, the VDP "forgets" the border. The H interrupt and the work on sprites go on then, but what it draws there is not shown.

Register 12 has two interlaced modes. Both make a picture that is twice as high, from fields with 262 and 263 lines in turn on NTSC, and 312 and 313 on PAL. In the first mode, both fields have the same lines, and the core fills two rows with each one. In the second mode, cells have 8 by 16 dots, and the core draws the lines of both fields in every field.

The debug register

Bits 7 and 8 choose the backdrop, sprites, plane A or plane B, and bit 6 turns the display off. If bit 6 is set, only the chosen layer is seen. Without it, the number of the colour in front has only those bits left that the chosen layer's number has too. With the backdrop chosen, nothing changes. A write does not draw the rest of the line again. A demo uses this a lot.

What is not emulated, or not known

  • Borders, mode 4's picture, and external interrupts.
  • The time of reads. A read of data needs no slot of its own. With a code for writes or one that is not valid, it gives $FFFF. What it should give is open.
  • Refresh slots while a line is drawn are where they are in V blank. Those of H32 are probably not accurate anywhere.
  • The estimates above: when registers are kept, DMA's first slots, where a change of name tables starts to count, and sprites after the display was off.
  • The interlace mode should be kept at the start of V blank. The setting that is not allowed is not interlaced here, and what it should do is not known.

Status says what was compared with the base and how. What VRAM holds was not compared.

From commit 1fd712ad794d. Edit this page