File: ../../Sources/A10_compute_k.vhd
0: ----------------------------------------------------------------------------------
1: -- Engineer: Vitor Mendes Camilo
2: --
3: -- Module Name: A10_compute_k - Behavioral
4: --
5: -- Description: Code segment A.10
6: -- Computation of the Golomg coding variable k
7: --
8: ----------------------------------------------------------------------------------
9:
10: library ieee;
11: use ieee.std_logic_1164.all;
12: use ieee.numeric_std.all;
13: use work.openjls_pkg.all;
14:
15: entity a10_compute_k is
16: generic (
17: A_WIDTH : natural := CO_AQ_WIDTH_STD;
18: K_WIDTH : natural := CO_K_WIDTH_STD;
19: N_WIDTH : natural := CO_NQ_WIDTH_STD
20: );
21: port (
22: iNq : in unsigned (N_WIDTH - 1 downto 0);
23: iAq : in unsigned (A_WIDTH - 1 downto 0);
24: oK : out unsigned (K_WIDTH - 1 downto 0)
25: );
26: end entity a10_compute_k;
27:
28: architecture behavioral of a10_compute_k is
29:
30: -- Worst case: Nq = 1 and Aq = 2^A_WIDTH - 1
31: constant MAX_K : natural := A_WIDTH;
32: constant W : natural := A_WIDTH + 1;
33:
34: begin
35:
36: -- Parallel formulation: compute all candidate shifts up-front, compare each
37: -- against Aq, then priority-encode for the smallest index whose shift
38: -- reaches/exceeds Aq. Depth collapses from MAX_K ripple levels to one
39: -- compare + log2(MAX_K+1) encoder levels.
40: p_compute_k : process (iNq, iAq) is
41:
42: variable vAq : unsigned(W - 1 downto 0);
43: variable vNq : unsigned(W - 1 downto 0);
44: variable vMatch : std_logic_vector(MAX_K downto 0);
45: variable vK : unsigned(oK'range);
46:
47: begin
48:
49: vAq := resize(iAq, W);
50: vNq := resize(iNq, W);
51:
52: -- Parallel compares: vMatch(k) = '1' iff (Nq << k) >= Aq
53: for k in 0 to MAX_K loop
54:
55: if (shift_left(vNq, k) >= vAq) then
56: vMatch(k) := '1';
57: else
58: vMatch(k) := '0';
59: end if;
60:
61: end loop;
62:
63: -- Priority encode: smallest index with match='1' wins.
64: -- Loop high→low, last assignment carried forward yields the lowest match.
65: vK := to_unsigned(MAX_K + 1, vK'length);
66:
67: for k in MAX_K downto 0 loop
68:
69: if (vMatch(k) = '1') then
70: vK := to_unsigned(k, vK'length);
71: end if;
72:
73: end loop;
74:
75: oK <= vK;
76:
77: end process p_compute_k;
78:
79: end architecture behavioral;