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
|
//! An array as cells: a variable the recording describes as `T name[N]`,
//! each element a cell with its index, the index variables that point into
//! it as tags, the cells a step compared framed as read, and a value a
//! step copied from one cell to another flying there on an arc, to land as
//! a write (a swap is two flights, one over and one under, the second a
//! little behind the first).
//! This is the idea drawn from memory: it changes a beat after the bytes do.
//! Reads `Moment.variables`, `Moment.read`, and the index of reads and writes.
const std = @import("std");
const sk = @import("sketch");
const anim = @import("anim");
const ui = @import("ui.zig");
const cursors = @import("cursor.zig");
const Recording = @import("Recording.zig");
const objects = @import("objects.zig");
const context = @import("context.zig");
const hexdump = @import("hexdump.zig");
const ties = @import("ties.zig");
const Stage = @import("stage.zig").Stage;
const theme = ui.theme;
const time = theme.time;
const Rect = ui.Rect;
const t = theme.color.text;
const fl = time.flight;
/// How long a value flies, by how far: duration follows distance.
fn flightTime(units: f32) f32 {
return if (units <= 200) fl.near else if (units <= 400) fl.mid else fl.far;
}
pub const Array = struct {
cursor: cursors.Cursor,
stage: *const Stage,
/// The array's name in the program.
variable: []const u8,
/// Variables that index it, shown as tags in the cell they point at.
indices: []const []const u8 = &.{},
focus: bool = false,
/// The timetable of a step that swaps two neighbouring cells (give it
/// to `Cursor.step`): the line fires; the registers strike; the two
/// runs of bytes strike, 6 f apart, and the projection draws from
/// them; only then does the first value lift and fly, the second a
/// little behind it; each lands with a strike; then the hold, and the
/// mark moves on. 2.9 s.
pub const swap: cursors.Plan = blk: {
const bytes = 2 * time.cascade;
const lands = bytes + time.project.bracket + time.project.draw + fl.lift + fl.near;
const advance = lands + fl.second + time.read_value;
break :blk .{ .at = .{ time.cascade, bytes, lands }, .advance = advance, .travel = time.step, .total = advance + time.step + time.rest };
};
pub fn draw(g: *const Array, f: *sk.Frame, at: anim.Placed) void {
const box = g.stage.box(at, .band);
if (box.h < theme.metric.context.tag + 24) return;
const u = context.at(f, at, box).faded(at.value(g.stage.drawing));
// A relayout moves the band: what is drawn in it stays inside it
// (its view, drawn below, clips itself and hands the clip back).
u.clip(.{ .x = 0, .y = 0, .w = box.w, .h = box.h });
defer u.unclip();
const now = g.cursor.now(at);
const rec = g.cursor.rec;
const moment = now.shown(.idea);
const v = rec.variable(f.scratch, moment, g.variable) orelse {
_ = u.panel(box.w, box.h, g.variable, .{ .focus = g.focus });
return;
};
const kind = rec.typeOf(f.scratch, v.type);
// Its declaration in the tag, and in its view its bytes.
_ = u.panel(box.w, box.h, objects.declare(f.scratch, rec, v), .{ .focus = g.focus });
const m = theme.metric.context;
// An array of numbers; anything else is read as 4-byte ones.
const elem: u64 = if (kind != null and kind.? == .array) kind.?.array.elem.size() orelse 4 else 4;
const signed = kind == null or kind.? != .array or kind.?.array.elem.* != .scalar or kind.?.array.elem.scalar.kind == .int;
// An element is read as its type says: a character quoted, a boolean by name.
const elem_type: Recording.types.Type = if (kind != null and kind.? == .array) kind.?.array.elem.* else .{ .scalar = .{ .kind = .int, .bits = 32 } };
const count: usize = @intCast(@min(v.size / elem, 32));
if (count == 0) return;
{
const first = v.addr & ~@as(u64, 15);
const dump = hexdump.panel(f, at, g.stage, u.print("{x} to {x}, an element a run", .{ v.addr, v.addr + v.size - 1 }), &.{});
const rows_n = @min(dump.rows, (v.addr + v.size - first + 15) / 16);
if (rows_n > 0) hexdump.draw(dump.u, now, 0, m.tag + m.pad, first, rows_n, .{ .array = .{ .addr = v.addr, .len = v.size, .elem = @intCast(elem), .type = elem_type }, .region = context.ink(context.regionOf(moment, v.addr)) });
if (rows_n > 0) @import("map.zig").window(.view, first, first + 16 * (rows_n - 1));
dump.veil(now);
u.clip(.{ .x = 0, .y = 0, .w = box.w, .h = box.h });
}
// The cells, in the left half: a size up as the subject.
const subject = box.h >= theme.layout.split.subject.band.h - 1;
const compact = !subject;
const cell: [2]f32 = if (subject) .{ 96, 72 } else .{ 80, 44 };
const size: f32 = if (subject) theme.size.mono.flight else theme.size.mono.small;
const label: f32 = if (subject) theme.size.mono.small else theme.size.mono.context;
const left = (box.w - @as(f32, @floatFromInt(count)) * cell[0]) / 2;
const top = m.tag + @as(f32, if (subject) 72 else 16) + label * 1.2 + 4;
const walking = now.walking();
// What moved: a cell written with what another cell held, which this step read.
const Flight = struct { from: usize, to: usize };
var flights: std.ArrayList(Flight) = .empty;
const values = f.scratch.alloc(?i64, count) catch return;
const old = f.scratch.alloc(?i64, count) catch return;
for (values, old, 0..) |*value, *was, i| {
value.* = number(now.moment(), v.addr + i * elem, elem, signed);
was.* = number(now.before(), v.addr + i * elem, elem, signed);
}
if (now.plan.gait != .sprint and !now.resting()) for (0..count) |to| {
if (now.writeAge(v.addr + to * elem, elem) != 0 or values[to] == null) continue;
for (0..count) |from| {
if (from != to and old[from] == values[to] and now.wasRead(v.addr + from * elem, elem)) {
flights.append(f.scratch, .{ .from = from, .to = to }) catch {};
break;
}
}
};
// Each cell as much as it has risen from its bytes (`establish`).
const rising = at.value(g.stage.rise);
for (0..count) |i| {
const risen = ties.rise(rising, i, count);
const cu = u.faded(risen.element).moved(0, risen.below);
const r: Rect = .{ .x = left + @as(f32, @floatFromInt(i)) * cell[0], .y = top, .w = cell[0] - 2, .h = cell[1] };
const addr = v.addr + i * elem;
// The cell a value is flying to is struck when it lands: the second a little after the first.
const flown: ?usize = for (flights.items, 0..) |flight, k| {
if (flight.to == i) break k;
} else null;
const delay = if (flown) |k| @as(f32, @floatFromInt(k)) * fl.second else 0;
const pending = !now.landed(.idea, delay) and now.writeAge(addr, elem) == 0;
var heat = now.written(addr, elem, .idea, delay);
// The value on its way is the anticipation: no frame as well.
if (flown != null) heat.arm = 0;
const ink = cu.cellWritten(r, heat, theme.color.raised);
// Staging: the compare is seen (framed as read) before anything moves.
if (now.wasRead(addr, elem) and now.writeAge(addr, elem) != 0) cu.cellRead(r, now.reading(.idea, 0));
// Its index over it, where a node has its address: the projection lands there.
const index_text = u.print("{d}", .{i});
const ly = r.y - 4 - label * 1.2;
cu.text(.mono, label, r.x + (r.w - @as(f32, @floatFromInt(index_text.len)) * label * 3 / 5) / 2, ly + label * 0.96, index_text, t.faint);
ties.anchor(u, .{ .node_address = addr }, .{ .x = r.x, .y = ly, .w = r.w, .h = label * 1.2 });
ties.anchor(u, .{ .node_part = addr }, r);
// Until its new value lands a cell keeps the old one, faint once
// the new is in the air: a copy does not erase.
const shown = if (pending) old[i] else values[i];
const digits = r;
const leaving = pending and flown != null and (!walking or now.since(.idea, delay) >= -(flightTime(cell[0]) + fl.lift));
if (shown) |n| cu.label(ink.face, size, digits, .center, objects.reading(u.f.scratch, n, elem_type), if (leaving) ui.rgb(t.faint) else ink.ink) else cu.unknown(digits);
}
// Values in flight, over the cells and under every name and pin: one over, the next under.
for (flights.items, 0..) |flight, k| {
const from = ui.Vec{ left + (@as(f32, @floatFromInt(flight.from)) + 0.5) * cell[0], top + cell[1] / 2 + 9 };
const to = ui.Vec{ left + (@as(f32, @floatFromInt(flight.to)) + 0.5) * cell[0], top + cell[1] / 2 + 9 };
const far = @abs(to[0] - from[0]);
const seconds = flightTime(far);
// Seconds until it lands. Anticipation: it lifts out of its
// cell before it goes, and is back to size as it comes down.
const left_to_go = -now.since(.idea, @as(f32, @floatFromInt(k)) * fl.second);
if (left_to_go <= 0) continue;
var along: f32 = undefined;
var lifted: f32 = undefined;
if (walking) {
if (left_to_go > seconds + fl.lift) continue;
along = 1 - std.math.clamp(left_to_go / seconds, 0, 1);
lifted = @min(std.math.clamp((seconds + fl.lift - left_to_go) / fl.lift, 0, 1), std.math.clamp(left_to_go / fl.lift, 0, 1));
} else {
// At a run: the same arc, eased, in the step's first frames.
along = now.moving(.idea, 0, 0);
lifted = 0;
if (along >= 1) continue;
}
// Arcs: an idea moves on a curve, slow out of one cell and into the other, quickest over the top.
const eased = cursors.glide(along);
// As high as a quarter of the way, clear of the row, and never out of the band.
const card_half = (cell[1] + 4) * fl.lift_scale / 2;
const room = @min(from[1] - card_half - m.tag - 2, box.h - 4 - card_half - from[1]);
const rise = @min(std.math.clamp(fl.arc * far, fl.arc_min, fl.arc_max) + cell[1] / 2, room);
const way: f32 = if (k % 2 == 0) -1 else 1;
const c = sk.math.mix(from, to, eased) + ui.Vec{ 0, way * rise * @sin(std.math.pi * eased) };
const grown = std.math.lerp(1, fl.lift_scale, lifted);
const w = (cell[0] + 4) * grown;
const h = (cell[1] + 4) * grown;
const card: Rect = .{ .x = c[0] - w / 2, .y = c[1] - h / 2, .w = w, .h = h };
u.fill(card, theme.color.page);
u.frame(card, t.bright, theme.stroke.line);
if (values[flight.to]) |n| u.label(.mono_bold, size, card, .center, objects.reading(u.f.scratch, n, elem_type), t.bright);
_ = compact;
}
// Under the cards in flight: index variables, pins under the cell they index, sliding from
// cell to cell as the idea strikes; two in one cell share its pin.
var at_cell: [32]std.ArrayList([]const u8) = @splat(.empty);
var slide: [32]?f32 = @splat(null);
for (g.indices) |name| {
const iv = rec.variable(f.scratch, now.moment(), name) orelse continue;
const here = number(now.moment(), iv.addr, @min(iv.size, 8), true) orelse continue;
if (here < 0 or here >= count) continue;
const k: usize = @intCast(here);
at_cell[k].append(f.scratch, name) catch {};
if (number(now.before(), iv.addr, @min(iv.size, 8), true)) |was| if (was != here and was >= 0 and was < count) {
slide[k] = @floatFromInt(was);
};
}
// And the registers that hold a cell's address: a pin, where a link lands.
const is = (now.moment().regs(rec.tid) catch null) orelse @import("ztrace").Regs{};
const was_regs = (now.before().regs(rec.tid) catch null) orelse is;
const ink = context.ink(context.regionOf(moment, v.addr));
for (&at_cell, 0..) |*names, k| {
const addr = v.addr + k * elem;
const cx = left + (@as(f32, @floatFromInt(k)) + 0.5) * cell[0];
ties.anchor(u, .{ .node_pin = addr }, .{ .x = cx - 1, .y = top + cell[1], .w = 2, .h = label + 10 });
const indices = names.items.len;
var shows: f32 = 1;
for (theme.metric.all_registers[0..16]) |name| {
const i = context.index(name).?;
const before = was_regs.named(name) == addr;
const after = is.named(name) == addr;
if (!before and !after) continue;
const s = ties.pinShows(now, i, .node);
const this = if (before and after) 1 else if (after) s.new else s.old;
if (this < 0.5) continue;
names.append(f.scratch, name) catch {};
if (indices == 0) shows = @min(shows, this);
}
if (names.items.len == 0) continue;
var x = cx;
// A pin's travel (`time.pin`): to the next cell, or farther.
if (slide[k]) |from| x = std.math.lerp(left + (from + 0.5) * cell[0], x, cursors.standard(now.moving(.idea, 0, if (@abs(from - @as(f32, @floatFromInt(k))) <= 1) time.pin.node else time.pin.nodes)));
// An index lives on the stack: its name in stack's ink; a register's primary.
var inks: [8]u24 = @splat(t.primary);
for (inks[0..@min(indices, inks.len)]) |*i_ink| i_ink.* = theme.color.region.stack;
u.faded(shows * ties.rise(rising, k, count).element).pinUnderInked(x, top + cell[1], names.items, inks[0..@min(names.items.len, inks.len)], if (names.items.len > indices) ink else t.primary, label);
}
}
};
/// `bytes` at `addr` as a number, null where never recorded.
fn number(moment: Recording.Moment, addr: u64, bytes: u64, signed: bool) ?i64 {
const n: usize = @intCast(@min(bytes, 8));
const raw = (Recording.word(moment, addr, n) catch return null) orelse return null;
if (!signed) return @bitCast(raw);
const shift: u6 = @intCast(64 - 8 * n);
return @as(i64, @bitCast(raw << shift)) >> shift;
}
test "a swap of neighbours is 2.9 seconds, its bytes before its flights" {
try std.testing.expectApproxEqAbs(@as(f32, 2.9), Array.swap.total, 1e-3);
try std.testing.expect(Array.swap.at[2] - fl.near - fl.lift > Array.swap.at[1]);
try std.testing.expect(flightTime(168) == fl.near and flightTime(336) == fl.mid and flightTime(900) == fl.far);
}
|