core: added new states
authorMichel Pollet <buserror@gmail.com>
Wed, 14 Sep 2011 08:10:39 +0000 (09:10 +0100)
committerMichel Pollet <buserror@gmail.com>
Wed, 14 Sep 2011 08:10:39 +0000 (09:10 +0100)
States cpu_Done and cpu_Crashed allow graceful exit
and cleanup of simavr applications.

Signed-off-by: Markus Lampert <mlampert@telus.net>
Signed-off-by: Michel Pollet <buserror@gmail.com>
simavr/sim/run_avr.c
simavr/sim/sim_avr.c
simavr/sim/sim_avr.h

index 9356c95..e43424f 100644 (file)
@@ -166,8 +166,11 @@ int main(int argc, char *argv[])
        signal(SIGINT, sig_int);
        signal(SIGTERM, sig_int);
 
-       for (;;)
-               avr_run(avr);
+       for (;;) {
+               int state = avr_run(avr);
+               if ( state == cpu_Done || state == cpu_Crashed)
+                       break;
+       }
        
        avr_terminate(avr);
 }
index 6d7f088..885bbdc 100644 (file)
@@ -99,7 +99,7 @@ void avr_sadly_crashed(avr_t *avr, uint8_t signal)
                        avr_gdb_init(avr);
        } 
        if (!avr->gdb)
-               exit(1); // no gdb ?
+               avr->state = cpu_Crashed;
 }
 
 static void _avr_io_command_write(struct avr_t * avr, avr_io_addr_t addr, uint8_t v, void * param)
@@ -210,9 +210,10 @@ void avr_callback_run_gdb(avr_t * avr)
 
        if (avr->state == cpu_Sleeping) {
                if (!avr->sreg[S_I]) {
-                       printf("simavr: sleeping with interrupts off, quitting gracefully\n");
+                       if ( avr->log_level) printf("simavr: sleeping with interrupts off, quitting gracefully\n");
                        avr_terminate(avr);
-                       exit(0);
+                       avr->state = cpu_Done;
+                       return;
                }
                /*
                 * try to sleep for as long as we can (?)
@@ -262,9 +263,10 @@ void avr_callback_run_raw(avr_t * avr)
 
        if (avr->state == cpu_Sleeping) {
                if (!avr->sreg[S_I]) {
-                       printf("simavr: sleeping with interrupts off, quitting gracefully\n");
+                       if ( avr->log_level) printf("simavr: sleeping with interrupts off, quitting gracefully\n");
                        avr_terminate(avr);
-                       exit(0);
+                       avr->state = cpu_Done;
+                       return;
                }
                /*
                 * try to sleep for as long as we can (?)
index 4ab0a38..b8091b6 100644 (file)
@@ -73,6 +73,8 @@ enum {
 
        cpu_Step,               // run ONE instruction, then...
        cpu_StepDone,   // tell gdb it's all OK, and give it registers
+       cpu_Done,       // avr software stopped gracefully
+       cpu_Crashed,    // avr software crashed (watchdog fired)
 };
 
 // this is only ever used if CONFIG_SIMAVR_TRACE is defined