I'm writing a simulation of a (very slow and primitive) processor.
For example: let's say the clock speed is a constant 1 hz. I assume this means that 1 instruction can/will be processed every second. Some instructions take longer than others. Adding 1 + 0 takes less time than 1 + 7. (The latter causes a ripple of carry bits which takes a non-zero amount of time.)
I need to be able to carry out instructions only after all other instructions have finished.
Do I need to:
- time how long the longest instruction takes and set the clock speed to greater than that?
- create a stateful watcher that won't allow future a instruction to be executed until the previous is complete
- Am I misunderstanding the problem completely?
In #1, it seems like I'm still risking a race condition of instruction being incomplete before the next begins. In #2, it seems like I'm risking an unpredictable/variable clock speed which could cause me issues later.
How can I resolve this? Are there any hints in how a real processor handles this issue?