From 1a60d0c2a8eeef3b39ef276f0f3552552a1519b1 Mon Sep 17 00:00:00 2001 From: manuel Date: Tue, 26 May 2009 14:49:37 +0200 Subject: adding ue4 (copy from ue3) --- ue4/mycpu/Makefile | 40 ++++ ue4/mycpu/ccpu.cpp | 89 +++++++++ ue4/mycpu/ccpu.h | 241 +++++++++++++++++++++++ ue4/mycpu/cdat.h | 326 +++++++++++++++++++++++++++++++ ue4/mycpu/cdisplay.h | 85 ++++++++ ue4/mycpu/cinstruction.cpp | 48 +++++ ue4/mycpu/cinstruction.h | 189 ++++++++++++++++++ ue4/mycpu/cmem.h | 109 +++++++++++ ue4/mycpu/cprogram.cpp | 161 +++++++++++++++ ue4/mycpu/cprogram.h | 106 ++++++++++ ue4/mycpu/displays.h | 76 ++++++++ ue4/mycpu/instructions.cpp | 341 ++++++++++++++++++++++++++++++++ ue4/mycpu/instructions.h | 454 +++++++++++++++++++++++++++++++++++++++++++ ue4/mycpu/mycpu.cpp | 155 +++++++++++++++ ue4/mycpu/test/test.sh | 42 ++++ ue4/mycpu/test/test1_memory | 1 + ue4/mycpu/test/test1_output | 19 ++ ue4/mycpu/test/test1_program | 13 ++ 18 files changed, 2495 insertions(+) create mode 100644 ue4/mycpu/Makefile create mode 100644 ue4/mycpu/ccpu.cpp create mode 100644 ue4/mycpu/ccpu.h create mode 100644 ue4/mycpu/cdat.h create mode 100644 ue4/mycpu/cdisplay.h create mode 100644 ue4/mycpu/cinstruction.cpp create mode 100644 ue4/mycpu/cinstruction.h create mode 100644 ue4/mycpu/cmem.h create mode 100644 ue4/mycpu/cprogram.cpp create mode 100644 ue4/mycpu/cprogram.h create mode 100644 ue4/mycpu/displays.h create mode 100644 ue4/mycpu/instructions.cpp create mode 100644 ue4/mycpu/instructions.h create mode 100644 ue4/mycpu/mycpu.cpp create mode 100755 ue4/mycpu/test/test.sh create mode 100644 ue4/mycpu/test/test1_memory create mode 100644 ue4/mycpu/test/test1_output create mode 100644 ue4/mycpu/test/test1_program (limited to 'ue4/mycpu') diff --git a/ue4/mycpu/Makefile b/ue4/mycpu/Makefile new file mode 100644 index 0000000..0dbb51a --- /dev/null +++ b/ue4/mycpu/Makefile @@ -0,0 +1,40 @@ +# Makefile for mycpu +# Author: Guenther Neuwirth (0626638), Manuel Mausz (0728348) +# Created: 26.05.2009 + +CXX= g++ +LD= $(CXX) +DEBUGFLAGS= -DNDEBUG +INCLUDE_PATH= -I/usr/local/include +CXXFLAGS= -O -ansi -pedantic-errors -Wall -Wno-long-long $(INCLUDE_PATH) $(DEBUGFLAGS) +LDFLAGS= +LIBS= -L/usr/local/lib -lboost_program_options + +BIN= mycpu +OBJS= cinstruction.o instructions.o cprogram.o ccpu.o mycpu.o +HEADERS= cdat.h cmem.h cinstruction.h instructions.h cprogram.h cdisplay.h displays.h ccpu.h + +.SUFFIXES: .cpp .o + +all: $(BIN) + +.cpp.o: + $(CXX) $(CXXFLAGS) -c $< -o $@ + +$(OBJS): $(HEADERS) + +$(BIN): $(OBJS) + $(LD) $(LDFLAGS) -o $@ $(OBJS) $(LIBS) + +debug: + @$(MAKE) all "DEBUGFLAGS=-DDEBUG -g" + +clean: + rm -f $(OBJS) $(BIN) + +run test: all + @./test/test.sh + +.PHONY: clean + +# vim600: noet sw=8 ts=8 diff --git a/ue4/mycpu/ccpu.cpp b/ue4/mycpu/ccpu.cpp new file mode 100644 index 0000000..af86200 --- /dev/null +++ b/ue4/mycpu/ccpu.cpp @@ -0,0 +1,89 @@ +/** + * @module ccpu + * @author Guenther Neuwirth (0626638), Manuel Mausz (0728348) + * @brief CPU implementation. Used as a container for memory and instructions. + * Implements an run method to execute the program (= the instructions). + * @date 10.05.2009 + */ + +#ifdef DEBUG +# include +# include +#endif +#include "ccpu.h" +#include "displays.h" + +using namespace std; + +CCPU::CCPU(const unsigned cnt) + : m_regcnt(cnt), m_memory(NULL), m_program(NULL), m_flagzero(false), m_flagsign(false) +{ + /* create registers */ + m_registers = new CDat[cnt]; + for(unsigned i = 0; i < cnt; ++i) + m_registers[i] = 0; + + /* create displays */ + m_displays.insert(new CDisplayWDEZ); + m_displays.insert(new CDisplayWHEX); +} + +/*----------------------------------------------------------------------------*/ + +CCPU::~CCPU() +{ + /* delete registers */ + delete[] m_registers; + m_registers = NULL; + + /* delete displays */ + std::set::iterator it; + for (it = m_displays.begin() ; it != m_displays.end(); ++it) + delete *it; +} + +/*----------------------------------------------------------------------------*/ + +void CCPU::run() +{ + if (m_memory == NULL) + throw runtime_error("CPU has no memory"); + if (m_program == NULL) + throw runtime_error("CPU has no program to execute"); + if (m_regcnt == 0) + throw runtime_error("CPU has no registers"); + + bool run = true; + while(run) + { + unsigned pc = static_cast(m_registers[0]); + + /* end of the program reached */ + if (pc == m_program->size()) + break; + + /* pc is out of bound */ + if (pc > m_program->size()) + throw runtime_error("Programcounter is out of bound"); + + /* execute instruction */ + (*m_program->at(pc))(this); + ++m_registers[0]; + } +} + +/*----------------------------------------------------------------------------*/ + +#if DEBUG +void CCPU::dumpRegisters(std::ostream& out) +{ + out << "[REGISTER DUMP]" << endl; + for(unsigned i = 0; i < getRegisterCount(); ++i) + { + out << "[" << std::setw(4) << std::setfill('0') << i << "] " + << m_registers[i] << endl; + } +} +#endif + +/* vim: set et sw=2 ts=2: */ diff --git a/ue4/mycpu/ccpu.h b/ue4/mycpu/ccpu.h new file mode 100644 index 0000000..6849623 --- /dev/null +++ b/ue4/mycpu/ccpu.h @@ -0,0 +1,241 @@ +/** + * @module ccpu + * @author Guenther Neuwirth (0626638), Manuel Mausz (0728348) + * @brief CPU implementation. Used as a container for memory and instructions. + * Implements a run method to execute the program (= the instructions). + * @date 10.05.2009 + */ + +#ifndef CCPU_H +#define CCPU_H 1 + +#include +#include +#include "cdat.h" +#include "cmem.h" +#include "cprogram.h" +#include "cdisplay.h" + +/** + * @class CCPU + * + * CPU implementation. Used as a container for memory and instructions. + * Implements a run method to execute the program (= the instructions). + */ +class CCPU +{ + public: + /** + * @method CCPU + * @brief Default ctor + * @param cnt number of registers to allocate for this cpu + * @return - + * @globalvars none + * @exception none + * @conditions none + */ + CCPU(const unsigned cnt); + + /** + * @method ~CCPU + * @brief Default dtor + * @param - + * @return - + * @globalvars none + * @exception none + * @conditions none + */ + ~CCPU(); + + /** + * @method getRegisterCount + * @brief get number of registers + * @param - + * @return number of registers + * @globalvars none + * @exception none + * @conditions none + */ + const unsigned getRegisterCount() const + { + return m_regcnt; + } + + /** + * @method getRegisters + * @brief get pointer to registers array + * @param - + * @return pointer to registers array + * @globalvars none + * @exception none + * @conditions none + */ + CDat *getRegisters() const + { + return m_registers; + } + + /** + * @method setMemory + * @brief set memory of cpu + * @param memory pointer to memory + * @return - + * @globalvars none + * @exception none + * @conditions none + */ + void setMemory(CMem *memory) + { + m_memory = memory; + } + + /** + * @method getMemory + * @brief get pointer to memory + * @param - + * @return pointer to memory + * @globalvars none + * @exception none + * @conditions none + */ + CMem *getMemory() const + { + return m_memory; + } + + /** + * @method setProgram + * @brief set program to execute + * @param program pointer to program + * @return - + * @globalvars none + * @exception none + * @conditions none + */ + void setProgram(const CProgram *program) + { + m_program = program; + } + + /** + * @method getProgram + * @brief get pointer to program + * @param - + * @return pointer to program + * @globalvars none + * @exception none + * @conditions none + */ + const CProgram *getProgram() + { + return m_program; + } + + /** + * @method getDisplays + * @brief get set of pointers to displays + * @param - + * @return reference to set of pointers to displays + * @globalvars none + * @exception none + * @conditions none + */ + const std::set& getDisplays() + { + return m_displays; + } + + /** + * @method setFlagZero + * @brief set zero flag + * @param value new value of zero flag + * @return - + * @globalvars none + * @exception none + * @conditions none + */ + void setFlagZero(const bool value) + { + m_flagzero = value; + } + + /** + * @method getFlagZero + * @brief get value of zero flag + * @param - + * @return value of zero flag + * @globalvars none + * @exception none + * @conditions none + */ + const bool getFlagZero() + { + return m_flagzero; + } + + /** + * @method setFlagSign + * @brief set sign flag + * @param value new value of sign flag + * @return - + * @globalvars none + * @exception none + * @conditions none + */ + void setFlagSign(const bool value) + { + m_flagsign = value; + } + + /** + * @method getFlagSign + * @brief get value of sign flag + * @param - + * @return value of sign flag + * @globalvars none + * @exception none + * @conditions none + */ + const bool getFlagSign() + { + return m_flagsign; + } + + /** + * @method run + * @brief execute current program + * @param - + * @return - + * @globalvars none + * @exception std::runtime_error + * @conditions none + */ + void run(); + +#if DEBUG + /** + * @method dumpRegisters + * @brief dump content of registers to outputstream + * @param out outputstream to write to + * @return void + * @globalvars none + * @exception none + * @conditions none + */ + void dumpRegisters(std::ostream& out); +#endif + + private: + /* members */ + CDat *m_registers; + unsigned m_regcnt; + CMem *m_memory; + const CProgram *m_program; + std::set m_displays; + bool m_flagzero; + bool m_flagsign; +}; + +#endif + +/* vim: set et sw=2 ts=2: */ diff --git a/ue4/mycpu/cdat.h b/ue4/mycpu/cdat.h new file mode 100644 index 0000000..a533fae --- /dev/null +++ b/ue4/mycpu/cdat.h @@ -0,0 +1,326 @@ +/** + * @module cdat + * @author Guenther Neuwirth (0626638), Manuel Mausz (0728348) + * @brief Datatype template and datatype definition for CCPU and CMem + * @date 10.05.2009 + */ + +#ifndef CDAT_H +#define CDAT_H 1 + +#include +#include + +/** + * @class CDatT + * + * Datatype template for CCPU and CMem. + */ +template +class CDatT + : boost::operators > +{ + public: + /** + * @method CDatT + * @brief Default ctor + * @param - + * @return - + * @globalvars none + * @exception bad_alloc + * @conditions none + */ + CDatT() + {} + + /** + * @method ~CDatT + * @brief Default dtor + * @param - + * @return - + * @globalvars none + * @exception none + * @conditions none + */ + ~CDatT() + {} + + /** + * @method CDatT + * @brief Copy constructor for CDatT + * @param other reference to CDatT which will be copied + * @return - + * @globalvars none + * @exception none + * @conditions none + */ + CDatT(const CDatT& other) + : m_value(other.m_value) + {} + + /** + * @method CDatT + * @brief Copy constructor for int + * @param newval new value for CDatT + * @return - + * @globalvars none + * @exception none + * @conditions none + */ + CDatT(T newval) + : m_value(newval) + {} + + /** + * @method getValue + * @brief returns value of CDatT + * @param - + * @return value of CDatT + * @globalvars none + * @exception none + * @conditions none + */ + T getValue() const + { + return m_value; + } + + /** + * @method operator T + * @brief convert to T + * @param - + * @return T + * @globalvars none + * @exception none + * @conditions none + */ + operator T() + { + return m_value; + } + + /** + * @method operator< + * @brief implementation of operator < + * @param x reference to CDatT + * @return true if cdat is less than object x + * @globalvars none + * @exception none + * @conditions none + */ + bool operator<(const CDatT& x) const + { + return m_value < x.m_value; + } + + /** + * @method operator== + * @brief implementation of operator == + * @param x reference to CDatT + * @return true if cdat equals object x + * @globalvars none + * @exception none + * @conditions none + */ + bool operator==(const CDatT& x) const + { + return m_value == x.m_value; + } + + /** + * @method operator+= + * @brief implementation of operator += + * @param x reference to CDatT + * @return refecence to CDatT + * @globalvars none + * @exception none + * @conditions none + */ + CDatT& operator+=(const CDatT& x) + { + m_value += x.m_value; + return *this; + } + + /** + * @method operator-= + * @brief implementation of operator -= + * @param x reference to CDatT + * @return refecence to CDatT + * @globalvars none + * @exception none + * @conditions none + */ + CDatT& operator-=(const CDatT& x) + { + m_value -= x.m_value; + return *this; + } + + /** + * @method operator*= + * @brief implementation of operator *= + * @param x reference to CDatT + * @return refecence to CDatT + * @globalvars none + * @exception none + * @conditions none + */ + CDatT& operator*=(const CDatT& x) + { + m_value *= x.m_value; + return *this; + } + + /** + * @method operator/= + * @brief implementation of operator /= + * @param x reference to CDatT + * @return refecence to CDatT + * @globalvars none + * @exception none + * @conditions none + */ + CDatT& operator/=(const CDatT& x) + { + m_value /= x.m_value; + return *this; + } + + /** + * @method operator%= + * @brief implementation of operator %= + * @param x reference to CDatT + * @return refecence to CDatT + * @globalvars none + * @exception none + * @conditions none + */ + CDatT& operator%=(const CDatT& x) + { + m_value %= x.m_value; + return *this; + } + + /** + * @method operator|= + * @brief implementation of operator |= + * @param x reference to CDatT + * @return refecence to CDatT + * @globalvars none + * @exception none + * @conditions none + */ + CDatT& operator|=(const CDatT& x) + { + m_value |= x.m_value; + return *this; + } + + /** + * @method operator&= + * @brief implementation of operator &= + * @param x reference to CDatT + * @return refecence to CDatT + * @globalvars none + * @exception none + * @conditions none + */ + CDatT& operator&=(const CDatT& x) + { + m_value &= x.m_value; + return *this; + } + + /** + * @method operator^= + * @brief implementation of operator ^= + * @param x reference to CDatT + * @return refecence to CDatT + * @globalvars none + * @exception none + * @conditions none + */ + CDatT& operator^=(const CDatT& x) + { + m_value ^= x.m_value; + return *this; + } + + /** + * @method operator++ + * @brief implementation of operator ++ + * @param - + * @return refecence to CDatT + * @globalvars none + * @exception none + * @conditions none + */ + CDatT& operator++() + { + m_value++; + return *this; + } + + /** + * @method operator-- + * @brief implementation of operator -- + * @param - + * @return refecence to CDatT + * @globalvars none + * @exception none + * @conditions none + */ + CDatT& operator--() + { + m_value--; + return *this; + } + + /** + * @method operator<< + * @brief Shift/output operator for outputstream + * @param stream reference to outputstream + * @param cdat object which will be printed to stream + * @return reference to outputstream + * @globalvars none + * @exception none + * @conditions none + */ + friend std::ostream& operator<<(std::ostream& stream, CDatT cdat) + { + stream << cdat.m_value; + return stream; + } + + /** + * @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, CDatT& cdat) + { + stream >> cdat.m_value; + return stream; + } + + private: + /* members */ + T m_value; +}; + +/** + * @class CDat + * + * Datatype for CCPU and CMem + */ +typedef CDatT CDat; + +#endif + +/* vim: set et sw=2 ts=2: */ diff --git a/ue4/mycpu/cdisplay.h b/ue4/mycpu/cdisplay.h new file mode 100644 index 0000000..82776ee --- /dev/null +++ b/ue4/mycpu/cdisplay.h @@ -0,0 +1,85 @@ +/** + * @module cdisplay + * @author Guenther Neuwirth (0626638), Manuel Mausz (0728348) + * @brief Abstract template class for displays + * @date 10.05.2009 + */ + +#ifndef CDISPLAY_H +#define CDISPLAY_H 1 + +/** + * @class CDisplayT + * + * Abstract template class for displays + */ +template +class CDisplayT +{ + public: + /** + * @method CDisplayT + * @brief Default ctor + * @param name name of display + * @return - + * @globalvars none + * @exception none + * @conditions none + */ + CDisplayT(std::string name) + : m_name(name) + {} + + /** + * @method ~CDisplayT + * @brief Default dtor + * @param - + * @return - + * @globalvars none + * @exception none + * @conditions none + */ + virtual ~CDisplayT() + {} + + /** + * @method getName + * @brief returns name of display + * @param - + * @return name of display + * @globalvars none + * @exception none + * @conditions none + */ + virtual const std::string& getName() + { + return m_name; + } + + /** + * @method display + * @brief prints value to display + * @param value value to display + * @return - + * @globalvars none + * @exception none + * @conditions none + */ + virtual void display(const T &value) = 0; + + protected: + /* members */ + /** name of display */ + std::string m_name; +}; + +/** + * @class CDisplay + * + * Memory definition for CCPU + */ +typedef CDisplayT CDisplay; + +#endif + +/* vim: set et sw=2 ts=2: */ diff --git a/ue4/mycpu/cinstruction.cpp b/ue4/mycpu/cinstruction.cpp new file mode 100644 index 0000000..a766015 --- /dev/null +++ b/ue4/mycpu/cinstruction.cpp @@ -0,0 +1,48 @@ +/** + * @module cinstruction + * @author Guenther Neuwirth (0626638), Manuel Mausz (0728348) + * @brief Abstract class for displays + * @date 13.05.2009 + */ + +#include +#include +#include +#include +#include "cinstruction.h" +#include "ccpu.h" + +using namespace std; + +const unsigned CInstruction::parseRegister(const std::string& str) +{ + unsigned reg; + if (str.length() < 2 || str[0] != 'r') + throw runtime_error("Invalid syntax of register"); + + try + { + reg = boost::lexical_cast(str.substr(1)); + } + catch(boost::bad_lexical_cast& ex) + { + throw runtime_error("Invalid syntax of register"); + } + + return reg; +} + +/*----------------------------------------------------------------------------*/ + +inline void CInstruction::checkRegister(CCPU *cpu, const unsigned regidx) +{ + assert(cpu != NULL); + if (regidx >= cpu->getRegisterCount()) + { + stringstream sstr; + sstr << "Register R" << regidx << " doesn't exist (out of bound)"; + throw runtime_error(sstr.str()); + } +} + +/* vim: set et sw=2 ts=2: */ diff --git a/ue4/mycpu/cinstruction.h b/ue4/mycpu/cinstruction.h new file mode 100644 index 0000000..4cc69de --- /dev/null +++ b/ue4/mycpu/cinstruction.h @@ -0,0 +1,189 @@ +/** + * @module cinstruction + * @author Guenther Neuwirth (0626638), Manuel Mausz (0728348) + * @brief Abstract class for displays + * @date 13.05.2009 + */ + +#ifndef CINSTRUCTION_H +#define CINSTRUCTION_H 1 + +#include +#include + +/* forward declare CCPU */ +class CCPU; + +/** + * @class CInstruction + * + * Abstract class for displays + */ +class CInstruction +{ + public: + /** + * @method CInstruction + * @brief Default ctor + * @param name name of instruction + * @return - + * @globalvars none + * @exception none + * @conditions none + */ + CInstruction(std::string name) + : m_name(name) + {} + + /** + * @method ~CInstruction + * @brief Default dtor + * @param - + * @return - + * @globalvars none + * @exception none + * @conditions none + */ + virtual ~CInstruction() + {} + + /** + * @method operator== + * @brief implementation of operator == + * @param name reference to std::string + * @return true if instructionname is name + * @globalvars none + * @exception none + * @conditions none + */ + virtual bool operator==(std::string& name) + { + return name == m_name; + } + + /** + * @method operator() + * @brief implementation of operator (CCPU) + * @param cpu pointer to cpu + * @return - + * @globalvars none + * @exception std::runtime_error + * @conditions none + */ + virtual CInstruction& operator()(CCPU *cpu) + { + execute(cpu); + return *this; + } + + /** + * @method getName + * @brief returns instruction name + * @param - + * @return name of instruction + * @globalvars none + * @exception none + * @conditions none + */ + virtual const std::string& getName() + { + return m_name; + } + + /** + * @method dump + * @brief dumps information about instruction to outputstream + * @param stream outputstream + * @return reference to outputstream + * @globalvars none + * @exception none + * @conditions none + */ + virtual std::ostream& dump(std::ostream& stream) + { + stream << m_name; + return stream; + } + + /** + * @method operator<< + * @brief Shift/output operator for outputstream + * @param stream reference to outputstream + * @param instr object which will be printed to stream + * @return reference to outputstream + * @globalvars none + * @exception none + * @conditions none + */ + friend std::ostream& operator<<(std::ostream& stream, CInstruction& instr) + { + return instr.dump(stream); + } + + /** + * @method parseRegister + * @brief parses register syntax Rx (e.g. "R1") + * @param str register in assembler syntax + * @return registernumber + * @globalvars none + * @exception std::runtime_error + * @conditions none + */ + virtual const unsigned parseRegister(const std::string& str); + + /** + * @method checkRegister + * @brief performs a register boundary check + * does the register exist in cpu? + * @param cpu pointer to cpu + * @param regidx registernumber + * @return - + * @globalvars none + * @exception std::runtime_error + * @conditions none + */ + virtual void checkRegister(CCPU *cpu, const unsigned regidx); + + /** + * @method factory + * @brief creates a new instance of this instruction + * @param - + * @return new instruction instance + * @globalvars none + * @exception none + * @conditions none + */ + virtual CInstruction *factory() = 0; + + /** + * @method compile + * @brief parses instruction parameters and prepares the + * instruction for executing + * @param params list of parameters of this instruction + * @return - + * @globalvars none + * @exception std::runtime_error + * @conditions none + */ + virtual void compile(std::list& params) = 0; + + /** + * @method execute + * @brief executes the instruction + * @param cpu pointer to cpu + * @return - + * @globalvars none + * @exception std::runtime_error + * @conditions none + */ + virtual void execute(CCPU *cpu) = 0; + + protected: + /* members */ + /** name of instruction */ + std::string m_name; +}; + +#endif + +/* vim: set et sw=2 ts=2: */ diff --git a/ue4/mycpu/cmem.h b/ue4/mycpu/cmem.h new file mode 100644 index 0000000..5045c34 --- /dev/null +++ b/ue4/mycpu/cmem.h @@ -0,0 +1,109 @@ +/** + * @module cmem + * @author Guenther Neuwirth (0626638), Manuel Mausz (0728348) + * @brief Memory template and memory definition for CCPU + * @date 10.05.2009 + */ + +#ifndef CMEM_H +#define CMEM_H 1 + +#include +#include +#include +#include +#include +#ifdef DEBUG +# include +# include +#endif +#include "cdat.h" + +/** + * @class CVectorMem + * + * Extends std::vector template for use as memory for CCPU. + */ +template > +class CVectorMem + : public std::vector +{ + public: + using std::vector::size; + using std::vector::at; + + /** + * @method initialize + * @brief initialize the vector with the content of istream. istream is + * read per line. empty lines will add unitialized elements. + * @param in inputstream to read from + * @return void + * @globalvars none + * @exception std::runtime_error + * @conditions none + */ + void initialize(std::istream& in) + { + if (!in.good()) + return; + + std::string line; + unsigned i = 0; + while (!in.eof() && in.good()) + { + ++i; + std::getline(in, line); + + /* skip last line if it's empty */ + if (line.empty() && in.eof()) + break; + + T value; + try + { + if (!line.empty()) + value = boost::lexical_cast(line); + } + catch(boost::bad_lexical_cast& ex) + { + std::stringstream sstr; + sstr << "Unable to convert input (line " << i << "): " << ex.what(); + throw std::runtime_error(sstr.str()); + } + + push_back(value); + } + } + +#if DEBUG + /** + * @method dump + * @brief dumps contents of vector to outputstream + * @param out outputstream to write to + * @return void + * @globalvars none + * @exception none + * @conditions none + */ + void dump(std::ostream& out) + { + out << "[MEMORY DUMP]" << std::endl; + for(unsigned i = 0; i < size(); ++i) + { + out << "[" << std::setw(4) << std::setfill('0') << i << "] " + << at(i) << std::endl; + } + } +#endif +}; + +/** + * @class CMem + * + * Memory definition for CCPU + */ +typedef CVectorMem CMem; + +#endif + +/* vim: set et sw=2 ts=2: */ diff --git a/ue4/mycpu/cprogram.cpp b/ue4/mycpu/cprogram.cpp new file mode 100644 index 0000000..3fcf734 --- /dev/null +++ b/ue4/mycpu/cprogram.cpp @@ -0,0 +1,161 @@ +/** + * @module cprogram + * @author Guenther Neuwirth (0626638), Manuel Mausz (0728348) + * @brief CProgram extends std::vector and adds a method for parsing programfile + * @date 12.05.2009 + */ + +#include +#include +#ifdef DEBUG +# include +# include +#endif +#include "cprogram.h" +#include "instructions.h" + +using namespace std; + +CProgram::CProgram() +{ + m_instrset.insert(new CInstructionInc); + m_instrset.insert(new CInstructionDec); + m_instrset.insert(new CInstructionAdd); + m_instrset.insert(new CInstructionSub); + m_instrset.insert(new CInstructionMul); + m_instrset.insert(new CInstructionDiv); + m_instrset.insert(new CInstructionLoad); + m_instrset.insert(new CInstructionStore); + m_instrset.insert(new CInstructionTest); + m_instrset.insert(new CInstructionLabel); + m_instrset.insert(new CInstructionJumpA); + m_instrset.insert(new CInstructionJumpZ); + m_instrset.insert(new CInstructionJumpS); + m_instrset.insert(new CInstructionWrite); +} + +/*----------------------------------------------------------------------------*/ + +CProgram::~CProgram() +{ + /* free instruction set */ + set::iterator it; + for (it = m_instrset.begin(); it != m_instrset.end(); ++it) + delete *it; + + /* free instruction */ + for (iterator it = begin(); it != end(); ++it) + delete *it; +} + +/*----------------------------------------------------------------------------*/ + +void CProgram::compile(std::istream& in) +{ + if (!in.good()) + return; + + string line; + unsigned i = 0; + while (!in.eof() && in.good()) + { + ++i; + + /* read stream per line */ + getline(in, line); + if (line.empty()) + continue; + + boost::trim(line); + boost::to_lower(line); + + /* ignore comments */ + if (line.find_first_of('#') == 0) + continue; + + /* get instruction name */ + size_t pos = line.find_first_of(' '); + string instrname(line.substr(0, pos)); + + /* search and create instruction */ + CInstruction *instrptr = NULL; + set::iterator it; + for (it = m_instrset.begin(); it != m_instrset.end(); ++it) + { + if (*(*it) == instrname) + { + instrptr = *it; + break; + } + } + if (instrptr == NULL) + { + stringstream sstr; + sstr << "Unknown instruction '" << instrname << "' on line " << i << "."; + throw runtime_error(sstr.str()); + } + + /* create instruction */ + CInstruction *instr = instrptr->factory(); + + /* parse instruction parameters */ + string params = (pos == string::npos) ? "" : line.substr(pos + 1); + boost::trim(params); + list instrparams; + boost::split(instrparams, params, boost::is_any_of(", \t"), boost::token_compress_on); + + /* let instruction parse the parameters. catch+throw exception */ + try + { + /* handle label instruction ourself, but still add a dummy instruction */ + if (instrname == "label") + { + if (instrparams.size() != 1) + throw runtime_error("Invalid paramater count - must be 1"); + string label(instrparams.front()); + if (label.length() < 2 || label[ label.length() - 1] != ':') + throw runtime_error("Label has invalid syntax"); + m_labels[ label.substr(0, label.length() - 1) ] = size(); + } + instr->compile(instrparams); + } + catch(runtime_error& ex) + { + stringstream sstr; + sstr << "Unable to compile instruction '" << instrname + << "' (line " << i << "): " << ex.what(); + throw runtime_error(sstr.str()); + } + + push_back(instr); + } +} + +/*----------------------------------------------------------------------------*/ + +unsigned CProgram::findLabel(const std::string& label) const +{ + map::const_iterator it; + it = m_labels.find(label); + if (it == m_labels.end()) + throw runtime_error("Unknown label '" + label + "'"); + return it->second; +} + +/*----------------------------------------------------------------------------*/ + +#if DEBUG +void CProgram::dump(std::ostream& out) +{ + out << "[PROGRAM DUMP]" << endl; + unsigned i = 0; + for(iterator it = begin(); it < end(); ++it) + { + out << "[" << std::setw(4) << std::setfill('0') << i << "] " + << *(*it) << endl; + ++i; + } +} +#endif + +/* vim: set et sw=2 ts=2: */ diff --git a/ue4/mycpu/cprogram.h b/ue4/mycpu/cprogram.h new file mode 100644 index 0000000..27e7647 --- /dev/null +++ b/ue4/mycpu/cprogram.h @@ -0,0 +1,106 @@ +/** + * @module cprogram + * @author Guenther Neuwirth (0626638), Manuel Mausz (0728348) + * @brief CProgram extends std::vector and adds a method for parsing programfile + * @date 10.05.2009 + */ + +#ifndef CPROGRAM_H +#define CPROGRAM_H 1 + +#include +#include +#include +#include "cinstruction.h" + +/** + * @class CProgram + * + * CProgram extends std::vector and adds a method for parsing + * programfile. This adds instances of CInstruction to CProgram itself. + */ +class CProgram + : public std::vector +{ + public: + /** + * @method CProgram + * @brief Default ctor + * @param - + * @return - + * @globalvars none + * @exception none + * @conditions none + */ + CProgram(); + + /** + * @method ~CProgram + * @brief Default dtor + * @param - + * @return - + * @globalvars none + * @exception none + * @conditions none + */ + ~CProgram(); + + /** + * @method getLabels + * @brief get reference to labels map + * @param - + * @return reference to labels map + * @globalvars none + * @exception none + * @conditions none + */ + const std::map& getLabels() const + { + return m_labels; + } + + /** + * @method findLabel + * @brief search for label + * @param label name of label to search for + * @return index of found label in program + * @globalvars none + * @exception std::runtime_error + * @conditions none + */ + unsigned findLabel(const std::string& label) const; + + /** + * @method compile + * @brief create instructions from parsing stream + * @param in inputstream to read from + * @return void + * @globalvars none + * @exception std::runtime_error + * @conditions none + */ + void compile(std::istream& in); + +#if DEBUG + /** + * @method dump + * @brief dumps contents to outputstream + * @param out outputstream to write to + * @return void + * @globalvars none + * @exception none + * @conditions none + */ + void dump(std::ostream& out); +#endif + + private: + /* members */ + /** set of known instructions */ + std::set m_instrset; + std::map m_labels; +}; + +#endif + +/* vim: set et sw=2 ts=2: */ diff --git a/ue4/mycpu/displays.h b/ue4/mycpu/displays.h new file mode 100644 index 0000000..d4f3f36 --- /dev/null +++ b/ue4/mycpu/displays.h @@ -0,0 +1,76 @@ +/** + * @module displays + * @author Guenther Neuwirth (0626638), Manuel Mausz (0728348) + * @brief Implementations of CDisplay + * @date 10.05.2009 + */ + +#ifndef DISPLAYS_H +#define DISPLAYS_H 1 + +#include +#include "cdisplay.h" + +/** + * @class CDisplayWDEZ + * + * Implementation of CDisplay + * Prints CDat to stdout as decimal + */ +class CDisplayWDEZ + : public CDisplay +{ + public: + CDisplayWDEZ() + : CDisplay("wdez") + {} + + /** + * @method display + * @brief prints value to display + * @param value value to display + * @return - + * @globalvars none + * @exception none + * @conditions none + */ + void display(const CDat &value) + { + std::cout << std::dec << value << std::endl; + } +}; + +/*============================================================================*/ + +/** + * @class CDisplayWHEX + * + * Implementation of CDisplay + * Prints CDat to stdout as decimal + */ +class CDisplayWHEX + : public CDisplay +{ + public: + CDisplayWHEX() + : CDisplay("whex") + {} + + /** + * @method display + * @brief prints value to display + * @param value value to display + * @return - + * @globalvars none + * @exception none + * @conditions none + */ + void display(const CDat &value) + { + std::cout << std::hex << value << std::endl; + } +}; + +#endif + +/* vim: set et sw=2 ts=2: */ diff --git a/ue4/mycpu/instructions.cpp b/ue4/mycpu/instructions.cpp new file mode 100644 index 0000000..ef9e944 --- /dev/null +++ b/ue4/mycpu/instructions.cpp @@ -0,0 +1,341 @@ +/** + * @module instructions + * @author Guenther Neuwirth (0626638), Manuel Mausz (0728348) + * @brief Implementations of CInstruction + * @date 10.05.2009 + */ + +#include +#include +#include "instructions.h" + +using namespace std; + +void CInstructionInc::compile(std::list& params) +{ + if (params.size() != 1) + throw runtime_error("Invalid paramater count - must be 1"); + m_regidx1 = parseRegister(params.front()); + params.pop_front(); +} + +/*----------------------------------------------------------------------------*/ + +void CInstructionInc::execute(CCPU *cpu) +{ + assert(cpu != NULL); + assert(cpu->getRegisters() != NULL); + checkRegister(cpu, m_regidx1); + cpu->getRegisters()[ m_regidx1 ]++; +} + +/*============================================================================*/ + +void CInstructionDec::compile(std::list& params) +{ + if (params.size() != 1) + throw runtime_error("Invalid paramater count - must be 1"); + m_regidx1 = parseRegister(params.front()); + params.pop_front(); +} + +/*----------------------------------------------------------------------------*/ + +void CInstructionDec::execute(CCPU *cpu) +{ + assert(cpu != NULL); + assert(cpu->getRegisters() != NULL); + checkRegister(cpu, m_regidx1); + cpu->getRegisters()[ m_regidx1 ]--; +} + +/*============================================================================*/ + +void CInstructionAdd::compile(std::list& params) +{ + if (params.size() != 3) + throw runtime_error("Invalid paramater count - must be 3"); + m_regidx1 = parseRegister(params.front()); + params.pop_front(); + m_regidx2 = parseRegister(params.front()); + params.pop_front(); + m_regidx3 = parseRegister(params.front()); + params.pop_front(); +} + +/*----------------------------------------------------------------------------*/ + +void CInstructionAdd::execute(CCPU *cpu) +{ + assert(cpu != NULL); + assert(cpu->getRegisters() != NULL); + checkRegister(cpu, m_regidx1); + checkRegister(cpu, m_regidx2); + checkRegister(cpu, m_regidx3); + cpu->getRegisters()[ m_regidx1 ] = cpu->getRegisters()[ m_regidx2 ] + + cpu->getRegisters()[ m_regidx3 ]; +} + +/*============================================================================*/ + +void CInstructionSub::compile(std::list& params) +{ + if (params.size() != 3) + throw runtime_error("Invalid paramater count - must be 3"); + m_regidx1 = parseRegister(params.front()); + params.pop_front(); + m_regidx2 = parseRegister(params.front()); + params.pop_front(); + m_regidx3 = parseRegister(params.front()); + params.pop_front(); +} + +/*----------------------------------------------------------------------------*/ + +void CInstructionSub::execute(CCPU *cpu) +{ + assert(cpu != NULL); + assert(cpu->getRegisters() != NULL); + checkRegister(cpu, m_regidx1); + checkRegister(cpu, m_regidx2); + checkRegister(cpu, m_regidx3); + cpu->getRegisters()[ m_regidx1 ] = cpu->getRegisters()[ m_regidx2 ] + - cpu->getRegisters()[ m_regidx3 ]; +} + +/*============================================================================*/ + +void CInstructionMul::compile(std::list& params) +{ + if (params.size() != 3) + throw runtime_error("Invalid paramater count - must be 3"); + m_regidx1 = parseRegister(params.front()); + params.pop_front(); + m_regidx2 = parseRegister(params.front()); + params.pop_front(); + m_regidx3 = parseRegister(params.front()); + params.pop_front(); +} + +/*----------------------------------------------------------------------------*/ + +void CInstructionMul::execute(CCPU *cpu) +{ + checkRegister(cpu, m_regidx1); + checkRegister(cpu, m_regidx2); + checkRegister(cpu, m_regidx3); + cpu->getRegisters()[ m_regidx1 ] = cpu->getRegisters()[ m_regidx2 ] + * cpu->getRegisters()[ m_regidx3 ]; +} + +/*============================================================================*/ + +void CInstructionDiv::compile(std::list& params) +{ + if (params.size() != 3) + throw runtime_error("Invalid paramater count - must be 3"); + m_regidx1 = parseRegister(params.front()); + params.pop_front(); + m_regidx2 = parseRegister(params.front()); + params.pop_front(); + m_regidx3 = parseRegister(params.front()); + params.pop_front(); +} + +/*----------------------------------------------------------------------------*/ + +void CInstructionDiv::execute(CCPU *cpu) +{ + assert(cpu != NULL); + assert(cpu->getRegisters() != NULL); + checkRegister(cpu, m_regidx1); + checkRegister(cpu, m_regidx2); + checkRegister(cpu, m_regidx3); + cpu->getRegisters()[ m_regidx1 ] = cpu->getRegisters()[ m_regidx2 ] + / cpu->getRegisters()[ m_regidx3 ]; +} + +/*============================================================================*/ + +void CInstructionLoad::compile(std::list& params) +{ + if (params.size() != 2) + throw runtime_error("Invalid paramater count - must be 2"); + m_regidx1 = parseRegister(params.front()); + params.pop_front(); + m_regidx2 = parseRegister(params.front()); + params.pop_front(); +} + +/*----------------------------------------------------------------------------*/ + +void CInstructionLoad::execute(CCPU *cpu) +{ + assert(cpu != NULL); + assert(cpu->getRegisters() != NULL); + assert(cpu->getMemory() != NULL); + checkRegister(cpu, m_regidx1); + checkRegister(cpu, m_regidx2); + CDat val(cpu->getRegisters()[ m_regidx2 ]); + cpu->getRegisters()[ m_regidx1 ] = (*cpu->getMemory())[ val ]; +} + +/*============================================================================*/ + +void CInstructionStore::compile(std::list& params) +{ + if (params.size() != 2) + throw runtime_error("Invalid paramater count - must be 2"); + m_regidx1 = parseRegister(params.front()); + params.pop_front(); + m_regidx2 = parseRegister(params.front()); + params.pop_front(); +} + +/*----------------------------------------------------------------------------*/ + +void CInstructionStore::execute(CCPU *cpu) +{ + assert(cpu != NULL); + assert(cpu->getRegisters() != NULL); + assert(cpu->getMemory() != NULL); + checkRegister(cpu, m_regidx1); + checkRegister(cpu, m_regidx2); + CDat val(cpu->getRegisters()[ m_regidx2 ]); + (*cpu->getMemory())[ val ] = cpu->getRegisters()[ m_regidx1 ]; +} + +/*============================================================================*/ + +void CInstructionTest::compile(std::list& params) +{ + if (params.size() != 1) + throw runtime_error("Invalid paramater count - must be 1"); + m_regidx1 = parseRegister(params.front()); + params.pop_front(); +} + +/*----------------------------------------------------------------------------*/ + +void CInstructionTest::execute(CCPU *cpu) +{ + assert(cpu != NULL); + assert(cpu->getRegisters() != NULL); + checkRegister(cpu, m_regidx1); + if (cpu->getRegisters()[ m_regidx1 ] == CDat(0)) + cpu->setFlagZero(true); + if (cpu->getRegisters()[ m_regidx1 ] < CDat(0)) + cpu->setFlagSign(true); +} + +/*============================================================================*/ + +void CInstructionJumpA::compile(std::list& params) +{ + if (params.size() != 1) + throw runtime_error("Invalid paramater count - must be 1"); + m_addr = params.front(); + params.pop_front(); +} + +/*----------------------------------------------------------------------------*/ + +void CInstructionJumpA::execute(CCPU *cpu) +{ + assert(cpu != NULL); + assert(cpu->getRegisters() != NULL); + assert(cpu->getProgram() != NULL); + if (m_addr.empty()) + throw runtime_error("Empty address"); + cpu->getRegisters()[ 0 ] = cpu->getProgram()->findLabel(m_addr); +} + +/*============================================================================*/ + +void CInstructionJumpZ::compile(std::list& params) +{ + if (params.size() != 1) + throw runtime_error("Invalid paramater count - must be 1"); + m_addr = params.front(); + params.pop_front(); +} + +/*----------------------------------------------------------------------------*/ + +void CInstructionJumpZ::execute(CCPU *cpu) +{ + assert(cpu != NULL); + assert(cpu->getRegisters() != NULL); + assert(cpu->getProgram() != NULL); + if (!cpu->getFlagZero()) + return; + if (m_addr.empty()) + throw runtime_error("Empty address"); + cpu->getRegisters()[ 0 ] = cpu->getProgram()->findLabel(m_addr); +} + +/*============================================================================*/ + +void CInstructionJumpS::compile(std::list& params) +{ + if (params.size() != 1) + throw runtime_error("Invalid paramater count - must be 1"); + m_addr = params.front(); + params.pop_front(); +} + +/*----------------------------------------------------------------------------*/ + +void CInstructionJumpS::execute(CCPU *cpu) +{ + assert(cpu != NULL); + assert(cpu->getRegisters() != NULL); + assert(cpu->getProgram() != NULL); + if (!cpu->getFlagSign()) + return; + if (m_addr.empty()) + throw runtime_error("Empty address"); + cpu->getRegisters()[ 0 ] = cpu->getProgram()->findLabel(m_addr); +} + +/*============================================================================*/ + +void CInstructionWrite::compile(std::list& params) +{ + if (params.size() != 2) + throw runtime_error("Invalid paramater count - must be 2"); + m_dev = params.front(); + params.pop_front(); + m_regidx1 = parseRegister(params.front()); + params.pop_front(); +} + +/*----------------------------------------------------------------------------*/ + +void CInstructionWrite::execute(CCPU *cpu) +{ + assert(cpu != NULL); + assert(cpu->getRegisters() != NULL); + checkRegister(cpu, m_regidx1); + if (m_dev.empty()) + throw runtime_error("Empty device"); + + CDisplay *display = NULL; + std::set displays = cpu->getDisplays(); + std::set::iterator it; + for(it = displays.begin(); it != displays.end(); ++it) + { + if ((*it)->getName() == m_dev) + { + display = *it; + break; + } + } + if (display == NULL) + throw runtime_error("Unknown display"); + + display->display(cpu->getRegisters()[ m_regidx1 ]); +} + + +/* vim: set et sw=2 ts=2: */ diff --git a/ue4/mycpu/instructions.h b/ue4/mycpu/instructions.h new file mode 100644 index 0000000..4c36562 --- /dev/null +++ b/ue4/mycpu/instructions.h @@ -0,0 +1,454 @@ +/** + * @module instructions + * @author Guenther Neuwirth (0626638), Manuel Mausz (0728348) + * @brief Implementations of CInstruction + * @date 10.05.2009 + */ + +#ifndef INSTRUCTIONS_H +#define INSTRUCTIONS_H 1 + +#include "cinstruction.h" +#include "ccpu.h" + +/** + * @class CInstructionInc + * + * Implementation of assembler command "inc" + * Syntax: inc R1 + * (R1++) + */ +class CInstructionInc + : public CInstruction +{ + public: + CInstructionInc() + : CInstruction("inc") + {} + + CInstructionInc *factory() + { + return new CInstructionInc; + } + + void compile(std::list& params); + void execute(CCPU *cpu); + + protected: + /** register number */ + unsigned m_regidx1; +}; + +/*============================================================================*/ + +/** + * @class CInstructionDec + * + * Implementation of assembler command "dec" + * Syntax: dec R1 + * (R1--) + */ +class CInstructionDec + : public CInstruction +{ + public: + CInstructionDec() + : CInstruction("dec") + {} + + CInstructionDec *factory() + { + return new CInstructionDec; + } + + void compile(std::list& params); + void execute(CCPU *cpu); + + protected: + /** register number */ + unsigned m_regidx1; +}; + +/*============================================================================*/ + +/** + * @class CInstructionAdd + * + * Implementation of assembler command "add" + * Syntax: add R1, R2, R3 + * (R1 = R2 + R3) + */ +class CInstructionAdd + : public CInstruction +{ + public: + CInstructionAdd() + : CInstruction("add") + {} + + CInstructionAdd *factory() + { + return new CInstructionAdd; + } + + void compile(std::list& params); + void execute(CCPU *cpu); + + protected: + /** register number */ + unsigned m_regidx1; + /** register number */ + unsigned m_regidx2; + /** register number */ + unsigned m_regidx3; +}; + +/*============================================================================*/ + +/** + * @class CInstructionSub + * + * Implementation of assembler command "sub" + * Syntax: sub R1, R2, R3 + * (R1 = R2 - R3) + */ +class CInstructionSub + : public CInstruction +{ + public: + CInstructionSub() + : CInstruction("sub") + {} + + CInstructionSub *factory() + { + return new CInstructionSub; + } + + void compile(std::list& params); + void execute(CCPU *cpu); + + protected: + /** register number */ + unsigned m_regidx1; + /** register number */ + unsigned m_regidx2; + /** register number */ + unsigned m_regidx3; +}; + +/*============================================================================*/ + +/** + * @class CInstructionMul + * + * Implementation of assembler command "mul" + * Syntax: mul R1, R2, R3 + * (R1 = R2 * R3) + */ +class CInstructionMul + : public CInstruction +{ + public: + CInstructionMul() + : CInstruction("mul") + {} + + CInstructionMul *factory() + { + return new CInstructionMul; + } + + void compile(std::list& params); + void execute(CCPU *cpu); + + protected: + /** register number */ + unsigned m_regidx1; + /** register number */ + unsigned m_regidx2; + /** register number */ + unsigned m_regidx3; +}; + +/*============================================================================*/ + +/** + * @class CInstructionDiv + * + * Implementation of assembler command "div" + * Syntax: div R1, R2, R3 + * (R1 = R2 / R3) + */ +class CInstructionDiv + : public CInstruction +{ + public: + CInstructionDiv() + : CInstruction("div") + {} + + CInstructionDiv *factory() + { + return new CInstructionDiv; + } + + void compile(std::list& params); + void execute(CCPU *cpu); + + protected: + /** register number */ + unsigned m_regidx1; + /** register number */ + unsigned m_regidx2; + /** register number */ + unsigned m_regidx3; +}; + +/*============================================================================*/ + +/** + * @class CInstructionLoad + * + * Implementation of assembler command "load" + * Syntax: load R1, R2 + * (R1 = memory[R2]) + */ +class CInstructionLoad + : public CInstruction +{ + public: + CInstructionLoad() + : CInstruction("load") + {} + + CInstructionLoad *factory() + { + return new CInstructionLoad; + } + + void compile(std::list& params); + void execute(CCPU *cpu); + + protected: + /** register number */ + unsigned m_regidx1; + /** register number */ + unsigned m_regidx2; +}; + +/*============================================================================*/ + +/** + * @class CInstructionStore + * + * Implementation of assembler command "store" + * Syntax: store R1, R2 + * (memory[R2] = R1) + */ +class CInstructionStore + : public CInstruction +{ + public: + CInstructionStore() + : CInstruction("store") + {} + + CInstructionStore *factory() + { + return new CInstructionStore; + } + + void compile(std::list& params); + void execute(CCPU *cpu); + + protected: + /** register number */ + unsigned m_regidx1; + /** register number */ + unsigned m_regidx2; +}; + +/*============================================================================*/ + +/** + * @class CInstructionTest + * + * Implementation of assembler command "test" + * Syntax: test R1 + * (R1 == 0: zeroflag: true, R1 < 0: signflag: true) + */ +class CInstructionTest + : public CInstruction +{ + public: + CInstructionTest() + : CInstruction("test") + {} + + CInstructionTest *factory() + { + return new CInstructionTest; + } + + void compile(std::list& params); + void execute(CCPU *cpu); + + protected: + /** register number */ + unsigned m_regidx1; +}; + +/*============================================================================*/ + +/** + * @class CInstructionLabel + * + * Implementation of assembler command "label" + * Syntax: label name: + */ +class CInstructionLabel + : public CInstruction +{ + public: + CInstructionLabel() + : CInstruction("label") + {} + + CInstructionLabel *factory() + { + return new CInstructionLabel; + } + + void compile(std::list& params) + {} + + void execute(CCPU *cpu) + {} +}; + +/*============================================================================*/ + +/** + * @class CInstructionJumpA + * + * Implementation of assembler command "jumpa" + * Syntax: jumpa labelname + * (jump to labelname) + */ +class CInstructionJumpA + : public CInstruction +{ + public: + CInstructionJumpA() + : CInstruction("jumpa"), m_addr("") + {} + + CInstructionJumpA *factory() + { + return new CInstructionJumpA; + } + + void compile(std::list& params); + void execute(CCPU *cpu); + + protected: + /** labelname */ + std::string m_addr; +}; + +/*============================================================================*/ + +/** + * @class CInstructionJumpZ + * + * Implementation of assembler command "jumpz" + * Syntax: jumpz labelname + * (jump to labelname if zeroflag) + */ +class CInstructionJumpZ + : public CInstruction +{ + public: + CInstructionJumpZ() + : CInstruction("jumpz"), m_addr("") + {} + + CInstructionJumpZ *factory() + { + return new CInstructionJumpZ; + } + + void compile(std::list& params); + void execute(CCPU *cpu); + + protected: + /** labelname */ + std::string m_addr; +}; + +/*============================================================================*/ + +/** + * @class CInstructionJumpS + * + * Implementation of assembler command "jumps" + * Syntax: jumps labelname + * (jump to labelname if signflag) + */ +class CInstructionJumpS + : public CInstruction +{ + public: + CInstructionJumpS() + : CInstruction("jumps"), m_addr("") + {} + + CInstructionJumpS *factory() + { + return new CInstructionJumpS; + } + + void compile(std::list& params); + void execute(CCPU *cpu); + + protected: + /** labelname */ + std::string m_addr; +}; + +/*============================================================================*/ + +/** + * @class CInstructionWrite + * + * Implementation of assembler command "write" + * Syntax: write DEV, R1 + * (write R1 to DEV, which is a name of a display) + */ +class CInstructionWrite + : public CInstruction +{ + public: + CInstructionWrite() + : CInstruction("write"), m_dev("") + {} + + CInstructionWrite *factory() + { + return new CInstructionWrite; + } + + void compile(std::list& params); + void execute(CCPU *cpu); + + protected: + /** register number */ + unsigned m_regidx1; + /** device name */ + std::string m_dev; +}; + +#endif + +/* vim: set et sw=2 ts=2: */ diff --git a/ue4/mycpu/mycpu.cpp b/ue4/mycpu/mycpu.cpp new file mode 100644 index 0000000..b25e721 --- /dev/null +++ b/ue4/mycpu/mycpu.cpp @@ -0,0 +1,155 @@ +/** + * @module mycpu + * @author Guenther Neuwirth (0626638), Manuel Mausz (0728348) + * @brief mycpu executes a programfile (in simple assembler) by parsing the + * programfile first. This creates a vector of instructions, which will + * be executed in linear order (except jumps) afterwards. In order to + * initialize the memory of the cpu before execution an optional + * memoryfile can be passed as commandline option. + * @date 13.05.2009 + * @par Exercise + * 4 + */ + +#include +#include +#include +#include +#include +#include "ccpu.h" +#include "cmem.h" +#include "cprogram.h" + +using namespace std; +namespace po = boost::program_options; + +/** + * @func main + * @brief program entry point + * @param argc standard parameter of main + * @param argv standard parameter of main + * @return 0 on success, not 0 otherwise + * @globalvars none + * @exception none + * @conditions none + * + * parse commandline options, create and initialize memory, + * create cprogram instance, which parses the programfile and + * execute CCPU::run() + * On error print error message to stderr. + * Unknown commandline options will print a usage message. + */ +int main(int argc, char* argv[]) +{ + string me(argv[0]); + + /* define commandline options */ + po::options_description desc("Allowed options"); + desc.add_options() + ("help,h", "this help message") + ("compile,c", po::value(), "input programfile") + ("memory,m", po::value(), "input memoryfile"); + + /* parse commandline options */ + po::variables_map vm; + try + { + po::store(po::parse_command_line(argc, argv, desc), vm); + po::notify(vm); + } + catch(po::error& ex) + { + cerr << me << ": Error: " << ex.what() << endl; + } + + /* print usage upon request or missing params */ + if (vm.count("help") || !vm.count("compile")) + { + cout << "Usage: " << me << " -c [-m ]" << endl; + cout << desc << endl; + return 0; + } + + /* create memory and optionally initialize memory from file */ + CMem memory; + if (vm.count("memory")) + { + string memoryfile(vm["memory"].as()); + ifstream file(memoryfile.c_str(), ios::in); + if (!file.is_open()) + { + cerr << me << ": Unable to open memoryfile '" << memoryfile << "' for reading." << endl; + return 1; + } + + try + { + memory.initialize(file); + file.close(); + } + catch(runtime_error& ex) + { + file.close(); + cerr << me << ": Error while reading from memoryfile:" << endl + << " " << ex.what() << endl; + return 1; + } + +#if DEBUG + memory.dump(cerr); +#endif + } + + /* create program instance */ + CProgram program; + string programfile(vm["compile"].as()); + ifstream file(programfile.c_str(), ios::in); + if (!file.is_open()) + { + cerr << me << ": Unable to open programfile '" << programfile << "' for reading." << endl; + return 1; + } + + try + { + program.compile(file); + file.close(); + } + catch(runtime_error& ex) + { + file.close(); + cerr << me << ": Error while compiling programfile:" << endl + << " " << ex.what() << endl; + return 1; + } + +#if DEBUG + program.dump(cerr); +#endif + + + /* create cpu and execute the program */ + try + { + CCPU cpu(256); + cpu.setMemory(&memory); + cpu.setProgram(&program); + cpu.run(); +#if DEBUG + //cpu.dumpRegisters(cerr); +#endif + } + catch(runtime_error& ex) + { + cerr << me << ": Error while executing program:" << endl + << " " << ex.what() << endl; +#if DEBUG + memory.dump(cerr); +#endif + return 1; + } + + return 0; +} + +/* vim: set et sw=2 ts=2: */ diff --git a/ue4/mycpu/test/test.sh b/ue4/mycpu/test/test.sh new file mode 100755 index 0000000..ff1076c --- /dev/null +++ b/ue4/mycpu/test/test.sh @@ -0,0 +1,42 @@ +#!/bin/bash + +binary="./mycpu" +tmpfile="test/tmpfile" +inputs=( $(ls test/* | grep _program | sort -n) ) + +for input in ${inputs[@]} +do + echo "Testing $input ..." + + programfile="$input" + args="-c $programfile" + memoryfile="${input/_program/_memory}" + reffile="${input/_program/_output}" + if [ -e "$memoryfile" ] + then + args+=" -m $memoryfile" + fi + + if [ ! -e "$reffile" ] + then + echo " ERROR: reference file $reffile doesn't exist" + exit 1 + fi + + rm -rf "$tmpfile" + echo " Executing $binary $args ..." + $binary $args > $tmpfile + + md5_1=$(md5sum < "$reffile") + md5_2=$(md5sum < "$tmpfile") + if [ "$md5_1" != "$md5_2" ] + then + echo " ERROR: output and $reffile differ" + diff -Naur "$reffile" "$tmpfile" + rm -rf "$tmpfile" + exit 1 + else + echo " SUCCESS" + fi + rm -rf "$tmpfile" +done diff --git a/ue4/mycpu/test/test1_memory b/ue4/mycpu/test/test1_memory new file mode 100644 index 0000000..209e3ef --- /dev/null +++ b/ue4/mycpu/test/test1_memory @@ -0,0 +1 @@ +20 diff --git a/ue4/mycpu/test/test1_output b/ue4/mycpu/test/test1_output new file mode 100644 index 0000000..ac30dc2 --- /dev/null +++ b/ue4/mycpu/test/test1_output @@ -0,0 +1,19 @@ +1 +2 +3 +4 +5 +6 +7 +8 +9 +10 +11 +12 +13 +14 +15 +16 +17 +18 +19 diff --git a/ue4/mycpu/test/test1_program b/ue4/mycpu/test/test1_program new file mode 100644 index 0000000..ae5e9d2 --- /dev/null +++ b/ue4/mycpu/test/test1_program @@ -0,0 +1,13 @@ +# set R2 = 10 +LOAD R2, R1 + +# start of loop +label Loop: +inc R3 +sub R4, R3, R2 +test R4 +jumpz EndLoop +write WDEZ, R3 +jumpa Loop + +label EndLoop: -- cgit v1.2.3