Author Topic: Completely predictable instruction timing  (Read 11939 times)

0 Members and 2 Guests are viewing this topic.

Online Kleinstein

  • Super Contributor
  • ***
  • Posts: 17310
  • Country: de
Re: Completely predictable instruction timing
« Reply #25 on: May 05, 2025, 09:20:04 am »
The AVRs / PICs and many other simple CPUs use some minimal pipeling or at least prefetch. The pipeline is however very simple and not real pipeling stall is possible. It can complicate the timing for a few instructions, like conditional branches. These may well take a different time whether the branch is taken or not. For the use this has not major consequence as one has different program branches and this different code anyway. AFAIK with the AVRs some complex instruction with pointer increments can in some cases (carry over to high byte) also take an extra cycle. So these instructions would have to be avoided.

For the ARM and similar processors it mainly gets complicated with a clock faster than the flash memory access and some buffers to accelerate the memory access most of the time. This gives hard to predict / calculate delays. Just the pipeline stall is still predictable and compilers may take it into account for optimization - so still doable, though a bit complicated. DMA can also mix up the timing totally as it can delay memory access. Similar interrupts tends to complicate things as there can be extra delay depending on the command executed when the interrupt happens.
With the more powerfull µCs like the ARM based µCs one would use the timer hardware for exact timing. Quite some things can be done via direct signals between pheriphery parts (like comparator triggers timer) - the details and what works an what does not work still depends on the µC model.
In some areas one can use the timer HW to resync the code execution, e.g. to compensate for the interrupt jitter.

For may DIY MS ADC I have done both versions: the AVR version via cycle counting in ASM code and the STM32L051 version with coupled timer and comparator perphery. Both ways work, with there limitations.

Realtime as in RTOS is a different thing - this is to provide a guaranteed maximum reaction time, but not clock accurate timing.
 

Online Berni

  • Super Contributor
  • ***
  • Posts: 5433
  • Country: si
Re: Completely predictable instruction timing
« Reply #26 on: May 05, 2025, 10:41:11 am »
I was reading more about ARM Cortex-M and was wondering where 8/16 bit processors still had a technical edge (not cost / availability / program complexity etc.) over more complex ARM processors. That is what prompted this question. If you have a pipelined processor how do you "even out" the timing difference between a a smoothly flowing pipe vs a stalled / bad data filled one? AVR8 and PIC16 are both pipelined so how do they have predictable timing?

I was thinking that an unpipelined processor like the original 8051 / (or perhaps Z80 as peter-h says)  could definitely have this predictability, but does any pipelined processor have it?

If the ultimate goal is timing predictability, do you just poison all cache / pipelines with lots of NOPs before the interesting parts happen to force everything to operate at the same (but slowest) speed?

Pipelining is not what causes this.

Even an 8bit AVR is pipelined, that's how it can do 1 instruction per clock cycle. It just means that there are multiple instructions "in flight" trough the CPU core where each instruction might be at a different stage of execution. This just helps keep more of the CPUs logic busy, but doesn't actually introduce extra latency. Only place extra latency shows up is when branching since this means flushing the pipeline. However the latency jump is constant, so this just shows up by branch instructions often needing 4x or even more cycles to execute.

When things get funky is Superscalar architecture CPUs.
These are capable of executing more than 1 instruction per clock cycle and this opens a whole new can of worms. Obviously you can't simultaneously execute two instructions that both access outside memory since the memory bus can only do 1 operation per cycle (also memory tends to run at a slower clock than the CPU core at these advanced high performance CPUs). But if all the other instructions move internal CPU registers then they can all execute simultaneously without problem, that is unless you perhaps tried to do too many ADD instructions and the core ran out of ALUs to perform the addition on, so some won't execute parallel...etc So this makes the relationships so complex that it is infeasible to document exact timing behavior.

The smaller ARM cores used in MCUs are often not superscalar and the slower ones are so slow that you can run RAM at identical clock to the core (hence no cache needed). Those have well predictable instruction timings. Faster ones need a cache because the memory subsystem can't scale as fast as CPU cores can. Often these caches can be simply turned off (but heavily impacts performance)

