advent-of-code/2021/day1/vhdl/top.vhdl

72 lines
1.5 KiB
VHDL
Raw Normal View History

2021-12-01 14:52:22 +01:00
library ieee;
use ieee.std_logic_1164.all,
ieee.numeric_std.all;
use work.util.all;
entity top is
generic (
MAX_INPUT_DIGITS : positive;
OUTPUT_WIDTH : positive;
STEP : natural range 1 to 2
);
port (
clk : in std_logic;
reset : in std_logic;
char : in character;
input_valid : in std_logic;
output : out unsigned(OUTPUT_WIDTH-1 downto 0);
output_valid : out std_logic
);
end entity;
architecture arch of top is
2021-12-01 14:52:22 +01:00
function WINDOW_SIZE return positive is
2021-12-01 14:52:22 +01:00
begin
if STEP = 1 then
return 1;
else
return 3;
end if;
end function;
type number_t is array(MAX_INPUT_DIGITS-1 downto 0) of digit_t;
2021-12-01 14:52:22 +01:00
type window_t is array(WINDOW_SIZE-1 downto 0) of number_t;
2021-12-01 14:52:22 +01:00
2021-12-01 14:52:22 +01:00
signal current_number : number_t;
2021-12-01 14:52:22 +01:00
signal window : window_t;
2021-12-01 14:52:22 +01:00
signal window_count : natural range 0 to WINDOW_SIZE := 0;
2021-12-01 14:52:22 +01:00
begin
process(clk)
begin
if rising_edge(clk) then
if reset then
output <= (others => '0');
output_valid <= '1';
2021-12-01 14:52:22 +01:00
window_count <= 0;
2021-12-01 14:52:22 +01:00
elsif input_valid then
output_valid <= '0';
if char = LF then
2021-12-01 14:52:22 +01:00
if window_count = WINDOW_SIZE then
if current_number > window(window'high) then
2021-12-01 14:52:22 +01:00
output <= output + 1;
end if;
output_valid <= '1';
else
2021-12-01 14:52:22 +01:00
window_count <= window_count + 1;
2021-12-01 14:52:22 +01:00
end if;
2021-12-01 14:52:22 +01:00
window <= window(window'high-1 downto 0) & current_number;
current_number <= (others => 0);
2021-12-01 14:52:22 +01:00
else
2021-12-01 14:52:22 +01:00
current_number <= current_number(current_number'left-1 downto 0) & char_to_digit(char);
2021-12-01 14:52:22 +01:00
end if;
end if;
end if;
end process;
end architecture;