source: branches/CpuSingleSize/Disassembler.pas

Last change on this file was 238, checked in by chronos, 16 months ago
  • Modified: Removed U prefix from unit names.
  • Fixed: Memory leaks.
File size: 2.9 KB
Line 
1unit Disassembler;
2
3interface
4
5uses
6 Classes, SysUtils, Cpu, Instructions, StrUtils, Memory;
7
8type
9
10 { TDisassembler }
11
12 TDisassembler = class
13 InstructionSet: TInstructionSet;
14 Memory: TMemory;
15 procedure Disassemble(Lines: TStrings);
16 procedure SaveToFile(FileName: string);
17 constructor Create;
18 destructor Destroy; override;
19 end;
20
21
22implementation
23
24procedure TDisassembler.Disassemble(Lines: TStrings);
25var
26 I: Integer;
27 J: Integer;
28 Value: TInteger;
29 Instruction: TInstruction;
30 InstructionInfo: TInstructionInfo;
31 Line: string;
32 InstText: string;
33 InstBytes: string;
34begin
35 Memory.Position := 0;
36 while Memory.Position < Memory.Size do begin
37 Line := IntToHex(Memory.Position, 8) + ' ';
38 Value := Memory.Read;
39 InstBytes := IntToHex(Value, 2) + ' ';
40 InstText := '';
41 if (Value >= 0) and (Value <= Integer(High(TInstruction))) then begin
42 Instruction := TInstruction(Value);
43 InstructionInfo := InstructionSet.SearchInstruction(Instruction);
44 if Assigned(InstructionInfo) then begin
45 InstText := InstructionInfo.Name;
46 for J := 0 to Length(InstructionInfo.Params) - 1 do begin
47 Value := Memory.Read;
48 InstBytes := InstBytes + IntToHex(Value, 2) + ' ';
49 if J > 0 then
50 InstText := InstText + ', ' else
51 InstText := InstText + ' ';
52 if InstructionInfo.Params[J] = ptNumber then begin
53 InstText := InstText + IntToHex(Value, 8);
54 end else
55 if InstructionInfo.Params[J] = ptReg then begin
56 InstText := InstText + 'R' + IntToStr(Value);
57 end else
58 if InstructionInfo.Params[J] = ptRegIndirect then begin
59 InstText := InstText + '(R' + IntToStr(Value) + ')';
60 end else
61 if InstructionInfo.Params[J] = ptRegIndirectIndex then begin
62 InstText := InstText + '(R' + IntToStr(Value);
63 Value := Memory.Read;
64 InstBytes := InstBytes + IntToHex(Value, 2) + ' ';
65 InstText := InstText + ' + ' + IntToStr(Value) + ')';
66 end else
67 if InstructionInfo.Params[J] = ptRegIndirectGroup then begin
68 InstText := InstText + '(R' + IntToStr(Value);
69 Value := Memory.Read;
70 InstBytes := InstBytes + IntToHex(Value, 2) + ' ';
71 InstText := InstText + ': R' + IntToStr(Value) + ')';
72 end;
73 end;
74 InstBytes := InstBytes + DupeString(' ', 13 - Length(InstBytes));
75 end;
76 end;
77 Line := Line + InstBytes + InstText;
78 Lines.Add(Line);
79 end;
80end;
81
82procedure TDisassembler.SaveToFile(FileName: string);
83var
84 Lines: TStringList;
85begin
86 Lines := TStringList.Create;
87 Disassemble(Lines);
88 Lines.SaveToFile(FileName);
89 Lines.Free;
90end;
91
92constructor TDisassembler.Create;
93begin
94 InstructionSet := TInstructionSet.Create;
95end;
96
97destructor TDisassembler.Destroy;
98begin
99 FreeAndNil(InstructionSet);
100 inherited;
101end;
102
103
104end.
105
Note: See TracBrowser for help on using the repository browser.