-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
261 lines (223 loc) · 6.49 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
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
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <limits.h>
#include <portaudio.h>
#include "platform.h"
#ifdef __MINGW32__
#define NEED_COLOR_FIX
#endif
#ifdef NEED_COLOR_FIX
#include <windows.h>
#define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004
#endif
#include "v68.h"
#include "v68io.h"
#include "tools.h"
#define DEFAULT_CPU_CLOCK 8000000
#define DEFAULT_RAM_SIZE 8 * 1024 * 1024
#define DEFAULT_SAMPLE_RATE 44100
#define DEFAULT_BUFFER_SIZE 4096
int opt_cpu_clock = DEFAULT_CPU_CLOCK;
int opt_ram_size = DEFAULT_RAM_SIZE;
int opt_sample_rate = DEFAULT_SAMPLE_RATE;
int opt_buffer_size = DEFAULT_BUFFER_SIZE;
char *opt_vgm_file = 0;
int opt_utf8 = 0;
int opt_verbosity = 0;
int opt_log_calls = 0;
int opt_dasm = 0;
char cmd_queue[V68_CMD_QUEUE_LEN][1024];
int cmd_queue_pos;
void queue_cmd(char *cmdline) {
if(cmd_queue_pos > V68_CMD_QUEUE_LEN) return;
strncpy(cmd_queue[cmd_queue_pos], cmdline, sizeof(cmd_queue[0]));
cmd_queue_pos++;
}
/* TODO: parse k, M, G etc */
int parse_intval(char *s) {
return atoi(s);
}
int running = 1;
void sighandler(int signum) {
signal(signum, NULL);
printf("Caught signal %d, coming out...\n", signum);
running = 0;
}
void print_help(char *argv0) {
printf("Usage: %s [options] command [args]\n", argv0);
printf("Options:\n");
printf("\t-c <command> Execute this command before main command. Example: -c mxdrv.x.\n");
printf("\t-C <clock> CPU clock. Default is %dMHz.\n", DEFAULT_CPU_CLOCK / 1000000);
printf("\t-M <size> Specify size of RAM. Default is %dM.\n", DEFAULT_RAM_SIZE / 1024 / 1024);
printf("\t-R <rate> Sample rate. Default is %d.\n", DEFAULT_SAMPLE_RATE);
printf("\t-b <size> Audio buffer size. Default is %d.\n", DEFAULT_BUFFER_SIZE);
printf("\t-l <logfile.vgm> VGM file logging.\n");
printf("\t-u Convert output to utf-8.\n");
printf("\t-v Increase verbosity by 1.\n");
printf("\t-t Trace system calls (DOS, FE and IOCS).\n");
printf("\t-d Disassemble instructions as they are executed.\n");
}
int parse_cmdline(int argc, char **argv) {
int first_nonarg_reached = 0;
char cmdbuf[1024];
cmdbuf[0] = 0;
for(int i = 1; i < argc; i++) {
if(!first_nonarg_reached) {
if(!strcmp(argv[i], "-c")) {
if(argc > i+1) {
i++;
queue_cmd(argv[i]);
} else {
fprintf(stderr, "-c requires an argument\n");
}
} else if (!strcmp(argv[i], "-C")) {
i++;
opt_cpu_clock = parse_intval(argv[i]);
} else if (!strcmp(argv[i], "-M")) {
i++;
opt_ram_size = parse_intval(argv[i]);
} else if (!strcmp(argv[i], "-R")) {
i++;
opt_sample_rate = parse_intval(argv[i]);
} else if (!strcmp(argv[i], "-b")) {
i++;
opt_buffer_size = parse_intval(argv[i]);
} else if (!strcmp(argv[i], "-l")) {
i++;
opt_vgm_file = strdup(argv[i]);
} else if (!strcmp(argv[i], "-u")) {
opt_utf8 = 1;
} else if (!strcmp(argv[i], "-vvv")) {
opt_verbosity+=3;
} else if (!strcmp(argv[i], "-vv")) {
opt_verbosity+=2;
} else if (!strcmp(argv[i], "-v")) {
opt_verbosity++;
} else if (!strcmp(argv[i], "-t")) {
opt_log_calls = 1;
} else if (!strcmp(argv[i], "-d")) {
opt_dasm = 1;
} else if(argv[i][0] == '-') {
print_help(argv[0]);
return 1;
} else {
first_nonarg_reached = 1;
}
}
if(first_nonarg_reached) {
strncat(cmdbuf, argv[i], sizeof(cmdbuf) - strlen(cmdbuf));
if(i + 1 < argc)
strncat(cmdbuf, " ", sizeof(cmdbuf) - strlen(cmdbuf));
}
}
if(cmdbuf[0]) {
queue_cmd(cmdbuf);
return 0;
}
print_help(argv[0]);
return 1;
}
int main(int argc, char **argv, char **envp) {
#ifdef NEED_COLOR_FIX
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
if (handle != INVALID_HANDLE_VALUE) {
DWORD mode = 0;
if (GetConsoleMode(handle, &mode)) {
mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
SetConsoleMode(handle, mode);
}
}
#endif
if(parse_cmdline(argc, argv)) {
fprintf(stderr, "Could not parse command line\n");
return -1;
}
int er = v68_init(opt_cpu_clock, opt_ram_size, opt_sample_rate);
if(er) {
fprintf(stderr, "Could not init VM! (%x)\n", er);
return -1;
}
v68.log_calls = opt_log_calls;
v68.log_dasm = opt_dasm;
v68.verbosity = opt_verbosity;
v68_boot();
for(int i = 0; i < cmd_queue_pos; i++) {
v68_queue_command(cmd_queue[i]);
}
v68_io_autodetect_drives();
v68_dump_drives();
char *v68_path = getenv("V68_PATH");
v68_env_set("PATH", v68_path ? v68_path : getenv("PATH"));
for(char **e = envp; *e; e++) {
v68_env_append(*e);
}
v68_run();
verbose1("v68_run finished, sound_touched=%d\n", v68.sound_touched);
PaStream *stream;
PaError err;
if(v68.sound_touched) {
verbose1("init sound\n");
err = Pa_Initialize();
if( err != paNoError ) goto error;
err = Pa_OpenDefaultStream(
&stream,
0, /* no input channels */
2, /* stereo output */
paInt16, /* 16 bit floating point output */
opt_sample_rate,
opt_buffer_size, /* frames per buffer */
NULL,
NULL
);
if( err != paNoError ) goto error;
err = Pa_StartStream( stream );
if( err != paNoError ) goto error;
signal(SIGINT, sighandler);
if(opt_vgm_file && opt_vgm_file[0]) {
struct vgm_logger l;
vgm_logger_begin(&l, opt_vgm_file);
v68.logger = &l;
}
#define ALLOCBUF(b, s) \
int16_t *b = malloc(s); \
if(!b) { \
fprintf(stderr, "Could not allocate %"PRIuSIZET" bytes sound bufer\n", s); \
return 1; \
}
ALLOCBUF(bufL, opt_buffer_size * sizeof(*bufL));
ALLOCBUF(bufR, opt_buffer_size * sizeof(*bufR));
ALLOCBUF(buf, opt_buffer_size * 2 * sizeof(*buf));
/* -- Play some stuff -- */
while(running) {
v68_fill_buffer(opt_buffer_size, bufL, bufR);
for(int i = 0; i < opt_buffer_size; i++) {
buf[i * 2] = bufL[i];
buf[i * 2 + 1] = bufR[i];
}
err = Pa_WriteStream( stream, buf, opt_buffer_size);
if( err ) goto error;
}
/* -- Close and shutdown -- */
err = Pa_StopStream( stream );
if( err != paNoError ) goto error;
err = Pa_CloseStream( stream );
if( err != paNoError ) goto error;
Pa_Terminate();
if(v68.logger)
vgm_logger_end(v68.logger);
}
v68_shutdown();
return err;
error:
Pa_Terminate();
fprintf( stderr, "An error occured while using the portaudio stream\n" );
fprintf( stderr, "Error number: %d\n", err );
fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
return err;
}