I've implemented a VHDL program that sums up the positions of switches and shows it on a 7-segment LED, but I've had to use lots of temporaries and a division. Is there a better way?
library ieee;
use ieee.std_logic_1164.all;
entity light is
port(
LEDR : out std_logic_vector(0 to 9);
LEDG : out std_logic_vector(0 to 7);
SW : in std_logic_vector(0 to 9);
KEY : in std_logic_vector(0 to 3);
HEX0 : out std_logic_vector(0 to 6);
HEX1 : out std_logic_vector(0 to 6);
HEX2 : out std_logic_vector(0 to 6);
HEX3 : out std_logic_vector(0 to 6)
);
end light;
architecture arch of light is
signal sum, sum2 : integer := 0;
signal x1,x2,x3,x4,x5,x6,x7,x8,x9 : integer := 0;
function led_value(x:integer)
return std_logic_vector is
begin
case x is
when 0 => return "0000001";
when 1 => return "1001111";
when 2 => return "0010010";
when 3 => return "0000110";
when 4 => return "1001100";
when 5 => return "0100100";
when 6 => return "0100000";
when 7 => return "0001111";
when 8 => return "0000000";
when 9 => return "0000100";
when others => return "1111111";
end case;
end led_value;
begin
x1 <= 1 when sw(1) = '1' else 0;
x2 <= 2 when sw(2) = '1' else 0;
x3 <= 3 when sw(3) = '1' else 0;
x4 <= 4 when sw(4) = '1' else 0;
x5 <= 5 when sw(5) = '1' else 0;
x6 <= 6 when sw(6) = '1' else 0;
x7 <= 7 when sw(7) = '1' else 0;
x8 <= 8 when sw(8) = '1' else 0;
x9 <= 9 when sw(9) = '1' else 0;
sum <= x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9;
sum2 <= (sum - sum mod 10) / 10;
hex0 <= led_value(sum mod 10);
hex1 <= led_value(sum2);
hex2 <= led_value(11);
hex3 <= led_value(11);
end arch;