unit frmConfig;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls, ExtCtrls;

type
  TConfigForm = class (TForm)
    Button1:        TButton;
    Button2:        TButton;
    Button3:        TButton;
    Panel1:         TPanel;
    PageControl1:   TPageControl;
    TabSheet1:      TTabSheet;
    TabSheet2:      TTabSheet;
    TabSheet3:      TTabSheet;
    lbBoot:         TComboBox;
    Label2:         TLabel;
    Label1:         TLabel;
    lbMemory:       TEdit;
    lbSectors:      TEdit;
    Label7:         TLabel;
    Label6:         TLabel;
    lbHeads:        TEdit;
    lbCylinder:     TEdit;
    Label5:         TLabel;
    lbHDDFile:      TEdit;
    Label4:         TLabel;
    lbHDDPresent:   TCheckBox;
    lbDiskSize:     TEdit;
    Label10:        TLabel;
    lbFloppyFile:   TEdit;
    Label9:         TLabel;
    lbFloppyType:   TComboBox;
    Label3:         TLabel;
    TabSheet4:      TTabSheet;
    Button6:        TButton;
    Label8:         TLabel;
    lbCDROMFile:    TEdit;
    lbCDRomPresent: TCheckBox;
    Button5:        TButton;
    Button4:        TButton;
    Button7:        TButton;
    Button8:        TButton;
    lbFPU:          TCheckBox;
    lbCDROMDrive:   TComboBox;
    Label11:        TLabel;
    lbFloppyDrive:  TComboBox;
    Label12:        TLabel;
    Button9:        TButton;
    procedure Button3Click (Sender: TObject);
    procedure FormActivate (Sender: TObject);
    procedure Button2Click (Sender: TObject);
    procedure lbCylinderChange (Sender: TObject);
    procedure Button4Click (Sender: TObject);
    procedure Button5Click (Sender: TObject);
    procedure Button6Click (Sender: TObject);
    procedure Button7Click (Sender: TObject);
    procedure Button8Click (Sender: TObject);
    procedure lbCDROMDriveSelect (Sender: TObject);
    procedure lbFloppyDriveSelect (Sender: TObject);
    procedure Button9Click (Sender: TObject);
  private
    { Private declarations }
  public
    procedure createEmptyFile (fileName: string; sizeFile: int64);
  end;

var
  ConfigForm: TConfigForm;

implementation

uses CONFIG, m2fMain, CreateIsoForm;

{$R *.dfm}

procedure TConfigForm.Button2Click (Sender: TObject);
  begin
    VMConfig.Memory  := StrToIntDef(lbMemory.Text, 32);
    VMConfig.BootDevice := lbBoot.ItemIndex;
    VMConfig.FloppyType := lbFloppyType.ItemIndex + 10;
    VMConfig.FloppyFile := lbFloppyFile.Text;
    VMConfig.HddPresent := lbHDDPresent.Checked;
    VMConfig.HddCylinder := StrToIntDef(lbCylinder.Text, 306);
    VMConfig.HddHeads := StrToIntDef(lbHeads.Text, 4);
    VMConfig.HddSectors := StrToIntDef(lbSectors.Text, 17);
    VMConfig.HddFile := lbHDDFile.Text;
    VMConfig.CdPresent := lbCDRomPresent.Checked;
    VMConfig.CdFile  := lbCDROMFile.Text;
    VMConfig.usefpu  := lbFPU.Checked;
    fMain.SaveVMConfig;

    fMain.LoadVMConfig;
    Button3.Click;
  end;

procedure TConfigForm.Button3Click (Sender: TObject);
  begin
    lbMemory.Text := Format('%d', [VMConfig.Memory]);
    lbBoot.ItemIndex := VMConfig.BootDevice;
    lbFloppyType.ItemIndex := VMConfig.FloppyType - 10;
    lbFloppyFile.Text := VMConfig.FloppyFile;
    lbHDDPresent.Checked := VMConfig.HddPresent;
    lbCylinder.Text := Format('%d', [VMConfig.HddCylinder]);
    lbHeads.Text  := Format('%d', [VMConfig.HddHeads]);
    lbSectors.Text := Format('%d', [VMConfig.HddSectors]);
    lbHDDFile.Text := VMConfig.HddFile;
    lbCDRomPresent.Checked := VMConfig.CdPresent;
    lbCDROMFile.Text := VMConfig.CdFile;
    lbFPU.Checked := VMConfig.usefpu;

    lbCylinderChange(nil);
  end;

