Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
8 years ago

String operation

I have a string


signal rx_uart_buf : string (0 to 127);

Now I want to test every char value


when ST_PARSE_COM =>
if (idx < RX_BUF_SIZE) then
    case character'pos(rx_uart_buf(idx)) is
        when 0  => ParseState <= ST_PARSE_IDLE;
        when 32 => ParseState <= ST_PARSE_ARG1;
    end case;
    idx := idx + 1;
 end if;

Is it right way to do it?

5 Replies

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    In what context? Strings are not suitable for synthesis, so I assume this is simulation only code?

    Also, strings can only have a positive range, so cannot start at 0 - it must start at 1 or above?

    Why bother coverting the char to an integer? why not just use the chars directly?

    
    if (idx < RX_BUF_SIZE) then
        case rx_uart_buf(idx) is
            when NUL  => ParseState <= ST_PARSE_IDLE;
            when ' ' => ParseState <= ST_PARSE_ARG1;
        end case;
        idx := idx + 1;
     end if;
    
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    --- Quote Start ---

    In what context? Strings are not suitable for synthesis, so I assume this is simulation only code?

    Also, strings can only have a positive range, so cannot start at 0 - it must start at 1 or above?

    Why bother coverting the char to an integer? why not just use the chars directly?

    
    if (idx < RX_BUF_SIZE) then
        case rx_uart_buf(idx) is
            when NUL  => ParseState <= ST_PARSE_IDLE;
            when ' ' => ParseState <= ST_PARSE_ARG1;
        end case;
        idx := idx + 1;
     end if;
    

    --- Quote End ---

    I see. Thank you. So it doesn't work in real life? I was planning to accumulate chars from terminal to rx_uart_buf and then parse the string.
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    A string is just an abstract concept. At hardware level they are just a set of integers.

    You could try using a string, and qartus may compile it (Ive done it as a proof of concept to myself) but I wouldnt trust it.
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    I see. Thank you. Quartus compiles it. But to be on safety side I've decided to define a string as array of bytes

    type byte_array is array (0 to RX_BUF_SIZE) of std_logic_vector(7 downto 0);

    signal rx_uart_buf : byte_array;
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    I think you mean:

    type byte_array is array (0 to RX_BUF_SIZE-1) of std_logic_vector(7 downto 0);