Skip to content

fix: Maupiti Island (was: "fix: (cpu): HLT waits for interrupt instead of stopping emulation") - #2465

Open
maximilien-noal wants to merge 3 commits into
masterfrom
fix/maupiti_island
Open

maximilien-noal wants to merge 3 commits into
masterfrom
fix/maupiti_island

Conversation

@maximilien-noal

@maximilien-noal maximilien-noal commented Nov 3, 2026 •

Copy link
Copy Markdown
Member

Description of Changes

  • HLT with IF=1 now idles the CPU (new State.IsHalted) until a hardware interrupt is delivered, instead of stopping emulation. Cycles still advance while halted, and CPU_CYCLES breakpoints are checked on each idle step (so monitor breakCycles and the test max-cycle failsafe still fire). HLT with IF=0 still stops emulation.
  • CfgCpu.HandleExternalInterrupt split: new DeliverExternalInterrupt clears IsHalted on delivery.
  • New CLI option --HltStopsEmulation toggles the previous behavior (HLT always stops emulation). ASM test fixtures and SingleStepTest enable it, since for them HLT is the expected end-of-program marker.
  • IrLifter: the CFG successor after HLT is not treated as a method successor (generated Hlt() exits), fixing "Successor not in blocks" in the code-generator dump.
  • HLT coverage as ASM fixtures (replaces the hand-crafted HltTest): hlt.asm is the hlt scenario of the generated-code catalogue (discovery, machine goldens, generated source ending in return Hlt(), compile, generated run with oracle); hltmask.asm covers the CPU cycle breakpoint firing while the CPU is halted; TestHltWaitsForHardwareInterrupt covers the wait-for-IRQ behavior (IRQ0 handler runs while halted, program resumes past the HLT), both interpreted and JIT.

Rationale behind Changes

Maupiti Island exited right after the V (VGA) / C (clavier) prompts. Its PIT calibration routine hooks IRQ0 and runs sti; hlt to wait for the timer tick; Spice86 stopped on any HLT. DOSBox Staging (CPU_HLT / hlt_decode) idles until an interrupt changes CS:IP. This PR matches that behavior by default.

It runs now:

image

Known limitation: generated C# code still exits on HLT.

Suggested Testing Steps

  • dotnet build tests/Spice86.Tests: succeeded (0 warnings, 0 errors) after the ASM-fixture commit.
  • dotnet test tests/Spice86.Tests --no-build -- --filter-not-class '*SingleStepTest*': 8264 passed, 1 failed — the failure is CfgNodeExecutionCompilerTest.Compile_SecondCallPreventsStaleSwap, a 30-second timing-window test that passes in isolation and as a class (load-dependent flake; the follow-up commit changes test infrastructure only).

Copilot AI balanced review requested due to automatic review settings November 3, 2026 08:31

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The halted idle path skips CPU-cycle breakpoint checks, permanently missing exact-count breakpoints and bypassing the maximum-cycle failsafe, so a missing IRQ can hang the HLT test indefinitely.

1 open issue
What changed in this PR

Updates interpreter HLT behavior so DOS programs can wait for hardware interrupts without terminating emulation, while retaining an option for legacy stop-on-HLT behavior.

Changes:

  • Adds halted CPU state and threads HltStopsEmulation through configuration and test-machine setup.
  • Excludes post-HLT successors from generated-method structural edges.
  • Preserves AX, DX, and DS around BIOS timer hooks, adds an ASM regression scenario, and updates BIOS-layout expectations.

Validation was limited to static review; no builds or tests were run.

