• Please review our updated Terms and Rules here

Automatic frame skip on the x86

carlos12

Experienced Member
Joined
May 10, 2020
Messages
183
Location
Madrid, Spain
OutRun for PC self adapts the FPS shown depending on the speed of the processor. But, and this really seems stunning to me, it makes it on real time! For example, on DOSBox you can start at a certain speed (let's say, 3000 cycles) and it runs very smooth. But if, during the game play, we gradually reduce the cycles down to 280 (which could be more or less equivalent to an 8088 at 4.77 Mhz), we'll see how the game continues its way, but n frames are being skipped, finally being quite jerky when ~280 is reached. The opposite is also true, starting slow and incrementing the speed makes the game run smoother on real time.

So I'm wondering what technique could be used to achieve this effect.

A less elegant solution I figured out would be calculating the approximate speed of the system, by counting cycles, and after that, skip the frames needed for every speed. I post here the code I wrote for counting cycles, in case it's useful for somebody, or if anybody would like to improve something on it:

Code:
#include <stdio.h>
#include <time.h>

void count_cpu_cycles (void)
{
	
	clock_t start, stop;
  	unsigned long t;
  
  	asm mov al, 0x34
  	asm out 0x43, al    /* tell the PIT which channel we're setting */

 	_CX = (int)(1193180L / 1000); /* 1193180L / Frequency in Hz*/
	outportb(0x40,_CL);			  /* Configuring timer 0 with the freq */
	outportb(0x40,_CH);

	start = clock();

	asm mov cx,8000h  /* Do it 32768 times */
loopClock:
	asm ror al,1
	asm sub al,al
	asm add al,250	/* Just a few random operations to consume cycles */
	asm loop loopClock

	stop = clock();

	outportb(0x43,0x34);
	outportb(0x40,0);
	outportb(0x40,0);  /* Restoring the timer */

	t = stop-start;
	printf ("Result = %lu ... %lu = %lu", stop, start, t);
}

I made a table of the values returned by a few systems, emulated via 86Box, so I could use them as a reference. For instance, this code returns 302 for the IBM PC and XT, 291 for the Compaq Portable, 276 for the original Tandy 1000 or 349 for an American XT clone, all of them running at 4.77 (the higher, the slower). I also have values from 8086 to 486 processors, that ranks from 160 to 6. I think this code could not be used for real time speed poling as it takes a time to count the cycles, I would use it once at the beginning to establish the frame skipping.

Another solution would be just asking the user if s/he is using a slow of fast processor (as Blood Money for PC does) and then accordingly skip 1, 2 or whatever the frames needed.

I think the autoskip solution would be fantastic but I have not a clue of how could be accomplished.

Thank you very much!
 
Why would you need to know the speed of the system at all? In a racing game the movement of things on the screen would depend on elapsed time and vehicle speed, no? You can calculate that independently of the frame rate.
 
Isn't that what most games do anyway? I mean, games don't run in slow-motion on slower systems, they just draw fewer frames per second - keeping the game speed intact.

Also, I would not even call this "frame skipping", as no frames are skipped. Skipping would mean there's a fixed target frame-rate the game can not uphold. But these games will simply scale with CPU speed.
 
I think the autoskip solution would be fantastic but I have not a clue of how could be accomplished.

You're overthinking the problem. On systems where speed is not known and single-frame lock (ie. full framerate) is not required (or achievable), it's common for most action games to calculate time-sensitive operations (usually game logic, input, and music) on a background timer, so that their results are guaranteed no matter what speed the system is. Then the part of the game that takes the most time -- rendering -- is done whenever it can. At the start of the render loop, it copies (hopefully atomically) the data it needs from the output of the gameplay calcs to render the screen, then it renders the screen. While it's rendering, several more calcs could have happened, because the game calcs are on a timer. So it's not that intentional frame skipping is occuring, but rather it's a natural effect of the game not being able to render frames fast enough to match the game calcs on a timer.

As bakemono wrote: "independently of the frame rate."

Watching dosbox's debugger output running outrun, we can see the game sets PIT 0 to 100Hz (mode 3) and doesn't change until you exit. Since I can hear the sound effects aren't updating at 100Hz, I'm guessing that timer is subdivided by 3 such that each portion of the timer handler has an effective 33 Hz rate. My guess is:
0: input
1: game calcs
2: sound

...then loops around (3: input, 4: game calcs, etc.)

One way to test this theory is by running the game on a ludicrously fast system (such that render time is instant), record video of the screen, and seeing how many unique frames per second we get. I did that, and found the following:

- If you include everything on the screen, the true framerate is ~58 fps because the score, timer, and your car's tires update every single render
- If you exclude those elements, looking at only the main track/cars/scenery updating, the framerate is about ~19 fps

So this proves my theory WRONG: With an effective framerate of only 20, they set the timer to 100 Hz, and every fifth tick (for a framerate of 100 / 5 = 20) is when they calculate the main game mechanics.

Here's a trdrop export of the main gameplay area so you can see what I'm talking about:


Edit: Watching this a few times, the fps increases a bit in one section, which might be an actual fps increase or it could be a measurement error on my part. But the overwhelming majority of the footage (not included in that clip) is ~20 fps.
 
Thank you so much! That was one my missing pieces. You're right, counting cycles to determine the approximate CPU speed, and then skipping frames accordingly is an overkill. I've been testing moving the game's coordinate calculations from the main loop to a custom interrupt procedure, poling at 60 or 100 Hz (I'm trying several options), and getting the help of a tick counter. It works great.
 
Back
Top