Ignore:
Timestamp:
Apr 12, 2019, 12:06:05 AM (6 years ago)
Author:
chronos
Message:
  • Added: Unit UMachine which will cover virtual hardware for virtual CPU.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/virtualcpu4/UFormMain.pas

    r173 r174  
    77uses
    88  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, ExtCtrls,
    9   ComCtrls, UCpu, UInstructionWriter, syncobjs;
     9  ComCtrls, UCpu, UMachine, UInstructionWriter, syncobjs;
    1010
    1111type
     
    3232    procedure Timer1Timer(Sender: TObject);
    3333  private
    34     InputBuffer: string;
    35     OutputBuffer: string;
    36     Lock: TCriticalSection;
    37     function CpuInput(Port: TAddress): TRegister;
    38     procedure CpuOutput(Port: TAddress; Value: TRegister);
    3934    procedure ReloadMemoryDump;
    4035    procedure ReloadRegisterDump;
    4136    procedure LoadProgram;
    4237  public
    43     Cpu: TCpu;
     38    Machine: TMachine;
    4439    InstructionWriter: TInstructionWriter;
    4540  end;
     
    5954procedure TFormMain.ButtonStartClick(Sender: TObject);
    6055begin
    61   Cpu.Start;
     56  Machine.Cpu.Start;
    6257end;
    6358
    6459procedure TFormMain.ButtonStopClick(Sender: TObject);
    6560begin
    66   Cpu.Stop;
     61  Machine.Cpu.Stop;
    6762end;
    6863
    6964procedure TFormMain.FormCreate(Sender: TObject);
    7065begin
    71   Lock := TCriticalSection.Create;
    72   Cpu := TCpu.Create;
    73   Cpu.OnInput := CpuInput;
    74   Cpu.OnOutput := CpuOutput;
    75   Cpu.DataSize := bw16;
    76   Cpu.AddressSize := bw16;
     66  Machine := TMachine.Create;
    7767  InstructionWriter := TInstructionWriter.Create;
    78   InstructionWriter.Cpu := Cpu;
     68  InstructionWriter.Cpu := Machine.Cpu;
    7969end;
    8070
     
    8272begin
    8373  InstructionWriter.Free;
    84   Cpu.Free;
    85   Lock.Free;
     74  Machine.Free;
    8675end;
    8776
     
    9685  I: Integer;
    9786begin
    98   if Item.Index < Length(Cpu.Memory) div ItemsPerLine then begin
     87  if Item.Index < Length(Machine.Memory) div ItemsPerLine then begin
    9988    Line := '';
    10089    for I := 0 to ItemsPerLine - 1 do
    101       Line := Line + IntToHex(Cpu.Memory[Item.Index * ItemsPerLine + I], 2) + ' ';
     90      Line := Line + IntToHex(Machine.Memory[Item.Index * ItemsPerLine + I], 2) + ' ';
    10291    Item.Caption := IntToHex(Item.Index * ItemsPerLine, 8);
    10392    Item.SubItems.Add(Line);
     
    10796procedure TFormMain.ListViewRegistersData(Sender: TObject; Item: TListItem);
    10897begin
    109   if Item.Index < Length(Cpu.Registers) + 2 then begin
     98  if Item.Index < Length(Machine.Cpu.Registers) + 2 then begin
    11099    if Item.Index = 0 then begin
    111100      Item.Caption := 'IP';
    112       Item.SubItems.Add(IntToHex(Cpu.IP, 16));
     101      Item.SubItems.Add(IntToHex(Machine.Cpu.IP, 16));
    113102    end else
    114103    if Item.Index = 1 then begin
    115104      Item.Caption := 'SP';
    116       Item.SubItems.Add(IntToHex(Cpu.SP, 16));
     105      Item.SubItems.Add(IntToHex(Machine.Cpu.SP, 16));
    117106    end else begin
    118107      Item.Caption := 'R' + IntToStr(Item.Index - 2);
    119       Item.SubItems.Add(IntToHex(Cpu.Registers[Item.Index - 2].D, 16));
     108      Item.SubItems.Add(IntToHex(Machine.Cpu.Registers[Item.Index - 2].D, 16));
    120109    end;
    121110  end;
     
    124113procedure TFormMain.Memo1KeyPress(Sender: TObject; var Key: char);
    125114begin
    126   Lock.Acquire;
    127   InputBuffer := InputBuffer + Key;
    128   Lock.Release;
     115  Machine.LockInput.Acquire;
     116  Machine.InputBuffer := Machine.InputBuffer + Key;
     117  Machine.LockInput.Release;
    129118end;
    130119
    131120procedure TFormMain.Timer1Timer(Sender: TObject);
    132121begin
    133   Label1.Caption := 'Ticks: ' + IntToStr(Cpu.Ticks);
     122  Label1.Caption := 'Ticks: ' + IntToStr(Machine.Cpu.Ticks);
    134123  ReloadMemoryDump;
    135124  ReloadRegisterDump;
    136   Lock.Acquire;
    137   Memo1.Lines.Text := Memo1.Lines.Text + OutputBuffer;
    138   SetLength(OutputBuffer, 0);
    139   Lock.Release;
     125  Machine.LockOutput.Acquire;
     126  Memo1.Lines.Text := Memo1.Lines.Text + Machine.OutputBuffer;
     127  SetLength(Machine.OutputBuffer, 0);
     128  Machine.LockOutput.Release;
    140129end;
    141130
    142131procedure TFormMain.ReloadMemoryDump;
    143132begin
    144   ListViewMemory.Items.Count := Length(Cpu.Memory) div ItemsPerLine;
     133  ListViewMemory.Items.Count := Length(Machine.Memory) div ItemsPerLine;
    145134  ListViewMemory.Refresh;
    146135end;
     
    148137procedure TFormMain.ReloadRegisterDump;
    149138begin
    150   ListViewRegisters.Items.Count := Length(Cpu.Registers);
     139  ListViewRegisters.Items.Count := Length(Machine.Cpu.Registers);
    151140  ListViewRegisters.Refresh;
    152141end;
     
    190179end;
    191180
    192 function TFormMain.CpuInput(Port: TAddress): TRegister;
    193 begin
    194   Result.Q := 0;
    195   case Port of
    196     0: begin
    197       Lock.Acquire;
    198       while (Length(InputBuffer) = 0) and not Cpu.Terminated do begin
    199         Lock.Release;
    200         Sleep(100);
    201         Lock.Acquire;
    202       end;
    203       if Length(InputBuffer) > 0 then begin
    204         Result.B := Ord(InputBuffer[1]);
    205         Delete(InputBuffer, 1, 1);
    206       end else Result.B := 0;
    207       Lock.Release;
    208     end;
    209   end;
    210 end;
    211 
    212 procedure TFormMain.CpuOutput(Port: TAddress; Value: TRegister);
    213 begin
    214   case Port of
    215     0: begin
    216       Lock.Acquire;
    217       OutputBuffer := OutputBuffer + Char(Value.B);
    218       Lock.Release;
    219     end;
    220   end;
    221 end;
    222 
    223 
    224 
    225181end.
    226182
Note: See TracChangeset for help on using the changeset viewer.