From e41c92320aef4a54b7f3b323f7302b180019b6d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCnther=20Neuwirth?= Date: Tue, 12 May 2009 19:53:07 +0200 Subject: some changes --- ue3/mycpu/ccpu.cpp | 93 ++++++++++++++++++---------------------------- ue3/mycpu/ccpu.h | 5 +-- ue3/mycpu/cinstruction.cpp | 22 +++++++---- ue3/mycpu/cinstruction.h | 85 +++++++++++++++++++++++------------------- ue3/mycpu/cmem.cpp | 5 ++- ue3/mycpu/cmem.h | 3 +- ue3/mycpu/test/test_prog | 2 +- 7 files changed, 104 insertions(+), 111 deletions(-) diff --git a/ue3/mycpu/ccpu.cpp b/ue3/mycpu/ccpu.cpp index 72ad96c..978f1f7 100644 --- a/ue3/mycpu/ccpu.cpp +++ b/ue3/mycpu/ccpu.cpp @@ -21,77 +21,56 @@ using namespace boost; CCPU::CCPU(const std::string& progfile, const std::string& memfile) : m_program(progfile), m_memory(memfile) { - m_instrtype["inc"] = new CInc(); - m_instrtype["dec"] = new CDec(); - m_instrtype["add"] = new CAdd(); - m_instrtype["sub"] = new CSub(); - m_instrtype["mul"] = new CMul(); - m_instrtype["div"] = new CDiv(); - m_instrtype["load"] = new CLoad(); - m_instrtype["store"] = new CStore(); - m_instrtype["test"] = new CTest(); - m_instrtype["label"] = new CLabel(); - m_instrtype["jumpa"] = new CJumpa(m_program.getJumpAddrs()); - m_instrtype["jumpz"] = new CJumpz(m_program.getJumpAddrs()); - m_instrtype["jumps"] = new CJumps(m_program.getJumpAddrs()); - m_instrtype["write"] = new CWrite(); + f_zero = false; + f_sign = false; + m_instrHandler["inc"] = new CInc(); + m_instrHandler["dec"] = new CDec(); + m_instrHandler["add"] = new CAdd(); + m_instrHandler["sub"] = new CSub(); + m_instrHandler["mul"] = new CMul(); + m_instrHandler["div"] = new CDiv(); + m_instrHandler["load"] = new CLoad(); + m_instrHandler["store"] = new CStore(); + m_instrHandler["test"] = new CTest(f_zero, f_sign); + m_instrHandler["label"] = new CLabel(); + m_instrHandler["jumpa"] = new CJumpa(m_program.getJumpAddrs()); + m_instrHandler["jumpz"] = new CJumpz(f_zero, f_sign, m_program.getJumpAddrs()); + m_instrHandler["jumps"] = new CJumps(f_zero, f_sign, m_program.getJumpAddrs()); + m_instrHandler["write"] = new CWrite(); } - +CCPU::~CCPU() +{ + std::map::iterator it; + for (it = m_instrHandler.begin(); it != m_instrHandler.end(); it++) + delete (*it).second ; +} void CCPU::proceed() { - + while (m_memory.getRegister("R0") < m_program.getMaxProgramCount()) { std::vector& i_list = m_program.getInstruction( m_memory.getRegister("R0") ); - /* switch (m_instrtype[i_list[0]]) - { - case INC: - cout << "fick mich"<< endl<exec(m_memory, i_list); +/* for(int i = 0; i < (int)i_list.size(); i++) cout << i_list[i] << " "; - m_memory.getRegister("R0")++; - cout << m_memory.getRegister("R0")<< endl<& i_list) -{ - -} diff --git a/ue3/mycpu/ccpu.h b/ue3/mycpu/ccpu.h index 6b16ba3..68a4b8d 100644 --- a/ue3/mycpu/ccpu.h +++ b/ue3/mycpu/ccpu.h @@ -52,8 +52,7 @@ class CCPU * @exception none * @conditions none */ - ~CCPU() - {} + ~CCPU(); void proceed(); @@ -66,7 +65,7 @@ class CCPU CProgram m_program; CMem m_memory; - std::map m_instrtype; + std::map m_instrHandler; bool f_zero, f_sign; // std::string m_memfiler; diff --git a/ue3/mycpu/cinstruction.cpp b/ue3/mycpu/cinstruction.cpp index 2c8dedf..3cbb033 100644 --- a/ue3/mycpu/cinstruction.cpp +++ b/ue3/mycpu/cinstruction.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include "cinstruction.h" @@ -28,6 +29,7 @@ void CSub::exec(CMem& mem, vector& instr) { mem.getRegister(instr[1]) = mem.getRegister(instr[2]) - mem.getRegister(instr[3]); + } @@ -47,7 +49,9 @@ void CDiv::exec(CMem& mem, vector& instr) void CLoad::exec(CMem& mem, vector& instr) { - mem.getRegister(instr[1]) = mem.getMem(instr[2]); + istringstream stmp ("22"); + stmp >> mem.getRegister(instr[1]); + cout <& instr) } -void CTest::test(CMem& mem, vector& instr, bool& f_zero, bool& f_sign) +void CTest::exec(CMem& mem, vector& instr) { if(mem.getRegister(instr[1]) == 0) f_zero = true; else f_zero = false; - if(mem.getRegister(instr[1]) <= 0) + if(mem.getRegister(instr[1]) < 0) f_sign = true; else f_sign = false; + } @@ -76,26 +81,27 @@ void CTest::test(CMem& mem, vector& instr, bool& f_zero, bool& f_sign) void CJumpa::exec(CMem& mem, vector& instr) { - mem.getRegister(instr[0]) = (int) m_jumpaddr[instr[1]]; + mem.getRegister("R0") = (int) m_jumpaddr[instr[1]]; } -void CJumpz::exec(CMem& mem, vector& instr, bool& f_zero) +void CJumpz::exec(CMem& mem, vector& instr) { if(f_zero) - mem.getRegister(instr[0]) = (int) m_jumpaddr[instr[1]]; + mem.getRegister("R0") = (int) m_jumpaddr[instr[1]]; } -void CJumps::exec(CMem& mem, vector& instr, bool& f_sign) +void CJumps::exec(CMem& mem, vector& instr) { if(f_sign) - mem.getRegister(instr[0]) = (int) m_jumpaddr[instr[1]]; + mem.getRegister("R0") = (int) m_jumpaddr[instr[1]]; } void CWrite::exec(CMem& mem, vector& instr) { + if(instr[1] == "WDEZ") cout << mem.getRegister(instr[2]) << endl; else if (instr[1] == "WHEX") diff --git a/ue3/mycpu/cinstruction.h b/ue3/mycpu/cinstruction.h index d393e09..7d929a5 100644 --- a/ue3/mycpu/cinstruction.h +++ b/ue3/mycpu/cinstruction.h @@ -30,8 +30,11 @@ class CInstruction { public: - - + CInstruction() + {} + CInstruction(std::map& jumpaddr) + : m_jumpaddr(jumpaddr) + {} /** * @method ~CInstruction @@ -48,7 +51,8 @@ class CInstruction virtual void exec(CMem& mem, std::vector& instr) = 0; - + protected: + std::map m_jumpaddr; }; @@ -59,12 +63,18 @@ class CInstruction class CFlagInstruction : public CInstruction { public: + public: + CFlagInstruction(bool& zero, bool& sign) + : f_zero(zero), f_sign(sign) + {} + CFlagInstruction(bool& zero, bool& sign, std::map& jumpaddr) + : CInstruction::CInstruction(jumpaddr), f_zero(zero), f_sign(sign) + {} + virtual void exec(CMem& mem, std::vector& instr) = 0; - void exec(CMem& mem, std::vector& instr) - {} - virtual void exec(CMem& mem, std::vector& instr, bool& flag) = 0; - - + protected: + bool &f_zero, &f_sign; + }; @@ -72,14 +82,14 @@ class CFlagInstruction : public CInstruction class CInc : public CInstruction { public: - virtual void exec(CMem& mem, std::vector& instr); + void exec(CMem& mem, std::vector& instr); }; class CDec : public CInstruction { public: - virtual void exec(CMem& mem, std::vector& instr); + void exec(CMem& mem, std::vector& instr); }; @@ -87,7 +97,7 @@ class CDec : public CInstruction class CAdd : public CInstruction { public: - virtual void exec(CMem& mem, std::vector& instr); + void exec(CMem& mem, std::vector& instr); }; @@ -95,7 +105,7 @@ class CAdd : public CInstruction class CSub : public CInstruction { public: - virtual void exec(CMem& mem, std::vector& instr); + void exec(CMem& mem, std::vector& instr); }; @@ -103,7 +113,7 @@ class CSub : public CInstruction class CMul : public CInstruction { public: - virtual void exec(CMem& mem, std::vector& instr); + void exec(CMem& mem, std::vector& instr); }; @@ -111,7 +121,7 @@ class CMul : public CInstruction class CDiv : public CInstruction { public: - virtual void exec(CMem& mem, std::vector& instr); + void exec(CMem& mem, std::vector& instr); }; @@ -119,7 +129,7 @@ class CDiv : public CInstruction class CLoad : public CInstruction { public: - virtual void exec(CMem& mem, std::vector& instr); + void exec(CMem& mem, std::vector& instr); }; @@ -127,16 +137,17 @@ class CLoad : public CInstruction class CStore : public CInstruction { public: - virtual void exec(CMem& mem, std::vector& instr); + void exec(CMem& mem, std::vector& instr); }; class CTest : public CFlagInstruction { - public: - void exec(CMem& mem, std::vector& instr, bool& flag) + public: + CTest(bool& zero, bool& sign) + : CFlagInstruction::CFlagInstruction(zero, sign) {} - virtual void test(CMem& mem, std::vector& instr, - bool& f_zero, bool& f_sign); + + void exec(CMem& mem, std::vector& instr); }; @@ -152,36 +163,32 @@ class CLabel : public CInstruction class CJumpa : public CInstruction { public: - CJumpa(std::map& jumpaddr) - : m_jumpaddr(jumpaddr) - {} - virtual void exec(CMem& mem, std::vector& instr); - protected: - std::map m_jumpaddr; + CJumpa(std::map& jumpaddr) + : CInstruction::CInstruction(jumpaddr) + {} + void exec(CMem& mem, std::vector& instr); + }; class CJumpz : public CFlagInstruction { + public: - CJumpz(std::map& jumpaddr) - : m_jumpaddr(jumpaddr) - {} - virtual void exec(CMem& mem, std::vector& instr, bool& f_zero); - protected: - std::map m_jumpaddr; + CJumpz(bool& zero, bool& sign, std::map& jumpaddr) + : CFlagInstruction::CFlagInstruction(zero, sign, jumpaddr) + {} + void exec(CMem& mem, std::vector& instr); }; class CJumps : public CFlagInstruction { public: - CJumps(std::map& jumpaddr) - : m_jumpaddr(jumpaddr) - {} - virtual void exec(CMem& mem, std::vector& instr, bool& f_sign); - protected: - std::map m_jumpaddr; + CJumps(bool& zero ,bool& sign, std::map& jumpaddr) + : CFlagInstruction::CFlagInstruction(zero, sign, jumpaddr) + {} + void exec(CMem& mem, std::vector& instr); }; @@ -189,7 +196,7 @@ class CJumps : public CFlagInstruction class CWrite : public CInstruction { public: - virtual void exec(CMem& mem, std::vector& instr); + void exec(CMem& mem, std::vector& instr); }; #endif diff --git a/ue3/mycpu/cmem.cpp b/ue3/mycpu/cmem.cpp index d27f74e..ec60b56 100644 --- a/ue3/mycpu/cmem.cpp +++ b/ue3/mycpu/cmem.cpp @@ -46,9 +46,10 @@ CDat& CMem::getRegister(const string reg) // if (regnr >= MAX_REGISTER ) - if (regnr == m_registers.size()) + if (regnr >= m_registers.size()) { - m_registers.push_back(CDat((int)0)); + for ( int i = m_registers.size(); i <= (int)regnr; i++) + m_registers.push_back(CDat((int)0)); return m_registers[m_registers.size() - 1]; } diff --git a/ue3/mycpu/cmem.h b/ue3/mycpu/cmem.h index 923ed01..06fa876 100644 --- a/ue3/mycpu/cmem.h +++ b/ue3/mycpu/cmem.h @@ -62,7 +62,8 @@ class CMem CDat& getMem(const std::string addr) { - return getRegister("R0"); + getRegister("R255") = 10; + return getRegister("R255"); } void setMem(const std::string addr, const CDat& value) {} diff --git a/ue3/mycpu/test/test_prog b/ue3/mycpu/test/test_prog index 7c5f929..74dac21 100644 --- a/ue3/mycpu/test/test_prog +++ b/ue3/mycpu/test/test_prog @@ -1,5 +1,5 @@ # set R2 = 10 -load R1, R2 +load R2, R1 # start of loop label Loop: -- cgit v1.2.3