/*
|
/*
|
* Copyright (c) 2016 by Alex I. Kuznetsov.
|
* Copyright (c) 2016 by Alex I. Kuznetsov.
|
*
|
*
|
* Part of the LXP32 CPU IP core.
|
* Part of the LXP32 CPU IP core.
|
*
|
*
|
* This module defines the Disassembler class which disassembles
|
* This module defines the Disassembler class which disassembles
|
* LXP32 executable code.
|
* LXP32 executable code.
|
*/
|
*/
|
|
|
#ifndef DISASSEMBLER_H_INCLUDED
|
#ifndef DISASSEMBLER_H_INCLUDED
|
#define DISASSEMBLER_H_INCLUDED
|
#define DISASSEMBLER_H_INCLUDED
|
|
|
#include <iostream>
|
#include <iostream>
|
#include <type_traits>
|
#include <type_traits>
|
#include <cstdint>
|
#include <cstdint>
|
|
|
class Disassembler {
|
class Disassembler {
|
public:
|
public:
|
enum Format {Bin,Textio,Dec,Hex};
|
enum Format {Bin,Textio,Dec,Hex};
|
typedef std::uint32_t Word;
|
typedef std::uint32_t Word;
|
private:
|
private:
|
class Operand {
|
class Operand {
|
public:
|
public:
|
enum Type {Register,Direct};
|
enum Type {Register,Direct};
|
private:
|
private:
|
Type _type;
|
Type _type;
|
int _value;
|
int _value;
|
public:
|
public:
|
Operand(Type t,int value);
|
Operand(Type t,int value);
|
|
|
Type type() const;
|
Type type() const;
|
int value() const;
|
int value() const;
|
std::string str() const;
|
|
};
|
};
|
|
|
std::istream &_is;
|
std::istream &_is;
|
std::ostream &_os;
|
std::ostream &_os;
|
Format _fmt;
|
Format _fmt;
|
|
bool _preferAliases;
|
int _lineNumber;
|
int _lineNumber;
|
Word _pos;
|
Word _pos;
|
public:
|
public:
|
Disassembler(std::istream &is,std::ostream &os);
|
Disassembler(std::istream &is,std::ostream &os);
|
void setFormat(Format fmt);
|
void setFormat(Format fmt);
|
void setBase(Word base);
|
void setBase(Word base);
|
|
void setPreferAliases(bool b);
|
void dump();
|
void dump();
|
|
|
template <typename T> static std::string hex(const T &w) {
|
template <typename T> static std::string hex(const T &w) {
|
static_assert(std::is_integral<T>::value,"Argument must be of integral type");
|
static_assert(std::is_integral<T>::value,"Argument must be of integral type");
|
const char *hexstr="0123456789ABCDEF";
|
const char *hexstr="0123456789ABCDEF";
|
std::string res;
|
std::string res;
|
|
|
res.reserve(sizeof(T)*2);
|
res.reserve(sizeof(T)*2);
|
|
|
for(int i=sizeof(T)*8-4;i>=0;i-=4) {
|
for(int i=sizeof(T)*8-4;i>=0;i-=4) {
|
res.push_back(hexstr[(w>>i)&0x0F]);
|
res.push_back(hexstr[(w>>i)&0x0F]);
|
}
|
}
|
return res;
|
return res;
|
}
|
}
|
private:
|
private:
|
bool getWord(Word &w);
|
bool getWord(Word &w);
|
|
std::string str(const Operand &op);
|
static Operand decodeRd1Operand(Word w);
|
static Operand decodeRd1Operand(Word w);
|
static Operand decodeRd2Operand(Word w);
|
static Operand decodeRd2Operand(Word w);
|
static Operand decodeDstOperand(Word w);
|
static Operand decodeDstOperand(Word w);
|
static std::string decodeSimpleInstruction(const std::string &op,Word w);
|
|
|
|
|
std::string decodeSimpleInstruction(const std::string &op,Word w);
|
std::string decodeAdd(Word w);
|
std::string decodeAdd(Word w);
|
std::string decodeAnd(Word w);
|
std::string decodeAnd(Word w);
|
std::string decodeCall(Word w);
|
std::string decodeCall(Word w);
|
std::string decodeCjmpxx(Word w);
|
std::string decodeCjmpxx(Word w);
|
std::string decodeDivs(Word w);
|
std::string decodeDivs(Word w);
|
std::string decodeDivu(Word w);
|
std::string decodeDivu(Word w);
|
std::string decodeHlt(Word w);
|
std::string decodeHlt(Word w);
|
std::string decodeJmp(Word w);
|
std::string decodeJmp(Word w);
|
std::string decodeLc(Word w,bool &valid,Word &operand);
|
std::string decodeLc(Word w,bool &valid,Word &operand);
|
|
std::string decodeLcs(Word w);
|
std::string decodeLsb(Word w);
|
std::string decodeLsb(Word w);
|
std::string decodeLub(Word w);
|
std::string decodeLub(Word w);
|
std::string decodeLw(Word w);
|
std::string decodeLw(Word w);
|
std::string decodeMods(Word w);
|
std::string decodeMods(Word w);
|
std::string decodeModu(Word w);
|
std::string decodeModu(Word w);
|
std::string decodeMul(Word w);
|
std::string decodeMul(Word w);
|
std::string decodeNop(Word w);
|
std::string decodeNop(Word w);
|
std::string decodeOr(Word w);
|
std::string decodeOr(Word w);
|
std::string decodeSb(Word w);
|
std::string decodeSb(Word w);
|
std::string decodeSl(Word w);
|
std::string decodeSl(Word w);
|
std::string decodeSrs(Word w);
|
std::string decodeSrs(Word w);
|
std::string decodeSru(Word w);
|
std::string decodeSru(Word w);
|
std::string decodeSub(Word w);
|
std::string decodeSub(Word w);
|
std::string decodeSw(Word w);
|
std::string decodeSw(Word w);
|
std::string decodeXor(Word w);
|
std::string decodeXor(Word w);
|
std::string decodeWord(Word w);
|
std::string decodeWord(Word w);
|
};
|
};
|
|
|
#endif
|
#endif
|
|
|