diff --git a/src/Makefile b/src/Makefile index e56dc622..90592ddf 100644 --- a/src/Makefile +++ b/src/Makefile @@ -5,7 +5,7 @@ CXXFLAGS=-g -fPIC LIB_OBJS=args.o obj.o mem.o core.o instruction.o enc.o util.o lex.yy.o -all: harptool libharplib.so libharplib.a libqsim-harp.so +all: harptool libharplib.so libharplib.a #libqsim-harp.so # Use -static so we don't have to install the library in order to just run # Harptool. @@ -19,19 +19,27 @@ libharplib.a: $(LIB_OBJS) ar rcs $@ $(LIB_OBJS) args.o : args.cpp include/args.h -enc.o : enc.cpp include/types.h include/util.h include/enc.h include/archdef.h include/instruction.h -harptool.o : harptool.cpp include/types.h include/core.h include/enc.h include/instruction.h include/mem.h include/obj.h include/archdef.h include/args.h include/help.h +enc.o : enc.cpp include/types.h include/util.h include/enc.h include/archdef.h\ + include/instruction.h +harptool.o : harptool.cpp include/types.h include/core.h include/enc.h \ + include/instruction.h include/mem.h include/obj.h \ + include/archdef.h include/args.h include/help.h instruction.o : instruction.cpp include/instruction.h include/obj.h -obj.o : obj.cpp include/types.h include/obj.h include/util.h include/asm-tokens.h +obj.o : obj.cpp include/types.h include/obj.h include/util.h \ + include/asm-tokens.h util.o : util.cpp include/types.h include/util.h mem.o : mem.cpp include/types.h include/util.h include/mem.h -core.o : core.cpp include/types.h include/util.h include/mem.h include/archdef.h +core.o : core.cpp include/types.h include/util.h include/mem.h \ + include/debug.h include/archdef.h QSIM_CXXFLAGS=-DEMU_INSTRUMENTATION -libqsim-harp.so: args.cpp enc.cpp instruction.cpp obj.cpp util.cpp mem.cpp core.cpp qsim-harp.cpp lex.yy.o include/qsim-harp.h include/types.h include/core.h include/util.h include/enc.h include/archdef.h include/instruction.h include/asm-tokens.h include/mem.h - $(CXX) $(CXXFLAGS) $(QSIM_CXXFLAGS) -shared -o $@ args.cpp enc.cpp instruction.cpp obj.cpp util.cpp mem.cpp core.cpp qsim-harp.cpp lex.yy.o - +libqsim-harp.so: args.cpp enc.cpp instruction.cpp obj.cpp util.cpp mem.cpp \ + core.cpp qsim-harp.cpp lex.yy.o include/qsim-harp.h \ + include/types.h include/core.h include/util.h include/enc.h \ + include/archdef.h include/instruction.h include/asm-tokens.h \ + include/mem.h + $(CXX) $(CXXFLAGS) $(QSIM_CXXFLAGS) -shared -o $@ $^ lex.yy.cc: scanner.lex flex scanner.lex diff --git a/src/core.cpp b/src/core.cpp index a80f6eda..4746b1c3 100644 --- a/src/core.cpp +++ b/src/core.cpp @@ -10,6 +10,7 @@ #include "include/mem.h" #include "include/enc.h" #include "include/core.h" +#include "include/debug.h" #ifdef EMU_INSTRUMENTATION #include "include/qsim-harp.h" @@ -36,18 +37,13 @@ Core::Core(const ArchDef &a, Decoder &d, MemoryUnit &mem, Word id) : /* Build the register file. */ Word regNum(0); for (Word j = 0; j < a.getNThds(); ++j) { - std::cout << "Pushing back a new register vector.\n"; reg.push_back(vector >(0)); for (Word i = 0; i < a.getNRegs(); ++i) { - std::cout << "Pushing back a new register in thread " << j << "\n"; reg[j].push_back(Reg(id, regNum++)); } pred.push_back(vector >(0)); - std::cout << "getnpregs returns " << a.getNPRegs() << '\n'; for (Word i = 0; i < a.getNPRegs(); ++i) { - std::cout << "Pushing back predicate reg " << i << ", thread " << j - << "\n"; pred[j].push_back(Reg(id, regNum++)); } } @@ -62,7 +58,7 @@ void Core::step() { if (activeThreads == 0) return; - //cout << "in step pc=0x" << hex << pc << '\n'; + D(3, "in step pc=0x" << hex << pc); /* Fetch and decode. */ if (wordSize < sizeof(pc)) pc &= ((1ll<<(wordSize*8))-1); @@ -72,13 +68,14 @@ void Core::step() { /* Todo: speed this up for the byte encoder? */ try { fetchMore = false; - fetchBuffer.resize(fetchPos + wordSize); + unsigned fetchSize(wordSize - (pc+fetchPos)%wordSize); + fetchBuffer.resize(fetchPos + fetchSize); Word fetched = mem.fetch(pc + fetchPos, supervisorMode); - writeWord(fetchBuffer, fetchPos, wordSize, fetched); + writeWord(fetchBuffer, fetchPos, fetchSize, fetched); decPos = 0; inst = iDec.decode(fetchBuffer, decPos); } catch (OutOfBytes o) { - //cout << "Caught OutOfBytes. Fetching more.\n"; + D(3, "Caught OutOfBytes. Fetching more."); fetchMore = true; } catch (MemoryUnit::PageFault pf) { fetchPos = 0; @@ -87,9 +84,8 @@ void Core::step() { interrupt(pf.notFound?1:2); } } while (fetchMore); - //cout << "Fetched at 0x" << hex << pc << '\n'; - //cout << "0x" << hex << pc << ": " << *inst << '\n'; - //cout << "sizeof(core)=" << dec << sizeof(*this) << '\n'; + D(3, "Fetched at 0x" << hex << pc); + D(3, "0x" << hex << pc << ": " << *inst); #ifdef EMU_INSTRUMENTATION { Addr pcPhys(mem.virtToPhys(pc)); diff --git a/src/enc.cpp b/src/enc.cpp index 4b44fc32..cc959e4f 100644 --- a/src/enc.cpp +++ b/src/enc.cpp @@ -8,6 +8,8 @@ #include #include + + #include "include/types.h" #include "include/util.h" #include "include/enc.h" diff --git a/src/include/debug.h b/src/include/debug.h new file mode 100644 index 00000000..9f83f763 --- /dev/null +++ b/src/include/debug.h @@ -0,0 +1,23 @@ +/******************************************************************************* + HARPtools by Chad D. Kersey, Spring 2013 +*******************************************************************************/ +#ifndef __DEBUG_H +#define __DEBUG_H + +#ifdef USE_DEBUG +#include + +#define D(lvl, x) do { \ + using namespace std; \ + if ((lvl) <= USE_DEBUG) { \ + cout << "DEBUG " << __FILE__ << ':' << dec << __LINE__ << ": " \ + << x << endl; \ + } \ +} while(0) +#else + +#define D(lvl, x) do {} while(0) + +#endif + +#endif diff --git a/src/include/mem.h b/src/include/mem.h index 34a2733b..7b93cb95 100644 --- a/src/include/mem.h +++ b/src/include/mem.h @@ -4,6 +4,7 @@ #ifndef __MEM_H #define __MEM_H +#include #include #include #include diff --git a/src/include/obj.h b/src/include/obj.h index 8db3fd8b..7dc19403 100644 --- a/src/include/obj.h +++ b/src/include/obj.h @@ -80,7 +80,9 @@ namespace Harp { return; noFit: - std::cout << "Attempt to bind a symbol to an address it cannot reach.\n"; + std::cout << "Attempt to bind a " << bits << "-bit " + << (relative?"":"non-") << "relative symbol to an address" + " it cannot reach.\n"; exit(1); } diff --git a/src/instruction.cpp b/src/instruction.cpp index 092baf84..41cc9e67 100644 --- a/src/instruction.cpp +++ b/src/instruction.cpp @@ -8,6 +8,7 @@ #include "include/obj.h" #include "include/core.h" #include "include/harpfloat.h" +#include "include/debug.h" #ifdef EMU_INSTRUMENTATION #include "include/qsim-harp.h" @@ -63,7 +64,7 @@ Instruction::InstTableEntry Instruction::instTable[] = { {"andp", false, false, false, false, AC_3PREG, ITYPE_INTBASIC}, {"orp", false, false, false, false, AC_3PREG, ITYPE_INTBASIC}, {"xorp", false, false, false, false, AC_3PREG, ITYPE_INTBASIC}, - {"notp", false, false, false, false, AC_3PREG, ITYPE_INTBASIC}, + {"notp", false, false, false, false, AC_2PREG, ITYPE_INTBASIC}, {"isneg", false, false, false, false, AC_PREG_REG, ITYPE_INTBASIC}, {"iszero", false, false, false, false, AC_PREG_REG, ITYPE_INTBASIC}, {"halt", false, false, false, true, AC_NONE, ITYPE_NULL }, @@ -106,6 +107,8 @@ ostream &Harp::operator<<(ostream& os, Instruction &inst) { } void Instruction::executeOn(Core &c) { + D(1, "Begin instruction execute."); + /* If I try to execute a privileged instruction in user mode, throw an exception 3. */ if (instTable[op].privileged && !c.supervisorMode) { @@ -262,5 +265,7 @@ void Instruction::executeOn(Core &c) { if (instTable[op].controlFlow) break; } + D(3, "End instruction execute."); + c.activeThreads = nextActiveThreads; } diff --git a/src/mem.cpp b/src/mem.cpp index 59caef77..732e7402 100644 --- a/src/mem.cpp +++ b/src/mem.cpp @@ -9,6 +9,7 @@ #include #include +#include "include/debug.h" #include "include/types.h" #include "include/util.h" #include "include/mem.h" @@ -39,12 +40,14 @@ void RomMemDevice::write(Addr, Word) { } Word RamMemDevice::read(Addr addr) { - Word w = readWord(contents, addr, wordSize); + D(2, "RAM read, addr=0x" << hex << addr); + Word w = readWord(contents, addr, wordSize - addr%wordSize); return w; } void RamMemDevice::write(Addr addr, Word w) { - writeWord(contents, addr, wordSize, w); + D(2, "RAM write, addr=0x" << hex << addr); + writeWord(contents, addr, wordSize - addr%wordSize, w); } MemDevice &MemoryUnit::ADecoder::doLookup(Addr a, Size &bit) { @@ -104,8 +107,12 @@ MemoryUnit::TLBEntry MemoryUnit::tlbLookup(Addr vAddr, Word flagMask) { if ((i = tlb.find(vAddr/pageSize)) != tlb.end()) { TLBEntry &t = i->second; if (t.flags & flagMask) return t; - else throw PageFault(vAddr, false); + else { + D(2, "Page fault on addr 0x" << hex << vAddr << "(bad flags)"); + throw PageFault(vAddr, false); + } } else { + D(2, "Page fault on addr 0x" << hex << vAddr << "(not in TLB)"); throw PageFault(vAddr, true); } } @@ -139,7 +146,7 @@ void MemoryUnit::write(Addr vAddr, Word w, bool sup) { } void MemoryUnit::tlbAdd(Addr virt, Addr phys, Word flags) { - cout << "tlbAdd(0x" << hex << virt << ", 0x" << phys << ", 0x" << flags << ")\n"; + D(1, "tlbAdd(0x" << hex << virt << ", 0x" << phys << ", 0x" << flags << ')'); tlb[virt/pageSize] = TLBEntry(phys/pageSize, flags); } diff --git a/src/test/Makefile b/src/test/Makefile index 1fac654d..e7cf5831 100644 --- a/src/test/Makefile +++ b/src/test/Makefile @@ -1,38 +1,44 @@ HARPLD = ../harptool -L HARPAS = ../harptool -A HARPEM = ../harptool -E -4WARCH = 6w16/2/2 +4BARCH = 4b16/16/2 -all: sieve.bin 2thread.bin sieve.4w.bin 2thread.4w.bin +all: simple.bin sieve.bin 2thread.bin simple.4b.bin sieve.4b.bin 2thread.4b.bin -run: sieve.out 2thread.out sieve.4w.out 2thread.4w.out +run: simple.out sieve.out 2thread.out simple.4b.out sieve.4b.out 2thread.4b.out -%.4w.out : %.4w.bin - $(HARPEM) -a $(4WARCH) -c $< > $@ +%.4b.out : %.4b.bin + $(HARPEM) -a $(4BARCH) -c $< > $@ %.out : %.bin $(HARPEM) -c $< > $@ -2thread.bin : lib.HOF 2thread.HOF boot.HOF - $(HARPLD) -o 2thread.bin boot.HOF 2thread.HOF lib.HOF +2thread.bin : boot.HOF lib.HOF 2thread.HOF + $(HARPLD) -o 2thread.bin $^ -2thread.4w.bin : lib.4w.HOF 2thread.4w.HOF boot.4w.HOF - $(HARPLD) --arch $(4WARCH) -o 2thread.4w.bin boot.4w.HOF 2thread.4w.HOF 2thread.4w.HOF lib.4w.HOF +2thread.4b.bin : boot.4b.HOF lib.4b.HOF 2thread.4b.HOF + $(HARPLD) --arch $(4BARCH) -o 2thread.4b.bin $^ -sieve.bin : lib.HOF sieve.HOF boot.HOF - $(HARPLD) -o sieve.bin boot.HOF sieve.HOF lib.HOF +simple.bin : boot.HOF lib.HOF simple.HOF + $(HARPLD) -o $@ $^ -sieve.4w.bin : lib.4w.HOF sieve.4w.HOF boot.4w.HOF - $(HARPLD) --arch $(4WARCH) -o sieve.4w.bin boot.4w.HOF sieve.4w.HOF lib.4w.HOF +sieve.bin : boot.HOF lib.HOF sieve.HOF + $(HARPLD) -o $@ $^ -%.4w.bin : %.4w.HOF - $(HARPLD) --arch $(4WARCH) -o $@ $< +simple.4b.bin : boot.4b.HOF lib.4b.HOF simple.4b.HOF + $(HARPLD) --arch $(4BARCH) -o $@ $^ + +sieve.4b.bin : boot.4b.HOF lib.4b.HOF sieve.4b.HOF + $(HARPLD) --arch $(4BARCH) -o $@ $^ + +%.4b.bin : %.4b.HOF + $(HARPLD) --arch $(4BARCH) -o $@ $< %.bin : %.HOF $(HARPLD) -o $@ $< -%.4w.HOF : %.s - $(HARPAS) --arch $(4WARCH) -o $@ $< +%.4b.HOF : %.s + $(HARPAS) --arch $(4BARCH) -o $@ $< %.HOF : %.s $(HARPAS) -o $@ $< diff --git a/src/test/sieve.s b/src/test/sieve.s index 311251da..82c5dc9b 100644 --- a/src/test/sieve.s +++ b/src/test/sieve.s @@ -74,5 +74,5 @@ loop4: ld %r1, %r0, array; .global .word array 0 /* Basically, 0 and 1 are pre-cleared. */ -.word _0 0 /* I would love to not have to give this a name. */ -.word SIZE-2 +.word _0 0 /* Given a name, contents are zero. */ +.word 0x1ffe /* Empty space of size SIZE-2 bytes. */