| 1 | unit UDisassembler;
|
|---|
| 2 |
|
|---|
| 3 | {$mode delphi}
|
|---|
| 4 |
|
|---|
| 5 | interface
|
|---|
| 6 |
|
|---|
| 7 | uses
|
|---|
| 8 | Classes, SysUtils, fgl, UCpu, UInstructionReader, Math, UOpcode, UVarInt;
|
|---|
| 9 |
|
|---|
| 10 | type
|
|---|
| 11 | TDisassemblerLine = class
|
|---|
| 12 | Address: Integer;
|
|---|
| 13 | Opcode: string;
|
|---|
| 14 | Instruction: string;
|
|---|
| 15 | end;
|
|---|
| 16 |
|
|---|
| 17 | { TDisassembler }
|
|---|
| 18 |
|
|---|
| 19 | TDisassembler = class(TInstructionReader)
|
|---|
| 20 | private
|
|---|
| 21 | OpcodeDefs: TOpcodeDefs;
|
|---|
| 22 | procedure ProcessParam(Param: TOpcodeParam; Line: TDisassemblerLine; Separator: string = '');
|
|---|
| 23 | public
|
|---|
| 24 | Output: TFPGObjectList<TDisassemblerLine>;
|
|---|
| 25 | procedure Process;
|
|---|
| 26 | constructor Create;
|
|---|
| 27 | destructor Destroy; override;
|
|---|
| 28 | end;
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 | implementation
|
|---|
| 32 |
|
|---|
| 33 | { TDisassembler }
|
|---|
| 34 |
|
|---|
| 35 | procedure TDisassembler.ProcessParam(Param: TOpcodeParam; Line: TDisassemblerLine; Separator: string = '');
|
|---|
| 36 | var
|
|---|
| 37 | Reg: TVarUInt;
|
|---|
| 38 | Data: TVarUInt;
|
|---|
| 39 | Address: TVarUInt;
|
|---|
| 40 | AddressRel: TVarInt;
|
|---|
| 41 | begin
|
|---|
| 42 | case Param of
|
|---|
| 43 | prReg: begin
|
|---|
| 44 | Reg := Read;
|
|---|
| 45 | Line.Opcode := Line.Opcode + ' ' + IntToHex(Integer(Reg), 2);
|
|---|
| 46 | Line.Instruction := Line.Instruction + Separator + ' R' + IntToStr(Integer(Reg));
|
|---|
| 47 | end;
|
|---|
| 48 | prData: begin
|
|---|
| 49 | Data := Read;
|
|---|
| 50 | Line.Opcode := Line.Opcode + ' ' + IntToHex(Integer(Data), 8);
|
|---|
| 51 | Line.Instruction := Line.Instruction + Separator + ' ' + IntToHexEx(QWord(Data), -1, '$');
|
|---|
| 52 | end;
|
|---|
| 53 | prAddr: begin
|
|---|
| 54 | Address := Read;
|
|---|
| 55 | Line.Opcode := Line.Opcode + ' ' + IntToHex(Integer(Address), 8);
|
|---|
| 56 | Line.Instruction := Line.Instruction + Separator + ' ' + IntToHexEx(QWord(Address), -1, '$');
|
|---|
| 57 | end;
|
|---|
| 58 | prAddrRel: begin
|
|---|
| 59 | AddressRel := ReadSigned;
|
|---|
| 60 | Line.Opcode := Line.Opcode + ' ' + IntToHexEx(Int64(AddressRel), 8);
|
|---|
| 61 | Line.Instruction := Line.Instruction + Separator + ' ' + IntToHexEx(Int64(AddressRel), -1, '$');
|
|---|
| 62 | end;
|
|---|
| 63 | end;
|
|---|
| 64 | end;
|
|---|
| 65 |
|
|---|
| 66 | procedure TDisassembler.Process;
|
|---|
| 67 | var
|
|---|
| 68 | Opcode: TVarUInt;
|
|---|
| 69 | Line: TDisassemblerLine;
|
|---|
| 70 | MemorySize: Integer;
|
|---|
| 71 | OpcodeDef: TOpcodeDef;
|
|---|
| 72 | begin
|
|---|
| 73 | Init;
|
|---|
| 74 | Output.Clear;
|
|---|
| 75 |
|
|---|
| 76 | MemorySize := MemSize(Cpu.Memory);
|
|---|
| 77 | while IP < MemorySize do begin
|
|---|
| 78 | Opcode := Read;
|
|---|
| 79 | if Opcode <= Integer(High(TOpcode)) then begin
|
|---|
| 80 | OpcodeDef := OpcodeDefs.SearchByOpcode(TOpcode(Integer(Opcode)));
|
|---|
| 81 | Line := TDisassemblerLine.Create;
|
|---|
| 82 | Line.Address := IP - 1;
|
|---|
| 83 | Line.Opcode := IntToHex(Integer(Opcode), 2);
|
|---|
| 84 | Line.Instruction := OpcodeDef.Name;
|
|---|
| 85 | ProcessParam(OpcodeDef.Param1, Line);
|
|---|
| 86 | ProcessParam(OpcodeDef.Param2, Line, ',');
|
|---|
| 87 | ProcessParam(OpcodeDef.Param3, Line, ',');
|
|---|
| 88 | ProcessParam(OpcodeDef.Param4, Line, ',');
|
|---|
| 89 | Output.Add(Line);
|
|---|
| 90 | end else begin
|
|---|
| 91 | {Line := TDisassemblerLine.Create;
|
|---|
| 92 | Line.Address := IP;
|
|---|
| 93 | Line.Opcode := IntToHex(Opcode, 2);
|
|---|
| 94 | Line.Instruction := '';
|
|---|
| 95 | Output.Add(Line);
|
|---|
| 96 | }
|
|---|
| 97 | end;
|
|---|
| 98 | end;
|
|---|
| 99 | end;
|
|---|
| 100 |
|
|---|
| 101 | constructor TDisassembler.Create;
|
|---|
| 102 | begin
|
|---|
| 103 | OpcodeDefs := TOpcodeDefs.Create;
|
|---|
| 104 | Output := TFPGObjectList<TDisassemblerLine>.Create;
|
|---|
| 105 | end;
|
|---|
| 106 |
|
|---|
| 107 | destructor TDisassembler.Destroy;
|
|---|
| 108 | begin
|
|---|
| 109 | FreeAndNil(Output);
|
|---|
| 110 | FreeAndNil(OpcodeDefs);
|
|---|
| 111 | inherited Destroy;
|
|---|
| 112 | end;
|
|---|
| 113 |
|
|---|
| 114 |
|
|---|
| 115 | end.
|
|---|
| 116 |
|
|---|