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
461 stars 73 forks source link

Simulator not masking 16 bit timer register writes in fixed-TOP modes? #117

Closed drf5n closed 2 years ago

drf5n commented 2 years ago

Per https://forum.arduino.cc/t/why-doesnt-analogwrite-protect-its-writes-to-16bit-registers/961470/5?u=davex I think I found a difference between the silicon and wokwi behavior in masking off the top bits of 16 bit register writes in fixed TOP modes.

Here's a sim: https://wokwi.com/arduino/projects/324214622608425554

Output on a Mega is:

TCCR0A 0b11 TCCR0B 0b11 OCR0A 0 OCR0B 0
TCCR1A 0b10000001 TCCR1B 0b11 TCCR1C 0b0 OCR1A 255 OCR1B 0
On real mega, OCR1A is 255
On Wokwi mega, OCR1A is 65535

on Wokwi is

TCCR0A 0b11 TCCR0B 0b11 OCR0A 0 OCR0B 0
TCCR1A 0b10000001 TCCR1B 0b11 TCCR1C 0b0 OCR1A 65535 OCR1B 0
On actual mega, OCR1A is 255
On Wokwi mega, OCR1A is 65535

The relevant section of the https://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-7810-Automotive-Microcontrollers-ATmega328P_Datasheet.pdf datasheet is this bit from 15.9.4:

image
urish commented 2 years ago

Thanks for the super detailed report!

Fixed + released a new version. Your test project now shows "255", just like the Mega would!

drf5n commented 2 years ago

I see it works properly for timers 3,4,5 as well. Thanks.

urish commented 2 years ago

Great :)