Merge pull request #1 from schuay/master
[simavr] / simavr / sim / sim_vcd_file.c
index 612be56..2584643 100644 (file)
@@ -26,6 +26,7 @@
 #include <string.h>
 #include "sim_vcd_file.h"
 #include "sim_avr.h"
+#include "sim_time.h"
 
 void _avr_vcd_notify(struct avr_irq_t * irq, uint32_t value, void * param);
 
@@ -37,7 +38,7 @@ int avr_vcd_init(struct avr_t * avr, const char * filename, avr_vcd_t * vcd, uin
        vcd->period = avr_usec_to_cycles(vcd->avr, period);
        
        for (int i = 0; i < AVR_VCD_MAX_SIGNALS; i++) {
-               avr_init_irq(&vcd->signal[i].irq, i, 1);
+               avr_init_irq(&avr->irq_pool, &vcd->signal[i].irq, i, 1, NULL /* TODO IRQ name */);
                avr_irq_register_notify(&vcd->signal[i].irq, _avr_vcd_notify, vcd);
        }
        
@@ -83,20 +84,22 @@ static char * _avr_vcd_get_signal_text(avr_vcd_signal_t * s, char * out, uint32_
 
 static void avr_vcd_flush_log(avr_vcd_t * vcd)
 {
-       if (!vcd->logindex)
-               return;
-//     printf("avr_vcd_flush_log %d\n", vcd->logindex);
-       uint32_t oldbase = 0;   // make sure it's different
-       char out[48];
-
 #if AVR_VCD_MAX_SIGNALS > 32
        uint64_t seen = 0;
 #else
        uint32_t seen = 0;
 #endif
-       for (int li = 0; li < vcd->logindex; li++) {
+       uint64_t oldbase = 0;   // make sure it's different
+       char out[48];
+
+       if (!vcd->logindex)
+               return;
+//     printf("avr_vcd_flush_log %d\n", vcd->logindex);
+
+
+       for (uint32_t li = 0; li < vcd->logindex; li++) {
                avr_vcd_log_t *l = &vcd->log[li];
-               uint32_t base = avr_cycles_to_usec(vcd->avr, l->when - vcd->start);
+               uint64_t base = avr_cycles_to_nsec(vcd->avr, l->when - vcd->start);     // 1ns base
 
                // if that trace was seen in this usec already, we fudge the base time
                // to make sure the new value is offset by one usec, to make sure we get
@@ -108,7 +111,7 @@ static void avr_vcd_flush_log(avr_vcd_t * vcd)
                        
                if (base > oldbase || li == 0) {
                        seen = 0;
-                       fprintf(vcd->output, "#%ld\n", base);
+                       fprintf(vcd->output, "#%llu\n", (long long unsigned int)base);
                        oldbase = base;
                }
                seen |= (1 << l->signal->irq.irq);      // mark this trace as seen for this timestamp
@@ -136,6 +139,7 @@ int avr_vcd_add_signal(avr_vcd_t * vcd,
        s->size = signal_bit_size;
        s->alias = ' ' + vcd->signal_count ;
        avr_connect_irq(signal_irq, &s->irq);
+       return 0;
 }
 
 
@@ -149,7 +153,7 @@ int avr_vcd_start(avr_vcd_t * vcd)
                return -1;
        }
                
-       fprintf(vcd->output, "$timescale 1us $end\n");
+       fprintf(vcd->output, "$timescale 1ns $end\n");  // 1ns base
        fprintf(vcd->output, "$scope module logic $end\n");
 
        for (int i = 0; i < vcd->signal_count; i++) {
@@ -169,6 +173,7 @@ int avr_vcd_start(avr_vcd_t * vcd)
        fprintf(vcd->output, "$end\n");
        vcd->start = vcd->avr->cycle;
        avr_cycle_timer_register(vcd->avr, vcd->period, _avr_vcd_timer, vcd);   
+       return 0;
 }
 
 int avr_vcd_stop(avr_vcd_t * vcd)
@@ -180,6 +185,7 @@ int avr_vcd_stop(avr_vcd_t * vcd)
        if (vcd->output)
                fclose(vcd->output);
        vcd->output = NULL;
+       return 0;
 }