new timeslicer

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Andrew Davie

Here's where it's at...


The "exciting" development is a new method for timeslice calculations.

Essentially, timeslicing is dividing the ARM-based code into small execution units that can be run sequentially (or even out of order) based on available time in the vertical blank (VB) or overscan (OS) of the TV frame.  In prior incantatinos I've estimated the available time in these areas with a bit of trial and error - set a variable (availableIdleTime) to something like 100000

At the start of OS or VB, I'd start the ARM timer, and then repeatedly check against the availableIdleTime. If there was 'sufficient time' to do another execution unit, then I would.  All I needed to know was the length of each (or the slowest) execution unit.  I got this timing from profiling through the Gopher emulator, which gave cycle-exact times of all the execution units.

All good and well, but it was a rough thing, and every now and then I'd 'trip' and take just a little bit too long. A conflagration of slightly-off-errors in my timing would all add up and push the time over the edge, and I'd get a too-many-scanlines-in-a-frame glitch.  SO I'd need to be conservative, reducing availableIdleTime to try and catch that worst-case problem.

This new scheduler does things a slightly different way. The OS and VB are timed with the '2600 TIM64T timer. It's basically a countdown timer in chunks of 64 cycles of 6502. In the new system, rather than have some estimated availableIdleTime value, I actually use the INTIM value (the countdown value of the timer) showing me free time. I pass this value to the ARM code, which then converts it to ARM CPU cycles and uses THAT to base the scheduling on. It's pretty cool.

Basically, flow of control is this...

In the 6502 code, I do things in this order...

set TIM64T (OS)
6502 overscan code
pass INTIM to ARM --> calculates availableIdleTime
start ARM timer
ARM overscan code
ARM timeslice/scheduler for remainder of availableIdleTime
wait INTIM = 0

set TIM64T (VB)
6502 VB code
pass INTIM to ARM --> calculates availableIdleTime
start ARM timer
ARM vb code
ARM timeslice/scheduler for remainder of availableIdleTime
wait INTIM = 0

6502 screen kernel

As to timing....

262 scanlines/frame
70MHz --> 60Hz frames --> 1,166,666 cycles/frame
--> 4452 ARM cycles/scanline (11666666/262)

or in terms of INTIM units (64 cycles), that's 4452*64/76
= 3749 ARM cycles per INTIM 'step'

 SO, after the VB (or OS) 6502 code has done its stuff, I know how many INTIM units are still available for further processing. I pass this to the ARM.  At the start of ARM code (OS or VB) I do the calculation.  Here's the VB handler...


#define INTIM_TO_ARM_CYCLES 3749 /* INTIM * 64 / 76 * 4452 */

void runARM_VerticalBlank() {

    availableIdleTime = RAM[_INTIM] * INTIM_TO_ARM_CYCLES;

    if (gameState == nextGameState) {
        (*verticalBlank[gameState])();
    }

    // common to ALL VBs...
    // insert code here
}


Essentially 'availableIdleTime' now tells the ARM code exactly how many T1TC cycles it has available.

So at the beginning of ARM's VB and OS, I reset the timer


void VB_Game() {

    T1TC = 0;
    T1TCR = 1;


Then, when I have complex programming tasks, I make sure that things are compartmentalised into smaller processing blocks.  I only process that particular block *this frame* if there's sufficient time.

void processBoardSquares() {
    while (T1TC < availableIdleTime - 10000) {

That "10000" is a placeholder. But basically it's me guaranteeing that no processing unit will take more than 10000 ARM cycles.  The loop will continue processing units until there's not enough time to do the largest, at which point the OS/VB returns.

 After that return the 6502 code will wait for INTIM to reach 0.

Now, back to the "10000".  When I get the game more refined, I use the Gopher profiler to give me actual values for the maximum time used by those processing units. I literally profile the game, find the largest time user and either subdivide it to smaller chunks, or accept that's the time that I use in place of the 10000.

What this does is maximise time for ARM stuff, without worring about overflow in the VB or OS.

It's a refinement of my previous systems, where I kind of guessed the availableIdleTime (I set it to about 100000). Now, instead, it's calclated from INTIM passed from the 6502 code.  So the 6502 stuff can use variable amounts of VB or OS, as required, and whatever is left over is available for the ARM code.

It's pretty neat.  My use-case is I have an 880-element 'board' , every cell of which needs to be processed every game frame.  I simply don't have enough time to process 880 in one frame (60Hz), and yet I still want it to run as fast as possible. So, this way, the board scan uses ALL of the ARM's available time in OS/VB. If it needs it.

Very efficient, and quite elegant with the 6502 saying, pretty much, "here's how much time is left in the OS/VB... use it as you wish".


Andrew Davie

Following issues with the game running on DEV carts with 60MHz ARM processors, I re-added the 60MHz autodetect.  In the scheduler, we need to know the correspondence between INTIM ticks (64 cycles of 6502 each), and ARM CPU cycles.  Previously I hardwired this for 70MHz, but now we're dealing with different ARM clocks, I've changed the code to calculate the ARM cpu cycles/INTIM ticks correspondence.

Here's the relevant code, but also my entire autodetect 'package' included in the attached file.

        // Calculate ARM cycles per TIA clock tick.
        // e.g., 70MHz --> 70000000 cycles/sec
        // /60 = 1,166,666 cycles/frame
        // /262 = 4,452 cycles/scanline
        // /76 = 58 cycles/TIA clock
        // *64 = ~3712 cycles per INTIM tick

        // Following code does the above, avoiding overflow

        armCycles = ((armFrequency >> 8) * (0x10000 / 60)) >> 8;    // cycles/frame
        armCycles = (armCycles * (0x10000 / 262)) >> 16;            // cycles/scanline
        armCycles = (armCycles * (64 * 0x10000 / 76)) >> 16;        // cycles/INTIM


Hopefully the above is self-explanatory.  We end up with 'armCycles' being a multiplier of the available INTIM clocks left unused in VB or OS when the ARM code starts running. To calculate available ARM time that is usable, we multiply by 'armCycles'.

So...

    availableIdleTime = RAM[_INTIM] * armCycles;

So, in theory we now have complete autodetect for SECAM/PAL/NTSC TV system and 60MHz/70MHz ARM speed
 in one package.  In theory, again, the scheduler should now run properly on all variants - provided the various tolerances for cycles allowed for each routine are correctly calculated by yours truly.