9 #include <readline/readline.h>
10 #include <readline/history.h>
15 * cli driver for dcpu16 core
17 * Justin Wind <justin.wind@gmail.com>
18 * 2012 04 10 - implementation started
22 static const char * const src_id_
= "$Id$";
24 /* global invocation options */
31 #define VERBOSE_PRINTF(...) do { if (opt_.verbose) printf(__VA_ARGS__); } while (0)
33 static void usage_(char *prog
, unsigned int full
) {
34 FILE *f
= full
? stdout
: stderr
;
35 char *x
= strrchr(prog
, '/');
41 fprintf(f
, "%s -- \n\n",
44 fprintf(f
, "Usage: %s [file]\n",
48 fprintf(f
, "\nOptions:\n"
49 "\t [file] -- ram image to load initially\n"
50 "\t -h -- this screen\n"
51 "\t -v -- verbose execution tracing\n");
53 fprintf(f
, "\n%78s\n", src_id_
);
58 int file_load_(struct dcpu16
*vm
, char *filename
) {
62 f
= fopen(filename
, "rb");
64 fprintf(stderr
, "%s('%s'):%s\n", "fopen", filename
, strerror(errno
));
68 r
= fread(vm
->ram
, sizeof(DCPU16_WORD
), DCPU16_RAM
, f
);
69 VERBOSE_PRINTF("read %zu words", r
);
72 fprintf(stderr
, "%s('%s'):%s\n", "fread", filename
, strerror(errno
));
78 int main(int argc
, char **argv
) {
81 char *prompt
= "dcpu16> ";
84 while ( (c
= getopt(argc
, argv
, "hv")) != EOF
) {
99 if (opt_
.verbose
< 1) {
100 dcpu16_warn_cb_set(NULL
);
101 dcpu16_trace_cb_set(NULL
);
102 } else if (opt_
.verbose
< 2) {
103 dcpu16_trace_cb_set(NULL
);
108 if ((vm
= dcpu16_new()) == NULL
) {
109 fprintf(stderr
, "could not allocate new dcpu instance\n");
110 exit(EX_UNAVAILABLE
);
114 file_load_(vm
, *argv
);
117 dcpu16_state_print(vm
);
118 dcpu16_disassemble_print(vm
, vm
->pc
);
119 while ( (command
= readline(prompt
)) ) {
120 if (strcasecmp(command
, "quit") == 0)
124 dcpu16_state_print(vm
);
125 dcpu16_disassemble_print(vm
, vm
->pc
);
128 printf("finished\n");