Rizin
unix-like reverse engineering framework and cli tools
lh5801.h File Reference
#include <stdint.h>
#include <stdlib.h>

Go to the source code of this file.

Classes

struct  lh5801_insn_class_desc
 
struct  lh5801_insn
 

Typedefs

typedef uint8_t ut8
 

Enumerations

enum  lh5801_insn_class {
  LH5801_INSNC_ADC , LH5801_INSNC_ADI , LH5801_INSNC_DCA , LH5801_INSNC_ADR ,
  LH5801_INSNC_SBC , LH5801_INSNC_SBI , LH5801_INSNC_DCS , LH5801_INSNC_AND ,
  LH5801_INSNC_ANI , LH5801_INSNC_ORA , LH5801_INSNC_ORI , LH5801_INSNC_EOR ,
  LH5801_INSNC_EAI , LH5801_INSNC_INC , LH5801_INSNC_DEC , LH5801_INSNC_CPA ,
  LH5801_INSNC_CPI , LH5801_INSNC_BIT , LH5801_INSNC_BII , LH5801_INSNC_LDA ,
  LH5801_INSNC_LDE , LH5801_INSNC_LIN , LH5801_INSNC_LDI , LH5801_INSNC_LDX ,
  LH5801_INSNC_STA , LH5801_INSNC_SDE , LH5801_INSNC_SIN , LH5801_INSNC_STX ,
  LH5801_INSNC_PSH , LH5801_INSNC_POP , LH5801_INSNC_ATT , LH5801_INSNC_TTA ,
  LH5801_INSNC_TIN , LH5801_INSNC_CIN , LH5801_INSNC_ROL , LH5801_INSNC_ROR ,
  LH5801_INSNC_SHL , LH5801_INSNC_SHR , LH5801_INSNC_DRL , LH5801_INSNC_DRR ,
  LH5801_INSNC_AEX , LH5801_INSNC_SEC , LH5801_INSNC_REC , LH5801_INSNC_CDV ,
  LH5801_INSNC_ATP , LH5801_INSNC_SPU , LH5801_INSNC_RPU , LH5801_INSNC_SPV ,
  LH5801_INSNC_RPV , LH5801_INSNC_SDP , LH5801_INSNC_RDP , LH5801_INSNC_ITA ,
  LH5801_INSNC_SIE , LH5801_INSNC_RIE , LH5801_INSNC_AM0 , LH5801_INSNC_AM1 ,
  LH5801_INSNC_NOP , LH5801_INSNC_HLT , LH5801_INSNC_OFF , LH5801_INSNC_JMP ,
  LH5801_INSNC_BCH , LH5801_INSNC_BCC , LH5801_INSNC_LOP , LH5801_INSNC_SJP ,
  LH5801_INSNC_VEJ , LH5801_INSNC_VMJ , LH5801_INSNC_VCC , LH5801_INSNC_RTN ,
  LH5801_INSNC_RTI , LH5801_INSNC_NUMBER
}
 

Functions

int lh5801_decode (struct lh5801_insn *, const uint8_t *, int)
 
void lh5801_print_insn (char *out, int size, const struct lh5801_insn *)
 

Variables

const struct lh5801_insn_class_desc lh5801_insn_class_descs [LH5801_INSNC_NUMBER]
 

Typedef Documentation

◆ ut8

typedef uint8_t ut8

Definition at line 11 of file lh5801.h.

Enumeration Type Documentation

◆ lh5801_insn_class

