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
|
//! 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 first value lifts and
/// flies, the second a little behind it; each lands with a strike;
/// then the hold, and the mark moves on. 2.1 s.
pub const swap: cursors.Plan = blk: {
const lands = time.fire + fl.lift + fl.near;
const advance = lands + fl.second + time.read_value;
break :blk .{ .at = .{ time.cascade, time.cascade, 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);
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 the right half its bytes.
_ = u.panel(box.w, box.h, objects.declare(f.scratch, rec, v), .{ .focus = g.focus });
const m = theme.metric.context;
const half = theme.layout.state.x - theme.layout.program.x;
// 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;
const count: usize = @intCast(@min(v.size / elem, 32));
if (count == 0) return;
{
const first = v.addr & ~@as(u64, 15);
const rows_n = @min(theme.layout.rowsIn(box.h), (v.addr + v.size - first + 15) / 16);
u.text(.sans_medium, theme.size.sans.tag, half + 12, m.tag - 6, "its bytes", t.primary);
u.text(.sans, theme.size.sans.micro, half + 12 + u.width(.sans_medium, theme.size.sans.tag, "its bytes") + 12, m.tag - 6, u.print("{x} to {x}, an element a run", .{ v.addr, v.addr + v.size - 1 }), t.faint);
hexdump.draw(u, now, half, m.tag + m.pad, first, rows_n, .{ .array = .{ .addr = v.addr, .len = v.size, .elem = @intCast(elem) }, .region = context.ink(context.regionOf(moment, v.addr)) });
}
// 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 = (half - 32 - @as(f32, @floatFromInt(count)) * cell[0]) / 2;
const top = m.tag + @as(f32, if (subject) 44 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;
}
}
};
for (0..count) |i| {
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 = u.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) u.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;
u.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| u.label(ink.face, size, digits, .center, u.print("{d}", .{n}), if (leaving) ui.rgb(t.faint) else ink.ink) else u.unknown(digits);
}
// 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);
};
}
for (at_cell, 0..) |names, k| {
if (names.items.len == 0) continue;
var x = left + (@as(f32, @floatFromInt(k)) + 0.5) * cell[0];
if (slide[k]) |was| x = std.math.lerp(left + (was + 0.5) * cell[0], x, cursors.standard(now.moving(.idea, 0, time.move)));
u.pinUnder(x, top + cell[1], names.items, t.primary, label);
}
// Values in flight, over everything: 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);
const rise = std.math.clamp(fl.arc * far, fl.arc_min, fl.arc_max) + cell[1] / 2;
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, u.print("{d}", .{n}), t.bright);
_ = compact;
}
}
};
/// `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.1 seconds" {
try std.testing.expectApproxEqAbs(@as(f32, 2.1), Array.swap.total, 1e-3);
try std.testing.expect(flightTime(168) == fl.near and flightTime(336) == fl.mid and flightTime(900) == fl.far);
}
|