Is a CPU's instruction duration strictly synchronized to the crystal vibration? Or does the CPU just have to be sure to achieve an instruction before the next tick?
Neither. The instruction duration will be some number of clock ticks, but that number can vary based on the requirements of the instruction. For example, if an instruction can't make forward progress until a particular memory location is in the L1 cache, then the instruction will not be completed before the next clock tick. No forward progress on that instruction will be made until that happens.
But when the CPU does decide to do something, the basic method by which it does it is to set up its internal switches so that a particular piece of information goes to a particular portion of the CPU. The it waits for the input to arrive at that portion and the output to arrive at the next portion. This waiting portion is the purpose of the clock.
Imagine a physical circuit that takes two binary inputs and sums them, outputting the sum on some third set of wires. To do an addition, the CPU must arrange for the two numbers to be added to get to this adder and the outputs to go to, say, a CPU register latch. The CPU can't tell the latch to store the output until the inputs reach the adder, the adder produces the output, and the output reaches the latch. This is the purpose of the clock -- to set the wait time between arranging input to go somewhere and expecting the output to be ready to use.
The variance is not a function of the CPU, it's more related to the motherboard, temperature fluctuations, etc. How much accuracy do you need, over what period? – mpez0 – 2015-02-08T20:51:19.660
7
There are many different types of CPU. Most use a crystal which vibrates at a specific frequency to time themselves. Meaning that most are about as accurate as a digital wristwatch (which uses the same type of crystal to keep time). http://en.wikipedia.org/wiki/Crystal_oscillator
– krowe – 2015-02-08T21:08:17.200Whatever the variance, it is not a problem for my personal needs: I am a web developer, so it is far from me:) – Gael – 2015-02-08T21:10:04.900
Even if the crystal have certainly a stable frequency, my question is more specific to the cpu: Is a cpu instruction duration strictly synchronized to the crystal vibration? Or the cpu have just to be sure to achieve an instruction before the next tick? – Gael – 2015-02-08T21:16:41.647
The more I think about it, the more I like this question. – misha256 – 2015-02-08T21:49:32.710
5@Gael some instructions take hundreds of ticks, or clock cycles, to complete. – misha256 – 2015-02-08T21:50:26.463
1@Gael: Also, it's pretty common for CPUs to slow themselves down or speed themselves up, and they do it pretty often. Though I don't know exactly how that relates to ticks. – Mooing Duck – 2015-02-09T17:29:56.390
18Voting to close this as seeking product, service, or learning material recommendations? Really, people? – a CVn – 2015-02-09T19:41:40.950
30This is a **legitimate question seeking to clarify how hardware works.** – studiohack – 2015-02-09T20:34:35.677
Many computer systems speed up or slow down the CPU clock based on circumstances. For example, since heat produced is roughly in proportion to performance, the clock will often be slowed when the CPU gets too warm. Additionally, some portable systems run slower on battery (and, curiously, some run slower when the battery is removed). Generally these changes are accomplished by changing clock speed. But understand that most "modern" systems have some scheme for independently maintaining the accuracy of the "real time clock" that provides time services to the software, in spite of the above. – Daniel R Hicks – 2015-02-09T21:14:50.027
@krowe: Generally, high-frequency crystals are less precise than the low-frequency ones (the latter being the ones used for watches, among other things). Watches typically use 32kHz crystals, for example. So a microcontroller running at 20MHz won't be able to time things as precisely as a dedicated RTC (real-time clock) circuit. PCs can time things precisely because motherboards have a (completely separate from CPU) onboard RTC to keep track of time; plus they can help themselves with NTP if configured to do so. – Tim Čas – 2015-02-10T23:07:17.983