cvs2-naomi-dc

cvs2-naomi-dc is a decompilation of Capcom vs. SNK 2 for NAOMI and Sega Dreamcast, with ports available for PC and other platforms.

README

Capcom vs. SNK 2 - NAOMI and Dreamcast program source

AI Disclosure: Yes, this used Claude. The names in here are Capcom's wherever they could be found, so blame them for pl0c.c and pshl27_. The US PS2 version's program kept its symbols, which name about 9,700 of the routines and the fighters' files. The rest, and every struct and field, are ours, written in Capcom's style. Blame us for those.

What this is

This is the source code of Capcom vs. SNK 2, turned back from the machine code of three releases: the NAOMI arcade GD-ROM in its Japanese version, Millionaire Fighting 2001 (disc GDL-0007A, MAME's cvs2mf), and in its American one, Mark of the Millennium 2001 (disc GDL-0008, MAME's cvs2), and the Japanese Dreamcast disc, Capcom vs. SNK 2: Millionaire Fighting 2001 (T-1249M). One source tree builds all three, and each comes out byte for byte as it shipped:

SNKGD_SL.BIN   159,383,552 bytes   sha1 0c2b935ae3cfb6c85410a209fec4eab497066d84   the Japanese arcade game image, as the GD-ROM has it
SNKGD_SL.BIN   159,383,552 bytes   sha1 27186663ebae9e8c7206327ba012296b63c29568   the same, decrypted
SNKGD_SL.BIN   159,383,552 bytes   sha1 ce1af4eaf9add2cacfc9a0c728263d9e9c12c631   the American arcade game image, as the GD-ROM has it
SNKGD_SL.BIN   159,383,552 bytes   sha1 1258fc29838da2b7d1202474df2e71be9615350f   the same, decrypted
1ST_READ.BIN     2,387,249 bytes   sha1 9ffd21399fdf3dfcdacaddfc1bb6e0dc9cfc28a8   the Dreamcast disc's program

It is a decompilation: the machine code has been rewritten as C, and as SH-4 assembler where the original was assembler (the SH-4 is the CPU of both machines), so people can read it and change it. Most files are the same for all three. Where the Dreamcast differs, the file says so with #ifdef TARGET_DC, and where the American disc does, with TARGET_US; the changes the two later versions share are under CVS2_REV2.

It follows the Marvel vs. Capcom 2 decompilation, for the same arcade board and the same compiler, in its layout, its style and its tools, and takes its names where the two games share code.

The same C also runs elsewhere. ports/ compiles it into PC, Linux, 3DS, PSP and MiSTer versions of the game, which play from your own disc.

