Quick-links:
There are only two sync routines:
There is a global variable, g_sync_handler, which holds a pointer to the sync routine to be called on the next 64us interrupt.
The Horizontal Sync routine is very simple.
It outputs the 4us H-Sync pulse (during which it calls the selected character polling routine), calls the selected Character Processing routine and then
sleeps until the 'Display Start' interrupt before finally returning to the main loop.
[ This 'Display Start' point is no longer found using sleep/wake-on-interrupt. See the Basic Operation section for details. ]
The Field Sync routine is a more complex affair.
This sync routine actually runs for eight scanlines. It outputs all three of the the sync pulse sections of the Field Blanking Period (Pre-equalising [short pulses], Vertical Sync [broad pulses] and Post-equalising [short pulses]).
[ The NTSC Field Sync is nine scanlines long. ]
During this routine, the timer interrupts are modified:
[
NTSC uses a 63.5us scanline, consequently a 31.75us half-scanline.
The simpler sleep/wake-up-on-interrupt method is still used for timing the pulses. Its overhead is not a problem in the v-sync routine.
]
Before returning to the main routine, both these interrupts are restored to their usual timings.
The routine outputs 6 'short-sync' half-scanlines, 5 'broad-sync' half-scanlines and finally 5 'short-sync' half-scanlines. During each of these half-scanlines, the character poll and processing routines are called.
[ The NTSC v-sync routine outputs 6 short-sync, 6 broad-sync and finally 6 short-sync half-scanlines. ]
This means that the character poll frequency is temporarily doubled.
This has no detrimental effect at all - it's purely a simple (codewise) way of ensuring that the characters are polled/processed at least as often as once-per-scanline.
Note: This routine does not cover the entire Field Blanking Period - only the specialised pulse sections. The Field Blanking Period continues until half-way through scanline 23. The remaining scanlines in the Blanking Period should have H-Syncs at the start of the scanlines, but no display signals. [ NTSC continues the blanking period up to (and including) scanline 20 ]