4 ms·
Writing a first-gen PSX (Playstation) game, NASCAR Racing, we had an in-house physics engine that needed the physics thread to run on a constant 30 Hz. PSX SDK
by sokoloff 4y ago
Writing a first-gen PSX (Playstation) game, NASCAR Racing, we had an in-house physics engine that needed the physics thread to run on a constant 30 Hz. PSX SDK didn't have pre-emptive multi-tasking. Sony US "checked with Japan" who said we were out of luck.
Then I remembered my old Atari 8-bit programming days and the vertical blank interrupt (which there was on the PSX, but we couldn't run all the physics in the VBI time allotment). What we could do though, was to use setjmp/longjmp to switch contexts between threads and then hack the vertical blank interrupt to save off the registers from the main thread, longjmp back to the physics thread, which would then restore the registers of the main thread and longjmp back to it. Bingo, 29.94Hz pre-emptive two-threading (which was all we needed).
(I don't recall if we actually used setjmp/longjmp or if we just stored away the PC register to return from the interrupt and monkeyed with it to return from the interrupt to the physics thread [as if it was interrupted at the start of a cycle of the engine] and then return from there to code that would restore the registers and make it appear to the main thread that it was returning from a VBI.)
- birdhouse 4y agoI wrote multithreaded code in C++ before for a different use case. Was the challenge here just getting 30hz or was there something limiting where you couldn’t get that output with just running a dedicated thread? I’m no expert. Teach me :)
- sokoloff 4y agoWe couldn't find another way to get a steady 30Hz. I don't think there was a pthreads implementation for the PSX SDK, but it's been a little over 27 years, so I can't swear to it. I do know we didn't do it without trying a bunch of other more conventional alternatives.