emuforge

The Cx4

The Cx4 is a processor for mathematics, a Hitachi HG51B169. Mega Man X2 and X3 have one. It has no program of its own. It runs programs from the game's ROM, in pages of 256 instructions. It also has a ROM with six tables of numbers, such as sines. Here, these are made from formulas.

It is in crates/snes-cx4, and files below are in its src. What is here rests on three sources (credits). Where they differ, lib.rs says what each one has:

Used for
Overload and Segher's "Capcom CX4"the registers, the instructions and their flags, which they tested on the chip
ikari_01's notes and measurementstimes, the cache, DMA, the main processor's registers, and who has the cartridge when. They were measured on a console
fullsnesthe registers and the formulas of the tables. What it says about the games' own programs was not used

Time

The chip has a crystal of its own, of 20 MHz. On NTSC, 176 of its cycles are 189 master clocks. How both lie beside each other when the console is turned on cannot be known, so both start at 0.

Everything happens in one cycle, and then takes its time. An instruction happens in its first cycle, and moving a byte does too. A byte that the program asks for from the cartridge arrives when its wait is over. When the chip has run up to a time, all that began before it has happened, and nothing else. So the main processor never sees anything early. What was begun last can still take time after that: at most 8 cycles for an instruction, or 16 for a byte of a DMA.

Cycles (timing.rs)
Most instructions, and one that is skipped1
A jump, a call or a return that is taken. Loading a page comes on top, which is a guess3
A byte from or to the cartridge1, and the waitstates of the ROM or the RAM (0 to 7 each)
A byte of a DMA between the chip's RAM and the cartridgethe same
A byte of a DMA from the ROM to the cartridge's RAMboth, one after the other
A page of 256 instructions512 times a byte from the cartridge

ikari_01 measured nine programs on a console. A test has them, and gives these times within 0.21 µs. Most of his numbers have one digit after the point. fullsnes calls the chip's timing unknown. Where nothing was measured, each thing takes one cycle, and waitstates come on top. That is a guess for what the main processor's registers start, for the instructions that he did not time, and for a write to the register for the place in the program.

The processor

It has an accumulator with 24 bits, four flags, 16 registers of 24 bits (R0 to R15), and registers for special things. One has the address on the cartridge's bus, and others have the two halves of a product of 48 bits, or the bytes to and from the memories. There are also 16 fixed numbers. A stack has room for 8 entries, and then overwrites its first one. The chip has 3 KB of RAM.

A program runs only from the cache, which has two halves of one page each:

  1. A jump, call or return to another page first looks if one of the halves has it.
  2. If not, it is loaded while the program waits. It goes to the half that the program is not in. If that one is locked, it goes to the program's own.
  3. If both are locked, the program stops.
  4. At the end of half 0, the program goes on in half 1, with the page that register P has. At the end of half 1, it stops.

A half has a page if it was loaded from the same address. That is a guess. When the place of the first page in the cartridge is changed, all pages have to be loaded again.

Instructions that are not known do nothing for one cycle. The chip counts them and keeps the last one, for reports (Console::unknown_to_chip). In 12 releases of both games, none came up in 90 seconds.

The tables

The chip's ROM has 1,024 numbers of 24 bits. No copy of it was used here, and nothing was compared with one. fullsnes has a formula for each of the six tables, and each one's first and last number. tables.rs works them all out when the emulator is compiled, with whole numbers only, so every computer gets the same:

NumberFormula
$000-$0FF$800000 / n, and $FFFFFF for 0
$100-$1FF$100000 × √n
$200-$27F$1000000 × sin(n × 90° / 128)
$280-$2FF$800000 × arcsin(n / 128) / 90°
$300-$37F$10000 × tan(n × 90° / 128)
$380-$3FF$1000000 × cos(n × 90° / 128), and $FFFFFF for 0

What is after the point is left out. With that, the twelve numbers that fullsnes shows are right. Rounding to the nearest would make the last one of four tables wrong. For all other numbers, no source says that, so one of them can be wrong by 1. Other tests show that no number is so near a whole one that the errors of the calculation could matter, and that each is at most 1 away from what a computer's floating-point numbers give.

What the main processor sees

