Files
RCM-Emulator/Makefile
T
Claude d661f2738c Model USB device mode, with an emulated PC on the cable
Both USB controllers were plain register caches: USBCMD.RESET never
cleared, so bdk's usb_device_init() timed out and none of Nyx's USB
tools could even wait for a cable.

- The Erista USB2 controller (USB1, 0x7D000000, bdk usbd.c): queue
  heads at ENDPOINTLISTADDR (bdk's are in the controller at +0x1000),
  transfer descriptors in DRAM, ENDPTPRIME / ENDPTSTATUS /
  ENDPTCOMPLETE, SETUP packets in queue head 0 with ENDPTSETUPSTAT,
  a self-clearing controller reset and the PHY's clock-valid bit. Byte
  and halfword accesses work, since bdk memcpy()s SETUP packets out.
- The Mariko XUSB device controller (0x700D0000, bdk xusbd.c): endpoint
  contexts and TRB rings with Link TRBs and cycle bits, the doorbell, a
  two-segment event ring with Port Status Change, Setup and Transfer
  events, EP_HALT / EP_STCHG, EP_RELOAD, and PORTSC's write-1-to-clear
  changes and link-state writes.
- The host (--usb-host, [usb] host, or the config window) plays the PC
  on whichever controller attaches: debounce, bus reset at high speed,
  enumeration with SET_ADDRESS recovery, then after 100 ms for its class
  driver to bind - bdk's XUSB driver stalls an early GET_MAX_LUN - the
  class. For mass storage it runs Bulk-Only Transport: GET_MAX_LUN,
  INQUIRY, TEST UNIT READY with REQUEST SENSE for the unit attention,
  READ CAPACITY, READ(10) of the first 64 and last 8 blocks with their
  CRC32s, checked against the SD image when the size matches, then allow
  removal, eject, and poll until the gadget detaches. For HID it reads
  the report descriptor, sets idle 0 and polls the interrupt endpoint.
  It only reads, and it runs on the controllers' register accesses, so
  runs repeat. [usb-host] lines log it; RCM_USB_TRACE=1 logs every
  descriptor, TRB and doorbell.

With hekate 6.5.3, Nyx's SD card mass storage enumerates as 11EC:A7E0,
reads back identical to the SD image and ends at "Disk ejected" on both
Erista and Mariko; the gamepad enumerates and serves its report
descriptor (with no Joy-Cons it has nothing to report). Without a host
Nyx waits and reports a timeout, as with no cable.

tests/usb/ runs a mass storage gadget over a RAM disk on each controller
and a HID gadget on the USB2 one, checks what the host logs - the
enumeration, the SCSI commands, CRC32s equal to the payload's, the eject
and the detach - and a run with nothing on the cable.

The Windows pread/pwrite shim moves from mmio.cpp to platform.h, as the
host reads the SD image too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01YYewwDPujjo5B2xRyC5Grb
2026-09-26 10:25:29 +00:00

131 lines
5.3 KiB
Makefile

# RCM Payload Emulator
# ARM32 emulator for Switch RCM payloads using Unicorn Engine + SDL2
SRC_DIR = . t210 display
SRCS = $(foreach dir,$(SRC_DIR),$(wildcard $(dir)/*.cpp))
# Vendored Dear ImGui (see third_party/imgui/). Listed explicitly so we don't
# accidentally pick up imgui_demo.cpp etc. if they're added later.
IMGUI_DIR = third_party/imgui
IMGUI_SRCS = \
$(IMGUI_DIR)/imgui.cpp \
$(IMGUI_DIR)/imgui_draw.cpp \
$(IMGUI_DIR)/imgui_tables.cpp \
$(IMGUI_DIR)/imgui_widgets.cpp \
$(IMGUI_DIR)/backends/imgui_impl_sdl2.cpp \
$(IMGUI_DIR)/backends/imgui_impl_sdlrenderer2.cpp
SRCS += $(IMGUI_SRCS)
OBJS = $(SRCS:.cpp=.o)
OUTPUT = rcm_emu
CXX = g++
# -MMD -MP emits .d sidecar files with each .o so a header change forces a
# rebuild of every TU that includes it. Avoids stale-layout bugs when EmuState
# (or any other shared header) gains a new field.
CXXFLAGS = -Wall -g -O2 -std=c++17 -MMD -MP -I. -I$(IMGUI_DIR) -I$(IMGUI_DIR)/backends
LDFLAGS =
LIBS = -lunicorn -lSDL2 -lpthread
# pkg-config for SDL2 if available
SDL2_CFLAGS := $(shell pkg-config --cflags sdl2 2>/dev/null)
SDL2_LIBS := $(shell pkg-config --libs sdl2 2>/dev/null)
ifneq ($(SDL2_CFLAGS),)
CXXFLAGS += $(SDL2_CFLAGS)
LIBS = -lunicorn $(SDL2_LIBS) -lpthread
endif
# ---- Windows cross-build ---------------------------------------------------
#
# `make windows` builds rcm_emu.exe with the MinGW toolchain. Both deps come
# from the cross sysroot the container prepares (see Dockerfile.windows):
# SDL2 from its official MinGW devel tree, unicorn built from source.
#
# Statically linked on purpose. A .exe that needs libstdc++, libgcc, winpthread
# and SDL2.dll beside it is not something anyone wants to download from a
# release page, and SDL2 is linked static here too so there is exactly one
# file to ship.
#
# SDL on Windows renames main to SDL_main and supplies its own entry point, so
# the link order -lmingw32 -lSDL2main -lSDL2 matters and SDL2main must come
# before SDL2. The trailing Windows libraries are what static SDL2 and unicorn
# pull in: without them the link fails on missing WinMM / OLE / socket symbols.
WIN_CXX = x86_64-w64-mingw32-g++
WIN_OUT = rcm_emu.exe
WIN_OBJS = $(SRCS:.cpp=.win.o)
WIN_CXXFLAGS = -Wall -g -O2 -std=c++17 -MMD -MP -I. -I$(IMGUI_DIR) -I$(IMGUI_DIR)/backends -I/usr/x86_64-w64-mingw32/include/SDL2 -DSDL_MAIN_HANDLED
WIN_LIBS = -lmingw32 -lSDL2main -lSDL2 -lunicorn -lpthread -lwinmm -limm32 -lole32 -loleaut32 -lversion -lsetupapi -lcfgmgr32 -lcomdlg32 -lgdi32 -lrpcrt4 -lws2_32 -luuid -lshell32 -ladvapi32 -luser32
WIN_LDFLAGS = -static -static-libgcc -static-libstdc++
.PHONY: all clean windows test
all: $(OUTPUT)
windows: $(WIN_OUT)
$(WIN_OUT): $(WIN_OBJS)
$(WIN_CXX) $(WIN_LDFLAGS) -o $@ $^ $(WIN_LIBS)
@echo " built $@ ($$(stat -c%s $@) bytes)"
%.win.o: %.cpp
$(WIN_CXX) $(WIN_CXXFLAGS) -c -o $@ $<
-include $(WIN_OBJS:.o=.d)
$(OUTPUT): $(OBJS)
$(CXX) $(LDFLAGS) -o $@ $^ $(LIBS)
%.o: %.cpp
$(CXX) $(CXXFLAGS) -c -o $@ $<
# Pull in the header-dep .d sidecars produced by -MMD. Silenced on first build
# (or after `make clean`) since the files don't exist yet.
-include $(OBJS:.o=.d)
# ---- Regression tests --------------------------------------------------------
#
# `make test` boots the emulator headless on small self-contained payloads and
# checks what they report. Needs arm-none-eabi-gcc for the BPMP side (the
# AArch64 side of the CCPLEX test is pre-assembled words in the source).
TEST_CC = arm-none-eabi-gcc
TEST_OBJCOPY = arm-none-eabi-objcopy
TEST_CFLAGS = -marm -march=armv4t -O2 -ffreestanding -nostdlib -fno-builtin \
-Wall -Wextra
tests/ccplex/payload.bin: tests/ccplex/payload.c tests/ccplex/link.ld
$(TEST_CC) $(TEST_CFLAGS) -T tests/ccplex/link.ld -o tests/ccplex/payload.elf $<
$(TEST_OBJCOPY) -O binary tests/ccplex/payload.elf $@
tests/se/payload.bin: tests/se/payload.c tests/se/vectors.h tests/se/link.ld
$(TEST_CC) $(TEST_CFLAGS) -T tests/se/link.ld -o tests/se/payload.elf $<
$(TEST_OBJCOPY) -O binary tests/se/payload.elf $@
USB_PAYLOADS = tests/usb/payload_1.bin tests/usb/payload_2.bin tests/usb/payload_3.bin
tests/usb/payload_%.bin: tests/usb/payload.c tests/usb/link.ld
$(TEST_CC) $(TEST_CFLAGS) -DSCENARIO=$* -T tests/usb/link.ld -o tests/usb/payload_$*.elf $<
$(TEST_OBJCOPY) -O binary tests/usb/payload_$*.elf $@
DISPLAY_SCENARIOS = 1 2 3 4 5 6
DISPLAY_PAYLOADS = $(DISPLAY_SCENARIOS:%=tests/display/payload_%.bin)
tests/display/payload_%.bin: tests/display/payload.c tests/display/link.ld
$(TEST_CC) $(TEST_CFLAGS) -DSCENARIO=$* -T tests/display/link.ld -o tests/display/payload_$*.elf $<
$(TEST_OBJCOPY) -O binary tests/display/payload_$*.elf $@
test: $(OUTPUT) tests/ccplex/payload.bin tests/se/payload.bin $(USB_PAYLOADS) $(DISPLAY_PAYLOADS)
sh tests/ccplex/run.sh ./$(OUTPUT) tests/ccplex/payload.bin
sh tests/se/run.sh ./$(OUTPUT) tests/se/payload.bin
sh tests/usb/run.sh ./$(OUTPUT) tests/usb
sh tests/display/run.sh ./$(OUTPUT) tests/display
clean:
rm -f $(OUTPUT) $(OBJS) $(OBJS:.o=.d) \
tests/ccplex/payload.elf tests/ccplex/payload.bin tests/ccplex/out.log \
tests/se/payload.elf tests/se/payload.bin tests/se/out.log \
tests/usb/payload_*.elf tests/usb/payload_*.bin tests/usb/out_*.log \
tests/display/payload_*.elf tests/display/payload_*.bin \
$(WIN_OUT) $(WIN_OBJS) $(WIN_OBJS:.o=.d)