Skip to content

Conversation

bscottm
Copy link
Contributor

@bscottm bscottm commented Aug 27, 2025

Re-engineered sim_video support. Submitted as two commits so that it's easier to cherry-pick the second commit.

The first commit is the re-engineered video that removes the SDL_main() asymmetry between macOS, Linux and Windows -- they all use SDL_main() if video is needed. Video is also automagically rescaled if it can't fit on the physical screen; SDL2 has been able to handle this for a long time. Audio is also asynchronous and won't stall the simulator while the tone plays out. There's a new "onto" API that allows the simulator to draw pixels directly into the SDL texture, reducing the amount of video frame buffering. Lastly, users can enable native hardware acceleration, for what it's worth; simulator video is not limited to the SDL software renderer.

The second commit is sim_timer.c cleanups, notably using actual constants for various conversions, e.g., MSEC_PER_SEC_l for millisecond to second (long) conversions. It also corrects the long-running issue on Windows where Sleep(1) can sleep for 1 + 15 + 5 milliseconds (requested delay + timer interrupt periodicity + wakeup latency) by using waitable timers.

- The main thread must be the event loop as noted in
  SDL_RenderPresent()'s documentation; it is mere coincidence that SDL2
  works in a thread other than the main thread. No asymmetry between
  macOS, Linux and Windows. (BESM6 is still an exception.)

  - Video/display-based simulators spawn the simulator in its own
    thread, requesting an 8M stack when created (since SDL 2.0.9).

  - main() defers SDL initialization via a condition variable until
    signaled. This occurs when either the simulator thread exits or the
    simulator asks for a display window.

- If the simulator's video frame is larger than the physical screen
  dimensions, it is automatically rescaled. While this may "uglify" the
  video, it's better than not being able to see the bottom of the
  simulator's video and unable to resize or move the frame.

- "SHOW VERSION": Report the simulator thread's stack size if the API is
  available. pthread-based implementations and Windows 8+ have an API to
  report thread stack size (unavailable on Windows XP.)

- "SET VIDEO NATIVE": Support SDL native rendering support (vice the
  SDL2 software renderer). For example, configure the PDP-11 VT "Moon
  Lander" demo mode as follows:

    set video native
    boot vt

- Window expose/open latency: Windows Subsystem for Linux (WSL) has
  highly variable window visibility times exceeding the code's original
  2 second delay.

  - New user event, EVENT_OPENCOMPLETE, signals to the caller via a
    condition variable that initial window opening and exposure events
    have been processed by the event loop.

- Asynchronous audio: The 330 Hz audio "beep" is completely
  asynchronous. vid_beep() keeps track of consecutively requested beeps,
  inserting a silence gap between them and continues to play out the
  tone.

  - PDP-11 VT demo mode: No awkward freeze when crashing into the Moon's
    surface waiting for the beep to finish playing.

- vid_draw_onto/vid_draw_onto_window: New interface functions that allow
  a simulator calback to execute in the SDL main and draw directly onto
  the texture, cutting down on the amount of extra buffer copying incurred
  by vid_draw and vid_draw_window.

SCP: Reduce scp.c compiler warnings

- Assignments in conditional expressions
- Hiding previously declared variables.
- Trim trailing spaces.
- Reduce/eliminate compiler warnings
    - Variables that hide previous definitions (e.g., uptr)
    - Assignments in conditional expressions (conditional expressions
      should be... conditionals, i.e. "==".)

- Readability: use constants for conversions, make it easier to see the
  time units.

- Use size_t for array indices.

- Windows: Prefer waitable timers over Sleep(). Sleep() is the fallback
  if initializing a waitable timer fails (which it doesn't, but there's
  no penalty for writing safe code.) A detailed discussion on Windows
  timers can be found at http://www.windowstimestamp.com/description

  Significantly reduces the occasional headache-inducing slow redraw
  (flicker) on Windows (Imlac, primarily, but also afflicts PDP11 VT
  sometimes.)
@bscottm bscottm force-pushed the scp-video-improvements branch from c7e37a2 to a1e1544 Compare August 30, 2025 19:09
@bscottm
Copy link
Contributor Author

bscottm commented Sep 1, 2025

Retracting temporarily -- strange refresh beat frequency on Linux X server.

@bscottm bscottm closed this Sep 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant