Rizin
unix-like reverse engineering framework and cli tools
snesdis.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2015-2018 condret <condr3t@protonmail.com>
2 // SPDX-FileCopyrightText: 2015-2018 pancake <pancake@nopcode.org>
3 // SPDX-License-Identifier: LGPL-3.0-only
4 
5 #include <rz_types.h>
6 #include <rz_util.h>
7 #include <rz_asm.h>
8 #include <rz_lib.h>
9 #include <string.h>
10 #include "snes_op_table.h"
11 
12 static int snesDisass(int M_flag, int X_flag, ut64 pc, RzAsmOp *op, const ut8 *buf, int len) {
13  snes_op_t *s_op = &snes_op[buf[0]];
14  int op_len = snes_op_get_size(M_flag, X_flag, s_op);
15  if (len < op_len) {
16  return 0;
17  }
18  const char *buf_asm = "invalid";
19  switch (s_op->len) {
20  case SNES_OP_8BIT:
21  buf_asm = s_op->name;
22  break;
23  case SNES_OP_16BIT:
24  if (*buf % 0x20 == 0x10 || *buf == 0x80) { // relative branch
25  buf_asm = sdb_fmt(s_op->name, (ut32)(pc + 2 + (st8)buf[1]));
26  } else {
27  buf_asm = sdb_fmt(s_op->name, buf[1]);
28  }
29  break;
30  case SNES_OP_24BIT:
31  if (*buf == 0x44 || *buf == 0x54) { // mvp and mvn
32  buf_asm = sdb_fmt(s_op->name, buf[1], buf[2]);
33  } else if (*buf == 0x82) { // brl
34  buf_asm = sdb_fmt(s_op->name, pc + 3 + (st16)rz_read_le16(buf + 1));
35  } else {
36  buf_asm = sdb_fmt(s_op->name, rz_read_le16(buf + 1));
37  }
38  break;
39  case SNES_OP_32BIT:
40  buf_asm = sdb_fmt(s_op->name, buf[1] | buf[2] << 8 | buf[3] << 16);
41  break;
42  case SNES_OP_IMM_M:
43  if (M_flag) {
44  buf_asm = sdb_fmt("%s #0x%02x", s_op->name, buf[1]);
45  } else {
46  buf_asm = sdb_fmt("%s #0x%04x", s_op->name, rz_read_le16(buf + 1));
47  }
48  break;
49  case SNES_OP_IMM_X:
50  if (X_flag) {
51  buf_asm = sdb_fmt("%s #0x%02x", s_op->name, buf[1]);
52  } else {
53  buf_asm = sdb_fmt("%s #0x%04x", s_op->name, rz_read_le16(buf + 1));
54  }
55  break;
56  }
57  rz_strbuf_set(&op->buf_asm, buf_asm);
58  return op_len;
59 }
size_t len
Definition: 6502dis.c:15
uint32_t ut32
RZ_API char * sdb_fmt(const char *fmt,...)
Definition: fmt.c:26
voidpf void * buf
Definition: ioapi.h:138
uint8_t ut8
Definition: lh5801.h:11
static ut16 rz_read_le16(const void *src)
Definition: rz_endian.h:206
RZ_API const char * rz_strbuf_set(RzStrBuf *sb, const char *s)
Definition: strbuf.c:153
#define st8
Definition: rz_types_base.h:16
#define st16
Definition: rz_types_base.h:14
@ SNES_OP_IMM_M
Definition: snes_op_table.h:14
@ SNES_OP_24BIT
Definition: snes_op_table.h:12
@ SNES_OP_IMM_X
Definition: snes_op_table.h:15
@ SNES_OP_32BIT
Definition: snes_op_table.h:13
@ SNES_OP_16BIT
Definition: snes_op_table.h:11
@ SNES_OP_8BIT
Definition: snes_op_table.h:10
static snes_op_t snes_op[]
Definition: snes_op_table.h:33
static int snes_op_get_size(int M_flag, int X_flag, snes_op_t *op)
Definition: snes_op_table.h:24
static int snesDisass(int M_flag, int X_flag, ut64 pc, RzAsmOp *op, const ut8 *buf, int len)
Definition: snesdis.c:12
const char * name
Definition: snes_op_table.h:19
Definition: dis.c:32
ut64(WINAPI *w32_GetEnabledXStateFeatures)()