Make a host, step 2: frames and the budget
A Terse32 program runs at the same speed on every host. Time comes in frames, 60 a second, and in each frame a program can do a fixed amount of work: 125,000 units. In this step the loop of step 1 becomes a frame loop.
The frame loop
Keep a budget, a signed number, and a frame counter, both 0 at reset. Then, for each frame:
1. Add 125,000 to the budget. 2. Run instructions while the budget is above zero. Each costs 1 unit if it was fetched from ROM, 4 if from RAM. 3. End the frame: if the budget is above zero, set it to 0; add 1 to the frame counter.
An instruction that starts with budget left always runs to its end, so the budget can end below zero. That debt is carried into the next frame: this is the carry rule. What is left above zero is dropped. When the budget runs out the program is simply paused; the next frame goes on with its next instruction.
A host with a screen starts a frame every sixtieth of a second. If it falls behind, it lets the program's time fall behind too: it never runs extra frames to catch up. A test runs the frames as fast as it can.
Calls cost too
An ecall costs its own 1 unit (or 4 from RAM) plus the call's charge. Every call on this page is charged 4, as is a call you do not have and a call that fails.
Three calls
100h,wait_frame: the program has finished this frame's work. End the frame now (the budget left above zero is dropped, as at any frame's end), and return the frame counter plus 1: the number of the frame in which the program goes on.102h,frame_count: the frame counter. It is N all through frame N.101h,next_event: the next input event. Inputs come with step 4, so for now the queue is always empty: check thata0and the 3 bytes after it are in RAM (else returnFFFF_FFFEh) and return 0.hello, in the next step, calls it every frame.
exit now also ends its frame: the frame still counts, and nothing runs after it.
Check yourself
This program calls frame_count 100,000 times, then waits for the next frame and exits with what wait_frame returned:
_start: li t0, 100000
li a7, 0x102 # frame_count
1: ecall
addi t0, t0, -1
bnez t0, 1b
li a7, 0x100 # wait_frame
ecall
li a7, 0x106 # exit with its result
ecall
As a file:
xxd -r -p > frames.terse <<EOF 544552530001240000010000000000006672616d657300 000000000000000000e1629382026a9308201073000000 fd12e39d02fe93080010730000009308601073000000 EOF terse run frames.terse --frames 10 echo $?
prints 6. Each pass of the loop costs 7 units: the ecall 1 + 4, the other two 1 each. 100,000 passes are 700,000 units, so the loop ends in frame 5 (frames 0 to 4 give 625,000), wait_frame returns 6, and the program exits in frame 6. If your host says 3, it does not charge calls; 5, wait_frame returns the counter instead of one more; 1, it has no budget at all.
How much code
In tcore the frame loop is about 25 lines of C (main.c), and the whole System device, all ten of its calls, about 60 (machine.c).
What you have now
A machine that runs programs at exactly the reference's speed. Next, give it a screen: Make a host, step 3: a driver shows hello.
The detail is in The Terse32 machine, section "Frames and the budget" (SPEC §frames); The system device, sections "wait_frame", "next_event", "frame_count and unix_time" and "exit" (SPEC §system); and Charges and limits, section "Charges" (SPEC §charges).