Rizin
unix-like reverse engineering framework and cli tools
analysis_chip8.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2017 maijin <maijin21@gmail.com>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 #include <string.h>
5 #include <rz_types.h>
6 #include <rz_lib.h>
7 #include <rz_asm.h>
8 #include <rz_analysis.h>
9 
10 static int chip8_anop(RzAnalysis *analysis, RzAnalysisOp *op, ut64 addr, const ut8 *data, int len, RzAnalysisOpMask mask) {
11  ut16 opcode = rz_read_be16(data);
12  // uint8_t x = (opcode >> 8) & 0x0F;
13  // uint8_t y = (opcode >> 4) & 0x0F;
14  uint8_t nibble = opcode & 0x0F;
15  uint16_t nnn = opcode & 0x0FFF;
16  uint8_t kk = opcode & 0xFF;
17  op->size = 2;
18  op->addr = addr;
20  switch (opcode & 0xF000) {
21  case 0x0000:
22  if (opcode == 0x00EE) {
24  }
25  break;
26  case 0x1000:
28  op->jump = nnn;
29  break;
30  case 0x2000:
32  op->jump = nnn;
33  break;
34  case 0x3000:
36  op->jump = addr + op->size * 2;
37  op->fail = addr + op->size;
38  break;
39  case 0x4000:
41  op->jump = addr + op->size * 2;
42  op->fail = addr + op->size;
43  break;
44  case 0x5000:
46  op->jump = addr + op->size * 2;
47  op->fail = addr + op->size;
48  break;
49  case 0x6000:
51  break;
52  case 0x7000:
54  break;
55  case 0x8000: {
56  switch (nibble) {
57  case 0x0:
59  break;
60  case 0x1:
61  op->type = RZ_ANALYSIS_OP_TYPE_OR;
62  break;
63  case 0x2:
65  break;
66  case 0x3:
68  break;
69  case 0x4:
71  break;
72  case 0x5:
74  break;
75  case 0x6:
77  break;
78  case 0x7:
80  break;
81  case 0xE:
83  break;
84  }
85  } break;
86  case 0x9000:
87  if (nibble == 0) {
89  op->jump = addr + op->size * 2;
90  op->fail = addr + op->size;
91  }
92  break;
93  case 0xA000:
95  break;
96  case 0xB000:
98  /* FIXME: this is wrong as op->jump depends on register V0 */
99  op->jump = nnn;
100  break;
101  case 0xE000:
102  if (kk == 0x9E || kk == 0xA1) {
103  rz_meta_set_string(analysis, RZ_META_TYPE_COMMENT, addr, "KEYPAD");
105  op->jump = addr + op->size * 2;
106  op->fail = addr + op->size;
107  }
108  break;
109  case 0xF000: {
110  switch (kk) {
111  case 0x07:
112  op->type = RZ_ANALYSIS_OP_TYPE_MOV;
113  break;
114  case 0x0A:
115  rz_meta_set_string(analysis, RZ_META_TYPE_COMMENT, addr, "KEYPAD");
116  op->type = RZ_ANALYSIS_OP_TYPE_MOV;
117  break;
118  case 0x15:
119  op->type = RZ_ANALYSIS_OP_TYPE_MOV;
120  break;
121  case 0x18:
122  op->type = RZ_ANALYSIS_OP_TYPE_MOV;
123  break;
124  case 0x1E:
125  op->type = RZ_ANALYSIS_OP_TYPE_ADD;
126  break;
127  case 0x29:
129  break;
130  case 0x30:
132  break;
133  case 0x33:
135  break;
136  case 0x55:
138  break;
139  case 0x65:
141  break;
142  case 0x75:
144  break;
145  case 0x85:
147  break;
148  }
149  } break;
150  }
151  return op->size;
152 }
153 
155  .name = "chip8",
156  .desc = "CHIP8 analysis plugin",
157  .license = "LGPL3",
158  .arch = "chip8",
159  .bits = 32,
160  .op = &chip8_anop,
161 };
162 
163 #ifndef RZ_PLUGIN_INCORE
166  .data = &rz_analysis_plugin_chip8,
168 };
169 #endif
size_t len
Definition: 6502dis.c:15
RzAnalysisPlugin rz_analysis_plugin_chip8
RZ_API RzLibStruct rizin_plugin
static int chip8_anop(RzAnalysis *analysis, RzAnalysisOp *op, ut64 addr, const ut8 *data, int len, RzAnalysisOpMask mask)
#define mask()
#define RZ_API
uint16_t ut16
uint8_t ut8
Definition: lh5801.h:11
RZ_API bool rz_meta_set_string(RzAnalysis *a, RzAnalysisMetaType type, ut64 addr, const char *s)
Definition: meta.c:141
@ RZ_META_TYPE_COMMENT
Definition: rz_analysis.h:295
RzAnalysisOpMask
Definition: rz_analysis.h:439
@ RZ_ANALYSIS_OP_TYPE_SUB
Definition: rz_analysis.h:402
@ RZ_ANALYSIS_OP_TYPE_LOAD
Definition: rz_analysis.h:416
@ RZ_ANALYSIS_OP_TYPE_UNK
Definition: rz_analysis.h:388
@ RZ_ANALYSIS_OP_TYPE_JMP
Definition: rz_analysis.h:368
@ RZ_ANALYSIS_OP_TYPE_AND
Definition: rz_analysis.h:411
@ RZ_ANALYSIS_OP_TYPE_CALL
Definition: rz_analysis.h:378
@ RZ_ANALYSIS_OP_TYPE_ADD
Definition: rz_analysis.h:401
@ RZ_ANALYSIS_OP_TYPE_OR
Definition: rz_analysis.h:410
@ RZ_ANALYSIS_OP_TYPE_STORE
Definition: rz_analysis.h:415
@ RZ_ANALYSIS_OP_TYPE_SHR
Definition: rz_analysis.h:406
@ RZ_ANALYSIS_OP_TYPE_RJMP
Definition: rz_analysis.h:370
@ RZ_ANALYSIS_OP_TYPE_CJMP
Definition: rz_analysis.h:373
@ RZ_ANALYSIS_OP_TYPE_MOV
Definition: rz_analysis.h:390
@ RZ_ANALYSIS_OP_TYPE_SHL
Definition: rz_analysis.h:407
@ RZ_ANALYSIS_OP_TYPE_RET
Definition: rz_analysis.h:385
@ RZ_ANALYSIS_OP_TYPE_XOR
Definition: rz_analysis.h:412
static ut16 rz_read_be16(const void *src)
Definition: rz_endian.h:50
@ RZ_LIB_TYPE_ANALYSIS
Definition: rz_lib.h:73
#define RZ_VERSION
Definition: rz_version.h:8
unsigned short uint16_t
Definition: sftypes.h:30
unsigned char uint8_t
Definition: sftypes.h:31
const char * version
Definition: rz_analysis.h:1239
Definition: dis.c:32
ut64(WINAPI *w32_GetEnabledXStateFeatures)()
static int addr
Definition: z80asm.c:58