Changeset 164


Ignore:
Timestamp:
Aug 7, 2018, 5:30:22 PM (6 years ago)
Author:
chronos
Message:
  • Added: More supported CPU instructions.
Location:
branches/virtcpu fixed int
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/virtcpu fixed int/UFormMain.lfm

    r163 r164  
    11object Form1: TForm1
    2   Left = 449
     2  Left = 456
    33  Height = 762
    4   Top = 145
     4  Top = 163
    55  Width = 1286
    66  Caption = 'Form1'
  • branches/virtcpu fixed int/UMachine.pas

    r163 r164  
    11unit UMachine;
     2
     3{$DEFINE EXT_MEMORY}
     4{$DEFINE EXT_IO}
     5{$DEFINE EXT_ARITHMETIC}
     6{$DEFINE EXT_CONDITIONAL}
     7//{$DEFINE EXT_LOGICAL}
     8{$DEFINE EXT_STACK}
     9{$DEFINE EXT_SUBROUTINE}
     10//{$DEFINE EXT_SHIFT}
     11//{$DEFINE EXT_BLOCK}
     12{$DEFINE EXT_GENERAL}
     13//{$DEFINE EXT_BIT}
     14
     15// Extension dependencies
     16{$IFDEF EXT_SUBROUTINE}
     17{$DEFINE EXT_STACK}
     18{$ENDIF}
     19
    220
    321{$mode delphi}{$H+}
     
    1230
    1331  TOpcode = (opNop, opLoad, opHalt, opLoadConst, opLoadMem, opStoreMem, opNeg,
    14     opJump, opJumpRel, opPush, opPop, opCall, opCallRel, opRet, opInput,
    15     opOutput, opInc, opDec);
     32    opJump, opJumpRel, opCall, opCallRel, opRet, opInput,
     33    opOutput, opInc, opDec, opAdd, opSub, opExchg,
     34    {$IFDEF EXT_STACK}
     35    opPush, opPop,
     36    {$ENDIF}
     37    {$IFDEF EXT_CONDITIONAL}
     38    opJumpRelCond, opJumpCond, opTestEqual, opTestNotEqual, opTestLess,
     39    opTestLessEqual, opGreater, opGreaterEqual
     40    {$ENDIF}
     41  );
    1642
    1743  TOpcodeHandler = procedure of object;
     
    3157    procedure OpcodeLoad;
    3258    procedure OpcodeLoadConst;
     59    {$IFDEF EXT_MEMORY}
    3360    procedure OpcodeLoadMem;
    3461    procedure OpcodeStoreMem;
     62    {$ENDIF}
    3563    procedure OpcodeNeg;
     64    {$IFDEF EXT_GENERAL}
     65    procedure OpcodeExchange;
     66    {$ENDIF}
    3667    procedure OpcodeJump;
    3768    procedure OpcodeJumpRel;
     69    {$IFDEF EXT_CONDITIONAL}
     70    procedure OpcodeTestEqual;
     71    procedure OpcodeTestNotEqual;
     72    procedure OpcodeTestGreatEqual;
     73    procedure OpcodeTestGreat;
     74    procedure OpcodeTestLessEqual;
     75    procedure OpcodeTestLess;
     76    procedure OpcodeJumpCond;
     77    procedure OpcodeJumpRelCond;
     78    {$ENDIF}
     79    {$IFDEF EXT_STACK}
    3880    procedure OpcodePush;
    3981    procedure OpcodePop;
     82    {$ENDIF}
     83    {$IFDEF EXT_SUBROUTINE}
    4084    procedure OpcodeCall;
    4185    procedure OpcodeCallRel;
    4286    procedure OpcodeReturn;
     87    {$ENDIF}
     88    {$IFDEF EXT_IO}
    4389    procedure OpcodeOutput;
    4490    procedure OpcodeInput;
     91    {$ENDIF}
    4592    procedure OpcodeInc;
    4693    procedure OpcodeDec;
     94    {$IFDEF EXT_ARITHMETIC}
     95    procedure OpcodeAdd;
     96    procedure OpcodeSub;
     97    {$ENDIF}
    4798  public
    4899    Registers: array of T;
    49100    IP: T;
     101    {$IFDEF EXT_CONDITIONAL}
     102    Condition: Boolean;
     103    {$ENDIF}
     104    {$IFDEF EXT_STACK}
    50105    SP: T;
     106    {$ENDIF}
    51107    Memory: array of T;
    52108    Terminated: Boolean;
     
    100156end;
    101157
     158{$IFDEF EXT_MEMORY}
    102159procedure TCPU.OpcodeLoadMem;
    103160var
     
    119176  Memory[Registers[P1]] := Registers[P2];
    120177end;
     178{$ENDIF}
    121179
    122180procedure TCPU.OpcodeNeg;
     
    128186end;
    129187
     188{$IFDEF EXT_GENERAL}
     189procedure TCPU.OpcodeExchange;
     190var
     191  P1, P2, Temp: T;
     192begin
     193  P1 := ReadNext;
     194  P2 := ReadNext;
     195  Temp := Registers[P1];
     196  Registers[P1] := Registers[P2];
     197  Registers[P2] := Temp;
     198end;
     199{$ENDIF}
     200
    130201procedure TCPU.OpcodeJump;
    131202begin
     
    138209end;
    139210
     211{$IFDEF EXT_CONDITIONAL}
     212procedure TCPU.OpcodeTestEqual;
     213begin
     214  Condition := ReadNext = ReadNext;
     215end;
     216
     217procedure TCPU.OpcodeTestNotEqual;
     218begin
     219  Condition := ReadNext <> ReadNext;
     220end;
     221
     222procedure TCPU.OpcodeTestGreatEqual;
     223begin
     224  Condition := ReadNext >= ReadNext;
     225end;
     226
     227procedure TCPU.OpcodeTestGreat;
     228begin
     229  Condition := ReadNext > ReadNext;
     230end;
     231
     232procedure TCPU.OpcodeTestLessEqual;
     233begin
     234  Condition := ReadNext <= ReadNext;
     235end;
     236
     237procedure TCPU.OpcodeTestLess;
     238begin
     239  Condition := ReadNext < ReadNext;
     240end;
     241
     242procedure TCPU.OpcodeJumpCond;
     243begin
     244  if Condition then IP := ReadNext;
     245end;
     246
     247procedure TCPU.OpcodeJumpRelCond;
     248begin
     249  if Condition then IP := IP + ReadNext;
     250end;
     251{$ENDIF}
     252
     253{$IFDEF EXT_STACK}
    140254procedure TCPU.OpcodePush;
    141255begin
     
    149263  Registers[ReadNext] := Memory[SP];
    150264end;
    151 
     265{$ENDIF}
     266
     267{$IFDEF EXT_SUBROUTINE}
    152268procedure TCPU.OpcodeCall;
    153269var
     
    175291  IP := Memory[SP];
    176292end;
    177 
     293{$ENDIF}
     294
     295{$IFDEF EXT_IO}
    178296procedure TCPU.OpcodeOutput;
    179297var
     
    197315    Registers[R1] := FOnInput(Registers[R2]);
    198316end;
     317{$ENDIF}
    199318
    200319procedure TCPU.OpcodeInc;
     
    214333end;
    215334
     335{$IFDEF EXT_ARITHMETIC}
     336procedure TCPU.OpcodeAdd;
     337var
     338  R1: T;
     339  R2: T;
     340begin
     341  R1 := ReadNext;
     342  R2 := ReadNext;
     343  Registers[R1] := Registers[R1] + Registers[R2];
     344end;
     345
     346procedure TCPU.OpcodeSub;
     347var
     348  R1: T;
     349  R2: T;
     350begin
     351  R1 := ReadNext;
     352  R2 := ReadNext;
     353  Registers[R1] := Registers[R1] - Registers[R2];
     354end;
     355{$ENDIF}
     356
    216357procedure TCPU.Start;
    217358var
     
    220361  Terminated := False;
    221362  IP := 0;
     363  {$IFDEF EXT_STACK}
    222364  SP := Length(Memory);
     365  {$ENDIF}
    223366  while not Terminated do begin
    224367    Opcode := ReadNext;
     
    243386  OpcodeHandlers[opHalt] := OpcodeHalt;
    244387  OpcodeHandlers[opLoadConst] := OpcodeLoadConst;
     388  {$IFDEF EXT_MEMORY}
    245389  OpcodeHandlers[opLoadMem] := OpcodeLoadMem;
    246390  OpcodeHandlers[opStoreMem] := OpcodeStoreMem;
     391  {$ENDIF}
    247392  OpcodeHandlers[opNeg] := OpcodeNeg;
    248393  OpcodeHandlers[opJump] := OpcodeJump;
    249394  OpcodeHandlers[opJumpRel] := OpcodeJumpRel;
     395  {$IFDEF EXT_GENERAL}
     396  OpcodeHandlers[opExchg] := OpcodeExchange;
     397  {$ENDIF}
     398  {$IFDEF EXT_STACK}
    250399  OpcodeHandlers[opPush] := OpcodePush;
    251400  OpcodeHandlers[opPop] := OpcodePop;
     401  {$ENDIF}
     402  {$IFDEF EXT_SUBROUTINE}
    252403  OpcodeHandlers[opCall] := OpcodeCall;
    253404  OpcodeHandlers[opCallRel] := OpcodeCallRel;
    254405  OpcodeHandlers[opRet] := OpcodeReturn;
     406  {$ENDIF}
     407  {$IFDEF EXT_IO}
    255408  OpcodeHandlers[opInput] := OpcodeInput;
    256409  OpcodeHandlers[opOutput] := OpcodeOutput;
     410  {$ENDIF}
    257411  OpcodeHandlers[opInc] := OpcodeInc;
    258412  OpcodeHandlers[opDec] := OpcodeDec;
     413  {$IFDEF EXT_ARITHMETIC}
     414  OpcodeHandlers[opAdd] := OpcodeAdd;
     415  OpcodeHandlers[opSub] := OpcodeSub;
     416  {$ENDIF}
     417  {$IFDEF EXT_ARITHMETIC}
     418  OpcodeHandlers[opJumpCond] := OpcodeJumpCond;
     419  OpcodeHandlers[opJumpRelCond] := OpcodeJumpRelCond;
     420  OpcodeHandlers[opTestEqual] := OpcodeTestEqual;
     421  OpcodeHandlers[opTestNotEqual] := OpcodeTestNotEqual;
     422  OpcodeHandlers[opTestLess] := OpcodeTestLess;
     423  OpcodeHandlers[opTestLessEqual] := OpcodeTestLessEqual;
     424  OpcodeHandlers[opGreater] := OpcodeTestGreat;
     425  OpcodeHandlers[opGreaterEqual] := OpcodeTestGreatEqual;
     426  {$ENDIF}
    259427end;
    260428
Note: See TracChangeset for help on using the changeset viewer.