Verilog allows the branches of a case statement to be defined as a constant in a different file. Example:
`define COND1 3'b001
`define COND2 3'b010
`define COND3 3'b100
module xyz(input wire [2:0] select, output reg value);
always @*
case(select)
`COND1: value = 1'b0;
`COND2: value = 1'b1;
`COND3: value = 1'b0;
default: value = 1'b0;
endmodule
How can I do the same in VHDL? I want to have my constants for the case defined in a package and to pull these constants into the current architecture and use the constants to define the branches for the case statement. Working example:
library ieee;
use ieee.std_logic_1164.all;
entity stuff is
port(
sel1: in std_logic_vector(2 downto 0);
val1: out std_logic
);
end entity;
architecture rtl of stuff is
constant COND1 : std_logic_vector(2 downto 0) := "001";
constant COND2 : std_logic_vector(2 downto 0) := "010";
constant COND3 : std_logic_vector(2 downto 0) := "100";
begin
process(sel1)
begin
case sel1 is
when COND1 => val1 <= '0';
when COND2 => val1 <= '1';
when COND3 => val1 <= '0';
when others => val1 <= '0';
end case;
end process;
end architecture;
Which works ok...
However, when I try it in my VHDL code I get a strange error:
..\simtools\ghdl\bin\ghdl.exe -a stuff2.vhdl
stuff2.vhdl:40:18: choice must be locally static expression
stuff2.vhdl:41:18: choice must be locally static expression
stuff2.vhdl:42:18: choice must be locally static expression
Here's the code that gives this error:
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity stuff is
generic(
CW : integer := 3
);
port(
sel1 : in std_logic_vector(CW-1 downto 0);
val1 : out std_logic
);
end entity;
architecture rtl of stuff is
function n(n_value:integer; n_width:integer)
return std_logic_vector is
begin
return std_logic_vector(to_unsigned(n_value, n_width));
end function;
constant CMD1 : std_logic_vector(2 downto 0) := n(0, 3);
constant CMD2 : std_logic_vector(2 downto 0) := n(1, 3);
constant CMD3 : std_logic_vector(2 downto 0) := n(2, 3);
constant CMD4 : std_logic_vector(2 downto 0) := n(3, 3);
constant CMD5 : std_logic_vector(2 downto 0) := n(4, 3);
signal sel2 : std_logic_vector(2 downto 0);
begin
sel2 <= sel1(2 downto 0);
process(sel2)
begin
case sel2 is
when CMD1 => val1 <= '0';
when CMD2 => val1 <= '1';
when CMD3 => val1 <= '0';
when others => val1 <= '0';
end case;
end process;
end architecture;