From 82b32bea14b181c34001dbe3b337b424a8e125d0 Mon Sep 17 00:00:00 2001
From: Markus Hitter <mah@jump-ing.de>
Date: Sat, 16 Nov 2013 22:28:42 +0100
Subject: ELF simulation info storage: bring SIMINFO_CPUFREQUENCY() into
 action.

Substantial part of this patch is a move from accessing protected
properties of class AvrDevice directly to using their Getters.
---
 src/avrreadelf.cpp | 27 +++++++++++++--------------
 src/avrreadelf.h   |  2 +-
 src/cmd/main.cpp   | 18 ++++++++++++------
 3 files changed, 26 insertions(+), 21 deletions(-)

diff --git a/src/avrreadelf.cpp b/src/avrreadelf.cpp
index 4c8eaa3..c18b817 100644
--- a/src/avrreadelf.cpp
+++ b/src/avrreadelf.cpp
@@ -89,18 +89,18 @@ typedef struct {
 } Elf32_Phdr;
 
 void ELFLoad(const AvrDevice * core) {
-    FILE * f = fopen(core->actualFilename.c_str(), "rb");
+    FILE * f = fopen(core->GetFname().c_str(), "rb");
     if(f == NULL)
-        avr_error("Could not open file: %s", core->actualFilename.c_str());
+        avr_error("Could not open file: %s", core->GetFname().c_str());
 
     Elf32_Ehdr header;
     fread(&header, sizeof(header), 1, f);
     if(header.e_ident[0] != 0x7F || header.e_ident[1] != 'E'
         || header.e_ident[2] != 'L' || header.e_ident[3] != 'F')
-        avr_error("File '%s' is not an ELF file", core->actualFilename.c_str());
+        avr_error("File '%s' is not an ELF file", core->GetFname().c_str());
     // TODO: fix endianity in header
     if(header.e_machine != 83)
-        avr_error("ELF file '%s' is not for Atmel AVR architecture (%d)", core->actualFilename.c_str(), header.e_machine);
+        avr_error("ELF file '%s' is not for Atmel AVR architecture (%d)", core->GetFname().c_str(), header.e_machine);
 
     for(int i = 0; i < header.e_phnum; i++) {
         fseek(f, header.e_phoff + i * header.e_phentsize, SEEK_SET);
@@ -115,7 +115,7 @@ void ELFLoad(const AvrDevice * core) {
             continue;  // not into a Flash
         if(progHeader.p_filesz != progHeader.p_memsz) {
             avr_error("Segment sizes 0x%x and 0x%x in ELF file '%s' must be the same",
-                progHeader.p_filesz, progHeader.p_memsz, core->actualFilename.c_str());
+                progHeader.p_filesz, progHeader.p_memsz, core->GetFname().c_str());
         }
         unsigned char * tmp = new unsigned char[progHeader.p_filesz];
         fseek(f, progHeader.p_offset, SEEK_SET);
@@ -134,20 +134,20 @@ unsigned int ELFGetDeviceNameAndSignature(const char *filename, char * devicenam
 #endif
 
 #ifndef _MSC_VER
-void ELFLoad(const AvrDevice * core) {
+void ELFLoad(AvrDevice * core) {
     bfd *abfd;
     asection *sec;
 
     // open file
     bfd_init();
-    abfd = bfd_openr(core->actualFilename.c_str(), NULL);
+    abfd = bfd_openr(core->GetFname().c_str(), NULL);
 
     if(abfd == NULL)
-        avr_error("Could not open file: %s", core->actualFilename.c_str());
+        avr_error("Could not open file: %s", core->GetFname().c_str());
 
     // check format
     if(bfd_check_format(abfd, bfd_object) == FALSE)
-        avr_error("File '%s' isn't a elf object", core->actualFilename.c_str());
+        avr_error("File '%s' isn't a elf object", core->GetFname().c_str());
 
     // reading out the symbols
     long storage_needed;
@@ -200,7 +200,7 @@ void ELFLoad(const AvrDevice * core) {
         }
         free(symbol_table);
     } else
-        avr_warning("Elf file '%s' has no symbol table!", core->actualFilename.c_str());
+        avr_warning("Elf file '%s' has no symbol table!", core->GetFname().c_str());
 
     // load program, data and - if available - eeprom, fuses and signature
     sec = abfd->sections;
@@ -240,9 +240,9 @@ void ELFLoad(const AvrDevice * core) {
                     avr_error("wrong device signature size in elf file, expected=3, given=%d", size);
                 } else {
                     unsigned int sig = (((tmp[2] << 8) + tmp[1]) << 8) + tmp[0];
-                    if(core->devSignature != -1 && sig != core->devSignature) {
+                    if(core->GetDeviceSignature() != -1 && sig != core->GetDeviceSignature()) {
                         free(tmp); // free memory before abort program
-                        avr_error("wrong device signature, expected=0x%x, given=0x%x", core->devSignature, sig);
+                        avr_error("wrong device signature, expected=0x%x, given=0x%x", core->GetDeviceSignature(), sig);
                     }
                 }
             }
@@ -251,7 +251,6 @@ void ELFLoad(const AvrDevice * core) {
             free(tmp);
         } else
         if ( ! strcmp(sec->name, ".siminfo")) {
-          avr_warning(".siminfo section found!");
           int size = sec->size;
           unsigned char *data, *data_ptr, *data_end;
 
@@ -272,7 +271,7 @@ void ELFLoad(const AvrDevice * core) {
                 data_ptr++; // the '\0' its self
                 break;
               case SIMINFO_TAG_CPUFREQUENCY:
-                avr_warning("frequency is %u", *(uint32_t *)data_ptr);
+                core->SetClockFreq((SystemClockOffset)1000000000 / *(uint32_t *)data_ptr);
                 data_ptr += sizeof(uint32_t);
                 break;
               default:
diff --git a/src/avrreadelf.h b/src/avrreadelf.h
index fb5a1e8..9695826 100644
--- a/src/avrreadelf.h
+++ b/src/avrreadelf.h
@@ -29,6 +29,6 @@
 #include "avrdevice.h"
 
 unsigned int ELFGetDeviceNameAndSignature(const char *filename, char *devicename);
-void ELFLoad(const AvrDevice * core);
+void ELFLoad(AvrDevice * core);
 
 #endif
diff --git a/src/cmd/main.cpp b/src/cmd/main.cpp
index 6c9160e..a55e1bf 100644
--- a/src/cmd/main.cpp
+++ b/src/cmd/main.cpp
@@ -145,7 +145,7 @@ int main(int argc, char *argv[]) {
     int global_gdb_debug = 0;
     bool globalWaitForGdbConnection = true; //please wait for gdb connection
     int userinterface_flag = 0;
-    unsigned long long fcpu = 4000000;
+    unsigned long long fcpu = 0;
     unsigned long long maxRunTime = 0;
     unsigned long long linestotrace = 1000000;
     UserInterface *ui;
@@ -237,9 +237,6 @@ int main(int argc, char *argv[]) {
                     cerr << "frequency is zero" << endl;
                     exit(1);
                 }
-                if(global_verbose_on)
-                    printf("Running with CPU frequency: %1.4f MHz (%lld Hz)\n",
-                           fcpu/1000000.0, fcpu);
                 break;
 
             case 'l':
@@ -424,8 +421,17 @@ int main(int argc, char *argv[]) {
     //if not gdb, the ui will be master controller :-)
     ui = (userinterface_flag == 1) ? new UserInterface(7777) : NULL;
     
-    dev1->SetClockFreq(1000000000 / fcpu); // time base is 1ns!
-    
+    if (fcpu != 0)
+        dev1->SetClockFreq((SystemClockOffset)1000000000 / fcpu); // time base is 1ns!
+
+    if (dev1->GetClockFreq() == 0)
+        avr_error("Clock frequency not given, use -F | --cpufrequency FREQ or "
+                  "insert a SIMINFO_CPUFREQUENCY(freq) macro into your source "
+                  "to specify it.");
+    avr_message("Running with CPU frequency: %1.3lf MHz (%lld Hz)\n",
+                (double)1000 / dev1->GetClockFreq(),
+                (unsigned long long)1000000000 / dev1->GetClockFreq());
+
     if(sysConHandler.GetTraceState())
         dev1->trace_on = 1;
     
-- 
1.8.3.2

