Rizin
unix-like reverse engineering framework and cli tools
gbdis.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2013-2018 condret <condr3t@protonmail.com>
2 // SPDX-FileCopyrightText: 2013-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 <stdio.h>
10 #include <string.h>
11 #include "gb_op_table.h"
12 
13 static int gbOpLength(int gboptype) {
14  switch (gboptype) {
15  case GB_8BIT:
16  return 1;
17  case GB_8BIT + ARG_8 + GB_IO:
18  case GB_8BIT + ARG_8:
19  case GB_16BIT:
20  return 2;
21  case GB_8BIT + ARG_16:
22  return 3;
23  default:
24  return 0;
25  }
26 }
27 
28 static void gb_hardware_register_name(char *reg, ut8 offset) {
29  switch (offset) {
30  case 0x00: // Joy pad info
31  rz_str_cpy(reg, "rP1") break;
32  case 0x01: // Serial Transfer Data
33  rz_str_cpy(reg, "rSB") break;
34  case 0x02: // Serial I/O Control
35  rz_str_cpy(reg, "rSC") break;
36  case 0x04: // Divider register
37  rz_str_cpy(reg, "rDIV") break;
38  case 0x05: // Timer Counter
39  rz_str_cpy(reg, "rTIMA") break;
40  case 0x06: // Timer modulo
41  rz_str_cpy(reg, "rTMA") break;
42  case 0x07: // Timer control
43  rz_str_cpy(reg, "rTAC") break;
44  case 0x0f: // Interrupt Flag
45  rz_str_cpy(reg, "rIF") break;
46  // Audio Channel #1
47  case 0x10: // Sweep Register
48  rz_str_cpy(reg, "rAUD1SWEEP") break;
49  case 0x11: // Sound length/Wave pattern duty
50  rz_str_cpy(reg, "rAUD1LEN") break;
51  case 0x12: // Envelope
52  rz_str_cpy(reg, "rAUD1ENV") break;
53  case 0x13: // Frequency low
54  rz_str_cpy(reg, "rAUD1LOW") break;
55  case 0x14: // Frequency high
56  rz_str_cpy(reg, "rAUD1HIGH") break;
57  // Audio Channel #2
58  case 0x16: // Sound length/Wave pattern duty
59  rz_str_cpy(reg, "rAUD2LEN") break;
60  case 0x17: // Envelope
61  rz_str_cpy(reg, "rAUD2ENV") break;
62  case 0x18: // Frequency low
63  rz_str_cpy(reg, "rAUD2LOW") break;
64  case 0x19: // Frequency high
65  rz_str_cpy(reg, "rAUD2HIGH") break;
66  // Sound Channel #3
67  case 0x1a: // Sound on/off
68  rz_str_cpy(reg, "rAUD3ENA") break;
69  case 0x1b: // Sound length
70  rz_str_cpy(reg, "rAUD3LEN") break;
71  case 0x1c: // Select output level
72  rz_str_cpy(reg, "rAUD3LEVEL") break;
73  case 0x1d: // Frequency low
74  rz_str_cpy(reg, "rAUD3LOW") break;
75  case 0x1e: // Frequency high
76  rz_str_cpy(reg, "rAUD3HIGH") break;
77  // Sound Channel #4
78  case 0x20: // Sound length
79  rz_str_cpy(reg, "rAUD4LEN") break;
80  case 0x21: // Envelope
81  rz_str_cpy(reg, "rAUD4ENV") break;
82  case 0x22: // Polynomial counter
83  rz_str_cpy(reg, "rAUD4POLY") break;
84  // Sound (general)
85  case 0x23:
86  rz_str_cpy(reg, "rAUD4GO") break;
87  case 0x24: // Channel control / ON-OFF / Volume
88  rz_str_cpy(reg, "rAUDVOL") break;
89  case 0x25: // Selection of Sound output terminal
90  rz_str_cpy(reg, "rAUDTERM") break;
91  case 0x26: // Sound on/off
92  rz_str_cpy(reg, "rAUDENA") break;
93  case 0x76: // Sound Channel 1&2 PCM amplitude
94  rz_str_cpy(reg, "rPCM12") break;
95  case 0x77: // Sound Channel 3&4 PCM amplitude
96  rz_str_cpy(reg, "rPCM34") break;
97  case 0x40: // LCD Control
98  rz_str_cpy(reg, "rLCDC") break;
99  case 0x41: // LCD Status
100  rz_str_cpy(reg, "rSTAT") break;
101  case 0x42: // Scroll Y
102  rz_str_cpy(reg, "rSCY") break;
103  case 0x43: // Scroll X
104  rz_str_cpy(reg, "rSCX") break;
105  case 0x44: // Y-Coordinate
106  rz_str_cpy(reg, "rLY") break;
107  case 0x45: // Y-Coordinate Compare
108  rz_str_cpy(reg, "rLYC") break;
109  case 0x46: // Transfer and Start Address
110  rz_str_cpy(reg, "rDMA") break;
111  case 0x47: // BG Palette Data
112  rz_str_cpy(reg, "rBGP") break;
113  case 0x48: // Object Palette 0 Data
114  rz_str_cpy(reg, "rOBP0") break;
115  case 0x49: // Object Palette 1 Data
116  rz_str_cpy(reg, "rOBP1") break;
117  case 0x4a: // Window Y Position
118  rz_str_cpy(reg, "rWY") break;
119  case 0x4b: // Window X Position
120  rz_str_cpy(reg, "rWX") break;
121  case 0x4d: // Select CPU Speed
122  rz_str_cpy(reg, "rKEY1") break;
123  case 0x4f: // Select Video RAM Bank
124  rz_str_cpy(reg, "rVBK") break;
125  case 0x51: // Horizontal Blanking, General Purpose DMA
126  case 0x52: // Horizontal Blanking, General Purpose DMA
127  case 0x53: // Horizontal Blanking, General Purpose DMA
128  case 0x54: // Horizontal Blanking, General Purpose DMA
129  case 0x55: // Horizontal Blanking, General Purpose DMA
130  sprintf(reg, "rHDMA%d", offset - 0x50);
131  break;
132  case 0x56: // Infrared Communications Port
133  rz_str_cpy(reg, "rRP") break;
134  case 0x68: // Background Color Palette Specification
135  rz_str_cpy(reg, "rBCPS") break;
136  case 0x69: // Background Color Palette Data
137  rz_str_cpy(reg, "rBCPD") break;
138  case 0x6a: // Object Color Palette Specification
139  rz_str_cpy(reg, "rOCPS") break;
140  case 0x6b: // Object Color Palette Data
141  rz_str_cpy(reg, "rOCPD") break;
142  case 0x70: // Select Main RAM Bank
143  rz_str_cpy(reg, "rSVBK") break;
144  case 0xff: // Interrupt Enable Flag
145  rz_str_cpy(reg, "rIE") break;
146  default:
147  // If unknown, return the original address
148  sprintf(reg, "0xff%02x", offset);
149  break;
150  }
151 }
152 
153 #ifndef GB_DIS_LEN_ONLY
154 static int gbDisass(RzAsmOp *op, const ut8 *buf, int len) {
155  int foo = gbOpLength(gb_op[buf[0]].type);
156  if (len < foo) {
157  return 0;
158  }
159  const char *buf_asm = "invalid";
160  char reg[32];
161  memset(reg, '\0', sizeof(reg));
162  switch (gb_op[buf[0]].type) {
163  case GB_8BIT:
164  buf_asm = sdb_fmt("%s", gb_op[buf[0]].name);
165  break;
166  case GB_16BIT:
167  buf_asm = sdb_fmt("%s %s", cb_ops[buf[1] >> 3u], cb_regs[buf[1] & 7u]);
168  break;
169  case GB_8BIT + ARG_8:
170  buf_asm = sdb_fmt(gb_op[buf[0]].name, buf[1]);
171  break;
172  case GB_8BIT + ARG_16:
173  buf_asm = sdb_fmt(gb_op[buf[0]].name, buf[1] + 0x100 * buf[2]);
174  break;
175  case GB_8BIT + ARG_8 + GB_IO:
177  buf_asm = sdb_fmt(gb_op[buf[0]].name, reg);
178  break;
179  }
180  rz_strbuf_set(&op->buf_asm, buf_asm);
181  return foo;
182 }
183 #endif
size_t len
Definition: 6502dis.c:15
RZ_API char * sdb_fmt(const char *fmt,...)
Definition: fmt.c:26
#define GB_IO
Definition: gb_op_table.h:8
static const char * cb_ops[]
Definition: gb_op_table.h:15
#define GB_16BIT
Definition: gb_op_table.h:5
#define ARG_16
Definition: gb_op_table.h:7
#define ARG_8
Definition: gb_op_table.h:6
static const char * cb_regs[]
Definition: gb_op_table.h:20
static gb_opcode gb_op[]
Definition: gb_op_table.h:22
#define GB_8BIT
Definition: gb_op_table.h:4
static void gb_hardware_register_name(char *reg, ut8 offset)
Definition: gbdis.c:28
static int gbOpLength(int gboptype)
Definition: gbdis.c:13
voidpf uLong offset
Definition: ioapi.h:144
voidpf void * buf
Definition: ioapi.h:138
sprintf
Definition: kernel.h:365
#define reg(n)
uint8_t ut8
Definition: lh5801.h:11
return memset(p, 0, total)
static RzMain foo[]
Definition: main.c:11
int type
Definition: mipsasm.c:17
#define rz_str_cpy(x, y)
Definition: rz_str.h:109
RZ_API const char * rz_strbuf_set(RzStrBuf *sb, const char *s)
Definition: strbuf.c:153
Definition: z80asm.h:102
Definition: dis.c:32