1. add -lz and -ldl to the linking options

2. allow reading from a write pipe: this is not only possible, but also used
   in the arduino library (wiring.c) to change a single output bit (read all
   bits, change one); this patch stores the last value written (initially 0) so
   that it can be retrived when read; the way this is done is not exactly
   right, as writing to PORTB/PORTC/PORTD has effect on reading only after one
   clock in the real hardware, requiring at least a nop in between

3. add info-dir-section to info file

4. add clock time to pin changes sent to the ui
   *** THIS MAY BREAK EXISTING APPLICATIONS ***

5. changed "Ran too long" into "Run finished"; the previous message looked like
   an error while in fact the run ended at the requested time

6. fixed heap insertion methods in systemclock.cpp: otherwise, serial transfers
   from the ui complete instantaneously

7. quick hack on hwuart.cpp to include U2X (double uart speed); this works with
   arduino, but is likely incorrect with other microcontrollers, if they do not
   have U2X; also, halving the prescaled clock is not what happens as described
   in the datasheet, which is instead to double the number of states in the
   receiver and transmitter state machine
   *** THIS IS QUICK HACK FOR ARDUINO, MAY BREAK simulavr ON OTHER MODELS ***

8. the new class PinStop allows stopping the simulation when a pin changes
   value; this only requires creating an object of this class, passing the Pin
   and the SystemClock objects to the constructor

9. in hwuart.cpp, the transition into RX_READ_STARTBIT is immediate: if
   pinRx==0 is detected while in state RX_READ_STOPBIT after the tenth sample
   or while in state RX_WAIT_FOR_LOWEDGE, then _this_ sample is the first of
   RX_READ_STARTBIT, not the next one

10. the new state RX_WAIT_FOR_HIGH in ui/serialrx.cpp (similar to that in
    hwuart.cpp) makes a line that just stays low not to begin a startbit; also:
    accept a byte only when the stopbit is correct (high)

11. variables traceClock and traceUart to allow tracing the clock and the uart

12. new class Measure to allow measuring time when writing to a port

diff -Naur simulavr-1.0.0/doc/simulavr.info simulavr-1.0.0-modified/doc/simulavr.info
--- simulavr-1.0.0/doc/simulavr.info	2012-02-12 16:43:26.000000000 +0100
+++ simulavr-1.0.0-modified/doc/simulavr.info	2016-01-29 17:19:50.000000000 +0100
@@ -1,6 +1,11 @@
 This is simulavr.info, produced by makeinfo version 4.13 from
 simulavr.texinfo.
 
+INFO-DIR-SECTION Simulators
+START-INFO-DIR-ENTRY
+* simulavr: (simulavr). A simulator for the Atmel AVR microcontrollers.
+END-INFO-DIR-ENTRY
+
 This file documents the simulavr program.
 
    Copyright (C) 2001, 2002, 2003 Theodore A. Roth
