// Headless Breaknes NES board (BreaksCore CoreApi): run a ROM with scripted pad input, write // the PPU register dump (Breaknes's own RegDumper) and the RAW fields asked for (240 x 341 // uint16), for issue #534 (Pac-Land). // // nes_headless ROM OUTDIR LASTFIELD START_FIELD RIGHT_FROM [field ...] // (START held for 6 fields from START_FIELD, RIGHT held from RIGHT_FROM; NES_LOG=1 also // writes Breaknes's Nintendulator-format CPU log; AUDIO=1 writes the APU output two ways, // point-sampled every DecimateEach+1 half-clocks as BreaknesSDL's SoundOutput does // (audio_point.s16) and averaged over the same window (audio_box.s16), issue #533) #include "pch.h" #include "CoreApi.h" #include #include #include #include static std::vector read_file(const char *path) { std::ifstream f(path, std::ios::binary); return std::vector((std::istreambuf_iterator(f)), std::istreambuf_iterator()); } int main(int argc, char **argv) { if (argc < 6) { fprintf(stderr, "usage: %s ROM OUTDIR LASTFIELD START_FIELD RIGHT_FROM [field ...]\n", argv[0]); return 2; } auto rom = read_file(argv[1]); std::string out = argv[2]; int last = atoi(argv[3]), start_at = atoi(argv[4]), right_from = atoi(argv[5]); std::set wanted; for (int i = 6; i < argc; i++) wanted.insert(atoi(argv[i])); SetNescartdbDir((char *)"Nescartdb"); char *ppus[] = { (char *)"RP2C02G-0" }; CreateBoardEx((char *)"NES (NES-CPU-07, 1987)", (char *)"RP2A03G", ppus, 1, (char *)"NES"); if (InsertCartridge(rom.data(), rom.size()) < 0) { fprintf(stderr, "InsertCartridge failed\n"); return 1; } size_t pad = IOCreateInstance(3); // NESController IOAttach(0, pad); Reset(); SetRAWColorMode(true); EnablePpuRegDump(true, (char *)out.c_str()); if (getenv("NES_LOG")) EnableNintendulatorLog(true); FILE *point = nullptr, *box = nullptr; size_t decimate_each = 0, counter = 0; double sum = 0; size_t summed = 0; if (getenv("AUDIO")) { APUSim::AudioSignalFeatures features{}; GetApuSignalFeatures(&features); decimate_each = features.SampleRate / 48000; fprintf(stderr, "APUSim sample rate %d, decimate each %zu\n", features.SampleRate, decimate_each); point = fopen((out + "/audio_point.s16").c_str(), "wb"); box = fopen((out + "/audio_box.s16").c_str(), "wb"); } std::vector frame(240 * 341); int field = 0; size_t last_v = 0; while (field <= last) { Step(); if (point) { float sample; SampleAudioSignal(&sample); sum += sample; summed++; if (counter >= decimate_each) { // SoundOutput::FeedSample's counter int16_t p = (int16_t)(sample * 32767.0f), b = (int16_t)(sum / summed * 32767.0); fwrite(&p, 2, 1, point); fwrite(&b, 2, 1, box); counter = 0; sum = 0; summed = 0; } else { counter++; } } size_t v = GetVCounter(), h = GetHCounter(); if (v < last_v) { if (wanted.count(field)) { std::string name = out + "/nes-" + std::to_string(field) + ".raw"; FILE *f = fopen(name.c_str(), "wb"); fwrite(frame.data(), 2, frame.size(), f); fclose(f); fprintf(stderr, "field %d written (PHI %zu)\n", field, GetPHICounter()); } field++; // Start: held for 6 fields from START_FIELD, or with START_EVERY=n for 6 fields of every // n until START_FIELD bool start = getenv("START_EVERY") ? field % atoi(getenv("START_EVERY")) < 6 && field < start_at : field >= start_at && field < start_at + 6; IOSetState(pad, 5, start); IOSetState(pad, 3, field >= right_from); // Right } last_v = v; if (!InResetState() && v < 240 && h < 341) { PPUSim::VideoOutSignal sample; SampleVideoSignal(&sample); frame[v * 341 + h] = sample.RAW.raw; } } EnablePpuRegDump(false, (char *)out.c_str()); if (point) { fclose(point); fclose(box); } return 0; }