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 #ifdef HAVE_LIBVNCSERVER
153 static struct dynamic_array rfbScreens_
;
154 /* wups, kbdAddEvent isn't null by default, so I guess track things externally */
155 struct rfb_instance_
{
156 rfbScreenInfoPtr screen
;
157 struct dcpu16_hw
*attached_display
;
158 struct dcpu16_hw
*attached_keyboard
;
161 /* locate or allocate the next display with an un-occupied framebuffer */
163 struct rfb_instance_
*rfbScreen_next_available_display_(struct dynamic_array
*rfbScreens
, int argc
, char *argv
[]) {
165 struct rfb_instance_ new_instance
, *s
;
167 fprintf(stderr
, "DEBUG: rfbScreens->entries:%zu\n", rfbScreens
->entries
);
169 for (i
= 0; i
< rfbScreens
->entries
; i
++) {
170 s
= (struct rfb_instance_
*)DYNARRAY_ITEM(*rfbScreens
, i
);
171 if (s
->attached_display
== NULL
)
175 new_instance
.screen
= lem1802_rfb_new(argc
, argv
);
176 new_instance
.attached_display
= NULL
;
177 new_instance
.attached_keyboard
= NULL
;
178 s
= dynarray_add(rfbScreens
, &new_instance
);
182 /* locate or allocate the next display with an un-occupied keyboard */
184 struct rfb_instance_
*rfbScreen_next_available_keyboard_(struct dynamic_array
*rfbScreens
, int argc
, char *argv
[]) {
186 struct rfb_instance_ new_instance
, *s
;
188 for (i
= 0; i
< rfbScreens
->entries
; i
++) {
189 s
= (struct rfb_instance_
*)DYNARRAY_ITEM(*rfbScreens
, i
);
190 if (s
->attached_keyboard
== NULL
)
194 new_instance
.screen
= lem1802_rfb_new(argc
, argv
);
195 new_instance
.attached_display
= NULL
;
196 new_instance
.attached_keyboard
= NULL
;
197 s
= dynarray_add(rfbScreens
, &new_instance
);
201 /* begin serving a screen */
202 void rfbScreen_start(rfbScreenInfoPtr s
) {
204 rfbRunEventLoop(s
, -1, TRUE
);
206 #endif /* HAVE_LIBVNCSERVER */
209 Here follows the various commands the shell can execute.
211 At invocation, a command function will have already had its
212 number of arguments vetted, but will need command-specific
213 argument verifications done.
215 The arg_vector contains the command as the first entry, and
216 as such, arg_count will always be at least 1.
217 However, the args_min and args_max entries in struct command_
218 only refer to the counts of arguments, not the entries in the
226 int (*func
)(struct dcpu16
*, int c
, char **v
);
227 void (*help
)(FILE *f
, unsigned int);
230 #define COMMAND_IMPL(x) static int command_##x##_(struct dcpu16 *vm, int arg_count, char **arg_vector)
231 #define COMMAND_HELP(x) static void command_##x##_help_(FILE *f, unsigned int summary)
232 #define COMMAND_ENTRY(x, y, z) { #x, y, z, command_##x##_, command_##x##_help_ }
236 (void)vm
, (void)arg_count
, (void)arg_vector
;
241 fprintf(f
, "\tquit\n");
244 fprintf(f
, "Exits the emulator.\n");
248 COMMAND_IMPL(reset
) {
249 (void)arg_count
, (void)arg_vector
;
252 printf("initialized\n");
255 COMMAND_HELP(reset
) {
256 fprintf(f
, "\treset\n");
259 fprintf(f
, "Clears and reinitializes emulator.\n");
267 addr
= str_to_word(arg_vector
[2]);
269 fprintf(stderr
, "address '%s' is not a valid word: %s\n", arg_vector
[2], strerror(errno
));
274 if (file_load_(vm
, arg_vector
[1], addr
)) {
275 fprintf(stderr
, "failed to load '%s'\n", arg_vector
[1]);
278 printf("loaded '%s'", arg_vector
[1]);
279 if (addr
) printf(" starting at 0x%04x", addr
);
285 fprintf(f
, "\tload file [addr]\n");
288 fprintf(f
, "Load binary image from 'file' into ram.\n");
296 for (i
= 1; i
< arg_count
; i
++) {
297 addr
[i
-1] = str_to_word(arg_vector
[i
]);
299 fprintf(stderr
, "address '%s' is not a valid word: %s\n", arg_vector
[i
], strerror(errno
));
303 if (arg_count
< 2) addr
[0] = vm
->reg
[DCPU16_REG_PC
];
304 if (arg_count
< 3) addr
[1] = addr
[0];
306 if (addr
[1] < addr
[0]) {
307 fprintf(stderr
, "\t'addr_start' must be before addr_end\n");
311 dcpu16_dump_ram(vm
, addr
[0], addr
[1]);
316 fprintf(f
, "\tdump [addr_start [addr_end]]\n");
319 fprintf(f
, "Displays contents of ram from addr_start to addr_end.\n");
323 COMMAND_IMPL(disassemble
) {
327 for (i
= 1; i
< arg_count
; i
++) {
328 addr
[i
-1] = str_to_word(arg_vector
[i
]);
330 fprintf(stderr
, "address '%s' is not a valid word: %s\n", arg_vector
[i
], strerror(errno
));
334 if (arg_count
< 2) addr
[0] = vm
->reg
[DCPU16_REG_PC
];
335 if (arg_count
< 3) addr
[1] = addr
[0];
337 if (addr
[1] < addr
[0]) {
338 fprintf(stderr
, "\t'addr_start' must be before addr_end\n");
342 for (i
= addr
[0]; i
<= addr
[1]; /* */ ) {
343 printf("0x%04x: ", i
);
344 i
+= dcpu16_disassemble_print(vm
, i
);
350 COMMAND_HELP(disassemble
) {
351 fprintf(f
, "\tdisassemble [addr_start [addr_end]]\n");
354 fprintf(f
, "Displays contents of ram parsed into instructions.\n");
359 unsigned long count
= 1;
362 if (arg_count
== 2) {
364 count
= strtoul(arg_vector
[1], &ep
, 0);
366 || !(*arg_vector
[1] && *ep
== '\0') ) {
367 fprintf(stderr
, "count '%s' is not a valid number: %s\n", arg_vector
[1], strerror(errno
));
372 fprintf(stderr
, "count must be positive\n");
378 dcpu16_disassemble_print(vm
, vm
->reg
[DCPU16_REG_PC
]);
382 if (count
> 1 && opt_
.verbose
)
383 dcpu16_state_print(vm
);
389 fprintf(f
, "\tstep [count]\n");
392 fprintf(f
, "Executes the next instruction, or the next count instructions.\n");
402 /* check if addr is a register */
403 for (addr
= 0; dcpu16_reg_names
[addr
]; addr
++) {
404 if (strcasecmp(arg_vector
[1], dcpu16_reg_names
[addr
]) == 0)
407 if (addr
< DCPU16_REG__NUM
) {
410 addr
= str_to_word(arg_vector
[1]);
412 fprintf(stderr
, "address '%s' is not a valid word: %s\n", arg_vector
[1], strerror(errno
));
418 value
= str_to_word(arg_vector
[2]);
420 fprintf(stderr
, "address '%s' is not a valid word: %s\n", arg_vector
[2], strerror(errno
));
430 fprintf(f
, "\tset addr value\n");
433 fprintf(f
, "Sets addr to value.");
436 #define MICROSECONDS_PER_CYCLE 10
438 struct sigaction act
;
439 struct timeval run_start_tv
, run_stop_tv
;
440 long long run_cycle_start
;
441 struct timeval start_tv
, now_tv
, diff_tv
;
442 long long cycle_start
, cycles_to_wait
;
443 struct timespec sleep_time
, rem_time
;
446 (void)arg_count
, (void)arg_vector
;
449 gettimeofday(&run_start_tv
, NULL
);
450 run_cycle_start
= vm
->cycle
;
452 memset(&act
, 0, sizeof act
);
453 act
.sa_handler
= sigint_handler_
;
454 act
.sa_flags
= SA_RESETHAND
;
456 if (sigaction(SIGINT
, &act
, NULL
)) {
457 fprintf(stderr
, "%s():%s\n", "sigaction", strerror(errno
));
462 gettimeofday(&start_tv
, NULL
);
463 cycle_start
= vm
->cycle
;
466 if (opt_
.verbose
> 1)
467 dcpu16_state_print(vm
);
468 else if (opt_
.verbose
) {
469 dcpu16_disassemble_print(vm
, vm
->reg
[DCPU16_REG_PC
]);
473 /* how many cycles did this instr use? */
474 cycles_to_wait
= vm
->cycle
- cycle_start
;
476 if (cycles_to_wait
== 0)
479 /* each cycle wants 10 microseconds */
481 /* how much of that did we spend already */
482 gettimeofday(&now_tv
, NULL
);
483 timeval_subtract(&diff_tv
, &now_tv
, &start_tv
);
484 /* do we have time to kill? */
485 if (cycles_to_wait
* MICROSECONDS_PER_CYCLE
> diff_tv
.tv_usec
) {
486 sleep_time
.tv_sec
= diff_tv
.tv_sec
;
487 /* this is not accurate.. */
488 sleep_time
.tv_nsec
= 250 * ( (cycles_to_wait
* MICROSECONDS_PER_CYCLE
) - diff_tv
.tv_usec
);
490 /* nanosleep doesn't interfere with libvncserver, unlike usleep */
491 while ( nanosleep(&sleep_time
, &rem_time
) ) {
492 sleep_time
= rem_time
;
493 fprintf(stderr
, "rem:%ld %ld\n", rem_time
.tv_sec
, rem_time
.tv_nsec
);
498 gettimeofday(&run_stop_tv
, NULL
);
499 timeval_subtract(&diff_tv
, &run_stop_tv
, &run_start_tv
);
500 run_usec
= diff_tv
.tv_sec
* 1000000;
501 run_usec
+= diff_tv
.tv_usec
;
502 fprintf(stderr
, "ran %llu cycles in %lds %dus (%lldus)\n",
503 vm
->cycle
- run_cycle_start
,
508 printf("interrupted...\n");
513 fprintf(f
, "\trun\n");
516 fprintf(f
, "Begins executing continuously.\n"
517 "May be interrupted with SIGINT.\n");
520 static const char * const display_filename_default_
=
523 #else /* HAVE_LIBPNG */
525 #endif /* HAVE_LIBPNG */
527 COMMAND_IMPL(display
) {
528 struct dcpu16_hw
*hw
;
529 const char *renderer
= arg_vector
[1];
530 const char *renderer_arg
= NULL
;
534 renderer_arg
= arg_vector
[2];
536 hw
= lem1802_new(vm
);
538 fprintf(stderr
, "failed to initialize new display\n");
542 /* handle per-renderer setup of data.. */
543 /* FIXME: these are awkward */
544 if (strcmp(renderer
, "pnm") == 0) {
545 if (renderer_arg
== NULL
)
546 renderer_arg
= display_filename_default_
;
547 renderer_data
= (void *)renderer_arg
;
551 if (strcmp(renderer
, "png") == 0) {
552 if (renderer_arg
== NULL
)
553 renderer_arg
= display_filename_default_
;
554 renderer_data
= (void *)renderer_arg
;
556 #endif /* HAVE_LIBPNG */
558 #ifdef HAVE_LIBVNCSERVER
559 if (strcmp(renderer
, "vnc") == 0) {
561 char *argv
[] = { "vm-dcpu16", NULL
};
562 struct rfb_instance_
*s
;
564 s
= rfbScreen_next_available_display_(&rfbScreens_
, argc
, argv
);
566 fprintf(stderr
, "failed to initialize vnc\n");
571 lem1802_vnc_associate(hw
, s
->screen
);
572 s
->attached_display
= hw
;
573 rfbScreen_start(s
->screen
);
574 renderer_data
= s
->screen
;
576 #endif /* HAVE_LIBVNCSERVER */
578 if (lem1802_renderer_set(hw
, renderer
, renderer_data
)) {
579 fprintf(stderr
, "failed to set back-end renderer for display\n");
584 if (dcpu16_hw_add(vm
, hw
)) {
585 fprintf(stderr
, "failed to attach new display\n");
592 COMMAND_HELP(display
) {
596 fprintf(f
, "\tdisplay renderer [renderer data]\n");
599 fprintf(f
, "Attaches new display unit, using 'renderer' as back-end output.\n"
602 fprintf(f
, "Supported renderers:\n");
604 while ( (lem1802_renderers_iter(&iter
, &name
, &args
)) ) {
605 fprintf(f
, "\t%s %s\n", name
, args
);
609 COMMAND_IMPL(keyboard
) {
610 struct dcpu16_hw
*hw
;
612 (void)arg_count
, (void)arg_vector
;
614 hw
= keyboard_new(vm
);
616 fprintf(stderr
, "failed to initialize new keyboard\n");
620 #ifdef HAVE_LIBVNCSERVER
621 struct rfb_instance_
*s
;
623 char *argv
[] = { "vm-dcpu16", NULL
};
625 s
= rfbScreen_next_available_keyboard_(&rfbScreens_
, argc
, argv
);
627 fprintf(stderr
, "failed to initialize vnc\n");
631 keyboard_vnc_associate(hw
, s
->screen
);
632 s
->attached_keyboard
= hw
;
634 if (dcpu16_hw_add(vm
, hw
)) {
635 fprintf(stderr
, "failed to attach new keyboard\n");
639 #endif /* HAVE_LIBVNCSERVER */
643 COMMAND_HELP(keyboard
) {
644 fprintf(f
, "\tkeyboard\n");
647 fprintf(f
, "Attaches new keyboard unit.\n");
650 /* gather all these together into a searchable table */
652 /* help command gets some assistance in declarations */
656 static struct command_ command_table_
[] = {
657 COMMAND_ENTRY(help
, 0, -1),
658 COMMAND_ENTRY(quit
, 0, -1),
659 COMMAND_ENTRY(load
, 1, 2),
660 COMMAND_ENTRY(dump
, 0, 2),
661 COMMAND_ENTRY(disassemble
, 0, 2),
662 COMMAND_ENTRY(step
, 0, 1),
663 COMMAND_ENTRY(run
, 0, 0),
664 COMMAND_ENTRY(set
, 2, 2),
665 COMMAND_ENTRY(reset
, 0, 0),
666 COMMAND_ENTRY(display
, 1, 2),
667 COMMAND_ENTRY(keyboard
, 0, 0),
668 { NULL
, 0, 0, NULL
, NULL
}
675 if (arg_count
== 2) {
676 for (c
= command_table_
; c
->func
; c
++) {
677 if (strcasecmp(arg_vector
[1], c
->name
) == 0) {
686 for (c
= command_table_
; c
->func
; c
++) {
693 fprintf(f
, "\thelp [command]\n");
696 fprintf(f
, "Displays a list of available commands, or detailed help on a specific command.\n");
700 int main(int argc
, char **argv
) {
701 const char prompt_fmt
[] = "PC:%04x> ";
704 char *line
, *line_prev
;
705 char **tok_v
, **tok_v_prev
;
706 int tok_c
, tok_c_prev
;
709 while ( (c
= getopt(argc
, argv
, "hv")) != EOF
) {
724 if (opt_
.verbose
< 1) {
725 dcpu16_warn_cb_set(NULL
);
726 dcpu16_trace_cb_set(NULL
);
727 } else if (opt_
.verbose
< 2) {
728 dcpu16_trace_cb_set(NULL
);
733 if ((vm
= dcpu16_new()) == NULL
) {
734 fprintf(stderr
, "could not allocate new dcpu16 instance\n");
735 exit(EX_UNAVAILABLE
);
738 #ifdef HAVE_LIBVNCSERVER
739 if (dynarray_init(&rfbScreens_
, sizeof(struct rfb_instance_
), 4)) {
740 fprintf(stderr
, "could not allocate rfb container\n");
741 exit(EX_UNAVAILABLE
);
743 #endif /* HAVE_LIBVNCSERVER */
746 if (file_load_(vm
, *argv
, 0)) {
747 fprintf(stderr
, "couldn't load '%s'\n", *argv
);
752 /* show state, read commands */
753 for (line
= line_prev
= NULL
,
754 tok_v
= tok_v_prev
= NULL
,
755 tok_c
= tok_c_prev
= 0,
756 snprintf(prompt
, sizeof prompt
, prompt_fmt
, vm
->reg
[DCPU16_REG_PC
]),
757 dcpu16_state_print(vm
);
759 (line
= readline(prompt
));
762 snprintf(prompt
, sizeof prompt
, prompt_fmt
, vm
->reg
[DCPU16_REG_PC
]),
763 dcpu16_state_print(vm
)) {
764 const char whitespace
[] = " \t";
769 /* skip whitespaces */
770 line_start
= line
+ strspn(line
, whitespace
);
773 /* a new command, remember previous for possible repetition */
775 /* turn new line into new arg array */
776 if (buf_tok_vect_(&tok_v
, &tok_c
, line_start
)) {
777 fprintf(stderr
, "failed to process command\n");
781 /* and keep track if it all for the next time around */
782 if (line_prev
) free(line_prev
);
785 if (tok_v_prev
) free(tok_v_prev
);
789 /* blank new command, but no prior command to repeat? ask again */
790 if (tok_v_prev
== NULL
|| tok_v_prev
[0] == NULL
|| *(tok_v_prev
[0]) == '\0') {
795 /* otherwise discard new line and promote prior */
802 /* look up command */
803 for (c
= command_table_
; c
->name
; c
++) {
804 if (strcasecmp(tok_v
[0], c
->name
) == 0) {
805 if (c
->args_min
> tok_c
- 1) {
806 fprintf(stderr
, "%s: not enough arguments\n", c
->name
);
812 && tok_c
- 1 > c
->args_max
) {
813 fprintf(stderr
, "%s: too many arguments\n", c
->name
);
818 r
= c
->func(vm
, tok_c
, tok_v
);
826 fprintf(stderr
, "didn't recognize '%s'\n", tok_v
[0]);
829 printf("\nfinished\n");