diff -Naur simulavr-1.0.0/src/avrdevice.cpp simulavr-1.0.0-modified/src/avrdevice.cpp
--- simulavr-1.0.0/src/avrdevice.cpp	2012-02-12 16:26:38.000000000 +0100
+++ simulavr-1.0.0-modified/src/avrdevice.cpp	2016-01-29 17:07:41.000000000 +0100
@@ -459,7 +459,7 @@
             if(BP.end() != find(BP.begin(), BP.end(), PC)) {
                 if(trace_on)
                     traceOut << "Breakpoint found at 0x" << hex << PC << dec << endl;
-                if(nextStepIn_ns != 0)
+                if(nextStepIn_ns != NULL)
                     *nextStepIn_ns=clockFreq;
                 untilCoreStepFinished = !(cpuCycles > 0);
                 dump_manager->cycle();
diff -Naur simulavr-1.0.0/src/hwuart.cpp simulavr-1.0.0-modified/src/hwuart.cpp
--- simulavr-1.0.0/src/hwuart.cpp	2012-02-12 16:26:38.000000000 +0100
+++ simulavr-1.0.0-modified/src/hwuart.cpp	2016-01-29 17:07:41.000000000 +0100
@@ -23,6 +23,10 @@
  *  $Id$
  */
 
+using namespace std;
+
+#include <iostream>
+#include <iomanip>
 #include "hwuart.h"
 #include "helper.h"
 
@@ -59,6 +63,8 @@
 #define UCSZ0 0x02
 #define UCPOL 0x01
 
+int traceUart = 0x00;
+
 void HWUart::SetUdr(unsigned char val) { 
     udrWrite=val;
     if ( usr&UDRE) { //the data register was empty
@@ -73,21 +79,26 @@
 void HWUart::SetUsr(unsigned char val) { 
     unsigned char usrold=usr;
 
-    if ( val & TXC) {
-        usr &=0xff-TXC; //clear TXC if 1 written to TXC
+    if (val & TXC) {
+        usr &= ~TXC; //clear TXC if 1 written to TXC
+        val &= ~TXC;
     }
 
     unsigned char irqold= ucr&usrold;
     unsigned char irqnew= ucr&usr;
 
-
     unsigned char changed=irqold^irqnew;
     unsigned char setnew= changed&irqnew;
     unsigned char clearnew= changed& (~irqnew);
 
     CheckForNewSetIrq(setnew);
     CheckForNewClearIrq(clearnew);
-} 
+
+    if (val & U2X)
+        usr |= U2X;
+    if (val & MPCM)
+        usr |= MPCM;
+}
 
 void HWUart::SetUbrr(unsigned char val) {
     ubrr = (ubrr & 0xff00) | val;
@@ -169,7 +180,10 @@
 
 unsigned int HWUart::CpuCycle() {
     baudCnt++; // TODO: this isn't implemented right, baud clock prescaler is a down counter!
-    if(baudCnt >= (ubrr + 1)) {
+
+    // quick hack: U2X should not divide the prescaler by two, rather it should
+    // double the number of states both in CpuCycleRx and CpuCycleTx
+    if(((usr&U2X)?baudCnt*2:baudCnt) >= (ubrr + 1)) {
         baudCnt = 0;
         CpuCycleRx();
         CpuCycleTx();
@@ -192,19 +206,31 @@
 
         switch (rxState) {
             case RX_WAIT_FOR_HIGH: //wait for startbit
+                if(traceUart)
+                    cout << "RX_WAIT_FOR_HIGH" << endl;
                 if (pinRx==1) rxState=RX_WAIT_FOR_LOWEDGE;
-                
+
                 break;
 
             case RX_WAIT_FOR_LOWEDGE:
-                if (pinRx==0) rxState=RX_READ_STARTBIT;
+                if(traceUart&0x02)
+                    cout << "RX_WAIT_FOR_LOWEDGE" << endl;
                 cntRxSamples=0;
                 rxLowCnt=0;
                 rxHighCnt=0;
-                break;
+                if (pinRx!=0)
+                    break;
+                rxState=RX_READ_STARTBIT;
+                if(traceUart)
+                    cout << "RX_WAIT_FOR_LOWEDGE -> RX_READ_STARTBIT" << endl;
+                // no break: this is already the first sample of the startbit
 
             case RX_READ_STARTBIT:
                 cntRxSamples++;
+                if(traceUart) {
+                    cout << "RX_READ_STARTBIT " << setw(2) << cntRxSamples;
+                    cout << "            " << pinRx << endl;
+                }
                 if (cntRxSamples>=8 && cntRxSamples<=10) {
                     if (pinRx==0) {
                         rxLowCnt++;
@@ -213,7 +239,7 @@
                     }
                 }
                 if (cntRxSamples>15) {
-                    if ( rxLowCnt>rxHighCnt) { //yes the startbit is low 
+                    if (rxLowCnt>rxHighCnt) { //yes the startbit is low 
                         cntRxSamples=0;
                         rxState= RX_READ_DATABIT;
                         rxDataTmp=0;
@@ -228,6 +254,10 @@
 
             case RX_READ_DATABIT:
                 cntRxSamples++;
+                if(traceUart) {
+                    cout << "RX_READ_DATABIT " << setw(2) << cntRxSamples;
+                    cout << "            " << pinRx << endl;
+                }
                 if (cntRxSamples>=8 && cntRxSamples<=10) {
                     if (pinRx==0) {
                         rxLowCnt++;
@@ -237,10 +267,16 @@
                 }
 
                 if (cntRxSamples>15) {
-                    if ( rxLowCnt<rxHighCnt) { //the bit was high
+                    if (rxLowCnt<rxHighCnt) { //the bit was high
+                        if(traceUart)
+                            cout << "rx: HIGH(0) " << endl;
                         rxDataTmp|=(1<<rxBitCnt);
                         readParity^=1; 
                     }
+                    else {
+                        if(traceUart)
+                            cout << "rx: LOW (1)" << endl;
+                    }
 
                     rxBitCnt++;
                     cntRxSamples=0;
@@ -261,6 +297,10 @@
 
             case RX_READ_PARITY:
                 cntRxSamples++;
+                if(traceUart) {
+                    cout << "RX_READ_PARITY" << setw(2) << cntRxSamples;
+                    cout << "            " << pinRx << endl;
+                }
                 if (cntRxSamples>=8 && cntRxSamples<=10) {
                     if (pinRx==0) {
                         rxLowCnt++;
@@ -272,7 +312,7 @@
                 if (cntRxSamples>15) {
                     bool actParity;
 
-                    if ( rxLowCnt<rxHighCnt) { //the bit was high 
+                    if (rxLowCnt<rxHighCnt) { //the bit was high 
                         actParity=1; 
                     } else {
                         actParity=0;
@@ -283,7 +323,7 @@
                     }
 
                     if (readParity==actParity) {
-                        usr&=0xff-UPE; //clear parity error
+                        usr&=~UPE; //clear parity error
                     } else {
                         usr|=UPE;
                     }
@@ -301,20 +341,27 @@
                         rxHighCnt++;
                     }
                 }
+                if(traceUart) {
+                    cout << "RX_READ_STOPBIT " << setw(2) << cntRxSamples;
+                    cout << "            " << pinRx << "           ";
+                    cout << rxLowCnt << " " << rxHighCnt << endl;
+                }
 
                 if (
-                        ((ucsrc&USBS) && (cntRxSamples>16)) || //if 2 stopbits used we have to wait full bit frame
-                        ((!(ucsrc&USBS)) && (cntRxSamples>10)) //in case of only 1 stopbit we can shorten the stopbit
+                        (cntRxSamples>15) || //if 2 stopbits used we have to wait full bit frame
+                        ((!(ucsrc&USBS)) && (cntRxSamples>10) && (pinRx==0)) //in case of only 1 stopbit we can shorten the stopbit
                    ) {
 
-                    if ( rxLowCnt<rxHighCnt) { //the bit was high this is ok
+                    if (rxLowCnt<rxHighCnt) { //the bit was high this is ok
                         udrRead=rxDataTmp&0xff;
-                        usr&=0xff-FE;
+                        if(traceUart)
+                            cout << "READ BYTE: " << udrRead << endl;
+                        usr &= ~FE;
                         if ( (ucr & CHR9) !=0 ) {
                             if ( rxDataTmp&0x100) { 
                                 ucr|=RXB8;
                             } else {
-                                ucr&=0xff-RXB8;
+                                ucr&=~RXB8;
                             }
 
                         }
@@ -325,18 +372,31 @@
                         cntRxSamples=0;
                         rxLowCnt=0;
                         rxHighCnt=0;
-                        rxState= RX_READ_STOPBIT2;  
+                        rxState= RX_READ_STOPBIT2;
                     } else {
                         if (usr&RXC) { //RXC is allready set->Overrun Error
                             usr|=OR;
                         }
                         usr|=RXC; //receiving is complete, regardless of framing error!
-                        if (rxLowCnt<rxHighCnt)
-                            rxState = RX_WAIT_FOR_LOWEDGE;
-                        else 
+                        if (rxLowCnt>=rxHighCnt)
                             rxState = RX_WAIT_FOR_HIGH;
+                        else if(pinRx==1)
+                            rxState = RX_WAIT_FOR_LOWEDGE;
+                        else {
+                            cntRxSamples=1;
+                            rxLowCnt=1;
+                            rxHighCnt=0;
+                            rxState = RX_READ_STARTBIT;
+                            if(traceUart) {
+                                cout << "RX_READ_STOPBIT -> READ_STARTBIT";
+                                cout << endl;
+                                cout << "RX_READ_STARTBIT ";
+                                cout << setw(2) << cntRxSamples;
+                                cout << "            " << pinRx << endl;
+                            }
+                        }
                     }
-                }   
+                }
                 break;
 
             case RX_READ_STOPBIT2:
@@ -348,12 +408,12 @@
                         rxHighCnt++;
                     }
                 } else if (cntRxSamples>10) { //the last stopbit could allways be shorter than normal data-bit
-                    if ( rxLowCnt<rxHighCnt) { //the bit was high this is ok
-                        usr&=0xff-FE;
+                    if (rxLowCnt<rxHighCnt) { //the bit was high this is ok
+                        usr &= ~FE;
                     } else { //stopbit was low so set framing error
                         usr|=FE;
                     }
-                    
+
                     usr|=RXC; //receiving is complete, regardless of framing error!
                     if (rxLowCnt<rxHighCnt)
                         rxState = RX_WAIT_FOR_LOWEDGE;
@@ -383,7 +443,7 @@
 }
 
 unsigned int HWUart::CpuCycleTx() {
-    /*************************************** TRANCEIVER PART **********************************/
+    /*************************************** TRANSMITTER PART **********************************/
     //unsigned char usr_old=usr;
 
     baudCnt16++;
@@ -611,9 +671,9 @@
     ubrr = 0;
     baudCnt = 0;
     baudCnt16 = 0;
-    
+
     regSeq = 0;
-    
+
     rxState = RX_WAIT_FOR_LOWEDGE;
     txState = TX_FIRST_RUN;
 
diff -Naur simulavr-1.0.0/src/Makefile.am simulavr-1.0.0-modified/src/Makefile.am
--- simulavr-1.0.0/src/Makefile.am	2012-02-12 16:26:38.000000000 +0100
+++ simulavr-1.0.0-modified/src/Makefile.am	2016-01-29 17:07:41.000000000 +0100
@@ -77,6 +77,7 @@
   hwtimer/icapturesrc.cpp hwstack.cpp hwtimer/hwtimer.cpp hwuart.cpp hwwado.cpp \
   ioregs.cpp irqsystem.cpp ui/keyboard.cpp ui/lcd.cpp memory.cpp \
   ui/mysocket.cpp net.cpp pin.cpp ui/extpin.cpp pinatport.cpp pinmon.cpp \
+  pinstop.cpp measure.cpp \
   rwmem.cpp ui/scope.cpp ui/serialrx.cpp ui/serialtx.cpp spisrc.cpp spisink.cpp \
   specialmem.cpp string2.cpp systemclock.cpp traceval.cpp ui/ui.cpp 
 
@@ -97,7 +98,8 @@
   config_deprecated.h decoder.h externaltype.h flash.h flashprog.h hwdecls.h \
   funktor.h hwacomp.h hwad.h hweeprom.h string2_template.h hwpinchange.h \
   hwport.h hwspi.h hwsreg.h hwstack.h hwuart.h hwwado.h ioregs.h irqsystem.h \
-  memory.h net.h pin.h pinatport.h pinnotify.h pinmon.h printable.h rwmem.h \
+  memory.h net.h pin.h pinatport.h pinnotify.h pinmon.h pinstop.h measure.h \
+  printable.h rwmem.h \
   simulationmember.h spisrc.h spisink.h specialmem.h systemclock.h \
   systemclocktypes.h traceval.h types.h
 
diff -Naur simulavr-1.0.0/src/Makefile.in simulavr-1.0.0-modified/src/Makefile.in
--- simulavr-1.0.0/src/Makefile.in	2012-02-12 16:26:48.000000000 +0100
+++ simulavr-1.0.0-modified/src/Makefile.in	2016-01-29 17:07:41.000000000 +0100
@@ -119,6 +119,7 @@
 	hwsreg.lo icapturesrc.lo hwstack.lo hwtimer.lo hwuart.lo \
 	hwwado.lo ioregs.lo irqsystem.lo keyboard.lo lcd.lo memory.lo \
 	mysocket.lo net.lo pin.lo extpin.lo pinatport.lo pinmon.lo \
+	pinstop.lo measure.lo \
 	rwmem.lo scope.lo serialrx.lo serialtx.lo spisrc.lo spisink.lo \
 	specialmem.lo string2.lo systemclock.lo traceval.lo ui.lo
 @USE_FAB_TRUE@am__objects_1 = avr_ATtiny2313.lo avr_ATtiny15.lo \
@@ -417,6 +418,7 @@
   hwtimer/icapturesrc.cpp hwstack.cpp hwtimer/hwtimer.cpp hwuart.cpp hwwado.cpp \
   ioregs.cpp irqsystem.cpp ui/keyboard.cpp ui/lcd.cpp memory.cpp \
   ui/mysocket.cpp net.cpp pin.cpp ui/extpin.cpp pinatport.cpp pinmon.cpp \
+  pinstop.cpp measure.cpp \
   rwmem.cpp ui/scope.cpp ui/serialrx.cpp ui/serialtx.cpp spisrc.cpp spisink.cpp \
   specialmem.cpp string2.cpp systemclock.cpp traceval.cpp ui/ui.cpp 
 
@@ -435,7 +437,8 @@
   config_deprecated.h decoder.h externaltype.h flash.h flashprog.h hwdecls.h \
   funktor.h hwacomp.h hwad.h hweeprom.h string2_template.h hwpinchange.h \
   hwport.h hwspi.h hwsreg.h hwstack.h hwuart.h hwwado.h ioregs.h irqsystem.h \
-  memory.h net.h pin.h pinatport.h pinnotify.h pinmon.h printable.h rwmem.h \
+  memory.h net.h pin.h pinatport.h pinnotify.h pinmon.h pinstop.h measure.h \
+  printable.h rwmem.h \
   simulationmember.h spisrc.h spisink.h specialmem.h systemclock.h \
   systemclocktypes.h traceval.h types.h
 
@@ -443,7 +446,7 @@
 simulavr_SOURCES = cmd/main.cpp cmd/dumpargs.cpp
 simulavr_LDADD = $(libsim_la_OBJECTS) $(LIBZ_FLAGS) \
   -L$(AVR_LIBBFD_LIBPATH) -lbfd -L$(AVR_LIBIBERTY_LIBPATH) -liberty \
-  $(LIBWSOCK_FLAGS)
+  $(LIBWSOCK_FLAGS) -lz -ldl
 
 @USE_VERILOG_TRUE@VPI_LIB = avr.vpi
 @USE_VERILOG_TRUE@libavrvpi_la_SOURCES = vpi.cpp cmd/dumpargs.cpp
@@ -672,6 +675,8 @@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/pin.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/pinatport.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/pinmon.Plo@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/pinstop.Plo@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/measure.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/prescalermux.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/rwmem.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/scope.Plo@am__quote@
diff -Naur simulavr-1.0.0/src/measure.cpp simulavr-1.0.0-modified/src/measure.cpp
--- simulavr-1.0.0/src/measure.cpp	1970-01-01 01:00:00.000000000 +0100
+++ simulavr-1.0.0-modified/src/measure.cpp	2016-01-29 17:07:41.000000000 +0100
@@ -0,0 +1,25 @@
+/*
+ *
+ */
+
+#include <measure.h>
+#include <iostream>
+#include <iomanip>
+
+using namespace std;
+
+RWTime::RWTime(TraceValueRegister *registry, SystemClock *sc):
+    RWMemoryMember(registry, "TIME")
+{
+  clock = sc;
+}
+
+unsigned char RWTime::get() const {
+  return 0x1e;
+}
+
+void RWTime::set(unsigned char c) {
+  cout << "SystemClock (request " << setw(3) << (int) c << "): ";
+  cout << setw(10) << clock->GetCurrentTime() << endl;
+}
+
diff -Naur simulavr-1.0.0/src/measure.h simulavr-1.0.0-modified/src/measure.h
--- simulavr-1.0.0/src/measure.h	1970-01-01 01:00:00.000000000 +0100
+++ simulavr-1.0.0-modified/src/measure.h	2016-01-29 17:07:41.000000000 +0100
@@ -0,0 +1,18 @@
+/*
+ *
+ */
+
+#include<rwmem.h>
+#include<traceval.h>
+#include<systemclock.h>
+
+class RWTime : public RWMemoryMember {
+  public:
+    RWTime(TraceValueRegister *registry, SystemClock *sc);
+  protected:
+    unsigned char get() const;
+    void set(unsigned char);
+  private:
+    SystemClock *clock;
+};
+
diff -Naur simulavr-1.0.0/src/pinstop.cpp simulavr-1.0.0-modified/src/pinstop.cpp
--- simulavr-1.0.0/src/pinstop.cpp	1970-01-01 01:00:00.000000000 +0100
+++ simulavr-1.0.0-modified/src/pinstop.cpp	2016-01-29 17:07:41.000000000 +0100
@@ -0,0 +1,22 @@
+#include "pinstop.h"
+
+using namespace std;
+
+PinStop::PinStop (Pin* p, SystemClock* sc) {
+	cout << "*** pinstop created" << endl;
+	prev = Pin::TRISTATE;
+	clock = sc;
+	prev = p->GetAnalog();
+	p->RegisterCallback(this);
+	}
+       
+void PinStop::PinStateHasChanged(Pin* p) {
+	// the check is necessary since this function gets called also when the
+	// pin has not changed its output value
+	if (prev != p->GetAnalog()) {
+		cout << "*** pin state changed; time=";
+		cout << clock->Instance().GetCurrentTime() << endl;
+		prev = p->GetAnalog();
+		clock->Instance().stop();
+		}
+	}
diff -Naur simulavr-1.0.0/src/pinstop.h simulavr-1.0.0-modified/src/pinstop.h
--- simulavr-1.0.0/src/pinstop.h	1970-01-01 01:00:00.000000000 +0100
+++ simulavr-1.0.0-modified/src/pinstop.h	2016-01-29 17:07:41.000000000 +0100
@@ -0,0 +1,25 @@
+#ifndef PINSTOP
+#define PINSTOP
+
+#include "systemclock.h"
+#include "pinnotify.h"
+
+/*
+ * create an object of this class passing the AVR pin name (e.g., "D5")
+ * and a systemclock; whenever the pin changes its value the clock stops
+ *
+ * there is no need to otherwise do anything with the resulting object, the
+ * constructor already registers as a listener for the pin changes
+ */
+
+class PinStop : public HasPinNotifyFunction {
+    public:
+        PinStop(Pin* p, SystemClock* sc);
+        ~PinStop() {}
+        void PinStateHasChanged(Pin* p);
+    private:
+        SystemClock* clock;
+	int prev;
+};
+
+#endif
diff -Naur simulavr-1.0.0/src/simulavr.i simulavr-1.0.0-modified/src/simulavr.i
--- simulavr-1.0.0/src/simulavr.i	2012-02-12 16:26:38.000000000 +0100
+++ simulavr-1.0.0-modified/src/simulavr.i	2016-01-29 17:07:41.000000000 +0100
@@ -25,7 +25,9 @@
 #include "spisink.h"
 #include "adcpin.h"
 #include "pinmon.h"
+#include "pinstop.h"
 #include "rwmem.h"
+#include "measure.h"
 #include "specialmem.h"
 #include "ui/scope.h"
 #include "avrerror.h"
@@ -75,7 +77,9 @@
 %include "spisink.h"
 %include "adcpin.h"
 %include "pinmon.h"
+%include "pinstop.h"
 %include "rwmem.h"
+%include "measure.h"
 %include "specialmem.h"
 %include "ui/scope.h"
 %include "avrerror.h"
diff -Naur simulavr-1.0.0/src/specialmem.cpp simulavr-1.0.0-modified/src/specialmem.cpp
--- simulavr-1.0.0/src/specialmem.cpp	2012-02-12 16:26:38.000000000 +0100
+++ simulavr-1.0.0-modified/src/specialmem.cpp	2016-01-29 17:07:41.000000000 +0100
@@ -40,14 +40,16 @@
         ofs.open(filename.c_str());
 }
 
+unsigned char last=0;
+
 void RWWriteToFile::set(unsigned char val) {
+    last=val;
     os << val;
     os.flush();
 }
 
 unsigned char RWWriteToFile::get() const {
-    cerr << "Invalid read access to RWWriteToFile register." << endl;
-    return 0;
+    return last;
 } 
 
 RWReadFromFile::RWReadFromFile(TraceValueRegister *registry,
diff -Naur simulavr-1.0.0/src/systemclock.cpp simulavr-1.0.0-modified/src/systemclock.cpp
--- simulavr-1.0.0/src/systemclock.cpp	2012-02-12 16:26:38.000000000 +0100
+++ simulavr-1.0.0-modified/src/systemclock.cpp	2016-01-29 17:07:41.000000000 +0100
@@ -35,6 +35,7 @@
 
 using namespace std;
 
+int traceClock = 0;
 
 template<typename Key, typename Value>
 MinHeap<Key, Value>::MinHeap()
@@ -67,10 +68,12 @@
 template<typename Key, typename Value>
 void MinHeap<Key, Value>::Insert(Key k, Value v)
 {
+	if(traceClock==1)
+		cout << "insert " << k << " " << v << endl;
 	resize(this->size()+1);
 	for(unsigned i = this->size();;) {
 		unsigned parent = i/2;
-		if(parent == 0 || (*this)[parent-1].first < k) {
+		if(parent == 0 || (*this)[parent-1].first <= k) {
 			(*this)[i-1].first = k;
 			(*this)[i-1].second = v;
 			return;
@@ -87,28 +90,43 @@
 void MinHeap<Key, Value>::RemoveMinimumAndInsert(Key k, Value v)
 {
 	assert(!this->empty());
+	if(traceClock==1) {
+		cout << "RemoveMinumAndInsert: k,v=" << k << "," << v << endl;
+		for(unsigned j = 0; j < this->size(); j++)
+		{
+			std::pair<Key,Value> item = (*this)[j];
+			cout << "pair: " << item.first << " " << item.second << endl;
+		}
+	}
 	unsigned i = 1;
 	for(;;) {
+		(*this)[i-1].first = k;
+		(*this)[i-1].second = v;
 		unsigned left = 2*i;
 		unsigned right = 2*i + 1;
 		unsigned smallest = i;
-		if(left-1 < this->size() && (*this)[left-1].first < k)
+		if(left-1 < this->size() &&
+			(*this)[left-1].first < (*this)[smallest-1].first)
 			smallest = left;
-		if(right-1 < this->size() && (*this)[right-1].first < k)
+		if(right-1 < this->size() &&
+			(*this)[right-1].first < (*this)[smallest-1].first)
 			smallest = right;
-		if(smallest == i) {
-			(*this)[smallest-1].first = k;
-			(*this)[smallest-1].second = v;
-			return;
-		}
-		Key k_temp = (*this)[smallest-1].first;
-		Value v_temp = (*this)[smallest-1].second;
-		(*this)[smallest-1].first = k;
-		(*this)[smallest-1].second = v;
-		k = k_temp;
-		v = v_temp;
+		if(smallest == i)
+			break;
+		if(traceClock==1)
+			cout << smallest << " " << left << " " << right << endl;
+		(*this)[i-1].first = (*this)[smallest-1].first;
+		(*this)[i-1].second = (*this)[smallest-1].second;
 		i = smallest;
 	}
+	if(traceClock==1) {
+		cout << "after:" << endl;
+		for(i = 0; i < this->size(); i++)
+		{
+			std::pair<Key,Value> item = (*this)[i];
+			cout << "pair: " << item.first << " " << item.second << endl;
+		}
+	}
 }
 
 
@@ -154,8 +172,10 @@
         SystemClockOffset nextStepIn_ns = -1;
         
         // do a step on simulation member
+	if(traceClock && !(currentTime % (traceClock*62)))
+		cout << "currentTime:   " << currentTime << endl;
         res = core->Step(untilCoreStepFinished, &nextStepIn_ns);
-        
+       
         if(nextStepIn_ns == 0) { // insert the next step behind the following!
             nextStepIn_ns = 1 + (syncMembers.IsEmpty() ? currentTime : syncMembers.front().first);
         } else if(nextStepIn_ns > 0)
@@ -213,6 +233,8 @@
 void SystemClock::Endless() {
     int steps = 0;
     
+    breakMessage=false;
+    
     signal(SIGINT, OnBreak);
     signal(SIGTERM, OnBreak);
 
@@ -230,6 +252,8 @@
 
 void SystemClock::Run(SystemClockOffset maxRunTime) {
     int steps = 0;
+
+    breakMessage = false;
     
     signal(SIGINT, OnBreak);
     signal(SIGTERM, OnBreak);
@@ -241,7 +265,7 @@
         Step(untilCoreStepFinished);
     }
 
-    cout << endl << "Ran too long.  Terminated after " << maxRunTime;
+    cout << endl << "Run finished.  Terminated at " << currentTime;
     cout << " simulated nanoseconds." << endl;
 
     Application::GetInstance()->PrintResults();
@@ -251,6 +275,8 @@
     int res = 0;
     bool untilCoreStepFinished;
     
+    breakMessage = false;
+    
     signal(SIGINT, OnBreak);
     signal(SIGTERM, OnBreak);
     
diff -Naur simulavr-1.0.0/src/ui/serialrx.cpp simulavr-1.0.0-modified/src/ui/serialrx.cpp
--- simulavr-1.0.0/src/ui/serialrx.cpp	2012-02-12 16:26:38.000000000 +0100
+++ simulavr-1.0.0-modified/src/ui/serialrx.cpp	2016-01-29 17:08:04.000000000 +0100
@@ -43,6 +43,10 @@
             SystemClock::Instance().Add(this); //as next Step() is called
         }
     }
+    else {
+        if(rxState == RX_WAIT_FOR_HIGH)
+            rxState = RX_WAIT_LOWEDGE;
+    }
 }
 
 void SerialRxBasic::Reset(){
@@ -98,11 +102,12 @@
             if (bitCnt>=maxBitCnt) {
                 // this bit IS STOP BIT... 
                 *timeToNextStepIn_ns= -1; //nothing more please
-                rxState= RX_WAIT_LOWEDGE;
+                rxState=(dataByte&0x4000) ? RX_WAIT_LOWEDGE : RX_WAIT_FOR_HIGH;
 
                 /// @todo This is bug if frame format is different (eg 7 or 9 bits)
                 unsigned char c=(unsigned char)((dataByte>>(16-maxBitCnt))&0xff); 
-                CharReceived(c);
+		if (dataByte&0x4000)
+                	CharReceived(c);
             } else {
                 *timeToNextStepIn_ns= (SystemClockOffset)1e9/baudrate/16*(7+7); //read middle of next bit
                 rxState=RX_READ_DATABIT_FIRST;
diff -Naur simulavr-1.0.0/src/ui/serialrx.h simulavr-1.0.0-modified/src/ui/serialrx.h
--- simulavr-1.0.0/src/ui/serialrx.h	2012-02-12 16:26:38.000000000 +0100
+++ simulavr-1.0.0-modified/src/ui/serialrx.h	2016-01-29 17:07:49.000000000 +0100
@@ -47,6 +47,7 @@
         int dataByte;
 
         enum T_RxState {
+            RX_WAIT_FOR_HIGH,
             RX_WAIT_LOWEDGE,
             RX_READ_STARTBIT,
             RX_READ_DATABIT_START,
diff -Naur simulavr-1.0.0/src/ui/ui.cpp simulavr-1.0.0-modified/src/ui/ui.cpp
--- simulavr-1.0.0/src/ui/ui.cpp	2012-02-12 16:26:38.000000000 +0100
+++ simulavr-1.0.0-modified/src/ui/ui.cpp	2016-01-29 17:07:41.000000000 +0100
@@ -28,6 +28,7 @@
 #include "ui.h"
 #include "hardware.h"
 #include "pin.h"
+#include "extpin.h"
 #include "systemclock.h"
 #include "avrerror.h"
 #include <sstream>
@@ -135,7 +136,7 @@
     }
     LastState[s]=c;
 
-    os << "set " << s << " " << c << endl;
+    os << "set " << s << " " << c << " " << SystemClock::Instance().GetCurrentTime() << endl;
     Write(os.str());
 
     //    SystemClock::Instance().Rescedule(this, 1000); //read ack back as fast as possible
