Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
15 changes: 2 additions & 13 deletions simulator/Avr/SimulatorAvr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#include <sim_elf.h>
#include <sim_io.h>
#include <sim_irq.h>
#include <sim_time.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
Expand All @@ -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.
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
17 changes: 13 additions & 4 deletions simulator/Avr/SimulatorAvr.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<avr_cycle_count_t>(milliseconds) *
kSimulatorAvrCyclesPerMillisecond;
}

/*!
* \brief Makes the AVR and loads an example into it.
Expand Down
27 changes: 27 additions & 0 deletions test/SimulatorAvrTest.cpp
Original file line number Diff line number Diff line change
@@ -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 <gtest/gtest.h>
#include <stdint.h>

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