Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 1 | // Copyright (c) FIRST and other WPILib contributors. |
| 2 | // Open Source Software; you can modify and/or share it under the terms of |
| 3 | // the WPILib BSD license file in the root directory of this project. |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 4 | |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 5 | #include <algorithm> |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 6 | #include <atomic> |
| 7 | #include <chrono> |
| 8 | #include <cstdio> |
| 9 | #include <thread> |
| 10 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 11 | #include <fmt/format.h> |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 12 | #include <wpi/timestamp.h> |
| 13 | |
| 14 | #include "MockHooksInternal.h" |
| 15 | #include "NotifierInternal.h" |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 16 | #include "hal/simulation/NotifierData.h" |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 17 | |
| 18 | static std::atomic<bool> programStarted{false}; |
| 19 | |
| 20 | static std::atomic<uint64_t> programStartTime{0}; |
| 21 | static std::atomic<uint64_t> programPauseTime{0}; |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 22 | static std::atomic<uint64_t> programStepTime{0}; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 23 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 24 | namespace hal::init { |
| 25 | void InitializeMockHooks() { |
| 26 | wpi::SetNowImpl(GetFPGATime); |
| 27 | } |
| 28 | } // namespace hal::init |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 29 | |
| 30 | namespace hal { |
| 31 | void RestartTiming() { |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 32 | programStartTime = wpi::NowDefault(); |
| 33 | programStepTime = 0; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 34 | if (programPauseTime != 0) { |
| 35 | programPauseTime = programStartTime.load(); |
| 36 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | void PauseTiming() { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 40 | if (programPauseTime == 0) { |
| 41 | programPauseTime = wpi::NowDefault(); |
| 42 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | void ResumeTiming() { |
| 46 | if (programPauseTime != 0) { |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 47 | programStartTime += wpi::NowDefault() - programPauseTime; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 48 | programPauseTime = 0; |
| 49 | } |
| 50 | } |
| 51 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 52 | bool IsTimingPaused() { |
| 53 | return programPauseTime != 0; |
| 54 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 55 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 56 | void StepTiming(uint64_t delta) { |
| 57 | programStepTime += delta; |
| 58 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 59 | |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 60 | uint64_t GetFPGATime() { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 61 | uint64_t curTime = programPauseTime; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 62 | if (curTime == 0) { |
| 63 | curTime = wpi::NowDefault(); |
| 64 | } |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 65 | return curTime + programStepTime - programStartTime; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 66 | } |
| 67 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 68 | double GetFPGATimestamp() { |
| 69 | return GetFPGATime() * 1.0e-6; |
| 70 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 71 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 72 | void SetProgramStarted() { |
| 73 | programStarted = true; |
| 74 | } |
| 75 | bool GetProgramStarted() { |
| 76 | return programStarted; |
| 77 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 78 | } // namespace hal |
| 79 | |
| 80 | using namespace hal; |
| 81 | |
| 82 | extern "C" { |
| 83 | void HALSIM_WaitForProgramStart(void) { |
| 84 | int count = 0; |
| 85 | while (!programStarted) { |
| 86 | count++; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 87 | fmt::print("Waiting for program start signal: {}\n", count); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 88 | std::this_thread::sleep_for(std::chrono::milliseconds(500)); |
| 89 | } |
| 90 | } |
| 91 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 92 | void HALSIM_SetProgramStarted(void) { |
| 93 | SetProgramStarted(); |
| 94 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 95 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 96 | HAL_Bool HALSIM_GetProgramStarted(void) { |
| 97 | return GetProgramStarted(); |
| 98 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 99 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 100 | void HALSIM_RestartTiming(void) { |
| 101 | RestartTiming(); |
| 102 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 103 | |
| 104 | void HALSIM_PauseTiming(void) { |
| 105 | PauseTiming(); |
| 106 | PauseNotifiers(); |
| 107 | } |
| 108 | |
| 109 | void HALSIM_ResumeTiming(void) { |
| 110 | ResumeTiming(); |
| 111 | ResumeNotifiers(); |
| 112 | } |
| 113 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 114 | HAL_Bool HALSIM_IsTimingPaused(void) { |
| 115 | return IsTimingPaused(); |
| 116 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 117 | |
| 118 | void HALSIM_StepTiming(uint64_t delta) { |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 119 | WaitNotifiers(); |
| 120 | |
| 121 | while (delta > 0) { |
| 122 | int32_t status = 0; |
| 123 | uint64_t curTime = HAL_GetFPGATime(&status); |
| 124 | uint64_t nextTimeout = HALSIM_GetNextNotifierTimeout(); |
| 125 | uint64_t step = std::min(delta, nextTimeout - curTime); |
| 126 | |
| 127 | StepTiming(step); |
| 128 | delta -= step; |
| 129 | |
| 130 | WakeupWaitNotifiers(); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | void HALSIM_StepTimingAsync(uint64_t delta) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 135 | StepTiming(delta); |
| 136 | WakeupNotifiers(); |
| 137 | } |
| 138 | } // extern "C" |