-
Notifications
You must be signed in to change notification settings - Fork 0
/
urclos_rt.c
167 lines (147 loc) · 3.38 KB
/
urclos_rt.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
#include <errno.h>
#include <locale.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#ifdef _WIN32
#include <conio.h>
#define INIT_TERM 0
#else
#include <termios.h>
struct termios orig_termios;
void reset_term() {
tcsetattr(0,TCSAFLUSH,&orig_termios);
}
void init_term() {
tcgetattr(0,&orig_termios);
atexit(reset_term);
struct termios raw = orig_termios;
raw.c_lflag &= ~(ECHO | ICANON);
tcsetattr(0, TCSAFLUSH, &raw);
}
#define INIT_TERM init_term()
#define getch getchar
#endif
extern size_t urcl_main();
union WordAndFloat {
size_t w;
float f;
};
uint16_t* fs;
size_t address = 0;
size_t urcl_in(size_t port) {
switch (port) {
case 1: {
return getchar();
}
case 32: {
return address;
}
case 33: {
return (size_t) fs[(int) address];
}
case 40: {
return (size_t) rand();
}
default: {
printf("\n\x1b[1;33mW:\x1b[0m unknown port %%%lu was read\n", port);
return 0;
}
}
}
void urcl_out(size_t port, size_t data) {
switch (port) {
case 1: {
printf("%lc", (uint32_t) data);
break;
}
case 16: {
putchar(data & 0xFF);
break;
}
case 19: {
putchar(data & 0x7F);
break;
}
case 2:
case 25: {
printf("%lu", data);
break;
}
case 24: {
printf("%li", data);
break;
}
case 27: {
printf("%04lx", data);
break;
}
case 28: {
union WordAndFloat f = { .w = data };
printf("%f", f.f);
break;
}
case 32: {
address = data;
break;
}
case 33: {
fs[(int) address] = (uint16_t) data;
break;
}
case 40: {
srand(data);
break;
}
default: {
printf("\n\x1b[1;33mW:\x1b[0m unknown port %%%lu was written to with %lu\n", port, data);
break;
}
}
}
bool is_little_endian() {
volatile uint32_t i = 0x01234567;
return (*((uint8_t*) (&i))) == 0x67;
}
void swap_bytes(uint16_t* buf, int size) {
for (int i = 0; i < size; i++) {
buf[i] = ((buf[i] & 0xff) << 8) | ((buf[i] >> 8) & 0xff);
}
}
int main(int argc, char* argv[]) {
setlocale(LC_ALL, "");
if (argc < 2) {
printf("Usage: %s <fs>\n", argv[0]);
return 1;
}
FILE* fs_file = fopen(argv[1], "r+b");
if (fs_file == NULL) {
perror("Loading filesystem failed");
return 2;
}
fseek(fs_file, 0, SEEK_END);
int size = ftell(fs_file);
rewind(fs_file);
fs = (uint16_t*) malloc(size);
if ((int) fread(fs, 1, size, fs_file) != size) {
perror("Loading filesystem failed");
return 3;
}
if (is_little_endian()) {
swap_bytes(fs, size / 2);
}
INIT_TERM;
(void) urcl_main();
printf("\n\x1b[1;32mI:\x1b[0m URCL-OS halted\n");
if (is_little_endian()) {
swap_bytes(fs, size / 2);
}
rewind(fs_file);
fwrite(fs, size, 1, fs_file);
fflush(fs_file);
fclose(fs_file);
free(fs);
return 0;
}