-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgauntlet_pro.c
More file actions
139 lines (123 loc) · 4.53 KB
/
gauntlet_pro.c
File metadata and controls
139 lines (123 loc) · 4.53 KB
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
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <termios.h>
#include <sys/ioctl.h>
#include <time.h>
#include "cutils.h"
#include "mquickjs.h"
#define WIDTH 64
#define HEIGHT 32
static uint8_t vram[WIDTH * HEIGHT];
static struct termios old_tty;
static void restore_term(void) {
printf("\x1b[?25h");
tcsetattr(STDIN_FILENO, TCSANOW, &old_tty);
}
static void setup_term(void) {
struct termios tty;
tcgetattr(STDIN_FILENO, &tty);
old_tty = tty;
atexit(restore_term);
tty.c_lflag &= ~(ICANON | ECHO);
tty.c_cc[VMIN] = 0;
tty.c_cc[VTIME] = 0;
tcsetattr(STDIN_FILENO, TCSANOW, &tty);
printf("\x1b[?25l");
}
static JSValue js_gauntlet_set(JSContext *ctx, JSValue *this_val, int argc, JSValue *argv) { return JS_UNDEFINED; }
static JSValue js_draw(JSContext *ctx, JSValue *this_val, int argc, JSValue *argv) {
int x, y, val;
JS_ToInt32(ctx, &x, argv[0]);
JS_ToInt32(ctx, &y, argv[1]);
JS_ToInt32(ctx, &val, argv[2]);
if (x >= 0 && x < WIDTH && y >= 0 && y < HEIGHT) vram[y * WIDTH + x] = val;
return JS_UNDEFINED;
}
static JSValue js_gauntlet_flush(JSContext *ctx, JSValue *this_val, int argc, JSValue *argv) {
printf("\x1b[H");
for (int y = 0; y < HEIGHT; y += 2) {
for (int x = 0; x < WIDTH; x++) {
int t = vram[y * WIDTH + x];
int b = vram[(y + 1) * WIDTH + x];
if (t && b) printf("█"); else if (t) printf("▀"); else if (b) printf("▄"); else printf(" ");
}
putchar('\n');
}
fflush(stdout);
usleep(16666);
return JS_UNDEFINED;
}
static JSValue js_gauntlet_poll(JSContext *ctx, JSValue *this_val, int argc, JSValue *argv) {
uint8_t c;
if (read(STDIN_FILENO, &c, 1) > 0) return JS_NewInt32(ctx, c);
return JS_NewInt32(ctx, 0);
}
static JSValue js_get_rom(JSContext *ctx, JSValue *this_val, int argc, JSValue *argv) {
JSCStringBuf b;
const char *path = JS_ToCString(ctx, argv[0], &b);
FILE *f = fopen(path, "rb");
if (!f) return JS_NULL;
fseek(f, 0, SEEK_END);
int size = ftell(f);
fseek(f, 0, SEEK_SET);
JSGCRef arr_ref;
JSValue *arr = JS_PushGCRef(ctx, &arr_ref);
*arr = JS_NewArray(ctx, size);
for (int i = 0; i < size; i++) JS_SetPropertyUint32(ctx, *arr, i, JS_NewInt32(ctx, fgetc(f)));
fclose(f);
return JS_PopGCRef(ctx, &arr_ref);
}
static JSValue js_performance_now(JSContext *ctx, JSValue *this_val, int argc, JSValue *argv) {
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return JS_NewInt64(ctx, (uint64_t)ts.tv_sec * 1000 + (ts.tv_nsec / 1000000));
}
static JSValue js_print(JSContext *ctx, JSValue *this_val, int argc, JSValue *argv) {
for(int i = 0; i < argc; i++) {
if (i != 0) putchar(' ');
JSCStringBuf buf;
const char *str = JS_ToCString(ctx, argv[i], &buf);
if (str) printf("%s", str);
}
putchar('\n');
return JS_UNDEFINED;
}
static JSValue js_gc(JSContext *ctx, JSValue *this_val, int argc, JSValue *argv) { JS_GC(ctx); return JS_UNDEFINED; }
static JSValue js_date_now(JSContext *ctx, JSValue *this_val, int argc, JSValue *argv) { return JS_NewInt32(ctx, 0); }
static JSValue js_load(JSContext *ctx, JSValue *this_val, int argc, JSValue *argv) { return JS_UNDEFINED; }
static JSValue js_setTimeout(JSContext *ctx, JSValue *this_val, int argc, JSValue *argv) { return JS_UNDEFINED; }
static JSValue js_clearTimeout(JSContext *ctx, JSValue *this_val, int argc, JSValue *argv) { return JS_UNDEFINED; }
#include "gauntlet_pro_stdlib.h"
int main(int argc, char **argv) {
if (argc < 2) return 1;
size_t mem_size = 1024 * 1024;
uint8_t *mem_buf = malloc(mem_size);
setup_term();
JSContext *ctx = JS_NewContext(mem_buf, mem_size, &gauntlet_pro_stdlib);
JSValue global = JS_GetGlobalObject(ctx);
if (argc >= 3) JS_SetPropertyStr(ctx, global, "ROM_PATH", JS_NewString(ctx, argv[2]));
FILE *f = fopen(argv[1], "rb");
fseek(f, 0, SEEK_END);
long len = ftell(f);
fseek(f, 0, SEEK_SET);
char *script = malloc(len + 1);
fread(script, 1, len, f);
script[len] = '\0';
fclose(f);
printf("DEBUG: Starting eval...\n");
JSValue val = JS_Eval(ctx, script, len, argv[1], 0);
if (JS_IsException(val)) {
restore_term();
JSValue ex = JS_GetException(ctx);
JSCStringBuf buf;
const char *msg = JS_ToCString(ctx, ex, &buf);
printf("CRASH: %s\n", msg ? msg : "unknown");
} else {
printf("DEBUG: Eval success\n");
}
JS_FreeContext(ctx);
return 0;
}