File Description
tests/​Spice86.Tests/​Spice86Creator.cs Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​RtcTests/​Makefile Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​RtcTests/​bios_timer_hook_registers.asm Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​cpuTests/​res/​DumpedListing/​intchain.txt Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​cpuTests/​res/​DumpedListing/​bootstrap_host_requested_batch_slash_path.txt Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​cpuTests/​res/​DumpedListing/​batch_vol_prints_label.txt Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​cpuTests/​res/​DumpedListing/​batch_vol_drive_argument.txt Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​cpuTests/​res/​DumpedListing/​batch_vol_cdrom_label.txt Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​cpuTests/​res/​DumpedListing/​batch_ver_prints.txt Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​cpuTests/​res/​DumpedListing/​batch_type_missing_file_error.txt Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​cpuTests/​res/​DumpedListing/​batch_type_displays_file.txt Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​cpuTests/​res/​DumpedListing/​batch_time_set_value.txt Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​cpuTests/​res/​DumpedListing/​batch_time_prints.txt Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​cpuTests/​res/​DumpedListing/​batch_time_host_sync.txt Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​cpuTests/​res/​DumpedListing/​batch_time_help.txt Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​cpuTests/​res/​DumpedListing/​batch_set_prompt_unsupported.txt Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​cpuTests/​res/​DumpedListing/​batch_set_enumerates_environment.txt Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​cpuTests/​res/​DumpedListing/​batch_rmdir_alias.txt Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​cpuTests/​res/​DumpedListing/​batch_rename_alias.txt Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​cpuTests/​res/​DumpedListing/​batch_ren_wildcard.txt Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​cpuTests/​res/​DumpedListing/​batch_ren_single_file.txt Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​cpuTests/​res/​DumpedListing/​batch_ren_missing_target.txt Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​cpuTests/​res/​DumpedListing/​batch_ren_missing_error.txt Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​cpuTests/​res/​DumpedListing/​batch_rd_removes_directory.txt Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​cpuTests/​res/​DumpedListing/​batch_rd_missing_error.txt Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​cpuTests/​res/​DumpedListing/​batch_multi_output_tight.txt Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​cpuTests/​res/​DumpedListing/​batch_multi_output_spaces_before_gt.txt Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​cpuTests/​res/​DumpedListing/​batch_multi_output_padded_targets.txt Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​cpuTests/​res/​DumpedListing/​batch_move_wildcard.txt Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​cpuTests/​res/​DumpedListing/​batch_move_to_directory.txt Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​cpuTests/​res/​DumpedListing/​batch_move_rename.txt Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​cpuTests/​res/​DumpedListing/​batch_move_missing_source.txt Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​cpuTests/​res/​DumpedListing/​batch_mkdir_alias.txt Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​cpuTests/​res/​DumpedListing/​batch_md_existing_error.txt Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​cpuTests/​res/​DumpedListing/​batch_md_creates_directory.txt Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​cpuTests/​res/​DumpedListing/​batch_invalid_redirect_no_file.txt Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​cpuTests/​res/​DumpedListing/​batch_invalid_redirect_launch_lt_gt.txt Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​cpuTests/​res/​DumpedListing/​batch_invalid_redirect_launch_bar_lt.txt Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​cpuTests/​res/​DumpedListing/​batch_invalid_redirect_launch_bar_gt.txt Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​cpuTests/​res/​DumpedListing/​batch_invalid_redirect_gt_bar.txt Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​cpuTests/​res/​DumpedListing/​batch_invalid_redirect_double_lt.txt Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​cpuTests/​res/​DumpedListing/​batch_invalid_redirect_double_gt.txt Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​cpuTests/​res/​DumpedListing/​batch_invalid_redirect_double_bar.txt Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​cpuTests/​res/​DumpedListing/​batch_invalid_redirect_bar_gt.txt Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​cpuTests/​res/​DumpedListing/​batch_imgmount_internal_precedence.txt Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​cpuTests/​res/​DumpedListing/​batch_for_wildcard_del.txt Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​cpuTests/​res/​DumpedListing/​batch_for_quoted_parenthesis.txt Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​cpuTests/​res/​DumpedListing/​batch_for_malformed_single_percent.txt Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​cpuTests/​res/​DumpedListing/​batch_for_malformed_no_variable.txt Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​cpuTests/​res/​DumpedListing/​batch_for_malformed_no_in.txt Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​cpuTests/​res/​DumpedListing/​batch_for_malformed_no_body.txt Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​cpuTests/​res/​DumpedListing/​batch_for_bare_star.txt Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​cpuTests/​res/​DumpedListing/​batch_erase_alias.txt Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​cpuTests/​res/​DumpedListing/​batch_echo_spacing_space_before_gt.txt Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​cpuTests/​res/​DumpedListing/​batch_echo_redirect.txt Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​cpuTests/​res/​DumpedListing/​batch_echo_on_status.txt Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​cpuTests/​res/​DumpedListing/​batch_echo_off_status.txt Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​cpuTests/​res/​DumpedListing/​batch_echo_off_inner_batch.txt Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​cpuTests/​res/​DumpedListing/​batch_echo_leading_spaces.txt Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​cpuTests/​res/​DumpedListing/​batch_echo_dot_text.txt Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​cpuTests/​res/​DumpedListing/​batch_echo_dot_blank.txt Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​cpuTests/​res/​DumpedListing/​batch_dir_with_pattern.txt Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​cpuTests/​res/​DumpedListing/​batch_dir_sort_on.txt Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​cpuTests/​res/​DumpedListing/​batch_dir_lists_files.txt Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​cpuTests/​res/​DumpedListing/​batch_dir_bare_format.txt Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​cpuTests/​res/​DumpedListing/​batch_dir_ad_directories.txt Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​cpuTests/​res/​DumpedListing/​batch_delete_alias.txt Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​cpuTests/​res/​DumpedListing/​batch_del_wildcard.txt Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​cpuTests/​res/​DumpedListing/​batch_del_single_file.txt Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​cpuTests/​res/​DumpedListing/​batch_del_missing_error.txt Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​cpuTests/​res/​DumpedListing/​batch_date_set_value.txt Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​cpuTests/​res/​DumpedListing/​batch_date_prints.txt Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​cpuTests/​res/​DumpedListing/​batch_date_host_sync.txt Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​cpuTests/​res/​DumpedListing/​batch_date_help.txt Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​cpuTests/​res/​DumpedListing/​batch_copy_wildcard.txt Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​cpuTests/​res/​DumpedListing/​batch_copy_to_directory.txt Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​cpuTests/​res/​DumpedListing/​batch_copy_switches.txt Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​cpuTests/​res/​DumpedListing/​batch_copy_single_file.txt Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​cpuTests/​res/​DumpedListing/​batch_copy_missing_source.txt Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​cpuTests/​res/​DumpedListing/​batch_copy_concat_two.txt Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​cpuTests/​res/​DumpedListing/​batch_copy_concat_three.txt Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​cpuTests/​res/​DumpedListing/​batch_cd_no_args.txt Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​cpuTests/​res/​DumpedListing/​batch_cd_dotdot_parent.txt Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​cpuTests/​res/​DumpedListing/​batch_cd_dot_stays.txt Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​cpuTests/​res/​DumpedListing/​batch_cd_changes_directory.txt Updated as part of this pull request.
tests/​Spice86.Tests/​Resources/​cpuTests/​res/​DumpedListing/​batch_cd_backslash_root.txt Updated as part of this pull request.
tests/​Spice86.Tests/​MachineTest.cs Updated as part of this pull request.
tests/​Spice86.Tests/​CpuTests/​SingleStepTests/​SingleStepTestMinimalMachine.cs Updated as part of this pull request.
tests/​Spice86.Tests/​CfgCpu/​CfgNodeExecutionCompilerTest.cs Updated as part of this pull request.
tests/​Spice86.Tests/​CallbackNumbersTest.cs Updated as part of this pull request.
tests/​Spice86.Tests/​AsmFixtures/​BiosDeviceScenarioCatalog.cs Updated as part of this pull request.
src/​Spice86/​Spice86DependencyInjection.cs Updated as part of this pull request.
src/​Spice86.Core/​Emulator/​ReverseEngineer/​CfgCodeGeneration/​Ir/​IrLifter.cs Updated as part of this pull request.
src/​Spice86.Core/​Emulator/​InterruptHandlers/​Timer/​TimerInt8Handler.cs Updated as part of this pull request.
src/​Spice86.Core/​Emulator/​CPU/​State.cs Updated as part of this pull request.
src/​Spice86.Core/​Emulator/​CPU/​CfgCpu/​InstructionExecutor/​InstructionExecutionHelper.cs Updated as part of this pull request.
src/​Spice86.Core/​CLI/​Configuration.cs Updated as part of this pull request.

