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
.screen
->port
+= rfbScreens
->entries
;
232 new_instance
.screen
->ipv6port
+= rfbScreens
->entries
;
234 new_instance
.attached_display
= NULL
;
235 new_instance
.attached_keyboard
= NULL
;
236 s
= dynarray_add(rfbScreens
, &new_instance
);
240 /* locate or allocate the next display with an un-occupied keyboard */
242 struct rfb_instance_
*rfbScreen_next_available_keyboard_(struct dynamic_array
*rfbScreens
, int argc
, char *argv
[]) {
244 struct rfb_instance_ new_instance
, *s
;
245 struct packed_args_
{
248 } parg
= { argc
, argv
};
250 for (i
= 0; i
< rfbScreens
->entries
; i
++) {
251 s
= (struct rfb_instance_
*)DYNARRAY_ITEM(*rfbScreens
, i
);
252 if (s
->attached_keyboard
== NULL
)
256 if (dcpu16_hw_module_lem1802
.ctl(NULL
, "new_rfbScreen", &parg
, &new_instance
.screen
)) {
257 fprintf(stderr
, "failed to allocate new rfbScreen");
261 new_instance
.attached_display
= NULL
;
262 new_instance
.attached_keyboard
= NULL
;
263 s
= dynarray_add(rfbScreens
, &new_instance
);
267 /* begin serving a screen */
268 void rfbScreen_start(rfbScreenInfoPtr s
) {
270 rfbRunEventLoop(s
, -1, TRUE
);
272 #endif /* HAVE_LIBVNCSERVER */
275 Here follows the various commands the shell can execute.
277 At invocation, a command function will have already had its
278 number of arguments vetted, but will need command-specific
279 argument verifications done.
281 The arg_vector contains the command as the first entry, and
282 as such, arg_count will always be at least 1.
283 However, the args_min and args_max entries in struct command_
284 only refer to the counts of arguments, not the entries in the
292 int (*func
)(struct dcpu16
*, int c
, char **v
);
293 void (*help
)(FILE *f
, unsigned int);
296 #define COMMAND_IMPL(x) static int command_##x##_(struct dcpu16 *vm, int arg_count, char **arg_vector)
297 #define COMMAND_HELP(x) static void command_##x##_help_(FILE *f, unsigned int summary)
298 #define COMMAND_ENTRY(x, y, z) { #x, y, z, command_##x##_, command_##x##_help_ }
302 (void)vm
, (void)arg_count
, (void)arg_vector
;
307 fprintf(f
, "\tquit\n");
310 fprintf(f
, "Exits the emulator.\n");
314 COMMAND_IMPL(reset
) {
315 (void)arg_count
, (void)arg_vector
;
318 printf("initialized\n");
321 COMMAND_HELP(reset
) {
322 fprintf(f
, "\treset\n");
325 fprintf(f
, "Clears and reinitializes emulator.\n");
333 addr
= str_to_word(arg_vector
[2]);
335 fprintf(stderr
, "address '%s' is not a valid word: %s\n", arg_vector
[2], strerror(errno
));
340 if (file_load_(vm
, arg_vector
[1], addr
)) {
341 fprintf(stderr
, "failed to load '%s'\n", arg_vector
[1]);
344 printf("loaded '%s'", arg_vector
[1]);
345 if (addr
) printf(" starting at 0x%04x", addr
);
351 fprintf(f
, "\tload file [addr]\n");
354 fprintf(f
, "Load binary image from 'file' into ram.\n");
362 for (i
= 1; i
< arg_count
; i
++) {
363 addr
[i
-1] = str_to_word(arg_vector
[i
]);
365 fprintf(stderr
, "address '%s' is not a valid word: %s\n", arg_vector
[i
], strerror(errno
));
369 if (arg_count
< 2) addr
[0] = vm
->reg
[DCPU16_REG_PC
];
370 if (arg_count
< 3) addr
[1] = addr
[0];
372 if (addr
[1] < addr
[0]) {
373 fprintf(stderr
, "\t'addr_start' must be before addr_end\n");
377 dump_ram_(vm
, addr
[0], addr
[1]);
382 fprintf(f
, "\tdump [addr_start [addr_end]]\n");
385 fprintf(f
, "Displays contents of ram from addr_start to addr_end.\n");
389 COMMAND_IMPL(disassemble
) {
393 for (i
= 1; i
< arg_count
; i
++) {
394 addr
[i
-1] = str_to_word(arg_vector
[i
]);
396 fprintf(stderr
, "address '%s' is not a valid word: %s\n", arg_vector
[i
], strerror(errno
));
400 if (arg_count
< 2) addr
[0] = vm
->reg
[DCPU16_REG_PC
];
401 if (arg_count
< 3) addr
[1] = addr
[0];
403 if (addr
[1] < addr
[0]) {
404 fprintf(stderr
, "\t'addr_start' must be before addr_end\n");
408 for (i
= addr
[0]; i
<= addr
[1]; /* */ ) {
409 printf("0x%04x: ", i
);
410 i
+= dcpu16_disassemble_print(vm
, i
);
416 COMMAND_HELP(disassemble
) {
417 fprintf(f
, "\tdisassemble [addr_start [addr_end]]\n");
420 fprintf(f
, "Displays contents of ram parsed into instructions.\n");
425 unsigned long count
= 1;
428 if (arg_count
== 2) {
430 count
= strtoul(arg_vector
[1], &ep
, 0);
432 || !(*arg_vector
[1] && *ep
== '\0') ) {
433 fprintf(stderr
, "count '%s' is not a valid number: %s\n", arg_vector
[1], strerror(errno
));
438 fprintf(stderr
, "count must be positive\n");
444 dcpu16_disassemble_print(vm
, vm
->reg
[DCPU16_REG_PC
]);
448 if (count
> 1 && opt_
.verbose
)
455 fprintf(f
, "\tstep [count]\n");
458 fprintf(f
, "Executes the next instruction, or the next count instructions.\n");
468 /* check if addr is a register */
469 for (addr
= 0; dcpu16_reg_names
[addr
]; addr
++) {
470 if (strcasecmp(arg_vector
[1], dcpu16_reg_names
[addr
]) == 0)
473 if (addr
< DCPU16_REG__NUM
) {
476 addr
= str_to_word(arg_vector
[1]);
478 fprintf(stderr
, "address '%s' is not a valid word: %s\n", arg_vector
[1], strerror(errno
));
484 value
= str_to_word(arg_vector
[2]);
486 fprintf(stderr
, "address '%s' is not a valid word: %s\n", arg_vector
[2], strerror(errno
));
496 fprintf(f
, "\tset addr value\n");
499 fprintf(f
, "Sets addr to value.");
502 #define NANOSECONDS_PER_CYCLE 10000
503 #define MIN_NANOSLEEP 31000
505 struct sigaction act
;
506 long long run_cycle_start
, run_cycle_end
;
507 long long cycle_start
, cycles_to_wait
;
509 struct timespec ts_run_start
, ts_run_end
, ts_run_diff
;
510 struct timespec ts_cycle_start
, ts_cycle_end_target
, ts_cycle_end
, ts_cycle_waste
, ts_cycle_rem
;
511 const struct timespec ts_cycle_time
= { .tv_sec
= 0, .tv_nsec
= NANOSECONDS_PER_CYCLE
};
513 (void)arg_count
, (void)arg_vector
;
516 gettimespecofday(&ts_run_start
);
517 run_cycle_start
= vm
->cycle_
;
519 memset(&act
, 0, sizeof act
);
520 act
.sa_handler
= sigint_handler_
;
521 act
.sa_flags
= SA_RESETHAND
;
523 if (sigaction(SIGINT
, &act
, NULL
)) {
524 fprintf(stderr
, "%s():%s\n", "sigaction", strerror(errno
));
529 gettimespecofday(&ts_cycle_start
);
530 ts_cycle_end_target
= ts_cycle_start
;
532 cycle_start
= vm
->cycle_
;
535 if (opt_
.verbose
> 1)
537 else if (opt_
.verbose
) {
538 dcpu16_disassemble_print(vm
, vm
->reg
[DCPU16_REG_PC
]);
542 /* how many cycles did this instr use? */
543 cycles_to_wait
= vm
->cycle_
- cycle_start
;
545 /* each cycle wants to take 10 microseconds */
546 while (cycles_to_wait
--)
547 timespec_add(&ts_cycle_end_target
, &ts_cycle_time
);
549 /* how much of that did we spend already */
550 gettimespecofday(&ts_cycle_end
);
552 /* do we have time to kill? */
553 if (timespec_subtract(&ts_cycle_waste
, &ts_cycle_end_target
, &ts_cycle_end
) == 0) {
554 /* nanosleep doesn't interfere with libvncserver, unlike usleep */
555 if (ts_cycle_waste
.tv_sec
== 0 && ts_cycle_waste
.tv_nsec
>= MIN_NANOSLEEP
)
556 while ( nanosleep(&ts_cycle_waste
, &ts_cycle_rem
) )
557 ts_cycle_waste
= ts_cycle_rem
;
559 /* negative, we've already blown our time */
561 fprintf(stderr
, "cycle time overrun %ld.%09lds\n", ts_cycle_waste
.tv_sec
, ts_cycle_waste
.tv_nsec
);
567 gettimespecofday(&ts_cycle_end
);
568 timespec_subtract(&ts_cycle_rem
, &ts_cycle_end_target
, &ts_cycle_end
);
569 fprintf(stderr
, "projected end: %ld.%09ld actual end: %ld.%09ld diff: %ld.%09ld\n",
570 ts_cycle_end_target
.tv_sec
, ts_cycle_end_target
.tv_nsec
,
571 ts_cycle_end
.tv_sec
, ts_cycle_end
.tv_nsec
,
572 ts_cycle_rem
.tv_sec
, ts_cycle_rem
.tv_nsec
);
577 run_cycle_end
= vm
->cycle_
;
578 gettimespecofday(&ts_run_end
);
579 timespec_subtract(&ts_run_diff
, &ts_run_end
, &ts_run_start
);
580 fprintf(stderr
, "ran %lld cycles in %ld.%09lds\n",
581 run_cycle_end
- run_cycle_start
,
582 ts_run_diff
.tv_sec
, ts_run_diff
.tv_nsec
);
584 printf("interrupted...\n");
589 fprintf(f
, "\trun\n");
592 fprintf(f
, "Begins executing continuously.\n"
593 "May be interrupted with SIGINT.\n");
596 static const char * const display_filename_default_
=
599 #else /* HAVE_LIBPNG */
601 #endif /* HAVE_LIBPNG */
603 COMMAND_IMPL(display
) {
604 struct dcpu16_hw
*hw
;
605 const char *renderer
= arg_vector
[1];
606 const char *renderer_arg
= NULL
;
610 renderer_arg
= arg_vector
[2];
612 hw
= dcpu16_hw_new(vm
, &dcpu16_hw_module_lem1802
, NULL
);
614 fprintf(stderr
, "failed to initialize new display\n");
618 /* handle per-renderer setup of data.. */
619 /* FIXME: these are awkward */
620 if (strcmp(renderer
, "pnm") == 0) {
621 renderer_data
= (void *)(renderer_arg
? renderer_arg
: display_filename_default_
);
625 if (strcmp(renderer
, "png") == 0) {
626 renderer_data
= (void *)(renderer_arg
? renderer_arg
: display_filename_default_
);
628 #endif /* HAVE_LIBPNG */
630 #ifdef HAVE_LIBVNCSERVER
631 if (strcmp(renderer
, "vnc") == 0) {
633 char *argv
[] = { "vm-dcpu16", NULL
};
634 struct rfb_instance_
*s
;
636 s
= rfbScreen_next_available_display_(&rfbScreens_
, argc
, argv
);
638 fprintf(stderr
, "failed to initialize vnc\n");
643 if (dcpu16_hw_ctl(hw
, "associate_rfbScreen", s
->screen
, NULL
)) {
644 fprintf(stderr
, "failed to configure display/vnc");
648 s
->attached_display
= hw
;
649 rfbScreen_start(s
->screen
);
650 renderer_data
= s
->screen
;
652 #endif /* HAVE_LIBVNCSERVER */
654 dcpu16_hw_ctl(hw
, "renderer", (char *)renderer
, NULL
);
655 dcpu16_hw_ctl(hw
, "renderer_data", renderer_data
, NULL
);
657 if (dcpu16_hw_attach(vm
, hw
)) {
658 fprintf(stderr
, "failed to attach new display\n");
665 COMMAND_HELP(display
) {
672 fprintf(f
, "\tdisplay renderer [renderer data]\n");
675 fprintf(f
, "Attaches new display unit, using 'renderer' as back-end output.\n"
678 fprintf(f
, "Supported renderers:\n");
682 if (dcpu16_hw_module_lem1802
.ctl(NULL
, "renderers_iter", &iter
, &renderer
)) {
683 fprintf(stderr
, "error fetching next renderer\n");
686 if (iter
== NULL
|| renderer
.name
== NULL
)
689 fprintf(f
, "\t%s %s\n", renderer
.name
, renderer
.args
);
693 COMMAND_IMPL(keyboard
) {
694 struct dcpu16_hw
*hw
;
696 (void)arg_count
, (void)arg_vector
;
698 hw
= dcpu16_hw_new(vm
, &dcpu16_hw_module_keyboard
, NULL
);
700 fprintf(stderr
, "failed to initialize new keyboard\n");
704 #ifdef HAVE_LIBVNCSERVER
705 struct rfb_instance_
*s
;
707 char *argv
[] = { "vm-dcpu16", NULL
};
709 s
= rfbScreen_next_available_keyboard_(&rfbScreens_
, argc
, argv
);
711 fprintf(stderr
, "failed to initialize vnc\n");
715 if (dcpu16_hw_ctl(hw
, "associate_rfbScreen", s
->screen
, NULL
)) {
716 fprintf(stderr
, "failed to configure keyboard/vnc\n");
720 s
->attached_keyboard
= hw
;
722 if (dcpu16_hw_attach(vm
, hw
)) {
723 fprintf(stderr
, "failed to attach new keyboard\n");
727 #endif /* HAVE_LIBVNCSERVER */
731 COMMAND_HELP(keyboard
) {
732 fprintf(f
, "\tkeyboard\n");
735 fprintf(f
, "Attaches new keyboard unit.\n");
738 /* gather all these together into a searchable table */
740 /* help command gets some assistance in declarations */
744 static struct command_ command_table_
[] = {
745 COMMAND_ENTRY(help
, 0, -1),
746 COMMAND_ENTRY(quit
, 0, -1),
747 COMMAND_ENTRY(load
, 1, 2),
748 COMMAND_ENTRY(dump
, 0, 2),
749 COMMAND_ENTRY(disassemble
, 0, 2),
750 COMMAND_ENTRY(step
, 0, 1),
751 COMMAND_ENTRY(run
, 0, 0),
752 COMMAND_ENTRY(set
, 2, 2),
753 COMMAND_ENTRY(reset
, 0, 0),
754 COMMAND_ENTRY(display
, 1, 2),
755 COMMAND_ENTRY(keyboard
, 0, 0),
756 { NULL
, 0, 0, NULL
, NULL
}
763 if (arg_count
== 2) {
764 for (c
= command_table_
; c
->func
; c
++) {
765 if (strcasecmp(arg_vector
[1], c
->name
) == 0) {
774 for (c
= command_table_
; c
->func
; c
++) {
781 fprintf(f
, "\thelp [command]\n");
784 fprintf(f
, "Displays a list of available commands, or detailed help on a specific command.\n");
788 int main(int argc
, char **argv
) {
789 const char prompt_fmt
[] = "PC:%04x> ";
792 char *line
, *line_prev
;
793 char **tok_v
, **tok_v_prev
;
794 int tok_c
, tok_c_prev
;
797 while ( (c
= getopt(argc
, argv
, "hv")) != EOF
) {
812 if (opt_
.verbose
< 1) {
813 dcpu16_warn_cb_set(NULL
);
814 dcpu16_trace_cb_set(NULL
);
815 } else if (opt_
.verbose
< 2) {
816 dcpu16_trace_cb_set(NULL
);
821 if ((vm
= dcpu16_new()) == NULL
) {
822 fprintf(stderr
, "could not allocate new dcpu16 instance\n");
823 exit(EX_UNAVAILABLE
);
826 #ifdef HAVE_LIBVNCSERVER
827 if (dynarray_init(&rfbScreens_
, sizeof(struct rfb_instance_
), 4)) {
828 fprintf(stderr
, "could not allocate rfb container\n");
829 exit(EX_UNAVAILABLE
);
831 #endif /* HAVE_LIBVNCSERVER */
834 if (file_load_(vm
, *argv
, 0)) {
835 fprintf(stderr
, "couldn't load '%s'\n", *argv
);
840 /* show state, read commands */
841 for (line
= line_prev
= NULL
,
842 tok_v
= tok_v_prev
= NULL
,
843 tok_c
= tok_c_prev
= 0,
844 snprintf(prompt
, sizeof prompt
, prompt_fmt
, vm
->reg
[DCPU16_REG_PC
]),
847 (line
= readline(prompt
));
850 snprintf(prompt
, sizeof prompt
, prompt_fmt
, vm
->reg
[DCPU16_REG_PC
]),
852 const char whitespace
[] = " \t";
857 /* skip whitespaces */
858 line_start
= line
+ strspn(line
, whitespace
);
861 /* a new command, remember previous for possible repetition */
863 /* turn new line into new arg array */
864 if (buf_tok_vect_(&tok_v
, &tok_c
, line_start
)) {
865 fprintf(stderr
, "failed to process command\n");
869 /* and keep track if it all for the next time around */
870 if (line_prev
) free(line_prev
);
873 if (tok_v_prev
) free(tok_v_prev
);
877 /* blank new command, but no prior command to repeat? ask again */
878 if (tok_v_prev
== NULL
|| tok_v_prev
[0] == NULL
|| *(tok_v_prev
[0]) == '\0') {
883 /* otherwise discard new line and promote prior */
890 /* look up command */
891 for (c
= command_table_
; c
->name
; c
++) {
892 if (strcasecmp(tok_v
[0], c
->name
) == 0) {
893 if (c
->args_min
> tok_c
- 1) {
894 fprintf(stderr
, "%s: not enough arguments\n", c
->name
);
900 && tok_c
- 1 > c
->args_max
) {
901 fprintf(stderr
, "%s: too many arguments\n", c
->name
);
906 r
= c
->func(vm
, tok_c
, tok_v
);
914 fprintf(stderr
, "didn't recognize '%s'\n", tok_v
[0]);
917 printf("\nfinished\n");