If you have an application with very strict cycle accurate timing requirements then just plonk down a small MCU to handle it, leave the rest of the heavy lifting to a separate high performance MCU. If you need  combined cycle accurate high performance, then a MCU is the wrong solution, use a more specialized chip like XMOS (mentioned above, they are nice indeed) or a FPGA.
 

Offline gerbay

  • Regular Contributor
  • *
  • Posts: 91
  • Country: tr
Re: Completely predictable instruction timing
« Reply #27 on: May 05, 2025, 11:37:45 am »
As long as there are conditional branches in the code we write, counting instructions is meaningless. You may be right for very small codes, but for large real-time systems, counting instructions makes no sense.
 

Offline mikerj

  • Super Contributor
  • ***
  • Posts: 3671
  • Country: gb
Re: Completely predictable instruction timing
« Reply #28 on: May 05, 2025, 01:05:52 pm »
As long as there are conditional branches in the code we write, counting instructions is meaningless.

Not true, provided the total cycle time for branch and no branch conditions is consistent then you can write cycle accurate isochronous code.  This was a core concept of PIC programming back in the day when coding for an application in which every instruction counted.
 

Offline gerbay

  • Regular Contributor
  • *
  • Posts: 91
  • Country: tr
Re: Completely predictable instruction timing
« Reply #29 on: May 05, 2025, 01:47:15 pm »
As long as there are conditional branches in the code we write, counting instructions is meaningless.

Not true, provided the total cycle time for branch and no branch conditions is consistent then you can write cycle accurate isochronous code.  This was a core concept of PIC programming back in the day when coding for an application in which every instruction counted.

I already mentioned that it could be for small codes. You are talking about PIC MCUs or equivalent MCUs. You can continue to count instructions in such MCUs. Today, even MCUs have switched to superscalar architecture. ARM Cortex-M7 can be given as an example. There is no point in counting instructions in such an MCU. If determinism is tried to be achieved by counting instructions, I think the system design is not very good.
 

Online mtwieg

  • Super Contributor
  • ***
  • Posts: 1861
  • Country: us
Re: Completely predictable instruction timing
« Reply #30 on: May 05, 2025, 03:25:04 pm »
I’m not an expert on this, but I think that an instruction on any processor will always take the same amount of time to run, if the system state is identical.

A slightly confused question.

All silicon I know of, will inherently take a fixed time to fetch + decode+ execute, it is other factors like time to deliver the opcode to the CPU fetch unit, and in the case of interrupts, time to complete other instructions.

Yeah depending on what assumptions one makes, any MCU/CPU can be considered perfectly deterministic. It's unclear what the OP is actually asking for.
 

Online SiliconWizard

  • Super Contributor
  • ***
  • Posts: 17840
  • Country: fr
Re: Completely predictable instruction timing
« Reply #31 on: May 05, 2025, 03:47:21 pm »
We don't quite know, this is kind of a recurring topic, though, and so we are assuming that the underlying reason is always the same: deterministic execution time for real-time systems. Maybe it's not what it is about this time.

Otherwise, the answer keeps pretty much being the same:
- Fully exact and predictable execution time per individual instruction is often not per se required to meet hard real time requirements. Bounded execution time is (and even so, there can be schemes that can accomodate a given piece of code not to fully complete and yet meet deadlines, as long as said code can be interrupted with no unacceptable consequence...)
- There are otherwise devices that make it "easier": either those simple, cycle-accurate CPUs, or XMOS processors as tggzzz mentioned. Or FPGAs / similar. The Parallax Propeller processors are also an interesting (although much less powerful) alternative to XMOS processors, for anyone curious.

As gerbay said, depending on the reason the question is asked, it may be the wrong question. Here, we don't know.

But if the OP doesn't yet know about them, I suggest looking at XMOS and Propeller processors, to get to know something different that may be more in the direction of what they have in mind, apart of course from implementing custom sequential logic on FPGA/programmable logic or silicon directly.

 

