metasm 1.0.0 → 1.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +3 -0
- data/.gitignore +3 -0
- data/.hgtags +3 -0
- data/Gemfile +3 -0
- data/INSTALL +61 -0
- data/LICENCE +458 -0
- data/README +29 -21
- data/Rakefile +10 -0
- data/TODO +10 -12
- data/doc/code_organisation.txt +3 -1
- data/doc/core/DynLdr.txt +247 -0
- data/doc/core/ExeFormat.txt +43 -0
- data/doc/core/Expression.txt +220 -0
- data/doc/core/GNUExports.txt +27 -0
- data/doc/core/Ia32.txt +236 -0
- data/doc/core/SerialStruct.txt +108 -0
- data/doc/core/VirtualString.txt +145 -0
- data/doc/core/WindowsExports.txt +61 -0
- data/doc/core/index.txt +1 -0
- data/doc/style.css +6 -3
- data/doc/usage/debugger.txt +327 -0
- data/doc/usage/index.txt +1 -0
- data/doc/use_cases.txt +2 -2
- data/metasm.gemspec +23 -0
- data/{lib/metasm.rb → metasm.rb} +15 -3
- data/{lib/metasm → metasm}/compile_c.rb +15 -9
- data/metasm/cpu/arc.rb +8 -0
- data/metasm/cpu/arc/decode.rb +404 -0
- data/metasm/cpu/arc/main.rb +191 -0
- data/metasm/cpu/arc/opcodes.rb +588 -0
- data/metasm/cpu/arm.rb +14 -0
- data/{lib/metasm → metasm/cpu}/arm/debug.rb +2 -2
- data/{lib/metasm → metasm/cpu}/arm/decode.rb +15 -18
- data/{lib/metasm → metasm/cpu}/arm/encode.rb +23 -8
- data/{lib/metasm → metasm/cpu}/arm/main.rb +3 -6
- data/metasm/cpu/arm/opcodes.rb +324 -0
- data/{lib/metasm → metasm/cpu}/arm/parse.rb +25 -13
- data/{lib/metasm → metasm/cpu}/arm/render.rb +2 -2
- data/metasm/cpu/arm64.rb +15 -0
- data/metasm/cpu/arm64/debug.rb +38 -0
- data/metasm/cpu/arm64/decode.rb +285 -0
- data/metasm/cpu/arm64/encode.rb +41 -0
- data/metasm/cpu/arm64/main.rb +105 -0
- data/metasm/cpu/arm64/opcodes.rb +232 -0
- data/metasm/cpu/arm64/parse.rb +20 -0
- data/metasm/cpu/arm64/render.rb +95 -0
- data/{lib/metasm/mips/compile_c.rb → metasm/cpu/bpf.rb} +4 -2
- data/metasm/cpu/bpf/decode.rb +110 -0
- data/metasm/cpu/bpf/main.rb +60 -0
- data/metasm/cpu/bpf/opcodes.rb +81 -0
- data/metasm/cpu/bpf/render.rb +30 -0
- data/{lib/metasm/ppc.rb → metasm/cpu/cy16.rb} +2 -4
- data/metasm/cpu/cy16/decode.rb +247 -0
- data/metasm/cpu/cy16/main.rb +63 -0
- data/metasm/cpu/cy16/opcodes.rb +78 -0
- data/metasm/cpu/cy16/render.rb +30 -0
- data/metasm/cpu/dalvik.rb +11 -0
- data/{lib/metasm → metasm/cpu}/dalvik/decode.rb +34 -34
- data/{lib/metasm → metasm/cpu}/dalvik/main.rb +71 -4
- data/{lib/metasm → metasm/cpu}/dalvik/opcodes.rb +21 -12
- data/{lib/metasm/mips.rb → metasm/cpu/ebpf.rb} +3 -4
- data/metasm/cpu/ebpf/debug.rb +61 -0
- data/metasm/cpu/ebpf/decode.rb +142 -0
- data/metasm/cpu/ebpf/main.rb +58 -0
- data/metasm/cpu/ebpf/opcodes.rb +97 -0
- data/metasm/cpu/ebpf/render.rb +36 -0
- data/metasm/cpu/ia32.rb +17 -0
- data/{lib/metasm → metasm/cpu}/ia32/compile_c.rb +23 -9
- data/{lib/metasm → metasm/cpu}/ia32/debug.rb +44 -6
- data/{lib/metasm → metasm/cpu}/ia32/decode.rb +342 -128
- data/{lib/metasm → metasm/cpu}/ia32/decompile.rb +75 -53
- data/{lib/metasm → metasm/cpu}/ia32/encode.rb +19 -13
- data/{lib/metasm → metasm/cpu}/ia32/main.rb +66 -8
- data/metasm/cpu/ia32/opcodes.rb +1424 -0
- data/{lib/metasm → metasm/cpu}/ia32/parse.rb +55 -17
- data/{lib/metasm → metasm/cpu}/ia32/render.rb +32 -5
- data/metasm/cpu/mcs51.rb +8 -0
- data/metasm/cpu/mcs51/decode.rb +99 -0
- data/metasm/cpu/mcs51/main.rb +87 -0
- data/metasm/cpu/mcs51/opcodes.rb +120 -0
- data/metasm/cpu/mips.rb +14 -0
- data/metasm/cpu/mips/debug.rb +42 -0
- data/{lib/metasm → metasm/cpu}/mips/decode.rb +59 -38
- data/{lib/metasm → metasm/cpu}/mips/encode.rb +4 -3
- data/{lib/metasm → metasm/cpu}/mips/main.rb +13 -6
- data/{lib/metasm → metasm/cpu}/mips/opcodes.rb +87 -18
- data/{lib/metasm → metasm/cpu}/mips/parse.rb +1 -1
- data/{lib/metasm → metasm/cpu}/mips/render.rb +1 -1
- data/{lib/metasm/dalvik.rb → metasm/cpu/msp430.rb} +1 -1
- data/metasm/cpu/msp430/decode.rb +243 -0
- data/metasm/cpu/msp430/main.rb +62 -0
- data/metasm/cpu/msp430/opcodes.rb +101 -0
- data/metasm/cpu/openrisc.rb +11 -0
- data/metasm/cpu/openrisc/debug.rb +106 -0
- data/metasm/cpu/openrisc/decode.rb +182 -0
- data/metasm/cpu/openrisc/decompile.rb +350 -0
- data/metasm/cpu/openrisc/main.rb +70 -0
- data/metasm/cpu/openrisc/opcodes.rb +109 -0
- data/metasm/cpu/openrisc/render.rb +37 -0
- data/{lib/metasm → metasm/cpu}/pic16c/decode.rb +6 -7
- data/{lib/metasm → metasm/cpu}/pic16c/main.rb +0 -0
- data/{lib/metasm → metasm/cpu}/pic16c/opcodes.rb +1 -1
- data/metasm/cpu/ppc.rb +11 -0
- data/{lib/metasm → metasm/cpu}/ppc/decode.rb +18 -37
- data/{lib/metasm → metasm/cpu}/ppc/decompile.rb +3 -3
- data/{lib/metasm → metasm/cpu}/ppc/encode.rb +2 -2
- data/{lib/metasm → metasm/cpu}/ppc/main.rb +23 -18
- data/{lib/metasm → metasm/cpu}/ppc/opcodes.rb +11 -6
- data/metasm/cpu/ppc/parse.rb +55 -0
- data/metasm/cpu/python.rb +8 -0
- data/metasm/cpu/python/decode.rb +116 -0
- data/metasm/cpu/python/main.rb +36 -0
- data/metasm/cpu/python/opcodes.rb +180 -0
- data/{lib/metasm → metasm/cpu}/sh4.rb +1 -1
- data/{lib/metasm → metasm/cpu}/sh4/decode.rb +50 -23
- data/{lib/metasm → metasm/cpu}/sh4/main.rb +38 -27
- data/{lib/metasm → metasm/cpu}/sh4/opcodes.rb +7 -8
- data/metasm/cpu/st20.rb +9 -0
- data/metasm/cpu/st20/decode.rb +173 -0
- data/metasm/cpu/st20/decompile.rb +283 -0
- data/metasm/cpu/st20/main.rb +37 -0
- data/metasm/cpu/st20/opcodes.rb +140 -0
- data/{lib/metasm/arm.rb → metasm/cpu/webasm.rb} +4 -5
- data/metasm/cpu/webasm/debug.rb +31 -0
- data/metasm/cpu/webasm/decode.rb +321 -0
- data/metasm/cpu/webasm/decompile.rb +386 -0
- data/metasm/cpu/webasm/encode.rb +104 -0
- data/metasm/cpu/webasm/main.rb +81 -0
- data/metasm/cpu/webasm/opcodes.rb +214 -0
- data/metasm/cpu/x86_64.rb +15 -0
- data/{lib/metasm → metasm/cpu}/x86_64/compile_c.rb +40 -25
- data/{lib/metasm → metasm/cpu}/x86_64/debug.rb +4 -4
- data/{lib/metasm → metasm/cpu}/x86_64/decode.rb +58 -15
- data/{lib/metasm → metasm/cpu}/x86_64/encode.rb +59 -28
- data/{lib/metasm → metasm/cpu}/x86_64/main.rb +18 -6
- data/metasm/cpu/x86_64/opcodes.rb +138 -0
- data/{lib/metasm → metasm/cpu}/x86_64/parse.rb +12 -4
- data/metasm/cpu/x86_64/render.rb +35 -0
- data/metasm/cpu/z80.rb +9 -0
- data/metasm/cpu/z80/decode.rb +286 -0
- data/metasm/cpu/z80/main.rb +67 -0
- data/metasm/cpu/z80/opcodes.rb +224 -0
- data/metasm/cpu/z80/render.rb +48 -0
- data/{lib/metasm/os/main.rb → metasm/debug.rb} +201 -407
- data/{lib/metasm → metasm}/decode.rb +104 -24
- data/{lib/metasm → metasm}/decompile.rb +804 -478
- data/{lib/metasm → metasm}/disassemble.rb +385 -170
- data/{lib/metasm → metasm}/disassemble_api.rb +684 -105
- data/{lib/metasm → metasm}/dynldr.rb +231 -138
- data/{lib/metasm → metasm}/encode.rb +20 -5
- data/{lib/metasm → metasm}/exe_format/a_out.rb +9 -6
- data/{lib/metasm → metasm}/exe_format/autoexe.rb +3 -0
- data/{lib/metasm → metasm}/exe_format/bflt.rb +57 -27
- data/{lib/metasm → metasm}/exe_format/coff.rb +35 -7
- data/{lib/metasm → metasm}/exe_format/coff_decode.rb +70 -23
- data/{lib/metasm → metasm}/exe_format/coff_encode.rb +24 -22
- data/{lib/metasm → metasm}/exe_format/dex.rb +26 -8
- data/{lib/metasm → metasm}/exe_format/dol.rb +1 -0
- data/{lib/metasm → metasm}/exe_format/elf.rb +108 -58
- data/{lib/metasm → metasm}/exe_format/elf_decode.rb +202 -36
- data/{lib/metasm → metasm}/exe_format/elf_encode.rb +126 -32
- data/metasm/exe_format/gb.rb +65 -0
- data/metasm/exe_format/javaclass.rb +424 -0
- data/{lib/metasm → metasm}/exe_format/macho.rb +218 -16
- data/{lib/metasm → metasm}/exe_format/main.rb +28 -3
- data/{lib/metasm → metasm}/exe_format/mz.rb +2 -0
- data/{lib/metasm → metasm}/exe_format/nds.rb +7 -4
- data/{lib/metasm → metasm}/exe_format/pe.rb +96 -11
- data/metasm/exe_format/pyc.rb +167 -0
- data/{lib/metasm → metasm}/exe_format/serialstruct.rb +67 -14
- data/{lib/metasm → metasm}/exe_format/shellcode.rb +7 -3
- data/metasm/exe_format/shellcode_rwx.rb +114 -0
- data/metasm/exe_format/swf.rb +205 -0
- data/metasm/exe_format/wasm.rb +402 -0
- data/{lib/metasm → metasm}/exe_format/xcoff.rb +7 -7
- data/metasm/exe_format/zip.rb +335 -0
- data/metasm/gui.rb +13 -0
- data/{lib/metasm → metasm}/gui/cstruct.rb +35 -41
- data/{lib/metasm → metasm}/gui/dasm_coverage.rb +11 -11
- data/{lib/metasm → metasm}/gui/dasm_decomp.rb +177 -114
- data/{lib/metasm → metasm}/gui/dasm_funcgraph.rb +0 -0
- data/metasm/gui/dasm_graph.rb +1754 -0
- data/{lib/metasm → metasm}/gui/dasm_hex.rb +16 -12
- data/{lib/metasm → metasm}/gui/dasm_listing.rb +43 -28
- data/{lib/metasm → metasm}/gui/dasm_main.rb +360 -77
- data/{lib/metasm → metasm}/gui/dasm_opcodes.rb +5 -19
- data/{lib/metasm → metasm}/gui/debug.rb +109 -34
- data/{lib/metasm → metasm}/gui/gtk.rb +174 -44
- data/{lib/metasm → metasm}/gui/qt.rb +14 -4
- data/{lib/metasm → metasm}/gui/win32.rb +180 -43
- data/{lib/metasm → metasm}/gui/x11.rb +59 -59
- data/{lib/metasm → metasm}/main.rb +421 -286
- data/metasm/os/emulator.rb +175 -0
- data/{lib/metasm/os/remote.rb → metasm/os/gdbremote.rb} +146 -54
- data/{lib/metasm → metasm}/os/gnu_exports.rb +1 -1
- data/{lib/metasm → metasm}/os/linux.rb +628 -151
- data/metasm/os/main.rb +335 -0
- data/{lib/metasm → metasm}/os/windows.rb +151 -58
- data/{lib/metasm → metasm}/os/windows_exports.rb +141 -0
- data/{lib/metasm → metasm}/parse.rb +49 -36
- data/{lib/metasm → metasm}/parse_c.rb +405 -246
- data/{lib/metasm → metasm}/preprocessor.rb +71 -41
- data/{lib/metasm → metasm}/render.rb +14 -38
- data/misc/hexdump.rb +4 -3
- data/misc/lint.rb +58 -0
- data/misc/objdiff.rb +4 -1
- data/misc/objscan.rb +1 -1
- data/misc/openrisc-parser.rb +79 -0
- data/misc/txt2html.rb +9 -7
- data/samples/bindiff.rb +3 -4
- data/samples/dasm-plugins/bindiff.rb +15 -0
- data/samples/dasm-plugins/bookmark.rb +133 -0
- data/samples/dasm-plugins/c_constants.rb +57 -0
- data/samples/dasm-plugins/colortheme_solarized.rb +125 -0
- data/samples/dasm-plugins/cppobj_funcall.rb +60 -0
- data/samples/dasm-plugins/dasm_all.rb +70 -0
- data/samples/dasm-plugins/demangle_cpp.rb +31 -0
- data/samples/dasm-plugins/deobfuscate.rb +251 -0
- data/samples/dasm-plugins/dump_text.rb +35 -0
- data/samples/dasm-plugins/export_graph_svg.rb +86 -0
- data/samples/dasm-plugins/findgadget.rb +75 -0
- data/samples/dasm-plugins/hl_opcode.rb +32 -0
- data/samples/dasm-plugins/hotfix_gtk_dbg.rb +19 -0
- data/samples/dasm-plugins/imm2off.rb +34 -0
- data/samples/dasm-plugins/match_libsigs.rb +93 -0
- data/samples/dasm-plugins/patch_file.rb +95 -0
- data/samples/dasm-plugins/scanfuncstart.rb +36 -0
- data/samples/dasm-plugins/scanxrefs.rb +29 -0
- data/samples/dasm-plugins/selfmodify.rb +197 -0
- data/samples/dasm-plugins/stringsxrefs.rb +28 -0
- data/samples/dasmnavig.rb +1 -1
- data/samples/dbg-apihook.rb +24 -9
- data/samples/dbg-plugins/heapscan.rb +283 -0
- data/samples/dbg-plugins/heapscan/compiled_heapscan_lin.c +155 -0
- data/samples/dbg-plugins/heapscan/compiled_heapscan_win.c +128 -0
- data/samples/dbg-plugins/heapscan/graphheap.rb +616 -0
- data/samples/dbg-plugins/heapscan/heapscan.rb +709 -0
- data/samples/dbg-plugins/heapscan/winheap.h +174 -0
- data/samples/dbg-plugins/heapscan/winheap7.h +307 -0
- data/samples/dbg-plugins/trace_func.rb +214 -0
- data/samples/disassemble-gui.rb +48 -7
- data/samples/disassemble.rb +31 -6
- data/samples/dump_upx.rb +24 -12
- data/samples/dynamic_ruby.rb +35 -27
- data/samples/elfencode.rb +15 -0
- data/samples/emubios.rb +251 -0
- data/samples/emudbg.rb +127 -0
- data/samples/exeencode.rb +6 -5
- data/samples/factorize-headers-peimports.rb +1 -1
- data/samples/lindebug.rb +186 -391
- data/samples/metasm-shell.rb +68 -57
- data/samples/peldr.rb +2 -2
- data/tests/all.rb +1 -1
- data/tests/arc.rb +26 -0
- data/tests/dynldr.rb +22 -4
- data/tests/expression.rb +57 -0
- data/tests/graph_layout.rb +285 -0
- data/tests/ia32.rb +80 -26
- data/tests/mcs51.rb +27 -0
- data/tests/mips.rb +10 -3
- data/tests/preprocessor.rb +18 -0
- data/tests/x86_64.rb +66 -18
- metadata +465 -219
- metadata.gz.sig +2 -0
- data/lib/metasm/arm/opcodes.rb +0 -177
- data/lib/metasm/gui.rb +0 -23
- data/lib/metasm/gui/dasm_graph.rb +0 -1354
- data/lib/metasm/ia32.rb +0 -14
- data/lib/metasm/ia32/opcodes.rb +0 -872
- data/lib/metasm/ppc/parse.rb +0 -52
- data/lib/metasm/x86_64.rb +0 -12
- data/lib/metasm/x86_64/opcodes.rb +0 -118
- data/samples/gdbclient.rb +0 -583
- data/samples/rubstop.rb +0 -399
@@ -4,8 +4,8 @@
|
|
4
4
|
# Licence is LGPL, see LICENCE in the top-level directory
|
5
5
|
|
6
6
|
|
7
|
-
require 'metasm/x86_64/opcodes'
|
8
|
-
require 'metasm/x86_64/encode'
|
7
|
+
require 'metasm/cpu/x86_64/opcodes'
|
8
|
+
require 'metasm/cpu/x86_64/encode'
|
9
9
|
require 'metasm/parse'
|
10
10
|
|
11
11
|
module Metasm
|
@@ -29,7 +29,7 @@ class X86_64
|
|
29
29
|
|
30
30
|
# needed due to how ruby inheritance works wrt constants
|
31
31
|
def parse_argregclasslist
|
32
|
-
[Reg, SimdReg, SegReg, DbgReg, CtrlReg, FpReg]
|
32
|
+
[Reg, SimdReg, SegReg, DbgReg, TstReg, CtrlReg, FpReg]
|
33
33
|
end
|
34
34
|
# same inheritance sh*t
|
35
35
|
def parse_modrm(lex, tok, cpu)
|
@@ -49,9 +49,11 @@ class X86_64
|
|
49
49
|
|
50
50
|
# check if the argument matches the opcode's argument spec
|
51
51
|
def parse_arg_valid?(o, spec, arg)
|
52
|
-
return if arg.kind_of? ModRM and ((arg.b and arg.b.val == 16 and arg.i) or (arg.i and arg.i.val == 16 and (arg.b or arg.s != 1)))
|
52
|
+
return if arg.kind_of? ModRM and ((arg.b and arg.b.val == 16 and arg.i) or (arg.i and arg.i.val == 16 and (arg.b or arg.s != 1))) # cannot encode [rip+base], only [rip+imm]
|
53
53
|
return if arg.kind_of? Reg and arg.sz >= 32 and arg.val == 16 # eip/rip only in modrm
|
54
54
|
return if o.props[:auto64] and arg.respond_to? :sz and arg.sz == 32
|
55
|
+
# vex c4/c5
|
56
|
+
return if o.fields[:vex_r] and not o.fields[:vex_b] and (spec == :modrm or spec == :modrmxmm or spec == :modrmymm) and (((arg.kind_of?(SimdReg) or arg.kind_of?(Reg)) and arg.val >= 8) or (arg.kind_of?(ModRM) and ((arg.b and arg.b.val >= 8) or (arg.i and arg.i.val >= 8))))
|
55
57
|
if o.name == 'movsxd'
|
56
58
|
return if not arg.kind_of? Reg and not arg.kind_of? ModRM
|
57
59
|
arg.sz ||= 32
|
@@ -62,7 +64,13 @@ class X86_64
|
|
62
64
|
return arg.sz == 32
|
63
65
|
end
|
64
66
|
end
|
67
|
+
return if o.name == 'xchg' and spec == :reg and o.args.include?(:reg_eax) and arg.kind_of?(Reg) and arg.sz == 32 and arg.val == 0
|
68
|
+
|
65
69
|
super(o, spec, arg)
|
66
70
|
end
|
71
|
+
|
72
|
+
def check_reserved_name(name)
|
73
|
+
Reg.s_to_i[name]
|
74
|
+
end
|
67
75
|
end
|
68
76
|
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# This file is part of Metasm, the Ruby assembly manipulation suite
|
2
|
+
# Copyright (C) 2006-2009 Yoann GUILLOT
|
3
|
+
#
|
4
|
+
# Licence is LGPL, see LICENCE in the top-level directory
|
5
|
+
|
6
|
+
|
7
|
+
require 'metasm/cpu/x86_64/opcodes'
|
8
|
+
require 'metasm/render'
|
9
|
+
|
10
|
+
module Metasm
|
11
|
+
class X86_64
|
12
|
+
def gui_hilight_word_regexp_init
|
13
|
+
ret = {}
|
14
|
+
|
15
|
+
%w[a b c d].each { |r|
|
16
|
+
ret["#{r}l"] = "[re]?#{r}x|#{r}l"
|
17
|
+
ret["#{r}h"] = "[re]?#{r}x|#{r}h"
|
18
|
+
ret["#{r}x"] = ret["e#{r}x"] = ret["r#{r}x"] = "[re]?#{r}x|#{r}[hl]"
|
19
|
+
}
|
20
|
+
|
21
|
+
%w[sp bp si di].each { |r|
|
22
|
+
ret["#{r}l"] = ret[r] = ret["e#{r}"] = ret["r#{r}"] = "[re]?#{r}|#{r}l"
|
23
|
+
}
|
24
|
+
|
25
|
+
(8..15).each { |i|
|
26
|
+
r = "r#{i}"
|
27
|
+
ret[r+'b'] = ret[r+'w'] = ret[r+'d'] = ret[r] = "#{r}[bwd]?"
|
28
|
+
}
|
29
|
+
|
30
|
+
ret['eip'] = ret['rip'] = '[re]ip'
|
31
|
+
|
32
|
+
ret
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
data/metasm/cpu/z80.rb
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
# This file is part of Metasm, the Ruby assembly manipulation suite
|
2
|
+
# Copyright (C) 2006-2009 Yoann GUILLOT
|
3
|
+
#
|
4
|
+
# Licence is LGPL, see LICENCE in the top-level directory
|
5
|
+
|
6
|
+
|
7
|
+
require 'metasm/main'
|
8
|
+
require 'metasm/cpu/z80/decode'
|
9
|
+
require 'metasm/cpu/z80/render'
|
@@ -0,0 +1,286 @@
|
|
1
|
+
# This file is part of Metasm, the Ruby assembly manipulation suite
|
2
|
+
# Copyright (C) 2006-2009 Yoann GUILLOT
|
3
|
+
#
|
4
|
+
# Licence is LGPL, see LICENCE in the top-level directory
|
5
|
+
|
6
|
+
|
7
|
+
require 'metasm/cpu/z80/opcodes'
|
8
|
+
require 'metasm/decode'
|
9
|
+
|
10
|
+
module Metasm
|
11
|
+
class Z80
|
12
|
+
def build_opcode_bin_mask(op)
|
13
|
+
# bit = 0 if can be mutated by an field value, 1 if fixed by opcode
|
14
|
+
op.bin_mask = Array.new(op.bin.length, 0)
|
15
|
+
op.fields.each { |f, (oct, off)|
|
16
|
+
op.bin_mask[oct] |= (@fields_mask[f] << off)
|
17
|
+
}
|
18
|
+
op.bin_mask.map! { |v| 255 ^ v }
|
19
|
+
end
|
20
|
+
|
21
|
+
def build_bin_lookaside
|
22
|
+
# sets up a hash byte value => list of opcodes that may match
|
23
|
+
# opcode.bin_mask is built here
|
24
|
+
lookaside = Array.new(256) { [] }
|
25
|
+
opcode_list.each { |op|
|
26
|
+
build_opcode_bin_mask op
|
27
|
+
b = op.bin[0]
|
28
|
+
msk = op.bin_mask[0]
|
29
|
+
next @unknown_opcode = op if not b
|
30
|
+
for i in b..(b | (255^msk))
|
31
|
+
lookaside[i] << op if i & msk == b & msk
|
32
|
+
end
|
33
|
+
}
|
34
|
+
lookaside
|
35
|
+
end
|
36
|
+
|
37
|
+
def decode_prefix(instr, byte)
|
38
|
+
case byte
|
39
|
+
when 0xDD; instr.prefix = 0xDD
|
40
|
+
when 0xFD; instr.prefix = 0xFD
|
41
|
+
# implicit 'else return false'
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
# tries to find the opcode encoded at edata.ptr
|
46
|
+
# if no match, tries to match a prefix (update di.instruction.prefix)
|
47
|
+
# on match, edata.ptr points to the first byte of the opcode (after prefixes)
|
48
|
+
def decode_findopcode(edata)
|
49
|
+
di = DecodedInstruction.new self
|
50
|
+
while edata.ptr < edata.data.length
|
51
|
+
byte = edata.data[edata.ptr]
|
52
|
+
byte = byte.unpack('C').first if byte.kind_of?(::String)
|
53
|
+
return di if di.opcode = @bin_lookaside[byte].find { |op|
|
54
|
+
# fetch the relevant bytes from edata
|
55
|
+
bseq = edata.data[edata.ptr, op.bin.length].unpack('C*')
|
56
|
+
# check against full opcode mask
|
57
|
+
op.bin.zip(bseq, op.bin_mask).all? { |b1, b2, m| b2 and ((b1 & m) == (b2 & m)) }
|
58
|
+
}
|
59
|
+
|
60
|
+
if decode_prefix(di.instruction, edata.get_byte)
|
61
|
+
nb = edata.data[edata.ptr]
|
62
|
+
nb = nb.unpack('C').first if nb.kind_of?(::String)
|
63
|
+
case nb
|
64
|
+
when 0xCB
|
65
|
+
# DD CB <disp8> <opcode_pfxCB> [<args>]
|
66
|
+
di.instruction.prefix |= edata.get_byte << 8
|
67
|
+
di.bin_length += 2
|
68
|
+
opc = edata.data[edata.ptr+1]
|
69
|
+
opc = opc.unpack('C').first if opc.kind_of?(::String)
|
70
|
+
bseq = [0xCB, opc]
|
71
|
+
# XXX in decode_instr_op, byte[0] is the immediate displacement instead of cb
|
72
|
+
return di if di.opcode = @bin_lookaside[nb].find { |op|
|
73
|
+
op.bin.zip(bseq, op.bin_mask).all? { |b1, b2, m| b2 and ((b1 & m) == (b2 & m)) }
|
74
|
+
}
|
75
|
+
when 0xED
|
76
|
+
di.instruction.prefix = nil
|
77
|
+
end
|
78
|
+
else
|
79
|
+
di.opcode = @unknown_opcode
|
80
|
+
return di
|
81
|
+
end
|
82
|
+
di.bin_length += 1
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
|
87
|
+
def decode_instr_op(edata, di)
|
88
|
+
before_ptr = edata.ptr
|
89
|
+
op = di.opcode
|
90
|
+
di.instruction.opname = op.name
|
91
|
+
bseq = edata.read(op.bin.length).unpack('C*') # decode_findopcode ensures that data >= op.length
|
92
|
+
pfx = di.instruction.prefix
|
93
|
+
|
94
|
+
field_val = lambda { |f|
|
95
|
+
if fld = op.fields[f]
|
96
|
+
(bseq[fld[0]] >> fld[1]) & @fields_mask[f]
|
97
|
+
end
|
98
|
+
}
|
99
|
+
|
100
|
+
op.args.each { |a|
|
101
|
+
di.instruction.args << case a
|
102
|
+
when :i8, :u8, :i16, :u16; Expression[edata.decode_imm(a, @endianness)]
|
103
|
+
when :iy; Expression[field_val[a]]
|
104
|
+
when :iy8; Expression[field_val[a]*8]
|
105
|
+
|
106
|
+
when :rp
|
107
|
+
v = field_val[a]
|
108
|
+
Reg.new(16, v)
|
109
|
+
when :rp2
|
110
|
+
v = field_val[a]
|
111
|
+
v = 4 if v == 3
|
112
|
+
Reg.new(16, v)
|
113
|
+
when :ry, :rz
|
114
|
+
v = field_val[a]
|
115
|
+
if v == 6
|
116
|
+
Memref.new(Reg.from_str('HL'), nil, 1)
|
117
|
+
else
|
118
|
+
Reg.new(8, v)
|
119
|
+
end
|
120
|
+
|
121
|
+
when :r_a; Reg.from_str('A')
|
122
|
+
when :r_af; Reg.from_str('AF')
|
123
|
+
when :r_hl; Reg.from_str('HL')
|
124
|
+
when :r_de; Reg.from_str('DE')
|
125
|
+
when :r_sp; Reg.from_str('SP')
|
126
|
+
when :r_i; Reg.from_str('I')
|
127
|
+
|
128
|
+
when :m16; Memref.new(nil, edata.decode_imm(:u16, @endianness), nil)
|
129
|
+
when :m_bc; Memref.new(Reg.from_str('BC'), nil, 1)
|
130
|
+
when :m_de; Memref.new(Reg.from_str('DE'), nil, 1)
|
131
|
+
when :m_sp; Memref.new(Reg.from_str('SP'), nil, 2)
|
132
|
+
when :m_hl; Memref.new(Reg.from_str('HL'), nil, 1)
|
133
|
+
when :mf8; Memref.new(nil, 0xff00 + edata.decode_imm(:u8, @endianness), 1)
|
134
|
+
when :mfc; Memref.new(Reg.from_str('C'), 0xff00, 1)
|
135
|
+
|
136
|
+
else raise SyntaxError, "Internal error: invalid argument #{a} in #{op.name}"
|
137
|
+
end
|
138
|
+
}
|
139
|
+
|
140
|
+
case pfx
|
141
|
+
when 0xDD
|
142
|
+
when 0xFD
|
143
|
+
when 0xCBDD
|
144
|
+
when 0xCBFD
|
145
|
+
end
|
146
|
+
|
147
|
+
di.bin_length += edata.ptr - before_ptr
|
148
|
+
|
149
|
+
return if edata.ptr > edata.length
|
150
|
+
|
151
|
+
di
|
152
|
+
end
|
153
|
+
|
154
|
+
# populate the @backtrace_binding hash with default values
|
155
|
+
def init_backtrace_binding
|
156
|
+
@backtrace_binding ||= {}
|
157
|
+
|
158
|
+
mask = 0xffff
|
159
|
+
|
160
|
+
opcode_list.map { |ol| ol.basename }.uniq.sort.each { |op|
|
161
|
+
binding = case op
|
162
|
+
when 'ld'; lambda { |di, a0, a1, *aa| a2 = aa[0] ; a2 ? { a0 => Expression[a1, :+, a2] } : { a0 => Expression[a1] } }
|
163
|
+
when 'ldi'; lambda { |di, a0, a1| hl = (a0 == :a ? a1 : a0) ; { a0 => Expression[a1], hl => Expression[hl, :+, 1] } }
|
164
|
+
when 'ldd'; lambda { |di, a0, a1| hl = (a0 == :a ? a1 : a0) ; { a0 => Expression[a1], hl => Expression[hl, :-, 1] } }
|
165
|
+
when 'add', 'adc', 'sub', 'sbc', 'and', 'xor', 'or'
|
166
|
+
lambda { |di, a0, a1|
|
167
|
+
e_op = { 'add' => :+, 'adc' => :+, 'sub' => :-, 'sbc' => :-, 'and' => :&, 'xor' => :^, 'or' => :| }[op]
|
168
|
+
ret = Expression[a0, e_op, a1]
|
169
|
+
ret = Expression[ret, e_op, :flag_c] if op == 'adc' or op == 'sbc'
|
170
|
+
ret = Expression[ret.reduce] if not a0.kind_of? Indirection
|
171
|
+
{ a0 => ret }
|
172
|
+
}
|
173
|
+
when 'cp', 'cmp'; lambda { |di, *a| {} }
|
174
|
+
when 'inc'; lambda { |di, a0| { a0 => Expression[a0, :+, 1] } }
|
175
|
+
when 'dec'; lambda { |di, a0| { a0 => Expression[a0, :-, 1] } }
|
176
|
+
when 'not'; lambda { |di, a0| { a0 => Expression[a0, :^, mask] } }
|
177
|
+
when 'push'
|
178
|
+
lambda { |di, a0| { :sp => Expression[:sp, :-, 2],
|
179
|
+
Indirection[:sp, 2, di.address] => Expression[a0] } }
|
180
|
+
when 'pop'
|
181
|
+
lambda { |di, a0| { :sp => Expression[:sp, :+, 2],
|
182
|
+
a0 => Indirection[:sp, 2, di.address] } }
|
183
|
+
when 'call'
|
184
|
+
lambda { |di, a0| { :sp => Expression[:sp, :-, 2],
|
185
|
+
Indirection[:sp, 2, di.address] => Expression[di.next_addr] }
|
186
|
+
}
|
187
|
+
when 'ret', 'reti'; lambda { |di, *a| { :sp => Expression[:sp, :+, 2] } }
|
188
|
+
# TODO callCC, retCC ...
|
189
|
+
when 'bswap'
|
190
|
+
lambda { |di, a0| { a0 => Expression[
|
191
|
+
[[a0, :&, 0xff00], :>>, 8], :|,
|
192
|
+
[[a0, :&, 0x00ff], :<<, 8]] } }
|
193
|
+
when 'nop', /^j/; lambda { |di, *a| {} }
|
194
|
+
end
|
195
|
+
|
196
|
+
# TODO flags ?
|
197
|
+
|
198
|
+
@backtrace_binding[op] ||= binding if binding
|
199
|
+
}
|
200
|
+
@backtrace_binding
|
201
|
+
end
|
202
|
+
|
203
|
+
# patch a forward binding from the backtrace binding
|
204
|
+
def fix_fwdemu_binding(di, fbd)
|
205
|
+
case di.opcode.name
|
206
|
+
when 'push', 'call'; fbd[Indirection[[:sp, :-, 2], 2]] = fbd.delete(Indirection[:sp, 2])
|
207
|
+
end
|
208
|
+
fbd
|
209
|
+
end
|
210
|
+
|
211
|
+
def get_xrefs_x(dasm, di)
|
212
|
+
return [] if not di.opcode.props[:setip]
|
213
|
+
|
214
|
+
case di.opcode.basename
|
215
|
+
when 'ret', 'reti'
|
216
|
+
return [Indirection[:sp, 2, di.address]]
|
217
|
+
when /^jr|^djnz/
|
218
|
+
# jmp/call are absolute addrs, only jr/djnz are relative
|
219
|
+
# also, the asm source should display the relative offset
|
220
|
+
return [Expression[[di.address, :+, di.bin_length], :+, di.instruction.args.first]]
|
221
|
+
end
|
222
|
+
|
223
|
+
case tg = di.instruction.args.first
|
224
|
+
when Memref; [Expression[tg.symbolic(di)]]
|
225
|
+
when Reg; [Expression[tg.symbolic(di)]]
|
226
|
+
when Expression, ::Integer; [Expression[tg]]
|
227
|
+
else
|
228
|
+
puts "unhandled setip at #{di.address} #{di.instruction}" if $DEBUG
|
229
|
+
[]
|
230
|
+
end
|
231
|
+
end
|
232
|
+
|
233
|
+
# checks if expr is a valid return expression matching the :saveip instruction
|
234
|
+
def backtrace_is_function_return(expr, di=nil)
|
235
|
+
expr = Expression[expr].reduce_rec
|
236
|
+
expr.kind_of?(Indirection) and expr.len == 2 and expr.target == Expression[:sp]
|
237
|
+
end
|
238
|
+
|
239
|
+
# updates the function backtrace_binding
|
240
|
+
# if the function is big and no specific register is given, do nothing (the binding will be lazily updated later, on demand)
|
241
|
+
def backtrace_update_function_binding(dasm, faddr, f, retaddrlist, *wantregs)
|
242
|
+
b = f.backtrace_binding
|
243
|
+
|
244
|
+
bt_val = lambda { |r|
|
245
|
+
next if not retaddrlist
|
246
|
+
b[r] = Expression::Unknown
|
247
|
+
bt = []
|
248
|
+
retaddrlist.each { |retaddr|
|
249
|
+
bt |= dasm.backtrace(Expression[r], retaddr, :include_start => true,
|
250
|
+
:snapshot_addr => faddr, :origin => retaddr)
|
251
|
+
}
|
252
|
+
if bt.length != 1
|
253
|
+
b[r] = Expression::Unknown
|
254
|
+
else
|
255
|
+
b[r] = bt.first
|
256
|
+
end
|
257
|
+
}
|
258
|
+
|
259
|
+
if not wantregs.empty?
|
260
|
+
wantregs.each(&bt_val)
|
261
|
+
else
|
262
|
+
bt_val[:sp]
|
263
|
+
end
|
264
|
+
|
265
|
+
b
|
266
|
+
end
|
267
|
+
|
268
|
+
# returns true if the expression is an address on the stack
|
269
|
+
def backtrace_is_stack_address(expr)
|
270
|
+
Expression[expr].expr_externals.include?(:sp)
|
271
|
+
end
|
272
|
+
|
273
|
+
# updates an instruction's argument replacing an expression with another (eg label renamed)
|
274
|
+
def replace_instr_arg_immediate(i, old, new)
|
275
|
+
i.args.map! { |a|
|
276
|
+
case a
|
277
|
+
when Expression; a == old ? new : Expression[a.bind(old => new).reduce]
|
278
|
+
when Memref
|
279
|
+
a.offset = (a.offset == old ? new : Expression[a.offset.bind(old => new).reduce]) if a.offset
|
280
|
+
a
|
281
|
+
else a
|
282
|
+
end
|
283
|
+
}
|
284
|
+
end
|
285
|
+
end
|
286
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
# This file is part of Metasm, the Ruby assembly manipulation suite
|
2
|
+
# Copyright (C) 2006-2009 Yoann GUILLOT
|
3
|
+
#
|
4
|
+
# Licence is LGPL, see LICENCE in the top-level directory
|
5
|
+
|
6
|
+
|
7
|
+
require 'metasm/main'
|
8
|
+
|
9
|
+
module Metasm
|
10
|
+
class Z80 < CPU
|
11
|
+
class Reg
|
12
|
+
class << self
|
13
|
+
attr_accessor :s_to_i, :i_to_s
|
14
|
+
end
|
15
|
+
@i_to_s = { 8 => { 0 => 'B', 1 => 'C', 2 => 'D', 3 => 'E',
|
16
|
+
4 => 'H', 5 => 'L', 7 => 'A' },
|
17
|
+
16 => { 0 => 'BC', 1 => 'DE', 2 => 'HL', 3 => 'SP',
|
18
|
+
4 => 'AF' } } # AF is 3 too
|
19
|
+
@s_to_i = @i_to_s.inject({}) { |h, (sz, rh)|
|
20
|
+
h.update rh.inject({}) { |hh, (i, n)|
|
21
|
+
hh.update n => [sz, i] } }
|
22
|
+
|
23
|
+
attr_accessor :sz, :i
|
24
|
+
def initialize(sz, i)
|
25
|
+
@sz = sz
|
26
|
+
@i = i
|
27
|
+
end
|
28
|
+
|
29
|
+
def symbolic(di=nil) ; to_s.to_sym ; end
|
30
|
+
|
31
|
+
def self.from_str(s)
|
32
|
+
raise "Bad name #{s.inspect}" if not x = @s_to_i[s]
|
33
|
+
new(*x)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
class Memref
|
38
|
+
attr_accessor :base, :offset, :sz
|
39
|
+
def initialize(base, offset, sz=nil)
|
40
|
+
@base = base
|
41
|
+
offset = Expression[offset] if offset
|
42
|
+
@offset = offset
|
43
|
+
@sz = sz
|
44
|
+
end
|
45
|
+
|
46
|
+
def symbolic(di=nil)
|
47
|
+
p = nil
|
48
|
+
p = Expression[p, :+, @base.symbolic] if base
|
49
|
+
p = Expression[p, :+, @offset] if offset
|
50
|
+
Indirection[p.reduce, @sz, (di.address if di)]
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def initialize(family = :latest)
|
55
|
+
super()
|
56
|
+
@endianness = :little
|
57
|
+
@size = 16
|
58
|
+
@family = family
|
59
|
+
end
|
60
|
+
|
61
|
+
def init_opcode_list
|
62
|
+
send("init_#@family")
|
63
|
+
@opcode_list
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
@@ -0,0 +1,224 @@
|
|
1
|
+
# This file is part of Metasm, the Ruby assembly manipulation suite
|
2
|
+
# Copyright (C) 2006-2009 Yoann GUILLOT
|
3
|
+
#
|
4
|
+
# Licence is LGPL, see LICENCE in the top-level directory
|
5
|
+
|
6
|
+
require 'metasm/cpu/z80/main'
|
7
|
+
|
8
|
+
module Metasm
|
9
|
+
|
10
|
+
class Z80
|
11
|
+
def addop(name, bin, *args)
|
12
|
+
o = Opcode.new name, bin
|
13
|
+
args.each { |a|
|
14
|
+
o.args << a if @fields_mask[a] or @valid_args[a]
|
15
|
+
o.props[a] = true if @valid_props[a]
|
16
|
+
o.fields[a] = [bin.length-1, @fields_shift[a]] if @fields_mask[a]
|
17
|
+
raise "wtf #{a.inspect}" unless @valid_args[a] or @valid_props[a] or @fields_mask[a]
|
18
|
+
}
|
19
|
+
@opcode_list << o
|
20
|
+
end
|
21
|
+
|
22
|
+
def addop_macrocc(name, bin, *args)
|
23
|
+
%w[nz z nc c po pe p m].each_with_index { |cc, i|
|
24
|
+
dbin = bin.dup
|
25
|
+
dbin[0] |= i << 3
|
26
|
+
addop name + cc, dbin, *args
|
27
|
+
}
|
28
|
+
end
|
29
|
+
|
30
|
+
# data from http://www.z80.info/decoding.htm
|
31
|
+
def init_z80_common
|
32
|
+
@opcode_list = []
|
33
|
+
@valid_args.update [:i8, :u8, :i16, :u16, :m16,
|
34
|
+
:r_a, :r_af, :r_hl, :r_de, :r_sp, :r_i,
|
35
|
+
:m_bc, :m_de, :m_sp, :m_hl, :mf8, :mfc
|
36
|
+
].inject({}) { |h, v| h.update v => true }
|
37
|
+
@fields_mask.update :rz => 7, :ry => 7, :rp => 3, :rp2 => 3, :iy => 7, :iy8 => 7
|
38
|
+
@fields_shift.update :rz => 0, :ry => 3, :rp => 4, :rp2 => 4, :iy => 3, :iy8 => 3
|
39
|
+
|
40
|
+
# some opcodes are in init_z80 when they are not part of the GB ABI
|
41
|
+
addop 'nop', [0b00_000_000]
|
42
|
+
addop 'jr', [0b00_011_000], :setip, :stopexec, :i8
|
43
|
+
%w[nz z nc c].each_with_index { |cc, i|
|
44
|
+
addop 'jr' + cc, [0b00_100_000 | (i << 3)], :setip, :i8
|
45
|
+
}
|
46
|
+
addop 'ld', [0b00_000_001], :rp, :i16
|
47
|
+
addop 'add', [0b00_001_001], :r_hl, :rp
|
48
|
+
|
49
|
+
addop 'ld', [0b00_000_010], :m_bc, :r_a
|
50
|
+
addop 'ld', [0b00_001_010], :r_a, :m_bc
|
51
|
+
addop 'ld', [0b00_010_010], :m_de, :r_a
|
52
|
+
addop 'ld', [0b00_011_010], :r_a, :m_de
|
53
|
+
|
54
|
+
addop 'inc', [0b00_000_011], :rp
|
55
|
+
addop 'dec', [0b00_001_011], :rp
|
56
|
+
addop 'inc', [0b00_000_100], :ry
|
57
|
+
addop 'dec', [0b00_000_101], :ry
|
58
|
+
addop 'ld', [0b00_000_110], :ry, :i8
|
59
|
+
|
60
|
+
addop 'rlca', [0b00_000_111] # rotate
|
61
|
+
addop 'rrca', [0b00_001_111]
|
62
|
+
addop 'rla', [0b00_010_111]
|
63
|
+
addop 'rra', [0b00_011_111]
|
64
|
+
|
65
|
+
addop 'daa', [0b00_100_111]
|
66
|
+
addop 'cpl', [0b00_101_111]
|
67
|
+
addop 'scf', [0b00_110_111]
|
68
|
+
addop 'ccf', [0b00_111_111]
|
69
|
+
|
70
|
+
addop 'halt', [0b01_110_110] # ld (HL), (HL)
|
71
|
+
addop 'ld', [0b01_000_000], :ry, :rz
|
72
|
+
|
73
|
+
addop 'add', [0b10_000_000], :r_a, :rz
|
74
|
+
addop 'adc', [0b10_001_000], :r_a, :rz
|
75
|
+
addop 'sub', [0b10_010_000], :r_a, :rz
|
76
|
+
addop 'sbc', [0b10_011_000], :r_a, :rz
|
77
|
+
addop 'and', [0b10_100_000], :r_a, :rz
|
78
|
+
addop 'xor', [0b10_101_000], :r_a, :rz
|
79
|
+
addop 'or', [0b10_110_000], :r_a, :rz
|
80
|
+
addop 'cmp', [0b10_111_000], :r_a, :rz # alias cp
|
81
|
+
addop 'cp', [0b10_111_000], :r_a, :rz # compare
|
82
|
+
|
83
|
+
addop_macrocc 'ret', [0b11_000_000], :setip
|
84
|
+
addop 'pop', [0b11_000_001], :rp2
|
85
|
+
addop 'ret', [0b11_001_001], :stopexec, :setip
|
86
|
+
addop 'jmp', [0b11_101_001], :r_hl, :setip, :stopexec # alias jp
|
87
|
+
addop 'jp', [0b11_101_001], :r_hl, :setip, :stopexec
|
88
|
+
addop 'ld', [0b11_111_001], :r_sp, :r_hl
|
89
|
+
addop_macrocc 'j', [0b11_000_010], :setip, :u16 # alias jp
|
90
|
+
addop_macrocc 'jp', [0b11_000_010], :setip, :u16
|
91
|
+
addop 'jmp', [0b11_000_011], :setip, :stopexec, :u16 # alias jp
|
92
|
+
addop 'jp', [0b11_000_011], :setip, :stopexec, :u16
|
93
|
+
|
94
|
+
addop 'di', [0b11_110_011] # disable interrupts
|
95
|
+
addop 'ei', [0b11_111_011]
|
96
|
+
addop_macrocc 'call', [0b11_000_100], :u16, :setip, :saveip
|
97
|
+
addop 'push', [0b11_000_101], :rp2
|
98
|
+
addop 'call', [0b11_001_101], :u16, :setip, :saveip, :stopexec
|
99
|
+
|
100
|
+
addop 'add', [0b11_000_110], :r_a, :i8
|
101
|
+
addop 'adc', [0b11_001_110], :r_a, :i8
|
102
|
+
addop 'sub', [0b11_010_110], :r_a, :i8
|
103
|
+
addop 'sbc', [0b11_011_110], :r_a, :i8
|
104
|
+
addop 'and', [0b11_100_110], :r_a, :i8
|
105
|
+
addop 'xor', [0b11_101_110], :r_a, :i8
|
106
|
+
addop 'or', [0b11_110_110], :r_a, :i8
|
107
|
+
addop 'cp', [0b11_111_110], :r_a, :i8
|
108
|
+
|
109
|
+
addop 'rst', [0b11_000_111], :iy8 # call off in page 0
|
110
|
+
|
111
|
+
addop 'rlc', [0xCB, 0b00_000_000], :rz # rotate
|
112
|
+
addop 'rrc', [0xCB, 0b00_001_000], :rz
|
113
|
+
addop 'rl', [0xCB, 0b00_010_000], :rz
|
114
|
+
addop 'rr', [0xCB, 0b00_011_000], :rz
|
115
|
+
addop 'sla', [0xCB, 0b00_100_000], :rz # shift
|
116
|
+
addop 'sra', [0xCB, 0b00_101_000], :rz
|
117
|
+
addop 'srl', [0xCB, 0b00_111_000], :rz
|
118
|
+
addop 'bit', [0xCB, 0b01_000_000], :iy, :rz # bit test
|
119
|
+
addop 'res', [0xCB, 0b10_000_000], :iy, :rz # bit reset
|
120
|
+
addop 'set', [0xCB, 0b11_000_000], :iy, :rz # bit set
|
121
|
+
end
|
122
|
+
|
123
|
+
# standard z80
|
124
|
+
def init_z80
|
125
|
+
init_z80_common
|
126
|
+
|
127
|
+
addop 'ex', [0b00_001_000], :r_af # XXX really ex AF, AF' ...
|
128
|
+
addop 'djnz', [0b00_010_000], :setip, :i8
|
129
|
+
|
130
|
+
addop 'ld', [0b00_100_010], :m16, :r_hl
|
131
|
+
addop 'ld', [0b00_101_010], :r_hl, :m16
|
132
|
+
addop 'ld', [0b00_110_010], :m16, :r_a
|
133
|
+
addop 'ld', [0b00_111_010], :r_a, :m16
|
134
|
+
|
135
|
+
addop 'exx', [0b11_011_001]
|
136
|
+
addop 'out', [0b11_010_011], :i8, :r_a
|
137
|
+
addop 'in', [0b11_011_011], :r_a, :i8
|
138
|
+
|
139
|
+
addop 'ex', [0b11_100_011], :m_sp, :r_hl
|
140
|
+
addop 'ex', [0b11_101_011], :r_de, :r_hl
|
141
|
+
|
142
|
+
addop 'sll', [0xCB, 0b00_110_000], :rz
|
143
|
+
|
144
|
+
addop 'in', [0xED, 0b01_110_000], :u16
|
145
|
+
addop 'in', [0xED, 0b01_000_000], :ry, :u16
|
146
|
+
addop 'out', [0xED, 0b01_110_001], :u16
|
147
|
+
addop 'out', [0xED, 0b01_000_001], :u16, :ry
|
148
|
+
addop 'sbc', [0xED, 0b01_000_010], :r_hl, :rp
|
149
|
+
addop 'adc', [0xED, 0b01_001_010], :r_hl, :rp
|
150
|
+
addop 'ld', [0xED, 0b01_000_011], :m16, :rp
|
151
|
+
addop 'ld', [0xED, 0b01_001_011], :rp, :m16
|
152
|
+
addop 'neg', [0xED, 0b01_000_100], :r_a, :iy # dummy int field
|
153
|
+
addop 'retn', [0xED, 0b01_000_101], :stopexec # dummy int != 1 ? (1 = reti)
|
154
|
+
addop 'reti', [0xED, 0b01_001_101], :stopexec, :setip
|
155
|
+
addop 'im', [0xED, 0b01_000_110], :iy
|
156
|
+
addop 'ld', [0xED, 0b01_000_111], :r_i, :r_a
|
157
|
+
addop 'ld', [0xED, 0b01_001_111], :r_r, :r_a
|
158
|
+
addop 'ld', [0xED, 0b01_010_111], :r_a, :r_i
|
159
|
+
addop 'ld', [0xED, 0b01_011_111], :r_a, :r_r
|
160
|
+
addop 'rrd', [0xED, 0b01_100_111]
|
161
|
+
addop 'rld', [0xED, 0b01_101_111]
|
162
|
+
|
163
|
+
addop 'ldi', [0xED, 0b10_100_000]
|
164
|
+
addop 'ldd', [0xED, 0b10_101_000]
|
165
|
+
addop 'ldir', [0xED, 0b10_110_000]
|
166
|
+
addop 'lddr', [0xED, 0b10_111_000]
|
167
|
+
addop 'cpi', [0xED, 0b10_100_001]
|
168
|
+
addop 'cpd', [0xED, 0b10_101_001]
|
169
|
+
addop 'cpir', [0xED, 0b10_110_001]
|
170
|
+
addop 'cpdr', [0xED, 0b10_111_001]
|
171
|
+
addop 'ini', [0xED, 0b10_100_010]
|
172
|
+
addop 'ind', [0xED, 0b10_101_010]
|
173
|
+
addop 'inir', [0xED, 0b10_110_010]
|
174
|
+
addop 'indr', [0xED, 0b10_111_010]
|
175
|
+
addop 'outi', [0xED, 0b10_100_011]
|
176
|
+
addop 'outd', [0xED, 0b10_101_011]
|
177
|
+
addop 'otir', [0xED, 0b10_110_011]
|
178
|
+
addop 'otdr', [0xED, 0b10_111_011]
|
179
|
+
|
180
|
+
addop 'unk_ed', [0xED], :i8
|
181
|
+
|
182
|
+
addop 'unk_nop', [], :i8 # undefined opcode = nop
|
183
|
+
@unknown_opcode = @opcode_list.last
|
184
|
+
end
|
185
|
+
|
186
|
+
# gameboy processor
|
187
|
+
# from http://nocash.emubase.de/pandocs.htm#cpucomparisionwithz80
|
188
|
+
def init_gb
|
189
|
+
init_z80_common
|
190
|
+
|
191
|
+
addop 'ld', [0x08], :m16, :r_sp
|
192
|
+
addop 'stop', [0x10]
|
193
|
+
|
194
|
+
addop 'ldi', [0x22], :m_hl, :r_a # (hl++) <- a
|
195
|
+
addop 'ldi', [0x2A], :r_a, :m_hl
|
196
|
+
addop 'ldd', [0x32], :m_hl, :r_a # (hl--) <- a
|
197
|
+
addop 'ldd', [0x3A], :r_a, :m_hl
|
198
|
+
|
199
|
+
addop 'reti', [0xD9], :setip, :stopexec
|
200
|
+
|
201
|
+
# override retpo/jpo
|
202
|
+
@opcode_list.delete_if { |op| op.bin[0] & 0xE5 == 0xE0 } # rm E0 E2 E8 EA F0 F2 F8 FA
|
203
|
+
addop 'ld', [0xE0], :mf8, :r_a # (0xff00 + :i8)
|
204
|
+
addop 'ld', [0xE2], :mfc, :r_a # (0xff00 + :r_c)
|
205
|
+
addop 'add', [0xE8], :r_sp, :i8
|
206
|
+
addop 'ld', [0xEA], :m16, :r_a
|
207
|
+
addop 'ld', [0xF0], :r_a, :mf8
|
208
|
+
addop 'ld', [0xF2], :r_a, :mfc
|
209
|
+
addop 'ld', [0xF8], :r_hl, :r_sp, :i8 # hl <- sp+:i8
|
210
|
+
addop 'ld', [0xFA], :r_a, :m16
|
211
|
+
|
212
|
+
addop 'swap', [0xCB, 0x30], :rz
|
213
|
+
|
214
|
+
addop 'inv_dd', [0xDD], :stopexec # invalid prefixes
|
215
|
+
addop 'inv_ed', [0xED], :stopexec
|
216
|
+
addop 'inv_fd', [0xFD], :stopexec
|
217
|
+
|
218
|
+
addop 'unk_nop', [], :i8 # undefined opcode = nop
|
219
|
+
@unknown_opcode = @opcode_list.last
|
220
|
+
end
|
221
|
+
|
222
|
+
alias init_latest init_z80
|
223
|
+
end
|
224
|
+
end
|