Lazarus

Programming => Graphics and Multimedia => Games => Topic started by: Rave on March 10, 2018, 01:42:19 pm

Title: High precision timing
Post by: Rave 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.
Title: Re: High precision timing
Post by: taazz 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.
Title: Re: High precision timing
Post by: Rave on March 10, 2018, 03:17:38 pm
How will I emulate properly a 1.5MHz system properly then?
Title: Re: High precision timing
Post by: rvk on March 10, 2018, 03:20:37 pm
On Windows you can go somewhat more accurate (to 1ms) than TTimer with timeSetEvent() (http://www.swissdelphicenter.ch/en/showcode.php?id=216) (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.
Title: Re: High precision timing
Post by: marcov 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
Title: Re: High precision timing
Post by: taazz 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.
Title: Re: High precision timing
Post by: Rave 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() (http://www.swissdelphicenter.ch/en/showcode.php?id=216) (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.
Title: Re: High precision timing
Post by: Ñuño_Martínez on March 13, 2018, 02:02:34 pm
Hi: I answered your other thread (http://forum.lazarus.freepascal.org/index.php?topic=40422.new#new).

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 (http://allegro-pas.sourceforge.net/docs/5.2/lib/Allegro5.html#al_create_timer), 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).
TinyPortal © 2005-2018