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.

101 lines
2.4KB

  1. #include <assert.h>
  2. #include "ClockMult.h"
  3. #include <stdio.h>
  4. void ClockMult::sampleClock()
  5. {
  6. if (isFreeRun()) {
  7. sampleClockFreeRunMode();
  8. } else {
  9. sampleClockLockedMode();
  10. }
  11. }
  12. void ClockMult::sampleClockFreeRunMode()
  13. {
  14. sawPhase += freeRunFreq;
  15. if (sawPhase >= 1) {
  16. sawPhase -= 1;
  17. // TODO: do we care about clock out? probably...
  18. }
  19. }
  20. void ClockMult::sampleClockLockedMode()
  21. {
  22. // printf("sampleClock: state=%d saw=%f\n", state, sawPhase);
  23. switch (state) {
  24. case State::INIT:
  25. break;
  26. case State::TRAINING:
  27. ++trainingCounter;
  28. break;
  29. case State::RUNNING:
  30. ++trainingCounter; // we are still training, even while running
  31. sawPhase += learnedFrequency;
  32. if (sawPhase >= 1) {
  33. sawPhase -= 1.f;
  34. }
  35. if (clockOutTimer > 0) {
  36. clockOutTimer--;
  37. } else {
  38. clockOutValue = false;
  39. // printf("clock out one-shot timed out, going low\n");
  40. }
  41. break;
  42. default:
  43. assert(false);
  44. }
  45. // printf("leave sampleClock: state=%d saw=%f\n", state, sawPhase);
  46. }
  47. /**
  48. * Sends one reference tick to the multiplier
  49. */
  50. void ClockMult::refClock()
  51. {
  52. if (isFreeRun()) {
  53. return;
  54. }
  55. // printf("refClock: state=%d\n", state);
  56. switch (state) {
  57. case State::INIT://
  58. state = State::TRAINING;
  59. trainingCounter = 0;
  60. // printf("refClock moved from INIT to TRAINIG\n");
  61. break;
  62. case State::TRAINING:
  63. case State::RUNNING:
  64. // printf("got end train with ctr = %d\n", trainingCounter);
  65. learnedPeriod = trainingCounter;
  66. trainingCounter = 0;
  67. learnedFrequency = (float) freqMultFactor / learnedPeriod;
  68. state = State::RUNNING;
  69. startNewClock();
  70. // printf("refClock moved from TRAINING to RUNNING. period = %d freq=%f clockOut=%d\n", learnedPeriod, learnedFrequency, clockOutValue);
  71. break;
  72. default:
  73. assert(0);
  74. }
  75. //printf("leave refClock: state=%d\n", state);
  76. }
  77. void ClockMult::startNewClock()
  78. {
  79. sawPhase = 0;
  80. clockOutValue = true;
  81. clockOutTimer = 10; // TODO: constants
  82. }
  83. void ClockMult::setMultiplier(int x)
  84. {
  85. freqMultFactor = x;
  86. }