Goal: name the count and the value compared against it
Even if you first encountered a timer as an API that calls a function later, its behavior depends on a progressing count and a comparison value. The objective here is to distinguish physical count, virtual count, and the CVAL deadline, then calculate whether a request is signaled. Build on virtual interrupt delivery. The lesson selects Generic Timer concepts while excluding 64-bit wraparound and the complete set of architectural extensions, using bounded nonnegative integers for explicit examples.
Advance the counter with model operations. Neither playback speed nor seconds spent looking at the screen changes virtual time. Substituting the browser's setTimeout for this counter would make results depend on tab scheduling and load. Equal numerical inputs and operation sequences should give equal results. Pausing playback therefore stops observation, while an advance-count operation changes the modeled clock. Keeping those actions distinct prevents display scheduling from being mistaken for a property of the timer being studied.
Virtual count subtracts the selected offset
In this configuration, virtual count equals physical count minus offset. Physical count 120 with offset 100 gives virtual count 20. Adding the values or interpreting that 20 as the amount of CPU time consumed by the VM answers a different question. The offset changes the time origin; it is not the timer deadline. The demonstration shows each VM's offset and the virtual count derived from the same physical count so that the subtraction remains visible.
With A's offset at 100 and B's at 90, physical count 120 produces virtual counts 20 and 30 respectively. Identical CVAL values of 25 then correspond to different physical deadlines. Changing an offset can change expiry even while CVAL is unchanged. Label the count domain in a test record. A log saying only that the value reached 25 is ambiguous unless it identifies physical count or the virtual count of a particular VM. Without that identity, another observer cannot reproduce the comparison.
| Physical count | VM | Offset | Virtual count | CVAL=25 reached |
|---|---|---|---|---|
| 120 | A | 100 | 20 | No |
| 120 | B | 90 | 30 | Yes |
| 125 | A | 100 | 25 | Yes |
Expiry is different from a signaled request
Within this model's numerical range, the timer has expired when virtual count is greater than or equal to CVAL. Equality matters: virtual count 25 with CVAL 25 is already due. Expiry alone does not establish delivery. Timer enable and masking, interrupt-controller eligibility, and the running virtual context are separate gates. A single fired flag that combines every gate removes the evidence needed to locate where progress stopped.
For virtual count 30, CVAL 25, ENABLE set, and IMASK set, the deadline is past but the timer output is suppressed. Clearing IMASK reevaluates the existing condition; it does not require counting down again from a future deadline. A disabled timer is not treated as producing an enabled output. Architectural status fields have validity conditions, so the model's computed deadline predicate must not be advertised as a literal reading of ISTATUS under every possible hardware configuration.
A small calculation with boundary tests
The code separates the deadline predicate due from the output signal. Advancing physical count through 124, 125, and 126 with offset 100 and CVAL 25 produces false, true, and true for due. Then retain count 126 and mask the timer: due stays true while signal becomes false. The calculation uses bounded teaching values. It does not read timer registers or measure the actual machine, so arithmetic validation and hardware observation remain distinct kinds of evidence.
As a deliberate defect, replace the comparison with greater-than and see the equality case delayed by one count. Reverse the offset sign and expiry appears too early. Testing only one sufficiently large count can miss both mistakes because the final predicate is true anyway. Use just-before, exact-deadline, and just-after cases, then vary enable and mask independently. These counterexamples are more informative than many repetitions of one passing case, despite requiring only a very small amount of code.
function timer(physical, offset, cval, enabled, masked) {
const virtual = physical - offset;
const due = virtual >= cval;
return {virtual, due, signal: enabled && !masked && due};
}
console.log([124,125,126].map(p => timer(p,100,25,true,false).due).join(','));
console.log(JSON.stringify(timer(126,100,25,true,true)));
Match units and distinguish a deadline from a duration
The value 25 means 25 counts in the selected counter domain, not 25 seconds. Conversion to elapsed time requires a frequency and an explicit rounding policy. At a teaching frequency of one thousand counts per second, ten milliseconds corresponds to ten counts. A different frequency or fractional interval can require a different conversion. The demonstration fixes count units and never presents the animation delay between frames as the period of the modeled hardware timer.
CVAL is also not simply a relative waiting duration. At virtual count 80, a target ten counts later is a comparison value of 90 in this elementary example. Writing 10 directly would select a deadline already in the past. State the meaning of the value before writing the arithmetic expression. This separates absolute deadlines from durations. The next context lesson considers whether a delayed periodic handler schedules from current time or from the previous planned deadline, which is a policy decision beyond this subtraction.
State the policy for a VM that is not running
In this model, advancing physical count also advances each VM's derived virtual count while that VM is not executing. Lack of scheduled CPU time does not freeze the selected virtual clock. A's deadline can therefore pass while B runs. When A returns, evaluate the current count, CVAL, enable state, and mask. Do not pretend that A's handler executed while its vCPU was absent; passage of modeled time and guest execution are separate events.
A design that pauses a virtual clock would need a different policy, including how offsets change. This model's trace is not a universal assertion about every hypervisor's time management. Fix the selected policy before testing context switching, then verify that each VM retains its own timer context. In your notes, list the condition that advances time separately from the condition that delivers work to a guest. That distinction remains useful even in this deliberately small configuration with only one vCPU per VM.
Exercise: predict count, expiry, and output separately
Set physical count to 150, A's offset to 100, CVAL to 50, ENABLE to one, and IMASK to zero. Predict virtual count, expiry, and output in order: 50, due, and signaled. Set only IMASK to one and count and expiry stay unchanged while output is suppressed. If you then change interrupt-controller masking, distinguish timer output from guest acknowledgement. The essential explanation identifies the precise column where a transition stops rather than calling every blocked path an unexpired timer.
Add regression cases at physical counts 149 and 151, then use another VM with offset 90. Repair the count domain or comparison rule rather than rewriting CVAL until one display looks correct. Also check that pausing observation does not secretly advance model time. These small counterexamples prepare you to read a timer as a combination of clock origin, comparator, output controls, and delivery path, instead of treating it as an opaque function that happens to call something later.