some refactoring and cleanup

This commit is contained in:
Christopher Mosher 2013-11-26 01:09:56 -05:00
parent ba6ae1751e
commit 2aab7240d2
3 changed files with 108 additions and 128 deletions

170
cpu.cpp
View File

@ -14,8 +14,21 @@
#include "nodes.h"
#include "addressbus.h"
#define TRACE 1
#define TRACEMEM 1
#define TRACEREG 1
//#define TRACESEG 1
//#define TRACEMEM 1
#ifdef TRACEREG
#define dumpRegs() dumpRegisters()
#else
#define dumpRegs()
#endif
#ifdef TRACESEG
#define dumpSegs() dumpSegments()
#else
#define dumpSegs()
#endif
CPU::CPU(AddressBus& addressBus) :
addressBus(addressBus) {
@ -171,31 +184,32 @@ static void pHexw(unsigned short x) {
std::cout << std::setw(4) << std::setfill('0') << std::hex << (unsigned long) x << std::dec;
}
void CPU::dumpSegs() {
void CPU::dumpSegments() {
for (int i = 0; i < segs.size(); ++i) {
std::cout << i << " ";
// std::cout << i << " ";
seg& s = segs[i];
if (s.pullup) {
std::cout << "+";
std::cout << "U";
} else if (s.pulldown) {
std::cout << "-";
std::cout << "D";
} else {
std::cout << "0";
std::cout << "f";
}
if (s.on) {
std::cout << "+";
} else {
std::cout << "-";
}
std::cout << std::endl;
// std::cout << std::endl;
}
std::cout << std::endl;
}
unsigned char CPU::mRead(unsigned short addr) {
unsigned char x;
x = this->addressBus.read(addr);
#ifdef TRACEMEM
std::cout << "--------------------------------------------- ";
std::cout << "-------------------------------------------------- ";
pHex(x);
std::cout << "<";
pHexw(addr);
@ -207,7 +221,7 @@ unsigned char CPU::mRead(unsigned short addr) {
void CPU::mWrite(unsigned short addr, unsigned char data) {
this->addressBus.write(addr, data);
#ifdef TRACEMEM
std::cout << "--------------------------------------------- ";
std::cout << "-------------------------------------------------- ";
pHex(data);
std::cout << ">";
pHexw(addr);
@ -215,7 +229,7 @@ void CPU::mWrite(unsigned short addr, unsigned char data) {
#endif
}
void CPU::dumpRegs() {
void CPU::dumpRegisters() {
std::cout << "A";
pHex(rA());
std::cout << " X";
@ -246,6 +260,9 @@ void CPU::dumpRegs() {
std::cout << " W";
}
}
if (! (isHigh(CLK1OUT) || isHigh(CLK2OUT))) {
std::cout << " PH- ";
}
std::cout << " DB";
pHex(rData());
std::cout << " AB";
@ -261,23 +278,13 @@ static void addRecalc(int n, std::set<int>& rcl) {
rcl.insert(n);
}
static void onTrans(trn& t, std::set<int>& rcl) {
if (t.on) {
return;
}
t.on = true;
addRecalc(t.c1, rcl);
addRecalc(t.c2, rcl); //looks like this is not necessary?
}
static void offTrans(trn& t, std::set<int>& rcl) {
if (!t.on) {
return;
}
t.on = false;
static void setTrans(trn& t, bool on, std::set<int>& rcl) {
if (t.on != on) {
t.on = on;
addRecalc(t.c1, rcl);
addRecalc(t.c2, rcl);
}
}
bool CPU::getGroupValue(const std::set<int>& s) {
if (s.find(VSS) != s.end()) {
@ -298,12 +305,6 @@ bool CPU::getGroupValue(const std::set<int>& s) {
return true;
}
}
for (std::set<int>::const_iterator i = s.begin(); i != s.end(); ++i) {
const seg& s = segs[*i];
if (s.on) {
return true;
}
}
return false;
}
@ -335,19 +336,14 @@ void CPU::recalcNode(int n, std::set<int>& rcl) {
addToGroup(n, g);
const bool gval = getGroupValue(g);
//std::cout << "gval: " << gval << " grp size: " << g.size() << std::endl;
for (std::set<int>::iterator ig = g.begin(); ig != g.end(); ++ig) {
//std::cout << "ig: " << *ig << std::endl;
seg& seg = segs[*ig];
if (seg.on != gval) {
//std::cout << "change seg on " << std::endl;
seg.on = gval;
for (std::vector<int>::iterator igate = seg.gates.begin(); igate != seg.gates.end(); ++igate) {
trn& t = trns[*igate];
if (seg.on) {
onTrans(t, rcl);
} else {
offTrans(t, rcl);
}
setTrans(trns[*igate], seg.on, rcl);
}
}
}
@ -365,7 +361,6 @@ void CPU::recalc(const std::set<int>& s) {
std::set<int> rcl;
for (std::set<int>::const_iterator ilist = list.begin(); ilist != list.end(); ++ilist) {
recalcNode(*ilist, rcl);
//std::cout << "done recalcNode" << std::endl;
}
// done.insert(rcl.begin(),rcl.end());
// std::set<int> v;
@ -402,20 +397,7 @@ void CPU::setSeg(int iseg, bool up) {
s.pulldown = !up;
}
void CPU::setHigh(int iseg) {
setSeg(iseg, true);
}
void CPU::setLow(int iseg) {
setSeg(iseg, false);
}
void CPU::putDataToChip(unsigned char data) {
/*
std::cout << "d2cpu: ";
pHex(data);
std::cout << std::endl;
*/
void CPU::putDataToChip(const unsigned char data) {
unsigned char x = data;
setSeg(DB0, x & 1);
@ -460,76 +442,60 @@ static void addDataToRecalc(std::set<int>& s) {
void CPU::rw() {
readBus();
std::set<int> s;
addDataToRecalc(s);
recalc(s);
writeBus();
}
void CPU::step() {
if (isHigh(CLK0)) {
setLow(CLK0);
recalc(CLK0);
} else {
setHigh(CLK0);
const bool h = isHigh(CLK0);
setSeg(CLK0, !h);
recalc(CLK0);
if (!h) {
rw();
}
#ifdef TRACE
dumpRegs();
#endif
dumpRegs();
dumpSegs();
}
void CPU::powerOn() {
std::cout << "initializing CPU..." << std::endl;
//dumpRegs();
//dumpSegs();
recalcAll();
//dumpSegs();
#ifdef TRACE
std::cout << "initial state" << std::endl;
dumpRegs();
#endif
setHigh(VCC);
setLow(VSS);
dumpSegs();
setHigh(CLK0);
setHigh(IRQ);
setLow(RES);
setHigh(NMI);
setHigh(RDY);
setLow(SO);
std::set<int> s;
s.insert(VCC);
s.insert(VSS);
s.insert(CLK0);
s.insert(IRQ);
s.insert(RES);
s.insert(NMI);
s.insert(RDY);
s.insert(SO);
recalc(s);
//dumpSegs();
std::cout << "recalc all" << std::endl;
std::cout << "recalcAll..." << std::endl;
recalcAll();
rw();
#ifdef TRACE
dumpRegs();
#endif
std::cout << "some power-up pre-reset cycles" << std::endl;
for (int i(0); i < 40; ++i) {
step();
}
dumpSegs();
std::cout << " RESET" << std::endl;
reset();
for (int i(0); i < 40; ++i) {
step();
}
std::cout << "setting pins..." << std::endl;
setSeg(VCC, true);
setSeg(VSS, false);
setSeg(CLK0, true);
setSeg(IRQ, true);
setSeg(RES, false);
setSeg(NMI, true);
setSeg(RDY, true);
setSeg(SO, false);
std::cout << "recalc all..." << std::endl;
recalcAll();
dumpRegs();
dumpSegs();
}
void CPU::reset() {
setHigh(RES);
setSeg(RES, true);
recalc(RES);
}

4
cpu.h
View File

@ -39,8 +39,8 @@ private:
unsigned char rY();
unsigned char rS();
unsigned short rPC();
void dumpSegs();
void dumpRegs();
void dumpSegments();
void dumpRegisters();
bool getGroupValue(const std::set<int>& s);
void addToGroup(int n, std::set<int>& s);
void recalcNode(int n, std::set<int>& rcl);

View File

@ -53,7 +53,21 @@ int main(int argc, char *argv[]) {
CPU cpu(mem);
/* Now let's just run some things to play with it. */
std::cout << "begin power-up..." << std::endl;
cpu.powerOn();
std::cout << "some power-up pre-reset cycles..." << std::endl;
for (int i(0); i < 10; ++i) {
cpu.tick();
}
std::cout << "RESET..." << std::endl;
cpu.reset();
for (int i(0); i < 40; ++i) {
cpu.tick();
}
return EXIT_SUCCESS;
}