1

I have below data in an input file to be read:

10101100 11010100 10101100 11010100

11111110 10111001 11111110 10111001

I need to read each nibble and write them to an array. But due to the whitespaces the length of the line varies, which affects the while len > 0 loop. How do I eliminate whitespaces from the line after readline and before read so that I would get the proper length in the len variable?

I am using the following code:

while not endfile(f) loop
    readline(f, L);
    len := L'length;
    while len > 0 loop
        read(L, b);
        mem(i) <= b;
        i := i + 1;
        len := len - b'length;
    end loop;
end loop;

Declarations:

constant filename:  string := "C:\Users\ChowdaryS\Downloads\topo.bin";
file f:       text open read_mode is filename;
variable L:   line;
variable i:   integer:= 0;
variable b:   std_logic_vector(3 downto 0);  
variable len: integer;
4
  • Can you guarantee a single space between entries? len := len - b'length - 1; Commented Mar 3, 2017 at 16:04
  • @Brian Drummond, yes there is either a single space or double space (not both) between databits. But the pattern remain same in whole file. Commented Mar 3, 2017 at 16:08
  • @Brian Drummond , len := len - b'length - 1; does not work, as no. of iterations will reduce and last nibble in each line is affected. Commented Mar 3, 2017 at 16:15
  • Ah. I missed you were reading nibbles not bytes. So subtract one (or 2, or not) depending on the value of the next character. Commented Mar 3, 2017 at 16:21

1 Answer 1

0

I spent some time testing the boundary conditions for trailing whilepace which necessitated a change to your nested loops:

        while not endfile(f) loop
            readline(f, L);
            -- len := L'length;
            -- while len >= b'length loop
            while L.all'length >= b'length loop
                read(L, b);
                -- len := L'length;
                mem(i) <= to_stdlogicvector(b);
                i := i + 1;
                -- len := len - b'length;
            end loop;
        end loop;

The len variable can be eliminated. After a read L will contain the remainder and can be examined directly.

The change is to support trailing whitespace characters that don't show up by copying and pasting your input file contents from your question to a file.

I imagine you're seeing trailing spaces on the line, artifacts of how the input file is written.

The above change is bullet proof for 1, 2 or three trailing spaces.

For the case of four or more trailing spaces or a one or more of a space, a non-blocking space or a horizontal tab trailing the last valid b value a fix requires scanning for any of these whitepace characters, you can count on just trivial rejection.

If you assume any non-whitespace character represents some portion of a b value you can simply blame failure of malformed b values on how the file was written.

That allows you to simply test for characters remaining that are only whitespace.

With an added function:

package body io is
    function iswhitespace (inpstr:  in  string) return boolean is
        constant NBSP: character  := character'val(128); 
    begin
        for i in inpstr'range loop
            if inpstr(i) /= ' ' and inpstr(i) /= NBSP and inpstr(i) /= HT then
                exit;
            elsif i = inpstr'RIGHT then
                return TRUE;
            end if;
        end loop;
        return FALSE;
    end function;

(keeping in mind the declaration in the package)

The textio nested loop condition look like:

        -- while L.all'length >= b'length loop
        while L.all'length >= b'length and not iswhitespace(L.all) loop

which makes file read immune to any length of trailing whitespace on a line.

Sign up to request clarification or add additional context in comments.

2 Comments

Hello , along with above answer , can i know how to neglect first 8 bits of a line read while reading in vhdl. for example if i have data like "0110AE01 0010 0100 1111 1011" i need to read only the nibbles neglecting first 8 bits(0110AE01) and put them into memory. How do i neglect first 8 bits?
That's not 8 bits it's 8 characters. Create a new read receiving variable of type string with constraint of 1 to 8. Following each readline read the header from L before reading data from the remainder of the line in L.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.