From 064eaaed391835d0e0d057466f6f552e43e79bd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCnther=20Neuwirth?= Date: Wed, 27 May 2009 16:43:43 +0200 Subject: add cdatset --- ue4/mycpu/Makefile | 3 +- ue4/mycpu/ccpu.h | 14 ++++----- ue4/mycpu/cdatset.h | 49 +++++++++++++++++++++++++++++ ue4/mycpu/cinstruction.h | 8 ++--- ue4/mycpu/cmem.h | 11 ++++--- ue4/mycpu/cprogram.h | 14 ++++----- ue4/mycpu/instructions.h | 80 ++++++++++++++++++++++++------------------------ ue4/mycpu/mycpu.cpp | 37 +++++++++++++++++++--- 8 files changed, 148 insertions(+), 68 deletions(-) create mode 100644 ue4/mycpu/cdatset.h (limited to 'ue4') diff --git a/ue4/mycpu/Makefile b/ue4/mycpu/Makefile index 8022614..9e6db7a 100644 --- a/ue4/mycpu/Makefile +++ b/ue4/mycpu/Makefile @@ -12,7 +12,8 @@ LIBS= -L/usr/local/lib -lboost_program_options BIN= mycpu OBJS= mycpu.o -HEADERS= cdat.h cmem.h cinstruction.h instructions.h cprogram.h cdisplay.h displays.h ccpu.h +HEADERS= cdat.h cdatn.h cdatset.h cmem.h cinstruction.h instructions.h \ + cprogram.h cdisplay.h displays.h ccpu.h .SUFFIXES: .cpp .o diff --git a/ue4/mycpu/ccpu.h b/ue4/mycpu/ccpu.h index 519cee9..545b870 100644 --- a/ue4/mycpu/ccpu.h +++ b/ue4/mycpu/ccpu.h @@ -22,7 +22,7 @@ #include "cprogram.h" /* forward declare CProgram */ -template +template , int width=0> class CProgram; /** @@ -31,7 +31,7 @@ class CProgram; * CPU implementation. Used as a container for memory and instructions. * Implements a run method to execute the program (= the instructions). */ -template +template, int width=0> class CCPU { typedef typename std::set *>::iterator displayiterator; @@ -250,8 +250,8 @@ class CCPU /*----------------------------------------------------------------------------*/ -template -CCPU::CCPU(const unsigned cnt) +template +CCPU::CCPU(const unsigned cnt) : m_regcnt(cnt), m_memory(NULL), m_program(NULL), m_flagzero(false), m_flagsign(false) { /* create registers */ @@ -266,7 +266,7 @@ CCPU::CCPU(const unsigned cnt) /*----------------------------------------------------------------------------*/ -template +template CCPU::~CCPU() { /* delete registers */ @@ -280,7 +280,7 @@ CCPU::~CCPU() /*----------------------------------------------------------------------------*/ -template +template void CCPU::run() { if (m_memory == NULL) @@ -312,7 +312,7 @@ void CCPU::run() /*----------------------------------------------------------------------------*/ #if DEBUG -template +template void CCPU::dumpRegisters(std::ostream& out) { out << "[REGISTER DUMP]" << std::endl; diff --git a/ue4/mycpu/cdatset.h b/ue4/mycpu/cdatset.h new file mode 100644 index 0000000..03e71f4 --- /dev/null +++ b/ue4/mycpu/cdatset.h @@ -0,0 +1,49 @@ +/** + * @module cdat + * @author Guenther Neuwirth (0626638), Manuel Mausz (0728348) + * @brief Datatype template and datatype definition for CCPU and CMem + * @date 26.05.2009 + */ + +#ifndef CDATSET_H +#define CDATSET_H 1 + +#include +#include + +/** + * @class CDat + * + * Datatype template for CCPU and CMem. + */ +template +class CDatSet + : public CDat +{ + public: + /** + * @method operator>> + * @brief Shift/read operator for inputstream + * @param stream reference to inputstream + * @param cdat reference to object which will be read from stream + * @return reference to inputstream + * @globalvars none + * @exception none + * @conditions none + */ + friend std::istream& operator>>(std::istream & stream, CDatSet& cdat) + { + std::string s; + stream >> s; + cdat.m_value = s.size(); + return stream; + } + + private: + /* members */ + T m_value; +}; + +#endif + +/* vim: set et sw=2 ts=2: */ diff --git a/ue4/mycpu/cinstruction.h b/ue4/mycpu/cinstruction.h index 8c35d2c..a2e3743 100644 --- a/ue4/mycpu/cinstruction.h +++ b/ue4/mycpu/cinstruction.h @@ -17,7 +17,7 @@ #include "ccpu.h" /* forward declare CCPU */ -template +template class CCPU; /** @@ -25,7 +25,7 @@ class CCPU; * * Abstract class for displays */ -template +template, int width=0> class CInstruction { public: @@ -193,7 +193,7 @@ class CInstruction /*----------------------------------------------------------------------------*/ -template +template, int width=0> const unsigned CInstruction::parseRegister(const std::string& str) { unsigned reg; @@ -214,7 +214,7 @@ const unsigned CInstruction::parseRegister(const std::string& str) /*----------------------------------------------------------------------------*/ -template +template, int width=0> inline void CInstruction::checkRegister(CCPU *cpu, const unsigned regidx) { assert(cpu != NULL); diff --git a/ue4/mycpu/cmem.h b/ue4/mycpu/cmem.h index 6b23111..c6b8735 100644 --- a/ue4/mycpu/cmem.h +++ b/ue4/mycpu/cmem.h @@ -13,6 +13,7 @@ #include #include #include +#include #ifdef DEBUG # include # include @@ -23,7 +24,7 @@ * * Extends std::vector template for use as memory for CCPU. */ -template +template, int width=0> class CMem : public std::vector { @@ -55,7 +56,8 @@ class CMem { ++i; std::getline(in, line); - + boost::algorithm::trim(line); + /* skip last line if it's empty */ if (line.empty() && in.eof()) break; @@ -63,8 +65,9 @@ class CMem T value; try { - if (!line.empty()) - value = boost::lexical_cast(line); + if (!line.empty()) + { + value = boost::lexical_cast(line); } catch(boost::bad_lexical_cast& ex) { diff --git a/ue4/mycpu/cprogram.h b/ue4/mycpu/cprogram.h index 43193ac..d8f7d5e 100644 --- a/ue4/mycpu/cprogram.h +++ b/ue4/mycpu/cprogram.h @@ -21,7 +21,7 @@ #include "instructions.h" /* forward declare CInstruction */ -template +template, int width=0> class CInstruction; /** @@ -30,7 +30,7 @@ class CInstruction; * CProgram extends std::vector and adds a method for parsing * programfile. This adds instances of CInstruction to CProgram itself. */ -template +template, int width=0> class CProgram : public std::vector *> { @@ -122,7 +122,7 @@ class CProgram /*----------------------------------------------------------------------------*/ -template +template, int width=0> CProgram::CProgram() { m_instrset.insert(new CInstructionInc); @@ -143,7 +143,7 @@ CProgram::CProgram() /*----------------------------------------------------------------------------*/ -template +template, int width=0> CProgram::~CProgram() { /* free instruction set */ @@ -157,7 +157,7 @@ CProgram::~CProgram() /*----------------------------------------------------------------------------*/ -template +template, int width=0> void CProgram::compile(std::istream& in) { if (!in.good()) @@ -241,7 +241,7 @@ void CProgram::compile(std::istream& in) /*----------------------------------------------------------------------------*/ -template +template, int width=0> unsigned CProgram::findLabel(const std::string& label) const { std::map::const_iterator it; @@ -254,7 +254,7 @@ unsigned CProgram::findLabel(const std::string& label) const /*----------------------------------------------------------------------------*/ #if DEBUG -template +template, int width=0> void CProgram::dump(std::ostream& out) { out << "[PROGRAM DUMP]" << std::endl; diff --git a/ue4/mycpu/instructions.h b/ue4/mycpu/instructions.h index fcff3e7..3f2fd51 100644 --- a/ue4/mycpu/instructions.h +++ b/ue4/mycpu/instructions.h @@ -19,7 +19,7 @@ * Syntax: inc R1 * (R1++) */ -template +template, int width=0> class CInstructionInc : public CInstruction { @@ -45,7 +45,7 @@ class CInstructionInc /*----------------------------------------------------------------------------*/ -template +template, int width=0> void CInstructionInc::compile(std::list& params) { if (params.size() != 1) @@ -56,7 +56,7 @@ void CInstructionInc::compile(std::list& params) /*----------------------------------------------------------------------------*/ -template +template, int width=0> void CInstructionInc::execute(CCPU *cpu) { assert(cpu != NULL); @@ -74,7 +74,7 @@ void CInstructionInc::execute(CCPU *cpu) * Syntax: dec R1 * (R1--) */ -template +template, int width=0> class CInstructionDec : public CInstruction { @@ -100,7 +100,7 @@ class CInstructionDec /*----------------------------------------------------------------------------*/ -template +template, int width=0> void CInstructionDec::compile(std::list& params) { if (params.size() != 1) @@ -111,7 +111,7 @@ void CInstructionDec::compile(std::list& params) /*----------------------------------------------------------------------------*/ -template +template, int width=0> void CInstructionDec::execute(CCPU *cpu) { assert(cpu != NULL); @@ -129,7 +129,7 @@ void CInstructionDec::execute(CCPU *cpu) * Syntax: add R1, R2, R3 * (R1 = R2 + R3) */ -template +template, int width=0> class CInstructionAdd : public CInstruction { @@ -159,7 +159,7 @@ class CInstructionAdd /*----------------------------------------------------------------------------*/ -template +template, int width=0> void CInstructionAdd::compile(std::list& params) { if (params.size() != 3) @@ -174,7 +174,7 @@ void CInstructionAdd::compile(std::list& params) /*----------------------------------------------------------------------------*/ -template +template, int width=0> void CInstructionAdd::execute(CCPU *cpu) { assert(cpu != NULL); @@ -195,7 +195,7 @@ void CInstructionAdd::execute(CCPU *cpu) * Syntax: sub R1, R2, R3 * (R1 = R2 - R3) */ -template +template, int width=0> class CInstructionSub : public CInstruction { @@ -225,7 +225,7 @@ class CInstructionSub /*----------------------------------------------------------------------------*/ -template +template, int width=0> void CInstructionSub::compile(std::list& params) { if (params.size() != 3) @@ -240,7 +240,7 @@ void CInstructionSub::compile(std::list& params) /*----------------------------------------------------------------------------*/ -template +template, int width=0> void CInstructionSub::execute(CCPU *cpu) { assert(cpu != NULL); @@ -261,7 +261,7 @@ void CInstructionSub::execute(CCPU *cpu) * Syntax: mul R1, R2, R3 * (R1 = R2 * R3) */ -template +template, int width=0> class CInstructionMul : public CInstruction { @@ -291,7 +291,7 @@ class CInstructionMul /*----------------------------------------------------------------------------*/ -template +template, int width=0> void CInstructionMul::compile(std::list& params) { if (params.size() != 3) @@ -306,7 +306,7 @@ void CInstructionMul::compile(std::list& params) /*----------------------------------------------------------------------------*/ -template +template, int width=0> void CInstructionMul::execute(CCPU *cpu) { super::checkRegister(cpu, m_regidx1); @@ -325,7 +325,7 @@ void CInstructionMul::execute(CCPU *cpu) * Syntax: div R1, R2, R3 * (R1 = R2 / R3) */ -template +template, int width=0> class CInstructionDiv : public CInstruction { @@ -355,7 +355,7 @@ class CInstructionDiv /*----------------------------------------------------------------------------*/ -template +template, int width=0> void CInstructionDiv::compile(std::list& params) { if (params.size() != 3) @@ -370,7 +370,7 @@ void CInstructionDiv::compile(std::list& params) /*----------------------------------------------------------------------------*/ -template +template, int width=0> void CInstructionDiv::execute(CCPU *cpu) { assert(cpu != NULL); @@ -391,7 +391,7 @@ void CInstructionDiv::execute(CCPU *cpu) * Syntax: load R1, R2 * (R1 = memory[R2]) */ -template +template, int width=0> class CInstructionLoad : public CInstruction { @@ -419,7 +419,7 @@ class CInstructionLoad /*----------------------------------------------------------------------------*/ -template +template, int width=0> void CInstructionLoad::compile(std::list& params) { if (params.size() != 2) @@ -432,7 +432,7 @@ void CInstructionLoad::compile(std::list& params) /*----------------------------------------------------------------------------*/ -template +template, int width=0> void CInstructionLoad::execute(CCPU *cpu) { assert(cpu != NULL); @@ -453,7 +453,7 @@ void CInstructionLoad::execute(CCPU *cpu) * Syntax: store R1, R2 * (memory[R2] = R1) */ -template +template, int width=0> class CInstructionStore : public CInstruction { @@ -481,7 +481,7 @@ class CInstructionStore /*----------------------------------------------------------------------------*/ -template +template, int width=0> void CInstructionStore::compile(std::list& params) { if (params.size() != 2) @@ -494,7 +494,7 @@ void CInstructionStore::compile(std::list& params) /*----------------------------------------------------------------------------*/ -template +template, int width=0> void CInstructionStore::execute(CCPU *cpu) { assert(cpu != NULL); @@ -515,7 +515,7 @@ void CInstructionStore::execute(CCPU *cpu) * Syntax: test R1 * (R1 == 0: zeroflag: true, R1 < 0: signflag: true) */ -template +template, int width=0> class CInstructionTest : public CInstruction { @@ -541,7 +541,7 @@ class CInstructionTest /*----------------------------------------------------------------------------*/ -template +template, int width=0> void CInstructionTest::compile(std::list& params) { if (params.size() != 1) @@ -552,7 +552,7 @@ void CInstructionTest::compile(std::list& params) /*----------------------------------------------------------------------------*/ -template +template, int width=0> void CInstructionTest::execute(CCPU *cpu) { assert(cpu != NULL); @@ -572,7 +572,7 @@ void CInstructionTest::execute(CCPU *cpu) * Implementation of assembler command "label" * Syntax: label name: */ -template +template, int width=0> class CInstructionLabel : public CInstruction { @@ -604,7 +604,7 @@ class CInstructionLabel * Syntax: jumpa labelname * (jump to labelname) */ -template +template, int width=0> class CInstructionJumpA : public CInstruction { @@ -630,7 +630,7 @@ class CInstructionJumpA /*----------------------------------------------------------------------------*/ -template +template, int width=0> void CInstructionJumpA::compile(std::list& params) { if (params.size() != 1) @@ -641,7 +641,7 @@ void CInstructionJumpA::compile(std::list& params) /*----------------------------------------------------------------------------*/ -template +template, int width=0> void CInstructionJumpA::execute(CCPU *cpu) { assert(cpu != NULL); @@ -661,7 +661,7 @@ void CInstructionJumpA::execute(CCPU *cpu) * Syntax: jumpz labelname * (jump to labelname if zeroflag) */ -template +template, int width=0> class CInstructionJumpZ : public CInstruction { @@ -687,7 +687,7 @@ class CInstructionJumpZ /*----------------------------------------------------------------------------*/ -template +template, int width=0> void CInstructionJumpZ::compile(std::list& params) { if (params.size() != 1) @@ -698,7 +698,7 @@ void CInstructionJumpZ::compile(std::list& params) /*----------------------------------------------------------------------------*/ -template +template, int width=0> void CInstructionJumpZ::execute(CCPU *cpu) { assert(cpu != NULL); @@ -720,7 +720,7 @@ void CInstructionJumpZ::execute(CCPU *cpu) * Syntax: jumps labelname * (jump to labelname if signflag) */ -template +template, int width=0> class CInstructionJumpS : public CInstruction { @@ -746,7 +746,7 @@ class CInstructionJumpS /*----------------------------------------------------------------------------*/ -template +template, int width=0> void CInstructionJumpS::compile(std::list& params) { if (params.size() != 1) @@ -757,7 +757,7 @@ void CInstructionJumpS::compile(std::list& params) /*----------------------------------------------------------------------------*/ -template +template, int width=0> void CInstructionJumpS::execute(CCPU *cpu) { assert(cpu != NULL); @@ -779,7 +779,7 @@ void CInstructionJumpS::execute(CCPU *cpu) * Syntax: write DEV, R1 * (write R1 to DEV, which is a name of a display) */ -template +template, int width=0> class CInstructionWrite : public CInstruction { @@ -808,7 +808,7 @@ class CInstructionWrite /*----------------------------------------------------------------------------*/ -template +template, int width=0> void CInstructionWrite::compile(std::list& params) { if (params.size() != 2) @@ -821,7 +821,7 @@ void CInstructionWrite::compile(std::list& params) /*----------------------------------------------------------------------------*/ -template +template, int width=0> void CInstructionWrite::execute(CCPU *cpu) { assert(cpu != NULL); diff --git a/ue4/mycpu/mycpu.cpp b/ue4/mycpu/mycpu.cpp index 6c9f71a..2d73177 100644 --- a/ue4/mycpu/mycpu.cpp +++ b/ue4/mycpu/mycpu.cpp @@ -17,6 +17,8 @@ #include #include #include "cdat.h" +#include "cdatn.h" +#include "cdatset.h" #include "cmem.h" #include "cprogram.h" #include "ccpu.h" @@ -73,7 +75,32 @@ int main(int argc, char* argv[]) } /* create memory and optionally initialize memory from file */ - CMem > memory; + + CMem *memory = NULL; + if (vm.count("format")) + { + string format(vm["format"].as()); + if(format == "s") + memory = new CMem >(); + else + { + try + { + int bc = boost::lexical_cast(format); + if (bc > 1 && bc < 33) + memory = new CMem >(); + } + catch(boost::bad_lexical_cast& ex) + { + std::stringstream sstr; + sstr << "Illegal format: (" << format << "): " << ex.what(); + throw std::runtime_error(sstr.str()); + } + } + } + else + + if (vm.count("memory")) { string memoryfile(vm["memory"].as()); @@ -86,7 +113,7 @@ int main(int argc, char* argv[]) try { - memory.initialize(file); + memory->initialize(file); file.close(); } catch(runtime_error& ex) @@ -98,7 +125,7 @@ int main(int argc, char* argv[]) } #if DEBUG - memory.dump(cerr); + memory->dump(cerr); #endif } @@ -134,7 +161,7 @@ int main(int argc, char* argv[]) try { CCPU > cpu(256); - cpu.setMemory(&memory); + cpu.setMemory(memory); cpu.setProgram(&program); cpu.run(); #if DEBUG @@ -146,7 +173,7 @@ int main(int argc, char* argv[]) cerr << me << ": Error while executing program:" << endl << " " << ex.what() << endl; #if DEBUG - memory.dump(cerr); + memory->dump(cerr); #endif return 1; } -- cgit v1.2.3