1 |
c62f5734
|
Christian Daniel
|
---------------------------------------------------------------------------------------------------
|
2 |
|
|
-- Filename : usbrx_halfband.vhd
|
3 |
|
|
-- Project : OsmoSDR FPGA Firmware
|
4 |
|
|
-- Purpose : Programmable sample value offset
|
5 |
|
|
---------------------------------------------------------------------------------------------------
|
6 |
|
|
|
7 |
|
|
-----------------------------------------------------------------------------------
|
8 |
|
|
-- Copyright (C) 2012 maintech GmbH, Otto-Hahn-Str. 15, 97204 Hoechberg, Germany --
|
9 |
|
|
-- written by Matthias Kleffel --
|
10 |
|
|
-- --
|
11 |
|
|
-- This program is free software; you can redistribute it and/or modify --
|
12 |
|
|
-- it under the terms of the GNU General Public License as published by --
|
13 |
|
|
-- the Free Software Foundation as version 3 of the License, or --
|
14 |
|
|
-- --
|
15 |
|
|
-- This program is distributed in the hope that it will be useful, --
|
16 |
|
|
-- but WITHOUT ANY WARRANTY; without even the implied warranty of --
|
17 |
|
|
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the --
|
18 |
|
|
-- GNU General Public License V3 for more details. --
|
19 |
|
|
-- --
|
20 |
|
|
-- You should have received a copy of the GNU General Public License --
|
21 |
|
|
-- along with this program. If not, see <http://www.gnu.org/licenses/>. --
|
22 |
|
|
-----------------------------------------------------------------------------------
|
23 |
|
|
|
24 |
|
|
library ieee;
|
25 |
|
|
use ieee.std_logic_1164.all;
|
26 |
|
|
use ieee.numeric_std.all;
|
27 |
|
|
library work;
|
28 |
|
|
use work.all;
|
29 |
|
|
use work.mt_toolbox.all;
|
30 |
|
|
use work.usbrx.all;
|
31 |
|
|
|
32 |
|
|
entity usbrx_offset is
|
33 |
|
|
port (
|
34 |
|
|
-- common
|
35 |
|
|
clk : in std_logic;
|
36 |
|
|
reset : in std_logic;
|
37 |
|
|
|
38 |
|
|
-- config
|
39 |
|
|
config : in usbrx_off_config_t;
|
40 |
|
|
|
41 |
|
|
-- input
|
42 |
|
|
in_clk : in std_logic;
|
43 |
|
|
in_i : in unsigned(13 downto 0);
|
44 |
|
|
in_q : in unsigned(13 downto 0);
|
45 |
|
|
|
46 |
|
|
-- output
|
47 |
|
|
out_clk : out std_logic;
|
48 |
|
|
out_i : out signed(15 downto 0);
|
49 |
|
|
out_q : out signed(15 downto 0)
|
50 |
|
|
);
|
51 |
|
|
end usbrx_offset;
|
52 |
|
|
|
53 |
|
|
architecture rtl of usbrx_offset is
|
54 |
|
|
|
55 |
|
|
-- clip & saturate sample
|
56 |
|
|
function doClipValue(x : signed) return signed is
|
57 |
83340d0b
|
Christian Daniel
|
variable xnorm : signed(x'length-1 downto 0) := x;
|
58 |
c62f5734
|
Christian Daniel
|
begin
|
59 |
83340d0b
|
Christian Daniel
|
if xnorm >= 32768 then
|
60 |
c62f5734
|
Christian Daniel
|
-- overflow
|
61 |
|
|
return to_signed(+32767,16);
|
62 |
83340d0b
|
Christian Daniel
|
elsif xnorm < -32768 then
|
63 |
c62f5734
|
Christian Daniel
|
-- underflow
|
64 |
|
|
return to_signed(-32768,16);
|
65 |
|
|
else
|
66 |
|
|
-- in range
|
67 |
83340d0b
|
Christian Daniel
|
return xnorm(15 downto 0);
|
68 |
c62f5734
|
Christian Daniel
|
end if;
|
69 |
|
|
end doClipValue;
|
70 |
|
|
|
71 |
83340d0b
|
Christian Daniel
|
-- multiplier input
|
72 |
|
|
signal mula_i, mula_q : signed(17 downto 0) := (others=>'0');
|
73 |
|
|
signal mulb_i, mulb_q : signed(17 downto 0) := (others=>'0');
|
74 |
|
|
|
75 |
|
|
-- multiplier output
|
76 |
|
|
signal mout_i, mout_q : signed(18 downto 0) := (others=>'0');
|
77 |
|
|
|
78 |
c62f5734
|
Christian Daniel
|
begin
|
79 |
|
|
|
80 |
|
|
-- control logic
|
81 |
|
|
process(clk)
|
82 |
83340d0b
|
Christian Daniel
|
variable mtmp_i, mtmp_q : signed(35 downto 0);
|
83 |
|
|
variable atmp_i, atmp_q : signed(19 downto 0);
|
84 |
c62f5734
|
Christian Daniel
|
begin
|
85 |
|
|
if rising_edge(clk) then
|
86 |
|
|
-- passthough clock
|
87 |
|
|
out_clk <= in_clk;
|
88 |
|
|
|
89 |
|
|
-- handle data
|
90 |
|
|
if in_clk='1' then
|
91 |
83340d0b
|
Christian Daniel
|
-- apply swap-flag & convert input into 18bit signed
|
92 |
|
|
if config.swap='0' then
|
93 |
|
|
mula_i <= signed(in_i xor "10000000000000") & "0000";
|
94 |
|
|
mula_q <= signed(in_q xor "10000000000000") & "0000";
|
95 |
|
|
else
|
96 |
|
|
mula_i <= signed(in_q xor "10000000000000") & "0000";
|
97 |
|
|
mula_q <= signed(in_i xor "10000000000000") & "0000";
|
98 |
|
|
end if;
|
99 |
|
|
|
100 |
|
|
-- apply gain
|
101 |
|
|
mulb_i <= signed("00" & config.igain);
|
102 |
|
|
mulb_q <= signed("00" & config.qgain);
|
103 |
|
|
mtmp_i := mula_i * mulb_i;
|
104 |
|
|
mtmp_q := mula_q * mulb_q;
|
105 |
|
|
mout_i <= mtmp_i(34 downto 16);
|
106 |
|
|
mout_q <= mtmp_q(34 downto 16);
|
107 |
c62f5734
|
Christian Daniel
|
|
108 |
83340d0b
|
Christian Daniel
|
-- add offset (also adds 0.5 for rounding of multiplier-output)
|
109 |
|
|
atmp_i := resize(mout_i,20) + resize(config.ioff&"1",20);
|
110 |
|
|
atmp_q := resize(mout_q,20) + resize(config.qoff&"1",20);
|
111 |
c62f5734
|
Christian Daniel
|
|
112 |
|
|
-- clip output
|
113 |
83340d0b
|
Christian Daniel
|
out_i <= doClipValue(atmp_i(19 downto 1));
|
114 |
|
|
out_q <= doClipValue(atmp_q(19 downto 1));
|
115 |
c62f5734
|
Christian Daniel
|
end if;
|
116 |
|
|
|
117 |
|
|
-- handle reset
|
118 |
|
|
if reset='1' then
|
119 |
|
|
out_clk <= '0';
|
120 |
|
|
out_i <= (others=>'0');
|
121 |
|
|
out_q <= (others=>'0');
|
122 |
|
|
end if;
|
123 |
|
|
end if;
|
124 |
|
|
end process;
|
125 |
|
|
|
126 |
|
|
end rtl;
|