Rizin
unix-like reverse engineering framework and cli tools
pic_baseline.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2018 thestr4ng3r <info@florianmaerkl.de>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 #include "pic_baseline.h"
5 
7  { "nop", PIC_BASELINE_OP_ARGS_NONE },
8  { "option", PIC_BASELINE_OP_ARGS_NONE },
9  { "sleep", PIC_BASELINE_OP_ARGS_NONE },
10  { "clrwdt", PIC_BASELINE_OP_ARGS_NONE },
11  { "tris", PIC_BASELINE_OP_ARGS_3F },
12  { "movlb", PIC_BASELINE_OP_ARGS_3K },
13  { "return", PIC_BASELINE_OP_ARGS_NONE },
14  { "retfie", PIC_BASELINE_OP_ARGS_NONE },
15  { "movwf", PIC_BASELINE_OP_ARGS_5F },
16  { "clrf", PIC_BASELINE_OP_ARGS_5F },
17  { "clrw", PIC_BASELINE_OP_ARGS_NONE },
18  { "subwf", PIC_BASELINE_OP_ARGS_1D_5F },
19  { "decf", PIC_BASELINE_OP_ARGS_1D_5F },
20  { "iorwf", PIC_BASELINE_OP_ARGS_1D_5F },
21  { "andwf", PIC_BASELINE_OP_ARGS_1D_5F },
22  { "xorwf", PIC_BASELINE_OP_ARGS_1D_5F },
23  { "andwf", PIC_BASELINE_OP_ARGS_1D_5F },
24  { "movf", PIC_BASELINE_OP_ARGS_1D_5F },
25  { "comf", PIC_BASELINE_OP_ARGS_1D_5F },
26  { "incf", PIC_BASELINE_OP_ARGS_1D_5F },
27  { "decfsz", PIC_BASELINE_OP_ARGS_1D_5F },
28  { "rrf", PIC_BASELINE_OP_ARGS_1D_5F },
29  { "rlf", PIC_BASELINE_OP_ARGS_1D_5F },
30  { "swapf", PIC_BASELINE_OP_ARGS_1D_5F },
31  { "incfsz", PIC_BASELINE_OP_ARGS_1D_5F },
32  { "bcf", PIC_BASELINE_OP_ARGS_3B_5F },
33  { "bsf", PIC_BASELINE_OP_ARGS_3B_5F },
34  { "btfsc", PIC_BASELINE_OP_ARGS_3B_5F },
35  { "btfss", PIC_BASELINE_OP_ARGS_3B_5F },
36  { "retlw", PIC_BASELINE_OP_ARGS_8K },
37  { "call", PIC_BASELINE_OP_ARGS_8K },
38  { "goto", PIC_BASELINE_OP_ARGS_9K },
39  { "movlw", PIC_BASELINE_OP_ARGS_8K },
40  { "iorlw", PIC_BASELINE_OP_ARGS_8K },
41  { "andlw", PIC_BASELINE_OP_ARGS_8K },
42  { "xorlw", PIC_BASELINE_OP_ARGS_8K }
43 };
44 
46  if (instr & 0xf000) {
48  }
49 
50  switch ((instr >> 6) & 0x3f) { // first 6 bits
51  case 0x7: // 0b000111
53  case 0x5: // 0b000101
55  case 0x1: // 0b000001
56  if (instr & (1 << 5)) {
58  }
59  if ((instr & 0x1f) == 0) { // last 5 bits
61  }
63  case 0x9: // 0b001001
65  case 0x3: // 0b000011
67  case 0xb: // 0b001011
69  case 0xa: // 0b001010
71  case 0xf: // 0b001111
73  case 0x4: // 0b000100
75  case 0x8: // 0b001000
77  case 0x0:
78  if (instr & (1 << 5)) {
80  } else {
81  switch (instr & 0x1f) { // last 5 bits
82  case 0x0: // 0b00000
84  case 0x4: // 0b00100
86  case 0x2: // 0b00010
88  case 0x3: // 0b00011
90  case 0x1: // 0b00001
91  case 0x5: // 0b00101
92  case 0x6: // 0b00110
93  case 0x7: // 0b00111
95  case 0x10: // 0b10000
96  case 0x11: // 0b10001
97  case 0x12: // 0b10010
98  case 0x13: // 0b10011
99  case 0x14: // 0b10100
100  case 0x15: // 0b10101
101  case 0x16: // 0b10110
102  case 0x17: // 0b10111
104  case 0x1e: // 0b11110
106  case 0x1f: // 0b11111
108  default:
110  }
111  }
112  case 0xd: // 0b001101
114  case 0xc: // 0b001100
116  case 0x2: // 0b000010
118  case 0xe: // 0b001110
120  case 0x6: // 0b000110
122  case 0x10: // 0b010000
123  case 0x11: // 0b010001
124  case 0x12: // 0b010010
125  case 0x13: // 0b010011
127  case 0x14: // 0b010100
128  case 0x15: // 0b010101
129  case 0x16: // 0b010110
130  case 0x17: // 0b010111
132  case 0x18: // 0b011000
133  case 0x19: // 0b011001
134  case 0x1a: // 0b011010
135  case 0x1b: // 0b011011
137  case 0x1c: // 0b011100
138  case 0x1d: // 0b011101
139  case 0x1e: // 0b011110
140  case 0x1f: // 0b011111
142  case 0x38: // 0b111000
143  case 0x39: // 0b111001
144  case 0x3a: // 0b111010
145  case 0x3b: // 0b111011
147  case 0x24: // 0b100100
148  case 0x25: // 0b100101
149  case 0x26: // 0b100110
150  case 0x27: // 0b100111
152  case 0x28: // 0b101000
153  case 0x29: // 0b101001
154  case 0x2a: // 0b101010
155  case 0x2b: // 0b101011
156  case 0x2c: // 0b101100
157  case 0x2d: // 0b101101
158  case 0x2e: // 0b101110
159  case 0x2f: // 0b101111
161  case 0x34: // 0b110100
162  case 0x35: // 0b110101
163  case 0x36: // 0b110110
164  case 0x37: // 0b110111
166  case 0x30: // 0b110000
167  case 0x31: // 0b110001
168  case 0x32: // 0b110010
169  case 0x33: // 0b110011
171  case 0x20: // 0b100000
172  case 0x21: // 0b100001
173  case 0x22: // 0b100010
174  case 0x23: // 0b100011
176  case 0x3c: // 0b111100
177  case 0x3d: // 0b111101
178  case 0x3e: // 0b111110
179  case 0x3f: // 0b111111
181  default:
183  }
184 }
185 
187  if (opcode >= PIC_BASELINE_OPCODE_INVALID) {
188  return NULL;
189  }
190  return &pic_baseline_op_info[opcode];
191 }
192 
193 int pic_baseline_disassemble(RzAsmOp *op, char *opbuf, const ut8 *b, int l) {
194 #define EMIT_INVALID \
195  { \
196  op->size = 1; \
197  strcpy(opbuf, "invalid"); \
198  return 1; \
199  }
200  if (!b || l < 2) {
202  }
203 
204  ut16 instr = rz_read_le16(b);
206  if (opcode == PIC_BASELINE_OPCODE_INVALID) {
208  }
209 
210  const PicBaselineOpInfo *op_info = pic_baseline_get_op_info(opcode);
211  if (!op_info) {
213  }
214 
215 #undef EMIT_INVALID
216 
217  op->size = 2;
218 
219  const char *buf_asm = "invalid";
220  switch (op_info->args) {
222  buf_asm = op_info->mnemonic;
223  break;
225  buf_asm = sdb_fmt("%s 0x%x", op_info->mnemonic, instr & PIC_BASELINE_OP_ARGS_2F_MASK_F);
226  break;
228  buf_asm = sdb_fmt("%s 0x%x", op_info->mnemonic, instr & PIC_BASELINE_OP_ARGS_3F_MASK_F);
229  break;
231  buf_asm = sdb_fmt("%s 0x%x", op_info->mnemonic, instr & PIC_BASELINE_OP_ARGS_3K_MASK_K);
232  break;
234  buf_asm = sdb_fmt("%s 0x%x, %c", op_info->mnemonic, instr & PIC_BASELINE_OP_ARGS_1D_5F_MASK_F,
235  (instr & PIC_BASELINE_OP_ARGS_1D_5F_MASK_D) >> 5 ? 'f' : 'w');
236  break;
238  buf_asm = sdb_fmt("%s 0x%x", op_info->mnemonic, instr & PIC_BASELINE_OP_ARGS_5F_MASK_F);
239  break;
241  buf_asm = sdb_fmt("%s 0x%x, 0x%x", op_info->mnemonic, instr & PIC_BASELINE_OP_ARGS_3B_5F_MASK_F,
242  (instr & PIC_BASELINE_OP_ARGS_3B_5F_MASK_B) >> 5);
243  break;
245  buf_asm = sdb_fmt("%s 0x%x", op_info->mnemonic, instr & PIC_BASELINE_OP_ARGS_8K_MASK_K);
246  break;
248  buf_asm = sdb_fmt("%s 0x%x", op_info->mnemonic, instr & PIC_BASELINE_OP_ARGS_9K_MASK_K);
249  break;
250  }
251  strcpy(opbuf, buf_asm);
252 
253  return op->size;
254 }
#define NULL
Definition: cris-opc.c:27
uint16_t ut16
RZ_API char * sdb_fmt(const char *fmt,...)
Definition: fmt.c:26
uint8_t ut8
Definition: lh5801.h:11
int pic_baseline_disassemble(RzAsmOp *op, char *opbuf, const ut8 *b, int l)
Definition: pic_baseline.c:193
#define EMIT_INVALID
const PicBaselineOpInfo * pic_baseline_get_op_info(PicBaselineOpcode opcode)
Definition: pic_baseline.c:186
static const PicBaselineOpInfo pic_baseline_op_info[PIC_BASELINE_OPCODE_INVALID]
Definition: pic_baseline.c:6
PicBaselineOpcode pic_baseline_get_opcode(ut16 instr)
Definition: pic_baseline.c:45
#define PIC_BASELINE_OP_ARGS_5F_MASK_F
Definition: pic_baseline.h:27
#define PIC_BASELINE_OP_ARGS_2F_MASK_F
Definition: pic_baseline.h:22
#define PIC_BASELINE_OP_ARGS_9K_MASK_K
Definition: pic_baseline.h:31
#define PIC_BASELINE_OP_ARGS_3F_MASK_F
Definition: pic_baseline.h:23
#define PIC_BASELINE_OP_ARGS_3B_5F_MASK_B
Definition: pic_baseline.h:28
#define PIC_BASELINE_OP_ARGS_8K_MASK_K
Definition: pic_baseline.h:30
#define PIC_BASELINE_OP_ARGS_1D_5F_MASK_D
Definition: pic_baseline.h:25
#define PIC_BASELINE_OP_ARGS_3K_MASK_K
Definition: pic_baseline.h:24
#define PIC_BASELINE_OP_ARGS_1D_5F_MASK_F
Definition: pic_baseline.h:26
PicBaselineOpcode
Definition: pic_baseline.h:38
@ PIC_BASELINE_OPCODE_MOVWF
Definition: pic_baseline.h:47
@ PIC_BASELINE_OPCODE_BTFSS
Definition: pic_baseline.h:67
@ PIC_BASELINE_OPCODE_RETFIE
Definition: pic_baseline.h:46
@ PIC_BASELINE_OPCODE_COMF
Definition: pic_baseline.h:57
@ PIC_BASELINE_OPCODE_SLEEP
Definition: pic_baseline.h:41
@ PIC_BASELINE_OPCODE_TRIS
Definition: pic_baseline.h:43
@ PIC_BASELINE_OPCODE_RRF
Definition: pic_baseline.h:60
@ PIC_BASELINE_OPCODE_RETLW
Definition: pic_baseline.h:68
@ PIC_BASELINE_OPCODE_SWAPF
Definition: pic_baseline.h:62
@ PIC_BASELINE_OPCODE_MOVF
Definition: pic_baseline.h:56
@ PIC_BASELINE_OPCODE_CALL
Definition: pic_baseline.h:69
@ PIC_BASELINE_OPCODE_SUBWF
Definition: pic_baseline.h:50
@ PIC_BASELINE_OPCODE_BCF
Definition: pic_baseline.h:64
@ PIC_BASELINE_OPCODE_BTFSC
Definition: pic_baseline.h:66
@ PIC_BASELINE_OPCODE_MOVLW
Definition: pic_baseline.h:71
@ PIC_BASELINE_OPCODE_CLRW
Definition: pic_baseline.h:49
@ PIC_BASELINE_OPCODE_NOP
Definition: pic_baseline.h:39
@ PIC_BASELINE_OPCODE_RLF
Definition: pic_baseline.h:61
@ PIC_BASELINE_OPCODE_OPTION
Definition: pic_baseline.h:40
@ PIC_BASELINE_OPCODE_ANDWF
Definition: pic_baseline.h:53
@ PIC_BASELINE_OPCODE_RETURN
Definition: pic_baseline.h:45
@ PIC_BASELINE_OPCODE_BSF
Definition: pic_baseline.h:65
@ PIC_BASELINE_OPCODE_DECFSZ
Definition: pic_baseline.h:59
@ PIC_BASELINE_OPCODE_IORWF
Definition: pic_baseline.h:52
@ PIC_BASELINE_OPCODE_XORLW
Definition: pic_baseline.h:74
@ PIC_BASELINE_OPCODE_INCF
Definition: pic_baseline.h:58
@ PIC_BASELINE_OPCODE_XORWF
Definition: pic_baseline.h:54
@ PIC_BASELINE_OPCODE_DECF
Definition: pic_baseline.h:51
@ PIC_BASELINE_OPCODE_CLRWDT
Definition: pic_baseline.h:42
@ PIC_BASELINE_OPCODE_IORLW
Definition: pic_baseline.h:72
@ PIC_BASELINE_OPCODE_INCFSZ
Definition: pic_baseline.h:63
@ PIC_BASELINE_OPCODE_INVALID
Definition: pic_baseline.h:75
@ PIC_BASELINE_OPCODE_MOVLB
Definition: pic_baseline.h:44
@ PIC_BASELINE_OPCODE_ANDLW
Definition: pic_baseline.h:73
@ PIC_BASELINE_OPCODE_ADDWF
Definition: pic_baseline.h:55
@ PIC_BASELINE_OPCODE_CLRF
Definition: pic_baseline.h:48
@ PIC_BASELINE_OPCODE_GOTO
Definition: pic_baseline.h:70
@ PIC_BASELINE_OP_ARGS_1D_5F
Definition: pic_baseline.h:15
@ PIC_BASELINE_OP_ARGS_5F
Definition: pic_baseline.h:16
@ PIC_BASELINE_OP_ARGS_3K
Definition: pic_baseline.h:14
@ PIC_BASELINE_OP_ARGS_3F
Definition: pic_baseline.h:13
@ PIC_BASELINE_OP_ARGS_8K
Definition: pic_baseline.h:18
@ PIC_BASELINE_OP_ARGS_3B_5F
Definition: pic_baseline.h:17
@ PIC_BASELINE_OP_ARGS_9K
Definition: pic_baseline.h:19
@ PIC_BASELINE_OP_ARGS_2F
Definition: pic_baseline.h:12
@ PIC_BASELINE_OP_ARGS_NONE
Definition: pic_baseline.h:11
#define PIC_BASELINE_OP_ARGS_3B_5F_MASK_F
Definition: pic_baseline.h:29
static ut16 rz_read_le16(const void *src)
Definition: rz_endian.h:206
#define b(i)
Definition: sha256.c:42
const char * mnemonic
Definition: pic_baseline.h:34
PicBaselineOpArgs args
Definition: pic_baseline.h:35
Definition: dis.c:32