title: How to convert big integers to smaller integers in vhdl?
body: <p>I have VHDL code where I'm trying to multiply pixel values. I have the following entity:</p>
  
  <pre><code>entity xGradient is
  
  port(
      clk : in std_logic;
      x11, x12, x13, x21, x22, x23, x31, x32, x33 : in integer range 0 to 255;
      gradientInX : out integer range 0 to 255
  );
  
  end xGradient;
  </code></pre>
  
  <p>I do the following operation on these:</p>
  
  <pre><code>gradientInX &lt;= x11 + 2*x21 + x31 - x13 - 2*x23 - x33;
  </code></pre>
  
  <p>The issue as you may notice is that the values obtained from the operation exceed the range of possible values my output integer can be. My simulation crashes when this happens. I don't know the right word for what I'm trying to do and therefore can't seem to figure out how to fix this problem and it's driving me crazy. I basically just want to truncate, or reduce the size of the output to ensure it fits into the gradientInX output port. I tried using a process block like this:</p>
  
  <pre><code>-- declared signals
  signal gradientMem: integer;
  
  --beginning of my architecture
  begin 
  
  SobelOperator : process(gradientMem)
  
  begin
      gradientMem &lt;= x11 + 2*x21 + x31 - x13 - 2*x23 - x33;
  
      if (gradientMem &gt; 255) then
          gradientMem &lt;= 255;
      elsif (gradientMem &lt; 0) then
          gradientMem &lt;= 0;
      end if;
  
  end process SobelOperator;
  </code></pre>
  
  <p>then assigning gradientMem to gradientInX but it doesn't work for some reason. Any help would really be appreciated. Note: I haven't included all the VHDL code as I thought it would be unnecessarily long. The error message I get is that the signal resulting value from the operation is out of range (since the resulting value is negative and the output has a range of only 0 to 255). </p>
  
  <p>Matthew</p>