#!/bin/sh # SPDX-License-Identifier: GPL-2.0-or-later # # Build megadrive-python's closed-box referee, Nuked-MD, headless, from a pinned source # tree that is never committed anywhere of ours: # # sh build_nuked_md.sh fetch Nuked-MD at the pin, build # NUKED_MD_SRC=/path/to/Nuked-MD sh build_nuked_md.sh build from that tree instead # sh build_nuked_md.sh --version print the tool, version and pin # # With NUKED_MD_SRC unset (from 2026-11-24) the script clones Nuked-MD into upstream/Nuked-MD # beside it (git-ignored) at the pin, 594ca2a, or checks an existing clone out at the pin; a # clone with local changes stops it, and it never deletes anything. NUKED_MD_SRC, when set, # names a tree to build instead, used as it is: megadrive-python's scripts/nuked_md.py passes # its own fetched copy (reference/src/Nuked-MD, from its scripts/fetch_references.py). The # binary lands in build/nukedmd-headless beside this script (NMD_BUILD_DIR to put it # elsewhere). Exit status as the family's tool contract says: 0 built, 3 no Nuked-MD tree # (or the fetch failed), anything else a failed build. # # Nuked-MD is GPL-2.0 (nukeykt, ogamespec, andkorzh). This tool links it, so the tool # is GPL, GPL-2.0-only as built (README.md, ../../LICENSE). Its own two files: # # nuked_md_headless.c the three back-end functions md.c calls (Audio_*, Video_*) # plus the pin tracer # nuked_md_sdl.h a stand-in for the SDL2 declarations md.c and controller.c # use, so no SDL is needed # # The binary is built locally and run locally; it is not distributed. # # Two build switches, both deliberate: # # -DTMSS_ENABLE=0 Nuked-MD's FC1004 carries TMSS. With TMSS enabled the bank # register (tmss.c, dff3) powers up *clear* and the cartridge is # decoded only after "SEGA" reaches $A14000, which no pre-TMSS # test ROM writes. With it 0 the same flip-flop powers up set # (SDFFS instead of SDFFR), which is the pre-TMSS board -- the # Model 1 VA7 the README calls the closest retail machine is # itself a TMSS board, so this is one of the referee's named # departures from any real console and is recorded as such. # -w Nuked-MD is built with warnings off upstream too; this tree's # gcc 15 would otherwise emit thousands of them. # # Built and used with gcc 15.2.0 (Ubuntu 15.2.0-16ubuntu1) on 2026-09-23, against # Nuked-MD @ 594ca2a ("FJ3002 FM wip"), the revision megadrive-python's # scripts/pinned_repos.json pins. set -e here=$(cd "$(dirname "$0")" && pwd) TOOL=nuked-md VERSION=0.2.0 URL=https://github.com/nukeykt/Nuked-MD PIN=594ca2a494104df559a907db7c8f5602a93b2078 if [ "${1:-}" = "--version" ]; then echo "$TOOL $VERSION (Nuked-MD ${PIN%"${PIN#???????}"})" exit 0 fi if [ $# -gt 0 ]; then echo "usage: sh build_nuked_md.sh [--version] (NUKED_MD_SRC, NMD_BUILD_DIR, CC, CFLAGS from the environment)" >&2 exit 2 fi # NMD_BUILD_DIR builds elsewhere (an experiment), leaving the tools' binary alone. out="${NMD_BUILD_DIR:-$here/build}" if [ -n "${NUKED_MD_SRC:-}" ]; then src=$NUKED_MD_SRC else src=$here/upstream/Nuked-MD mkdir -p "$here/upstream" if [ ! -d "$src/.git" ]; then git clone --quiet "$URL" "$src" || { echo "could not clone $URL" >&2; exit 3; } fi if [ "$(git -C "$src" rev-parse HEAD)" != "$PIN" ]; then if [ -n "$(git -C "$src" status --porcelain)" ]; then echo "$src has local changes and is not at $PIN; move it aside first" >&2 exit 1 fi git -C "$src" fetch --quiet origin || { echo "could not fetch $URL" >&2; exit 3; } git -C "$src" -c advice.detachedHead=false checkout --quiet "$PIN" fi if [ -n "$(git -C "$src" status --porcelain)" ]; then echo "$src has local changes; move it aside first" >&2 exit 1 fi fi if [ ! -f "$src/md.c" ]; then echo "no Nuked-MD tree at $src (unset NUKED_MD_SRC to fetch the pinned one," >&2 echo "or point it at a Nuked-MD checkout at $PIN)" >&2 exit 3 fi mkdir -p "$out" "$out/sdl-stub" # md.c and controller.c say #include "SDL.h"; give them ours, on the include path # ahead of anything else, without touching the referee's tree. cp "$here/nuked_md_sdl.h" "$out/sdl-stub/SDL.h" CC=${CC:-gcc} CFLAGS=${CFLAGS:--O2 -w -Wno-incompatible-pointer-types -DTMSS_ENABLE=0} echo "nukedmd-headless (Nuked-MD @ $(cd "$src" && git rev-parse --short HEAD 2>/dev/null || echo 594ca2a))" # savestate.c's load_state is renamed so that the driver's own can wrap it (it re-aims # the chip's internal pointers after a load; see nuked_md_headless.c). $CC $CFLAGS -I "$out/sdl-stub" -I "$src" -Dload_state=nmd_inner_load_state \ -c "$src/savestate.c" -o "$out/savestate.o" $CC $CFLAGS -I "$out/sdl-stub" -I "$src" -o "$out/nukedmd-headless" \ "$here/nuked_md_headless.c" \ "$src/md.c" "$src/68k.c" "$src/z80.c" "$src/fc1004.c" "$src/vdp.c" \ "$src/fm.c" "$src/arbiter.c" "$src/iochip.c" "$src/tmss.c" "$src/vram.c" \ "$src/cartridge.c" "$src/controller.c" "$out/savestate.o" "$src/fj3002_fm.c" -lz echo "built $out/nukedmd-headless" printf '{"tool": "%s", "tool_version": "%s", "compiler": "%s", "cflags": "%s", "built": "%s", "upstream": "%s", "source": "%s", "patches": []}\n' \ "$TOOL" "$VERSION" "$($CC --version | head -n 1)" "$CFLAGS" "$(date -u +%Y-%m-%dT%H:%M:%SZ)" \ "$(git -C "$src" rev-parse HEAD 2>/dev/null || echo unknown)" \ "$([ -n "${NUKED_MD_SRC:-}" ] && echo NUKED_MD_SRC || echo fetched)" > "$out/build-info.json"