# записать в файл биос с диска (если он там есть)

use Fcntl;

$Debug = 0;
#$SrcName="cpm1.kdi";
#$SrcName="mdos5.kdi";

if ($ARGV[0] eq "") {
  die "usage: GetBios filename.kdi\n";
}

$SrcName = $ARGV[0];
$SrcName =~ m/(.*)\.kdi/i;
$DstName = "$1\.kdi_";

 print "dbg: name <$SrcName> -> <$DstName>\n" if ($Debug);

open Fkdi,"<$SrcName" or die "Can't open source file $SrcName\n";
binmode Fkdi;

seek(Fkdi,0,SEEK_SET);

read(Fkdi,$InfSector,0x80) or die "error read info sector\n";

( # S         S        S
   $LoadAddr,$RunAddr,$Count,
  # C         C        C    C           C        C       S            S
   $SizeDisk,$Density,$TpI,$SkewFactor,$SecSize,$InSide,$SecPerTrack,$TrkPerDisk,
  # S    C    C    C    S    S    C    C    S
   $SPT,$BSH,$BLM,$EXM,$DSM,$DRM,$AL0,$AL1,$CKS,
  # S    C
   $OFS,$CRC)=unpack("SSSCCCCCCSSSCCCSSCCSSC",$InfSector);

 printf ("%04x\t%04x\t%d\t%d\t%d\n",$LoadAddr,$RunAddr,$Count,$OFS,128<<$SecSize) if ($Debug);;


 @a=unpack("C"x31,$InfSector);
 $sum=0x66;for ($i=0;$i<31;$i++) {$sum+=$a[$i];};$sum&=0xff;
 die "Err: info sector crc error" if ($sum != $CRC) ;


seek( Fkdi,0x80,SEEK_SET );
read( Fkdi,$BUF,((128<<$SecSize)*$Count)-128 ) or die "error read bios body\n";
close Fsrc;

open Fbios,">$DstName"."InfSector";
binmode Fbios;
print Fbios $InfSector;
close Fbios;

open Fbios,">$DstName"."bios";
binmode Fbios;
print Fbios $BUF;
close Fbios;


open Fbios,">$DstName"."AllData";
binmode Fbios;
print Fbios $InfSector;
print Fbios $BUF;
close Fbios;

open Finfo,">$DstName"."cfg";
printf Finfo "LoadAddr : %04X\nRunAddr  : %04X\nSecCount : %4d\nSecSize  : %4d\n",$LoadAddr,$RunAddr,$Count,(128<<$SecSize);
close Finfo;


