From 72a50a16bbc72f78d4bab340b452bdfdd5cd97f9 Mon Sep 17 00:00:00 2001 From: Alan Steremberg Date: Mon, 28 Sep 2026 10:56:10 -0400 Subject: [PATCH] Cart start-up: cold boot "DK" carts; wire up "Cart Interrupt Disabled" A cart loaded after boot in Multi-Pak slot 3 made CART toggle every CPU cycle forever -- the autostart of a program pak, whose CART pin is tied to Q. A disk-style ROM (HDB-DOS, Disk BASIC) never drives CART; BASIC starts it from the "DK" signature at power-on. Under the toggling, FujiNet's CFGLOAD.BIN (which re-enables the CART interrupt) hung in an endless FIRQ after 17 DriveWire reads; an OSD reset cleared it. - A cart whose first two bytes are "DK" now gets a cold boot (the existing AMW/EE_Cold_Bt path) instead of the autostart interrupt. - OSD "Cart Interrupt Disabled" (status[16] -> SWITCH[4]) was not connected; it now suppresses the slot-3 autostart interrupt. - Program paks are unchanged: Arkanoid loaded after boot still autostarts. Verified in the Verilator sim with the hardware sequence (cart loaded after boot, slot 3, no reset): the FujiNet S3 cart now reads all 98 sectors of CONFIG, matching the MiSTer after a manual reset. Regression tests unchanged. The sim gains --cart-frame N to load the cart after boot, as the OSD and MGL do; loading it before reset had hidden the problem. Co-Authored-By: Claude Opus 5.5 (1M context) --- rtl/coco3fpga.sv | 28 +++++++++++++++++++++++++--- verilator/.gitignore | 2 +- verilator/README.md | 15 ++++++++++++++- verilator/sim_main.cpp | 14 ++++++++++++-- 4 files changed, 52 insertions(+), 7 deletions(-) diff --git a/rtl/coco3fpga.sv b/rtl/coco3fpga.sv index 9c25df1..36ab999 100644 --- a/rtl/coco3fpga.sv +++ b/rtl/coco3fpga.sv @@ -1206,10 +1206,13 @@ wire AMW_End; assign AMW_ACK = AMW_End; +// Cart start-up state; see the cart_firq_enable block below. +reg cart_dk_0, cart_is_dk, cart_coldboot; + AMW coco_AMW ( .CLK(clk_sys), .RESET_N(RESET_N), - .Trigger(AMW_Trigger), + .Trigger(AMW_Trigger | cart_coldboot), // OSD cold boot, MPI/memory change, or a "DK" cart load .Restart(1'b0), .Cycle_Run(AMW_WR), .AMW_Adrs(AMW_Adrs), @@ -1786,6 +1789,13 @@ reg cart_firq_enable; reg firq_trig; reg cart_lock; +// A cart loaded after boot is started one of two ways, as on a real CoCo: +// - a program pak ties CART to Q, so it gets the autostart FIRQ below +// (cart_firq_enable low: CART toggles every cycle while in slot 3); +// - a disk-style ROM (HDB-DOS, Disk BASIC: "DK" at $C000) never drives CART, +// and BASIC starts it from the signature at power-on. Such a cart gets a +// cold boot instead. Toggling CART under it made software that re-enables +// the CART interrupt -- FujiNet's CFGLOAD -- hang in an endless FIRQ. always @(negedge clk_sys or negedge RESET_N) begin if(!RESET_N) @@ -1793,17 +1803,29 @@ begin firq_trig <= 1'b0; cart_firq_enable <= 1'b1; cart_lock <= 1'b0; + cart_dk_0 <= 1'b0; + cart_is_dk <= 1'b0; + cart_coldboot <= 1'b0; end else begin if ((ioctl_index[5:0] == 6'd1) & ioctl_download) firq_trig <= 1'b1; + if ((ioctl_index[5:0] == 6'd1) & ioctl_wr) + begin + if (ioctl_addr == 25'd0) cart_dk_0 <= (ioctl_data == 8'h44); // 'D' + if (ioctl_addr == 25'd1) cart_is_dk <= cart_dk_0 & (ioctl_data == 8'h4B); // 'K' + end + if (firq_trig) if (!ioctl_download) begin - cart_firq_enable <= 1'b0; cart_lock <= 1'b1; + if (cart_is_dk) + cart_coldboot <= 1'b1; // start it the way BASIC would: cold boot + else + cart_firq_enable <= 1'b0; // program pak: autostart FIRQ end end end @@ -1828,7 +1850,7 @@ begin 2'b01: CART_INT_IN_N <= (SER_IRQ); 2'b10: - CART_INT_IN_N <= (!CART_INT_IN_N | cart_firq_enable); + CART_INT_IN_N <= (!CART_INT_IN_N | cart_firq_enable | SWITCH[4]); // SWITCH[4]: OSD "Cart Interrupt Disabled" 2'b11: CART_INT_IN_N <= (SER_IRQ); endcase diff --git a/verilator/.gitignore b/verilator/.gitignore index ea1c4e3..ce93c2a 100644 --- a/verilator/.gitignore +++ b/verilator/.gitignore @@ -4,7 +4,7 @@ fujinet_boot*/ fujinet_boot/ imgui.ini obj_dir/ -roms/*.rom +roms/**/*.rom screenshot_*.png test-out*/ test-out/ diff --git a/verilator/README.md b/verilator/README.md index cf45b31..bc451dc 100644 --- a/verilator/README.md +++ b/verilator/README.md @@ -165,7 +165,20 @@ pty; `tests/fujinet_boot.sh`). generator (`RX_CLK = TX_CLK`). That is unchanged for control `$1x` and for baud select 0. -With both fixes, the L3 cart at 19,200 (the MiSTer configuration) boots from the +3. **Slot 3 hung FujiNet's CONFIG loader.** Once a cart finished downloading + after boot, slot 3 toggled the CART interrupt every CPU cycle, forever. That + is right for a program pak (CART tied to Q autostarts it), but a disk-style + ROM such as HDB-DOS never drives CART. FujiNet's `CFGLOAD.BIN` re-enables + that interrupt and then hung in an endless FIRQ, after reading 17 sectors. + A reset cleared it, which is why "load cart, then Reset" worked. The OSD's + "Cart Interrupt Disabled" option was not connected to anything. **Fix:** a + cart that starts with `DK` gets a cold boot instead, the way BASIC starts + it at power-on, and the OSD option now suppresses the autostart interrupt. + Program paks behave as before. The sim reproduces both cases with + `--cart-frame N`, which loads the cart after boot the way the OSD and MGL + do. + +With these fixes, the L3 cart at 19,200 (the MiSTer configuration) boots from the real FujiNet daemon with no checksum errors, and gets to FujiNet's CONFIG splash. The S3 cart (115,200) reaches the CONFIG host-slots screen. diff --git a/verilator/sim_main.cpp b/verilator/sim_main.cpp index 86cebb3..ced9651 100644 --- a/verilator/sim_main.cpp +++ b/verilator/sim_main.cpp @@ -115,6 +115,9 @@ static int reset_hold = 0; // clk_sys cycles of RESET at start static std::string rom_path = "roms/boot0.rom"; static std::string diskrom_path = "roms/boot1.rom"; static std::string cart_path, cas_path, font_path; +// --cart-frame: deliver the cart after boot, as Main_MiSTer does when the cart +// is picked from the OSD or an MGL. -1 = with the ROMs, before reset ends. +static int cart_frame = -1; static std::string fdc_path[4], sdc_path[2]; // Screenshots / logging @@ -564,6 +567,10 @@ static void on_frame(int frame) { pulse_status(0); } if ((frame % 60) == 0) set_rtc(); + if (frame == cart_frame && !cart_path.empty()) { + printf("Loading cartridge at frame %d: %s\n", frame, cart_path.c_str()); + bus.QueueDownload(cart_path, IDX_CART, true); + } } // Edge counts over the last run_until_next_frame(), for the "no frame" report. @@ -610,6 +617,7 @@ static void print_usage(const char* argv0) { "\n" "Media\n" " --cart FILE Program Pak (.ccc), index 1; selects MPI slot 3\n" +" --cart-frame N load the cart at frame N, after boot (as the OSD/MGL do)\n" " --cas FILE cassette image (.cas), index 2\n" " --disk FILE / --disk0..3 floppy drive 0..3 (.dsk); selects MPI slot 4\n" " --sdc FILE / --sdc1 CoCoSDC drive 0/1 (.dsk/.vhd); selects MPI slot 2\n" @@ -677,6 +685,7 @@ static int parse_args(int argc, char** argv) { else if (a == "--font") font_path = next(); else if (a == "--cart") { cart_path = next(); cart_or_disk_mpi = 1; } else if (a == "--cas") cas_path = next(); + else if (a == "--cart-frame") cart_frame = atoi(next()); else if (a == "--disk" || a == "--disk0") { fdc_path[0] = next(); cart_or_disk_mpi = 2; } else if (a == "--disk1") fdc_path[1] = next(); else if (a == "--disk2") fdc_path[2] = next(); @@ -927,7 +936,8 @@ static void draw_control_window() { int main(int argc, char** argv, char** env) { Verilated::commandArgs(argc, argv); - // Defaults, as a fresh MiSTer config: MPI slot 4 (disk), 512K. + // Default Multi-Pak slot 4 (Disk), 512K. A fresh MiSTer config is slot 2 + // (CoCoSDC); slot 4 boots Disk BASIC from boot1.rom with no SDC image. set_status_field(12, 2, 2); if (parse_args(argc, argv)) return 0; @@ -976,7 +986,7 @@ int main(int argc, char** argv, char** env) { queue_rom(rom_path, IDX_BOOT0, "CoCo3 ROM"); queue_rom(diskrom_path, IDX_BOOT1, "Disk BASIC ROM"); queue_rom(font_path, IDX_FONT, "font"); - queue_rom(cart_path, IDX_CART, "cartridge"); + if (cart_frame < 0) queue_rom(cart_path, IDX_CART, "cartridge"); queue_rom(cas_path, IDX_CAS, "cassette"); for (int d = 0; d < 4; d++) mount(BLK_FDC0 + d, fdc_path[d]); for (int d = 0; d < 2; d++) mount(BLK_SDC0 + d, sdc_path[d]);