Online Berni

  • Super Contributor
  • ***
  • Posts: 5433
  • Country: si
Re: Completely predictable instruction timing
« Reply #32 on: May 05, 2025, 07:30:29 pm »
I already mentioned that it could be for small codes. You are talking about PIC MCUs or equivalent MCUs. You can continue to count instructions in such MCUs. Today, even MCUs have switched to superscalar architecture. ARM Cortex-M7 can be given as an example. There is no point in counting instructions in such an MCU. If determinism is tried to be achieved by counting instructions, I think the system design is not very good.

Don't think anyone instruction counts the flow around an entire application.

Usually you are only interested in the timing within one small walled off piece of code. Like maybe a bitbang routine where you need precise timing only while the function is executing, so you can turn off interrupts and just count whatever is inside that function. Or you might be interested in the timing of an interrupt handler, so you know X cycles elapse between a timer firing and a certain line of code executing.

Not all ARM MCUs are complex enough to be superscalar. There are still plenty of smaller Cortex M chips that are focused on being small simple and low power. There performance is not the focus, so they stick to simple sequential pipelines. Also those are the ones that are slow enough where RAM is fast enough to keep up with the CPU so a cache is not needed.

Interrupt entry latency is also usually constant on these simpler ARMs. Or some bigger chips even have a feature to artificially increase the latency of interrupt entry to the worst case scenario, this makes interrupt timing a consistent number of cycles at the expense of some performance. Another trick on the bigger chips is placing the timing critical code into a memory area that is closer to the CPU (some chips have a small amount of "tightly coupled RAM" that is fast enough to run at full CPU speed, hence no cache needed)

So you can get cycle accurate timing with modern chips if you pick carefully, but yeah once you go to the highest performance chips it is not there, the chips simply wouldn't be able to run at those high performance levels without caches and similar tricks.
 

Offline tggzzz

  • Super Contributor
  • ***
  • Posts: 23122
  • Country: gb
  • Numbers, not adjectives
    • Having fun doing more, with less
Re: Completely predictable instruction timing
« Reply #33 on: May 05, 2025, 09:55:14 pm »
As long as there are conditional branches in the code we write, counting instructions is meaningless. You may be right for very small codes, but for large real-time systems, counting instructions makes no sense.

Conditional branches are irrelevant to the point. You are interested in the maximum time between two points, by whichever path is the slowest.

The number of bytes in the system is irrelevant. What matters is the timing requirements (guaranteed latency or statistical latency) and the mechanisms used to satisfy the requirements.
There are lies, damned lies, statistics - and ADC/DAC specs.
Glider pilot's aphorism: "there is no substitute for span". Retort: "There is a substitute: skill+imagination. But you can buy span".
Having fun doing more, with less
 

Offline PCB.Wiz

  • Super Contributor
  • ***
  • Posts: 3426
  • Country: au
Re: Completely predictable instruction timing
« Reply #34 on: May 05, 2025, 11:28:55 pm »
...I was thinking that an unpipelined processor like the original 8051 / (or perhaps Z80 as peter-h says)  could definitely have this predictability, but does any pipelined processor have it?

If the ultimate goal is timing predictability, do you just poison all cache / pipelines with lots of NOPs before the interesting parts happen to force everything to operate at the same (but slowest) speed?

Or, you take the route of the PI PICO and use a PIO state engine microcode.

Some details of that are a little frustrating - they have no wait on edge and no single clock granular (time-out + wait on edge)
 
 

Offline tooki

  • Super Contributor
  • ***
  • Posts: 16141
  • Country: ch
Re: Completely predictable instruction timing
« Reply #35 on: May 06, 2025, 06:33:37 pm »
Um.  I was very nonplussed to discover that "Real Time OS" does not necessarily imply "fast" or "deterministic"; usually it just means "guaranteed latency less than some quantum (that is much larger than a single instruction cycle.)"

