Ignore:
Timestamp:
Sep 30, 2016, 10:13:35 PM (8 years ago)
Author:
chronos
Message:
  • Modified: Start/stop virtual machine by buttons. Thread moved from main form to TMachine class.
Location:
branches/virt simple
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/virt simple

    • Property svn:ignore set to
      lib
      project1
      project1.lps
  • branches/virt simple/UMachine.pas

    r88 r89  
    6363  TSystemCallEvent = procedure(Index: TValue) of object;
    6464
     65  TMachineThread = class;
     66
    6567  { TMachine }
    6668
     
    7072    FOnPortOut: TPortOutEvent;
    7173    FOnSystemCall: TSystemCallEvent;
     74    FRunning: Boolean;
    7275    InstructionHandlers: array[TOpcode] of TInstructionHandler;
     76    Thread: TMachineThread;
    7377    function GetValue(Param: TParam): TValue;
    7478    procedure InstructionAdd;
     
    103107    procedure Init(MemorySize, RegisterCount: Integer);
    104108    procedure Run;
     109    procedure Start;
     110    procedure Stop;
    105111    constructor Create;
    106112    destructor Destroy; override;
     113    property Running: Boolean read FRunning;
    107114    property OnPortOut: TPortOutEvent read FOnPortOut write FOnPortOut;
    108115    property OnPortIn: TPortInEvent read FOnPortIn write FOnPortIn;
     
    110117  end;
    111118
     119  { TMachineThread }
     120
     121  TMachineThread = class(TThread)
     122    Machine: TMachine;
     123    procedure Execute; override;
     124  end;
     125
    112126
    113127implementation
     128
     129{ TMachineThread }
     130
     131procedure TMachineThread.Execute;
     132begin
     133  Machine.Run;
     134end;
    114135
    115136
     
    369390begin
    370391  while not Terminated do begin
    371     with TInstruction(Instructions[IP]) do
    372     if Assigned(InstructionHandlers[Opcode]) then
    373       InstructionHandlers[Opcode]
    374       else raise Exception.Create('Unsupported instruction opcode ' + OpcodeString[Opcode]);
     392    if IP < Instructions.Count then begin
     393      with TInstruction(Instructions[IP]) do
     394      if Assigned(InstructionHandlers[Opcode]) then
     395        InstructionHandlers[Opcode]
     396        else raise Exception.Create('Unsupported instruction opcode ' + OpcodeString[Opcode]);
     397    end else raise Exception.Create('Reached end of program memory');
    375398    Inc(IP);
     399  end;
     400end;
     401
     402procedure TMachine.Start;
     403begin
     404  if not Running then begin
     405    Terminated := False;
     406    Thread := TMachineThread.Create(True);
     407    Thread.Machine := Self;
     408    Thread.Start;
     409    FRunning := True;
     410  end;
     411end;
     412
     413procedure TMachine.Stop;
     414begin
     415  if Running then begin
     416    Terminated := True;
     417    Thread.Terminate;
     418    Thread.WaitFor;
     419    FreeAndNil(Thread);
     420    FRunning := False;
    376421  end;
    377422end;
Note: See TracChangeset for help on using the changeset viewer.