summaryrefslogtreecommitdiff
path: root/anim/scenes/zoom.zig
blob: ee24e7d0ec0a0b678427acd8cc263d199c52aa94 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//! 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);
    // 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 = ctx.ties(s);
    canim.establish(s, .{ ctx.clock, ctx.registers, ctx.stack, ctx.backtrace, ctx.source, ctx.disasm, 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, split.stack, 2, .{ ctx.source, ctx.disasm, ctx.registers, ctx.backtrace });
    // Under the camera the stack holds still: six free words above rsp for the new frame.
    ctx.stage.holdStack(s, at.moment(s), 6);
    at.walk(s, .moment, call.enter + 4);
    s.wait(canim.theme.time.beat);
    ctx.stage.releaseStack(s);
    cam.back(s, .{ ctx.source, ctx.disasm, ctx.registers, ctx.backtrace });

    says.line(s, "Nine instructions and three words of stack later\n`fib(4)` begins: the same lines, one call deeper.");
    at.walk(s, .line, end);
    says.close(s);
    cam.apply(s);
}