Not even that. "Real-time OS" is really one of those meaningless marketing wank terms (like low drop-out regulator or low-noise amplifier), which gives you some idea about the intended applications, but no more than that. Definitely it does not mean anything specific in how it's implemented, or what kind of timing guarantees it exactly gives. IOW, something being called an RTOS is enough to make it an RTOS.
I think that's a bit of an exaggeration.

For the extremely critical applications, there are so-called "hard real-time" OSes, which by definition are the ones whose timing is completely deterministic.
 

Offline tggzzz

  • Super Contributor
  • ***
  • Posts: 23122
  • Country: gb
  • Numbers, not adjectives
    • Having fun doing more, with less
Re: Completely predictable instruction timing
« Reply #36 on: May 06, 2025, 06:43:14 pm »
Um.  I was very nonplussed to discover that "Real Time OS" does not necessarily imply "fast" or "deterministic"; usually it just means "guaranteed latency less than some quantum (that is much larger than a single instruction cycle.)"

Not even that. "Real-time OS" is really one of those meaningless marketing wank terms (like low drop-out regulator or low-noise amplifier), which gives you some idea about the intended applications, but no more than that. Definitely it does not mean anything specific in how it's implemented, or what kind of timing guarantees it exactly gives. IOW, something being called an RTOS is enough to make it an RTOS.
I think that's a bit of an exaggeration.

For the extremely critical applications, there are so-called "hard real-time" OSes, which by definition are the ones whose timing is completely deterministic.

Or rather the timing is guaranteed to be within specified bounds, without exception - and definitely without statistics such as mean (ugh!) or 95th percentile etc.
There are lies, damned lies, statistics - and ADC/DAC specs.
Glider pilot's aphorism: "there is no substitute for span". Retort: "There is a substitute: skill+imagination. But you can buy span".
Having fun doing more, with less
 

Online Kleinstein

  • Super Contributor
  • ***
  • Posts: 17310
  • Country: de
Re: Completely predictable instruction timing
« Reply #37 on: May 06, 2025, 07:20:43 pm »
Things like a real time OS can usually not guarantee absolute clock cycle stable reaction times. This is a thing that need either speciallized periphery hardware or a suitable CPU with fixed execution times and speciallized ASM code - so usually not using an OS, or if at all an code part that comed with the OS and would fully occupy the CPU for that task.

It gets rather tricky to get more than one job done under to constraints of cycle accurate timing from the code (it is possible, but tricky as all cases need to take the same number of cycles).

The real time OS things are more like guarantee something like 1 µs or 10 µs response time, but this would usually still be hundreds of clock cycles. Normal compilers can also not generate code with a defiend run time they more like try to optimize for a fast execution, and don't create code for a specific delay / speed. In some cases a simulator could simulate the actual time needed, so one could check code for a defined run time.
 

Offline gerbay

  • Regular Contributor
  • *
  • Posts: 91
  • Country: tr
Re: Completely predictable instruction timing
« Reply #38 on: May 06, 2025, 07:31:27 pm »
Things like a real time OS can usually not guarantee absolute clock cycle stable reaction times. This is a thing that need either speciallized periphery hardware or a suitable CPU with fixed execution times and speciallized ASM code - so usually not using an OS, or if at all an code part that comed with the OS and would fully occupy the CPU for that task.

It gets rather tricky to get more than one job done under to constraints of cycle accurate timing from the code (it is possible, but tricky as all cases need to take the same number of cycles).

The real time OS things are more like guarantee something like 1 µs or 10 µs response time, but this would usually still be hundreds of clock cycles. Normal compilers can also not generate code with a defiend run time they more like try to optimize for a fast execution, and don't create code for a specific delay / speed. In some cases a simulator could simulate the actual time needed, so one could check code for a defined run time.

So can we say that companies that pay for rtos like vxworks, integrity, lynxos, qnx, pikeos are paying for nothing?
 

Offline nctnico

  • Super Contributor
  • ***
  • Posts: 30412
  • Country: nl
    • NCT Developments
