Belle II KLM Scint Firmware  1
PedestalWriter.vhd
1 -- Module Name: PedestalWriter - Behavioral
2 -- Create Date: 11:07:41 09/21/2018
3 library IEEE;
4 use IEEE.STD_LOGIC_1164.ALL;
5 use IEEE.NUMERIC_STD.ALL;
6 use IEEE.STD_LOGIC_MISC.ALL;
7 use IEEE.STD_LOGIC_UNSIGNED.ALL;
8 Library work;
9 use work.klm_scint_pkg.all;
44 
45 entity PedestalWriter is
46  Generic (
47  -- PED_RAM_tSCE_g : integer := 7; -- 55 ns -- setup chip ena (min 40 ns)
48  PED_RAM_tSCE_g : integer := 6; -- 40 ns -- setup chip ena (min 40 ns)
49  PED_RAM_tLZWE_g : integer := 20; -- 16 ns -- WEb high to low Z (min 10 ns)
50  PED_RAM_tSA_g : integer := 1; -- 8 ns -- setup addr (min 0 ns)
51  PED_RAM_tHA_g : integer := 1 -- 8 ns -- hold addr (min 0 ns)
52  );
53  Port (
54  clk : in STD_logic;
55  ena : in std_logic_vector(1 downto 0) := (others => '0');
56  ack : out std_logic_vector(1 downto 0) := (others => '0');
57  -- initial address
58  asic_addr : in slv3(1 downto 0);-- := (others => "000");
59  chan_addr : in slv4(1 downto 0) := (others => "0000");
60  win_addr : in std_logic_vector(8 downto 0) := (others => '0');
61  samp_addr : in slv4(1 downto 0) := (others => (others=>'0'));
62  -- sample data
63  even_sample : in slv12(1 downto 0) := (others => (others=>'0'));
64  odd_sample : in slv12(1 downto 0) := (others => (others=>'0'));
65  -- ped_wr_state : out std_logic_vector(1 downto 0);
66  --PedRAM pins
67  RAM_din : out std_logic_vector(7 downto 0) := (others => '0');
68  RAM_WEb : out std_logic := '1';
69  -- RAM_OEb : out std_logic := '0';
70  RAM_ADDR : out std_logic_vector(21 downto 0) := (others => '1')
71  );
72 end PedestalWriter;
73 
74 architecture Behavioral of PedestalWriter is
75 
76 
77  type pedestal_writing_machine is (
78  IDLE,
79  WRITE_TO_SRAM,
80  CHECK_DONE
81  );
82  signal ped_write_state : pedestal_writing_machine := IDLE;
83 
84 
85  signal addr : slv22(1 downto 0) := (others => "0000000000000000000000"); -- initial address
86  -- signal ack_i : std_logic_vector(1 downto 0) := "00"; -- remember last bus serviced for arbitration reasons
87  signal tx_dc : slv4(1 downto 0) := (others => "0000"); -- daughter card number for each bus
88  signal pedarr : slv8(2 downto 0) := (others => "00000000");
89  signal next_addr : std_logic_vector(21 downto 0) := (others => '0');
90 
91  constant ram_sce_strobe : std_logic_vector(PED_RAM_tSCE_g - 1 downto 0) := (others => '0');
92  signal ram_web_i : std_logic_vector(PED_RAM_tSCE_g - 1 downto 0) := (others => '1');
93  signal load_RAM_WEb_strobe : std_logic := '0';
94 
95 begin
96 
97  tx_dc(0) <= '0' & asic_addr(0);
98  tx_dc(1) <= ('0' & asic_addr(1)) + "0101";
99 
100  addr(0) <= tx_dc(0) & chan_addr(0) & win_addr & samp_addr(0) & "0"; -- samp start must always be even
101  addr(1) <= tx_dc(1) & chan_addr(1) & win_addr & samp_addr(1) & "0"; -- samp start must always be even
102 
103  -- pack (here): pedarr(0) <= even_sample(*)(11 downto 4);
104  -- pack (here): pedarr(1) <= even_sample(*)(3 downto 0) & odd_sample(*)(11 downto 8);
105  -- pack (here): pedarr(2) <= odd_sample(*)(7 downto 0);
106  -- unpack: even_sample <= pedarr(0) & pedarr(1)(7 downto 4);
107  -- unpack: odd_sample <= pedarr(1)(3 downto 0) & pedarr(2);
108 
109  ce_shift_reg : process(clk, RAM_WEb_i, load_RAM_WEb_strobe)
110  begin
111  if rising_edge(clk) then
112  if load_RAM_WEb_strobe = '1' then
113  RAM_WEb_i <= ram_sce_strobe;
114  else
115  RAM_WEb_i <= RAM_WEb_i(RAM_WEb_i'left - 1 downto 0) & '1';
116  end if;
117  end if;
118  end process;
119  RAM_WEb <= RAM_WEb_i(RAM_WEb_i'left);
120 
121  write_one_window_of_peds : process(clk, ena, pedarr, addr, next_addr)
122  variable byte_cnt : integer range 0 to 2 := 0; -- count groups of 3 adddresses
123  variable timer : integer range 0 to (PED_RAM_tSCE_g + PED_RAM_tLZWE_g) := 0;
124  begin
125  if (rising_edge(clk)) then
126  case ped_write_state is
127 
128  when IDLE =>
129  -- ped_wr_state <= "00";
130  byte_cnt := 0;
131  timer := 0;
132  ack <= "00";
133  -- unfair arbiter (biased toward bus 0)
134  if ena(0) = '1' then
135  ack <= "01";
136  next_addr <= addr(0) + ("0" & addr(0)(21 downto 1)); -- easy 1.5x for even numbers only!
137  pedarr(0) <= even_sample(0)(11 downto 4);
138  pedarr(1) <= even_sample(0)(3 downto 0) & odd_sample(0)(11 downto 8);
139  pedarr(2) <= odd_sample(0)(7 downto 0);
140  elsif ena(1) = '1' then
141  ack <= "10";
142  next_addr <= addr(1) + ("0" & addr(1)(21 downto 1));
143  pedarr(0) <= even_sample(1)(11 downto 4);
144  pedarr(1) <= even_sample(1)(3 downto 0) & odd_sample(1)(11 downto 8);
145  pedarr(2) <= odd_sample(1)(7 downto 0);
146  end if;
147  if (ena(0) or ena(1)) = '1' then
148  load_RAM_WEb_strobe <= '1';
149  ped_write_state <= WRITE_TO_SRAM;
150  else
151  ped_write_state <= IDLE;
152  end if;
153 
154 
155  When WRITE_TO_SRAM =>
156  -- ped_wr_state <= "01";
157  load_RAM_WEb_strobe <= '0';
158  if timer > 1 then
159  ack <= "00";
160  end if;
161  RAM_ADDR <= next_addr;
162  RAM_din <= pedarr(byte_cnt);
163  if timer < (PED_RAM_tSCE_g + PED_RAM_tLZWE_g - 1) then
164  timer := timer + 1;
165  ped_write_state <= WRITE_TO_SRAM;
166  else
167  timer := 0;
168  ped_write_state <= CHECK_DONE;
169  end if;
170 
171 
172  When CHECK_DONE =>
173  -- ped_wr_state <= "10";
174  if byte_cnt < 2 then
175  byte_cnt := byte_cnt + 1;
176  next_addr <= next_addr + "0000000000000000000001";
177  load_RAM_WEb_strobe <= '1';
178  ped_write_state <= WRITE_TO_SRAM;
179  else
180  byte_cnt := 0;
181  ped_write_state <= IDLE;
182  end if;
183 
184  end case;
185  end if;
186  end process;
187 
188 
189 
190 end Behavioral;