Enumerator
LH5801_INSNC_ADC 
LH5801_INSNC_ADI 
LH5801_INSNC_DCA 
LH5801_INSNC_ADR 
LH5801_INSNC_SBC 
LH5801_INSNC_SBI 
LH5801_INSNC_DCS 
LH5801_INSNC_AND 
LH5801_INSNC_ANI 
LH5801_INSNC_ORA 
LH5801_INSNC_ORI 
LH5801_INSNC_EOR 
LH5801_INSNC_EAI 
LH5801_INSNC_INC 
LH5801_INSNC_DEC 
LH5801_INSNC_CPA 
LH5801_INSNC_CPI 
LH5801_INSNC_BIT 
LH5801_INSNC_BII 
LH5801_INSNC_LDA 
LH5801_INSNC_LDE 
LH5801_INSNC_LIN 
LH5801_INSNC_LDI 
LH5801_INSNC_LDX 
LH5801_INSNC_STA 
LH5801_INSNC_SDE 
LH5801_INSNC_SIN 
LH5801_INSNC_STX 
LH5801_INSNC_PSH 
LH5801_INSNC_POP 
LH5801_INSNC_ATT 
LH5801_INSNC_TTA 
LH5801_INSNC_TIN 
LH5801_INSNC_CIN 
LH5801_INSNC_ROL 
LH5801_INSNC_ROR 
LH5801_INSNC_SHL 
LH5801_INSNC_SHR 
LH5801_INSNC_DRL 
LH5801_INSNC_DRR 
LH5801_INSNC_AEX 
LH5801_INSNC_SEC 
LH5801_INSNC_REC 
LH5801_INSNC_CDV 
LH5801_INSNC_ATP 
LH5801_INSNC_SPU 
LH5801_INSNC_RPU 
LH5801_INSNC_SPV 
LH5801_INSNC_RPV 
LH5801_INSNC_SDP 
LH5801_INSNC_RDP 
LH5801_INSNC_ITA 
LH5801_INSNC_SIE 
LH5801_INSNC_RIE 
LH5801_INSNC_AM0 
LH5801_INSNC_AM1 
LH5801_INSNC_NOP 
LH5801_INSNC_HLT 
LH5801_INSNC_OFF 
LH5801_INSNC_JMP 
LH5801_INSNC_BCH 
LH5801_INSNC_BCC 
LH5801_INSNC_LOP 
LH5801_INSNC_SJP 
LH5801_INSNC_VEJ 
LH5801_INSNC_VMJ 
LH5801_INSNC_VCC 
LH5801_INSNC_RTN 
LH5801_INSNC_RTI 
LH5801_INSNC_NUMBER 

Definition at line 14 of file lh5801.h.

