Ignore:
Timestamp:
Nov 2, 2023, 11:18:06 PM (7 months ago)
Author:
chronos
Message:
  • Added: Storage form.
  • Added: Dissasembler form.
  • Added: Debug and release build modes.
  • Added: Support for interface translation.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/ByteArray/Cpu.pas

    r47 r50  
    4141
    4242  TInstructionEvent = procedure of object;
     43  TCpuThread = class;
    4344
    4445  { TCpu }
     
    4647  TCpu = class
    4748  private
    48     Instructions: array[TInstruction] of TInstructionEvent;
     49    FHalted: Boolean;
     50    FRunning: Boolean;
     51    FThread: TCpuThread;
     52    FInstructions: array[TInstruction] of TInstructionEvent;
     53    FTicks: QWord;
    4954    procedure Push(Value: TBigInt; Size: TBigIntSize);
    5055    function Pop(Size: TBigIntSize): TBigInt;
     
    8489    procedure InstructionDecSize;
    8590    procedure InitInstructions;
     91    procedure SetRunning(AValue: Boolean);
     92    procedure Run;
     93    procedure Step;
    8694  public
    8795    Regs: array[TRegIndex] of TBigInt;
    8896    PC: TBigInt;
    8997    SP: TBigInt;
    90     Terminated: Boolean;
    9198    DataWidth: TBigIntSize;
    9299    AddressWidth: TBigIntSize;
     
    99106    procedure WriteRegister(Reg: TRegIndex);
    100107    procedure Reset;
    101     procedure Run;
    102     procedure Step;
     108    procedure Start;
     109    procedure Stop;
    103110    constructor Create;
    104111    destructor Destroy; override;
     112    property Ticks: QWord read FTicks;
     113    property Running: Boolean read FRunning write SetRunning;
     114    property Halted: Boolean read FHalted;
    105115  end;
    106116
     117  { TCpuThread }
     118
     119  TCpuThread = class(TThread)
     120    Cpu: TCpu;
     121    procedure Execute; override;
     122  end;
     123
     124
    107125implementation
     126
     127{ TCpuThread }
     128
     129procedure TCpuThread.Execute;
     130begin
     131  repeat
     132    if not Cpu.Halted then Cpu.Step
     133      else Sleep(1);
     134    //Cpu.CheckInterreupts;
     135  until Terminated;
     136end;
    108137
    109138{ TCpu }
     
    128157procedure TCpu.InstructionHalt;
    129158begin
    130   Terminated := True;
     159  FHalted := True;
    131160end;
    132161
     
    443472procedure TCpu.InitInstructions;
    444473begin
    445   Instructions[inNop] := InstructionNop;
    446   Instructions[inHalt] := InstructionHalt;
    447   Instructions[inLoadConst] := InstructionLoadConst;
    448   Instructions[inLoadConstSize] := InstructionLoadConstSize;
    449   Instructions[inLoad] := InstructionLoad;
    450   Instructions[inLoadSize] := InstructionLoadSize;
    451   Instructions[inLoadMem] := InstructionLoadMem;
    452   Instructions[inLoadMemSize] := InstructionLoadMemSize;
    453   Instructions[inStoreMem] := InstructionStoreMem;
    454   Instructions[inStoreMemSize] := InstructionStoreMemSize;
    455   Instructions[inJump] := InstructionJump;
    456   Instructions[inJumpSize] := InstructionJumpSize;
    457   Instructions[inJumpNotZero] := InstructionJumpNotZero;
    458   Instructions[inJumpNotZeroSize] := InstructionJumpNotZeroSize;
    459   Instructions[inJumpZero] := InstructionJumpZero;
    460   Instructions[inJumpZeroSize] := InstructionJumpZeroSize;
    461   Instructions[inJumpRel] := InstructionJumpRel;
    462   Instructions[inJumpRelSize] := InstructionJumpRelSize;
    463   Instructions[inCall] := InstructionCall;
    464   Instructions[inCallSize] := InstructionCallSize;
    465   Instructions[inRet] := InstructionRet;
    466   Instructions[inRetSize] := InstructionRetSize;
    467   Instructions[inPush] := InstructionPush;
    468   Instructions[inPushSize] := InstructionPushSize;
    469   Instructions[inPop] := InstructionPop;
    470   Instructions[inPopSize] := InstructionPopSize;
    471   Instructions[inInput] := InstructionInput;
    472   Instructions[inInputSize] := InstructionInputSize;
    473   Instructions[inOutput] := InstructionOutput;
    474   Instructions[inOutputSize] := InstructionOutputSize;
    475   Instructions[inInc] := InstructionInc;
    476   Instructions[inIncSize] := InstructionIncSize;
    477   Instructions[inDec] := InstructionDec;
    478   Instructions[inDecSize] := InstructionDecSize;
     474  FInstructions[inNop] := InstructionNop;
     475  FInstructions[inHalt] := InstructionHalt;
     476  FInstructions[inLoadConst] := InstructionLoadConst;
     477  FInstructions[inLoadConstSize] := InstructionLoadConstSize;
     478  FInstructions[inLoad] := InstructionLoad;
     479  FInstructions[inLoadSize] := InstructionLoadSize;
     480  FInstructions[inLoadMem] := InstructionLoadMem;
     481  FInstructions[inLoadMemSize] := InstructionLoadMemSize;
     482  FInstructions[inStoreMem] := InstructionStoreMem;
     483  FInstructions[inStoreMemSize] := InstructionStoreMemSize;
     484  FInstructions[inJump] := InstructionJump;
     485  FInstructions[inJumpSize] := InstructionJumpSize;
     486  FInstructions[inJumpNotZero] := InstructionJumpNotZero;
     487  FInstructions[inJumpNotZeroSize] := InstructionJumpNotZeroSize;
     488  FInstructions[inJumpZero] := InstructionJumpZero;
     489  FInstructions[inJumpZeroSize] := InstructionJumpZeroSize;
     490  FInstructions[inJumpRel] := InstructionJumpRel;
     491  FInstructions[inJumpRelSize] := InstructionJumpRelSize;
     492  FInstructions[inCall] := InstructionCall;
     493  FInstructions[inCallSize] := InstructionCallSize;
     494  FInstructions[inRet] := InstructionRet;
     495  FInstructions[inRetSize] := InstructionRetSize;
     496  FInstructions[inPush] := InstructionPush;
     497  FInstructions[inPushSize] := InstructionPushSize;
     498  FInstructions[inPop] := InstructionPop;
     499  FInstructions[inPopSize] := InstructionPopSize;
     500  FInstructions[inInput] := InstructionInput;
     501  FInstructions[inInputSize] := InstructionInputSize;
     502  FInstructions[inOutput] := InstructionOutput;
     503  FInstructions[inOutputSize] := InstructionOutputSize;
     504  FInstructions[inInc] := InstructionInc;
     505  FInstructions[inIncSize] := InstructionIncSize;
     506  FInstructions[inDec] := InstructionDec;
     507  FInstructions[inDecSize] := InstructionDecSize;
     508end;
     509
     510procedure TCpu.SetRunning(AValue: Boolean);
     511begin
     512  if FRunning = AValue then Exit;
     513  if AValue then Start
     514    else Stop;
    479515end;
    480516
     
    516552  PC := 0;
    517553  SP := 0;
    518   Terminated := False;
     554  FHalted := False;
     555  FTicks := 0;
    519556end;
    520557
     
    522559begin
    523560  Reset;
    524   while not Terminated do
     561  while not FHalted do
    525562    Step;
    526563end;
     
    531568begin
    532569  Instruction := TInstruction(Byte(Read(1)));
    533   Instructions[Instruction];
     570  FInstructions[Instruction];
     571  Inc(FTicks);
     572end;
     573
     574procedure TCpu.Start;
     575begin
     576  if not Running then begin
     577    Reset;
     578    FThread := TCpuThread.Create(True);
     579    FThread.Cpu := Self;
     580    FThread.Start;
     581    FRunning := True;
     582  end;
     583end;
     584
     585procedure TCpu.Stop;
     586begin
     587  if Running then begin
     588    FHalted := True;
     589    FThread.Terminate;
     590    FThread.WaitFor;
     591    FreeAndNil(FThread);
     592    FRunning := False;
     593  end;
    534594end;
    535595
     
    544604destructor TCpu.Destroy;
    545605begin
     606  Stop;
    546607  FreeAndNil(Memory);
    547608  FreeAndNil(IO);
Note: See TracChangeset for help on using the changeset viewer.