aboutsummaryrefslogtreecommitdiff
path: root/examples/zero_stream/src/main.zig
blob: 8e529cbcf79359e501d922a2305f21ad8ec6d7b5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// SPDX-FileCopyrightText: 2024 Himbeer <himbeer@disroot.org>
//
// 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,
    };
}