summaryrefslogtreecommitdiff
path: root/riscv/riscv-probe/libfemto/drivers/semihost.c
blob: 9f478342bdfc1351560b4b3b2aa430bc66e6818e (plain)
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
// See LICENSE for license details.

#ifdef __riscv

#include "femto.h"

enum {
    SBI_CONSOLE_PUTCHAR = 1,
    SBI_CONSOLE_GETCHAR = 2,
    SBI_SHUTDOWN = 8
};

#define __syscall(...) \
    asm volatile ("ecall\n\t" : "+r"(a0) : __VA_ARGS__ : "memory")

static inline long semihost_call0(long n)
{
    register long a7 __asm__("a7") = n;
    register long a0 __asm__("a0");
    __syscall("r"(a7));
    return a0;
}

static inline long semihost_call1(long n, long a)
{
    register long a7 __asm__("a7") = n;
    register long a0 __asm__("a0") = a;
    __syscall("r"(a7), "0"(a0));
    return a0;
}

static int semihost_getchar()
{
    semihost_call0(SBI_CONSOLE_GETCHAR);
}

static int semihost_putchar(int ch)
{
    semihost_call1(SBI_CONSOLE_PUTCHAR, ch);
}

static void semihost_poweroff(int status)
{
    semihost_call0(SBI_SHUTDOWN);
    __builtin_unreachable();
}

console_device_t console_semihost = {
    NULL,
    semihost_getchar,
    semihost_putchar
};


poweroff_device_t poweroff_semihost = {
	NULL,
	semihost_poweroff
};

#endif