Statistics
| Branch: | Tag: | Revision:

root / usrp2 / vrt / vita_tx_control.v @ a9d30712

History | View | Annotate | Download (6.21 KB)

1
//
2
// Copyright 2011 Ettus Research LLC
3
//
4
// This program is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// This program is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
//
17

    
18

    
19
module vita_tx_control
20
  #(parameter BASE=0,
21
    parameter WIDTH=32)
22
   (input clk, input reset, input clear,
23
    input set_stb, input [7:0] set_addr, input [31:0] set_data,
24
    
25
    input [63:0] vita_time,
26
    output error, output ack,
27
    output reg [31:0] error_code,
28
    output reg packet_consumed,
29
    
30
    // From vita_tx_deframer
31
    input [5+64+16+WIDTH-1:0] sample_fifo_i,
32
    input sample_fifo_src_rdy_i,
33
    output sample_fifo_dst_rdy_o,
34
    
35
    // To DSP Core
36
    output [WIDTH-1:0] sample,
37
    output reg run,
38
    input strobe,
39

    
40
    output [31:0] debug
41
    );
42

    
43
   wire [63:0] send_time = sample_fifo_i[63:0];
44
   wire [15:0] seqnum = sample_fifo_i[79:64];
45
   wire        eop = sample_fifo_i[80];
46
   wire        eob = sample_fifo_i[81];
47
   wire        sob = sample_fifo_i[82];
48
   wire        send_at = sample_fifo_i[83];
49
   wire        seqnum_err = sample_fifo_i[84];
50
   
51
   wire        now, early, late, too_early;
52

    
53
   // FIXME ignore too_early for now for timing reasons
54
   assign too_early = 0;
55
   time_compare 
56
     time_compare (.time_now(vita_time), .trigger_time(send_time), 
57
		   .now(now), .early(early), .late(late), .too_early());
58

    
59
   reg 	       late_qual, late_del;
60

    
61
   always @(posedge clk)
62
     if(reset | clear)
63
       late_del <= 0;
64
     else
65
       late_del <= late;
66
   
67
   always @(posedge clk)
68
     if(reset | clear)
69
       late_qual <= 0;
70
     else
71
       late_qual <= (sample_fifo_src_rdy_i & ~sample_fifo_dst_rdy_o);
72
      
73
   localparam IBS_IDLE = 0;
74
   localparam IBS_RUN = 1;  // FIXME do we need this?
75
   localparam IBS_CONT_BURST = 2;
76
   localparam IBS_ERROR = 3;
77
   localparam IBS_ERROR_DONE = 4;
78
   localparam IBS_ERROR_WAIT = 5;
