Snitch Runtime
Loading...
Searching...
No Matches
openocd.h
1// Copyright 2023 ETH Zurich and University of Bologna.
2// Licensed under the Apache License, Version 2.0, see LICENSE for details.
3// SPDX-License-Identifier: Apache-2.0
4//
5
6#include <stdint.h>
7
8#pragma once
9
10#define SEMIHOST_EXIT_SUCCESS 0x20026
11#define SEMIHOST_EXIT_ERROR 0x20023
12
13/* riscv semihosting standard:
14 * IN: a0 holds syscall number
15 * IN: a1 holds pointer to arg struct
16 * OUT: a0 holds return value (if exists)
17 */
18static inline long __ocd_semihost(long n, long _a1) {
19 register long a0 asm("a0") = n;
20 register long a1 asm("a1") = _a1;
21
22 // riscv magic values for semihosting
23 asm volatile(
24 ".option norvc;\t\n"
25 "slli zero,zero,0x1f\t\n"
26 "ebreak\t\n"
27 "srai zero,zero,0x7\t\n"
28 ".option rvc;\t\n"
29 : "+r"(a0)
30 : "r"(a1));
31
32 return a0;
33}
34
35static inline int __ocd_semihost_write(int fd, uint8_t *buffer, int len) {
36 uint32_t args[3] = {(long)fd, (long)buffer, (long)len};
37 __asm__ __volatile__("" : : : "memory");
38 return __ocd_semihost(0x05, (long)args);
39}
40
41static inline void __ocd_semihost_exit(int status) {
42 __ocd_semihost(0x18,
43 status == 0 ? SEMIHOST_EXIT_SUCCESS : SEMIHOST_EXIT_ERROR);
44}