Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do timers work in simAVR? #65

Closed
leptun opened this issue Apr 20, 2020 · 1 comment
Closed

How do timers work in simAVR? #65

leptun opened this issue Apr 20, 2020 · 1 comment
Labels
question Further information is requested

Comments

@leptun
Copy link
Collaborator

leptun commented Apr 20, 2020

How do the timers work in simAVR? Do they function just like in the real hardware by having their clock tied to the CPU's clock source? I'm asking this, since the way the timers+WDT behave looks a lot like they are tied to the host's clock, not actually to the simulated program.
I have a feeling (not sure how to test it) that when the simulation slows down because of many IRQs or something like that, the timers keep on ticking at the usual rate. This would explain why I get WDT resets with the Print from SD menu when it has to load a lot of data (when it has to find a file on the card by filename).

I'm also curious why the CPU seems to be overclocked all the time. Is it really clocking so fast or are the timers running faster than they should (aka, _millis()/_micros() tick too fast)? Maybe we could make a "TPS" (ticks/s) reading derived from the program counter so that we can see how fast the simulation is running at any time.

@leptun leptun added the question Further information is requested label Apr 20, 2020
@leptun leptun changed the title How do timers work in simAVR How do timers work in simAVR? Apr 20, 2020
@vintagepc
Copy link
Owner

Jotting down some thoughts and observations on this so far:

I kinda poked at some of the timer stuff yesterday (but I need to do something simpler first... my brain is still melting after the dive into why avrdude-slic3r wasn't flashing things right.) I'll dig into this in more depth afterwards.(Holy crap, have I learned a lot about the hardware the last few weeks...)

I did some timing measurements which seem to suggest we're not horribly overclocked, with CPU ticks happening about every 40-50ish nanoseconds. (16MHz suggests once per 62 ns). so I think that it's a combination of things as the above would only explain being about 1/3 too fast, and not the very high values observed coming out of the timer for millis/micros.

Probably the place to start is timer2's overflows are ticking as expected, for the delay handling.

From what I've seen of the timers in general (and the wdt) they appear to register themselves for a number of AVR CPU cycles in the future, which is correct for the situation and should stay in sync when one cycle takes longer than expected due to excessive IRQ load, or if the simulation is overclocking.

I think the WDT issue is also partially compounded by #59.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants