Started simX
This commit is contained in:
116
simX/include/archdef.h
Normal file
116
simX/include/archdef.h
Normal file
@@ -0,0 +1,116 @@
|
||||
/*******************************************************************************
|
||||
HARPtools by Chad D. Kersey, Summer 2011
|
||||
*******************************************************************************/
|
||||
#ifndef __ARCHDEF_H
|
||||
#define __ARCHDEF_H
|
||||
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
|
||||
#include <cstdlib>
|
||||
#include <stdio.h>
|
||||
#include "types.h"
|
||||
|
||||
namespace Harp {
|
||||
class ArchDef {
|
||||
public:
|
||||
struct Undefined {};
|
||||
|
||||
ArchDef(const std::string &s) {
|
||||
std::istringstream iss(s.c_str());
|
||||
|
||||
wordSize = 4;
|
||||
encChar = 'w';
|
||||
nRegs = 32;
|
||||
nPRegs = 0;
|
||||
nThds = 8;
|
||||
nWarps = 8;
|
||||
|
||||
extent = EXT_WARPS;
|
||||
|
||||
// if (!iss) { extent = EXT_NULL; return; }
|
||||
// iss >> encChar;
|
||||
// if (!iss) { extent = EXT_WORDSIZE; return; }
|
||||
// iss >> nRegs;
|
||||
// if (!iss) { extent = EXT_ENC; return; }
|
||||
// char sep;
|
||||
// iss >> sep >> nPRegs;
|
||||
// if (!iss || sep != '/') { extent = EXT_REGS; return; }
|
||||
// iss >> sep >> nThds;
|
||||
// if (!iss || sep != '/') { extent = EXT_PREGS; return; }
|
||||
// iss >> sep >> nWarps;
|
||||
// if (!iss || sep != '/') { extent = EXT_THDS; return; }
|
||||
// extent = EXT_WARPS;
|
||||
}
|
||||
|
||||
operator std::string () const {
|
||||
if (extent == EXT_NULL) return "";
|
||||
|
||||
std::ostringstream oss;
|
||||
if (extent >= EXT_WORDSIZE) oss << wordSize;
|
||||
if (extent >= EXT_ENC ) oss << encChar;
|
||||
if (extent >= EXT_REGS ) oss << nRegs;
|
||||
if (extent >= EXT_PREGS ) oss << '/' << nPRegs;
|
||||
if (extent >= EXT_THDS ) oss << '/' << nThds;
|
||||
if (extent >= EXT_WARPS ) oss << '/' << nWarps;
|
||||
|
||||
return oss.str();
|
||||
}
|
||||
|
||||
bool operator==(const ArchDef &r) const {
|
||||
Extent minExtent(r.extent > extent ? extent : r.extent);
|
||||
|
||||
// Can't be equal if we can't specify a binary encoding at all.
|
||||
if (minExtent < EXT_PREGS) return false;
|
||||
|
||||
if (minExtent >= EXT_WORDSIZE) { if (wordSize!=r.wordSize) return false; }
|
||||
if (minExtent >= EXT_ENC ) { if (encChar != r.encChar) return false; }
|
||||
if (minExtent >= EXT_REGS ) { if (nRegs != r.nRegs) return false; }
|
||||
if (minExtent >= EXT_PREGS ) { if (nPRegs != r.nPRegs) return false; }
|
||||
if (minExtent >= EXT_THDS ) { if (nThds != r.nThds) return false; }
|
||||
if (minExtent >= EXT_WARPS ) { if (nWarps != r.nWarps) return false; }
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool operator!=(const ArchDef &r) const { return !(*this == r); }
|
||||
|
||||
Size getWordSize() const {
|
||||
if (extent < EXT_WORDSIZE) throw Undefined(); else return wordSize;
|
||||
}
|
||||
|
||||
char getEncChar() const {
|
||||
if (extent<EXT_ENC||encChar=='x') throw Undefined(); else return encChar;
|
||||
}
|
||||
|
||||
RegNum getNRegs() const {
|
||||
if (extent < EXT_REGS) throw Undefined(); else return nRegs;
|
||||
}
|
||||
|
||||
RegNum getNPRegs() const {
|
||||
if (extent < EXT_PREGS) throw Undefined(); else return nPRegs;
|
||||
}
|
||||
|
||||
ThdNum getNThds() const {
|
||||
if (extent < EXT_THDS) throw Undefined(); else return nThds;
|
||||
}
|
||||
|
||||
ThdNum getNWarps() const {
|
||||
if (extent < EXT_WARPS) throw Undefined(); else return nWarps;
|
||||
}
|
||||
|
||||
private:
|
||||
enum Extent {
|
||||
EXT_NULL, EXT_WORDSIZE, EXT_ENC, EXT_REGS, EXT_PREGS, EXT_THDS, EXT_WARPS
|
||||
};
|
||||
|
||||
Extent extent;
|
||||
|
||||
Size wordSize;
|
||||
ThdNum nThds, nWarps;
|
||||
RegNum nRegs, nPRegs;
|
||||
char encChar;
|
||||
};
|
||||
};
|
||||
|
||||
#endif
|
||||
61
simX/include/args.h
Normal file
61
simX/include/args.h
Normal file
@@ -0,0 +1,61 @@
|
||||
/*******************************************************************************
|
||||
HARPtools by Chad D. Kersey, Summer 2011
|
||||
*******************************************************************************/
|
||||
#ifndef __ARGS_H
|
||||
#define __ARGS_H
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
#include <map>
|
||||
|
||||
namespace HarpTools {
|
||||
struct BadArg { BadArg(std::string s) : arg(s) {} std::string arg; };
|
||||
|
||||
class CommandLineArg {
|
||||
public:
|
||||
CommandLineArg(std::string s, std::string l, const char *helpText);
|
||||
CommandLineArg(std::string l, const char *helpText);
|
||||
virtual int read(int argc, char** argv) = 0;
|
||||
|
||||
static void readArgs(int argc, char **argv);
|
||||
static void clearArgs();
|
||||
static void showHelp(std::ostream &os);
|
||||
|
||||
private:
|
||||
static std::string helpString;
|
||||
static std::map<std::string, CommandLineArg *> longArgs;
|
||||
static std::map<std::string, CommandLineArg *> shortArgs;
|
||||
};
|
||||
|
||||
template <typename T> class CommandLineArgSetter : public CommandLineArg {
|
||||
public:
|
||||
CommandLineArgSetter(std::string s, std::string l, const char *ht, T &x) :
|
||||
CommandLineArg(s, l, ht), x(x) {}
|
||||
CommandLineArgSetter(std::string l, const char *ht, T &x) :
|
||||
CommandLineArg(l, ht), x(x) {}
|
||||
|
||||
int read(int argc, char **argv) {
|
||||
std::istringstream iss(argv[1]);
|
||||
iss >> x;
|
||||
return 1;
|
||||
}
|
||||
private:
|
||||
T &x;
|
||||
};
|
||||
|
||||
class CommandLineArgFlag : public CommandLineArg {
|
||||
public:
|
||||
CommandLineArgFlag(std::string s, std::string l, const char *ht, bool &x) :
|
||||
CommandLineArg(s, l, ht), x(x) { x = false; }
|
||||
CommandLineArgFlag(std::string l, const char *ht, bool &x) :
|
||||
CommandLineArg(l, ht), x(x) { x = false; }
|
||||
|
||||
int read(int argc, char **argv) { x = true; return 0; }
|
||||
private:
|
||||
bool &x;
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
16
simX/include/asm-tokens.h
Normal file
16
simX/include/asm-tokens.h
Normal file
@@ -0,0 +1,16 @@
|
||||
#ifndef HARPTOOL_ASM_TOKENS
|
||||
#define HARPTOOL_ASM_TOKENS
|
||||
|
||||
namespace HarpTools {
|
||||
enum AsmTokens {
|
||||
ASM_T_DIR_DEF = 1, ASM_T_DIR_PERM, ASM_T_DIR_BYTE, ASM_T_DIR_WORD,
|
||||
ASM_T_DIR_SPACE, ASM_T_DIR_STRING, ASM_T_DIR_ALIGN, ASM_T_DIR_ENTRY,
|
||||
ASM_T_DIR_GLOBAL, ASM_T_DIR_ARG_NUM, ASM_T_DIR_ARG_STRING,
|
||||
ASM_T_DIR_ARG_SYM, ASM_T_DIR_ARG_R, ASM_T_DIR_ARG_W, ASM_T_DIR_ARG_X,
|
||||
ASM_T_DIR_END, ASM_T_LABEL, ASM_T_PRED, ASM_T_INST,
|
||||
ASM_T_PREG, ASM_T_REG, ASM_T_REG_RA, ASM_T_REG_SP,
|
||||
ASM_T_REG_FP, ASM_T_LIT, ASM_T_SYM, ASM_T_PEXP
|
||||
};
|
||||
};
|
||||
|
||||
#endif
|
||||
166
simX/include/core.h
Normal file
166
simX/include/core.h
Normal file
@@ -0,0 +1,166 @@
|
||||
/*******************************************************************************
|
||||
HARPtools by Chad D. Kersey, Summer 2011
|
||||
*******************************************************************************/
|
||||
#ifndef __CORE_H
|
||||
#define __CORE_H
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <stack>
|
||||
#include <map>
|
||||
#include <set>
|
||||
|
||||
#include "types.h"
|
||||
#include "archdef.h"
|
||||
#include "enc.h"
|
||||
#include "mem.h"
|
||||
#include "debug.h"
|
||||
|
||||
#include "Vcache_simX.h"
|
||||
#include "verilated.h"
|
||||
|
||||
#ifdef VCD_OUTPUT
|
||||
#include <verilated_vcd_c.h>
|
||||
#endif
|
||||
|
||||
|
||||
#include "trace.h"
|
||||
|
||||
namespace Harp {
|
||||
|
||||
#ifdef EMU_INSTRUMENTATION
|
||||
void reg_doWrite(Word cpuId, Word regNum);
|
||||
void reg_doRead(Word cpuId, Word regNum);
|
||||
#endif
|
||||
|
||||
template <typename T> class Reg {
|
||||
public:
|
||||
Reg(): cpuId(0), regNum(0), val(0) {}
|
||||
Reg(Word c, Word n): cpuId(c), regNum(n), val(0) {}
|
||||
|
||||
Reg &operator=(T r) { if (regNum) {val = r; doWrite();} return *this; }
|
||||
|
||||
operator T() const { doRead(); return val; }
|
||||
|
||||
void trunc(Size s) {
|
||||
Word mask((~0ull >> (sizeof(Word)-s)*8));
|
||||
val &= mask;
|
||||
}
|
||||
|
||||
private:
|
||||
Word cpuId, regNum;
|
||||
T val;
|
||||
|
||||
#ifdef EMU_INSTRUMENTATION
|
||||
/* Access size here is 8, representing the register size of 64-bit cores. */
|
||||
void doWrite() const { reg_doWrite(cpuId, regNum); }
|
||||
void doRead() const { reg_doRead(cpuId, regNum); }
|
||||
#else
|
||||
void doWrite() const {}
|
||||
void doRead() const {}
|
||||
#endif
|
||||
};
|
||||
|
||||
// Entry in the IPDOM Stack
|
||||
struct DomStackEntry {
|
||||
DomStackEntry(
|
||||
unsigned p, const std::vector<std::vector<Reg<Word> > >& m,
|
||||
std::vector<bool> &tm, Word pc
|
||||
): pc(pc), fallThrough(false), uni(false)
|
||||
{
|
||||
std::cout << "DomStackEntry TMASK: ";
|
||||
for (unsigned i = 0; i < m.size(); ++i)
|
||||
{
|
||||
std::cout << " " << (!bool(m[i][p]) && tm[i]);
|
||||
tmask.push_back(!bool(m[i][p]) && tm[i]);
|
||||
}
|
||||
std::cout << "\n";
|
||||
}
|
||||
|
||||
DomStackEntry(const std::vector<bool> &tmask):
|
||||
tmask(tmask), fallThrough(true), uni(false) {}
|
||||
|
||||
bool fallThrough;
|
||||
bool uni;
|
||||
std::vector<bool> tmask;
|
||||
Word pc;
|
||||
};
|
||||
|
||||
class Warp;
|
||||
|
||||
class Core {
|
||||
public:
|
||||
Core(const ArchDef &a, Decoder &d, MemoryUnit &mem, Word id=0);
|
||||
|
||||
Vcache_simX * cache_simulator;
|
||||
|
||||
bool interrupt(Word r0);
|
||||
bool running() const;
|
||||
|
||||
void fetch();
|
||||
void decode();
|
||||
void scheduler();
|
||||
void gpr_read();
|
||||
void execute_unit();
|
||||
void load_store();
|
||||
|
||||
void step();
|
||||
|
||||
void printStats() const;
|
||||
|
||||
const ArchDef &a;
|
||||
Decoder &iDec;
|
||||
MemoryUnit &mem;
|
||||
|
||||
Word interruptEntry;
|
||||
|
||||
unsigned long steps;
|
||||
std::vector<Warp> w;
|
||||
std::map<Word, std::set<Warp *> > b; // Barriers
|
||||
};
|
||||
|
||||
class Warp {
|
||||
public:
|
||||
Warp(Core *c, Word id=0);
|
||||
|
||||
void step(trace_inst_t *);
|
||||
bool interrupt(Word r0);
|
||||
bool running() const { return activeThreads; }
|
||||
#ifdef EMU_INSTRUMENTATION
|
||||
bool getSupervisorMode() const { return supervisorMode; }
|
||||
#endif
|
||||
|
||||
void printStats() const;
|
||||
|
||||
struct MemAccess {
|
||||
MemAccess(bool w, Word a): wr(w), addr(a) {}
|
||||
bool wr;
|
||||
Word addr;
|
||||
};
|
||||
std::vector<MemAccess> memAccesses;
|
||||
|
||||
// private:
|
||||
Core *core;
|
||||
|
||||
Word pc, shadowPc, id;
|
||||
Size activeThreads, shadowActiveThreads;
|
||||
std::vector<std::vector<Reg<Word> > > reg;
|
||||
std::vector<std::vector<Reg<bool> > > pred;
|
||||
std::vector<Reg<uint16_t> > csr;
|
||||
|
||||
std::vector<bool> tmask, shadowTmask;
|
||||
std::stack<DomStackEntry> domStack;
|
||||
|
||||
std::vector<Word> shadowReg;
|
||||
std::vector<bool> shadowPReg;
|
||||
|
||||
bool interruptEnable, shadowInterruptEnable, supervisorMode,
|
||||
shadowSupervisorMode, spawned;
|
||||
|
||||
unsigned long steps, insts, loads, stores;
|
||||
|
||||
friend class Instruction;
|
||||
};
|
||||
};
|
||||
|
||||
#endif
|
||||
30
simX/include/debug.h
Normal file
30
simX/include/debug.h
Normal file
@@ -0,0 +1,30 @@
|
||||
/*******************************************************************************
|
||||
HARPtools by Chad D. Kersey, Spring 2013
|
||||
*******************************************************************************/
|
||||
#ifndef __DEBUG_H
|
||||
#define __DEBUG_H
|
||||
|
||||
// #define USE_DEBUG 9
|
||||
#define USE_DEBUG -1
|
||||
|
||||
#ifdef USE_DEBUG
|
||||
#include <iostream>
|
||||
|
||||
#define D(lvl, x) do { \
|
||||
using namespace std; \
|
||||
if ((lvl) <= USE_DEBUG) { \
|
||||
cout << "DEBUG " << __FILE__ << ':' << dec << __LINE__ << ": " \
|
||||
<< x << endl; \
|
||||
} \
|
||||
} while(0)
|
||||
|
||||
#define D_RAW(x) do { \
|
||||
std::cout << x; \
|
||||
} while (0)
|
||||
#else
|
||||
|
||||
#define D(lvl, x) do {} while(0)
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
73
simX/include/enc.h
Normal file
73
simX/include/enc.h
Normal file
@@ -0,0 +1,73 @@
|
||||
/*******************************************************************************
|
||||
HARPtools by Chad D. Kersey, Summer 2011
|
||||
*******************************************************************************/
|
||||
#ifndef __ENC_H
|
||||
#define __ENC_H
|
||||
|
||||
#include <map>
|
||||
|
||||
#include "types.h"
|
||||
#include "instruction.h"
|
||||
#include "obj.h"
|
||||
#include "trace.h"
|
||||
// } trace_inst_t;
|
||||
|
||||
namespace Harp {
|
||||
class DataChunk;
|
||||
class TextChunk;
|
||||
class Ref;
|
||||
|
||||
class Encoder {
|
||||
public:
|
||||
Encoder() {}
|
||||
virtual ~Encoder() {}
|
||||
|
||||
virtual Size encode(Ref *&ref, std::vector<Byte> &v, Size n,
|
||||
Instruction &i) = 0;
|
||||
void encodeChunk(DataChunk &dest, const TextChunk &src);
|
||||
};
|
||||
|
||||
class Decoder {
|
||||
public:
|
||||
Decoder() : haveRefs(false) {}
|
||||
Decoder(const std::vector<Ref*> &refVec) : haveRefs(true) {
|
||||
setRefs(refVec);
|
||||
}
|
||||
|
||||
virtual ~Decoder() {}
|
||||
|
||||
void setRefs(const std::vector<Ref*> &);
|
||||
void clearRefs() { refMap.clear(); }
|
||||
virtual Instruction *decode(const std::vector<Byte> &v, Size &n, trace_inst_t * trace_inst) = 0;
|
||||
virtual Instruction *decode(const std::vector<Byte> &v, Size &n) = 0;
|
||||
void decodeChunk(TextChunk &dest, const DataChunk &src);
|
||||
protected:
|
||||
bool haveRefs;
|
||||
std::map <Size, Ref*> refMap;
|
||||
};
|
||||
|
||||
class WordDecoder : public Decoder {
|
||||
public:
|
||||
WordDecoder(const ArchDef &);
|
||||
virtual Instruction *decode(const std::vector<Byte> &v, Size &n, trace_inst_t * trace_inst);
|
||||
virtual Instruction *decode(const std::vector<Byte> &v, Size &n) {printf("Not implemented\n");}
|
||||
|
||||
private:
|
||||
Size n, o, r, p, i1, i2, i3;
|
||||
Word oMask, rMask, pMask, i1Mask, i2Mask, i3Mask;
|
||||
|
||||
// FARES
|
||||
Size inst_s, opcode_s, reg_s, func3_s;
|
||||
Size shift_opcode, shift_rd, shift_rs1, shift_rs2, shift_func3, shift_func7;
|
||||
Size shift_j_u_immed, shift_s_b_immed, shift_i_immed;
|
||||
|
||||
|
||||
|
||||
Word reg_mask, func3_mask, func7_mask, opcode_mask, i_immed_mask,
|
||||
s_immed_mask, b_immed_mask, u_immed_mask, j_immed_mask;
|
||||
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
123
simX/include/harpfloat.h
Normal file
123
simX/include/harpfloat.h
Normal file
@@ -0,0 +1,123 @@
|
||||
/*******************************************************************************
|
||||
HARPtools by Chad D. Kersey, Summer 2011
|
||||
*******************************************************************************/
|
||||
#include <math.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "types.h"
|
||||
|
||||
#ifdef DEBUG
|
||||
#include <iostream>
|
||||
|
||||
#define DEBUGMSG(x) do { \
|
||||
std::cout << __FILE__ << ':' <<__LINE__ << ": " << x << '\n'; \
|
||||
} while(0)
|
||||
#else
|
||||
#define DEBUGMSG(x) do { } while(0)
|
||||
#endif
|
||||
|
||||
namespace Harp {
|
||||
// This class serves to handle the strange-precision floating point that can
|
||||
// crop up in HARP.
|
||||
class Float {
|
||||
public:
|
||||
Float(Word_u bin, Size n): sz(n) {
|
||||
DEBUGMSG("Float(0x" << std::hex << bin << ", " << std::dec << n << ')');
|
||||
|
||||
bool sign(bin >> (n*8 - 1));
|
||||
|
||||
Size expSz;
|
||||
if (n < 4) {
|
||||
expSz = 5;
|
||||
} else if (n < 8) {
|
||||
expSz = 8;
|
||||
} else {
|
||||
expSz = 11;
|
||||
}
|
||||
|
||||
Size sigSz = n*8 - expSz - 1;
|
||||
|
||||
DEBUGMSG(" exp: " << std::dec << expSz <<
|
||||
" bits, sig: " << std::dec << sigSz << " bits.");
|
||||
|
||||
int exp = (bin >> sigSz) & ((1<<expSz) - 1);
|
||||
Word_u sig = bin & ((1llu<<sigSz) - 1);
|
||||
DEBUGMSG(" sig=" << std::dec << sig << " exp=" << exp);
|
||||
|
||||
if (exp == 0) {
|
||||
// Subnormal
|
||||
d = sig / pow(2, ((1<<(expSz-1))-2)) / pow(2, sigSz);
|
||||
DEBUGMSG(" Denorm.");
|
||||
} else if (exp == ((1<<expSz) - 1)) {
|
||||
// Infinity
|
||||
d = HUGE_VAL;
|
||||
DEBUGMSG(" Inf.");
|
||||
} else {
|
||||
// Normalized, implied 1.
|
||||
exp -= (1<<(expSz - 1)) - 1;
|
||||
d = pow(2.0, exp - int(sigSz)) * double((1ll << sigSz) + sig);
|
||||
DEBUGMSG(" Norm, exp=" << exp);
|
||||
}
|
||||
|
||||
if (sign) d = -d;
|
||||
|
||||
DEBUGMSG("Set to " << d);
|
||||
}
|
||||
|
||||
Float(double d, Size n): sz(n), d(d) { DEBUGMSG("Float(double, size)"); }
|
||||
|
||||
operator Word_u() {
|
||||
DEBUGMSG("Float -> Word_u: " << d);
|
||||
Size expSz;
|
||||
if (sz < 4) {
|
||||
expSz = 5;
|
||||
} else if (sz < 8) {
|
||||
expSz = 8;
|
||||
} else {
|
||||
expSz = 11;
|
||||
}
|
||||
|
||||
Size sigSz = 8*sz - expSz - 1;
|
||||
|
||||
bool sign(d < 0);
|
||||
|
||||
bool inf(isinf(d)), zero(d == 0.0);
|
||||
int exp;
|
||||
|
||||
if (!inf && !zero) exp = floor(log2(fabs(d)));
|
||||
|
||||
Word_u rval;
|
||||
if (inf) {
|
||||
// Infinity
|
||||
DEBUGMSG(" Inf.");
|
||||
rval = ((1llu<<expSz)-1llu)<<sigSz;
|
||||
} else if (!zero && abs(exp) < (1<<(expSz-1)) - 1) {
|
||||
// Normalized with implied 1.
|
||||
Word_u sig = (fabs(d) * pow(2.0, -exp) - 1.0) * pow(2.0, sigSz);
|
||||
DEBUGMSG(" Norm, exp=" << exp << ", sig=" << sig);
|
||||
rval = ((((exp + ((1llu<<(expSz-1)) - 1llu))
|
||||
&((1llu<<expSz)-1llu)))<<sigSz) | sig;
|
||||
} else if (!zero && exp > -(1<<(expSz-1)) - sigSz) {
|
||||
// Subnormal number.
|
||||
Word_u sig = round(fabs(d)*pow(2.0,((1<<(expSz-1))-2))*pow(2.0, sigSz));
|
||||
DEBUGMSG(" Denorm, exp=" << exp << ", sig=" << sig);
|
||||
rval = sig;
|
||||
} else {
|
||||
// Zero.
|
||||
rval = 0;
|
||||
}
|
||||
|
||||
if (sign) rval |= 1llu<<(sz*8 - 1);
|
||||
|
||||
DEBUGMSG(" Returning 0x" << std::hex << rval);
|
||||
|
||||
return rval;
|
||||
}
|
||||
|
||||
operator double() { DEBUGMSG("Float->double " << d); return d; }
|
||||
|
||||
private:
|
||||
double d;
|
||||
Size sz;
|
||||
};
|
||||
};
|
||||
37
simX/include/help.h
Normal file
37
simX/include/help.h
Normal file
@@ -0,0 +1,37 @@
|
||||
/*******************************************************************************
|
||||
HARPtools by Chad D. Kersey, Summer 2011
|
||||
*******************************************************************************/
|
||||
#ifndef __HELP_H
|
||||
#define __HELP_H
|
||||
|
||||
/* Help messages. */
|
||||
namespace HarpTools {
|
||||
namespace Help {
|
||||
const char *mainHelp =
|
||||
"--help, -h, no arguments\n"
|
||||
" Print this message.\n"
|
||||
"-E, --emu; -A, --asm; -L, --ld; -D, --disasm\n"
|
||||
" Invoke the emulator, assembler, linker, and disassembler, "
|
||||
"respectively.\n"
|
||||
"<mode> --help\n"
|
||||
" Display contextual help.\n",
|
||||
*emuHelp = "HARP Emulator command line arguments:\n"
|
||||
" -c, --core <filename> RAM image\n"
|
||||
" -a, --arch <arch string> Architecture string\n"
|
||||
" -s, --stats Print stats on exit.\n"
|
||||
" -b, --basic Disable virtual memory.\n"
|
||||
" -i, --batch Disable console input.\n",
|
||||
*asmHelp = "HARP Assembler command line arguments:\n"
|
||||
" -a, --arch <arch string>\n"
|
||||
" -o, --output <filename>\n",
|
||||
*ldHelp = "HARP Linker command line arguments:\n"
|
||||
" -o, --output <filename>\n"
|
||||
" -a, --arch <filename>\n"
|
||||
" -f, --format <foramt string>\n"
|
||||
" --offset <bytes>\n",
|
||||
*disasmHelp = "HARP Disassembler command line arguments:\n"
|
||||
" -a, --arch <arch string> Architecture string.\n"
|
||||
" -o, --output <filename> Output filename.\n";
|
||||
};
|
||||
};
|
||||
#endif
|
||||
144
simX/include/instruction.h
Normal file
144
simX/include/instruction.h
Normal file
@@ -0,0 +1,144 @@
|
||||
/*******************************************************************************
|
||||
HARPtools by Chad D. Kersey, Summer 2011
|
||||
*******************************************************************************/
|
||||
#ifndef __INSTRUCTION_H
|
||||
#define __INSTRUCTION_H
|
||||
|
||||
#include <map>
|
||||
#include <iostream>
|
||||
|
||||
#include "types.h"
|
||||
|
||||
namespace Harp {
|
||||
class Warp;
|
||||
class Ref;
|
||||
|
||||
enum Opcode
|
||||
{
|
||||
NOP = 0,
|
||||
R_INST = 51,
|
||||
L_INST = 3,
|
||||
I_INST = 19,
|
||||
S_INST = 35,
|
||||
B_INST = 99,
|
||||
LUI_INST = 55,
|
||||
AUIPC_INST = 23,
|
||||
JAL_INST = 111,
|
||||
JALR_INST = 103,
|
||||
SYS_INST = 115,
|
||||
TRAP = 0x7f,
|
||||
FENCE = 0x0f,
|
||||
PJ_INST = 0x7b,
|
||||
GPGPU = 0x6b
|
||||
};
|
||||
|
||||
enum InstType { N_TYPE, R_TYPE, I_TYPE, S_TYPE, B_TYPE, U_TYPE, J_TYPE};
|
||||
|
||||
// We build a table of instruction information out of this.
|
||||
struct InstTableEntry_t {
|
||||
const char *opString;
|
||||
bool controlFlow, relAddress, allSrcArgs, privileged;
|
||||
InstType iType;
|
||||
|
||||
};
|
||||
|
||||
static std::map<int, struct InstTableEntry_t> instTable =
|
||||
{
|
||||
{Opcode::NOP, {"nop" , false, false, false, false, InstType::N_TYPE }},
|
||||
{Opcode::R_INST, {"r_type", false, false, false, false, InstType::R_TYPE }},
|
||||
{Opcode::L_INST, {"load" , false, false, false, false, InstType::I_TYPE }},
|
||||
{Opcode::I_INST, {"i_type", false, false, false, false, InstType::I_TYPE }},
|
||||
{Opcode::S_INST, {"store" , false, false, false, false, InstType::S_TYPE }},
|
||||
{Opcode::B_INST, {"branch", true , false, false, false, InstType::B_TYPE }},
|
||||
{Opcode::LUI_INST, {"lui" , false, false, false, false, InstType::U_TYPE }},
|
||||
{Opcode::AUIPC_INST, {"auipc" , false, false, false, false, InstType::U_TYPE }},
|
||||
{Opcode::JAL_INST, {"jal" , true , false, false, false, InstType::J_TYPE }},
|
||||
{Opcode::JALR_INST, {"jalr" , true , false, false, false, InstType::I_TYPE }},
|
||||
{Opcode::SYS_INST, {"SYS" , true , false, false, false, InstType::I_TYPE }},
|
||||
{Opcode::TRAP, {"TRAP" , true , false, false, false, InstType::I_TYPE }},
|
||||
{Opcode::FENCE, {"fence" , true , false, false, false, InstType::I_TYPE }},
|
||||
{Opcode::PJ_INST, {"pred j", true , false, false, false, InstType::R_TYPE }},
|
||||
{Opcode::GPGPU, {"gpgpu" , false, false, false, false, InstType::R_TYPE }}
|
||||
};
|
||||
|
||||
static const Size MAX_REG_SOURCES(3);
|
||||
static const Size MAX_PRED_SOURCES(2);
|
||||
|
||||
class Instruction;
|
||||
|
||||
struct DivergentBranchException {};
|
||||
struct DomainException {};
|
||||
|
||||
std::ostream &operator<<(std::ostream &, Instruction &);
|
||||
|
||||
class Instruction {
|
||||
public:
|
||||
Instruction() :
|
||||
predicated(false), nRsrc(0), nPsrc(0), immsrcPresent(false),
|
||||
rdestPresent(false), pdestPresent(false), refLiteral(NULL)
|
||||
{
|
||||
}
|
||||
|
||||
void executeOn(Warp &warp);
|
||||
friend std::ostream &operator<<(std::ostream &, Instruction &);
|
||||
|
||||
/* Setters used to "craft" the instruction. */
|
||||
void setOpcode (Opcode opc) { op = opc; }
|
||||
void setPred (RegNum pReg) { predicated = true; pred = pReg; }
|
||||
void setDestReg (RegNum destReg) { rdestPresent = true; rdest = destReg; }
|
||||
void setSrcReg (RegNum srcReg) { rsrc[nRsrc++] = srcReg; }
|
||||
void setFunc3 (Word func3) { this->func3 = func3; }
|
||||
void setFunc7 (Word func7) { this->func7 = func7; }
|
||||
void setDestPReg(RegNum dPReg) { pdestPresent = true; pdest = dPReg; }
|
||||
void setSrcPReg (RegNum srcPReg) { psrc[nPsrc++] = srcPReg; }
|
||||
Word *setSrcImm () { immsrcPresent = true; immsrc = 0xa5; return &immsrc;}
|
||||
void setSrcImm (Word srcImm) { immsrcPresent = true; immsrc = srcImm; }
|
||||
void setImmRef (Ref &r) { refLiteral = &r; }
|
||||
|
||||
/* Getters used by encoders. */
|
||||
Opcode getOpcode() const { return op; }
|
||||
bool hasPred() const { return predicated; }
|
||||
RegNum getPred() const { return pred; }
|
||||
RegNum getNRSrc() const { return nRsrc; }
|
||||
RegNum getRSrc(RegNum i) const { return rsrc[i]; }
|
||||
RegNum getNPSrc() const { return nPsrc; }
|
||||
RegNum getPSrc(RegNum i) const { return psrc[i]; }
|
||||
bool hasRDest() const { return rdestPresent; }
|
||||
RegNum getRDest() const { return rdest; }
|
||||
bool hasPDest() const { return pdestPresent; }
|
||||
RegNum getPDest() const { return pdest; }
|
||||
bool hasImm() const { return immsrcPresent; }
|
||||
Word getImm() const { return immsrc; }
|
||||
bool hasRefLiteral() const { return refLiteral != NULL; }
|
||||
Ref *getRefLiteral() const { return refLiteral; }
|
||||
|
||||
/* Getters used as table lookup. */
|
||||
bool hasRelImm() const { return (*(instTable.find(op))).second.relAddress; }
|
||||
|
||||
private:
|
||||
bool predicated;
|
||||
RegNum pred;
|
||||
Opcode op;
|
||||
int nRsrc, nPsrc;
|
||||
RegNum rsrc[MAX_REG_SOURCES], psrc[MAX_PRED_SOURCES];
|
||||
bool immsrcPresent;
|
||||
Word immsrc;
|
||||
Word func3;
|
||||
Word func7;
|
||||
bool rdestPresent, pdestPresent;
|
||||
RegNum rdest, pdest;
|
||||
Ref *refLiteral;
|
||||
|
||||
public:
|
||||
|
||||
|
||||
};
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
// static struct InstTableEntry {
|
||||
// const char *opString;
|
||||
// bool controlFlow, relAddress, allSrcArgs, privileged;
|
||||
// InstType iType;
|
||||
// };
|
||||
432
simX/include/mem.h
Normal file
432
simX/include/mem.h
Normal file
@@ -0,0 +1,432 @@
|
||||
/*******************************************************************************
|
||||
HARPtools by Chad D. Kersey, Summer 2011
|
||||
*******************************************************************************/
|
||||
#ifndef __MEM_H
|
||||
#define __MEM_H
|
||||
|
||||
#include <ostream>
|
||||
#include <vector>
|
||||
#include <queue>
|
||||
#include <map>
|
||||
#include <pthread.h>
|
||||
|
||||
#include "types.h"
|
||||
|
||||
namespace Harp {
|
||||
void *consoleInputThread(void *);
|
||||
struct BadAddress {};
|
||||
|
||||
class MemDevice {
|
||||
public:
|
||||
virtual ~MemDevice() {}
|
||||
virtual Size size() const = 0;
|
||||
virtual Word read(Addr) = 0;
|
||||
virtual void write(Addr, Word) = 0;
|
||||
virtual Byte *base() { return NULL; } /* Null if unavailable. */
|
||||
};
|
||||
|
||||
class RamMemDevice : public MemDevice {
|
||||
public:
|
||||
RamMemDevice(Size size, Size wordSize);
|
||||
RamMemDevice(const char* filename, Size wordSize);
|
||||
~RamMemDevice() {}
|
||||
|
||||
virtual Size size() const { return contents.size(); };
|
||||
virtual Word read(Addr);
|
||||
virtual void write(Addr, Word);
|
||||
virtual Byte *base() { return &contents[0]; }
|
||||
|
||||
protected:
|
||||
Size wordSize;
|
||||
std::vector<Byte> contents;
|
||||
};
|
||||
|
||||
class RomMemDevice : public RamMemDevice {
|
||||
public:
|
||||
RomMemDevice(const char* filename, Size wordSize) :
|
||||
RamMemDevice(filename, wordSize) {}
|
||||
RomMemDevice(Size size, Size wordSize) :
|
||||
RamMemDevice(size, wordSize) {}
|
||||
~RomMemDevice();
|
||||
|
||||
virtual void write(Addr, Word);
|
||||
};
|
||||
|
||||
class Core;
|
||||
class ConsoleMemDevice : public MemDevice {
|
||||
public:
|
||||
ConsoleMemDevice(Size wS, std::ostream &o, Core &core, bool batch = false);
|
||||
~ConsoleMemDevice() {}
|
||||
|
||||
//virtual Size wordSize() const { return wordSize; }
|
||||
virtual Size size() const { return wordSize; }
|
||||
virtual Word read(Addr) { pthread_mutex_lock(&cBufLock);
|
||||
char c = cBuf.front();
|
||||
cBuf.pop();
|
||||
pthread_mutex_unlock(&cBufLock);
|
||||
return Word(c); }
|
||||
virtual void write(Addr a, Word w) { output << char(w); }
|
||||
|
||||
void poll();
|
||||
|
||||
friend void *Harp::consoleInputThread(void *);
|
||||
|
||||
private:
|
||||
std::ostream &output;
|
||||
Size wordSize;
|
||||
Core &core;
|
||||
|
||||
std::queue<char> cBuf;
|
||||
pthread_mutex_t cBufLock;
|
||||
};
|
||||
|
||||
class DiskControllerMemDevice : public MemDevice {
|
||||
public:
|
||||
DiskControllerMemDevice(Size wordSize, Size blockSize, Core &c) :
|
||||
wordSize(wordSize), blockSize(blockSize), core(c), disks() {}
|
||||
|
||||
void addDisk(Byte *file, Size n) { disks.push_back(Disk(file, n)); }
|
||||
|
||||
virtual Size size() const { return wordSize * 6; }
|
||||
virtual Word read(Addr);
|
||||
virtual void write(Addr, Word);
|
||||
|
||||
private:
|
||||
Word curDisk, curBlock, nBlocks, physAddr, command, status;
|
||||
enum Status { OK = 0, INVALID_DISK, INVALID_BLOCK };
|
||||
struct Disk {
|
||||
Disk(Byte *f, Size n): file(f), blocks(n) {}
|
||||
Byte *file;
|
||||
Size blocks;
|
||||
};
|
||||
std::vector <Disk> disks;
|
||||
Core &core;
|
||||
Size wordSize, blockSize;;
|
||||
};
|
||||
|
||||
class MemoryUnit {
|
||||
public:
|
||||
MemoryUnit(Size pageSize, Size addrBytes, bool disableVm = false) :
|
||||
pageSize(pageSize), addrBytes(addrBytes), ad(), disableVm(disableVm)
|
||||
{
|
||||
if (!disableVm)
|
||||
tlb[0] = TLBEntry(0, 077);
|
||||
}
|
||||
void attach(MemDevice &m, Addr base);
|
||||
|
||||
//Size wordSize();
|
||||
struct PageFault {
|
||||
PageFault(Addr a, bool nf) : faultAddr(a), notFound(nf) {}
|
||||
Addr faultAddr;
|
||||
bool notFound;
|
||||
}; /* Thrown on page fault. */
|
||||
|
||||
Word read(Addr, bool sup); /* For data accesses. */
|
||||
Word fetch(Addr, bool sup); /* For instruction accesses. */
|
||||
Byte *getPtr(Addr, Size);
|
||||
void write(Addr, Word, bool sup, Size);
|
||||
void tlbAdd(Addr virt, Addr phys, Word flags);
|
||||
void tlbRm(Addr va);
|
||||
void tlbFlush() { tlb.clear(); }
|
||||
|
||||
#ifdef EMU_INSTRUMENTATION
|
||||
Addr virtToPhys(Addr va);
|
||||
#endif
|
||||
|
||||
private:
|
||||
class ADecoder {
|
||||
public:
|
||||
ADecoder() : zeroChild(NULL), oneChild(NULL), range(0) {}
|
||||
ADecoder(MemDevice &md, Size range) :
|
||||
zeroChild(NULL), oneChild(NULL), range(range), md(&md) {}
|
||||
Byte *getPtr(Addr a, Size sz, Size wordSize);
|
||||
Word read(Addr a, bool sup, Size wordSize);
|
||||
void write(Addr a, Word w, bool sup, Size wordSize);
|
||||
void map(Addr a, MemDevice &md, Size range, Size bit);
|
||||
private:
|
||||
MemDevice &doLookup(Addr a, Size &bit);
|
||||
ADecoder *zeroChild, *oneChild;
|
||||
MemDevice *md;
|
||||
Size range;
|
||||
};
|
||||
|
||||
ADecoder ad;
|
||||
|
||||
struct TLBEntry {
|
||||
TLBEntry() {}
|
||||
TLBEntry(Word pfn, Word flags): pfn(pfn), flags(flags) {}
|
||||
Word flags;
|
||||
Word pfn;
|
||||
};
|
||||
|
||||
std::map<Addr, TLBEntry> tlb;
|
||||
TLBEntry tlbLookup(Addr vAddr, Word flagMask);
|
||||
|
||||
Size pageSize, addrBytes;
|
||||
|
||||
bool disableVm;
|
||||
};
|
||||
|
||||
|
||||
class RAM : public MemDevice {
|
||||
public:
|
||||
uint8_t* mem[1 << 12];
|
||||
|
||||
RAM(){
|
||||
for(uint32_t i = 0;i < (1 << 12);i++) mem[i] = NULL;
|
||||
}
|
||||
~RAM(){
|
||||
for(uint32_t i = 0;i < (1 << 12);i++) if(mem[i]) delete [] mem[i];
|
||||
}
|
||||
|
||||
void clear(){
|
||||
for(uint32_t i = 0;i < (1 << 12);i++)
|
||||
{
|
||||
if(mem[i])
|
||||
{
|
||||
delete mem[i];
|
||||
mem[i] = NULL;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t* get(uint32_t address){
|
||||
|
||||
if(mem[address >> 20] == NULL) {
|
||||
uint8_t* ptr = new uint8_t[1024*1024];
|
||||
for(uint32_t i = 0;i < 1024*1024;i+=4) {
|
||||
ptr[i + 0] = 0xaa;
|
||||
ptr[i + 1] = 0xbb;
|
||||
ptr[i + 2] = 0xcc;
|
||||
ptr[i + 3] = 0xdd;
|
||||
}
|
||||
mem[address >> 20] = ptr;
|
||||
}
|
||||
return &mem[address >> 20][address & 0xFFFFF];
|
||||
}
|
||||
|
||||
void read(uint32_t address,uint32_t length, uint8_t *data){
|
||||
for(unsigned i = 0;i < length;i++){
|
||||
data[i] = (*this)[address + i];
|
||||
}
|
||||
}
|
||||
|
||||
void write(uint32_t address,uint32_t length, uint8_t *data){
|
||||
for(unsigned i = 0;i < length;i++){
|
||||
(*this)[address + i] = data[i];
|
||||
}
|
||||
}
|
||||
|
||||
virtual Size size() const { return (1<<31); };
|
||||
|
||||
void getBlock(uint32_t address, uint8_t *data)
|
||||
{
|
||||
uint32_t block_number = address & 0xffffff00; // To zero out block offset
|
||||
uint32_t bytes_num = 256;
|
||||
|
||||
this->read(block_number, bytes_num, data);
|
||||
}
|
||||
|
||||
void getWord(uint32_t address, uint32_t * data)
|
||||
{
|
||||
data[0] = 0;
|
||||
|
||||
uint8_t first = *get(address + 0);
|
||||
uint8_t second = *get(address + 1);
|
||||
uint8_t third = *get(address + 2);
|
||||
uint8_t fourth = *get(address + 3);
|
||||
|
||||
|
||||
// std::cout << std::hex;
|
||||
// std::cout << "RAM: READING ADDRESS " << address + 0 << " DATA: " << (uint32_t) first << "\n";
|
||||
// std::cout << "RAM: READING ADDRESS " << address + 1 << " DATA: " << (uint32_t) second << "\n";
|
||||
// std::cout << "RAM: READING ADDRESS " << address + 2 << " DATA: " << (uint32_t) third << "\n";
|
||||
// std::cout << "RAM: READING ADDRESS " << address + 3 << " DATA: " << (uint32_t) fourth << "\n";
|
||||
|
||||
data[0] = (data[0] << 0) | fourth;
|
||||
data[0] = (data[0] << 8) | third;
|
||||
data[0] = (data[0] << 8) | second;
|
||||
data[0] = (data[0] << 8) | first;
|
||||
// data[0] = (data[0] << 0) | first;
|
||||
// data[0] = (data[0] << 8) | second;
|
||||
// data[0] = (data[0] << 8) | third;
|
||||
// data[0] = (data[0] << 8) | fourth;
|
||||
|
||||
// std::cout << "FINAL DATA: " << data[0] << "\n";
|
||||
|
||||
}
|
||||
|
||||
void writeWord(uint32_t address, uint32_t * data)
|
||||
{
|
||||
uint32_t data_to_write = *data;
|
||||
|
||||
uint32_t byte_mask = 0xFF;
|
||||
|
||||
for (int i = 0; i < 4; i++)
|
||||
{
|
||||
// std::cout << "RAM: DATA TO WRITE " << data_to_write << "\n";
|
||||
// std::cout << "RAM: DATA TO MASK " << byte_mask << "\n";
|
||||
// std::cout << "RAM: WRITING ADDRESS " << address + i << " DATA: " << (data_to_write & byte_mask) << "\n";
|
||||
(*this)[address + i] = data_to_write & byte_mask;
|
||||
data_to_write = data_to_write >> 8;
|
||||
}
|
||||
}
|
||||
|
||||
void writeHalf(uint32_t address, uint32_t * data)
|
||||
{
|
||||
uint32_t data_to_write = *data;
|
||||
|
||||
uint32_t byte_mask = 0xFF;
|
||||
|
||||
for (int i = 0; i < 2; i++)
|
||||
{
|
||||
// std::cout << "RAM: DATA TO WRITE " << data_to_write << "\n";
|
||||
// std::cout << "RAM: DATA TO MASK " << byte_mask << "\n";
|
||||
// std::cout << "RAM: WRITING ADDRESS " << address + i << " DATA: " << (data_to_write & byte_mask) << "\n";
|
||||
(*this)[address + i] = data_to_write & byte_mask;
|
||||
data_to_write = data_to_write >> 8;
|
||||
}
|
||||
}
|
||||
|
||||
void writeByte(uint32_t address, uint32_t * data)
|
||||
{
|
||||
uint32_t data_to_write = *data;
|
||||
|
||||
uint32_t byte_mask = 0xFF;
|
||||
|
||||
(*this)[address] = data_to_write & byte_mask;
|
||||
data_to_write = data_to_write >> 8;
|
||||
|
||||
}
|
||||
|
||||
uint8_t& operator [](uint32_t address) {
|
||||
return *get(address);
|
||||
}
|
||||
|
||||
virtual void write(Addr addr, Word w)
|
||||
{
|
||||
uint32_t word = (uint32_t) w;
|
||||
writeWord(addr, &word);
|
||||
}
|
||||
|
||||
virtual Word read(Addr addr)
|
||||
{
|
||||
uint32_t w;
|
||||
getWord(addr, &w);
|
||||
// std::cout << "RAM: read -> " << w << " at addr: " << addr << "\n";
|
||||
return (Word) w;
|
||||
}
|
||||
|
||||
virtual Byte *base()
|
||||
{
|
||||
return (Byte *) this->get(0);
|
||||
}
|
||||
|
||||
// MEMORY UTILS
|
||||
|
||||
uint32_t hti_old(char c) {
|
||||
if (c >= 'A' && c <= 'F')
|
||||
return c - 'A' + 10;
|
||||
if (c >= 'a' && c <= 'f')
|
||||
return c - 'a' + 10;
|
||||
return c - '0';
|
||||
}
|
||||
|
||||
uint32_t hToI_old(char *c, uint32_t size) {
|
||||
uint32_t value = 0;
|
||||
for (uint32_t i = 0; i < size; i++) {
|
||||
value += hti_old(c[i]) << ((size - i - 1) * 4);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void loadHexImpl(std::string path) {
|
||||
this->clear();
|
||||
FILE *fp = fopen(&path[0], "r");
|
||||
if(fp == 0){
|
||||
std::cout << path << " not found" << std::endl;
|
||||
}
|
||||
//Preload 0x0 <-> 0x80000000 jumps
|
||||
((uint32_t*)this->get(0))[0] = 0xf1401073;
|
||||
((uint32_t*)this->get(0))[1] = 0xf1401073;
|
||||
|
||||
// ((uint32_t*)this->get(0))[1] = 0xf1401073;
|
||||
((uint32_t*)this->get(0))[2] = 0x30101073;
|
||||
|
||||
((uint32_t*)this->get(0))[3] = 0x800000b7;
|
||||
((uint32_t*)this->get(0))[4] = 0x000080e7;
|
||||
|
||||
((uint32_t*)this->get(0x80000000))[0] = 0x00000097;
|
||||
|
||||
((uint32_t*)this->get(0xb0000000))[0] = 0x01C02023;
|
||||
// F00FFF10
|
||||
((uint32_t*)this->get(0xf00fff10))[0] = 0x12345678;
|
||||
|
||||
|
||||
|
||||
|
||||
fseek(fp, 0, SEEK_END);
|
||||
uint32_t size = ftell(fp);
|
||||
fseek(fp, 0, SEEK_SET);
|
||||
char* content = new char[size];
|
||||
int x = fread(content, 1, size, fp);
|
||||
|
||||
if (!x) { std::cout << "COULD NOT READ FILE\n"; exit(1);}
|
||||
|
||||
int offset = 0;
|
||||
char* line = content;
|
||||
// std::cout << "WHTA\n";
|
||||
while (1) {
|
||||
if (line[0] == ':') {
|
||||
uint32_t byteCount = hToI_old(line + 1, 2);
|
||||
uint32_t nextAddr = hToI_old(line + 3, 4) + offset;
|
||||
uint32_t key = hToI_old(line + 7, 2);
|
||||
switch (key) {
|
||||
case 0:
|
||||
for (uint32_t i = 0; i < byteCount; i++) {
|
||||
|
||||
unsigned add = nextAddr + i;
|
||||
|
||||
*(this->get(add)) = hToI_old(line + 9 + i * 2, 2);
|
||||
// std::cout << "lhi: Address: " << std::hex <<(add) << "\tValue: " << std::hex << hToI_old(line + 9 + i * 2, 2) << std::endl;
|
||||
}
|
||||
break;
|
||||
case 2:
|
||||
// cout << offset << endl;
|
||||
offset = hToI_old(line + 9, 4) << 4;
|
||||
break;
|
||||
case 4:
|
||||
// cout << offset << endl;
|
||||
offset = hToI_old(line + 9, 4) << 16;
|
||||
break;
|
||||
default:
|
||||
// cout << "??? " << key << endl;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
while (*line != '\n' && size != 0) {
|
||||
line++;
|
||||
size--;
|
||||
}
|
||||
if (size <= 1)
|
||||
break;
|
||||
line++;
|
||||
size--;
|
||||
}
|
||||
|
||||
|
||||
if (content) delete[] content;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
210
simX/include/obj.h
Normal file
210
simX/include/obj.h
Normal file
@@ -0,0 +1,210 @@
|
||||
/*******************************************************************************
|
||||
HARPtools by Chad D. Kersey, Summer 2011
|
||||
*******************************************************************************/
|
||||
#ifndef __OBJ_H
|
||||
#define __OBJ_H
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <list>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "types.h"
|
||||
#include "archdef.h"
|
||||
#include "instruction.h"
|
||||
#include "enc.h"
|
||||
#include "asm-tokens.h"
|
||||
|
||||
namespace Harp {
|
||||
class Decoder;
|
||||
class Encoder;
|
||||
|
||||
class Ref {
|
||||
public:
|
||||
std::string name;
|
||||
Ref(const std::string &n, bool r, Size ib = 0):
|
||||
name(n), bound(false), relative(r), ibase(ib) { }
|
||||
virtual ~Ref() { }
|
||||
virtual void bind(Addr addr, Addr base = 0) = 0;
|
||||
virtual Addr getAddr() const = 0;
|
||||
|
||||
bool bound, relative;
|
||||
Size ibase;
|
||||
};
|
||||
|
||||
/* Used in not-yet-encoded code objects, plain old data. */
|
||||
class SimpleRef : public Ref {
|
||||
public:
|
||||
SimpleRef(const std::string &name, Addr &addr, bool rel = false) :
|
||||
Ref(name, rel), addr(addr) { }
|
||||
virtual void bind(Addr addr, Addr base = 0) {
|
||||
std::cout << "Attempted to bind a SimpleRef.\n";
|
||||
exit(1);
|
||||
}
|
||||
virtual Addr getAddr() const { return this->addr; }
|
||||
Byte *getAddrPtr() { return (Byte*)&addr; }
|
||||
|
||||
private:
|
||||
Addr &addr;
|
||||
};
|
||||
|
||||
// /* Used in already-encoded code objects. */
|
||||
// class OffsetRef : public Ref {
|
||||
// public:
|
||||
// OffsetRef(
|
||||
// const std::string &name, std::vector<Byte> &v, Size offset, Size bits,
|
||||
// Size ws, bool rel = false, Size ibase = 0
|
||||
// ) : Ref(name, rel, ibase), data(v), offset(offset), bits(bits), wordSize(ws)
|
||||
// {}
|
||||
|
||||
// virtual void bind(Addr addr, Addr base = 0) {
|
||||
// Size bytes(bits/8), remainder(bits%8);
|
||||
|
||||
// if (relative) {
|
||||
// addr = addr - base;
|
||||
// Word_s addr_s(addr);
|
||||
// if ((addr_s >> bits) != ~0ull && (addr_s >> bits) != 0) goto noFit;
|
||||
// } else {
|
||||
// Addr mask = (1ull<<bits)-1;
|
||||
// if (addr > mask) goto noFit;
|
||||
// }
|
||||
|
||||
// { Byte mask((1ull<<remainder) - 1);
|
||||
// Size i;
|
||||
// for (i = 0; i < bytes; i++) {
|
||||
// data[offset+i] = addr & 0xff;
|
||||
// addr >>= 8;
|
||||
// }
|
||||
// data[offset+i] &= ~mask;
|
||||
// data[offset+i] |= (addr&mask);
|
||||
// bound = true;
|
||||
// }
|
||||
|
||||
// return;
|
||||
// noFit:
|
||||
// std::cout << "Attempt to bind a " << bits << "-bit "
|
||||
// << (relative?"":"non-") << "relative symbol to an address"
|
||||
// " it cannot reach.\n";
|
||||
// exit(1);
|
||||
// }
|
||||
|
||||
// virtual Addr getAddr() const {
|
||||
// Size bytes = bits/8, remainder = bits%8;
|
||||
// Byte mask((1<<remainder)-1);
|
||||
// Addr a(data[offset]&mask);
|
||||
|
||||
// for (Size i = 0; i < bytes-1; i++) {
|
||||
// a |= data[offset + bytes - i - 1];
|
||||
// a <<= 8;
|
||||
// }
|
||||
// return a;
|
||||
// }
|
||||
|
||||
// Size getOffset() const { return offset; }
|
||||
// Size getBits() const { return bits; }
|
||||
|
||||
// private:
|
||||
// std::vector<Byte> &data;
|
||||
// Size offset, bits, wordSize;
|
||||
// };
|
||||
|
||||
// class Chunk {
|
||||
// public:
|
||||
// Chunk(std::string n, Size a = 0, Word f = 0) :
|
||||
// name(n), alignment(a), bound(false), flags(f), global(false) {}
|
||||
// virtual ~Chunk() { for (Size i = 0; i < refs.size(); i++) delete refs[i]; }
|
||||
// void bind(Addr a) { address = a; bound = true; }
|
||||
// void setGlobal() { global = true; }
|
||||
// bool isGlobal() const { return global; }
|
||||
// std::string name;
|
||||
// Size alignment;
|
||||
// bool bound, global;
|
||||
// Addr address;
|
||||
// Word flags;
|
||||
// std::vector<Ref*> refs;
|
||||
// };
|
||||
|
||||
// class TextChunk : public Chunk {
|
||||
// public:
|
||||
// TextChunk(std::string n, Size a = 0, Word f = 0)
|
||||
// : Chunk(n, a, f), instructions() {}
|
||||
|
||||
// ~TextChunk() {
|
||||
// for (Size i = 0; i < instructions.size(); i++) delete instructions[i];
|
||||
// }
|
||||
|
||||
// std::vector<Instruction*> instructions;
|
||||
// };
|
||||
|
||||
// class DataChunk : public Chunk {
|
||||
// public:
|
||||
// DataChunk(std::string n, Size a = 0, Word f = 0)
|
||||
// : Chunk(n, a, f), size(0), contents() {}
|
||||
// Size size;
|
||||
// std::vector<Byte> contents; /* 0 to size bytes in length. */
|
||||
// };
|
||||
|
||||
// class Obj {
|
||||
// public:
|
||||
// ~Obj() { for (Size i = 0; i < chunks.size(); i++) delete chunks[i]; }
|
||||
// std::vector<Chunk*> chunks;
|
||||
// Size entry;
|
||||
// };
|
||||
|
||||
// class DynObj : public Obj {
|
||||
// public:
|
||||
// std::vector<std::string> deps;
|
||||
// };
|
||||
|
||||
// class ObjReader {
|
||||
// public:
|
||||
// virtual Obj *read(std::istream &input) = 0;
|
||||
// private:
|
||||
// };
|
||||
|
||||
// class ObjWriter {
|
||||
// public:
|
||||
// virtual void write(std::ostream &output, const Obj &o) = 0;
|
||||
// private:
|
||||
// };
|
||||
|
||||
// class AsmReader : public ObjReader {
|
||||
// public:
|
||||
// AsmReader(ArchDef arch) :
|
||||
// wordSize(arch.getWordSize()), nRegs(arch.getNRegs()) {}
|
||||
// virtual Obj *read(std::istream &input);
|
||||
// private:
|
||||
// Size wordSize, nRegs;
|
||||
|
||||
// // Operand type sequences indexed by argument class
|
||||
// enum ArgType {AT_END, AT_REG, AT_PREG, AT_LIT};
|
||||
// static ArgType operandtype_table[][4]; // ArgClass -> ArgType[arg_idx]
|
||||
// };
|
||||
|
||||
// class HOFReader : public ObjReader {
|
||||
// public:
|
||||
// HOFReader(ArchDef &arch) : arch(arch) {}
|
||||
// Obj *read(std::istream &input);
|
||||
// private:
|
||||
// const ArchDef &arch;
|
||||
// };
|
||||
|
||||
// class AsmWriter : public ObjWriter {
|
||||
// public:
|
||||
// AsmWriter(ArchDef arch): wordSize(arch.getWordSize()) {}
|
||||
// virtual void write(std::ostream &output, const Obj &obj);
|
||||
// private:
|
||||
// Size wordSize;
|
||||
// };
|
||||
|
||||
// class HOFWriter : public ObjWriter {
|
||||
// public:
|
||||
// HOFWriter(ArchDef &arch) : arch(arch) {}
|
||||
// virtual void write(std::ostream &output, const Obj &obj);
|
||||
// private:
|
||||
// const ArchDef &arch;
|
||||
// };
|
||||
};
|
||||
|
||||
#endif
|
||||
169
simX/include/qsim-harp.h
Normal file
169
simX/include/qsim-harp.h
Normal file
@@ -0,0 +1,169 @@
|
||||
/*******************************************************************************
|
||||
HARPtools by Chad D. Kersey, Summer 2011
|
||||
*******************************************************************************/
|
||||
#ifndef EMU_INSTRUMENTATION
|
||||
#define EMU_INSTRUMENTATION
|
||||
#endif
|
||||
|
||||
#ifndef __QSIM_HARP_H
|
||||
#define __QSIM_HARP_H
|
||||
|
||||
#include "types.h"
|
||||
#include "core.h"
|
||||
#include "enc.h"
|
||||
#include "instruction.h"
|
||||
#include "mem.h"
|
||||
#include "obj.h"
|
||||
#include "archdef.h"
|
||||
|
||||
#include <stdint.h>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
#include <qsim.h>
|
||||
|
||||
namespace Harp {
|
||||
class OSDomain {
|
||||
public:
|
||||
OSDomain(Harp::ArchDef &arch, std::string imgFile);
|
||||
|
||||
bool idle(unsigned i) const { return cpus[i].idle(); }
|
||||
int get_tid(unsigned i) const { return cpus[i].get_tid(); }
|
||||
bool get_prot(unsigned i) const { return cpus[i].get_prot(); }
|
||||
|
||||
int get_n() const { return cpus.size(); }
|
||||
|
||||
uint64_t run(unsigned i, uint64_t n) { return cpus[i].run(n); }
|
||||
void connect_console(std::ostream &s);
|
||||
void timer_interrupt() { /* TODO: timer convention */ }
|
||||
void interrupt(unsigned i, int vec) { cpus[i].interrupt(vec); }
|
||||
bool booted(unsigned i) const { return cpus[i].booted(); }
|
||||
void save_state(const char* state_file);
|
||||
|
||||
template <typename T>
|
||||
void set_atomic_cb
|
||||
(T *p, typename Qsim::OSDomain::atomic_cb_obj<T>::atomic_cb_t f)
|
||||
{
|
||||
atomic_cbs.push_back(new Qsim::OSDomain::atomic_cb_obj<T>(p, f));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void set_inst_cb
|
||||
(T* p, typename Qsim::OSDomain::inst_cb_obj<T>::inst_cb_t f)
|
||||
{
|
||||
inst_cbs.push_back(new Qsim::OSDomain::inst_cb_obj<T>(p, f));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void set_int_cb
|
||||
(T *p, typename Qsim::OSDomain::int_cb_obj<T>::int_cb_t f)
|
||||
{
|
||||
int_cbs.push_back(new Qsim::OSDomain::int_cb_obj<T>(p, f));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void set_mem_cb
|
||||
(T *p, typename Qsim::OSDomain::mem_cb_obj<T>::mem_cb_t f)
|
||||
{
|
||||
mem_cbs.push_back(new Qsim::OSDomain::mem_cb_obj<T>(p, f));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void set_magic_cb
|
||||
(T *p, typename Qsim::OSDomain::magic_cb_obj<T>::magic_cb_t f)
|
||||
{
|
||||
magic_cbs.push_back(new Qsim::OSDomain::magic_cb_obj<T>(p, f));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void set_io_cb
|
||||
(T *p, typename Qsim::OSDomain::io_cb_obj<T>::io_cb_t f)
|
||||
{ /* Do nothing. We have no separate IO address space. */ }
|
||||
|
||||
template <typename T>
|
||||
void set_reg_cb
|
||||
(T *p, typename Qsim::OSDomain::reg_cb_obj<T>::reg_cb_t f)
|
||||
{
|
||||
reg_cbs.push_back(new Qsim::OSDomain::reg_cb_obj<T>(p, f));
|
||||
}
|
||||
|
||||
template <typename T> void mem_rd(T& d, uint64_t paddr);
|
||||
template <typename T> void mem_rd_virt(unsigned i, T& d, uint64_t vaddr);
|
||||
template <typename T> void mem_wr(T& d, uint64_t paddr);
|
||||
template <typename T> void mem_wr_virt(unsigned i, T& d, uint64_t vaddr);
|
||||
|
||||
static OSDomain *osDomain;
|
||||
|
||||
bool do_atomic(unsigned c) {
|
||||
bool rval(false);
|
||||
for (unsigned i = 0; i < atomic_cbs.size(); ++i)
|
||||
if ((*atomic_cbs[i])(c)) rval = true;
|
||||
return rval;
|
||||
}
|
||||
|
||||
void do_inst(unsigned c, uint64_t va, uint64_t pa, uint8_t l,
|
||||
const uint8_t *b, enum inst_type t)
|
||||
{
|
||||
for (unsigned i = 0; i < inst_cbs.size(); ++i)
|
||||
(*inst_cbs[i])(c, va, pa, l, b, t);
|
||||
}
|
||||
|
||||
void do_int(unsigned c, int v) {
|
||||
for (unsigned i = 0; i < int_cbs.size(); ++i)
|
||||
(*int_cbs[i])(c, v);
|
||||
}
|
||||
|
||||
void do_mem(unsigned c, uint64_t va, uint64_t pa, uint8_t s, bool w) {
|
||||
for (unsigned i = 0; i < mem_cbs.size(); ++i)
|
||||
(*mem_cbs[i])(c, va, pa, s, w);
|
||||
}
|
||||
|
||||
bool do_magic(unsigned c, uint64_t r0) {
|
||||
bool rval(false);
|
||||
for (unsigned i = 0; i < magic_cbs.size(); ++i)
|
||||
if ((*magic_cbs[i])(c, r0)) rval = true;
|
||||
return rval;
|
||||
}
|
||||
|
||||
void do_reg(unsigned c, int r, uint8_t s, bool w) {
|
||||
for (unsigned i = 0; i < reg_cbs.size(); ++i)
|
||||
(*reg_cbs[i])(c, r, s, w);
|
||||
}
|
||||
|
||||
private:
|
||||
class Cpu {
|
||||
public:
|
||||
Cpu(Harp::OSDomain &osd);
|
||||
Cpu(): dec(NULL), core(NULL) {}
|
||||
~Cpu() { if (dec) delete dec; if (core) delete core; }
|
||||
|
||||
bool idle() const { return false; }
|
||||
int get_tid() const { return 0; }
|
||||
bool get_prot() const { return core->getSupervisorMode(); }
|
||||
uint64_t run(uint64_t n);
|
||||
void interrupt(int vec) { core->interrupt(vec); }
|
||||
bool booted() const { return core->running(); }
|
||||
|
||||
Harp::OSDomain *osd;
|
||||
Harp::Decoder *dec;
|
||||
Harp::Core *core;
|
||||
};
|
||||
|
||||
Harp::ArchDef arch;
|
||||
|
||||
Harp::MemoryUnit mu;
|
||||
Harp::RamMemDevice ram;
|
||||
Harp::ConsoleMemDevice *console;
|
||||
|
||||
std::vector <Harp::OSDomain::Cpu> cpus;
|
||||
|
||||
std::vector <Qsim::OSDomain::atomic_cb_obj_base*> atomic_cbs;
|
||||
std::vector <Qsim::OSDomain::inst_cb_obj_base*> inst_cbs;
|
||||
std::vector <Qsim::OSDomain::int_cb_obj_base*> int_cbs;
|
||||
std::vector <Qsim::OSDomain::mem_cb_obj_base*> mem_cbs;
|
||||
std::vector <Qsim::OSDomain::magic_cb_obj_base*> magic_cbs;
|
||||
std::vector <Qsim::OSDomain::reg_cb_obj_base*> reg_cbs;
|
||||
};
|
||||
};
|
||||
#endif
|
||||
33
simX/include/trace.h
Normal file
33
simX/include/trace.h
Normal file
@@ -0,0 +1,33 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
namespace Harp {
|
||||
|
||||
typedef struct
|
||||
{
|
||||
// Warp step
|
||||
bool valid_inst;
|
||||
unsigned pc;
|
||||
|
||||
// Core scheduler
|
||||
int wid;
|
||||
|
||||
// Encoder
|
||||
int rs1;
|
||||
int rs2;
|
||||
int rd;
|
||||
|
||||
// Instruction execute
|
||||
bool is_lw;
|
||||
bool is_sw;
|
||||
unsigned * mem_addresses;
|
||||
|
||||
// dmem interface
|
||||
int mem_stall_cycles;
|
||||
int fetch_stall_cycles;
|
||||
|
||||
// Instruction execute
|
||||
bool stall_warp;
|
||||
} trace_inst_t;
|
||||
|
||||
}
|
||||
25
simX/include/types.h
Normal file
25
simX/include/types.h
Normal file
@@ -0,0 +1,25 @@
|
||||
/*******************************************************************************
|
||||
HARPtools by Chad D. Kersey, Summer 2011
|
||||
*******************************************************************************/
|
||||
#ifndef __TYPES_H
|
||||
#define __TYPES_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
namespace Harp {
|
||||
typedef uint8_t Byte;
|
||||
typedef uint32_t Word;
|
||||
typedef uint32_t Word_u;
|
||||
typedef int32_t Word_s;
|
||||
|
||||
typedef Word_u Addr;
|
||||
typedef Word_u Size;
|
||||
|
||||
typedef unsigned RegNum;
|
||||
typedef unsigned ThdNum;
|
||||
|
||||
enum MemFlags {RD_USR = 1, WR_USR = 2, EX_USR = 4,
|
||||
RD_SUP = 8, WR_SUP = 16, EX_SUP = 32};
|
||||
};
|
||||
|
||||
#endif
|
||||
24
simX/include/util.h
Normal file
24
simX/include/util.h
Normal file
@@ -0,0 +1,24 @@
|
||||
/*******************************************************************************
|
||||
HARPtools by Chad D. Kersey, Summer 2011
|
||||
*******************************************************************************/
|
||||
#ifndef __UTIL_H
|
||||
#define __UTIL_H
|
||||
|
||||
#include <vector>
|
||||
#include "types.h"
|
||||
|
||||
namespace Harp {
|
||||
Word_u bytesToWord(const Byte *b, Size wordSize);
|
||||
void wordToBytes(Byte *b, Word_u w, Size wordSize);
|
||||
Word_u flagsToWord(bool r, bool w, bool x);
|
||||
void wordToFlags(bool &r, bool &w, bool &x, Word_u f);
|
||||
|
||||
class OutOfBytes {};
|
||||
|
||||
Byte readByte(const std::vector<Byte> &b, Size &n);
|
||||
Word_u readWord(const std::vector<Byte> &b, Size &n, Size wordSize);
|
||||
void writeByte(std::vector<Byte> &p, Size &n, Byte b);
|
||||
void writeWord(std::vector<Byte> &p, Size &n, Size wordSize, Word w);
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user