// SPDX-FileCopyrightText: 2024 Himbeer // // SPDX-License-Identifier: AGPL-3.0-or-later const std = @import("std"); const Result = extern struct { value: usize, status: usize, }; export fn _start() void { main(); } pub fn main() void { const s1 = "Hello"; asm volatile ( \\ li a7, 100000 \\ ecall : : [s] "{a0}" (s1.ptr), [n] "{a1}" (s1.len), : "a7" ); const path = "/foo\x00"; asm volatile ( \\ li a7, 100003 \\ li a2, 0 \\ ecall : : [path] "{a0}" (path.ptr), [readFn] "{a1}" (&read), : "a7", "a2" ); const s2 = "Stream resource provided"; asm volatile ( \\ li a7, 100000 \\ ecall : : [s] "{a0}" (s2.ptr), [n] "{a1}" (s2.len), : "a7" ); const handle = asm volatile ( \\ li a7, 100001 \\ ecall : [handle] "={a0}" (-> usize), : [path] "{a0}" (path.ptr), : "a7", "a1" ); var s3_buf: [128]u8 = undefined; const s3 = std.fmt.bufPrint(&s3_buf, "Resource opened, handle = {d}", .{handle}) catch unreachable; asm volatile ( \\ li a7, 100000 \\ ecall : : [s] "{a0}" (s3.ptr), [n] "{a1}" (s3.len), : "a7" ); var buf = [8]u8{ 1, 2, 3, 4, 5, 6, 7, 8 }; asm volatile ( \\ li a7, 100009 \\ ecall : : [handle] "{a0}" (handle), [buffer] "{a1}" (&buf), [len] "{a2}" (buf.len - 1), ); var s4_buf: [384]u8 = undefined; const s4 = std.fmt.bufPrint(&s4_buf, "Bytes after reading: {any}\r\n", .{buf}) catch unreachable; asm volatile ( \\ li a7, 100000 \\ ecall : : [s] "{a0}" (s4.ptr), [n] "{a1}" (s4.len), : "a7" ); } pub export fn read(ptr: [*]u8, len: usize) Result { const buffer = ptr[0..len]; for (buffer, 0..) |_, i| { buffer[i] = 0; } return .{ .value = buffer.len, .status = 0, }; }