Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] Integer to Natural Type Casting in VHDL

Status
Not open for further replies.

dzafar

Member level 4
Joined
Jan 17, 2017
Messages
76
Helped
1
Reputation
2
Reaction score
1
Trophy points
8
Activity points
690
Hello there,

I understand the following:

Integer: 32 bit signed value
Natural: 32 bit unsigned value starting at 0
Positive: 32 bit unsigned value starting at 1

My question is, is the following allowed/legal? Or do we have to do type casting?

ENTITY addition IS
PORT (
A, B: in positive;​
C: out natural);​
end addition;

ARCHITECTURE my_arch OF addition IS
BEGIN
C <= A + B;​
end ARCHITECTURE;
 

Yes. It's legal. Both Positive and Natural are subtypes of Integer.

- - - Updated - - -

You might go outside of the range of natural with high enough positive valuea - but that's a different thing.
 
  • Like
Reactions: dzafar

    dzafar

    Points: 2
    Helpful Answer Positive Rating
Hello there,

I understand the following:

Integer: 32 bit signed value
Natural: 32 bit unsigned value starting at 0
Positive: 32 bit unsigned value starting at 1

This is slightly wrong.
Integer is implementation defined, so whoever implements the compiler (like mentor, quartus etc) can define it as many bits as they want. So in theory you could have 16, 32 or 64 bit integer, depending on which tool you are using. By convention, everyone chooses to use 32 bits.

Also, you have natual and positivie wrong.

Code:
subtype natural is integer range 0 to integer'high;
subtype positive is integer range 1 to integer'high;

So natural and positive use 1 fewer bit to be represented. But they are just integers and are fully mixable.

Another point to note here though is that the number of bits used is at the processor level - NOT in VHDL. you have NO access to bits in integers.

- - - Updated - - -

You might go outside of the range of natural with high enough positive valuea - but that's a different thing.

This would be true of all integer types, so it is nothing special for natural.
 
  • Like
Reactions: dzafar

    dzafar

    Points: 2
    Helpful Answer Positive Rating
Thanks for the reply guys. All really helpful.

I do have another (very similar) question.

If I say: partial and A are of type unsigned (7 downto 0), can I do the following then:

if (A > 128)
partial := partial + A;

i.e. can I compare unsigned with integer (128)?
 

can I compare unsigned with integer?
Yes, you can compare.
But you can't assign an integer to an unsigned.
 
  • Like
Reactions: dzafar

    dzafar

    Points: 2
    Helpful Answer Positive Rating
yes - all arithmetic functions are defined for signed/unsigned and integer.
Ensure that the integers are appropriatly sized though. The functions will convert the integer to the length of the longest operand, and will truncate the result:

eg.
Code:
a : unsigned(7 downto 0);

a := a + 257;

You will get a warning about truncation and only add 1.

simularly, this will always be false:

if a > 257 then
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top