13 #include <readline/readline.h>
14 #ifdef HAVE_LIBVNCSERVER
16 #endif /* HAVE_LIBVNCSERVER */
21 #include "hw_lem1802.h"
22 #include "hw_keyboard.h"
25 * shell-like driver for dcpu16 core
26 * provides a basic interface to control a single emulation instance
28 * Justin Wind <justin.wind@gmail.com>
29 * 2012 04 10 - implementation started
30 * 2012 04 12 - cleanup, better shell loop
31 * 2012 05 12 - support v1.7 style devices
34 * handle quotes in shell command parsing
35 * use readline/history.h, since we're using readline anyhow
36 * ncurses windowing or something, for future display capabilities
39 static const char * const src_id_
= "$Id$";
41 /* global invocation options */
48 /* global run state, first sigint caught will drop out of run loop and back into shell */
49 static volatile unsigned int running_
= 0;
51 void sigint_handler_(int sig
) {
56 #define VERBOSE_PRINTF(...) do { if (opt_.verbose) printf(__VA_ARGS__); } while (0)
59 void usage_(char *prog
, unsigned int full
) {
60 FILE *f
= full
? stdout
: stderr
;
61 char *x
= strrchr(prog
, '/');
67 fprintf(f
, "%s -- dcpu16 emulator core shell\n\n",
70 fprintf(f
, "Usage: %s [-v] [file]\n",
74 fprintf(f
, "\nOptions:\n"
75 "\t [file] -- ram image to load initially\n"
76 "\t -v -- prints slightly more information while operating\n"
77 "\t -h -- this screen\n");
79 fprintf(f
, "\n%78s\n", src_id_
);
84 /* flense a buffer into a newly-allocated argument list */
86 int buf_tok_vect_(char ***v
, int *c
, char *buf
) {
87 const char *sep
= " \t";
88 const char *quot
= "\"'`";
89 const size_t v_grow
= 32;
94 *v
= malloc(v_sz
* sizeof **v
);
96 fprintf(stderr
, "%s():%s\n", "malloc", strerror(errno
));
100 for ( (*v
)[*c
] = strqtok_r(buf
, sep
, '\\', quot
, &qt
, &st
);
102 (*v
)[*c
] = strqtok_r(NULL
, sep
, '\\', quot
, &qt
, &st
)
106 if ((size_t)(*c
) == v_sz
) {
107 void *tmp_ptr
= realloc(*v
, (v_sz
+ v_grow
) * sizeof **v
);
108 if (tmp_ptr
== NULL
) {
109 fprintf(stderr
, "%s():%s\n", "realloc", strerror(errno
));
122 resets the vm if addr is zero then
123 loads an image from filename into ram starting at addr
126 int file_load_(struct dcpu16
*vm
, char *filename
, DCPU16_WORD addr
) {
133 f
= fopen(filename
, "rb");
135 fprintf(stderr
, "%s('%s'):%s\n", "fopen", filename
, strerror(errno
));
139 r
= fread(vm
->ram
+ addr
, sizeof(DCPU16_WORD
), DCPU16_RAM
- addr
, f
);
140 VERBOSE_PRINTF("read %zu words", r
);
141 if (addr
) VERBOSE_PRINTF(" starting at 0x%04x", addr
);
142 VERBOSE_PRINTF("\n");
145 fprintf(stderr
, "%s('%s'):%s\n", "fread", filename
, strerror(errno
));
152 * print raw ram contents from start to stop
155 void dump_ram_(struct dcpu16
*vm
, DCPU16_WORD start
, DCPU16_WORD end
) {
157 const unsigned int n
= 8; /* words per line */
161 for (i
= start
, j
= 0; i
<= end
; i
++, j
++) {
163 printf("0x%04x:\t", i
);
164 printf(" %04x%s", vm
->ram
[i
], (j
% n
) == (n
- 1) ? "\n" : "");
166 if ((j
% n
) != (n
- 1))
172 print the current state of the machine
173 shows current cycle count, registers, and next instruction
176 void state_print_(struct dcpu16
*vm
) {
182 for (i
= 0; i
< 8; i
++)
183 printf(" %s:0x%04x", dcpu16_reg_names
[i
], vm
->reg
[i
]);
186 printf("(0x%08llx) %2s:0x%04x %2s:0x%04x %2s:0x%04x %2s:0x%04x [%2s]:",
188 dcpu16_reg_names
[DCPU16_REG_EX
], vm
->reg
[DCPU16_REG_EX
],
189 dcpu16_reg_names
[DCPU16_REG_SP
], vm
->reg
[DCPU16_REG_SP
],
190 dcpu16_reg_names
[DCPU16_REG_PC
], vm
->reg
[DCPU16_REG_PC
],
191 dcpu16_reg_names
[DCPU16_REG_IA
], vm
->reg
[DCPU16_REG_IA
],
194 dcpu16_disassemble_print(vm
, vm
->reg
[DCPU16_REG_PC
]);
199 #ifdef HAVE_LIBVNCSERVER
200 static struct dynamic_array rfbScreens_
;
201 /* wups, kbdAddEvent isn't null by default, so I guess track associations externally */
202 struct rfb_instance_
{
203 rfbScreenInfoPtr screen
;
204 struct dcpu16_hw
*attached_display
;
205 struct dcpu16_hw
*attached_keyboard
;
208 /* locate or allocate the next display with an un-occupied framebuffer */
210 struct rfb_instance_
*rfbScreen_next_available_display_(struct dynamic_array
*rfbScreens
, int argc
, char *argv
[]) {
212 struct rfb_instance_ new_instance
, *s
;
213 struct packed_args_
{
216 } parg
= { argc
, argv
};
218 fprintf(stderr
, "DEBUG: rfbScreens->entries:%zu\n", rfbScreens
->entries
);
220 for (i
= 0; i
< rfbScreens
->entries
; i
++) {
221 s
= (struct rfb_instance_
*)DYNARRAY_ITEM(*rfbScreens
, i
);
222 if (s
->attached_display
== NULL
)
226 if (dcpu16_hw_module_lem1802
.ctl(NULL
, "new_rfbScreen", &parg
, &new_instance
.screen
)) {
227 fprintf(stderr
, "failed to allocate new rfbScreen");
231 new_instance
.attached_display
= NULL
;
232 new_instance
.attached_keyboard
= NULL
;
233 s
= dynarray_add(rfbScreens
, &new_instance
);
237 /* locate or allocate the next display with an un-occupied keyboard */
239 struct rfb_instance_
*rfbScreen_next_available_keyboard_(struct dynamic_array
*rfbScreens
, int argc
, char *argv
[]) {
241 struct rfb_instance_ new_instance
, *s
;
242 struct packed_args_
{
245 } parg
= { argc
, argv
};
247 for (i
= 0; i
< rfbScreens
->entries
; i
++) {
248 s
= (struct rfb_instance_
*)DYNARRAY_ITEM(*rfbScreens
, i
);
249 if (s
->attached_keyboard
== NULL
)
253 if (dcpu16_hw_module_lem1802
.ctl(NULL
, "new_rfbScreen", &parg
, &new_instance
.screen
)) {
254 fprintf(stderr
, "failed to allocate new rfbScreen");
258 new_instance
.attached_display
= NULL
;
259 new_instance
.attached_keyboard
= NULL
;
260 s
= dynarray_add(rfbScreens
, &new_instance
);
264 /* begin serving a screen */
265 void rfbScreen_start(rfbScreenInfoPtr s
) {
267 rfbRunEventLoop(s
, -1, TRUE
);
269 #endif /* HAVE_LIBVNCSERVER */
272 Here follows the various commands the shell can execute.
274 At invocation, a command function will have already had its
275 number of arguments vetted, but will need command-specific
276 argument verifications done.
278 The arg_vector contains the command as the first entry, and
279 as such, arg_count will always be at least 1.
280 However, the args_min and args_max entries in struct command_
281 only refer to the counts of arguments, not the entries in the
289 int (*func
)(struct dcpu16
*, int c
, char **v
);
290 void (*help
)(FILE *f
, unsigned int);
293 #define COMMAND_IMPL(x) static int command_##x##_(struct dcpu16 *vm, int arg_count, char **arg_vector)
294 #define COMMAND_HELP(x) static void command_##x##_help_(FILE *f, unsigned int summary)
295 #define COMMAND_ENTRY(x, y, z) { #x, y, z, command_##x##_, command_##x##_help_ }
299 (void)vm
, (void)arg_count
, (void)arg_vector
;
304 fprintf(f
, "\tquit\n");
307 fprintf(f
, "Exits the emulator.\n");
311 COMMAND_IMPL(reset
) {
312 (void)arg_count
, (void)arg_vector
;
315 printf("initialized\n");
318 COMMAND_HELP(reset
) {
319 fprintf(f
, "\treset\n");
322 fprintf(f
, "Clears and reinitializes emulator.\n");
330 addr
= str_to_word(arg_vector
[2]);
332 fprintf(stderr
, "address '%s' is not a valid word: %s\n", arg_vector
[2], strerror(errno
));
337 if (file_load_(vm
, arg_vector
[1], addr
)) {
338 fprintf(stderr
, "failed to load '%s'\n", arg_vector
[1]);
341 printf("loaded '%s'", arg_vector
[1]);
342 if (addr
) printf(" starting at 0x%04x", addr
);
348 fprintf(f
, "\tload file [addr]\n");
351 fprintf(f
, "Load binary image from 'file' into ram.\n");
359 for (i
= 1; i
< arg_count
; i
++) {
360 addr
[i
-1] = str_to_word(arg_vector
[i
]);
362 fprintf(stderr
, "address '%s' is not a valid word: %s\n", arg_vector
[i
], strerror(errno
));
366 if (arg_count
< 2) addr
[0] = vm
->reg
[DCPU16_REG_PC
];
367 if (arg_count
< 3) addr
[1] = addr
[0];
369 if (addr
[1] < addr
[0]) {
370 fprintf(stderr
, "\t'addr_start' must be before addr_end\n");
374 dump_ram_(vm
, addr
[0], addr
[1]);
379 fprintf(f
, "\tdump [addr_start [addr_end]]\n");
382 fprintf(f
, "Displays contents of ram from addr_start to addr_end.\n");
386 COMMAND_IMPL(disassemble
) {
390 for (i
= 1; i
< arg_count
; i
++) {
391 addr
[i
-1] = str_to_word(arg_vector
[i
]);
393 fprintf(stderr
, "address '%s' is not a valid word: %s\n", arg_vector
[i
], strerror(errno
));
397 if (arg_count
< 2) addr
[0] = vm
->reg
[DCPU16_REG_PC
];
398 if (arg_count
< 3) addr
[1] = addr
[0];
400 if (addr
[1] < addr
[0]) {
401 fprintf(stderr
, "\t'addr_start' must be before addr_end\n");
405 for (i
= addr
[0]; i
<= addr
[1]; /* */ ) {
406 printf("0x%04x: ", i
);
407 i
+= dcpu16_disassemble_print(vm
, i
);
413 COMMAND_HELP(disassemble
) {
414 fprintf(f
, "\tdisassemble [addr_start [addr_end]]\n");
417 fprintf(f
, "Displays contents of ram parsed into instructions.\n");
422 unsigned long count
= 1;
425 if (arg_count
== 2) {
427 count
= strtoul(arg_vector
[1], &ep
, 0);
429 || !(*arg_vector
[1] && *ep
== '\0') ) {
430 fprintf(stderr
, "count '%s' is not a valid number: %s\n", arg_vector
[1], strerror(errno
));
435 fprintf(stderr
, "count must be positive\n");
441 dcpu16_disassemble_print(vm
, vm
->reg
[DCPU16_REG_PC
]);
445 if (count
> 1 && opt_
.verbose
)
452 fprintf(f
, "\tstep [count]\n");
455 fprintf(f
, "Executes the next instruction, or the next count instructions.\n");
465 /* check if addr is a register */
466 for (addr
= 0; dcpu16_reg_names
[addr
]; addr
++) {
467 if (strcasecmp(arg_vector
[1], dcpu16_reg_names
[addr
]) == 0)
470 if (addr
< DCPU16_REG__NUM
) {
473 addr
= str_to_word(arg_vector
[1]);
475 fprintf(stderr
, "address '%s' is not a valid word: %s\n", arg_vector
[1], strerror(errno
));
481 value
= str_to_word(arg_vector
[2]);
483 fprintf(stderr
, "address '%s' is not a valid word: %s\n", arg_vector
[2], strerror(errno
));
493 fprintf(f
, "\tset addr value\n");
496 fprintf(f
, "Sets addr to value.");
499 #define MICROSECONDS_PER_CYCLE 10
501 struct sigaction act
;
502 struct timeval run_start_tv
, run_stop_tv
;
503 long long run_cycle_start
;
504 struct timeval start_tv
, now_tv
, diff_tv
;
505 long long cycle_start
, cycles_to_wait
;
506 struct timespec sleep_time
, rem_time
;
509 (void)arg_count
, (void)arg_vector
;
512 gettimeofday(&run_start_tv
, NULL
);
513 run_cycle_start
= vm
->cycle_
;
515 memset(&act
, 0, sizeof act
);
516 act
.sa_handler
= sigint_handler_
;
517 act
.sa_flags
= SA_RESETHAND
;
519 if (sigaction(SIGINT
, &act
, NULL
)) {
520 fprintf(stderr
, "%s():%s\n", "sigaction", strerror(errno
));
525 gettimeofday(&start_tv
, NULL
);
526 cycle_start
= vm
->cycle_
;
529 if (opt_
.verbose
> 1)
531 else if (opt_
.verbose
) {
532 dcpu16_disassemble_print(vm
, vm
->reg
[DCPU16_REG_PC
]);
536 /* how many cycles did this instr use? */
537 cycles_to_wait
= vm
->cycle_
- cycle_start
;
539 if (cycles_to_wait
== 0)
542 /* each cycle wants 10 microseconds */
544 /* how much of that did we spend already */
545 gettimeofday(&now_tv
, NULL
);
546 timeval_subtract(&diff_tv
, &now_tv
, &start_tv
);
547 /* do we have time to kill? */
548 if (cycles_to_wait
* MICROSECONDS_PER_CYCLE
> diff_tv
.tv_usec
) {
549 sleep_time
.tv_sec
= diff_tv
.tv_sec
;
550 /* this is not accurate.. */
551 sleep_time
.tv_nsec
= 250 * ( (cycles_to_wait
* MICROSECONDS_PER_CYCLE
) - diff_tv
.tv_usec
);
553 /* nanosleep doesn't interfere with libvncserver, unlike usleep */
554 while ( nanosleep(&sleep_time
, &rem_time
) ) {
555 sleep_time
= rem_time
;
556 fprintf(stderr
, "rem:%ld %ld\n", rem_time
.tv_sec
, rem_time
.tv_nsec
);
561 gettimeofday(&run_stop_tv
, NULL
);
562 timeval_subtract(&diff_tv
, &run_stop_tv
, &run_start_tv
);
563 run_usec
= diff_tv
.tv_sec
* 1000000;
564 run_usec
+= diff_tv
.tv_usec
;
565 fprintf(stderr
, "ran %llu cycles in %lds %dus (%lldus)\n",
566 vm
->cycle_
- run_cycle_start
,
571 printf("interrupted...\n");
576 fprintf(f
, "\trun\n");
579 fprintf(f
, "Begins executing continuously.\n"
580 "May be interrupted with SIGINT.\n");
583 static const char * const display_filename_default_
=
586 #else /* HAVE_LIBPNG */
588 #endif /* HAVE_LIBPNG */
590 COMMAND_IMPL(display
) {
591 struct dcpu16_hw
*hw
;
592 const char *renderer
= arg_vector
[1];
593 const char *renderer_arg
= NULL
;
597 renderer_arg
= arg_vector
[2];
599 hw
= dcpu16_hw_new(vm
, &dcpu16_hw_module_lem1802
, NULL
);
601 fprintf(stderr
, "failed to initialize new display\n");
605 /* handle per-renderer setup of data.. */
606 /* FIXME: these are awkward */
607 if (strcmp(renderer
, "pnm") == 0) {
608 renderer_data
= (void *)(renderer_arg
? renderer_arg
: display_filename_default_
);
612 if (strcmp(renderer
, "png") == 0) {
613 renderer_data
= (void *)(renderer_arg
? renderer_arg
: display_filename_default_
);
615 #endif /* HAVE_LIBPNG */
617 #ifdef HAVE_LIBVNCSERVER
618 if (strcmp(renderer
, "vnc") == 0) {
620 char *argv
[] = { "vm-dcpu16", NULL
};
621 struct rfb_instance_
*s
;
623 s
= rfbScreen_next_available_display_(&rfbScreens_
, argc
, argv
);
625 fprintf(stderr
, "failed to initialize vnc\n");
630 if (dcpu16_hw_ctl(hw
, "associate_rfbScreen", s
->screen
, NULL
)) {
631 fprintf(stderr
, "failed to configure display/vnc");
635 s
->attached_display
= hw
;
636 rfbScreen_start(s
->screen
);
637 renderer_data
= s
->screen
;
639 #endif /* HAVE_LIBVNCSERVER */
641 dcpu16_hw_ctl(hw
, "renderer", (char *)renderer
, NULL
);
642 dcpu16_hw_ctl(hw
, "renderer_data", renderer_data
, NULL
);
644 if (dcpu16_hw_attach(vm
, hw
)) {
645 fprintf(stderr
, "failed to attach new display\n");
652 COMMAND_HELP(display
) {
659 fprintf(f
, "\tdisplay renderer [renderer data]\n");
662 fprintf(f
, "Attaches new display unit, using 'renderer' as back-end output.\n"
665 fprintf(f
, "Supported renderers:\n");
669 if (dcpu16_hw_module_lem1802
.ctl(NULL
, "renderers_iter", &iter
, &renderer
)) {
670 fprintf(stderr
, "error fetching next renderer\n");
673 if (iter
== NULL
|| renderer
.name
== NULL
)
676 fprintf(f
, "\t%s %s\n", renderer
.name
, renderer
.args
);
680 COMMAND_IMPL(keyboard
) {
681 struct dcpu16_hw
*hw
;
683 (void)arg_count
, (void)arg_vector
;
685 hw
= dcpu16_hw_new(vm
, &dcpu16_hw_module_keyboard
, NULL
);
687 fprintf(stderr
, "failed to initialize new keyboard\n");
691 #ifdef HAVE_LIBVNCSERVER
692 struct rfb_instance_
*s
;
694 char *argv
[] = { "vm-dcpu16", NULL
};
696 s
= rfbScreen_next_available_keyboard_(&rfbScreens_
, argc
, argv
);
698 fprintf(stderr
, "failed to initialize vnc\n");
702 if (dcpu16_hw_ctl(hw
, "associate_rfbScreen", s
->screen
, NULL
)) {
703 fprintf(stderr
, "failed to configure keyboard/vnc\n");
707 s
->attached_keyboard
= hw
;
709 if (dcpu16_hw_attach(vm
, hw
)) {
710 fprintf(stderr
, "failed to attach new keyboard\n");
714 #endif /* HAVE_LIBVNCSERVER */
718 COMMAND_HELP(keyboard
) {
719 fprintf(f
, "\tkeyboard\n");
722 fprintf(f
, "Attaches new keyboard unit.\n");
725 /* gather all these together into a searchable table */
727 /* help command gets some assistance in declarations */
731 static struct command_ command_table_
[] = {
732 COMMAND_ENTRY(help
, 0, -1),
733 COMMAND_ENTRY(quit
, 0, -1),
734 COMMAND_ENTRY(load
, 1, 2),
735 COMMAND_ENTRY(dump
, 0, 2),
736 COMMAND_ENTRY(disassemble
, 0, 2),
737 COMMAND_ENTRY(step
, 0, 1),
738 COMMAND_ENTRY(run
, 0, 0),
739 COMMAND_ENTRY(set
, 2, 2),
740 COMMAND_ENTRY(reset
, 0, 0),
741 COMMAND_ENTRY(display
, 1, 2),
742 COMMAND_ENTRY(keyboard
, 0, 0),
743 { NULL
, 0, 0, NULL
, NULL
}
750 if (arg_count
== 2) {
751 for (c
= command_table_
; c
->func
; c
++) {
752 if (strcasecmp(arg_vector
[1], c
->name
) == 0) {
761 for (c
= command_table_
; c
->func
; c
++) {
768 fprintf(f
, "\thelp [command]\n");
771 fprintf(f
, "Displays a list of available commands, or detailed help on a specific command.\n");
775 int main(int argc
, char **argv
) {
776 const char prompt_fmt
[] = "PC:%04x> ";
779 char *line
, *line_prev
;
780 char **tok_v
, **tok_v_prev
;
781 int tok_c
, tok_c_prev
;
784 while ( (c
= getopt(argc
, argv
, "hv")) != EOF
) {
799 if (opt_
.verbose
< 1) {
800 dcpu16_warn_cb_set(NULL
);
801 dcpu16_trace_cb_set(NULL
);
802 } else if (opt_
.verbose
< 2) {
803 dcpu16_trace_cb_set(NULL
);
808 if ((vm
= dcpu16_new()) == NULL
) {
809 fprintf(stderr
, "could not allocate new dcpu16 instance\n");
810 exit(EX_UNAVAILABLE
);
813 #ifdef HAVE_LIBVNCSERVER
814 if (dynarray_init(&rfbScreens_
, sizeof(struct rfb_instance_
), 4)) {
815 fprintf(stderr
, "could not allocate rfb container\n");
816 exit(EX_UNAVAILABLE
);
818 #endif /* HAVE_LIBVNCSERVER */
821 if (file_load_(vm
, *argv
, 0)) {
822 fprintf(stderr
, "couldn't load '%s'\n", *argv
);
827 /* show state, read commands */
828 for (line
= line_prev
= NULL
,
829 tok_v
= tok_v_prev
= NULL
,
830 tok_c
= tok_c_prev
= 0,
831 snprintf(prompt
, sizeof prompt
, prompt_fmt
, vm
->reg
[DCPU16_REG_PC
]),
834 (line
= readline(prompt
));
837 snprintf(prompt
, sizeof prompt
, prompt_fmt
, vm
->reg
[DCPU16_REG_PC
]),
839 const char whitespace
[] = " \t";
844 /* skip whitespaces */
845 line_start
= line
+ strspn(line
, whitespace
);
848 /* a new command, remember previous for possible repetition */
850 /* turn new line into new arg array */
851 if (buf_tok_vect_(&tok_v
, &tok_c
, line_start
)) {
852 fprintf(stderr
, "failed to process command\n");
856 /* and keep track if it all for the next time around */
857 if (line_prev
) free(line_prev
);
860 if (tok_v_prev
) free(tok_v_prev
);
864 /* blank new command, but no prior command to repeat? ask again */
865 if (tok_v_prev
== NULL
|| tok_v_prev
[0] == NULL
|| *(tok_v_prev
[0]) == '\0') {
870 /* otherwise discard new line and promote prior */
877 /* look up command */
878 for (c
= command_table_
; c
->name
; c
++) {
879 if (strcasecmp(tok_v
[0], c
->name
) == 0) {
880 if (c
->args_min
> tok_c
- 1) {
881 fprintf(stderr
, "%s: not enough arguments\n", c
->name
);
887 && tok_c
- 1 > c
->args_max
) {
888 fprintf(stderr
, "%s: too many arguments\n", c
->name
);
893 r
= c
->func(vm
, tok_c
, tok_v
);
901 fprintf(stderr
, "didn't recognize '%s'\n", tok_v
[0]);
904 printf("\nfinished\n");