module BitCounter(
input wire [7:0] data,
output reg [3:0] high_bit_count,
output reg [3:0] low_bit_count
);
always @(*) begin
high_bit_count = 0;
low_bit_count = 0;
for (i = 0; i < 8; i = i + 1) begin
if (data[i] == 1) begin
if (i < 4)
low_bit_count = low_bit_count + 1;
else
high_bit_count = high_bit_count + 1;
end
end
end
endmodule
VHDL模块示例
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.numeric_std.ALL;
entity BitCounter is
port (
data : in std_logic_vector(7 downto 0);
high_bit_count : out std_logic_vector(3 downto 0);
low_bit_count : out std_logic_vector(3 downto 0)
);
end entity BitCounter;
architecture Behavioral of BitCounter is
begin
high_bit_count <= (others => '0');
low_bit_count <= (others => '0');
process(data)
begin
for i in 0 to 7 loop
if data(i) = '1' then
if i < 4 then
low_bit_count <= low_bit_count + 1;
else
high_bit_count <= high_bit_count + 1;
end if;
end if;
end loop;
end process;
end architecture Behavioral;