Re: Completely predictable instruction timing
« Reply #39 on: May 06, 2025, 07:46:38 pm »
Things like a real time OS can usually not guarantee absolute clock cycle stable reaction times. This is a thing that need either speciallized periphery hardware or a suitable CPU with fixed execution times and speciallized ASM code - so usually not using an OS, or if at all an code part that comed with the OS and would fully occupy the CPU for that task.

It gets rather tricky to get more than one job done under to constraints of cycle accurate timing from the code (it is possible, but tricky as all cases need to take the same number of cycles).

The real time OS things are more like guarantee something like 1 µs or 10 µs response time, but this would usually still be hundreds of clock cycles. Normal compilers can also not generate code with a defiend run time they more like try to optimize for a fast execution, and don't create code for a specific delay / speed. In some cases a simulator could simulate the actual time needed, so one could check code for a defined run time.

So can we say that companies that pay for rtos like vxworks, integrity, lynxos, qnx, pikeos are paying for nothing?
They are not paying for nothing but the fact is an RTOS is still software and even though we have GHz microcontrollers nowadays, software response time is still in the microseconds range. And for many realtime applications that can be more than good enough. When people read realtime, some immediately jump to nano-second or even pico-second time accuracy while in many cases the actual requirements are in the milli-second range.

What an RTOS typically brings to the table are device drivers, protocol stacks, an organised way to deal with processes and messaging between them. Although you have to research whether an RTOS is a good fit for your project. Wrongly applied, an RTOS can make things worse instead of better. A typical failure is having too many seperate processes which then smother the whole system with thread concurrency and synchronisation issues.
« Last Edit: May 06, 2025, 07:50:15 pm by nctnico »
There are small lies, big lies and then there is what is on the screen of your oscilloscope.
 

Offline iMo

  • Super Contributor
  • ***
  • Posts: 6978
  • Country: li
Re: Completely predictable instruction timing
« Reply #40 on: May 06, 2025, 07:54:51 pm »
Afaik avr/pic/dspic/msp430/cortex_M0_M0+ are in-order exec models, thus predictable and clock precise.
CM7 (and perhaps M3/M4..) may use dual pipe-lining (and mostly in-order exec but not sure with M7) and pic32MX/MZ series may use (or it is using) out-of-order execution which may cause issues. And when talking lowest jitter any onchip PLL based clock will always be worse than clocking them from an external quality oscillator..
PS: I spent my "youth" :) counting cycles with pics, there were even some tools generating complex asm structures with many loops for generating any precise frequencies or periods/duty cycles (I have here several counters and LC meters built around clock precise pics).

PS: Also using onchip oscillators is not the best solution when chasing jitter. The crystal oscillator could be modulated by the internal mcu's noise (and it is in practice). So best is to use an external oscillator driving the mc's clock input hard (like the canned oscillators or special designs).
« Last Edit: May 06, 2025, 08:31:42 pm by iMo »
Readers discretion is advised..
 
The following users thanked this post: PCB.Wiz

Offline tggzzz

  • Super Contributor
  • ***
  • Posts: 23122
  • Country: gb
  • Numbers, not adjectives
    • Having fun doing more, with less
Re: Completely predictable instruction timing
« Reply #41 on: May 06, 2025, 09:31:21 pm »
Things like a real time OS can usually not guarantee absolute clock cycle stable reaction times. This is a thing that need either speciallized periphery hardware or a suitable CPU with fixed execution times and speciallized ASM code - so usually not using an OS, or if at all an code part that comed with the OS and would fully occupy the CPU for that task.

It is helpful to have an MCU where every peripheral has an associated counter, noting the clock cycle on which an input occurred, and specifying the clock cycle on which the next output should occur. If you also have language support for the peripheral/counter pair, then it becomes trivial to specify cycle accurate i/o at the application task level.

xC plus xCORE MCUs have that.

Why don't all other MCUs? Because they haphazardly accreted features with no overall hardware-plus-software conceptual design. And that's a manifestation of the unnatural division between hardware and software mentality/skillsets.

Quote
It gets rather tricky to get more than one job done under to constraints of cycle accurate timing from the code (it is possible, but tricky as all cases need to take the same number of cycles).

