Debian Codegraph @

hdl/fpga/ip/nuand/rfic_spi_controller/vhdl/tb/rfic_model.vhd

raw
1 --
2 --
3 -- !!! ======================== [ WARNING ] ======================== !!!
4 --
5 -- QUICK-AND-DIRTY TESTBENCH ... SCHEDULED FOR CLEAN UP
6 --
7 -- !!! ======================== [ WARNING ] ======================== !!!
8 --
9 --
10
11
12 -- Copyright (c) 2013 Nuand LLC
13 --
14 -- Permission is hereby granted, free of charge, to any person obtaining a copy
15 -- of this software and associated documentation files (the "Software"), to deal
16 -- in the Software without restriction, including without limitation the rights
17 -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18 -- copies of the Software, and to permit persons to whom the Software is
19 -- furnished to do so, subject to the following conditions:
20 --
21 -- The above copyright notice and this permission notice shall be included in
22 -- all copies or substantial portions of the Software.
23 --
24 -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27 -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29 -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
30 -- THE SOFTWARE.
31
32 library ieee ;
33 use ieee.std_logic_1164.all ;
34 use ieee.numeric_std.all ;
35 use ieee.math_real.all ;
36 use ieee.math_complex.all ;
37
38 entity rfic_model is
39 port (
40 -- LMS RX Interface
41 rx_clock : in std_logic ;
42 rx_clock_out : buffer std_logic ;
43 rx_data : buffer signed(11 downto 0) ;
44 rx_enable : in std_logic ;
45 rx_iq_select : buffer std_logic ;
46
47 -- LMS TX Interface
48 tx_clock : in std_logic ;
49 tx_data : in signed(11 downto 0) ;
50 tx_enable : in std_logic ;
51 tx_iq_select : in std_logic ;
52
53 -- LMS SPI Interface
54 sclk : in std_logic ;
55 sen : in std_logic ;
56 sdio : in std_logic ;
57 sdo : buffer std_logic ;
58
59 -- LMS Control Interface
60 pll_out : buffer std_logic ;
61 resetx : in std_logic
62 ) ;
63 end entity ; -- rfic_model
64
65 architecture arch of rfic_model is
66
67 constant SPI_CMD_WIDTH : positive := 1;
68 constant SPI_ADDR_WIDTH : positive := 15;
69 constant SPI_DATA_WIDTH : positive := 8;
70 constant SPI_MSG_WIDTH : positive := 16; -- total number of bits in a SPI message
71 constant SPI_NUM_REGS : positive := 128; -- number of SPI registers
72
73 type spi_registers_t is array( 0 to SPI_NUM_REGS-1 ) of std_logic_vector( SPI_DATA_WIDTH-1 downto 0 );
74
75 signal spi_registers : spi_registers_t := (others => (others => '1') );
76
77 begin
78
79 pll_out <= '0' ;
80
81 -- Write transmit samples to a file or drop them on the floor
82
83 -- Read receive samples from a file or predetermined tone
84 rx_clock_out <= rx_clock ;
85
86 drive_rx_data : process( resetx, rx_clock )
87 begin
88 if( resetx = '0' ) then
89 rx_data <= (others =>'0') ;
90 rx_iq_select <= '0' ;
91 elsif( rising_edge(rx_clock) ) then
92 if( rx_enable = '1' ) then
93 rx_data <= rx_data + 1 ;
94 rx_iq_select <= not rx_iq_select ;
95 else
96 rx_data <= (others =>'0') ;
97 rx_iq_select <= '0' ;
98 end if ;
99 end if ;
100 end process ;
101
102 -- Keep track of the SPI register set
103 spi_reg_proc : process( sclk )
104 variable ptr : natural range 0 to SPI_MSG_WIDTH-1 := 0;
105 variable addr : std_logic_vector( SPI_ADDR_WIDTH-1 downto 0 ) := (others => '0');
106 variable data : std_logic_vector( SPI_DATA_WIDTH-1 downto 0 ) := (others => '0');
107
108 -- Not going to bother making this generic
109 variable cmd : std_logic := '0';
110 constant RD_CMD : std_logic := '0';
111 constant WR_CMD : std_logic := '1';
112
113 -- Trying this method out as a thought experiment since SPI is isolated.
114 -- The main register array is a signal because we may want to use it elsewhere.
115 type spi_state_t is ( RX_CMD, RX_ADDR, RX_DATA, TX_DATA );
116 variable spi_state : spi_state_t := RX_CMD;
117 begin
118 sdo <= '0'; -- default
119 if( falling_edge(sclk) ) then
120 if( sen = '0' ) then
121 case( spi_state ) is
122 when RX_CMD =>
123
124 cmd := sdio;
125 ptr := ptr + 1;
126
127 if( ptr = SPI_CMD_WIDTH ) then
128 ptr := 0;
129 spi_state := RX_ADDR;
130 end if;
131
132 when RX_ADDR =>
133
134 addr := addr(addr'left-1 downto 0) & sdio;
135 ptr := ptr + 1;
136
137 if( ptr = SPI_ADDR_WIDTH ) then
138 if( cmd = RD_CMD ) then
139 --data := spi_registers(to_integer(unsigned(addr)));
140 ptr := 0;
141 spi_state := TX_DATA;
142 elsif( cmd = WR_CMD ) then
143 ptr := 0;
144 spi_state := RX_DATA;
145 else
146 report "Unknown command value" severity failure;
147 end if;
148 end if;
149
150 when RX_DATA =>
151
152 data := data(data'left-1 downto 0) & sdio;
153 ptr := ptr + 1;
154
155 if( ptr = SPI_DATA_WIDTH ) then
156 --spi_registers(to_integer(unsigned(addr))) <= data;
157 ptr := 0;
158 spi_state := RX_CMD;
159 end if;
160
161 when TX_DATA =>
162
163 sdo <= data(data'left);
164 data := data(data'left-1 downto 0) & "0";
165 ptr := ptr + 1;
166
167 if( ptr = SPI_DATA_WIDTH ) then
168 ptr := 0;
169 spi_state := RX_CMD;
170 end if;
171
172 end case;
173
174 else
175 ptr := 0;
176 data := (others => '0');
177 addr := (others => '0');
178 cmd := '0';
179 spi_state := RX_CMD;
180 end if;
181
182 end if;
183
184 end process;
185
186 end architecture ; -- arch