14  {
84 
86 };
@ LH5801_INSNC_DCS
Definition: lh5801.h:21
@ LH5801_INSNC_PSH
Definition: lh5801.h:43
@ LH5801_INSNC_TIN
Definition: lh5801.h:47
@ LH5801_INSNC_ADC
Definition: lh5801.h:15
@ LH5801_INSNC_SPU
Definition: lh5801.h:60
@ LH5801_INSNC_POP
Definition: lh5801.h:44
@ LH5801_INSNC_RPV
Definition: lh5801.h:63
@ LH5801_INSNC_CPA
Definition: lh5801.h:30
@ LH5801_INSNC_SIE
Definition: lh5801.h:67
@ LH5801_INSNC_ATP
Definition: lh5801.h:59
@ LH5801_INSNC_BIT
Definition: lh5801.h:32
@ LH5801_INSNC_TTA
Definition: lh5801.h:46
@ LH5801_INSNC_AM1
Definition: lh5801.h:70
@ LH5801_INSNC_AM0
Definition: lh5801.h:69
@ LH5801_INSNC_DEC
Definition: lh5801.h:29
@ LH5801_INSNC_NUMBER
Definition: lh5801.h:85
@ LH5801_INSNC_JMP
Definition: lh5801.h:74
@ LH5801_INSNC_RTN
Definition: lh5801.h:82
@ LH5801_INSNC_SPV
Definition: lh5801.h:62
@ LH5801_INSNC_REC
Definition: lh5801.h:57
@ LH5801_INSNC_LDI
Definition: lh5801.h:37
@ LH5801_INSNC_SDE
Definition: lh5801.h:40
@ LH5801_INSNC_DCA
Definition: lh5801.h:17
@ LH5801_INSNC_BCH
Definition: lh5801.h:75
@ LH5801_INSNC_RIE
Definition: lh5801.h:68
@ LH5801_INSNC_LOP
Definition: lh5801.h:77
@ LH5801_INSNC_ATT
Definition: lh5801.h:45
@ LH5801_INSNC_LIN
Definition: lh5801.h:36
@ LH5801_INSNC_VMJ
Definition: lh5801.h:80
@ LH5801_INSNC_VCC
Definition: lh5801.h:81
@ LH5801_INSNC_ANI
Definition: lh5801.h:23
@ LH5801_INSNC_SBI
Definition: lh5801.h:20
@ LH5801_INSNC_DRR
Definition: lh5801.h:54
@ LH5801_INSNC_CIN
Definition: lh5801.h:48
@ LH5801_INSNC_LDE
Definition: lh5801.h:35
@ LH5801_INSNC_SJP
Definition: lh5801.h:78
@ LH5801_INSNC_OFF
Definition: lh5801.h:73
@ LH5801_INSNC_ITA
Definition: lh5801.h:66
@ LH5801_INSNC_RDP
Definition: lh5801.h:65
@ LH5801_INSNC_STA
Definition: lh5801.h:39
@ LH5801_INSNC_LDA
Definition: lh5801.h:34
@ LH5801_INSNC_ORA
Definition: lh5801.h:24
@ LH5801_INSNC_SEC
Definition: lh5801.h:56
@ LH5801_INSNC_AND
Definition: lh5801.h:22
@ LH5801_INSNC_SDP
Definition: lh5801.h:64
@ LH5801_INSNC_RTI
Definition: lh5801.h:83
@ LH5801_INSNC_HLT
Definition: lh5801.h:72
@ LH5801_INSNC_VEJ
Definition: lh5801.h:79
@ LH5801_INSNC_ROR
Definition: lh5801.h:50
@ LH5801_INSNC_ADI
Definition: lh5801.h:16
@ LH5801_INSNC_ADR
Definition: lh5801.h:18
@ LH5801_INSNC_ROL
Definition: lh5801.h:49
@ LH5801_INSNC_CPI
Definition: lh5801.h:31
@ LH5801_INSNC_ORI
Definition: lh5801.h:25
@ LH5801_INSNC_SBC
Definition: lh5801.h:19
@ LH5801_INSNC_LDX
Definition: lh5801.h:38
@ LH5801_INSNC_INC
Definition: lh5801.h:28
@ LH5801_INSNC_SIN
Definition: lh5801.h:41
@ LH5801_INSNC_EAI
Definition: lh5801.h:27
@ LH5801_INSNC_SHL
Definition: lh5801.h:51
@ LH5801_INSNC_NOP
Definition: lh5801.h:71
@ LH5801_INSNC_AEX
Definition: lh5801.h:55
@ LH5801_INSNC_SHR
Definition: lh5801.h:52
@ LH5801_INSNC_BII
Definition: lh5801.h:33
@ LH5801_INSNC_DRL
Definition: lh5801.h:53
@ LH5801_INSNC_BCC
Definition: lh5801.h:76
@ LH5801_INSNC_EOR
Definition: lh5801.h:26
@ LH5801_INSNC_CDV
Definition: lh5801.h:58
@ LH5801_INSNC_STX
Definition: lh5801.h:42
@ LH5801_INSNC_RPU
Definition: lh5801.h:61

Function Documentation

◆ lh5801_decode()

int lh5801_decode ( struct lh5801_insn insn,
const uint8_t buf,
int  len 
)

Definition at line 674 of file lh5801.c.

