An emulator, assembler, and disassembler for the Sega Game Gear
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

44 lines
1.1 KiB

  1. /* Copyright (C) 2014-2015 Ben Kurtovic <ben.kurtovic@gmail.com>
  2. Released under the terms of the MIT License. See LICENSE for details. */
  3. #include "util.h"
  4. #if defined __APPLE__
  5. #include <mach/mach_time.h>
  6. #elif defined __linux__
  7. #include <time.h>
  8. #ifndef CLOCK_MONOTONIC
  9. #error "Unsupported operating system or compiler."
  10. #endif
  11. #else
  12. #error "Unsupported operating system or compiler."
  13. #endif
  14. #define NS_PER_SEC 1000000000
  15. /*
  16. Convert a BCD-encoded hexadecimal number to decimal.
  17. */
  18. uint8_t bcd_decode(uint8_t num)
  19. {
  20. return ((num >> 4) * 10) + (num & 0x0F);
  21. }
  22. /*
  23. Return monotonic time, in nanoseconds.
  24. */
  25. uint64_t get_time_ns()
  26. {
  27. #if defined __APPLE__
  28. static mach_timebase_info_data_t tb_info;
  29. if (tb_info.denom == 0)
  30. mach_timebase_info(&tb_info);
  31. // Apple's docs pretty much say "screw overflow" here...
  32. return mach_absolute_time() * tb_info.numer / tb_info.denom;
  33. #elif defined __linux__
  34. struct timespec spec;
  35. clock_gettime(CLOCK_MONOTONIC, &spec);
  36. return spec.tv_sec * NS_PER_SEC + spec.tv_nsec;
  37. #endif
  38. }