| 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_writer is |
| 30 |
generic ( |
| 31 |
NUM_STREAMS : natural := 1; |
| 32 |
FIFO_USEDW_WIDTH : natural := 12; |
| 33 |
FIFO_DATA_WIDTH : natural := 32; |
| 34 |
META_FIFO_USEDW_WIDTH : natural := 5; |
| 35 |
META_FIFO_DATA_WIDTH : natural := 128 |
| 36 |
); |
| 37 |
port ( |
| 38 |
clock : in std_logic; |
| 39 |
reset : in std_logic; |
| 40 |
enable : in std_logic; |
| 41 |
|
| 42 |
usb_speed : in std_logic; |
| 43 |
meta_en : in std_logic; |
| 44 |
packet_en : in std_logic; |
| 45 |
eight_bit_mode_en : in std_logic := '0'; |
| 46 |
highly_packed_mode_en : in std_logic; |
| 47 |
timestamp : in unsigned(63 downto 0); |
| 48 |
mini_exp : in std_logic_vector(1 downto 0); |
| 49 |
|
| 50 |
in_sample_controls : in sample_controls_t(0 to NUM_STREAMS-1) := (others => SAMPLE_CONTROL_DISABLE); |
| 51 |
in_samples : in sample_streams_t(0 to NUM_STREAMS-1) := (others => ZERO_SAMPLE); |
| 52 |
|
| 53 |
fifo_usedw : in std_logic_vector(FIFO_USEDW_WIDTH-1 downto 0); |
| 54 |
fifo_clear : buffer std_logic; |
| 55 |
fifo_write : buffer std_logic := '0'; |
| 56 |
fifo_full : in std_logic; |
| 57 |
fifo_data : out std_logic_vector(FIFO_DATA_WIDTH-1 downto 0) := (others => '0'); |
| 58 |
|
| 59 |
packet_control : in packet_control_t; |
| 60 |
packet_ready : out std_logic; |
| 61 |
|
| 62 |
meta_fifo_full : in std_logic; |
| 63 |
meta_fifo_usedw : in std_logic_vector(META_FIFO_USEDW_WIDTH-1 downto 0); |
| 64 |
meta_fifo_data : out std_logic_vector(META_FIFO_DATA_WIDTH-1 downto 0) := (others => '0'); |
| 65 |
meta_fifo_write : out std_logic := '0'; |
| 66 |
|
| 67 |
overflow_led : buffer std_logic; |
| 68 |
overflow_count : buffer unsigned(63 downto 0); |
| 69 |
overflow_duration : in unsigned(15 downto 0) |
| 70 |
); |
| 71 |
end entity; |
| 72 |
|
| 73 |
architecture simple of fifo_writer is |
| 74 |
|
| 75 |
constant DMA_BUF_SIZE_SS : natural := GPIF_BUF_SIZE_SS; |
| 76 |
constant DMA_BUF_SIZE_HS : natural := GPIF_BUF_SIZE_HS; |
| 77 |
|
| 78 |
signal dma_buf_size : natural range DMA_BUF_SIZE_HS to DMA_BUF_SIZE_SS := DMA_BUF_SIZE_SS; |
| 79 |
|
| 80 |
signal fifo_enough : boolean := false; |
| 81 |
signal overflow_detected : std_logic := '0'; |
| 82 |
|
| 83 |
type meta_state_t is ( |
| 84 |
IDLE, |
| 85 |
META_WRITE, |
| 86 |
META_DOWNCOUNT, |
| 87 |
PACKET_WAIT_EOP |
| 88 |
); |
| 89 |
|
| 90 |
type meta_fsm_t is record |
| 91 |
state : meta_state_t; |
| 92 |
dma_downcount : natural range 0 to 65536; |
| 93 |
meta_write : std_logic; |
| 94 |
meta_data : std_logic_vector(meta_fifo_data'range); |
| 95 |
meta_written : std_logic; |
| 96 |
end record; |
| 97 |
|
| 98 |
constant META_FSM_RESET_VALUE : meta_fsm_t := ( |
| 99 |
state => IDLE, |
| 100 |
dma_downcount => 0, |
| 101 |
meta_write => '0', |
| 102 |
meta_data => (others => '-'), |
| 103 |
meta_written => '0' |
| 104 |
); |
| 105 |
|
| 106 |
signal meta_current : meta_fsm_t := META_FSM_RESET_VALUE; |
| 107 |
signal meta_future : meta_fsm_t := META_FSM_RESET_VALUE; |
| 108 |
|
| 109 |
type fifo_state_t is ( |
| 110 |
CLEAR, |
| 111 |
WRITE_SAMPLES, |
| 112 |
WRITE_PACKET_PAYLOAD, |
| 113 |
HOLDOFF |
| 114 |
); |
| 115 |
|
| 116 |
type ch_offsets_t is array( natural range <> ) of natural range fifo_data'low to fifo_data'high; |
| 117 |
|
| 118 |
type fifo_fsm_t is record |
| 119 |
state : fifo_state_t; |
| 120 |
fifo_clear : std_logic; |
| 121 |
fifo_write : std_logic; |
| 122 |
fifo_data : unsigned(fifo_data'range); |
| 123 |
fifo_12b_buf : unsigned(95 downto 0); |
| 124 |
write_cycle : natural range 0 to 3; |
| 125 |
samples_left : natural range 0 to in_sample_controls'length; |
| 126 |
in_sample_controls_r: sample_controls_t(0 to NUM_STREAMS-1); |
| 127 |
in_samples_r : sample_streams_t(0 to NUM_STREAMS-1); |
| 128 |
eight_bit_delay : std_logic; |
| 129 |
end record; |
| 130 |
|
| 131 |
constant FIFO_FSM_RESET_VALUE : fifo_fsm_t := ( |
| 132 |
state => CLEAR, |
| 133 |
fifo_clear => '1', |
| 134 |
fifo_write => '0', |
| 135 |
fifo_data => (others => '-'), |
| 136 |
fifo_12b_buf => (others => '-'), |
| 137 |
write_cycle => 0, |
| 138 |
samples_left => 0, |
| 139 |
in_sample_controls_r=> (others => SAMPLE_CONTROL_DISABLE), |
| 140 |
in_samples_r => (others => ZERO_SAMPLE), |
| 141 |
eight_bit_delay => '0' |
| 142 |
); |
| 143 |
|
| 144 |
signal fifo_current : fifo_fsm_t := FIFO_FSM_RESET_VALUE; |
| 145 |
signal fifo_future : fifo_fsm_t := FIFO_FSM_RESET_VALUE; |
| 146 |
|
| 147 |
signal sync_mini_exp: std_logic_vector(1 downto 0); |
| 148 |
|
| 149 |
signal meta_fifo_used_v_r : unsigned(meta_fifo_usedw'length downto 0) := (others => '0'); |
| 150 |
signal fifo_used_v_r : unsigned(fifo_usedw'length downto 0) := (others => '0'); |
| 151 |
|
| 152 |
begin |
| 153 |
|
| 154 |
-- Throw an error if port widths don't make sense |
| 155 |
assert (fifo_data'length >= (NUM_STREAMS*2*in_samples(in_samples'low).data_i'length) ) |
| 156 |
report "fifo_data port width too narrow to support " & integer'image(NUM_STREAMS) & " MIMO streams." |
| 157 |
severity failure; |
| 158 |
|
| 159 |
-- Determine the DMA buffer size based on USB speed |
| 160 |
calc_buf_size : process( clock, reset ) |
| 161 |
begin |
| 162 |
if( reset = '1' ) then |
| 163 |
dma_buf_size <= DMA_BUF_SIZE_SS; |
| 164 |
elsif( rising_edge(clock) ) then |
| 165 |
if( usb_speed = '0' ) then |
| 166 |
dma_buf_size <= DMA_BUF_SIZE_SS; |
| 167 |
else |
| 168 |
dma_buf_size <= DMA_BUF_SIZE_HS; |
| 169 |
end if; |
| 170 |
end if; |
| 171 |
end process; |
| 172 |
|
| 173 |
-- Calculate whether there's enough room in the destination FIFO |
| 174 |
calc_fifo_free : process( clock, reset ) |
| 175 |
constant FIFO_MAX : natural := 2**fifo_usedw'length; |
| 176 |
constant META_MAX : natural := 2**meta_fifo_usedw'length; |
| 177 |
variable fifo_used_v : unsigned(fifo_usedw'length downto 0) := (others => '0'); |
| 178 |
variable meta_fifo_used_v : unsigned(meta_fifo_usedw'length downto 0) := (others => '0'); |
| 179 |
begin |
| 180 |
if( reset = '1' ) then |
| 181 |
fifo_enough <= false; |
| 182 |
fifo_used_v := (others => '0'); |
| 183 |
meta_fifo_used_v_r <= (others => '0'); |
| 184 |
fifo_used_v_r <= (others => '0'); |
| 185 |
elsif( rising_edge(clock) ) then |
| 186 |
-- the outputs of the dcfifo are not registered so give the long combinatorial |
| 187 |
-- data path, let's register the values. one extra clock cycle will not change |
| 188 |
-- the fidelity of the result. the meta_fifo represents at minimum 204 timestamps. |
| 189 |
-- there is no way for an off by 1 clock cycle timing to overflow the meta fifo |
| 190 |
-- when the buffer leaves 4 full meta buffers empty |
| 191 |
fifo_used_v := unsigned(fifo_full & fifo_usedw); |
| 192 |
fifo_used_v_r <= fifo_used_v; |
| 193 |
meta_fifo_used_v := unsigned(meta_fifo_full & meta_fifo_usedw); |
| 194 |
meta_fifo_used_v_r <= meta_fifo_used_v; |
| 195 |
|
| 196 |
if( fifo_full = '0' and ((FIFO_MAX - fifo_used_v_r) > ( dma_buf_size )) and |
| 197 |
( ( meta_en = '1' and meta_fifo_full = '0' and ( META_MAX - 4 ) > meta_fifo_used_v_r ) |
| 198 |
or (meta_en = '0') ) ) then |
| 199 |
fifo_enough <= true; |
| 200 |
else |
| 201 |
fifo_enough <= false; |
| 202 |
end if; |
| 203 |
end if; |
| 204 |
end process; |
| 205 |
|
| 206 |
-- ------------------------------------------------------------------------ |
| 207 |
-- MINI EXP PIN SYNCHRONIZER |
| 208 |
-- ------------------------------------------------------------------------ |
| 209 |
generate_sync_mimo_rx_en : for i in mini_exp'range generate |
| 210 |
U_sync_mini_exp : entity work.synchronizer |
| 211 |
generic map ( |
| 212 |
RESET_LEVEL => '0' |
| 213 |
) |
| 214 |
port map ( |
| 215 |
reset => '0', |
| 216 |
clock => clock, |
| 217 |
async => mini_exp(i), |
| 218 |
sync => sync_mini_exp(i) |
| 219 |
); |
| 220 |
end generate; |
| 221 |
|
| 222 |
-- ------------------------------------------------------------------------ |
| 223 |
-- META FIFO WRITER |
| 224 |
-- ------------------------------------------------------------------------ |
| 225 |
|
| 226 |
-- Meta FIFO synchronous process |
| 227 |
meta_fsm_sync : process( clock, reset ) |
| 228 |
begin |
| 229 |
if( reset = '1' ) then |
| 230 |
meta_current <= META_FSM_RESET_VALUE; |
| 231 |
elsif( rising_edge(clock) ) then |
| 232 |
meta_current <= meta_future; |
| 233 |
end if; |
| 234 |
end process; |
| 235 |
|
| 236 |
packet_ready <= '1' when fifo_enough else '0'; |
| 237 |
|
| 238 |
-- Meta FIFO combinatorial process |
| 239 |
meta_fsm_comb : process( all ) |
| 240 |
variable packet_flags : std_logic_vector(7 downto 0); |
| 241 |
begin |
| 242 |
|
| 243 |
meta_future <= meta_current; |
| 244 |
|
| 245 |
meta_future.meta_write <= '0'; |
| 246 |
-- currently the GPIF modules overwrites the bottom 16 bits of the flags field |
| 247 |
if( packet_en = '0' ) then |
| 248 |
meta_future.meta_data <= x"FFF" & "11" & sync_mini_exp & x"FFFF" & std_logic_vector(timestamp) & x"12344321"; |
| 249 |
else |
| 250 |
packet_flags := packet_control.pkt_flags; |
| 251 |
meta_future.meta_data <= x"FFF" & "11" & sync_mini_exp & x"FFFF" & std_logic_vector(timestamp) & |
| 252 |
packet_control.pkt_core_id & packet_flags & |
| 253 |
std_logic_vector(to_unsigned(integer(meta_current.dma_downcount), 16)); |
| 254 |
end if; |
| 255 |
|
| 256 |
|
| 257 |
case meta_current.state is |
| 258 |
when IDLE => |
| 259 |
|
| 260 |
meta_future.dma_downcount <= dma_buf_size - 4; |
| 261 |
|
| 262 |
if( fifo_enough ) then |
| 263 |
if( packet_en = '1' ) then |
| 264 |
-- use downcount to count number of DWORDS for packets |
| 265 |
|
| 266 |
if( packet_control.pkt_sop = '1' ) then |
| 267 |
meta_future.state <= PACKET_WAIT_EOP; |
| 268 |
|
| 269 |
-- meta is not written yet, but there should be space |
| 270 |
meta_future.meta_written <= '1'; |
| 271 |
|
| 272 |
-- EOP and VALID must be asserted together, count the last VALID now |
| 273 |
if( packet_control.data_valid = '1') then |
| 274 |
meta_future.dma_downcount <= 2; |
| 275 |
else |
| 276 |
meta_future.dma_downcount <= 1; |
| 277 |
end if; |
| 278 |
end if; |
| 279 |
else |
| 280 |
meta_future.state <= META_WRITE; |
| 281 |
end if; |
| 282 |
else |
| 283 |
meta_future.meta_written <= '0'; |
| 284 |
end if; |
| 285 |
|
| 286 |
when META_WRITE => |
| 287 |
|
| 288 |
for i in in_samples'range loop |
| 289 |
if (meta_fifo_full = '0' and in_samples(i).data_v = '1') then |
| 290 |
meta_future.meta_write <= '1'; |
| 291 |
meta_future.meta_written <= '1'; |
| 292 |
meta_future.state <= META_DOWNCOUNT; |
| 293 |
end if; |
| 294 |
end loop; |
| 295 |
|
| 296 |
when META_DOWNCOUNT => |
| 297 |
|
| 298 |
if( fifo_current.fifo_write = '1' and meta_current.meta_write = '0' ) then |
| 299 |
meta_future.dma_downcount <= meta_current.dma_downcount - NUM_STREAMS; |
| 300 |
end if; |
| 301 |
|
| 302 |
if( meta_current.dma_downcount <= 2 ) then |
| 303 |
-- Look for 2 because of the 2 cycles passing |
| 304 |
-- through IDLE and META_WRITE after this. |
| 305 |
-- 8bit format requires an additional 2 cycle delay. |
| 306 |
if( eight_bit_mode_en = '1' ) then |
| 307 |
if( fifo_future.eight_bit_delay = '1' and fifo_future.samples_left = 0) then |
| 308 |
meta_future.state <= IDLE; |
| 309 |
end if; |
| 310 |
else |
| 311 |
meta_future.state <= IDLE; |
| 312 |
end if; |
| 313 |
end if; |
| 314 |
|
| 315 |
-- Patches the late meta write for MIMO mode |
| 316 |
if( in_sample_controls'length = 2 and |
| 317 |
in_sample_controls(0).enable = '1' and |
| 318 |
in_sample_controls(1).enable = '1' and |
| 319 |
eight_bit_mode_en = '0' and |
| 320 |
meta_current.dma_downcount <= NUM_STREAMS + 2 ) |
| 321 |
then |
| 322 |
meta_future.state <= IDLE; |
| 323 |
end if; |
| 324 |
|
| 325 |
when PACKET_WAIT_EOP => |
| 326 |
|
| 327 |
if( packet_control.data_valid = '1' ) then |
| 328 |
meta_future.dma_downcount <= meta_current.dma_downcount + 1; |
| 329 |
|
| 330 |
if( packet_control.pkt_eop = '1' ) then |
| 331 |
meta_future.meta_write <= '1'; |
| 332 |
meta_future.state <= IDLE; |
| 333 |
end if; |
| 334 |
end if; |
| 335 |
|
| 336 |
when others => |
| 337 |
|
| 338 |
meta_future.state <= IDLE; |
| 339 |
|
| 340 |
end case; |
| 341 |
|
| 342 |
-- Abort? |
| 343 |
if( (enable = '0') or (meta_en = '0') ) then |
| 344 |
meta_future.meta_write <= '0'; |
| 345 |
meta_future.meta_written <= '0'; |
| 346 |
meta_future.state <= IDLE; |
| 347 |
end if; |
| 348 |
|
| 349 |
-- Output assignments |
| 350 |
meta_fifo_write <= meta_current.meta_write; |
| 351 |
meta_fifo_data <= meta_current.meta_data; |
| 352 |
|
| 353 |
end process; |
| 354 |
|
| 355 |
|
| 356 |
-- ------------------------------------------------------------------------ |
| 357 |
-- SAMPLE FIFO WRITER |
| 358 |
-- ------------------------------------------------------------------------ |
| 359 |
|
| 360 |
-- Sample FIFO synchronous process |
| 361 |
fifo_fsm_sync : process( clock, reset ) |
| 362 |
begin |
| 363 |
if( reset = '1' ) then |
| 364 |
fifo_current <= FIFO_FSM_RESET_VALUE; |
| 365 |
elsif( rising_edge(clock) ) then |
| 366 |
fifo_current <= fifo_future; |
| 367 |
end if; |
| 368 |
end process; |
| 369 |
|
| 370 |
-- Sample FIFO combinatorial process |
| 371 |
fifo_fsm_comb : process( all ) |
| 372 |
|
| 373 |
-- -------------------------------------------------------------------- |
| 374 |
-- MIMO PACKER: STEP 1 of 3 |
| 375 |
-- -------------------------------------------------------------------- |
| 376 |
-- This block receives samples as an array of sample streams, one |
| 377 |
-- element per channel. These streams need to be packed into a single, |
| 378 |
-- wide bus that is written to a FIFO and eventually delivered to the |
| 379 |
-- host. When all channels are enabled, this wide bus will contain one |
| 380 |
-- I/Q sample pair for each channel. When channels are disabled, the |
| 381 |
-- wide bus may contain multiple samples from the remaining enabled |
| 382 |
-- channels in order to more efficiently use the available USB bandwidth. |
| 383 |
-- For example, a 2x2 MIMO design with 16-bit samples will pack its data |
| 384 |
-- into a 64-bit wide bus in one of the following ways: |
| 385 |
-- | 63:48 | 47:32 | 31:16 | 15:0 | Bit indices |
| 386 |
-- 1. | Q1 | I1 | Q0 | I0 | Channels 0 & 1 enabled |
| 387 |
-- 2. | Q0' | I0' | Q0 | I0 | Channel 0 only enabled |
| 388 |
-- 3. | Q1' | I1' | Q1 | I1 | Channel 1 only enabled |
| 389 |
function pack( sc : sample_controls_t; |
| 390 |
ss : sample_streams_t; |
| 391 |
d : unsigned ) return unsigned is |
| 392 |
constant LEN : natural := ss(ss'low).data_i'length + ss(ss'low).data_q'length; |
| 393 |
variable rv : unsigned(d'range) := (others => '0'); |
| 394 |
begin |
| 395 |
rv := d; |
| 396 |
for i in sc'range loop |
| 397 |
if( (sc(i).enable = '1') and (ss(i).data_v = '1') ) then |
| 398 |
rv := unsigned(ss(i).data_q) & unsigned(ss(i).data_i) & |
| 399 |
rv(rv'high downto rv'low+LEN); |
| 400 |
end if; |
| 401 |
end loop; |
| 402 |
return rv; |
| 403 |
end function; |
| 404 |
|
| 405 |
-- -------------------------------------------------------------------- |
| 406 |
-- MIMO PACKER: STEP 1 of 3 (8bit mode) |
| 407 |
-- -------------------------------------------------------------------- |
| 408 |
-- This block receives samples as an array of sample streams, one |
| 409 |
-- element per channel. These streams need to be packed into a single, |
| 410 |
-- wide bus that is written to a FIFO and eventually delivered to the |
| 411 |
-- host. When all channels are enabled, this wide bus will contain one |
| 412 |
-- I/Q sample pair for each channel. When channels are disabled, the |
| 413 |
-- wide bus may contain multiple samples from the remaining enabled |
| 414 |
-- channels in order to more efficiently use the available USB bandwidth. |
| 415 |
-- For example, a 2x2 MIMO design with 16-bit samples will pack its data |
| 416 |
-- into a 64-bit wide bus in one of the following ways: |
| 417 |
-- | Sample Set 1 | Sample Set 0 | |
| 418 |
-- | 63:56 | 55:48 | 47:40 | 39:32 | 31:24 | 23:16 | 15:8 | 7:0 | Bit indices |
| 419 |
-- 1. | Q1 | I1 | Q0 | I0 | Q1 | I1 | Q0 | I0 | Channels 0 & 1 enabled |
| 420 |
-- 2. | Q0' | I0' | Q0 | I0 | Q0' | I0' | Q0 | I0 | Channel 0 only enabled |
| 421 |
-- 3. | Q1' | I1' | Q1 | I1 | Q1' | I1' | Q1 | I1 | Channel 1 only enabled |
| 422 |
function pack_eight_bit_mode( sc : sample_controls_t; |
| 423 |
ss : sample_streams_t; |
| 424 |
d : unsigned ) return unsigned is |
| 425 |
constant IQ_PAIR_LEN : natural := ss(ss'low).data_i'length/2 + ss(ss'low).data_q'length/2; |
| 426 |
variable rv : unsigned(d'range) := (others => '0'); |
| 427 |
begin |
| 428 |
rv := d; |
| 429 |
for i in sc'range loop |
| 430 |
if( (sc(i).enable = '1') and (ss(i).data_v = '1') ) then |
| 431 |
rv := unsigned(ss(i).data_q(11 downto 4)) & |
| 432 |
unsigned(ss(i).data_i(11 downto 4)) & |
| 433 |
rv(rv'high downto rv'low+IQ_PAIR_LEN); |
| 434 |
end if; |
| 435 |
end loop; |
| 436 |
return rv; |
| 437 |
end function; |
| 438 |
|
| 439 |
|
| 440 |
-- -------------------------------------------------------------------- |
| 441 |
-- MIMO PACKER: STEP 1 of 3 (SC12Q11 Highly Packed Mode) |
| 442 |
-- -------------------------------------------------------------------- |
| 443 |
-- This block receives samples as an array of sample streams, one |
| 444 |
-- element per channel. These streams need to be packed into a single, |
| 445 |
-- wide bus that is written to a FIFO and delivered to the host. |
| 446 |
-- For SC12Q11 (SC16_Q11_PACKED) format, each I and Q sample is 12 bits wide. |
| 447 |
-- |
| 448 |
-- This packing scheme efficiently utilizes the available bandwidth |
| 449 |
-- for the SC12Q11 format, allowing for 8 complete IQ pairs every 3 cycles. |
| 450 |
-- |
| 451 |
-- The packing process fills 3 cycles of a 64-bit buffer with 8 24b IQ pairs: |
| 452 |
-- Cycle 1: | 63:60 | 59:48 | 47:36 | 35:24 | 23:12 | 11:0 | Bit indices |
| 453 |
-- | Q0' | I0 | Q1 | I1 | Q0 | I0 | Samples |
| 454 |
-- [-----][--------full 12-bit samples-----------] |
| 455 |
-- 4-bit |
| 456 |
-- overlap |
| 457 |
-- |
| 458 |
-- Cycle 2: | 63:56 | 55:44 | 43:32 | 31:20 | 19:8 | 7:0 | Bit indices |
| 459 |
-- | I1' | Q0 | I0 | Q1 | I1 | Q0' | Samples |
| 460 |
-- [-----][--------full 12-bit samples--------][---] |
| 461 |
-- 8-bit 8-bit |
| 462 |
-- overlap overlap |
| 463 |
-- |
| 464 |
-- Cycle 3: | 63:52 | 51:40 | 39:28 | 27:16 | 15:4 | 3:0 | Bit indices |
| 465 |
-- | Q1 | I1 | Q0 | I0 | Q1 | I1' | Samples |
| 466 |
-- [--------full 12-bit samples-----------][-----] |
| 467 |
-- 4-bit |
| 468 |
-- overlap |
| 469 |
-- |
| 470 |
-- Note: Samples with apostrophes (') indicate partial samples that span |
| 471 |
-- across cycle boundaries. The complete samples are reconstructed by |
| 472 |
-- combining these partial segments from different cycles. |
| 473 |
function pack_sc12q11( sc : sample_controls_t; |
| 474 |
ss : sample_streams_t; |
| 475 |
d : unsigned ) return unsigned is |
| 476 |
constant IQ_PAIR_LEN : positive := 24; |
| 477 |
variable rv : unsigned(d'range) := (others => '0'); |
| 478 |
begin |
| 479 |
rv := d; |
| 480 |
for i in sc'range loop |
| 481 |
if( (sc(i).enable = '1') and (ss(i).data_v = '1') ) then |
| 482 |
rv := unsigned(ss(i).data_q(11 downto 0)) & |
| 483 |
unsigned(ss(i).data_i(11 downto 0)) & |
| 484 |
rv(rv'high downto rv'low+IQ_PAIR_LEN); |
| 485 |
end if; |
| 486 |
end loop; |
| 487 |
return rv; |
| 488 |
end function; |
| 489 |
|
| 490 |
procedure handle_fifo_write( |
| 491 |
signal fifo_current : in fifo_fsm_t; |
| 492 |
signal meta_current : in meta_fsm_t; |
| 493 |
signal meta_en : in std_logic; |
| 494 |
signal eight_bit_mode_en : in std_logic; |
| 495 |
variable write_req : out std_logic; |
| 496 |
signal fifo_future : out fifo_fsm_t |
| 497 |
) is |
| 498 |
variable in_sample_controls : sample_controls_t(0 to NUM_STREAMS-1); |
| 499 |
variable in_samples : sample_streams_t(0 to NUM_STREAMS-1); |
| 500 |
begin |
| 501 |
in_sample_controls := fifo_current.in_sample_controls_r; |
| 502 |
in_samples := fifo_current.in_samples_r; |
| 503 |
|
| 504 |
if( ((meta_current.meta_written = '1') or (meta_en = '0')) ) then |
| 505 |
-- Check for valid data |
| 506 |
write_req := '0'; |
| 507 |
for i in in_sample_controls'range loop |
| 508 |
if( in_sample_controls(i).enable = '1' ) then |
| 509 |
write_req := write_req or in_samples(i).data_v; |
| 510 |
end if; |
| 511 |
end loop; |
| 512 |
|
| 513 |
-- Received valid data |
| 514 |
if( write_req = '1' ) then |
| 515 |
if( fifo_current.samples_left = 0 ) then |
| 516 |
fifo_future.samples_left <= NUM_STREAMS - count_enabled_channels(in_sample_controls); |
| 517 |
fifo_future.fifo_write <= write_req; |
| 518 |
|
| 519 |
if( eight_bit_mode_en = '1' ) then |
| 520 |
fifo_future.fifo_write <= write_req and fifo_current.eight_bit_delay; |
| 521 |
fifo_future.eight_bit_delay <= not fifo_current.eight_bit_delay; |
| 522 |
end if; |
| 523 |
|
| 524 |
if( highly_packed_mode_en = '1' ) then |
| 525 |
fifo_future.write_cycle <= (fifo_current.write_cycle + 1) mod 4; |
| 526 |
if fifo_current.write_cycle = 0 then |
| 527 |
fifo_future.fifo_write <= '0'; |
| 528 |
end if; |
| 529 |
end if; |
| 530 |
else |
| 531 |
-- MIMO PACKER: STEP 3 of 3 |
| 532 |
fifo_future.samples_left <= fifo_current.samples_left - 1; |
| 533 |
end if; |
| 534 |
end if; |
| 535 |
else |
| 536 |
fifo_future.fifo_write <= '0'; |
| 537 |
end if; |
| 538 |
end; |
| 539 |
|
| 540 |
variable write_req : std_logic := '0'; |
| 541 |
|
| 542 |
begin |
| 543 |
|
| 544 |
fifo_future <= fifo_current; |
| 545 |
|
| 546 |
fifo_future.fifo_clear <= '0'; |
| 547 |
fifo_future.fifo_write <= '0'; |
| 548 |
|
| 549 |
fifo_future.in_samples_r <= in_samples; |
| 550 |
fifo_future.in_sample_controls_r <= in_sample_controls; |
| 551 |
|
| 552 |
-- MIMO PACKER: STEP 1 of 3 |
| 553 |
if( packet_en = '0' ) then |
| 554 |
fifo_future.fifo_data <= pack(fifo_current.in_sample_controls_r, |
| 555 |
fifo_current.in_samples_r, |
| 556 |
fifo_current.fifo_data); |
| 557 |
|
| 558 |
if( highly_packed_mode_en = '1' ) then |
| 559 |
fifo_future.fifo_12b_buf <= pack_sc12q11( |
| 560 |
sc => in_sample_controls, |
| 561 |
ss => in_samples, |
| 562 |
d => fifo_current.fifo_12b_buf |
| 563 |
); |
| 564 |
|
| 565 |
if fifo_current.write_cycle = 1 then |
| 566 |
fifo_future.fifo_data <= fifo_current.fifo_12b_buf( |
| 567 |
fifo_data'length-1 downto fifo_current.fifo_12b_buf'low); |
| 568 |
elsif fifo_current.write_cycle = 2 then |
| 569 |
fifo_future.fifo_data <= fifo_current.fifo_12b_buf( |
| 570 |
fifo_data'length-1+16 downto fifo_current.fifo_12b_buf'low+16); |
| 571 |
elsif fifo_current.write_cycle = 3 then |
| 572 |
fifo_future.fifo_data <= fifo_current.fifo_12b_buf( |
| 573 |
fifo_data'length-1+32 downto fifo_current.fifo_12b_buf'low+32); |
| 574 |
end if; |
| 575 |
end if; |
| 576 |
|
| 577 |
if( eight_bit_mode_en = '1' ) then |
| 578 |
fifo_future.fifo_data <= pack_eight_bit_mode(fifo_current.in_sample_controls_r, |
| 579 |
fifo_current.in_samples_r, |
| 580 |
fifo_current.fifo_data); |
| 581 |
end if; |
| 582 |
end if; |
| 583 |
|
| 584 |
case fifo_current.state is |
| 585 |
|
| 586 |
when CLEAR => |
| 587 |
|
| 588 |
-- MIMO PACKER: STEP 2 of 3 |
| 589 |
-- Compute "samples left" to fill up the fifo_data bus |
| 590 |
fifo_future.samples_left <= NUM_STREAMS - count_enabled_channels(in_sample_controls); |
| 591 |
|
| 592 |
if( enable = '1' ) then |
| 593 |
fifo_future.fifo_clear <= '0'; |
| 594 |
if( packet_en = '1' ) then |
| 595 |
fifo_future.state <= WRITE_PACKET_PAYLOAD; |
| 596 |
fifo_future.samples_left <= NUM_STREAMS - 1; |
| 597 |
else |
| 598 |
fifo_future.state <= WRITE_SAMPLES; |
| 599 |
end if; |
| 600 |
else |
| 601 |
fifo_future.fifo_clear <= '1'; |
| 602 |
fifo_future.eight_bit_delay <= '0'; |
| 603 |
end if; |
| 604 |
|
| 605 |
when WRITE_PACKET_PAYLOAD => |
| 606 |
|
| 607 |
if( meta_current.meta_written = '1' or (fifo_enough and packet_control.pkt_sop = '1' )) then |
| 608 |
-- This code converts DWORD packet data into a fifo_data std_logic_vector |
| 609 |
-- that is controlled by a generic. |
| 610 |
if( packet_control.data_valid = '1' ) then |
| 611 |
if( packet_control.pkt_eop = '1' and fifo_current.samples_left /= 0 ) then |
| 612 |
-- End of packet asserted, however fifo_data is not full so |
| 613 |
-- zero out the unset bits, commit what has been accumulated |
| 614 |
fifo_future.samples_left <= NUM_STREAMS - 1; |
| 615 |
fifo_future.fifo_write <= '1'; |
| 616 |
|
| 617 |
if (fifo_current.fifo_data'high > 31) then |
| 618 |
fifo_future.fifo_data(fifo_future.fifo_data'high |
| 619 |
downto fifo_future.fifo_data'high - 31) <= (others => '0'); |
| 620 |
end if; |
| 621 |
fifo_future.fifo_data(31 downto 0) <= unsigned(packet_control.data); |
| 622 |
elsif( fifo_current.samples_left = 0 ) then |
| 623 |
-- DWORDs perfectly filled fifo_data, commit fifo_data |
| 624 |
fifo_future.samples_left <= NUM_STREAMS - 1; |
| 625 |
fifo_future.fifo_write <= '1'; |
| 626 |
|
| 627 |
if (fifo_current.fifo_data'high > 31) then |
| 628 |
fifo_future.fifo_data <= unsigned(packet_control.data) & |
| 629 |
fifo_current.fifo_data(fifo_current.fifo_data'high downto 32); |
| 630 |
else |
| 631 |
fifo_future.fifo_data <= unsigned(packet_control.data); |
| 632 |
end if; |
| 633 |
else |
| 634 |
fifo_future.fifo_data(fifo_future.fifo_data'high |
| 635 |
downto fifo_future.fifo_data'high - 31) <= |
| 636 |
unsigned(packet_control.data); |
| 637 |
|
| 638 |
if (fifo_current.fifo_data'high > 31) then |
| 639 |
fifo_future.fifo_data(fifo_future.fifo_data'high - 32 downto 0) <= |
| 640 |
(others => '0'); |
| 641 |
end if; |
| 642 |
fifo_future.samples_left <= fifo_current.samples_left - 1; |
| 643 |
end if; |
| 644 |
|
| 645 |
end if; |
| 646 |
end if; |
| 647 |
|
| 648 |
when WRITE_SAMPLES => |
| 649 |
|
| 650 |
handle_fifo_write( |
| 651 |
fifo_current => fifo_current, |
| 652 |
meta_current => meta_current, |
| 653 |
meta_en => meta_en, |
| 654 |
eight_bit_mode_en => eight_bit_mode_en, |
| 655 |
write_req => write_req, |
| 656 |
fifo_future => fifo_future |
| 657 |
); |
| 658 |
|
| 659 |
if( fifo_full = '1' or ( meta_current.meta_written = '0' and meta_en = '1') ) then |
| 660 |
fifo_future.fifo_write <= '0'; |
| 661 |
fifo_future.state <= HOLDOFF; |
| 662 |
fifo_future.write_cycle <= fifo_current.write_cycle; |
| 663 |
fifo_future.fifo_12b_buf <= fifo_current.fifo_12b_buf; |
| 664 |
end if; |
| 665 |
|
| 666 |
when HOLDOFF => |
| 667 |
fifo_future.write_cycle <= fifo_current.write_cycle; |
| 668 |
fifo_future.fifo_12b_buf <= fifo_current.fifo_12b_buf; |
| 669 |
if( fifo_enough ) then |
| 670 |
fifo_future.state <= WRITE_SAMPLES; |
| 671 |
|
| 672 |
handle_fifo_write( |
| 673 |
fifo_current => fifo_current, |
| 674 |
meta_current => meta_current, |
| 675 |
meta_en => meta_en, |
| 676 |
eight_bit_mode_en => eight_bit_mode_en, |
| 677 |
write_req => write_req, |
| 678 |
fifo_future => fifo_future |
| 679 |
); |
| 680 |
|
| 681 |
end if; |
| 682 |
|
| 683 |
when others => |
| 684 |
|
| 685 |
fifo_future.state <= CLEAR; |
| 686 |
|
| 687 |
end case; |
| 688 |
|
| 689 |
-- Abort? |
| 690 |
if( enable = '0' ) then |
| 691 |
fifo_future.fifo_clear <= '1'; |
| 692 |
fifo_future.fifo_write <= '0'; |
| 693 |
fifo_future.state <= CLEAR; |
| 694 |
end if; |
| 695 |
|
| 696 |
-- Output assignments |
| 697 |
fifo_clear <= fifo_current.fifo_clear; |
| 698 |
fifo_write <= fifo_current.fifo_write; |
| 699 |
fifo_data <= std_logic_vector(fifo_current.fifo_data); |
| 700 |
|
| 701 |
end process; |
| 702 |
|
| 703 |
|
| 704 |
-- ------------------------------------------------------------------------ |
| 705 |
-- OVERFLOW |
| 706 |
-- ------------------------------------------------------------------------ |
| 707 |
|
| 708 |
-- Overflow detection |
| 709 |
detect_overflows : process( clock, reset ) |
| 710 |
begin |
| 711 |
if( reset = '1' ) then |
| 712 |
overflow_detected <= '0'; |
| 713 |
elsif( rising_edge( clock ) ) then |
| 714 |
overflow_detected <= '0'; |
| 715 |
if( enable = '1' and in_samples(in_samples'low).data_v = '1' and |
| 716 |
fifo_full = '1' and fifo_current.fifo_clear = '0' ) then |
| 717 |
overflow_detected <= '1'; |
| 718 |
end if; |
| 719 |
end if; |
| 720 |
end process; |
| 721 |
|
| 722 |
-- Count the number of times we overflow, but only if they are discontinuous |
| 723 |
-- meaning we have an overflow condition, a non-overflow condition, then |
| 724 |
-- another overflow condition counts as 2 overflows, but an overflow condition |
| 725 |
-- followed by N overflow conditions counts as a single overflow condition. |
| 726 |
count_overflows : process( clock, reset ) |
| 727 |
variable prev_overflow : std_logic := '0'; |
| 728 |
begin |
| 729 |
if( reset = '1' ) then |
| 730 |
prev_overflow := '0'; |
| 731 |
overflow_count <= (others =>'0'); |
| 732 |
elsif( rising_edge( clock ) ) then |
| 733 |
if( prev_overflow = '0' and overflow_detected = '1' ) then |
| 734 |
overflow_count <= overflow_count + 1; |
| 735 |
end if; |
| 736 |
prev_overflow := overflow_detected; |
| 737 |
end if; |
| 738 |
end process; |
| 739 |
|
| 740 |
-- Active high assertion for overflow_duration when the overflow |
| 741 |
-- condition has been detected. The LED will stay asserted |
| 742 |
-- if multiple overflows have occurred |
| 743 |
blink_overflow_led : process( clock, reset ) |
| 744 |
variable downcount : natural range 0 to 2**overflow_duration'length-1; |
| 745 |
begin |
| 746 |
if( reset = '1' ) then |
| 747 |
downcount := 0; |
| 748 |
overflow_led <= '0'; |
| 749 |
elsif( rising_edge(clock) ) then |
| 750 |
-- Default to not being asserted |
| 751 |
overflow_led <= '0'; |
| 752 |
|
| 753 |
-- Countdown so we can see what happened |
| 754 |
if( overflow_detected = '1' ) then |
| 755 |
downcount := to_integer(overflow_duration); |
| 756 |
elsif( downcount /= 0 ) then |
| 757 |
downcount := downcount - 1; |
| 758 |
overflow_led <= '1'; |
| 759 |
end if; |
| 760 |
end if; |
| 761 |
end process; |
| 762 |
|
| 763 |
end architecture; |