Ignore:
Timestamp:
Sep 22, 2019, 7:13:15 PM (5 years ago)
Author:
chronos
Message:
  • Modified: Address CPU memory as pointer instead array of bytes.
  • Added: TMachine class to represent virtual machine itself.
Location:
branches/virtcpu varint
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/virtcpu varint

    • Property svn:ignore set to
      lib
      virtcpu
      virtcpu.lps
  • branches/virtcpu varint/UFormMain.pas

    r195 r196  
    1111type
    1212
    13   { TForm1 }
    14 
    15   TForm1 = class(TForm)
     13  { TFormMain }
     14
     15  TFormMain = class(TForm)
    1616    Button1: TButton;
    1717    Button2: TButton;
     
    3838    procedure CpuOutput(Port, Value: T);
    3939  public
    40     Cpu: TCPU;
     40    Machine: TMachine;
    4141    InstructionWriter: TInstructionWriter;
    4242  end;
    4343
    4444var
    45   Form1: TForm1;
     45  FormMain: TFormMain;
    4646
    4747const
     
    5353{$R *.lfm}
    5454
    55 { TForm1 }
    56 
    57 procedure TForm1.FormShow(Sender: TObject);
     55{ TFormMain }
     56
     57procedure TFormMain.FormShow(Sender: TObject);
    5858var
    5959  LabelStart: Integer;
     
    104104end;
    105105
    106 procedure TForm1.FormDestroy(Sender: TObject);
    107 begin
    108   InstructionWriter.Free;
    109   Cpu.Free;
    110 end;
    111 
    112 procedure TForm1.FormKeyPress(Sender: TObject; var Key: char);
    113 begin
    114 end;
    115 
    116 procedure TForm1.FormCreate(Sender: TObject);
    117 begin
    118   Cpu := TCPU.Create(nil);
    119   Cpu.OnInput := CpuInput;
    120   Cpu.OnOutput := CpuOutput;
     106procedure TFormMain.FormDestroy(Sender: TObject);
     107begin
     108  FreeAndNil(InstructionWriter);
     109  FreeAndNil(Machine);
     110end;
     111
     112procedure TFormMain.FormKeyPress(Sender: TObject; var Key: char);
     113begin
     114end;
     115
     116procedure TFormMain.FormCreate(Sender: TObject);
     117begin
     118  Machine := TMachine.Create(nil);
     119  Machine.Cpu.OnInput := CpuInput;
     120  Machine.Cpu.OnOutput := CpuOutput;
    121121  InstructionWriter := TInstructionWriter.Create;
    122   InstructionWriter.Cpu := Cpu;
    123 end;
    124 
    125 procedure TForm1.Button1Click(Sender: TObject);
    126 begin
    127   Cpu.Start;
    128 end;
    129 
    130 procedure TForm1.Button2Click(Sender: TObject);
    131 begin
    132   Cpu.Stop;
    133 end;
    134 
    135 procedure TForm1.ListViewMemoryData(Sender: TObject; Item: TListItem);
     122  InstructionWriter.Cpu := Machine.Cpu;
     123end;
     124
     125procedure TFormMain.Button1Click(Sender: TObject);
     126begin
     127  Machine.Cpu.Start;
     128end;
     129
     130procedure TFormMain.Button2Click(Sender: TObject);
     131begin
     132  Machine.Cpu.Stop;
     133end;
     134
     135procedure TFormMain.ListViewMemoryData(Sender: TObject; Item: TListItem);
    136136var
    137137  Line: string;
    138138  I: Integer;
    139139begin
    140   if Item.Index < Length(Cpu.Memory) div ItemsPerLine then begin
     140  if Item.Index < Machine.MemorySize div ItemsPerLine then begin
    141141    Line := '';
    142142    for I := 0 to ItemsPerLine - 1 do
    143       Line := Line + IntToHex(Cpu.Memory[Item.Index * ItemsPerLine + I], 2) + ' ';
     143      Line := Line + IntToHex(PByte(NativeUInt(Machine.Memory) + Item.Index * ItemsPerLine + I)^, 2) + ' ';
    144144    Item.Caption := IntToHex(Item.Index * ItemsPerLine, 8);
    145145    Item.SubItems.Add(Line);
     
    147147end;
    148148
    149 procedure TForm1.ListViewRegistersData(Sender: TObject; Item: TListItem);
    150 begin
    151   if Item.Index < Length(Cpu.Registers) + 1 then begin
     149procedure TFormMain.ListViewRegistersData(Sender: TObject; Item: TListItem);
     150begin
     151  if Item.Index < Length(Machine.Cpu.Registers) + 1 then begin
    152152    if Item.Index = 0 then begin
    153153      Item.Caption := 'IP';
    154       Item.SubItems.Add(IntToHex(Int64(Cpu.IP), 8));
     154      Item.SubItems.Add(IntToHex(Int64(Machine.Cpu.IP), 8));
    155155    end else
    156156    if Item.Index = 1 then begin
    157157      Item.Caption := 'SP';
    158       Item.SubItems.Add(IntToHex(Int64(Cpu.SP), 8));
     158      Item.SubItems.Add(IntToHex(Int64(Machine.Cpu.SP), 8));
    159159    end else begin
    160160      Item.Caption := 'R' + IntToStr(Item.Index - 2);
    161       Item.SubItems.Add(IntToHex(Int64(Cpu.Registers[Item.Index - 2]), 8));
     161      Item.SubItems.Add(IntToHex(Int64(Machine.Cpu.Registers[Item.Index - 2]), 8));
    162162    end;
    163163  end;
    164164end;
    165165
    166 procedure TForm1.Memo1KeyPress(Sender: TObject; var Key: char);
     166procedure TFormMain.Memo1KeyPress(Sender: TObject; var Key: char);
    167167begin
    168168  SetLength(KeyInputBuffer, Length(KeyInputBuffer) + 1);
     
    170170end;
    171171
    172 procedure TForm1.Timer1Timer(Sender: TObject);
     172procedure TFormMain.Timer1Timer(Sender: TObject);
    173173begin
    174174  ReloadMemoryDump;
    175175  ReloadRegisterDump;
    176   LabelTicks.Caption := 'Ticks: ' + IntToStr(Cpu.Ticks);
    177 end;
    178 
    179 procedure TForm1.ReloadMemoryDump;
    180 begin
    181   ListViewMemory.Items.Count := Length(Cpu.Memory) div ItemsPerLine;
     176  LabelTicks.Caption := 'Ticks: ' + IntToStr(Machine.Cpu.Ticks);
     177end;
     178
     179procedure TFormMain.ReloadMemoryDump;
     180begin
     181  ListViewMemory.Items.Count := Machine.MemorySize div ItemsPerLine;
    182182  ListViewMemory.Refresh;
    183183end;
    184184
    185 procedure TForm1.ReloadRegisterDump;
    186 begin
    187   ListViewRegisters.Items.Count := Length(Cpu.Registers);
     185procedure TFormMain.ReloadRegisterDump;
     186begin
     187  ListViewRegisters.Items.Count := Length(Machine.Cpu.Registers);
    188188  ListViewRegisters.Refresh;
    189189end;
    190190
    191 function TForm1.CpuInput(Port: T): T;
     191function TFormMain.CpuInput(Port: T): T;
    192192begin
    193193  Result := 0;
    194194  case Integer(Port) of
    195195    0: begin
    196       while (Length(KeyInputBuffer) = 0) and not Cpu.Terminated do begin
     196      while (Length(KeyInputBuffer) = 0) and not Machine.Cpu.Terminated do begin
    197197        Sleep(100);
    198198        Application.ProcessMessages;
     
    208208end;
    209209
    210 procedure TForm1.CpuOutput(Port, Value: T);
     210procedure TFormMain.CpuOutput(Port, Value: T);
    211211begin
    212212  case Integer(Port) of
Note: See TracChangeset for help on using the changeset viewer.