summaryrefslogtreecommitdiff
path: root/anim/scenes/intro_memory.zig
blob: 7b250982fccd44f7e122e6ed0819ca4d0b5c3a76 (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
//! The introduction, chapter I: memory (design/motion.md, "A scene: the
//! introduction", I1 to I10; mocks 29, 30, 34, 37), a function the scene
//! `intro` calls after its title (`chapter`). The frame built from
//! its bytes up, on the `intro` recording (the list program from main's
//! call of `reverse`) at reverse's first instruction, its `push rbp` (the
//! push chapter II walks; rsp holds the way back to main), 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 x2 framing the stack's box and the dump's top rows together
//! (the place marked `128 bytes` on the box), `ink`, and three chains.
const std = @import("std");
const anim = @import("anim");
const canim = @import("canim");
const intro = @import("intro.zig");

const t = canim.theme.time;
const f = intro.f;
const run = intro.run;
const start = intro.start;

/// The chapter's last picture, which chapter II opens on: the address
/// space, the 128 bytes and their place's tie (each an object of its
/// own), the last text, and `e`, the frame it has been read by.
pub const One = struct {
    space: *canim.Segments,
    dump: *canim.Words,
    objs: [3]anim.Obj,
    caption: anim.Obj,
    e: f32,
};

/// From the cursor, where its first text arrives.
pub fn chapter(s: *anim.Frame, rec: *canim.Recording) One {
    // reverse's first instruction (its call's first moment): its `push rbp`, which chapter III walks.
    const moment = rec.call("reverse", 0).enter;
    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), .tick = 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 = [3]anim.Obj{ 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), with the
    // room over it that the walked word's bytes and offsets fly in, and its
    // sixteen rows over the stage's foot.
    const near = ui.framing(.{ .x = 888, .y = 66, .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 (18 f apart), a text's lead (60 f) after it lands, not
    // the first's 120: little "here" over an empty stage, and they come while it is read.
    const said_at = s.cursor;
    says.line(s, "A running program is bytes in memory:\nhere are 128 of them.");
    const lead_end = s.cursor;
    s.cursor = said_at + t.caption_in + t.caption_lead;
    run(s, dump.in, 7 * canim.regroup.rows_apart + 18);
    s.cursor = @max(s.cursor, lead_end);
    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, canim.regroup.regroup_frames);
    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, 48);
    // 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, then the libraries, each file 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(48), .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.");
    // The rows' addresses take the stack's ink as the link's head lands and its band is framed (42), 12 f.
    const placing = s.cursor;
    run(s, place.clock, 60);
    const placed_at = s.cursor;
    s.cursor = placing + f(42);
    s.play(.{dump.placed.to(1)}, .{ .seconds = f(12), .ease = anim.ease.linear });
    s.cursor = placed_at;
    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.");
    // At x2: the stack's box with its names, the link, and the dump's top rows to a byte's edge,
    // the place marked on the box (`128 bytes`), nothing cut at the frame's edge but the rows running on.
    const landed = space.layout(1, s.arena);
    cam.go(s, .{ .x = 280, .y = 44, .w = 960, .h = 448 }, 2, .{});
    s.play(.{place.tick.?.to(1)}, .{ .seconds = f(12), .ease = anim.ease.linear });
    s.wait(1.0);
    s.play(.{place.tick.?.to(0)}, .{ .seconds = f(12), .ease = anim.ease.linear });
    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: a chain.\n`→` points to a word, `←` holds a value.");
    run(s, dump.left, 24);
    run(s, dump.chains, 36 * 2 + 30);
    // The chapter's last picture, held until its text is read: then the seam.
    s.wait(@max(says.read_by - s.cursor, 0));
    cam.apply(s);
    return .{ .space = space, .dump = dump, .objs = objs, .caption = says.current.?, .e = s.cursor };
}