-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
156 lines (133 loc) · 3.5 KB
/
main.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "nano_malloc.h"
#include "native.h"
#include "memory.h"
#include "vm.h"
#define VERSION "Lox68k 1.6"
#define AUTHOR "by Fred Bayer"
#ifdef LOX_DBG
#define DBG_STR "debug"
#else
#define DBG_STR "nodeb"
#endif
#ifdef KIT68K
#include "monitor4x.h"
const char* loxLibSrc;
int main() {
if (ON_KIT()) { // Running on actual 68008 Kit
// Welcome message on LCD
lcd_clear(); lcd_puts(VERSION);
lcd_goto(11,0); lcd_puts(DBG_STR);
lcd_goto(0,1); lcd_puts(AUTHOR);
// init 100 Hz ticker
clock() = 0;
IRQ2_VECTOR = (void *)ticker;
// ANDI #$f0ff,SR ; clear interrupt mask in status register
_word(0x027c); _word(0xf0ff);
} else { // Running on IDE68k Simulator
TRAP1_VECTOR = (void *)rte; // short-cut TRAP #1
*port0 = 0x40; // make IRQ button check fail
// Load ROM file with FFP lib and Lox standard lib.
if (loadROM() == 0)
printf("%s loaded.\n", "ROM");
else {
printf("%s not found.\n", "ROM");
loxLibSrc = NULL;
}
}
init_freelist();
initVM();
printf("%s [%s] %s\n", VERSION, DBG_STR, AUTHOR);
if (loxLibSrc) {
putstr("Loading standard library.\n");
interpret(loxLibSrc);
}
// REPL
for (;;) {
putstr("> ");
readLine();
if (!ON_KIT() && big_buffer[0] == '&') {
if (loadSource(big_buffer + 1) == 0)
printf("%s loaded.\n", "File");
else {
printf("%s not found.\n", "File");
continue;
}
}
interpret(big_buffer);
}
return 0; // not reached
}
#else
static char* readFile(const char* path) {
FILE* file = fopen(path, "rb");
if (file == NULL) {
fprintf(stderr, "Could not open \"%s\".\n", path);
return NULL;
}
fseek(file, 0L, SEEK_END);
size_t fileSize = ftell(file);
rewind(file);
char* buffer = (char*)malloc(fileSize + 1);
if (buffer == NULL) {
fprintf(stderr, "Not enough memory to read \"%s\".\n", path);
return NULL;
}
size_t bytesRead = fread(buffer, sizeof(char), fileSize, file);
if (bytesRead < fileSize) {
fprintf(stderr, "Could not read \"%s\".\n", path);
return NULL;
}
buffer[bytesRead] = '\0';
fclose(file);
return buffer;
}
static bool runFile(const char* path) {
char* source = readFile(path);
if (source) {
InterpretResult result = interpret(source);
free(source);
return result == INTERPRET_OK;
}
return false;
}
static void repl(void) {
for (;;) {
putstr("> ");
if (!readLine()) {
putstr("\n");
break;
}
if (big_buffer[0] == '&')
runFile(big_buffer + 1);
else
interpret(big_buffer);
}
}
// Usage: [lw]loxd? [ <source>* [-]]
// - starts REPL after loading all sources.
//
int main(int argc, const char* argv[]) {
int arg;
init_freelist();
initVM();
printf("%s [%s] %s\n", VERSION, DBG_STR, AUTHOR);
if (argc == 1)
repl();
else {
for (arg=1; arg<argc; arg++) {
if (argv[arg][0] == '-')
repl();
else {
printf("Loading %s\n", argv[arg]);
if (!runFile(argv[arg]))
exit(10);
}
}
}
freeVM();
return 0;
}
#endif