Skip to content

Commit

Permalink
Lift MSP430 machine language to RzIL: first 3 instructions and 1 addr…
Browse files Browse the repository at this point in the history
…essing mode
  • Loading branch information
moste00 committed Jun 28, 2024
1 parent 19ec6ba commit 225d05a
Show file tree
Hide file tree
Showing 13 changed files with 1,351 additions and 143 deletions.
197 changes: 140 additions & 57 deletions librz/arch/isa/msp430/msp430_disas.c

Large diffs are not rendered by default.

100 changes: 70 additions & 30 deletions librz/arch/isa/msp430/msp430_disas.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#ifndef MSP430_DISAS_H
#define MSP430_DISAS_H

enum msp430_oneop_opcodes {
typedef enum msp430_oneop_opcodes {
MSP430_RRC,
MSP430_SWPB,
MSP430_RRA,
Expand All @@ -16,9 +16,9 @@ enum msp430_oneop_opcodes {
MSP430_CALL,
MSP430_RETI,
MSP430_UNUSED,
};
} Msp430OneopOpcodes;

enum msp430_jumps {
typedef enum msp430_jumps {
MSP430_JNE,
MSP430_JEQ,
MSP430_JNC,
Expand All @@ -27,10 +27,9 @@ enum msp430_jumps {
MSP430_JGE,
MSP430_JL,
MSP430_JMP,
};
} Msp430Jumps;

enum msp430_twoop_opcodes {
MSP430_JMP_OPC = 0x1,
typedef enum msp430_twoop_opcodes {
MSP430_MOV = 0x4,
MSP430_ADD,
MSP430_ADDC,
Expand All @@ -43,23 +42,44 @@ enum msp430_twoop_opcodes {
MSP430_BIS,
MSP430_XOR,
MSP430_AND,
};
} Msp430TwoopOpcodes;

enum msp430_addr_modes {
MSP430_DIRECT,
MSP430_INDEXED,
MSP430_INDIRECT,
MSP430_INDIRECT_INC,
};
typedef enum msp430_emulated_opcodes {
MSP430_ADC,
MSP430_BR,
MSP430_CLR,
MSP430_CLRC,
MSP430_CLRN,
MSP430_CLRZ,
MSP430_DADC,
MSP430_DEC,
MSP430_DECD,
MSP430_DINT,
MSP430_EINT,
MSP430_INC,
MSP430_INCD,
MSP430_INV,
MSP430_NOP,
MSP430_POP,
MSP430_RET,
MSP430_RLA,
MSP430_RLC,
MSP430_SBC,
MSP430_SETC,
MSP430_SETN,
MSP430_SETZ,
MSP430_TST
} Msp430EmulatedOpcodes;

enum msp430_cmd_type {
typedef enum msp430_cmd_type {
MSP430_ONEOP,
MSP430_TWOOP,
MSP430_JUMP,
MSP430_INV,
};
MSP430_EMULATE,
MSP430_INVALID,
} Msp430CmdType;

enum msp430_registers {
typedef enum msp430_registers {
MSP430_PC,
MSP430_SP,
MSP430_SR,
Expand All @@ -76,25 +96,45 @@ enum msp430_registers {
MSP430_R13,
MSP430_R14,
MSP430_R15,
};
} Msp430Registers;

typedef enum Msp430AddressingMode {
MSP430_REG, ///< register: Rn, contents of Rn
MSP430_INDX, ///< indexed: offset(Rn), contents of Memory[offset + Rn]
MSP430_SYM, ///< symbolic: offset, contents of Memory[offset + PC] (as if indexed with Rn = PC)
MSP430_ABS, ///< absolute: &addr, contents of Memory[addr] (as if indexed with a zeroed Rn)
MSP430_IND_REG, ///< indirect register: @Rn, contents of Memory[Rn] (as if indexed with offset = 0)
MSP430_IND_AUTOINC, ///< indirect register auto-increment: @Rn+, same as with indirect register but automatically increments Rn
MSP430_IMM ///< immediate: #literal, the literal value itself is the argument
} Msp430AddressingMode;

struct msp430_cmd {
ut8 type;
ut8 opcode;
st16 jmp_addr;
ut16 call_addr;
ut8 jmp_cond;
/**
* \brief represents a disassembled instructions, also used for lifting
* */
typedef struct msp430_cmd {
ut8 type; ///< whether it's a one-operand, two-operand, emulated, jump or invalid
ut8 opcode; ///< which kind of operation

st16 jmp_addr; ///< for jumps, what's the address to jump to
ut8 jmp_cond; ///< for jumps, when will the jump be taken

// Null-delimited string representation of an assembly operation mnemonic.
// Length of array: 'i', 'n', 'v', 'a', 'l', 'i', 'd', '\0'
// (This is longer than any real assembly mnemonic.)
char instr[7 + 1];
char instr[7 + 1]; ///< Null-delimited string representation of an assembly operation mnemonic.

bool is_byte; ///< does it have a .b ?

// Null-delimited string representation of assembly operands.
// Length of array: 2 * ('0', 'x', 4-digit hexadecimal numeral, '(', 'r', 2-digit
// decimal numeral, ')'), ',', ' ', '\0'
char operands[2 * (2 + 4 + 2 + 3) + 2 + 1];
};
char operands[2 * (2 + 4 + 2 + 3) + 2 + 1]; ///< Null-delimited string representation of assembly operands.

// The source and the dst of the operands, along with their modes
// This info is contained in the strings above, but parsing strings to obtain it is ugly so we replicate it here
ut32 src; ///< src, doesn't get overwritten for eumlated instructions
ut32 dst; ///< dst, doesn't get overwritten for eumlated instructions
Msp430AddressingMode src_mode; ///< the addressing mode used by src, will determine how to interpret its 32 bits
Msp430AddressingMode dst_mode; ///< the addressing mode used by dst, will determine how to interpret its 32 bits
} Msp430Cmd;

int msp430_decode_command(const ut8 *instr, int len, struct msp430_cmd *cmd);
int msp430_decode_command(const ut8 *instr, int len, Msp430Cmd *cmd);
#endif /* MSP430_DISAS_H */
Loading

0 comments on commit 225d05a

Please sign in to comment.