procedure TConfigForm.Button4Click (Sender: TObject);
  var
    OD: TOpenDialog;
  begin

    OD := TOpenDialog.Create(nil);
    OD.Title := 'Select image file';
    od.Filter := '*.img|*.img|*.iso|*.iso|*.ima|*.ima';


    if OD.Execute then
      begin
      lbHDDFile.Text := OD.FileName;
      end;

    OD.Free;

  end;

procedure TConfigForm.Button5Click (Sender: TObject);
  var
    OD: TOpenDialog;
  begin

    OD := TOpenDialog.Create(nil);
    OD.Title := 'Select image file';
    od.Filter := '*.img|*.img|*.iso|*.iso|*.ima|*.ima';


    if OD.Execute then
      begin
      lbFloppyFile.Text := OD.FileName;
      end;

    OD.Free;
  end;

procedure TConfigForm.Button6Click (Sender: TObject);
  var
    OD: TOpenDialog;
  begin

    OD := TOpenDialog.Create(nil);
    OD.Title := 'Select image file';
    od.Filter := '*.img|*.img|*.iso|*.iso|*.ima|*.ima';


    if OD.Execute then
      begin
      lbCDROMFile.Text := OD.FileName;
      end;

    OD.Free;
  end;

procedure TConfigForm.Button7Click (Sender: TObject);
  var
    bytes: integer;
  begin
    bytes := StrToIntDef(lbCylinder.Text, 306) * StrToIntDef(lbHeads.Text, 4) *
      StrToIntDef(lbSectors.Text, 17) * BYTES_PER_SECTOR;

    if MessageDlg(Format('Создать пустой образ диска %s?',
      [ExtractFileName(lbHDDFile.Text)]), mtConfirmation, [mbYes, mbNo], 0) = idYes then
      CreateEmptyFile(lbHDDFile.Text, bytes);
  end;

procedure TConfigForm.Button8Click (Sender: TObject);
  begin
    FormCreateIso := TFormCreateIso.Create(Self);
    FormCreateIso.ShowModal;
    FormCreateIso.Free;
  end;

procedure TConfigForm.Button9Click (Sender: TObject);
  begin
    if MessageDlg(Format('Создать пустой образ диска %s?',
      [ExtractFileName(lbFloppyFile.Text)]), mtConfirmation, [mbYes, mbNo], 0) =
      idYes then
      CreateEmptyFile(lbFLoppyFile.Text, 1474560);
  end;

procedure TConfigForm.createEmptyFile (fileName: string; sizeFile: int64);
  var
    countWrite, i: integer;
    zeroBuffer: array [0..511] of byte;
    f, modval:  integer;
  begin
    for i := 0 to 511 do
      zeroBuffer[i] := 0;

    f := FileCreate(filename);


    countWrite := sizeFile div 512;
    ModVal := sizeFile mod 512;

    for I := 1 to countWrite do
      begin
      FileWrite(f, zeroBuffer[0], 512);
      end;

    if modval <> 0 then
      FileWrite(f, zeroBuffer[0], modval);


    FileClose(f);

    ShowMessage(Format('Файл %s был создан', [filename]));
  end;

procedure TConfigForm.FormActivate (Sender: TObject);
  var
    dr, i, dt: integer;
  begin
    onactivate := nil;

    dr := getlogicaldrives;
    for i := 0 to 31 do
      if (dr shr i) and 1 = 1 then
        begin
        dt := getdrivetype(PChar(char(byte('A') + i) + ':\'));
        if (dt = DRIVE_CDROM) then
          lbCDROMDrive.Items.Append(char(byte('A') + i))
        else
          if (dt = DRIVE_REMOVABLE) then
            lbFloppyDrive.Items.Append(char(byte('A') + i));

        end;

    PageControl1.ActivePageIndex := 0;
    Button3.Click;
  end;

procedure TConfigForm.lbCDROMDriveSelect (Sender: TObject);
  begin
    lbCDROMFile.Text := Format('\\.\CDRom%d', [(Sender as TComboBox).ItemIndex]);
  end;

procedure TConfigForm.lbCylinderChange (Sender: TObject);
  var
    bytes: integer;
  begin

    bytes := StrToIntDef(lbCylinder.Text, 306) * StrToIntDef(lbHeads.Text, 4) *
      StrToIntDef(lbSectors.Text, 17) * BYTES_PER_SECTOR;
    LbDiskSize.Text := Format('%.f Mb', [bytes / 1024 / 1024]);

  end;

procedure TConfigForm.lbFloppyDriveSelect (Sender: TObject);
  begin
    lbFloppyFile.Text := Format('\\.\%s:',
      [(Sender as TComboBox).Items[(Sender as TComboBox).ItemIndex]]);
  end;

end.
