refactor: add top-level Emu6502 class

This commit is contained in:
Christopher Mosher 2013-12-15 01:13:44 -05:00
parent 84715444c9
commit 55df84b305
10 changed files with 114 additions and 45 deletions

View File

@ -6,27 +6,28 @@
*/
#include "Common.h"
#include "TransNetwork.h"
#include "SegmentCache.h"
#include "trans.h"
Common Common::create(const SegmentCache& segs) {
return Common(
segs.get("-vss"), segs.get("+vcc"),
segs.get("-clk0"),
segs.get("-irq"), segs.get("-res"), segs.get("-nmi"),
segs.get("+rdy"), segs.get("+so"),
segs.get("-db0"), segs.get("-db1"), segs.get("-db2"), segs.get("-db3"), segs.get("-db4"), segs.get("-db5"), segs.get("-db6"), segs.get("-db7"),
segs.get("-ab0"), segs.get("-ab1"), segs.get("-ab2"), segs.get("-ab3"), segs.get("-ab4"), segs.get("-ab5"), segs.get("-ab6"), segs.get("-ab7"),
segs.get("-ab8"), segs.get("-ab9"), segs.get("-ab10"), segs.get("-ab11"), segs.get("-ab12"), segs.get("-ab13"), segs.get("-ab14"), segs.get("-ab15"),
segs.get("-rw"), segs.get("-sync"),
segs.get("-clk1out"), segs.get("-clk2out"),
segs.get("-a0"), segs.get("-a1"), segs.get("-a2"), segs.get("-a3"), segs.get("-a4"), segs.get("-a5"), segs.get("-a6"), segs.get("-a7"),
segs.get("-x0"), segs.get("-x1"), segs.get("-x2"), segs.get("-x3"), segs.get("-x4"), segs.get("-x5"), segs.get("-x6"), segs.get("-x7"),
segs.get("-y0"), segs.get("-y1"), segs.get("-y2"), segs.get("-y3"), segs.get("-y4"), segs.get("-y5"), segs.get("-y6"), segs.get("-y7"),
segs.get("-pcl0"), segs.get("-pcl1"), segs.get("-pcl2"), segs.get("-pcl3"), segs.get("-pcl4"), segs.get("-pcl5"), segs.get("-pcl6"), segs.get("-pcl7"),
segs.get("-pch0"), segs.get("-pch1"), segs.get("-pch2"), segs.get("-pch3"), segs.get("-pch4"), segs.get("-pch5"), segs.get("-pch6"), segs.get("-pch7"),
segs.get("+Pout0"), segs.get("+Pout1"), segs.get("+Pout2"), segs.get("+Pout3"), segs.get("+Pout4"), /*no P5 */segs.get("+Pout6"), segs.get("+Pout7"),
segs.get("-s0"), segs.get("-s1"), segs.get("-s2"), segs.get("-s3"), segs.get("-s4"), segs.get("-s5"), segs.get("-s6"), segs.get("-s7"));
Common::Common(const TransNetwork& tn) :
Common(
tn.segs.get("-vss"), tn.segs.get("+vcc"),
tn.segs.get("-clk0"),
tn.segs.get("-irq"), tn.segs.get("-res"), tn.segs.get("-nmi"),
tn.segs.get("+rdy"), tn.segs.get("+so"),
tn.segs.get("-db0"), tn.segs.get("-db1"), tn.segs.get("-db2"), tn.segs.get("-db3"), tn.segs.get("-db4"), tn.segs.get("-db5"), tn.segs.get("-db6"), tn.segs.get("-db7"),
tn.segs.get("-ab0"), tn.segs.get("-ab1"), tn.segs.get("-ab2"), tn.segs.get("-ab3"), tn.segs.get("-ab4"), tn.segs.get("-ab5"), tn.segs.get("-ab6"), tn.segs.get("-ab7"),
tn.segs.get("-ab8"), tn.segs.get("-ab9"), tn.segs.get("-ab10"), tn.segs.get("-ab11"), tn.segs.get("-ab12"), tn.segs.get("-ab13"), tn.segs.get("-ab14"), tn.segs.get("-ab15"),
tn.segs.get("-rw"), tn.segs.get("-sync"),
tn.segs.get("-clk1out"), tn.segs.get("-clk2out"),
tn.segs.get("-a0"), tn.segs.get("-a1"), tn.segs.get("-a2"), tn.segs.get("-a3"), tn.segs.get("-a4"), tn.segs.get("-a5"), tn.segs.get("-a6"), tn.segs.get("-a7"),
tn.segs.get("-x0"), tn.segs.get("-x1"), tn.segs.get("-x2"), tn.segs.get("-x3"), tn.segs.get("-x4"), tn.segs.get("-x5"), tn.segs.get("-x6"), tn.segs.get("-x7"),
tn.segs.get("-y0"), tn.segs.get("-y1"), tn.segs.get("-y2"), tn.segs.get("-y3"), tn.segs.get("-y4"), tn.segs.get("-y5"), tn.segs.get("-y6"), tn.segs.get("-y7"),
tn.segs.get("-pcl0"), tn.segs.get("-pcl1"), tn.segs.get("-pcl2"), tn.segs.get("-pcl3"), tn.segs.get("-pcl4"), tn.segs.get("-pcl5"), tn.segs.get("-pcl6"), tn.segs.get("-pcl7"),
tn.segs.get("-pch0"), tn.segs.get("-pch1"), tn.segs.get("-pch2"), tn.segs.get("-pch3"), tn.segs.get("-pch4"), tn.segs.get("-pch5"), tn.segs.get("-pch6"), tn.segs.get("-pch7"),
tn.segs.get("+Pout0"), tn.segs.get("+Pout1"), tn.segs.get("+Pout2"), tn.segs.get("+Pout3"), tn.segs.get("+Pout4"), /*no P5 */tn.segs.get("+Pout6"), tn.segs.get("+Pout7"),
tn.segs.get("-s0"), tn.segs.get("-s1"), tn.segs.get("-s2"), tn.segs.get("-s3"), tn.segs.get("-s4"), tn.segs.get("-s5"), tn.segs.get("-s6"), tn.segs.get("-s7")) {
}
unsigned short Common::rAddr() const {

View File

@ -10,13 +10,11 @@
#include "setpSeg.h"
class SegmentCache;
class TransNetwork;
class Segment;
class Common {
public:
static Common create(const SegmentCache& segs);
Segment *VSS, *VCC;
Segment *CLK0;
Segment *IRQ, *RES, *NMI;
@ -47,6 +45,7 @@ public:
void addDataToRecalc(setpSeg& s) const;
Common(const TransNetwork& segs);
private:
Common(

View File

@ -14,7 +14,7 @@ class Common;
class Cpu6502Helper {
public:
explicit Cpu6502Helper(Cpu6502& cpu, Common& common);
Cpu6502Helper(Cpu6502& cpu, Common& common);
virtual ~Cpu6502Helper();
void powerOn();

8
Emu6502.cpp Normal file
View File

@ -0,0 +1,8 @@
/*
* File: Emu6502.cpp
* Author: Christopher
*
* Created on December 15, 2013, 12:43 AM
*/
#include "Emu6502.h"

55
Emu6502.h Normal file
View File

@ -0,0 +1,55 @@
/*
* File: Emu6502.h
* Author: Christopher
*
* Created on December 15, 2013, 12:43 AM
*/
#ifndef EMU6502_H
#define EMU6502_H
#include "Cpu6502Helper.h"
#include "Cpu6502.h"
#include "Trace.h"
#include "Common.h"
#include "TransNetwork.h"
#include "SegmentCache.h"
#include <istream>
class AddressBus;
class Emu6502 {
public:
Emu6502(std::istream& transistors, AddressBus& mem) : tn(transistors, segs), c(tn), trace(segs, c), cpu(mem, trace, c), cpuhelper(cpu, c) {
}
virtual ~Emu6502() {
}
void powerOn() {
this->cpuhelper.powerOn();
}
void tick() {
this->cpuhelper.tick();
}
void reset() {
this->cpuhelper.reset();
}
private:
Emu6502(const Emu6502&) = delete;
Emu6502 operator=(const Emu6502&) = delete;
SegmentCache segs;
TransNetwork tn;
Common c;
Trace trace;
Cpu6502 cpu;
Cpu6502Helper cpuhelper;
};
#endif /* EMU6502_H */

View File

@ -1,7 +1,7 @@
CXXFLAGS=-g -std=c++11 -Wall -O3
DOXYGEN=doxygen
SRCS = v6502.cpp SegmentCache.cpp Common.cpp TransNetwork.cpp Trace.cpp Circuit.cpp StateCalculator.cpp Cpu6502.cpp Cpu6502Helper.cpp
SRCS = v6502.cpp Emu6502.cpp SegmentCache.cpp Common.cpp TransNetwork.cpp Trace.cpp Circuit.cpp StateCalculator.cpp Cpu6502.cpp Cpu6502Helper.cpp
OBJS = $(SRCS:.cpp=.o)
DEPS = $(SRCS:.cpp=.d)

View File

@ -14,8 +14,8 @@
#include <map>
#include <set>
class Common;
class Segment;
class Common;
class SegmentCache {
public:

View File

@ -13,8 +13,9 @@
#include <iostream>
#include "StateCalculator.h"
#include "Common.h"
#include "SegmentCache.h"
TransNetwork::TransNetwork(std::istream& in) {
TransNetwork::TransNetwork(std::istream& in, SegmentCache& segs) : segs(segs) {
std::string c1, gate, c2;
in >> c1 >> gate >> c2;
while (in.good()) {

View File

@ -9,17 +9,17 @@
#define TRANSNETWORK_H
class Trans;
#include "SegmentCache.h"
#include <set>
#include <memory>
#include <istream>
class SegmentCache;
class Common;
class TransNetwork {
public:
SegmentCache segs;
TransNetwork(std::istream& readFromHere);
TransNetwork(std::istream& readFromHere, SegmentCache& segs);
virtual ~TransNetwork();
private:
@ -27,7 +27,10 @@ private:
TransNetwork(const TransNetwork&) = delete;
TransNetwork& operator=(const TransNetwork&) = delete;
SegmentCache& segs;
std::set<std::shared_ptr<Trans >> transes;
friend Common;
};
#endif /* TRANSNETWORK_H */

View File

@ -1,13 +1,9 @@
#include "Emu6502.h"
#include "addressbus.h"
#include "Cpu6502.h"
#include "Cpu6502Helper.h"
#include "TransNetwork.h"
#include "Trace.h"
#include "Common.h"
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <iostream>
#include <set>
#include <cstdlib>
int main(int argc, char *argv[]) {
AddressBus mem;
@ -30,15 +26,17 @@ int main(int argc, char *argv[]) {
std::ifstream if_trans("transistors");
if (!if_trans.is_open()) {
std::cerr << "error opening file: transistors" << std::endl;
exit(EXIT_FAILURE);
}
TransNetwork tn(if_trans);
Common c = Common::create(tn.segs);
Trace trace(tn.segs, c);
Cpu6502 cpu(mem, trace, c);
Cpu6502Helper cpuhelper(cpu, c);
Emu6502 emu(if_trans, mem);
@ -47,23 +45,27 @@ int main(int argc, char *argv[]) {
/* turn on the CPU */
std::cout << "----------------------------------------" << std::endl;
std::cout << "begin power-up..." << std::endl;
cpuhelper.powerOn();
emu.powerOn();
std::cout << "end power-up..." << std::endl;
std::cout << "----------------------------------------" << std::endl;
/* run it a bit, before resetting */
std::cout << "some power-up pre-reset cycles..." << std::endl;
for (int i(0); i < 10; ++i) {
cpuhelper.tick();
emu.tick();
}
std::cout << "----------------------------------------" << std::endl;
/* reset the CPU, and let it run for a little while, then exit */
std::cout << "RESET..." << std::endl;
cpuhelper.reset();
emu.reset();
for (int i(0); i < 50; ++i) {
cpuhelper.tick();
emu.tick();
}
return EXIT_SUCCESS;
}