14 #include <readline/readline.h>
15 #ifdef HAVE_LIBVNCSERVER
17 #endif /* HAVE_LIBVNCSERVER */
22 #include "hw_lem1802.h"
23 #include "hw_keyboard.h"
26 * shell-like driver for dcpu16 core
27 * provides a basic interface to control a single emulation instance
29 * Justin Wind <justin.wind@gmail.com>
30 * 2012 04 10 - implementation started
31 * 2012 04 12 - cleanup, better shell loop
32 * 2012 05 12 - support v1.7 style devices
35 * handle quotes in shell command parsing
36 * use readline/history.h, since we're using readline anyhow
37 * ncurses windowing or something, for future display capabilities
40 static const char * const src_id_
= "$Id$";
42 /* global invocation options */
49 /* global run state, first sigint caught will drop out of run loop and back into shell */
50 static volatile unsigned int running_
= 0;
52 void sigint_handler_(int sig
) {
57 #define VERBOSE_PRINTF(...) do { if (opt_.verbose) printf(__VA_ARGS__); } while (0)
59 #define DEBUG_PRINTF(...) do { if (opt_.verbose > 4) fprintf(stderr, __VA_ARGS__); } while (0)
61 #define DEBUG_PRINTF(...) do { } while (0)
65 void usage_(char *prog
, unsigned int full
) {
66 FILE *f
= full
? stdout
: stderr
;
67 char *x
= strrchr(prog
, '/');
73 fprintf(f
, "%s -- dcpu16 emulator core shell\n\n",
76 fprintf(f
, "Usage: %s [-v] [file]\n",
80 fprintf(f
, "\nOptions:\n"
81 "\t [file] -- ram image to load initially\n"
82 "\t -v -- prints slightly more information while operating\n"
83 "\t -h -- this screen\n");
85 fprintf(f
, "\n%78s\n", src_id_
);
90 /* flense a buffer into a newly-allocated argument list */
92 int buf_tok_vect_(char ***v
, int *c
, char *buf
) {
93 const char *sep
= " \t";
94 const char *quot
= "\"'`";
95 const size_t v_grow
= 32;
100 *v
= malloc(v_sz
* sizeof **v
);
102 fprintf(stderr
, "%s():%s\n", "malloc", strerror(errno
));
106 for ( (*v
)[*c
] = strqtok_r(buf
, sep
, '\\', quot
, &qt
, &st
);
108 (*v
)[*c
] = strqtok_r(NULL
, sep
, '\\', quot
, &qt
, &st
)
112 if ((size_t)(*c
) == v_sz
) {
113 void *tmp_ptr
= realloc(*v
, (v_sz
+ v_grow
) * sizeof **v
);
114 if (tmp_ptr
== NULL
) {
115 fprintf(stderr
, "%s():%s\n", "realloc", strerror(errno
));
128 resets the vm if addr is zero then
129 loads an image from filename into ram starting at addr
132 int file_load_(struct dcpu16
*vm
, char *filename
, DCPU16_WORD addr
) {
139 f
= fopen(filename
, "rb");
141 fprintf(stderr
, "%s('%s'):%s\n", "fopen", filename
, strerror(errno
));
145 r
= fread(vm
->ram
+ addr
, sizeof(DCPU16_WORD
), DCPU16_RAM
- addr
, f
);
146 VERBOSE_PRINTF("read %zu words", r
);
147 if (addr
) VERBOSE_PRINTF(" starting at 0x%04x", addr
);
148 VERBOSE_PRINTF("\n");
151 fprintf(stderr
, "%s('%s'):%s\n", "fread", filename
, strerror(errno
));
158 * print raw ram contents from start to stop
161 void dump_ram_(struct dcpu16
*vm
, DCPU16_WORD start
, DCPU16_WORD end
) {
163 const unsigned int n
= 8; /* words per line */
167 for (i
= start
, j
= 0; i
<= end
; i
++, j
++) {
169 printf("0x%04x:\t", i
);
170 printf(" %04x%s", vm
->ram
[i
], (j
% n
) == (n
- 1) ? "\n" : "");
172 if ((j
% n
) != (n
- 1))
178 print the current state of the machine
179 shows current cycle count, registers, and next instruction
182 void state_print_(struct dcpu16
*vm
) {
188 for (i
= 0; i
< 8; i
++)
189 printf(" %s:0x%04x", dcpu16_reg_names
[i
], vm
->reg
[i
]);
192 printf("(0x%08llx) %2s:0x%04x %2s:0x%04x %2s:0x%04x %2s:0x%04x [%2s]:",
194 dcpu16_reg_names
[DCPU16_REG_EX
], vm
->reg
[DCPU16_REG_EX
],
195 dcpu16_reg_names
[DCPU16_REG_SP
], vm
->reg
[DCPU16_REG_SP
],
196 dcpu16_reg_names
[DCPU16_REG_PC
], vm
->reg
[DCPU16_REG_PC
],
197 dcpu16_reg_names
[DCPU16_REG_IA
], vm
->reg
[DCPU16_REG_IA
],
200 dcpu16_disassemble_print(vm
, vm
->reg
[DCPU16_REG_PC
]);
205 #ifdef HAVE_LIBVNCSERVER
206 static struct dynamic_array rfbScreens_
;
207 /* wups, kbdAddEvent isn't null by default, so I guess track associations externally */
208 struct rfb_instance_
{
209 rfbScreenInfoPtr screen
;
210 struct dcpu16_hw
*attached_display
;
211 struct dcpu16_hw
*attached_keyboard
;
214 enum rfbscreen_next_what_
{
218 /* locate next rfb not paired to requested type */
220 struct rfb_instance_
*rfbScreen_next_available_(enum rfbscreen_next_what_ what
, struct dynamic_array
*rfbScreens
, int argc
, char *argv
[]) {
222 struct rfb_instance_ new_instance
, *s
;
223 struct packed_args_
{
226 } parg
= { argc
, argv
};
228 for (i
= 0; i
< rfbScreens
->entries
; i
++) {
229 struct dcpu16_hw
*attached
;
231 s
= (struct rfb_instance_
*)DYNARRAY_ITEM(*rfbScreens
, i
);
233 case NEXT_DISPLAY
: attached
= s
->attached_display
; break;
234 case NEXT_KEYBOARD
: attached
= s
->attached_keyboard
; break;
236 if (attached
== NULL
)
240 /* no available rfb, create new */
241 if (dcpu16_hw_module_lem1802
.ctl(NULL
, "new_rfbScreen", &parg
, &new_instance
.screen
)) {
242 fprintf(stderr
, "failed to allocate new rfbScreen\n");
246 new_instance
.attached_display
= NULL
;
247 new_instance
.attached_keyboard
= NULL
;
249 new_instance
.screen
->port
+= rfbScreens
->entries
;
250 new_instance
.screen
->ipv6port
+= rfbScreens
->entries
;
252 DEBUG_PRINTF("%s>> port:%u\n", __func__
, new_instance
.screen
->port
);
254 s
= dynarray_add(rfbScreens
, &new_instance
);
260 /* begin serving a screen */
261 void rfbScreen_start(rfbScreenInfoPtr s
) {
263 rfbRunEventLoop(s
, -1, TRUE
);
265 #endif /* HAVE_LIBVNCSERVER */
268 Here follows the various commands the shell can execute.
270 At invocation, a command function will have already had its
271 number of arguments vetted, but will need command-specific
272 argument verifications done.
274 The arg_vector contains the command as the first entry, and
275 as such, arg_count will always be at least 1.
276 However, the args_min and args_max entries in struct command_
277 only refer to the counts of arguments, not the entries in the
285 int (*func
)(struct dcpu16
*, int c
, char **v
);
286 void (*help
)(FILE *f
, unsigned int);
289 #define COMMAND_IMPL(x) static int command_##x##_(struct dcpu16 *vm, int arg_count, char **arg_vector)
290 #define COMMAND_HELP(x) static void command_##x##_help_(FILE *f, unsigned int summary)
291 #define COMMAND_ENTRY(x, y, z) { #x, y, z, command_##x##_, command_##x##_help_ }
295 (void)vm
, (void)arg_count
, (void)arg_vector
;
300 fprintf(f
, "\tquit\n");
303 fprintf(f
, "Exits the emulator.\n");
307 COMMAND_IMPL(reset
) {
308 (void)arg_count
, (void)arg_vector
;
311 printf("initialized\n");
314 COMMAND_HELP(reset
) {
315 fprintf(f
, "\treset\n");
318 fprintf(f
, "Clears and reinitializes emulator.\n");
326 addr
= str_to_word(arg_vector
[2]);
328 fprintf(stderr
, "address '%s' is not a valid word: %s\n", arg_vector
[2], strerror(errno
));
333 if (file_load_(vm
, arg_vector
[1], addr
)) {
334 fprintf(stderr
, "failed to load '%s'\n", arg_vector
[1]);
337 printf("loaded '%s'", arg_vector
[1]);
338 if (addr
) printf(" starting at 0x%04x", addr
);
344 fprintf(f
, "\tload file [addr]\n");
347 fprintf(f
, "Load binary image from 'file' into ram.\n");
355 for (i
= 1; i
< arg_count
; i
++) {
356 addr
[i
-1] = str_to_word(arg_vector
[i
]);
358 fprintf(stderr
, "address '%s' is not a valid word: %s\n", arg_vector
[i
], strerror(errno
));
362 if (arg_count
< 2) addr
[0] = vm
->reg
[DCPU16_REG_PC
];
363 if (arg_count
< 3) addr
[1] = addr
[0];
365 if (addr
[1] < addr
[0]) {
366 fprintf(stderr
, "\t'addr_start' must be before addr_end\n");
370 dump_ram_(vm
, addr
[0], addr
[1]);
375 fprintf(f
, "\tdump [addr_start [addr_end]]\n");
378 fprintf(f
, "Displays contents of ram from addr_start to addr_end.\n");
382 COMMAND_IMPL(disassemble
) {
386 for (i
= 1; i
< arg_count
; i
++) {
387 addr
[i
-1] = str_to_word(arg_vector
[i
]);
389 fprintf(stderr
, "address '%s' is not a valid word: %s\n", arg_vector
[i
], strerror(errno
));
393 if (arg_count
< 2) addr
[0] = vm
->reg
[DCPU16_REG_PC
];
394 if (arg_count
< 3) addr
[1] = addr
[0];
396 if (addr
[1] < addr
[0]) {
397 fprintf(stderr
, "\t'addr_start' must be before addr_end\n");
401 for (i
= addr
[0]; i
<= addr
[1]; /* */ ) {
402 printf("0x%04x: ", i
);
403 i
+= dcpu16_disassemble_print(vm
, i
);
409 COMMAND_HELP(disassemble
) {
410 fprintf(f
, "\tdisassemble [addr_start [addr_end]]\n");
413 fprintf(f
, "Displays contents of ram parsed into instructions.\n");
418 unsigned long count
= 1;
421 if (arg_count
== 2) {
423 count
= strtoul(arg_vector
[1], &ep
, 0);
425 || !(*arg_vector
[1] && *ep
== '\0') ) {
426 fprintf(stderr
, "count '%s' is not a valid number: %s\n", arg_vector
[1], strerror(errno
));
431 fprintf(stderr
, "count must be positive\n");
437 dcpu16_disassemble_print(vm
, vm
->reg
[DCPU16_REG_PC
]);
441 if (count
> 1 && opt_
.verbose
)
448 fprintf(f
, "\tstep [count]\n");
451 fprintf(f
, "Executes the next instruction, or the next count instructions.\n");
461 /* check if addr is a register */
462 for (addr
= 0; dcpu16_reg_names
[addr
]; addr
++) {
463 if (strcasecmp(arg_vector
[1], dcpu16_reg_names
[addr
]) == 0)
466 if (addr
< DCPU16_REG__NUM
) {
469 addr
= str_to_word(arg_vector
[1]);
471 fprintf(stderr
, "address '%s' is not a valid word: %s\n", arg_vector
[1], strerror(errno
));
477 value
= str_to_word(arg_vector
[2]);
479 fprintf(stderr
, "address '%s' is not a valid word: %s\n", arg_vector
[2], strerror(errno
));
489 fprintf(f
, "\tset addr value\n");
492 fprintf(f
, "Sets addr to value.");
495 #define NANOSECONDS_PER_CYCLE 10000
496 #define MIN_NANOSLEEP 31000
498 struct sigaction act
;
499 long long run_cycle_start
, run_cycle_end
;
500 long long cycle_start
, cycles_to_wait
;
502 struct timespec ts_run_start
, ts_run_end
, ts_run_diff
;
503 struct timespec ts_cycle_start
, ts_cycle_end_target
, ts_cycle_end
, ts_cycle_waste
, ts_cycle_rem
;
504 const struct timespec ts_cycle_time
= { .tv_sec
= 0, .tv_nsec
= NANOSECONDS_PER_CYCLE
};
506 (void)arg_count
, (void)arg_vector
;
509 gettimespecofday(&ts_run_start
);
510 run_cycle_start
= vm
->cycle_
;
512 memset(&act
, 0, sizeof act
);
513 act
.sa_handler
= sigint_handler_
;
514 act
.sa_flags
= SA_RESETHAND
;
516 if (sigaction(SIGINT
, &act
, NULL
)) {
517 fprintf(stderr
, "%s():%s\n", "sigaction", strerror(errno
));
522 gettimespecofday(&ts_cycle_start
);
523 ts_cycle_end_target
= ts_cycle_start
;
525 cycle_start
= vm
->cycle_
;
528 if (opt_
.verbose
> 1)
530 else if (opt_
.verbose
) {
531 dcpu16_disassemble_print(vm
, vm
->reg
[DCPU16_REG_PC
]);
535 /* how many cycles did this instr use? */
536 cycles_to_wait
= vm
->cycle_
- cycle_start
;
538 /* each cycle wants to take 10 microseconds */
539 while (cycles_to_wait
--)
540 timespec_add(&ts_cycle_end_target
, &ts_cycle_time
);
542 /* how much of that did we spend already */
543 gettimespecofday(&ts_cycle_end
);
545 /* do we have time to kill? */
546 if (timespec_subtract(&ts_cycle_waste
, &ts_cycle_end_target
, &ts_cycle_end
) == 0) {
547 /* nanosleep doesn't interfere with libvncserver, unlike usleep */
548 if (ts_cycle_waste
.tv_sec
== 0 && ts_cycle_waste
.tv_nsec
>= MIN_NANOSLEEP
)
549 while ( nanosleep(&ts_cycle_waste
, &ts_cycle_rem
) )
550 ts_cycle_waste
= ts_cycle_rem
;
552 /* negative, we've already blown our time */
553 DEBUG_PRINTF("cycle time overrun %ld.%09lds\n", ts_cycle_waste
.tv_sec
, ts_cycle_waste
.tv_nsec
);
558 gettimespecofday(&ts_cycle_end
);
559 timespec_subtract(&ts_cycle_rem
, &ts_cycle_end_target
, &ts_cycle_end
);
560 DEBUG_PRINTF("projected end: %ld.%09ld actual end: %ld.%09ld diff: %ld.%09ld\n",
561 ts_cycle_end_target
.tv_sec
, ts_cycle_end_target
.tv_nsec
,
562 ts_cycle_end
.tv_sec
, ts_cycle_end
.tv_nsec
,
563 ts_cycle_rem
.tv_sec
, ts_cycle_rem
.tv_nsec
);
568 run_cycle_end
= vm
->cycle_
;
569 gettimespecofday(&ts_run_end
);
570 timespec_subtract(&ts_run_diff
, &ts_run_end
, &ts_run_start
);
571 VERBOSE_PRINTF("ran %lld cycles in %ld.%09lds\n",
572 run_cycle_end
- run_cycle_start
,
573 ts_run_diff
.tv_sec
, ts_run_diff
.tv_nsec
);
575 printf("interrupted...\n");
580 fprintf(f
, "\trun\n");
583 fprintf(f
, "Begins executing continuously.\n"
584 "May be interrupted with SIGINT.\n");
587 static const char * const display_filename_default_
=
590 #else /* HAVE_LIBPNG */
592 #endif /* HAVE_LIBPNG */
594 COMMAND_IMPL(display
) {
595 struct dcpu16_hw
*hw
;
596 const char *renderer
= arg_vector
[1];
597 const char *renderer_arg
= NULL
;
601 renderer_arg
= arg_vector
[2];
603 hw
= dcpu16_hw_new(vm
, &dcpu16_hw_module_lem1802
, NULL
);
605 fprintf(stderr
, "failed to initialize new display\n");
609 /* handle per-renderer setup of data.. */
610 /* FIXME: these are awkward */
611 if (strcmp(renderer
, "pnm") == 0) {
612 renderer_data
= (void *)(renderer_arg
? renderer_arg
: display_filename_default_
);
616 if (strcmp(renderer
, "png") == 0) {
617 renderer_data
= (void *)(renderer_arg
? renderer_arg
: display_filename_default_
);
619 #endif /* HAVE_LIBPNG */
621 #ifdef HAVE_LIBVNCSERVER
622 if (strcmp(renderer
, "vnc") == 0) {
624 char *argv
[] = { "vm-dcpu16", NULL
};
625 struct rfb_instance_
*s
;
627 s
= rfbScreen_next_available_(NEXT_DISPLAY
, &rfbScreens_
, argc
, argv
);
629 fprintf(stderr
, "failed to initialize vnc\n");
634 if (dcpu16_hw_ctl(hw
, "associate_rfbScreen", s
->screen
, NULL
)) {
635 fprintf(stderr
, "failed to configure display/vnc");
639 s
->attached_display
= hw
;
640 rfbScreen_start(s
->screen
);
641 renderer_data
= s
->screen
;
643 DEBUG_PRINTF("attached display to rfb (frameBuffer:%p)\n", s
->screen
->frameBuffer
);
645 #endif /* HAVE_LIBVNCSERVER */
647 dcpu16_hw_ctl(hw
, "renderer", (char *)renderer
, NULL
);
648 dcpu16_hw_ctl(hw
, "renderer_data", renderer_data
, NULL
);
650 if (dcpu16_hw_attach(vm
, hw
)) {
651 fprintf(stderr
, "failed to attach new display\n");
658 COMMAND_HELP(display
) {
665 fprintf(f
, "\tdisplay renderer [renderer data]\n");
668 fprintf(f
, "Attaches new display unit, using 'renderer' as back-end output.\n"
671 fprintf(f
, "Supported renderers:\n");
675 if (dcpu16_hw_module_lem1802
.ctl(NULL
, "renderers_iter", &iter
, &renderer
)) {
676 fprintf(stderr
, "error fetching next renderer\n");
679 if (iter
== NULL
|| renderer
.name
== NULL
)
682 fprintf(f
, "\t%s %s\n", renderer
.name
, renderer
.args
);
686 COMMAND_IMPL(keyboard
) {
687 struct dcpu16_hw
*hw
;
689 (void)arg_count
, (void)arg_vector
;
691 hw
= dcpu16_hw_new(vm
, &dcpu16_hw_module_keyboard
, NULL
);
693 fprintf(stderr
, "failed to initialize new keyboard\n");
697 #ifdef HAVE_LIBVNCSERVER
698 struct rfb_instance_
*s
;
700 char *argv
[] = { "vm-dcpu16", NULL
};
702 s
= rfbScreen_next_available_(NEXT_KEYBOARD
, &rfbScreens_
, argc
, argv
);
704 fprintf(stderr
, "failed to initialize vnc\n");
708 if (dcpu16_hw_ctl(hw
, "associate_rfbScreen", s
->screen
, NULL
)) {
709 fprintf(stderr
, "failed to configure keyboard/vnc\n");
713 s
->attached_keyboard
= hw
;
715 if (dcpu16_hw_attach(vm
, hw
)) {
716 fprintf(stderr
, "failed to attach new keyboard\n");
720 #endif /* HAVE_LIBVNCSERVER */
724 COMMAND_HELP(keyboard
) {
725 fprintf(f
, "\tkeyboard\n");
728 fprintf(f
, "Attaches new keyboard unit.\n");
731 /* gather all these together into a searchable table */
733 /* help command gets some assistance in declarations */
737 static struct command_ command_table_
[] = {
738 COMMAND_ENTRY(help
, 0, -1),
739 COMMAND_ENTRY(quit
, 0, -1),
740 COMMAND_ENTRY(load
, 1, 2),
741 COMMAND_ENTRY(dump
, 0, 2),
742 COMMAND_ENTRY(disassemble
, 0, 2),
743 COMMAND_ENTRY(step
, 0, 1),
744 COMMAND_ENTRY(run
, 0, 0),
745 COMMAND_ENTRY(set
, 2, 2),
746 COMMAND_ENTRY(reset
, 0, 0),
747 COMMAND_ENTRY(display
, 1, 2),
748 COMMAND_ENTRY(keyboard
, 0, 0),
749 { NULL
, 0, 0, NULL
, NULL
}
756 if (arg_count
== 2) {
757 for (c
= command_table_
; c
->func
; c
++) {
758 if (strcasecmp(arg_vector
[1], c
->name
) == 0) {
767 for (c
= command_table_
; c
->func
; c
++) {
774 fprintf(f
, "\thelp [command]\n");
777 fprintf(f
, "Displays a list of available commands, or detailed help on a specific command.\n");
781 void msg_verbose_filter_(unsigned int level
, char *fmt
, ...) {
782 static const char * const levels
[] = { "error", "info", "debug" };
783 FILE *f
= (level
<= MSG_ERROR
) ? stderr
: stdout
;
786 if (level
+ 2 > opt_
.verbose
)
789 if (level
< sizeof levels
/ sizeof *levels
)
790 fprintf(f
, "[%s %s] ", "dcpu16", levels
[level
]);
792 fprintf(f
, "[%s %u] ", "dcpu16", level
);
795 vfprintf(f
, fmt
, ap
);
803 int main(int argc
, char **argv
) {
804 const char prompt_fmt
[] = "PC:%04x> ";
807 char *line
, *line_prev
;
808 char **tok_v
, **tok_v_prev
;
809 int tok_c
, tok_c_prev
;
812 while ( (c
= getopt(argc
, argv
, "hv")) != EOF
) {
830 dcpu16_msg_set_default(msg_verbose_filter_
);
832 if ((vm
= dcpu16_new()) == NULL
) {
833 fprintf(stderr
, "could not allocate new dcpu16 instance\n");
834 exit(EX_UNAVAILABLE
);
837 #ifdef HAVE_LIBVNCSERVER
838 if (dynarray_init(&rfbScreens_
, sizeof(struct rfb_instance_
), 4)) {
839 fprintf(stderr
, "could not allocate rfb container\n");
840 exit(EX_UNAVAILABLE
);
842 #endif /* HAVE_LIBVNCSERVER */
845 if (file_load_(vm
, *argv
, 0)) {
846 fprintf(stderr
, "couldn't load '%s'\n", *argv
);
851 /* show state, read commands */
852 for (line
= line_prev
= NULL
,
853 tok_v
= tok_v_prev
= NULL
,
854 tok_c
= tok_c_prev
= 0,
855 snprintf(prompt
, sizeof prompt
, prompt_fmt
, vm
->reg
[DCPU16_REG_PC
]),
858 (line
= readline(prompt
));
861 snprintf(prompt
, sizeof prompt
, prompt_fmt
, vm
->reg
[DCPU16_REG_PC
]),
863 const char whitespace
[] = " \t";
868 /* skip whitespaces */
869 line_start
= line
+ strspn(line
, whitespace
);
872 /* a new command, remember previous for possible repetition */
874 /* turn new line into new arg array */
875 if (buf_tok_vect_(&tok_v
, &tok_c
, line_start
)) {
876 fprintf(stderr
, "failed to process command\n");
880 /* and keep track if it all for the next time around */
881 if (line_prev
) free(line_prev
);
884 if (tok_v_prev
) free(tok_v_prev
);
888 /* blank new command, but no prior command to repeat? ask again */
889 if (tok_v_prev
== NULL
|| tok_v_prev
[0] == NULL
|| *(tok_v_prev
[0]) == '\0') {
894 /* otherwise discard new line and promote prior */
901 /* look up command */
902 for (c
= command_table_
; c
->name
; c
++) {
903 if (strcasecmp(tok_v
[0], c
->name
) == 0) {
904 if (c
->args_min
> tok_c
- 1) {
905 fprintf(stderr
, "%s: not enough arguments\n", c
->name
);
911 && tok_c
- 1 > c
->args_max
) {
912 fprintf(stderr
, "%s: too many arguments\n", c
->name
);
917 r
= c
->func(vm
, tok_c
, tok_v
);
925 fprintf(stderr
, "didn't recognize '%s'\n", tok_v
[0]);
928 printf("\nfinished\n");