Skip to content

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

@urish

Description

@urish

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".

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions