Changeset 89 for branches/virt simple


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:
6 edited

Legend:

Unmodified
Added
Removed
  • branches/virt simple

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

    r88 r89  
    5353  OutValue: Integer;
    5454begin
     55  Line := Trim(Line);
     56  // Strip comments
     57  if Pos(';', Line) > 0 then begin
     58    Line := Trim(Copy(Line, 1, Pos(';', Line) - 1));
     59  end;
     60  if Line = '' then Exit;
    5561  Name := Trim(GetPart(Line, ' '));
    5662  OpcodeIndex := GetOpcodeFromName(Name);
  • branches/virt simple/UFormMain.lfm

    r88 r89  
    150150    Top = 31
    151151    Width = 502
     152    Font.Name = 'Liberation Mono'
     153    ParentFont = False
    152154    ScrollBars = ssAutoBoth
    153155    TabOrder = 5
     
    164166    Left = 532
    165167    Height = 25
    166     Top = 759
     168    Top = 760
    167169    Width = 100
    168170    Caption = 'Compile'
    169171    OnClick = ButtonCompileClick
    170172    TabOrder = 6
     173  end
     174  object ButtonStart: TButton
     175    Left = 664
     176    Height = 25
     177    Top = 760
     178    Width = 100
     179    Caption = 'Start'
     180    OnClick = ButtonStartClick
     181    TabOrder = 7
     182  end
     183  object ButtonStop: TButton
     184    Left = 784
     185    Height = 25
     186    Top = 760
     187    Width = 100
     188    Caption = 'Stop'
     189    OnClick = ButtonStopClick
     190    TabOrder = 8
    171191  end
    172192  object Timer1: TTimer
     
    176196    top = 205
    177197  end
     198  object ActionList1: TActionList
     199    left = 1051
     200    top = 808
     201    object ASave: TAction
     202      Caption = 'Save'
     203      OnExecute = ASaveExecute
     204      ShortCut = 16467
     205    end
     206  end
    178207end
  • branches/virt simple/UFormMain.pas

    r88 r89  
    77uses
    88  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ComCtrls,
    9   ExtCtrls, StdCtrls, fgl, UMachine, UBigInt, UCompiler;
     9  ExtCtrls, StdCtrls, ActnList, fgl, UMachine, UBigInt, UCompiler;
    1010
    1111type
    1212
    13   { TMachineThread }
    14 
    15   TMachineThread = class(TThread)
    16     Machine: TMachine;
    17     procedure Execute; override;
    18   end;
    19 
    2013{ TForm1 }
    2114
    2215  TForm1 = class(TForm)
     16    ASave: TAction;
     17    ActionList1: TActionList;
    2318    ButtonCompile: TButton;
     19    ButtonStart: TButton;
     20    ButtonStop: TButton;
    2421    Label1: TLabel;
    2522    Label2: TLabel;
     
    3532    MemoOutput: TMemo;
    3633    Timer1: TTimer;
     34    procedure ASaveExecute(Sender: TObject);
    3735    procedure ButtonCompileClick(Sender: TObject);
     36    procedure ButtonStartClick(Sender: TObject);
     37    procedure ButtonStopClick(Sender: TObject);
    3838    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
    3939    procedure FormCreate(Sender: TObject);
     
    4646    procedure Timer1Timer(Sender: TObject);
    4747  private
    48     MachineThread: TMachineThread;
    4948    Console: string;
    5049    procedure ReloadMemory;
    5150    procedure DoSystemCall(Index: Integer);
     51    procedure UpdateInterface;
    5252  public
    5353    Machine: TMachine;
     
    6565{$R *.lfm}
    6666
    67 { TMachineThread }
    68 
    69 procedure TMachineThread.Execute;
    70 begin
    71   Machine.Run;
    72 end;
    73 
    7467{ TForm1 }
    7568
     
    8376procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);
    8477begin
    85   Machine.Terminated := True;
    86   MachineThread.WaitFor;
    87   MemoSource.Lines.SaveToFile(DefaultFileName);
     78  Machine.Stop;
     79  ASave.Execute;
    8880end;
    8981
     
    9486end;
    9587
     88procedure TForm1.ASaveExecute(Sender: TObject);
     89begin
     90  MemoSource.Lines.SaveToFile(DefaultFileName);
     91end;
     92
     93procedure TForm1.ButtonStartClick(Sender: TObject);
     94begin
     95  Machine.Start;
     96  UpdateInterface;
     97end;
     98
     99procedure TForm1.ButtonStopClick(Sender: TObject);
     100begin
     101  Machine.Stop;
     102  UpdateInterface;
     103end;
     104
    96105procedure TForm1.FormDestroy(Sender: TObject);
    97106begin
     
    104113  MemoSource.Lines.LoadFromFile(DefaultFileName);
    105114  ReloadMemory;
     115  UpdateInterface;
    106116  with Machine do begin
    107117    Init(32000, 16);
    108     with Instructions do begin
     118{    with Instructions do begin
    109119      AddInst(opNoOperation);
    110120      AddInst(opIncrement, ptRegister, False, 0);
     
    122132      AddInst(opHalt);
    123133    end;
    124     MachineThread := TMachineThread.Create(True);
    125     MachineThread.Machine := Machine;
    126     MachineThread.Start;
     134    }
    127135  end;
    128136end;
     
    200208end;
    201209
     210procedure TForm1.UpdateInterface;
     211begin
     212  ButtonStart.Enabled := not Machine.Running;
     213  ButtonStop.Enabled := Machine.Running;
     214end;
     215
    202216end.
    203217
  • 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;
  • branches/virt simple/example.vasm

    r88 r89  
    66  PUSH -1
    77  POP [10]
    8   JR 0
     8  LD R0, 65
     9  SYS 0          ; Output character
     10  LD R0, 1000
     11  SYS 2          ; Delay 1 second
     12  JP 0
    913  LD [R4], [R3]
    1014  HALT
Note: See TracChangeset for help on using the changeset viewer.