That is only true with traditional (i.e. old fashioned :) ) MCUs. Some modern embedded multi-core MCUs simply dedicate one core to each task.

That removes the need for interrupts. If the cores don't have caches, then you achieve timings that can be calculated without executing the code. Providing that worst case timing is shorter than the i/o requirements, then you have guaranteed hard realtime performance.

Don't settle for the imperfections of the past; demand more for the future :)
There are lies, damned lies, statistics - and ADC/DAC specs.
Glider pilot's aphorism: "there is no substitute for span". Retort: "There is a substitute: skill+imagination. But you can buy span".
Having fun doing more, with less
 

Offline tggzzz

  • Super Contributor
  • ***
  • Posts: 23122
  • Country: gb
  • Numbers, not adjectives
    • Having fun doing more, with less
Re: Completely predictable instruction timing
« Reply #42 on: May 06, 2025, 09:37:59 pm »
Afaik avr/pic/dspic/msp430/cortex_M0_M0+ are in-order exec models, thus predictable and clock precise.

Do they have any form of cache? Data, instruction, TLB, etc? If so, all bets are off.

Do they have and use interrupts? If so, all bets are off.

Quote
PS: Also using onchip oscillators is not the best solution when chasing jitter. The crystal oscillator could be modulated by the internal mcu's noise (and it is in practice). So best is to use an external oscillator driving the mc's clock input hard (like the canned oscillators or special designs).

Depends on the cause and scale of the jitter. If the jitter is introduced by the software (e.g. cache misses, interrupts) or synchronisation across clock boundaries, then the jitter will be in quanta of one clock cycle.
There are lies, damned lies, statistics - and ADC/DAC specs.
Glider pilot's aphorism: "there is no substitute for span". Retort: "There is a substitute: skill+imagination. But you can buy span".
Having fun doing more, with less
 

Online Siwastaja

  • Super Contributor
  • ***
  • Posts: 11440
  • Country: fi
Re: Completely predictable instruction timing
« Reply #43 on: May 07, 2025, 05:26:38 am »
Um.  I was very nonplussed to discover that "Real Time OS" does not necessarily imply "fast" or "deterministic"; usually it just means "guaranteed latency less than some quantum (that is much larger than a single instruction cycle.)"

Not even that. "Real-time OS" is really one of those meaningless marketing wank terms (like low drop-out regulator or low-noise amplifier), which gives you some idea about the intended applications, but no more than that. Definitely it does not mean anything specific in how it's implemented, or what kind of timing guarantees it exactly gives. IOW, something being called an RTOS is enough to make it an RTOS.
I think that's a bit of an exaggeration.

For the extremely critical applications, there are so-called "hard real-time" OSes, which by definition are the ones whose timing is completely deterministic.

Proving my point: you now have to add that extra qualifier "hard" to convey that message.

RTOS in modern-day usage usually just means "OS", just like "LDO" means "linear regulator" for many. That is why I found tggzzz's "RTOS implemented in silicon" such funny remark, as it is totally detached from the reality people expect from RTOS'es; basically tggzzz just wanted to say "real time implemented in silicon", and not "operating system implemented in silicon". And yet most people mean just "operating system".

The #1 reason to use an RTOS is the same as using any general purpose OS: to be able to slap together large pieces of existing code, including black box libraries, which depend on concepts like blocking function calls, time-slicing threading and so on. And in most cases people could not care less about any real-time guarantees, or understand how to prove anything about it.
 
The following users thanked this post: tooki

Online Berni

  • Super Contributor
  • ***
  • Posts: 5433
  • Country: si
Re: Completely predictable instruction timing
« Reply #44 on: May 07, 2025, 06:08:35 am »
Afaik avr/pic/dspic/msp430/cortex_M0_M0+ are in-order exec models, thus predictable and clock precise.
Do they have any form of cache? Data, instruction, TLB, etc? If so, all bets are off.

Do they have and use interrupts? If so, all bets are off.

