From 55df84b3058231d71227bffa355d8269b34c8fb9 Mon Sep 17 00:00:00 2001 From: Christopher Mosher Date: Sun, 15 Dec 2013 01:13:44 -0500 Subject: [PATCH] refactor: add top-level Emu6502 class --- Common.cpp | 37 ++++++++++++++++---------------- Common.h | 5 ++--- Cpu6502Helper.h | 2 +- Emu6502.cpp | 8 +++++++ Emu6502.h | 55 ++++++++++++++++++++++++++++++++++++++++++++++++ Makefile | 2 +- SegmentCache.h | 2 +- TransNetwork.cpp | 3 ++- TransNetwork.h | 11 ++++++---- v6502.cpp | 34 ++++++++++++++++-------------- 10 files changed, 114 insertions(+), 45 deletions(-) create mode 100644 Emu6502.cpp create mode 100644 Emu6502.h diff --git a/Common.cpp b/Common.cpp index 15a6724..f96c062 100644 --- a/Common.cpp +++ b/Common.cpp @@ -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 { diff --git a/Common.h b/Common.h index 7e34ce9..74b6096 100644 --- a/Common.h +++ b/Common.h @@ -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( diff --git a/Cpu6502Helper.h b/Cpu6502Helper.h index ecaa41b..95c4fb0 100644 --- a/Cpu6502Helper.h +++ b/Cpu6502Helper.h @@ -14,7 +14,7 @@ class Common; class Cpu6502Helper { public: - explicit Cpu6502Helper(Cpu6502& cpu, Common& common); + Cpu6502Helper(Cpu6502& cpu, Common& common); virtual ~Cpu6502Helper(); void powerOn(); diff --git a/Emu6502.cpp b/Emu6502.cpp new file mode 100644 index 0000000..c063d99 --- /dev/null +++ b/Emu6502.cpp @@ -0,0 +1,8 @@ +/* + * File: Emu6502.cpp + * Author: Christopher + * + * Created on December 15, 2013, 12:43 AM + */ + +#include "Emu6502.h" diff --git a/Emu6502.h b/Emu6502.h new file mode 100644 index 0000000..63f3333 --- /dev/null +++ b/Emu6502.h @@ -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 + +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 */ diff --git a/Makefile b/Makefile index 1f594e0..69b842b 100644 --- a/Makefile +++ b/Makefile @@ -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) diff --git a/SegmentCache.h b/SegmentCache.h index f05a409..5917632 100644 --- a/SegmentCache.h +++ b/SegmentCache.h @@ -14,8 +14,8 @@ #include #include -class Common; class Segment; +class Common; class SegmentCache { public: diff --git a/TransNetwork.cpp b/TransNetwork.cpp index f0eb75b..aa3bb8e 100644 --- a/TransNetwork.cpp +++ b/TransNetwork.cpp @@ -13,8 +13,9 @@ #include #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()) { diff --git a/TransNetwork.h b/TransNetwork.h index 30d7826..6043991 100644 --- a/TransNetwork.h +++ b/TransNetwork.h @@ -9,17 +9,17 @@ #define TRANSNETWORK_H class Trans; -#include "SegmentCache.h" #include #include #include +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> transes; + + friend Common; }; #endif /* TRANSNETWORK_H */ diff --git a/v6502.cpp b/v6502.cpp index dbd76c7..94647a1 100644 --- a/v6502.cpp +++ b/v6502.cpp @@ -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 -#include #include +#include #include +#include 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; }