Search code examples
vhdl

VHDL subtraction calculation


How does VHDL do a subtraction calculation? Does it do the two's complement? For a 2bit subtractor, I was showed to use vectors for i/o but it would require 3 input pins/switches to be able to test the out. (Downloading to DE0 Nano Board). So I tried to use the integer values:

entity TwoBitSubtractor is port(
    x,y         : in integer range 0 to 3;
    result     : out integer range -3 to 3);
end TwoBitSubtractor;

architecture gates of TwoBitSubtractor is
begin
    result <= x - y;
 end gates;

It compiled successfully but some of the outputs in the board confuses me. For example: 01 - 10 = 100

Here is a ss of the DE0 nano board

enter image description here

For a -1 wouldn't it be a 111 instead?

DE0 nanoboard specs http://www.ti.com/lit/ug/tidu737/tidu737.pdf


Solution

  • You are calculating with integer signals, but you can set IO pins only with type std_logic or a bunch of them using std_logic_vector. So somewhere in your top entity, which you haven't shown us, there's a conversion between integer and std_logic(_vector). There is no implicit functionality, you have to cast to signed or unsigned datatype, look here(Duolos VHDL designer guide) for conversion functions.

    I guess you are using unsigned values, try to use signed in Top level entity. Same is for the inputs, you can't specify an integer with "01" or "10", only with literals like 0, 1, 2, 3.