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

0 Members and 1 Guest are viewing this topic.

Offline djnzTopic starter

  • Regular Contributor
  • *
  • Posts: 183
  • Country: 00
Completely predictable instruction timing
« on: May 04, 2025, 03:15:22 pm »
This is a bit of a thought experiment / "in theory can we do this" type question. Let's assume that there is zero jitter in our system clock.
 
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.
 

Offline peter-h

  • Super Contributor
  • ***
  • Posts: 6114
  • Country: gb
  • Doing electronics since the 1960s...
Re: Completely predictable instruction timing
« Reply #1 on: May 04, 2025, 03:44:47 pm »
Z80 and many others of that era :)
Z80 Z180 Z280 Z8 S8 8031 8051 H8/300 H8/500 80x86 90S1200 32F417
 

Offline jnk0le

  • Regular Contributor
  • *
  • Posts: 229
  • Country: pl
Re: Completely predictable instruction timing
« Reply #2 on: May 04, 2025, 03:46:48 pm »
All AVR8 mcu's were designed exactly like that (and most other 8/16 bitters), but you can do this even on lower end cortex-m/risc-v with 0ws flash, 0ws SRAMs and no DMA bursts stalling your busses

EDIT: not to mention the XMOS barrel processors, designed for implementing software peripherals. Where even the IDE can calculate the timmings of your code.
« Last Edit: May 04, 2025, 03:58:19 pm by jnk0le »
 
The following users thanked this post: neillnz

Online SiliconWizard

  • Super Contributor
  • ***
  • Posts: 17836
  • Country: fr
Re: Completely predictable instruction timing
« Reply #3 on: May 04, 2025, 04:21:39 pm »
Almost anything not using a pipeline nor cache, so that restricts it to old devices these days or niche ultra-simple ones.

But it's still a matter of number of clock cycles.
Time doesn't magically elapse in any other kind of metric than clock cycles in a CPU, at least in a conventional digital CPU.
 

Offline amwales

  • Regular Contributor
  • *
  • Posts: 141
  • Country: gb
Re: Completely predictable instruction timing
« Reply #4 on: May 04, 2025, 04:34:37 pm »
You can do reliable cycle counting with the 16bit dsPIC, very few gotchas, very well documented.
 

Online tooki

  • Super Contributor
  • ***
  • Posts: 16127
  • Country: ch
Re: Completely predictable instruction timing
« Reply #5 on: May 04, 2025, 08:03:13 pm »
This is a bit of a thought experiment / "in theory can we do this" type question. Let's assume that there is zero jitter in our system clock.
 
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 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.

Realistically, I think this is the domain of true real-time OSes. Hence the name. They’re commonly used for systems where absolutely deterministic behavior is required, like safety-critical telematics and avionics.
 

Online tggzzz

  • Super Contributor
  • ***
  • Posts: 23122
  • Country: gb
  • Numbers, not adjectives
    • Having fun doing more, with less
Re: Completely predictable instruction timing
« Reply #6 on: May 04, 2025, 08:20:34 pm »
This is a bit of a thought experiment / "in theory can we do this" type question. Let's assume that there is zero jitter in our system clock.
 
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.

XMOS xCORE processors up to 32 cores 4000MIPs/chip do that. Buy them at DigiKey.

The toolsystem does more. The IDE will tell you the exact timing of all the flow control paths between here and there. That enables calculation of hard realtime behaviour; none of this measure and hope you ran across the worst case crap!e

That gives FPGA-like determinism.

BTW, you don't need an RTOS either; the fundamentals interprocess communication and scheduling are implemented in hardware.
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: SteveThackery

Online tggzzz

  • Super Contributor
  • ***
  • Posts: 23122
  • Country: gb
  • Numbers, not adjectives
    • Having fun doing more, with less
Re: Completely predictable instruction timing
« Reply #7 on: May 04, 2025, 08:26:09 pm »
Almost anything not using a pipeline nor cache, so that restricts it to old devices these days or niche ultra-simple ones.

But it's still a matter of number of clock cycles.
Time doesn't magically elapse in any other kind of metric than clock cycles in a CPU, at least in a conventional digital CPU.

XMOS xCORE are modern and not simple: 32 cores per 4000MIPS chip. Buy them at DigiKey. https://www.digikey.co.uk/en/supplier-centers/xmos

