wokwi / avr8js

Arduino (8-bit AVR) simulator, written in JavaScript and runs in the browser / Node.js
https://blog.wokwi.com/avr8js-simulate-arduino-in-javascript/
MIT License
481 stars 78 forks source link

Incorrect timer count when stopping a timer #72

Closed urish closed 3 years ago

urish commented 3 years ago

When you stop a timer (by setting the CS bits to 0), the counter should keep running until the current instruction execution completes.

Here is a small Arduino program that reproduces the issue:

void setup() {
  byte tcnt = 0;
  asm(R"(
      EOR r1, r1      
      LDI r16, 0x1    ; TCCR2B = 1 << CS20;
      STS 0xb1, r16   ; Should start counting after this instruction
      STS 0xb1, r1    ; Should stop counting after this instruction
      NOP
      NOP
      LDS %0, 0xb2   ; TCNT should equal 2 at this point
  )": "=r"(tcnt));
  Serial.begin(115200);
  Serial.print("Result: ");
  Serial.println(tcnt);
}

void loop() {
}

When running on silicone, the output is Result: 2. However, running inside the simulation, you get: Result: 0.