79

    
80
   wire [31:0] CODE_EOB_ACK = {seqnum,16'd1};
81
   wire [31:0] CODE_UNDERRUN = {seqnum,16'd2};
82
   wire [31:0] CODE_SEQ_ERROR = {seqnum,16'd4};
83
   wire [31:0] CODE_TIME_ERROR = {seqnum,16'd8};
84
   wire [31:0] CODE_UNDERRUN_MIDPKT = {seqnum,16'd16};
85
   wire [31:0] CODE_SEQ_ERROR_MIDBURST = {seqnum,16'd32};
86
   
87
   reg [2:0] ibs_state;
88

    
89
   wire [31:0] error_policy;
90
   setting_reg #(.my_addr(BASE+3)) sr_error_policy
91
     (.clk(clk),.rst(reset),.strobe(set_stb),.addr(set_addr),
92
      .in(set_data),.out(error_policy),.changed());
93

    
94
   wire        policy_wait = error_policy[0];
95
   wire        policy_next_packet = error_policy[1];
96
   wire        policy_next_burst = error_policy[2];
97
   reg 	       send_error, send_ack;
98
   
99
   always @(posedge clk)
100
     if(reset | clear)
101
       begin
102
	  ibs_state <= IBS_IDLE;
103
	  send_error <= 0;
104
	  send_ack <= 0;
105
	  error_code <= 0;
106
       end
107
     else
108
       case(ibs_state)
109
	 IBS_IDLE :
110
	   if(sample_fifo_src_rdy_i)
111
	     if(seqnum_err)
112
	       begin
113
		  ibs_state <= IBS_ERROR;
114
		  error_code <= CODE_SEQ_ERROR;
115
		  send_error <= 1;
116
	       end
117
	     else if(~send_at | now)
118
	       ibs_state <= IBS_RUN;
119
	     else if((late_qual & late_del) | too_early)
120
	       begin
121
		  ibs_state <= IBS_ERROR;
122
		  error_code <= CODE_TIME_ERROR;
123
		  send_error <= 1;
124
	       end
125
	 
126
	 IBS_RUN :
127
	   if(strobe)
128
	     if(~sample_fifo_src_rdy_i)
129
	       begin
130
		  ibs_state <= IBS_ERROR;
131
		  error_code <= CODE_UNDERRUN_MIDPKT;
132
		  send_error <= 1;
133
	       end
134
	     else if(eop)
135
	       if(eob)
136
		 begin
137
		    ibs_state <= IBS_ERROR_DONE;  // Not really an error
138
		    error_code <= CODE_EOB_ACK;
139
		    send_ack <= 1;
140
		 end
141
	       else
142
		 ibs_state <= IBS_CONT_BURST;
143

    
144
	 IBS_CONT_BURST :
145
	   if(strobe)
146
	     begin
147
		if(policy_next_packet)
148
		  ibs_state <= IBS_ERROR_DONE;
149
		else if(policy_wait)
150
		  ibs_state <= IBS_ERROR_WAIT;
151
		else
152
		  ibs_state <= IBS_ERROR;
153
		error_code <= CODE_UNDERRUN;
154
		send_error <= 1;
155
	     end
156
	   else if(sample_fifo_src_rdy_i)
157
	     if(seqnum_err)
158
	       begin
159
		  ibs_state <= IBS_ERROR;
160
		  error_code <= CODE_SEQ_ERROR_MIDBURST;
161
		  send_error <= 1;
162
	       end
163
	     else
164
	       ibs_state <= IBS_RUN;
165
	 
166
	 IBS_ERROR :
167
	   begin
168
	      send_error <= 0;
169
	      if(sample_fifo_src_rdy_i & eop)
170
		if(policy_next_packet | (policy_next_burst & eob))
171
		  ibs_state <= IBS_IDLE;
172
		else if(policy_wait)
173
		  ibs_state <= IBS_ERROR_WAIT;
174
	   end
175

    
176
	 IBS_ERROR_DONE :
177
	   begin
178
	      send_error <= 0;
179
	      send_ack <= 0;
180
	      ibs_state <= IBS_IDLE;
181
	   end
182
	 
183
	 IBS_ERROR_WAIT :
184
	   send_error <= 0;
185
       endcase // case (ibs_state)
186

    
187
   
188
   assign sample_fifo_dst_rdy_o = (ibs_state == IBS_ERROR) | (strobe & (ibs_state == IBS_RUN));  // FIXME also cleanout
189

    
190
   //register the output sample
191
   reg [31:0] sample_held;
192
   assign sample = sample_held;
193
   always @(posedge clk)
194
     if(reset | clear)
195
        sample_held <= 0;
196
     else if (~run)
197
       sample_held <= 0;
198
     else if (strobe)
199
       sample_held <= sample_fifo_i[5+64+16+WIDTH-1:5+64+16];
200

    
201
   assign error = send_error;
202
   assign ack = send_ack;
203

    
204
   localparam MAX_IDLE = 1000000; 
205
   // approx 10 ms timeout with a 100 MHz clock, but burning samples will slow that down
206
   reg [19:0] countdown;
207
   
208
   always @(posedge clk)
209
     if(reset | clear)
210
       begin
211
	  run <= 0;
212
	  countdown <= 0;
213
       end
214
     else 
215
       if (ibs_state == IBS_RUN)
216
	 if(eob & eop & strobe & sample_fifo_src_rdy_i)
217
	   run <= 0;
218
	 else 
219
	   begin
220
	      run <= 1;
221
	      countdown <= MAX_IDLE;
222
	   end
223
       else
224
	 if (countdown == 0)
225
	   run <= 0;
226
	 else
227
	   countdown <= countdown - 1;
228
   	   
229
   always @(posedge clk)
230
     if(reset | clear)
231
       packet_consumed <= 0;
232
     else
233
       packet_consumed <= eop & sample_fifo_src_rdy_i & sample_fifo_dst_rdy_o;
234
   
235
   assign debug = { { now,late_qual,late_del,ack,eop,eob,sob,send_at },
236
		    { sample_fifo_src_rdy_i, sample_fifo_dst_rdy_o, strobe, run, error, ibs_state[2:0] },
237
		    { 8'b0 },
238
		    { 8'b0 } };
239
   
240
endmodule // vita_tx_control