spo/ass2/implementations/cpu.cpp
2025-12-10 18:28:54 +01:00

51 lines
1 KiB
C++

#include "../headers/cpu.h"
#include <iostream>
void cpu::start() {
if (running) return;
running = true;
nitUre = std::thread(&cpu::zankaUre, this);
}
void cpu::stop() {
running = false;
if (nitUre.joinable())
nitUre.join();
}
bool cpu::isRunning() {
return running.load();
}
int cpu::getSpeed() {
return hitrostKhz;
}
void cpu::setSpeed(int speedKhz) {
if (speedKhz < 1) speedKhz = 1;
hitrostKhz = speedKhz;
}
void cpu::zankaUre() {
while (running) {
for (int i = 0; i < operacijeNaTick; i++) {
bool ok = cpu::m->execute();
if (!ok) {
running = false;
//std::cout << i << std::endl;
break;
}
}
if (!running) break;
double ticksPerSecond = (hitrostKhz * 1000.0) / operacijeNaTick;
double sleepTimeSec = 1.0 / ticksPerSecond;
std::this_thread::sleep_for(std::chrono::duration<double>(sleepTimeSec));
}
}
void cpu::step() {
cpu::m->execute();
}