| 1 |
-- Copyright (c) 2017 Nuand LLC |
| 2 |
-- |
| 3 |
-- Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 |
-- of this software and associated documentation files (the "Software"), to deal |
| 5 |
-- in the Software without restriction, including without limitation the rights |
| 6 |
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 |
-- copies of the Software, and to permit persons to whom the Software is |
| 8 |
-- furnished to do so, subject to the following conditions: |
| 9 |
-- |
| 10 |
-- The above copyright notice and this permission notice shall be included in |
| 11 |
-- all copies or substantial portions of the Software. |
| 12 |
-- |
| 13 |
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 |
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 |
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 |
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 |
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 |
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 19 |
-- THE SOFTWARE. |
| 20 |
|
| 21 |
library ieee; |
| 22 |
use ieee.std_logic_1164.all; |
| 23 |
use ieee.numeric_std.all; |
| 24 |
|
| 25 |
library work; |
| 26 |
use work.fifo_readwrite_p.all; |
| 27 |
use work.fx3_gpif_p.all; |
| 28 |
|
| 29 |
entity fifo_reader is |
| 30 |
generic ( |
| 31 |
NUM_STREAMS : natural := 1; |
| 32 |
FIFO_READ_THROTTLE : natural range 0 to 255 := 1; |
| 33 |
FIFO_USEDW_WIDTH : natural := 12; |
| 34 |
FIFO_DATA_WIDTH : natural := 32; |
| 35 |
META_FIFO_USEDW_WIDTH : natural := 3; |
| 36 |
META_FIFO_DATA_WIDTH : natural := 128 |
| 37 |
); |
| 38 |
port ( |
| 39 |
clock : in std_logic; |
| 40 |
reset : in std_logic; |
| 41 |
enable : in std_logic; |
| 42 |
|
| 43 |
usb_speed : in std_logic; |
| 44 |
meta_en : in std_logic; |
| 45 |
packet_en : in std_logic; |
| 46 |
eight_bit_mode_en : in std_logic; |
| 47 |
highly_packed_mode_en : in std_logic; |
| 48 |
timestamp : in unsigned(63 downto 0); |
| 49 |
|
| 50 |
fifo_usedw : in std_logic_vector(FIFO_USEDW_WIDTH-1 downto 0); |
| 51 |
fifo_read : buffer std_logic := '0'; |
| 52 |
fifo_empty : in std_logic; |
| 53 |
fifo_data : in std_logic_vector(FIFO_DATA_WIDTH-1 downto 0); |
| 54 |
fifo_holdoff : in std_logic := '0'; |
| 55 |
|
| 56 |
packet_control : out packet_control_t; |
| 57 |
packet_empty : out std_logic; |
| 58 |
packet_ready : in std_logic; |
| 59 |
|
| 60 |
meta_fifo_usedw : in std_logic_vector(META_FIFO_USEDW_WIDTH-1 downto 0); |
| 61 |
meta_fifo_read : buffer std_logic := '0'; |
| 62 |
meta_fifo_empty : in std_logic; |
| 63 |
meta_fifo_data : in std_logic_vector(META_FIFO_DATA_WIDTH-1 downto 0); |
| 64 |
|
| 65 |
in_sample_controls : in sample_controls_t(0 to NUM_STREAMS-1) := (others => SAMPLE_CONTROL_DISABLE); |
| 66 |
out_samples : out sample_streams_t(0 to NUM_STREAMS-1) := (others => ZERO_SAMPLE); |
| 67 |
|
| 68 |
underflow_led : buffer std_logic; |
| 69 |
underflow_count : buffer unsigned(63 downto 0); |
| 70 |
underflow_duration : in unsigned(15 downto 0) |
| 71 |
); |
| 72 |
end entity; |
| 73 |
|
| 74 |
architecture simple of fifo_reader is |
| 75 |
|
| 76 |
constant DMA_BUF_SIZE_SS : natural := GPIF_BUF_SIZE_SS; |
| 77 |
constant DMA_BUF_SIZE_HS : natural := GPIF_BUF_SIZE_HS; |
| 78 |
constant MAX_TIMESTAMP : unsigned(timestamp'high downto timestamp'low) := (others => '1'); |
| 79 |
|
| 80 |
signal dma_buf_size : natural range DMA_BUF_SIZE_HS to DMA_BUF_SIZE_SS := DMA_BUF_SIZE_SS; |
| 81 |
signal underflow_detected : std_logic := '0'; |
| 82 |
|
| 83 |
type meta_state_t is ( |
| 84 |
META_LOAD, |
| 85 |
META_WAIT, |
| 86 |
META_DOWNCOUNT |
| 87 |
); |
| 88 |
|
| 89 |
type meta_fsm_t is record |
| 90 |
state : meta_state_t; |
| 91 |
dma_downcount : natural range 0 to DMA_BUF_SIZE_SS; |
| 92 |
meta_pkt_sop : std_logic; |
| 93 |
meta_pkt_eop : std_logic; |
| 94 |
skip_padding : std_logic; |
| 95 |
meta_read : std_logic; |
| 96 |
meta_cache : std_logic_vector(META_FIFO_DATA_WIDTH-1 downto 0); |
| 97 |
meta_p_time : unsigned(63 downto 0); |
| 98 |
meta_p_time_r : unsigned(63 downto 0); |
| 99 |
meta_time_go : std_logic; |
| 100 |
meta_fifo_empty : std_logic; |
| 101 |
meta_fifo_data : std_logic_vector(META_FIFO_DATA_WIDTH-1 downto 0); |
| 102 |
end record; |
| 103 |
|
| 104 |
constant META_FSM_RESET_VALUE : meta_fsm_t := ( |
| 105 |
state => META_LOAD, |
| 106 |
dma_downcount => 0, |
| 107 |
meta_pkt_sop => '0', |
| 108 |
meta_pkt_eop => '0', |
| 109 |
skip_padding => '0', |
| 110 |
meta_read => '0', |
| 111 |
meta_cache => (others => '0'), |
| 112 |
meta_p_time => (others => '-'), |
| 113 |
meta_p_time_r => (others => '-'), |
| 114 |
meta_time_go => '0', |
| 115 |
meta_fifo_empty => '1', |
| 116 |
meta_fifo_data => (others => '0') |
| 117 |
); |
| 118 |
|
| 119 |
signal meta_current : meta_fsm_t := META_FSM_RESET_VALUE; |
| 120 |
signal meta_future : meta_fsm_t := META_FSM_RESET_VALUE; |
| 121 |
|
| 122 |
type fifo_state_t is ( |
| 123 |
COMPUTE_ENABLED_CHANNELS, |
| 124 |
COMPUTE_OFFSETS, |
| 125 |
READ_PACKET, |
| 126 |
READ_SAMPLES, |
| 127 |
READ_THROTTLE, |
| 128 |
READ_HOLDOFF |
| 129 |
); |
| 130 |
|
| 131 |
type ch_offsets_t is array( natural range <> ) of natural range fifo_data'low to fifo_data'high; |
| 132 |
|
| 133 |
type fifo_fsm_t is record |
| 134 |
state : fifo_state_t; |
| 135 |
downcount : natural range 0 to FIFO_READ_THROTTLE; |
| 136 |
sample_controls_reg : sample_controls_t(in_sample_controls'range); |
| 137 |
enabled_channels : natural range 0 to in_sample_controls'length; |
| 138 |
ch_shift : natural range 0 to out_samples'high; |
| 139 |
ch_offsets : ch_offsets_t(in_sample_controls'range); |
| 140 |
samples_left_init : natural range 0 to in_sample_controls'length; |
| 141 |
samples_left : natural range 0 to in_sample_controls'length; |
| 142 |
packet_control : packet_control_t; |
| 143 |
packet_data_cache : std_logic_vector(31 downto 0); |
| 144 |
fifo_read : std_logic; |
| 145 |
out_samples : sample_streams_t(out_samples'range); |
| 146 |
eight_bit_sample_sel: std_logic; |
| 147 |
|
| 148 |
-- Highly packed mode signals |
| 149 |
packed_buffer : std_logic_vector(47 downto 0); |
| 150 |
packed_cycle : integer range 0 to 4; |
| 151 |
end record; |
| 152 |
|
| 153 |
constant FIFO_FSM_RESET_VALUE : fifo_fsm_t := ( |
| 154 |
state => COMPUTE_ENABLED_CHANNELS, |
| 155 |
downcount => FIFO_READ_THROTTLE, |
| 156 |
sample_controls_reg => (others => SAMPLE_CONTROL_DISABLE), |
| 157 |
enabled_channels => 0, |
| 158 |
ch_shift => 0, |
| 159 |
ch_offsets => (others => 0), |
| 160 |
samples_left_init => 0, |
| 161 |
samples_left => 0, |
| 162 |
packet_control => PACKET_CONTROL_DEFAULT, |
| 163 |
packet_data_cache => (others => '0'), |
| 164 |
fifo_read => '0', |
| 165 |
out_samples => (others => ZERO_SAMPLE), |
| 166 |
eight_bit_sample_sel => '0', |
| 167 |
packed_buffer => (others => '0'), |
| 168 |
packed_cycle => 0 |
| 169 |
); |
| 170 |
|
| 171 |
signal fifo_current : fifo_fsm_t := FIFO_FSM_RESET_VALUE; |
| 172 |
signal fifo_future : fifo_fsm_t := FIFO_FSM_RESET_VALUE; |
| 173 |
|
| 174 |
begin |
| 175 |
|
| 176 |
-- Throw compile/synthesis error if things don't make sense |
| 177 |
assert ( (in_sample_controls'low = out_samples'low) and |
| 178 |
(in_sample_controls'high = out_samples'high) ) |
| 179 |
report "in_sample_controls must have same range as out_samples" |
| 180 |
severity failure; |
| 181 |
|
| 182 |
-- Determine the DMA buffer size based on USB speed |
| 183 |
calc_buf_size : process( clock, reset ) |
| 184 |
begin |
| 185 |
if( reset = '1' ) then |
| 186 |
dma_buf_size <= DMA_BUF_SIZE_SS; |
| 187 |
elsif( rising_edge(clock) ) then |
| 188 |
if( usb_speed = '0' ) then |
| 189 |
dma_buf_size <= DMA_BUF_SIZE_SS; |
| 190 |
else |
| 191 |
dma_buf_size <= DMA_BUF_SIZE_HS; |
| 192 |
end if; |
| 193 |
end if; |
| 194 |
end process; |
| 195 |
|
| 196 |
|
| 197 |
-- ------------------------------------------------------------------------ |
| 198 |
-- META FIFO FSM |
| 199 |
-- ------------------------------------------------------------------------ |
| 200 |
|
| 201 |
-- Meta FIFO synchronous process |
| 202 |
meta_fsm_sync : process( clock, reset ) |
| 203 |
begin |
| 204 |
if( reset = '1' ) then |
| 205 |
meta_current <= META_FSM_RESET_VALUE; |
| 206 |
elsif( rising_edge(clock) ) then |
| 207 |
meta_current <= meta_future; |
| 208 |
end if; |
| 209 |
end process; |
| 210 |
|
| 211 |
packet_empty <= '1' when ( meta_current.meta_fifo_empty = '1' and meta_current.state /= META_WAIT ) else '0' ; |
| 212 |
|
| 213 |
-- Meta FIFO combinatorial process |
| 214 |
meta_fsm_comb : process( all ) |
| 215 |
constant META_NOW : unsigned(63 downto 0) := (others => '1'); |
| 216 |
variable meta_time : unsigned(63 downto 0); |
| 217 |
variable packet_len : integer; |
| 218 |
begin |
| 219 |
|
| 220 |
meta_future <= meta_current; |
| 221 |
|
| 222 |
meta_future.meta_read <= '0'; |
| 223 |
meta_future.meta_pkt_sop <= '0'; |
| 224 |
meta_future.meta_pkt_eop <= '0'; |
| 225 |
meta_future.meta_fifo_empty <= meta_fifo_empty; |
| 226 |
meta_future.meta_fifo_data <= meta_fifo_data; |
| 227 |
meta_time := unsigned(meta_current.meta_fifo_data(95 downto 32)) - 1; |
| 228 |
meta_future.meta_p_time_r <= meta_time; |
| 229 |
|
| 230 |
case meta_current.state is |
| 231 |
|
| 232 |
when META_LOAD => |
| 233 |
|
| 234 |
meta_future.skip_padding <= '0'; |
| 235 |
meta_future.meta_p_time <= meta_current.meta_p_time_r; |
| 236 |
meta_future.meta_cache <= meta_current.meta_fifo_data; |
| 237 |
|
| 238 |
if( meta_current.dma_downcount = NUM_STREAMS ) then |
| 239 |
meta_future.dma_downcount <= 0; |
| 240 |
end if; |
| 241 |
|
| 242 |
if( fifo_current.ch_shift = 0 ) then |
| 243 |
if( meta_current.meta_fifo_empty = '0' and (packet_en = '0' or |
| 244 |
(packet_en = '1' and packet_ready = '1') ) ) then |
| 245 |
meta_future.meta_read <= '1'; |
| 246 |
meta_future.state <= META_WAIT; |
| 247 |
if( packet_en = '1' or (meta_current.meta_p_time_r > timestamp and meta_current.meta_p_time_r /= META_NOW) ) then |
| 248 |
meta_future.meta_time_go <= '0'; |
| 249 |
else |
| 250 |
meta_future.meta_time_go <= '1'; |
| 251 |
end if; |
| 252 |
else |
| 253 |
meta_future.meta_time_go <= '0'; |
| 254 |
end if; |
| 255 |
end if; |
| 256 |
|
| 257 |
when META_WAIT => |
| 258 |
|
| 259 |
if( packet_en = '1' ) then |
| 260 |
packet_len := to_integer(unsigned(meta_current.meta_cache(15 downto 0))); |
| 261 |
meta_future.dma_downcount <= packet_len - 1; |
| 262 |
else |
| 263 |
meta_future.dma_downcount <= dma_buf_size - 4; |
| 264 |
end if; |
| 265 |
|
| 266 |
if( (timestamp >= meta_current.meta_p_time or meta_current.meta_p_time = MAX_TIMESTAMP) |
| 267 |
and ( packet_en = '0' or ( packet_en = '1' and packet_ready = '1' ) ) ) then |
| 268 |
meta_future.meta_time_go <= '1'; |
| 269 |
meta_future.state <= META_DOWNCOUNT; |
| 270 |
meta_future.meta_pkt_sop <= '1'; |
| 271 |
else |
| 272 |
meta_future.meta_time_go <= '0'; |
| 273 |
end if; |
| 274 |
|
| 275 |
when META_DOWNCOUNT => |
| 276 |
|
| 277 |
meta_future.meta_time_go <= '1'; |
| 278 |
if( packet_en = '0' ) then |
| 279 |
if( fifo_current.fifo_read = '1') then |
| 280 |
meta_future.dma_downcount <= meta_current.dma_downcount - NUM_STREAMS; |
| 281 |
if( meta_current.dma_downcount <= 2 ) then |
| 282 |
-- Look for 2 because of the 2 cycles passing |
| 283 |
-- through META_LOAD and META_WAIT after this. |
| 284 |
meta_future.state <= META_LOAD; |
| 285 |
end if; |
| 286 |
end if; |
| 287 |
else |
| 288 |
if( fifo_current.packet_control.data_valid = '1') then |
| 289 |
meta_future.dma_downcount <= meta_current.dma_downcount - 1; |
| 290 |
end if; |
| 291 |
if( meta_current.dma_downcount <= 1 and packet_ready = '1' ) then |
| 292 |
meta_future.state <= META_LOAD; |
| 293 |
meta_future.meta_pkt_eop <= '1'; |
| 294 |
end if; |
| 295 |
end if; |
| 296 |
|
| 297 |
if( meta_current.meta_cache(0) = '1' ) then |
| 298 |
meta_future.skip_padding <= '1'; |
| 299 |
end if; |
| 300 |
|
| 301 |
when others => |
| 302 |
|
| 303 |
meta_future.state <= META_LOAD; |
| 304 |
|
| 305 |
end case; |
| 306 |
|
| 307 |
-- Abort? |
| 308 |
if( (enable = '0') or (meta_en = '0') ) then |
| 309 |
meta_future <= META_FSM_RESET_VALUE; |
| 310 |
end if; |
| 311 |
|
| 312 |
-- Output assignments |
| 313 |
meta_fifo_read <= meta_current.meta_read; |
| 314 |
|
| 315 |
end process; |
| 316 |
|
| 317 |
|
| 318 |
-- ------------------------------------------------------------------------ |
| 319 |
-- SAMPLE FIFO FSM |
| 320 |
-- ------------------------------------------------------------------------ |
| 321 |
|
| 322 |
-- Sample FIFO synchronous process |
| 323 |
fifo_fsm_sync : process( clock, reset ) |
| 324 |
begin |
| 325 |
if( reset = '1' ) then |
| 326 |
fifo_current <= FIFO_FSM_RESET_VALUE; |
| 327 |
elsif( rising_edge(clock) ) then |
| 328 |
fifo_current <= fifo_future; |
| 329 |
end if; |
| 330 |
end process; |
| 331 |
|
| 332 |
-- Sample FIFO combinatorial process |
| 333 |
fifo_fsm_comb : process( all ) |
| 334 |
|
| 335 |
-- -------------------------------------------------------------------- |
| 336 |
-- MIMO UNPACKER: STEP 1 of 5 |
| 337 |
-- -------------------------------------------------------------------- |
| 338 |
-- The sample FIFO output is a wide data bus that may contain more |
| 339 |
-- than one sample for a given channel. This function unpacks |
| 340 |
-- the bus into an array of sample streams. For example, a 2x2 MIMO |
| 341 |
-- design with 16-bit samples will pack its data into a 64-bit wide |
| 342 |
-- bus in one of the following ways: |
| 343 |
-- | 63:48 | 47:32 | 31:16 | 15:0 | Bits |
| 344 |
-- 1. | Q1 | I1 | Q0 | I0 | Channels 0 & 1 enabled |
| 345 |
-- 2. | Q0' | I0' | Q0 | I0 | Channel 0 only enabled |
| 346 |
-- 3. | Q1' | I1' | Q1 | I1 | Channel 1 only enabled |
| 347 |
-- This function will return an array of length 2. The 0th |
| 348 |
-- element containing I0/Q0, and the 1st element I1/Q1. It is up |
| 349 |
-- to the state machine to select between element 0 and 1 based |
| 350 |
-- on which stream(s) is/are enabled. |
| 351 |
function unpack( c : sample_controls_t; |
| 352 |
d : std_logic_vector ) return sample_streams_t is |
| 353 |
variable rv : sample_streams_t(c'range); |
| 354 |
constant OFFSET_UNIT : natural := rv(rv'low).data_i'length + |
| 355 |
rv(rv'low).data_q'length; |
| 356 |
-- The following 4 constants are platform-specific and perhaps |
| 357 |
-- should be parameters instead. This is good enough for now. |
| 358 |
constant I_HIGH : natural := 11; |
| 359 |
constant I_LOW : natural := 0; |
| 360 |
constant Q_HIGH : natural := 27; |
| 361 |
constant Q_LOW : natural := 16; |
| 362 |
begin |
| 363 |
-- Ensure the array indices are normalized |
| 364 |
assert (c'low = 0) and (c'high >= c'low) |
| 365 |
report "Invalid range for parameter 'c'" |
| 366 |
severity failure; |
| 367 |
|
| 368 |
for i in rv'range loop |
| 369 |
rv(i).data_i := resize(signed(shift_right(unsigned(d),i*OFFSET_UNIT)(I_HIGH downto I_LOW)),rv(i).data_i'length); |
| 370 |
rv(i).data_q := resize(signed(shift_right(unsigned(d),i*OFFSET_UNIT)(Q_HIGH downto Q_LOW)),rv(i).data_q'length); |
| 371 |
rv(i).data_v := '0'; |
| 372 |
end loop; |
| 373 |
|
| 374 |
return rv; |
| 375 |
end function; |
| 376 |
|
| 377 |
-- -------------------------------------------------------------------- |
| 378 |
-- MIMO UNPACKER: STEP 1 of 5 (8-bit/half band mode) |
| 379 |
-- -------------------------------------------------------------------- |
| 380 |
-- The sample FIFO output is a wide data bus that may contain more |
| 381 |
-- than one sample for a given channel. This function unpacks |
| 382 |
-- the bus into an array of sample streams. For example, a 2x2 MIMO |
| 383 |
-- design with 8-bit samples will pack its data into a 64-bit wide |
| 384 |
-- bus in one of the following ways: |
| 385 |
-- | Sample 1 | Sample 0 | |
| 386 |
-- | 63:56 | 55:48 | 47:40 | 39:32 | 31:24 | 23:16 | 15:8 | 7:0 | Bits |
| 387 |
-- 1. | Q1 | I1 | Q0 | I0 | Q1 | I1 | Q0 | I0 | Channels 0 & 1 enabled |
| 388 |
-- 2. | Q0' | I0' | Q0 | I0 | Q0' | I0' | Q0 | I0 | Channel 0 only enabled |
| 389 |
-- 3. | Q1' | I1' | Q1 | I1 | Q1' | I1' | Q1 | I1 | Channel 1 only enabled |
| 390 |
-- |
| 391 |
-- Note: This function is meant to be ran twice to retreive both samples on the bus |
| 392 |
function unpack_eight_bit_mode( c : sample_controls_t; |
| 393 |
d : std_logic_vector; |
| 394 |
sample : std_logic ) return sample_streams_t is |
| 395 |
|
| 396 |
variable rv : sample_streams_t(c'range); |
| 397 |
constant OFFSET_UNIT : natural := 16; |
| 398 |
constant SAMPLE_OFFSET_UNIT : natural := 2 * OFFSET_UNIT; |
| 399 |
-- The following 4 constants are platform-specific and perhaps |
| 400 |
-- should be parameters instead. This is good enough for now. |
| 401 |
constant I_HIGH : natural := 7; |
| 402 |
constant I_LOW : natural := 0; |
| 403 |
constant Q_HIGH : natural := 15; |
| 404 |
constant Q_LOW : natural := 8; |
| 405 |
|
| 406 |
-- 8bit mode constants |
| 407 |
constant SIGMA_DELTA_BITS : signed (3 downto 0) := "0000"; |
| 408 |
begin |
| 409 |
-- Ensure the array indices are normalized |
| 410 |
assert (c'low = 0) and (c'high >= c'low) |
| 411 |
report "Invalid range for parameter 'c'" |
| 412 |
severity failure; |
| 413 |
|
| 414 |
if (sample = '0') then |
| 415 |
for i in rv'range loop |
| 416 |
rv(i).data_i := shift_left(resize(signed(shift_right(unsigned(d),i*OFFSET_UNIT)(I_HIGH downto I_LOW)), rv(i).data_i'length),4); |
| 417 |
rv(i).data_q := shift_left(resize(signed(shift_right(unsigned(d),i*OFFSET_UNIT)(Q_HIGH downto Q_LOW)), rv(i).data_q'length),4); |
| 418 |
rv(i).data_v := '0'; |
| 419 |
end loop; |
| 420 |
elsif (sample = '1') then |
| 421 |
for i in rv'range loop |
| 422 |
rv(i).data_i := shift_left(resize(signed(shift_left(shift_right(unsigned(d),i*OFFSET_UNIT + SAMPLE_OFFSET_UNIT)(I_HIGH downto I_LOW),0)), rv(i).data_i'length),4); |
| 423 |
rv(i).data_q := shift_left(resize(signed(shift_left(shift_right(unsigned(d),i*OFFSET_UNIT + SAMPLE_OFFSET_UNIT)(Q_HIGH downto Q_LOW),0)), rv(i).data_q'length),4); |
| 424 |
rv(i).data_v := '0'; |
| 425 |
end loop; |
| 426 |
else |
| 427 |
report "fifo_reader: Sample choice out of range" severity failure; |
| 428 |
end if; |
| 429 |
|
| 430 |
return rv; |
| 431 |
end function; |
| 432 |
|
| 433 |
-- -------------------------------------------------------------------- |
| 434 |
-- MIMO UNPACKER: STEP 1 of 5 (SC12Q11 Highly Packed Mode) |
| 435 |
-- -------------------------------------------------------------------- |
| 436 |
-- The sample FIFO output is a wide data bus that contains multiple |
| 437 |
-- 12-bit I/Q samples packed across multiple cycles. This procedure unpacks |
| 438 |
-- data from a 64-bit wide bus across three cycles to extract eight 12-bit I/Q pairs. |
| 439 |
-- |
| 440 |
-- The packing scheme for 12-bit samples across three 64-bit cycles: |
| 441 |
-- Cycle 1: | 63:60 | 59:48 | 47:36 | 35:24 | 23:12 | 11:0 | Bit indices |
| 442 |
-- | Q0' | I0 | Q1 | I1 | Q0 | I0 | Samples |
| 443 |
-- [-----][--------full 12-bit samples-----------] |
| 444 |
-- 4-bit |
| 445 |
-- overlap |
| 446 |
-- |
| 447 |
-- Cycle 2: | 63:56 | 55:44 | 43:32 | 31:20 | 19:8 | 7:0 | Bit indices |
| 448 |
-- | I1' | Q0 | I0 | Q1 | I1 | Q0' | Samples |
| 449 |
-- [-----][--------full 12-bit samples--------][---] |
| 450 |
-- 8-bit 8-bit |
| 451 |
-- overlap overlap |
| 452 |
-- |
| 453 |
-- Cycle 3: | 63:52 | 51:40 | 39:28 | 27:16 | 15:4 | 3:0 | Bit indices |
| 454 |
-- | Q1 | I1 | Q0 | I0 | Q1 | I1' | Samples |
| 455 |
-- [--------full 12-bit samples-----------][-----] |
| 456 |
-- 4-bit |
| 457 |
-- overlap |
| 458 |
-- |
| 459 |
-- Note: Samples with apostrophes (') indicate partial samples that span |
| 460 |
-- across cycle boundaries. The complete samples are reconstructed by |
| 461 |
-- combining these partial segments from different cycles. |
| 462 |
procedure unpack_sc12q11( |
| 463 |
signal data : in std_logic_vector(63 downto 0); |
| 464 |
signal previous_data : in std_logic_vector(47 downto 0); |
| 465 |
signal cycle : in integer; |
| 466 |
variable rv : out sample_streams_t |
| 467 |
) is |
| 468 |
constant OFFSET_UNIT : natural := 24; |
| 469 |
begin |
| 470 |
for i in rv'range loop |
| 471 |
rv(i).data_i := resize(signed(shift_right(unsigned(data),i*OFFSET_UNIT)(11 downto 0)),rv(i).data_i'length); |
| 472 |
rv(i).data_q := resize(signed(shift_right(unsigned(data),i*OFFSET_UNIT)(23 downto 12)),rv(i).data_q'length); |
| 473 |
rv(i).data_v := '0'; |
| 474 |
end loop; |
| 475 |
|
| 476 |
if (cycle = 1) then |
| 477 |
rv(0).data_i := resize(signed(previous_data(43 downto 32)),rv(0).data_i'length); |
| 478 |
rv(0).data_q := resize(signed(data(7 downto 0) & previous_data(47 downto 44)),rv(0).data_q'length); |
| 479 |
rv(1).data_i := resize(signed(shift_right(unsigned(data),8)(11 downto 0)),rv(1).data_i'length); |
| 480 |
rv(1).data_q := resize(signed(shift_right(unsigned(data),8)(23 downto 12)),rv(1).data_q'length); |
| 481 |
elsif (cycle = 2) then |
| 482 |
rv(0).data_i := resize(signed(previous_data(27 downto 16)),rv(0).data_i'length); |
| 483 |
rv(0).data_q := resize(signed(previous_data(39 downto 28)),rv(0).data_q'length); |
| 484 |
rv(1).data_i := resize(signed(fifo_data(3 downto 0) & previous_data(47 downto 40)), rv(1).data_i'length); |
| 485 |
rv(1).data_q := resize(signed(fifo_data(15 downto 4)), rv(1).data_q'length); |
| 486 |
elsif (cycle = 3) then |
| 487 |
rv(0).data_i := resize(signed(previous_data(11 downto 0)),rv(0).data_i'length); |
| 488 |
rv(0).data_q := resize(signed(previous_data(23 downto 12)),rv(0).data_q'length); |
| 489 |
rv(1).data_i := resize(signed(previous_data(35 downto 24)),rv(0).data_i'length); |
| 490 |
rv(1).data_q := resize(signed(previous_data(47 downto 36)),rv(0).data_q'length); |
| 491 |
end if; |
| 492 |
end procedure; |
| 493 |
|
| 494 |
-- -------------------------------------------------------------------- |
| 495 |
-- MIMO UNPACKER: STEP 3 of 5 |
| 496 |
-- -------------------------------------------------------------------- |
| 497 |
-- The FIFO data has been unpacked into an array containing I and Q |
| 498 |
-- samples for each of the possible channels they belong to. We need to |
| 499 |
-- figure out which index of this unpacked array belongs to which |
| 500 |
-- channel so we can output it to the correct endpoint, as follows: |
| 501 |
-- a. All channel indices start at 0. |
| 502 |
-- b. For each channel, add 1 to the index for each previous |
| 503 |
-- channel that is enabled. For 2x2 MIMO: |
| 504 |
-- ch_enabled | array index of the unpacked data stream |
| 505 |
-- 0 & 1 | ch0 = 0 ; ch1 = 0 + 1 = 1 |
| 506 |
-- 0 only | ch0 = 0 ; ch1 = - = 0 (ch1 is disabled, doesn't matter) |
| 507 |
-- 1 only | ch0 = - = 0; ch1 = 0 + 0 = 0 (ch0 is disabled, doesn't matter) |
| 508 |
function compute_initial_channel_offsets( x : sample_controls_t ) return ch_offsets_t is |
| 509 |
variable rv : ch_offsets_t(x'range) := (others => 0); |
| 510 |
begin |
| 511 |
rv := (others => 0); |
| 512 |
for i in x'low+1 to x'high loop |
| 513 |
for j in x'low to x'high-1 loop |
| 514 |
if( x(j).enable = '1' ) then |
| 515 |
rv(i) := rv(i) + 1; |
| 516 |
end if; |
| 517 |
end loop; |
| 518 |
end loop; |
| 519 |
return rv; |
| 520 |
end function; |
| 521 |
|
| 522 |
variable unpacked : sample_streams_t(out_samples'range); |
| 523 |
variable read_req : std_logic := '0'; |
| 524 |
|
| 525 |
begin |
| 526 |
|
| 527 |
fifo_future <= fifo_current; |
| 528 |
|
| 529 |
fifo_future.fifo_read <= '0'; |
| 530 |
fifo_future.packet_control.pkt_sop <= meta_current.meta_pkt_sop; |
| 531 |
|
| 532 |
fifo_future.packet_control.data_valid <= '0'; |
| 533 |
-- MIMO UNPACKER: STEP 1 of 5 |
| 534 |
if (highly_packed_mode_en = '1' and NUM_STREAMS = 2) then |
| 535 |
unpack_sc12q11( |
| 536 |
data => fifo_data, |
| 537 |
previous_data => fifo_current.packed_buffer, |
| 538 |
cycle => fifo_current.packed_cycle, |
| 539 |
rv => unpacked |
| 540 |
); |
| 541 |
elsif (eight_bit_mode_en = '1') then |
| 542 |
unpacked := unpack_eight_bit_mode(fifo_current.sample_controls_reg, fifo_data, fifo_current.eight_bit_sample_sel); |
| 543 |
else |
| 544 |
unpacked := unpack(fifo_current.sample_controls_reg, fifo_data); |
| 545 |
end if; |
| 546 |
|
| 547 |
for i in fifo_future.out_samples'range loop |
| 548 |
if( fifo_current.sample_controls_reg(i).enable = '1' ) then |
| 549 |
fifo_future.out_samples(i) <= unpacked(fifo_current.ch_offsets(i) + fifo_current.ch_shift); |
| 550 |
end if; |
| 551 |
end loop; |
| 552 |
|
| 553 |
case fifo_current.state is |
| 554 |
|
| 555 |
when COMPUTE_ENABLED_CHANNELS => |
| 556 |
|
| 557 |
-- MIMO UNPACKER: STEP 2 of 5 |
| 558 |
-- Count the number of enabled channels |
| 559 |
fifo_future.enabled_channels <= count_enabled_channels(in_sample_controls); |
| 560 |
|
| 561 |
-- Register the sample control settings |
| 562 |
fifo_future.sample_controls_reg <= in_sample_controls; |
| 563 |
|
| 564 |
fifo_future.state <= COMPUTE_OFFSETS; |
| 565 |
|
| 566 |
when COMPUTE_OFFSETS => |
| 567 |
|
| 568 |
-- MIMO UNPACKER: STEP 3 of 5 |
| 569 |
fifo_future.ch_offsets <= compute_initial_channel_offsets(fifo_current.sample_controls_reg); |
| 570 |
|
| 571 |
-- MIMO UNPACKER: STEP 4 of 5 |
| 572 |
-- Compute the number of valid samples that each channel has remaining in fifo_data that |
| 573 |
-- still need to be processed (not including the first). This becomes the number of clock |
| 574 |
-- cycles to wait before asserting the FIFO read request to get a new batch of samples. |
| 575 |
fifo_future.samples_left_init <= NUM_STREAMS - fifo_current.enabled_channels; |
| 576 |
|
| 577 |
if (fifo_current.samples_left = FIFO_FSM_RESET_VALUE.samples_left) then |
| 578 |
fifo_future.samples_left <= NUM_STREAMS - fifo_current.enabled_channels; |
| 579 |
fifo_future.ch_shift <= 0; |
| 580 |
end if; |
| 581 |
|
| 582 |
if( packet_en = '1' ) then |
| 583 |
fifo_future.state <= READ_PACKET; |
| 584 |
fifo_future.samples_left <= NUM_STREAMS - 1; |
| 585 |
else |
| 586 |
fifo_future.state <= READ_SAMPLES; |
| 587 |
end if; |
| 588 |
|
| 589 |
when READ_PACKET => |
| 590 |
if( meta_current.meta_pkt_eop = '1' and meta_current.skip_padding = '1') then |
| 591 |
fifo_future.samples_left <= NUM_STREAMS - 1; |
| 592 |
end if; |
| 593 |
|
| 594 |
if( fifo_data'high > 31) then |
| 595 |
if( fifo_current.samples_left = 0) then |
| 596 |
fifo_future.packet_control.data <= fifo_current.packet_data_cache; |
| 597 |
elsif( fifo_current.samples_left = 1) then |
| 598 |
fifo_future.packet_control.data <= fifo_data(31 downto 0); |
| 599 |
fifo_future.packet_data_cache <= fifo_data(63 downto 32); |
| 600 |
end if; |
| 601 |
else |
| 602 |
fifo_future.packet_control.data <= fifo_data(31 downto 0); |
| 603 |
end if; |
| 604 |
|
| 605 |
fifo_future.packet_control.pkt_eop <= '0'; |
| 606 |
|
| 607 |
if( meta_current.meta_time_go = '1' and meta_current.dma_downcount > 0 and packet_ready = '1' ) then |
| 608 |
if( meta_current.dma_downcount = 1 ) then |
| 609 |
fifo_future.packet_control.pkt_eop <= '1'; |
| 610 |
end if; |
| 611 |
fifo_future.packet_control.data_valid <= '1'; |
| 612 |
|
| 613 |
if( fifo_current.samples_left = NUM_STREAMS - 1) then |
| 614 |
fifo_future.fifo_read <= '1'; |
| 615 |
end if; |
| 616 |
|
| 617 |
if( fifo_current.samples_left = 0 ) then |
| 618 |
fifo_future.samples_left <= NUM_STREAMS - 1; |
| 619 |
else |
| 620 |
fifo_future.samples_left <= fifo_current.samples_left - 1; |
| 621 |
end if; |
| 622 |
end if; |
| 623 |
|
| 624 |
when READ_SAMPLES => |
| 625 |
|
| 626 |
fifo_future.downcount <= FIFO_FSM_RESET_VALUE.downcount; |
| 627 |
|
| 628 |
if( fifo_holdoff = '1' ) then |
| 629 |
-- Pause for a spell |
| 630 |
fifo_future.state <= READ_HOLDOFF; |
| 631 |
|
| 632 |
elsif( fifo_empty = '0' and |
| 633 |
(meta_en = '0' or (meta_en = '1' and meta_current.meta_time_go = '1')) ) then |
| 634 |
|
| 635 |
-- Check for valid data request |
| 636 |
read_req := '0'; |
| 637 |
for i in in_sample_controls'range loop |
| 638 |
read_req := read_req or (in_sample_controls(i).data_req and in_sample_controls(i).enable); |
| 639 |
fifo_future.out_samples(i).data_v <= in_sample_controls(i).data_req and |
| 640 |
in_sample_controls(i).enable; |
| 641 |
end loop; |
| 642 |
|
| 643 |
-- Received a valid data request |
| 644 |
if( read_req = '1' ) then |
| 645 |
if( fifo_current.samples_left = 0 ) then |
| 646 |
fifo_future.samples_left <= fifo_current.samples_left_init; |
| 647 |
fifo_future.ch_shift <= 0; |
| 648 |
fifo_future.fifo_read <= read_req; |
| 649 |
if( eight_bit_mode_en = '1' ) then |
| 650 |
fifo_future.fifo_read <= read_req and fifo_current.eight_bit_sample_sel; |
| 651 |
fifo_future.eight_bit_sample_sel <= not fifo_current.eight_bit_sample_sel; |
| 652 |
elsif (highly_packed_mode_en = '1' and NUM_STREAMS = 2) then |
| 653 |
fifo_future.packed_cycle <= (fifo_current.packed_cycle + 1); |
| 654 |
fifo_future.packed_buffer <= fifo_data(fifo_data'high downto fifo_data'high-47); |
| 655 |
if fifo_current.packed_cycle = 3 then |
| 656 |
fifo_future.packed_cycle <= 0; |
| 657 |
fifo_future.fifo_read <= '0'; |
| 658 |
end if; |
| 659 |
end if; |
| 660 |
else |
| 661 |
-- MIMO UNPACKER: STEP 5 of 5 |
| 662 |
-- Add the number of enabled channels to each channel's offset index. |
| 663 |
-- This points that channel to the next valid sample in the current fifo_data. |
| 664 |
fifo_future.ch_shift <= fifo_current.ch_shift + fifo_current.enabled_channels; |
| 665 |
fifo_future.samples_left <= fifo_current.samples_left - 1; |
| 666 |
end if; |
| 667 |
|
| 668 |
if( FIFO_FSM_RESET_VALUE.downcount /= 0 ) then |
| 669 |
fifo_future.state <= READ_THROTTLE; |
| 670 |
end if; |
| 671 |
end if; |
| 672 |
|
| 673 |
end if; |
| 674 |
|
| 675 |
when READ_THROTTLE => |
| 676 |
|
| 677 |
-- If in this state, downcount is guaranteed to be >= 1 |
| 678 |
if( fifo_current.downcount = 1 ) then |
| 679 |
fifo_future.state <= READ_SAMPLES; |
| 680 |
else |
| 681 |
fifo_future.downcount <= fifo_current.downcount - 1; |
| 682 |
end if; |
| 683 |
|
| 684 |
when READ_HOLDOFF => |
| 685 |
|
| 686 |
if( fifo_holdoff = '0' ) then |
| 687 |
fifo_future.state <= READ_SAMPLES; |
| 688 |
end if; |
| 689 |
|
| 690 |
when others => |
| 691 |
|
| 692 |
fifo_future.state <= FIFO_FSM_RESET_VALUE.state; |
| 693 |
|
| 694 |
end case; |
| 695 |
|
| 696 |
-- Abort? |
| 697 |
if( enable = '0' ) then |
| 698 |
fifo_future.fifo_read <= '0'; |
| 699 |
fifo_future.state <= FIFO_FSM_RESET_VALUE.state; |
| 700 |
for i in fifo_current.out_samples'range loop |
| 701 |
fifo_future.out_samples(i).data_v <= '0'; |
| 702 |
end loop; |
| 703 |
end if; |
| 704 |
|
| 705 |
if( fifo_empty = '1' and packet_en = '0' ) then |
| 706 |
-- Re-evaluate the MIMO settings |
| 707 |
fifo_future.state <= FIFO_FSM_RESET_VALUE.state; |
| 708 |
end if; |
| 709 |
|
| 710 |
-- Output assignments |
| 711 |
fifo_read <= fifo_current.fifo_read; |
| 712 |
out_samples <= fifo_current.out_samples; |
| 713 |
|
| 714 |
packet_control <= fifo_current.packet_control; |
| 715 |
|
| 716 |
end process; |
| 717 |
|
| 718 |
|
| 719 |
-- ------------------------------------------------------------------------ |
| 720 |
-- UNDERFLOW |
| 721 |
-- ------------------------------------------------------------------------ |
| 722 |
|
| 723 |
-- Underflow detection |
| 724 |
detect_underflows : process( clock, reset ) |
| 725 |
begin |
| 726 |
if( reset = '1' ) then |
| 727 |
underflow_detected <= '0'; |
| 728 |
elsif( rising_edge( clock ) ) then |
| 729 |
underflow_detected <= '0'; |
| 730 |
if( enable = '1' and fifo_empty = '1' and |
| 731 |
(meta_en = '0' or (meta_en = '1' and meta_current.meta_time_go = '1')) ) then |
| 732 |
underflow_detected <= '1'; |
| 733 |
end if; |
| 734 |
end if; |
| 735 |
end process; |
| 736 |
|
| 737 |
-- Count the number of times we underflow, but only if they are discontinuous |
| 738 |
-- meaning we have an underflow condition, a non-underflow condition, then |
| 739 |
-- another underflow condition counts as 2 underflows, but an underflow condition |
| 740 |
-- followed by N underflow conditions counts as a single underflow condition. |
| 741 |
count_underflows : process( clock, reset ) |
| 742 |
variable prev_underflow : std_logic := '0'; |
| 743 |
begin |
| 744 |
if( reset = '1' ) then |
| 745 |
prev_underflow := '0'; |
| 746 |
underflow_count <= (others =>'0'); |
| 747 |
elsif( rising_edge( clock ) ) then |
| 748 |
if( prev_underflow = '0' and underflow_detected = '1' ) then |
| 749 |
underflow_count <= underflow_count + 1; |
| 750 |
end if; |
| 751 |
prev_underflow := underflow_detected; |
| 752 |
end if; |
| 753 |
end process; |
| 754 |
|
| 755 |
-- Active high assertion for underflow_duration when the underflow |
| 756 |
-- condition has been detected. The LED will stay asserted |
| 757 |
-- if multiple underflows have occurred |
| 758 |
blink_underflow_led : process( clock, reset ) |
| 759 |
variable downcount : natural range 0 to 2**underflow_duration'length-1 := 0; |
| 760 |
begin |
| 761 |
if( reset = '1' ) then |
| 762 |
downcount := 0; |
| 763 |
underflow_led <= '0'; |
| 764 |
elsif( rising_edge(clock) ) then |
| 765 |
-- Default to not being asserted |
| 766 |
underflow_led <= '0'; |
| 767 |
|
| 768 |
-- Countdown so we can see what happened |
| 769 |
if( downcount /= 0 ) then |
| 770 |
downcount := downcount - 1; |
| 771 |
underflow_led <= '1'; |
| 772 |
end if; |
| 773 |
|
| 774 |
-- Underflow occurred so light it up |
| 775 |
if( underflow_detected = '1' ) then |
| 776 |
downcount := to_integer(underflow_duration); |
| 777 |
end if; |
| 778 |
end if; |
| 779 |
end process; |
| 780 |
|
| 781 |
end architecture; |