Rizin
unix-like reverse engineering framework and cli tools
dcpu16.h File Reference

Go to the source code of this file.

Macros

#define ut8   unsigned char
 
#define ut16   unsigned short
 

Functions

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

Macro Definition Documentation

◆ ut16

#define ut16   unsigned short

Definition at line 9 of file dcpu16.h.

◆ ut8

#define ut8   unsigned char

Definition at line 8 of file dcpu16.h.

Function Documentation

◆ dcpu16_assemble()

int dcpu16_assemble ( ut8 out,
const char *  unoline 
)

Definition at line 216 of file asm.c.

216  {
217  ut16 wordA = 0, wordB = 0;
218  int basic_opcode = 0;
219  int non_basic_opcode = 0;
220  char line[256] = { 0 }, *param;
221  int off = 0;
222  // uberflow!
223  clean_line(line, unoline);
224 
225  if (!(*line))
226  return 0;
227  if (strlen(line) < 4)
228  return 0;
229  param = line + 3; /* Cut off first 3 characters */
230 
231  /* Basic instructions */
232  // cmon! use an array
233  if (!strncmp("SET", line, 3))
234  basic_opcode = 0x1;
235  else if (!strncmp("ADD", line, 3))
236  basic_opcode = 0x2;
237  else if (!strncmp("SUB", line, 3))
238  basic_opcode = 0x3;
239  else if (!strncmp("MUL", line, 3))
240  basic_opcode = 0x4;
241  else if (!strncmp("DIV", line, 3))
242  basic_opcode = 0x5;
243  else if (!strncmp("MOD", line, 3))
244  basic_opcode = 0x6;
245  else if (!strncmp("SHL", line, 3))
246  basic_opcode = 0x7;
247  else if (!strncmp("SHR", line, 3))
248  basic_opcode = 0x8;
249  else if (!strncmp("AND", line, 3))
250  basic_opcode = 0x9;
251  else if (!strncmp("BOR", line, 3))
252  basic_opcode = 0xA;
253  else if (!strncmp("XOR", line, 3))
254  basic_opcode = 0xB;
255  else if (!strncmp("IFE", line, 3))
256  basic_opcode = 0xC;
257  else if (!strncmp("IFN", line, 3))
258  basic_opcode = 0xD;
259  else if (!strncmp("IFG", line, 3))
260  basic_opcode = 0xE;
261  else if (!strncmp("IFB", line, 3))
262  basic_opcode = 0xF;
263 
264  /* Non basic instructions */
265  if (basic_opcode == 0) {
266  if (!strncmp("JSR", line, 3)) {
267  non_basic_opcode = 0x1;
268  } else {
269  fprintf(stderr, "Unknown instruction\n");
270  return -1;
271  }
272  }
273 
274  /* Decode basic instructions */
275  if (basic_opcode != 0) {
276  ut8 paramA = 0, paramB = 0;
277 
278  /* Find comma */
279  int cn = 0;
280  while (cn < 256 && param[cn] != ',' && param[cn] != '\n' && param[cn] != 0)
281  cn++;
282 
283  if (param[cn] == ',') {
284  ut16 first_word;
285  int extraA = 0;
286  int extraB = 0;
287  char *pa, *pb;
288  /* Split parameter string to A and B */
289  param[cn] = 0;
290  pa = param;
291  pb = param + cn + 1;
292 
293  /* Increment address for the start word */
294  // current_address++;
295 
296  /* Parameter A */
297  paramA = decode_parameter(pa, &extraA, &wordA);
298  // if (extraA == 1) current_address++;
299 
300  /* Parameter B */
301  paramB = decode_parameter(pb, &extraB, &wordB);
302  // if (extraB == 1) current_address++;
303 
304  /* Put everything together */
305  first_word = ((paramB & 0x3F) << 10) | ((paramA & 0x3F) << 4) | (basic_opcode & 0xF);
306 
307  /* write opcode */
308 #if NOTEND
309  memcpy(out, &first_word, 2);
310  if (extraA == 1) {
311  memcpy(out + 2, &wordA, 2);
312  off = 4;
313  } else
314  off = 2;
315  if (extraB == 1) {
316  memcpy(out + off, &wordB, 2);
317  off += 2;
318  }
319 #else
320  out[0] = (first_word >> 8) & 0xff;
321  out[1] = first_word & 0xff;
322  if (extraA == 1) {
323  out[2] = (wordA >> 8) & 0xff;
324  out[3] = wordA & 0xff;
325  off = 4;
326  } else
327  off = 2;
328  if (extraB == 1) {
329  out[off] = (wordB >> 8) & 0xff;
330  out[off + 1] = wordB & 0xff;
331  off += 2;
332  }
333 #endif
334  } else {
335  fprintf(stderr, "Missing comma\n");
336  return -1;
337  }
338  }
339 
340  /* Non basic instructions */
341  if (non_basic_opcode == 0x1) { /* JSR */
342  int extraX = 0;
343  ut16 first_word, wordX = 0;
344  ut8 p = decode_parameter(param, &extraX, &wordX);
345 
346  first_word = ((p & 0x3F) << 10) | ((non_basic_opcode & 0x3F) << 4) | (basic_opcode & 0xF);
347 #if NOTEND
348  memcpy(out, &first_word, 2);
349  if (extraX == 1) {
350  memcpy(out + 2, &wordX, 2);
351  off = 4;
352  } else
353  off = 2;
354 #else
355  out[0] = (first_word >> 8) & 0xff;
356  out[1] = first_word & 0xff;
357  if (extraX == 1) {
358  out[2] = (wordX >> 8) & 0xff;
359  out[3] = wordX & 0xff;
360  off = 4;
361  } else
362  off = 2;
363 #endif
364  }
365  return off;
366 }
static void clean_line(char *oline, const char *line)
Definition: asm.c:48
static ut8 decode_parameter(char *param, int *extra_word_needed, ut16 *extra_word_value)
Definition: asm.c:71
const lzma_allocator const uint8_t size_t uint8_t * out
Definition: block.h:528
uint16_t ut16
uint8_t ut8
Definition: lh5801.h:11
void * p
Definition: libc.cpp:67
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
line
Definition: setup.py:34
int off
Definition: pal.c:13

References clean_line(), decode_parameter(), setup::line, memcpy(), off, out, and p.

Referenced by assemble().

◆ 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 }
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().