No pipeline.
No cache.
No interrupts.
RTOS implemented in Silicon.

What more could you want for hard realtime embedded?
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 jpanhalt

  • Super Contributor
  • ***
  • Posts: 4842
  • Country: us
Re: Completely predictable instruction timing
« Reply #8 on: May 04, 2025, 08:33:29 pm »
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.

For whether any MCU's always takes the same amount of time for any instruction, I don't know. But the time for various instructions is always defined and is predictable.  If you want to write isochronous code, regardless of branches, you may want to resort to Assembly.  In that language, at least with Microchip devices, it is quite easy to do.
 

Offline radiolistener

  • Super Contributor
  • ***
  • Posts: 5745
  • Country: Earth
Re: Completely predictable instruction timing
« Reply #9 on: May 04, 2025, 09:12:03 pm »
This is a bit of a thought experiment / "in theory can we do this" type question. Let's assume that there is zero jitter in our system clock.

Zero jitter clock is impossible, it don't exist. All real-world clock sources exhibit some amount of timing variation, commonly referred to as jitter, which is typically characterized using RMS (Root Mean Square) or peak-to-peak values in the picosecond or femtosecond range.

However, the jitter introduced by general-purpose processor or FPGA GPIOs is often significantly higher. This is due to factors such as internal bus contention, variable instruction timing, and lack of dedicated clock routing. As a result, such sources often exhibit worse jitter performance than even low-cost oscillators based on digital synthesizers and PLL's.
« Last Edit: May 04, 2025, 09:34:23 pm by radiolistener »
 

Online tggzzz

  • Super Contributor
  • ***
  • Posts: 23122
  • Country: gb
  • Numbers, not adjectives
    • Having fun doing more, with less
Re: Completely predictable instruction timing
« Reply #10 on: May 04, 2025, 09:32:59 pm »
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.

For whether any MCU's always takes the same amount of time for any instruction, I don't know. But the time for various instructions is always defined and is predictable.  If you want to write isochronous code, regardless of branches, you may want to resort to Assembly.  In that language, at least with Microchip devices, it is quite easy to do.

How long does it take to load a register from memory?
Will it take the same time the next time that instruction is executed?
Your answer should include the effects of caches (data, TLB, etc) and interrupts.

Hint: it is not possible with most modern processors.
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 jpanhalt

  • Super Contributor
  • ***
  • Posts: 4842
  • Country: us
Re: Completely predictable instruction timing
« Reply #11 on: May 04, 2025, 10:58:29 pm »
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.

For whether any MCU's always takes the same amount of time for any instruction, I don't know. But the time for various instructions is always defined and is predictable.  If you want to write isochronous code, regardless of branches, you may want to resort to Assembly.  In that language, at least with Microchip devices, it is quite easy to do.

How long does it take to load a register from memory?
Will it take the same time the next time that instruction is executed?
Your answer should include the effects of caches (data, TLB, etc) and interrupts.

Hint: it is not possible with most modern processors.

Define modern.   

