| 1 | unit Disassembler;
|
|---|
| 2 |
|
|---|
| 3 | interface
|
|---|
| 4 |
|
|---|
| 5 | uses
|
|---|
| 6 | Classes, SysUtils, Cpu, Instructions, StrUtils, IntMemory, Int;
|
|---|
| 7 |
|
|---|
| 8 | type
|
|---|
| 9 |
|
|---|
| 10 | { TDisassembler }
|
|---|
| 11 |
|
|---|
| 12 | TDisassembler = class
|
|---|
| 13 | InstructionSet: TInstructionSet;
|
|---|
| 14 | Memory: TIntMemory;
|
|---|
| 15 | procedure Disassemble(Lines: TStrings);
|
|---|
| 16 | procedure SaveToFile(FileName: string);
|
|---|
| 17 | constructor Create;
|
|---|
| 18 | destructor Destroy; override;
|
|---|
| 19 | end;
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 | implementation
|
|---|
| 23 |
|
|---|
| 24 | procedure TDisassembler.Disassemble(Lines: TStrings);
|
|---|
| 25 | var
|
|---|
| 26 | I: Integer;
|
|---|
| 27 | Value: TInt;
|
|---|
| 28 | Instruction: TInstruction;
|
|---|
| 29 | InstructionInfo: TInstructionInfo;
|
|---|
| 30 | Line: string;
|
|---|
| 31 | InstText: string;
|
|---|
| 32 | InstBytes: string;
|
|---|
| 33 | begin
|
|---|
| 34 | Memory.Position := 0;
|
|---|
| 35 | while Memory.Position < Memory.Size do begin
|
|---|
| 36 | Line := IntToHex(Memory.Position, 8) + ' ';
|
|---|
| 37 | Value := Memory.ReadPos;
|
|---|
| 38 | InstBytes := IntToHex(Value, 2) + ' ';
|
|---|
| 39 | InstText := '';
|
|---|
| 40 | if (Value >= 0) and (Value <= Integer(High(TInstruction))) then begin
|
|---|
| 41 | Instruction := TInstruction(Value);
|
|---|
| 42 | InstructionInfo := InstructionSet.SearchInstruction(Instruction);
|
|---|
| 43 | if Assigned(InstructionInfo) then begin
|
|---|
| 44 | InstText := InstructionInfo.Name;
|
|---|
| 45 | for I := 0 to Length(InstructionInfo.Params) - 1 do begin
|
|---|
| 46 | Value := Memory.ReadPos;
|
|---|
| 47 | InstBytes := InstBytes + IntToHex(Value, 2) + ' ';
|
|---|
| 48 | if I > 0 then
|
|---|
| 49 | InstText := InstText + ', ' else
|
|---|
| 50 | InstText := InstText + ' ';
|
|---|
| 51 | if InstructionInfo.Params[I] = ptNumber then begin
|
|---|
| 52 | InstText := InstText + IntToHex(Value, 8);
|
|---|
| 53 | end else
|
|---|
| 54 | if InstructionInfo.Params[I] = ptIndirect then begin
|
|---|
| 55 | InstText := InstText + '(' + IntToStr(Value) + ')';
|
|---|
| 56 | end else
|
|---|
| 57 | if InstructionInfo.Params[I] = ptIndirect2 then begin
|
|---|
| 58 | InstText := InstText + '((' + IntToStr(Value) + '))';
|
|---|
| 59 | end else raise Exception.Create('Unsupported instruction parameter type');
|
|---|
| 60 | end;
|
|---|
| 61 | InstBytes := InstBytes + DupeString(' ', 13 - Length(InstBytes));
|
|---|
| 62 | end;
|
|---|
| 63 | end;
|
|---|
| 64 | Line := Line + InstBytes + InstText;
|
|---|
| 65 | Lines.Add(Line);
|
|---|
| 66 | end;
|
|---|
| 67 | end;
|
|---|
| 68 |
|
|---|
| 69 | procedure TDisassembler.SaveToFile(FileName: string);
|
|---|
| 70 | var
|
|---|
| 71 | Lines: TStringList;
|
|---|
| 72 | begin
|
|---|
| 73 | Lines := TStringList.Create;
|
|---|
| 74 | Disassemble(Lines);
|
|---|
| 75 | Lines.SaveToFile(FileName);
|
|---|
| 76 | Lines.Free;
|
|---|
| 77 | end;
|
|---|
| 78 |
|
|---|
| 79 | constructor TDisassembler.Create;
|
|---|
| 80 | begin
|
|---|
| 81 | InstructionSet := TInstructionSet.Create;
|
|---|
| 82 | end;
|
|---|
| 83 |
|
|---|
| 84 | destructor TDisassembler.Destroy;
|
|---|
| 85 | begin
|
|---|
| 86 | FreeAndNil(InstructionSet);
|
|---|
| 87 | inherited;
|
|---|
| 88 | end;
|
|---|
| 89 |
|
|---|
| 90 | end.
|
|---|
| 91 |
|
|---|