-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhex2dec_vram
107 lines (91 loc) · 2.84 KB
/
hex2dec_vram
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
#!/bin/bash
#Convert data from Altera hex-file to decimal
if [ -z "$1" -o -z "$2" ]
then
echo "Format isn't correct!"
echo -e "Use \033[1mhex2dec_vram VRAM.hex bash\033[0m to create decimal output to stdout (slow)."
echo -e "Use \033[1mhex2dec_vram VRAM.hex octave_out\033[0m to create decimal output to stdout (fast, using Octave)."
echo -e "Use \033[1mhex2dec_vram VRAM.hex octave\033[0m to convert format and open data in Octave session."
echo -e "Last optional parameter can set bit-width (default 12 bit)."
exit
fi
if [ -z "$3" ]
then
hex_bits=12
else
hex_bits="$3"
fi
max_bits=`echo "2^$hex_bits" | bc`
thr_bits=`echo "$max_bits/2-1" | bc`
suf_array="a b c d e f g h i j k l m n o p"
VRAM_H=`cut "$1" -c10,11,12,13 | head -n -1`
full_length=`wc -l "$1" | cut -d " " -f1`
full_length=`expr $full_length - 1`
function data_file_prepare() {
#$1 - 32-bit signal 0 to 7
#$1 - 16 bit sel from 32-bit signal
#$3 - real bit-width of signal
local hex_bits="$3"
local max_bits=`echo "2^$hex_bits" | bc`
local thr_bits=`echo "$max_bits/2-1" | bc`
if [ "$2" = 0 ]
then
local bits_sel="10,11,12,13"
else
local bits_sel="6,7,8,9"
fi
cut vr -c$bits_sel | head -n -1 > vr_clean
local full_length=`wc -l vr_clean | cut -d " " -f1`
octave -q --eval "x = hex2dec(textread(\"vr_clean\", \"%s\"));
for k=1:$full_length if(x(k)>$thr_bits) x(k)=x(k)-$max_bits; end; end
file_id = fopen(\"x$1_$2\", 'w');
fprintf(file_id,\"%i\\n\",x);
fclose(file_id);"
}
#Use octave for format conversion
case $2 in
octave)
echo "$VRAM_H" > vr
echo "Use Octave with $hex_bits bit-width and $full_length numbers"
octave -q --persist --eval "x = hex2dec(textread(\"vr\", \"%s\"));
for k=1:$full_length if(x(k)>$thr_bits) x(k)=x(k)-$max_bits; end; end
x_absfft = abs(fft(x));
f=20*log10(x_absfft/max(x_absfft));"
exit
;;
octave_out)
echo "$VRAM_H" > vr
octave -q --eval "x = hex2dec(textread(\"vr\", \"%s\"));
for k=1:$full_length if(x(k)>$thr_bits) x(k)=x(k)-$max_bits; end; end
printf(\"%i\\n\",x);"
exit
;;
f0)
cp $1 vr
data_file_prepare $3 $4 $5
exit
;;
*) echo "Exit"
;;
esac
split_lines=`expr $full_length / 16`
echo "$VRAM_H" | split -l $split_lines -
function convert_vram() {
DATA_CONV=`cat $1`
for DATA in $DATA_CONV
do
VRAM_D=`echo "ibase=16; $DATA" | bc`
if [ "$VRAM_D" -gt $thr_bits ]
then
VRAM_D=`expr $VRAM_D - $max_bits`
fi
echo "$VRAM_D"
done
}
for suf in $suf_array
do
convert_vram xa$suf > xa${suf}_dec &
done
wait
cat xa[a-p]_dec
rm xa*