Ignore:
Timestamp:
Sep 22, 2019, 9:31:49 PM (5 years ago)
Author:
chronos
Message:
  • Modified: All parts of virtual machine have own form in Forms subdirectory.
  • Modified: Main form moved to Forms subdirectory.
  • Modified: TCpu class moved to UCpu unit.
  • Added: Assembler and dissasembler forms.
Location:
branches/virtcpu varint/Forms
Files:
1 added
1 moved

Legend:

Unmodified
Added
Removed
  • branches/virtcpu varint/Forms/UFormMain.pas

    r196 r197  
    77uses
    88  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ComCtrls,
    9   StdCtrls, ExtCtrls, UMachine, UInstructionWriter;
     9  StdCtrls, ExtCtrls, UMachine, UInstructionWriter, UCpu;
    1010
    1111type
     
    1414
    1515  TFormMain = class(TForm)
    16     Button1: TButton;
    17     Button2: TButton;
    18     LabelTicks: TLabel;
    19     ListViewMemory: TListView;
    20     ListViewRegisters: TListView;
    21     Memo1: TMemo;
     16    ButtonStart: TButton;
     17    ButtonStop: TButton;
     18    ButtonAssembler: TButton;
     19    ButtonDisassembler: TButton;
     20    ButtonCpuState: TButton;
     21    ButtonScreen: TButton;
     22    ButtonMemory: TButton;
     23    ButtonConsole: TButton;
    2224    Timer1: TTimer;
    23     procedure Button1Click(Sender: TObject);
    24     procedure Button2Click(Sender: TObject);
     25    procedure ButtonStartClick(Sender: TObject);
     26    procedure ButtonStopClick(Sender: TObject);
     27    procedure ButtonAssemblerClick(Sender: TObject);
     28    procedure ButtonConsoleClick(Sender: TObject);
     29    procedure ButtonCpuStateClick(Sender: TObject);
     30    procedure ButtonDisassemblerClick(Sender: TObject);
     31    procedure ButtonMemoryClick(Sender: TObject);
     32    procedure ButtonScreenClick(Sender: TObject);
    2533    procedure FormCreate(Sender: TObject);
    2634    procedure FormDestroy(Sender: TObject);
    27     procedure FormKeyPress(Sender: TObject; var Key: char);
    2835    procedure FormShow(Sender: TObject);
    29     procedure ListViewMemoryData(Sender: TObject; Item: TListItem);
    30     procedure ListViewRegistersData(Sender: TObject; Item: TListItem);
    31     procedure Memo1KeyPress(Sender: TObject; var Key: char);
    32     procedure Timer1Timer(Sender: TObject);
    3336  private
    34     KeyInputBuffer: array of Char;
    35     procedure ReloadMemoryDump;
    36     procedure ReloadRegisterDump;
    37     function CpuInput(Port: T): T;
    38     procedure CpuOutput(Port, Value: T);
    3937  public
    4038    Machine: TMachine;
     
    4543  FormMain: TFormMain;
    4644
    47 const
    48   ItemsPerLine = 16;
    49 
    5045
    5146implementation
    5247
    5348{$R *.lfm}
     49
     50uses
     51  UFormConsole, UFormMemory, UFormScreen, UFormCpuState, UFormAssembler,
     52  UFormDisassembler;
    5453
    5554{ TFormMain }
     
    110109end;
    111110
    112 procedure TFormMain.FormKeyPress(Sender: TObject; var Key: char);
    113 begin
    114 end;
    115 
    116111procedure TFormMain.FormCreate(Sender: TObject);
    117112begin
    118113  Machine := TMachine.Create(nil);
    119   Machine.Cpu.OnInput := CpuInput;
    120   Machine.Cpu.OnOutput := CpuOutput;
    121114  InstructionWriter := TInstructionWriter.Create;
    122   InstructionWriter.Cpu := Machine.Cpu;
    123115end;
    124116
    125 procedure TFormMain.Button1Click(Sender: TObject);
     117procedure TFormMain.ButtonStartClick(Sender: TObject);
    126118begin
    127119  Machine.Cpu.Start;
    128120end;
    129121
    130 procedure TFormMain.Button2Click(Sender: TObject);
     122procedure TFormMain.ButtonStopClick(Sender: TObject);
    131123begin
    132124  Machine.Cpu.Stop;
    133125end;
    134126
    135 procedure TFormMain.ListViewMemoryData(Sender: TObject; Item: TListItem);
    136 var
    137   Line: string;
    138   I: Integer;
     127procedure TFormMain.ButtonAssemblerClick(Sender: TObject);
    139128begin
    140   if Item.Index < Machine.MemorySize div ItemsPerLine then begin
    141     Line := '';
    142     for I := 0 to ItemsPerLine - 1 do
    143       Line := Line + IntToHex(PByte(NativeUInt(Machine.Memory) + Item.Index * ItemsPerLine + I)^, 2) + ' ';
    144     Item.Caption := IntToHex(Item.Index * ItemsPerLine, 8);
    145     Item.SubItems.Add(Line);
    146   end;
     129  if not Assigned(FormAssembler) then
     130    FormAssembler := TFormAssembler.Create(Self);
     131  FormAssembler.Assembler.InstructionWriter.Memory := Machine.Memory;
     132  FormAssembler.Show;
    147133end;
    148134
    149 procedure TFormMain.ListViewRegistersData(Sender: TObject; Item: TListItem);
     135procedure TFormMain.ButtonConsoleClick(Sender: TObject);
    150136begin
    151   if Item.Index < Length(Machine.Cpu.Registers) + 1 then begin
    152     if Item.Index = 0 then begin
    153       Item.Caption := 'IP';
    154       Item.SubItems.Add(IntToHex(Int64(Machine.Cpu.IP), 8));
    155     end else
    156     if Item.Index = 1 then begin
    157       Item.Caption := 'SP';
    158       Item.SubItems.Add(IntToHex(Int64(Machine.Cpu.SP), 8));
    159     end else begin
    160       Item.Caption := 'R' + IntToStr(Item.Index - 2);
    161       Item.SubItems.Add(IntToHex(Int64(Machine.Cpu.Registers[Item.Index - 2]), 8));
    162     end;
    163   end;
     137  if not Assigned(FormConsole) then
     138    FormConsole := TFormConsole.Create(nil);
     139  FormConsole.Machine := Machine;
     140  FormConsole.Show;
    164141end;
    165142
    166 procedure TFormMain.Memo1KeyPress(Sender: TObject; var Key: char);
     143procedure TFormMain.ButtonCpuStateClick(Sender: TObject);
    167144begin
    168   SetLength(KeyInputBuffer, Length(KeyInputBuffer) + 1);
    169   KeyInputBuffer[High(KeyInputBuffer)] := Key;
     145  if not Assigned(FormCpuState) then
     146     FormCpuState := TFormCpuState.Create(Self);
     147   FormCpuState.Machine := Machine;
     148   FormCpuState.Show;
     149   FormCpuState.Reload;
    170150end;
    171151
    172 procedure TFormMain.Timer1Timer(Sender: TObject);
     152procedure TFormMain.ButtonDisassemblerClick(Sender: TObject);
    173153begin
    174   ReloadMemoryDump;
    175   ReloadRegisterDump;
    176   LabelTicks.Caption := 'Ticks: ' + IntToStr(Machine.Cpu.Ticks);
     154  if not Assigned(FormDisassembler) then
     155    FormDisassembler := TFormDisassembler.Create(Self);
     156  FormDisassembler.Disassembler.Cpu := Machine.Cpu;
     157  FormDisassembler.Show;
     158  FormDisassembler.Reload;
    177159end;
    178160
    179 procedure TFormMain.ReloadMemoryDump;
     161procedure TFormMain.ButtonMemoryClick(Sender: TObject);
    180162begin
    181   ListViewMemory.Items.Count := Machine.MemorySize div ItemsPerLine;
    182   ListViewMemory.Refresh;
     163  if not Assigned(FormMemory) then
     164    FormMemory := TFormMemory.Create(Self);
     165  FormMemory.Machine := Machine;
     166  FormMemory.Show;
     167  FormMemory.Reload;
    183168end;
    184169
    185 procedure TFormMain.ReloadRegisterDump;
     170procedure TFormMain.ButtonScreenClick(Sender: TObject);
    186171begin
    187   ListViewRegisters.Items.Count := Length(Machine.Cpu.Registers);
    188   ListViewRegisters.Refresh;
    189 end;
    190 
    191 function TFormMain.CpuInput(Port: T): T;
    192 begin
    193   Result := 0;
    194   case Integer(Port) of
    195     0: begin
    196       while (Length(KeyInputBuffer) = 0) and not Machine.Cpu.Terminated do begin
    197         Sleep(100);
    198         Application.ProcessMessages;
    199       end;
    200       if Length(KeyInputBuffer) > 0 then begin
    201         Result := Ord(KeyInputBuffer[0]);
    202         if Length(KeyInputBuffer) > 1 then
    203           Move(KeyInputBuffer[1], KeyInputBuffer[0], Length(KeyInputBuffer) - 1);
    204         SetLength(KeyInputBuffer, Length(KeyInputBuffer) - 1);
    205       end else Result := 0;
    206     end;
    207   end;
    208 end;
    209 
    210 procedure TFormMain.CpuOutput(Port, Value: T);
    211 begin
    212   case Integer(Port) of
    213     0: Memo1.Lines.Text := Memo1.Lines.Text + Char(Value);
    214   end;
     172  if not Assigned(FormScreen) then
     173    FormScreen := TFormScreen.Create(Self);
     174  FormScreen.Machine := Machine;
     175  FormScreen.Show;
     176  FormScreen.Reload;
    215177end;
    216178
Note: See TracChangeset for help on using the changeset viewer.