-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnewlib_stubs.c
361 lines (278 loc) · 6.39 KB
/
newlib_stubs.c
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#ifdef __cplusplus
extern "C" {
#endif
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <stdbool.h>
#include <reent.h>
#include "data_font.h"
#include "data_gfx.h"
extern void turtle_x(int);
extern void turtle_y(int);
extern void turtle_z(int);
extern void turtle_set(int);
extern void mc_putc(uint8_t c);
extern void mc_sleep(void);
extern int main(int argc, char **argv);
void _start() {
main(0, NULL);
while (1) {
mc_sleep();
}
}
/*uint32_t ticks_ms = 0;
void DG_DrawFrame()
{
// RGB888
turtle_z(-20);
for (int y = 0; y < DOOMGENERIC_RESY; ++y) {
turtle_y(DOOMGENERIC_RESY - y);
for (int x = 0; x < DOOMGENERIC_RESX; ++x) {
turtle_x(DOOMGENERIC_RESX - x);
uint32_t rgb = DG_ScreenBuffer[DOOMGENERIC_RESX * y + x];
turtle_set(rgb % 13);
}
}
//ticks_ms += 16;
}
void DG_SleepMs(uint32_t ms)
{
//SDL_Delay(ms);
}
uint32_t DG_GetTicksMs()
{
ticks_ms += 1;
return ticks_ms;
}
int DG_GetKey(int* pressed, unsigned char* doomKey)
{
return 0;
if (s_KeyQueueReadIndex == s_KeyQueueWriteIndex){
//key queue is empty
return 0;
}else{
unsigned short keyData = s_KeyQueue[s_KeyQueueReadIndex];
s_KeyQueueReadIndex++;
s_KeyQueueReadIndex %= KEYQUEUE_SIZE;
*pressed = keyData >> 8;
*doomKey = keyData & 0xFF;
return 1;
}
return 0;
}
void DG_SetWindowTitle(const char * title) {}*/
#define MAX_FDS 16
typedef struct {
off_t offset;
bool valid;
uint8_t *data;
off_t size;
} fd_info;
fd_info fd_table[MAX_FDS] = {};
int next_fd = 3;
#define STDOUT_FD 0
#define STDERR_FD 1
#define STDIN_FD 2
ssize_t _read(int fd, void *buf, size_t count) {
uint8_t *buf_bytes = (uint8_t *)buf;
if (fd < 0 || fd >= MAX_FDS) {
errno = EBADF;
return -1;
}
fd_info *info = &fd_table[fd];
if (!info->valid) {
errno = EBADF;
return -1;
}
ssize_t num_read = 0;
while (count > 0) {
if (info->offset == info->size) {
break;
}
*buf_bytes++ = info->data[info->offset++];
--count;
++num_read;
}
return num_read;
}
ssize_t _write(int fd, const void *buf, size_t count) {
if (fd == STDOUT_FD || fd == STDERR_FD) {
uint8_t *buf_bytes = (uint8_t*)buf;
for (size_t i = 0; i < count; ++i) {
mc_putc(buf_bytes[i]);
}
return count;
} else {
errno = ENOSYS;
return -1;
}
}
#define MEMORY_SIZE (1 * 65536)
uint8_t memory[MEMORY_SIZE] = {};
uint8_t *next = memory;
void *sbrk(intptr_t increment) {
uint8_t *result = next;
intptr_t current_size = (intptr_t)next - (intptr_t)memory;
if (current_size + increment >= MEMORY_SIZE) {
errno = ENOMEM;
return NULL;
}
next += increment;
return result;
}
void _exit(int code) {
const char msg[] = "exit\n";
_write(STDOUT_FD, msg, sizeof(msg) - 1);
// TODO:
while (1) {}
}
int _open(const char *pathname, int flags, mode_t mode) {
if (strcmp(pathname, "data/gfx.bmp") == 0 && next_fd < MAX_FDS) {
int fd = next_fd++;
fd_table[fd].valid = true;
fd_table[fd].offset = 0;
fd_table[fd].data = (uint8_t*)data_gfx_file;
fd_table[fd].size = data_gfx_fsize;
return fd;
}
if (strcmp(pathname, "data/font.bmp") == 0 && next_fd < MAX_FDS) {
int fd = next_fd++;
fd_table[fd].valid = true;
fd_table[fd].offset = 0;
fd_table[fd].data = (uint8_t*)data_font_file;
fd_table[fd].size = data_font_fsize;
return fd;
}
printf("ignoring open %s %d\n", pathname, next_fd);
errno = ENOSYS;
return -1;
}
int _close(int fd) {
if (0 <= fd && fd < MAX_FDS) {
if (fd_table[fd].valid) {
fd_table[fd].valid = false;
return 0;
} else {
errno = EBADF;
return -1;
}
}
const char msg[] = "ignoring close\n";
_write(STDOUT_FD, msg, sizeof(msg) - 1);
errno = ENOSYS;
return -1;
}
int _link(const char *oldpath, const char *newpath) {
const char msg[] = "ignoring link\n";
_write(STDOUT_FD, msg, sizeof(msg) - 1);
errno = ENOSYS;
return -1;
}
int _unlink(const char *pathname) {
const char msg[] = "ignoring unlink\n";
_write(STDOUT_FD, msg, sizeof(msg) - 1);
errno = ENOSYS;
return -1;
}
off_t _lseek(int fd, off_t offset, int whence) {
if (fd < 0 || fd >= MAX_FDS) {
errno = EBADF;
return (off_t)-1;
}
fd_info *info = &fd_table[fd];
if (!info->valid) {
errno = EBADF;
return (off_t)-1;
}
off_t new_offset;
switch (whence) {
case SEEK_SET:
new_offset = offset;
break;
case SEEK_CUR:
new_offset = info->offset + offset;
break;
case SEEK_END:
new_offset = info->size + offset;
break;
default:
errno = EINVAL;
return (off_t)-1;
}
if (new_offset < 0 || new_offset > info->size) {
errno = EINVAL;
return (off_t)-1;
}
info->offset = new_offset;
return info->offset;
}
void *_calloc_r(struct _reent* r, size_t num, size_t size) {
const char msg[] = "ignoring calloc\n";
_write(STDOUT_FD, msg, sizeof(msg) - 1);
return calloc(num, size);
}
void *_malloc_r(struct _reent* r, size_t size) {
const char msg[] = "malloc of size ";
_write(STDOUT_FD, msg, sizeof(msg) - 1);
for (int i = 0; i < sizeof(size_t) * 2; ++i) {
int off = sizeof(size_t) * 2 - 1 - i;
const char m1 = (size >> (off * 4)) & 0xF;
char m2;
if (m1 < 0xA) {
m2 = m1 + '0';
} else {
m2 = m1 - 0xA + 'A';
}
_write(STDOUT_FD, &m2, 1);
}
_write(STDOUT_FD, "\n", 1);
return malloc(size);
}
void _free_r(struct _reent* r, void *ptr) {
free(ptr);
}
void *_realloc_r(struct _reent* r, void *ptr, size_t new_size) {
return realloc(ptr, new_size);
}
long double __extenddftf2(double a) {
return 0.0;
}
double __trunctfdf2(long double a) {
return 0.0;
}
int mkdir(const char *pathname, mode_t mode) {
const char msg[] = "ignoring mkdir\n";
_write(STDOUT_FD, msg, sizeof(msg) - 1);
errno = ENOSYS;
return -1;
}
int _fstat(int fd, struct stat *statbuf) {
if (fd < 0 || fd >= MAX_FDS) {
errno = EBADF;
return -1;
}
fd_info *info = &fd_table[fd];
if (!info->valid) {
errno = EBADF;
return -1;
}
//statbuf->st_mode = ???
statbuf->st_nlink = 1;
statbuf->st_size = info->size;
statbuf->st_blksize = 512;
statbuf->st_blocks = info->size / (512 * 1024);
return 0;
}
int raise(int sig) {
const char msg[] = "signal raised\n";
_write(STDOUT_FD, msg, sizeof(msg) - 1);
// TODO:
while (1) {}
}
#ifdef __cplusplus
}
#endif