commit ad80f6ddfb0d3eb655413a166fa15fab1b19d1f8
parent 1be28b565b3da52c64f87f313806d554d1f39afe
Author: Sylvia Ivory <git@sivory.net>
Date: Tue, 20 Jan 2026 19:37:48 -0800
Add 4-meeasure
Diffstat:
6 files changed, 57 insertions(+), 10 deletions(-)
diff --git a/build.zig b/build.zig
@@ -145,6 +145,8 @@ fn lab(name: []const u8, units: []const []const u8, b: *std.Build, add_exe: add_
fn handle_lab(name: []const u8, b: *std.Build, add_exe: add_exe_ty) !void {
if (std.mem.eql(u8, name, "2-gpio")) {
try lab(name, &.{ "blink_1", "blink_2", "loopback", "act_blink", "all" }, b, add_exe);
+ } else if (std.mem.eql(u8, name, "4-measure")) {
+ try lab(name, &.{"main"}, b, add_exe);
} else {
@panic("invalid lab name");
}
diff --git a/fake-pi/pi.zig b/fake-pi/pi.zig
@@ -21,3 +21,9 @@ pub const mem = struct {
return get_u32(address);
}
};
+
+// Stubs
+pub fn cycle_counter_init() void {}
+pub fn cycle_counter_get() u32 {
+ return 0;
+}
diff --git a/pi/root.zig b/pi/root.zig
@@ -1,2 +1,16 @@
pub const mem = @import("./mem.zig");
pub extern fn nop() void;
+
+pub inline fn cycle_counter_init() void {
+ const in: u32 = 1;
+ asm volatile ("MCR p15, 0, %[in], c15, c12, 0"
+ :
+ : [in] "r" (in),
+ );
+}
+
+pub inline fn cycle_counter_read() u32 {
+ return asm volatile ("MCR p15, 0, %[result], c15, c12, 1"
+ : [result] "=r" (-> u32),
+ );
+}
diff --git a/src/devices/timer.zig b/src/devices/timer.zig
@@ -6,8 +6,8 @@ const COUNTER_LOWER: usize = BASE_ADDRESS + 0x0004;
const COUNTER_UPPER: usize = BASE_ADDRESS + 0x0008;
pub fn current_count() u64 {
- const upper = mem.get_u32_barrier(COUNTER_UPPER);
- const lower = mem.get_u32_barrier(COUNTER_LOWER);
+ const upper = mem.get_u32_barrier(@ptrFromInt(COUNTER_UPPER));
+ const lower = mem.get_u32_barrier(@ptrFromInt(COUNTER_LOWER));
const extended: u64 = @intCast(upper);
return (extended << 32) | lower;
diff --git a/src/labs/4-measure.zig b/src/labs/4-measure.zig
@@ -0,0 +1,32 @@
+const std = @import("std");
+const pi = @import("pi");
+
+const uart = @import("../devices/mini-uart.zig");
+const timer = @import("../devices/timer.zig");
+
+pub fn main() !void {
+ try uart.initialize(115200, .Gpio14, .Gpio15);
+ pi.cycle_counter_init();
+
+ const start = pi.cycle_counter_read();
+
+ const s = timer.current_count();
+ while ((timer.current_count() - s) < 1000 * 1000) {}
+
+ const end = pi.cycle_counter_read();
+ const total = end - start;
+
+ var buffer: [1024]u8 = undefined;
+
+ {
+ const slice = try std.fmt.bufPrint(&buffer, "cycles per second = {}\n", .{total});
+ uart.write_slice(slice);
+ }
+
+ const exp = 700 * 1000 * 1000;
+
+ {
+ const slice = try std.fmt.bufPrint(&buffer, "expected - measured = {} cycles of error\n", .{exp - total});
+ uart.write_slice(slice);
+ }
+}
diff --git a/src/main.zig b/src/main.zig
@@ -1,8 +1 @@
-const uart = @import("devices/mini-uart.zig");
-
-pub fn main() !void {
- try uart.initialize(115200, .Gpio14, .Gpio15);
- uart.write_slice("Hello, World!\n");
-
- @panic("oops");
-}
+pub fn main() !void {}