Changeset 154


Ignore:
Timestamp:
Apr 23, 2018, 2:13:10 PM (7 years ago)
Author:
chronos
Message:
  • Added: More opcodes with handlers.
Location:
branches/virtualcpu2
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/virtualcpu2/UFormMain.lfm

    r153 r154  
    77  ClientHeight = 732
    88  ClientWidth = 1504
     9  DesignTimePPI = 120
    910  OnCreate = FormCreate
    1011  OnDestroy = FormDestroy
    1112  OnShow = FormShow
    12   LCLVersion = '1.8.0.4'
     13  LCLVersion = '1.8.2.0'
    1314  object ButtonRun: TButton
    1415    Left = 32
  • branches/virtualcpu2/UFormMain.pas

    r153 r154  
    9595  Counter = 3;
    9696  Temp = 4;
     97  Jump = 5;
    9798var
    9899  Writer: TInstructionWriter;
     
    103104    Machine := Self.Machine;
    104105    AddGetDataSize(Temp);
    105     AddCopyConst(Temp, 1);
     106    AddCopyConst8(Temp, 1);
    106107
    107108    AddCopyConst(Counter, 8);
     
    116117    AddDec(Counter);
    117118    AddTestNotZero(Counter);
    118     AddJumpRelCond8(LoopAddr - Addr);
     119    AddCopyConst8(Jump, LoopAddr - (Addr + 3));
     120    AddJumpRelCond8(Jump);
    119121    AddHalt;
    120122  end;
  • branches/virtualcpu2/UMachine.pas

    r153 r154  
    66
    77uses
    8   Classes, SysUtils, Math;
     8  Classes, SysUtils;
    99
    1010type
    11  Int8    = ShortInt;
    12  Int16  = SmallInt;
    13  Int32  = Longint;
    14  PInt8 = ^Int8;
    15  PInt16 = ^Int16;
    16  PInt32 = ^Int32;
    17  UInt8  = Byte;
    18  UInt16 = Word;
    19  UInt32 = Cardinal;
    20  PUInt8  = ^UInt8;
    21  PUInt16 = ^UInt16;
    22  PUInt32 = ^UInt16;
     11  Int8 = ShortInt;
     12  Int16 = SmallInt;
     13  Int32 = Longint;
     14  PInt8 = ^Int8;
     15  PInt16 = ^Int16;
     16  PInt32 = ^Int32;
     17  UInt8 = Byte;
     18  UInt16 = Word;
     19  UInt32 = Cardinal;
     20  PUInt8 = ^UInt8;
     21  PUInt16 = ^UInt16;
     22  PUInt32 = ^UInt16;
    2323
    2424
     
    3838
    3939  TOpcode = (
     40    // Special
     41    opNop,
     42    opHalt,
     43    opGetSize,
     44    opSetSize, opSetSize8, opSetSize16, opSetSize32, opSetSize64,
    4045    // Data move
    4146    opCopy,
     
    8287    // Block operations
    8388    opLdir,
    84     opLddr,
    85     // Special
    86     opNop,
    87     opHalt,
    88     opGetSize,
    89     opSetSize, opSetSize8, opSetSize16, opSetSize32, opSetSize64
     89    opLddr
    9090  );
    9191
     
    121121    procedure OpcodeXor;
    122122    procedure OpcodePop;
     123    procedure OpcodePop8;
     124    procedure OpcodePop16;
     125    procedure OpcodePop32;
     126    procedure OpcodePop64;
    123127    procedure OpcodePush;
    124128    procedure OpcodePush8;
     
    133137    procedure OpcodeInc64;
    134138    procedure OpcodeDec;
     139    procedure OpcodeDec8;
     140    procedure OpcodeDec16;
     141    procedure OpcodeDec32;
     142    procedure OpcodeDec64;
    135143    procedure OpcodeTestNotZero;
    136144    procedure OpcodeTestZero;
    137145    procedure OpcodeJumpRel;
     146    procedure OpcodeJumpRel8;
     147    procedure OpcodeJumpRel16;
     148    procedure OpcodeJumpRel32;
     149    procedure OpcodeJumpRel64;
    138150    procedure OpcodeJumpAbs;
     151    procedure OpcodeJumpAbs8;
     152    procedure OpcodeJumpAbs16;
     153    procedure OpcodeJumpAbs32;
     154    procedure OpcodeJumpAbs64;
    139155    procedure OpcodeJumpRelCond;
    140156    procedure OpcodeJumpRelCond8;
     157    procedure OpcodeJumpRelCond16;
     158    procedure OpcodeJumpRelCond32;
     159    procedure OpcodeJumpRelCond64;
    141160    procedure OpcodeJumpAbsCond;
     161    procedure OpcodeJumpAbsCond8;
     162    procedure OpcodeJumpAbsCond16;
     163    procedure OpcodeJumpAbsCond32;
     164    procedure OpcodeJumpAbsCond64;
    142165    procedure OpcodeCallRel;
    143166    procedure OpcodeCallAbs;
     
    151174    procedure OpcodeSetSize64;
    152175    function ReadNext: TDataInt;
     176    function ReadNext8: UInt8;
     177    function ReadNext16: UInt16;
     178    function ReadNext32: UInt32;
     179    function ReadNext64: UInt64;
    153180    function ReadNextS: TDataInt;
    154     function ReadNext8: Byte;
    155     function ReadNextS8: ShortInt;
    156     function ReadNext16: Word;
    157     function ReadNext32: Cardinal;
    158     function ReadNext64: QWord;
     181    function ReadNextS8: Int8;
     182    function ReadNextS16: Int16;
     183    function ReadNextS32: Int32;
     184    function ReadNextS64: Int64;
    159185    procedure SetOpcode(Opcode: TOpcode; Handler: TOpcodeHandler; DataSize: TDataSize = dsNative);
    160186    procedure InitOpcode;
     
    192218    procedure AddHalt;
    193219    procedure AddTestNotZero(Reg: TRegIndex);
    194     procedure AddJumpRelCond(RelAddr: Integer);
    195     procedure AddJumpRelCond8(RelAddr: ShortInt);
     220    procedure AddJumpRelCond(Reg: Integer);
     221    procedure AddJumpRelCond8(Reg: TRegIndex);
    196222    constructor Create;
    197223  end;
     
    244270  SetOpcode(opInc, OpcodeInc64, ds64);
    245271  SetOpcode(opDec, OpcodeDec);
     272  SetOpcode(opDec, OpcodeDec8, ds8);
     273  SetOpcode(opDec, OpcodeDec16, ds16);
     274  SetOpcode(opDec, OpcodeDec32, ds32);
     275  SetOpcode(opDec, OpcodeDec64, ds64);
    246276  SetOpcode(opTestZero, OpcodeTestZero);
    247277  SetOpcode(opTestNotZero, OpcodeTestNotZero);
    248278  SetOpcode(opJumpRel, OpcodeJumpRel);
     279  SetOpcode(opJumpRel, OpcodeJumpRel8, ds8);
     280  SetOpcode(opJumpRel, OpcodeJumpRel16, ds16);
     281  SetOpcode(opJumpRel, OpcodeJumpRel32, ds32);
     282  SetOpcode(opJumpRel, OpcodeJumpRel64, ds64);
    249283  SetOpcode(opJumpAbs, OpcodeJumpAbs);
     284  SetOpcode(opJumpAbs, OpcodeJumpAbs8, ds8);
     285  SetOpcode(opJumpAbs, OpcodeJumpAbs16, ds16);
     286  SetOpcode(opJumpAbs, OpcodeJumpAbs32, ds32);
     287  SetOpcode(opJumpAbs, OpcodeJumpAbs64, ds64);
    250288  SetOpcode(opJumpRelCond, OpcodeJumpRelCond);
    251289  SetOpcode(opJumpRelCond, OpcodeJumpRelCond8, ds8);
     290  SetOpcode(opJumpRelCond, OpcodeJumpRelCond16, ds16);
     291  SetOpcode(opJumpRelCond, OpcodeJumpRelCond32, ds32);
     292  SetOpcode(opJumpRelCond, OpcodeJumpRelCond64, ds64);
    252293  SetOpcode(opJumpAbsCond, OpcodeJumpAbsCond);
    253   SetOpcode(opCallRel, OpcodeCAllRel);
     294  SetOpcode(opJumpAbsCond, OpcodeJumpAbsCond8, ds8);
     295  SetOpcode(opJumpAbsCond, OpcodeJumpAbsCond16, ds16);
     296  SetOpcode(opJumpAbsCond, OpcodeJumpAbsCond32, ds32);
     297  SetOpcode(opJumpAbsCond, OpcodeJumpAbsCond64, ds64);
     298  SetOpcode(opCallRel, OpcodeCallRel);
    254299  SetOpcode(opCallAbs, OpcodeCallAbs);
    255300  SetOpcode(opCallRelCond, OpcodeCallRelCond);
     
    267312  SetOpcode(opPush, OpcodePush64, ds64);
    268313  SetOpcode(opPop, OpcodePop);
     314  SetOpcode(opPop, OpcodePop8, ds8);
     315  SetOpcode(opPop, OpcodePop16, ds16);
     316  SetOpcode(opPop, OpcodePop32, ds32);
     317  SetOpcode(opPop, OpcodePop64, ds64);
     318  SetOpcode(opSetSize, OpcodeSetSize);
    269319  SetOpcode(opSetSize8, OpcodeSetSize8);
     320  SetOpcode(opSetSize16, OpcodeSetSize16);
     321  SetOpcode(opSetSize32, OpcodeSetSize32);
     322  SetOpcode(opSetSize64, OpcodeSetSize64);
    270323end;
    271324
     
    495548procedure TMachine.OpcodePop;
    496549var
    497   Dest: TRegIndex;
    498 begin
    499   Dest := ReadNext8;
    500   PDataInt(@Registers[Dest])^ := PDataInt(@Memory[SP])^;
     550  Reg: TRegIndex;
     551begin
     552  Reg := ReadNext8;
     553  PDataInt(@Registers[Reg])^ := PDataInt(@Memory[SP])^;
    501554  Inc(SP, SizeOf(TDataInt));
     555end;
     556
     557procedure TMachine.OpcodePop8;
     558var
     559  Reg: TRegIndex;
     560begin
     561  Reg := ReadNext8;
     562  PUInt8(@Registers[Reg])^ := PUInt8(@Memory[SP])^;
     563  Inc(SP, SizeOf(UInt8));
     564end;
     565
     566procedure TMachine.OpcodePop16;
     567var
     568  Reg: TRegIndex;
     569begin
     570  Reg := ReadNext8;
     571  PUInt16(@Registers[Reg])^ := PUInt16(@Memory[SP])^;
     572  Inc(SP, SizeOf(UInt16));
     573end;
     574
     575procedure TMachine.OpcodePop32;
     576var
     577  Reg: TRegIndex;
     578begin
     579  Reg := ReadNext8;
     580  PUInt32(@Registers[Reg])^ := PUInt32(@Memory[SP])^;
     581  Inc(SP, SizeOf(UInt32));
     582end;
     583
     584procedure TMachine.OpcodePop64;
     585var
     586  Reg: TRegIndex;
     587begin
     588  Reg := ReadNext8;
     589  PUInt64(@Registers[Reg])^ := PUInt64(@Memory[SP])^;
     590  Inc(SP, SizeOf(UInt64));
    502591end;
    503592
     
    612701end;
    613702
     703procedure TMachine.OpcodeDec8;
     704var
     705  Reg: TRegIndex;
     706begin
     707  Reg := ReadNext8;
     708  PUInt8(@Registers[Reg])^ := PUInt8(@Registers[Reg])^ - 1;
     709end;
     710
     711procedure TMachine.OpcodeDec16;
     712var
     713  Reg: TRegIndex;
     714begin
     715  Reg := ReadNext8;
     716  PUInt16(@Registers[Reg])^ := PUInt16(@Registers[Reg])^ - 1;
     717end;
     718
     719procedure TMachine.OpcodeDec32;
     720var
     721  Reg: TRegIndex;
     722begin
     723  Reg := ReadNext8;
     724  PUInt32(@Registers[Reg])^ := PUInt32(@Registers[Reg])^ - 1;
     725end;
     726
     727procedure TMachine.OpcodeDec64;
     728var
     729  Reg: TRegIndex;
     730begin
     731  Reg := ReadNext8;
     732  PUInt64(@Registers[Reg])^ := PUInt64(@Registers[Reg])^ - 1;
     733end;
     734
    614735procedure TMachine.OpcodeTestNotZero;
    615736var
     
    630751procedure TMachine.OpcodeJumpRel;
    631752var
    632   RelAddr: TAddrInt;
    633 begin
    634   RelAddr := ReadNextS;
    635   IP := IP + RelAddr;
     753  Reg: TRegIndex;
     754begin
     755  Reg := ReadNext8;
     756  IP := IP + PDataInt(@Registers[Reg])^;
     757end;
     758
     759procedure TMachine.OpcodeJumpRel8;
     760var
     761  Reg: TRegIndex;
     762begin
     763  Reg := ReadNext8;
     764  IP := IP + PInt8(@Registers[Reg])^;
     765end;
     766
     767procedure TMachine.OpcodeJumpRel16;
     768var
     769  Reg: TRegIndex;
     770begin
     771  Reg := ReadNext8;
     772  IP := IP + PInt16(@Registers[Reg])^;
     773end;
     774
     775procedure TMachine.OpcodeJumpRel32;
     776var
     777  Reg: TRegIndex;
     778begin
     779  Reg := ReadNext8;
     780  IP := IP + PInt32(@Registers[Reg])^;
     781end;
     782
     783procedure TMachine.OpcodeJumpRel64;
     784var
     785  Reg: TRegIndex;
     786begin
     787  Reg := ReadNext8;
     788  IP := IP + PInt64(@Registers[Reg])^;
    636789end;
    637790
    638791procedure TMachine.OpcodeJumpAbs;
    639792var
    640   Addr: TAddrInt;
    641 begin
    642   Addr := ReadNext;
    643   IP := Addr;
     793  Reg: TRegIndex;
     794begin
     795  Reg := ReadNext8;
     796  IP := PDataInt(@Registers[Reg])^;
     797end;
     798
     799procedure TMachine.OpcodeJumpAbs8;
     800var
     801  Reg: TRegIndex;
     802begin
     803  Reg := ReadNext8;
     804  IP := PByte(@Registers[Reg])^;
     805end;
     806
     807procedure TMachine.OpcodeJumpAbs16;
     808var
     809  Reg: TRegIndex;
     810begin
     811  Reg := ReadNext8;
     812  IP := PWord(@Registers[Reg])^;
     813end;
     814
     815procedure TMachine.OpcodeJumpAbs32;
     816var
     817  Reg: TRegIndex;
     818begin
     819  Reg := ReadNext8;
     820  IP := PCardinal(@Registers[Reg])^;
     821end;
     822
     823procedure TMachine.OpcodeJumpAbs64;
     824var
     825  Reg: TRegIndex;
     826begin
     827  Reg := ReadNext8;
     828  IP := PQWord(@Registers[Reg])^;
    644829end;
    645830
    646831procedure TMachine.OpcodeJumpRelCond;
    647832var
    648   RelAddr: TAddrInt;
    649 begin
    650   RelAddr := ReadNextS;
    651   if Condition then IP := IP + RelAddr;
     833  Reg: TRegIndex;
     834begin
     835  Reg := ReadNext8;
     836  if Condition then IP := IP + PDataInt(@Registers[Reg])^;
    652837end;
    653838
    654839procedure TMachine.OpcodeJumpRelCond8;
    655840var
    656   RelAddr: Int8;
    657 begin
    658   RelAddr := ReadNextS8;
    659   if Condition then IP := IP + RelAddr;
     841  Reg: TRegIndex;
     842begin
     843  Reg := ReadNext8;
     844  if Condition then IP := IP + PInt8(@Registers[Reg])^;
     845end;
     846
     847procedure TMachine.OpcodeJumpRelCond16;
     848var
     849  Reg: TRegIndex;
     850begin
     851  Reg := ReadNext8;
     852  if Condition then IP := IP + PInt16(@Registers[Reg])^;
     853end;
     854
     855procedure TMachine.OpcodeJumpRelCond32;
     856var
     857  Reg: TRegIndex;
     858begin
     859  Reg := ReadNext8;
     860  if Condition then IP := IP + PInt32(@Registers[Reg])^;
     861end;
     862
     863procedure TMachine.OpcodeJumpRelCond64;
     864var
     865  Reg: TRegIndex;
     866begin
     867  Reg := ReadNext8;
     868  if Condition then IP := IP + PInt64(@Registers[Reg])^;
    660869end;
    661870
    662871procedure TMachine.OpcodeJumpAbsCond;
    663872var
    664   Addr: TAddrInt;
    665 begin
    666   Addr := ReadNext;
    667   if Condition then IP := Addr;
     873  Reg: TRegIndex;
     874begin
     875  Reg := ReadNext8;
     876  if Condition then IP := PDataInt(@Registers[Reg])^;
     877end;
     878
     879procedure TMachine.OpcodeJumpAbsCond8;
     880var
     881  Reg: TRegIndex;
     882begin
     883  Reg := ReadNext8;
     884  if Condition then IP := PByte(@Registers[Reg])^;
     885end;
     886
     887procedure TMachine.OpcodeJumpAbsCond16;
     888var
     889  Reg: TRegIndex;
     890begin
     891  Reg := ReadNext8;
     892  if Condition then IP := PWord(@Registers[Reg])^;
     893end;
     894
     895procedure TMachine.OpcodeJumpAbsCond32;
     896var
     897  Reg: TRegIndex;
     898begin
     899  Reg := ReadNext8;
     900  if Condition then IP := PCardinal(@Registers[Reg])^;
     901end;
     902
     903procedure TMachine.OpcodeJumpAbsCond64;
     904var
     905  Reg: TRegIndex;
     906begin
     907  Reg := ReadNext8;
     908  if Condition then IP := PQWord(@Registers[Reg])^;
    668909end;
    669910
     
    7621003end;
    7631004
    764 function TMachine.ReadNext8: Byte;
     1005function TMachine.ReadNext8: UInt8;
    7651006begin
    7661007  CheckIP;
     
    7691010end;
    7701011
    771 function TMachine.ReadNextS8: ShortInt;
     1012function TMachine.ReadNextS8: Int8;
    7721013begin
    7731014  CheckIP;
     
    7761017end;
    7771018
    778 function TMachine.ReadNext16: Word;
     1019function TMachine.ReadNextS16: Int16;
     1020begin
     1021  CheckIP;
     1022  Result := PInt16(@Memory[IP])^;
     1023  Inc(IP, SizeOf(Int16));
     1024end;
     1025
     1026function TMachine.ReadNextS32: Int32;
     1027begin
     1028  CheckIP;
     1029  Result := PInt32(@Memory[IP])^;
     1030  Inc(IP, SizeOf(Int32));
     1031end;
     1032
     1033function TMachine.ReadNextS64: Int64;
     1034begin
     1035  CheckIP;
     1036  Result := PInt64(@Memory[IP])^;
     1037  Inc(IP, SizeOf(Int64));
     1038end;
     1039
     1040function TMachine.ReadNext16: UInt16;
    7791041begin
    7801042  CheckIP;
     
    7831045end;
    7841046
    785 function TMachine.ReadNext32: Cardinal;
     1047function TMachine.ReadNext32: UInt32;
    7861048begin
    7871049  CheckIP;
     
    7901052end;
    7911053
    792 function TMachine.ReadNext64: QWord;
     1054function TMachine.ReadNext64: UInt64;
    7931055begin
    7941056  CheckIP;
     
    8811143end;
    8821144
    883 procedure TInstructionWriter.AddJumpRelCond(RelAddr: Integer);
     1145procedure TInstructionWriter.AddJumpRelCond(Reg: Integer);
    8841146begin
    8851147  AddData8(Integer(opJumpRelCond));
    886   AddDataS(RelAddr - SizeOf(Byte) - SizeOf(TDataInt));
    887 end;
    888 
    889 procedure TInstructionWriter.AddJumpRelCond8(RelAddr: ShortInt);
     1148  AddDataS(Reg);
     1149end;
     1150
     1151procedure TInstructionWriter.AddJumpRelCond8(Reg: TRegIndex);
    8901152begin
    8911153  AddData8(Integer(opSetSize8));
    8921154  AddData8(Integer(opJumpRelCond));
    893   AddDataS8(RelAddr - SizeOf(Byte) - SizeOf(TDataInt));
     1155  AddData8(Reg);
    8941156end;
    8951157
Note: See TracChangeset for help on using the changeset viewer.