Review effort: Balanced

@maximilien-noal maximilien-noal self-assigned this Nov 3, 2026
@maximilien-noal maximilien-noal added compatibility Emulator compatibility with DOS apps bugfix fixes a bug CFGCPU Related to the future Control Flow CPU labels Nov 3, 2026
@maximilien-noal maximilien-noal changed the title fix(cpu): HLT waits for interrupt instead of stopping emulation fix: Maupiti Island (was: "fix: (cpu): HLT waits for interrupt instead of stopping emulation") Nov 3, 2026
@maximilien-noal
maximilien-noal force-pushed the fix/maupiti_island branch 3 times, most recently from 5eeef6b to 95bcdc5 Compare November 6, 2026 12:01
Comment thread tests/Spice86.Tests/HltTest.cs Resolved
mov dx, 0
sti
hlt
mov [0], dx ; only the wait-mode run reaches here: the handler set dx to 1

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I look at the generated code, this line is never reached.
I wonder if hlt shouldnt be an infinite wait loop with a jump dispatcher in mode where hlt doesnt cause emulator exit

maximilien-noal and others added 3 commits November 8, 2026 22:42
Match DOSBox Staging: with IF=1, HLT idles until an IRQ is delivered.
Add --HltStopsEmulation to keep the old behavior (used by tests).
Skip HLT successor in IrLifter.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
The HLT idle path skipped CPU_CYCLES breakpoints, so exact-count
breakpoints and the test max-cycle failsafe never fired while waiting
for an IRQ.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- hlt.asm: STI; HLT wait program registered as the "hlt" scenario in the
  generated-code catalogue: discovery, machine goldens, generated source
  (return Hlt()), compile and generated run with oracle.
- hltmask.asm: masked-PIC wait program for the cycle-breakpoint-while-halted check.
- HltStopsEmulation is now an AsmRunSettings option, wired through
  Spice86Creator, with a .hltwait golden-key suffix when non-default.
- MachineTest: TestHlt, TestHltWaitsForHardwareInterrupt and
  TestHltCycleBreakpointTriggersWhileHalted (interpreted and JIT).

Remove HltTest.cs.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
InterruptFlag = true;
CheckExternalEvents(biosSegment, 0x0031);
// F000:0031 hlt
return Hlt();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to be the 1st hlt in the listing, others are not generated

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bugfix fixes a bug CFGCPU Related to the future Control Flow CPU compatibility Emulator compatibility with DOS apps

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants