Initial commit. Harptool, some docs, and the initial logo attempt.
git-svn-id: http://www.cdkersey.com/harp/harptool@1 0246edb2-e076-4747-b392-db732a341fa2
This commit is contained in:
87
src/include/archdef.h
Normal file
87
src/include/archdef.h
Normal file
@@ -0,0 +1,87 @@
|
||||
/*******************************************************************************
|
||||
HARPtools by Chad D. Kersey, Summer 2011
|
||||
*******************************************************************************/
|
||||
#ifndef __ARCHDEF_H
|
||||
#define __ARCHDEF_H
|
||||
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
|
||||
#include "types.h"
|
||||
|
||||
namespace Harp {
|
||||
class ArchDef {
|
||||
public:
|
||||
struct Undefined {};
|
||||
|
||||
ArchDef(const std::string &s) {
|
||||
std::istringstream iss(s.c_str());
|
||||
|
||||
iss >> wordSize;
|
||||
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; }
|
||||
extent = EXT_THDS;
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
return oss.str();
|
||||
}
|
||||
|
||||
bool operator==(const ArchDef &r) const {
|
||||
return (extent == r.extent) && (wordSize == r.wordSize) &&
|
||||
(encChar == r.encChar) && (nRegs == r.nRegs) &&
|
||||
(nPRegs == r.nPRegs) && (nThds == r.nThds);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
private:
|
||||
enum {
|
||||
EXT_NULL, EXT_WORDSIZE, EXT_ENC, EXT_REGS, EXT_PREGS, EXT_THDS
|
||||
} extent;
|
||||
|
||||
Size wordSize;
|
||||
ThdNum nThds;
|
||||
RegNum nRegs, nPRegs;
|
||||
char encChar;
|
||||
};
|
||||
};
|
||||
|
||||
#endif
|
||||
61
src/include/args.h
Normal file
61
src/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
|
||||
10
src/include/asm-tokens.h
Normal file
10
src/include/asm-tokens.h
Normal file
@@ -0,0 +1,10 @@
|
||||
namespace HarpTools {
|
||||
enum AsmTokens {
|
||||
ASM_T_DIR_DEF = 1, ASM_T_DIR_PERM, ASM_T_DIR_BYTE, ASM_T_DIR_WORD,
|
||||
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_LIT,
|
||||
ASM_T_SYM, ASM_T_PEXP
|
||||
};
|
||||
};
|
||||
43
src/include/core.h
Normal file
43
src/include/core.h
Normal file
@@ -0,0 +1,43 @@
|
||||
/*******************************************************************************
|
||||
HARPtools by Chad D. Kersey, Summer 2011
|
||||
*******************************************************************************/
|
||||
#ifndef __CORE_H
|
||||
#define __CORE_H
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "types.h"
|
||||
#include "archdef.h"
|
||||
#include "enc.h"
|
||||
#include "mem.h"
|
||||
|
||||
namespace Harp {
|
||||
class Core {
|
||||
public:
|
||||
Core(const ArchDef &a, Decoder &d, MemoryUnit &mem);
|
||||
void step();
|
||||
bool interrupt(Word r0);
|
||||
bool running() const { return activeThreads; }
|
||||
|
||||
private:
|
||||
const ArchDef &a;
|
||||
Decoder &iDec;
|
||||
MemoryUnit &mem;
|
||||
|
||||
Word pc, interruptEntry, shadowPc;
|
||||
Size activeThreads, shadowActiveThreads;
|
||||
std::vector<std::vector<Word> > reg;
|
||||
std::vector<std::vector<bool> > pred;
|
||||
|
||||
std::vector<Word> shadowReg;
|
||||
std::vector<bool> shadowPReg;
|
||||
|
||||
bool interruptEnable, shadowInterruptEnable, supervisorMode,
|
||||
shadowSupervisorMode;
|
||||
|
||||
friend class Instruction;
|
||||
};
|
||||
};
|
||||
|
||||
#endif
|
||||
84
src/include/enc.h
Normal file
84
src/include/enc.h
Normal file
@@ -0,0 +1,84 @@
|
||||
/*******************************************************************************
|
||||
HARPtools by Chad D. Kersey, Summer 2011
|
||||
*******************************************************************************/
|
||||
#ifndef __ENC_H
|
||||
#define __ENC_H
|
||||
|
||||
#include <map>
|
||||
|
||||
#include "types.h"
|
||||
#include "instruction.h"
|
||||
#include "obj.h"
|
||||
|
||||
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) = 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);
|
||||
|
||||
private:
|
||||
Size n, o, r, p, i1, i2, i3;
|
||||
Word oMask, rMask, pMask, i1Mask, i2Mask, i3Mask;
|
||||
};
|
||||
|
||||
class ByteDecoder : public Decoder {
|
||||
public:
|
||||
ByteDecoder(const ArchDef &);
|
||||
virtual Instruction *decode(const std::vector<Byte> &v, Size &n);
|
||||
private:
|
||||
Size wordSize;
|
||||
};
|
||||
|
||||
class WordEncoder : public Encoder {
|
||||
public:
|
||||
WordEncoder(const ArchDef &);
|
||||
virtual Size encode(Ref *&ref, std::vector<Byte> &v,
|
||||
Size n, Instruction &i);
|
||||
private:
|
||||
Size n, o, r, p, i1, i2, i3;
|
||||
Word oMask, rMask, pMask, i1Mask, i2Mask, i3Mask;
|
||||
};
|
||||
|
||||
class ByteEncoder : public Encoder {
|
||||
public:
|
||||
ByteEncoder(const ArchDef &);
|
||||
virtual Size encode(Ref *&ref, std::vector<Byte> &v,
|
||||
Size n, Instruction &i);
|
||||
private:
|
||||
Size wordSize;
|
||||
};
|
||||
};
|
||||
|
||||
#endif
|
||||
34
src/include/help.h
Normal file
34
src/include/help.h
Normal file
@@ -0,0 +1,34 @@
|
||||
/*******************************************************************************
|
||||
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",
|
||||
*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
|
||||
93
src/include/instruction.h
Normal file
93
src/include/instruction.h
Normal file
@@ -0,0 +1,93 @@
|
||||
/*******************************************************************************
|
||||
HARPtools by Chad D. Kersey, Summer 2011
|
||||
*******************************************************************************/
|
||||
#ifndef __INSTRUCTION_H
|
||||
#define __INSTRUCTION_H
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include "types.h"
|
||||
|
||||
namespace Harp {
|
||||
class Core;
|
||||
class Ref;
|
||||
|
||||
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:
|
||||
enum Opcode { NOP, DI, EI, TLBADD, TLBFLUSH, NEG, NOT,
|
||||
AND, OR, XOR, ADD, SUB, MUL, DIV, MOD, SHL, SHR,
|
||||
ANDI, ORI, XORI, ADDI, SUBI, MULI, DIVI, MODI, SHLI, SHRI,
|
||||
JALI, JALR, JMPI, JMPR, CLONE, JALIS, JALRS,
|
||||
JMPRT, LD, ST, LDI, RTOP, ANDP, ORP, XORP, NOTP, ISNEG,
|
||||
ISZERO, HALT, TRAP, JMPRU, SKEP, RETI, TLBRM };
|
||||
enum ArgClass {
|
||||
AC_NONE, AC_2REG, AC_2IMM, AC_3REG, AC_3PREG, AC_3IMM, AC_3REGSRC,
|
||||
AC_1IMM, AC_1REG, AC_3IMMSRC, AC_PREG_REG, AC_2PREG
|
||||
};
|
||||
static const char *opStrings[];
|
||||
static const bool allSrcArgs[], privileged[], relAddress[], isControlFlow[];
|
||||
static const ArgClass argClasses[];
|
||||
|
||||
Instruction() :
|
||||
predicated(false), nRsrc(0), nPsrc(0), immsrcPresent(false),
|
||||
rdestPresent(false), pdestPresent(false), refLiteral(NULL) {}
|
||||
|
||||
void executeOn(Core &core);
|
||||
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 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 relAddress[op]; }
|
||||
|
||||
private:
|
||||
bool predicated;
|
||||
RegNum pred;
|
||||
Opcode op;
|
||||
int nRsrc, nPsrc;
|
||||
RegNum rsrc[MAX_REG_SOURCES], psrc[MAX_PRED_SOURCES];
|
||||
bool immsrcPresent;
|
||||
Word immsrc;
|
||||
bool rdestPresent, pdestPresent;
|
||||
RegNum rdest, pdest;
|
||||
Ref *refLiteral;
|
||||
};
|
||||
};
|
||||
|
||||
#endif
|
||||
163
src/include/mem.h
Normal file
163
src/include/mem.h
Normal file
@@ -0,0 +1,163 @@
|
||||
/*******************************************************************************
|
||||
HARPtools by Chad D. Kersey, Summer 2011
|
||||
*******************************************************************************/
|
||||
#ifndef __MEM_H
|
||||
#define __MEM_H
|
||||
|
||||
#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);
|
||||
~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) :
|
||||
pageSize(pageSize), addrBytes(addrBytes), ad()
|
||||
{
|
||||
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);
|
||||
void tlbAdd(Addr virt, Addr phys, Word flags);
|
||||
void tlbRm(Addr va);
|
||||
void tlbFlush() { tlb.clear(); }
|
||||
|
||||
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;
|
||||
};
|
||||
};
|
||||
|
||||
#endif
|
||||
200
src/include/obj.h
Normal file
200
src/include/obj.h
Normal file
@@ -0,0 +1,200 @@
|
||||
/*******************************************************************************
|
||||
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 "enc.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, i;
|
||||
|
||||
if (relative) {
|
||||
addr = addr - base;
|
||||
Word_s addr_s(addr);
|
||||
if ((addr_s >> bits) != -1 && (addr_s >> bits) != 0) goto noFit;
|
||||
} else {
|
||||
Addr mask = (1ll<<bits)-1;
|
||||
if (addr > mask) goto noFit;
|
||||
}
|
||||
|
||||
{ Byte mask((1<<remainder) - 1);
|
||||
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 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()) {}
|
||||
virtual Obj *read(std::istream &input);
|
||||
private:
|
||||
Size wordSize;
|
||||
};
|
||||
|
||||
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
|
||||
25
src/include/types.h
Normal file
25
src/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 uint64_t Word;
|
||||
typedef uint64_t Word_u;
|
||||
typedef int64_t Word_s;
|
||||
|
||||
typedef Word Addr;
|
||||
typedef Word 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
src/include/util.h
Normal file
24
src/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