program dspconve;
{ This program converts a ADSP2181 - EXE file in an asm - file for x86,
  to include the EXE in binary format as far data segment to host program. }
{ Feel free to use the function in you application
{ Rev. 1  05/04/2000 created by Michael Roch, SPEKTRA GmbH, Germany Dresden }

uses dos,crt;

type
     listeleptr = ^listele;

     listele = record
               next: listeleptr;
               st  : string[20];
               end;


var
  quelle : text;    { sourcefile }
  ziel   : text;    { destination - textfile }
  str,quellname,zielname : string;
  Path   : PathStr;
  Dir    : DirStr;
  Name   : NameStr;
  Ext    : ExtStr;
  i      : word;
  zeilenanfang : boolean;
  liste, akt, last : listeleptr;
  recanz : word;
  blockende : boolean;

const
  convstr:string[16]='0123456789ABCDEF';
  initstranz = 4;
  inistrfeld : array [1..initstranz] of string[128]=(
  ';This Modul is automaic generated in program DSPCONV and contains the source ',
  ';from ADSP2181 exe file to build an far data segment in binary form ',
  ';starts on label _"filename"DSPCODE ',
  '');

begin
writeln('ADSP 2181 - EXE to ASM convertion program');
writeln;
{$I-}
if paramcount = 0 then begin
  writeln('error - no filename in parameter');
  halt(3);
  end;
Path:=paramstr(1);
fsplit(Path,Dir,Name,Ext);
if (Name = '') or (length(name)>8) then begin
  writeln('error - incorrect filename: ',Path);
  halt(3);
  end;
if ext='' then ext:='.EXE';
assign(quelle,dir+name+ext);
if ioresult <>0 then begin
  writeln('error - file not found: ',dir+name+ext);
  halt(3);
  end;
reset(quelle);
if ioresult <>0 then begin
  writeln('error - file not found: ',dir+name+ext);
  halt(3);
  end;
assign(ziel,name+'.ASM');
if ioresult <>0 then begin
  writeln('error - incorrect filename: ',name+'.ASM');
  halt(3);
  end;
rewrite(ziel);
if ioresult <>0 then begin
  writeln('error - file access not possible: ',name+'.ASM');
  halt(3);
  end;
{$I+}

for i:=1 to length(name) do name[i]:=upcase(name[i]);

writeln(ziel,';This Modul is automaic generated in program DSPCONV and contains the source ');
writeln(ziel,';from ADSP2181 exe file to build an far data segment in binary form ');
writeln(ziel,';starts on label _',name,'_DSPCODE ');
writeln(ziel);
writeln(ziel,#9'PUBLIC'#9'_',name,'_DSPCODE');
writeln(ziel,name,#9'segment'#9'para public ''CODE'' ');
writeln(ziel,'_',name,'_DSPCODE:');
writeln(ziel);

readln(quelle,str);
if pos(#$1b#$1b'i',str)=0 then begin
  writeln('error: invalid filestructure');
  close(ziel);
  halt(3);
  end;
writeln(ziel,#9'db'#9#39,str,#39);
readln(quelle,str);
while (pos('@PA',str)<>0) or (pos('@DA',str)<>0) do begin
  writeln(ziel,#9'db'#9#39,str,#39);
  new(liste);
  akt:=liste;
  akt^.next:=nil;
  readln(quelle,akt^.st);
  recanz:=0;
  blockende:=false;
  repeat
    readln(quelle,str);
    if str[1]='#' then begin
      blockende:=true;
      delete(str,1,4);        { clear '#123' }
      end
    else inc(recanz);
    new(last);
    last^.next:=nil;
    akt^.next:=last;
    akt:=last;
    akt^.st:=str;
    until blockende;
  str:='';
  for i:=3 downto 0 do str:=str+convstr[((recanz shr (i*4))and $f)+1];
  new(last);
  last^.next:=liste^.next;    { length to 2nd position in list }
  liste^.next:=last;
  last^.st:=str;
  while liste<>nil do begin
    str:=#9'db'#9;
    zeilenanfang:=true;
    i:=1;
    while i<(length(liste^.st)+1) do begin
      if (length(liste^.st)-i+1)>=2 then begin
        if (liste^.st[i] in ['0'..'9','a'..'f','A'..'F']) and (liste^.st[i+1] in ['0'..'9','a'..'f','A'..'F']) then begin
          if not zeilenanfang then str:=str+',';
          str:=str+'0'+liste^.st[i]+liste^.st[i+1]+'h';
          zeilenanfang:=false;
          inc(i,2);
          end
        else begin
          writeln('error in sourcefile - incorrect character ',liste^.st);
          close(ziel);
          halt(3);
          end;
        end
      else begin
        writeln('error in sourcefile - bad length of string ',liste^.st);
        close(ziel);
        halt(3);
        end;
      end;
    writeln(ziel,str);
    akt:=liste;
    liste:=liste^.next;
    dispose(akt);
    end;
  readln(quelle,str);
  end;
if pos(#$1b#$1b'o',str)=0 then begin
  writeln('incorrect end of sourcefile: ',str);
  close(ziel);
  halt(3);
  end;
writeln(ziel,#9'db'#9#39,str,#39);
writeln(ziel);
writeln(ziel,name,#9'ends');
writeln(ziel);
writeln(ziel,#9'end');

close(ziel);
if ioresult <>0 then begin
  writeln('Fehler - File konnte nicht geschlossen werden: ',name+'.ASM');
  halt(3);
  end;

end.





