cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
farmerjojo
Visitor
Visitor
1,479 Views
Registered: ‎09-20-2017

casting error

Jump to solution

Hi,

I am using Vivado to learn VHDL and get an error for this simple piece of code.

 

architecture Behavioral of tmp is
  type short is range -127 to 127;
  type long is range -1024 to 1024;
begin
  process(a) is
    variable s: short := 0;
    variable l: long := -127;
  begin
    s := (short)l;         -- error on this line
  end process;
end Behavioral;

 

I am trying to assign the value of l to s (which in this case is numerically compatible) but I appreciate that the types are not compatible hence the cast.

 

The error that I get is as follows.

 

Error: type error near l ; current type long ; expected type void

 

If casting is not supported, what is the proper way to do this assignment?

 

If casting is supported, what have I got wrong here?

 

Regards

FarmerJo

 

0 Kudos
1 Solution

Accepted Solutions
hpoetzl
Voyager
Voyager
2,057 Views
Registered: ‎06-24-2013

Hey FarmerJo,

 

Type casting in VHDL doesn't work 'out of the box' for new types.

You basically need to define a function which does the conversion which can be tricky.

 

In this particular case, your 'types' are both related to the existing 'integer' type, so it would be better to make those subtypes which would also allow for seamless conversion while still giving you all the range checks.

 

Something like this ...

architecture Behavioral of tmp is
    subtype short is integer range -127 to 127;
    subtype long is integer range -1024 to 1024;
begin
    process(a) is
        variable s: short := 0;
        variable l: long := -127;
    begin
        s := l;
    end process;
end Behavioral;

 

Hope this helps,

Herbert

-------------- Yes, I do this for fun!

View solution in original post

3 Replies
hpoetzl
Voyager
Voyager
2,058 Views
Registered: ‎06-24-2013

Hey FarmerJo,

 

Type casting in VHDL doesn't work 'out of the box' for new types.

You basically need to define a function which does the conversion which can be tricky.

 

In this particular case, your 'types' are both related to the existing 'integer' type, so it would be better to make those subtypes which would also allow for seamless conversion while still giving you all the range checks.

 

Something like this ...

architecture Behavioral of tmp is
    subtype short is integer range -127 to 127;
    subtype long is integer range -1024 to 1024;
begin
    process(a) is
        variable s: short := 0;
        variable l: long := -127;
    begin
        s := l;
    end process;
end Behavioral;

 

Hope this helps,

Herbert

-------------- Yes, I do this for fun!

View solution in original post

farmerjojo
Visitor
Visitor
1,452 Views
Registered: ‎09-20-2017

Hi Herbert,

Thanks for the answer.

 

I like the thinking here.

 

Many thanks

Regards

FarmerJo

0 Kudos
hpoetzl
Voyager
Voyager
1,443 Views
Registered: ‎06-24-2013

You're welcome!

 

All the best,

Herbert

-------------- Yes, I do this for fun!
0 Kudos