Changeset 59 for branches/ByteArray/Disassembler.pas
- Timestamp:
- Nov 25, 2023, 11:47:52 PM (12 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/ByteArray/Disassembler.pas
r55 r59 4 4 5 5 uses 6 Classes, SysUtils, Cpu, Instructions, StrUtils, Memory ;6 Classes, SysUtils, Cpu, Instructions, StrUtils, Memory, BigInt; 7 7 8 8 type … … 12 12 TDisassembler = class 13 13 private 14 function RegToStr(RegIndex: Byte): string; 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; 15 25 public 16 InstructionSet: TInstruction Set;26 InstructionSet: TInstructionInfos; 17 27 Memory: TMemory; 28 DataWidth: TBigIntSize; 29 AddressWidth: TBigIntSize; 18 30 procedure Disassemble(Lines: TStrings); 19 31 procedure SaveToFile(FileName: string); … … 25 37 implementation 26 38 27 uses 28 BigInt; 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; 29 47 30 function TDisassembler.RegToStr(RegIndex: Byte): string; 48 procedure TDisassembler.DisassembleAddress; 49 var 50 Text: string; 31 51 begin 32 Result := Chr(Ord('A') + RegIndex); 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); 33 85 end; 34 86 35 87 procedure TDisassembler.Disassemble(Lines: TStrings); 36 88 var 37 I: Integer;38 89 J: Integer; 39 90 Value: TBigInt; … … 41 92 InstructionInfo: TInstructionInfo; 42 93 Line: string; 43 InstText: string; 44 InstBytes: string; 94 MemoryPos: Integer; 95 InstructionByteSize: Integer; 96 I: Integer; 45 97 begin 46 98 Memory.Position := 0; 47 99 while Memory.Position < Memory.Size do begin 48 100 Line := IntToHex(Memory.Position, 8) + ' '; 101 MemoryPos := Memory.Position; 49 102 Value := Memory.ReadPos(1); 50 103 InstBytes := IntToHex(Value, 2) + ' '; 51 104 InstText := ''; 105 InstAddressWidth := AddressWidth; 106 InstDataWidth := DataWidth; 52 107 if (Value >= 0) and (Value <= Integer(High(TInstruction))) then begin 53 108 Instruction := TInstruction(Int64(Value)); … … 56 111 InstText := InstructionInfo.Name; 57 112 for J := 0 to Length(InstructionInfo.Params) - 1 do begin 58 Value := Memory.ReadPos(1);59 InstBytes := InstBytes + IntToHex(Value, 2) + ' ';60 113 if J > 0 then 61 114 InstText := InstText + ', ' else 62 115 InstText := InstText + ' '; 63 if InstructionInfo.Params[J] = ptNumber then begin 64 InstText := InstText + IntToHex(Value) + 'h'; 65 end else 66 if InstructionInfo.Params[J] = ptReg then begin 67 InstText := InstText + RegToStr(Value); 68 end else 69 if InstructionInfo.Params[J] = ptRegIndirect then begin 70 InstText := InstText + '(' + RegToStr(Value) + ')'; 71 end else 72 if InstructionInfo.Params[J] = ptRegIndirectIndex then begin 73 InstText := InstText + '(' + RegToStr(Value); 74 Value := Memory.ReadPos(1); 75 InstBytes := InstBytes + IntToHex(Value, 2) + ' '; 76 InstText := InstText + ' + ' + IntToStr(Value) + ')'; 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; 77 124 end; 78 125 end; 79 InstBytes := InstBytes + DupeString(' ', 13 - Length(InstBytes));80 126 end; 81 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 82 137 Line := Line + InstBytes + InstText; 83 138 Lines.Add(Line); … … 97 152 constructor TDisassembler.Create; 98 153 begin 99 InstructionSet := TInstructionSet.Create; 154 InstructionSet := TInstructionInfos.Create; 155 InstructionSet.Init; 156 DataWidth := 1; 157 AddressWidth := 1; 100 158 end; 101 159
Note:
See TracChangeset
for help on using the changeset viewer.