Rishka
RISC-V virtual runtime in C/C++ made for ESP32-WROVER
librishka_impl.hpp
1/*
2 * This file is part of the Rishka distribution (https://github.com/nthnn/rishka).
3 * Copyright (c) 2024 Nathanne Isip.
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 3.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef LIBRISHKA_IMPL_H
19#define LIBRISHKA_IMPL_H
20
21#include "librishka.h"
22
23enum rishka_syscall {
24 RISHKA_SC_IO_PRINTS,
25 RISHKA_SC_IO_PRINTN,
26 RISHKA_SC_IO_PRINTU,
27 RISHKA_SC_IO_PRINTD,
28 RISHKA_SC_IO_READCH,
29 RISHKA_SC_IO_READLINE,
30 RISHKA_SC_IO_AVAILABLE,
31 RISHKA_SC_IO_PEEK,
32 RISHKA_SC_IO_FIND,
33 RISHKA_SC_IO_FIND_UNTIL,
34 RISHKA_SC_IO_SET_TIMEOUT,
35 RISHKA_SC_IO_GET_TIMEOUT,
36
37 RISHKA_SC_SYS_DELAY_MS,
38 RISHKA_SC_SYS_MICROS,
39 RISHKA_SC_SYS_MILLIS,
40 RISHKA_SC_SYS_SHELLEXEC,
41 RISHKA_SC_SYS_EXIT,
42 RISHKA_SC_SYS_INFOS,
43 RISHKA_SC_SYS_INFON,
44 RISHKA_SC_SYS_RANDOM,
45 RISHKA_SC_SYS_CD,
46 RISHKA_SC_SYS_WD,
47
48 RISHKA_SC_GPIO_PIN_MODE,
49 RISHKA_SC_GPIO_DIGITAL_READ,
50 RISHKA_SC_GPIO_DIGITAL_WRITE,
51 RISHKA_SC_GPIO_ANALOG_READ,
52 RISHKA_SC_GPIO_ANALOG_WRITE,
53 RISHKA_SC_GPIO_PULSE_IN,
54 RISHKA_SC_GPIO_PULSE_IN_LONG,
55 RISHKA_SC_GPIO_SHIFT_IN,
56 RISHKA_SC_GPIO_SHIFT_OUT,
57 RISHKA_SC_GPIO_TONE,
58 RISHKA_SC_GPIO_NO_TONE,
59
60 RISHKA_SC_INT_ENABLE,
61 RISHKA_SC_INT_DISABLE,
62 RISHKA_SC_INT_ATTACH,
63 RISHKA_SC_INT_DETACH,
64
65 RISHKA_SC_FS_MKDIR,
66 RISHKA_SC_FS_RMDIR,
67 RISHKA_SC_FS_DELETE,
68 RISHKA_SC_FS_EXISTS,
69 RISHKA_SC_FS_ISFILE,
70 RISHKA_SC_FS_ISDIR,
71 RISHKA_SC_FS_OPEN,
72 RISHKA_SC_FS_CLOSE,
73 RISHKA_SC_FS_AVAILABLE,
74 RISHKA_SC_FS_FLUSH,
75 RISHKA_SC_FS_PEEK,
76 RISHKA_SC_FS_SEEK,
77 RISHKA_SC_FS_SIZE,
78 RISHKA_SC_FS_READ,
79 RISHKA_SC_FS_WRITEB,
80 RISHKA_SC_FS_WRITES,
81 RISHKA_SC_FS_POS,
82 RISHKA_SC_FS_PATH,
83 RISHKA_SC_FS_NAME,
84 RISHKA_SC_FS_IS_OK,
85 RISHKA_SC_FS_NEXT,
86 RISHKA_SC_FS_BUFSIZE,
87 RISHKA_SC_FS_LASTWRITE,
88 RISHKA_SC_FS_SEEKDIR,
89 RISHKA_SC_FS_NEXT_NAME,
90 RISHKA_SC_FS_REWIND,
91
92 RISHKA_SC_ARG_COUNT,
93 RISHKA_SC_ARG_STR,
94
95 RISHKA_SC_I2C_BEGIN,
96 RISHKA_SC_I2C_END,
97 RISHKA_SC_I2C_BEGIN_TRANSMISSION,
98 RISHKA_SC_I2C_END_TRANSMISSION,
99 RISHKA_SC_I2C_WRITE,
100 RISHKA_SC_I2C_SLAVE_WRITE,
101 RISHKA_SC_I2C_READ,
102 RISHKA_SC_I2C_PEEK,
103 RISHKA_SC_I2C_REQUEST,
104 RISHKA_SC_I2C_AVAILABLE,
105 RISHKA_SC_I2C_FLUSH,
106 RISHKA_SC_I2C_ON_RECEIVE,
107 RISHKA_SC_I2C_ON_REQUEST,
108 RISHKA_SC_I2C_GET_TIMEOUT,
109 RISHKA_SC_I2C_SET_TIMEOUT,
110 RISHKA_SC_I2C_GET_CLOCK,
111 RISHKA_SC_I2C_SET_CLOCK,
112 RISHKA_SC_I2C_PINS,
113 RISHKA_SC_I2C_BUFSIZE,
114
115 RISHKA_SC_KB_LAYOUT_NAME,
116 RISHKA_SC_KB_LAYOUT_DESC,
117 RISHKA_SC_KB_LED_GET_NUM,
118 RISHKA_SC_KB_LED_GET_CAPS,
119 RISHKA_SC_KB_LED_GET_SCROLL,
120 RISHKA_SC_KB_LED_SET_NUM,
121 RISHKA_SC_KB_LED_SET_CAPS,
122 RISHKA_SC_KB_LED_SET_SCROLL,
123 RISHKA_SC_KB_NEXT_SCAN_CODE,
124 RISHKA_SC_KB_DEVICE_TYPE,
125 RISHKA_SC_KB_AVAILABLE_VK,
126 RISHKA_SC_KB_IS_AVAILABLE,
127 RISHKA_SC_KB_IS_VK_DOWN,
128 RISHKA_SC_KB_LOCK,
129 RISHKA_SC_KB_UNLOCK,
130 RISHKA_SC_KB_RESET,
131
132 RISHKA_SC_DISPLAY_SCREEN_HEIGHT,
133 RISHKA_SC_DISPLAY_SCREEN_WIDTH,
134 RISHKA_SC_DISPLAY_VIEWPORT_HEIGHT,
135 RISHKA_SC_DISPLAY_VIEWPORT_WIDTH,
136 RISHKA_SC_DISPLAY_SUPPORTED_COLORS,
137
138 RISHKA_SC_NVS_COMMIT,
139 RISHKA_SC_NVS_ERASE,
140 RISHKA_SC_NVS_ERASE_ALL,
141 RISHKA_SC_NVS_SET_I8,
142 RISHKA_SC_NVS_SET_I16,
143 RISHKA_SC_NVS_SET_I32,
144 RISHKA_SC_NVS_SET_I64,
145 RISHKA_SC_NVS_SET_U8,
146 RISHKA_SC_NVS_SET_U16,
147 RISHKA_SC_NVS_SET_U32,
148 RISHKA_SC_NVS_SET_U64,
149 RISHKA_SC_NVS_SET_STRING,
150 RISHKA_SC_NVS_GET_I8,
151 RISHKA_SC_NVS_GET_I16,
152 RISHKA_SC_NVS_GET_I32,
153 RISHKA_SC_NVS_GET_I64,
154 RISHKA_SC_NVS_GET_U8,
155 RISHKA_SC_NVS_GET_U16,
156 RISHKA_SC_NVS_GET_U32,
157 RISHKA_SC_NVS_GET_U64,
158 RISHKA_SC_NVS_GET_STRING,
159 RISHKA_SC_NVS_HAS_WIFI_CONFIG,
160 RISHKA_SC_NVS_SET_WIFI_SSID,
161 RISHKA_SC_NVS_SET_WIFI_PWORD,
162
163 RISHKA_SC_WIFI_CONNECT,
164 RISHKA_SC_WIFI_RECONNECT,
165 RISHKA_SC_WIFI_DISCONNECT,
166 RISHKA_SC_WIFI_ERASE_AP,
167 RISHKA_SC_WIFI_IS_CONNECTED,
168 RISHKA_SC_WIFI_SET_AUTO_RECONNECT,
169 RISHKA_SC_WIFI_GET_AUTO_RECONNECT,
170 RISHKA_SC_WIFI_WAIT_FOR_RESULT,
171 RISHKA_SC_WIFI_SET_MINSEC,
172 RISHKA_SC_WIFI_SET_SCAN_METHOD,
173 RISHKA_SC_WIFI_SET_SORT_METHOD,
174 RISHKA_SC_WIFI_STATUS,
175 RISHKA_SC_WIFI_SSID,
176 RISHKA_SC_WIFI_PSK,
177 RISHKA_SC_WIFI_BSSID,
178 RISHKA_SC_WIFI_RSSI,
179 RISHKA_SC_WIFI_SET_LOCAL_IP,
180 RISHKA_SC_WIFI_GET_LOCAL_IP,
181 RISHKA_SC_WIFI_SET_GATEWAY_IP,
182 RISHKA_SC_WIFI_GET_GATEWAY_IP,
183 RISHKA_SC_WIFI_SET_SUBNET_MASK,
184 RISHKA_SC_WIFI_GET_SUBNET_MASK,
185 RISHKA_SC_WIFI_SET_DNS_IP,
186 RISHKA_SC_WIFI_GET_DNS_IP,
187 RISHKA_SC_WIFI_MAC_ADDR,
188 RISHKA_SC_WIFI_BROADCAST_IP,
189 RISHKA_SC_WIFI_NETWORK_ID,
190 RISHKA_SC_WIFI_ENABLE_IPV6,
191 RISHKA_SC_WIFI_LINK_LOCAL_IPV6,
192 RISHKA_SC_WIFI_GLOBAL_IPV6,
193
194 RISHKA_SC_RT_STRPASS,
195 RISHKA_SC_RT_YIELD,
196 RISHKA_SC_RT_FORK_STREAM
197};
198
199static inline long long int double_to_long(double d) {
200 union {
201 double input;
202 long long int output;
203 } data;
204
205 data.input = d;
206 return data.output;
207}
208
209static inline double long_to_float(long long int l) {
210 union {
211 double output;
212 long long int input;
213 } data;
214
215 data.input = l;
216 return data.output;
217}
218
219static inline i64 rishka_sc_0(i32 scallid) {
220 register i64 a0 asm("a0") = 0;
221 register i64 scid asm("a7") = scallid;
222
223 asm volatile ("scall" : "+r"(a0) : "r"(scid));
224 return a0;
225}
226
227static inline i64 rishka_sc_1(i32 scallid, i64 arg0) {
228 register i64 a0 asm("a0") = arg0;
229 register i64 scid asm("a7") = scallid;
230
231 asm volatile ("scall" : "+r"(a0) : "r"(scid));
232 return a0;
233}
234
235static inline i64 rishka_sc_2(i32 scallid, i64 arg0, i64 arg1) {
236 register i64 a0 asm("a0") = arg0;
237 register i64 a1 asm("a1") = arg1;
238 register i64 scid asm("a7") = scallid;
239
240 asm volatile ("scall" : "+r"(a0) : "r"(a1), "r"(scid));
241 return a0;
242}
243
244static inline i64 rishka_sc_3(i32 scallid, i64 arg0, i64 arg1, i64 arg2) {
245 register i64 a0 asm("a0") = arg0;
246 register i64 a1 asm("a1") = arg1;
247 register i64 a2 asm("a2") = arg2;
248 register i64 scid asm("a7") = scallid;
249
250 asm volatile ("scall" : "+r"(a0) : "r"(a1), "r"(a2), "r"(scid));
251 return a0;
252}
253
254static inline i64 rishka_sc_4(i32 scallid, i64 arg0, i64 arg1, i64 arg2, i64 arg3) {
255 register i64 a0 asm("a0") = arg0;
256 register i64 a1 asm("a1") = arg1;
257 register i64 a2 asm("a2") = arg2;
258 register i64 a3 asm("a3") = arg3;
259 register i64 scid asm("a7") = scallid;
260
261 asm volatile ("scall" : "+r"(a0) : "r"(a1), "r"(a2), "r"(a3), "r"(scid));
262 return a0;
263}
264
265static inline rune rt_strpass() {
266 return (rune) rishka_sc_0(RISHKA_SC_RT_STRPASS);
267}
268
269static inline string get_rt_string(u32 len) {
270 string str = (string) Memory::alloc(len + 1);
271 for(u32 i = 0; i < len; i++)
272 str[i] = rt_strpass();
273
274 str[len] = '\0';
275 return str;
276}
277
278#endif
static any alloc(usize size)
Allocate memory.
Definition: librishka_mem.cpp:69
Main header file for the Rishka SDK.
char * string
Alias for the string type.
Definition: types.h:40
signed int i32
Alias for the signed 32-bit integer type.
Definition: types.h:58
char rune
Alias for the Unicode character type.
Definition: types.h:34
unsigned int u32
Alias for the unsigned 32-bit integer type.
Definition: types.h:82
signed long int i64
Alias for the signed 64-bit integer type.
Definition: types.h:64