core: added extra init/deinit functions to avr_t
authorChristian Balles <code@ballessay.de>
Tue, 19 Oct 2010 21:45:58 +0000 (23:45 +0200)
committerMichel Pollet <buserror@gmail.com>
Mon, 25 Oct 2010 11:59:00 +0000 (12:59 +0100)
allows programs like the simduino example to modify the init/deinit function
to fix a crash

Signed-off-by: Christian Balles <code@ballessay.de>
simavr/sim/sim_avr.c
simavr/sim/sim_avr.h

index 7add414..d21f109 100644 (file)
@@ -41,6 +41,8 @@ int avr_init(avr_t * avr)
        // cpu is in limbo before init is finished.
        avr->state = cpu_Limbo;
        avr->frequency = 1000000;       // can be overriden via avr_mcu_section
+       if (avr->special_init)
+               avr->special_init(avr);
        if (avr->init)
                avr->init(avr);
        avr->state = cpu_Running;
@@ -50,13 +52,16 @@ int avr_init(avr_t * avr)
 
 void avr_terminate(avr_t * avr)
 {
-       if (avr->vcd)
+       if (avr->special_deinit)
+               avr->special_deinit(avr);
+       if (avr->vcd) {
                avr_vcd_close(avr->vcd);
-       avr->vcd = NULL;
+               avr->vcd = NULL;
+       }
        avr_deallocate_ios(avr);
 
-       free(avr->flash);
-       free(avr->data);
+       if (avr->flash) free(avr->flash);
+       if (avr->data) free(avr->data);
        avr->flash = avr->data = NULL;
 }
 
index 9695c05..6d205d4 100644 (file)
@@ -100,6 +100,10 @@ typedef struct avr_t {
        
        // called at init time
        void (*init)(struct avr_t * avr);
+       // called at init time (for special purposes like using a memory mapped file as flash see: simduino)
+       void (*special_init)(struct avr_t * avr);
+       // called at termination time ( to clean special initalizations)
+       void (*special_deinit)(struct avr_t * avr);
        // called at reset time
        void (*reset)(struct avr_t * avr);