Changeset 14 for branches/test2/Cpu.pas


Ignore:
Timestamp:
Nov 6, 2025, 10:35:31 PM (35 hours ago)
Author:
chronos
Message:
  • Added: More instructions to test2 project.
Location:
branches/test2
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/test2

    • Property svn:ignore set to
      lib
      Project1
      Project1.res
      Project1.lps
  • branches/test2/Cpu.pas

    r13 r14  
    44
    55uses
    6   Classes, SysUtils;
     6  Classes, SysUtils, Generics.Collections;
    77
    88type
    9   TOperation = (opNoOperation, opTerminate, opCopyMem, opIncrement, opAdd, opSet,
    10     opInput, opOutput, opJump, opJumpRelative, opCall, opReturn, opPush, opPop);
     9  TOperation = (opNoOperation, opTerminate, opCopyMem, opCopyMemIndex, opSet,
     10    opExchange,
     11    opIncrement, opDecrement,
     12    opAdd, opSubtract, opMultiply, opDivide,
     13    opShiftLeft, opShiftRight,
     14    opInput, opOutput,
     15    opJump, opJumpZero, opJumpNotZero,
     16    opJumpRelative, opJumpRelativeZero, opJumpRelativeNotZero,
     17    opPush, opPop,
     18    opCall, opReturn,
     19    opOr, opXor, opAnd,
     20    opSetBit, opResetBit);
    1121
    1222  TData = Cardinal;
     
    1626    Source: TData;
    1727    Destination: TData;
     28    Count: TData;
    1829  end;
     30
     31  TInstructions = TList<TInstruction>;
    1932
    2033  TInputEvent = function (Address: TData): TData of object;
     
    3043    function Pop: TData;
    3144  public
    32     Instructions: array of TInstruction;
     45    Instructions: TInstructions;
    3346    IP: Integer;
    3447    SP: Integer;
    3548    Terminated: Boolean;
    3649    Memory: array of TData;
    37     function AddInstruction(Operation: TOperation; Destination, Source: TData): TInstruction;
     50    function AddInstruction(Operation: TOperation; Destination: TData = 0;
     51      Source: TData = 0; Count: TData = 0): TInstruction;
    3852    procedure Reset;
    3953    procedure Step;
    4054    procedure Run;
     55    constructor Create;
    4156    destructor Destroy; override;
    4257    property OnInput: TInputEvent read FOnInput write FOnInput;
     
    6176end;
    6277
    63 function TCpu.AddInstruction(Operation: TOperation; Destination,
    64   Source: TData): TInstruction;
     78function TCpu.AddInstruction(Operation: TOperation; Destination: TData = 0;
     79  Source: TData = 0; Count: TData = 0): TInstruction;
    6580begin
    6681  Result.Operation := Operation;
    6782  Result.Destination := Destination;
    6883  Result.Source := Source;
    69   SetLength(Instructions, Length(Instructions) + 1);
    70   Instructions[Length(Instructions) - 1] := Result;
     84  Result.Count := Count;
     85  Instructions.Add(Result);
    7186end;
    7287
     
    8196var
    8297  Instruction: TInstruction;
     98  Temp: TData;
    8399begin
    84100  Instruction := Instructions[IP];
    85   IP := (IP + 1) mod Length(Instructions);
     101  IP := (IP + 1) mod Instructions.Count;
    86102  case Instruction.Operation of
    87103    opNoOperation: ;
    88104    opTerminate: Terminated := True;
    89105    opCopyMem: Memory[Instruction.Destination] := Memory[Instruction.Source];
     106    opCopyMemIndex: Memory[Instruction.Destination] := Memory[Instruction.Source] + Instruction.Count;
     107    opExchange: begin
     108      Temp := Memory[Instruction.Destination];
     109      Memory[Instruction.Destination] := Memory[Instruction.Source];
     110      Memory[Instruction.Source] := Temp;
     111    end;
    90112    opIncrement: Memory[Instruction.Destination] := Memory[Instruction.Destination] + 1;
     113    opDecrement: Memory[Instruction.Destination] := Memory[Instruction.Destination] - 1;
    91114    opAdd: Memory[Instruction.Destination] := Memory[Instruction.Destination] + Memory[Instruction.Source];
     115    opSubtract: Memory[Instruction.Destination] := Memory[Instruction.Destination] - Memory[Instruction.Source];
     116    opMultiply: Memory[Instruction.Destination] := Memory[Instruction.Destination] * Memory[Instruction.Source];
     117    opDivide: Memory[Instruction.Destination] := Memory[Instruction.Destination] div Memory[Instruction.Source];
    92118    opSet: Memory[Instruction.Destination] := Instruction.Source;
    93119    opOutput: if Assigned(FOnOutput) then FOnOutput(Instruction.Destination, Memory[Instruction.Source]);
     
    95121      else Memory[Instruction.Destination] := 0;
    96122    opJump: IP := Instruction.Destination;
     123    opJumpZero: if Memory[Instruction.Source] = 0 then IP := Instruction.Destination;
     124    opJumpNotZero: if Memory[Instruction.Source] <> 0 then IP := IP + Instruction.Destination;
    97125    opJumpRelative: IP := IP + Instruction.Destination;
     126    opJumpRelativeZero: if Memory[Instruction.Source] = 0 then IP := IP + Instruction.Destination;
     127    opJumpRelativeNotZero: if Memory[Instruction.Source] <> 0 then IP := IP + Instruction.Destination;
    98128    opPush: Push(Memory[Instruction.Source]);
    99129    opPop: Memory[Instruction.Destination] := Pop;
     
    103133    end;
    104134    opReturn: IP := Pop;
     135    opShiftLeft: Memory[Instruction.Destination] := Memory[Instruction.Destination] shl Memory[Instruction.Source];
     136    opShiftRight: Memory[Instruction.Destination] := Memory[Instruction.Destination] shr Memory[Instruction.Source];
     137    opOr: Memory[Instruction.Destination] := Memory[Instruction.Destination] or Memory[Instruction.Source];
     138    opXor: Memory[Instruction.Destination] := Memory[Instruction.Destination] xor Memory[Instruction.Source];
     139    opAnd: Memory[Instruction.Destination] := Memory[Instruction.Destination] and Memory[Instruction.Source];
     140    opSetBit: Memory[Instruction.Destination] := Memory[Instruction.Destination] or (1 shl Memory[Instruction.Source]);
     141    opResetBit: Memory[Instruction.Destination] := Memory[Instruction.Destination] and (High(TData) xor (1 shl Memory[Instruction.Source]));
    105142  end;
    106143
     
    121158procedure TCpu.Run;
    122159begin
    123   if Length(Instructions) = 0 then
     160  if Instructions.Count = 0 then
    124161    raise Exception.Create('No program for execution.');
    125162  Reset;
     
    128165end;
    129166
     167constructor TCpu.Create;
     168begin
     169  Instructions := TInstructions.Create;
     170end;
     171
    130172destructor TCpu.Destroy;
    131173begin
    132174  Terminated := True;
     175  FreeAndNil(Instructions);
    133176end;
    134177
Note: See TracChangeset for help on using the changeset viewer.