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