AddressWhat is there
$00-$3F, $80-$BF: $6000-$6BFF, and again at $7000the chip's RAM
the same banks: $7F40-$7FFFregisters
the same banks: $8000-$FFFFthe ROM: 2 MB, as LoROM
$70-$77: $0000-$7FFFthe cartridge's RAM, if it has any. It reads as 0 if not
the rest of $40-$7D, and $C0-$FFopen bus
Register
$7F40-$7F46DMA: from where (3 bytes), how many bytes (2), and the lower bytes of where to (2)
$7F47DMA: the bank of where to. Writing it starts the DMA
$7F48which half of the cache to load. Writing it loads that half
$7F49-$7F4Bwhere the first page of the program is
$7F4Cwhich halves of the cache are locked
$7F4D-$7F4Ethe page to start or to load
$7F4Fthe instruction to start with. Writing it starts the program
$7F50waitstates: bits 6-4 for the ROM, and 2-0 for the cartridge's RAM. Both are 3 when the console is turned on
$7F51with bit 0, the interrupt ends and no new one comes
$7F53read: bit 7 says that the chip uses the cartridge, 6 that it is busy, 1 that an interrupt waits, and 0 that it is suspended. Writing it stops everything
$7F55-$7F5Dsuspend it ($55), or for 32, 64 ... 224 cycles ($56-$5C), or let it go on ($5D)
$7F5Ewriting it clears bit 1 of the status, but not the interrupt
$7F60-$7F7Fwhat the main processor reads as vectors while the chip is busy
$7F80-$7FAF, and again at $7FC0R0 to R15, three bytes each

Who has the ROM and the RAM

One of both has the cartridge at a time. The chip has it while it loads a page, does a DMA, or waits for a byte that its program asked for. Then the main processor reads 0 in the ROM and in the cartridge's RAM, and cannot write to that RAM. (That it cannot write is a guess.) While the chip only calculates, or is suspended, the main processor can use both.

While the chip is busy, its registers take the place of the end of bank $00. Addresses $00:FFE0 to $00:FFFF give $7F60 to $7F7F, and $00:FFC0 to $00:FFDF give $7F40 to $7F5F. So a game can put a vector there that goes to its main memory. An interrupt then works, even if the chip has the ROM. The bus reads vectors from these addresses as it reads anything else, so this needs nothing special.

While its program runs, or a DMA uses its RAM, $6000 to $7F3F reads as $FF, and the chip's RAM cannot be written.

Its interrupt comes when a program, a load of a page, or a DMA ends, unless bit 0 of $7F51 is set. It stays until the game writes a 1 there.

When the chip is quiet, because it does nothing and no byte is on its way, only a write to its registers can change that. Then the core reads the ROM without looking at the interrupt again. If it is busy and its interrupt is on, it says when that could come. For a load or a DMA, this is the earliest possible end. While a program runs, it is the next instruction, as any one could be the last.

For example: bytes for the chip, step by step

A game has 16 numbers of one byte in its ROM, at $00:8000. It wants them in the chip's RAM, and then an interrupt:

Main processorThe chip
1writes 0 to $7F51, so that interrupts can come
2writes $00, $80, $00 to $7F40-$7F42 (from $00:8000), 16 and 0 to $7F43-$7F44, and $00, $60 to $7F45-$7F46keeps them
3writes $00 to $7F47, the bank of where tostarts the DMA, in the next of its cycles. It has the cartridge now, and its RAM is hidden
4goes on in main memory. In the ROM, it would read 0 as its next instructioncopies a byte from the ROM to its RAM in each 4 cycles: one, and 3 waitstates
5after 64 cycles, which is 3.2 µs, it has nothing left to do. It gives the cartridge back, and its interrupt begins. The bus knew this time as the chip's next event. In a test, this is 69 master clocks after step 3
6enters its IRQ after the instruction that sees it. Its program reads TIMEUP ($4211), finds that it was not the console's own timer, and writes 1 to $7F51the interrupt ends

The 16 bytes are now at $6000. A program that is started through $7F4F ends in the same way.

What is a guess, and what is not there

lib.rs has the whole list. The largest are:

  • What happens at the start of a program: the registers are copied. If no half has its page, half 0 is loaded, or half 1 if 0 is locked.
  • When a program starts an access before the last one is done, it waits for it. ikari_01 says that the chip crashes then.
  • Where a DMA cannot be done, such as from the RAM to the same RAM, the chip stays busy until $7F53 is written. ikari_01 says this, and that it is looked at for each byte is a guess.
  • What its program reads where the cartridge has nothing is 0. Only three instructions use the cartridge.
  • What is not known when the console is turned on is 0. A reset keeps the chip's RAM and nothing else.
  • $7F52 chooses how two ROM chips are used. No header says that there are two, so it does nothing here.
  • A pin that chooses a HiROM map, and one that takes the ROM and RAM away from the chip, are not there. No game that is known uses the first.

Each instruction has tests with values that were worked out by hand. Other tests run random programs in pieces of any length, and check that the time of the next interrupt is never too late.

From commit 019fda0af379. Edit this page