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

Timer value should not increment on the same cycle as TCNTn write #36

Closed urish closed 4 years ago

urish commented 4 years ago

According to the datasheet: "A CPU write overrides (has priority over) all counter clear or count operations".

Currently, writing to the TCNTn register modifies its value, and then the tick() function runs and immediately checks (and possibly updates) the new value.

This creates a discrepancy between the simulation and the real AVR core, and can be demonstrated by the following program:

int main() {
  int value = 0;
  cli();
  TCCR0A = 0; 
  TCCR0B = 1 << CS00; 
  TCNT0 = 0;
  // TCNT0 should not increment in this clock cycle
  value = TCNT0;
  sei();
  Serial.begin(115200);
  Serial.print(value);
}

Running this code in the simulation prints "1", while running it on an Arduino Uno board prints "0".