aboutsummaryrefslogtreecommitdiff
path: root/examples/zero_stream/src/main.zig
diff options
context:
space:
mode:
Diffstat (limited to 'examples/zero_stream/src/main.zig')
-rw-r--r--examples/zero_stream/src/main.zig94
1 files changed, 94 insertions, 0 deletions
diff --git a/examples/zero_stream/src/main.zig b/examples/zero_stream/src/main.zig
new file mode 100644
index 0000000..7ba9ecf
--- /dev/null
+++ b/examples/zero_stream/src/main.zig
@@ -0,0 +1,94 @@
+// SPDX-FileCopyrightText: 2024 Himbeer <himbeer@disroot.org>
+//
+// SPDX-License-Identifier: AGPL-3.0-or-later
+
+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"
+ );
+
+ const s3 = "Resource opened";
+ asm volatile (
+ \\ li a7, 100000
+ \\ ecall
+ :
+ : [s] "{a0}" (s3.ptr),
+ [n] "{a1}" (s3.len),
+ : "a7"
+ );
+
+ var buf: [8]u8 = undefined;
+ asm volatile (
+ \\ li a7, 100009
+ \\ ecall
+ :
+ : [handle] "{a0}" (handle),
+ [buffer] "{a1}" (&buf),
+ [len] "{a2}" (buf.len),
+ );
+
+ const s4 = "Bytes read";
+ asm volatile (
+ \\ li a7, 100000
+ \\ ecall
+ :
+ : [s] "{a0}" (s4.ptr),
+ [n] "{a1}" (s4.len),
+ : "a7"
+ );
+}
+
+pub fn read(buffer: []u8) Result {
+ for (buffer, 0..) |_, i| {
+ buffer[i] = 0;
+ }
+
+ return .{
+ .value = buffer.len,
+ .status = 0,
+ };
+}