Ignore:
Timestamp:
Apr 13, 2019, 12:03:31 AM (6 years ago)
Author:
chronos
Message:
  • Added: Disassemble instruction.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/virtualcpu4/UInstructionWriter.pas

    r180 r181  
    66
    77uses
    8   Classes, SysUtils, UCpu, UMemory, fgl;
     8  Classes, SysUtils, UCpu;
    99
    1010type
     
    3131    procedure Write64(Value: QWord);
    3232    procedure WriteAddress(Value: TAddress);
    33     procedure WriteAddressSigned(Value: TAddressSigned);
     33    procedure WriteAddressSigned(Value: TAddressSigned); inline;
    3434    procedure WriteData(Value: QWord);
    3535    procedure WriteString(Text: string);
     
    6464  end;
    6565
    66   { TAssembler }
    67 
    68   TAssembler = class
    69     Source: TStringList;
    70     Dest: array of Byte;
    71     procedure Compile;
    72   end;
    73 
    74   TDisassemblerLine = class
    75     Address: Integer;
    76     Opcode: string;
    77     Instruction: string;
    78   end;
    79 
    80   { TDisassembler }
    81 
    82   TDisassembler = class
    83     Memory: TMemoryPos;
    84     Output: TFPGObjectList<TDisassemblerLine>;
    85     procedure Process;
    86     constructor Create;
    87     destructor Destroy; override;
    88   end;
    89 
    90 const
    91   OpcodeName: array[TOpcode] of string = ('NOP', 'HALT', 'LD', 'LDI', 'JP', 'JPZ',
    92     'JPNZ', 'JR', 'JRZ', 'JRNZ', 'NEG', 'CLR', 'LDM', 'STM', 'EX', 'PUSH', 'POP',
    93     'CALL', 'RET', 'ADD', 'ADDI', 'SUB', 'SUBI', 'INC', 'DEC', 'IN', 'OUT',
    94     'SHL', 'SHR', 'DP8', 'DP16', 'DP32', 'DP64', 'DS', 'AS', 'TEST', 'AND', 'OR',
    95     'XOR', 'LDDR', 'LDIR', 'MUL', 'DIV', 'MOD', 'AP8', 'AP16', 'AP32', 'AP64');
    9666
    9767implementation
    98 
    99 { TDisassembler }
    100 
    101 procedure TDisassembler.Process;
    102 var
    103   Opcode: Byte;
    104   Line: TDisassemblerLine;
    105 begin
    106   Output.Clear;
    107   Memory.Position := Memory.Data;
    108 
    109   while Memory.Position < Memory.Data + Memory.Size do begin
    110     Opcode := Memory.ReadByte;
    111     if Opcode < Integer(High(TOpcode)) then begin
    112       Line := TDisassemblerLine.Create;
    113       Line.Address := Memory.Position - Memory.Data;
    114       Line.Opcode := IntToHex(Opcode, 2);
    115       Line.Instruction := OpcodeName[TOpcode(Opcode)];
    116       Output.Add(Line);
    117     end;
    118   end;
    119 end;
    120 
    121 constructor TDisassembler.Create;
    122 begin
    123   Output := TFPGObjectList<TDisassemblerLine>.Create;
    124   Memory := TMemoryPos.Create;
    125 end;
    126 
    127 destructor TDisassembler.Destroy;
    128 begin
    129   Memory.Free;
    130   Output.Free;
    131   inherited Destroy;
    132 end;
    133 
    134 { TAssembler }
    135 
    136 procedure TAssembler.Compile;
    137 begin
    138 
    139 end;
    14068
    14169{ TInstructionWriter }
Note: See TracChangeset for help on using the changeset viewer.