Rizin
unix-like reverse engineering framework and cli tools
dis.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include "dcpu16.h"

Go to the source code of this file.

Classes

struct  op_code
 
struct  op_basic
 
struct  op_nbasic
 
union  op
 

Functions

static int needWord (ut8 type)
 
static int valPrint (char *out, size_t size_out, ut8 type, ut16 value)
 
static int instrPrint (char *out, size_t size_out, const op *o)
 
static int instrGet (ut16 in, op *o, ut16 a, ut16 b)
 
static int instrGetCycles (const op *o)
 
int dcpu16_disasm (char *out, size_t size_out, const ut16 *inp, int len, int *cost)
 

Variables

static const int opCycle []
 
static const int opCycleB [] = { 0, 2 }
 
static const char * opName []
 
static const char * opNameB [] = { "reserved", "jsr" }
 
static const char * regs []
 

Function Documentation

◆ dcpu16_disasm()

int dcpu16_disasm ( char *  out,
size_t  size_out,
const ut16 inp,
int  len,
int cost 
)

Definition at line 130 of file dis.c.

130  {
131  op o = { { 0 } };
132  int delta = instrGet(inp[0], &o, inp[1], inp[2]);
133  if (cost)
134  *cost = instrGetCycles(&o) + ((o.b.opcode >= 0xc) ? 1 : 0);
135  instrPrint(out, size_out, &o);
136  // ind = (o.b.opcode >= 0xC);
137  return delta << 1;
138 }
const lzma_allocator const uint8_t size_t uint8_t * out
Definition: block.h:528
static int instrPrint(char *out, size_t size_out, const op *o)
Definition: dis.c:77
static int instrGetCycles(const op *o)
Definition: dis.c:124
static int instrGet(ut16 in, op *o, ut16 a, ut16 b)
Definition: dis.c:94
ut8 opcode
Definition: dis.c:17
Definition: dis.c:32
struct op_basic b
Definition: dis.c:34
static st64 delta
Definition: vmenus.c:2425

References op::b, delta, instrGet(), instrGetCycles(), instrPrint(), op_basic::opcode, and out.

Referenced by disassemble().

◆ instrGet()

static int instrGet ( ut16  in,
op o,
ut16  a,
ut16  b 
)
static

Definition at line 94 of file dis.c.

94  {
95  int ret = 1;
96  o->code.opcode = in & 0xF;
97  if (!(o->code.opcode = in & 0xF)) {
98  /* Non basic op code */
99  o->n.opcode = (in >> 4) & 0x3F;
100  o->n.a_type = (in >> 10) & 0x3F;
101 
102  if (needWord(o->n.a_type)) {
103  o->n.a = a;
104  ret++;
105  }
106  } else {
107  o->b.a_type = (in >> 4) & 0x3F;
108  o->b.b_type = (in >> 10) & 0x3F;
109  if (needWord(o->b.a_type)) {
110  o->b.a = a;
111  ret++;
112  if (needWord(o->b.b_type)) {
113  o->b.b = b;
114  ret++;
115  }
116  } else if (needWord(o->b.b_type)) {
117  o->b.b = a;
118  ret++;
119  }
120  }
121  return ret;
122 }
const lzma_allocator const uint8_t * in
Definition: block.h:527
static int needWord(ut8 type)
Definition: dis.c:57
#define b(i)
Definition: sha256.c:42
#define a(i)
Definition: sha256.c:41
ut16 a
Definition: dis.c:21
ut16 b
Definition: dis.c:22
ut8 b_type
Definition: dis.c:19
ut8 a_type
Definition: dis.c:18
ut8 opcode
Definition: dis.c:13
ut8 a_type
Definition: dis.c:27
ut16 a
Definition: dis.c:29
ut8 opcode
Definition: dis.c:28
struct op_nbasic n
Definition: dis.c:35
struct op_code code
Definition: dis.c:33

References op_basic::a, op_nbasic::a, a, op_basic::a_type, op_nbasic::a_type, op_basic::b, op::b, b, op_basic::b_type, op::code, in, op::n, needWord(), op_code::opcode, and op_nbasic::opcode.

Referenced by dcpu16_disasm().

◆ instrGetCycles()

static int instrGetCycles ( const op o)
static

Definition at line 124 of file dis.c.

124  {
125  if (o->code.opcode == 0)
126  return opCycleB[o->n.opcode] + needWord(o->n.a_type);
127  return opCycle[o->b.opcode] + needWord(o->b.a_type) + needWord(o->b.b_type);
128 }
static const int opCycle[]
Definition: dis.c:38
static const int opCycleB[]
Definition: dis.c:41

