diff options
Diffstat (limited to 'src/host/overlay.zig')
| -rw-r--r-- | src/host/overlay.zig | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/src/host/overlay.zig b/src/host/overlay.zig new file mode 100644 index 0000000..919fff6 --- /dev/null +++ b/src/host/overlay.zig @@ -0,0 +1,76 @@ +//! Debug overlays drawn by the host over any sketch: the live state's fields +//! and bytes, from the reflected layout, and the audio output waveform. +const std = @import("std"); +const abi = @import("abi"); +const memory = @import("memory.zig"); +const fields = @import("fields.zig"); + +const List = std.ArrayList(abi.Command); +const shown_bytes = 0x100; + +/// Each field hashes its path into a dark shade; bytes it owns wear the same +/// shade in the hex view below, and padding stays gray. +fn shade(path: []const u8) abi.Color { + const hash = std.hash.Wyhash.hash(0, path); + return .{ .r = @as(u8, @truncate(hash)) / 2, .g = @as(u8, @truncate(hash >> 8)) / 2, .b = @as(u8, @truncate(hash >> 16)) / 2 }; +} + +pub fn drawMemory(out: *List, arena: std.mem.Allocator, layout: memory.Layout, state: []const u8, width: f32) !void { + const rows = (layout.fields.len + 1) / 2; + const legend_top: f32 = 24; + const hex_top = legend_top + @as(f32, @floatFromInt(rows)) * 14 + 16; + const shown = @min(state.len, shown_bytes); + const height = hex_top + @as(f32, @floatFromInt((shown + 15) / 16)) * 14 + 10; + try out.append(arena, .{ .rect = .{ .dst = .{ .x = 12, .y = 4, .w = width - 24, .h = height }, .color = .{ .r = 230, .g = 254, .b = 242 } } }); + try out.append(arena, .{ .text = .{ .x = 30, .y = 8, .scale = 1, .color = .{ .r = 24, .g = 24, .b = 24 }, .bytes = "MEMORY / overlay memory to hide" } }); + + // Legend: two fixed-width columns filled down then across. + const column = (width - 60) / 2; + for (layout.fields, 0..) |leaf, i| { + var value: std.Io.Writer.Allocating = .init(arena); + try fields.format(leaf, state, &value.writer); + const name = if (leaf.path.len > 26) try std.fmt.allocPrint(arena, "{s}...", .{leaf.path[0..23]}) else leaf.path; + const line = try std.fmt.allocPrint(arena, "{x:0>4} {s: <26} {s}", .{ leaf.offset, name, value.written() }); + try out.append(arena, .{ .text = .{ + .x = 30 + @as(f32, @floatFromInt(i / rows)) * column, + .y = legend_top + @as(f32, @floatFromInt(i % rows)) * 14, + .scale = 1, + .color = shade(leaf.path), + .bytes = line[0..@min(line.len, @as(usize, @intFromFloat(column / 8)) - 1)], + } }); + } + + var colors: [shown_bytes]abi.Color = @splat(.{ .r = 0x9f, .g = 0x9f, .b = 0x9f }); + for (layout.fields) |leaf| { + if (leaf.offset >= shown) continue; + @memset(colors[leaf.offset..@min(shown, leaf.offset + leaf.size)], shade(leaf.path)); + } + for (state[0..shown], 0..) |byte, i| { + try out.append(arena, .{ .text = .{ + .x = 30 + @as(f32, @floatFromInt(i % 16)) * 24, + .y = hex_top + @as(f32, @floatFromInt(i / 16)) * 14, + .scale = 1, + .color = colors[i], + .bytes = try std.fmt.allocPrint(arena, "{X:0>2}", .{byte}), + } }); + } +} + +pub fn drawScope(out: *List, arena: std.mem.Allocator, samples: []const f32, rate: u32, canvas: [2]u32) !void { + const green: abi.Color = .{ .r = 105, .g = 245, .b = 160 }; + const x = @as(f32, @floatFromInt(canvas[0])) - 526; + const top = @as(f32, @floatFromInt(canvas[1])) - 130; + const mid = top + 72; + try out.append(arena, .{ .rect = .{ .dst = .{ .x = x, .y = top, .w = 514, .h = 118 }, .color = .{ .r = 24, .g = 24, .b = 24 } } }); + try out.append(arena, .{ .rect = .{ .dst = .{ .x = x + 1, .y = mid, .w = 512, .h = 1 }, .color = .{ .r = 60, .g = 85, .b = 65 } } }); + const ms = @as(f32, @floatFromInt(samples.len * 1000)) / @as(f32, @floatFromInt(@max(rate, 1))); + try out.append(arena, .{ .text = .{ .x = x + 8, .y = top + 10, .scale = 1, .color = green, .bytes = try std.fmt.allocPrint(arena, "OUTPUT ch1 / {d:.1}ms / overlay sound to hide", .{ms}) } }); + for (samples, 0..) |sample, i| { + const a = std.math.clamp(if (std.math.isFinite(sample)) sample else 0, -1, 1); + const previous = samples[i -| 1]; + const b = std.math.clamp(if (std.math.isFinite(previous)) previous else 0, -1, 1); + const y1 = mid - a * 36; + const y2 = mid - b * 36; + try out.append(arena, .{ .rect = .{ .dst = .{ .x = x + 1 + @as(f32, @floatFromInt(i)), .y = @min(y1, y2), .w = 1, .h = @max(1, @abs(y2 - y1)) }, .color = green } }); + } +} |
