Rizin
unix-like reverse engineering framework and cli tools
lm32_isa.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2015 Felix Held
2 // SPDX-License-Identifier: BSD-2-Clause
3 
4 /* lm32 support for rizin */
5 #ifndef INCLUDE_LM32_ISA_H
6 #define INCLUDE_LM32_ISA_H
7 
8 #include <rz_types.h>
9 
10 #define RzAsmLm32RegNameLength 8
11 #define RzAsmLm32OpNameLength 12
12 
13 typedef struct rz_asm_lm32_csr_t {
17 
18 #define RzAsmLm32CsrNumber 21
19 
21  { 0x00, "IE" },
22  { 0x01, "IM" },
23  { 0x02, "IP" },
24  { 0x03, "ICC" },
25  { 0x04, "DCC" },
26  { 0x05, "CC" },
27  { 0x06, "CFG" },
28  { 0x07, "EBA" },
29  { 0x08, "DC" },
30  { 0x09, "DEBA" },
31  { 0x0a, "CFG2" },
32  { 0x0e, "JTX" },
33  { 0x0f, "JRX" },
34  { 0x10, "BP0" },
35  { 0x11, "BP1" },
36  { 0x12, "BP2" },
37  { 0x13, "BP3" },
38  { 0x18, "WP0" },
39  { 0x19, "WP1" },
40  { 0x1a, "WP2" },
41  { 0x1b, "WP3" },
42 };
43 
44 typedef struct rz_asm_lm32_reg_t {
48 
49 #define RzAsmLm32RegNumber 39
50 
52  { 0x00, "r0" },
53  { 0x00, "zero" },
54  { 0x01, "r1" },
55  { 0x02, "r2" },
56  { 0x03, "r3" },
57  { 0x04, "r4" },
58  { 0x05, "r5" },
59  { 0x06, "r6" },
60  { 0x07, "r7" },
61  { 0x08, "r8" },
62  { 0x09, "r9" },
63  { 0x0a, "r10" },
64  { 0x0b, "r11" },
65  { 0x0c, "r12" },
66  { 0x0d, "r13" },
67  { 0x0e, "r14" },
68  { 0x0f, "r15" },
69  { 0x10, "r16" },
70  { 0x11, "r17" },
71  { 0x12, "r18" },
72  { 0x13, "r19" },
73  { 0x14, "r20" },
74  { 0x15, "r21" },
75  { 0x16, "r22" },
76  { 0x17, "r23" },
77  { 0x18, "r24" },
78  { 0x19, "r25" },
79  { 0x1a, "gp" },
80  { 0x1a, "r26" },
81  { 0x1b, "fp" },
82  { 0x1b, "r27" },
83  { 0x1c, "sp" },
84  { 0x1c, "r28" },
85  { 0x1d, "ra" },
86  { 0x1d, "r29" },
87  { 0x1e, "ea" },
88  { 0x1e, "r30" },
89  { 0x1f, "ba" },
90  { 0x1f, "r31" },
91 };
92 
103  reg_csr, // wcsr
104  csr_reg, // rcsr
107 
109  lm32_op_srui = 0x00,
110  lm32_op_nori = 0x01,
111  lm32_op_muli = 0x02,
112  lm32_op_sh = 0x03,
113  lm32_op_lb = 0x04,
114  lm32_op_sri = 0x05,
115  lm32_op_xori = 0x06,
116  lm32_op_lh = 0x07,
117  lm32_op_andi = 0x08,
119  lm32_op_lw = 0x0a,
120  lm32_op_lhu = 0x0b,
121  lm32_op_sb = 0x0c,
122  lm32_op_addi = 0x0d,
123  lm32_op_ori = 0x0e,
124  lm32_op_sli = 0x0f,
125  lm32_op_lbu = 0x10,
126  lm32_op_be = 0x11,
127  lm32_op_bg = 0x12,
128  lm32_op_bge = 0x13,
129  lm32_op_bgeu = 0x14,
130  lm32_op_bgu = 0x15,
131  lm32_op_sw = 0x16,
132  lm32_op_bne = 0x17,
139  lm32_op_orhi = 0x1e,
141  lm32_op_sru = 0x20,
142  lm32_op_nor = 0x21,
143  lm32_op_mul = 0x22,
144  lm32_op_divu = 0x23,
145  lm32_op_rcsr = 0x24,
146  lm32_op_sr = 0x25,
147  lm32_op_xor = 0x26,
148  lm32_op_div = 0x27,
149  lm32_op_and = 0x28,
150  lm32_op_xnor = 0x29,
154  lm32_op_add = 0x2d,
155  lm32_op_or = 0x2e,
156  lm32_op_sl = 0x2f,
157  lm32_op_b = 0x30,
158  lm32_op_modu = 0x31,
159  lm32_op_sub = 0x32,
161  lm32_op_wcsr = 0x34,
162  lm32_op_mod = 0x35,
163  lm32_op_call = 0x36,
165  lm32_op_bi = 0x38,
166  lm32_op_cmpe = 0x39,
167  lm32_op_cmpg = 0x3a,
173 };
174 
175 typedef struct rz_asm_lm32_opcode {
179 
180 #define RzAsmLm32OpcodeNumber 0x40
181 
183  { reg_imm5, "srui" }, // 0x00
184  { reg_imm16_zeroextend, "nori" }, // 0x01
185  { reg_imm16_signextend, "muli" }, // 0x02
186  { reg_imm16_signextend, "sh" }, // 0x03
187  { reg_imm16_signextend, "lb" }, // 0x04
188  { reg_imm5, "sri" }, // 0x05
189  { reg_imm16_zeroextend, "xori" }, // 0x06
190  { reg_imm16_signextend, "lh" }, // 0x07
191  { reg_imm16_zeroextend, "andi" }, // 0x08
192  { reg_imm16_zeroextend, "xnori" }, // 0x09
193  { reg_imm16_signextend, "lw" }, // 0x0a
194  { reg_imm16_signextend, "lhu" }, // 0x0b
195  { reg_imm16_signextend, "sb" }, // 0x0c
196  { reg_imm16_signextend, "addi" }, // 0x0d
197  { reg_imm16_zeroextend, "ori" }, // 0x0e
198  { reg_imm5, "sli" }, // 0x0f
199  { reg_imm16_signextend, "lbu" }, // 0x10
200  { reg_imm16_shift2_signextend, "be" }, // 0x11
201  { reg_imm16_shift2_signextend, "bg" }, // 0x12
202  { reg_imm16_shift2_signextend, "bge" }, // 0x13
203  { reg_imm16_shift2_signextend, "bgeu" }, // 0x14
204  { reg_imm16_shift2_signextend, "bgu" }, // 0x15
205  { reg_imm16_signextend, "sw" }, // 0x16
206  { reg_imm16_shift2_signextend, "bne" }, // 0x17
207  { reg_imm16_zeroextend, "andhi" }, // 0x18
208  { reg_imm16_signextend, "cmpei" }, // 0x19
209  { reg_imm16_signextend, "cmpgi" }, // 0x1a
210  { reg_imm16_signextend, "cmpgei" }, // 0x1b
211  { reg_imm16_zeroextend, "cmpgeui" }, // 0x1c
212  { reg_imm16_zeroextend, "cmpgui" }, // 0x1d
213  { reg_imm16_zeroextend, "orhi" }, // 0x1e
214  { reg_imm16_signextend, "cmpnei" }, // 0x1f
215  { three_regs, "sru" }, // 0x20
216  { three_regs, "nor" }, // 0x21
217  { three_regs, "mul" }, // 0x22
218  { three_regs, "divu" }, // 0x23
219  { csr_reg, "rcsr" }, // 0x24
220  { three_regs, "sr" }, // 0x25
221  { three_regs, "xor" }, // 0x26
222  { three_regs, "div" }, // 0x27
223  { three_regs, "and" }, // 0x28
224  { three_regs, "xnor" }, // 0x29
225  { reserved, "reserved" }, // 0x2a
226  { raise_instr, "raise" }, // 0x2b (break, scall)
227  { two_regs, "sextb" }, // 0x2c
228  { three_regs, "add" }, // 0x2d
229  { three_regs, "or" }, // 0x2e
230  { three_regs, "sl" }, // 0x2f
231  { one_reg, "b" }, // 0x30
232  { three_regs, "modu" }, // 0x31
233  { three_regs, "sub" }, // 0x32
234  { reserved, "reserved" }, // 0x33
235  { reg_csr, "wcsr" }, // 0x34
236  { three_regs, "mod" }, // 0x35
237  { one_reg, "call" }, // 0x36
238  { two_regs, "sexth" }, // 0x37
239  { imm26, "bi" }, // 0x38
240  { three_regs, "cmpe" }, // 0x39
241  { three_regs, "cmpg" }, // 0x3a
242  { three_regs, "cmpge" }, // 0x3b
243  { three_regs, "cmpgeu" }, // 0x3c
244  { three_regs, "cmpgu" }, // 0x3d
245  { imm26, "calli" }, // 0x3e
246  { three_regs, "cmpne" }, // 0x3f
247 };
248 
249 typedef struct rz_asm_lm32_instruction {
250  // address of this instruction
252  // instruction dword
254 
255  // op code
257  // op type and name
259  // dest is the destination register (the one right after most commands)
262  // src0 is the leftmost source register
265  // src1 is the second (rightmost) source register
268  // control and status register number
271  // immediates in expanded form
273  // set if the command has a second string representation
276 
277 /*
278  * register number field name convention
279  * bits 25..21 -> reg u (either a register or a csr number)
280  * bits 20..16 -> reg v
281  * bits 15..11 -> reg w
282  */
283 static const int RzAsmLm32OpShift = 26;
284 static const ut32 RzAsmLm32OpMask = 0xfc000000;
285 static const int RzAsmLm32RegUShift = 21;
286 static const ut32 RzAsmLm32RegUMask = 0x03e00000;
287 static const int RzAsmLm32RegVShift = 16;
288 static const ut32 RzAsmLm32RegVMask = 0x001f0000;
289 static const int RzAsmLm32RegWShift = 11;
290 static const ut32 RzAsmLm32RegWMask = 0x0000f800;
291 static const ut32 RzAsmLm32Imm5Mask = 0x0000001f;
292 static const ut32 RzAsmLm32Imm16Mask = 0x0000ffff;
293 static const ut32 RzAsmLm32Imm16SignBitPos = 15;
294 static const ut32 RzAsmLm32Imm26Mask = 0x03ffffff;
295 static const ut32 RzAsmLm32Imm26SignBitPos = 25;
296 
297 static const ut32 RzAsmLm32InstrImm5InvalidBitsMask = 0x0000ffe0;
298 static const ut32 RzAsmLm32InstrOneRegInvalidBitsMask = 0x001fffff;
299 static const ut32 RzAsmLm32InstrTwoRegsInvalidBitsMask = 0x001f07ff;
300 static const ut32 RzAsmLm32InstrWcsrInvalidBitsMask = 0x0000ffff;
301 
302 // common helper functions
303 
304 static ut8 extract_opcode(ut32 instr_dword) {
305  return (instr_dword & RzAsmLm32OpMask) >> RzAsmLm32OpShift;
306 }
307 
308 static ut8 extract_reg_u(ut32 instr_dword) {
309  return (instr_dword & RzAsmLm32RegUMask) >> RzAsmLm32RegUShift;
310 }
311 
312 static ut8 extract_reg_v(ut32 instr_dword) {
313  return (instr_dword & RzAsmLm32RegVMask) >> RzAsmLm32RegVShift;
314 }
315 
316 static ut8 extract_reg_w(ut32 instr_dword) {
317  return (instr_dword & RzAsmLm32RegWMask) >> RzAsmLm32RegWShift;
318 }
319 
320 static ut8 extract_imm5(ut32 instr_dword) {
321  return instr_dword & RzAsmLm32Imm5Mask;
322 }
323 
324 static ut16 extract_imm16(ut32 instr_dword) {
325  return instr_dword & RzAsmLm32Imm16Mask;
326 }
327 
328 static ut32 extract_imm26(ut32 instr_dword) {
329  return instr_dword & RzAsmLm32Imm26Mask;
330 }
331 
332 #endif // INCLUDE_LM32_ISA_H
uint16_t ut16
uint32_t ut32
uint8_t ut8
Definition: lh5801.h:11
static const ut32 RzAsmLm32Imm26SignBitPos
Definition: lm32_isa.h:295
struct rz_asm_lm32_csr_t RzAsmLm32Csr
static const ut32 RzAsmLm32InstrImm5InvalidBitsMask
Definition: lm32_isa.h:297
struct rz_asm_lm32_instruction RzAsmLm32Instruction
static ut32 extract_imm26(ut32 instr_dword)
Definition: lm32_isa.h:328
static const ut32 RzAsmLm32Imm16Mask
Definition: lm32_isa.h:292
static ut8 extract_opcode(ut32 instr_dword)
Definition: lm32_isa.h:304
static const int RzAsmLm32OpShift
Definition: lm32_isa.h:283
#define RzAsmLm32OpcodeNumber
Definition: lm32_isa.h:180
static const ut32 RzAsmLm32InstrWcsrInvalidBitsMask
Definition: lm32_isa.h:300
static const ut32 RzAsmLm32InstrTwoRegsInvalidBitsMask
Definition: lm32_isa.h:299
static const RzAsmLm32Csr RzAsmLm32Csrs[RzAsmLm32CsrNumber]
Definition: lm32_isa.h:20
rz_asm_lm32_instr_type_t
Definition: lm32_isa.h:93
@ reg_imm16_signextend
Definition: lm32_isa.h:95
@ reg_imm16_zeroextend
Definition: lm32_isa.h:97
@ two_regs
Definition: lm32_isa.h:101
@ raise_instr
Definition: lm32_isa.h:99
@ csr_reg
Definition: lm32_isa.h:104
@ imm26
Definition: lm32_isa.h:105
@ one_reg
Definition: lm32_isa.h:100
@ reg_imm16_shift2_signextend
Definition: lm32_isa.h:96
@ three_regs
Definition: lm32_isa.h:102
@ reg_imm5
Definition: lm32_isa.h:98
@ reserved
Definition: lm32_isa.h:94
@ reg_csr
Definition: lm32_isa.h:103
static const ut32 RzAsmLm32OpMask
Definition: lm32_isa.h:284
static ut8 extract_reg_w(ut32 instr_dword)
Definition: lm32_isa.h:316
static ut8 extract_imm5(ut32 instr_dword)
Definition: lm32_isa.h:320
static const ut32 RzAsmLm32RegVMask
Definition: lm32_isa.h:288
static const int RzAsmLm32RegUShift
Definition: lm32_isa.h:285
static ut8 extract_reg_v(ut32 instr_dword)
Definition: lm32_isa.h:312
struct rz_asm_lm32_reg_t RzAsmLm32Reg
static const RzAsmLm32Opcode RzAsmLm32OpcodeList[RzAsmLm32OpcodeNumber]
Definition: lm32_isa.h:182
#define RzAsmLm32RegNumber
Definition: lm32_isa.h:49
static const ut32 RzAsmLm32Imm26Mask
Definition: lm32_isa.h:294
static const ut32 RzAsmLm32RegUMask
Definition: lm32_isa.h:286
static const ut32 RzAsmLm32InstrOneRegInvalidBitsMask
Definition: lm32_isa.h:298
#define RzAsmLm32OpNameLength
Definition: lm32_isa.h:11
#define RzAsmLm32RegNameLength
Definition: lm32_isa.h:10
static const int RzAsmLm32RegVShift
Definition: lm32_isa.h:287
static const int RzAsmLm32RegWShift
Definition: lm32_isa.h:289
static const ut32 RzAsmLm32Imm5Mask
Definition: lm32_isa.h:291
enum rz_asm_lm32_instr_type_t RzAsmLm32InstructionType
rz_asm_lm32_opcode_list_t
Definition: lm32_isa.h:108
@ lm32_op_bg
Definition: lm32_isa.h:127
@ lm32_op_lbu
Definition: lm32_isa.h:125
@ lm32_op_add
Definition: lm32_isa.h:154
@ lm32_op_wcsr
Definition: lm32_isa.h:161
@ lm32_op_cmpgeui
Definition: lm32_isa.h:137
@ lm32_op_lhu
Definition: lm32_isa.h:120
@ lm32_op_xori
Definition: lm32_isa.h:115
@ lm32_op_cmpg
Definition: lm32_isa.h:167
@ lm32_op_sextb
Definition: lm32_isa.h:153
@ lm32_op_mod
Definition: lm32_isa.h:162
@ lm32_op_call
Definition: lm32_isa.h:163
@ lm32_op_xnori
Definition: lm32_isa.h:118
@ lm32_op_cmpe
Definition: lm32_isa.h:166
@ lm32_op_calli
Definition: lm32_isa.h:171
@ lm32_op_reserved1
Definition: lm32_isa.h:151
@ lm32_op_mul
Definition: lm32_isa.h:143
@ lm32_op_andhi
Definition: lm32_isa.h:133
@ lm32_op_bgeu
Definition: lm32_isa.h:129
@ lm32_op_sli
Definition: lm32_isa.h:124
@ lm32_op_nor
Definition: lm32_isa.h:142
@ lm32_op_cmpei
Definition: lm32_isa.h:134
@ lm32_op_cmpgui
Definition: lm32_isa.h:138
@ lm32_op_sb
Definition: lm32_isa.h:121
@ lm32_op_sh
Definition: lm32_isa.h:112
@ lm32_op_or
Definition: lm32_isa.h:155
@ lm32_op_bge
Definition: lm32_isa.h:128
@ lm32_op_div
Definition: lm32_isa.h:148
@ lm32_op_and
Definition: lm32_isa.h:149
@ lm32_op_lb
Definition: lm32_isa.h:113
@ lm32_op_sub
Definition: lm32_isa.h:159
@ lm32_op_cmpnei
Definition: lm32_isa.h:140
@ lm32_op_be
Definition: lm32_isa.h:126
@ lm32_op_rcsr
Definition: lm32_isa.h:145
@ lm32_op_cmpgeu
Definition: lm32_isa.h:169
@ lm32_op_bgu
Definition: lm32_isa.h:130
@ lm32_op_modu
Definition: lm32_isa.h:158
@ lm32_op_addi
Definition: lm32_isa.h:122
@ lm32_op_xnor
Definition: lm32_isa.h:150
@ lm32_op_bne
Definition: lm32_isa.h:132
@ lm32_op_sw
Definition: lm32_isa.h:131
@ lm32_op_ori
Definition: lm32_isa.h:123
@ lm32_op_raise
Definition: lm32_isa.h:152
@ lm32_op_xor
Definition: lm32_isa.h:147
@ lm32_op_divu
Definition: lm32_isa.h:144
@ lm32_op_cmpge
Definition: lm32_isa.h:168
@ lm32_op_reserved2
Definition: lm32_isa.h:160
@ lm32_op_sl
Definition: lm32_isa.h:156
@ lm32_op_sr
Definition: lm32_isa.h:146
@ lm32_op_lh
Definition: lm32_isa.h:116
@ lm32_op_bi
Definition: lm32_isa.h:165
@ lm32_op_cmpgi
Definition: lm32_isa.h:135
@ lm32_op_andi
Definition: lm32_isa.h:117
@ lm32_op_cmpgu
Definition: lm32_isa.h:170
@ lm32_op_sri
Definition: lm32_isa.h:114
@ lm32_op_b
Definition: lm32_isa.h:157
@ lm32_op_sexth
Definition: lm32_isa.h:164
@ lm32_op_cmpgei
Definition: lm32_isa.h:136
@ lm32_op_nori
Definition: lm32_isa.h:110
@ lm32_op_lw
Definition: lm32_isa.h:119
@ lm32_op_srui
Definition: lm32_isa.h:109
@ lm32_op_sru
Definition: lm32_isa.h:141
@ lm32_op_orhi
Definition: lm32_isa.h:139
@ lm32_op_muli
Definition: lm32_isa.h:111
@ lm32_op_cmpne
Definition: lm32_isa.h:172
#define RzAsmLm32CsrNumber
Definition: lm32_isa.h:18
static const RzAsmLm32Reg RzAsmLm32Regs[RzAsmLm32RegNumber]
Definition: lm32_isa.h:51
static const ut32 RzAsmLm32RegWMask
Definition: lm32_isa.h:290
struct rz_asm_lm32_opcode RzAsmLm32Opcode
static ut8 extract_reg_u(ut32 instr_dword)
Definition: lm32_isa.h:308
static ut16 extract_imm16(ut32 instr_dword)
Definition: lm32_isa.h:324
static const ut32 RzAsmLm32Imm16SignBitPos
Definition: lm32_isa.h:293
#define st32
Definition: rz_types_base.h:12
Definition: z80asm.h:102
char src1_reg_str[RzAsmLm32RegNameLength]
Definition: lm32_isa.h:267
char dest_reg_str[RzAsmLm32RegNameLength]
Definition: lm32_isa.h:261
RzAsmLm32Opcode op_decode
Definition: lm32_isa.h:258
char csr_reg_str[RzAsmLm32RegNameLength]
Definition: lm32_isa.h:270
char src0_reg_str[RzAsmLm32RegNameLength]
Definition: lm32_isa.h:264
RzAsmLm32InstructionType type
Definition: lm32_isa.h:176