1 | unit Disassembler;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | uses
|
---|
6 | Classes, SysUtils, Cpu, Instructions, StrUtils, IntMemory, BigInt;
|
---|
7 |
|
---|
8 | type
|
---|
9 |
|
---|
10 | { TDisassembler }
|
---|
11 |
|
---|
12 | TDisassembler = class
|
---|
13 | private
|
---|
14 | InstText: string;
|
---|
15 | InstBytes: string;
|
---|
16 | InstDataWidth: TBigIntSize;
|
---|
17 | InstAddressWidth: TBigIntSize;
|
---|
18 | procedure DisassembleData;
|
---|
19 | procedure DisassembleAddress;
|
---|
20 | procedure DisassembleRegister;
|
---|
21 | procedure DisassembleRegisterIndirect;
|
---|
22 | procedure DisassembleRegisterIndirectIndex;
|
---|
23 | procedure DisassembleDataWidth;
|
---|
24 | procedure DisassembleAddressWidth;
|
---|
25 | public
|
---|
26 | InstructionSet: TInstructionInfos;
|
---|
27 | Memory: TMemory;
|
---|
28 | DataWidth: TBigIntSize;
|
---|
29 | AddressWidth: TBigIntSize;
|
---|
30 | procedure Disassemble(Lines: TStrings);
|
---|
31 | procedure SaveToFile(FileName: string);
|
---|
32 | constructor Create;
|
---|
33 | destructor Destroy; override;
|
---|
34 | end;
|
---|
35 |
|
---|
36 |
|
---|
37 | implementation
|
---|
38 |
|
---|
39 | procedure TDisassembler.DisassembleData;
|
---|
40 | var
|
---|
41 | Text: string;
|
---|
42 | begin
|
---|
43 | Text := IntToHex(Memory.ReadPos(InstDataWidth));
|
---|
44 | if not (Copy(Text, 1, 1)[1] in ['0'..'9']) then Text := '0' + Text;
|
---|
45 | InstText := InstText + Text + 'h';
|
---|
46 | end;
|
---|
47 |
|
---|
48 | procedure TDisassembler.DisassembleAddress;
|
---|
49 | var
|
---|
50 | Text: string;
|
---|
51 | begin
|
---|
52 | Text := IntToHex(Memory.ReadPos(InstAddressWidth));
|
---|
53 | if not (Copy(Text, 1, 1)[1] in ['0'..'9']) then Text := '0' + Text;
|
---|
54 | InstText := InstText + Text + 'h';
|
---|
55 | end;
|
---|
56 |
|
---|
57 | procedure TDisassembler.DisassembleRegister;
|
---|
58 | begin
|
---|
59 | InstText := InstText + 'R' + IntToStr(Byte(Memory.ReadPos(1)));
|
---|
60 | end;
|
---|
61 |
|
---|
62 | procedure TDisassembler.DisassembleRegisterIndirect;
|
---|
63 | begin
|
---|
64 | InstText := InstText + '(R' + IntToStr(Byte(Memory.ReadPos(1))) + ')';
|
---|
65 | end;
|
---|
66 |
|
---|
67 | procedure TDisassembler.DisassembleRegisterIndirectIndex;
|
---|
68 | begin
|
---|
69 | InstText := InstText + '(R' + IntToStr(Byte(Memory.ReadPos(1))) +
|
---|
70 | ' + ';
|
---|
71 | DisassembleAddress;
|
---|
72 | InstText := InstText + ')';
|
---|
73 | end;
|
---|
74 |
|
---|
75 | procedure TDisassembler.DisassembleDataWidth;
|
---|
76 | begin
|
---|
77 | InstDataWidth := Memory.ReadPos(1);
|
---|
78 | InstText := InstText + 'D' + IntToStr(InstDataWidth);
|
---|
79 | end;
|
---|
80 |
|
---|
81 | procedure TDisassembler.DisassembleAddressWidth;
|
---|
82 | begin
|
---|
83 | InstAddressWidth := Memory.ReadPos(1);
|
---|
84 | InstText := InstText + 'A' + IntToStr(InstAddressWidth);
|
---|
85 | end;
|
---|
86 |
|
---|
87 | procedure TDisassembler.Disassemble(Lines: TStrings);
|
---|
88 | var
|
---|
89 | J: Integer;
|
---|
90 | Value: TBigInt;
|
---|
91 | Instruction: TInstruction;
|
---|
92 | InstructionInfo: TInstructionInfo;
|
---|
93 | Line: string;
|
---|
94 | MemoryPos: Integer;
|
---|
95 | InstructionByteSize: Integer;
|
---|
96 | I: Integer;
|
---|
97 | begin
|
---|
98 | Memory.Position := 0;
|
---|
99 | while Memory.Position < Memory.Size do begin
|
---|
100 | Line := IntToHex(Memory.Position, 8) + ' ';
|
---|
101 | MemoryPos := Memory.Position;
|
---|
102 | Value := Memory.ReadPos(1);
|
---|
103 | InstBytes := IntToHex(Value, 2) + ' ';
|
---|
104 | InstText := '';
|
---|
105 | InstAddressWidth := AddressWidth;
|
---|
106 | InstDataWidth := DataWidth;
|
---|
107 | if (Value >= 0) and (Value <= Integer(High(TInstruction))) then begin
|
---|
108 | Instruction := TInstruction(Int64(Value));
|
---|
109 | InstructionInfo := InstructionSet.SearchInstruction(Instruction);
|
---|
110 | if Assigned(InstructionInfo) then begin
|
---|
111 | InstText := InstructionInfo.Name;
|
---|
112 | for J := 0 to Length(InstructionInfo.Params) - 1 do begin
|
---|
113 | if J > 0 then
|
---|
114 | InstText := InstText + ', ' else
|
---|
115 | InstText := InstText + ' ';
|
---|
116 | case InstructionInfo.Params[J] of
|
---|
117 | ptData: DisassembleData;
|
---|
118 | ptAddress: DisassembleAddress;
|
---|
119 | ptReg: DisassembleRegister;
|
---|
120 | ptRegIndirect: DisassembleRegisterIndirect;
|
---|
121 | ptRegIndirectIndex: DisassembleRegisterIndirectIndex;
|
---|
122 | ptDataWidth: DisassembleDataWidth;
|
---|
123 | ptAddressWidth: DisassembleAddressWidth;
|
---|
124 | end;
|
---|
125 | end;
|
---|
126 | end;
|
---|
127 | end;
|
---|
128 |
|
---|
129 | // Instruction bytes to hex string
|
---|
130 | InstructionByteSize := Memory.Position - MemoryPos;
|
---|
131 | Memory.Position := MemoryPos;
|
---|
132 | InstBytes := '';
|
---|
133 | for I := 0 to InstructionByteSize - 1 do
|
---|
134 | InstBytes := InstBytes + IntToHex(Memory.ReadPos(1), 2) + ' ';
|
---|
135 | InstBytes := InstBytes + DupeString(' ', 13 - Length(InstBytes));
|
---|
136 |
|
---|
137 | Line := Line + InstBytes + InstText;
|
---|
138 | Lines.Add(Line);
|
---|
139 | end;
|
---|
140 | end;
|
---|
141 |
|
---|
142 | procedure TDisassembler.SaveToFile(FileName: string);
|
---|
143 | var
|
---|
144 | Lines: TStringList;
|
---|
145 | begin
|
---|
146 | Lines := TStringList.Create;
|
---|
147 | Disassemble(Lines);
|
---|
148 | Lines.SaveToFile(FileName);
|
---|
149 | Lines.Free;
|
---|
150 | end;
|
---|
151 |
|
---|
152 | constructor TDisassembler.Create;
|
---|
153 | begin
|
---|
154 | InstructionSet := TInstructionInfos.Create;
|
---|
155 | InstructionSet.Init;
|
---|
156 | DataWidth := 1;
|
---|
157 | AddressWidth := 1;
|
---|
158 | end;
|
---|
159 |
|
---|
160 | destructor TDisassembler.Destroy;
|
---|
161 | begin
|
---|
162 | FreeAndNil(InstructionSet);
|
---|
163 | inherited;
|
---|
164 | end;
|
---|
165 |
|
---|
166 | end.
|
---|
167 |
|
---|