unit FormMain;

interface

uses
  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, Machine,
  Assembler, FormDisassembler, FormEx, FormMemory;

type

  { TFormMain }

  TFormMain = class(TFormEx)
    Memo1: TMemo;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private
    procedure ConsoleWrite(Sender: TObject);
    function MouseGetPosition: TPoint;
  public
    SubFormDisassembler: TFormDisassembler;
    SubFormMemory: TFormMemory;
    Machine: TMachine;
  end;

var
  FormMain: TFormMain;


implementation

{$R *.lfm}

{ TFormMain }

procedure TFormMain.FormCreate(Sender: TObject);
begin
  Machine := TMachine.Create;
  Machine.Console.OnWrite := ConsoleWrite;
  Machine.Mouse.OnGetPosition := MouseGetPosition;
end;

procedure TFormMain.FormDestroy(Sender: TObject);
begin
  FreeAndNil(SubFormMemory);
  FreeAndNil(SubFormDisassembler);
  FreeAndNil(Machine);
end;

procedure TFormMain.FormShow(Sender: TObject);
var
  Assembler: TAssembler;
  Lines: TStringList;
begin
  Lines := TStringList.Create;
  Lines.LoadFromFile('Example.asm');

  Assembler := TAssembler.Create;
  with Assembler do begin
    Compile(Lines.Text);
    Machine.Memory.CopyFrom(Memory, 0, 0, Memory.Size);
    Free;
  end;

  Lines.Free;

  Machine.Run;

  SubFormDisassembler := TFormDisassembler.Create(nil);
  SubFormDisassembler.Disassembler.Memory := Machine.Memory;
  SubFormDisassembler.Show;

  SubFormMemory := TFormMemory.Create(nil);
  SubFormMemory.Memory := Machine.Memory;
  SubFormMemory.Show;
end;

procedure TFormMain.ConsoleWrite(Sender: TObject);
begin
  Memo1.Lines.Text := Memo1.Lines.Text + Machine.Console.OutputBuffer;
  Machine.Console.OutputBuffer := '';
end;

function TFormMain.MouseGetPosition: TPoint;
begin
  Result := Mouse.CursorPos;
end;

end.

