sylveos

Toy Operating System
Log | Files | Refs

commit c77975b17de56cc8e316243c27d98880f2921cc6
parent c8030e031cbd861761a7b54caea213fb3b2fe2f6
Author: Sylvia Ivory <git@sivory.net>
Date:   Thu, 15 Jan 2026 21:43:27 -0800

Remove util.zig

Diffstat:
Mbuild.zig | 21+++++++++++++++++----
Afake-pi/pi.zig | 21+++++++++++++++++++++
Mpi/mem.zig | 6+++---
Api/root.zig | 1+
Msrc/devices/gpio.zig | 10+++++-----
Msrc/devices/timer.zig | 11+++--------
Msrc/labs/2-gpio.zig | 28++++++++++++++++++----------
Dsrc/util.zig | 11-----------
8 files changed, 68 insertions(+), 41 deletions(-)

diff --git a/build.zig b/build.zig @@ -4,11 +4,18 @@ const config = @import("build.zig.zon"); const add_exe_ty = *const fn (exe_name: []const u8, path: std.Build.LazyPath, b: *std.Build) anyerror!*std.Build.Step.Compile; fn add_exe_fake_pi(exe_name: []const u8, path: std.Build.LazyPath, b: *std.Build) !*std.Build.Step.Compile { + const pi = b.createModule(.{ + .root_source_file = b.path("fake-pi/pi.zig"), + .target = b.graph.host, + .optimize = .ReleaseSafe, + }); + const boot = b.createModule(.{ .root_source_file = path, .target = b.graph.host, .optimize = .ReleaseSafe, }); + boot.addImport("pi", pi); const fake_pi = b.createModule(.{ .root_source_file = b.path("fake-pi/main.zig"), @@ -16,7 +23,6 @@ fn add_exe_fake_pi(exe_name: []const u8, path: std.Build.LazyPath, b: *std.Build .optimize = .ReleaseFast, .link_libc = true, }); - fake_pi.addImport("boot", boot); // TODO; C should get its own module so we can have fake-pi be in debug for faster comp @@ -51,9 +57,15 @@ fn add_exe_real(exe_name: []const u8, path: std.Build.LazyPath, b: *std.Build) ! .abi = .eabi, }; + const pi = b.createModule(.{ + .root_source_file = b.path("pi/root.zig"), + .target = b.graph.host, + .optimize = .ReleaseSafe, + }); + // BCM2835 is **very specifically** the ARM1176JZF-S // https://www.raspberrypi.com/documentation/computers/processors.html - const pi = b.createModule(.{ + const boot = b.createModule(.{ .root_source_file = path, .target = b.resolveTargetQuery(target), .optimize = .ReleaseSafe, @@ -63,12 +75,13 @@ fn add_exe_real(exe_name: []const u8, path: std.Build.LazyPath, b: *std.Build) ! .link_libc = false, .no_builtin = true, }); - pi.addAssemblyFile(b.path("start.s")); + boot.addAssemblyFile(b.path("start.s")); + boot.addImport("pi", pi); const exe = b.addExecutable(.{ .name = exe_name, .linkage = .static, - .root_module = pi, + .root_module = boot, }); exe.setLinkerScript(b.path("linker.ld")); diff --git a/fake-pi/pi.zig b/fake-pi/pi.zig @@ -0,0 +1,21 @@ +pub const mem = struct { + const Operation = enum { + Read, + Write, + General, + }; + + pub inline fn barrier(op: Operation) void { + _ = op; + } + + pub extern fn put_u32(address: *u32, value: u32) void; + pub inline fn put_u32_barrier(address: *u32, value: u32) void { + return put_u32(address, value); + } + + pub extern fn get_u32(address: *u32) u32; + pub inline fn get_u32_barrier(address: *u32) u32 { + return get_u32(address); + } +}; diff --git a/pi/mem.zig b/pi/mem.zig @@ -37,7 +37,7 @@ pub inline fn put_u32(address: *u32, value: u32) void { asm volatile ("str %[value], [%[address]]" : : [value] "r" (value), - [address] "=r" (address), + [address] "r" (address), ); } @@ -51,8 +51,8 @@ pub inline fn put_u32_barrier( pub inline fn get_u32(address: *u32) u32 { return asm volatile ("str %[result], [%[address]]" - : [result] "r" (-> u32), - : [address] "=r" (address), + : [result] "=r" (-> u32), + : [address] "r" (address), ); } diff --git a/pi/root.zig b/pi/root.zig @@ -0,0 +1 @@ +pub const mem = @import("./mem.zig"); diff --git a/src/devices/gpio.zig b/src/devices/gpio.zig @@ -1,5 +1,5 @@ const std = @import("std"); -const util = @import("../util.zig"); +const mem = @import("pi").mem; // Page 90, Table 6-1: GPIO Register Assignment const BASE_OFFSET: usize = 0x0020_0000; @@ -53,10 +53,10 @@ pub fn fn_sel(pin: u8, sel: FunctionSelect) Error!void { const offset: u5 = @truncate((pin % GPIO_PER_FSEL) * 3); const mask = ~(@as(u32, 0b111) << offset); - var state = util.get_u32(address); + var state = mem.get_u32(address); state &= mask; state |= (@as(u32, @intCast(@intFromEnum(sel))) << offset); - util.put_u32(address, state); + mem.put_u32(address, state); } fn addr_bitset(pin: u8, address: *u32) void { @@ -65,7 +65,7 @@ fn addr_bitset(pin: u8, address: *u32) void { // We don't want to preserve the old value // SET and CLR are separated for this purpose - util.put_u32(address, mask); + mem.put_u32(address, mask); } // Turn on @@ -105,7 +105,7 @@ pub fn read(pin: u8) Error!bool { const offset: u5 = @truncate(pin % 32); const mask = (@as(u32, 1) << offset); - return (util.get_u32(address) & mask) == mask; + return (mem.get_u32(address) & mask) == mask; } pub fn write(pin: u8, state: bool) Error!void { diff --git a/src/devices/timer.zig b/src/devices/timer.zig @@ -1,7 +1,5 @@ const std = @import("std"); - -const barrier = @import("barrier.zig"); -const util = @import("../util.zig"); +const mem = @import("pi").mem; var base_address: usize = 0; @@ -14,11 +12,8 @@ pub fn initialize(base_addr: u32) void { } pub fn current_count() u64 { - var b = barrier.obtain(); - defer b.release(); - - const upper = util.get_u32(base_address + COUNTER_UPPER_OFFSET); - const lower = util.get_u32(base_address + COUNTER_LOWER_OFFSET); + const upper = mem.get_u32_barrier(base_address + COUNTER_UPPER_OFFSET); + const lower = mem.get_u32_barrier(base_address + COUNTER_LOWER_OFFSET); const extended: u64 = @intCast(upper); return (extended << 32) | lower; diff --git a/src/labs/2-gpio.zig b/src/labs/2-gpio.zig @@ -1,20 +1,28 @@ +const std = @import("std"); + const gpio = @import("../devices/gpio.zig"); -const util = @import("../util.zig"); const led0 = 20; const led1 = 21; const led2 = 27; const act = 47; +// TODO; replace with actual cycles +fn delay_cycles(count: usize) void { + for (0..count) |_| { + std.atomic.spinLoopHint(); + } +} + pub fn blink_1() !void { try gpio.set_output(led0); for (0..10) |_| { try gpio.set_on(led0); - util.delay_cycles(1000000); + delay_cycles(1000000); try gpio.set_off(led0); - util.delay_cycles(1000000); + delay_cycles(1000000); } try gpio.set_on(led0); @@ -27,11 +35,11 @@ pub fn blink_2() !void { for (0..10) |_| { try gpio.set_on(led0); try gpio.set_off(led1); - util.delay_cycles(3000000); + delay_cycles(3000000); try gpio.set_off(led0); try gpio.set_on(led1); - util.delay_cycles(3000000); + delay_cycles(3000000); } } @@ -50,7 +58,7 @@ pub fn loopback() !void { try gpio.write(output, v); try gpio.write(led1, try gpio.read(input)); - util.delay_cycles(1500000); + delay_cycles(1500000); v = !v; } } @@ -60,10 +68,10 @@ pub fn act_blink() !void { for (0..10) |_| { try gpio.set_on(act); - util.delay_cycles(1000000); + delay_cycles(1000000); try gpio.set_off(act); - util.delay_cycles(1000000); + delay_cycles(1000000); } try gpio.set_on(act); @@ -80,13 +88,13 @@ pub fn all() !void { try gpio.set_on(led1); try gpio.set_on(led2); try gpio.set_off(act); - util.delay_cycles(1000000); + delay_cycles(1000000); try gpio.set_off(led0); try gpio.set_off(led1); try gpio.set_off(led2); try gpio.set_on(act); - util.delay_cycles(1000000); + delay_cycles(1000000); } try gpio.set_on(act); diff --git a/src/util.zig b/src/util.zig @@ -1,11 +0,0 @@ -pub extern fn nop() void; -pub extern fn get_u32(address: *u32) u32; -pub extern fn put_u32(address: *u32, value: u32) void; - -pub noinline fn delay_cycles(ticks: usize) void { - var counter = ticks; - while (counter > 0) { - counter -= 1; - nop(); // Prevent optimization - } -}