Refer to pages 461-462 of this: (http://ww1.microchip.com/downloads/en/DeviceDoc/PIC16-L-F15324-44-data-sheet-40001889B.pdf).  Those timings are accurate.  None are ambiguous.  They define what you need to consider if there are branches.  Interrupt latency is also consistent, but if you want isochronous code, you need to consider that.
 

Online tggzzz

  • Super Contributor
  • ***
  • Posts: 23122
  • Country: gb
  • Numbers, not adjectives
    • Having fun doing more, with less
Re: Completely predictable instruction timing
« Reply #12 on: May 04, 2025, 11:15:18 pm »
 >:(
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.

For whether any MCU's always takes the same amount of time for any instruction, I don't know. But the time for various instructions is always defined and is predictable.  If you want to write isochronous code, regardless of branches, you may want to resort to Assembly.  In that language, at least with Microchip devices, it is quite easy to do.

How long does it take to load a register from memory?
Will it take the same time the next time that instruction is executed?
Your answer should include the effects of caches (data, TLB, etc) and interrupts.

Hint: it is not possible with most modern processors.

Define modern.   

Refer to pages 461-462 of this: (http://ww1.microchip.com/downloads/en/DeviceDoc/PIC16-L-F15324-44-data-sheet-40001889B.pdf).  Those timings are accurate.  None are ambiguous.  They define what you need to consider if there are branches.  Interrupt latency is also consistent, but if you want isochronous code, you need to consider that.

Modern: how about up to 4000MIPS, 32 core, scalable, 32 bit.

I'm not going to bother to look at the details of yet another me-too processor.

In hard realtime systems you are concerned with how long it take to get from this instruction to that instruction, in the worst case. That includes everything, including the effects of interrupts, caches, branches.

The XMOS xCORE hardware/software architecture allows the IDE to calculate that without running the code. Yes, that is - uniquely - possible. (Key point: no caches, with 32 cores interrupts aren't necessary)

Does the microchip IDE calculate that? Down to the processor cycle? No.
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 David Hess

  • Super Contributor
  • ***
  • Posts: 19271
  • Country: us
  • DavidH
Re: Completely predictable instruction timing
« Reply #13 on: May 05, 2025, 04:11:39 am »
8/16 bit PIC and 8 bit ARV have fixed instruction timing.
 

Online PCB.Wiz

  • Super Contributor
  • ***
  • Posts: 3417
  • Country: au
Re: Completely predictable instruction timing
« Reply #14 on: May 05, 2025, 04:19:24 am »
This is a bit of a thought experiment / "in theory can we do this" type question. Let's assume that there is zero jitter in our system clock.
 
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.
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.

One simple technique for 8-bit MCU's to get a jitter free interrupt delay, is to always sit in IDLE before the interrupt arrives.
That removes the finish-current-opcode effect.

8 bit MCUs tend to have less-variable memory pathways than more complex MCUs, but even lowly 8-bit MCUs can sometimes come with alignment changes that can affect the time taken to get a complete opcode. ie if you recompile the code and the alignment moves in memory, the measured time can change.

In theory, you could design MCU that de-jittered interrupts by always taking the longest opcode time to respond, but I've not seen a MCU that does that.

 


 

Offline jpanhalt

  • Super Contributor
  • ***
  • Posts: 4842
  • Country: us
Re: Completely predictable instruction timing
« Reply #15 on: May 05, 2025, 04:29:03 am »
Quote from: djnz
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 not going to bother to look at the details of yet another me-too processor.
How about sticking to the question that was asked?  For that, the answer is yes.  You may need to look outside your limited realm of experience.

@djnz,
Going past the thought experiment, in my experience when such timing is critical, a clocked interrupt can be used to even timings out. 
 

Online Kleinstein

  • Super Contributor
  • ***
  • Posts: 17296
  • Country: de
Re: Completely predictable instruction timing
« Reply #16 on: May 05, 2025, 06:40:24 am »
Simpler CPUs and µCs have a predictable, fixed timing. I have used this for the AVR based multislope ADC that really depends on the timing. It is still extra effort to write the code in ASM and get the same run-time over different cases. This kind of excludes interrupts.

Modern, fast CPUs tend to use chaches and similar to accelerate memory access and this kind of breaks the predictablity. Also DMA can add delay at unpredictable times, especially if initiated from IO parts. Pipelines make things more complicated as the speed depends on the instructions before - it can still be reasonable predictable and repeatable.

One a few ps scale there can be additional jitter from the CPU internal state effecting the exact delays (relative to the clock) for outputs.
As an example, for the atmel mega48 µC I saw a small (roughly 10s of ps range) effect of ADC clock on the IO output timing.
Similar FPGAs have minor coupling that can cause additional jitter or delay modulation from internal signals.
 

Online tggzzz

  • Super Contributor
  • ***
  • Posts: 23122
  • Country: gb
  • Numbers, not adjectives
    • Having fun doing more, with less
Re: Completely predictable instruction timing
« Reply #17 on: May 05, 2025, 06:58:22 am »
Quote from: djnz
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 not going to bother to look at the details of yet another me-too processor.
How about sticking to the question that was asked?  For that, the answer is yes.  You may need to look outside your limited realm of experience.

@djnz,
Going past the thought experiment, in my experience when such timing is critical, a clocked interrupt can be used to even timings out.

How about finding out the more important question, and answering that?
https://en.wikipedia.org/wiki/XY_problem
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

Online gerbay

  • Regular Contributor
  • *
  • Posts: 91
  • Country: tr
Re: Completely predictable instruction timing
« Reply #18 on: May 05, 2025, 07:04:22 am »
Real-time is completely dependent on your timing and jitter requirements. For many applications, modern CPU/MCU performance is sufficient for real-time requirements. Jitter is always present and it depends on how precise you measure. Nowadays, calculations are no longer done by counting instructions. Timing and jitter measurements are made to meet timing requirements. In applications where CPU/MCUs are not sufficient (relatively few), you may need to use FPGAs or special ASIC chips.
 

Online tggzzz

  • Super Contributor
  • ***
  • Posts: 23122
  • Country: gb
  • Numbers, not adjectives
    • Having fun doing more, with less
Re: Completely predictable instruction timing
« Reply #19 on: May 05, 2025, 07:15:12 am »
Real-time is completely dependent on your timing and jitter requirements. For many applications, modern CPU/MCU performance is sufficient for real-time requirements. Jitter is always present and it depends on how precise you measure. Nowadays, calculations are no longer done by counting instructions. Timing and jitter measurements are made to meet timing requirements. In applications where CPU/MCUs are not sufficient (relatively few), you may need to use FPGAs or special ASIC chips.

Not true for the XMOS xCORE processors. The IDE does the instruction timing calculation accurately, without running code and hoping you spotted the worst case.

Those MCUs do indeed have some of the advantages of FPGAs, without the pain.
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 djnzTopic starter

  • Regular Contributor
  • *
  • Posts: 183
  • Country: 00
Re: Completely predictable instruction timing
« Reply #20 on: May 05, 2025, 07:21:37 am »
@tggzzz: I had read about XMOS previously on this forum - makes sense that a company focusing on audio applications will want something like this. Thanks

@radiolistener: I understand that zero jitter is not possible (Heisenberg says "hi") hence the initial prologue of "this is a thought experiment".

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?


« Last Edit: May 05, 2025, 07:25:01 am by djnz »
 

Offline westfw

  • Super Contributor
  • ***
  • Posts: 4668
  • Country: us
Re: Completely predictable instruction timing
« Reply #21 on: May 05, 2025, 08:20:03 am »
Quote
I think this is the domain of true real-time OSes. Hence the name. They’re commonly used for systems where absolutely deterministic behavior is required, like safety-critical telematics and avionics.

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.)"  I particularly remember a competitor in the IP routing business touting their fancy RTOS-based router that could always route a packet in "less than a millisecond."  Which I though was pretty awful, since we were switching packets MUCH faster than that.  Except... only "most of the time", and we couldn't make any similar guarantee.  (And in fact someone made a PhD thesis out of analyzing the variability of packet switching times...)

I think that many DSPs can provide deterministic timing.

It's possible that many "modern" processors are also deterministic, or can be configured to be deterministic.  But it seems that most of the time, something is not well documented enough to do this in reality.  (In particular, the exact behavior of "flash accelerators" and caching is frequently not well described.)
 

Online Siwastaja

  • Super Contributor
  • ***
  • Posts: 11416
  • Country: fi
Re: Completely predictable instruction timing
« Reply #22 on: May 05, 2025, 08:47:15 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 kinda liked how tggzzz used this term to coin this gem of marketing:
"RTOS implemented in Silicon."

 :-DD
 

Offline guenthert

  • Frequent Contributor
  • **
  • Posts: 859
  • Country: de
Re: Completely predictable instruction timing
« Reply #23 on: May 05, 2025, 09:02:48 am »
   The king-of-the-hill XMOS products have been mentioned, but for hobbyist the TI Sitara chips might be of interest as they are used on the Beagle Boards and offer two 32bit programmable realtime units (PRU) running next to the ARM chip.  Cheaper is the old, odd, breadboard-friendly Parallax Propeller (P8X32A), cheaper yet the recent Raspberry Pico (RP2040, RP2350) with its, admittedly limited, PIO "state machines". 

   All those allow precise timing with cycle granularity.  Information regarding jitter less than a cycle is much harder to come by.
 

Online iMo

  • Super Contributor
  • ***
  • Posts: 6970
  • Country: li
Re: Completely predictable instruction timing
« Reply #24 on: May 05, 2025, 09:15:44 am »
Are there any processors in which instructions always take the same time to run?.

All PIC ucontrollers except the pic32 line..

Leapsecond site says less than 2ps jitter..

http://www.leapsecond.com/pic/picdiv.htm
« Last Edit: May 05, 2025, 09:20:02 am by iMo »
Readers discretion is advised..
 
The following users thanked this post: guenthert


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf

 

- - -
-->