From eab50d1c040774351f332a1568fa332c377860bd Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 12 Nov 2026 11:32:59 -0300 Subject: canim: the introduction as one video, with a chapter on registers From the user. One scene, intro, in four chapters: memory, registers, the stack, code and meaning (210 s). The registers chapter builds the panel before the stack needs it: sixteen rows with their chains, rip pinned on the code band and rsp and rbp on the stack's, rdi opened into its parts as mov edi changes one byte, a call to malloc stepped over with its result in rax, the registers a scene never changes folded. The chapters join at seams that say their title and keep it as the clock's label; one recording (from push(9), no agent, the heap from --heap) and one clock. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNCKgYCGiANnEwcEQxa --- anim/src/context.zig | 175 ++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 166 insertions(+), 9 deletions(-) (limited to 'anim/src/context.zig') diff --git a/anim/src/context.zig b/anim/src/context.zig index 874a22a..577071d 100644 --- a/anim/src/context.zig +++ b/anim/src/context.zig @@ -103,16 +103,75 @@ pub const Registers = struct { folds: bool = true, /// Its tag's title (a `pair`'s names its run). title: []const u8 = "registers", + /// The quiet rows folded on a beat the scene plays (`fold`, 0 to 1: + /// every register a row, then folded; the introduction's II8); null: + /// folded, or as a dive has it. + fold: ?anim.Value = null, + /// One register opened into its bytes and its parts (`reg` `parts`, `unpack`, `pack`). + parts: ?*const Parts = null, + /// Registers framed as read on a beat the scene plays (rip's value too). + framed: []const Framed = &.{}, + + pub const Framed = struct { name: []const u8, shows: anim.Value }; + + /// `parts` (design/README.md, `reg`): register `name`'s value as its + /// eight bytes from 72 on a pitch of 30, high first, the zeros in front + /// at `pad`, and under it a row for each name its low bytes go by, + /// widest first, a bracket under the bytes it is. Clocks in frames: + /// `unpack` (the rows under it make room 0 to 18, the bytes spread 18 + /// to 30, the parts' rows 30 to 54, 6 f apart); `pack` (the parts' rows + /// fade 0 to 12, then the bytes close into the value and the rows close + /// up, 12 to 36); `echo`, 0 to 1: the part `echoed` and its bytes framed as read. + pub const Parts = struct { + name: []const u8, + unpack: anim.Value, + pack: anim.Value, + echo: anim.Value, + echoed: usize = 0, + + /// The rows it takes under its register (0 to 3). + fn room(p: *const Parts, placed: anim.Placed) f32 { + const un = placed.value(p.unpack); + if (un < 0) return 0; + const pk = placed.value(p.pack); + return 3 * theme.ease(.glide, std.math.clamp(un / 18, 0, 1)) * (1 - theme.ease(.glide, std.math.clamp((pk - 12) / 24, 0, 1))); + } + + /// How far its bytes have spread out of the value (0 closed, 1 open). + fn spread(p: *const Parts, placed: anim.Placed) f32 { + const un = placed.value(p.unpack); + if (un < 18) return 0; + const pk = placed.value(p.pack); + return theme.ease(.glide, std.math.clamp((un - 18) / 12, 0, 1)) * (1 - theme.ease(.glide, std.math.clamp((pk - 12) / 24, 0, 1))); + } + + /// How much part row `j`'s shows. + fn shows(p: *const Parts, placed: anim.Placed, j: usize) f32 { + const un = placed.value(p.unpack); + return std.math.clamp((un - 30 - 6 * @as(f32, @floatFromInt(j))) / 12, 0, 1) * (1 - std.math.clamp(placed.value(p.pack) / 12, 0, 1)); + } + + /// Its names, widest first (4, 2 and 1 bytes): `eax ax al`, `edi di dil`, `r8d r8w r8b`. + fn names(u: ui.Ui, name: []const u8) [3][]const u8 { + if (name.len >= 2 and std.ascii.isDigit(name[1])) return .{ u.print("{s}d", .{name}), u.print("{s}w", .{name}), u.print("{s}b", .{name}) }; + const bare = name[1..]; + if (bare.len == 2 and bare[1] == 'x') return .{ u.print("e{s}", .{bare}), bare, u.print("{c}l", .{bare[0]}) }; + return .{ u.print("e{s}", .{bare}), bare, u.print("{s}l", .{bare}) }; + } + }; const Shown = struct { regs: zt.Regs, moment: Recording.Moment }; pub fn draw(r: *const Registers, f: *sk.Frame, placed: anim.Placed) void { const box = r.stage.box(placed, .registers); const u = at(f, placed, box); - // In a dive the panel comes whole out of the map and folds as it lands (`Zoom.fold`). - const fold = if (ui.mapping) 1 else foldOf(box); + // In a dive the panel comes whole out of the map and folds as it lands (`Zoom.fold`); on a beat, as the scene plays it. + const fold = if (r.fold) |v| theme.ease(.glide, std.math.clamp(placed.value(v), 0, 1)) else if (ui.mapping) 1 else foldOf(box); var shape = box; shape.h = std.math.lerp(theme.layout.panelH(@floatFromInt(theme.metric.all_registers.len)), box.h, fold); + // A register opened into its parts: the rows under it make room. + const room = if (r.parts) |p| p.room(placed) * m.row else 0; + shape.h += room; const now = r.cursor.now(placed); const rec = r.cursor.rec; const is: Shown = .{ .regs = (now.moment().regs(rec.tid) catch null) orelse zt.Regs{}, .moment = now.moment() }; @@ -128,23 +187,30 @@ pub const Registers = struct { const full = fullRows(r.moves); const top = body.y + m.pad; // The quiet ones under a rule across, in pwndbg's order; rip the last row. - if (full < 16 and r.folds) u.faded(fold).fill(.{ .x = theme.stroke.rule, .y = top + @as(f32, @floatFromInt(full)) * m.row - 1, .w = shape.w - 2 * theme.stroke.rule, .h = theme.stroke.rule }, theme.color.rule); + // (On a beat the rule draws once they have folded, 12 f.) + const rule_shows = if (r.fold != null) std.math.clamp((placed.value(r.fold.?) - 1) * 3, 0, 1) else fold; + if (full < 16 and r.folds) u.faded(rule_shows).fill(.{ .x = theme.stroke.rule, .y = top + @as(f32, @floatFromInt(full)) * m.row - 1 + room, .w = shape.w - 2 * theme.stroke.rule, .h = theme.stroke.rule }, theme.color.rule); var rows_before: usize = 0; var quiet_before: usize = 0; + // The rows under an opened register are `room` lower. + var below: f32 = 0; for (theme.metric.all_registers[0..16], 0..) |name, i| { const unfolded = top + @as(f32, @floatFromInt(i)) * m.row; if (r.moves >> @intCast(i) & 1 != 0) { - r.row(u, now, is, was, name, std.math.lerp(unfolded, top + @as(f32, @floatFromInt(rows_before)) * m.row, fold), shape.w); + r.row(u, placed, now, is, was, name, std.math.lerp(unfolded, top + @as(f32, @floatFromInt(rows_before)) * m.row, fold) + below, shape.w); rows_before += 1; + if (r.parts) |p| if (std.mem.eql(u8, p.name, name)) { + below = room; + }; continue; } if (!r.folds) continue; - const y = std.math.lerp(unfolded, top + @as(f32, @floatFromInt(full + quiet_before / q.quiet_per_row)) * m.row, fold); + const y = std.math.lerp(unfolded, top + @as(f32, @floatFromInt(full + quiet_before / q.quiet_per_row)) * m.row, fold) + below; const x = std.math.lerp(theme.metric.context.side, q.quiet_from + @as(f32, @floatFromInt(quiet_before % q.quiet_per_row)) * q.quiet_pitch, fold); quiet_before += 1; r.quietRow(u, now, is, was, name, x, y, shape.w, fold); } - r.rip(u, now, std.math.lerp(top + 16 * m.row, top + @as(f32, @floatFromInt(if (r.folds) registerRows(r.moves) - 1 else full)) * m.row, fold), shape.w, is, was); + r.rip(u, placed, now, std.math.lerp(top + 16 * m.row, top + @as(f32, @floatFromInt(if (r.folds) registerRows(r.moves) - 1 else full)) * m.row, fold) + below, shape.w, is, was); veiled(u, shape, now.veil); } @@ -157,7 +223,15 @@ pub const Registers = struct { const read_shows = if (r.carrier(now, name, was.regs.named(name) orelse value)) |c| c.shows else 0; if (fold < 1) { const dx = x - theme.metric.context.side; - _ = rows.reg(u.faded(1 - fold).moved(dx, 0), y, w - dx, name, is.moment, value, .{ .hops = 1 - std.math.clamp(fold / 0.4, 0, 1) }); + const v = u.faded(1 - fold).moved(dx, 0); + const hops = 1 - std.math.clamp(fold / 0.4, 0, 1); + // Its chain reads memory as the panels show it: what a step wrote under it, from memory's strike (6 f), never in a frame. + const k = if (now.resting()) 1 else std.math.clamp(now.since(.memory, 0) / time.strike.in, 0, 1); + if (k < 1 and cursors.chainMoved(was.moment, is.moment, value)) { + v.text(.mono, theme.size.mono.context, m.side, rows.baseline(y), name, t.dim); + _ = rows.chain(v.faded(1 - k), m.chain_x, y, was.moment, value, w - dx - m.side, .{ .hops = hops }); + _ = rows.chain(v.faded(k), m.chain_x, y, is.moment, value, w - dx - m.side, .{ .hops = hops }); + } else _ = rows.reg(v, y, w - dx, name, is.moment, value, .{ .hops = hops }); } const hops = rows.quiet(u.faded(fold), x, y, name, is.moment, value, read_shows); anchor(u, i, hops, y, value, w); @@ -185,8 +259,12 @@ pub const Registers = struct { /// rip: where execution is, a current mark like the source's and the /// disassembly's; it moves with them, at the advance. - fn rip(r: *const Registers, u: ui.Ui, now: cursors.Now, y: f32, w: f32, is: Shown, was: Shown) void { + fn rip(r: *const Registers, u: ui.Ui, placed: anim.Placed, now: cursors.Now, y: f32, w: f32, is: Shown, was: Shown) void { const rec = r.cursor.rec; + defer for (r.framed) |fr| if (std.mem.eql(u8, fr.name, "rip")) { + const pc = rec.register(now.marks[1], "rip"); + u.read(.{ .x = theme.metric.context.chain_x, .y = y, .w = @as(f32, @floatFromInt(ui.digits(pc))) * ui.advance(theme.size.mono.context), .h = m.row }, std.math.clamp(placed.value(fr.shows), 0, 1)); + }; // The flags' corners are secondary: 6 f after the registers strike, // not with them; in a continue's wave, in its place after r15. const delay = if (now.plan.wave) ties.strikeDelay(now, index("eflags").?) else cursors.write_apart; @@ -206,9 +284,26 @@ pub const Registers = struct { rows.rip(u, y, w, rec.moment(now.marks[1]), rec.register(now.marks[1], "rip"), is.regs.eflags, changed, held, 0, reads, now.reading(.cpu, 0), .{ .moment = rec.moment(now.marks[0]), .pc = rec.register(now.marks[0], "rip"), .eflags = was.regs.eflags, .going = now.advancing(), .flip = if (now.resting()) 1 else flip }); } - fn row(r: *const Registers, u: ui.Ui, now: cursors.Now, line_is: Shown, line_was: Shown, name: []const u8, y: f32, w: f32) void { + fn row(r: *const Registers, u_row: ui.Ui, placed: anim.Placed, now: cursors.Now, line_is: Shown, line_was: Shown, name: []const u8, y: f32, w: f32) void { const rec = r.cursor.rec; const i = index(name) orelse return; + // Opened into its parts: its value as bytes, its chain gone (the name stays). + const opened = if (r.parts) |p| (if (std.mem.eql(u8, p.name, name)) p else null) else null; + const spread = if (opened) |p| p.spread(placed) else 0; + if (spread > 0) r.partsRow(u_row, placed, now, opened.?, line_is, line_was, name, i, y, spread); + // The value as it was written crossfades with its own pairs where they stand (the spread's + // first tenth), before the pairs move off and the zeros in front come. + const u = u_row.faded(std.math.clamp(1 - 10 * spread, 0, 1)); + // (Its name drawn whole over the fading row's: never dimmer for the crossfade.) + if (spread > 0) u_row.text(.mono, theme.size.mono.context, m.side, rows.baseline(y), name, t.dim); + defer for (r.framed) |fr| if (std.mem.eql(u8, fr.name, name)) { + // Round the value shown: the one before the step's until its strike lands. + const struck = now.registerAge(i) != 0 or now.since(.cpu, ties.strikeDelay(now, i)) >= time.strike.in / 2.0; + const side = if (struck) line_is else line_was; + const value = side.regs.named(name) orelse 0; + const shown = ui.Value.of(value, rows.regionOf(side.moment, value) != .none); + u.read(.{ .x = m.chain_x, .y = y, .w = shown.width(), .h = m.row }, std.math.clamp(placed.value(fr.shows), 0, 1)); + }; // Every register is the program's, at its line stops, as every // other panel is: what crossed into the kernel is the crossing's // to show, and a register is framed as carrying it only where it @@ -278,6 +373,68 @@ pub const Registers = struct { anchor(u, i, hops, y, if (landed or ran < 1) value_now else value_was, w); } + /// A register opened into its parts (`Parts`): its eight bytes, high + /// first, the zeros in front at `pad`, spreading out of the value as it + /// was written (`spread`); the bytes a write changed armed and struck as + /// a dump's; under it its parts' rows, each a bracket under its bytes. + fn partsRow(r: *const Registers, u: ui.Ui, placed: anim.Placed, now: cursors.Now, p: *const Parts, is: Shown, was: Shown, name: []const u8, i: usize, y: f32, spread: f32) void { + const size = theme.size.mono.context; + const adv = ui.advance(size); + const pitch: f32 = 30; + const value_now = is.regs.named(name) orelse return; + const value_was = was.regs.named(name) orelse value_now; + const age = now.registerAge(i); + const delay = if (age == 0) ties.strikeDelay(now, i) else 0; + const heat = if (r.quiet) ui.Heat.none else now.register(i, delay); + const landed = now.resting() or now.since(.cpu, delay) >= time.strike.in / 2.0; + const value = if (landed) value_now else value_was; + // Closed: the pairs side by side, ranged so that the last ends where the value's hex digits end. + const shown = ui.Value.of(value, rows.regionOf(is.moment, value) != .none); + const closed_end = m.chain_x + u.width(.mono, size, shown.prefix) + @as(f32, @floatFromInt(shown.digits().len)) * adv; + var lead: usize = 0; + while (lead < 7 and (value >> @intCast(8 * (7 - lead))) & 0xff == 0) lead += 1; + for (0..8) |k| { + const b: u8 = @truncate(value >> @intCast(8 * (7 - k))); + const before: u8 = @truncate(value_was >> @intCast(8 * (7 - k))); + const after: u8 = @truncate(value_now >> @intCast(8 * (7 - k))); + const open_x = m.chain_x + pitch * @as(f32, @floatFromInt(k)); + // (The zeros in front close onto the value's first digit, fading: never over the name.) + const closed_x = @max(closed_end - 2 * adv * @as(f32, @floatFromInt(8 - k)), if (k < lead) m.chain_x else m.chain_x - 2 * adv); + const x = std.math.lerp(closed_x, open_x, spread); + const cell: Rect = .{ .x = x, .y = y, .w = 2 * adv, .h = m.row }; + const struck = if (before != after) u.faded(spread).written(cell, heat) else null; + const pad = k < lead and struck == null; + // The value's own pairs at once; the zeros in front over the last 40% (never piled up), at `pad`. + const v = u.faded(if (k < lead) std.math.clamp((spread - 0.6) / 0.4, 0, 1) * @as(f32, if (pad) 0.55 else 1) else std.math.clamp(10 * spread, 0, 1)); + const text = u.print("{x:0>2}", .{b}); + if (struck) |in| v.text(in.face, size, x, rows.baseline(y), text, in.ink) else v.text(.mono, size, x, rows.baseline(y), text, if (pad) t.faint else t.primary); + } + // Its parts' rows, widest first: a bracket under the bytes each is, its name before it. + const parts = Parts.names(u, name); + const counts = [3]usize{ 4, 2, 1 }; + const echo = std.math.clamp(placed.value(p.echo), 0, 1); + for (parts, counts, 0..) |part, n, j| { + const shows_ = p.shows(placed, j); + const ry = y + m.row * @as(f32, @floatFromInt(j + 1)); + const x0 = m.chain_x + pitch * @as(f32, @floatFromInt(8 - n)) - 3; + const x1 = m.chain_x + pitch * 7 + 2 * adv + 3; + const by = ry + 10; + if (shows_ > 0) { + const v = u.faded(shows_); + v.fill(.{ .x = x0, .y = by, .w = x1 - x0, .h = 2 }, t.dim); + v.fill(.{ .x = x0, .y = by - 11, .w = 2, .h = 13 }, t.dim); + v.fill(.{ .x = x1 - 2, .y = by - 11, .w = 2, .h = 13 }, t.dim); + const nx = x0 - 10 - v.width(.mono, size, part); + v.text(.mono, size, nx, rows.baseline(ry), part, t.dim); + // The part an instruction names, and its bytes in the register's row, framed as read together (an echo). + if (j == p.echoed and echo > 0) { + u.read(.{ .x = nx, .y = ry, .w = v.width(.mono, size, part), .h = m.row }, echo * shows_); + u.read(.{ .x = x0 + 3, .y = y, .w = x1 - x0 - 6, .h = m.row }, echo * shows_); + } + } + } + } + /// The `fd` class's note after a register's value: faint, what it is open on. fn fdNote(u: ui.Ui, hops: rows.Hops, y: f32, on: []const u8) void { if (hops.n > 0 and on.len > 0) u.text(.mono, theme.size.mono.context, hops.end + 8, rows.baseline(y), on, t.faint); -- cgit v1.3