References op_basic::a_type, op_nbasic::a_type, op::b, op_basic::b_type, op::code, op::n, needWord(), op_code::opcode, op_basic::opcode, op_nbasic::opcode, opCycle, and opCycleB.

Referenced by dcpu16_disasm().

◆ instrPrint()

static int instrPrint ( char *  out,
size_t  size_out,
const op o 
)
static

Definition at line 77 of file dis.c.

77  {
78  char arg[32], arg2[32];
79  if (o->code.opcode == 0) {
80  valPrint(arg, sizeof(arg), o->n.a_type, o->n.a);
81  if (o->n.opcode > 1) {
82  strncpy(out, "invalid", size_out);
83  out[size_out - 1] = 0;
84  return strlen(out);
85  }
86  return snprintf(out, size_out, "%s %s",
87  opNameB[o->n.opcode], arg);
88  }
89  valPrint(arg, sizeof(arg), o->b.a_type, o->b.a);
90  valPrint(arg2, sizeof(arg2), o->b.b_type, o->b.b);
91  return snprintf(out, size_out, "%s %s, %s", opName[o->b.opcode], arg, arg2);
92 }
static int valPrint(char *out, size_t size_out, ut8 type, ut16 value)
Definition: dis.c:61
static const char * opNameB[]
Definition: dis.c:50
static const char * opName[]
Definition: dis.c:43
snprintf
Definition: kernel.h:364

References op_basic::a, op_nbasic::a, op_basic::a_type, op_nbasic::a_type, op_basic::b, op::b, op_basic::b_type, op::code, op::n, op_code::opcode, op_basic::opcode, op_nbasic::opcode, opName, opNameB, out, snprintf, and valPrint().

Referenced by dcpu16_disasm().

◆ needWord()

static int needWord ( ut8  type)
inlinestatic

Definition at line 57 of file dis.c.

57  {
58  return ((type <= 0x17) && (type > 0x0f)) || (type == 0x1e) || (type == 0x1f);
59 }
int type
Definition: mipsasm.c:17

References type.

Referenced by instrGet(), and instrGetCycles().

◆ valPrint()

static int valPrint ( char *  out,
size_t  size_out,
ut8  type,
ut16  value 
)
static

Definition at line 61 of file dis.c.

61  {
62  if (type <= 0x07)
63  return snprintf(out, size_out, "%s", regs[type]);
64  if (type <= 0x0f)
65  return snprintf(out, size_out, "[%s]", regs[type - 0x08]);
66  if (type <= 0x17)
67  return snprintf(out, size_out, "[%s + %#hx]", regs[type - 0x10], value);
68  if (type <= 0x1d)
69  return snprintf(out, size_out, "%s", regs[type - 0x18 + 0x08]);
70  if (type == 0x1e)
71  return snprintf(out, size_out, "[%#hx]", value);
72  if (type == 0x1f)
73  return snprintf(out, size_out, "%#hx", value);
74  return snprintf(out, size_out, "%#hx", (short)(type - 0x20));
75 }
static int value
Definition: cmd_api.c:93
static const char * regs[]
Definition: dis.c:52

References out, regs, snprintf, type, and value.

Referenced by instrPrint().

Variable Documentation

◆ opCycle

const int opCycle[]
static
Initial value:
= {
0, 1, 2, 2, 2, 3, 3, 2, 2, 1, 1, 1, 2, 2, 2, 2
}

Definition at line 38 of file dis.c.

Referenced by instrGetCycles().

◆ opCycleB

const int opCycleB[] = { 0, 2 }
static

Definition at line 41 of file dis.c.

Referenced by instrGetCycles().

◆ opName

const char* opName[]
static
Initial value:
= {
"", "set",
"add", "sub", "mul", "div", "mod",
"shl", "shr", "and", "bor", "xor",
"ife", "ifn", "ifg", "ifb"
}

Definition at line 43 of file dis.c.

Referenced by instrPrint().

◆ opNameB

const char* opNameB[] = { "reserved", "jsr" }
static

Definition at line 50 of file dis.c.

Referenced by instrPrint().

◆ regs

const char* regs[]
static
Initial value:
= {
"a", "b", "c", "x", "y", "z", "i", "j",
"pop", "peek", "push", "sp", "pc", "o"
}

Definition at line 52 of file dis.c.

Referenced by valPrint().