The simpler MCUs still run the same way as old 8051s 6802s..etc All the execution is deterministic because they can clock the whole chip (including RAM and peripherals) from the same clock. Even larger MCUs can work if they are not superscalar architecture and the core has access to fast enough non cached RAM.

Interrupts also are perfectly fine if you need cycle accuracy. If you need a cycle accurate area in the main thread then just disable interrupts for that area (often CPUs even have a dedicated instruction for that). Or even better use the interrupts as part of your cycle counting. You usually see interrupt latency documented in the datasheet, so you can know how many cycles there are between the interrupt firing and the first line of the interrupt handler executing, then count on from there. This is actually often the best way to do stuff since you can have a hardware timer kick off your interrupt at a precise point in time, then just cycle count from there on.

But yeah if you have a project that absolutely requires cycle accurate timing to work, then you better pick a chip that is designed to do it, not just happens be able to do it somewhat if massaged just right. There are MCUs with mini CPLDs for timing(Cypress), MCUs with programmable state machines in IO (Pi Pico), MCUs with timing focused architecture (XMOS), or just straight up programmable logic (CPLD/FPGA) where you can do any crazy timing thing you want (even sub cycle timing with tricks).
 

Online Kleinstein

  • Super Contributor
  • ***
  • Posts: 17310
  • Country: de
Re: Completely predictable instruction timing
« Reply #45 on: May 07, 2025, 06:45:48 am »
Interrupt latency is usually only an upper bound. Depending on the command just executed when the interrupt happens, the exact timing can vary. Things like a return from a subroutine often takes more than 1 cycle and the current instruction is usually completed before jumping to the interrupt. With more complex CPUs memory wait states or caches can also cause variability. So there is a few cylces of variability in the delay when an interrupt is started.

For cycle accurate interrupts one usually needs to add some sync code, that waits depending on a hardware timer that triggered together with the ISR. Alternatively the output action would use a hardware timer that started with the same signal as the ISR. It is usually not the code that needs to be cycle accurate, but the resulting output or it is about the difference in time measurements.

With a fast µC and high clock speed a few clock cylces are not much - maybe in the 10 ns range. However clock cycle accurate means that the timing (especially the output) is fixed to the 10 ps or so range. So quite a step down in jitter.
 

Offline tooki

  • Super Contributor
  • ***
  • Posts: 16141
  • Country: ch
Re: Completely predictable instruction timing
« Reply #46 on: May 07, 2025, 07:00:15 am »
Proving my point: you now have to add that extra qualifier "hard" to convey that message.

RTOS in modern-day usage usually just means "OS", just like "LDO" means "linear regulator" for many. That is why I found tggzzz's "RTOS implemented in silicon" such funny remark, as it is totally detached from the reality people expect from RTOS'es; basically tggzzz just wanted to say "real time implemented in silicon", and not "operating system implemented in silicon". And yet most people mean just "operating system".

The #1 reason to use an RTOS is the same as using any general purpose OS: to be able to slap together large pieces of existing code, including black box libraries, which depend on concepts like blocking function calls, time-slicing threading and so on. And in most cases people could not care less about any real-time guarantees, or understand how to prove anything about it.
Fair enough!

The impression I get is that nowadays, applications that need truly cycle-by-cycle deterministic behavior just use an FPGA.
 
The following users thanked this post: Siwastaja

Offline Andy Chee

  • Super Contributor
  • ***
  • Posts: 2177
  • Country: au
Re: Completely predictable instruction timing
« Reply #47 on: May 07, 2025, 07:53:40 am »
Are there any processors in which instructions always take the same time to run? I don't mean same number of cycles for each instruction, but rather, a completely predictable amount of time required for any particular instruction to complete (fetch + decode+ execute) irrespective of factors like pipeline stall etc.
I’m having trouble understanding the distinction between no. of cycles, and your definition of instruction completion.  To me, they are one in the same.

On another note, many moons ago I dabbled with Ubicom/Scenix/Parallax’s SX28 microcontroller, which relied on a software peripheral approach.

