2017-06-04 20:38:34 +00:00
|
|
|
#include "stdafx.h"
|
|
|
|
#include "Processor.h"
|
|
|
|
|
2017-09-06 12:22:23 +00:00
|
|
|
EightBit::Processor::Processor(Bus& bus)
|
2017-11-10 22:41:50 +00:00
|
|
|
: m_bus(bus) {
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
|
|
|
|
2018-08-25 00:34:30 +00:00
|
|
|
void EightBit::Processor::powerOn() {
|
|
|
|
raise(RESET());
|
|
|
|
raise(HALT());
|
2017-12-10 21:41:48 +00:00
|
|
|
raise(INT());
|
2018-08-25 00:34:30 +00:00
|
|
|
raise(POWER());
|
|
|
|
}
|
|
|
|
|
|
|
|
void EightBit::Processor::handleRESET() {
|
2017-12-10 21:41:48 +00:00
|
|
|
raise(RESET());
|
2018-08-11 20:19:19 +00:00
|
|
|
PC() = 0;
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
|
|
|
|
2018-08-25 00:34:30 +00:00
|
|
|
void EightBit::Processor::handleINT() {
|
|
|
|
raise(INT());
|
|
|
|
}
|
|
|
|
|
2018-08-25 11:09:26 +00:00
|
|
|
void EightBit::Processor::handleIRQ() {
|
|
|
|
raise(IRQ());
|
|
|
|
}
|
|
|
|
|
2018-04-14 08:39:06 +00:00
|
|
|
int EightBit::Processor::run(const int limit) {
|
2017-08-30 22:17:34 +00:00
|
|
|
int current = 0;
|
2018-08-11 20:19:19 +00:00
|
|
|
while (LIKELY(powered()) && current < limit)
|
2018-08-25 00:34:30 +00:00
|
|
|
current += step();
|
2017-08-30 22:17:34 +00:00
|
|
|
return current;
|
|
|
|
}
|
2018-08-27 11:57:44 +00:00
|
|
|
|
|
|
|
// http://graphics.stanford.edu/~seander/bithacks.html#FixedSignExtend
|
|
|
|
int8_t EightBit::Processor::signExtend(int b, uint8_t x) {
|
|
|
|
const uint8_t m = 1 << (b - 1); // mask can be pre-computed if b is fixed
|
|
|
|
x = x & ((1 << b) - 1); // (Skip this if bits in x above position b are already zero.)
|
|
|
|
const auto result = (x ^ m) - m;
|
|
|
|
return result;
|
|
|
|
}
|