From: Michael on
Hi there - I am slowly teaching myself VHDL this weekend. I am getting
an error that I do not understand: "parse error, unexpected IF". My
very simple code is at the bottom of this post, and the error is being
caused by the "if switches(0)=0 then" line. Can somebody tell me what
I'm doing wrong? I'm sure it's terribly simple - but coming from a C
background I am having trouble understanding what I'm doing wrong.

Thanks!

-Michael

library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;

entity hello_world is
port (
clk, enc_a, end_b : in std_logic;
switches : in std_logic_vector (3 downto 0);
led : out std_logic_vector (7 downto 0)
);
end hello_world;

architecture rtl of hello_world is
signal cnt : std_logic_vector (30 downto 0);
signal enccnt : std_logic_vector (7 downto 0);
begin
process(clk)
begin
if rising_edge(clk) then
cnt <= cnt + 1;
end if;
end process;

if switches(0)=0 then
led <= cnt(30 downto 23);
else
led <= enccnt;
end if;
end rtl;
From: Frank Buss on
Michael wrote:

> Hi there - I am slowly teaching myself VHDL this weekend. I am getting
> an error that I do not understand: "parse error, unexpected IF". My
> very simple code is at the bottom of this post, and the error is being
> caused by the "if switches(0)=0 then" line. Can somebody tell me what
> I'm doing wrong? I'm sure it's terribly simple - but coming from a C
> background I am having trouble understanding what I'm doing wrong.

You can't use IF outside of processes, a bit like that you can't use the C
IF outside of functions. You could write it like this:

led <= cnt(30 downto 23) when switches(0)=0 else enccnt;

A bit like the ?-operator in C.

--
Frank Buss, fb(a)frank-buss.de
http://www.frank-buss.de, http://www.it4-systems.de
From: Nicolas Matringe on
Michael a �crit :
> Hi there - I am slowly teaching myself VHDL this weekend. I am getting
> an error that I do not understand: "parse error, unexpected IF". My
> very simple code is at the bottom of this post, and the error is being
> caused by the "if switches(0)=0 then" line. Can somebody tell me what
> I'm doing wrong? I'm sure it's terribly simple - but coming from a C
> background I am having trouble understanding what I'm doing wrong.

Hello
There are a few points that need clarifying, besides you "unexpected if"
problem which has already been dealt with.


> library IEEE;
> use IEEE.STD_LOGIC_1164.ALL;
> use IEEE.STD_LOGIC_ARITH.ALL;
> use IEEE.STD_LOGIC_UNSIGNED.ALL;

NEVER, in any case, use these non-standard libraries called
std_logic_arith, std_logic_signed and std_logic_unsigned. Use
numeric_std instead.
With this library, declare your signal cnt as unsigned instead of
std_logic_vector

[...]
>
> if switches(0)=0 then
> led <= cnt(30 downto 23);
> else
> led <= enccnt;
> end if;

Your if statement should be inside a process, as has already been said.
Second point : signal switches is an array of std_logic, not an array of
integers. std_logic litteral constants must be written betwen single quotes:
if switches(0) = '0' then
led <= std_logic_vector(led(30 downto 23));

Since cnt is now unsigned, you need to cast it to std_logic_vector
before assigning to led.

Nicolas
From: Michael on
On Apr 19, 10:41 pm, Frank Buss <f...(a)frank-buss.de> wrote:
> Michael wrote:
> > Hi there - I am slowly teaching myself VHDL this weekend. I am getting
> > an error that I do not understand: "parse error, unexpected IF". My
> > very simple code is at the bottom of this post, and the error is being
> > caused by the "if switches(0)=0 then" line. Can somebody tell me what
> > I'm doing wrong? I'm sure it's terribly simple - but coming from a C
> > background I am having trouble understanding what I'm doing wrong.
>
> You can't use IF outside of processes, a bit like that you can't use the C
> IF outside of functions. You could write it like this:
>
> led <= cnt(30 downto 23) when switches(0)=0 else enccnt;
>
> A bit like the ?-operator in C.

Hi Frank - thanks for clearing that up. I had not realized that
limitation of IF. Why can ifs only be used inside processes? That
strikes me as an odd limitation, though I'm sure there's a good reason
behind it.

I tried your suggestion for the change, but I got this error: "can not
have such operands in this context.". That strikes me as an odd error
- as led, cnt, and enccnt are of the same type.

Any idea what is wrong? Thanks again!

-Michael
From: michael on
On Apr 20, 9:05 am, Michael <nleah...(a)gmail.com> wrote:
> On Apr 19, 10:41 pm, Frank Buss <f...(a)frank-buss.de> wrote:
>
> > Michael wrote:
> > > Hi there - I am slowly teaching myself VHDL this weekend. I am getting
> > > an error that I do not understand: "parse error, unexpected IF". My
> > > very simple code is at the bottom of this post, and the error is being
> > > caused by the "if switches(0)=0 then" line. Can somebody tell me what
> > > I'm doing wrong? I'm sure it's terribly simple - but coming from a C
> > > background I am having trouble understanding what I'm doing wrong.
>
> > You can't use IF outside of processes, a bit like that you can't use the C
> > IF outside of functions. You could write it like this:
>
> > led <= cnt(30 downto 23) when switches(0)=0 else enccnt;
>
> > A bit like the ?-operator in C.
>
> Hi Frank - thanks for clearing that up. I had not realized that
> limitation of IF. Why can ifs only be used inside processes? That
> strikes me as an odd limitation, though I'm sure there's a good reason
> behind it.
>
> I tried your suggestion for the change, but I got this error: "can not
> have such operands in this context.". That strikes me as an odd error
> - as led, cnt, and enccnt are of the same type.
>
> Any idea what is wrong? Thanks again!
>
> -Michael

Hello again - I took Nicholas's suggestion to change the comparison
to: switches(0)='0' (I added single quotes around the 0) - and that
fixed the problem. The code synthesized properly and is now running as
expected on my Spartan-3E dev board! Thanks!

-Michael