summaryrefslogtreecommitdiff
path: root/anim/scenes/intro_memory.zig
blob: 07e694a924c53da4622de3ff3567d81f76c41da0 (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
//! The introduction, chapter I: memory (design/motion.md, "A scene: the
//! introduction", I1 to I10; mocks 29, 30, 34, 37). The frame built from
//! its bytes up, on the `list` recording stopped in `reverse`'s prologue,
//! at its first call (line 17: the push chapter II walks; the 128 bytes at
//! rsp then are the load's but for `prev`, not yet stored), each part in
//! the place it keeps: the 128 bytes at the top
//! of the stack where the stack panel will stand, under the camera at x2;
//! made words (`regroup`); three of them addresses; then the address space
//! to scale in the program column (`segments`), each cluster enlarged, its
//! text beside it; back to the working frame, the dump tied to its place,
//! a visit at x4, `ink`, and three chains.
const std = @import("std");
const anim = @import("anim");
const canim = @import("canim");
const recordings = @import("recordings");

pub const background = canim.background;
pub const scene = anim.Live(frame, struct {});

const t = canim.theme.time;

fn f(frames: f32) f32 {
    return frames / 60;
}

/// Clock `v` from 0 to `frames`, linear, from the cursor; the cursor after it.
fn run(s: *anim.Frame, v: anim.Value, frames: f32) void {
    s.play(.{v.to(0)}, .{ .seconds = 0 });
    s.play(.{v.to(frames)}, .{ .seconds = f(frames), .ease = anim.ease.linear });
}

/// The same, the cursor left where it was.
fn start(s: *anim.Frame, v: anim.Value, frames: f32) void {
    const at = s.cursor;
    run(s, v, frames);
    s.cursor = at;
}

/// The first moment from `from` whose instruction reads `text`.
fn on(rec: *canim.Recording, from: u64, text: []const u8) u64 {
    var buf: [256]u8 = undefined;
    var i = from;
    while (i < rec.end()) : (i += 1) if (rec.instruction(i, rec.register(i, "rip"), &buf)) |here| if (std.mem.indexOf(u8, here.text, text) != null) return i;
    return from;
}

fn frame(s: *anim.Frame) void {
    const rec = canim.open(recordings.list);
    const reverse = rec.call("reverse", 0);
    // reverse's first call (line 17, the tracer's `__zt_var` for its first
    // local): the push chapter II walks. The recording begins after
    // reverse's own `push rbp` (`--from` breaks past the prologue), so it is
    // the first push of reverse's that the recording has.
    const moment = on(rec, reverse.enter, "call");
    const ui = canim.ui;
    const at = canim.regroup.at;

    const space = s.new(canim.Segments.init(s, rec, moment));
    const dump = s.new(canim.Words.init(s, rec, moment));
    const place = s.new(canim.segments.Place{ .segments = space, .addr = rec.register(moment, "rsp"), .x = at.bracket, .y0 = at.top + 2, .y1 = at.top + 16 * 24 - 2, .clock = s.value(-1), .quiet = s.value(0) });

    // `ink`'s echoes: the walked word's band first, then the rest at a run, 2 f apart.
    const values = canim.Words.read(rec, moment);
    var order: [canim.regroup.words]usize = undefined;
    order[0] = dump.walked;
    var n_order: usize = 1;
    for (0..canim.regroup.words) |i| if (i != dump.walked) {
        order[n_order] = i;
        n_order += 1;
    };
    for (order, 0..) |i, k| {
        space.echoes[k] = .{ .addr = values.words[i], .shows = s.value(0) };
    }
    space.n_echoes = order.len;

    const objs = .{ s.custom(space, .{ .x = -640, .y = -360, .scale = ui.scale }), s.custom(dump, .{ .x = -640, .y = -360, .scale = ui.scale }), s.custom(place, .{ .x = -640, .y = -360, .scale = ui.scale }) };
    inline for (objs) |o| s.play(.{o.fadeIn()}, .{ .seconds = 0 });

    // The camera at x2 on the dump from the first frame (I1 to I3).
    const near = ui.framing(.{ .x = 888, .y = 72, .w = 896, .h = 408 }, 2);
    var cam: canim.Camera = .{};
    cam.moves[0] = .{ .from = near, .to = near, .p = s.value(1) };
    cam.count = 1;
    cam.at = near;
    var says: canim.Narrator = .{};
    var asides: canim.Asides = .{};

    // I1: the bytes, a row at a time.
    s.wait(1.0);
    says.line(s, "A running program is bytes in memory:\nhere, 128 of them at the top of the stack.");
    run(s, dump.in, 7 * 12 + 18);
    s.wait(t.beat);

    // I2: eight a row; the return address walked, the other fifteen at a run.
    says.line(s, "Eight bytes make a word, stored low byte first:\nthe lowest byte is its last two digits.");
    run(s, dump.regroup, 386);
    s.wait(t.beat);

    // I3: three words that hold addresses, framed as read.
    says.line(s, "Some words are addresses:\nthey name a place in memory.");
    run(s, dump.framed, 66);
    s.wait(@max(says.read_by - s.cursor, 0));

    // Into the column: the caption leaves, the camera out to x1, the dump to its tag on the shelf.
    s.play(.{says.current.?.fadeOut()}, .{ .seconds = t.caption_out, .ease = canim.ease.exit });
    says.current = canim.caption(s, "");
    s.wait(t.caption_gap);
    cam.back(s, .{});
    run(s, dump.fold, 30);
    // Folded, the three frames go with the rows: unfolded, the dump is plain words again.
    s.play(.{dump.framed.to(-1)}, .{ .seconds = 0 });

    // I4: the whole, deliberately.
    const column_x = canim.theme.layout.state.x + 8;
    const l = space.layout(0, s.arena);
    asides.say(s, "Its address space, 128 TiB, to scale: mapped only at each end.", column_x, (l.top + l.foot) / 2);
    run(s, space.whole, 72 + 18 + 72);
    s.wait(t.beat);

    // I5: the top end, a box a cluster, each riding out as the one before lands.
    const clusters = @min(l.space.clusters.len, 6);
    var top_end: usize = 0;
    while (top_end < clusters and l.space.clusters[top_end].lo > canim.segments.user_end / 2) top_end += 1;
    asides.say(s, "Enlarged, the top end: the stack, the libraries and the tracer's ring, each band with its addresses and size.", column_x, l.boxes[0].y + 12);
    for (0..top_end) |i| {
        start(s, space.rides[i], 6 + 72 + 90 + 2 * 12 + 12);
        s.wait(f(78));
    }
    s.wait(f(90 + 2 * 12 + 12));
    // I6: the foot, the program by segment.
    for (top_end..clusters) |i| {
        asides.say(s, "At the foot, the program: its code, its data, its heap.", column_x, l.boxes[i].y + l.boxes[i].h / 2);
        run(s, space.rides[i], 6 + 72 + 90 + 2 * 12 + 12);
    }
    s.wait(t.beat);

    // Back to the working frame: the texts leave, the drawing to the stage's height, the dump unfolds.
    asides.leave(s);
    s.play(.{space.form.to(1)}, .{ .seconds = f(24), .ease = anim.ease.linear });
    s.play(.{dump.fold.to(0)}, .{ .seconds = f(30), .ease = anim.ease.linear });

    // I7: the dump's place: the tie bracket, the link onto the stack's box.
    says.line(s, "These 128 bytes are here,\nat the very top of the stack.");
    s.play(.{dump.placed.to(1)}, .{ .seconds = f(12), .ease = anim.ease.linear });
    s.cursor -= f(12);
    run(s, place.clock, 60);
    s.wait(t.beat);

    // I8: a visit at x4 to where the link lands, and back.
    says.line(s, "Were the bar the Eiffel Tower,\nthey would be an atom thick, at its top.");
    const landed = space.layout(1, s.arena);
    const head = canim.Segments.place(landed, place.addr) orelse .{ 0, 0 };
    cam.go(s, .{ .x = head[0] - 112, .y = 48, .w = 224, .h = 104 }, 4, .{});
    s.wait(t.beat);
    cam.back(s, .{});

    // I9: `ink`: the return address walked, its band framed; the rest at a run; the link quiets.
    const walked = values.words[dump.walked];
    const band = for (landed.space.bands) |b| {
        if (walked >= b.lo and walked < b.hi) break b;
    } else landed.space.bands[0];
    says.line(s, std.fmt.allocPrint(s.arena, "A word takes the ink of where it points:\n`{x}` is {s}'s code.", .{ walked, band.name }) catch "");
    const inking = s.cursor;
    start(s, place.quiet, 24);
    for (order[1..], 1..) |_, k| {
        s.cursor = inking + f(@floatFromInt(72 + 2 * (k - 1)));
        s.play(.{space.echoes[k].shows.to(1)}, .{ .seconds = f(4), .ease = anim.ease.linear });
        s.play(.{space.echoes[k].shows.to(0)}, .{ .seconds = f(8), .ease = anim.ease.linear });
    }
    s.cursor = inking;
    s.play(.{space.echoes[0].shows.to(1)}, .{ .seconds = f(6), .ease = anim.ease.linear });
    s.cursor = inking + f(72 + 2 * 16 + 12);
    s.play(.{space.echoes[0].shows.to(0)}, .{ .seconds = f(18), .ease = anim.ease.linear });
    s.cursor = inking;
    run(s, dump.ink, 72 + 2 * 16 + 12 + 18);
    s.wait(t.beat);

    // I10: the words to their field's left, then three chains, walked.
    says.line(s, "Follow an address and you reach another word:\na chain.");
    run(s, dump.left, 24);
    run(s, dump.chains, 36 * 2 + 30);
    s.wait(t.beat);
    // The chapter's last picture.
    says.close(s);
    cam.apply(s);
}