新しい記事を書く事で広告が消せます。
スポンサーサイト
PICを作ろう ,インテルHEXを読む(5) ~昼休みにまったり進めるプロジェクト~
文字数制限に引っかかってしまうので、肝心なところだけ
//
// (c)2007 BakaOyaji
begin : readandwrite
ifp = $fopen( infile_name, "r" ) ;
if( ifp == 0 ) begin
error_code = `IHEX_ERROR_READOPEN;
disable readandwrite ;
end // if
ofp = $fopen( outfile_name, "w" ) ;
if( ofp == 0 ) begin
error_code = `IHEX_ERROR_WRITEOPEN;
disable readandwrite ;
end
byteperword = (bitperword+7)/8 ; // need to check the varid range
forever begin
// perform a line
if( $fgets( linebuf , ifp) == c_EOF ) begin
error_code = `IHEX_ERROR_UNEXPECTED_EOF ;
disable readandwrite ;
end
begin : find_comma // find ':'
for( i = 0 ; i < `IHEX_MAXSTRLENGTH ; i = i+1 ) begin
if( linebuf [(i*8) +:8] == ":" ) begin
disable find_comma ; // found
end // if
if( linebuf [(i*8) +:8] == 8'h00 ) begin
error_code = `IHEX_ERROR_UNEXPECTEDFORMAT ;
disable readandwrite ;
end // if
end // for
// line is too long
error_code = `IHEX_ERROR_UNEXPECTEDFORMAT ;
disable readandwrite ;
end // find_comma
i = i -8 ;
if( i < 2 ) begin
error_code = `IHEX_ERROR_UNEXPECTEDFORMAT ;
disable readandwrite ;
end // if
record_type = hex2int2( linebuf[(i*8) +: 16 ]) ;
offset = hex2int4( linebuf[((i+2)*8) +: 32] ) ;
record_length = hex2int2( linebuf[((i+6)*8) +: 16] ) ;
if( ( record_length == -1 )
|| (offset == -1 )
|| (record_type == -1 ) ) begin
error_code = `IHEX_ERROR_UNEXPECTEDFORMAT ;
disable readandwrite ;
end // if
case( record_type )
`IHEX_REC_DATA : begin
$fwrite( ofp , "@%4h ", (segment + offset)/byteperword ) ;
repeat( record_length / byteperword ) begin
i = i - 2 * byteperword ;
if( i < 0 ) begin
error_code = `IHEX_ERROR_UNEXPECTEDFORMAT ;
disable readandwrite ;
end // if
j = i ;
repeat( byteperword ) begin // put a word
$fwrite( ofp , "%2s", linebuf[(j*8) +: 16] ) ;
j = j+2 ;
end // repeat
$fwrite( ofp , " ");
end // repeat
$fwrite( ofp, "\n" ) ;
end
:
:
endcase
end // forever
end // readandwrite