Getting software UART, I2C, and PWM modules to work happily with each other, would almost be impossible without cycle determinism.
 

Offline tggzzz

  • Super Contributor
  • ***
  • Posts: 23122
  • Country: gb
  • Numbers, not adjectives
    • Having fun doing more, with less
Re: Completely predictable instruction timing
« Reply #48 on: May 07, 2025, 08:11:57 am »
RTOS in modern-day usage usually just means "OS", just like "LDO" means "linear regulator" for many. That is why I found tggzzz's "RTOS implemented in silicon" such funny remark, as it is totally detached from the reality people expect from RTOS'es; basically tggzzz just wanted to say "real time implemented in silicon", and not "operating system implemented in silicon". And yet most people mean just "operating system".

The #1 reason to use an RTOS is the same as using any general purpose OS: to be able to slap together large pieces of existing code, including black box libraries, which depend on concepts like blocking function calls, time-slicing threading and so on. And in most cases people could not care less about any real-time guarantees, or understand how to prove anything about it.

I make a strong distinction between the OS (of whatever sort) and the libraries used with it. Frequently the availability of high quality libraries is more important than the specific ordinary or soft realtime OS - and can be used to select the OS. Ditto language and libraries.

I also make a strong distinction between "realtime" and "hard realtime". Perhaps that is a consequence of my having implemented both hard and soft realtime applications. Not realising the distinction is a failure of education and/or imagination.
There are lies, damned lies, statistics - and ADC/DAC specs.
Glider pilot's aphorism: "there is no substitute for span". Retort: "There is a substitute: skill+imagination. But you can buy span".
Having fun doing more, with less
 
The following users thanked this post: tooki

Offline tggzzz

  • Super Contributor
  • ***
  • Posts: 23122
  • Country: gb
  • Numbers, not adjectives
    • Having fun doing more, with less
Re: Completely predictable instruction timing
« Reply #49 on: May 07, 2025, 08:19:08 am »
Afaik avr/pic/dspic/msp430/cortex_M0_M0+ are in-order exec models, thus predictable and clock precise.
Do they have any form of cache? Data, instruction, TLB, etc? If so, all bets are off.

Do they have and use interrupts? If so, all bets are off.

The simpler MCUs still run the same way as old 8051s 6802s..etc All the execution is deterministic because they can clock the whole chip (including RAM and peripherals) from the same clock. Even larger MCUs can work if they are not superscalar architecture and the core has access to fast enough non cached RAM.

Agreed, but having the same clock is not strictly necessary.

Quote
Interrupts also are perfectly fine if you need cycle accuracy. If you need a cycle accurate area in the main thread then just disable interrupts for that area (often CPUs even have a dedicated instruction for that). Or even better use the interrupts as part of your cycle counting. You usually see interrupt latency documented in the datasheet, so you can know how many cycles there are between the interrupt firing and the first line of the interrupt handler executing, then count on from there. This is actually often the best way to do stuff since you can have a hardware timer kick off your interrupt at a precise point in time, then just cycle count from there on.

That can work in special cases. It obviously depends on the length and proportion of the section where interrupts are disabled, and the required interrupt latency.

Quote
But yeah if you have a project that absolutely requires cycle accurate timing to work, then you better pick a chip that is designed to do it, not just happens be able to do it somewhat if massaged just right. There are MCUs with mini CPLDs for timing(Cypress), MCUs with programmable state machines in IO (Pi Pico), MCUs with timing focused architecture (XMOS), or just straight up programmable logic (CPLD/FPGA) where you can do any crazy timing thing you want (even sub cycle timing with tricks).

Yup.

There are many ways to skin a cat. It is the engineer's responsibility to know the strengths/weaknesses of the individual approaches, and to choose the appropriate one for the task in hand. Too many people have only been taught about hammers, and don't realise screws exist for a reason :(
There are lies, damned lies, statistics - and ADC/DAC specs.
Glider pilot's aphorism: "there is no substitute for span". Retort: "There is a substitute: skill+imagination. But you can buy span".
Having fun doing more, with less
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf

 

- - -
-->