Skip to content

Commit

Permalink
script : support binary format numbers (0b....).
Browse files Browse the repository at this point in the history
  • Loading branch information
jfdelnero committed Oct 17, 2024
1 parent e5a434b commit cd7ca0d
Showing 1 changed file with 67 additions and 2 deletions.
69 changes: 67 additions & 2 deletions lib_jtag_core/src/script/script.c
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,53 @@ static int get_param_str( script_ctx * ctx, char * line, int param_offset,char *
return -1;
}

env_var_value strbin_to_val( char * str )
{
int l;
env_var_value val;
env_var_value mask;

val = 0;

// -> end of the string
l = 0;
while(str[l])
{
l++;
}

// -> find the lowest bit
while(l)
{
if( str[l] == '1' || str[l] == '0' )
{
break;
}
l--;
};

mask = 0x1;
// convert
while(l >= 0)
{
if( str[l] != '1' && str[l] != '0' )
{
break;
}

if( str[l] == '1')
{
val |= mask;
}

mask <<= 1;

l--;
}

return val;
}

static env_var_value str_to_int(char * str)
{
env_var_value value;
Expand All @@ -263,9 +310,27 @@ static env_var_value str_to_int(char * str)
{
if( strlen(str) > 2 )
{
if( str[0]=='0' && ( str[1]=='x' || str[1]=='X'))
if( str[0]=='0' )
{
value = (env_var_value)STRTOVALUE(str, NULL, 0);
switch( str[1] )
{
// hex
case 'x':
case 'X':
value = (env_var_value)STRTOVALUE(str, NULL, 0);
break;

// binary
case 'b':
case 'B':
value = strbin_to_val(str);
break;

// decimal
default:
value = atoi(str);
break;
}
}
else
{
Expand Down

0 comments on commit cd7ca0d

Please sign in to comment.