I'm writing an emulator as a side project right now, and I would like to emulate the machine I've chosen at the proper rate as the original hardware. My system should be more powerful to the extent that the time to perform a single instruction is negligible, so if I just have a function, say tick
, which performs a single instruction, it will run way too quickly.
I was wondering if there is any way, in C, to call a function at some given interval (in MHz). For context, I'm writing this on a mac, so anything POSIX or in the OS X SDK would work (I looked through libdispatch but couldn't see anything).
Would it be best to simply have a loop running and calculating the time delta since the last loop? This seems rather inefficient (also preemption might become a factor here). What would some other ways of doing this be? Thanks.