Lösung des Praktikums Systementwurf
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

convert_data.py 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #!/usr/bin/env python3
  2. import os
  3. import sys
  4. import pathlib
  5. from importlib.machinery import SourceFileLoader
  6. def get_test_and_filename( extension ):
  7. filename = sys.argv[ 1 ].replace( '.py', extension )
  8. testname = os.path.basename( sys.argv[ 1 ].replace( '.py', '' ) )
  9. return testname, filename
  10. def convert_to_c( data ):
  11. testname, filename = get_test_and_filename( '.h' )
  12. with open( filename, 'w' ) as f:
  13. f.write( '#pragma once\n' )
  14. f.write( 'float expected_' + testname +'[] = {' )
  15. f.write( '{:e}'.format( data[ 0 ] ) )
  16. for i in data[ 1 : ]:
  17. f.write( ',{:e}'.format( i ) )
  18. f.write( '};\n' )
  19. def convert_to_vhdl( data ):
  20. testname, filename = get_test_and_filename( '.vhd' )
  21. with open( filename, 'w' ) as f:
  22. f.write( 'library ieee;\n' )
  23. f.write( ' use ieee.std_logic_1164.all;\n' )
  24. f.write( '\n' )
  25. f.write( 'library work;\n' )
  26. f.write( ' use work.test_utility.all;\n' )
  27. f.write( '\n' )
  28. f.write( 'package ' + testname + '_data is\n' )
  29. f.write( ' constant expected : real_array( 0 to 1023 ) := (' )
  30. f.write( '{:e}'.format( data[ 0 ] ) )
  31. for i in data[ 1 : ]:
  32. f.write( ',{:e}'.format( i ) )
  33. f.write( ');\n' )
  34. f.write( 'end package ' + testname + '_data;\n' )
  35. def convert():
  36. if ( len( sys.argv ) != 2 ):
  37. raise( Exception( "Usage: {} expected_data.py" ) )
  38. cwd = pathlib.Path( os.getcwd() )
  39. expected_path = cwd.joinpath( sys.argv[ 1 ] )
  40. expected = SourceFileLoader( 'expected', str( expected_path ) ).load_module()
  41. convert_to_c( expected.data )
  42. convert_to_vhdl( expected.data )
  43. def main():
  44. try:
  45. convert()
  46. except Exception as e:
  47. print( e )
  48. if ( __name__ == '__main__' ):
  49. main()