
(* programme pour traduire un Pcode en clair : version MIAS 1999 *)


uses crt, dos ;

const 
	nbTPI 	=	27 ;
	TPI : 	array[1..nbTPI] of string[8]
		=	(
			'ADD     ',	'SUB     ',	'MUL     ',	
			'NEG     ',	'ET      ',	'OU      ',	
			'LNEG    ',	'EQU     ',	'GEQ     ',
			'LEQ     ',	'LES     ',	'GRT     ',	
			'NEQ     ',	'LDC     ',	'LDO     ',
			'STO     ',	'MOV     ',	'CHK     ',	
			'PECRIRE ',	'PLIRE   ',	'UJP     ',	
			'FJP     ',	'INIT    ',	'RES     ',	
			'RET     ',	'ERREUR  ',	'PECRIREC'
			) ;

Type
	pinstruction = record
		operation : integer ;
		argument : longint
	end ;

Var
	pcode : file of pinstruction ;	(* fichier a traduire *)

	voperation : integer ;
	vargument : longint ;

	fmap : text ;		(* fichier texte de sortie *)

	fichier : PathStr ;             (* types definis dans dos *)
	repertoire : DirStr ;
	nom : NameStr ;
	extension : ExtStr ;


function position : longint ;
begin
	position := filepos( pcode )
end ;

procedure consulter ; 		(* lire une ligne du Pcode *)
var v : pinstruction ;
begin
	read( pcode, v ) ;
	voperation := v.operation ;
	vargument := v.argument
end ;

procedure ecrirearg ;
begin
	if 	(
		((voperation = 23) and (vargument <> 0)) or  (* INIT *)
		(voperation =  14) or 	(* LDC *)
		(voperation =  15) or 	(* LDO *)
		(voperation =  18) or 	(* CHK *)
		(voperation =  21) or 	(* UJP *)
		(voperation =  22) or 	(* FJP *)
		(voperation =  24) or 	(* RES *)
		(voperation =  27)	(* PECRIREC *)
		) then write(fmap, ' ', vargument ) 
end ;  




begin
	
	if paramcount = 0 then begin
		writeln( 'Commande : PCodeMap Fichier[.PCO]' ) ;
		halt( 1 )
	end ;

	fichier := fexpand( paramstr( 1 ) ) ;
	fsplit( fichier, repertoire, nom, extension ) ;

	if nom + extension = '' then begin
		writeln( 'Commande : PCodeMap Fichier[.PCO]' ) ;
		halt( 1 )
	end ;

	if extension = '' then extension := '.PCO' ;

	{$I-}
	assign( pcode, repertoire + nom + extension ) ;
	reset( pcode ) ;
	{$I+}

	if IOResult <> 0 then begin
		write('Impossible d''ouvrir le fichier : ' ) ;
		writeln( repertoire + nom + extension ) ;
		halt( 1 )
	end ;

	if eof( pcode ) then begin
		writeln( 'Fichier vide !' ) ;
		halt( 1 )
	end ;

	{$I-}
	assign( fmap, repertoire + nom + '.MAP' ) ;
	rewrite( fmap ) ;
	{$I+}

	if IOResult <> 0 then begin
		write('Impossible d''ouvrir un fichier : ' ) ;
		writeln( repertoire + nom + '.MAP' ) ;
		halt( 1 )
	end ;

	while not eof( pcode ) do begin    (* la boucle principale *)
		consulter ;
		write( fmap, position - 1 : 12 ) ;
		write( fmap, ' : ' ) ;
		write( fmap,  TPI[voperation] ) ;
		ecrirearg ;
		writeln(fmap, '' )
	end (* while not eof( pcode ) *) ;
	close( pcode ) ;
	close( fmap ) ;
	writeln ;
	write( '(Pcode en clair dans le fichier : ' ) ;
	writeln( repertoire, nom, '.MAP)')
end.