Changeset 157


Ignore:
Timestamp:
Apr 24, 2018, 11:08:11 AM (7 years ago)
Author:
chronos
Message:
  • Modified: Use fixed byte data type for register index and opcode index.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/virtualcpu3/UMachine.pas

    r156 r157  
    2222    TOutputEvent = procedure (Port: T; Value: T) of object;
    2323    TInputEvent = function (Port: T): T of object;
     24    PT = ^T;
     25    TOpcodeIndex = Byte;
     26    POpcodeIndex = ^TOpcodeIndex;
     27    TRegIndex = Byte;
     28    PRegIndex = ^TRegIndex;
    2429  var
    2530    Terminated: Boolean;
    2631    OpcodeHandlers: array[TOpcode] of TOpcodeHandler;
    2732    function ReadNext: T;
     33    function ReadOpcode: TOpcodeIndex;
     34    function ReadReg: TRegIndex;
    2835    procedure Step;
    2936    procedure AddOpcode(Opcode: TOpcode; Handler: TOpcodeHandler);
     
    5158    procedure OpcodeLoadMemoryDisplacement;
    5259    procedure OpcodeMultiplication;
    53     procedure OpcodeNop;
     60    procedure OpcodeNoOperation;
    5461    procedure OpcodeOr;
    5562    procedure OpcodeOutput;
     
    6774  public
    6875    Registers: array of T;
    69     Memory: array of T;
     76    Memory: array of Byte;
    7077    IP: T;
    7178    SP: T;
     
    8390function TCPU.ReadNext: T;
    8491begin
    85   Result := Memory[IP];
    86   Inc(IP);
     92  Result := PT(@Memory[IP])^;
     93  Inc(IP, SizeOf(T));
     94end;
     95
     96function TCPU.ReadOpcode: TOpcodeIndex;
     97begin
     98  Result := POpcodeIndex(@Memory[IP])^;
     99  Inc(IP, SizeOf(TOpcodeIndex));
     100end;
     101
     102function TCPU.ReadReg: TRegIndex;
     103begin
     104  Result := PRegIndex(@Memory[IP])^;
     105  Inc(IP, SizeOf(TRegIndex));
    87106end;
    88107
     
    103122end;
    104123
    105 procedure TCPU.OpcodeNop;
     124procedure TCPU.OpcodeNoOperation;
    106125begin
    107126end;
     
    114133procedure TCPU.OpcodeLoad;
    115134var
    116   Src, Dest: T;
    117 begin
    118   Dest := ReadNext;
    119   Src := ReadNext;
     135  Src, Dest: TRegIndex;
     136begin
     137  Dest := ReadReg;
     138  Src := ReadReg;
    120139  Registers[Dest] := Registers[Src];
    121140end;
     
    123142procedure TCPU.OpcodeLoadConst;
    124143var
    125   Reg: T;
    126 begin
    127   Reg := ReadNext;
     144  Reg: TRegIndex;
     145begin
     146  Reg := ReadReg;
    128147  Registers[Reg] := ReadNext;
    129148end;
     
    131150procedure TCPU.OpcodeLoadMemory;
    132151var
    133   Src, Dest: T;
    134 begin
    135   Dest := ReadNext;
    136   Src := ReadNext;
    137   Registers[Dest] := Memory[Registers[Src]];
     152  Src, Dest: TRegIndex;
     153begin
     154  Dest := ReadReg;
     155  Src := ReadReg;
     156  Registers[Dest] := PT(@Memory[Registers[Src]])^;
    138157end;
    139158
    140159procedure TCPU.OpcodeStoreMemory;
    141160var
    142   Src, Dest: T;
    143 begin
    144   Dest := ReadNext;
    145   Src := ReadNext;
    146   Memory[Registers[Dest]] := Registers[Src];
     161  Src, Dest: TRegIndex;
     162begin
     163  Dest := ReadReg;
     164  Src := ReadReg;
     165  PT(@Memory[Registers[Dest]])^ := Registers[Src];
    147166end;
    148167
    149168procedure TCPU.OpcodeLoadMemoryDisplacement;
    150169var
    151   Reg, Src, Dest: T;
    152 begin
    153   Dest := ReadNext;
    154   Src := ReadNext;
    155   Reg := ReadNext;
    156   Registers[Dest] := Memory[Registers[Src] + Registers[Reg]];
     170  Disp, Src, Dest: TRegIndex;
     171begin
     172  Dest := ReadReg;
     173  Src := ReadReg;
     174  Disp := ReadReg;
     175  Registers[Dest] := PT(@Memory[Registers[Src] + Registers[Disp]])^;
    157176end;
    158177
    159178procedure TCPU.OpcodeStoreMemoryDisplacement;
    160179var
    161   Reg, Src, Dest: T;
    162 begin
    163   Dest := ReadNext;
    164   Reg := ReadNext;
    165   Src := ReadNext;
    166   Memory[Registers[Dest] + Registers[Reg]] := Registers[Src];
     180  Disp, Src, Dest: TRegIndex;
     181begin
     182  Dest := ReadReg;
     183  Disp := ReadReg;
     184  Src := ReadReg;
     185  PT(@Memory[Registers[Dest] + Registers[Disp]])^ := Registers[Src];
    167186end;
    168187
    169188procedure TCPU.OpcodeIncrement;
    170189var
    171   Reg: T;
    172 begin
    173   Reg := ReadNext;
     190  Reg: TRegIndex;
     191begin
     192  Reg := ReadReg;
    174193  Registers[Reg] := Registers[Reg] + 1;
    175194end;
     
    177196procedure TCPU.OpcodeDecrement;
    178197var
    179   Reg: T;
    180 begin
    181   Reg := ReadNext;
     198  Reg: TRegIndex;
     199begin
     200  Reg := ReadReg;
    182201  Registers[Reg] := Registers[Reg] - 1;
    183202end;
     
    185204procedure TCPU.OpcodeAddition;
    186205var
    187   Dest, Src: T;
    188 begin
    189   Dest := ReadNext;
    190   Src := ReadNext;
     206  Dest, Src: TRegIndex;
     207begin
     208  Dest := ReadReg;
     209  Src := ReadReg;
    191210  Registers[Dest] := Registers[Dest] + Registers[Src];
    192211end;
     
    194213procedure TCPU.OpcodeSubtraction;
    195214var
    196   Dest, Src: T;
    197 begin
    198   Dest := ReadNext;
    199   Src := ReadNext;
     215  Dest, Src: TRegIndex;
     216begin
     217  Dest := ReadReg;
     218  Src := ReadReg;
    200219  Registers[Dest] := Registers[Dest] - Registers[Src];
    201220end;
     
    203222procedure TCPU.OpcodeMultiplication;
    204223var
    205   Dest, Src: T;
    206 begin
    207   Dest := ReadNext;
    208   Src := ReadNext;
     224  Dest, Src: TRegIndex;
     225begin
     226  Dest := ReadReg;
     227  Src := ReadReg;
    209228  Registers[Dest] := Registers[Dest] * Registers[Src];
    210229end;
     
    212231procedure TCPU.OpcodeDivision;
    213232var
    214   Dest, Src: T;
    215 begin
    216   Dest := ReadNext;
    217   Src := ReadNext;
     233  Dest, Src: TRegIndex;
     234begin
     235  Dest := ReadReg;
     236  Src := ReadReg;
    218237  Registers[Dest] := Registers[Dest] div Registers[Src];
    219238end;
     
    221240procedure TCPU.OpcodeShiftLeft;
    222241var
    223   Dest, Src: T;
    224 begin
    225   Dest := ReadNext;
    226   Src := ReadNext;
     242  Dest, Src: TRegIndex;
     243begin
     244  Dest := ReadReg;
     245  Src := ReadReg;
    227246  Registers[Dest] := Registers[Dest] shl Registers[Src];
    228247end;
     
    230249procedure TCPU.OpcodeShiftRight;
    231250var
    232   Src, Dest: T;
    233 begin
    234   Dest := ReadNext;
    235   Src := ReadNext;
     251  Src, Dest: TRegIndex;
     252begin
     253  Dest := ReadReg;
     254  Src := ReadReg;
    236255  Registers[Dest] := Registers[Dest] shr Registers[Src];
    237256end;
     
    239258procedure TCPU.OpcodeAnd;
    240259var
    241   Src, Dest: T;
    242 begin
    243   Dest := ReadNext;
    244   Src := ReadNext;
     260  Src, Dest: TRegIndex;
     261begin
     262  Dest := ReadReg;
     263  Src := ReadReg;
    245264  Registers[Dest] := Registers[Dest] and Registers[Src];
    246265end;
     
    248267procedure TCPU.OpcodeOr;
    249268var
    250   Src, Dest: T;
    251 begin
    252   Dest := ReadNext;
    253   Src := ReadNext;
     269  Src, Dest: TRegIndex;
     270begin
     271  Dest := ReadReg;
     272  Src := ReadReg;
    254273  Registers[Dest] := Registers[Dest] or Registers[Src];
    255274end;
     
    257276procedure TCPU.OpcodeXor;
    258277var
    259   Src, Dest: T;
    260 begin
    261   Dest := ReadNext;
    262   Src := ReadNext;
     278  Src, Dest: TRegIndex;
     279begin
     280  Dest := ReadReg;
     281  Src := ReadReg;
    263282  Registers[Dest] := Registers[Dest] xor Registers[Src];
    264283end;
     
    266285procedure TCPU.OpcodeJump;
    267286var
    268   Reg: T;
    269 begin
    270   Reg := ReadNext;
     287  Reg: TRegIndex;
     288begin
     289  Reg := ReadReg;
    271290  IP := Registers[Reg];
    272291end;
     
    274293procedure TCPU.OpcodeJumpConditional;
    275294var
    276   Reg: T;
    277 begin
    278   Reg := ReadNext;
     295  Reg: TRegIndex;
     296begin
     297  Reg := ReadReg;
    279298  if Condition then
    280299    IP := Registers[Reg];
     
    283302procedure TCPU.OpcodeJumpRelative;
    284303var
    285   Reg: T;
    286 begin
    287   Reg := ReadNext;
     304  Reg: TRegIndex;
     305begin
     306  Reg := ReadReg;
    288307  IP := IP + Registers[Reg];
    289308end;
     
    291310procedure TCPU.OpcodeJumpRelativeConditional;
    292311var
    293   Reg: T;
    294 begin
    295   Reg := ReadNext;
     312  Reg: TRegIndex;
     313begin
     314  Reg := ReadReg;
    296315  if Condition then
    297316    IP := IP + Registers[Reg];
     
    300319procedure TCPU.OpcodePush;
    301320var
    302   Reg: T;
    303 begin
    304   SP := SP - 1;
    305   Reg := ReadNext;
    306   Memory[SP] := Registers[Reg];
     321  Reg: TRegIndex;
     322begin
     323  SP := SP - SizeOf(T);
     324  Reg := ReadReg;
     325  PT(@Memory[SP])^ := Registers[Reg];
    307326end;
    308327
    309328procedure TCPU.OpcodePop;
    310329var
    311   Reg: T;
    312 begin
    313   Reg := ReadNext;
    314   Registers[Reg] := Memory[SP];
    315   SP := SP + 1;
     330  Reg: TRegIndex;
     331begin
     332  Reg := ReadReg;
     333  Registers[Reg] := PT(@Memory[SP])^;
     334  SP := SP + SizeOf(T);
    316335end;
    317336
    318337procedure TCPU.OpcodeCall;
    319338var
    320   Reg: T;
    321 begin
    322   SP := SP - 1;
    323   Reg := ReadNext;
    324   Memory[SP] := IP;
     339  Reg: TRegIndex;
     340begin
     341  SP := SP - SizeOf(T);
     342  Reg := ReadReg;
     343  PT(@Memory[SP])^ := IP;
    325344  IP := Registers[Reg];
    326345end;
     
    328347procedure TCPU.OpcodeReturn;
    329348begin
    330   IP := Memory[SP];
    331   SP := SP + 1;
     349  IP := PT(@Memory[SP])^;
     350  SP := SP + SizeOf(T);
    332351end;
    333352
    334353procedure TCPU.OpcodeInput;
    335354var
    336   Src, Dest: T;
    337 begin
    338   Dest := ReadNext;
    339   Src := ReadNext;
     355  Src, Dest: TRegIndex;
     356begin
     357  Dest := ReadReg;
     358  Src := ReadReg;
    340359  if Assigned(FOnInput) then
    341360    Registers[Dest] := FOnInput(Registers[Src]);
     
    344363procedure TCPU.OpcodeOutput;
    345364var
    346   Dest, Src: T;
    347 begin
    348   Dest := ReadNext;
    349   Src := ReadNext;
     365  Dest, Src: TRegIndex;
     366begin
     367  Dest := ReadReg;
     368  Src := ReadReg;
    350369  if Assigned(FOnOutput) then
    351370    FOnOutput(Registers[Dest], Registers[Src]);
     
    354373procedure TCPU.OpcodeTestZero;
    355374var
    356   Reg: T;
    357 begin
    358   Reg := ReadNext;
     375  Reg: TRegIndex;
     376begin
     377  Reg := ReadReg;
    359378  Condition := Registers[Reg] = 0;
    360379end;
     
    362381procedure TCPU.OpcodeTestNotZero;
    363382var
    364   Reg: T;
    365 begin
    366   Reg := ReadNext;
     383  Reg: TRegIndex;
     384begin
     385  Reg := ReadReg;
    367386  Condition := Registers[Reg] <> 0;
    368387end;
     
    370389procedure TCPU.OpcodeExchange;
    371390var
    372   Dest, Src, Temp: T;
    373 begin
    374   Dest := ReadNext;
    375   Src := ReadNext;
     391  Dest, Src: TRegIndex;
     392  Temp: T;
     393begin
     394  Dest := ReadReg;
     395  Src := ReadReg;
    376396  Temp := Registers[Dest];
    377397  Registers[Dest] := Registers[Src];
     
    385405  Handler: TOpcodeHandler;
    386406begin
    387   Opcode := TOpcode(ReadNext);
     407  Opcode := TOpcode(ReadOpcode);
    388408  if Opcode <= High(TOpcode) then begin
    389409    Handler := OpcodeHandlers[Opcode];
     
    400420procedure TCPU.InitOpcodes;
    401421begin
    402   AddOpcode(opNop, @OpcodeNop);
     422  AddOpcode(opNop, @OpcodeNoOperation);
    403423  AddOpcode(opHalt, @OpcodeHalt);
    404424  AddOpcode(opAnd, @OpcodeAnd);
Note: See TracChangeset for help on using the changeset viewer.