diff options
Diffstat (limited to 'src/builtins.zig')
| -rw-r--r-- | src/builtins.zig | 335 |
1 files changed, 2 insertions, 333 deletions
diff --git a/src/builtins.zig b/src/builtins.zig index d203cd9e..5dae567a 100644 --- a/src/builtins.zig +++ b/src/builtins.zig @@ -12,8 +12,6 @@ const Pane = pardes.Pane; const panes = @import("panes.zig"); const layout = @import("layout.zig"); const config = @import("config.zig"); -const builtin = @import("builtin"); -const Header = @import("esp32p4_gpio.zig").Header; const limits = @import("memory.zig").limits; /// What this build's own frontend draws and its host does: the render's. @@ -21,9 +19,8 @@ pub const frontend: config.Runtime.Capabilities = .{ .font_picker = pardes.font_picker, .panel_transitions = pardes.hosted, .scene_shaders = pardes.platform == .gui or pardes.platform == .macos, - // The tty's font belongs to its emulator, and the P4 firmware's belongs to - // whatever terminal is on the other end of the serial line. - .tagline_font_size = pardes.platform != .tui and pardes.platform != .esp32p4, + // The tty's font belongs to its emulator. + .tagline_font_size = pardes.platform != .tui, .window_opacity = pardes.platform == .gui or pardes.platform == .macos, .window_blur = pardes.platform == .macos, // Only the SDL shell shapes with HarfBuzz and honours the setting. macOS @@ -520,13 +517,6 @@ test "Ligatures is a setting only in the shell that shapes text" { try std.testing.expectEqual(!exists, std.mem.indexOf(u8, storage[0..out.end], "Ligatures unsupported\n") != null); } -comptime { - for ([_][]const u8{ "Peek", "Poke", "Hexdump" }) |name| - if (@hasField(registry.Builtin(), name) != Board.enabled) @compileError( - "bare-metal memory word gating leaked: " ++ name, - ); -} - // ---- the two acme verbs ---- /// Look at its argument: open the file or address it names, else find the word, as a right click does. @@ -2048,327 +2038,6 @@ pub const Lspwhy = struct { } }; -/// Read the board memory at an address, into a pane. -pub const Peek = struct { - pub const scope: Scope = .session; - pub const takes_arg = true; - pub const requires_arg = true; - pub const enabled = Board.enabled; - pub const output: OutputTraits = .{ .name = config.peek_buffer }; - pub fn run(c: Ctx) void { - Board.peek(c.p, c.id, c.arg orelse "") catch |err| - c.p.reportError(c.id, "peek", err); - } -}; - -/// Write a value to the board memory at an address. -pub const Poke = struct { - pub const scope: Scope = .session; - pub const takes_arg = true; - pub const requires_arg = true; - pub const enabled = Board.enabled; - pub fn run(c: Ctx) void { - Board.poke(c.p, c.id, c.arg orelse "") catch |err| - c.p.reportError(c.id, "poke", err); - } -}; - -/// Dump a range of the board memory in hex, into a pane. -pub const Hexdump = struct { - pub const scope: Scope = .session; - pub const takes_arg = true; - pub const requires_arg = true; - pub const enabled = Board.enabled; - pub const output: OutputTraits = .{ .name = config.hexdump_buffer }; - pub fn run(c: Ctx) void { - Board.hexdump(c.p, c.id, c.arg orelse "") catch |err| - c.p.reportError(c.id, "hexdump", err); - } -}; - -/// Read or set the board GPIO pins, into a pane. -pub const Gpio = struct { - pub const scope: Scope = .session; - pub const takes_arg = true; - pub const enabled = Board.enabled; - pub const output: OutputTraits = .{ .name = config.gpio_buffer }; - pub fn run(c: Ctx) void { - Board.gpio(c.p, c.id, c.arg orelse "") catch |err| - c.p.reportError(c.id, "gpio", err); - } -}; - -const Board = struct { - const enabled = pardes.platform == .esp32p4; - - comptime { - if (enabled and pardes.hosted) @compileError("an OS is not bare metal"); - if (enabled and builtin.os.tag != .freestanding) @compileError("the P4 firmware is freestanding"); - if (enabled and builtin.target.cpu.arch.isWasm()) @compileError("wasm addresses are not a bus"); - } - - // Bound output sent over the board's 115200-baud console. - const max_bytes: u32 = 4096; - const max_words: u32 = max_bytes / 4; - - const address_space_end: u64 = 1 << 32; - - const Error = error{ - MissingAddress, - BadAddress, - BadCount, - MissingValue, - BadValue, - MisalignedAddress, - ExtraArgument, - BadPin, - NoPads, - }; - - fn parseHex(comptime T: type, tok: []const u8, bad: Error) Error!T { - const body = if (tok.len > 2 and tok[0] == '0' and (tok[1] | 0x20) == 'x') tok[2..] else tok; - return std.fmt.parseInt(T, body, 16) catch bad; - } - - fn parseAddr(tok: []const u8) Error!u32 { - return parseHex(u32, tok, Error.BadAddress); - } - - fn parseCount(tok: []const u8) Error!u64 { - return parseHex(u64, tok, Error.BadCount); - } - - fn parseValue(tok: []const u8) Error!u32 { - return parseHex(u32, tok, Error.BadValue); - } - - // Callers reject unaligned words; volatile accesses must reach the device. - fn readWord(addr: u32) u32 { - const cell: *allowzero const volatile u32 = @ptrFromInt(@as(usize, addr)); - return cell.*; - } - - fn writeWord(addr: u32, value: u32) void { - const cell: *allowzero volatile u32 = @ptrFromInt(@as(usize, addr)); - cell.* = value; - } - - fn readByte(addr: u32) u8 { - const cell: *allowzero const volatile u8 = @ptrFromInt(@as(usize, addr)); - return cell.*; - } - - const Limit = enum { - console, - space, - }; - - const Extent = struct { - count: u32, - limit: ?Limit, - }; - - fn extent(addr: u32, requested: u64, unit: u32, cap: u32) Extent { - var count = requested; - var limit: ?Limit = null; - if (count > cap) { - count = cap; - limit = .console; - } - const fits = (address_space_end - addr) / unit; - if (count > fits) { - count = fits; - limit = .space; - } - return .{ .count = @intCast(count), .limit = limit }; - } - - fn writeNote(w: *std.Io.Writer, e: Extent, requested: u64, unit_name: []const u8) !void { - switch (e.limit orelse return) { - .console => try w.print( - "clamped: 0x{x} {s} requested, 0x{x} shown (0x{x}-byte cap, one 115200-baud console)\n", - .{ requested, unit_name, e.count, max_bytes }, - ), - .space => try w.print( - "clamped: 0x{x} {s} requested, 0x{x} shown (the 32-bit address space ends at 0x100000000)\n", - .{ requested, unit_name, e.count }, - ), - } - } - - fn peek(p: *Pardes, id: usize, argument: []const u8) !void { - var it = std.mem.tokenizeAny(u8, argument, " \t\r\n"); - const addr = try parseAddr(it.next() orelse return Error.MissingAddress); - const requested = if (it.next()) |tok| try parseCount(tok) else 0x1; - if (it.next() != null) return Error.ExtraArgument; - if (addr % 4 != 0) return Error.MisalignedAddress; - - const e = extent(addr, requested, 4, max_words); - var out: std.Io.Writer.Allocating = .init(p.gpa); - errdefer out.deinit(); - try writeNote(&out.writer, e, requested, "words"); - for (0..e.count) |i| { - const at = addr + @as(u32, @intCast(i * 4)); - try out.writer.print("{x:0>8}: {x:0>8}\n", .{ at, readWord(at) }); - } - const content = try out.toOwnedSlice(); - try fill(p, id, .{ .cmd = .Peek }, content); - } - - fn poke(p: *Pardes, id: usize, argument: []const u8) !void { - var it = std.mem.tokenizeAny(u8, argument, " \t\r\n"); - const addr = try parseAddr(it.next() orelse return Error.MissingAddress); - const value = try parseValue(it.next() orelse return Error.MissingValue); - if (it.next() != null) return Error.ExtraArgument; - if (addr % 4 != 0) return Error.MisalignedAddress; - - writeWord(addr, value); - const back = readWord(addr); - var buf: [96]u8 = undefined; - // unreachable below: three 8-digit hex words and 18 bytes fit 96 - p.setMessage(id, std.fmt.bufPrint( - &buf, - "{x:0>8}: wrote {x:0>8}, reads {x:0>8}", - .{ addr, value, back }, - ) catch unreachable); - } - - fn gpio(p: *Pardes, id: usize, argument: []const u8) !void { - var it = std.mem.tokenizeAny(u8, argument, " \t\r\n"); - const tok = it.next() orelse { - const content = try p.gpa.dupe(u8, Header.text); - return fill(p, id, .{ .cmd = .Gpio }, content); - }; - if (it.next() != null) return Error.ExtraArgument; - const pin = std.fmt.parseInt(u16, tok, 10) catch return Error.BadPin; - - const toggle = p.host.vtable.gpio_toggle orelse return Error.NoPads; - var was: u8 = 0; - var now: u8 = 0; - if (!toggle(p.host.ctx, pin, &was, &now)) return Error.BadPin; - - var buf: [48]u8 = undefined; - // unreachable: three small numbers and 10 bytes fit 48 - p.setMessage(id, std.fmt.bufPrint(&buf, "GPIO {d}: {d}->{d}", .{ pin, was, now }) catch unreachable); - } - - const row_bytes: u32 = limits.hexdump_row_bytes; - - fn hexdump(p: *Pardes, id: usize, argument: []const u8) !void { - var it = std.mem.tokenizeAny(u8, argument, " \t\r\n"); - const addr = try parseAddr(it.next() orelse return Error.MissingAddress); - const requested = if (it.next()) |tok| try parseCount(tok) else 0x100; - if (it.next() != null) return Error.ExtraArgument; - - const e = extent(addr, requested, 1, max_bytes); - var out: std.Io.Writer.Allocating = .init(p.gpa); - errdefer out.deinit(); - try writeNote(&out.writer, e, requested, "bytes"); - var row: u32 = 0; - while (row < e.count) : (row += row_bytes) { - const n = @min(row_bytes, e.count - row); - var bytes: [row_bytes]u8 = undefined; - for (0..n) |i| bytes[i] = readByte(addr + row + @as(u32, @intCast(i))); - try out.writer.print("{x:0>8} ", .{addr + row}); - for (0..row_bytes) |i| { - if (i == row_bytes / 2) try out.writer.writeByte(' '); - if (i < n) - try out.writer.print(" {x:0>2}", .{bytes[i]}) - else - try out.writer.writeAll(" "); - } - try out.writer.writeAll(" |"); - for (0..n) |i| try out.writer.writeByte( - if (bytes[i] >= 0x20 and bytes[i] < 0x7f) bytes[i] else '.', - ); - try out.writer.writeAll("|\n"); - } - const content = try out.toOwnedSlice(); - try fill(p, id, .{ .cmd = .Hexdump }, content); - } - - fn fill(p: *Pardes, id: usize, from: panes.Output.Origin, content: []u8) !void { - const pane = p.panes[id] orelse { - p.gpa.free(content); - return error.MissingPane; - }; - const dir = if (pane.file) |f| panes.Dir.ofName(f.path) else pane.cwdSlice(); - try panes.Output.fillResults(p, id, dir, from, "", content, null); - } - - test "the clamp reports the tighter bound and never wraps the address space" { - const eq = std.testing.expectEqual; - try eq(Extent{ .count = 3, .limit = null }, extent(0x4ff40000, 3, 4, max_words)); - try eq(Extent{ .count = max_words, .limit = .console }, extent(0x4ff40000, 99_999, 4, max_words)); - try eq(Extent{ .count = max_bytes, .limit = .console }, extent(0, 100_000, 1, max_bytes)); - try eq(Extent{ .count = 16, .limit = .space }, extent(0xfffffff0, 64, 1, max_bytes)); - try eq(Extent{ .count = 4, .limit = .space }, extent(0xfffffff0, 64, 4, max_words)); - try eq(Extent{ .count = 0, .limit = .space }, extent(0xffffffff, 1, 4, max_words)); - try eq(Extent{ .count = max_bytes, .limit = .console }, extent(0xffff0000, 1 << 20, 1, max_bytes)); - } - - test "a clamp note is written exactly when something was clamped" { - var buf: [256]u8 = undefined; - var w: std.Io.Writer = .fixed(&buf); - - try writeNote(&w, extent(0x4ff40000, 3, 4, max_words), 3, "words"); - try std.testing.expectEqualStrings("", w.buffered()); - - try writeNote(&w, extent(0x4ff40000, 99_999, 4, max_words), 99_999, "words"); - try std.testing.expectEqualStrings( - "clamped: 0x1869f words requested, 0x400 shown (0x1000-byte cap, one 115200-baud console)\n", - w.buffered(), - ); - - w = .fixed(&buf); - try writeNote(&w, extent(0xfffffff0, 64, 1, max_bytes), 64, "bytes"); - try std.testing.expectEqualStrings( - "clamped: 0x40 bytes requested, 0x10 shown (the 32-bit address space ends at 0x100000000)\n", - w.buffered(), - ); - } - - test "every literal is hex, with or without the prefix" { - const eq = std.testing.expectEqual; - try eq(0x4ff40000, parseAddr("0x4ff40000")); - try eq(0x4ff40000, parseAddr("4ff40000")); - try eq(0x4ff40000, parseAddr("0X4FF40000")); - try eq(0x4ff40000, parseAddr("4FF40000")); - try eq(0x100, parseCount("100")); - try eq(0x256, parseCount("256")); - try eq(0xdeadbeef, parseValue("deadbeef")); - try std.testing.expectError(Error.BadAddress, parseAddr("0x100000000")); - try std.testing.expectError(Error.BadAddress, parseAddr("0x")); - try std.testing.expectError(Error.BadAddress, parseAddr("nope")); - try std.testing.expectError(Error.BadAddress, parseAddr("12g4")); - try std.testing.expectError(Error.BadCount, parseCount("-1")); - try std.testing.expectError(Error.BadValue, parseValue("0x1_0000_0000")); - } - - test "the pinout fits the board's own grid, in two aligned columns" { - const cols: usize = @import("pardes_config").esp32p4_cols; - const usable = cols - 7; - - var rows: usize = 0; - var pins: usize = 0; - var first_bar: ?usize = null; - var it = std.mem.splitScalar(u8, Header.text, '\n'); - while (it.next()) |line| { - try std.testing.expect(line.len <= usable); - rows += 1; - const bar = std.mem.indexOfScalar(u8, line, '|') orelse continue; - if (line[line.len - 1] == '+') continue; - pins += 1; - if (first_bar) |b| try std.testing.expectEqual(b, bar) else first_bar = bar; - } - try std.testing.expectEqual(@as(usize, 13), pins); - try std.testing.expect(rows > 15); - - try std.testing.expect(std.mem.indexOf(u8, Header.text, "GPIO 20 | 17 |") != null); - try std.testing.expect(std.mem.indexOf(u8, Header.text, "| 8 | --") != null); - } -}; - test "Exit and Restore name a modified +New scratch as well as files" { const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true }); defer p.deinit(); |
