summaryrefslogtreecommitdiffstats
path: root/sdr_lib/bus_interface.v
blob: b326889c035d376dba361161ac8e052e3e1647ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// -*- verilog -*-
//
//  USRP - Universal Software Radio Peripheral
//
//  Copyright (C) 2003 Matt Ettus
//
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation; either version 2 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 51 Franklin Street, Boston, MA  02110-1301  USA
//

// Interface to Cypress FX2 bus
// A packet is 512 Bytes.  Each fifo line is 4 bytes
// Fifo has 1024 or 2048 lines

module bus_interface
  (     input usbclk,
	input reset,
	inout [15:0] usbdata,   // TRISTATE
	input wire [5:0] usbctl,
	output wire [5:0] usbrdy,
	output [31:0] txdata,
	input [31:0] rxdata,
	input txclk,
	input txstrobe,
	input rxclk,
	input rxstrobe,
	output [11:0] debugbus,
	input clear_status
	);

   parameter   IN_CHANNELS = 1;
   parameter   OUT_CHANNELS = 1;
   parameter   bitmask = (IN_CHANNELS*2)-1;

   wire have_space, have_pkt_rdy;
   wire WR, RD, OE;
   reg tx_underrun, rx_overrun;
   
   assign WR = usbctl[0];
   assign RD = usbctl[1];
   assign OE = usbctl[2];
   
   assign usbrdy[0] = have_space;
   assign usbrdy[1] = have_pkt_rdy;
   assign usbrdy[2] = tx_underrun;
   assign usbrdy[3] = rx_overrun;
   
   reg [IN_CHANNELS*2*16-1:0] fifo_in;
   wire [OUT_CHANNELS*2*16-1:0] fifo_out;
   
   wire [15:0] usbdata_in = usbdata;
   
   reg select_out;
   reg select_in;
   
   reg commit;
   reg rd_next;
   reg [15:0] usbdata_out;
   wire [10:0] txfifolevel,rxfifolevel;
   reg [8:0] write_count;
   wire tx_empty;
   wire tx_full;
   wire rx_empty;
   wire rx_full;
   wire [31:0] txd;
   wire rdreq;
	
   // Tri-state bus macro
   bustri bustri(.data(usbdata_out),
		 .enabledt(OE),
		 .tridata(usbdata)  );

   //////////////////////////////////////////////
   // TX Side (USB --> DAC)
   always @(posedge usbclk, posedge reset)
     begin
	if(reset)
	  begin
	     fifo_in <= #1 0;
	     write_count <= #1 0;
	  end
	else 
	  if(WR & ~write_count[8])
	    begin
	       case(write_count[0])
		 1'b0 : fifo_in[31:16] <= #1 usbdata_in;  // I
		 1'b1 : fifo_in[15:0] <= #1 usbdata_in;   // Q
	       endcase
	       write_count <= #1 write_count + 9'd1;
	    end
	  else
	    write_count <= #1 WR ? write_count : 9'b0;
     end
   
   always @(posedge usbclk)
     if(reset)
       commit <= #1 1'b0;
     else
       if(write_count[0] && ~write_count[8] && WR)
	 commit <= #1 1'b1;
       else 
	 commit <= #1 1'b0;
   
   assign rdreq = txstrobe & !tx_empty;
   assign txdata = tx_empty ? 32'b0 : txd;
   
   always @(posedge txclk)
     if(reset)
       tx_underrun <= 1'b0;
     else if(txstrobe & tx_empty)
       tx_underrun <= 1'b1;
     else if(clear_status)
       tx_underrun <= 1'b0;

   fifo_1c_2k	txfifo (.data ( fifo_in ),
			.wrreq ( commit ),
			.wrclk ( usbclk ),

			.q ( txd ),			
			.rdreq ( rdreq),
			.rdclk ( txclk ),
			
			.aclr ( reset ),

			.rdempty ( tx_empty ),
			.rdusedw (  ),
			.wrfull ( tx_full ),
			.wrusedw ( txfifolevel )
			);
   
   assign have_space = (txfifolevel <= (2048-128));
   
   //////////////////////////////
   // Receive FIFO (ADC --> USB)

   always @(posedge rxclk)
     if(reset)
       rx_overrun <= 1'b0;
     else if(rxstrobe & rx_full)
       rx_overrun <= 1'b1;
     else if(clear_status)
       rx_overrun <= 1'b0;

   always @(select_out, fifo_out)
     case(select_out)
       0 : usbdata_out = fifo_out[31:16];  // I
       1 : usbdata_out = fifo_out[15:0];   // Q
     endcase
  
/*
	always @(posedge usbclk, posedge reset)
	if(reset)
		usbdata_out <= #1 16'b0;
	else
		if(select_out)
			usbdata_out = fifo_out[31:16];
		else
			usbdata_out = fifo_out[15:0];
	*/
	 
   always @(negedge usbclk, posedge reset)
     if(reset)
       select_out <= #1 1'b0;
     else if(~RD)
       select_out <= #1 1'b0;
     else 
       select_out <= #1 ~select_out;
 
   fifo_1c_2k	rxfifo (.data ( rxdata ), // counter ),
			.wrreq (rxstrobe & ~rx_full ),
			.wrclk ( rxclk ),
			
			.q ( fifo_out ),
			.rdreq ( select_out ),// & RD ), // FIXME
			.rdclk ( usbclk ),
			
			.aclr ( reset ),
			
			.rdempty ( rx_empty ),
			.rdusedw ( rxfifolevel ),
			.wrfull ( rx_full ),
			.wrusedw (  )
			);
   
   assign have_pkt_rdy = (rxfifolevel >= 128);

   // Debugging Aids
   assign debugbus[0] = tx_underrun;
   assign debugbus[1] = rx_overrun;
   assign debugbus[2] = tx_empty;
   assign debugbus[3] = tx_full;
   assign debugbus[4] = rx_empty;
   assign debugbus[5] = rx_full;
   assign debugbus[6] = txstrobe;
   assign debugbus[7] = rxstrobe;
   assign debugbus[8] = select_out;
   assign debugbus[9] = rxstrobe & ~rx_full;
   assign debugbus[10] = have_space;
   assign debugbus[11] = have_pkt_rdy;
   
endmodule // bus_interface