This is release 0.1: all three programs build byte for byte, and the ports play all three (the PSP the Dreamcast's alone).

For a tour of how the game itself works (the frame loop, inputs, grooves, ratios, special moves, hit boxes, damage, the computer's play), read docs/HOW_THE_GAME_WORKS.md.

Choose your version

To build a program that is byte for byte the original:

These need Windows, Hitachi's compiler and Sega's SDK libraries. None of them can be included here, so the sections below say where to find them.

To build a port, which plays the game on another machine:

These need only the ports' own tools (ports/README.md says which). The releases have them ready to play.

To play as the arcade did, at the board's own speed (59.79 frames a second at 31 kHz, 59.01 at 15 kHz), with its pixels and the sound on time, see "As the arcade" in the PC's PLAYING.md and in ports/MISTER.md.

Building

The arcade build uses the original toolchain (Hitachi's SHC 5.1 Release 08, asmsh 4.0, lnk 6.0A and elf2bin) and Sega's NAOMI libraries: the compiler turns the C into machine code, the assembler does the same for the assembler files, and the linker joins the pieces into one program.

An arcade game on a GD-ROM is a disc with one big file on it, SNKGD_SL.BIN, which the NAOMI's DIMM board reads into its memory at power-on. The file is encrypted with DES, and the key is in the game's PIC, a small chip on the DIMM board. Decrypted, the file starts with two programs, the game and the operator's test menu, and then the game's data (the pictures, models, sound and the rest). Both programs are built from this source, and so are the fighters' animations, boxes, attacks and the computer's play (data/). This section builds the Japanese disc's; the next says what the American disc's build changes. The rest of the data is not part of it: the build takes it from your own disc.

You need Windows (the compiler's tools are Win32 programs), Python 3 with pycryptodome (pip install pycryptodome), your own disc and PIC, and these from Sega's SDKs. The Internet Archive has the NAOMI SDK as the item NaomiSDK:

  • archive/hitachi/pcv51r08.zip - the compiler, sha1 104e78c75f83a70b023850daa1ea3b729e852ab2
  • archive/hitachi/pcv51r01.zip - an older release of it, which built the sound library, sha1 d219742f1388d67db84252bebc25739d4384f191
  • archive/naomi/libwin.zip - Sega's NAOMI libraries, sha1 a8723959364f9793d0185a73889d64706108b2d1

and the Internet Archive item dc_sdks has the Dreamcast's SDK, whose libraries the NAOMI shares:

  • "Sega Katana Development Software - Release 10.zip", sha1 6b063a04e543f851ace49e5aef49488e4d976ce4: its Lib folder (Shinobi, Kamui2 and the compiler's run-time library). The zip holds a disc image with an installer; install it (to C:\KATANA).

Then:

  1. Put your gdl-0007a.chd and the PIC, 317-5078-com.pic, in rom/: the PIC as it is, or in a zip of the set (cvs2.zip, cvs2mf.zip or cvs2gd.zip). The CHD may also be in MAME's folder of the set (rom/cvs2mf/).

  2. Unpack the zips, and tell the build where they are with three variables, all of which it needs: SHC is the compiler's bin folder, SHC_5_1R01 the older release's, and SDK_LIBS is four library folders, in this order:

    set SHC=C:\pcv51r08\bin
    set SHC_5_1R01=C:\pcv51r01\bin
    set SDK_LIBS=C:\libwin\lib\zero;C:\libwin\lib;C:\libwin\libsrc\libjamma\zero;C:\KATANA\Lib
    

    ROM_DIR (the folder of your disc, rom if unset) and PYTHON (python if unset) may be set too.

  3. Run build.bat (or make). make takes the same variables, and they can go on its command line, which is surer than the environment when a make from MSYS2 is run from another shell:

    make SHC=C:/pcv51r08/bin SHC_5_1R01=C:/pcv51r01/bin "SDK_LIBS=C:/libwin/lib/zero;C:/libwin/lib;C:/libwin/libsrc/libjamma/zero;C:/KATANA/Lib"
    

The build first takes two things from your disc (tools/cvs2rom.py): the game's data after the fighters', and 8 KB of Z80 code. The NAOMI has a small chip of its own (315-6149) between the game and the cabinet's controls and coin slots, with a Z80 inside. It has no program until the game gives it one, so Sega's library carries the Z80 program as a block of data and sends it to the chip at start-up. It is Sega's, and it is not SH-4 code, so it is not written out here.

Then it builds the two programs, puts the image together, and checks it. It prints a SHA-1 line for each of these:

build/cvs2.bin               1,623,468 bytes   sha1 c3a46a43f54d3002ffd1cf7b33774e874d90d1fb   the image's head and the game program
build/cvs2test.bin             222,544 bytes   sha1 a12c0ea32ef417ed241ae96e3a903014ad43df05   the test program
build/SNKGD_SL.BIN         159,383,552 bytes   sha1 27186663ebae9e8c7206327ba012296b63c29568   the image, decrypted
build/gdrom/SNKGD_SL.BIN   159,383,552 bytes   sha1 0c2b935ae3cfb6c85410a209fec4eab497066d84   the image encrypted again, as the GD-ROM has it

The last is the decrypted image encrypted again with your PIC's key, so it is the disc's own file.

rom/ can hold SNKGD_SL.BIN (as the disc has it, or already decrypted) in place of the CHD, and 317-5078-com.pic in place of the zip. ROM_DIR names another folder. The CHD MAME knows as gdl-0007a has the SHA-1 2c7969edc7ce9af1101d4803b47b321dc05503e8 (what chdman info calls its SHA1), and the PIC is 16,384 bytes, sha1 0882d0e12ca4fb81dda2268cd12724a10278c220.

No other version of the compiler or the libraries will do. Sega's libraries are part of the program, and most of their modules are linked straight from these zips. The modules that the zips only have in a later version than the one Capcom linked are built from src/lib.

The American version

The American disc, GDL-0008, came a month after the Japanese one (its program says August 4, 2001, against July 5). Its test program and Sega's libraries are the same; its game program has 236 routines changed (27 of them only in a value they load), 22 new and one left out, in 73 of Capcom's files, most of them the way the Dreamcast version has them, and the data of 18 of its 48 fighters differ. The source builds it with TARGET_US (and CVS2_REV2, the changes it shares with the Dreamcast version).

It takes the same zips, the same three variables (SHC, SHC_5_1R01 and the four folders of SDK_LIBS) and the same PIC as the Japanese build. Put your gdl-0008.chd in rom/ (or MAME's folder of the set, rom/cvs2/), beside the PIC, and run build.bat us (or make us). It prints:

build/cvs2us.bin                1,626,828 bytes   sha1 8d14c4f59472643b65b5d519a8efee73e2e5c0e2   the image's head and the game program
build/cvs2test.bin                222,544 bytes   sha1 a12c0ea32ef417ed241ae96e3a903014ad43df05   the test program, the Japanese disc's
build/us/SNKGD_SL.BIN         159,383,552 bytes   sha1 1258fc29838da2b7d1202474df2e71be9615350f   the image, decrypted
build/us/gdrom/SNKGD_SL.BIN   159,383,552 bytes   sha1 ce1af4eaf9add2cacfc9a0c728263d9e9c12c631   the image encrypted again, as the GD-ROM has it

The CHD MAME knows as gdl-0008 has the SHA-1 a45c7df4ca6692ff0d1a34f8269e17760fbd3a04 (chdman info's SHA1).

The Dreamcast version

The same source also builds the Dreamcast version, the Japanese disc's program. It is the same game with a home menu around it: options, a training mode, survival, replays, the colour and groove edits, a memory card save, a link to the Neo Geo Pocket, and a network game over the modem, with a lobby, rankings and a software keyboard. Capcom built it from the arcade's source in the summer of 2001, so most of the files are the arcade's own.

A file that differs between the two says so with #ifdef TARGET_DC. The files only the home version has are in src/dc, and the Dreamcast's own builds of Sega's library modules (and of the network libraries Sega's Dreamcast games took from ACCESS and others) are in src/dc/lib. Every routine comes out as the disc's exact bytes:

1ST_READ.BIN    2,387,249 bytes   sha1 9ffd21399fdf3dfcdacaddfc1bb6e0dc9cfc28a8

To build it you need the arcade's zips and these, for the libraries only the Dreamcast has:

  • from the item NaomiSDK: archive/hitachi/pcv5r28.zip, sha1 fb6c49b2c8dba63e26ea59f41bdda080f4872217, the compiler release whose C++ compiler (shcpp.exe) built Sega's modem library
  • from the item dc_sdks:
    • "Sega Katana Development Software - Release 9.zip", sha1 a8b0d630851bd90b39dceb65cd2957bd13208383: its Lib folder (sh4nlfzz.lib). Install it to a folder of its own (C:\KATANA_R9).
    • "Sega Katana Development Software - Release 11b.zip", sha1 d74b6a0e9e8b7cfc22a930579a1413bce5139cb0: its Lib folder (modem.lib, a later build of the disc's modem library, from which two modules link). The zip holds Sega's installer (Setup.exe), which puts Lib in the folder it installs to. That is C:\KATANA unless you change it, so give it a folder of its own (C:\KATANA_R11b).
    • "Dreamcast SDK (Sega Library) Ver.1.55J (Vol.2) (For SHC Users) (Japan)_redump.zip", sha1 8f972a622c550dad7d22c928870c5ebca2aa4d24: a CD image; its folder optionlib/drepass/lib (sg_dpldr.lib)
  • naomi_dc_000920.zip, sha1 4db29373afb7a1e576c59a4f8f567f392cc8f2b3: its adxlib/adx593.LZH (7-Zip opens it), CRI's ADX 5.93, whose lib/sh4 folder has cri_adxn.lib
  • kunoichi202.zip, sha1 e7576a9519a42b4282d9ba866d27602f457950a1, Sega's SDK 2.00F: its lib.kn folder (the disc's ADX 5.94 and Sofdec, adx_sofdec_mwk2.lib)

Keep SHC and SHC_5_1R01 as the arcade's build has them (the Dreamcast's sound library is the arcade's), set SHC_5_0R28 to the bin folder of pcv5r28.zip, and SDK_LIBS to nine folders: the arcade's four, then ADX 5.93's, Katana R9's, Sega Library 1.55J's, Kunoichi's and Katana R11b's:

set SHC=C:\pcv51r08\bin
set SHC_5_1R01=C:\pcv51r01\bin
set SHC_5_0R28=C:\pcv5r28\bin
set SDK_LIBS=C:\libwin\lib\zero;C:\libwin\lib;C:\libwin\libsrc\libjamma\zero;C:\KATANA\Lib;C:\adx593\lib\sh4;C:\KATANA_R9\Lib;C:\sl155j\optionlib\drepass\lib;C:\kunoichi202\lib.kn;C:\KATANA_R11b\Lib

and run build.bat dc (or make dc, with the four on its command line). The build writes build/1ST_READ.BIN and checks its SHA-1. It needs nothing from your disc. (The arcade's build is the same with nine folders as with four.)

Things that were learned on the way

  • No text was left behind. Marvel vs. Capcom 2's cartridges still hold pieces of what was on the developers' PC (a list of source files, pages of a linker map, the compiler's options). A GD-ROM is made from files, so this disc has nothing of the kind, and neither has the Dreamcast's. The names and the files' split came from the code itself (see Names).
  • A module only matches from its start. The compiler puts the constants a routine needs in pools in the code, and a pool can hold the constants of the routines before it in the same file. So each file had to be written from its first routine, in the order the program has them, and where a pool stops being shared is where a file ends.
  • A routine can be too big to optimise. At about 256 branches in one routine, SHC gives up on it ("Function not optimized") and keeps every local in memory. The code then only matches when the C has the same number of branches as Capcom's.
  • Some of the fake matches are the disc's. A few routines here only match with a statement that never runs (if( 0 ) ... ;, marked "left out of the build"). The compiler still counts what it reads and writes, so it can change which register a value gets. These are the routines where nothing more natural has been found yet, and they are a good place to start contributing.
  • Not every library was built with the same compiler. The sound library (libsnd 1.05a, March 2001) was built with SHC 5.1 Release 01, not Release 08, and the Dreamcast's modem library is C++, built with SHC 5.0 Release 28's shcpp. A folder whose file shc names a release is built with that release.
  • The Dreamcast's network libraries are in no SDK. No SDK has the disc's versions of the PPP, TCP/IP and modem libraries (ACCESS's ntPpp and ntTcp 1.07, the modem's REV 1.0309, mdmAPI 4.06). Katana Release 10's share most of their modules, and link from it; the others were decompiled. ACCESS built theirs with -extra=a=800, which starts each pool and the code after it on a cache line.

Layout

src/           game code, a file for each of Capcom's modules
src/lib/       the modules of Sega's libraries that the SDK has no matching version of (see Sega's libraries)
src/test/      the test menu's own code
src/dc/        the files only the Dreamcast version has (src/dc/lib: its builds of Sega's and the
               network libraries' modules)
include/       headers; player.h includes the shared ones (pl_work.h has PLAYER_WORK)
data/          the fighters' data, a file for each part of each fighter's pack (tools/chrpack.py packs them)
docs/          HOW_THE_GAME_WORKS.md: a tour of the game's code
cvs2.sub       link order and section addresses (cvs2test.sub for the test menu, cvs2us.sub for the
               American disc, cvs2dc.sub for the Dreamcast)
functions.tsv  every routine of the game program: address, size, name, file (functions_test.tsv: the
               test menu's; functions_us.tsv: the American disc's; functions_dc.tsv: the Dreamcast's)
tools/         cvs2rom.py (with chd.py): reads your disc; chrpack.py: packs the fighters' data
rom/           your disc and PIC go here
symbols.txt    every named address of the arcade's game program (symbols_test.txt: the test menu's,
               symbols_us.txt: the American disc's, symbols_dc.txt: the Dreamcast's)
pointers.tsv   the words of the arcade's program that hold addresses, and those addresses
               (pointers_test.tsv, pointers_us.tsv, pointers_dc.tsv: the others'), which a port needs
               to load the program's data
ports/         the ports

A character's code is src/plNN.c, where NN is the character's number in hex (pl00.c is Ryu, pl0c.c Cammy, character 12). Its shots are in pshlNN.c, its other effects in plsNN.c and shlNN_*.c (these numbered in decimal), and the code all the fighters share in pl_*.c.

Names

The arcade and Dreamcast discs have no source text, symbols or map. The US PS2 version's program does: it was built with its symbols left in, which name every routine but give no types or fields. Its routines were matched to these by their shape (the tables, calls and data they share, their size and order), so the names come from four places, in this order:

  • The PS2 version's. Capcom's own names, for about 9,700 of the routines and for the files it names whole: the fighters (pl00.c to pl2f.c), their shots (pshlNN.c) and effects (plsNN.c, efctNN.c), and the stages (st00.c to st0a.c).
  • Marvel vs. Capcom 2's. Capcom's text left in that game's cartridges gave its decompilation Capcom's own names (PLAYER_WORK, routine_2, pl_char_set, Sys). The two games share some code (much of the system's, a little of the memory card's, and Sega's libraries), and a lot of the shape of their works, so those have the same names here.
  • Street Fighter III: Third Strike's. The fighters' code follows Capcom's older CPS engine more than Marvel vs. Capcom 2 does, and Third Strike's symbols name the same things (vital_old, the red part of the vitality bar).
  • What the code does. Everything else is named for what it does, in the same style. The game's own debug screens print a few names, which are used too. A field we don't understand yet is called unk_ and its offset.

The other file names are ours, in the style of Marvel vs. Capcom 2's (pl_main.c, s_play.c).

Compiler

Two versions of the same compiler can turn the same C into different machine code, and so can two sets of options. This game's own options are not known, but Marvel vs. Capcom 2's were left in its ROM, and they build this game's code exactly:

-cpu=sh4 -endian=little -fpu=single -round=nearest -macsave=0 -pic=0 -optimize=1 -size -string=const -extra=a=400

-extra=a=400 is not in the manual. It makes the assembler stage put a nop after each mov Rm,R0, and this game's code has those too.

With these options, the compiler builds all the C here to the exact bytes: the Japanese arcade game program (about 11,300 routines, 9,800 of them Capcom's, in 285 files), its test menu, the American one, and the Dreamcast's program (about 15,600 routines; the game's in 358 files, 75 of them only the home version's). Nothing is patched afterwards. The few assembler files (.src) are ones that were assembler to begin with: the start-up, the cache routines, the image's head, and some of Sega's library modules.

Not every file saw the same headers. The machine code shows how each file thought a routine was declared, and for a routine or two the files do not agree (include/player.h says where). Most likely a prototy