Files
RCM-Emulator/platform.h
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

70 lines
2.1 KiB
C++

#ifndef RCM_PLATFORM_H
#define RCM_PLATFORM_H
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
// Small portability helpers for the disk-image code, which is the one place
// the emulator does raw POSIX file I/O.
// MinGW's open() defaults to TEXT mode: _read() turns every CR LF into LF
// and stops at the first 0x1A byte. On an SD card or eMMC image that is
// silent sector corruption, so every image fd must be opened O_BINARY.
// POSIX has no such flag (and no such mode), so it is a no-op there.
#ifndef O_BINARY
#define O_BINARY 0
#endif
// Size of an open file, in 64 bits. MinGW's plain struct stat carries a
// 32-bit st_size, which cannot describe the 4 GiB chunks Hekate splits a
// rawnand dump into; _fstati64 can.
static inline int64_t file_size64(int fd) {
#ifdef _WIN32
struct _stati64 st;
if (_fstati64(fd, &st) != 0)
return -1;
return (int64_t)st.st_size;
#else
struct stat st;
if (fstat(fd, &st) != 0)
return -1;
return (int64_t)st.st_size;
#endif
}
// pread/pwrite are POSIX and MinGW does not have them. Seek-then-read is
// equivalent here: the emulated storage is touched only from the CPU thread,
// so the atomicity real pread() buys against a shared file offset is not in
// play.
#ifdef _WIN32
#include <io.h>
static inline ssize_t pread(int fd, void *buf, size_t n, long long off) {
if (_lseeki64(fd, off, SEEK_SET) < 0)
return -1;
return _read(fd, buf, (unsigned int)n);
}
static inline ssize_t pwrite(int fd, const void *buf, size_t n, long long off) {
if (_lseeki64(fd, off, SEEK_SET) < 0)
return -1;
return _write(fd, buf, (unsigned int)n);
}
#else
#include <unistd.h>
#endif
// Large zero-filled memory that only becomes resident where it is touched
// (platform.cpp). zeroed_reset() makes all of it zero again, at the same
// address, without touching it.
uint8_t *zeroed_alloc(size_t size);
void zeroed_reset(uint8_t *p, size_t size);
void zeroed_free(uint8_t *p, size_t size);
// fopen() that also accepts a UTF-8 name on Windows (a dropped file).
FILE *platform_fopen(const char *path, const char *mode);
#endif // RCM_PLATFORM_H