From 288cde96c2f17d4d788c2fc0b5e11fb00ad7321a Mon Sep 17 00:00:00 2001 From: Michel Pollet Date: Wed, 17 Jan 2018 10:18:42 +0000 Subject: [PATCH] shavr: More updates Reworked the command buffer and that sort of things. Will work toward passing a context all the way down, so hanlde possibly multiple instances of the core. Signed-off-by: Michel Pollet --- shavr/src/history.c | 78 +++++++++++++++++++++++++++++------------ shavr/src/history.h | 46 ++++++++---------------- shavr/src/history_avr.c | 23 ++++++------ shavr/src/shavr.c | 22 ++++++------ shavr/src/sim_prepare.c | 1 + 5 files changed, 94 insertions(+), 76 deletions(-) diff --git a/shavr/src/history.c b/shavr/src/history.c index 4b11fe5..92c5258 100644 --- a/shavr/src/history.c +++ b/shavr/src/history.c @@ -30,6 +30,25 @@ #include "history.h" +typedef struct line_t { + TAILQ_ENTRY(line_t) queue; + + int size, len, pos; + char * line; +} line_t, *line_p; + +typedef struct history_t { + int ttyin; + int ttyout; + void * private_context; + struct history_params_t *p; + + TAILQ_HEAD(cmd_queue_t,line_t) cmd; + line_p current; + void * state; + int temp; +} history_t, *history_p; + static struct termios orig_termios; /* TERMinal I/O Structure */ void fatal(char *message) { @@ -105,12 +124,8 @@ line_new( { line_p res = malloc(sizeof(line_t)); memset(res, 0, sizeof(*res)); - res->prev = h->tail; - if (!h->tail) - h->head = res; - else - h->tail->next = res; - h->tail = res; + + TAILQ_INSERT_TAIL(&h->cmd, res, queue); h->current = res; return res; } @@ -136,9 +151,9 @@ history_display( history_p h) { char s[16]; - int pl = strlen(h->prompt); + int pl = strlen(h->p->prompt); write(h->ttyout, "\033[0m\r", 5); - write(h->ttyout, h->prompt, pl); + write(h->ttyout, h->p->prompt, pl); if (h->current) { write(h->ttyout, h->current->line, h->current->len); pl += h->current->pos; @@ -228,24 +243,28 @@ line_key( } break; case 13: { // return!! if (h->current->len) { - if (!h->process_line || h->process_line(h, h->current)) { + if (!h->p->process_line || + h->p->process_line(h->private_context, + h->current->line)) { res++; - if (h->current != h->tail) + if (h->current != TAILQ_LAST(&h->cmd, cmd_queue_t)) line_dup(h, h->current); line_new(h); } } else write(1, "\012", 1); // next line } break; - case 16: { // control-P -- previous line - previous_line: - if (h->current->prev) - h->current = h->current->prev; + case 16: {// control-P -- previous line + previous_line: ; + line_p p = TAILQ_PREV(h->current, cmd_queue_t, queue); + if (p) + h->current = p; } break; case 14: { // control-N -- next line - next_line: - if (h->current->next) - h->current = h->current->next; + next_line: ; + line_p n = TAILQ_NEXT(h->current, queue); + if (n) + h->current = n; } break; case 27: { PT_YIELD(h->state); @@ -281,7 +300,7 @@ line_key( } break; } - if ((insert || delete) && h->current != h->tail) { + if ((insert || delete) && h->current != TAILQ_LAST(&h->cmd, cmd_queue_t)) { line_dup(h, h->current); } line_p l = h->current; @@ -321,13 +340,23 @@ line_key( return res; } - -void -history_init( - history_p h) +struct history_t * +history_new( + int ttyin, int ttyout, + struct history_params_t * params, + void * private_context) { - atexit(tty_atexit); + struct history_t * h = malloc(sizeof(*h)); + + memset(h, 0, sizeof(*h)); + + h->ttyin = ttyin; + h->ttyout = ttyout; + h->p = params; + h->private_context = private_context; + atexit(tty_atexit); + TAILQ_INIT(&h->cmd); /* store current tty settings in orig_termios */ if (tcgetattr(h->ttyout, &orig_termios) < 0) fatal("can't get tty settings"); @@ -336,7 +365,10 @@ history_init( int fl = fcntl(h->ttyin, F_GETFL); fcntl(0, F_SETFL, fl | O_ASYNC); + history_display(h); + + return h; } int diff --git a/shavr/src/history.h b/shavr/src/history.h index bcb211d..30158f3 100644 --- a/shavr/src/history.h +++ b/shavr/src/history.h @@ -22,43 +22,27 @@ #ifndef HISTORY_H_ #define HISTORY_H_ +#include -typedef struct line_t { - struct line_t * next; - struct line_t * prev; +struct history_t; - int size, len, pos; - char * line; -} line_t, *line_p; - - -typedef struct history_t { - int ttyin; - int ttyout; - - line_p head, tail; - line_p current; +typedef struct history_params_t { char prompt[32]; - void * state; - int temp; - - void *param; - int (*process_line)(struct history_t *h, line_p l); - - //struct history_cmd_list_t * cmd_list; -} history_t, *history_p; - - -void -history_init( - history_p h); - + int (*process_line)( + struct history_params_t *p, + const char *cmd_line); +} history_params_t; + +struct history_t * +history_new( + int ttyin, int ttyout, + struct history_params_t * params, + void * private_context); int history_idle( - history_p h); - + struct history_t * h); void history_display( - history_p h); + struct history_t * h); #endif /* HISTORY_H_ */ diff --git a/shavr/src/history_avr.c b/shavr/src/history_avr.c index b3aad91..3820583 100644 --- a/shavr/src/history_avr.c +++ b/shavr/src/history_avr.c @@ -33,14 +33,16 @@ int history_redisplay = 0; - +/* + * Called when the user type 'return' on the shell + */ static int _history_process_line( - struct history_t *h, - line_p l ) + void * param, + const char *cmd_line ) { printf("\r\n"); - history_cmd_execute(NULL, l->line); + history_cmd_execute(NULL, cmd_line); return 1; } @@ -103,17 +105,16 @@ callback_sleep_prompt( #endif } -history_t history = { - .ttyin = 0, - .ttyout = 1, +history_params_t history_avr_params = { .prompt = "avr: ", .process_line = _history_process_line, }; +static struct history_t *history = NULL; void history_avr_init() { - history_init(&history); - prompt_fd = history.ttyin; + history = history_new(0, 1, &history_avr_params, NULL); + prompt_fd = 0; /* ttyin */ // avr->sleep = callback_sleep_prompt; avr_global_logger_set(raw_std_logger); } @@ -128,9 +129,9 @@ void history_avr_idle() prompt_event++; if (history_redisplay) { history_redisplay = 0; - history_display(&history); + history_display(history); prompt_event++; } if (prompt_event) - history_idle(&history); + history_idle(history); } diff --git a/shavr/src/shavr.c b/shavr/src/shavr.c index ac29e9a..a978c4c 100644 --- a/shavr/src/shavr.c +++ b/shavr/src/shavr.c @@ -35,14 +35,12 @@ #include "sim_hex.h" #include "sim_gdb.h" #include "uart_pty.h" -#include "sim_vcd_file.h" #include "sim_args.h" #include "history_avr.h" uart_pty_t uart_pty; avr_t * avr = NULL; -avr_vcd_t vcd_file; elf_firmware_t *code = NULL; avr_t * @@ -50,10 +48,10 @@ sim_prepare( sim_args_t * a ); // TODO: Move to a header -typedef struct avr_flash_desc_t { - char avr_flash_path[1024]; - int avr_flash_fd; -} avr_flash_desc_t; +typedef struct shavr_runtime_t { + char avr_flash_path[1024]; + int avr_flash_fd; +} shavr_runtime_t; // avr special flash initalization // here: open and map a file to enable a persistent storage for the flash memory @@ -62,7 +60,7 @@ avr_special_init( avr_t * avr, void * data) { - avr_flash_desc_t *flash_data = (avr_flash_desc_t *)data; + shavr_runtime_t *flash_data = (shavr_runtime_t *)data; printf("%s\n", __func__); // open the file @@ -89,7 +87,7 @@ avr_special_deinit( avr_t* avr, void * data) { - avr_flash_desc_t *flash_data = (avr_flash_desc_t *)data; + shavr_runtime_t *flash_data = (shavr_runtime_t *)data; printf("%s\n", __func__); lseek(flash_data->avr_flash_fd, SEEK_SET, 0); @@ -107,7 +105,9 @@ avr_run_thread( void * ignore) { while (1) { - avr_run(avr); + int state = avr_run(avr); + if (state == cpu_Done || state == cpu_Crashed) + break; } return NULL; } @@ -117,7 +117,7 @@ main( int argc, const char *argv[]) { - avr_flash_desc_t flash_data; + shavr_runtime_t flash_data; sim_args_t args; if (sim_args_parse(&args, argc, argv, NULL)) { @@ -148,7 +148,7 @@ main( history_avr_init(); // pthread_t run; - //pthread_create(&run, NULL, avr_run_thread, NULL); + //pthread_create(&run, NULL, avr_run_thread, avr); printf("Running...\n"); while (1) { diff --git a/shavr/src/sim_prepare.c b/shavr/src/sim_prepare.c index 650d46b..36614c0 100644 --- a/shavr/src/sim_prepare.c +++ b/shavr/src/sim_prepare.c @@ -38,6 +38,7 @@ static avr_t * avr = NULL; static avr_vcd_t input; +// TODO: Signal the run thread and let it terminate static void sig_int( int sign) -- 2.39.5