674  {
675  int fd = (buf[0] == 0xfd);
676  int type = -1;
677  unsigned i;
678  struct lh5801_insn_desc desc;
679 
680  if (fd) {
681  buf++;
682  len--;
683  }
684 
685  if (len == 0)
686  return 0;
687 
688  /* Find the correct opcode */
689  for (i = 0; i < ARRAY_LENGTH(lh5801_insn_descs); i++) {
690  ut8 byte = *buf;
691  unsigned fmt;
692  unsigned ifmt_reg;
693 
695  fmt = desc.format;
696  ifmt_reg = fmt & LH5801_IFMT_REG_MASK;
697 
698  if (!lh5801_ifmt_fd_matches(fmt, fd))
699  continue;
700 
701  /* Ignore instructions referencing the register number 3. */
702  if (ifmt_reg == LH5801_IFMT_RREG && (byte >> 4) % 4 == 3)
703  continue;
704 
705  /* Reduce the opcode byte to the relevant bits */
706  if (ifmt_reg == LH5801_IFMT_RREG)
707  byte &= 0xcf; /* xxRRxxxx */
708  if (fmt & LH5801_IFMT_COND)
709  byte &= 0xf1; /* xxxxCCCx */
710  if (fmt & LH5801_IFMT_BCH)
711  byte &= 0xef; /* xxxSxxxx */
712 
713  if (byte == desc.opcode) {
714  type = i;
715  break;
716  }
717 
718  /* The short vector subroutine jump instructions require
719  * special treatment. */
720  if (fmt & LH5801_IFMT_VEJ) {
721  if (!(byte & 1) && byte >= 0xc0 && byte <= 0xf6) {
722  type = i;
723  break;
724  }
725  }
726  }
727  if (type == -1)
728  return -1;
729 
730  /* fill the insn structure. */
731  insn->iclass = desc.iclass;
732  insn->type = type;
733  insn->fd = fd;
734  insn->opcode = buf[0];
735  switch (LH5801_IFMT_IMMS(desc.format)) {
736  case 3: insn->imm[2] = buf[3]; // fallthrough
737  case 2: insn->imm[1] = buf[2]; // fallthrough
738  case 1: insn->imm[0] = buf[1]; // fallthrough
739  }
740 
741  /* return the instruction length */
742  return fd + 1 + LH5801_IFMT_IMMS(desc.format);
743 }
size_t len
Definition: 6502dis.c:15
lzma_index ** i
Definition: index.h:629
const char * desc
Definition: bin_vsf.c:19
voidpf void * buf
Definition: ioapi.h:138
const struct lh5801_insn_desc lh5801_insn_descs[]
Definition: lh5801.c:156
static bool lh5801_ifmt_fd_matches(enum lh5801_insn_format fmt, int fd)
Definition: lh5801.c:137
@ LH5801_IFMT_VEJ
Definition: lh5801.c:124
@ LH5801_IFMT_REG_MASK
Definition: lh5801.c:114
@ LH5801_IFMT_RREG
Definition: lh5801.c:110
@ LH5801_IFMT_BCH
Definition: lh5801.c:121
@ LH5801_IFMT_COND
Definition: lh5801.c:118
#define ARRAY_LENGTH(a)
Definition: lh5801.c:17
#define LH5801_IFMT_IMMS(f)
Definition: lh5801.c:134
uint8_t ut8
Definition: lh5801.h:11
int type
Definition: mipsasm.c:17
ut8 fd
Definition: lh5801.h:103
ut8 type
Definition: lh5801.h:102
ut8 opcode
Definition: lh5801.h:104
ut8 iclass
Definition: lh5801.h:101
ut8 imm[3]
Definition: lh5801.h:105
static const z80_opcode fd[]
Definition: z80_tab.h:997

References ARRAY_LENGTH, desc, lh5801_insn::fd, fd, i, lh5801_insn::iclass, lh5801_insn::imm, len, LH5801_IFMT_BCH, LH5801_IFMT_COND, lh5801_ifmt_fd_matches(), LH5801_IFMT_IMMS, LH5801_IFMT_REG_MASK, LH5801_IFMT_RREG, LH5801_IFMT_VEJ, lh5801_insn_descs, lh5801_insn::opcode, lh5801_insn::type, and type.

Referenced by disassemble().

◆ lh5801_print_insn()

void lh5801_print_insn ( char *  out,
int  size,
const struct lh5801_insn insn 
)

Definition at line 792 of file lh5801.c.

