crc_Lösung

This commit is contained in:
allamaaki80515 2026-01-13 09:59:29 +01:00
parent 44296a640b
commit 8997e1cb97

View File

@ -8,33 +8,36 @@ library work;
entity crc is entity crc is
port ( port (
clk : in std_logic; clk : in std_logic;
reset : in std_logic; reset : in std_logic;
task_start : in std_logic;
task_state : out work.task.State; task_start : in std_logic;
signal_read : out std_logic; task_state : out work.task.State;
signal_readdata : in std_logic_vector(31 downto 0);
signal_write : out std_logic; signal_read : out std_logic;
signal_writedata: out std_logic_vector(31 downto 0) signal_readdata : in std_logic_vector( 31 downto 0 );
signal_write : out std_logic;
signal_writedata : out std_logic_vector( 31 downto 0 )
); );
end entity crc; end entity crc;
architecture rtl of crc is architecture rtl of crc is
signal current_task_state : work.task.State; signal current_task_state : work.task.State;
signal next_task_state : work.task.State; signal next_task_state : work.task.State;
signal index : integer range 0 to work.task.STREAM_LEN := 0; signal index : integer range 0 to work.task.STREAM_LEN;
constant CRC_INIT : std_logic_vector(31 downto 0) := X"FFFFFFFF"; constant CRC_INIT : std_logic_vector(31 downto 0) := X"FFFFFFFF";
constant CRC_POLY : std_logic_vector(31 downto 0) := X"EDB88320"; constant CRC_POLY : std_logic_vector(31 downto 0) := X"EDB88320";
signal crc : std_logic_vector(31 downto 0) := CRC_INIT;
signal data_reg : std_logic_vector(31 downto 0); signal crc_reg : std_logic_vector(31 downto 0) := CRC_INIT;
signal data_valid : std_logic := '0'; signal data_reg : std_logic_vector(31 downto 0);
signal data_ready : std_logic := '0';
begin begin
task_state <= current_task_state; task_state <= current_task_state;
-- TASK STATE MACHINE (VORLAGE - nicht ändern!) task_state_transitions : process ( current_task_state, task_start, index ) is
task_state_transitions: process(current_task_state, task_start, index)
begin begin
next_task_state <= current_task_state; next_task_state <= current_task_state;
case current_task_state is case current_task_state is
@ -43,8 +46,7 @@ begin
next_task_state <= work.task.TASK_RUNNING; next_task_state <= work.task.TASK_RUNNING;
end if; end if;
when work.task.TASK_RUNNING => when work.task.TASK_RUNNING =>
-- WICHTIG: Schreiben nach 1024 Werten (index 0-1023) if index = work.task.STREAM_LEN then
if index = 1023 then
next_task_state <= work.task.TASK_DONE; next_task_state <= work.task.TASK_DONE;
end if; end if;
when work.task.TASK_DONE => when work.task.TASK_DONE =>
@ -54,67 +56,71 @@ begin
end case; end case;
end process; end process;
-- Data Channel Control
signal_read <= '1' when current_task_state = work.task.TASK_RUNNING signal_read <= '1' when current_task_state = work.task.TASK_RUNNING
and data_valid = '0' and data_ready = '0'
and index < 1024 and index < work.task.STREAM_LEN
else '0'; else '0';
signal_write <= '1' when current_task_state = work.task.TASK_DONE else '0'; signal_write <= '1' when current_task_state = work.task.TASK_DONE else '0';
signal_writedata <= crc xor X"FFFFFFFF"; -- Final XOR
-- Haupt-Sync Process (identisch zur SW-Version) signal_writedata <= crc_reg xor X"FFFFFFFF";
sync: process(clk, reset)
variable temp_crc : std_logic_vector(31 downto 0);
begin
if reset = '1' then
current_task_state <= work.task.TASK_IDLE;
index <= 0;
crc <= CRC_INIT;
data_reg <= (others => '0');
data_valid <= '0';
elsif rising_edge(clk) then sync : process ( clk, reset ) is
current_task_state <= next_task_state; variable temp_crc : std_logic_vector(31 downto 0);
begin
if reset = '1' then
current_task_state <= work.task.TASK_IDLE;
index <= 0;
crc_reg <= CRC_INIT;
data_reg <= X"00000000";
data_ready <= '0';
case next_task_state is elsif rising_edge(clk) then
when work.task.TASK_IDLE => current_task_state <= next_task_state;
index <= 0;
crc <= CRC_INIT;
data_valid <= '0';
when work.task.TASK_RUNNING => case next_task_state is
-- 1. DATA LESEN (Timing: signal_read='1' -> NEXT CLK data_valid) when work.task.TASK_IDLE =>
if signal_read = '1' then index <= 0;
data_reg <= signal_readdata; crc_reg <= CRC_INIT;
data_valid <= '1'; data_ready <= '0';
end if; data_reg <= X"00000000";
-- 2. CRC UPDATE (zlib: XOR dann 32x bitweise LSB-first) when work.task.TASK_RUNNING =>
if data_valid = '1' then -- DATA LESEN (Timing korrekt)
temp_crc := crc xor data_reg; if signal_read = '1' then
crc <= temp_crc; data_reg <= signal_readdata;
data_ready <= '1';
end if;
-- 32 Bit LSB-first Verarbeitung in EINEM Takt (wie SW) -- CRC UPDATE (INDEX PRÜFEN VOR Update!)
for i in 0 to 31 loop if data_ready = '1' then
if temp_crc(0) = '1' then -- WICHTIG: Index bleibt gleich bis TASK_DONE triggert!
temp_crc := std_logic_vector(shift_right(unsigned(temp_crc), 1)) xor CRC_POLY; --temp_crc := crc_reg xor data_reg; -- Berechne aber schreibe nicht sofort
else
temp_crc := std_logic_vector(shift_right(unsigned(temp_crc), 1));
end if;
end loop;
crc <= temp_crc;
data_valid <= '0'; temp_crc := crc_reg xor data_reg;
for i in 0 to 31 loop
if temp_crc(0) = '1' then
temp_crc := std_logic_vector(shift_right(unsigned(temp_crc), 1)) xor CRC_POLY;
else
temp_crc := std_logic_vector(shift_right(unsigned(temp_crc), 1));
end if;
end loop;
crc_reg <= temp_crc;
data_ready <= '0';
-- INDEX NUR erhöhen wenn NICHT letzter!
if index < work.task.STREAM_LEN then
index <= index + 1; index <= index + 1;
end if; end if; -- Bei index=1023: bleibt 1023 → TASK_DONE triggert!
end if;
when work.task.TASK_DONE =>
index <= 0;
data_ready <= '0';
end case;
end if;
end process sync;
when work.task.TASK_DONE =>
index <= 0;
data_valid <= '0';
end case;
end if;
end process;
end architecture rtl; end architecture rtl;