From 4a62a79b883fd22dad3f06affb449397b736defa Mon Sep 17 00:00:00 2001 From: virskl Date: Tue, 17 Nov 2026 17:03:38 +0100 Subject: [PATCH] SimulatorAvr runs on past 71.6 minutes The end of each stretch of simulated time was taken from simavr's own conversion, which takes the microseconds as a uint32_t: past 4294967 ms it wrapped, the end fell below the cycles already run, and Run() ran none. A run of the page or the control program lasts until Ctrl+C, so the AVR stood still after 71.6 minutes. The cycles are computed in 64 bits now, by a function of SimulatorAvr's own, which SimulatorAvrTest checks past the wrap; it fails with the old arithmetic. GetMillis(), which nothing called, goes. --- AGENTS.md | 2 ++ CMakeLists.txt | 4 ++++ simulator/Avr/SimulatorAvr.cpp | 15 ++------------- simulator/Avr/SimulatorAvr.h | 17 +++++++++++++---- test/SimulatorAvrTest.cpp | 27 +++++++++++++++++++++++++++ 5 files changed, 48 insertions(+), 17 deletions(-) create mode 100644 test/SimulatorAvrTest.cpp diff --git a/AGENTS.md b/AGENTS.md index b35b1ef..7ceef9c 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -639,6 +639,8 @@ all of the simulator but `main.cpp` is one library, built once, because it reads never the library. `SimulatorSerialHostTest` checks what a client of `--tcp` may do that the sketch never sees, connect a second time and come back, on port 18087. +`SimulatorAvrTest` checks the simulated time `SimulatorAvr` runs the AVR to, past the 71.6 +minutes where simavr's own conversion wrapped; it is built wherever simavr is, as `SimulatorAvr`. The control program's classes without a window are tested once under the `control/` prefix: `ControlCommandTest` the lines, `ControlLinkTest` the waiting for `ready` and the cutting into diff --git a/CMakeLists.txt b/CMakeLists.txt index 51e6f17..81c8fe4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -215,6 +215,10 @@ endif() if(Simavr_FOUND) add_executable(SimulatorAvr simulator/Avr/mainAvr.cpp simulator/Avr/SimulatorAvr.cpp) target_link_libraries(SimulatorAvr PRIVATE Simulator PkgConfig::Simavr) + add_executable(SimulatorAvrTest test/SimulatorAvrTest.cpp) + set_target_properties(SimulatorAvrTest PROPERTIES CXX_STANDARD 17) + target_link_libraries(SimulatorAvrTest PRIVATE Simulator PkgConfig::Simavr GTest::gtest_main) + gtest_discover_tests(SimulatorAvrTest TEST_PREFIX "simulator/") elseif(MAXMATRIX_AVR_TESTS) message(FATAL_ERROR "MAXMATRIX_AVR_TESTS needs simavr's library, see .devcontainer/apt-packages.txt") endif() diff --git a/simulator/Avr/SimulatorAvr.cpp b/simulator/Avr/SimulatorAvr.cpp index bd8af6c..83c5d45 100644 --- a/simulator/Avr/SimulatorAvr.cpp +++ b/simulator/Avr/SimulatorAvr.cpp @@ -21,7 +21,6 @@ #include #include #include -#include #include #include #include @@ -41,10 +40,8 @@ constexpr uint8_t kClockBit{5u}; /* Serial is the first and only UART of the ATmega328P */ constexpr char kUart{'0'}; -/* the bits of a byte on the wire, and the clock's microseconds per millisecond - */ +/* the bits of a byte on the wire */ constexpr uint8_t kBitsPerByte{8}; -constexpr uint32_t kMicrosecondsPerMillisecond{1000}; /*! * \brief Passes on simavr's log, errors only. @@ -76,13 +73,6 @@ SimulatorAvr::~SimulatorAvr() { } } -uint32_t SimulatorAvr::GetMillis() const { - if (avr_ == nullptr) { - return 0; - } - return (avr_cycles_to_usec(avr_, avr_->cycle) / kMicrosecondsPerMillisecond); -} - bool SimulatorAvr::Load(char const* elf_file) { avr_global_logger_set(LogSimavr); if (elf_read_firmware(elf_file, &firmware_) != 0) { @@ -138,8 +128,7 @@ void SimulatorAvr::ConnectUart() { } bool SimulatorAvr::Run(uint32_t milliseconds) { - avr_cycle_count_t const end{ - avr_usec_to_cycles(avr_, milliseconds * kMicrosecondsPerMillisecond)}; + avr_cycle_count_t const end{ConvertToCycles(milliseconds)}; ReceiveSerial(); while (avr_->cycle < end) { diff --git a/simulator/Avr/SimulatorAvr.h b/simulator/Avr/SimulatorAvr.h index d14cb2e..bb78ccd 100644 --- a/simulator/Avr/SimulatorAvr.h +++ b/simulator/Avr/SimulatorAvr.h @@ -25,6 +25,8 @@ /* SimulatorAvr Parameter */ constexpr char kSimulatorAvrMcu[] = "atmega328p"; constexpr uint32_t kSimulatorAvrFrequency{16000000u}; +constexpr uint32_t kSimulatorAvrCyclesPerMillisecond{kSimulatorAvrFrequency / + 1000u}; /*! * \brief An ATmega328P in simavr that runs the .elf of an example, wired to the @@ -86,13 +88,20 @@ class SimulatorAvr { SimulatorAvr& operator=(SimulatorAvr const&) = delete; /*! - * \brief Returns the simulated time. + * \brief Returns the cycles the AVR runs in `milliseconds`, at its 16 MHz. * - * It counts the cycles the AVR has run, at its 16 MHz, not the host's clock. + * In 64 bits throughout: simavr's own conversion takes the microseconds as + * 32, which wrap after 71.6 minutes, and a run of the page or the control + * program lasts until Ctrl+C. * - * \return The milliseconds since the AVR started, or 0 before Load(). + * \param[in] milliseconds The simulated time. + * \return The cycles. */ - MAXMATRIX_NODISCARD uint32_t GetMillis() const; + MAXMATRIX_NODISCARD static constexpr avr_cycle_count_t ConvertToCycles( + uint32_t milliseconds) { + return static_cast(milliseconds) * + kSimulatorAvrCyclesPerMillisecond; + } /*! * \brief Makes the AVR and loads an example into it. diff --git a/test/SimulatorAvrTest.cpp b/test/SimulatorAvrTest.cpp new file mode 100644 index 0000000..4c465c5 --- /dev/null +++ b/test/SimulatorAvrTest.cpp @@ -0,0 +1,27 @@ +// Copyright 2026 Andreas Burnickl +// SPDX-License-Identifier: GPL-3.0-or-later + +/* SimulatorAvr: the simulated time it runs the AVR to. */ + +#include "Avr/SimulatorAvr.h" + +#include +#include + +namespace { + +/* Bug: the end of a stretch was taken from simavr's microseconds as a uint32_t, + * which wrapped after 4294967 ms, 71.6 minutes; a run of the page or the + * control program, which lasts until Ctrl+C, then ran no cycle any more, and + * the AVR stood still */ +TEST(SimulatorAvrTest, CyclesGoOnPastSeventyOneMinutes) { + uint32_t const past_the_wrap{4294968u}; + + EXPECT_EQ(SimulatorAvr::ConvertToCycles(1), 16000u); + EXPECT_EQ(SimulatorAvr::ConvertToCycles(past_the_wrap), + uint64_t{past_the_wrap} * 16000u); + EXPECT_GT(SimulatorAvr::ConvertToCycles(past_the_wrap), + SimulatorAvr::ConvertToCycles(past_the_wrap - 1)); +} + +} // namespace