2017-06-21 22:55:25 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <chrono>
|
2017-07-04 12:00:23 +00:00
|
|
|
#include <intrin.h>
|
|
|
|
#include <iostream>
|
2017-06-21 22:55:25 +00:00
|
|
|
|
|
|
|
namespace EightBit {
|
|
|
|
template<class ConfigurationT, class BoardT> class TestHarness {
|
|
|
|
public:
|
|
|
|
TestHarness(const ConfigurationT& configuration)
|
2017-07-06 20:32:52 +00:00
|
|
|
: m_board(configuration),
|
|
|
|
m_totalCycles(0),
|
|
|
|
m_instructions(0),
|
|
|
|
m_startHostCycles(0),
|
|
|
|
m_finishHostCycles(0) {
|
2017-06-21 22:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
~TestHarness() {
|
2017-07-05 16:40:16 +00:00
|
|
|
std::cout << std::dec << std::endl;
|
2017-06-22 09:16:02 +00:00
|
|
|
std::cout.imbue(std::locale(""));
|
|
|
|
|
2017-06-25 22:48:15 +00:00
|
|
|
std::cout << "Guest cycles = " << m_totalCycles << std::endl;
|
2017-06-22 09:16:02 +00:00
|
|
|
std::cout << "Seconds = " << getElapsedSeconds() << std::endl;
|
2017-06-21 22:55:25 +00:00
|
|
|
|
2017-07-05 18:15:58 +00:00
|
|
|
std::cout << getCyclesPerSecond() << " MHz" << std::endl;
|
2017-07-06 20:32:52 +00:00
|
|
|
std::cout << getInstructionsPerSecond() << " instructions per second" << std::endl;
|
2017-06-25 22:48:15 +00:00
|
|
|
|
|
|
|
auto elapsedHostCycles = m_finishHostCycles - m_startHostCycles;
|
|
|
|
std::cout << "Host cycles = " << elapsedHostCycles << std::endl;
|
|
|
|
|
|
|
|
auto efficiency = elapsedHostCycles / m_totalCycles;
|
|
|
|
std::cout << "Efficiency = " << efficiency << std::endl;
|
2017-06-21 22:55:25 +00:00
|
|
|
}
|
|
|
|
|
2017-06-22 09:16:02 +00:00
|
|
|
std::chrono::steady_clock::duration getElapsedTime() const {
|
|
|
|
return m_finishTime - m_startTime;
|
|
|
|
}
|
2017-06-21 22:55:25 +00:00
|
|
|
|
2017-07-05 18:15:58 +00:00
|
|
|
double getElapsedSeconds() const {
|
|
|
|
return std::chrono::duration_cast<std::chrono::duration<double>>(getElapsedTime()).count();
|
2017-06-21 22:55:25 +00:00
|
|
|
}
|
|
|
|
|
2017-07-05 18:15:58 +00:00
|
|
|
double getCyclesPerSecond() const {
|
|
|
|
return (m_totalCycles / 1000000 ) / getElapsedSeconds();
|
2017-06-21 22:55:25 +00:00
|
|
|
}
|
|
|
|
|
2017-07-06 20:32:52 +00:00
|
|
|
long long getInstructionsPerSecond() {
|
|
|
|
auto floating = m_instructions / getElapsedSeconds();
|
|
|
|
return (long long)floating;
|
|
|
|
}
|
|
|
|
|
2017-06-21 22:55:25 +00:00
|
|
|
void runLoop() {
|
2017-06-22 09:16:02 +00:00
|
|
|
m_startTime = now();
|
2017-07-06 20:32:52 +00:00
|
|
|
m_totalCycles = m_instructions = 0L;
|
2017-06-25 22:48:15 +00:00
|
|
|
m_startHostCycles = currentHostCycles();
|
2017-06-21 22:55:25 +00:00
|
|
|
|
2017-07-05 16:40:16 +00:00
|
|
|
auto& cpu = m_board.CPU();
|
2017-06-21 22:55:25 +00:00
|
|
|
while (!cpu.isHalted()) {
|
|
|
|
m_totalCycles += cpu.step();
|
2017-07-06 20:32:52 +00:00
|
|
|
++m_instructions;
|
2017-06-21 22:55:25 +00:00
|
|
|
}
|
|
|
|
|
2017-06-25 22:48:15 +00:00
|
|
|
m_finishHostCycles = currentHostCycles();
|
2017-06-22 09:16:02 +00:00
|
|
|
m_finishTime = now();
|
2017-06-21 22:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void initialise() {
|
|
|
|
m_board.initialise();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
BoardT m_board;
|
|
|
|
long long m_totalCycles;
|
2017-07-06 20:32:52 +00:00
|
|
|
long long m_instructions;
|
2017-06-22 09:16:02 +00:00
|
|
|
std::chrono::steady_clock::time_point m_startTime;
|
|
|
|
std::chrono::steady_clock::time_point m_finishTime;
|
2017-06-25 22:48:15 +00:00
|
|
|
unsigned __int64 m_startHostCycles;
|
|
|
|
unsigned __int64 m_finishHostCycles;
|
2017-06-22 09:16:02 +00:00
|
|
|
|
2017-06-25 22:48:15 +00:00
|
|
|
static std::chrono::steady_clock::time_point now() {
|
2017-06-22 09:16:02 +00:00
|
|
|
return std::chrono::steady_clock::now();
|
|
|
|
}
|
2017-06-25 22:48:15 +00:00
|
|
|
|
|
|
|
static unsigned __int64 currentHostCycles() {
|
|
|
|
return __rdtsc();
|
|
|
|
}
|
2017-06-21 22:55:25 +00:00
|
|
|
};
|
|
|
|
}
|