792  {
793  const struct lh5801_insn_class_desc *iclass =
795  const struct lh5801_insn_desc desc = lh5801_insn_descs[insn->type];
796  const char *mnem = iclass->mnem;
797  char mnembuf[4];
798  char regbuf[8];
799 
800  /* Conditional instructions have special mnemonics. */
801  if (desc.format & LH5801_IFMT_COND) {
802  mnembuf[0] = mnem[0]; /* the first character is the same. */
803  mnembuf[1] = "chzv"[(insn->opcode >> 2) % 4]; /* which flag */
804  mnembuf[2] = (insn->opcode & 2) ? 's' : 'r'; /* set or reset */
805  mnembuf[3] = '\0';
806  mnem = mnembuf;
807  }
808 
809  /*
810  * operand print modes:
811  * IMM0: rl/rh, REG|LOW, REG|HIGH
812  * r, REG
813  * (r), REG|MEM -> would MEM imply FD_MOD?
814  * s,p S, P
815  * vej i VEJ
816  * IMM1: IMM0,i IMM1
817  * a,i ACCU
818  * IMM2: ij (jump)
819  * (ij)
820  * s,ij (ldi)
821  * IMM3: (ij),k
822  */
823 
825  case LH5801_IFMT_VEJ:
826  snprintf(out, size, "%s %02xh", mnem, insn->opcode);
827  break;
828  case LH5801_IFMT_IMM0:
829  snprintf(out, size, "%s", mnem);
830  break;
835  snprintf(out, size, "%s %s", mnem, print_reg(regbuf, insn));
836  break;
837  case LH5801_IFMT_IMM1:
838  snprintf(out, size, "%s %02xh", mnem, insn->imm[0]);
839  break;
844  snprintf(out, size, "%s %s, %02xh", mnem,
845  print_reg(regbuf, insn), insn->imm[0]);
846  break;
848  snprintf(out, size, "%s %c%02xh", mnem,
849  (insn->opcode & 0x10) ? '-' : '+', insn->imm[0]);
850  break;
851  case LH5801_IFMT_IMM2:
852  if (desc.format & LH5801_IFMT_FD_MOD) {
853  snprintf(out, size, "%s %s(%02x%02xh)", mnem,
854  insn->fd ? "#" : "",
855  insn->imm[0], insn->imm[1]);
856  } else {
857  snprintf(out, size, "%s %02x%02xh", mnem,
858  insn->imm[0], insn->imm[1]);
859  }
860  break;
861  case LH5801_IFMT_IMM3:
862  if (desc.format & LH5801_IFMT_FD_MOD) {
863  snprintf(out, size, "%s %s(%02x%02xh), %02xh", mnem,
864  insn->fd ? "#" : "",
865  insn->imm[0], insn->imm[1], insn->imm[2]);
866  } else {
867  snprintf(out, size, "imm3 invalid format");
868  }
869  break;
870  default:
871  snprintf(out, size, "%s, BUG: unknown format 0x%x -> 0x%x",
872  mnem, desc.format,
873  desc.format & ~LH5801_IFMT_RMODE_MASK &
875  }
876 }
#define mnem(n, mn)
const lzma_allocator const uint8_t size_t uint8_t * out
Definition: block.h:528
voidpf void uLong size
Definition: ioapi.h:138
snprintf
Definition: kernel.h:364
static char * print_reg(char *buf, const struct lh5801_insn *insn)
Definition: lh5801.c:747
const struct lh5801_insn_class_desc lh5801_insn_class_descs[LH5801_INSNC_NUMBER]
Definition: lh5801.c:20
@ LH5801_IFMT_IMM3
Definition: lh5801.c:99
@ LH5801_IFMT_PREG
Definition: lh5801.c:113
@ LH5801_IFMT_RMODE_MASK
Definition: lh5801.c:131
@ LH5801_IFMT_SREG
Definition: lh5801.c:112
@ LH5801_IFMT_IMM2
Definition: lh5801.c:98
@ LH5801_IFMT_FD_MOD
Definition: lh5801.c:106
@ LH5801_IFMT_FD_MASK
Definition: lh5801.c:107
@ LH5801_IFMT_IMM1
Definition: lh5801.c:97
@ LH5801_IFMT_AREG
Definition: lh5801.c:111
@ LH5801_IFMT_IMM0
Definition: lh5801.c:96

References desc, lh5801_insn::fd, lh5801_insn_desc::iclass, lh5801_insn::iclass, lh5801_insn::imm, LH5801_IFMT_AREG, LH5801_IFMT_BCH, LH5801_IFMT_COND, LH5801_IFMT_FD_MASK, LH5801_IFMT_FD_MOD, LH5801_IFMT_IMM0, LH5801_IFMT_IMM1, LH5801_IFMT_IMM2, LH5801_IFMT_IMM3, LH5801_IFMT_PREG, LH5801_IFMT_RMODE_MASK, LH5801_IFMT_RREG, LH5801_IFMT_SREG, LH5801_IFMT_VEJ, lh5801_insn_class_descs, lh5801_insn_descs, mnem, lh5801_insn::opcode, out, print_reg(), snprintf, and lh5801_insn::type.

Referenced by disassemble().

Variable Documentation

◆ lh5801_insn_class_descs

const struct lh5801_insn_class_desc lh5801_insn_class_descs[LH5801_INSNC_NUMBER]

Definition at line 11 of file lh5801.h.