From 19975bb80e0a062dc08ef5c95a545d2c6236e12e Mon Sep 17 00:00:00 2001 From: Piotr Fila Date: Sat, 31 Jan 2026 23:57:56 +0100 Subject: [PATCH 01/23] speed up cache regen by not compiling sqlite twice --- tools/regz/build.zig | 7 ------- tools/regz/build.zig.zon | 4 ---- 2 files changed, 11 deletions(-) diff --git a/tools/regz/build.zig b/tools/regz/build.zig index 1570f018b..7d2ef742c 100644 --- a/tools/regz/build.zig +++ b/tools/regz/build.zig @@ -16,19 +16,12 @@ pub fn build(b: *Build) !void { .iconv = false, }); - const sqlite3_dep = b.dependency("sqlite3", .{ - .target = target, - .optimize = .ReleaseSafe, - }); - const sqlite3_lib = sqlite3_dep.artifact("sqlite3"); - const zqlite_dep = b.dependency("zqlite", .{ .target = target, .optimize = optimize, }); const zqlite = zqlite_dep.module("zqlite"); - zqlite.linkLibrary(sqlite3_lib); const regz = b.addExecutable(.{ .name = "regz", diff --git a/tools/regz/build.zig.zon b/tools/regz/build.zig.zon index 154dc6eaa..bdb2a6424 100644 --- a/tools/regz/build.zig.zon +++ b/tools/regz/build.zig.zon @@ -13,10 +13,6 @@ .url = "git+https://github.com/mattnite/zig-build-libxml2.git#5474281ad4d173ed298ee789c7dce4f5edb78e10", .hash = "libxml2-0.0.0-Kr0Y1Ac4ngAiRuFTS2qMO9j-KZsD0PKFKKXoZjynTHLq", }, - .sqlite3 = .{ - .url = "git+https://github.com/allyourcodebase/sqlite3#8f840560eae88ab66668c6827c64ffbd0d74ef37", - .hash = "sqlite3-3.51.0-DMxLWssOAABZ8cAvU_LfBIbp0kZjm824PU8sSLXpEDdr", - }, .zqlite = .{ .url = "git+https://github.com/karlseguin/zqlite.zig#b44ed5cdc64859b08446c246f307e65ebe2b2d9c", .hash = "zqlite-0.0.0-RWLaY_y_mADh2LdbDrG_2HT2dBAcsAR8Jig_7-dOJd0B", From 0d097f11ae4bbd1d801f1e32890d76df89eb7374 Mon Sep 17 00:00:00 2001 From: Piotr Fila Date: Sun, 1 Feb 2026 04:14:53 +0100 Subject: [PATCH 02/23] reduce use of Mmio --- port/raspberrypi/rp2xxx/patches/rp2040.zon | 187 +++++++++++++ port/raspberrypi/rp2xxx/patches/rp2350.zon | 305 ++++++++++++++++++++- port/raspberrypi/rp2xxx/src/hal/gpio.zig | 75 +---- port/raspberrypi/rp2xxx/src/hal/usb.zig | 8 +- 4 files changed, 488 insertions(+), 87 deletions(-) diff --git a/port/raspberrypi/rp2xxx/patches/rp2040.zon b/port/raspberrypi/rp2xxx/patches/rp2040.zon index 75af11c9e..bb46f181c 100644 --- a/port/raspberrypi/rp2xxx/patches/rp2040.zon +++ b/port/raspberrypi/rp2xxx/patches/rp2040.zon @@ -1,4 +1,191 @@ .{ + .{ .add_enum_and_apply = .{ + .parent = "types.peripherals.IO_BANK0", + .@"enum" = .{ + .name = "Override", + .bitsize = 2, + .fields = .{ + .{ .value = 0, .name = "normal" }, + .{ .value = 1, .name = "invert" }, + .{ .value = 2, .name = "low" }, + .{ .value = 3, .name = "high" }, + }, + }, + .apply_to = .{ + "types.peripherals.IO_BANK0.GPIO0_CTRL.INOVER", + "types.peripherals.IO_BANK0.GPIO0_CTRL.IRQOVER", + "types.peripherals.IO_BANK0.GPIO0_CTRL.OEOVER", + "types.peripherals.IO_BANK0.GPIO0_CTRL.OUTOVER", + "types.peripherals.IO_BANK0.GPIO1_CTRL.INOVER", + "types.peripherals.IO_BANK0.GPIO1_CTRL.IRQOVER", + "types.peripherals.IO_BANK0.GPIO1_CTRL.OEOVER", + "types.peripherals.IO_BANK0.GPIO1_CTRL.OUTOVER", + "types.peripherals.IO_BANK0.GPIO2_CTRL.INOVER", + "types.peripherals.IO_BANK0.GPIO2_CTRL.IRQOVER", + "types.peripherals.IO_BANK0.GPIO2_CTRL.OEOVER", + "types.peripherals.IO_BANK0.GPIO2_CTRL.OUTOVER", + "types.peripherals.IO_BANK0.GPIO3_CTRL.INOVER", + "types.peripherals.IO_BANK0.GPIO3_CTRL.IRQOVER", + "types.peripherals.IO_BANK0.GPIO3_CTRL.OEOVER", + "types.peripherals.IO_BANK0.GPIO3_CTRL.OUTOVER", + "types.peripherals.IO_BANK0.GPIO4_CTRL.INOVER", + "types.peripherals.IO_BANK0.GPIO4_CTRL.IRQOVER", + "types.peripherals.IO_BANK0.GPIO4_CTRL.OEOVER", + "types.peripherals.IO_BANK0.GPIO4_CTRL.OUTOVER", + "types.peripherals.IO_BANK0.GPIO5_CTRL.INOVER", + "types.peripherals.IO_BANK0.GPIO5_CTRL.IRQOVER", + "types.peripherals.IO_BANK0.GPIO5_CTRL.OEOVER", + "types.peripherals.IO_BANK0.GPIO5_CTRL.OUTOVER", + "types.peripherals.IO_BANK0.GPIO6_CTRL.INOVER", + "types.peripherals.IO_BANK0.GPIO6_CTRL.IRQOVER", + "types.peripherals.IO_BANK0.GPIO6_CTRL.OEOVER", + "types.peripherals.IO_BANK0.GPIO6_CTRL.OUTOVER", + "types.peripherals.IO_BANK0.GPIO7_CTRL.INOVER", + "types.peripherals.IO_BANK0.GPIO7_CTRL.IRQOVER", + "types.peripherals.IO_BANK0.GPIO7_CTRL.OEOVER", + "types.peripherals.IO_BANK0.GPIO7_CTRL.OUTOVER", + "types.peripherals.IO_BANK0.GPIO8_CTRL.INOVER", + "types.peripherals.IO_BANK0.GPIO8_CTRL.IRQOVER", + "types.peripherals.IO_BANK0.GPIO8_CTRL.OEOVER", + "types.peripherals.IO_BANK0.GPIO8_CTRL.OUTOVER", + "types.peripherals.IO_BANK0.GPIO9_CTRL.INOVER", + "types.peripherals.IO_BANK0.GPIO9_CTRL.IRQOVER", + "types.peripherals.IO_BANK0.GPIO9_CTRL.OEOVER", + "types.peripherals.IO_BANK0.GPIO9_CTRL.OUTOVER", + "types.peripherals.IO_BANK0.GPIO10_CTRL.INOVER", + "types.peripherals.IO_BANK0.GPIO10_CTRL.IRQOVER", + "types.peripherals.IO_BANK0.GPIO10_CTRL.OEOVER", + "types.peripherals.IO_BANK0.GPIO10_CTRL.OUTOVER", + "types.peripherals.IO_BANK0.GPIO11_CTRL.INOVER", + "types.peripherals.IO_BANK0.GPIO11_CTRL.IRQOVER", + "types.peripherals.IO_BANK0.GPIO11_CTRL.OEOVER", + "types.peripherals.IO_BANK0.GPIO11_CTRL.OUTOVER", + "types.peripherals.IO_BANK0.GPIO12_CTRL.INOVER", + "types.peripherals.IO_BANK0.GPIO12_CTRL.IRQOVER", + "types.peripherals.IO_BANK0.GPIO12_CTRL.OEOVER", + "types.peripherals.IO_BANK0.GPIO12_CTRL.OUTOVER", + "types.peripherals.IO_BANK0.GPIO13_CTRL.INOVER", + "types.peripherals.IO_BANK0.GPIO13_CTRL.IRQOVER", + "types.peripherals.IO_BANK0.GPIO13_CTRL.OEOVER", + "types.peripherals.IO_BANK0.GPIO13_CTRL.OUTOVER", + "types.peripherals.IO_BANK0.GPIO14_CTRL.INOVER", + "types.peripherals.IO_BANK0.GPIO14_CTRL.IRQOVER", + "types.peripherals.IO_BANK0.GPIO14_CTRL.OEOVER", + "types.peripherals.IO_BANK0.GPIO14_CTRL.OUTOVER", + "types.peripherals.IO_BANK0.GPIO15_CTRL.INOVER", + "types.peripherals.IO_BANK0.GPIO15_CTRL.IRQOVER", + "types.peripherals.IO_BANK0.GPIO15_CTRL.OEOVER", + "types.peripherals.IO_BANK0.GPIO15_CTRL.OUTOVER", + "types.peripherals.IO_BANK0.GPIO16_CTRL.INOVER", + "types.peripherals.IO_BANK0.GPIO16_CTRL.IRQOVER", + "types.peripherals.IO_BANK0.GPIO16_CTRL.OEOVER", + "types.peripherals.IO_BANK0.GPIO16_CTRL.OUTOVER", + "types.peripherals.IO_BANK0.GPIO17_CTRL.INOVER", + "types.peripherals.IO_BANK0.GPIO17_CTRL.IRQOVER", + "types.peripherals.IO_BANK0.GPIO17_CTRL.OEOVER", + "types.peripherals.IO_BANK0.GPIO17_CTRL.OUTOVER", + "types.peripherals.IO_BANK0.GPIO18_CTRL.INOVER", + "types.peripherals.IO_BANK0.GPIO18_CTRL.IRQOVER", + "types.peripherals.IO_BANK0.GPIO18_CTRL.OEOVER", + "types.peripherals.IO_BANK0.GPIO18_CTRL.OUTOVER", + "types.peripherals.IO_BANK0.GPIO19_CTRL.INOVER", + "types.peripherals.IO_BANK0.GPIO19_CTRL.IRQOVER", + "types.peripherals.IO_BANK0.GPIO19_CTRL.OEOVER", + "types.peripherals.IO_BANK0.GPIO19_CTRL.OUTOVER", + "types.peripherals.IO_BANK0.GPIO20_CTRL.INOVER", + "types.peripherals.IO_BANK0.GPIO20_CTRL.IRQOVER", + "types.peripherals.IO_BANK0.GPIO20_CTRL.OEOVER", + "types.peripherals.IO_BANK0.GPIO20_CTRL.OUTOVER", + "types.peripherals.IO_BANK0.GPIO21_CTRL.INOVER", + "types.peripherals.IO_BANK0.GPIO21_CTRL.IRQOVER", + "types.peripherals.IO_BANK0.GPIO21_CTRL.OEOVER", + "types.peripherals.IO_BANK0.GPIO21_CTRL.OUTOVER", + "types.peripherals.IO_BANK0.GPIO22_CTRL.INOVER", + "types.peripherals.IO_BANK0.GPIO22_CTRL.IRQOVER", + "types.peripherals.IO_BANK0.GPIO22_CTRL.OEOVER", + "types.peripherals.IO_BANK0.GPIO22_CTRL.OUTOVER", + "types.peripherals.IO_BANK0.GPIO23_CTRL.INOVER", + "types.peripherals.IO_BANK0.GPIO23_CTRL.IRQOVER", + "types.peripherals.IO_BANK0.GPIO23_CTRL.OEOVER", + "types.peripherals.IO_BANK0.GPIO23_CTRL.OUTOVER", + "types.peripherals.IO_BANK0.GPIO24_CTRL.INOVER", + "types.peripherals.IO_BANK0.GPIO24_CTRL.IRQOVER", + "types.peripherals.IO_BANK0.GPIO24_CTRL.OEOVER", + "types.peripherals.IO_BANK0.GPIO24_CTRL.OUTOVER", + "types.peripherals.IO_BANK0.GPIO25_CTRL.INOVER", + "types.peripherals.IO_BANK0.GPIO25_CTRL.IRQOVER", + "types.peripherals.IO_BANK0.GPIO25_CTRL.OEOVER", + "types.peripherals.IO_BANK0.GPIO25_CTRL.OUTOVER", + "types.peripherals.IO_BANK0.GPIO26_CTRL.INOVER", + "types.peripherals.IO_BANK0.GPIO26_CTRL.IRQOVER", + "types.peripherals.IO_BANK0.GPIO26_CTRL.OEOVER", + "types.peripherals.IO_BANK0.GPIO26_CTRL.OUTOVER", + "types.peripherals.IO_BANK0.GPIO27_CTRL.INOVER", + "types.peripherals.IO_BANK0.GPIO27_CTRL.IRQOVER", + "types.peripherals.IO_BANK0.GPIO27_CTRL.OEOVER", + "types.peripherals.IO_BANK0.GPIO27_CTRL.OUTOVER", + "types.peripherals.IO_BANK0.GPIO28_CTRL.INOVER", + "types.peripherals.IO_BANK0.GPIO28_CTRL.IRQOVER", + "types.peripherals.IO_BANK0.GPIO28_CTRL.OEOVER", + "types.peripherals.IO_BANK0.GPIO28_CTRL.OUTOVER", + "types.peripherals.IO_BANK0.GPIO29_CTRL.INOVER", + "types.peripherals.IO_BANK0.GPIO29_CTRL.IRQOVER", + "types.peripherals.IO_BANK0.GPIO29_CTRL.OEOVER", + "types.peripherals.IO_BANK0.GPIO29_CTRL.OUTOVER", + }, + } }, + .{ .add_enum_and_apply = .{ + .parent = "types.peripherals.IO_BANK0", + .@"enum" = .{ + .name = "Function", + .bitsize = 5, + .fields = .{ + .{ .value = 0, .name = "xip" }, + .{ .value = 1, .name = "spi" }, + .{ .value = 2, .name = "uart" }, + .{ .value = 3, .name = "i2c" }, + .{ .value = 4, .name = "pwm" }, + .{ .value = 5, .name = "sio" }, + .{ .value = 6, .name = "pio0" }, + .{ .value = 7, .name = "pio1" }, + .{ .value = 8, .name = "gpck" }, + .{ .value = 9, .name = "usb" }, + .{ .value = 0x1f, .name = "disabled" }, + }, + }, + .apply_to = .{ + "types.peripherals.IO_BANK0.GPIO0_CTRL.FUNCSEL", + "types.peripherals.IO_BANK0.GPIO1_CTRL.FUNCSEL", + "types.peripherals.IO_BANK0.GPIO2_CTRL.FUNCSEL", + "types.peripherals.IO_BANK0.GPIO3_CTRL.FUNCSEL", + "types.peripherals.IO_BANK0.GPIO4_CTRL.FUNCSEL", + "types.peripherals.IO_BANK0.GPIO5_CTRL.FUNCSEL", + "types.peripherals.IO_BANK0.GPIO6_CTRL.FUNCSEL", + "types.peripherals.IO_BANK0.GPIO7_CTRL.FUNCSEL", + "types.peripherals.IO_BANK0.GPIO8_CTRL.FUNCSEL", + "types.peripherals.IO_BANK0.GPIO9_CTRL.FUNCSEL", + "types.peripherals.IO_BANK0.GPIO10_CTRL.FUNCSEL", + "types.peripherals.IO_BANK0.GPIO11_CTRL.FUNCSEL", + "types.peripherals.IO_BANK0.GPIO12_CTRL.FUNCSEL", + "types.peripherals.IO_BANK0.GPIO13_CTRL.FUNCSEL", + "types.peripherals.IO_BANK0.GPIO14_CTRL.FUNCSEL", + "types.peripherals.IO_BANK0.GPIO15_CTRL.FUNCSEL", + "types.peripherals.IO_BANK0.GPIO16_CTRL.FUNCSEL", + "types.peripherals.IO_BANK0.GPIO17_CTRL.FUNCSEL", + "types.peripherals.IO_BANK0.GPIO18_CTRL.FUNCSEL", + "types.peripherals.IO_BANK0.GPIO19_CTRL.FUNCSEL", + "types.peripherals.IO_BANK0.GPIO20_CTRL.FUNCSEL", + "types.peripherals.IO_BANK0.GPIO21_CTRL.FUNCSEL", + "types.peripherals.IO_BANK0.GPIO22_CTRL.FUNCSEL", + "types.peripherals.IO_BANK0.GPIO23_CTRL.FUNCSEL", + "types.peripherals.IO_BANK0.GPIO24_CTRL.FUNCSEL", + "types.peripherals.IO_BANK0.GPIO25_CTRL.FUNCSEL", + "types.peripherals.IO_BANK0.GPIO26_CTRL.FUNCSEL", + "types.peripherals.IO_BANK0.GPIO27_CTRL.FUNCSEL", + "types.peripherals.IO_BANK0.GPIO28_CTRL.FUNCSEL", + "types.peripherals.IO_BANK0.GPIO29_CTRL.FUNCSEL", + }, + } }, .{ .add_enum_and_apply = .{ .parent = "types.peripherals.USB_DPRAM", .@"enum" = .{ diff --git a/port/raspberrypi/rp2xxx/patches/rp2350.zon b/port/raspberrypi/rp2xxx/patches/rp2350.zon index 2f28af5b9..093aa5c9a 100644 --- a/port/raspberrypi/rp2xxx/patches/rp2350.zon +++ b/port/raspberrypi/rp2xxx/patches/rp2350.zon @@ -1,19 +1,300 @@ .{ - .{ - .add_enum = .{ - .parent = "types.peripherals.PADS_BANK0", - .@"enum" = .{ - .name = "DriveStrength", - .bitsize = 2, - .fields = .{ - .{ .value = 0x0, .name = "2mA" }, - .{ .value = 0x1, .name = "4mA" }, - .{ .value = 0x2, .name = "8mA" }, - .{ .value = 0x3, .name = "12mA" }, + .{ .add_enum_and_apply = .{ + .parent = "types.peripherals.IO_BANK0", + .@"enum" = .{ + .name = "Override", + .bitsize = 2, + .fields = .{ + .{ .value = 0, .name = "normal" }, + .{ .value = 1, .name = "invert" }, + .{ .value = 2, .name = "low" }, + .{ .value = 3, .name = "high" }, + }, + }, + .apply_to = .{ + "types.peripherals.IO_BANK0.GPIO0_CTRL.INOVER", + "types.peripherals.IO_BANK0.GPIO0_CTRL.IRQOVER", + "types.peripherals.IO_BANK0.GPIO0_CTRL.OEOVER", + "types.peripherals.IO_BANK0.GPIO0_CTRL.OUTOVER", + "types.peripherals.IO_BANK0.GPIO1_CTRL.INOVER", + "types.peripherals.IO_BANK0.GPIO1_CTRL.IRQOVER", + "types.peripherals.IO_BANK0.GPIO1_CTRL.OEOVER", + "types.peripherals.IO_BANK0.GPIO1_CTRL.OUTOVER", + "types.peripherals.IO_BANK0.GPIO2_CTRL.INOVER", + "types.peripherals.IO_BANK0.GPIO2_CTRL.IRQOVER", + "types.peripherals.IO_BANK0.GPIO2_CTRL.OEOVER", + "types.peripherals.IO_BANK0.GPIO2_CTRL.OUTOVER", + "types.peripherals.IO_BANK0.GPIO3_CTRL.INOVER", + "types.peripherals.IO_BANK0.GPIO3_CTRL.IRQOVER", + "types.peripherals.IO_BANK0.GPIO3_CTRL.OEOVER", + "types.peripherals.IO_BANK0.GPIO3_CTRL.OUTOVER", + "types.peripherals.IO_BANK0.GPIO4_CTRL.INOVER", + "types.peripherals.IO_BANK0.GPIO4_CTRL.IRQOVER", + "types.peripherals.IO_BANK0.GPIO4_CTRL.OEOVER", + "types.peripherals.IO_BANK0.GPIO4_CTRL.OUTOVER", + "types.peripherals.IO_BANK0.GPIO5_CTRL.INOVER", + "types.peripherals.IO_BANK0.GPIO5_CTRL.IRQOVER", + "types.peripherals.IO_BANK0.GPIO5_CTRL.OEOVER", + "types.peripherals.IO_BANK0.GPIO5_CTRL.OUTOVER", + "types.peripherals.IO_BANK0.GPIO6_CTRL.INOVER", + "types.peripherals.IO_BANK0.GPIO6_CTRL.IRQOVER", + "types.peripherals.IO_BANK0.GPIO6_CTRL.OEOVER", + "types.peripherals.IO_BANK0.GPIO6_CTRL.OUTOVER", + "types.peripherals.IO_BANK0.GPIO7_CTRL.INOVER", + "types.peripherals.IO_BANK0.GPIO7_CTRL.IRQOVER", + "types.peripherals.IO_BANK0.GPIO7_CTRL.OEOVER", + "types.peripherals.IO_BANK0.GPIO7_CTRL.OUTOVER", + "types.peripherals.IO_BANK0.GPIO8_CTRL.INOVER", + "types.peripherals.IO_BANK0.GPIO8_CTRL.IRQOVER", + "types.peripherals.IO_BANK0.GPIO8_CTRL.OEOVER", + "types.peripherals.IO_BANK0.GPIO8_CTRL.OUTOVER", + "types.peripherals.IO_BANK0.GPIO9_CTRL.INOVER", + "types.peripherals.IO_BANK0.GPIO9_CTRL.IRQOVER", + "types.peripherals.IO_BANK0.GPIO9_CTRL.OEOVER", + "types.peripherals.IO_BANK0.GPIO9_CTRL.OUTOVER", + "types.peripherals.IO_BANK0.GPIO10_CTRL.INOVER", + "types.peripherals.IO_BANK0.GPIO10_CTRL.IRQOVER", + "types.peripherals.IO_BANK0.GPIO10_CTRL.OEOVER", + "types.peripherals.IO_BANK0.GPIO10_CTRL.OUTOVER", + "types.peripherals.IO_BANK0.GPIO11_CTRL.INOVER", + "types.peripherals.IO_BANK0.GPIO11_CTRL.IRQOVER", + "types.peripherals.IO_BANK0.GPIO11_CTRL.OEOVER", + "types.peripherals.IO_BANK0.GPIO11_CTRL.OUTOVER", + "types.peripherals.IO_BANK0.GPIO12_CTRL.INOVER", + "types.peripherals.IO_BANK0.GPIO12_CTRL.IRQOVER", + "types.peripherals.IO_BANK0.GPIO12_CTRL.OEOVER", + "types.peripherals.IO_BANK0.GPIO12_CTRL.OUTOVER", + "types.peripherals.IO_BANK0.GPIO13_CTRL.INOVER", + "types.peripherals.IO_BANK0.GPIO13_CTRL.IRQOVER", + "types.peripherals.IO_BANK0.GPIO13_CTRL.OEOVER", + "types.peripherals.IO_BANK0.GPIO13_CTRL.OUTOVER", + "types.peripherals.IO_BANK0.GPIO14_CTRL.INOVER", + "types.peripherals.IO_BANK0.GPIO14_CTRL.IRQOVER", + "types.peripherals.IO_BANK0.GPIO14_CTRL.OEOVER", + "types.peripherals.IO_BANK0.GPIO14_CTRL.OUTOVER", + "types.peripherals.IO_BANK0.GPIO15_CTRL.INOVER", + "types.peripherals.IO_BANK0.GPIO15_CTRL.IRQOVER", + "types.peripherals.IO_BANK0.GPIO15_CTRL.OEOVER", + "types.peripherals.IO_BANK0.GPIO15_CTRL.OUTOVER", + "types.peripherals.IO_BANK0.GPIO16_CTRL.INOVER", + "types.peripherals.IO_BANK0.GPIO16_CTRL.IRQOVER", + "types.peripherals.IO_BANK0.GPIO16_CTRL.OEOVER", + "types.peripherals.IO_BANK0.GPIO16_CTRL.OUTOVER", + "types.peripherals.IO_BANK0.GPIO17_CTRL.INOVER", + "types.peripherals.IO_BANK0.GPIO17_CTRL.IRQOVER", + "types.peripherals.IO_BANK0.GPIO17_CTRL.OEOVER", + "types.peripherals.IO_BANK0.GPIO17_CTRL.OUTOVER", + "types.peripherals.IO_BANK0.GPIO18_CTRL.INOVER", + "types.peripherals.IO_BANK0.GPIO18_CTRL.IRQOVER", + "types.peripherals.IO_BANK0.GPIO18_CTRL.OEOVER", + "types.peripherals.IO_BANK0.GPIO18_CTRL.OUTOVER", + "types.peripherals.IO_BANK0.GPIO19_CTRL.INOVER", + "types.peripherals.IO_BANK0.GPIO19_CTRL.IRQOVER", + "types.peripherals.IO_BANK0.GPIO19_CTRL.OEOVER", + "types.peripherals.IO_BANK0.GPIO19_CTRL.OUTOVER", + "types.peripherals.IO_BANK0.GPIO20_CTRL.INOVER", + "types.peripherals.IO_BANK0.GPIO20_CTRL.IRQOVER", + "types.peripherals.IO_BANK0.GPIO20_CTRL.OEOVER", + "types.peripherals.IO_BANK0.GPIO20_CTRL.OUTOVER", + "types.peripherals.IO_BANK0.GPIO21_CTRL.INOVER", + "types.peripherals.IO_BANK0.GPIO21_CTRL.IRQOVER", + "types.peripherals.IO_BANK0.GPIO21_CTRL.OEOVER", + "types.peripherals.IO_BANK0.GPIO21_CTRL.OUTOVER", + "types.peripherals.IO_BANK0.GPIO22_CTRL.INOVER", + "types.peripherals.IO_BANK0.GPIO22_CTRL.IRQOVER", + "types.peripherals.IO_BANK0.GPIO22_CTRL.OEOVER", + "types.peripherals.IO_BANK0.GPIO22_CTRL.OUTOVER", + "types.peripherals.IO_BANK0.GPIO23_CTRL.INOVER", + "types.peripherals.IO_BANK0.GPIO23_CTRL.IRQOVER", + "types.peripherals.IO_BANK0.GPIO23_CTRL.OEOVER", + "types.peripherals.IO_BANK0.GPIO23_CTRL.OUTOVER", + "types.peripherals.IO_BANK0.GPIO24_CTRL.INOVER", + "types.peripherals.IO_BANK0.GPIO24_CTRL.IRQOVER", + "types.peripherals.IO_BANK0.GPIO24_CTRL.OEOVER", + "types.peripherals.IO_BANK0.GPIO24_CTRL.OUTOVER", + "types.peripherals.IO_BANK0.GPIO25_CTRL.INOVER", + "types.peripherals.IO_BANK0.GPIO25_CTRL.IRQOVER", + "types.peripherals.IO_BANK0.GPIO25_CTRL.OEOVER", + "types.peripherals.IO_BANK0.GPIO25_CTRL.OUTOVER", + "types.peripherals.IO_BANK0.GPIO26_CTRL.INOVER", + "types.peripherals.IO_BANK0.GPIO26_CTRL.IRQOVER", + "types.peripherals.IO_BANK0.GPIO26_CTRL.OEOVER", + "types.peripherals.IO_BANK0.GPIO26_CTRL.OUTOVER", + "types.peripherals.IO_BANK0.GPIO27_CTRL.INOVER", + "types.peripherals.IO_BANK0.GPIO27_CTRL.IRQOVER", + "types.peripherals.IO_BANK0.GPIO27_CTRL.OEOVER", + "types.peripherals.IO_BANK0.GPIO27_CTRL.OUTOVER", + "types.peripherals.IO_BANK0.GPIO28_CTRL.INOVER", + "types.peripherals.IO_BANK0.GPIO28_CTRL.IRQOVER", + "types.peripherals.IO_BANK0.GPIO28_CTRL.OEOVER", + "types.peripherals.IO_BANK0.GPIO28_CTRL.OUTOVER", + "types.peripherals.IO_BANK0.GPIO29_CTRL.INOVER", + "types.peripherals.IO_BANK0.GPIO29_CTRL.IRQOVER", + "types.peripherals.IO_BANK0.GPIO29_CTRL.OEOVER", + "types.peripherals.IO_BANK0.GPIO29_CTRL.OUTOVER", + "types.peripherals.IO_BANK0.GPIO30_CTRL.INOVER", + "types.peripherals.IO_BANK0.GPIO30_CTRL.IRQOVER", + "types.peripherals.IO_BANK0.GPIO30_CTRL.OEOVER", + "types.peripherals.IO_BANK0.GPIO30_CTRL.OUTOVER", + "types.peripherals.IO_BANK0.GPIO31_CTRL.INOVER", + "types.peripherals.IO_BANK0.GPIO31_CTRL.IRQOVER", + "types.peripherals.IO_BANK0.GPIO31_CTRL.OEOVER", + "types.peripherals.IO_BANK0.GPIO31_CTRL.OUTOVER", + "types.peripherals.IO_BANK0.GPIO32_CTRL.INOVER", + "types.peripherals.IO_BANK0.GPIO32_CTRL.IRQOVER", + "types.peripherals.IO_BANK0.GPIO32_CTRL.OEOVER", + "types.peripherals.IO_BANK0.GPIO32_CTRL.OUTOVER", + "types.peripherals.IO_BANK0.GPIO33_CTRL.INOVER", + "types.peripherals.IO_BANK0.GPIO33_CTRL.IRQOVER", + "types.peripherals.IO_BANK0.GPIO33_CTRL.OEOVER", + "types.peripherals.IO_BANK0.GPIO33_CTRL.OUTOVER", + "types.peripherals.IO_BANK0.GPIO34_CTRL.INOVER", + "types.peripherals.IO_BANK0.GPIO34_CTRL.IRQOVER", + "types.peripherals.IO_BANK0.GPIO34_CTRL.OEOVER", + "types.peripherals.IO_BANK0.GPIO34_CTRL.OUTOVER", + "types.peripherals.IO_BANK0.GPIO35_CTRL.INOVER", + "types.peripherals.IO_BANK0.GPIO35_CTRL.IRQOVER", + "types.peripherals.IO_BANK0.GPIO35_CTRL.OEOVER", + "types.peripherals.IO_BANK0.GPIO35_CTRL.OUTOVER", + "types.peripherals.IO_BANK0.GPIO36_CTRL.INOVER", + "types.peripherals.IO_BANK0.GPIO36_CTRL.IRQOVER", + "types.peripherals.IO_BANK0.GPIO36_CTRL.OEOVER", + "types.peripherals.IO_BANK0.GPIO36_CTRL.OUTOVER", + "types.peripherals.IO_BANK0.GPIO37_CTRL.INOVER", + "types.peripherals.IO_BANK0.GPIO37_CTRL.IRQOVER", + "types.peripherals.IO_BANK0.GPIO37_CTRL.OEOVER", + "types.peripherals.IO_BANK0.GPIO37_CTRL.OUTOVER", + "types.peripherals.IO_BANK0.GPIO38_CTRL.INOVER", + "types.peripherals.IO_BANK0.GPIO38_CTRL.IRQOVER", + "types.peripherals.IO_BANK0.GPIO38_CTRL.OEOVER", + "types.peripherals.IO_BANK0.GPIO38_CTRL.OUTOVER", + "types.peripherals.IO_BANK0.GPIO39_CTRL.INOVER", + "types.peripherals.IO_BANK0.GPIO39_CTRL.IRQOVER", + "types.peripherals.IO_BANK0.GPIO39_CTRL.OEOVER", + "types.peripherals.IO_BANK0.GPIO39_CTRL.OUTOVER", + "types.peripherals.IO_BANK0.GPIO40_CTRL.INOVER", + "types.peripherals.IO_BANK0.GPIO40_CTRL.IRQOVER", + "types.peripherals.IO_BANK0.GPIO40_CTRL.OEOVER", + "types.peripherals.IO_BANK0.GPIO40_CTRL.OUTOVER", + "types.peripherals.IO_BANK0.GPIO41_CTRL.INOVER", + "types.peripherals.IO_BANK0.GPIO41_CTRL.IRQOVER", + "types.peripherals.IO_BANK0.GPIO41_CTRL.OEOVER", + "types.peripherals.IO_BANK0.GPIO41_CTRL.OUTOVER", + "types.peripherals.IO_BANK0.GPIO42_CTRL.INOVER", + "types.peripherals.IO_BANK0.GPIO42_CTRL.IRQOVER", + "types.peripherals.IO_BANK0.GPIO42_CTRL.OEOVER", + "types.peripherals.IO_BANK0.GPIO42_CTRL.OUTOVER", + "types.peripherals.IO_BANK0.GPIO43_CTRL.INOVER", + "types.peripherals.IO_BANK0.GPIO43_CTRL.IRQOVER", + "types.peripherals.IO_BANK0.GPIO43_CTRL.OEOVER", + "types.peripherals.IO_BANK0.GPIO43_CTRL.OUTOVER", + "types.peripherals.IO_BANK0.GPIO44_CTRL.INOVER", + "types.peripherals.IO_BANK0.GPIO44_CTRL.IRQOVER", + "types.peripherals.IO_BANK0.GPIO44_CTRL.OEOVER", + "types.peripherals.IO_BANK0.GPIO44_CTRL.OUTOVER", + "types.peripherals.IO_BANK0.GPIO45_CTRL.INOVER", + "types.peripherals.IO_BANK0.GPIO45_CTRL.IRQOVER", + "types.peripherals.IO_BANK0.GPIO45_CTRL.OEOVER", + "types.peripherals.IO_BANK0.GPIO45_CTRL.OUTOVER", + "types.peripherals.IO_BANK0.GPIO46_CTRL.INOVER", + "types.peripherals.IO_BANK0.GPIO46_CTRL.IRQOVER", + "types.peripherals.IO_BANK0.GPIO46_CTRL.OEOVER", + "types.peripherals.IO_BANK0.GPIO46_CTRL.OUTOVER", + "types.peripherals.IO_BANK0.GPIO47_CTRL.INOVER", + "types.peripherals.IO_BANK0.GPIO47_CTRL.IRQOVER", + "types.peripherals.IO_BANK0.GPIO47_CTRL.OEOVER", + "types.peripherals.IO_BANK0.GPIO47_CTRL.OUTOVER", + }, + } }, + .{ .add_enum_and_apply = .{ + .parent = "types.peripherals.IO_BANK0", + .@"enum" = .{ + .name = "Function", + .bitsize = 5, + .fields = .{ + .{ .value = 0, .name = "hstx" }, + .{ .value = 1, .name = "spi" }, + .{ .value = 2, .name = "uart" }, + .{ .value = 3, .name = "i2c" }, + .{ .value = 4, .name = "pwm" }, + .{ .value = 5, .name = "sio" }, + .{ .value = 6, .name = "pio0" }, + .{ .value = 7, .name = "pio1" }, + .{ .value = 8, .name = "pio2" }, + .{ + .value = 9, + .name = "gpck", + .description = "Also QMI_CS1 and Trace", }, + .{ .value = 10, .name = "usb" }, + .{ .value = 11, .name = "uart_alt" }, + .{ .value = 0x1f, .name = "disabled" }, }, }, - }, + .apply_to = .{ + "types.peripherals.IO_BANK0.GPIO0_CTRL.FUNCSEL", + "types.peripherals.IO_BANK0.GPIO1_CTRL.FUNCSEL", + "types.peripherals.IO_BANK0.GPIO2_CTRL.FUNCSEL", + "types.peripherals.IO_BANK0.GPIO3_CTRL.FUNCSEL", + "types.peripherals.IO_BANK0.GPIO4_CTRL.FUNCSEL", + "types.peripherals.IO_BANK0.GPIO5_CTRL.FUNCSEL", + "types.peripherals.IO_BANK0.GPIO6_CTRL.FUNCSEL", + "types.peripherals.IO_BANK0.GPIO7_CTRL.FUNCSEL", + "types.peripherals.IO_BANK0.GPIO8_CTRL.FUNCSEL", + "types.peripherals.IO_BANK0.GPIO9_CTRL.FUNCSEL", + "types.peripherals.IO_BANK0.GPIO10_CTRL.FUNCSEL", + "types.peripherals.IO_BANK0.GPIO11_CTRL.FUNCSEL", + "types.peripherals.IO_BANK0.GPIO12_CTRL.FUNCSEL", + "types.peripherals.IO_BANK0.GPIO13_CTRL.FUNCSEL", + "types.peripherals.IO_BANK0.GPIO14_CTRL.FUNCSEL", + "types.peripherals.IO_BANK0.GPIO15_CTRL.FUNCSEL", + "types.peripherals.IO_BANK0.GPIO16_CTRL.FUNCSEL", + "types.peripherals.IO_BANK0.GPIO17_CTRL.FUNCSEL", + "types.peripherals.IO_BANK0.GPIO18_CTRL.FUNCSEL", + "types.peripherals.IO_BANK0.GPIO19_CTRL.FUNCSEL", + "types.peripherals.IO_BANK0.GPIO20_CTRL.FUNCSEL", + "types.peripherals.IO_BANK0.GPIO21_CTRL.FUNCSEL", + "types.peripherals.IO_BANK0.GPIO22_CTRL.FUNCSEL", + "types.peripherals.IO_BANK0.GPIO23_CTRL.FUNCSEL", + "types.peripherals.IO_BANK0.GPIO24_CTRL.FUNCSEL", + "types.peripherals.IO_BANK0.GPIO25_CTRL.FUNCSEL", + "types.peripherals.IO_BANK0.GPIO26_CTRL.FUNCSEL", + "types.peripherals.IO_BANK0.GPIO27_CTRL.FUNCSEL", + "types.peripherals.IO_BANK0.GPIO28_CTRL.FUNCSEL", + "types.peripherals.IO_BANK0.GPIO29_CTRL.FUNCSEL", + "types.peripherals.IO_BANK0.GPIO30_CTRL.FUNCSEL", + "types.peripherals.IO_BANK0.GPIO31_CTRL.FUNCSEL", + "types.peripherals.IO_BANK0.GPIO32_CTRL.FUNCSEL", + "types.peripherals.IO_BANK0.GPIO33_CTRL.FUNCSEL", + "types.peripherals.IO_BANK0.GPIO34_CTRL.FUNCSEL", + "types.peripherals.IO_BANK0.GPIO35_CTRL.FUNCSEL", + "types.peripherals.IO_BANK0.GPIO36_CTRL.FUNCSEL", + "types.peripherals.IO_BANK0.GPIO37_CTRL.FUNCSEL", + "types.peripherals.IO_BANK0.GPIO38_CTRL.FUNCSEL", + "types.peripherals.IO_BANK0.GPIO39_CTRL.FUNCSEL", + "types.peripherals.IO_BANK0.GPIO40_CTRL.FUNCSEL", + "types.peripherals.IO_BANK0.GPIO41_CTRL.FUNCSEL", + "types.peripherals.IO_BANK0.GPIO42_CTRL.FUNCSEL", + "types.peripherals.IO_BANK0.GPIO43_CTRL.FUNCSEL", + "types.peripherals.IO_BANK0.GPIO44_CTRL.FUNCSEL", + "types.peripherals.IO_BANK0.GPIO45_CTRL.FUNCSEL", + "types.peripherals.IO_BANK0.GPIO46_CTRL.FUNCSEL", + "types.peripherals.IO_BANK0.GPIO47_CTRL.FUNCSEL", + }, + } }, + .{ .add_enum = .{ + .parent = "types.peripherals.PADS_BANK0", + .@"enum" = .{ + .name = "DriveStrength", + .bitsize = 2, + .fields = .{ + .{ .value = 0x0, .name = "2mA" }, + .{ .value = 0x1, .name = "4mA" }, + .{ .value = 0x2, .name = "8mA" }, + .{ .value = 0x3, .name = "12mA" }, + }, + }, + } }, .{ .set_enum_type = .{ .of = "types.peripherals.PADS_BANK0.GPIO0.DRIVE", .to = "types.peripherals.PADS_BANK0.DriveStrength" } }, .{ .set_enum_type = .{ .of = "types.peripherals.PADS_BANK0.GPIO1.DRIVE", .to = "types.peripherals.PADS_BANK0.DriveStrength" } }, .{ .set_enum_type = .{ .of = "types.peripherals.PADS_BANK0.GPIO2.DRIVE", .to = "types.peripherals.PADS_BANK0.DriveStrength" } }, diff --git a/port/raspberrypi/rp2xxx/src/hal/gpio.zig b/port/raspberrypi/rp2xxx/src/hal/gpio.zig index 4cb8a9102..7126bf601 100644 --- a/port/raspberrypi/rp2xxx/src/hal/gpio.zig +++ b/port/raspberrypi/rp2xxx/src/hal/gpio.zig @@ -15,55 +15,19 @@ const NUM_BANK0_GPIOS = switch (chip) { .RP2350 => 48, }; -pub const Function = - switch (chip) { - .RP2040 => enum(u5) { - xip = 0, - spi, - uart, - i2c, - pwm, - sio, - pio0, - pio1, - gpck, - usb, - disabled = 0x1f, - }, - .RP2350 => enum(u5) { - hstx = 0, - spi, - uart, - i2c, - pwm, - sio, - pio0, - pio1, - pio2, - gpck, // Also QMI_CS1 and Trace - usb, - uart_alt, - disabled = 0x1f, - }, - }; pub const Direction = enum(u1) { in, out, }; -pub const Override = enum(u2) { - normal, - invert, - low, - high, -}; - pub const SlewRate = enum(u1) { slow, fast, }; +pub const Function = microzig.chip.types.peripherals.IO_BANK0.Function; +pub const Override = microzig.chip.types.peripherals.IO_BANK0.Override; pub const DriveStrength = microzig.chip.types.peripherals.PADS_BANK0.DriveStrength; pub const Pull = enum { @@ -259,37 +223,10 @@ pub const Mask = pub const Pin = enum(u6) { _, - pub const Regs = - switch (chip) { - .RP2040 => extern struct { - status: @TypeOf(IO_BANK0.GPIO0_STATUS), - ctrl: microzig.mmio.Mmio(packed struct(u32) { - FUNCSEL: Function, - reserved8: u3 = 0, - OUTOVER: Override, - reserved12: u2 = 0, - OEOVER: Override, - reserved16: u2 = 0, - INOVER: Override, - reserved28: u10 = 0, - IRQOVER: Override, - padding: u2 = 0, - }), - }, - .RP2350 => extern struct { - status: @TypeOf(IO_BANK0.GPIO0_STATUS), - ctrl: microzig.mmio.Mmio(packed struct(u32) { - FUNCSEL: Function, - reserved12: u7 = 0, - OUTOVER: Override, - OEOVER: Override, - INOVER: Override, - reserved28: u10 = 0, - IRQOVER: Override, - padding: u2 = 0, - }), - }, - }; + pub const Regs = struct { + status: @TypeOf(IO_BANK0.GPIO0_STATUS), + ctrl: @TypeOf(IO_BANK0.GPIO0_CTRL), + }; pub const RegsArray = switch (chip) { .RP2040 => *volatile [30]Regs, diff --git a/port/raspberrypi/rp2xxx/src/hal/usb.zig b/port/raspberrypi/rp2xxx/src/hal/usb.zig index 24c7afc2c..a8fc03c61 100644 --- a/port/raspberrypi/rp2xxx/src/hal/usb.zig +++ b/port/raspberrypi/rp2xxx/src/hal/usb.zig @@ -62,12 +62,8 @@ fn PerEndpoint(T: type) type { }; } -// It would be nice to instead generate those arrays automatically with a regz patch. -const BufferControlMmio = microzig.mmio.Mmio(@TypeOf(peripherals.USB_DPRAM.EP0_IN_BUFFER_CONTROL).underlying_type); -const buffer_control: *volatile [16]PerEndpoint(BufferControlMmio) = @ptrCast(&peripherals.USB_DPRAM.EP0_IN_BUFFER_CONTROL); - -const EndpointControlMimo = microzig.mmio.Mmio(@TypeOf(peripherals.USB_DPRAM.EP1_IN_CONTROL).underlying_type); -const endpoint_control: *volatile [15]PerEndpoint(EndpointControlMimo) = @ptrCast(&peripherals.USB_DPRAM.EP1_IN_CONTROL); +const buffer_control: *volatile [16]PerEndpoint(@TypeOf(peripherals.USB_DPRAM.EP0_IN_BUFFER_CONTROL)) = @ptrCast(&peripherals.USB_DPRAM.EP0_IN_BUFFER_CONTROL); +const endpoint_control: *volatile [15]PerEndpoint(@TypeOf(peripherals.USB_DPRAM.EP1_IN_CONTROL)) = @ptrCast(&peripherals.USB_DPRAM.EP1_IN_CONTROL); // +++++++++++++++++++++++++++++++++++++++++++++++++ // Code From 205861476fb7f95e1f05035e593a5dcac3b0ba0e Mon Sep 17 00:00:00 2001 From: Piotr Fila Date: Sun, 1 Feb 2026 04:21:27 +0100 Subject: [PATCH 03/23] Mmio -> OldMmio in non-generated code --- core/src/cpus/cortex_m.zig | 8 +-- core/src/cpus/cortex_m/m0.zig | 8 +-- core/src/cpus/cortex_m/m0plus.zig | 18 +++--- core/src/cpus/cortex_m/m3.zig | 24 ++++---- core/src/cpus/cortex_m/m33.zig | 28 +++++----- core/src/cpus/cortex_m/m4.zig | 28 +++++----- core/src/cpus/cortex_m/m55.zig | 22 ++++---- core/src/cpus/cortex_m/m7.zig | 56 +++++++++---------- core/src/mmio.zig | 2 + .../espressif/esp/src/hal/clocks/esp32_c3.zig | 2 +- port/raspberrypi/rp2xxx/src/hal/gpio.zig | 1 - port/stmicro/stm32/src/hals/STM32L47X/lcd.zig | 4 +- 12 files changed, 101 insertions(+), 100 deletions(-) diff --git a/core/src/cpus/cortex_m.zig b/core/src/cpus/cortex_m.zig index 2a8e2e425..7221ebf8e 100644 --- a/core/src/cpus/cortex_m.zig +++ b/core/src/cpus/cortex_m.zig @@ -1155,7 +1155,7 @@ pub const types = struct { /// System Timer (SysTick). pub const SysTick = extern struct { /// Control and Status Register. - CTRL: mmio.Mmio(packed struct(u32) { + CTRL: mmio.OldMmio(packed struct(u32) { /// Enables the counter: /// 0 = counter disabled. /// 1 = counter enabled. @@ -1176,20 +1176,20 @@ pub const types = struct { reserved1: u15 = 0, }), /// Reload Value Register. - LOAD: mmio.Mmio(packed struct(u32) { + LOAD: mmio.OldMmio(packed struct(u32) { /// Value to load into the VAL register when the counter is enabled and when it reaches 0. RELOAD: u24, reserved0: u8 = 0, }), /// Current Value Register. - VAL: mmio.Mmio(packed struct(u32) { + VAL: mmio.OldMmio(packed struct(u32) { /// Reads return the current value of the SysTick counter. /// A write of any value clears the field to 0, and also clears the CTRL.COUNTFLAG bit to 0. CURRENT: u24, reserved0: u8 = 0, }), /// Calibration Register. - CALIB: mmio.Mmio(packed struct(u32) { + CALIB: mmio.OldMmio(packed struct(u32) { /// Reload value for 10ms (100Hz) timing, subject to system clock skew errors. If the value /// reads as zero, the calibration value is not known. TENMS: u24, diff --git a/core/src/cpus/cortex_m/m0.zig b/core/src/cpus/cortex_m/m0.zig index 1e5d8927c..c9a7c7b42 100644 --- a/core/src/cpus/cortex_m/m0.zig +++ b/core/src/cpus/cortex_m/m0.zig @@ -9,7 +9,7 @@ pub const SystemControlBlock = extern struct { /// CPUID Base Register. CPUID: u32, /// Interrupt Control and State Register. - ICSR: mmio.Mmio(packed struct(u32) { + ICSR: mmio.OldMmio(packed struct(u32) { /// Contains the active exception number: /// 0 = Thread mode /// Nonzero = The exception number[a] of the currently active exception. @@ -79,7 +79,7 @@ pub const SystemControlBlock = extern struct { }), reserved0: u32 = 0, /// Application Interrupt and Reset Control Register. - AIRCR: mmio.Mmio(packed struct { + AIRCR: mmio.OldMmio(packed struct { reserved0: u1 = 0, /// Reserved for debug use. This bit reads as 0. When writing to the register you must /// write 0 to this bit, otherwise behavior is Unpredictable. @@ -101,7 +101,7 @@ pub const SystemControlBlock = extern struct { VECTKEY: u16, }), /// System Control Register. - SCR: mmio.Mmio(packed struct(u32) { + SCR: mmio.OldMmio(packed struct(u32) { reserved0: u1 = 0, /// Indicates sleep-on-exit when returning from Handler mode to Thread mode: /// 0 = do not sleep when returning to Thread mode. @@ -130,7 +130,7 @@ pub const SystemControlBlock = extern struct { reserved2: u27 = 0, }), /// Configuration Control Register. - CCR: mmio.Mmio(packed struct(u32) { + CCR: mmio.OldMmio(packed struct(u32) { reserved0: u3 = 0, /// Always reads as one, indicates that all unaligned accesses generate a HardFault. UNALIGN_TRP: u1, diff --git a/core/src/cpus/cortex_m/m0plus.zig b/core/src/cpus/cortex_m/m0plus.zig index 7aedb4113..b28c9f475 100644 --- a/core/src/cpus/cortex_m/m0plus.zig +++ b/core/src/cpus/cortex_m/m0plus.zig @@ -9,7 +9,7 @@ pub const SystemControlBlock = extern struct { /// CPUID Base Register. CPUID: u32, /// Interrupt Control and State Register. - ICSR: mmio.Mmio(packed struct(u32) { + ICSR: mmio.OldMmio(packed struct(u32) { /// Contains the active exception number: /// 0 = Thread mode /// Nonzero = The exception number[a] of the currently active exception. @@ -80,7 +80,7 @@ pub const SystemControlBlock = extern struct { /// Vector Table Offset Register. VTOR: u32, /// Application Interrupt and Reset Control Register. - AIRCR: mmio.Mmio(packed struct { + AIRCR: mmio.OldMmio(packed struct { reserved0: u1 = 0, /// Reserved for debug use. This bit reads as 0. When writing to the register you must /// write 0 to this bit, otherwise behavior is Unpredictable. @@ -102,7 +102,7 @@ pub const SystemControlBlock = extern struct { VECTKEY: u16, }), /// System Control Register. - SCR: mmio.Mmio(packed struct(u32) { + SCR: mmio.OldMmio(packed struct(u32) { reserved0: u1 = 0, /// Indicates sleep-on-exit when returning from Handler mode to Thread mode: /// 0 = do not sleep when returning to Thread mode. @@ -131,7 +131,7 @@ pub const SystemControlBlock = extern struct { reserved2: u27 = 0, }), /// Configuration Control Register. - CCR: mmio.Mmio(packed struct(u32) { + CCR: mmio.OldMmio(packed struct(u32) { reserved0: u3 = 0, /// Always reads as one, indicates that all unaligned accesses generate a HardFault. UNALIGN_TRP: u1, @@ -196,7 +196,7 @@ pub const NestedVectorInterruptController = extern struct { pub const MemoryProtectionUnit = extern struct { /// MPU Type Register - TYPE: mmio.Mmio(packed struct(u32) { + TYPE: mmio.OldMmio(packed struct(u32) { /// Indicates support for unified or separate instructions and data address regions. SEPARATE: u1, reserved0: u7 = 0, @@ -207,7 +207,7 @@ pub const MemoryProtectionUnit = extern struct { reserved1: u8 = 0, }), /// MPU Control Register - CTRL: mmio.Mmio(packed struct(u32) { + CTRL: mmio.OldMmio(packed struct(u32) { /// Enables the MPU ENABLE: u1, /// Enables of operation of MPU during HardFault and MNIHandlers. @@ -217,13 +217,13 @@ pub const MemoryProtectionUnit = extern struct { reserved0: u29 = 0, }), /// MPU Region Number Register - RNR: mmio.Mmio(packed struct(u32) { + RNR: mmio.OldMmio(packed struct(u32) { /// Indicates the memory region accessed by MPU RBAR and PMU RLAR. REGION: u8, reserved0: u24 = 0, }), /// MPU Region Base Address Register - RBAR: mmio.Mmio(packed struct(u32) { + RBAR: mmio.OldMmio(packed struct(u32) { /// MPU region field. REGION: u4, /// MPU region number valid bit. @@ -232,7 +232,7 @@ pub const MemoryProtectionUnit = extern struct { ADDR: u27, }), /// MPU Attribute and Size Register - RASR: mmio.Mmio(packed struct(u32) { + RASR: mmio.OldMmio(packed struct(u32) { /// Region enable bit. ENABLE: u1, /// Specifies the size of the MPU region. The minimum permitted value is 7 (b00111). diff --git a/core/src/cpus/cortex_m/m3.zig b/core/src/cpus/cortex_m/m3.zig index 7ec731d22..bdb164abb 100644 --- a/core/src/cpus/cortex_m/m3.zig +++ b/core/src/cpus/cortex_m/m3.zig @@ -9,7 +9,7 @@ pub const SystemControlBlock = extern struct { /// CPUID Base Register. CPUID: u32, /// Interrupt Control and State Register. - ICSR: mmio.Mmio(packed struct(u32) { + ICSR: mmio.OldMmio(packed struct(u32) { VECTACTIVE: u9, reserved0: u2 = 0, RETTOBASE: u1, @@ -28,7 +28,7 @@ pub const SystemControlBlock = extern struct { /// Vector Table Offset Register. VTOR: u32, /// Application Interrupt and Reset Control Register. - AIRCR: mmio.Mmio(packed struct { + AIRCR: mmio.OldMmio(packed struct { VECTRESET: u1, VECTCLRACTIVE: u1, SYSRESETREQ: u1, @@ -39,7 +39,7 @@ pub const SystemControlBlock = extern struct { VECTKEY: u16, }), /// System Control Register. - SCR: mmio.Mmio(packed struct { + SCR: mmio.OldMmio(packed struct { reserved0: u1 = 0, SLEEPONEXIT: u1, SLEEPDEEP: u1, @@ -48,7 +48,7 @@ pub const SystemControlBlock = extern struct { reserved2: u27 = 0, }), /// Configuration Control Register. - CCR: mmio.Mmio(packed struct(u32) { + CCR: mmio.OldMmio(packed struct(u32) { NONBASETHRDENA: u1, USERSETMPEND: u1, reserved0: u1 = 0, @@ -62,9 +62,9 @@ pub const SystemControlBlock = extern struct { /// System Handlers Priority Registers. SHPR: [3]u32, /// System Handler Control and State Register. - SHCSR: mmio.Mmio(shared.scb.SHCSR), + SHCSR: mmio.OldMmio(shared.scb.SHCSR), /// Configurable Fault Status Register. - CFSR: mmio.Mmio(packed struct(u32) { + CFSR: mmio.OldMmio(packed struct(u32) { /// MemManage Fault Register. MMFSR: shared.scb.MMFSR, /// BusFault Status Register. @@ -73,7 +73,7 @@ pub const SystemControlBlock = extern struct { UFSR: shared.scb.UFSR, }), /// HardFault Status Register. - HFSR: mmio.Mmio(shared.scb.HFSR), + HFSR: mmio.OldMmio(shared.scb.HFSR), reserved1: u32 = 0, /// MemManage Fault Address Register. MMFAR: u32, @@ -108,7 +108,7 @@ pub const NestedVectorInterruptController = extern struct { pub const MemoryProtectionUnit = extern struct { /// MPU Type Register - TYPE: mmio.Mmio(packed struct(u32) { + TYPE: mmio.OldMmio(packed struct(u32) { /// Indicates support for unified or separate instructions and data address regions. SEPARATE: u1, reserved0: u7 = 0, @@ -119,7 +119,7 @@ pub const MemoryProtectionUnit = extern struct { reserved1: u8 = 0, }), /// MPU Control Register - CTRL: mmio.Mmio(packed struct(u32) { + CTRL: mmio.OldMmio(packed struct(u32) { /// Enables the MPU ENABLE: u1, /// Enables of operation of MPU during HardFault and MNIHandlers. @@ -129,7 +129,7 @@ pub const MemoryProtectionUnit = extern struct { reserved0: u29 = 0, }), /// MPU Region Number Register - RNR: mmio.Mmio(packed struct(u32) { + RNR: mmio.OldMmio(packed struct(u32) { /// Indicates the memory region accessed by MPU RBAR and PMU RLAR. REGION: u8, reserved0: u24 = 0, @@ -151,7 +151,7 @@ pub const MemoryProtectionUnit = extern struct { /// MPU Alias 3 Region Attribute and Size Register RASR_A3: RASR_Register, - pub const RBAR_Register = mmio.Mmio(packed struct(u32) { + pub const RBAR_Register = mmio.OldMmio(packed struct(u32) { /// MPU region field. REGION: u4, /// MPU region number valid bit. @@ -160,7 +160,7 @@ pub const MemoryProtectionUnit = extern struct { ADDR: u27, }); - pub const RASR_Register = mmio.Mmio(packed struct(u32) { + pub const RASR_Register = mmio.OldMmio(packed struct(u32) { /// Region enable bit. ENABLE: u1, /// Specifies the size of the MPU protection region. diff --git a/core/src/cpus/cortex_m/m33.zig b/core/src/cpus/cortex_m/m33.zig index 3e93d5f96..fb0f4eeb1 100644 --- a/core/src/cpus/cortex_m/m33.zig +++ b/core/src/cpus/cortex_m/m33.zig @@ -18,7 +18,7 @@ pub const SystemControlBlock = extern struct { /// CPUID Base Register. CPUID: u32, /// Interrupt Control and State Register. - ICSR: mmio.Mmio(packed struct(u32) { + ICSR: mmio.OldMmio(packed struct(u32) { /// Contains the active exception number: /// 0 = Thread mode /// Nonzero = The exception number[a] of the currently active exception. @@ -109,7 +109,7 @@ pub const SystemControlBlock = extern struct { /// Vector Table Offset Register. VTOR: u32, /// Application Interrupt and Reset Control Register. - AIRCR: mmio.Mmio(packed struct { + AIRCR: mmio.OldMmio(packed struct { reserved0: u1 = 0, /// Reserved for debug use. This bit reads as 0. When writing to the register you must /// write 0 to this bit, otherwise behavior is Unpredictable. @@ -160,7 +160,7 @@ pub const SystemControlBlock = extern struct { VECTKEY: u16, }), /// System Control Register. - SCR: mmio.Mmio(packed struct(u32) { + SCR: mmio.OldMmio(packed struct(u32) { reserved0: u1 = 0, /// Indicates sleep-on-exit when returning from Handler mode to Thread mode: /// 0 = do not sleep when returning to Thread mode. @@ -192,7 +192,7 @@ pub const SystemControlBlock = extern struct { reserved1: u27 = 0, }), /// Configuration and Control Register. - CCR: mmio.Mmio(packed struct(u32) { + CCR: mmio.OldMmio(packed struct(u32) { reserved0: u1 = 0, /// User set pending determines if unpriviledged access to the STIR generates a fault. USERSETMPEND: u1, @@ -219,9 +219,9 @@ pub const SystemControlBlock = extern struct { /// System Handler Priority Registers. SHPR: [12]u8, /// System Handler Control and State Register. - SHCSR: mmio.Mmio(shared.scb.SHCSR), + SHCSR: mmio.OldMmio(shared.scb.SHCSR), /// Configurable Fault Status Register. - CFSR: mmio.Mmio(packed struct(u32) { + CFSR: mmio.OldMmio(packed struct(u32) { /// MemManage Fault Register. MMFSR: shared.scb.MMFSR, /// BusFault Status Register. @@ -230,7 +230,7 @@ pub const SystemControlBlock = extern struct { UFSR: shared.scb.UFSR, }), /// HardFault Status Register. - HFSR: mmio.Mmio(shared.scb.HFSR), + HFSR: mmio.OldMmio(shared.scb.HFSR), reserved0: u32 = 0, /// MemManage Fault Address Register. MMFAR: u32, @@ -240,7 +240,7 @@ pub const SystemControlBlock = extern struct { _AFSR: u32, reserved1: [18]u32, /// Coprocessor Access Control Register. - CPACR: mmio.Mmio(packed struct(u32) { + CPACR: mmio.OldMmio(packed struct(u32) { CP0: Privilege, CP1: Privilege, CP2: Privilege, @@ -269,7 +269,7 @@ pub const SystemControlBlock = extern struct { }; pub const FloatingPointUnit = extern struct { - FPCCR: mmio.Mmio(packed struct(u32) { + FPCCR: mmio.OldMmio(packed struct(u32) { LSPACT: u1, USER: u1, S: u1, @@ -382,7 +382,7 @@ pub const SecurityAttributionUnit = extern struct { pub const MemoryProtectionUnit = extern struct { /// MPU Type Register. - TYPE: mmio.Mmio(packed struct(u32) { + TYPE: mmio.OldMmio(packed struct(u32) { /// Indicates support for unified or separate instructions and data address regions. SEPARATE: u1, reserved0: u7 = 0, @@ -391,7 +391,7 @@ pub const MemoryProtectionUnit = extern struct { reserved1: u16 = 0, }), /// MPU Control Register. - CTRL: mmio.Mmio(packed struct(u32) { + CTRL: mmio.OldMmio(packed struct(u32) { /// Enables the MPU ENABLE: u1, /// Enables of operation of MPU during HardFault and MNIHandlers. @@ -401,7 +401,7 @@ pub const MemoryProtectionUnit = extern struct { reserved0: u29 = 0, }), /// MPU Region Number Register. - RNR: mmio.Mmio(packed struct(u32) { + RNR: mmio.OldMmio(packed struct(u32) { /// Indicates the memory region accessed by MPU RBAR and PMU RLAR. REGION: u8, reserved0: u24 = 0, @@ -429,7 +429,7 @@ pub const MemoryProtectionUnit = extern struct { MPU_MAIR1: u32, /// MPU Region Address Register format. - pub const RBAR_Register = mmio.Mmio(packed struct(u32) { + pub const RBAR_Register = mmio.OldMmio(packed struct(u32) { /// Execute Never defines if code can be executed from this region. XN: u1, /// Access permissions. @@ -442,7 +442,7 @@ pub const MemoryProtectionUnit = extern struct { }); /// MPU Region Limit Address Register format. - pub const RLAR_Register = mmio.Mmio(packed struct(u32) { + pub const RLAR_Register = mmio.OldMmio(packed struct(u32) { /// Enable the region. EN: u1, /// Attribue Index associates a set of attributes in the MPU MAIR0 and MPU MAIR1 fields. diff --git a/core/src/cpus/cortex_m/m4.zig b/core/src/cpus/cortex_m/m4.zig index 0fb631508..2e2cbc9cc 100644 --- a/core/src/cpus/cortex_m/m4.zig +++ b/core/src/cpus/cortex_m/m4.zig @@ -9,7 +9,7 @@ pub const SystemControlBlock = extern struct { /// CPUID Base Register. CPUID: u32, /// Interrupt Control and State Register. - ICSR: mmio.Mmio(packed struct(u32) { + ICSR: mmio.OldMmio(packed struct(u32) { VECTACTIVE: u9, reserved0: u2 = 0, RETTOBASE: u1, @@ -28,7 +28,7 @@ pub const SystemControlBlock = extern struct { /// Vector Table Offset Register. VTOR: u32, /// Application Interrupt and Reset Control Register. - AIRCR: mmio.Mmio(packed struct { + AIRCR: mmio.OldMmio(packed struct { VECTRESET: u1, VECTCLRACTIVE: u1, SYSRESETREQ: u1, @@ -39,7 +39,7 @@ pub const SystemControlBlock = extern struct { VECTKEY: u16, }), /// System Control Register. - SCR: mmio.Mmio(packed struct { + SCR: mmio.OldMmio(packed struct { reserved0: u1 = 0, SLEEPONEXIT: u1, SLEEPDEEP: u1, @@ -48,7 +48,7 @@ pub const SystemControlBlock = extern struct { reserved2: u27 = 0, }), /// Configuration Control Register. - CCR: mmio.Mmio(packed struct(u32) { + CCR: mmio.OldMmio(packed struct(u32) { NONBASETHRDENA: u1, USERSETMPEND: u1, reserved0: u1 = 0, @@ -62,9 +62,9 @@ pub const SystemControlBlock = extern struct { /// System Handlers Priority Registers. SHP: [12]u8, /// System Handler Contol and State Register. - SHCSR: mmio.Mmio(shared.scb.SHCSR), + SHCSR: mmio.OldMmio(shared.scb.SHCSR), /// Configurable Fault Status Register. - CFSR: mmio.Mmio(packed struct(u32) { + CFSR: mmio.OldMmio(packed struct(u32) { /// MemManage Fault Register. MMFSR: shared.scb.MMFSR, /// BusFault Status Register. @@ -73,7 +73,7 @@ pub const SystemControlBlock = extern struct { UFSR: shared.scb.UFSR, }), /// HardFault Status Register. - HFSR: mmio.Mmio(shared.scb.HFSR), + HFSR: mmio.OldMmio(shared.scb.HFSR), /// Debug Fault Status Register. DFSR: u32, /// MemManage Fault Address Register. @@ -83,7 +83,7 @@ pub const SystemControlBlock = extern struct { /// Auxilary Feature Register. AFSR: u32, reserved0: [18]u32, - CPACR: mmio.Mmio(packed struct(u32) { + CPACR: mmio.OldMmio(packed struct(u32) { reserved0: u20, CP10: Privilege, CP11: Privilege, @@ -126,7 +126,7 @@ pub const NestedVectorInterruptController = extern struct { pub const MemoryProtectionUnit = extern struct { /// MPU Type Register. - TYPE: mmio.Mmio(packed struct(u32) { + TYPE: mmio.OldMmio(packed struct(u32) { /// Indicates support for unified or separate instructions and data address regions. SEPARATE: u1, reserved0: u7 = 0, @@ -137,7 +137,7 @@ pub const MemoryProtectionUnit = extern struct { reserved1: u8 = 0, }), /// MPU Control Register. - CTRL: mmio.Mmio(packed struct(u32) { + CTRL: mmio.OldMmio(packed struct(u32) { /// Enables the MPU. ENABLE: u1, /// Enables of operation of MPU during HardFault and MNIHandlers. @@ -147,7 +147,7 @@ pub const MemoryProtectionUnit = extern struct { reserved0: u29 = 0, }), /// MPU Region Number Register. - RNR: mmio.Mmio(packed struct(u32) { + RNR: mmio.OldMmio(packed struct(u32) { /// Indicates the memory region accessed by MPU RBAR and PMU RLAR. REGION: u8, reserved0: u24 = 0, @@ -169,7 +169,7 @@ pub const MemoryProtectionUnit = extern struct { /// MPU Alias 3 Region Attribute and Size Register. RASR_A3: RASR_Register, - pub const RBAR_Register = mmio.Mmio(packed struct(u32) { + pub const RBAR_Register = mmio.OldMmio(packed struct(u32) { /// MPU region field. REGION: u4, /// MPU region number valid bit. @@ -178,7 +178,7 @@ pub const MemoryProtectionUnit = extern struct { ADDR: u27, }); - pub const RASR_Register = mmio.Mmio(packed struct(u32) { + pub const RASR_Register = mmio.OldMmio(packed struct(u32) { /// Region enable bit. ENABLE: u1, /// Specifies the size of the MPU protection region. @@ -204,7 +204,7 @@ pub const MemoryProtectionUnit = extern struct { }; pub const FloatingPointUnit = extern struct { - FPCCR: mmio.Mmio(packed struct(u32) { + FPCCR: mmio.OldMmio(packed struct(u32) { LSPACT: u1, USER: u1, reserved0: u1 = 0, diff --git a/core/src/cpus/cortex_m/m55.zig b/core/src/cpus/cortex_m/m55.zig index 467adfc9f..ed8518705 100644 --- a/core/src/cpus/cortex_m/m55.zig +++ b/core/src/cpus/cortex_m/m55.zig @@ -13,7 +13,7 @@ pub const SystemControlBlock = extern struct { /// CPUID Base Register. CPUID: u32, /// Interrupt Control and State Register. - ICSR: mmio.Mmio(packed struct(u32) { + ICSR: mmio.OldMmio(packed struct(u32) { /// Contains the active exception number: /// 0 = Thread mode /// Nonzero = The exception number[a] of the currently active exception. @@ -104,7 +104,7 @@ pub const SystemControlBlock = extern struct { /// Vector Table Offset Register. VTOR: u32, /// Application Interrupt and Reset Control Register. - AIRCR: mmio.Mmio(packed struct { + AIRCR: mmio.OldMmio(packed struct { reserved0: u1 = 0, /// Reserved for debug use. This bit reads as 0. When writing to the register you must /// write 0 to this bit, otherwise behavior is Unpredictable. @@ -157,7 +157,7 @@ pub const SystemControlBlock = extern struct { /// System Control Register. SCR: u32, /// Configuration and Control Register. - CCR: mmio.Mmio(packed struct(u32) { + CCR: mmio.OldMmio(packed struct(u32) { reserved0: u1 = 0, /// User set pending determines if unpriviledged access to the STIR generates a fault. USERSETMPEND: u1, @@ -184,9 +184,9 @@ pub const SystemControlBlock = extern struct { /// System Handler Priority Registers. SHPR: [12]u8, /// System Handler Control and State Register. - SHCSR: mmio.Mmio(shared.scb.SHCSR), + SHCSR: mmio.OldMmio(shared.scb.SHCSR), /// Configurable Fault Status Register. - CFSR: mmio.Mmio(packed struct(u32) { + CFSR: mmio.OldMmio(packed struct(u32) { /// MemManage Fault Register. MMFSR: shared.scb.MMFSR, /// BusFault Status Register. @@ -195,7 +195,7 @@ pub const SystemControlBlock = extern struct { UFSR: shared.scb.UFSR, }), /// HardFault Status Register. - HFSR: mmio.Mmio(shared.scb.HFSR), + HFSR: mmio.OldMmio(shared.scb.HFSR), /// Debug Fault Status Register. DFSR: u32, /// MemManage Fault Address Register. @@ -273,7 +273,7 @@ pub const SecurityAttributionUnit = extern struct { pub const MemoryProtectionUnit = extern struct { /// MPU Type Register. - TYPE: mmio.Mmio(packed struct(u32) { + TYPE: mmio.OldMmio(packed struct(u32) { /// Indicates support for unified or separate instructions and data address regions. SEPARATE: u1, reserved0: u7 = 0, @@ -282,7 +282,7 @@ pub const MemoryProtectionUnit = extern struct { reserved1: u16 = 0, }), /// MPU Control Register. - CTRL: mmio.Mmio(packed struct(u32) { + CTRL: mmio.OldMmio(packed struct(u32) { /// Enables the MPU ENABLE: u1, /// Enables of operation of MPU during HardFault and MNIHandlers. @@ -292,7 +292,7 @@ pub const MemoryProtectionUnit = extern struct { reserved0: u29 = 0, }), /// MPU Region Number Register. - RNR: mmio.Mmio(packed struct(u32) { + RNR: mmio.OldMmio(packed struct(u32) { /// Indicates the memory region accessed by MPU RBAR and PMU RLAR. REGION: u8, reserved0: u24 = 0, @@ -320,7 +320,7 @@ pub const MemoryProtectionUnit = extern struct { MPU_MAIR1: u32, /// MPU Region Address Register format. - pub const RBAR_Register = mmio.Mmio(packed struct(u32) { + pub const RBAR_Register = mmio.OldMmio(packed struct(u32) { /// Execute Never defines if code can be executed from this region. XN: u1, /// Access permissions. @@ -333,7 +333,7 @@ pub const MemoryProtectionUnit = extern struct { }); /// MPU Region Limit Address Register format. - pub const RLAR_Register = mmio.Mmio(packed struct(u32) { + pub const RLAR_Register = mmio.OldMmio(packed struct(u32) { /// Enable the region. EN: u1, /// Attribue Index associates a set of attributes in the MPU MAIR0 and MPU MAIR1 fields. diff --git a/core/src/cpus/cortex_m/m7.zig b/core/src/cpus/cortex_m/m7.zig index 9127054ed..43e44131c 100644 --- a/core/src/cpus/cortex_m/m7.zig +++ b/core/src/cpus/cortex_m/m7.zig @@ -9,7 +9,7 @@ pub const SystemControlBlock = extern struct { /// CPUID Base Register CPUID: u32, /// Interrupt Control and State Register - ICSR: mmio.Mmio(packed struct(u32) { + ICSR: mmio.OldMmio(packed struct(u32) { VECTACTIVE: u9, reserved0: u2 = 0, RETTOBASE: u1, @@ -28,7 +28,7 @@ pub const SystemControlBlock = extern struct { /// Vector Table Offset Register VTOR: u32, /// Application Interrupt and Reset Control Register - AIRCR: mmio.Mmio(packed struct(u32) { + AIRCR: mmio.OldMmio(packed struct(u32) { /// Reserved for Debug use. Must be written as 0. VECTRESET: u1, // WO /// Reserved for Debug use. Must be written as 0. @@ -47,7 +47,7 @@ pub const SystemControlBlock = extern struct { /// System Control Register SCR: u32, /// Configuration Control Register - CCR: mmio.Mmio(packed struct(u32) { + CCR: mmio.OldMmio(packed struct(u32) { NONBASETHRDENA: u1, USERSETMPEND: u1, reserved0: u1 = 0, @@ -67,7 +67,7 @@ pub const SystemControlBlock = extern struct { }), /// System Handler Priority Register 1 (SHPR1) - SHPR1: mmio.Mmio(packed struct(u32) { + SHPR1: mmio.OldMmio(packed struct(u32) { /// Priority of system handler 4, MemManage PRI_4: u8, /// Priority of system handler 5, BusFault @@ -79,7 +79,7 @@ pub const SystemControlBlock = extern struct { }), /// System Handler Priority Register 2 (SHPR2) - SHPR2: mmio.Mmio(packed struct(u32) { + SHPR2: mmio.OldMmio(packed struct(u32) { /// Reserved bits [23:0] reserved0: u24 = 0, /// Priority of system handler 11, SVCall @@ -87,7 +87,7 @@ pub const SystemControlBlock = extern struct { }), /// System Handler Priority Register 3 (SHPR3) - SHPR3: mmio.Mmio(packed struct(u32) { + SHPR3: mmio.OldMmio(packed struct(u32) { /// Reserved bits [15:0] reserved0: u16 = 0, /// Priority of system handler 14, PendSV @@ -97,9 +97,9 @@ pub const SystemControlBlock = extern struct { }), /// System Handler Contol and State Register - SHCSR: mmio.Mmio(shared.scb.SHCSR), + SHCSR: mmio.OldMmio(shared.scb.SHCSR), /// Configurable Fault Status Register - CFSR: mmio.Mmio(packed struct(u32) { + CFSR: mmio.OldMmio(packed struct(u32) { /// MemManage Fault Register. MMFSR: shared.scb.MMFSR, /// BusFault Status Register. @@ -114,7 +114,7 @@ pub const SystemControlBlock = extern struct { /// UsageFault Status Register UFSR: u32, /// HardFault Status Register - HFSR: mmio.Mmio(shared.scb.HFSR), + HFSR: mmio.OldMmio(shared.scb.HFSR), /// MemManage Fault Address Register MMAR: u32, /// BusFault Address Register @@ -170,7 +170,7 @@ pub const NestedVectorInterruptController = extern struct { pub const MemoryProtectionUnit = extern struct { /// MPU Type Register - TYPE: mmio.Mmio(packed struct(u32) { + TYPE: mmio.OldMmio(packed struct(u32) { SEPARATE: u1, reserved0: u7 = 0, DREGION: u8, @@ -178,14 +178,14 @@ pub const MemoryProtectionUnit = extern struct { reserved1: u8 = 0, }), /// MPU Control Register - CTRL: mmio.Mmio(packed struct(u32) { + CTRL: mmio.OldMmio(packed struct(u32) { ENABLE: u1, HFNMIENA: u1, PRIVDEFENA: u1, padding: u29 = 0, }), /// MPU RNRber Register - RNR: mmio.Mmio(packed struct(u32) { + RNR: mmio.OldMmio(packed struct(u32) { REGION: u8, padding: u24 = 0, }), @@ -206,13 +206,13 @@ pub const MemoryProtectionUnit = extern struct { /// MPU Alias 3 Region Attribute and Size Register RASR_A3: RASR_Register, - pub const RBAR_Register = mmio.Mmio(packed struct(u32) { + pub const RBAR_Register = mmio.OldMmio(packed struct(u32) { REGION: u4, VALID: u1, ADDR: u27, }); - pub const RASR_Register = mmio.Mmio(packed struct(u32) { + pub const RASR_Register = mmio.OldMmio(packed struct(u32) { /// Region enable bit ENABLE: u1, /// Region Size @@ -239,7 +239,7 @@ pub const MemoryProtectionUnit = extern struct { pub const DebugRegisters = extern struct { /// Debyg Halting Control and Status Register - DHCSR: mmio.Mmio(packed struct { + DHCSR: mmio.OldMmio(packed struct { reserved0: u6 = 0, S_RESET_ST: u1, S_RETIRE_ST: u1, @@ -258,18 +258,18 @@ pub const DebugRegisters = extern struct { }), /// Debug Core Register Selector Register /// TODO: Reserved have values ? see armv7-m reference manual - DCRSR: mmio.Mmio(packed struct { + DCRSR: mmio.OldMmio(packed struct { reserved0: u15 = 0, REGWnR: u1, reserved1: u9 = 0, REGSEL: u7, }), /// Debug Core Register Data Register - DCRDR: mmio.Mmio(packed struct { + DCRDR: mmio.OldMmio(packed struct { DBGTMP: u32, }), /// Debug exception and Monitor Control Register - DEMCR: mmio.Mmio(packed struct { + DEMCR: mmio.OldMmio(packed struct { reserved0: u7 = 0, TRCENA: u1, reserved1: u4 = 0, @@ -292,7 +292,7 @@ pub const DebugRegisters = extern struct { pub const ITM = extern struct { /// Stimulus Port Registers (0-255) - STIM: [256]mmio.Mmio(packed union { + STIM: [256]mmio.OldMmio(packed union { WRITE_U8: u8, WRITE_U16: u16, WRITE_U32: u32, @@ -305,21 +305,21 @@ pub const ITM = extern struct { reserved0: [640]u32, // Padding to 0xE00 /// Trace Enable Registers (0-7) - TER: [8]mmio.Mmio(packed struct(u32) { + TER: [8]mmio.OldMmio(packed struct(u32) { STIMENA: u32, // Enable bits for stimulus ports }), reserved1: [10]u32, // Padding to 0xE40 /// Trace Privilege Register - TPR: mmio.Mmio(packed struct(u32) { + TPR: mmio.OldMmio(packed struct(u32) { PRIVMASK: u32, // Privilege mask for stimulus ports }), reserved2: [15]u32, // Padding to 0xE80 /// Trace Control Register - TCR: mmio.Mmio(packed struct(u32) { + TCR: mmio.OldMmio(packed struct(u32) { ITMENA: u1, // ITM enable TSENA: u1, // Local timestamp enable SYNCENA: u1, // Sync packet enable @@ -337,28 +337,28 @@ pub const ITM = extern struct { pub const TPIU = extern struct { /// Supported Parallel Port Sizes Register - SSPSR: mmio.Mmio(packed struct(u32) { + SSPSR: mmio.OldMmio(packed struct(u32) { SWIDTH: u32, }), /// Current Parallel Port Size Register - CSPSR: mmio.Mmio(packed struct(u32) { + CSPSR: mmio.OldMmio(packed struct(u32) { CWIDTH: u32, }), reserved0: [2]u32, /// Asynchronous Clock Prescaler Register - ACPR: mmio.Mmio(packed struct(u32) { + ACPR: mmio.OldMmio(packed struct(u32) { SWOSCALER: u16, padding: u16 = 0, }), reserved1: [55]u32, /// Selected Pin Protocol Register - SPPR: mmio.Mmio(packed struct(u32) { + SPPR: mmio.OldMmio(packed struct(u32) { TXMODE: u2, padding: u30 = 0, }), reserved2: [524]u32, /// TPIU Type Register - TYPE: mmio.Mmio(packed struct(u32) { + TYPE: mmio.OldMmio(packed struct(u32) { reserved0: u6 = 0, FIFOSZ: u3, PTINVALID: u1, @@ -371,7 +371,7 @@ pub const TPIU = extern struct { }; pub const FloatingPointUnit = extern struct { - FPCCR: mmio.Mmio(packed struct(u32) { + FPCCR: mmio.OldMmio(packed struct(u32) { LSPACT: u1, USER: u1, S: u1, diff --git a/core/src/mmio.zig b/core/src/mmio.zig index 98c08579a..8ff90808a 100644 --- a/core/src/mmio.zig +++ b/core/src/mmio.zig @@ -1,6 +1,8 @@ const std = @import("std"); const assert = std.debug.assert; +pub const OldMmio = Mmio; + pub fn Mmio(comptime PackedT: type) type { @setEvalBranchQuota(2_000); diff --git a/port/espressif/esp/src/hal/clocks/esp32_c3.zig b/port/espressif/esp/src/hal/clocks/esp32_c3.zig index 5a9eb7ca9..ffe27f9f5 100644 --- a/port/espressif/esp/src/hal/clocks/esp32_c3.zig +++ b/port/espressif/esp/src/hal/clocks/esp32_c3.zig @@ -240,7 +240,7 @@ fn bbpll_configure(pll_freq: CpuClockSource.PllClock.PllFreq) void { } const I2C_ANA_MST_TYPE = extern struct { - ANA_CONF0: microzig.mmio.Mmio(packed struct { + ANA_CONF0: microzig.mmio.OldMmio(packed struct { reserved0: u2, BBPLL_STOP_FORCE_HIGH: u1, BBPLL_STOP_FORCE_LOW: u1, diff --git a/port/raspberrypi/rp2xxx/src/hal/gpio.zig b/port/raspberrypi/rp2xxx/src/hal/gpio.zig index 7126bf601..3f677f866 100644 --- a/port/raspberrypi/rp2xxx/src/hal/gpio.zig +++ b/port/raspberrypi/rp2xxx/src/hal/gpio.zig @@ -15,7 +15,6 @@ const NUM_BANK0_GPIOS = switch (chip) { .RP2350 => 48, }; - pub const Direction = enum(u1) { in, out, diff --git a/port/stmicro/stm32/src/hals/STM32L47X/lcd.zig b/port/stmicro/stm32/src/hals/STM32L47X/lcd.zig index eef142bbc..53f24e1cc 100644 --- a/port/stmicro/stm32/src/hals/STM32L47X/lcd.zig +++ b/port/stmicro/stm32/src/hals/STM32L47X/lcd.zig @@ -2,11 +2,11 @@ const microzig = @import("microzig"); const LCD = microzig.chip.peripherals.LCD; -const LCD_COM_L = microzig.mmio.Mmio(packed struct(u32) { +const LCD_COM_L = microzig.mmio.OldMmio(packed struct(u32) { SEG0T31: u32, }); -const LCD_COM_H = microzig.mmio.Mmio(packed struct(u32) { +const LCD_COM_H = microzig.mmio.OldMmio(packed struct(u32) { SEG32T44: u12, reserved: u20, }); From d644a36c809bf573fb39fedb179d381688758cb3 Mon Sep 17 00:00:00 2001 From: Piotr Fila Date: Sun, 1 Feb 2026 15:04:18 +0100 Subject: [PATCH 04/23] Add access field to Mmio --- core/src/mmio.zig | 30 ++++++++++++++++++++++++++++-- tools/regz/src/gen.zig | 31 +++++++++++++++++++++++++++++-- 2 files changed, 57 insertions(+), 4 deletions(-) diff --git a/core/src/mmio.zig b/core/src/mmio.zig index 8ff90808a..842de6b91 100644 --- a/core/src/mmio.zig +++ b/core/src/mmio.zig @@ -1,9 +1,35 @@ const std = @import("std"); const assert = std.debug.assert; -pub const OldMmio = Mmio; +pub fn OldMmio(comptime PackedT: type) type { + var access: MmioAccess(PackedT) = undefined; + for (@typeInfo(PackedT).@"struct".fields) |fld| + @field(access, fld.name) = .read_write; + return Mmio(PackedT, access); +} + +pub const Access = enum { + read_only, + read_write, +}; + +pub fn MmioAccess(comptime PackedT: type) type { + @setEvalBranchQuota(20_000); + const info = @typeInfo(PackedT).@"struct"; + var field_names: [info.fields.len][:0]const u8 = undefined; + for (&field_names, info.fields) |*dst, src| + dst.* = src.name; + return @import("../src/core/usb.zig").Struct( + .auto, + null, + &field_names, + &@splat(Access), + &@splat(.{}), + ); +} -pub fn Mmio(comptime PackedT: type) type { +pub fn Mmio(comptime PackedT: type, access: MmioAccess(PackedT)) type { + _ = access; @setEvalBranchQuota(2_000); const size = @bitSizeOf(PackedT); diff --git a/tools/regz/src/gen.zig b/tools/regz/src/gen.zig index c5bff78d1..f45661ddc 100644 --- a/tools/regz/src/gen.zig +++ b/tools/regz/src/gen.zig @@ -1147,7 +1147,7 @@ fn write_register( register.size_bits, }); - try write_fields(db, arena, fields, register.size_bits, register_reset, writer); + try write_fields_and_access(db, arena, fields, register.size_bits, register_reset, writer); try writer.writeAll("}),\n"); } else if (array_prefix.len != 0) { try writer.print("{f}: {s}u{},\n", .{ @@ -1193,7 +1193,7 @@ fn get_field_default(field: Database.StructField, maybe_register_reset: ?Registe return (register_reset.value & field_mask) >> @intCast(field.offset_bits); } -fn write_fields( +fn write_fields_and_access( db: *Database, arena: Allocator, fields: []const Database.StructField, @@ -1235,6 +1235,19 @@ fn write_fields( var offset: u64 = 0; + const AccessType = enum { + read_only, + read_write, + }; + + const RegAndAccess = union(enum) { + normal: struct { []const u8, AccessType }, + reserved: u8, + padding, + }; + + var access: std.ArrayList(RegAndAccess) = .empty; + for (expanded_fields.items) |field| { log.debug("next field: offset={} field.offset_bits={}", .{ offset, field.offset_bits }); if (offset > field.offset_bits) { @@ -1260,6 +1273,7 @@ fn write_fields( if (offset < field.offset_bits) { try writer.print("reserved{}: u{} = 0,\n", .{ field.offset_bits, field.offset_bits - offset }); offset = field.offset_bits; + try access.append(arena, .{ .reserved = field.offset_bits }); } assert(offset == field.offset_bits); @@ -1330,6 +1344,8 @@ fn write_fields( log.debug("adding size bits to offset: offset={} field.size_bits={}", .{ offset, field.size_bits }); offset += field.size_bits; + + try access.append(arena, .{ .normal = .{ field.name, .read_write } }); } log.debug("before padding: offset={} register_size_bits={}", .{ offset, register_size_bits }); @@ -1337,10 +1353,19 @@ fn write_fields( if (offset < register_size_bits) { log.debug("writing padding", .{}); try writer.print("padding: u{} = 0,\n", .{register_size_bits - offset}); + try access.append(arena, .padding); } else { log.debug("No padding", .{}); } + try writer.writeAll("}, .{\n"); + + for (access.items) |it| switch (it) { + .normal => |data| try writer.print(".{s} = .{t},\n", data), + .reserved => |num| try writer.print(".reserved{} = .read_only,\n", .{num}), + .padding => try writer.writeAll(".padding = .read_only,\n"), + }; + try out_writer.writeAll(buf.written()); } @@ -1795,6 +1820,8 @@ test "gen.peripheral instantiation" { }, &vfs); } +// TODO: Adapt tests to new Mmio + test "gen.peripherals with a shared type" { var db = try tests.peripherals_with_shared_type(std.testing.allocator); defer db.destroy(); From 56a18626bd00bb537bd6f9a8e407606246f552bd Mon Sep 17 00:00:00 2001 From: Piotr Fila Date: Sun, 1 Feb 2026 16:02:00 +0100 Subject: [PATCH 05/23] remove unused Mmio.toggle --- core/src/mmio.zig | 38 -------------------------------------- 1 file changed, 38 deletions(-) diff --git a/core/src/mmio.zig b/core/src/mmio.zig index 842de6b91..3906c3c38 100644 --- a/core/src/mmio.zig +++ b/core/src/mmio.zig @@ -83,43 +83,5 @@ pub fn Mmio(comptime PackedT: type, access: MmioAccess(PackedT)) type { } write(addr, val); } - - /// In field `field_name` of struct `val`, toggle (only) all bits that are set in `value`. - inline fn toggle_field(val: anytype, comptime field_name: []const u8, value: anytype) void { - const FieldType = @TypeOf(@field(val, field_name)); - switch (@typeInfo(FieldType)) { - .int => { - @field(val, field_name) = @field(val, field_name) ^ value; - }, - .@"enum" => |enum_info| { - // same as for the .Int case, but casting to and from the u... tag type U of the enum FieldType - const U = enum_info.tag_type; - @field(val, field_name) = - @as(FieldType, @enumFromInt(@as(U, @intFromEnum(@field(val, field_name))) ^ - @as(U, @intFromEnum(@as(FieldType, value))))); - }, - else => |T| { - @compileError("unsupported register field type '" ++ @typeName(T) ++ "'"); - }, - } - } - - /// In field `field_name` of this register, toggle (only) all bits that are set in `value`. - /// A one-field version of toggle(), more helpful if `field_name` is comptime calculated. - pub inline fn toggle_one(addr: *volatile Self, comptime field_name: []const u8, value: anytype) void { - var val = read(addr); - toggle_field(&val, field_name, value); - write(addr, val); - } - - /// For each `.Field = value` entry of `fields`: - /// In field `F` of this register, toggle (only) all bits that are set in `value`. - pub inline fn toggle(addr: *volatile Self, fields: anytype) void { - var val = read(addr); - inline for (@typeInfo(@TypeOf(fields)).@"struct".fields) |field| { - toggle_field(&val, field.name, @field(fields, field.name)); - } - write(addr, val); - } }; } From 1cb0480d7245cd96e48f91433355fc2e9efd1ca0 Mon Sep 17 00:00:00 2001 From: Piotr Fila Date: Sun, 1 Feb 2026 17:50:04 +0100 Subject: [PATCH 06/23] Mmio: do more checks on underlying_type --- core/src/mmio.zig | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/core/src/mmio.zig b/core/src/mmio.zig index 3906c3c38..29ae76217 100644 --- a/core/src/mmio.zig +++ b/core/src/mmio.zig @@ -32,17 +32,33 @@ pub fn Mmio(comptime PackedT: type, access: MmioAccess(PackedT)) type { _ = access; @setEvalBranchQuota(2_000); - const size = @bitSizeOf(PackedT); - if ((size % 8) != 0) - @compileError("size must be divisible by 8!"); + const IntT, const reg_fields = switch (@typeInfo(PackedT)) { + .@"struct" => |info| .{ switch (info.layout) { + .@"packed" => info.backing_integer.?, + else => @compileError("Struct must be packed"), + }, info.fields }, + else => @compileError("Unsupported type: " ++ @typeName(PackedT)), + }; + _ = reg_fields; + + if (@bitSizeOf(PackedT) != 8 * @sizeOf(PackedT)) + @compileError("Size in bits must be divisible by 8"); - if (!std.math.isPowerOfTwo(size / 8)) - @compileError("size must encode a power of two number of bytes!"); + if (!std.math.isPowerOfTwo(@sizeOf(PackedT))) + @compileError("Size in bytes must be a power of two"); - const IntT = std.meta.Int(.unsigned, size); + if (@alignOf(PackedT) != @sizeOf(PackedT)) + @compileError("PackedT must be naturally aligned"); - if (@sizeOf(PackedT) != (size / 8)) - @compileError(std.fmt.comptimePrint("IntT and PackedT must have the same size!, they are {} and {} bytes respectively", .{ size / 8, @sizeOf(PackedT) })); + if (@sizeOf(IntT) != @sizeOf(PackedT)) @compileError(std.fmt.comptimePrint( + "IntT and PackedT must have the same size, they are {} and {} bytes respectively", + .{ @sizeOf(IntT), @sizeOf(PackedT) }, + )); + + if (@bitSizeOf(IntT) != @bitSizeOf(PackedT)) @compileError(std.fmt.comptimePrint( + "IntT and PackedT must have the same bitsize, they are {} and {} bits respectively", + .{ @bitSizeOf(IntT), @bitSizeOf(PackedT) }, + )); return extern struct { const Self = @This(); @@ -56,9 +72,6 @@ pub fn Mmio(comptime PackedT: type, access: MmioAccess(PackedT)) type { } pub inline fn write(addr: *volatile Self, val: PackedT) void { - comptime { - assert(@bitSizeOf(PackedT) == @bitSizeOf(IntT)); - } addr.write_raw(@bitCast(val)); } From d1de386e6c9b3006fa72c177479453376266478c Mon Sep 17 00:00:00 2001 From: Piotr Fila Date: Sun, 1 Feb 2026 17:50:44 +0100 Subject: [PATCH 07/23] remove Mmio.write_raw in favor of just accessing the .raw field --- core/src/mmio.zig | 6 +--- .../espressif/esp/src/hal/clocks/esp32_c3.zig | 3 +- port/espressif/esp/src/hal/i2c.zig | 8 +++--- port/espressif/esp/src/hal/spi.zig | 8 +++--- port/espressif/esp/src/hal/system.zig | 16 +++++------ port/nordic/nrf5x/src/hal/clocks.zig | 15 ++++++---- port/nordic/nrf5x/src/hal/spim.zig | 6 ++-- port/nordic/nrf5x/src/hal/time.zig | 10 ++++--- port/nxp/mcx/src/mcxa153/hal/gpio.zig | 10 +++---- port/nxp/mcx/src/mcxa153/hal/syscon.zig | 28 ++++++++----------- .../mcx/src/mcxn947/hal/flexcomm/LP_I2C.zig | 5 ++-- port/nxp/mcx/src/mcxn947/hal/gpio.zig | 8 +++--- port/nxp/mcx/src/mcxn947/hal/pin.zig | 2 +- port/nxp/mcx/src/mcxn947/hal/syscon.zig | 8 +++--- port/raspberrypi/rp2xxx/src/hal/i2c_slave.zig | 4 +-- .../raspberrypi/rp2xxx/src/hal/pio/common.zig | 2 +- port/raspberrypi/rp2xxx/src/hal/resets.zig | 10 +++---- port/raspberrypi/rp2xxx/src/hal/spi.zig | 10 +++---- .../rp2xxx/src/hal/system_timer.zig | 4 +-- port/raspberrypi/rp2xxx/src/hal/uart.zig | 4 +-- port/raspberrypi/rp2xxx/src/hal/usb.zig | 12 ++++---- .../stmicro/stm32/src/hals/common/gpio_v2.zig | 10 +++---- port/wch/ch32v/src/hals/dma.zig | 15 +++++----- port/wch/ch32v/src/hals/i2c.zig | 6 ++-- port/wch/ch32v/src/hals/usart.zig | 4 +-- 25 files changed, 108 insertions(+), 106 deletions(-) diff --git a/core/src/mmio.zig b/core/src/mmio.zig index 29ae76217..1ac355e2f 100644 --- a/core/src/mmio.zig +++ b/core/src/mmio.zig @@ -72,11 +72,7 @@ pub fn Mmio(comptime PackedT: type, access: MmioAccess(PackedT)) type { } pub inline fn write(addr: *volatile Self, val: PackedT) void { - addr.write_raw(@bitCast(val)); - } - - pub inline fn write_raw(addr: *volatile Self, val: IntT) void { - addr.raw = val; + addr.raw = @bitCast(val); } /// Set field `field_name` of this register to `value`. diff --git a/port/espressif/esp/src/hal/clocks/esp32_c3.zig b/port/espressif/esp/src/hal/clocks/esp32_c3.zig index ffe27f9f5..266178d90 100644 --- a/port/espressif/esp/src/hal/clocks/esp32_c3.zig +++ b/port/espressif/esp/src/hal/clocks/esp32_c3.zig @@ -153,9 +153,10 @@ fn switch_to_xtal(div: u10) void { } fn apb_freq_update(freq: u32) void { + // Why not just use the underlying register fields? const value = ((freq >> 12) & @as(u32, std.math.maxInt(u16))) | (((freq >> 12) & @as(u32, std.math.maxInt(u16))) << 16); - RTC_CNTL.STORE5.write_raw(value); + RTC_CNTL.STORE5.raw = value; } fn rom_cpu_frequency_update(freq: u32) void { diff --git a/port/espressif/esp/src/hal/i2c.zig b/port/espressif/esp/src/hal/i2c.zig index 7e565df8a..bf23c2504 100644 --- a/port/espressif/esp/src/hal/i2c.zig +++ b/port/espressif/esp/src/hal/i2c.zig @@ -147,13 +147,13 @@ pub const I2C = struct { self.reset(); // Disable all I2C interrupts - regs.INT_ENA.write_raw(0); + regs.INT_ENA.raw = 0; // Clear all I2C interrupts self.clear_interrupts(); // Configure controller - regs.CTR.write_raw(0); + regs.CTR.raw = 0; regs.CTR.modify(.{ .MS_MODE = 1, // Set I2C controller to master mode .SDA_FORCE_OUT = 1, // Use open drain output for SDA @@ -236,7 +236,7 @@ pub const I2C = struct { fn reset_command_list(self: I2C) void { // Reset all command registers for (0..self.get_regs().COMD.len) |i| - self.get_regs().COMD[@intCast(i)].write_raw(0); + self.get_regs().COMD[@intCast(i)].raw = 0; } /// Set the filter threshold in clock cycles @@ -343,7 +343,7 @@ pub const I2C = struct { /// Clear all interrupts inline fn clear_interrupts(self: I2C) void { - self.get_regs().INT_CLR.write_raw(0x3ffff); + self.get_regs().INT_CLR.raw = 0x3ffff; } inline fn start_transmission(self: I2C) void { diff --git a/port/espressif/esp/src/hal/spi.zig b/port/espressif/esp/src/hal/spi.zig index bda596b81..f5c880176 100644 --- a/port/espressif/esp/src/hal/spi.zig +++ b/port/espressif/esp/src/hal/spi.zig @@ -129,7 +129,7 @@ pub const SPI = enum(u2) { }); // this also enables using all 16 words - regs.USER.write_raw(0); + regs.USER.raw = 0; regs.CLK_GATE.modify(.{ .MST_CLK_ACTIVE = 1, @@ -143,11 +143,11 @@ pub const SPI = enum(u2) { }); // this also disables all cs lines - regs.MISC.write_raw(0); + regs.MISC.raw = 0; - regs.SLAVE.write_raw(0); + regs.SLAVE.raw = 0; - regs.CLOCK.write_raw(config.get_clock_config()); + regs.CLOCK.raw = config.get_clock_config(); regs.DMA_INT_CLR.modify(.{ .TRANS_DONE_INT_CLR = 1, diff --git a/port/espressif/esp/src/hal/system.zig b/port/espressif/esp/src/hal/system.zig index a1643d8ca..831f6fc92 100644 --- a/port/espressif/esp/src/hal/system.zig +++ b/port/espressif/esp/src/hal/system.zig @@ -85,16 +85,16 @@ pub fn init() void { pub fn clocks_enable_set(mask: PeripheralMask) void { var current_mask: u64 = @bitCast(@as(u64, SYSTEM.PERIP_CLK_EN0.raw) | ((@as(u64, SYSTEM.PERIP_CLK_EN1.raw) << 32))); current_mask |= @as(u43, @bitCast(mask)); - SYSTEM.PERIP_CLK_EN0.write_raw(@intCast(current_mask & 0xffff_ffff)); - SYSTEM.PERIP_CLK_EN1.write_raw(@intCast(current_mask >> 32)); + SYSTEM.PERIP_CLK_EN0.raw = @intCast(current_mask & 0xffff_ffff); + SYSTEM.PERIP_CLK_EN1.raw = @intCast(current_mask >> 32); } /// Clears the bits in the mask of the PERIP_CLK_ENx registers. pub fn clocks_enable_clear(mask: PeripheralMask) void { var current_mask: u64 = @bitCast(@as(u64, SYSTEM.PERIP_CLK_EN0.raw) | ((@as(u64, SYSTEM.PERIP_CLK_EN1.raw) << 32))); current_mask &= ~@as(u43, @bitCast(mask)); - SYSTEM.PERIP_CLK_EN0.write_raw(@intCast(current_mask & 0xffff_ffff)); - SYSTEM.PERIP_CLK_EN1.write_raw(@intCast(current_mask >> 32)); + SYSTEM.PERIP_CLK_EN0.raw = @intCast(current_mask & 0xffff_ffff); + SYSTEM.PERIP_CLK_EN1.raw = @intCast(current_mask >> 32); } /// Sets and clears the bits in the mask of the PERIP_RST_ENx registers. Resets the peripherals. @@ -107,16 +107,16 @@ pub fn peripheral_reset(mask: PeripheralMask) void { pub fn peripheral_reset_set(mask: PeripheralMask) void { var current_mask: u64 = @bitCast(@as(u64, SYSTEM.PERIP_RST_EN0.raw) | ((@as(u64, SYSTEM.PERIP_RST_EN1.raw) << 32))); current_mask |= @as(u43, @bitCast(mask)); - SYSTEM.PERIP_RST_EN0.write_raw(@intCast(current_mask & 0xffff_ffff)); - SYSTEM.PERIP_RST_EN1.write_raw(@intCast(current_mask >> 32)); + SYSTEM.PERIP_RST_EN0.raw = @intCast(current_mask & 0xffff_ffff); + SYSTEM.PERIP_RST_EN1.raw = @intCast(current_mask >> 32); } /// Clears the bits in the mask of the PERIP_RST_ENx registers. pub fn peripheral_reset_clear(mask: PeripheralMask) void { var current_mask: u64 = @bitCast(@as(u64, SYSTEM.PERIP_RST_EN0.raw) | ((@as(u64, SYSTEM.PERIP_RST_EN1.raw) << 32))); current_mask &= ~@as(u43, @bitCast(mask)); - SYSTEM.PERIP_RST_EN0.write_raw(@intCast(current_mask & 0xffff_ffff)); - SYSTEM.PERIP_RST_EN1.write_raw(@intCast(current_mask >> 32)); + SYSTEM.PERIP_RST_EN0.raw = @intCast(current_mask & 0xffff_ffff); + SYSTEM.PERIP_RST_EN1.raw = @intCast(current_mask >> 32); } /// Enable clocks and release peripherals from reset. diff --git a/port/nordic/nrf5x/src/hal/clocks.zig b/port/nordic/nrf5x/src/hal/clocks.zig index cbddea02d..d9a45daaa 100644 --- a/port/nordic/nrf5x/src/hal/clocks.zig +++ b/port/nordic/nrf5x/src/hal/clocks.zig @@ -16,25 +16,27 @@ const version: enum { pub const hfxo = struct { pub fn start() void { + // Consider patching the svd switch (version) { .nrf51 => { CLOCK.TASKS_HFCLKSTART = 1; while (CLOCK.EVENTS_HFCLKSTARTED == 0) {} }, .nrf52 => { - CLOCK.TASKS_HFCLKSTART.write_raw(1); + CLOCK.TASKS_HFCLKSTART.raw = 1; while (CLOCK.EVENTS_HFCLKSTARTED.raw == 0) {} }, } } pub fn stop() void { + // Consider patching the svd switch (version) { .nrf51 => { CLOCK.TASKS_HFCLKSTOP = 1; }, .nrf52 => { - CLOCK.TASKS_HFCLKSTOP.write_raw(1); + CLOCK.TASKS_HFCLKSTOP.raw = 1; }, } } @@ -63,13 +65,14 @@ pub const lfclk = struct { }; pub fn calibrate() void { + // Consider patching the svd switch (version) { .nrf51 => { CLOCK.TASKS_CAL = 1; while (CLOCK.EVENTS_DONE == 0) {} }, .nrf52 => { - CLOCK.TASKS_CAL.write_raw(1); + CLOCK.TASKS_CAL.raw(1); while (CLOCK.EVENTS_DONE.raw == 0) {} }, } @@ -131,25 +134,27 @@ pub const lfclk = struct { } pub fn start() void { + // Consider patching the svd switch (version) { .nrf51 => { CLOCK.TASKS_LFCLKSTART = 1; while (CLOCK.EVENTS_LFCLKSTARTED == 0) {} }, .nrf52 => { - CLOCK.TASKS_LFCLKSTART.write_raw(1); + CLOCK.TASKS_LFCLKSTART.raw = 1; while (CLOCK.EVENTS_LFCLKSTARTED.raw == 0) {} }, } } pub fn stop() void { + // Consider patching the svd switch (version) { .nrf51 => { CLOCK.TASKS_LFCLKSTOP = 1; }, .nrf52 => { - CLOCK.TASKS_LFCLKSTOP.write_raw(1); + CLOCK.TASKS_LFCLKSTOP.raw = 1; }, } } diff --git a/port/nordic/nrf5x/src/hal/spim.zig b/port/nordic/nrf5x/src/hal/spim.zig index f410a26bc..aa21e7b23 100644 --- a/port/nordic/nrf5x/src/hal/spim.zig +++ b/port/nordic/nrf5x/src/hal/spim.zig @@ -174,7 +174,7 @@ pub const SPIM = enum(u1) { } regs.ENABLE.write(.{ .ENABLE = .Enabled }); - regs.INTENCLR.write_raw(0xFFFFFFFF); + regs.INTENCLR.raw = 0xFFFF_FFFF; } pub fn write_blocking(spi: SPIM, data: []const u8, timeout: ?Duration) TransactionError!void { @@ -240,10 +240,10 @@ pub const SPIM = enum(u1) { fn prepare_dma_transfer(spi: SPIM, tx: []const u8, rx: []u8) TransactionError!void { const regs = spi.get_regs(); - regs.RXD.PTR.write_raw(@intFromPtr(rx.ptr)); + regs.RXD.PTR.raw = @intFromPtr(rx.ptr); regs.RXD.MAXCNT.write(.{ .MAXCNT = @truncate(rx.len) }); - regs.TXD.PTR.write_raw(@intFromPtr(tx.ptr)); + regs.TXD.PTR.raw = @intFromPtr(tx.ptr); regs.TXD.MAXCNT.write(.{ .MAXCNT = @truncate(tx.len) }); regs.EVENTS_END.write(.{ .EVENTS_END = .NotGenerated }); diff --git a/port/nordic/nrf5x/src/hal/time.zig b/port/nordic/nrf5x/src/hal/time.zig index f1f8ea0c0..7aec18246 100644 --- a/port/nordic/nrf5x/src/hal/time.zig +++ b/port/nordic/nrf5x/src/hal/time.zig @@ -51,14 +51,15 @@ pub fn init() void { rtc.CC[COMPARE_INDEX].write(.{ .COMPARE = 0x800000 }); // Clear counter, then start timer + // Consider patching the svd switch (version) { .nrf51 => { rtc.TASKS_CLEAR = 1; rtc.TASKS_START = 1; }, .nrf52 => { - rtc.TASKS_CLEAR.write_raw(1); - rtc.TASKS_START.write_raw(1); + rtc.TASKS_CLEAR.raw = 1; + rtc.TASKS_START.raw = 1; }, } @@ -70,6 +71,7 @@ pub fn init() void { /// the elapsed time. pub fn rtc_interrupt() callconv(.c) void { switch (version) { + // Consider patching the svd .nrf51 => { if (rtc.EVENTS_OVRFLW == 1) { rtc.EVENTS_OVRFLW = 0; @@ -83,12 +85,12 @@ pub fn rtc_interrupt() callconv(.c) void { }, .nrf52 => { if (rtc.EVENTS_OVRFLW.raw == 1) { - rtc.EVENTS_OVRFLW.write_raw(0); + rtc.EVENTS_OVRFLW.raw = 0; next_period(); } if (rtc.EVENTS_COMPARE[COMPARE_INDEX].raw == 1) { - rtc.EVENTS_COMPARE[COMPARE_INDEX].write_raw(0); + rtc.EVENTS_COMPARE[COMPARE_INDEX].raw = 0; next_period(); } }, diff --git a/port/nxp/mcx/src/mcxa153/hal/gpio.zig b/port/nxp/mcx/src/mcxa153/hal/gpio.zig index 8e50b97b6..600ef8022 100644 --- a/port/nxp/mcx/src/mcxa153/hal/gpio.zig +++ b/port/nxp/mcx/src/mcxa153/hal/gpio.zig @@ -27,7 +27,7 @@ pub const GPIO = enum(u7) { const old: u32 = regs.PDOR.raw; const new = @as(u32, output) << gpio.get_pin(); - regs.PDOR.write_raw(old & ~gpio.get_mask() | new); + regs.PDOR.raw = old & ~gpio.get_mask() | new; } pub fn get(gpio: GPIO) bool { @@ -40,7 +40,7 @@ pub const GPIO = enum(u7) { const regs = gpio.get_regs(); const old: u32 = regs.PTOR.raw; - regs.PTOR.write_raw(old | gpio.get_mask()); + regs.PTOR.raw = old | gpio.get_mask(); } pub fn set_direction(gpio: GPIO, direction: Direction) void { @@ -48,7 +48,7 @@ pub const GPIO = enum(u7) { const old: u32 = regs.PDDR.raw; const new = @as(u32, @intFromEnum(direction)) << gpio.get_pin(); - regs.PDDR.write_raw(old & ~gpio.get_mask() | new); + regs.PDDR.raw = old & ~gpio.get_mask() | new; } pub fn set_interrupt_config(gpio: GPIO, trigger: InterruptConfig) void { @@ -56,7 +56,7 @@ pub const GPIO = enum(u7) { const irqc = @as(u32, @intFromEnum(trigger)) << 16; const isf = @as(u32, 1) << 24; - regs.ICR[gpio.get_pin()].write_raw(irqc | isf); + regs.ICR[gpio.get_pin()].raw = irqc | isf; } pub fn get_interrupt_flag(gpio: GPIO) bool { @@ -69,7 +69,7 @@ pub const GPIO = enum(u7) { const regs = gpio.get_regs(); const old: u32 = regs.ISFR0.raw; - regs.ISFR0.write_raw(old | gpio.get_mask()); + regs.ISFR0.raw = old | gpio.get_mask(); } fn get_regs(gpio: GPIO) *volatile chip.types.peripherals.GPIO0 { diff --git a/port/nxp/mcx/src/mcxa153/hal/syscon.zig b/port/nxp/mcx/src/mcxa153/hal/syscon.zig index 82f56c532..e4a168694 100644 --- a/port/nxp/mcx/src/mcxa153/hal/syscon.zig +++ b/port/nxp/mcx/src/mcxa153/hal/syscon.zig @@ -15,12 +15,11 @@ pub fn enable_clock(comptime peripheral: Peripheral) void { defer freeze_clock_configuration(); switch (peripheral.cc()) { - 0 => chip.peripherals.MRCC0.MRCC_GLB_CC0_SET.write_raw( + 0 => chip.peripherals.MRCC0.MRCC_GLB_CC0_SET.raw = @as(u32, @bitCast(chip.peripherals.MRCC0.MRCC_GLB_CC0_SET)) | peripheral.mask(), - ), - 1 => chip.peripherals.MRCC0.MRCC_GLB_CC1_SET.write_raw( + + 1 => chip.peripherals.MRCC0.MRCC_GLB_CC1_SET.raw = @as(u32, @bitCast(chip.peripherals.MRCC0.MRCC_GLB_CC1_SET)) | peripheral.mask(), - ), } } @@ -29,12 +28,11 @@ pub fn disable_clock(comptime peripheral: Peripheral) void { defer freeze_clock_configuration(); switch (peripheral.cc()) { - 0 => chip.peripherals.MRCC0.MRCC_GLB_CC0_CLR.write_raw( + 0 => chip.peripherals.MRCC0.MRCC_GLB_CC0_CLR.raw = @as(u32, @bitCast(chip.peripherals.MRCC0.MRCC_GLB_CC0_CLR)) | peripheral.mask(), - ), - 1 => chip.peripherals.MRCC0.MRCC_GLB_CC1_CLR.write_raw( + + 1 => chip.peripherals.MRCC0.MRCC_GLB_CC1_CLR.raw = @as(u32, @bitCast(chip.peripherals.MRCC0.MRCC_GLB_CC1_CLR)) | peripheral.mask(), - ), } } @@ -43,12 +41,11 @@ pub fn reset_release(comptime peripheral: Peripheral) void { defer freeze_clock_configuration(); switch (peripheral.cc()) { - 0 => chip.peripherals.MRCC0.MRCC_GLB_RST0_SET.write_raw( + 0 => chip.peripherals.MRCC0.MRCC_GLB_RST0_SET.raw = @as(u32, @bitCast(chip.peripherals.MRCC0.MRCC_GLB_RST0_SET)) | peripheral.mask(), - ), - 1 => chip.peripherals.MRCC0.MRCC_GLB_RST1_SET.write_raw( + + 1 => chip.peripherals.MRCC0.MRCC_GLB_RST1_SET.raw = @as(u32, @bitCast(chip.peripherals.MRCC0.MRCC_GLB_RST1_SET)) | peripheral.mask(), - ), } } @@ -57,12 +54,11 @@ pub fn reset_assert(comptime peripheral: Peripheral) void { defer freeze_clock_configuration(); switch (peripheral.cc()) { - 0 => chip.peripherals.MRCC0.MRCC_GLB_RST0_CLR.write_raw( + 0 => chip.peripherals.MRCC0.MRCC_GLB_RST0_CLR.raw = @as(u32, @bitCast(chip.peripherals.MRCC0.MRCC_GLB_RST0_CLR)) | peripheral.mask(), - ), - 1 => chip.peripherals.MRCC0.MRCC_GLB_RST1_CLR.write_raw( + + 1 => chip.peripherals.MRCC0.MRCC_GLB_RST1_CLR.raw = @as(u32, @bitCast(chip.peripherals.MRCC0.MRCC_GLB_RST1_CLR)) | peripheral.mask(), - ), } } diff --git a/port/nxp/mcx/src/mcxn947/hal/flexcomm/LP_I2C.zig b/port/nxp/mcx/src/mcxn947/hal/flexcomm/LP_I2C.zig index 9c74160d3..acae9659a 100644 --- a/port/nxp/mcx/src/mcxn947/hal/flexcomm/LP_I2C.zig +++ b/port/nxp/mcx/src/mcxn947/hal/flexcomm/LP_I2C.zig @@ -147,7 +147,7 @@ pub const LP_I2C = enum(u4) { } pub fn clear_flags(i2c: LP_I2C) void { - i2c.get_regs().MSR.write_raw(0); + i2c.get_regs().MSR.raw = 0; } fn reset_fifos(i2c: LP_I2C) void { @@ -369,7 +369,8 @@ pub const LP_I2C = enum(u4) { flags = i2c.get_regs().MSR.read(); try i2c.check_flags(); } - i2c.get_regs().MSR.write_raw(1 << 9); + // Why nut use the underlying struct? + i2c.get_regs().MSR.raw = 1 << 9; } fn wait_for_tx_space(i2c: LP_I2C) Error!void { diff --git a/port/nxp/mcx/src/mcxn947/hal/gpio.zig b/port/nxp/mcx/src/mcxn947/hal/gpio.zig index dce1d4e95..58a9986c0 100644 --- a/port/nxp/mcx/src/mcxn947/hal/gpio.zig +++ b/port/nxp/mcx/src/mcxn947/hal/gpio.zig @@ -33,9 +33,9 @@ pub const GPIO = enum(u8) { const new: u32 = @as(u32, 1) << gpio.get_pin(); if (output == 1) - regs.PSOR.write_raw(new) + regs.PSOR.raw = new else - regs.PCOR.write_raw(new); + regs.PCOR.raw = new; } /// Returns the logical input of the GPIO. @@ -49,7 +49,7 @@ pub const GPIO = enum(u8) { pub fn toggle(gpio: GPIO) void { const regs = gpio.get_regs(); - regs.PTOR.write_raw(gpio.get_mask()); + regs.PTOR.raw = gpio.get_mask(); } pub fn set_direction(gpio: GPIO, direction: Direction) void { @@ -57,7 +57,7 @@ pub const GPIO = enum(u8) { const old: u32 = regs.PDDR.raw; const new = @as(u32, @intFromEnum(direction)) << gpio.get_pin(); - regs.PDDR.write_raw((old & ~gpio.get_mask()) | new); + regs.PDDR.raw = (old & ~gpio.get_mask()) | new; } /// Returns the gpio's control register diff --git a/port/nxp/mcx/src/mcxn947/hal/pin.zig b/port/nxp/mcx/src/mcxn947/hal/pin.zig index 93c141e90..abd3da96d 100644 --- a/port/nxp/mcx/src/mcxn947/hal/pin.zig +++ b/port/nxp/mcx/src/mcxn947/hal/pin.zig @@ -36,7 +36,7 @@ pub const Pin = enum(u8) { const base = @intFromPtr(&pin.get_port().get_regs().PCR0); const reg: PinTy = @ptrFromInt(base + pin.get_n() * @as(u32, 4)); - reg.write_raw(@as(u16, @bitCast(config))); + reg.raw = @as(u16, @bitCast(config)); } /// Returns the pin configurator (essentially a builder). diff --git a/port/nxp/mcx/src/mcxn947/hal/syscon.zig b/port/nxp/mcx/src/mcxn947/hal/syscon.zig index 22f4afaf8..06891ab42 100644 --- a/port/nxp/mcx/src/mcxn947/hal/syscon.zig +++ b/port/nxp/mcx/src/mcxn947/hal/syscon.zig @@ -18,7 +18,7 @@ pub fn module_enable_clock(module: Module) void { if (!module.can_control_clock()) return; const reg = &chip.peripherals.SYSCON0.AHBCLKCTRLSET[module.cc()]; - reg.write_raw(@as(u32, 1) << module.offset()); + reg.raw = @as(u32, 1) << module.offset(); } /// Disables the module's clock. @@ -27,7 +27,7 @@ pub fn module_disable_clock(module: Module) void { if (!module.can_control_clock()) return; const reg = &chip.peripherals.SYSCON0.AHBCLKCTRLCLR[module.cc()]; - reg.write_raw(@as(u32, 1) << module.offset()); + reg.raw = @as(u32, 1) << module.offset(); } // same as for `module_enable_clock` @@ -38,7 +38,7 @@ pub fn module_reset_assert(module: Module) void { if (!module.can_reset()) return; const reg = &chip.peripherals.SYSCON0.PRESETCTRLSET[module.cc()]; - reg.write_raw(@as(u32, 1) << module.offset()); + reg.raw = @as(u32, 1) << module.offset(); } /// Release the module's reset. @@ -47,7 +47,7 @@ pub fn module_reset_release(module: Module) void { if (!module.can_reset()) return; const reg = &chip.peripherals.SYSCON0.PRESETCTRLCLR[module.cc()]; - reg.write_raw(@as(u32, 1) << module.offset()); + reg.raw = @as(u32, 1) << module.offset(); } // This enum can be automatically generated using `generate.zig`, diff --git a/port/raspberrypi/rp2xxx/src/hal/i2c_slave.zig b/port/raspberrypi/rp2xxx/src/hal/i2c_slave.zig index a3fdd9145..a57ce9561 100644 --- a/port/raspberrypi/rp2xxx/src/hal/i2c_slave.zig +++ b/port/raspberrypi/rp2xxx/src/hal/i2c_slave.zig @@ -295,13 +295,13 @@ fn isr_common(self: *Self) void { // If we have no more data, fill the TX FIFO with zeros while (self.regs.IC_STATUS.read().TFNF == .NOT_FULL) { - self.regs.IC_DATA_CMD.write_raw(0); + self.regs.IC_DATA_CMD.raw = 0; } } else { // Fill the TX FIFO with data from the transfer buffer while (self.transfer_index < self.transfer_length and self.regs.IC_STATUS.read().TFNF == .NOT_FULL) { - self.regs.IC_DATA_CMD.write_raw(@intCast(self.transfer_buffer[self.transfer_index])); + self.regs.IC_DATA_CMD.raw = @intCast(self.transfer_buffer[self.transfer_index]); self.transfer_index += 1; } } diff --git a/port/raspberrypi/rp2xxx/src/hal/pio/common.zig b/port/raspberrypi/rp2xxx/src/hal/pio/common.zig index 8a2a58d9a..4d6d06ac1 100644 --- a/port/raspberrypi/rp2xxx/src/hal/pio/common.zig +++ b/port/raspberrypi/rp2xxx/src/hal/pio/common.zig @@ -217,7 +217,7 @@ pub fn PioImpl(EnumType: type, chip: Chip) type { const mask = @as(u32, 1) << index; var val = self.get_regs().INPUT_SYNC_BYPASS.raw; val |= mask; - self.get_regs().INPUT_SYNC_BYPASS.write_raw(val); + self.get_regs().INPUT_SYNC_BYPASS.raw = val; } pub fn get_sm_regs(self: EnumType, sm: StateMachine) *volatile StateMachine.Regs { diff --git a/port/raspberrypi/rp2xxx/src/hal/resets.zig b/port/raspberrypi/rp2xxx/src/hal/resets.zig index 6aafe637f..59c6fd92d 100644 --- a/port/raspberrypi/rp2xxx/src/hal/resets.zig +++ b/port/raspberrypi/rp2xxx/src/hal/resets.zig @@ -85,24 +85,24 @@ pub const Mask = pub fn reset(mask: Mask) void { const raw_mask: u32 = @bitCast(mask); - RESETS.RESET.write_raw(raw_mask); - RESETS.RESET.write_raw(0); + RESETS.RESET.raw = raw_mask; + RESETS.RESET.raw = 0; wait_for_reset_done(mask); } pub inline fn reset_block(mask: Mask) void { - hw.set_alias(RESETS).RESET.write_raw(@bitCast(mask)); + hw.set_alias(RESETS).RESET.raw = @bitCast(mask); } pub inline fn unreset_block(mask: Mask) void { - hw.clear_alias(RESETS).RESET.write_raw(@bitCast(mask)); + hw.clear_alias(RESETS).RESET.raw = @bitCast(mask); } pub fn unreset_block_wait(mask: Mask) void { const raw_mask: u32 = @bitCast(mask); - hw.clear_alias(RESETS).RESET.write_raw(raw_mask); + hw.clear_alias(RESETS).RESET.raw = raw_mask; wait_for_reset_done(mask); } diff --git a/port/raspberrypi/rp2xxx/src/hal/spi.zig b/port/raspberrypi/rp2xxx/src/hal/spi.zig index 7c0c7ea5f..c6bdd10bb 100644 --- a/port/raspberrypi/rp2xxx/src/hal/spi.zig +++ b/port/raspberrypi/rp2xxx/src/hal/spi.zig @@ -213,7 +213,7 @@ pub const SPI = enum(u1) { var count: usize = 0; while (spi.is_writable()) { const element = src_iter.next_element() orelse break; - spi_regs.SSPDR.write_raw(element.value); + spi_regs.SSPDR.raw = element.value; count += 1; } spi_regs.SSPCR1.modify(.{ @@ -230,7 +230,7 @@ pub const SPI = enum(u1) { }); var tx_remaining = repeat_count; while (tx_remaining > 0 and spi.is_writable()) { - spi_regs.SSPDR.write_raw(repeated_byte); + spi_regs.SSPDR.raw = repeated_byte; tx_remaining -= 1; } spi_regs.SSPCR1.modify(.{ @@ -279,7 +279,7 @@ pub const SPI = enum(u1) { while (rx_remaining > 0 or tx_remaining > 0) { if (tx_remaining > 0 and spi.is_writable() and rx_remaining < tx_remaining + fifo_depth) { const element = src_iter.next_element() orelse unreachable; - spi_regs.SSPDR.write_raw(element.value); + spi_regs.SSPDR.raw = element.value; tx_remaining -= 1; } if (rx_remaining > 0 and spi.is_readable()) { @@ -330,7 +330,7 @@ pub const SPI = enum(u1) { while (!spi.is_writable()) { hw.tight_loop_contents(); } - spi_regs.SSPDR.write_raw(element.value); + spi_regs.SSPDR.raw = element.value; } // Drain RX FIFO, then wait for shifting to finish (which may be *after* @@ -387,7 +387,7 @@ pub const SPI = enum(u1) { tx_remaining -= spi.prime_tx_fifo_repeated(PacketType, repeated_tx_data, tx_remaining); while (rx_remaining > 0 or tx_remaining > 0) { if (tx_remaining > 0 and spi.is_writable() and rx_remaining < tx_remaining + fifo_depth) { - spi_regs.SSPDR.write_raw(repeated_tx_data); + spi_regs.SSPDR.raw = repeated_tx_data; tx_remaining -= 1; } if (rx_remaining > 0 and spi.is_readable()) { diff --git a/port/raspberrypi/rp2xxx/src/hal/system_timer.zig b/port/raspberrypi/rp2xxx/src/hal/system_timer.zig index 9b9a2a955..c58ba8055 100644 --- a/port/raspberrypi/rp2xxx/src/hal/system_timer.zig +++ b/port/raspberrypi/rp2xxx/src/hal/system_timer.zig @@ -43,13 +43,13 @@ pub const Timer = enum(u1) { /// Enables or disables the interrupt for the given alarm. pub fn set_interrupt_enabled(timer: Timer, alarm: Alarm, enable: bool) void { const regs = timer.get_regs(); - regs.INTE.write_raw(@as(u4, @intFromBool(enable)) << @intFromEnum(alarm)); + regs.INTE.raw = @as(u4, @intFromBool(enable)) << @intFromEnum(alarm); } /// Clears the interrupt flag for the given alarm. pub fn clear_interrupt(timer: Timer, alarm: Alarm) void { const regs = timer.get_regs(); - regs.INTR.write_raw(@as(u4, 1) << @intFromEnum(alarm)); + regs.INTR.raw = @as(u4, 1) << @intFromEnum(alarm); } /// Schedules an alarm to be triggered when the low word of the timer diff --git a/port/raspberrypi/rp2xxx/src/hal/uart.zig b/port/raspberrypi/rp2xxx/src/hal/uart.zig index fcb96054e..2d25fe90e 100644 --- a/port/raspberrypi/rp2xxx/src/hal/uart.zig +++ b/port/raspberrypi/rp2xxx/src/hal/uart.zig @@ -213,7 +213,7 @@ pub const UART = enum(u1) { }); var tx_remaining = src.len; while (tx_remaining > 0 and uart.is_writeable()) { - uart_regs.UARTDR.write_raw(src[src.len - tx_remaining]); + uart_regs.UARTDR.raw = src[src.len - tx_remaining]; tx_remaining -= 1; } uart_regs.UARTCR.modify(.{ @@ -306,7 +306,7 @@ pub const UART = enum(u1) { while (!uart.is_writeable()) { try deadline.check(time.get_time_since_boot()); } - uart_regs.UARTDR.write_raw(payload[offset]); + uart_regs.UARTDR.raw = payload[offset]; offset += 1; } } diff --git a/port/raspberrypi/rp2xxx/src/hal/usb.zig b/port/raspberrypi/rp2xxx/src/hal/usb.zig index a8fc03c61..7de5411b5 100644 --- a/port/raspberrypi/rp2xxx/src/hal/usb.zig +++ b/port/raspberrypi/rp2xxx/src/hal/usb.zig @@ -174,17 +174,17 @@ pub fn Polled(config: Config) type { // Clear the control portion of DPRAM. This may not be necessary -- the // datasheet is ambiguous -- but the C examples do it, and so do we. - peripherals.USB_DPRAM.SETUP_PACKET_LOW.write_raw(0); - peripherals.USB_DPRAM.SETUP_PACKET_HIGH.write_raw(0); + peripherals.USB_DPRAM.SETUP_PACKET_LOW.raw = 0; + peripherals.USB_DPRAM.SETUP_PACKET_HIGH.raw = 0; for (1..config.max_endpoints_count) |i| { - endpoint_control[i - 1].in.write_raw(0); - endpoint_control[i - 1].out.write_raw(0); + endpoint_control[i - 1].in.raw = 0; + endpoint_control[i - 1].out.raw = 0; } for (0..config.max_endpoints_count) |i| { - buffer_control[i].in.write_raw(0); - buffer_control[i].out.write_raw(0); + buffer_control[i].in.raw = 0; + buffer_control[i].out.raw = 0; } // Mux the controller to the onboard USB PHY. I was surprised that there are diff --git a/port/stmicro/stm32/src/hals/common/gpio_v2.zig b/port/stmicro/stm32/src/hals/common/gpio_v2.zig index fba4909f6..dae67f03e 100644 --- a/port/stmicro/stm32/src/hals/common/gpio_v2.zig +++ b/port/stmicro/stm32/src/hals/common/gpio_v2.zig @@ -136,7 +136,7 @@ pub const Pin = enum(usize) { const pin: u5 = @intCast(@intFromEnum(gpio) % 16); const modMask: u32 = gpio.mask_2bit(); - port.PUPDR.write_raw((port.PUPDR.raw & ~modMask) | @as(u32, @intFromEnum(bias)) << (pin << 1)); + port.PUPDR.raw = (port.PUPDR.raw & ~modMask) | @as(u32, @intFromEnum(bias)) << (pin << 1); } pub inline fn set_speed(gpio: Pin, speed: OSPEEDR) void { @@ -144,7 +144,7 @@ pub const Pin = enum(usize) { const pin: u5 = @intCast(@intFromEnum(gpio) % 16); const modMask: u32 = gpio.mask_2bit(); - port.OSPEEDR.write_raw((port.OSPEEDR.raw & ~modMask) | @as(u32, @intFromEnum(speed)) << (pin << 1)); + port.OSPEEDR.raw = (port.OSPEEDR.raw & ~modMask) | @as(u32, @intFromEnum(speed)) << (pin << 1); } pub inline fn set_moder(gpio: Pin, moder: MODER) void { @@ -152,14 +152,14 @@ pub const Pin = enum(usize) { const pin: u5 = @intCast(@intFromEnum(gpio) % 16); const modMask: u32 = gpio.mask_2bit(); - port.MODER.write_raw((port.MODER.raw & ~modMask) | @as(u32, @intFromEnum(moder)) << (pin << 1)); + port.MODER.raw = (port.MODER.raw & ~modMask) | @as(u32, @intFromEnum(moder)) << (pin << 1); } pub inline fn set_output_type(gpio: Pin, otype: OT) void { const port = gpio.get_port(); const pin: u5 = @intCast(@intFromEnum(gpio) % 16); - port.OTYPER.write_raw((port.OTYPER.raw & ~gpio.mask()) | @as(u32, @intFromEnum(otype)) << pin); + port.OTYPER.raw = (port.OTYPER.raw & ~gpio.mask()) | @as(u32, @intFromEnum(otype)) << pin; } pub inline fn set_alternate_function(gpio: Pin, afr: AF) void { @@ -167,7 +167,7 @@ pub const Pin = enum(usize) { const pin: u5 = @intCast(@intFromEnum(gpio) % 16); const afrMask: u32 = @as(u32, 0b1111) << ((pin % 8) << 2); const register = if (pin > 7) &port.AFR[1] else &port.AFR[0]; - register.write_raw((register.raw & ~afrMask) | @as(u32, @intFromEnum(afr)) << ((pin % 8) << 2)); + register.raw = (register.raw & ~afrMask) | @as(u32, @intFromEnum(afr)) << ((pin % 8) << 2); } pub fn from_port(port: Port, pin: u4) Pin { diff --git a/port/wch/ch32v/src/hals/dma.zig b/port/wch/ch32v/src/hals/dma.zig index 32687c725..3b4a05f70 100644 --- a/port/wch/ch32v/src/hals/dma.zig +++ b/port/wch/ch32v/src/hals/dma.zig @@ -227,7 +227,7 @@ pub const Channel = enum(u3) { // Clear all interrupt flags for this channel. There are four interrupts per channel, so we // shift by 4 * (channel - 1). Channel enum is 1-indexed, so subtract 1 for bit position. const flag_shift: u5 = (@as(u5, @intFromEnum(chan)) - 1) * 4; - regs.INTFCR.write_raw(@as(u32, 0b1111) << flag_shift); + regs.INTFCR.raw = @as(u32, 0b1111) << flag_shift; // NOTE: DIR bit affects transfer direction even in MEM2MEM mode (undocumented behavior): // - Always place the peripheral address in PADDR when a peripheral is involved. @@ -239,14 +239,14 @@ pub const Channel = enum(u3) { // Periph→Mem: PADDR=read (periph), MADDR=write (memory), DIR=0 // Mem→Mem: PADDR=read (source), MADDR=write (dest), DIR=0 if (H.is_peripheral(WriteType)) { - regs.MADDR.write_raw(read_addr); - regs.PADDR.write_raw(write_addr); + regs.MADDR.raw = read_addr; + regs.PADDR.raw = write_addr; } else { - regs.MADDR.write_raw(write_addr); - regs.PADDR.write_raw(read_addr); + regs.MADDR.raw = write_addr; + regs.PADDR.raw = read_addr; } // Set the amount of data to transfer - regs.CNTR.write_raw(count); + regs.CNTR.raw = count; // Set the priority regs.CFGR.modify(.{ .PL = @intFromEnum(config.priority) }); // Set the rest of the config @@ -323,7 +323,8 @@ pub const Channel = enum(u3) { pub fn clear_complete_flag(comptime chan: Channel) void { const regs = chan.get_regs(); const flag_shift: u5 = (@as(u5, @intFromEnum(chan)) - 1) * 4; - regs.INTFCR.write_raw(@as(u32, 0b0010) << flag_shift); // Clear only TCIF + // Why not use the underlying struct? + regs.INTFCR.raw = @as(u32, 0b0010) << flag_shift; // Clear only TCIF } pub fn get_remaining_count(comptime chan: Channel) u16 { diff --git a/port/wch/ch32v/src/hals/i2c.zig b/port/wch/ch32v/src/hals/i2c.zig index 62f18a5a5..ef1b57ad4 100644 --- a/port/wch/ch32v/src/hals/i2c.zig +++ b/port/wch/ch32v/src/hals/i2c.zig @@ -178,7 +178,7 @@ pub const I2C = enum(u1) { } // Write clock configuration - regs.CKCFGR.write_raw(ccr); + regs.CKCFGR.raw = ccr; // Enable peripheral first i2c.enable(); @@ -231,7 +231,7 @@ pub const I2C = enum(u1) { /// Send 7-bit address with direction bit inline fn send_address(i2c: I2C, addr: Address, direction: enum { write, read }) void { const addr_byte = @as(u8, @intFromEnum(addr)) << 1 | @intFromBool(direction == .read); - i2c.get_regs().DATAR.write_raw(addr_byte); + i2c.get_regs().DATAR.raw = addr_byte; } /// Common wait for STAR1/STAR2 flag with timeout @@ -330,7 +330,7 @@ pub const I2C = enum(u1) { try i2c.wait_flag_star1("TxE", 1, deadline); // Write data to DATAR - regs.DATAR.write_raw(element.value); + regs.DATAR.raw = element.value; } // Wait for BTF (Byte Transfer Finished) - ensures last byte is transmitted diff --git a/port/wch/ch32v/src/hals/usart.zig b/port/wch/ch32v/src/hals/usart.zig index f71655363..d796815c4 100644 --- a/port/wch/ch32v/src/hals/usart.zig +++ b/port/wch/ch32v/src/hals/usart.zig @@ -242,7 +242,7 @@ pub const USART = enum(u2) { const fraction = ((fraction_part * 16 + 50) / 100) & 0x0F; const brr_value = (mantissa << 4) | fraction; - regs.BRR.write_raw(@intCast(brr_value)); + regs.BRR.raw = @intCast(brr_value); } /// Check if transmit data register is empty (can write) @@ -262,7 +262,7 @@ pub const USART = enum(u2) { /// Write a single byte (non-blocking) pub inline fn write_byte(usart: USART, byte: u8) void { - usart.get_regs().DATAR.write_raw(byte); + usart.get_regs().DATAR.raw = byte; } /// Read a single byte (non-blocking) From c7480a78634d6a0cf31b9401f965655cb0510060 Mon Sep 17 00:00:00 2001 From: Piotr Fila Date: Sun, 1 Feb 2026 18:09:47 +0100 Subject: [PATCH 08/23] rename addr in Mmio to self --- core/src/mmio.zig | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/core/src/mmio.zig b/core/src/mmio.zig index 1ac355e2f..08db6ea40 100644 --- a/core/src/mmio.zig +++ b/core/src/mmio.zig @@ -61,36 +61,38 @@ pub fn Mmio(comptime PackedT: type, access: MmioAccess(PackedT)) type { )); return extern struct { - const Self = @This(); - raw: IntT, pub const underlying_type = PackedT; - pub inline fn read(addr: *volatile Self) PackedT { - return @bitCast(addr.raw); + pub inline fn read(self: *volatile @This()) PackedT { + return @bitCast(self.raw); } - pub inline fn write(addr: *volatile Self, val: PackedT) void { - addr.raw = @bitCast(val); + pub inline fn write(self: *volatile @This(), val: PackedT) void { + self.raw = @bitCast(val); } /// Set field `field_name` of this register to `value`. /// A one-field version of modify(), more helpful if `field_name` is comptime calculated. - pub inline fn modify_one(addr: *volatile Self, comptime field_name: []const u8, value: @FieldType(underlying_type, field_name)) void { - var val = read(addr); + pub inline fn modify_one( + self: *volatile @This(), + comptime field_name: []const u8, + value: @FieldType(underlying_type, field_name), + ) void { + var val = self.read(); @field(val, field_name) = value; - write(addr, val); + self.write(val); } /// For each `.Field = value` entry of `fields`: /// Set field `Field` of this register to `value`. - pub inline fn modify(addr: *volatile Self, fields: anytype) void { - var val = read(addr); + pub inline fn modify(self: *volatile @This(), fields: anytype) void { + var val = self.read(); inline for (@typeInfo(@TypeOf(fields)).@"struct".fields) |field| { @field(val, field.name) = @field(fields, field.name); } - write(addr, val); + self.write(val); } }; } From 774ee655841333e443c027bfbf3eb03051f7ab79 Mon Sep 17 00:00:00 2001 From: Piotr Fila Date: Mon, 2 Feb 2026 01:35:17 +0100 Subject: [PATCH 09/23] initial implementation of register access types --- core/src/mmio.zig | 180 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 170 insertions(+), 10 deletions(-) diff --git a/core/src/mmio.zig b/core/src/mmio.zig index 08db6ea40..8bda69602 100644 --- a/core/src/mmio.zig +++ b/core/src/mmio.zig @@ -8,9 +8,59 @@ pub fn OldMmio(comptime PackedT: type) type { return Mmio(PackedT, access); } -pub const Access = enum { - read_only, - read_write, +/// Access type of a single field. Special and illegal have +/// the same functionality, but display different error messages. +pub const Access = struct { + /// Effect of reading the field + pub const Read = enum { + /// Reading does not affect the field value. + unaffected, + /// Reading changes the field value in an implementation-defined way. + special, + /// This register should never be read from. + illegal, + // There exist more! + }; + + pub const Write = enum { + /// Writing sets the field value to what was written. + normal, + /// Writing has no effect. + ignored, + /// Write 1 to set, 0 to leave unaffected. + set_mask, + /// Write 1 to clear, 0 to leave unaffected. + clear_mask, + /// Write 1 to toggle, 0 to leave unaffected. + toggle_mask, + /// Writing changes the this field in an implementation-defined way. + special, + /// This register should never be written to. + illegal, + // There exist more! + }; + + read: Read, + write: Write, + + pub const read_only: @This() = .{ .read = .unaffected, .write = .ignored }; + pub const read_write: @This() = .{ .read = .unaffected, .write = .normal }; + pub const write_only: @This() = .{ .read = .illegal, .write = .normal }; +}; + +/// If a field is not null, it contains the name of one of the registers +/// that prevent this capability, so that a nice error message can be displayed. +const Capabilities = struct { + /// If null, register can be read from. + read: ?[:0]const u8 = null, + /// If null, register can be written to. + write: ?[:0]const u8 = null, + /// If null, register can have bits set by a write operation. + set_mask: ?[:0]const u8 = null, + /// If null, register can have bits cleared by a write operation. + clear_mask: ?[:0]const u8 = null, + /// If null, register can have bits toggled by a write operation. + toggle_mask: ?[:0]const u8 = null, }; pub fn MmioAccess(comptime PackedT: type) type { @@ -28,8 +78,7 @@ pub fn MmioAccess(comptime PackedT: type) type { ); } -pub fn Mmio(comptime PackedT: type, access: MmioAccess(PackedT)) type { - _ = access; +pub fn Mmio(comptime PackedT: type, access_type: MmioAccess(PackedT)) type { @setEvalBranchQuota(2_000); const IntT, const reg_fields = switch (@typeInfo(PackedT)) { @@ -39,7 +88,6 @@ pub fn Mmio(comptime PackedT: type, access: MmioAccess(PackedT)) type { }, info.fields }, else => @compileError("Unsupported type: " ++ @typeName(PackedT)), }; - _ = reg_fields; if (@bitSizeOf(PackedT) != 8 * @sizeOf(PackedT)) @compileError("Size in bits must be divisible by 8"); @@ -64,12 +112,58 @@ pub fn Mmio(comptime PackedT: type, access: MmioAccess(PackedT)) type { raw: IntT, pub const underlying_type = PackedT; + const capabilities: Capabilities = blk: { + var ret: Capabilities = .{}; + for (reg_fields) |field| { + const a: Access = @field(access_type, field.name); + switch (a.read) { + .unaffected => {}, + .special, .illegal => ret.read = field.name, + } + switch (a.write) { + .normal => { + ret.set_mask = field.name; + ret.clear_mask = field.name; + ret.toggle_mask = field.name; + }, + .ignored => {}, + .set_mask => { + ret.write = field.name; + ret.clear_mask = field.name; + ret.toggle_mask = field.name; + }, + .clear_mask => { + ret.write = field.name; + ret.set_mask = field.name; + ret.toggle_mask = field.name; + }, + .toggle_mask => { + ret.write = field.name; + ret.set_mask = field.name; + ret.clear_mask = field.name; + }, + .special, .illegal => { + ret.write = field.name; + ret.set_mask = field.name; + ret.clear_mask = field.name; + ret.toggle_mask = field.name; + }, + } + break :blk ret; + } + }; pub inline fn read(self: *volatile @This()) PackedT { + if (capabilities.read) |name| + reg_type_op_error(name, "reading from the register"); + return @bitCast(self.raw); } pub inline fn write(self: *volatile @This(), val: PackedT) void { + if (capabilities.write) |name| + reg_type_op_error(name, "writing to the register"); + self.raw = @bitCast(val); } @@ -80,19 +174,85 @@ pub fn Mmio(comptime PackedT: type, access: MmioAccess(PackedT)) type { comptime field_name: []const u8, value: @FieldType(underlying_type, field_name), ) void { - var val = self.read(); + if (capabilities.read orelse capabilities.write) |name| + reg_type_op_error(name, "modifying this register by read-modify-write"); + + if (@field(access_type, field_name).write != .normal) + reg_type_op_error(field_name, "modifying this field by read-modify-write"); + + var val: PackedT = @bitCast(self.raw); @field(val, field_name) = value; - self.write(val); + self.raw = @bitCast(val); } /// For each `.Field = value` entry of `fields`: /// Set field `Field` of this register to `value`. + /// This is implemented using read-modify-write. pub inline fn modify(self: *volatile @This(), fields: anytype) void { - var val = self.read(); + if (capabilities.read orelse capabilities.write) |name| + reg_type_op_error(name, "modifying this register by read-modify-write"); + + self.modify_passed_value_and_write( + @bitCast(@as(IntT, 0)), + fields, + .normal, + "modifying this field by read-modify-write", + ); + } + + pub inline fn set_mask(self: *volatile @This(), fields: anytype) void { + if (capabilities.set_mask) |name| + reg_type_op_error(name, "setting bits in this register by masking"); + + self.modify_passed_value_and_write( + @bitCast(@as(IntT, 0)), + fields, + .set_mask, + "setting bits in this field by masking", + ); + } + + pub inline fn clear_mask(self: *volatile @This(), fields: anytype) void { + if (capabilities.clear_mask) |name| + reg_type_op_error(name, "clearing bits in this register by masking"); + + self.modify_passed_value_and_write( + @bitCast(@as(IntT, 0)), + fields, + .clear_mask, + "clearing bits in this field by masking", + ); + } + + inline fn modify_passed_value_and_write( + self: *volatile @This(), + initial: PackedT, + fields: anytype, + allowed_write_access: Access.Write, + comptime action: [:0]const u8, + ) void { + var val = initial; inline for (@typeInfo(@TypeOf(fields)).@"struct".fields) |field| { + if (@field(access_type, field.name).write != allowed_write_access) + reg_type_op_error(field.name, action); @field(val, field.name) = @field(fields, field.name); } - self.write(val); + self.raw = @bitCast(val); + } + + fn reg_type_op_error(comptime reg_name: [:0]const u8, comptime action: []const u8) noreturn { + const a = @field(access_type, reg_name); + @compileError( + "Register field's \"" ++ reg_name ++ "\" access type is read: " ++ + @tagName(a.read) ++ ", write: " ++ @tagName(a.write) ++ ",\nso " ++ action ++ + \\ is not possible. + \\ + \\If you think the access type is wrong, you can add a svd patch + \\in port/.../.../patches or to your build.zig directly. + \\ + \\-freference-trace may be useful to locate where this was called. + , + ); } }; } From e34ae3a065dc7166809dce9ccb88b784429a9b37 Mon Sep 17 00:00:00 2001 From: Piotr Fila Date: Mon, 2 Feb 2026 01:46:12 +0100 Subject: [PATCH 10/23] mark padding and reserved fields as read only --- core/src/mmio.zig | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/core/src/mmio.zig b/core/src/mmio.zig index 8bda69602..03192f834 100644 --- a/core/src/mmio.zig +++ b/core/src/mmio.zig @@ -2,9 +2,13 @@ const std = @import("std"); const assert = std.debug.assert; pub fn OldMmio(comptime PackedT: type) type { + const startsWith = std.mem.startsWith; + var access: MmioAccess(PackedT) = undefined; - for (@typeInfo(PackedT).@"struct".fields) |fld| - @field(access, fld.name) = .read_write; + for (@typeInfo(PackedT).@"struct".fields) |fld| { + const read_only = startsWith(u8, fld.name, "reserved") or startsWith(u8, fld.name, "padding"); + @field(access, fld.name) = if (read_only) .read_only else .read_write; + } return Mmio(PackedT, access); } From d16c3d34f20696a6bc6e43a79cb7c2177e71e899 Mon Sep 17 00:00:00 2001 From: Piotr Fila Date: Mon, 2 Feb 2026 05:29:35 +0100 Subject: [PATCH 11/23] output correct access type from regz --- core/src/mmio.zig | 23 +++++++++++++++-------- tools/regz/src/gen.zig | 25 +++++++++++++++---------- 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/core/src/mmio.zig b/core/src/mmio.zig index 03192f834..618b2d1d3 100644 --- a/core/src/mmio.zig +++ b/core/src/mmio.zig @@ -17,8 +17,10 @@ pub fn OldMmio(comptime PackedT: type) type { pub const Access = struct { /// Effect of reading the field pub const Read = enum { - /// Reading does not affect the field value. - unaffected, + /// Reading returns the currently stored field value and does not affect it. + normal, + /// Reading is not an error, but the returned value is meaningless. + garbage, /// Reading changes the field value in an implementation-defined way. special, /// This register should never be read from. @@ -47,9 +49,10 @@ pub const Access = struct { read: Read, write: Write, - pub const read_only: @This() = .{ .read = .unaffected, .write = .ignored }; - pub const read_write: @This() = .{ .read = .unaffected, .write = .normal }; - pub const write_only: @This() = .{ .read = .illegal, .write = .normal }; + pub const read_only: @This() = .{ .read = .normal, .write = .ignored }; + pub const read_write: @This() = .{ .read = .normal, .write = .normal }; + pub const write_only: @This() = .{ .read = .garbage, .write = .normal }; + pub const reserved: @This() = .{ .read = .garbage, .write = .ignored }; }; /// If a field is not null, it contains the name of one of the registers @@ -121,7 +124,7 @@ pub fn Mmio(comptime PackedT: type, access_type: MmioAccess(PackedT)) type { for (reg_fields) |field| { const a: Access = @field(access_type, field.name); switch (a.read) { - .unaffected => {}, + .normal, .garbage => {}, .special, .illegal => ret.read = field.name, } switch (a.write) { @@ -178,7 +181,9 @@ pub fn Mmio(comptime PackedT: type, access_type: MmioAccess(PackedT)) type { comptime field_name: []const u8, value: @FieldType(underlying_type, field_name), ) void { - if (capabilities.read orelse capabilities.write) |name| + if (capabilities.read) |name| + reg_type_op_error(name, "modifying this register by read-modify-write"); + if (capabilities.write) |name| reg_type_op_error(name, "modifying this register by read-modify-write"); if (@field(access_type, field_name).write != .normal) @@ -193,7 +198,9 @@ pub fn Mmio(comptime PackedT: type, access_type: MmioAccess(PackedT)) type { /// Set field `Field` of this register to `value`. /// This is implemented using read-modify-write. pub inline fn modify(self: *volatile @This(), fields: anytype) void { - if (capabilities.read orelse capabilities.write) |name| + if (capabilities.read) |name| + reg_type_op_error(name, "modifying this register by read-modify-write"); + if (capabilities.write) |name| reg_type_op_error(name, "modifying this register by read-modify-write"); self.modify_passed_value_and_write( diff --git a/tools/regz/src/gen.zig b/tools/regz/src/gen.zig index f45661ddc..f7452f7fb 100644 --- a/tools/regz/src/gen.zig +++ b/tools/regz/src/gen.zig @@ -1235,13 +1235,11 @@ fn write_fields_and_access( var offset: u64 = 0; - const AccessType = enum { - read_only, - read_write, - }; - const RegAndAccess = union(enum) { - normal: struct { []const u8, AccessType }, + normal: struct { + name: []const u8, + access: ?Database.Access, + }, reserved: u8, padding, }; @@ -1345,7 +1343,7 @@ fn write_fields_and_access( log.debug("adding size bits to offset: offset={} field.size_bits={}", .{ offset, field.size_bits }); offset += field.size_bits; - try access.append(arena, .{ .normal = .{ field.name, .read_write } }); + try access.append(arena, .{ .normal = .{ .name = field.name, .access = field.access } }); } log.debug("before padding: offset={} register_size_bits={}", .{ offset, register_size_bits }); @@ -1361,9 +1359,16 @@ fn write_fields_and_access( try writer.writeAll("}, .{\n"); for (access.items) |it| switch (it) { - .normal => |data| try writer.print(".{s} = .{t},\n", data), - .reserved => |num| try writer.print(".reserved{} = .read_only,\n", .{num}), - .padding => try writer.writeAll(".padding = .read_only,\n"), + .normal => |data| { + const access_str = switch (data.access orelse .read_write) { + .read_write_once, .read_write => "read_write", + .read_only => "read_only", + .write_only, .write_once => "write_only", + }; + try writer.print(".{s} = .{s},\n", .{ data.name, access_str }); + }, + .reserved => |num| try writer.print(".reserved{} = .reserved,\n", .{num}), + .padding => try writer.writeAll(".padding = .reserved,\n"), }; try out_writer.writeAll(buf.written()); From c4a988b67c0f5e2a4244d647679ec8870debcb5b Mon Sep 17 00:00:00 2001 From: Piotr Fila Date: Mon, 2 Feb 2026 05:30:23 +0100 Subject: [PATCH 12/23] redo mmio operations --- core/src/mmio.zig | 212 +++++++++++++++++++++------------------------- 1 file changed, 95 insertions(+), 117 deletions(-) diff --git a/core/src/mmio.zig b/core/src/mmio.zig index 618b2d1d3..21caae89f 100644 --- a/core/src/mmio.zig +++ b/core/src/mmio.zig @@ -55,20 +55,12 @@ pub const Access = struct { pub const reserved: @This() = .{ .read = .garbage, .write = .ignored }; }; -/// If a field is not null, it contains the name of one of the registers -/// that prevent this capability, so that a nice error message can be displayed. -const Capabilities = struct { - /// If null, register can be read from. - read: ?[:0]const u8 = null, - /// If null, register can be written to. - write: ?[:0]const u8 = null, - /// If null, register can have bits set by a write operation. - set_mask: ?[:0]const u8 = null, - /// If null, register can have bits cleared by a write operation. - clear_mask: ?[:0]const u8 = null, - /// If null, register can have bits toggled by a write operation. - toggle_mask: ?[:0]const u8 = null, -}; +fn check_type_has_all_fields(T: type, fields: anytype) void { + inline for (@typeInfo(@TypeOf(fields)).@"struct".fields) |field| { + if (!@hasField(T, field.name)) + @compileError("Field " ++ field.name ++ " not present in " ++ @typeName(T)); + } +} pub fn MmioAccess(comptime PackedT: type) type { @setEvalBranchQuota(20_000); @@ -119,59 +111,24 @@ pub fn Mmio(comptime PackedT: type, access_type: MmioAccess(PackedT)) type { raw: IntT, pub const underlying_type = PackedT; - const capabilities: Capabilities = blk: { - var ret: Capabilities = .{}; - for (reg_fields) |field| { - const a: Access = @field(access_type, field.name); - switch (a.read) { - .normal, .garbage => {}, - .special, .illegal => ret.read = field.name, - } - switch (a.write) { - .normal => { - ret.set_mask = field.name; - ret.clear_mask = field.name; - ret.toggle_mask = field.name; - }, - .ignored => {}, - .set_mask => { - ret.write = field.name; - ret.clear_mask = field.name; - ret.toggle_mask = field.name; - }, - .clear_mask => { - ret.write = field.name; - ret.set_mask = field.name; - ret.toggle_mask = field.name; - }, - .toggle_mask => { - ret.write = field.name; - ret.set_mask = field.name; - ret.clear_mask = field.name; - }, - .special, .illegal => { - ret.write = field.name; - ret.set_mask = field.name; - ret.clear_mask = field.name; - ret.toggle_mask = field.name; - }, - } - break :blk ret; - } - }; + const all_zeros: PackedT = @bitCast(@as(IntT, 0)); pub inline fn read(self: *volatile @This()) PackedT { - if (capabilities.read) |name| - reg_type_op_error(name, "reading from the register"); - + comptime for (reg_fields) |field| + switch (@field(access_type, field.name).read) { + .normal, .garbage, .special => {}, + .illegal => reg_type_op_error(field.name, "reading from any fields of the register"), + }; return @bitCast(self.raw); } - pub inline fn write(self: *volatile @This(), val: PackedT) void { - if (capabilities.write) |name| - reg_type_op_error(name, "writing to the register"); - - self.raw = @bitCast(val); + pub inline fn write(self: *volatile @This(), w: PackedT) void { + comptime for (reg_fields) |field| + switch (@field(access_type, field.name).write) { + .normal, .ignored, .set_mask, .clear_mask, .toggle_mask, .special => {}, + .illegal => reg_type_op_error(field.name, "writing to any fields of the register"), + }; + self.raw = @bitCast(w); } /// Set field `field_name` of this register to `value`. @@ -181,74 +138,95 @@ pub fn Mmio(comptime PackedT: type, access_type: MmioAccess(PackedT)) type { comptime field_name: []const u8, value: @FieldType(underlying_type, field_name), ) void { - if (capabilities.read) |name| - reg_type_op_error(name, "modifying this register by read-modify-write"); - if (capabilities.write) |name| - reg_type_op_error(name, "modifying this register by read-modify-write"); - - if (@field(access_type, field_name).write != .normal) - reg_type_op_error(field_name, "modifying this field by read-modify-write"); - - var val: PackedT = @bitCast(self.raw); - @field(val, field_name) = value; - self.raw = @bitCast(val); + // Replace with @Struct when migrating to zig 0.16 + var fields: @import("core/usb.zig").Struct( + .auto, + null, + &.{field_name}, + &.{@TypeOf(value)}, + &.{.{}}, + ) = undefined; + @field(fields, field_name) = value; + self.modify(fields); } /// For each `.Field = value` entry of `fields`: /// Set field `Field` of this register to `value`. /// This is implemented using read-modify-write. pub inline fn modify(self: *volatile @This(), fields: anytype) void { - if (capabilities.read) |name| - reg_type_op_error(name, "modifying this register by read-modify-write"); - if (capabilities.write) |name| - reg_type_op_error(name, "modifying this register by read-modify-write"); - - self.modify_passed_value_and_write( - @bitCast(@as(IntT, 0)), - fields, - .normal, - "modifying this field by read-modify-write", - ); + check_type_has_all_fields(PackedT, fields); + const Fields = @TypeOf(fields); + + const r = self.read(); + var w: PackedT = undefined; + inline for (reg_fields) |field| { + const access = @field(access_type, field.name); + @field(w, field.name) = if (@hasField(Fields, field.name)) + // Overwrite this field + if (access.write == .normal and (access.read == .normal or access.read == .garbage)) + @field(fields, field.name) + else + reg_type_op_error(field.name, "modifying this field by read-modify-write") + else switch (access.write) { + // Leave field unchanged + .normal => if (access.read == .normal) + @field(r, field.name) + else + // This should actually be: + // reg_type_op_error(field.name, "modifying any field in this register by read-modify-write") + @field(all_zeros, field.name), + // Preserve old functionality + .ignored => @field(r, field.name), + // Write zeros so that nothing happens + .set_mask, .clear_mask, .toggle_mask => @field(all_zeros, field.name), + else => reg_type_op_error(field.name, "modifying any field in this register by read-modify-write"), + }; + } + self.write(w); } pub inline fn set_mask(self: *volatile @This(), fields: anytype) void { - if (capabilities.set_mask) |name| - reg_type_op_error(name, "setting bits in this register by masking"); - - self.modify_passed_value_and_write( - @bitCast(@as(IntT, 0)), - fields, - .set_mask, - "setting bits in this field by masking", - ); + check_type_has_all_fields(PackedT, fields); + const Fields = @TypeOf(fields); + + var w: PackedT = undefined; + inline for (reg_fields) |field| { + const access = @field(access_type, field.name); + @field(w, field.name) = if (@hasField(Fields, field.name)) + // Set bits in this field + if (access.write == .set_mask) + @field(fields, field.name) + else + reg_type_op_error(field.name, "setting bits of this field by masking") + else switch (access.write) { + // Write zeros so that nothing happens + .ignored, .set_mask, .clear_mask, .toggle_mask => @field(all_zeros, field.name), + else => reg_type_op_error(field.name, "setting bits of any field in this register by masking"), + }; + } + self.write(w); } pub inline fn clear_mask(self: *volatile @This(), fields: anytype) void { - if (capabilities.clear_mask) |name| - reg_type_op_error(name, "clearing bits in this register by masking"); - - self.modify_passed_value_and_write( - @bitCast(@as(IntT, 0)), - fields, - .clear_mask, - "clearing bits in this field by masking", - ); - } - - inline fn modify_passed_value_and_write( - self: *volatile @This(), - initial: PackedT, - fields: anytype, - allowed_write_access: Access.Write, - comptime action: [:0]const u8, - ) void { - var val = initial; - inline for (@typeInfo(@TypeOf(fields)).@"struct".fields) |field| { - if (@field(access_type, field.name).write != allowed_write_access) - reg_type_op_error(field.name, action); - @field(val, field.name) = @field(fields, field.name); + check_type_has_all_fields(PackedT, fields); + const Fields = @TypeOf(fields); + + var w: PackedT = undefined; + inline for (reg_fields) |field| { + const access = @field(access_type, field.name); + @field(w, field.name) = if (@hasField(Fields, field.name)) + // Clear bits in this field + if (access.write == .clear_mask) + @field(fields, field.name) + else + reg_type_op_error(field.name, "clearing bits of this field by masking") + else switch (access.write) { + // Write zeros so that nothing happens + .ignored, .set_mask, .clear_mask, .toggle_mask => @field(all_zeros, field.name), + else => reg_type_op_error(field.name, "clearing bits of any field in this register by masking"), + }; } - self.raw = @bitCast(val); + self.write(w); } fn reg_type_op_error(comptime reg_name: [:0]const u8, comptime action: []const u8) noreturn { From 269cf1eb4b28dd3b626a637e4aea7ae3e96f475d Mon Sep 17 00:00:00 2001 From: Piotr Fila Date: Mon, 2 Feb 2026 21:06:30 +0100 Subject: [PATCH 13/23] fix some CI issues --- core/src/mmio.zig | 5 ++++- port/nordic/nrf5x/src/hal/clocks.zig | 2 +- port/wch/ch32v/src/cpus/qingkev2-rv32ec.zig | 1 - port/wch/ch32v/src/cpus/qingkev3-rv32imac.zig | 1 - tools/regz/src/gen.zig | 2 +- 5 files changed, 6 insertions(+), 5 deletions(-) diff --git a/core/src/mmio.zig b/core/src/mmio.zig index 21caae89f..0272f5ce4 100644 --- a/core/src/mmio.zig +++ b/core/src/mmio.zig @@ -135,7 +135,7 @@ pub fn Mmio(comptime PackedT: type, access_type: MmioAccess(PackedT)) type { /// A one-field version of modify(), more helpful if `field_name` is comptime calculated. pub inline fn modify_one( self: *volatile @This(), - comptime field_name: []const u8, + comptime field_name: [:0]const u8, value: @FieldType(underlying_type, field_name), ) void { // Replace with @Struct when migrating to zig 0.16 @@ -154,6 +154,7 @@ pub fn Mmio(comptime PackedT: type, access_type: MmioAccess(PackedT)) type { /// Set field `Field` of this register to `value`. /// This is implemented using read-modify-write. pub inline fn modify(self: *volatile @This(), fields: anytype) void { + @setEvalBranchQuota(10_000); check_type_has_all_fields(PackedT, fields); const Fields = @TypeOf(fields); @@ -186,6 +187,7 @@ pub fn Mmio(comptime PackedT: type, access_type: MmioAccess(PackedT)) type { } pub inline fn set_mask(self: *volatile @This(), fields: anytype) void { + @setEvalBranchQuota(10_000); check_type_has_all_fields(PackedT, fields); const Fields = @TypeOf(fields); @@ -208,6 +210,7 @@ pub fn Mmio(comptime PackedT: type, access_type: MmioAccess(PackedT)) type { } pub inline fn clear_mask(self: *volatile @This(), fields: anytype) void { + @setEvalBranchQuota(10_000); check_type_has_all_fields(PackedT, fields); const Fields = @TypeOf(fields); diff --git a/port/nordic/nrf5x/src/hal/clocks.zig b/port/nordic/nrf5x/src/hal/clocks.zig index d9a45daaa..839802230 100644 --- a/port/nordic/nrf5x/src/hal/clocks.zig +++ b/port/nordic/nrf5x/src/hal/clocks.zig @@ -72,7 +72,7 @@ pub const lfclk = struct { while (CLOCK.EVENTS_DONE == 0) {} }, .nrf52 => { - CLOCK.TASKS_CAL.raw(1); + CLOCK.TASKS_CAL.raw = 1; while (CLOCK.EVENTS_DONE.raw == 0) {} }, } diff --git a/port/wch/ch32v/src/cpus/qingkev2-rv32ec.zig b/port/wch/ch32v/src/cpus/qingkev2-rv32ec.zig index 7ae681cee..227a6dc79 100644 --- a/port/wch/ch32v/src/cpus/qingkev2-rv32ec.zig +++ b/port/wch/ch32v/src/cpus/qingkev2-rv32ec.zig @@ -69,7 +69,6 @@ pub inline fn system_init(comptime chip: anytype) void { // RCC->CFGR0 &= (uint32_t)0xF8FF0000; RCC.CFGR0.modify(.{ .SW = 0, - .SWS = 0, .HPRE = 0, .ADCPRE = 0, .MCO = 0, diff --git a/port/wch/ch32v/src/cpus/qingkev3-rv32imac.zig b/port/wch/ch32v/src/cpus/qingkev3-rv32imac.zig index e839eb843..7d1b45a33 100644 --- a/port/wch/ch32v/src/cpus/qingkev3-rv32imac.zig +++ b/port/wch/ch32v/src/cpus/qingkev3-rv32imac.zig @@ -102,7 +102,6 @@ pub inline fn system_init(comptime chip: anytype) void { // RCC->CFGR0 &= (uint32_t)0xF8FF0000; RCC.CFGR0.modify(.{ .SW = 0, - .SWS = 0, .HPRE = 0, .ADCPRE = 0, .MCO = 0, diff --git a/tools/regz/src/gen.zig b/tools/regz/src/gen.zig index f7452f7fb..ab37404f9 100644 --- a/tools/regz/src/gen.zig +++ b/tools/regz/src/gen.zig @@ -1365,7 +1365,7 @@ fn write_fields_and_access( .read_only => "read_only", .write_only, .write_once => "write_only", }; - try writer.print(".{s} = .{s},\n", .{ data.name, access_str }); + try writer.print(".{f} = .{s},\n", .{ std.zig.fmtId(data.name), access_str }); }, .reserved => |num| try writer.print(".reserved{} = .reserved,\n", .{num}), .padding => try writer.writeAll(".padding = .reserved,\n"), From 91f12ab03bc7bf818e5a342ea7efee3b60b5e57c Mon Sep 17 00:00:00 2001 From: Piotr Fila Date: Tue, 3 Feb 2026 18:43:36 +0100 Subject: [PATCH 14/23] Remove unused Mmio file --- tools/regz/src/mmio.zig | 49 ----------------------------------------- 1 file changed, 49 deletions(-) delete mode 100644 tools/regz/src/mmio.zig diff --git a/tools/regz/src/mmio.zig b/tools/regz/src/mmio.zig deleted file mode 100644 index 22714cfbb..000000000 --- a/tools/regz/src/mmio.zig +++ /dev/null @@ -1,49 +0,0 @@ -const std = @import("std"); - -pub fn Mmio(comptime size: u8, comptime PackedT: type) type { - if ((size % 8) != 0) - @compileError("size must be divisible by 8!"); - - if (!std.math.isPowerOfTwo(size / 8)) - @compileError("size must encode a power of two number of bytes!"); - - const IntT = std.meta.Int(.unsigned, size); - - if (@sizeOf(PackedT) != (size / 8)) - @compileError(std.fmt.comptimePrint("IntT and PackedT must have the same size!, they are {} and {} bytes respectively", .{ size / 8, @sizeOf(PackedT) })); - - return extern struct { - const Self = @This(); - - raw: IntT, - - pub const underlying_type = PackedT; - - pub inline fn read(addr: *volatile Self) PackedT { - return @as(PackedT, @bitCast(addr.raw)); - } - - pub inline fn write(addr: *volatile Self, val: PackedT) void { - // This is a workaround for a compiler bug related to miscompilation - // If the tmp var is not used, result location will fuck things up - const tmp = @as(IntT, @bitCast(val)); - addr.raw = tmp; - } - - pub inline fn modify(addr: *volatile Self, fields: anytype) void { - var val = read(addr); - inline for (@typeInfo(@TypeOf(fields)).@"struct".fields) |field| { - @field(val, field.name) = @field(fields, field.name); - } - write(addr, val); - } - - pub inline fn toggle(addr: *volatile Self, fields: anytype) void { - var val = read(addr); - inline for (@typeInfo(@TypeOf(fields)).@"struct".fields) |field| { - @field(val, field.name) = @field(val, field.name) ^ @field(fields, field.name); - } - write(addr, val); - } - }; -} From 0b77e6e957273a526359a431529dee1dc1dc16b6 Mon Sep 17 00:00:00 2001 From: Piotr Fila Date: Sun, 8 Feb 2026 03:07:32 +0100 Subject: [PATCH 15/23] rename add_enum -> add_type --- port/nordic/nrf5x/patches/nrf51.zon | 12 ++++---- port/nordic/nrf5x/patches/nrf528xx.zon | 12 ++++---- port/raspberrypi/rp2xxx/patches/rp2350.zon | 24 +++++++-------- tools/regz/src/Database.zig | 34 ++++++++++++---------- tools/regz/src/patch.zig | 33 +++++++-------------- 5 files changed, 54 insertions(+), 61 deletions(-) diff --git a/port/nordic/nrf5x/patches/nrf51.zon b/port/nordic/nrf5x/patches/nrf51.zon index 98c6f3cc8..b3f49b978 100644 --- a/port/nordic/nrf5x/patches/nrf51.zon +++ b/port/nordic/nrf5x/patches/nrf51.zon @@ -1,8 +1,8 @@ .{ .{ - .add_enum = .{ + .add_type = .{ .parent = "types.peripherals.GPIO", - .@"enum" = .{ + .type = .{ .@"enum" = .{ .name = "Pull", .bitsize = 2, .fields = .{ @@ -10,14 +10,14 @@ .{ .value = 0x1, .name = "down" }, .{ .value = 0x2, .name = "up" }, }, - }, + } }, }, }, .{ .set_enum_type = .{ .of = "types.peripherals.GPIO.PIN_CNF.PULL", .to = "types.peripherals.GPIO.Pull" } }, .{ - .add_enum = .{ + .add_type = .{ .parent = "types.peripherals.GPIO", - .@"enum" = .{ + .type = .{ .@"enum" = .{ .name = "DriveStrength", .bitsize = 3, .fields = .{ @@ -30,7 +30,7 @@ .{ .value = 0x6, .name = "SOD1" }, .{ .value = 0x7, .name = "HOD1" }, }, - }, + } }, }, }, .{ .set_enum_type = .{ .of = "types.peripherals.GPIO.PIN_CNF.DRIVE", .to = "types.peripherals.GPIO.DriveStrength" } }, diff --git a/port/nordic/nrf5x/patches/nrf528xx.zon b/port/nordic/nrf5x/patches/nrf528xx.zon index f7cdd28e9..9dedb3351 100644 --- a/port/nordic/nrf5x/patches/nrf528xx.zon +++ b/port/nordic/nrf5x/patches/nrf528xx.zon @@ -1,8 +1,8 @@ .{ .{ - .add_enum = .{ + .add_type = .{ .parent = "types.peripherals.P0", - .@"enum" = .{ + .type = .{ .@"enum" = .{ .name = "Pull", .bitsize = 2, .fields = .{ @@ -10,14 +10,14 @@ .{ .value = 0x1, .name = "down" }, .{ .value = 0x2, .name = "up" }, }, - }, + } }, }, }, .{ .set_enum_type = .{ .of = "types.peripherals.P0.PIN_CNF.PULL", .to = "types.peripherals.P0.Pull" } }, .{ - .add_enum = .{ + .add_type = .{ .parent = "types.peripherals.P0", - .@"enum" = .{ + .type = .{ .@"enum" = .{ .name = "DriveStrength", .bitsize = 3, .fields = .{ @@ -30,7 +30,7 @@ .{ .value = 0x6, .name = "SOD1" }, .{ .value = 0x7, .name = "HOD1" }, }, - }, + } }, }, }, .{ .set_enum_type = .{ .of = "types.peripherals.P0.PIN_CNF.DRIVE", .to = "types.peripherals.P0.DriveStrength" } }, diff --git a/port/raspberrypi/rp2xxx/patches/rp2350.zon b/port/raspberrypi/rp2xxx/patches/rp2350.zon index 093aa5c9a..c8e3f147e 100644 --- a/port/raspberrypi/rp2xxx/patches/rp2350.zon +++ b/port/raspberrypi/rp2xxx/patches/rp2350.zon @@ -282,9 +282,9 @@ "types.peripherals.IO_BANK0.GPIO47_CTRL.FUNCSEL", }, } }, - .{ .add_enum = .{ + .{ .add_type = .{ .parent = "types.peripherals.PADS_BANK0", - .@"enum" = .{ + .type = .{ .@"enum" = .{ .name = "DriveStrength", .bitsize = 2, .fields = .{ @@ -293,7 +293,7 @@ .{ .value = 0x2, .name = "8mA" }, .{ .value = 0x3, .name = "12mA" }, }, - }, + } }, } }, .{ .set_enum_type = .{ .of = "types.peripherals.PADS_BANK0.GPIO0.DRIVE", .to = "types.peripherals.PADS_BANK0.DriveStrength" } }, .{ .set_enum_type = .{ .of = "types.peripherals.PADS_BANK0.GPIO1.DRIVE", .to = "types.peripherals.PADS_BANK0.DriveStrength" } }, @@ -343,9 +343,9 @@ .{ .set_enum_type = .{ .of = "types.peripherals.PADS_BANK0.GPIO45.DRIVE", .to = "types.peripherals.PADS_BANK0.DriveStrength" } }, .{ .set_enum_type = .{ .of = "types.peripherals.PADS_BANK0.GPIO46.DRIVE", .to = "types.peripherals.PADS_BANK0.DriveStrength" } }, .{ .set_enum_type = .{ .of = "types.peripherals.PADS_BANK0.GPIO47.DRIVE", .to = "types.peripherals.PADS_BANK0.DriveStrength" } }, - .{ .add_enum = .{ + .{ .add_type = .{ .parent = "types.peripherals.USB_DPRAM", - .@"enum" = .{ + .type = .{ .@"enum" = .{ .name = "EndpointType", .bitsize = 2, .fields = .{ @@ -354,7 +354,7 @@ .{ .value = 0x2, .name = "bulk" }, .{ .value = 0x3, .name = "interrupt" }, }, - }, + } }, } }, .{ .set_enum_type = .{ .of = "types.peripherals.USB_DPRAM.EP1_IN_CONTROL.ENDPOINT_TYPE", .to = "types.peripherals.USB_DPRAM.EndpointType" } }, .{ .set_enum_type = .{ .of = "types.peripherals.USB_DPRAM.EP1_OUT_CONTROL.ENDPOINT_TYPE", .to = "types.peripherals.USB_DPRAM.EndpointType" } }, @@ -387,9 +387,9 @@ .{ .set_enum_type = .{ .of = "types.peripherals.USB_DPRAM.EP15_IN_CONTROL.ENDPOINT_TYPE", .to = "types.peripherals.USB_DPRAM.EndpointType" } }, .{ .set_enum_type = .{ .of = "types.peripherals.USB_DPRAM.EP15_OUT_CONTROL.ENDPOINT_TYPE", .to = "types.peripherals.USB_DPRAM.EndpointType" } }, .{ - .add_enum = .{ + .add_type = .{ .parent = "types.peripherals.DMA", - .@"enum" = .{ + .type = .{ .@"enum" = .{ .name = "DataSize", .bitsize = 2, .fields = .{ @@ -397,7 +397,7 @@ .{ .value = 0x1, .name = "size_16" }, .{ .value = 0x2, .name = "size_32" }, }, - }, + } }, }, }, .{ .set_enum_type = .{ .of = "types.peripherals.DMA.CH0_CTRL_TRIG.DATA_SIZE", .to = "types.peripherals.DMA.DataSize" } }, @@ -417,9 +417,9 @@ .{ .set_enum_type = .{ .of = "types.peripherals.DMA.CH14_CTRL_TRIG.DATA_SIZE", .to = "types.peripherals.DMA.DataSize" } }, .{ .set_enum_type = .{ .of = "types.peripherals.DMA.CH15_CTRL_TRIG.DATA_SIZE", .to = "types.peripherals.DMA.DataSize" } }, .{ - .add_enum = .{ + .add_type = .{ .parent = "types.peripherals.DMA", - .@"enum" = .{ + .type = .{ .@"enum" = .{ .name = "Dreq", .bitsize = 6, .fields = .{ @@ -484,7 +484,7 @@ .{ .value = 62, .name = "timer3" }, .{ .value = 63, .name = "permanent" }, }, - }, + } }, }, }, .{ .set_enum_type = .{ .of = "types.peripherals.DMA.CH0_CTRL_TRIG.TREQ_SEL", .to = "types.peripherals.DMA.Dreq" } }, diff --git a/tools/regz/src/Database.zig b/tools/regz/src/Database.zig index d9d751d27..5fee87b86 100644 --- a/tools/regz/src/Database.zig +++ b/tools/regz/src/Database.zig @@ -2095,21 +2095,25 @@ pub fn apply_patch(db: *Database, zon_text: [:0]const u8, diags: *std.zon.parse. set_prop.description, }); }, - .add_enum => |add_enum| { - const struct_id = try db.get_struct_ref(add_enum.parent); - - const enum_id = try db.create_enum(struct_id, .{ - .name = add_enum.@"enum".name, - .description = add_enum.@"enum".description, - .size_bits = add_enum.@"enum".bitsize, - }); - - for (add_enum.@"enum".fields) |enum_field| { - try db.add_enum_field(enum_id, .{ - .name = enum_field.name, - .description = enum_field.description, - .value = enum_field.value, - }); + .add_type => |add_type| { + const struct_id = try db.get_struct_ref(add_type.parent); + + switch (add_type.type) { + .@"enum" => |info| { + const enum_id = try db.create_enum(struct_id, .{ + .name = info.name, + .description = info.description, + .size_bits = info.bitsize, + }); + + for (info.fields) |enum_field| { + try db.add_enum_field(enum_id, .{ + .name = enum_field.name, + .description = enum_field.description, + .value = enum_field.value, + }); + } + }, } }, .set_enum_type => |set_enum_type| { diff --git a/tools/regz/src/patch.zig b/tools/regz/src/patch.zig index a95a4926f..9f8298ad5 100644 --- a/tools/regz/src/patch.zig +++ b/tools/regz/src/patch.zig @@ -3,7 +3,7 @@ const Allocator = std.mem.Allocator; const Database = @import("Database.zig"); const Arch = @import("arch.zig").Arch; -pub const Type = struct { +pub const Type = union(enum) { pub const EnumField = struct { name: []const u8, description: ?[]const u8 = null, @@ -16,6 +16,8 @@ pub const Type = struct { bitsize: u8, fields: []const EnumField = &.{}, }; + + @"enum": Enum, }; pub const Patch = union(enum) { @@ -29,9 +31,15 @@ pub const Patch = union(enum) { value: []const u8, description: ?[]const u8 = null, }, - add_enum: struct { + add_interrupt: struct { + device_name: []const u8, + idx: i32, + name: []const u8, + description: ?[]const u8 = null, + }, + add_type: struct { parent: []const u8, - @"enum": Type.Enum, + type: Type, }, /// The replaced type MUST be the same size. Bit or Byte size depends on the /// context @@ -39,12 +47,6 @@ pub const Patch = union(enum) { of: []const u8, to: ?[]const u8, }, - add_interrupt: struct { - device_name: []const u8, - idx: i32, - name: []const u8, - description: ?[]const u8 = null, - }, /// Creates a new enum type in the specified parent struct and applies it /// to all the specified field references. This is a convenience patch that /// combines `add_enum` with multiple `set_enum_type` operations. @@ -53,17 +55,4 @@ pub const Patch = union(enum) { @"enum": Type.Enum, apply_to: []const []const u8, }, - - pub fn from_json_str(allocator: Allocator, json_str: []const u8) !std.json.Parsed(Patch) { - return std.json.parseFromSlice(Patch, allocator, json_str, .{}); - } -}; - -/// List for assembling patches in build scripts -pub const PatchList = struct { - entries: std.array_list.Managed(Patch), - - pub fn append(list: *PatchList, patch: Patch) void { - list.append(patch) catch @panic("OOM"); - } }; From 20420417a1b38eb76e82c4a7d774002fd4c35e68 Mon Sep 17 00:00:00 2001 From: Piotr Fila Date: Sun, 8 Feb 2026 03:29:23 +0100 Subject: [PATCH 16/23] rename add_enum_and_apply -> add_type_and_apply --- port/raspberrypi/rp2xxx/patches/rp2040.zon | 102 ++++++++++----------- port/raspberrypi/rp2xxx/patches/rp2350.zon | 12 +-- tools/regz/src/Database.zig | 77 +++++++--------- tools/regz/src/analysis.zig | 2 +- tools/regz/src/patch.zig | 38 ++++---- tools/sorcerer/src/RegzWindow.zig | 18 ++-- tools/sorcerer/src/test_diff.zig | 24 ++--- 7 files changed, 133 insertions(+), 140 deletions(-) diff --git a/port/raspberrypi/rp2xxx/patches/rp2040.zon b/port/raspberrypi/rp2xxx/patches/rp2040.zon index bb46f181c..f7f9da55d 100644 --- a/port/raspberrypi/rp2xxx/patches/rp2040.zon +++ b/port/raspberrypi/rp2xxx/patches/rp2040.zon @@ -1,7 +1,7 @@ .{ - .{ .add_enum_and_apply = .{ + .{ .add_type_and_apply = .{ .parent = "types.peripherals.IO_BANK0", - .@"enum" = .{ + .type = .{ .@"enum" = .{ .name = "Override", .bitsize = 2, .fields = .{ @@ -10,7 +10,7 @@ .{ .value = 2, .name = "low" }, .{ .value = 3, .name = "high" }, }, - }, + } }, .apply_to = .{ "types.peripherals.IO_BANK0.GPIO0_CTRL.INOVER", "types.peripherals.IO_BANK0.GPIO0_CTRL.IRQOVER", @@ -134,9 +134,9 @@ "types.peripherals.IO_BANK0.GPIO29_CTRL.OUTOVER", }, } }, - .{ .add_enum_and_apply = .{ + .{ .add_type_and_apply = .{ .parent = "types.peripherals.IO_BANK0", - .@"enum" = .{ + .type = .{ .@"enum" = .{ .name = "Function", .bitsize = 5, .fields = .{ @@ -152,7 +152,7 @@ .{ .value = 9, .name = "usb" }, .{ .value = 0x1f, .name = "disabled" }, }, - }, + } }, .apply_to = .{ "types.peripherals.IO_BANK0.GPIO0_CTRL.FUNCSEL", "types.peripherals.IO_BANK0.GPIO1_CTRL.FUNCSEL", @@ -186,9 +186,9 @@ "types.peripherals.IO_BANK0.GPIO29_CTRL.FUNCSEL", }, } }, - .{ .add_enum_and_apply = .{ + .{ .add_type_and_apply = .{ .parent = "types.peripherals.USB_DPRAM", - .@"enum" = .{ + .type = .{ .@"enum" = .{ .name = "EndpointType", .bitsize = 2, .fields = .{ @@ -197,7 +197,7 @@ .{ .name = "bulk", .value = 2 }, .{ .name = "interrupt", .value = 3 }, }, - }, + } }, .apply_to = .{ "types.peripherals.USB_DPRAM.EP1_IN_CONTROL.ENDPOINT_TYPE", "types.peripherals.USB_DPRAM.EP1_OUT_CONTROL.ENDPOINT_TYPE", @@ -231,9 +231,9 @@ "types.peripherals.USB_DPRAM.EP15_OUT_CONTROL.ENDPOINT_TYPE", }, } }, - .{ .add_enum_and_apply = .{ + .{ .add_type_and_apply = .{ .parent = "types.peripherals.DMA", - .@"enum" = .{ + .type = .{ .@"enum" = .{ .name = "DataSize", .bitsize = 2, .fields = .{ @@ -241,7 +241,7 @@ .{ .name = "size_16", .value = 1 }, .{ .name = "size_32", .value = 2 }, }, - }, + } }, .apply_to = .{ "types.peripherals.DMA.CH0_CTRL_TRIG.DATA_SIZE", "types.peripherals.DMA.CH1_CTRL_TRIG.DATA_SIZE", @@ -257,9 +257,9 @@ "types.peripherals.DMA.CH11_CTRL_TRIG.DATA_SIZE", }, } }, - .{ .add_enum_and_apply = .{ + .{ .add_type_and_apply = .{ .parent = "types.peripherals.DMA", - .@"enum" = .{ + .type = .{ .@"enum" = .{ .name = "Dreq", .bitsize = 6, .fields = .{ @@ -309,7 +309,7 @@ .{ .name = "timer3", .value = 62 }, .{ .name = "permanent", .value = 63 }, }, - }, + } }, .apply_to = .{ "types.peripherals.DMA.CH0_CTRL_TRIG.TREQ_SEL", "types.peripherals.DMA.CH1_CTRL_TRIG.TREQ_SEL", @@ -325,9 +325,9 @@ "types.peripherals.DMA.CH11_CTRL_TRIG.TREQ_SEL", }, } }, - .{ .add_enum_and_apply = .{ + .{ .add_type_and_apply = .{ .parent = "types.peripherals.USB_DPRAM", - .@"enum" = .{ + .type = .{ .@"enum" = .{ .name = "BufferOffset", .bitsize = 2, .fields = .{ @@ -336,7 +336,7 @@ .{ .name = "512", .value = 2 }, .{ .name = "1024", .value = 3 }, }, - }, + } }, .apply_to = .{ "types.peripherals.USB_DPRAM.EP0_IN_BUFFER_CONTROL.DOUBLE_BUFFER_ISO_OFFSET", "types.peripherals.USB_DPRAM.EP0_OUT_BUFFER_CONTROL.DOUBLE_BUFFER_ISO_OFFSET", @@ -372,18 +372,18 @@ "types.peripherals.USB_DPRAM.EP15_OUT_BUFFER_CONTROL.DOUBLE_BUFFER_ISO_OFFSET", }, } }, - .{ .add_enum_and_apply = .{ + .{ .add_type_and_apply = .{ .parent = "types.peripherals.ROSC", - .@"enum" = .{ + .type = .{ .@"enum" = .{ .name = "Password", .bitsize = 16, .fields = .{.{ .name = "PASS", .value = 38550 }}, - }, + } }, .apply_to = .{ "types.peripherals.ROSC.FREQA.PASSWD", "types.peripherals.ROSC.FREQB.PASSWD" }, } }, - .{ .add_enum_and_apply = .{ + .{ .add_type_and_apply = .{ .parent = "types.peripherals.PWM", - .@"enum" = .{ + .type = .{ .@"enum" = .{ .name = "DivMode", .bitsize = 2, .fields = .{ @@ -408,7 +408,7 @@ .value = 3, }, }, - }, + } }, .apply_to = .{ "types.peripherals.PWM.CH0_CSR.DIVMODE", "types.peripherals.PWM.CH1_CSR.DIVMODE", @@ -420,9 +420,9 @@ "types.peripherals.PWM.CH7_CSR.DIVMODE", }, } }, - .{ .add_enum_and_apply = .{ + .{ .add_type_and_apply = .{ .parent = "types.peripherals.PIO0", - .@"enum" = .{ + .type = .{ .@"enum" = .{ .name = "StatusSelect", .bitsize = 1, .fields = .{ .{ @@ -434,7 +434,7 @@ .description = "All-ones if RX FIFO level < N, otherwise all-zeroes", .value = 1, } }, - }, + } }, .apply_to = .{ "types.peripherals.PIO0.SM0_EXECCTRL.STATUS_SEL", "types.peripherals.PIO0.SM1_EXECCTRL.STATUS_SEL", @@ -442,9 +442,9 @@ "types.peripherals.PIO0.SM3_EXECCTRL.STATUS_SEL", }, } }, - .{ .add_enum_and_apply = .{ + .{ .add_type_and_apply = .{ .parent = "types.peripherals.I2C0", - .@"enum" = .{ + .type = .{ .@"enum" = .{ .name = "RxUnderState", .bitsize = 1, .fields = .{ .{ @@ -456,16 +456,16 @@ .description = "RX_UNDER interrupt is active", .value = 1, } }, - }, + } }, .apply_to = .{ "types.peripherals.I2C0.IC_INTR_STAT.R_RX_UNDER", "types.peripherals.I2C0.IC_RAW_INTR_STAT.RX_UNDER" }, } }, - .{ .add_enum_and_apply = .{ + .{ .add_type_and_apply = .{ .parent = "types.peripherals.DMA", - .@"enum" = .{ + .type = .{ .@"enum" = .{ .name = "RingSize", .bitsize = 4, .fields = .{.{ .name = "RING_NONE", .value = 0 }}, - }, + } }, .apply_to = .{ "types.peripherals.DMA.CH0_CTRL_TRIG.RING_SIZE", "types.peripherals.DMA.CH1_CTRL_TRIG.RING_SIZE", @@ -481,9 +481,9 @@ "types.peripherals.DMA.CH11_CTRL_TRIG.RING_SIZE", }, } }, - .{ .add_enum_and_apply = .{ + .{ .add_type_and_apply = .{ .parent = "types.peripherals.CLOCKS", - .@"enum" = .{ + .type = .{ .@"enum" = .{ .name = "AuxSrc", .bitsize = 4, .fields = .{ @@ -499,12 +499,12 @@ .{ .name = "clk_rtc", .value = 9 }, .{ .name = "clk_ref", .value = 10 }, }, - }, + } }, .apply_to = .{ "types.peripherals.CLOCKS.CLK_GPOUT0_CTRL.AUXSRC", "types.peripherals.CLOCKS.CLK_GPOUT1_CTRL.AUXSRC" }, } }, - .{ .add_enum_and_apply = .{ + .{ .add_type_and_apply = .{ .parent = "types.peripherals.CLOCKS", - .@"enum" = .{ + .type = .{ .@"enum" = .{ .name = "AuxSrcPh", .bitsize = 4, .fields = .{ @@ -520,12 +520,12 @@ .{ .name = "clk_rtc", .value = 9 }, .{ .name = "clk_ref", .value = 10 }, }, - }, + } }, .apply_to = .{ "types.peripherals.CLOCKS.CLK_GPOUT2_CTRL.AUXSRC", "types.peripherals.CLOCKS.CLK_GPOUT3_CTRL.AUXSRC" }, } }, - .{ .add_enum_and_apply = .{ + .{ .add_type_and_apply = .{ .parent = "types.peripherals.CLOCKS", - .@"enum" = .{ + .type = .{ .@"enum" = .{ .name = "AuxSrcExtra", .bitsize = 3, .fields = .{ @@ -536,16 +536,16 @@ .{ .name = "clksrc_gpin0", .value = 4 }, .{ .name = "clksrc_gpin1", .value = 5 }, }, - }, + } }, .apply_to = .{ "types.peripherals.CLOCKS.CLK_USB_CTRL.AUXSRC", "types.peripherals.CLOCKS.CLK_ADC_CTRL.AUXSRC", "types.peripherals.CLOCKS.CLK_RTC_CTRL.AUXSRC", }, } }, - .{ .add_enum_and_apply = .{ + .{ .add_type_and_apply = .{ .parent = "types.peripherals.BUSCTRL", - .@"enum" = .{ + .type = .{ .@"enum" = .{ .name = "PerfSelect", .bitsize = 5, .fields = .{ @@ -570,7 +570,7 @@ .{ .name = "rom_contested", .value = 18 }, .{ .name = "rom", .value = 19 }, }, - }, + } }, .apply_to = .{ "types.peripherals.BUSCTRL.PERFSEL0.PERFSEL0", "types.peripherals.BUSCTRL.PERFSEL1.PERFSEL1", @@ -578,9 +578,9 @@ "types.peripherals.BUSCTRL.PERFSEL3.PERFSEL3", }, } }, - .{ .add_enum_and_apply = .{ + .{ .add_type_and_apply = .{ .parent = "types.peripherals.PADS_BANK0", - .@"enum" = .{ + .type = .{ .@"enum" = .{ .name = "DriveStrength", .bitsize = 2, .fields = .{ @@ -589,7 +589,7 @@ .{ .name = "8mA", .value = 2 }, .{ .name = "12mA", .value = 3 }, }, - }, + } }, .apply_to = .{ "types.peripherals.PADS_BANK0.GPIO0.DRIVE", "types.peripherals.PADS_BANK0.GPIO1.DRIVE", @@ -625,9 +625,9 @@ "types.peripherals.PADS_BANK0.SWD.DRIVE", }, } }, - .{ .add_enum_and_apply = .{ + .{ .add_type_and_apply = .{ .parent = "types.peripherals.PADS_QSPI", - .@"enum" = .{ + .type = .{ .@"enum" = .{ .name = "DriveStrength", .bitsize = 2, .fields = .{ @@ -636,7 +636,7 @@ .{ .name = "8mA", .value = 2 }, .{ .name = "12mA", .value = 3 }, }, - }, + } }, .apply_to = .{ "types.peripherals.PADS_QSPI.GPIO_QSPI_SCLK.DRIVE", "types.peripherals.PADS_QSPI.GPIO_QSPI_SD0.DRIVE", diff --git a/port/raspberrypi/rp2xxx/patches/rp2350.zon b/port/raspberrypi/rp2xxx/patches/rp2350.zon index c8e3f147e..6b8ed717c 100644 --- a/port/raspberrypi/rp2xxx/patches/rp2350.zon +++ b/port/raspberrypi/rp2xxx/patches/rp2350.zon @@ -1,7 +1,7 @@ .{ - .{ .add_enum_and_apply = .{ + .{ .add_type_and_apply = .{ .parent = "types.peripherals.IO_BANK0", - .@"enum" = .{ + .type = .{ .@"enum" = .{ .name = "Override", .bitsize = 2, .fields = .{ @@ -10,7 +10,7 @@ .{ .value = 2, .name = "low" }, .{ .value = 3, .name = "high" }, }, - }, + } }, .apply_to = .{ "types.peripherals.IO_BANK0.GPIO0_CTRL.INOVER", "types.peripherals.IO_BANK0.GPIO0_CTRL.IRQOVER", @@ -206,9 +206,9 @@ "types.peripherals.IO_BANK0.GPIO47_CTRL.OUTOVER", }, } }, - .{ .add_enum_and_apply = .{ + .{ .add_type_and_apply = .{ .parent = "types.peripherals.IO_BANK0", - .@"enum" = .{ + .type = .{ .@"enum" = .{ .name = "Function", .bitsize = 5, .fields = .{ @@ -230,7 +230,7 @@ .{ .value = 11, .name = "uart_alt" }, .{ .value = 0x1f, .name = "disabled" }, }, - }, + } }, .apply_to = .{ "types.peripherals.IO_BANK0.GPIO0_CTRL.FUNCSEL", "types.peripherals.IO_BANK0.GPIO1_CTRL.FUNCSEL", diff --git a/tools/regz/src/Database.zig b/tools/regz/src/Database.zig index 5fee87b86..116fb35c6 100644 --- a/tools/regz/src/Database.zig +++ b/tools/regz/src/Database.zig @@ -2097,24 +2097,7 @@ pub fn apply_patch(db: *Database, zon_text: [:0]const u8, diags: *std.zon.parse. }, .add_type => |add_type| { const struct_id = try db.get_struct_ref(add_type.parent); - - switch (add_type.type) { - .@"enum" => |info| { - const enum_id = try db.create_enum(struct_id, .{ - .name = info.name, - .description = info.description, - .size_bits = info.bitsize, - }); - - for (info.fields) |enum_field| { - try db.add_enum_field(enum_id, .{ - .name = enum_field.name, - .description = enum_field.description, - .value = enum_field.value, - }); - } - }, - } + _ = try db.add_type_helper(struct_id, add_type.type.@"enum".name, add_type.type); }, .set_enum_type => |set_enum_type| { const enum_id = if (set_enum_type.to) |to| try db.get_enum_ref(to) else null; @@ -2134,35 +2117,45 @@ pub fn apply_patch(db: *Database, zon_text: [:0]const u8, diags: *std.zon.parse. .idx = add_interrupt.idx, }); }, - .add_enum_and_apply => |add_enum_patch| { + .add_type_and_apply => |add_type_patch| { // First, create the enum (same as add_enum) - const struct_id = try db.get_struct_ref(add_enum_patch.parent); - - const enum_id = try db.create_enum(struct_id, .{ - .name = add_enum_patch.@"enum".name, - .description = add_enum_patch.@"enum".description, - .size_bits = add_enum_patch.@"enum".bitsize, - }); + const struct_id = try db.get_struct_ref(add_type_patch.parent); - for (add_enum_patch.@"enum".fields) |enum_field| { - try db.add_enum_field(enum_id, .{ - .name = enum_field.name, - .description = enum_field.description, - .value = enum_field.value, - }); - } + const type_id = try db.add_type_helper(struct_id, add_type_patch.type.@"enum".name, add_type_patch.type); // Then, apply to all specified fields (same as set_enum_type) - for (add_enum_patch.apply_to) |field_ref| { + for (add_type_patch.apply_to) |field_ref| { const field_name, const register_ref = try get_ref_last_component(field_ref); const register_id = try db.get_register_ref(register_ref orelse return error.InvalidRef); - try db.set_register_field_enum_id(register_id, field_name, enum_id); + try db.set_register_field_enum_id(register_id, field_name, type_id); } }, } } } +fn add_type_helper(db: *Database, parent: StructID, name: []const u8, @"type": Patch.Type) !EnumID { + switch (@"type") { + .@"enum" => |info| { + const enum_id = try db.create_enum(parent, .{ + .name = name, + .description = info.description, + .size_bits = info.bitsize, + }); + + for (info.fields) |enum_field| { + try db.add_enum_field(enum_id, .{ + .name = enum_field.name, + .description = enum_field.description, + .value = enum_field.value, + }); + } + + return enum_id; + }, + } +} + pub const ToZigOptions = gen.ToZigOptions; pub fn to_zig(db: *Database, output_dir: Directory, opts: ToZigOptions) !void { @@ -2177,7 +2170,7 @@ test "all" { _ = svd; } -test "add_enum_and_apply patch creates enum and applies to fields" { +test "add_type_and_apply patch creates enum and applies to fields" { const allocator = std.testing.allocator; var db = try Database.create(allocator); @@ -2223,11 +2216,11 @@ test "add_enum_and_apply patch creates enum and applies to fields" { .offset_bits = 0, }); - // Apply the add_enum_and_apply patch + // Apply the add_type_and_apply patch const patch_zon: [:0]const u8 = \\.{ \\ .{ - \\ .add_enum_and_apply = .{ + \\ .add_type_and_apply = .{ \\ .parent = "types.peripherals.TEST_PERIPHERAL", \\ .@"enum" = .{ \\ .name = "TestMode", @@ -2277,7 +2270,7 @@ test "add_enum_and_apply patch creates enum and applies to fields" { try std.testing.expectEqual(enum_info.id, fields2[0].enum_id.?); } -test "add_enum_and_apply patch with empty apply_to list" { +test "add_type_and_apply patch with empty apply_to list" { const allocator = std.testing.allocator; var db = try Database.create(allocator); @@ -2293,7 +2286,7 @@ test "add_enum_and_apply patch with empty apply_to list" { const patch_zon: [:0]const u8 = \\.{ \\ .{ - \\ .add_enum_and_apply = .{ + \\ .add_type_and_apply = .{ \\ .parent = "types.peripherals.TEST_PERIPHERAL", \\ .@"enum" = .{ \\ .name = "UnusedEnum", @@ -2321,7 +2314,7 @@ test "add_enum_and_apply patch with empty apply_to list" { try std.testing.expectEqual(@as(u8, 4), enum_info.size_bits); } -test "add_enum_and_apply patch with invalid field reference" { +test "add_type_and_apply patch with invalid field reference" { const allocator = std.testing.allocator; var db = try Database.create(allocator); @@ -2336,7 +2329,7 @@ test "add_enum_and_apply patch with invalid field reference" { const patch_zon: [:0]const u8 = \\.{ \\ .{ - \\ .add_enum_and_apply = .{ + \\ .add_type_and_apply = .{ \\ .parent = "types.peripherals.TEST_PERIPHERAL", \\ .@"enum" = .{ \\ .name = "TestEnum", diff --git a/tools/regz/src/analysis.zig b/tools/regz/src/analysis.zig index 2ac9e9c74..a1a57a6bf 100644 --- a/tools/regz/src/analysis.zig +++ b/tools/regz/src/analysis.zig @@ -2,7 +2,7 @@ //! //! This module provides analysis capabilities to identify and group anonymous //! enums that are equivalent (same fields, descriptions, and size) within a -//! peripheral type. This enables suggesting `add_enum_and_apply` patches. +//! peripheral type. This enables suggesting `add_type_and_apply` patches. const Analysis = @This(); const std = @import("std"); diff --git a/tools/regz/src/patch.zig b/tools/regz/src/patch.zig index 9f8298ad5..0f2d72fde 100644 --- a/tools/regz/src/patch.zig +++ b/tools/regz/src/patch.zig @@ -3,24 +3,24 @@ const Allocator = std.mem.Allocator; const Database = @import("Database.zig"); const Arch = @import("arch.zig").Arch; -pub const Type = union(enum) { - pub const EnumField = struct { - name: []const u8, - description: ?[]const u8 = null, - value: u32, - }; +pub const Patch = union(enum) { + pub const Type = union(enum) { + pub const EnumField = struct { + name: []const u8, + description: ?[]const u8 = null, + value: u32, + }; - pub const Enum = struct { - name: []const u8, - description: ?[]const u8 = null, - bitsize: u8, - fields: []const EnumField = &.{}, - }; + pub const Enum = struct { + name: []const u8, + description: ?[]const u8 = null, + bitsize: u8, + fields: []const EnumField = &.{}, + }; - @"enum": Enum, -}; + @"enum": Enum, + }; -pub const Patch = union(enum) { override_arch: struct { device_name: []const u8, arch: Arch, @@ -47,12 +47,12 @@ pub const Patch = union(enum) { of: []const u8, to: ?[]const u8, }, - /// Creates a new enum type in the specified parent struct and applies it + /// Creates a new type in the specified parent struct and applies it /// to all the specified field references. This is a convenience patch that - /// combines `add_enum` with multiple `set_enum_type` operations. - add_enum_and_apply: struct { + /// combines `add_type` with multiple `set_enum_type` operations. + add_type_and_apply: struct { parent: []const u8, - @"enum": Type.Enum, + type: Type, apply_to: []const []const u8, }, }; diff --git a/tools/sorcerer/src/RegzWindow.zig b/tools/sorcerer/src/RegzWindow.zig index e9eb20e50..65541201f 100644 --- a/tools/sorcerer/src/RegzWindow.zig +++ b/tools/sorcerer/src/RegzWindow.zig @@ -1062,7 +1062,7 @@ fn get_patch_label(patch: regz.Patch, arena: Allocator) []const u8 { .add_enum => |p| std.fmt.allocPrint(arena, "add_enum: {s}", .{p.@"enum".name}) catch "add_enum", .set_enum_type => |p| std.fmt.allocPrint(arena, "set_enum_type: {s}", .{p.of}) catch "set_enum_type", .add_interrupt => |p| std.fmt.allocPrint(arena, "add_interrupt: {s}", .{p.name}) catch "add_interrupt", - .add_enum_and_apply => |p| std.fmt.allocPrint(arena, "add_enum_and_apply: {s}", .{p.@"enum".name}) catch "add_enum_and_apply", + .add_type_and_apply => |p| std.fmt.allocPrint(arena, "add_type_and_apply: {s}", .{p.@"enum".name}) catch "add_type_and_apply", }; } @@ -1201,7 +1201,7 @@ fn show_patch_details(w: *RegzWindow, arena: Allocator) void { .add_enum => |p| show_add_enum_widget(p, arena), .set_enum_type => |p| show_set_enum_type_widget(p), .add_interrupt => |p| show_add_interrupt_widget(p), - .add_enum_and_apply => |p| show_add_enum_and_apply_widget(p, arena), + .add_type_and_apply => |p| show_add_type_and_apply_widget(p, arena), } }, .diff => { @@ -1793,7 +1793,7 @@ fn show_add_interrupt_widget(p: anytype) void { if (p.description) |d| labeled_field("Description", d); } -fn show_add_enum_and_apply_widget(p: anytype, arena: Allocator) void { +fn show_add_type_and_apply_widget(p: anytype, arena: Allocator) void { labeled_field("Parent", p.parent); show_enum_details(p.@"enum", arena); @@ -2170,8 +2170,8 @@ fn create_patch_from_group(w: *RegzWindow, arena: Allocator, pending: PendingPat const enum_name = std.mem.sliceTo(&pending.enum_name_buffer, 0); if (enum_name.len == 0) return error.EmptyEnumName; - // Create the add_enum_and_apply patch - const patch = try create_add_enum_and_apply_patch( + // Create the add_type_and_apply patch + const patch = try create_add_type_and_apply_patch( w.arena.allocator(), pending.peripheral_name, enum_name, @@ -2290,8 +2290,8 @@ fn on_database_changed(w: *RegzWindow) void { w.cached_diff = null; } -/// Create an add_enum_and_apply patch from an equivalence group -fn create_add_enum_and_apply_patch( +/// Create an add_type_and_apply patch from an equivalence group +fn create_add_type_and_apply_patch( alloc: Allocator, peripheral_name: []const u8, enum_name: []const u8, @@ -2301,7 +2301,7 @@ fn create_add_enum_and_apply_patch( const parent = try std.fmt.allocPrint(alloc, "types.peripherals.{s}", .{peripheral_name}); // Get the EnumField type from the Patch type using type introspection - const AddEnumAndApply = std.meta.TagPayload(regz.Patch, .add_enum_and_apply); + const AddEnumAndApply = std.meta.TagPayload(regz.Patch, .add_type_and_apply); const EnumType = @TypeOf(@as(AddEnumAndApply, undefined).@"enum"); const EnumFieldType = std.meta.Child(@TypeOf(@as(EnumType, undefined).fields)); @@ -2326,7 +2326,7 @@ fn create_add_enum_and_apply_patch( } return .{ - .add_enum_and_apply = .{ + .add_type_and_apply = .{ .parent = parent, .@"enum" = .{ .name = try alloc.dupe(u8, enum_name), diff --git a/tools/sorcerer/src/test_diff.zig b/tools/sorcerer/src/test_diff.zig index 1a1ecc5b2..34e97d848 100644 --- a/tools/sorcerer/src/test_diff.zig +++ b/tools/sorcerer/src/test_diff.zig @@ -290,9 +290,9 @@ const TestEnum = struct { }; const TestPatch = union(enum) { - add_enum_and_apply: struct { + add_type_and_apply: struct { parent: []const u8, - @"enum": TestEnum, + type: union(enum) { @"enum": TestEnum }, apply_to: []const []const u8, }, }; @@ -302,9 +302,9 @@ test "zon serialize single patch - raw output" { defer arena.deinit(); const allocator = arena.allocator(); - const patch = TestPatch{ .add_enum_and_apply = .{ + const patch = TestPatch{ .add_type_and_apply = .{ .parent = "types.peripherals.TEST", - .@"enum" = .{ + .type = .{ .@"enum" = .{ .name = "TestEnum", .description = null, .bitsize = 2, @@ -312,7 +312,7 @@ test "zon serialize single patch - raw output" { .{ .name = "val0", .description = null, .value = 0 }, .{ .name = "val1", .description = null, .value = 1 }, }, - }, + } }, .apply_to = &.{ "types.peripherals.TEST.REG1.FIELD", "types.peripherals.TEST.REG2.FIELD", @@ -328,7 +328,7 @@ test "zon serialize single patch - raw output" { std.debug.print("\n=== Raw ZON output (single patch) ===\n{s}\n=== End ===\n", .{output}); // Check that the output contains expected content - try std.testing.expect(std.mem.indexOf(u8, output, "add_enum_and_apply") != null); + try std.testing.expect(std.mem.indexOf(u8, output, "add_type_and_apply") != null); try std.testing.expect(std.mem.indexOf(u8, output, "TestEnum") != null); } @@ -337,25 +337,25 @@ test "zon serialize multiple patches - raw output" { defer arena.deinit(); const allocator = arena.allocator(); - const patch1 = TestPatch{ .add_enum_and_apply = .{ + const patch1 = TestPatch{ .add_type_and_apply = .{ .parent = "types.peripherals.TEST1", - .@"enum" = .{ + .type = .{ .@"enum" = .{ .name = "Enum1", .description = null, .bitsize = 2, .fields = &.{}, - }, + } }, .apply_to = &.{}, } }; - const patch2 = TestPatch{ .add_enum_and_apply = .{ + const patch2 = TestPatch{ .add_type_and_apply = .{ .parent = "types.peripherals.TEST2", - .@"enum" = .{ + .type = .{ .@"enum" = .{ .name = "Enum2", .description = null, .bitsize = 2, .fields = &.{}, - }, + } }, .apply_to = &.{}, } }; From d02a05d98cb7970049eca18a93e2246571323a21 Mon Sep 17 00:00:00 2001 From: Piotr Fila Date: Sun, 8 Feb 2026 03:38:36 +0100 Subject: [PATCH 17/23] move type name out of enum --- port/nordic/nrf5x/patches/nrf51.zon | 4 +-- port/nordic/nrf5x/patches/nrf528xx.zon | 4 +-- port/raspberrypi/rp2xxx/patches/rp2040.zon | 34 +++++++++++----------- port/raspberrypi/rp2xxx/patches/rp2350.zon | 12 ++++---- tools/regz/src/Database.zig | 4 +-- tools/regz/src/patch.zig | 3 +- tools/sorcerer/src/test_diff.zig | 8 ++--- 7 files changed, 35 insertions(+), 34 deletions(-) diff --git a/port/nordic/nrf5x/patches/nrf51.zon b/port/nordic/nrf5x/patches/nrf51.zon index b3f49b978..8b89ac873 100644 --- a/port/nordic/nrf5x/patches/nrf51.zon +++ b/port/nordic/nrf5x/patches/nrf51.zon @@ -2,8 +2,8 @@ .{ .add_type = .{ .parent = "types.peripherals.GPIO", + .type_name = "Pull", .type = .{ .@"enum" = .{ - .name = "Pull", .bitsize = 2, .fields = .{ .{ .value = 0x0, .name = "disabled" }, @@ -17,8 +17,8 @@ .{ .add_type = .{ .parent = "types.peripherals.GPIO", + .type_name = "DriveStrength", .type = .{ .@"enum" = .{ - .name = "DriveStrength", .bitsize = 3, .fields = .{ .{ .value = 0x0, .name = "SOS1" }, diff --git a/port/nordic/nrf5x/patches/nrf528xx.zon b/port/nordic/nrf5x/patches/nrf528xx.zon index 9dedb3351..cad418bcc 100644 --- a/port/nordic/nrf5x/patches/nrf528xx.zon +++ b/port/nordic/nrf5x/patches/nrf528xx.zon @@ -2,8 +2,8 @@ .{ .add_type = .{ .parent = "types.peripherals.P0", + .type_name = "Pull", .type = .{ .@"enum" = .{ - .name = "Pull", .bitsize = 2, .fields = .{ .{ .value = 0x0, .name = "disabled" }, @@ -17,8 +17,8 @@ .{ .add_type = .{ .parent = "types.peripherals.P0", + .type_name = "DriveStrength", .type = .{ .@"enum" = .{ - .name = "DriveStrength", .bitsize = 3, .fields = .{ .{ .value = 0x0, .name = "SOS1" }, diff --git a/port/raspberrypi/rp2xxx/patches/rp2040.zon b/port/raspberrypi/rp2xxx/patches/rp2040.zon index f7f9da55d..f2e73bbe1 100644 --- a/port/raspberrypi/rp2xxx/patches/rp2040.zon +++ b/port/raspberrypi/rp2xxx/patches/rp2040.zon @@ -1,8 +1,8 @@ .{ .{ .add_type_and_apply = .{ .parent = "types.peripherals.IO_BANK0", + .type_name = "Override", .type = .{ .@"enum" = .{ - .name = "Override", .bitsize = 2, .fields = .{ .{ .value = 0, .name = "normal" }, @@ -136,8 +136,8 @@ } }, .{ .add_type_and_apply = .{ .parent = "types.peripherals.IO_BANK0", + .type_name = "Function", .type = .{ .@"enum" = .{ - .name = "Function", .bitsize = 5, .fields = .{ .{ .value = 0, .name = "xip" }, @@ -188,8 +188,8 @@ } }, .{ .add_type_and_apply = .{ .parent = "types.peripherals.USB_DPRAM", + .type_name = "EndpointType", .type = .{ .@"enum" = .{ - .name = "EndpointType", .bitsize = 2, .fields = .{ .{ .name = "control", .value = 0 }, @@ -233,8 +233,8 @@ } }, .{ .add_type_and_apply = .{ .parent = "types.peripherals.DMA", + .type_name = "DataSize", .type = .{ .@"enum" = .{ - .name = "DataSize", .bitsize = 2, .fields = .{ .{ .name = "size_8", .value = 0 }, @@ -259,8 +259,8 @@ } }, .{ .add_type_and_apply = .{ .parent = "types.peripherals.DMA", + .type_name = "Dreq", .type = .{ .@"enum" = .{ - .name = "Dreq", .bitsize = 6, .fields = .{ .{ .name = "pio0_tx0", .value = 0 }, @@ -327,8 +327,8 @@ } }, .{ .add_type_and_apply = .{ .parent = "types.peripherals.USB_DPRAM", + .type_name = "BufferOffset", .type = .{ .@"enum" = .{ - .name = "BufferOffset", .bitsize = 2, .fields = .{ .{ .name = "128", .value = 0 }, @@ -374,8 +374,8 @@ } }, .{ .add_type_and_apply = .{ .parent = "types.peripherals.ROSC", + .type_name = "Password", .type = .{ .@"enum" = .{ - .name = "Password", .bitsize = 16, .fields = .{.{ .name = "PASS", .value = 38550 }}, } }, @@ -383,8 +383,8 @@ } }, .{ .add_type_and_apply = .{ .parent = "types.peripherals.PWM", + .type_name = "DivMode", .type = .{ .@"enum" = .{ - .name = "DivMode", .bitsize = 2, .fields = .{ .{ @@ -422,8 +422,8 @@ } }, .{ .add_type_and_apply = .{ .parent = "types.peripherals.PIO0", + .type_name = "StatusSelect", .type = .{ .@"enum" = .{ - .name = "StatusSelect", .bitsize = 1, .fields = .{ .{ .name = "TXLEVEL", @@ -444,8 +444,8 @@ } }, .{ .add_type_and_apply = .{ .parent = "types.peripherals.I2C0", + .type_name = "RxUnderState", .type = .{ .@"enum" = .{ - .name = "RxUnderState", .bitsize = 1, .fields = .{ .{ .name = "INACTIVE", @@ -461,8 +461,8 @@ } }, .{ .add_type_and_apply = .{ .parent = "types.peripherals.DMA", + .type_name = "RingSize", .type = .{ .@"enum" = .{ - .name = "RingSize", .bitsize = 4, .fields = .{.{ .name = "RING_NONE", .value = 0 }}, } }, @@ -483,8 +483,8 @@ } }, .{ .add_type_and_apply = .{ .parent = "types.peripherals.CLOCKS", + .type_name = "AuxSrc", .type = .{ .@"enum" = .{ - .name = "AuxSrc", .bitsize = 4, .fields = .{ .{ .name = "clksrc_pll_sys", .value = 0 }, @@ -504,8 +504,8 @@ } }, .{ .add_type_and_apply = .{ .parent = "types.peripherals.CLOCKS", + .type_name = "AuxSrcPh", .type = .{ .@"enum" = .{ - .name = "AuxSrcPh", .bitsize = 4, .fields = .{ .{ .name = "clksrc_pll_sys", .value = 0 }, @@ -525,8 +525,8 @@ } }, .{ .add_type_and_apply = .{ .parent = "types.peripherals.CLOCKS", + .type_name = "AuxSrcExtra", .type = .{ .@"enum" = .{ - .name = "AuxSrcExtra", .bitsize = 3, .fields = .{ .{ .name = "clksrc_pll_usb", .value = 0 }, @@ -545,8 +545,8 @@ } }, .{ .add_type_and_apply = .{ .parent = "types.peripherals.BUSCTRL", + .type_name = "PerfSelect", .type = .{ .@"enum" = .{ - .name = "PerfSelect", .bitsize = 5, .fields = .{ .{ .name = "apb_contested", .value = 0 }, @@ -580,8 +580,8 @@ } }, .{ .add_type_and_apply = .{ .parent = "types.peripherals.PADS_BANK0", + .type_name = "DriveStrength", .type = .{ .@"enum" = .{ - .name = "DriveStrength", .bitsize = 2, .fields = .{ .{ .name = "2mA", .value = 0 }, @@ -627,8 +627,8 @@ } }, .{ .add_type_and_apply = .{ .parent = "types.peripherals.PADS_QSPI", + .type_name = "DriveStrength", .type = .{ .@"enum" = .{ - .name = "DriveStrength", .bitsize = 2, .fields = .{ .{ .name = "2mA", .value = 0 }, diff --git a/port/raspberrypi/rp2xxx/patches/rp2350.zon b/port/raspberrypi/rp2xxx/patches/rp2350.zon index 6b8ed717c..7ca61064f 100644 --- a/port/raspberrypi/rp2xxx/patches/rp2350.zon +++ b/port/raspberrypi/rp2xxx/patches/rp2350.zon @@ -1,8 +1,8 @@ .{ .{ .add_type_and_apply = .{ .parent = "types.peripherals.IO_BANK0", + .type_name = "Override", .type = .{ .@"enum" = .{ - .name = "Override", .bitsize = 2, .fields = .{ .{ .value = 0, .name = "normal" }, @@ -208,8 +208,8 @@ } }, .{ .add_type_and_apply = .{ .parent = "types.peripherals.IO_BANK0", + .type_name = "Function", .type = .{ .@"enum" = .{ - .name = "Function", .bitsize = 5, .fields = .{ .{ .value = 0, .name = "hstx" }, @@ -284,8 +284,8 @@ } }, .{ .add_type = .{ .parent = "types.peripherals.PADS_BANK0", + .type_name = "DriveStrength", .type = .{ .@"enum" = .{ - .name = "DriveStrength", .bitsize = 2, .fields = .{ .{ .value = 0x0, .name = "2mA" }, @@ -345,8 +345,8 @@ .{ .set_enum_type = .{ .of = "types.peripherals.PADS_BANK0.GPIO47.DRIVE", .to = "types.peripherals.PADS_BANK0.DriveStrength" } }, .{ .add_type = .{ .parent = "types.peripherals.USB_DPRAM", + .type_name = "EndpointType", .type = .{ .@"enum" = .{ - .name = "EndpointType", .bitsize = 2, .fields = .{ .{ .value = 0x0, .name = "control" }, @@ -389,8 +389,8 @@ .{ .add_type = .{ .parent = "types.peripherals.DMA", + .type_name = "DataSize", .type = .{ .@"enum" = .{ - .name = "DataSize", .bitsize = 2, .fields = .{ .{ .value = 0x0, .name = "size_8" }, @@ -419,8 +419,8 @@ .{ .add_type = .{ .parent = "types.peripherals.DMA", + .type_name = "Dreq", .type = .{ .@"enum" = .{ - .name = "Dreq", .bitsize = 6, .fields = .{ .{ .value = 0, .name = "pio0_tx0" }, diff --git a/tools/regz/src/Database.zig b/tools/regz/src/Database.zig index 116fb35c6..9c84ab1c0 100644 --- a/tools/regz/src/Database.zig +++ b/tools/regz/src/Database.zig @@ -2097,7 +2097,7 @@ pub fn apply_patch(db: *Database, zon_text: [:0]const u8, diags: *std.zon.parse. }, .add_type => |add_type| { const struct_id = try db.get_struct_ref(add_type.parent); - _ = try db.add_type_helper(struct_id, add_type.type.@"enum".name, add_type.type); + _ = try db.add_type_helper(struct_id, add_type.type_name, add_type.type); }, .set_enum_type => |set_enum_type| { const enum_id = if (set_enum_type.to) |to| try db.get_enum_ref(to) else null; @@ -2121,7 +2121,7 @@ pub fn apply_patch(db: *Database, zon_text: [:0]const u8, diags: *std.zon.parse. // First, create the enum (same as add_enum) const struct_id = try db.get_struct_ref(add_type_patch.parent); - const type_id = try db.add_type_helper(struct_id, add_type_patch.type.@"enum".name, add_type_patch.type); + const type_id = try db.add_type_helper(struct_id, add_type_patch.type_name, add_type_patch.type); // Then, apply to all specified fields (same as set_enum_type) for (add_type_patch.apply_to) |field_ref| { diff --git a/tools/regz/src/patch.zig b/tools/regz/src/patch.zig index 0f2d72fde..5d0f90acc 100644 --- a/tools/regz/src/patch.zig +++ b/tools/regz/src/patch.zig @@ -12,7 +12,6 @@ pub const Patch = union(enum) { }; pub const Enum = struct { - name: []const u8, description: ?[]const u8 = null, bitsize: u8, fields: []const EnumField = &.{}, @@ -39,6 +38,7 @@ pub const Patch = union(enum) { }, add_type: struct { parent: []const u8, + type_name: []const u8, type: Type, }, /// The replaced type MUST be the same size. Bit or Byte size depends on the @@ -52,6 +52,7 @@ pub const Patch = union(enum) { /// combines `add_type` with multiple `set_enum_type` operations. add_type_and_apply: struct { parent: []const u8, + type_name: []const u8, type: Type, apply_to: []const []const u8, }, diff --git a/tools/sorcerer/src/test_diff.zig b/tools/sorcerer/src/test_diff.zig index 34e97d848..b0be3495f 100644 --- a/tools/sorcerer/src/test_diff.zig +++ b/tools/sorcerer/src/test_diff.zig @@ -283,7 +283,6 @@ const TestEnumField = struct { }; const TestEnum = struct { - name: []const u8, description: ?[]const u8 = null, bitsize: u8, fields: []const TestEnumField = &.{}, @@ -292,6 +291,7 @@ const TestEnum = struct { const TestPatch = union(enum) { add_type_and_apply: struct { parent: []const u8, + type_name: []const u8, type: union(enum) { @"enum": TestEnum }, apply_to: []const []const u8, }, @@ -304,8 +304,8 @@ test "zon serialize single patch - raw output" { const patch = TestPatch{ .add_type_and_apply = .{ .parent = "types.peripherals.TEST", + .type_name = "TestEnum", .type = .{ .@"enum" = .{ - .name = "TestEnum", .description = null, .bitsize = 2, .fields = &.{ @@ -339,8 +339,8 @@ test "zon serialize multiple patches - raw output" { const patch1 = TestPatch{ .add_type_and_apply = .{ .parent = "types.peripherals.TEST1", + .type_name = "Enum1", .type = .{ .@"enum" = .{ - .name = "Enum1", .description = null, .bitsize = 2, .fields = &.{}, @@ -350,8 +350,8 @@ test "zon serialize multiple patches - raw output" { const patch2 = TestPatch{ .add_type_and_apply = .{ .parent = "types.peripherals.TEST2", + .type_name = "Enum2", .type = .{ .@"enum" = .{ - .name = "Enum2", .description = null, .bitsize = 2, .fields = &.{}, From 313879aa80c7c2ddc5fa8b14f081695472defb4f Mon Sep 17 00:00:00 2001 From: Piotr Fila Date: Sun, 8 Feb 2026 03:50:37 +0100 Subject: [PATCH 18/23] use add_type_and_apply more --- port/nordic/nrf5x/patches/nrf51.zon | 66 ++-- port/nordic/nrf5x/patches/nrf528xx.zon | 66 ++-- port/raspberrypi/rp2xxx/patches/rp2350.zon | 394 +++++++++++---------- 3 files changed, 265 insertions(+), 261 deletions(-) diff --git a/port/nordic/nrf5x/patches/nrf51.zon b/port/nordic/nrf5x/patches/nrf51.zon index 8b89ac873..2f1fcb0e4 100644 --- a/port/nordic/nrf5x/patches/nrf51.zon +++ b/port/nordic/nrf5x/patches/nrf51.zon @@ -1,37 +1,37 @@ .{ - .{ - .add_type = .{ - .parent = "types.peripherals.GPIO", - .type_name = "Pull", - .type = .{ .@"enum" = .{ - .bitsize = 2, - .fields = .{ - .{ .value = 0x0, .name = "disabled" }, - .{ .value = 0x1, .name = "down" }, - .{ .value = 0x2, .name = "up" }, - }, - } }, + .{ .add_type_and_apply = .{ + .parent = "types.peripherals.GPIO", + .type_name = "Pull", + .type = .{ .@"enum" = .{ + .bitsize = 2, + .fields = .{ + .{ .value = 0x0, .name = "disabled" }, + .{ .value = 0x1, .name = "down" }, + .{ .value = 0x2, .name = "up" }, + }, + } }, + .apply_to = .{ + "types.peripherals.GPIO.PIN_CNF.PULL", }, - }, - .{ .set_enum_type = .{ .of = "types.peripherals.GPIO.PIN_CNF.PULL", .to = "types.peripherals.GPIO.Pull" } }, - .{ - .add_type = .{ - .parent = "types.peripherals.GPIO", - .type_name = "DriveStrength", - .type = .{ .@"enum" = .{ - .bitsize = 3, - .fields = .{ - .{ .value = 0x0, .name = "SOS1" }, - .{ .value = 0x1, .name = "HOS1" }, - .{ .value = 0x2, .name = "SOH1" }, - .{ .value = 0x3, .name = "HOH1" }, - .{ .value = 0x4, .name = "DOS1" }, - .{ .value = 0x5, .name = "DOH1" }, - .{ .value = 0x6, .name = "SOD1" }, - .{ .value = 0x7, .name = "HOD1" }, - }, - } }, + } }, + .{ .add_type_and_apply = .{ + .parent = "types.peripherals.GPIO", + .type_name = "DriveStrength", + .type = .{ .@"enum" = .{ + .bitsize = 3, + .fields = .{ + .{ .value = 0x0, .name = "SOS1" }, + .{ .value = 0x1, .name = "HOS1" }, + .{ .value = 0x2, .name = "SOH1" }, + .{ .value = 0x3, .name = "HOH1" }, + .{ .value = 0x4, .name = "DOS1" }, + .{ .value = 0x5, .name = "DOH1" }, + .{ .value = 0x6, .name = "SOD1" }, + .{ .value = 0x7, .name = "HOD1" }, + }, + } }, + .apply_to = .{ + "types.peripherals.GPIO.PIN_CNF.DRIVE", }, - }, - .{ .set_enum_type = .{ .of = "types.peripherals.GPIO.PIN_CNF.DRIVE", .to = "types.peripherals.GPIO.DriveStrength" } }, + } }, } diff --git a/port/nordic/nrf5x/patches/nrf528xx.zon b/port/nordic/nrf5x/patches/nrf528xx.zon index cad418bcc..6a59e60a0 100644 --- a/port/nordic/nrf5x/patches/nrf528xx.zon +++ b/port/nordic/nrf5x/patches/nrf528xx.zon @@ -1,37 +1,37 @@ .{ - .{ - .add_type = .{ - .parent = "types.peripherals.P0", - .type_name = "Pull", - .type = .{ .@"enum" = .{ - .bitsize = 2, - .fields = .{ - .{ .value = 0x0, .name = "disabled" }, - .{ .value = 0x1, .name = "down" }, - .{ .value = 0x2, .name = "up" }, - }, - } }, + .{ .add_type_and_apply = .{ + .parent = "types.peripherals.P0", + .type_name = "Pull", + .type = .{ .@"enum" = .{ + .bitsize = 2, + .fields = .{ + .{ .value = 0x0, .name = "disabled" }, + .{ .value = 0x1, .name = "down" }, + .{ .value = 0x2, .name = "up" }, + }, + } }, + .apply_to = .{ + "types.peripherals.GPIO.PIN_CNF.PULL", }, - }, - .{ .set_enum_type = .{ .of = "types.peripherals.P0.PIN_CNF.PULL", .to = "types.peripherals.P0.Pull" } }, - .{ - .add_type = .{ - .parent = "types.peripherals.P0", - .type_name = "DriveStrength", - .type = .{ .@"enum" = .{ - .bitsize = 3, - .fields = .{ - .{ .value = 0x0, .name = "SOS1" }, - .{ .value = 0x1, .name = "HOS1" }, - .{ .value = 0x2, .name = "SOH1" }, - .{ .value = 0x3, .name = "HOH1" }, - .{ .value = 0x4, .name = "DOS1" }, - .{ .value = 0x5, .name = "DOH1" }, - .{ .value = 0x6, .name = "SOD1" }, - .{ .value = 0x7, .name = "HOD1" }, - }, - } }, + } }, + .{ .add_type_and_apply = .{ + .parent = "types.peripherals.P0", + .type_name = "DriveStrength", + .type = .{ .@"enum" = .{ + .bitsize = 3, + .fields = .{ + .{ .value = 0x0, .name = "SOS1" }, + .{ .value = 0x1, .name = "HOS1" }, + .{ .value = 0x2, .name = "SOH1" }, + .{ .value = 0x3, .name = "HOH1" }, + .{ .value = 0x4, .name = "DOS1" }, + .{ .value = 0x5, .name = "DOH1" }, + .{ .value = 0x6, .name = "SOD1" }, + .{ .value = 0x7, .name = "HOD1" }, + }, + } }, + .apply_to = .{ + "types.peripherals.GPIO.PIN_CNF.DRIVE", }, - }, - .{ .set_enum_type = .{ .of = "types.peripherals.P0.PIN_CNF.DRIVE", .to = "types.peripherals.P0.DriveStrength" } }, + } }, } diff --git a/port/raspberrypi/rp2xxx/patches/rp2350.zon b/port/raspberrypi/rp2xxx/patches/rp2350.zon index 7ca61064f..2696ae4c4 100644 --- a/port/raspberrypi/rp2xxx/patches/rp2350.zon +++ b/port/raspberrypi/rp2xxx/patches/rp2350.zon @@ -282,7 +282,7 @@ "types.peripherals.IO_BANK0.GPIO47_CTRL.FUNCSEL", }, } }, - .{ .add_type = .{ + .{ .add_type_and_apply = .{ .parent = "types.peripherals.PADS_BANK0", .type_name = "DriveStrength", .type = .{ .@"enum" = .{ @@ -294,56 +294,58 @@ .{ .value = 0x3, .name = "12mA" }, }, } }, + .apply_to = .{ + "types.peripherals.PADS_BANK0.GPIO0.DRIVE", + "types.peripherals.PADS_BANK0.GPIO1.DRIVE", + "types.peripherals.PADS_BANK0.GPIO2.DRIVE", + "types.peripherals.PADS_BANK0.GPIO3.DRIVE", + "types.peripherals.PADS_BANK0.GPIO4.DRIVE", + "types.peripherals.PADS_BANK0.GPIO5.DRIVE", + "types.peripherals.PADS_BANK0.GPIO6.DRIVE", + "types.peripherals.PADS_BANK0.GPIO7.DRIVE", + "types.peripherals.PADS_BANK0.GPIO8.DRIVE", + "types.peripherals.PADS_BANK0.GPIO9.DRIVE", + "types.peripherals.PADS_BANK0.GPIO10.DRIVE", + "types.peripherals.PADS_BANK0.GPIO11.DRIVE", + "types.peripherals.PADS_BANK0.GPIO12.DRIVE", + "types.peripherals.PADS_BANK0.GPIO13.DRIVE", + "types.peripherals.PADS_BANK0.GPIO14.DRIVE", + "types.peripherals.PADS_BANK0.GPIO15.DRIVE", + "types.peripherals.PADS_BANK0.GPIO16.DRIVE", + "types.peripherals.PADS_BANK0.GPIO17.DRIVE", + "types.peripherals.PADS_BANK0.GPIO18.DRIVE", + "types.peripherals.PADS_BANK0.GPIO19.DRIVE", + "types.peripherals.PADS_BANK0.GPIO20.DRIVE", + "types.peripherals.PADS_BANK0.GPIO21.DRIVE", + "types.peripherals.PADS_BANK0.GPIO22.DRIVE", + "types.peripherals.PADS_BANK0.GPIO23.DRIVE", + "types.peripherals.PADS_BANK0.GPIO24.DRIVE", + "types.peripherals.PADS_BANK0.GPIO25.DRIVE", + "types.peripherals.PADS_BANK0.GPIO26.DRIVE", + "types.peripherals.PADS_BANK0.GPIO27.DRIVE", + "types.peripherals.PADS_BANK0.GPIO28.DRIVE", + "types.peripherals.PADS_BANK0.GPIO29.DRIVE", + "types.peripherals.PADS_BANK0.GPIO30.DRIVE", + "types.peripherals.PADS_BANK0.GPIO31.DRIVE", + "types.peripherals.PADS_BANK0.GPIO32.DRIVE", + "types.peripherals.PADS_BANK0.GPIO33.DRIVE", + "types.peripherals.PADS_BANK0.GPIO34.DRIVE", + "types.peripherals.PADS_BANK0.GPIO35.DRIVE", + "types.peripherals.PADS_BANK0.GPIO36.DRIVE", + "types.peripherals.PADS_BANK0.GPIO37.DRIVE", + "types.peripherals.PADS_BANK0.GPIO38.DRIVE", + "types.peripherals.PADS_BANK0.GPIO39.DRIVE", + "types.peripherals.PADS_BANK0.GPIO40.DRIVE", + "types.peripherals.PADS_BANK0.GPIO41.DRIVE", + "types.peripherals.PADS_BANK0.GPIO42.DRIVE", + "types.peripherals.PADS_BANK0.GPIO43.DRIVE", + "types.peripherals.PADS_BANK0.GPIO44.DRIVE", + "types.peripherals.PADS_BANK0.GPIO45.DRIVE", + "types.peripherals.PADS_BANK0.GPIO46.DRIVE", + "types.peripherals.PADS_BANK0.GPIO47.DRIVE", + }, } }, - .{ .set_enum_type = .{ .of = "types.peripherals.PADS_BANK0.GPIO0.DRIVE", .to = "types.peripherals.PADS_BANK0.DriveStrength" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.PADS_BANK0.GPIO1.DRIVE", .to = "types.peripherals.PADS_BANK0.DriveStrength" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.PADS_BANK0.GPIO2.DRIVE", .to = "types.peripherals.PADS_BANK0.DriveStrength" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.PADS_BANK0.GPIO3.DRIVE", .to = "types.peripherals.PADS_BANK0.DriveStrength" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.PADS_BANK0.GPIO4.DRIVE", .to = "types.peripherals.PADS_BANK0.DriveStrength" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.PADS_BANK0.GPIO5.DRIVE", .to = "types.peripherals.PADS_BANK0.DriveStrength" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.PADS_BANK0.GPIO6.DRIVE", .to = "types.peripherals.PADS_BANK0.DriveStrength" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.PADS_BANK0.GPIO7.DRIVE", .to = "types.peripherals.PADS_BANK0.DriveStrength" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.PADS_BANK0.GPIO8.DRIVE", .to = "types.peripherals.PADS_BANK0.DriveStrength" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.PADS_BANK0.GPIO9.DRIVE", .to = "types.peripherals.PADS_BANK0.DriveStrength" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.PADS_BANK0.GPIO10.DRIVE", .to = "types.peripherals.PADS_BANK0.DriveStrength" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.PADS_BANK0.GPIO11.DRIVE", .to = "types.peripherals.PADS_BANK0.DriveStrength" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.PADS_BANK0.GPIO12.DRIVE", .to = "types.peripherals.PADS_BANK0.DriveStrength" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.PADS_BANK0.GPIO13.DRIVE", .to = "types.peripherals.PADS_BANK0.DriveStrength" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.PADS_BANK0.GPIO14.DRIVE", .to = "types.peripherals.PADS_BANK0.DriveStrength" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.PADS_BANK0.GPIO15.DRIVE", .to = "types.peripherals.PADS_BANK0.DriveStrength" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.PADS_BANK0.GPIO16.DRIVE", .to = "types.peripherals.PADS_BANK0.DriveStrength" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.PADS_BANK0.GPIO17.DRIVE", .to = "types.peripherals.PADS_BANK0.DriveStrength" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.PADS_BANK0.GPIO18.DRIVE", .to = "types.peripherals.PADS_BANK0.DriveStrength" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.PADS_BANK0.GPIO19.DRIVE", .to = "types.peripherals.PADS_BANK0.DriveStrength" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.PADS_BANK0.GPIO20.DRIVE", .to = "types.peripherals.PADS_BANK0.DriveStrength" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.PADS_BANK0.GPIO21.DRIVE", .to = "types.peripherals.PADS_BANK0.DriveStrength" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.PADS_BANK0.GPIO22.DRIVE", .to = "types.peripherals.PADS_BANK0.DriveStrength" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.PADS_BANK0.GPIO23.DRIVE", .to = "types.peripherals.PADS_BANK0.DriveStrength" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.PADS_BANK0.GPIO24.DRIVE", .to = "types.peripherals.PADS_BANK0.DriveStrength" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.PADS_BANK0.GPIO25.DRIVE", .to = "types.peripherals.PADS_BANK0.DriveStrength" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.PADS_BANK0.GPIO26.DRIVE", .to = "types.peripherals.PADS_BANK0.DriveStrength" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.PADS_BANK0.GPIO27.DRIVE", .to = "types.peripherals.PADS_BANK0.DriveStrength" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.PADS_BANK0.GPIO28.DRIVE", .to = "types.peripherals.PADS_BANK0.DriveStrength" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.PADS_BANK0.GPIO29.DRIVE", .to = "types.peripherals.PADS_BANK0.DriveStrength" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.PADS_BANK0.GPIO30.DRIVE", .to = "types.peripherals.PADS_BANK0.DriveStrength" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.PADS_BANK0.GPIO31.DRIVE", .to = "types.peripherals.PADS_BANK0.DriveStrength" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.PADS_BANK0.GPIO32.DRIVE", .to = "types.peripherals.PADS_BANK0.DriveStrength" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.PADS_BANK0.GPIO33.DRIVE", .to = "types.peripherals.PADS_BANK0.DriveStrength" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.PADS_BANK0.GPIO34.DRIVE", .to = "types.peripherals.PADS_BANK0.DriveStrength" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.PADS_BANK0.GPIO35.DRIVE", .to = "types.peripherals.PADS_BANK0.DriveStrength" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.PADS_BANK0.GPIO36.DRIVE", .to = "types.peripherals.PADS_BANK0.DriveStrength" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.PADS_BANK0.GPIO37.DRIVE", .to = "types.peripherals.PADS_BANK0.DriveStrength" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.PADS_BANK0.GPIO38.DRIVE", .to = "types.peripherals.PADS_BANK0.DriveStrength" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.PADS_BANK0.GPIO39.DRIVE", .to = "types.peripherals.PADS_BANK0.DriveStrength" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.PADS_BANK0.GPIO40.DRIVE", .to = "types.peripherals.PADS_BANK0.DriveStrength" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.PADS_BANK0.GPIO41.DRIVE", .to = "types.peripherals.PADS_BANK0.DriveStrength" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.PADS_BANK0.GPIO42.DRIVE", .to = "types.peripherals.PADS_BANK0.DriveStrength" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.PADS_BANK0.GPIO43.DRIVE", .to = "types.peripherals.PADS_BANK0.DriveStrength" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.PADS_BANK0.GPIO44.DRIVE", .to = "types.peripherals.PADS_BANK0.DriveStrength" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.PADS_BANK0.GPIO45.DRIVE", .to = "types.peripherals.PADS_BANK0.DriveStrength" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.PADS_BANK0.GPIO46.DRIVE", .to = "types.peripherals.PADS_BANK0.DriveStrength" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.PADS_BANK0.GPIO47.DRIVE", .to = "types.peripherals.PADS_BANK0.DriveStrength" } }, - .{ .add_type = .{ + .{ .add_type_and_apply = .{ .parent = "types.peripherals.USB_DPRAM", .type_name = "EndpointType", .type = .{ .@"enum" = .{ @@ -355,154 +357,156 @@ .{ .value = 0x3, .name = "interrupt" }, }, } }, + .apply_to = .{ + "types.peripherals.USB_DPRAM.EP1_IN_CONTROL.ENDPOINT_TYPE", + "types.peripherals.USB_DPRAM.EP1_OUT_CONTROL.ENDPOINT_TYPE", + "types.peripherals.USB_DPRAM.EP2_IN_CONTROL.ENDPOINT_TYPE", + "types.peripherals.USB_DPRAM.EP2_OUT_CONTROL.ENDPOINT_TYPE", + "types.peripherals.USB_DPRAM.EP3_IN_CONTROL.ENDPOINT_TYPE", + "types.peripherals.USB_DPRAM.EP3_OUT_CONTROL.ENDPOINT_TYPE", + "types.peripherals.USB_DPRAM.EP4_IN_CONTROL.ENDPOINT_TYPE", + "types.peripherals.USB_DPRAM.EP4_OUT_CONTROL.ENDPOINT_TYPE", + "types.peripherals.USB_DPRAM.EP5_IN_CONTROL.ENDPOINT_TYPE", + "types.peripherals.USB_DPRAM.EP5_OUT_CONTROL.ENDPOINT_TYPE", + "types.peripherals.USB_DPRAM.EP6_IN_CONTROL.ENDPOINT_TYPE", + "types.peripherals.USB_DPRAM.EP6_OUT_CONTROL.ENDPOINT_TYPE", + "types.peripherals.USB_DPRAM.EP7_IN_CONTROL.ENDPOINT_TYPE", + "types.peripherals.USB_DPRAM.EP7_OUT_CONTROL.ENDPOINT_TYPE", + "types.peripherals.USB_DPRAM.EP8_IN_CONTROL.ENDPOINT_TYPE", + "types.peripherals.USB_DPRAM.EP8_OUT_CONTROL.ENDPOINT_TYPE", + "types.peripherals.USB_DPRAM.EP9_IN_CONTROL.ENDPOINT_TYPE", + "types.peripherals.USB_DPRAM.EP9_OUT_CONTROL.ENDPOINT_TYPE", + "types.peripherals.USB_DPRAM.EP10_IN_CONTROL.ENDPOINT_TYPE", + "types.peripherals.USB_DPRAM.EP10_OUT_CONTROL.ENDPOINT_TYPE", + "types.peripherals.USB_DPRAM.EP11_IN_CONTROL.ENDPOINT_TYPE", + "types.peripherals.USB_DPRAM.EP11_OUT_CONTROL.ENDPOINT_TYPE", + "types.peripherals.USB_DPRAM.EP12_IN_CONTROL.ENDPOINT_TYPE", + "types.peripherals.USB_DPRAM.EP12_OUT_CONTROL.ENDPOINT_TYPE", + "types.peripherals.USB_DPRAM.EP13_IN_CONTROL.ENDPOINT_TYPE", + "types.peripherals.USB_DPRAM.EP13_OUT_CONTROL.ENDPOINT_TYPE", + "types.peripherals.USB_DPRAM.EP14_IN_CONTROL.ENDPOINT_TYPE", + "types.peripherals.USB_DPRAM.EP14_OUT_CONTROL.ENDPOINT_TYPE", + "types.peripherals.USB_DPRAM.EP15_IN_CONTROL.ENDPOINT_TYPE", + "types.peripherals.USB_DPRAM.EP15_OUT_CONTROL.ENDPOINT_TYPE", + }, } }, - .{ .set_enum_type = .{ .of = "types.peripherals.USB_DPRAM.EP1_IN_CONTROL.ENDPOINT_TYPE", .to = "types.peripherals.USB_DPRAM.EndpointType" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.USB_DPRAM.EP1_OUT_CONTROL.ENDPOINT_TYPE", .to = "types.peripherals.USB_DPRAM.EndpointType" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.USB_DPRAM.EP2_IN_CONTROL.ENDPOINT_TYPE", .to = "types.peripherals.USB_DPRAM.EndpointType" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.USB_DPRAM.EP2_OUT_CONTROL.ENDPOINT_TYPE", .to = "types.peripherals.USB_DPRAM.EndpointType" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.USB_DPRAM.EP3_IN_CONTROL.ENDPOINT_TYPE", .to = "types.peripherals.USB_DPRAM.EndpointType" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.USB_DPRAM.EP3_OUT_CONTROL.ENDPOINT_TYPE", .to = "types.peripherals.USB_DPRAM.EndpointType" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.USB_DPRAM.EP4_IN_CONTROL.ENDPOINT_TYPE", .to = "types.peripherals.USB_DPRAM.EndpointType" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.USB_DPRAM.EP4_OUT_CONTROL.ENDPOINT_TYPE", .to = "types.peripherals.USB_DPRAM.EndpointType" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.USB_DPRAM.EP5_IN_CONTROL.ENDPOINT_TYPE", .to = "types.peripherals.USB_DPRAM.EndpointType" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.USB_DPRAM.EP5_OUT_CONTROL.ENDPOINT_TYPE", .to = "types.peripherals.USB_DPRAM.EndpointType" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.USB_DPRAM.EP6_IN_CONTROL.ENDPOINT_TYPE", .to = "types.peripherals.USB_DPRAM.EndpointType" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.USB_DPRAM.EP6_OUT_CONTROL.ENDPOINT_TYPE", .to = "types.peripherals.USB_DPRAM.EndpointType" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.USB_DPRAM.EP7_IN_CONTROL.ENDPOINT_TYPE", .to = "types.peripherals.USB_DPRAM.EndpointType" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.USB_DPRAM.EP7_OUT_CONTROL.ENDPOINT_TYPE", .to = "types.peripherals.USB_DPRAM.EndpointType" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.USB_DPRAM.EP8_IN_CONTROL.ENDPOINT_TYPE", .to = "types.peripherals.USB_DPRAM.EndpointType" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.USB_DPRAM.EP8_OUT_CONTROL.ENDPOINT_TYPE", .to = "types.peripherals.USB_DPRAM.EndpointType" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.USB_DPRAM.EP9_IN_CONTROL.ENDPOINT_TYPE", .to = "types.peripherals.USB_DPRAM.EndpointType" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.USB_DPRAM.EP9_OUT_CONTROL.ENDPOINT_TYPE", .to = "types.peripherals.USB_DPRAM.EndpointType" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.USB_DPRAM.EP10_IN_CONTROL.ENDPOINT_TYPE", .to = "types.peripherals.USB_DPRAM.EndpointType" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.USB_DPRAM.EP10_OUT_CONTROL.ENDPOINT_TYPE", .to = "types.peripherals.USB_DPRAM.EndpointType" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.USB_DPRAM.EP11_IN_CONTROL.ENDPOINT_TYPE", .to = "types.peripherals.USB_DPRAM.EndpointType" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.USB_DPRAM.EP11_OUT_CONTROL.ENDPOINT_TYPE", .to = "types.peripherals.USB_DPRAM.EndpointType" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.USB_DPRAM.EP12_IN_CONTROL.ENDPOINT_TYPE", .to = "types.peripherals.USB_DPRAM.EndpointType" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.USB_DPRAM.EP12_OUT_CONTROL.ENDPOINT_TYPE", .to = "types.peripherals.USB_DPRAM.EndpointType" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.USB_DPRAM.EP13_IN_CONTROL.ENDPOINT_TYPE", .to = "types.peripherals.USB_DPRAM.EndpointType" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.USB_DPRAM.EP13_OUT_CONTROL.ENDPOINT_TYPE", .to = "types.peripherals.USB_DPRAM.EndpointType" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.USB_DPRAM.EP14_IN_CONTROL.ENDPOINT_TYPE", .to = "types.peripherals.USB_DPRAM.EndpointType" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.USB_DPRAM.EP14_OUT_CONTROL.ENDPOINT_TYPE", .to = "types.peripherals.USB_DPRAM.EndpointType" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.USB_DPRAM.EP15_IN_CONTROL.ENDPOINT_TYPE", .to = "types.peripherals.USB_DPRAM.EndpointType" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.USB_DPRAM.EP15_OUT_CONTROL.ENDPOINT_TYPE", .to = "types.peripherals.USB_DPRAM.EndpointType" } }, - .{ - .add_type = .{ - .parent = "types.peripherals.DMA", - .type_name = "DataSize", - .type = .{ .@"enum" = .{ - .bitsize = 2, - .fields = .{ - .{ .value = 0x0, .name = "size_8" }, - .{ .value = 0x1, .name = "size_16" }, - .{ .value = 0x2, .name = "size_32" }, - }, - } }, + .{ .add_type_and_apply = .{ + .parent = "types.peripherals.DMA", + .type_name = "DataSize", + .type = .{ .@"enum" = .{ + .bitsize = 2, + .fields = .{ + .{ .value = 0x0, .name = "size_8" }, + .{ .value = 0x1, .name = "size_16" }, + .{ .value = 0x2, .name = "size_32" }, + }, + } }, + .apply_to = .{ + "types.peripherals.DMA.CH0_CTRL_TRIG.DATA_SIZE", + "types.peripherals.DMA.CH1_CTRL_TRIG.DATA_SIZE", + "types.peripherals.DMA.CH2_CTRL_TRIG.DATA_SIZE", + "types.peripherals.DMA.CH3_CTRL_TRIG.DATA_SIZE", + "types.peripherals.DMA.CH4_CTRL_TRIG.DATA_SIZE", + "types.peripherals.DMA.CH5_CTRL_TRIG.DATA_SIZE", + "types.peripherals.DMA.CH6_CTRL_TRIG.DATA_SIZE", + "types.peripherals.DMA.CH7_CTRL_TRIG.DATA_SIZE", + "types.peripherals.DMA.CH8_CTRL_TRIG.DATA_SIZE", + "types.peripherals.DMA.CH9_CTRL_TRIG.DATA_SIZE", + "types.peripherals.DMA.CH10_CTRL_TRIG.DATA_SIZE", + "types.peripherals.DMA.CH11_CTRL_TRIG.DATA_SIZE", + "types.peripherals.DMA.CH12_CTRL_TRIG.DATA_SIZE", + "types.peripherals.DMA.CH13_CTRL_TRIG.DATA_SIZE", + "types.peripherals.DMA.CH14_CTRL_TRIG.DATA_SIZE", + "types.peripherals.DMA.CH15_CTRL_TRIG.DATA_SIZE", }, - }, - .{ .set_enum_type = .{ .of = "types.peripherals.DMA.CH0_CTRL_TRIG.DATA_SIZE", .to = "types.peripherals.DMA.DataSize" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.DMA.CH1_CTRL_TRIG.DATA_SIZE", .to = "types.peripherals.DMA.DataSize" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.DMA.CH2_CTRL_TRIG.DATA_SIZE", .to = "types.peripherals.DMA.DataSize" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.DMA.CH3_CTRL_TRIG.DATA_SIZE", .to = "types.peripherals.DMA.DataSize" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.DMA.CH4_CTRL_TRIG.DATA_SIZE", .to = "types.peripherals.DMA.DataSize" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.DMA.CH5_CTRL_TRIG.DATA_SIZE", .to = "types.peripherals.DMA.DataSize" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.DMA.CH6_CTRL_TRIG.DATA_SIZE", .to = "types.peripherals.DMA.DataSize" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.DMA.CH7_CTRL_TRIG.DATA_SIZE", .to = "types.peripherals.DMA.DataSize" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.DMA.CH8_CTRL_TRIG.DATA_SIZE", .to = "types.peripherals.DMA.DataSize" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.DMA.CH9_CTRL_TRIG.DATA_SIZE", .to = "types.peripherals.DMA.DataSize" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.DMA.CH10_CTRL_TRIG.DATA_SIZE", .to = "types.peripherals.DMA.DataSize" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.DMA.CH11_CTRL_TRIG.DATA_SIZE", .to = "types.peripherals.DMA.DataSize" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.DMA.CH12_CTRL_TRIG.DATA_SIZE", .to = "types.peripherals.DMA.DataSize" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.DMA.CH13_CTRL_TRIG.DATA_SIZE", .to = "types.peripherals.DMA.DataSize" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.DMA.CH14_CTRL_TRIG.DATA_SIZE", .to = "types.peripherals.DMA.DataSize" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.DMA.CH15_CTRL_TRIG.DATA_SIZE", .to = "types.peripherals.DMA.DataSize" } }, - .{ - .add_type = .{ - .parent = "types.peripherals.DMA", - .type_name = "Dreq", - .type = .{ .@"enum" = .{ - .bitsize = 6, - .fields = .{ - .{ .value = 0, .name = "pio0_tx0" }, - .{ .value = 1, .name = "pio0_tx1" }, - .{ .value = 2, .name = "pio0_tx2" }, - .{ .value = 3, .name = "pio0_tx3" }, - .{ .value = 4, .name = "pio0_rx0" }, - .{ .value = 5, .name = "pio0_rx1" }, - .{ .value = 6, .name = "pio0_rx2" }, - .{ .value = 7, .name = "pio0_rx3" }, - .{ .value = 8, .name = "pio1_tx0" }, - .{ .value = 9, .name = "pio1_tx1" }, - .{ .value = 10, .name = "pio1_tx2" }, - .{ .value = 11, .name = "pio1_tx3" }, - .{ .value = 12, .name = "pio1_rx0" }, - .{ .value = 13, .name = "pio1_rx1" }, - .{ .value = 14, .name = "pio1_rx2" }, - .{ .value = 15, .name = "pio1_rx3" }, - .{ .value = 16, .name = "pio2_tx0" }, - .{ .value = 17, .name = "pio2_tx1" }, - .{ .value = 18, .name = "pio2_tx2" }, - .{ .value = 19, .name = "pio2_tx3" }, - .{ .value = 20, .name = "pio2_rx0" }, - .{ .value = 21, .name = "pio2_rx1" }, - .{ .value = 22, .name = "pio2_rx2" }, - .{ .value = 23, .name = "pio2_rx3" }, - .{ .value = 24, .name = "spi0_tx" }, - .{ .value = 25, .name = "spi0_rx" }, - .{ .value = 26, .name = "spi1_tx" }, - .{ .value = 27, .name = "spi1_rx" }, - .{ .value = 28, .name = "uart0_tx" }, - .{ .value = 29, .name = "uart0_rx" }, - .{ .value = 30, .name = "uart1_tx" }, - .{ .value = 31, .name = "uart1_rx" }, - .{ .value = 32, .name = "pwm_wrap0" }, - .{ .value = 33, .name = "pwm_wrap1" }, - .{ .value = 34, .name = "pwm_wrap2" }, - .{ .value = 35, .name = "pwm_wrap3" }, - .{ .value = 36, .name = "pwm_wrap4" }, - .{ .value = 37, .name = "pwm_wrap5" }, - .{ .value = 38, .name = "pwm_wrap6" }, - .{ .value = 39, .name = "pwm_wrap7" }, - .{ .value = 40, .name = "pwm_wrap8" }, - .{ .value = 41, .name = "pwm_wrap9" }, - .{ .value = 42, .name = "pwm_wrap10" }, - .{ .value = 43, .name = "pwm_wrap11" }, - .{ .value = 44, .name = "i2c0_tx" }, - .{ .value = 45, .name = "i2c0_rx" }, - .{ .value = 46, .name = "i2c1_tx" }, - .{ .value = 47, .name = "i2c1_rx" }, - .{ .value = 48, .name = "adc" }, - .{ .value = 49, .name = "xip_stream" }, - .{ .value = 50, .name = "xip_qmitx" }, - .{ .value = 51, .name = "xip_qmirx" }, - .{ .value = 52, .name = "hstx" }, - .{ .value = 53, .name = "coresight" }, - .{ .value = 54, .name = "sha256" }, - .{ .value = 59, .name = "timer0" }, - .{ .value = 60, .name = "timer1" }, - .{ .value = 61, .name = "timer2" }, - .{ .value = 62, .name = "timer3" }, - .{ .value = 63, .name = "permanent" }, - }, - } }, + } }, + .{ .add_type_and_apply = .{ + .parent = "types.peripherals.DMA", + .type_name = "Dreq", + .type = .{ .@"enum" = .{ + .bitsize = 6, + .fields = .{ + .{ .value = 0, .name = "pio0_tx0" }, + .{ .value = 1, .name = "pio0_tx1" }, + .{ .value = 2, .name = "pio0_tx2" }, + .{ .value = 3, .name = "pio0_tx3" }, + .{ .value = 4, .name = "pio0_rx0" }, + .{ .value = 5, .name = "pio0_rx1" }, + .{ .value = 6, .name = "pio0_rx2" }, + .{ .value = 7, .name = "pio0_rx3" }, + .{ .value = 8, .name = "pio1_tx0" }, + .{ .value = 9, .name = "pio1_tx1" }, + .{ .value = 10, .name = "pio1_tx2" }, + .{ .value = 11, .name = "pio1_tx3" }, + .{ .value = 12, .name = "pio1_rx0" }, + .{ .value = 13, .name = "pio1_rx1" }, + .{ .value = 14, .name = "pio1_rx2" }, + .{ .value = 15, .name = "pio1_rx3" }, + .{ .value = 16, .name = "pio2_tx0" }, + .{ .value = 17, .name = "pio2_tx1" }, + .{ .value = 18, .name = "pio2_tx2" }, + .{ .value = 19, .name = "pio2_tx3" }, + .{ .value = 20, .name = "pio2_rx0" }, + .{ .value = 21, .name = "pio2_rx1" }, + .{ .value = 22, .name = "pio2_rx2" }, + .{ .value = 23, .name = "pio2_rx3" }, + .{ .value = 24, .name = "spi0_tx" }, + .{ .value = 25, .name = "spi0_rx" }, + .{ .value = 26, .name = "spi1_tx" }, + .{ .value = 27, .name = "spi1_rx" }, + .{ .value = 28, .name = "uart0_tx" }, + .{ .value = 29, .name = "uart0_rx" }, + .{ .value = 30, .name = "uart1_tx" }, + .{ .value = 31, .name = "uart1_rx" }, + .{ .value = 32, .name = "pwm_wrap0" }, + .{ .value = 33, .name = "pwm_wrap1" }, + .{ .value = 34, .name = "pwm_wrap2" }, + .{ .value = 35, .name = "pwm_wrap3" }, + .{ .value = 36, .name = "pwm_wrap4" }, + .{ .value = 37, .name = "pwm_wrap5" }, + .{ .value = 38, .name = "pwm_wrap6" }, + .{ .value = 39, .name = "pwm_wrap7" }, + .{ .value = 40, .name = "pwm_wrap8" }, + .{ .value = 41, .name = "pwm_wrap9" }, + .{ .value = 42, .name = "pwm_wrap10" }, + .{ .value = 43, .name = "pwm_wrap11" }, + .{ .value = 44, .name = "i2c0_tx" }, + .{ .value = 45, .name = "i2c0_rx" }, + .{ .value = 46, .name = "i2c1_tx" }, + .{ .value = 47, .name = "i2c1_rx" }, + .{ .value = 48, .name = "adc" }, + .{ .value = 49, .name = "xip_stream" }, + .{ .value = 50, .name = "xip_qmitx" }, + .{ .value = 51, .name = "xip_qmirx" }, + .{ .value = 52, .name = "hstx" }, + .{ .value = 53, .name = "coresight" }, + .{ .value = 54, .name = "sha256" }, + .{ .value = 59, .name = "timer0" }, + .{ .value = 60, .name = "timer1" }, + .{ .value = 61, .name = "timer2" }, + .{ .value = 62, .name = "timer3" }, + .{ .value = 63, .name = "permanent" }, + }, + } }, + .apply_to = .{ + "types.peripherals.DMA.CH0_CTRL_TRIG.TREQ_SEL", + "types.peripherals.DMA.CH1_CTRL_TRIG.TREQ_SEL", + "types.peripherals.DMA.CH2_CTRL_TRIG.TREQ_SEL", + "types.peripherals.DMA.CH3_CTRL_TRIG.TREQ_SEL", + "types.peripherals.DMA.CH4_CTRL_TRIG.TREQ_SEL", + "types.peripherals.DMA.CH5_CTRL_TRIG.TREQ_SEL", + "types.peripherals.DMA.CH6_CTRL_TRIG.TREQ_SEL", + "types.peripherals.DMA.CH7_CTRL_TRIG.TREQ_SEL", + "types.peripherals.DMA.CH8_CTRL_TRIG.TREQ_SEL", + "types.peripherals.DMA.CH9_CTRL_TRIG.TREQ_SEL", + "types.peripherals.DMA.CH10_CTRL_TRIG.TREQ_SEL", + "types.peripherals.DMA.CH11_CTRL_TRIG.TREQ_SEL", + "types.peripherals.DMA.CH12_CTRL_TRIG.TREQ_SEL", + "types.peripherals.DMA.CH13_CTRL_TRIG.TREQ_SEL", + "types.peripherals.DMA.CH14_CTRL_TRIG.TREQ_SEL", + "types.peripherals.DMA.CH15_CTRL_TRIG.TREQ_SEL", }, - }, - .{ .set_enum_type = .{ .of = "types.peripherals.DMA.CH0_CTRL_TRIG.TREQ_SEL", .to = "types.peripherals.DMA.Dreq" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.DMA.CH1_CTRL_TRIG.TREQ_SEL", .to = "types.peripherals.DMA.Dreq" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.DMA.CH2_CTRL_TRIG.TREQ_SEL", .to = "types.peripherals.DMA.Dreq" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.DMA.CH3_CTRL_TRIG.TREQ_SEL", .to = "types.peripherals.DMA.Dreq" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.DMA.CH4_CTRL_TRIG.TREQ_SEL", .to = "types.peripherals.DMA.Dreq" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.DMA.CH5_CTRL_TRIG.TREQ_SEL", .to = "types.peripherals.DMA.Dreq" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.DMA.CH6_CTRL_TRIG.TREQ_SEL", .to = "types.peripherals.DMA.Dreq" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.DMA.CH7_CTRL_TRIG.TREQ_SEL", .to = "types.peripherals.DMA.Dreq" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.DMA.CH8_CTRL_TRIG.TREQ_SEL", .to = "types.peripherals.DMA.Dreq" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.DMA.CH9_CTRL_TRIG.TREQ_SEL", .to = "types.peripherals.DMA.Dreq" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.DMA.CH10_CTRL_TRIG.TREQ_SEL", .to = "types.peripherals.DMA.Dreq" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.DMA.CH11_CTRL_TRIG.TREQ_SEL", .to = "types.peripherals.DMA.Dreq" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.DMA.CH12_CTRL_TRIG.TREQ_SEL", .to = "types.peripherals.DMA.Dreq" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.DMA.CH13_CTRL_TRIG.TREQ_SEL", .to = "types.peripherals.DMA.Dreq" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.DMA.CH14_CTRL_TRIG.TREQ_SEL", .to = "types.peripherals.DMA.Dreq" } }, - .{ .set_enum_type = .{ .of = "types.peripherals.DMA.CH15_CTRL_TRIG.TREQ_SEL", .to = "types.peripherals.DMA.Dreq" } }, + } }, .{ .add_interrupt = .{ .device_name = "RP2350", .idx = 40, From ca2e0ac6ba59f660f80577edc597b3c53eb37ada Mon Sep 17 00:00:00 2001 From: Piotr Fila Date: Sun, 8 Feb 2026 04:27:53 +0100 Subject: [PATCH 19/23] update regz unit tests --- tools/regz/src/Database.zig | 18 ++++----- tools/regz/src/gen.zig | 78 ++++++++++++++++++++++++++++++++++++- 2 files changed, 85 insertions(+), 11 deletions(-) diff --git a/tools/regz/src/Database.zig b/tools/regz/src/Database.zig index 9c84ab1c0..9dea87de4 100644 --- a/tools/regz/src/Database.zig +++ b/tools/regz/src/Database.zig @@ -2222,8 +2222,8 @@ test "add_type_and_apply patch creates enum and applies to fields" { \\ .{ \\ .add_type_and_apply = .{ \\ .parent = "types.peripherals.TEST_PERIPHERAL", - \\ .@"enum" = .{ - \\ .name = "TestMode", + \\ .type_name = "TestMode", + \\ .type = .{ .@"enum" = .{ \\ .bitsize = 2, \\ .fields = .{ \\ .{ .value = 0x0, .name = "mode_a" }, @@ -2231,7 +2231,7 @@ test "add_type_and_apply patch creates enum and applies to fields" { \\ .{ .value = 0x2, .name = "mode_c" }, \\ .{ .value = 0x3, .name = "mode_d" }, \\ }, - \\ }, + \\ } }, \\ .apply_to = .{ \\ "types.peripherals.TEST_PERIPHERAL.REG0.MODE", \\ "types.peripherals.TEST_PERIPHERAL.REG1.MODE", @@ -2288,14 +2288,14 @@ test "add_type_and_apply patch with empty apply_to list" { \\ .{ \\ .add_type_and_apply = .{ \\ .parent = "types.peripherals.TEST_PERIPHERAL", - \\ .@"enum" = .{ - \\ .name = "UnusedEnum", + \\ .type_name = "UnusedEnum", + \\ .type = .{ .@"enum" = .{ \\ .bitsize = 4, \\ .fields = .{ \\ .{ .value = 0, .name = "value0" }, \\ .{ .value = 1, .name = "value1" }, \\ }, - \\ }, + \\ } }, \\ .apply_to = .{}, \\ }, \\ }, @@ -2331,13 +2331,13 @@ test "add_type_and_apply patch with invalid field reference" { \\ .{ \\ .add_type_and_apply = .{ \\ .parent = "types.peripherals.TEST_PERIPHERAL", - \\ .@"enum" = .{ - \\ .name = "TestEnum", + \\ .type_name = "TestEnum", + \\ .type = .{ .@"enum" = .{ \\ .bitsize = 2, \\ .fields = .{ \\ .{ .value = 0, .name = "value0" }, \\ }, - \\ }, + \\ } }, \\ .apply_to = .{ \\ "types.peripherals.TEST_PERIPHERAL.NONEXISTENT.FIELD", \\ }, diff --git a/tools/regz/src/gen.zig b/tools/regz/src/gen.zig index ab37404f9..3f15bf8be 100644 --- a/tools/regz/src/gen.zig +++ b/tools/regz/src/gen.zig @@ -1817,6 +1817,9 @@ test "gen.peripheral instantiation" { \\ TEST_REGISTER: mmio.Mmio(packed struct(u32) { \\ TEST_FIELD: u1 = 0x1, \\ padding: u31 = 0, + \\ }, .{ + \\ .TEST_FIELD = .read_write, + \\ .padding = .reserved, \\ }), \\}; \\ @@ -1825,8 +1828,6 @@ test "gen.peripheral instantiation" { }, &vfs); } -// TODO: Adapt tests to new Mmio - test "gen.peripherals with a shared type" { var db = try tests.peripherals_with_shared_type(std.testing.allocator); defer db.destroy(); @@ -1903,6 +1904,9 @@ test "gen.peripherals with a shared type" { \\ TEST_REGISTER: mmio.Mmio(packed struct(u32) { \\ TEST_FIELD: u1, \\ padding: u31 = 0, + \\ }, .{ + \\ .TEST_FIELD = .read_write, + \\ .padding = .reserved, \\ }), \\}; \\ @@ -1979,6 +1983,9 @@ test "gen.peripheral with modes" { \\ COMMON_REGISTER: mmio.Mmio(packed struct(u32) { \\ TEST_FIELD: u1, \\ padding: u31 = 0, + \\ }, .{ + \\ .TEST_FIELD = .read_write, + \\ .padding = .reserved, \\ }), \\ }, \\ TEST_MODE2: extern struct { @@ -1988,6 +1995,9 @@ test "gen.peripheral with modes" { \\ COMMON_REGISTER: mmio.Mmio(packed struct(u32) { \\ TEST_FIELD: u1, \\ padding: u31 = 0, + \\ }, .{ + \\ .TEST_FIELD = .read_write, + \\ .padding = .reserved, \\ }), \\ }, \\}; @@ -2141,6 +2151,9 @@ test "gen.field with named enum" { \\ TEST_REGISTER: mmio.Mmio(packed struct(u8) { \\ TEST_FIELD: TEST_ENUM, \\ padding: u4 = 0, + \\ }, .{ + \\ .TEST_FIELD = .read_write, + \\ .padding = .reserved, \\ }), \\}; \\ @@ -2194,6 +2207,9 @@ test "gen.field with named enum and named default" { \\ TEST_REGISTER: mmio.Mmio(packed struct(u8) { \\ TEST_FIELD: TEST_ENUM = .TEST_ENUM_FIELD2, \\ padding: u4 = 0, + \\ }, .{ + \\ .TEST_FIELD = .read_write, + \\ .padding = .reserved, \\ }), \\}; \\ @@ -2247,6 +2263,9 @@ test "gen.field with named enum and unnamed default" { \\ TEST_REGISTER: mmio.Mmio(packed struct(u8) { \\ TEST_FIELD: TEST_ENUM = @enumFromInt(0xA), \\ padding: u4 = 0, + \\ }, .{ + \\ .TEST_FIELD = .read_write, + \\ .padding = .reserved, \\ }), \\}; \\ @@ -2298,6 +2317,9 @@ test "gen.field with anonymous enum" { \\ _, \\ }, \\ padding: u4 = 0, + \\ }, .{ + \\ .TEST_FIELD = .read_write, + \\ .padding = .reserved, \\ }), \\}; \\ @@ -2349,6 +2371,9 @@ test "gen.field with anonymous enum and default" { \\ _, \\ } = .TEST_ENUM_FIELD2, \\ padding: u4 = 0, + \\ }, .{ + \\ .TEST_FIELD = .read_write, + \\ .padding = .reserved, \\ }), \\}; \\ @@ -2863,6 +2888,9 @@ test "gen.register with count and fields" { \\ PORTB: [4]mmio.Mmio(packed struct(u8) { \\ TEST_FIELD: u4, \\ padding: u4 = 0, + \\ }, .{ + \\ .TEST_FIELD = .read_write, + \\ .padding = .reserved, \\ }), \\ /// offset: 0x04 \\ DDRB: u8, @@ -2919,6 +2947,14 @@ test "gen.field with count, width of one, offset, and padding" { \\ TEST_FIELD3: u1, \\ TEST_FIELD4: u1, \\ padding: u1 = 0, + \\ }, .{ + \\ .reserved2 = .reserved, + \\ .TEST_FIELD0 = .read_write, + \\ .TEST_FIELD1 = .read_write, + \\ .TEST_FIELD2 = .read_write, + \\ .TEST_FIELD3 = .read_write, + \\ .TEST_FIELD4 = .read_write, + \\ .padding = .reserved, \\ }), \\}; \\ @@ -2968,6 +3004,11 @@ test "gen.field with count, multi-bit width, offset, and padding" { \\ TEST_FIELD0: u2, \\ TEST_FIELD1: u2, \\ padding: u2 = 0, + \\ }, .{ + \\ .reserved2 = .reserved, + \\ .TEST_FIELD0 = .read_write, + \\ .TEST_FIELD1 = .read_write, + \\ .padding = .reserved, \\ }), \\}; \\ @@ -3093,6 +3134,9 @@ test "gen.peripheral type with register and field" { \\ /// test field \\ TEST_FIELD: u1, \\ padding: u31 = 0, + \\ }, .{ + \\ .TEST_FIELD = .read_write, + \\ .padding = .reserved, \\ }), \\}; \\ @@ -3148,6 +3192,9 @@ test "gen.name collisions in enum name cause them to be anonymous" { \\ TEST_ENUM_FIELD2 = 0x1, \\ _, \\ }, + \\ }, .{ + \\ .TEST_FIELD1 = .read_write, + \\ .TEST_FIELD2 = .read_write, \\ }), \\}; \\ @@ -3198,6 +3245,9 @@ test "gen.pick one enum field in value collisions" { \\ _, \\ }, \\ padding: u4 = 0, + \\ }, .{ + \\ .TEST_FIELD = .read_write, + \\ .padding = .reserved, \\ }), \\}; \\ @@ -3248,6 +3298,9 @@ test "gen.pick one enum field in value collisions" { // \\ _, // \\ }, // \\ padding: u4 = 0, +// \\ }, .{ +// \\ .TEST_FIELD = .read_write, +// \\ .padding = .reserved, // \\ }), // \\}; // \\ @@ -3297,6 +3350,9 @@ test "gen.pick one enum field in value collisions" { // \\ /// test field 1 // \\ TEST_FIELD: u1, // \\ padding: u31 = 0, +// \\ }, .{ +// \\ .TEST_FIELD = .read_write, +// \\ .padding = .reserved, // \\ }), // \\}; // \\ @@ -3350,6 +3406,9 @@ test "gen.nested struct field in a peripheral" { \\ /// test field 1 \\ TEST_FIELD: u1, \\ padding: u31 = 0, + \\ }, .{ + \\ .TEST_FIELD = .read_write, + \\ .padding = .reserved, \\ }), \\ }, \\}; @@ -3402,6 +3461,9 @@ test "gen.nested struct field in a peripheral that has a named type" { \\ /// test field 1 \\ TEST_FIELD: u1, \\ padding: u31 = 0, + \\ }, .{ + \\ .TEST_FIELD = .read_write, + \\ .padding = .reserved, \\ }), \\ }; \\ @@ -3462,6 +3524,9 @@ test "gen.nested struct field in a peripheral with offset" { \\ /// test field 1 \\ TEST_FIELD: u1, \\ padding: u31 = 0, + \\ }, .{ + \\ .TEST_FIELD = .read_write, + \\ .padding = .reserved, \\ }), \\ }, \\}; @@ -3518,6 +3583,9 @@ test "gen.nested struct field in nested struct field" { \\ /// test field 1 \\ TEST_FIELD: u1, \\ padding: u31 = 0, + \\ }, .{ + \\ .TEST_FIELD = .read_write, + \\ .padding = .reserved, \\ }), \\ }, \\ }, @@ -3571,6 +3639,9 @@ test "gen.nested struct field next to register" { \\ /// test field 1 \\ TEST_FIELD: u1, \\ padding: u31 = 0, + \\ }, .{ + \\ .TEST_FIELD = .read_write, + \\ .padding = .reserved, \\ }), \\ }; \\ @@ -3583,6 +3654,9 @@ test "gen.nested struct field next to register" { \\ /// test field 1 \\ TEST_FIELD: u1, \\ padding: u31 = 0, + \\ }, .{ + \\ .TEST_FIELD = .read_write, + \\ .padding = .reserved, \\ }), \\}; \\ From 4b4f48187a73d360c981d391f8b139e19b3f3a30 Mon Sep 17 00:00:00 2001 From: Piotr Fila Date: Sun, 8 Feb 2026 05:13:10 +0100 Subject: [PATCH 20/23] fix sorcerer --- tools/sorcerer/src/RegzWindow.zig | 132 +++++++++++++++--------------- 1 file changed, 67 insertions(+), 65 deletions(-) diff --git a/tools/sorcerer/src/RegzWindow.zig b/tools/sorcerer/src/RegzWindow.zig index 65541201f..0d1ff80a3 100644 --- a/tools/sorcerer/src/RegzWindow.zig +++ b/tools/sorcerer/src/RegzWindow.zig @@ -1059,10 +1059,10 @@ fn get_patch_label(patch: regz.Patch, arena: Allocator) []const u8 { return switch (patch) { .override_arch => |p| std.fmt.allocPrint(arena, "override_arch: {s}", .{p.device_name}) catch "override_arch", .set_device_property => |p| std.fmt.allocPrint(arena, "set_device_property: {s}", .{p.key}) catch "set_device_property", - .add_enum => |p| std.fmt.allocPrint(arena, "add_enum: {s}", .{p.@"enum".name}) catch "add_enum", + .add_type => |p| std.fmt.allocPrint(arena, "add_type: {t} {s}", .{ p.type, p.type_name }) catch "add_type", .set_enum_type => |p| std.fmt.allocPrint(arena, "set_enum_type: {s}", .{p.of}) catch "set_enum_type", .add_interrupt => |p| std.fmt.allocPrint(arena, "add_interrupt: {s}", .{p.name}) catch "add_interrupt", - .add_type_and_apply => |p| std.fmt.allocPrint(arena, "add_type_and_apply: {s}", .{p.@"enum".name}) catch "add_type_and_apply", + .add_type_and_apply => |p| std.fmt.allocPrint(arena, "add_type_and_apply: {t} {s}", .{ p.type, p.type_name }) catch "add_type_and_apply", }; } @@ -1198,7 +1198,7 @@ fn show_patch_details(w: *RegzWindow, arena: Allocator) void { switch (patch) { .override_arch => |p| show_override_arch_widget(p), .set_device_property => |p| show_set_device_property_widget(p), - .add_enum => |p| show_add_enum_widget(p, arena), + .add_type => |p| show_add_type_widget(p, arena), .set_enum_type => |p| show_set_enum_type_widget(p), .add_interrupt => |p| show_add_interrupt_widget(p), .add_type_and_apply => |p| show_add_type_and_apply_widget(p, arena), @@ -1702,80 +1702,82 @@ fn show_set_device_property_widget(p: anytype) void { if (p.description) |desc| labeled_field("Description", desc); } -fn show_add_enum_widget(p: anytype, arena: Allocator) void { +fn show_add_type_widget(p: anytype, arena: Allocator) void { labeled_field("Parent", p.parent); - show_enum_details(p.@"enum", arena); + show_type_details(p, arena); } -fn show_enum_details(e: anytype, arena: Allocator) void { - labeled_field("Name", e.name); - if (e.description) |d| labeled_field("Description", d); - const bitsize_str = std.fmt.allocPrint(arena, "{d}", .{e.bitsize}) catch "?"; +fn show_type_details(p: anytype, arena: Allocator) void { + labeled_field("Name", p.type_name); + if (p.type.@"enum".description) |d| labeled_field("Description", d); + const bitsize_str = std.fmt.allocPrint(arena, "{d}", .{p.type.@"enum".bitsize}) catch "?"; labeled_field("Bit Size", bitsize_str); _ = dvui.spacer(@src(), .{ .min_size_content = .{ .h = 8 } }); - if (e.fields.len > 0) { - _ = dvui.label(@src(), "Fields:", .{}, .{ - .font = .{ .weight = .bold }, - .color_text = dvui.Color.fromHex("FBB829"), - }); + switch (p.type) { + .@"enum" => |e| if (e.fields.len > 0) { + _ = dvui.label(@src(), "Fields:", .{}, .{ + .font = .{ .weight = .bold }, + .color_text = dvui.Color.fromHex("FBB829"), + }); - _ = dvui.spacer(@src(), .{ .min_size_content = .{ .h = 4 } }); + _ = dvui.spacer(@src(), .{ .min_size_content = .{ .h = 4 } }); - // Table for enum fields - const header_style: dvui.GridWidget.CellStyle = .{ - .cell_opts = .{ - .border = .{ .y = 0, .h = 1, .x = 0, .w = 0 }, - }, - }; + // Table for enum fields + const header_style: dvui.GridWidget.CellStyle = .{ + .cell_opts = .{ + .border = .{ .y = 0, .h = 1, .x = 0, .w = 0 }, + }, + }; - // Column widths: Name (120 fixed), Value (60 fixed), Description (proportional -1) - var col_widths: [3]f32 = .{ 0, 0, 0 }; - var grid = dvui.grid(@src(), .{ .col_widths = &col_widths }, .{}, .{ - .expand = .both, - .background = true, - .padding = dvui.Rect.all(4), - }); - defer grid.deinit(); + // Column widths: Name (120 fixed), Value (60 fixed), Description (proportional -1) + var col_widths: [3]f32 = .{ 0, 0, 0 }; + var grid = dvui.grid(@src(), .{ .col_widths = &col_widths }, .{}, .{ + .expand = .both, + .background = true, + .padding = dvui.Rect.all(4), + }); + defer grid.deinit(); - // Layout: fixed 120 for Name, fixed 60 for Value, rest for Description - dvui.columnLayoutProportional(&.{ 120, 60, -1 }, &col_widths, grid.data().contentRect().w); + // Layout: fixed 120 for Name, fixed 60 for Value, rest for Description + dvui.columnLayoutProportional(&.{ 120, 60, -1 }, &col_widths, grid.data().contentRect().w); - // Table headers - dvui.gridHeading(@src(), grid, 0, "Name", .fixed, header_style); - dvui.gridHeading(@src(), grid, 1, "Value", .fixed, header_style); - dvui.gridHeading(@src(), grid, 2, "Description", .fixed, header_style); + // Table headers + dvui.gridHeading(@src(), grid, 0, "Name", .fixed, header_style); + dvui.gridHeading(@src(), grid, 1, "Value", .fixed, header_style); + dvui.gridHeading(@src(), grid, 2, "Description", .fixed, header_style); - // Table rows - for (e.fields, 0..) |field, row_num| { - var cell_num: dvui.GridWidget.Cell = .colRow(0, row_num); + // Table rows + for (e.fields, 0..) |field, row_num| { + var cell_num: dvui.GridWidget.Cell = .colRow(0, row_num); - // Name column - { - defer cell_num.col_num += 1; - var cell = grid.bodyCell(@src(), cell_num, .{}); - defer cell.deinit(); - dvui.labelNoFmt(@src(), field.name, .{}, .{}); - } + // Name column + { + defer cell_num.col_num += 1; + var cell = grid.bodyCell(@src(), cell_num, .{}); + defer cell.deinit(); + dvui.labelNoFmt(@src(), field.name, .{}, .{}); + } - // Value column - { - defer cell_num.col_num += 1; - var cell = grid.bodyCell(@src(), cell_num, .{}); - defer cell.deinit(); - const value_str = std.fmt.allocPrint(arena, "{d}", .{field.value}) catch "?"; - dvui.labelNoFmt(@src(), value_str, .{}, .{}); - } + // Value column + { + defer cell_num.col_num += 1; + var cell = grid.bodyCell(@src(), cell_num, .{}); + defer cell.deinit(); + const value_str = std.fmt.allocPrint(arena, "{d}", .{field.value}) catch "?"; + dvui.labelNoFmt(@src(), value_str, .{}, .{}); + } - // Description column - { - defer cell_num.col_num += 1; - var cell = grid.bodyCell(@src(), cell_num, .{}); - defer cell.deinit(); - dvui.labelNoFmt(@src(), field.description orelse "", .{}, .{}); + // Description column + { + defer cell_num.col_num += 1; + var cell = grid.bodyCell(@src(), cell_num, .{}); + defer cell.deinit(); + dvui.labelNoFmt(@src(), field.description orelse "", .{}, .{}); + } } - } + }, } } @@ -1795,7 +1797,7 @@ fn show_add_interrupt_widget(p: anytype) void { fn show_add_type_and_apply_widget(p: anytype, arena: Allocator) void { labeled_field("Parent", p.parent); - show_enum_details(p.@"enum", arena); + show_type_details(p, arena); _ = dvui.spacer(@src(), .{ .min_size_content = .{ .h = 8 } }); @@ -2302,7 +2304,7 @@ fn create_add_type_and_apply_patch( // Get the EnumField type from the Patch type using type introspection const AddEnumAndApply = std.meta.TagPayload(regz.Patch, .add_type_and_apply); - const EnumType = @TypeOf(@as(AddEnumAndApply, undefined).@"enum"); + const EnumType = @TypeOf(@as(AddEnumAndApply, undefined).type.@"enum"); const EnumFieldType = std.meta.Child(@TypeOf(@as(EnumType, undefined).fields)); // Convert fields (note: Database.EnumField.value is u64, Patch.EnumField.value is u32) @@ -2328,12 +2330,12 @@ fn create_add_type_and_apply_patch( return .{ .add_type_and_apply = .{ .parent = parent, - .@"enum" = .{ - .name = try alloc.dupe(u8, enum_name), + .type_name = try alloc.dupe(u8, enum_name), + .type = .{ .@"enum" = .{ .description = if (group.description) |d| try alloc.dupe(u8, d) else null, .bitsize = group.size_bits, .fields = fields, - }, + } }, .apply_to = apply_to, }, }; From d44cfcdb12a9affec5762e4dbe8acbc382c09495 Mon Sep 17 00:00:00 2001 From: Piotr Fila Date: Sun, 8 Feb 2026 05:13:24 +0100 Subject: [PATCH 21/23] fix register usage --- port/nordic/nrf5x/patches/nrf528xx.zon | 4 ++-- port/nxp/mcx/src/mcxn947/hal/flexcomm/LP_UART.zig | 2 +- port/stmicro/stm32/src/hals/common/i2c_v2.zig | 2 +- port/texasinstruments/msp430/src/hal/watchdog.zig | 10 +++++----- port/wch/ch32v/src/cpus/qingkev4-rv32imacf.zig | 1 - 5 files changed, 9 insertions(+), 10 deletions(-) diff --git a/port/nordic/nrf5x/patches/nrf528xx.zon b/port/nordic/nrf5x/patches/nrf528xx.zon index 6a59e60a0..3f2b7e6db 100644 --- a/port/nordic/nrf5x/patches/nrf528xx.zon +++ b/port/nordic/nrf5x/patches/nrf528xx.zon @@ -11,7 +11,7 @@ }, } }, .apply_to = .{ - "types.peripherals.GPIO.PIN_CNF.PULL", + "types.peripherals.P0.PIN_CNF.PULL", }, } }, .{ .add_type_and_apply = .{ @@ -31,7 +31,7 @@ }, } }, .apply_to = .{ - "types.peripherals.GPIO.PIN_CNF.DRIVE", + "types.peripherals.P0.PIN_CNF.DRIVE", }, } }, } diff --git a/port/nxp/mcx/src/mcxn947/hal/flexcomm/LP_UART.zig b/port/nxp/mcx/src/mcxn947/hal/flexcomm/LP_UART.zig index 592eac7b9..346299d02 100644 --- a/port/nxp/mcx/src/mcxn947/hal/flexcomm/LP_UART.zig +++ b/port/nxp/mcx/src/mcxn947/hal/flexcomm/LP_UART.zig @@ -90,7 +90,7 @@ pub const LP_UART = enum(u4) { stat.MSBF = if (config.bit_order == .lsb) .LSB_FIRST else .MSB_FIRST; stat.RXINV = if (config.rx_invert) .INVERTED else .NOT_INVERTED; - regs.STAT.modify(stat); + regs.STAT.write(stat); uart.set_enabled(config.enable_send, config.enable_receive); diff --git a/port/stmicro/stm32/src/hals/common/i2c_v2.zig b/port/stmicro/stm32/src/hals/common/i2c_v2.zig index 79951c469..130ba73cd 100644 --- a/port/stmicro/stm32/src/hals/common/i2c_v2.zig +++ b/port/stmicro/stm32/src/hals/common/i2c_v2.zig @@ -124,7 +124,7 @@ const I2C = struct { regs.CR1.modify(.{ .PE = 0 }); - regs.TIMINGR.modify(i2c.timingr); + regs.TIMINGR.write(i2c.timingr); regs.CR1.modify(.{ .PE = 1 }); } diff --git a/port/texasinstruments/msp430/src/hal/watchdog.zig b/port/texasinstruments/msp430/src/hal/watchdog.zig index 10d05c842..cc407b133 100644 --- a/port/texasinstruments/msp430/src/hal/watchdog.zig +++ b/port/texasinstruments/msp430/src/hal/watchdog.zig @@ -2,9 +2,9 @@ const microzig = @import("microzig"); const WDTCTL = µzig.chip.peripherals.Watchdog_Timer.WDTCTL; pub fn disable() void { - WDTCTL.modify(.{ - .WDTHOLD = 1, - // This needs to be patched - .padding = 0x5A, - }); + var reg = WDTCTL.read(); + reg.WDTHOLD = 1; + // TODO: Allow adding fields in patches + reg.padding = 0x5A; + WDTCTL.write(reg); } diff --git a/port/wch/ch32v/src/cpus/qingkev4-rv32imacf.zig b/port/wch/ch32v/src/cpus/qingkev4-rv32imacf.zig index e64dbbb1b..fe4228a31 100644 --- a/port/wch/ch32v/src/cpus/qingkev4-rv32imacf.zig +++ b/port/wch/ch32v/src/cpus/qingkev4-rv32imacf.zig @@ -204,7 +204,6 @@ pub inline fn system_init(comptime chip: anytype) void { // RCC->CFGR0 &= (uint32_t)0xF0FF0000; RCC.CFGR0.modify(.{ .SW = 0, - .SWS = 0, .HPRE = 0, .PPRE1 = 0, .PPRE2 = 0, From 4d5b7ac68ace5a3b87992cc1b36dddbc4edd0263 Mon Sep 17 00:00:00 2001 From: Piotr Fila Date: Sun, 8 Feb 2026 05:30:28 +0100 Subject: [PATCH 22/23] add write-1-to-clear access type to regz --- core/src/mmio.zig | 1 + port/wch/ch32v/src/cpus/qingkev4-rv32imac.zig | 1 - tools/regz/src/Database.zig | 2 ++ tools/regz/src/gen.zig | 1 + tools/regz/src/svd.zig | 2 ++ 5 files changed, 6 insertions(+), 1 deletion(-) diff --git a/core/src/mmio.zig b/core/src/mmio.zig index 0272f5ce4..898eb12df 100644 --- a/core/src/mmio.zig +++ b/core/src/mmio.zig @@ -52,6 +52,7 @@ pub const Access = struct { pub const read_only: @This() = .{ .read = .normal, .write = .ignored }; pub const read_write: @This() = .{ .read = .normal, .write = .normal }; pub const write_only: @This() = .{ .read = .garbage, .write = .normal }; + pub const rw1z: @This() = .{ .read = .normal, .write = .clear_mask }; pub const reserved: @This() = .{ .read = .garbage, .write = .ignored }; }; diff --git a/port/wch/ch32v/src/cpus/qingkev4-rv32imac.zig b/port/wch/ch32v/src/cpus/qingkev4-rv32imac.zig index c0e246b8b..aceba604e 100644 --- a/port/wch/ch32v/src/cpus/qingkev4-rv32imac.zig +++ b/port/wch/ch32v/src/cpus/qingkev4-rv32imac.zig @@ -164,7 +164,6 @@ pub inline fn system_init(comptime chip: anytype) void { // RCC->CFGR0 &= (uint32_t)0xF0FF0000; RCC.CFGR0.modify(.{ .SW = 0, - .SWS = 0, .HPRE = 0, .PPRE1 = 0, .PPRE2 = 0, diff --git a/tools/regz/src/Database.zig b/tools/regz/src/Database.zig index 9dea87de4..4108f35a5 100644 --- a/tools/regz/src/Database.zig +++ b/tools/regz/src/Database.zig @@ -357,6 +357,8 @@ pub const Access = enum { write_only, write_once, read_write_once, + // read normal, write 1 to clear + rw1z, pub const BaseType = []const u8; pub const default = .read_write; diff --git a/tools/regz/src/gen.zig b/tools/regz/src/gen.zig index 3f15bf8be..650fc0ed3 100644 --- a/tools/regz/src/gen.zig +++ b/tools/regz/src/gen.zig @@ -1364,6 +1364,7 @@ fn write_fields_and_access( .read_write_once, .read_write => "read_write", .read_only => "read_only", .write_only, .write_once => "write_only", + .rw1z => "rw1z", }; try writer.print(".{f} = .{s},\n", .{ std.zig.fmtId(data.name), access_str }); }, diff --git a/tools/regz/src/svd.zig b/tools/regz/src/svd.zig index 050b691f0..2e3704dc3 100644 --- a/tools/regz/src/svd.zig +++ b/tools/regz/src/svd.zig @@ -1012,6 +1012,8 @@ fn parse_access(str: []const u8) !Access { Access.write_once else if (std.ascii.eqlIgnoreCase("read-writeOnce", str)) Access.read_write_once + else if (std.ascii.eqlIgnoreCase("read/clear", str)) + Access.rw1z else blk: { log.warn("invalid access type: '{s}'", .{str}); break :blk error.UnknownAccessType; From be397288684e5a99100ead0cf4b4bc10b9b7426a Mon Sep 17 00:00:00 2001 From: Piotr Fila Date: Sun, 8 Feb 2026 06:27:45 +0100 Subject: [PATCH 23/23] simplify access handling --- core/src/mmio.zig | 11 +++-- tools/regz/src/Database.zig | 62 ++++++++++++++++++++------ tools/regz/src/atdf.zig | 21 +++------ tools/regz/src/gen.zig | 88 ++++++++++++++++++------------------- tools/regz/src/svd.zig | 43 +++--------------- tools/regz/src/targetdb.zig | 16 +------ 6 files changed, 113 insertions(+), 128 deletions(-) diff --git a/core/src/mmio.zig b/core/src/mmio.zig index 898eb12df..a6162a189 100644 --- a/core/src/mmio.zig +++ b/core/src/mmio.zig @@ -49,10 +49,13 @@ pub const Access = struct { read: Read, write: Write, - pub const read_only: @This() = .{ .read = .normal, .write = .ignored }; - pub const read_write: @This() = .{ .read = .normal, .write = .normal }; - pub const write_only: @This() = .{ .read = .garbage, .write = .normal }; - pub const rw1z: @This() = .{ .read = .normal, .write = .clear_mask }; + // Mapping of svd types + pub const @"read-only": @This() = .{ .read = .normal, .write = .ignored }; + pub const @"read-write": @This() = .{ .read = .normal, .write = .normal }; + pub const @"read-write-once": @This() = .@"read-write"; + pub const @"write-only": @This() = .{ .read = .garbage, .write = .normal }; + pub const @"write-once": @This() = .@"write-only"; + pub const @"read/clear": @This() = .{ .read = .normal, .write = .clear_mask }; pub const reserved: @This() = .{ .read = .garbage, .write = .ignored }; }; diff --git a/tools/regz/src/Database.zig b/tools/regz/src/Database.zig index 4108f35a5..033b08375 100644 --- a/tools/regz/src/Database.zig +++ b/tools/regz/src/Database.zig @@ -352,22 +352,58 @@ pub const DeviceProperty = struct { // not sure how to communicate the *_once values in generated code // besides adding it to documentation comments pub const Access = enum { - read_write, - read_only, - write_only, - write_once, - read_write_once, + @"read-write", + @"read-only", + @"write-only", + @"write-once", + @"read-write-once", // read normal, write 1 to clear - rw1z, + @"read/clear", pub const BaseType = []const u8; - pub const default = .read_write; + pub const default: @This() = .@"read-write"; - pub fn to_string(access: Access) []const u8 { - return inline for (@typeInfo(Access).@"enum".fields) |field| { - if (@field(Access, field.name) == access) - break field.name; - } else unreachable; + pub fn try_parse(str: []const u8) ?@This() { + inline for (@typeInfo(Access).@"enum".fields) |field| { + if (std.ascii.eqlIgnoreCase(field.name, str)) + return @field(@This(), field.name); + } + return null; + } + + pub fn parse(str: []const u8) @This() { + return try_parse(str) orelse { + const fmt = comptime blk: { + var ret: []const u8 = "Failed to parse access string '{s}', it must be one of "; + for (@typeInfo(Access).@"enum".fields) |field| + ret = ret ++ "'" ++ field.name ++ "', "; + break :blk ret ++ "defaulting to '{t}'"; + }; + log.warn(fmt, .{ str, default }); + return default; + }; + } + + pub fn try_parse_short(str: []const u8) ?@This() { + return if (std.mem.eql(u8, str, "RW") or std.mem.eql(u8, str, "R/W")) + .@"read-write" + else if (std.mem.eql(u8, str, "R")) + .@"read-only" + else if (std.mem.eql(u8, str, "W")) + .@"write-only" + else + null; + } + + pub fn parse_short(str: []const u8) @This() { + return try_parse_short(str) orelse { + log.info("unrecognized access string: '{s}', expected one of 'RW', 'R/W', 'R', 'W'", .{str}); + return .default; + }; + } + + pub fn to_string(self: @This()) []const u8 { + return @tagName(self); } }; @@ -1713,7 +1749,7 @@ pub const CreateRegisterOptions = struct { size_bits: u64, /// count if there is an array count: ?u64 = null, - access: Access = .read_write, + access: Access = .default, reset_mask: ?u64 = null, reset_value: ?u64 = null, }; diff --git a/tools/regz/src/atdf.zig b/tools/regz/src/atdf.zig index 70c140570..5f589e459 100644 --- a/tools/regz/src/atdf.zig +++ b/tools/regz/src/atdf.zig @@ -602,9 +602,9 @@ fn load_register( else null, .access = if (node.get_attribute("rw")) |access_str| - try access_from_string(access_str) + Database.Access.try_parse_short(access_str) orelse return error.InvalidAccessStr else - .read_write, + .default, }); if (node.get_attribute("modes")) |modes| { @@ -673,7 +673,7 @@ fn load_field(ctx: *Context, node: xml.Node, peripheral_struct_id: StructID, par // FIXME: field specific r/w //if (node.get_attribute("rw")) |access_str| blk: { - // const access = access_from_string(access_str) catch break :blk; + // const access = .try_parse_short(access_str) catch break :blk; // switch (access) { // .read_only, .write_only => try db.attrs.access.put( // db.gpa, @@ -715,7 +715,7 @@ fn load_field(ctx: *Context, node: xml.Node, peripheral_struct_id: StructID, par // FIXME: field based access //if (node.get_attribute("rw")) |access_str| blk: { - // const access = access_from_string(access_str) catch break :blk; + // const access = .try_parse_short(access_str) catch break :blk; // switch (access) { // .read_only, .write_only => try db.attrs.access.put( // db.gpa, @@ -760,17 +760,6 @@ fn load_field(ctx: *Context, node: xml.Node, peripheral_struct_id: StructID, par } } -fn access_from_string(str: []const u8) !Database.Access { - return if (std.mem.eql(u8, "RW", str)) - .read_write - else if (std.mem.eql(u8, "R", str)) - .read_only - else if (std.mem.eql(u8, "W", str)) - .write_only - else - error.InvalidAccessStr; -} - fn load_enum( ctx: *Context, module_node: xml.Node, @@ -1174,7 +1163,7 @@ test "atdf.register with bitfields and enum" { const ctrla = try db.get_register_by_name(allocator, struct_id, "CTRLA"); // access is read-write, so its entry is omitted (we assume read-write by default) - try expectEqual(.read_write, ctrla.access); + try expectEqual(.@"read-write", ctrla.access); // check name and description try expectEqualStrings("CTRLA", ctrla.name); diff --git a/tools/regz/src/gen.zig b/tools/regz/src/gen.zig index 650fc0ed3..5c40ac147 100644 --- a/tools/regz/src/gen.zig +++ b/tools/regz/src/gen.zig @@ -1360,13 +1360,13 @@ fn write_fields_and_access( for (access.items) |it| switch (it) { .normal => |data| { - const access_str = switch (data.access orelse .read_write) { - .read_write_once, .read_write => "read_write", - .read_only => "read_only", - .write_only, .write_once => "write_only", - .rw1z => "rw1z", - }; - try writer.print(".{f} = .{s},\n", .{ std.zig.fmtId(data.name), access_str }); + try writer.print( + ".{f} = .{f},\n", + .{ + std.zig.fmtId(data.name), + std.zig.fmtId(@tagName(data.access orelse Database.Access.default)), + }, + ); }, .reserved => |num| try writer.print(".reserved{} = .reserved,\n", .{num}), .padding => try writer.writeAll(".padding = .reserved,\n"), @@ -1506,7 +1506,7 @@ test "gen.StructFieldIterator.single register" { .name = "TEST_REGISTER", .size_bits = 32, .offset_bytes = 0, - .access = .read_write, + .access = .default, .reset_mask = 0xFF, .reset_value = 0xAA, .count = null, @@ -1533,7 +1533,7 @@ test "gen.StructFieldIterator.two registers perfect overlap" { .name = "TEST_REGISTER1", .size_bits = 32, .offset_bytes = 0, - .access = .read_write, + .access = .@"read-write", .reset_mask = 0xFF, .reset_value = 0xAA, .count = null, @@ -1545,7 +1545,7 @@ test "gen.StructFieldIterator.two registers perfect overlap" { .name = "TEST_REGISTER2", .size_bits = 32, .offset_bytes = 0, - .access = .read_write, + .access = .@"read-write", .reset_mask = 0xFF, .reset_value = 0xAA, .count = null, @@ -1574,7 +1574,7 @@ test "gen.StructFieldIterator.two registers overlap but one is smaller" { .name = "TEST_REGISTER1", .size_bits = 32, .offset_bytes = 0, - .access = .read_write, + .access = .default, .reset_mask = 0xFF, .reset_value = 0xAA, .count = null, @@ -1586,7 +1586,7 @@ test "gen.StructFieldIterator.two registers overlap but one is smaller" { .name = "TEST_REGISTER2", .size_bits = 16, .offset_bytes = 0, - .access = .read_write, + .access = .default, .reset_mask = 0xFF, .reset_value = 0xAA, .count = null, @@ -1615,7 +1615,7 @@ test "gen.StructFieldIterator.two registers overlap with different offsets" { .name = "TEST_REGISTER1", .size_bits = 32, .offset_bytes = 0, - .access = .read_write, + .access = .default, .reset_mask = 0xFF, .reset_value = 0xAA, .count = null, @@ -1627,7 +1627,7 @@ test "gen.StructFieldIterator.two registers overlap with different offsets" { .name = "TEST_REGISTER2", .size_bits = 16, .offset_bytes = 2, - .access = .read_write, + .access = .default, .reset_mask = 0xFF, .reset_value = 0xAA, .count = null, @@ -1678,7 +1678,7 @@ test "gen.StructFieldIterator.one nested struct field and a register" { .name = "TEST_REGISTER", .size_bits = 32, .offset_bytes = 0, - .access = .read_write, + .access = .default, .reset_mask = 0xFF, .reset_value = 0xAA, .count = null, @@ -1819,7 +1819,7 @@ test "gen.peripheral instantiation" { \\ TEST_FIELD: u1 = 0x1, \\ padding: u31 = 0, \\ }, .{ - \\ .TEST_FIELD = .read_write, + \\ .TEST_FIELD = .@"read-write", \\ .padding = .reserved, \\ }), \\}; @@ -1906,7 +1906,7 @@ test "gen.peripherals with a shared type" { \\ TEST_FIELD: u1, \\ padding: u31 = 0, \\ }, .{ - \\ .TEST_FIELD = .read_write, + \\ .TEST_FIELD = .@"read-write", \\ .padding = .reserved, \\ }), \\}; @@ -1985,7 +1985,7 @@ test "gen.peripheral with modes" { \\ TEST_FIELD: u1, \\ padding: u31 = 0, \\ }, .{ - \\ .TEST_FIELD = .read_write, + \\ .TEST_FIELD = .@"read-write", \\ .padding = .reserved, \\ }), \\ }, @@ -1997,7 +1997,7 @@ test "gen.peripheral with modes" { \\ TEST_FIELD: u1, \\ padding: u31 = 0, \\ }, .{ - \\ .TEST_FIELD = .read_write, + \\ .TEST_FIELD = .@"read-write", \\ .padding = .reserved, \\ }), \\ }, @@ -2153,7 +2153,7 @@ test "gen.field with named enum" { \\ TEST_FIELD: TEST_ENUM, \\ padding: u4 = 0, \\ }, .{ - \\ .TEST_FIELD = .read_write, + \\ .TEST_FIELD = .@"read-write", \\ .padding = .reserved, \\ }), \\}; @@ -2209,7 +2209,7 @@ test "gen.field with named enum and named default" { \\ TEST_FIELD: TEST_ENUM = .TEST_ENUM_FIELD2, \\ padding: u4 = 0, \\ }, .{ - \\ .TEST_FIELD = .read_write, + \\ .TEST_FIELD = .@"read-write", \\ .padding = .reserved, \\ }), \\}; @@ -2265,7 +2265,7 @@ test "gen.field with named enum and unnamed default" { \\ TEST_FIELD: TEST_ENUM = @enumFromInt(0xA), \\ padding: u4 = 0, \\ }, .{ - \\ .TEST_FIELD = .read_write, + \\ .TEST_FIELD = .@"read-write", \\ .padding = .reserved, \\ }), \\}; @@ -2319,7 +2319,7 @@ test "gen.field with anonymous enum" { \\ }, \\ padding: u4 = 0, \\ }, .{ - \\ .TEST_FIELD = .read_write, + \\ .TEST_FIELD = .@"read-write", \\ .padding = .reserved, \\ }), \\}; @@ -2373,7 +2373,7 @@ test "gen.field with anonymous enum and default" { \\ } = .TEST_ENUM_FIELD2, \\ padding: u4 = 0, \\ }, .{ - \\ .TEST_FIELD = .read_write, + \\ .TEST_FIELD = .@"read-write", \\ .padding = .reserved, \\ }), \\}; @@ -2890,7 +2890,7 @@ test "gen.register with count and fields" { \\ TEST_FIELD: u4, \\ padding: u4 = 0, \\ }, .{ - \\ .TEST_FIELD = .read_write, + \\ .TEST_FIELD = .@"read-write", \\ .padding = .reserved, \\ }), \\ /// offset: 0x04 @@ -2950,11 +2950,11 @@ test "gen.field with count, width of one, offset, and padding" { \\ padding: u1 = 0, \\ }, .{ \\ .reserved2 = .reserved, - \\ .TEST_FIELD0 = .read_write, - \\ .TEST_FIELD1 = .read_write, - \\ .TEST_FIELD2 = .read_write, - \\ .TEST_FIELD3 = .read_write, - \\ .TEST_FIELD4 = .read_write, + \\ .TEST_FIELD0 = .@"read-write", + \\ .TEST_FIELD1 = .@"read-write", + \\ .TEST_FIELD2 = .@"read-write", + \\ .TEST_FIELD3 = .@"read-write", + \\ .TEST_FIELD4 = .@"read-write", \\ .padding = .reserved, \\ }), \\}; @@ -3007,8 +3007,8 @@ test "gen.field with count, multi-bit width, offset, and padding" { \\ padding: u2 = 0, \\ }, .{ \\ .reserved2 = .reserved, - \\ .TEST_FIELD0 = .read_write, - \\ .TEST_FIELD1 = .read_write, + \\ .TEST_FIELD0 = .@"read-write", + \\ .TEST_FIELD1 = .@"read-write", \\ .padding = .reserved, \\ }), \\}; @@ -3136,7 +3136,7 @@ test "gen.peripheral type with register and field" { \\ TEST_FIELD: u1, \\ padding: u31 = 0, \\ }, .{ - \\ .TEST_FIELD = .read_write, + \\ .TEST_FIELD = .@"read-write", \\ .padding = .reserved, \\ }), \\}; @@ -3194,8 +3194,8 @@ test "gen.name collisions in enum name cause them to be anonymous" { \\ _, \\ }, \\ }, .{ - \\ .TEST_FIELD1 = .read_write, - \\ .TEST_FIELD2 = .read_write, + \\ .TEST_FIELD1 = .@"read-write", + \\ .TEST_FIELD2 = .@"read-write", \\ }), \\}; \\ @@ -3247,7 +3247,7 @@ test "gen.pick one enum field in value collisions" { \\ }, \\ padding: u4 = 0, \\ }, .{ - \\ .TEST_FIELD = .read_write, + \\ .TEST_FIELD = .@"read-write", \\ .padding = .reserved, \\ }), \\}; @@ -3300,7 +3300,7 @@ test "gen.pick one enum field in value collisions" { // \\ }, // \\ padding: u4 = 0, // \\ }, .{ -// \\ .TEST_FIELD = .read_write, +// \\ .TEST_FIELD = .@"read-write", // \\ .padding = .reserved, // \\ }), // \\}; @@ -3352,7 +3352,7 @@ test "gen.pick one enum field in value collisions" { // \\ TEST_FIELD: u1, // \\ padding: u31 = 0, // \\ }, .{ -// \\ .TEST_FIELD = .read_write, +// \\ .TEST_FIELD = .@"read-write", // \\ .padding = .reserved, // \\ }), // \\}; @@ -3408,7 +3408,7 @@ test "gen.nested struct field in a peripheral" { \\ TEST_FIELD: u1, \\ padding: u31 = 0, \\ }, .{ - \\ .TEST_FIELD = .read_write, + \\ .TEST_FIELD = .@"read-write", \\ .padding = .reserved, \\ }), \\ }, @@ -3463,7 +3463,7 @@ test "gen.nested struct field in a peripheral that has a named type" { \\ TEST_FIELD: u1, \\ padding: u31 = 0, \\ }, .{ - \\ .TEST_FIELD = .read_write, + \\ .TEST_FIELD = .@"read-write", \\ .padding = .reserved, \\ }), \\ }; @@ -3526,7 +3526,7 @@ test "gen.nested struct field in a peripheral with offset" { \\ TEST_FIELD: u1, \\ padding: u31 = 0, \\ }, .{ - \\ .TEST_FIELD = .read_write, + \\ .TEST_FIELD = .@"read-write", \\ .padding = .reserved, \\ }), \\ }, @@ -3585,7 +3585,7 @@ test "gen.nested struct field in nested struct field" { \\ TEST_FIELD: u1, \\ padding: u31 = 0, \\ }, .{ - \\ .TEST_FIELD = .read_write, + \\ .TEST_FIELD = .@"read-write", \\ .padding = .reserved, \\ }), \\ }, @@ -3641,7 +3641,7 @@ test "gen.nested struct field next to register" { \\ TEST_FIELD: u1, \\ padding: u31 = 0, \\ }, .{ - \\ .TEST_FIELD = .read_write, + \\ .TEST_FIELD = .@"read-write", \\ .padding = .reserved, \\ }), \\ }; @@ -3656,7 +3656,7 @@ test "gen.nested struct field next to register" { \\ TEST_FIELD: u1, \\ padding: u31 = 0, \\ }, .{ - \\ .TEST_FIELD = .read_write, + \\ .TEST_FIELD = .@"read-write", \\ .padding = .reserved, \\ }), \\}; diff --git a/tools/regz/src/svd.zig b/tools/regz/src/svd.zig index 2e3704dc3..2ec6ede3c 100644 --- a/tools/regz/src/svd.zig +++ b/tools/regz/src/svd.zig @@ -481,7 +481,7 @@ fn load_register_with_dim_element_group(ctx: *Context, node: xml.Node, parent: S }, .size_bits = size, .count = if (dim_elements.resolve() == .array) dim_elements.dim else null, - .access = register_props.access orelse .read_write, + .access = register_props.access orelse .default, .reset_mask = register_props.reset_mask, .reset_value = register_props.reset_value, }); @@ -514,7 +514,7 @@ fn load_single_register(ctx: *Context, node: xml.Node, parent: StructID) !void { return error.MissingRegisterOffset, .size_bits = size, .count = null, - .access = register_props.access orelse .read_write, + .access = register_props.access orelse .default, .reset_mask = register_props.reset_mask, .reset_value = register_props.reset_value, }); @@ -550,10 +550,7 @@ fn load_field(ctx: *Context, node: xml.Node, register_id: RegisterID, props: *co null; const access = if (node.get_value("access")) |access_str| - parse_access(access_str) catch blk: { - log.warn("Failed to parse access string '{s}', it must be one of 'read-value', 'write-only', 'read-write', 'writeOnce', or 'read-writeOnce', defaulting to 'read-write'", .{access_str}); - break :blk .read_write; - } + Access.parse(access_str) else props.access; @@ -978,12 +975,7 @@ const RegisterProperties = struct { else null, .access = if (node.get_value("access")) |access_str| - parse_access(access_str) catch blk: { - log.warn("Failed to parse access string '{s}', it must be one of 'read-only'," ++ - "'write-only', 'write', 'read-write', 'writeOnce', or 'read-writeOnce', " ++ - "defaulting to 'read-write'", .{access_str}); - break :blk .read_write; - } + Access.parse(access_str) else null, .protection = null, @@ -999,27 +991,6 @@ const RegisterProperties = struct { } }; -fn parse_access(str: []const u8) !Access { - return if (std.ascii.eqlIgnoreCase("read-only", str)) - Access.read_only - else if (std.ascii.eqlIgnoreCase("write-only", str)) - Access.write_only - else if (std.ascii.eqlIgnoreCase("write", str)) - Access.write_only - else if (std.ascii.eqlIgnoreCase("read-write", str)) - Access.read_write - else if (std.ascii.eqlIgnoreCase("writeOnce", str)) - Access.write_once - else if (std.ascii.eqlIgnoreCase("read-writeOnce", str)) - Access.read_write_once - else if (std.ascii.eqlIgnoreCase("read/clear", str)) - Access.rw1z - else blk: { - log.warn("invalid access type: '{s}'", .{str}); - break :blk error.UnknownAccessType; - }; -} - test "svd.device register properties" { const text = \\ @@ -1059,7 +1030,7 @@ test "svd.device register properties" { const register = try db.get_register_by_name(arena.allocator(), struct_id, "TEST_REGISTER"); try expectEqual(32, register.size_bits); - try expectEqual(.read_only, register.access); + try expectEqual(.@"read-only", register.access); try expectEqual(0, register.reset_value); try expectEqual(0xFFFFFFFF, register.reset_mask); } @@ -1105,7 +1076,7 @@ test "svd.peripheral register properties" { const register = try db.get_register_by_name(arena.allocator(), struct_id, "TEST_REGISTER"); try expectEqual(16, register.size_bits); - try expectEqual(.write_only, register.access); + try expectEqual(.@"write-only", register.access); try expectEqual(1, register.reset_value); try expectEqual(0xFFFF, register.reset_mask); } @@ -1154,7 +1125,7 @@ test "svd.register register properties" { const register = try db.get_register_by_name(arena.allocator(), struct_id, "TEST_REGISTER"); try expectEqual(8, register.size_bits); - try expectEqual(.read_write, register.access); + try expectEqual(.@"read-write", register.access); try expectEqual(2, register.reset_value); try expectEqual(0xFF, register.reset_mask); } diff --git a/tools/regz/src/targetdb.zig b/tools/regz/src/targetdb.zig index ade6d1796..6fdf0bbe5 100644 --- a/tools/regz/src/targetdb.zig +++ b/tools/regz/src/targetdb.zig @@ -277,21 +277,7 @@ fn load_bitfield(db: *Database, register_id: RegisterID, node: xml.Node) !void { const end_bit = try std.fmt.parseInt(u8, end_str, 0); const access_str = node.get_attribute("rwaccess"); - const access: Database.Access = if (access_str) |str| - if (std.mem.eql(u8, str, "RW")) - .read_write - else if (std.mem.eql(u8, str, "R/W")) - .read_write - else if (std.mem.eql(u8, str, "R")) - .read_only - else if (std.mem.eql(u8, str, "W")) - .write_only - else blk: { - log.info("unrecognized access string: '{s}'", .{str}); - break :blk .read_write; - } - else - .read_write; + const access: Database.Access = if (access_str) |str| .parse_short(str) else .default; const enum_id: ?EnumID = if (node.find_child(&.{"bitenum"}) != null) blk: { const enum_id = try db.create_enum(null, .{