# The ZX Spectrum Reverse Engineering and Clone Desgin Blog

## VSync Pulses

#### Mar 27, 2007

The vertical sync for field 1 comprises of a sequence of 6 short pre-equalising pulses, a sequence of 5 long synchronisation pulses and a sequence of 5 short post-equalising pulses.

A pulse has a duty cycle of 32us, and both types are specified as:

Pulse Active Low (us) High (us)
Short230
Long302

We will need two timers, one for 2us and one for 30us, to produce the correct length low pulse. At the end of a low pulse we can simply put the vertical sync line high until the start of the next pulse. We're generating one pulse per half scanline, as VC is counting half scanlines, and so we'll get the expected 32us duty cycle.

#### 2us Timer

Lets consider how many HC clock cycles elapse during 2us:

2x10-6 x 1/7x106 = 14

14 is an awkward number, so if we round to the nearest power of 2 we get 16.

16 x 1/7x106 = 2.28us

Our half lines start when HC is 304 and HC is 80. 2.28us later HC will be 304 + 16 and 80 + 16. We need to see what happens to HC at a binary level during this 2.28us transition:

Decimal Binary
3041 0011 0000
3191 0011 1111
3201 0100 0000

800 0101 0000
950 0101 1111
960 0110 0000

Notice that HC4 goes low 16 HC clock cycles after the start of our half scanline. We have our 2us timer!

#### 30us Timer

Taking the same approach, lets consider how many HC clock cycles elapse during 30us:

30x10-6 x 1/7x106 = 210

210 again is an awkward number, so if we round to the nearest power of 2 we get 208.

208 x 1/7x106 = 29.7us

So 29.7us after the start of our half scanline, HC will be (304 + 208) MOD 448 = 64 and (80 + 208) MOD 448 = 288.

Looking at this transition in binary:

Decimal Binary
3041 0011 0000
640 0100 0000

800 0101 0000
2881 0010 0000

This transition isn't as easy to spot as the 2.28us. By inspection we see HC8 changes state, but HC4 to HC7 change dramatically.

I almost fell into the trap of thinking that we could look for a change to HC8 AND HC6 for the 304 to 64 transition, but thankfully I spotted that this would get confused by the half line start point 80. Instead, it would make sense to look for HC8HC7 • HC6HC5HC4

A similar full decode of HC4 to HC8 should be carried out for HC=288, and as a lucky twist, our horizontal blank/sync start comparator is checking HC4 to HC7 against 2 (0010) which is exactly what we need.

#### VSync Summary

To generate our long and short VSync pulses we need:

1. Our half-line start signals: HC=80 and HC=304
2. A 2us has elapsed signal: HC4 going low.
3. A 30us has elapsed signal: Check HC4 to HC8 against 00100 and 10010.
4. A long/short pulse selector: Determined by the scanline we're on.

Now we just need to put that all together.....

At the risk of being a little dry, the following logic emerges:

 VCclk = HC=5 • HC8 + HBlank T2us = HC4 T30us = (HC8 • HC7 • HC6 • HC5 • HC4) + (HC=2 • HC8) = (HC8 + HC7 + HC5 + HC4) • HC6 + (HC=2 • HC8)