diff options
Diffstat (limited to 'anim/scenes/zoom.zig')
| -rw-r--r-- | anim/scenes/zoom.zig | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/anim/scenes/zoom.zig b/anim/scenes/zoom.zig new file mode 100644 index 0000000..6eb3248 --- /dev/null +++ b/anim/scenes/zoom.zig @@ -0,0 +1,50 @@ +//! One line of C, all the way down: the instructions it is, the registers +//! they change, the words they write on the stack. Every level is on +//! screen the whole time; the scene steps an instruction at a time and +//! says which one to look at. +//! +//! How it moves (design/motion.md): a step is an instruction: the pc's +//! row fires in the disassembly, the registers it changed strike, then +//! the stack, and the mark walks down the listing. The camera goes +//! nearer once, announced (its target framed, the rest cleared first), +//! for the call: the eight bytes of the way back, and the frame built +//! under them; then back, and the line is where it was, one call deeper. +const anim = @import("anim"); +const canim = @import("canim"); +const recordings = @import("recordings"); + +pub const background = canim.background; +pub const scene = anim.Live(frame, struct {}); + +fn frame(s: *anim.Frame) void { + const rec = canim.open(recordings.steps); + const t = canim.theme.time; + // fib(5), about to run line 6: `return fib(n-1) + fib(n-2);`. Its first call is fib(4). + const call = rec.call("fib", 1); + var at = canim.cursor(s, rec, rec.lineMoment(6, rec.call("fib", 0).enter)); + const end = rec.lineMoment(6, call.enter); + at.journey(.moment, end); + // A step is an instruction: it fires on the pc's row. + at.levels = .{ .fire = .disasm, .source_rows = 14 }; + const split = canim.layout.split.none; + const ctx = canim.context(s, at, .{ .split = split }); + var cam: canim.Camera = .{}; + var says: canim.Narrator = .{}; + + const joins = canim.ties(s, .{ .cursor = at }); + canim.establish(s, .{ ctx.clock, ctx.source, ctx.disasm, ctx.registers, ctx.stack, ctx.backtrace, joins }); + says.line(s, "A line of C is a handful of instructions.\nThis one calls `fib(n-1)`: follow it down."); + // The CPU's level: an instruction a step. The argument is worked out in `rdi`. + at.walk(s, .moment, call.enter - 1); + + says.line(s, "`call` writes eight bytes on the stack: the way back.\nThe new frame saves `rbp` under them, then its own `n`."); + // Nearer, for the bytes: the stack alone, twice the size. + cam.go(s, canim.layout.stack, 2, .{ ctx.source, ctx.disasm, ctx.registers, ctx.backtrace }); + at.walk(s, .moment, call.enter + 4); + cam.back(s, .{ ctx.source, ctx.disasm, ctx.registers, ctx.backtrace }); + + says.line(s, "Eleven instructions and three words of stack later\n`fib(4)` begins: the same lines, one call deeper."); + at.walk(s, .line, end); + s.wait(t.before_cut); + cam.apply(s); +} |
