* * *

Author Topic: High precision timing  (Read 2148 times)

Rave

  • Full Member
  • ***
  • Posts: 160
High precision timing
« on: March 10, 2018, 01:42:19 pm »
Don't propose EpikTimer, I've already checked it and it's really obtuse to use for what I want it to use.

I need something like TTimer, but one that can execute stuff faster than 1000 times per second, at least 1500000 times per second, with a convenient OnTimer interface. Why? I'm building an emulator so the precise timing is kinda important.

taazz

  • Hero Member
  • *****
  • Posts: 5336
Re: High precision timing
« Reply #1 on: March 10, 2018, 02:45:42 pm »
That's impossible as far as I know. Even the hardware PIT on intel CPUs is around 1.2Mhz which is lower than your current requirements, I doubt you will find anything less than 10ms (aka 100hz) interval. You probably should change your requirements from executing 1.5 millions time a second, to swap the execution 100 times a second.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Rave

  • Full Member
  • ***
  • Posts: 160
Re: High precision timing
« Reply #2 on: March 10, 2018, 03:17:38 pm »
How will I emulate properly a 1.5MHz system properly then?

rvk

  • Hero Member
  • *****
  • Posts: 3457
Re: High precision timing
« Reply #3 on: March 10, 2018, 03:20:37 pm »
On Windows you can go somewhat more accurate (to 1ms) than TTimer with timeSetEvent() (it's defined in mmsystem).

You also might whip up your own timer in a thread using QueryPerformanceFrequency and QueryPerformanceCounter. Although I'm not sure how accurate you can go (could depend on the cpu) and how cpu-intensive that will be.

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 6498
Re: High precision timing
« Reply #4 on: March 10, 2018, 03:30:15 pm »
How will I emulate properly a 1.5MHz system properly then?

Time sequences large than 1 instruction, and pause after them to make the timing work.

This will require to identify writes to memory mapped memory hardware (like keyboard/mouse/joystick handling and audio/video) and emulate them, but usually these are known for old systems

taazz

  • Hero Member
  • *****
  • Posts: 5336
Re: High precision timing
« Reply #5 on: March 10, 2018, 03:36:58 pm »
How will I emulate properly a 1.5MHz system properly then?
usually with spinwait. You measure the cpu hz and decide on how often you add a delay and how long that delay needs to be. You simple loop through the calculated amount of times to emulate a pause.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Rave

  • Full Member
  • ***
  • Posts: 160
Re: High precision timing
« Reply #6 on: March 10, 2018, 08:04:17 pm »
How will I emulate properly a 1.5MHz system properly then?
usually with spinwait. You measure the cpu hz and decide on how often you add a delay and how long that delay needs to be. You simple loop through the calculated amount of times to emulate a pause.

Spinwait? Tell me more, please.

How will I emulate properly a 1.5MHz system properly then?

Time sequences large than 1 instruction, and pause after them to make the timing work.

This will require to identify writes to memory mapped memory hardware (like keyboard/mouse/joystick handling and audio/video) and emulate them, but usually these are known for old systems
Well, the latter will be easy since I'm designing all the stuff (the emulator is a part of "fantasy console" with custom cpu, think Pico-8 if it'd use e.g. Notch's DCPU-16), but yeah.
On Windows you can go somewhat more accurate (to 1ms) than TTimer with timeSetEvent() (it's defined in mmsystem).

You also might whip up your own timer in a thread using QueryPerformanceFrequency and QueryPerformanceCounter. Although I'm not sure how accurate you can go (could depend on the cpu) and how cpu-intensive that will be.

Thanks, but no. I want to keep it single-threaded if possible and I want something that'd work on every platform.
« Last Edit: March 10, 2018, 08:06:00 pm by Rave »

Ñuño_Martínez

  • Hero Member
  • *****
  • Posts: 800
    • Burdjia
Re: High precision timing
« Reply #7 on: March 13, 2018, 02:02:34 pm »
Hi: I answered your other thread.

Prease DON'T duplicate threads!!!  >:(

1500000 ticks per second is too much and I doubt you'll find a timer with such precision today.  For example, from the Allegro documentation, you can select microseconds (1000000tps) but most drivers allows milliseconds only (1000tps).  Anyway you can't expect to emulate with such precision in modern systems due to multithread and such.

I recommend you to use another event to time your emulation instead of the clock.  For example, most game and home computer emulators use screen retrace (i.e. frames per second) because in most cases you know how many clock cycles executes in each one and it is a rather low frequency (50fps in Europe, 60fps in USA and Japan, etc).
« Last Edit: March 13, 2018, 02:05:28 pm by Ñuño_Martínez »
Are you interested in game programming? Join the Pascal Game Development community!
Also visit the Game Development Portal

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus