CHIP-8 delay timer in C++ I'm writing a CHIP-8 emulator in C++, which has two timers. If these timers are non-zero, they are decremented by 1 at 60Hz. I'm still getting the hang of C++, so I wanted to see if I'm doing this the Right Way.

Here's the header file:

#include &lt;chrono&gt;

struct TimedRegister
{
  unsigned char value;
  std::chrono::time_point&lt;std::chrono::high_resolution_clock&gt; last_write;

  TimedRegister();
  void set(const unsigned char new_value);
  void decrement();
};


And this is the the implementation:

#include "chip8.hpp"

#include &lt;chrono&gt;

TimedRegister::TimedRegister()
  : value(0)
{}

void
TimedRegister::set(const unsigned char new_value)
{
  value = new_value;
  last_write = std::chrono::high_resolution_clock::now();
}

void
TimedRegister::decrement()
{
  if (value == 0)
    return;

  const auto now = std::chrono::high_resolution_clock::now();
  if (std::chrono::duration_cast&lt;std::chrono::milliseconds&gt;(now - last_write)
        .count() &gt;= 1000 / 60) {
    --value;
    last_write = now;
  }
}


The decrement function is called from the main emulator loop like this:

while (!chip8.halted) {
  chip8.delay_timer.decrement();
}


They seem to work fine, but I'm not sure if I'm getting away with using a heavy-handed, brute-force approach.
When doing emulation you should disconnect from the real world and only provide a abstract view to a world.

In other words don't use a real time clock instead simulate the timer by linking it to the emulated clockspeed. For example if you pick 8 MHz you would have 133 333 instructions executed per decrement of each timer.

This will allow you to pause and fast forward when debugging programs.