TrickyDicky
Advanced Member level 7
- Joined
- Jun 7, 2010
- Messages
- 7,110
- Helped
- 2,081
- Reputation
- 4,181
- Reaction score
- 2,048
- Trophy points
- 1,393
- Activity points
- 39,769
1. You should be setting it to signed 16 fixed all the time, as thats what the number is.
2. you can output reals to a text file, but they get written in the scientific format:1.234e7
You'll have to write a custom function to make it more readible:
or just borrow mine:
2. you can output reals to a text file, but they get written in the scientific format:1.234e7
You'll have to write a custom function to make it more readible:
or just borrow mine:
Code VHDL - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 ------------------------------------------------------------------------------------ --Returns the size of the given integer as if it were a string in the given Radix ------------------------------------------------------------------------------------ function get_int_length(x : integer; radix : positive range 2 to 36 := 10) return integer is variable temp : integer := abs x; variable len : integer := 0; begin if x = 0 then len := 1; end if; while temp > 0 loop temp := temp / radix; len := len + 1; end loop; if x < 0 then len := len + 1; --add extra character for -ve sign end if; return len; end function get_int_length; -------------------------------------------------------------------------------------------------- --casts a real to an integer, with or without rounding -------------------------------------------------------------------------------------------------- function real_to_int( x : real; round : boolean := false) return integer is variable ret : integer; variable temp : integer; begin if round then ret := integer(x); else temp := integer(x); --always round towards 0 if temp >= 0 then --rounds positive numbers down if real(temp) > x then ret := temp - 1; else ret := temp; end if; else --rounds -ve numbers up if real(temp) < x then ret := temp + 1; else ret := temp; end if; end if; end if; return ret; end function; ------------------------------------------------------------------------ --Converts a real to a string --gives a string representation of a real if you dont want the enire M.FeE format, --and just want m.f ------------------------------------------------------------------------ function real_to_string(x : real; num_frac : positive; radix : positive range 2 to 36 := 10) return string is --mantissa and fraction as integers constant ABS_X : real := abs(x); constant MANTISSA : integer := real_to_int(ABS_X, false); constant FRACTION : integer := real_to_int( ( ( ABS_X-real(MANTISSA) )*real(radix**num_frac) + 0.5 ),--takes care of rounding false ); constant STRING_LEN_M : integer := get_int_length(MANTISSA, radix); --length of mantissa constant STRING_LEN_F : integer := get_int_length(FRACTION, radix); --length of fractional part constant PAD_LEN : integer := num_frac - STRING_LEN_F; --number of pad 0's function x_is_neg return integer is begin if x < 0.0 then return 1; else return 0; end if; end function x_is_neg; variable pad_loop : integer := 0; variable i : integer; variable ret_string : string(1 to (STRING_LEN_M + STRING_LEN_F + PAD_LEN) + 1 + x_is_neg ); variable neg_offset : integer := 0; begin if x < 0.0 then ret_string(1) := '-'; neg_offset := 1; end if; ret_string( (1+neg_offset) to (STRING_LEN_M + neg_offset) ) := int_to_string(MANTISSA, radix); ret_string(STRING_LEN_M+1+neg_offset) := '.'; --pad out the string while pad_loop < PAD_LEN loop i := STRING_LEN_M + 2 + pad_loop + neg_offset; ret_string(i) := '0'; pad_loop := pad_loop + 1; end loop; ret_string( (STRING_LEN_M+2+PAD_LEN+neg_offset) to ret_string'high) := int_to_string(FRACTION, radix); return ret_string; end function;