From 815c827247bc211e4785016985666fbfd201f423 Mon Sep 17 00:00:00 2001 From: Martin Perner Date: Sun, 31 Oct 2010 22:44:29 +0100 Subject: [PATCH] sim: arith. fixes, optimized mem access --- 3b_sim/Makefile | 4 ++-- 3b_sim/ccpu.cpp | 8 ++++---- 3b_sim/ccpu.hpp | 3 +++ 3b_sim/cdat.hpp | 8 +++++--- 3b_sim/cmem.cpp | 35 ++++++++++++++++++++++++++++++++++- 3b_sim/cmem.hpp | 5 +++++ 3b_sim/cpmem.cpp | 5 +++-- 3b_sim/cpmem.hpp | 4 +++- 3b_sim/sim.cpp | 8 +++++--- 3c_disasm/Iinstr.hpp | 18 +++++++++--------- 3c_disasm/Makefile | 2 +- 11 files changed, 74 insertions(+), 26 deletions(-) diff --git a/3b_sim/Makefile b/3b_sim/Makefile index e6d4b04..c4272a6 100644 --- a/3b_sim/Makefile +++ b/3b_sim/Makefile @@ -13,7 +13,7 @@ $(PROG): $(sources:.cpp=.o) -include $(sources:.cpp=.d) .PHONY: libs libs: -# $(MAKE) -C instr all + $(MAKE) -C instr all $(sources:.cpp=.o): $(CC) $(CPPFLAGS) -c -o ${@} ${@:.o=.cpp} @@ -21,7 +21,7 @@ $(sources:.cpp=.o): .PHONY:clean clean: rm -rf $(PROG) $(sources:.cpp=.o) $(sources:.cpp=.d) -# $(MAKE) -C instr clean + $(MAKE) -C instr clean .PHONY: run run: $(PROG) diff --git a/3b_sim/ccpu.cpp b/3b_sim/ccpu.cpp index 148fe81..4974fcc 100644 --- a/3b_sim/ccpu.cpp +++ b/3b_sim/ccpu.cpp @@ -112,8 +112,8 @@ CDat CCpu::getFlags() const { void CCpu::updateFlags(CDat val) { this->m_Z = (val == 0); this->m_S = ((val >> (BIT_LEN-1)) & 0x1); - this->m_C = false; - this->m_O = false; +/* this->m_C = false; + this->m_O = false; */ } void CCpu::updateFlags(CDatd val, CDat a, CDat b) { @@ -134,12 +134,12 @@ void CCpu::updateFlags(bool z, bool o, bool c, bool s) CDat CCpu::getRegister(const int addr) const { - return m_reg.get(addr); + return m_reg.getDirect(addr); } void CCpu::setRegister(const int addr, CDat data) { - m_reg.set(addr, data); + m_reg.setDirect(addr, data); } CDat CCpu::getRAM(const int addr) const diff --git a/3b_sim/ccpu.hpp b/3b_sim/ccpu.hpp index bef4f9e..24f6f31 100644 --- a/3b_sim/ccpu.hpp +++ b/3b_sim/ccpu.hpp @@ -39,8 +39,11 @@ class CCpu { void setNextPC(CDat); CDat getFlags() const; + /* will only change zero and sign */ void updateFlags(CDat); + /* will change all flags */ void updateFlags(CDatd, CDat, CDat); + /* will change all flags */ void updateFlags(bool z, bool o, bool c, bool s); diff --git a/3b_sim/cdat.hpp b/3b_sim/cdat.hpp index 39ea074..b5d212b 100644 --- a/3b_sim/cdat.hpp +++ b/3b_sim/cdat.hpp @@ -7,7 +7,9 @@ typedef __UINT32_TYPE__ CDat; typedef __UINT64_TYPE__ CDatd; -#define BIT_LEN 32 -static_assert(sizeof(CDat) == 4, "The size of the datatype for int is NOT 4 bytes (32 Bit!)"); -static_assert(sizeof(CDatd) > 4, "The size of the datatype for double int is NOT bigger than 4 bytes (32 Bit!)"); +#define BYTE_COUNT 4 + +#define BIT_LEN (BYTE_COUNT * 8) +static_assert(sizeof(CDat) == BYTE_COUNT, "The size of the datatype for int is NOT 4 bytes (32 Bit!)"); +static_assert(sizeof(CDatd) > BYTE_COUNT, "The size of the datatype for double int is NOT bigger than 4 bytes (32 Bit!)"); #endif diff --git a/3b_sim/cmem.cpp b/3b_sim/cmem.cpp index 4b49a08..3b8010d 100644 --- a/3b_sim/cmem.cpp +++ b/3b_sim/cmem.cpp @@ -11,7 +11,7 @@ void CMem::set(const MEMORY_ADDRESS address, const T data) throw out_of_range(error.str()); } - MEMORY_ADDRESS temp = address; + MEMORY_ADDRESS temp = (address & (~(BYTE_COUNT-1))) / BYTE_COUNT; auto iter = m_memory.begin(); while(temp > 0) { ++iter; @@ -25,6 +25,39 @@ void CMem::set(const MEMORY_ADDRESS address, const T data) template T CMem::get(const MEMORY_ADDRESS address) const +{ + if(address >= MAX_MEMORY) { + stringstream error; + error << "memoryaddress " << address << " out of range"; + throw out_of_range(error.str()); + } + MEMORY_ADDRESS temp = (address & (~(BYTE_COUNT-1))) / BYTE_COUNT; + return m_memory[temp]; +} + +template +void CMem::setDirect(const MEMORY_ADDRESS address, const T data) +{ + if(address >= MAX_MEMORY) { + stringstream error; + error << "memoryaddress " << address << " out of range"; + throw out_of_range(error.str()); + } + + MEMORY_ADDRESS temp = address; + auto iter = m_memory.begin(); + while(temp > 0) { + ++iter; + temp--; + } + + iter = m_memory.insert(iter, data); + ++iter; + m_memory.erase(iter); +} + +template +T CMem::getDirect(const MEMORY_ADDRESS address) const { if(address >= MAX_MEMORY) { stringstream error; diff --git a/3b_sim/cmem.hpp b/3b_sim/cmem.hpp index 05739db..8e348c7 100644 --- a/3b_sim/cmem.hpp +++ b/3b_sim/cmem.hpp @@ -21,8 +21,13 @@ private: const int MAX_MEMORY; std::vector m_memory; public: + /* aligns to BIT_LEN words, aka. does calc direct memorycell from address */ void set(const MEMORY_ADDRESS address, const T data); T get(const MEMORY_ADDRESS address) const; + + /* doesn't align, user has to do */ + void setDirect(const MEMORY_ADDRESS address, const T data); + T getDirect(const MEMORY_ADDRESS address) const; CMem(int size) : MAX_MEMORY(size), m_memory(size) {}; }; diff --git a/3b_sim/cpmem.cpp b/3b_sim/cpmem.cpp index 31205e6..7972aa8 100644 --- a/3b_sim/cpmem.cpp +++ b/3b_sim/cpmem.cpp @@ -18,7 +18,7 @@ void CPMem::set(const MEMORY_ADDRESS address, const T data) throw out_of_range(error.str()); } - MEMORY_ADDRESS temp = address; + MEMORY_ADDRESS temp = (address & (~(BYTE_COUNT-1))) / BYTE_COUNT; auto iter = m_memory.begin(); while(temp > 0) { ++iter; @@ -38,7 +38,8 @@ T CPMem::get(const MEMORY_ADDRESS address) const error << "memoryaddress " << address << " out of range"; throw out_of_range(error.str()); } - return m_memory[address]; + MEMORY_ADDRESS temp = (address & (~(BYTE_COUNT-1))) / BYTE_COUNT; + return m_memory[temp]; } template diff --git a/3b_sim/cpmem.hpp b/3b_sim/cpmem.hpp index fd09f3c..6b64a0e 100644 --- a/3b_sim/cpmem.hpp +++ b/3b_sim/cpmem.hpp @@ -21,7 +21,8 @@ template class CPMem { private: - //MAX_MEMORY-1 zugreifbare Speicherzellen + //MAX_MEMORY-1 zugreifbare Speicherzellen aus BYTE_COUNT Bytes + //adressierung erfolgt aber byteweise const int MAX_MEMORY; std::vector m_memory; public: @@ -30,6 +31,7 @@ public: void set(const MEMORY_ADDRESS address, const T data); //retuniert referenz eines cdat objekts mit dem Wert von address T get(const MEMORY_ADDRESS address) const; + T getByte(const MEMORY_ADDRESS address) const; CPMem(int size) : MAX_MEMORY(size), m_memory(size, NULL) {}; ~CPMem(); }; diff --git a/3b_sim/sim.cpp b/3b_sim/sim.cpp index bd7f5d0..4fe52b0 100644 --- a/3b_sim/sim.cpp +++ b/3b_sim/sim.cpp @@ -302,6 +302,7 @@ void printStatus(const vector&) CDat stackd = global_cpu->getRAM(stackp); cout << "Stack pointer: " << stackp << " @stackpointer: " << stackd << endl; cout << "PSW: 0x" << std::hex << setw(8) << setfill('0') << global_cpu->getFlags() << std::dec << endl; + cout << "cur PC: 0x" << std::hex << setw(8) << setfill('0') << global_cpu->getCurPC() << " next PC: 0x" << setw(8) << setfill('0') << global_cpu->getNextPC() << std::dec << endl; } @@ -445,6 +446,7 @@ int main(int argc, char* argv[]) case 2: case 3: cerr << "ignoring labels and comments for now" << endl; + break; default: cerr << "i was to lazy to implement the other format types for now" << endl; } @@ -494,9 +496,9 @@ int main(int argc, char* argv[]) Completers.push_back(CompleterElement("quit", &doExit, "Exits program")); Completers.push_back(CompleterElement("exit", &doExit, "Exits program")); Completers.push_back(CompleterElement("step [count]",&execStep, "Runs [count] ticks. if count is not given one tick is executed.")); - Completers.push_back(CompleterElement("dre [s] [e]",&printReg, "Prints registers. if s is given, only register s is printed. if s and e are given the registers from s to e are printed. if omitted all registers a printed.")); - Completers.push_back(CompleterElement("dra [s] [e]",&printRAM, "Prints RAM. if s is given, only RAM-Addr. s is printed. if s and e are given the RAM-Addrs from s to e are printed. if omitted the first 16 RAM-Addrs are printed.")); - Completers.push_back(CompleterElement("dpr [s] [e]",&printPROG, "Prints program. if s is given, only Prog-Addr. s is printed. if s and e are given the Prog-Addrs from s to e are printed. if omitted the first 16 Prog-Addrs are printed.")); + Completers.push_back(CompleterElement("dreg [s] [e]",&printReg, "Prints registers. if s is given, only register s is printed. if s and e are given the registers from s to e are printed. if omitted all registers a printed.")); + Completers.push_back(CompleterElement("ddata [s] [e]",&printRAM, "Prints RAM. if s is given, only RAM-Addr. s is printed. if s and e are given the RAM-Addrs from s to e are printed. if omitted the first 16 RAM-Addrs are printed.")); + Completers.push_back(CompleterElement("dprog [s] [e]",&printPROG, "Prints program. if s is given, only Prog-Addr. s is printed. if s and e are given the Prog-Addrs from s to e are printed. if omitted the first 16 Prog-Addrs are printed.")); Completers.push_back(CompleterElement("break addr",&setBreak, "Sets a breakpoint for address addr.")); Completers.push_back(CompleterElement("run",&execRun, "Runs till next breakpoint or end of program.")); Completers.push_back(CompleterElement("setpc [num]",&setPC, "Sets PC to num. if num is omitted 0 is used.")); diff --git a/3c_disasm/Iinstr.hpp b/3c_disasm/Iinstr.hpp index 416db47..d94c88f 100644 --- a/3c_disasm/Iinstr.hpp +++ b/3c_disasm/Iinstr.hpp @@ -51,13 +51,13 @@ class Iinstr { std::string name; short m_ra, m_rb, m_rd; bool m_c, m_d, m_hl, m_f, m_s; - int m_imm; + CDat m_imm; short m_cond; boost::dynamic_bitset<> argbits; Iinstr() : opcode(0), name(""), m_ra(0), m_rb(0), m_rd(0), m_c(0), m_d(0), m_hl(0), m_f(0), m_s(0), m_imm(0), m_cond(ALWAYS), argbits(32) {} - int generate16ImmFill(const int value) const { - int i = value; + CDat generate16ImmFill(const CDat value) const { + CDat i = value; if(m_hl == true && m_f == true) { i <<= 16; i |= 0x0000FFFF; @@ -76,8 +76,8 @@ class Iinstr { return i; } - int generate16ImmSign(const int value) const { - int i = value; + CDat generate16ImmSign(const CDat value) const { + CDat i = value; if(m_hl == true) { i <<= 16; } @@ -88,8 +88,8 @@ class Iinstr { return i; } - int generate12ImmSign(const int value) const { - int i = value; + CDat generate12ImmSign(const CDat value) const { + CDat i = value; if(m_s == true && (i & 0x0800) != 0) { i |= 0xFFFFF000; } @@ -97,8 +97,8 @@ class Iinstr { return i; } - int generate15ImmSign(const int value) const { - int i = value; + CDat generate15ImmSign(const CDat value) const { + CDat i = value; if(m_s == true && (i & 0x4000) != 0) { i |= 0xFFFF8000; } diff --git a/3c_disasm/Makefile b/3c_disasm/Makefile index 960717e..062b6fe 100644 --- a/3c_disasm/Makefile +++ b/3c_disasm/Makefile @@ -24,7 +24,7 @@ clean: $(MAKE) -C instr clean .PHONY: run -run: $(PROG) +run: all ./$(PROG) sum.dthex %.d: %.cpp -- 2.25.1