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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
|
//! The introduction, chapter II: the stack (design/motion.md, "A scene: the
//! introduction", II1 to II4, here six beats; mock 31). It opens on chapter
//! I's last picture, at the same moment of the `intro` recording (reverse's
//! first instruction, its `push rbp`): the address space and the dump's link
//! fade, the words glide into the stack panel's rows where it stands in the
//! working frame (sixteen of them and four `free` over rsp, the window held
//! there so that nothing in the panel moves while the frame is made) and
//! become it; the convention said there (lower addresses drawn higher), the
//! offsets coming down the rows. rsp's and rbp's rows arrive over the stack,
//! rsp's pin landing on the top word. The call first: the way back main
//! left framed, main's three rows round its call arriving in the program
//! column, the word aimed at the row it returns to (`aim`, drawn head first,
//! standing at 60% through the push). Then reverse's first rows under
//! main's (the disassembly) and `push rbp` walked: the pc's mark on its
//! row, the touch to the word, rbp's value carried to it and struck, rsp's
//! pin climbing. `mov rbp, rsp` and `sub rsp` walked: the frame; its roles
//! (the way back named) and spans, a `column` each, the panels' tags last
//! (`frame up`). Then the registers built over it a row at a time, their
//! tag last. The text stands beside on the left, in the program column,
//! over the code.
//!
//! What a beat has not reached yet is under the page (`Cover`, and the
//! stack's own `reveal`), lifted as the beat says it: the context's panels
//! draw every column at once.
const std = @import("std");
const anim = @import("anim");
const sk = @import("sketch");
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;
const row = canim.theme.metric.context.row;
const tag = canim.theme.metric.context.tag;
const pad = canim.theme.metric.context.pad;
fn f(frames: f32) f32 {
return frames / 60;
}
fn place(s: *anim.Frame, data: anytype) anim.Obj {
return s.custom(data, .{ .x = -640, .y = -360, .scale = canim.ui.scale });
}
/// The page over a part of a panel the beat has not reached: lifted (its fade out) as the beat says it.
const Cover = struct {
box: canim.Rect,
pub fn draw(c: *const Cover, fr: *sk.Frame, placed: anim.Placed) void {
(canim.ui.Ui{ .f = fr, .at = placed }).fill(c.box, canim.theme.color.page);
}
};
/// A value framed as read (6 f in with its object's fade).
const Read = struct {
box: canim.Rect,
pub fn draw(r: *const Read, fr: *sk.Frame, placed: anim.Placed) void {
(canim.ui.Ui{ .f = fr, .at = placed }).read(r.box, 1);
}
};
fn cover(s: *anim.Frame, box: canim.Rect) anim.Obj {
const o = place(s, s.new(Cover{ .box = box }));
s.play(.{o.fadeIn()}, .{ .seconds = 0 });
return o;
}
/// A panel's tag and rules under the page, for `frame up`.
fn chrome(s: *anim.Frame, b: canim.Rect, strip: bool) [5]?anim.Obj {
const w = canim.theme.stroke.rule;
return .{
cover(s, .{ .x = b.x, .y = b.y, .w = b.w, .h = tag }),
cover(s, .{ .x = b.x, .y = b.y + tag, .w = w, .h = b.h - tag }),
cover(s, .{ .x = b.x + b.w - w, .y = b.y + tag, .w = w, .h = b.h - tag }),
cover(s, .{ .x = b.x, .y = b.y + b.h - w, .w = b.w, .h = w }),
if (strip) cover(s, .{ .x = b.x + b.w - canim.theme.strip.cost, .y = b.y + tag, .w = canim.theme.strip.cost - w, .h = b.h - tag - w }) else null,
};
}
/// Lifts `covers` one after another, `apart` frames apart, `each` frames each, from the cursor; the cursor after the last.
fn lift(s: *anim.Frame, covers: []const ?anim.Obj, apart: f32, each: f32) void {
const at = s.cursor;
var end = at;
for (covers, 0..) |o, i| if (o) |obj| {
s.cursor = at + f(apart * @as(f32, @floatFromInt(i)));
s.play(.{obj.fadeOut()}, .{ .seconds = f(each), .ease = anim.ease.linear });
end = s.cursor;
};
s.cursor = end;
}
/// A value at once, from the cursor.
fn set(s: *anim.Frame, v: anim.Value, to: f32) void {
s.play(.{v.to(to)}, .{ .seconds = 0 });
}
/// 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 });
}
fn frame(s: *anim.Frame) void {
const rec = canim.open(recordings.intro);
const reverse = rec.call("reverse", 0);
// Chapter I's moment: reverse's first instruction, `push rbp`; then `mov rbp, rsp` and `sub rsp, 0x18`.
const moment = reverse.enter;
const framed = moment + 3;
var at = canim.cursor(s, rec, moment);
at.journey(.moment, framed);
at.levels = .{ .fire = .disasm, .source_rows = 14, .introduction = true };
var split = canim.layout.split.none;
split.backtrace.h = 0;
const ctx = canim.context(s, at, .{ .split = split });
const g = layout(ctx.stage, rec, moment);
const sb = g.stack;
const rb = ctx.stage.first.registers;
const rsp = g.rsp;
const first = g.first;
const top = g.top;
const slots = canim.layout.rowsIn(sb.h);
// The window held where it is, four `free` words over rsp: the push and `sub rsp` fill them.
hold(s, ctx.stage, moment);
// The panels and the page over what they have not said yet, first: chapter I's
// picture, the words gliding into the stack among them, draws over them.
// The stack, its offsets, roles and spans under the page until their beats.
// (Rows 4 f apart: the offsets come down while the convention is read.)
const reveal: canim.Stack.Reveal = .{ .offsets = s.value(-1), .roles = s.value(-1), .spans = s.value(-1), .apart = 4 };
const stack = canim.add(s, canim.Stack{ .cursor = ctx.cursor, .stage = ctx.stage, .reveal = reveal });
// The registers, under the page: rbp's and rsp's rows (the scene's two, the first two), the rest, the chrome.
s.play(.{ctx.registers.fadeIn()}, .{ .seconds = 0 });
var regs_rows: [16]?anim.Obj = @splat(null);
for (0..@min(canim.layout.rowsIn(rb.h), 16)) |k|
// (From the third row on 2 higher: the rule over the folded rows goes with them.)
regs_rows[k] = cover(s, .{ .x = rb.x + 2, .y = rb.y + tag + pad + row * @as(f32, @floatFromInt(k)) - @as(f32, if (k >= 2) 2 else 0), .w = rb.w - 4, .h = row + @as(f32, if (k >= 2) 2 else 0) });
const regs_chrome = chrome(s, rb, false);
// The pins' lane: rsp's row's lifted with rsp, the rest (rbp's pin) with the push.
var lane: [32]?anim.Obj = @splat(null);
const rsp_row: usize = held_free;
for (0..@min(slots, 32)) |k| lane[k] = cover(s, .{ .x = sb.x + 2, .y = top + row * @as(f32, @floatFromInt(k)), .w = canim.theme.metric.slot.lane_end - 2, .h = row });
const stack_chrome = chrome(s, sb, true);
// Reverse's first rows, the disassembly under main's: its rows with the push, its tag and strip last.
const disasm_chrome = chrome(s, ctx.stage.first.disasm, true);
// Chapter I's last picture: the address space, the words with their chains, the link standing.
const space = s.new(canim.Segments.init(s, rec, moment));
const dump = s.new(canim.Words.init(s, rec, moment));
dump.panel = .{ .x = sb.x, .top = top, .first = first };
const at_ = canim.regroup.at;
const tie = s.new(canim.segments.Place{ .segments = space, .addr = rsp, .x = at_.bracket, .y0 = at_.top + 2, .y1 = at_.top + 16 * 24 - 2, .clock = s.value(60), .quiet = s.value(24) });
for ([_]anim.Value{ space.form, dump.placed }) |v| set(s, v, 1);
set(s, space.whole, 999);
for (space.rides) |v| set(s, v, 999);
for ([_]anim.Value{ dump.in, dump.ink, dump.chains }) |v| set(s, v, 999);
set(s, dump.regroup, canim.regroup.regroup_frames);
set(s, dump.left, 24);
const objs = .{ place(s, space), place(s, dump), place(s, tie) };
inline for (objs) |o| s.play(.{o.fadeIn()}, .{ .seconds = 0 });
// main's rows, in the program column over reverse's; the aim joins them to the word.
const back_y = rowY(top, first, rsp);
const main_rows = s.new(canim.descent.Caller{ .rec = rec, .at = moment, .call = rec.register(moment - 1, "rip"), .box = g.caller, .in = s.value(-1), .faint = s.value(0) });
s.play(.{place(s, main_rows).fadeIn()}, .{ .seconds = 0 });
const main_word: canim.Rect = .{ .x = sb.x + canim.theme.metric.slot.address + 12 * 12 + canim.theme.metric.context.arrow_w, .y = back_y, .w = @as(f32, @floatFromInt(canim.ui.digits(rec.register(moment - 1, "rip") + 5))) * 12, .h = row };
const word_frame = place(s, s.new(Read{ .box = main_word }));
// The relations, and rsp's link to its word as II2 names it.
const joins, const rsp_link = ctx.tiesWith(s, "rsp", 1.0);
s.play(.{joins.fadeIn()}, .{ .seconds = 0 });
var asides: canim.Asides = .{};
const left = canim.theme.canvas.margin_x;
// II1: the address space and the link fade (18 f); the words glide into
// the stack's rows (36 f) and become the panel (12 f). Where it first
// stands, its convention said, unlike the bar's: the offsets come down
// its rows (a column, 4 f apart), growing as the addresses do.
s.wait(t.beat);
s.play(.{ objs[0].fadeOut(), objs[2].fadeOut() }, .{ .seconds = f(18), .ease = anim.ease.linear });
s.play(.{dump.become.to(1)}, .{ .seconds = f(36), .ease = anim.ease.linear });
s.play(.{ stack.fadeIn(), objs[1].fadeOut() }, .{ .seconds = f(12), .ease = anim.ease.linear });
asides.say(s, texts[0], left, rowY(top, first, first) + row / 2);
const column = reveal.apart * @as(f32, @floatFromInt(slots)) + 12;
run(s, reveal.offsets, column);
s.wait(t.beat);
// II2: rsp's and rbp's rows arrive over the stack (12 f), rsp's pin lands on the top word (12 f),
// then rsp's link from its row down to the word, held, drawn back.
asides.say(s, texts[1], left, back_y + row / 2);
lift(s, &.{ regs_rows[0], regs_rows[1] }, 0, 12);
lift(s, &.{lane[rsp_row]}, 0, 12);
rsp_link.play(s);
s.wait(rsp_link.total());
s.wait(f(18));
s.wait(t.beat);
// II3: the call, first: the way back main left, framed; main's rows; the aim drawn to its row.
asides.say(s, texts[2], left, back_y + row / 2);
s.play(.{word_frame.fadeIn()}, .{ .seconds = f(6), .ease = anim.ease.linear });
s.wait(f(12));
run(s, main_rows.in, 12 + 2 * 2 + 12);
s.wait(f(6));
at.aim(s, .drawn);
s.wait(f(24 + 30));
s.wait(t.beat);
// II4: the push: reverse's first rows under main's, rbp's pin; `push rbp` walked
// (its row fires, the touch to the word, rbp's value carried there and struck, rsp's pin climbing).
asides.say(s, texts[3], left, rowY(top, first, rsp - 8) + row / 2);
at.aim(s, .standing);
s.play(.{ word_frame.fadeOut(), main_rows.faint.to(1) }, .{ .seconds = f(18), .ease = anim.ease.linear });
s.play(.{ctx.disasm.fadeIn()}, .{ .seconds = f(12), .ease = anim.ease.linear });
var lanes: [32]?anim.Obj = lane;
lanes[rsp_row] = null;
lift(s, &lanes, 0, 12);
s.wait(f(12));
at.levels.carry = .after_touch;
at.walk(s, .moment, moment + 1);
at.levels.carry = .off;
s.wait(t.beat);
// II5: the frame made (`mov rbp, rsp`, `sub rsp`); its roles, spans, and the tags last.
asides.say(s, texts[4], left, g.span_y);
at.aim(s, .off);
at.walk(s, .moment, moment + 2);
at.walk(s, .moment, framed);
run(s, reveal.roles, column);
run(s, reveal.spans, reveal.apart * 4 + 12);
s.wait(t.beat);
// `frame up`: the rules (12 f), then the tags and strips (12 f).
const at_rules = s.cursor;
lift(s, disasm_chrome[1..4], 0, 12);
s.cursor = at_rules;
lift(s, stack_chrome[1..4], 0, 12);
const at_tags = s.cursor;
lift(s, &.{ disasm_chrome[0], disasm_chrome[4] }, 0, 12);
s.cursor = at_tags;
lift(s, &.{ stack_chrome[0], stack_chrome[4] }, 0, 12);
s.wait(t.beat);
// II6: the registers built over the stack, a row at a time, their tag last.
asides.say(s, texts[5], left, g.registers_y);
lift(s, regs_rows[2..], 12, 12);
lift(s, regs_chrome[1..4], 0, 12);
lift(s, &.{regs_chrome[0]}, 0, 12);
// The chapter's last picture.
s.wait(@max(asides.read_by - s.cursor, 0));
s.wait(t.before_cut);
}
/// The chapter's texts, in order (chapter III opens on the last two).
pub const texts = [_][]const u8{
"Here lower addresses are drawn higher: down a word is up a row.",
"`rsp` holds the address of the top of the stack.",
"A call pushes the address to come back to: here, `main+50`.",
"A push moves `rsp` down a word and stores there: `push rbp` keeps the caller's `rbp`.",
"`rbp` marks the frame's base. A function's words are its frame: the saved rbp, its locals, the way back.",
"The registers are the other place values live: seventeen of them.",
};
/// Free words over rsp in the held window.
pub const held_free = 4;
/// The stack's window held from the first frame (no scroll to it), `held_free` words over rsp at `moment`.
pub fn hold(s: *anim.Frame, stage: *canim.Stage, moment: u64) void {
s.play(.{ stage.hold.to(@floatFromInt(moment)), stage.hold_free.to(held_free), stage.hold_in.to(1) }, .{ .seconds = 0 });
}
/// Where the chapter's parts stand (chapter III opens on its last picture,
/// laid out the same): the stack under the registers, its window from
/// `first` (four free words over rsp), its rows from `top`; main's rows
/// in the program column level with the lower stack, reverse's under them.
pub fn layout(stage: *canim.Stage, rec: *canim.Recording, moment: u64) struct { stack: canim.Rect, rsp: u64, first: u64, top: f32, caller: canim.Rect, span_y: f32, registers_y: f32 } {
const rb = stage.first.registers;
// Sixteen slots and the headroom under the registers' box (the words
// become them where they stand), not the column's rest.
stage.first.stack.h = canim.layout.panelH(16 + held_free);
stage.first.stack.y = rb.y + rb.h + canim.theme.canvas.row_gap;
const sb = stage.first.stack;
const rsp = rec.register(moment, "rsp");
const first = rsp - 8 * held_free;
const top = sb.y + tag + pad;
const caller: canim.Rect = .{ .x = canim.theme.layout.program.x, .y = rowY(top, first, rsp) + row / 2 + 152, .w = canim.theme.layout.program.w, .h = 4 * row };
// Seven rows: reverse's from its first, which none of the chapter's steps scrolls away.
stage.first.disasm = .{ .x = caller.x, .y = caller.y + caller.h + canim.theme.canvas.row_gap, .w = caller.w, .h = canim.layout.panelH(7) };
const rsp_framed = rec.register(moment + 3, "rsp");
return .{ .stack = sb, .rsp = rsp, .first = first, .top = top, .caller = caller, .span_y = (rowY(top, first, rsp_framed) + rowY(top, first, rsp + 8)) / 2, .registers_y = rb.y + tag + pad + row * 2.5 };
}
fn rowY(t0: f32, from: u64, addr: u64) f32 {
return t0 + row * @as(f32, @floatFromInt(@as(i64, @bitCast(addr -% from)) >> 3));
}
|