Index: trunk/MultiChannelUSB/Paella.v
===================================================================
--- trunk/MultiChannelUSB/Paella.v	(revision 54)
+++ trunk/MultiChannelUSB/Paella.v	(revision 59)
@@ -66,14 +66,9 @@
 	assign	USB_PA6		=	~usb_pktend;
 
-	reg				led_reg;	
-	assign	LED			=	led_reg;
-
 	wire			usb_wrreq, usb_rdreq, usb_rden, usb_pktend;
-	wire			usb_fifo_aclr;
-	reg				usb_fifo_tx_wrreq;
-	reg				usb_fifo_rx_rdreq;
-	wire			usb_fifo_tx_full, usb_fifo_rx_empty;
-	reg		[7:0]	usb_fifo_tx_data;
-	wire	[7:0]	usb_fifo_rx_data;
+	wire			usb_aclr;
+	wire			usb_tx_wrreq, usb_rx_rdreq;
+	wire			usb_tx_full, usb_rx_empty;
+	wire	[7:0]	usb_tx_data, usb_rx_data;
 	wire	[1:0]	usb_addr;
 
@@ -81,5 +76,5 @@
 	assign	USB_SLWR = ~usb_wrreq;
 
-	usb_fifo usb_fifo_unit
+	usb_fifo usb_unit
 	(
 		.usb_clk(USB_IFCLK),
@@ -94,18 +89,15 @@
 
 		.clk(CLK_50MHz),
-		.aclr(usb_fifo_aclr),
-
-		.tx_full(usb_fifo_tx_full),
-		.tx_wrreq((~usb_fifo_tx_full) & usb_fifo_tx_wrreq),
-		.tx_data(usb_fifo_tx_data),
-
-		.rx_empty(usb_fifo_rx_empty),
-		.rx_rdreq((~usb_fifo_rx_empty) & usb_fifo_rx_rdreq),
-		.rx_q(usb_fifo_rx_data)
+		.aclr(usb_aclr),
+
+		.tx_full(usb_tx_full),
+		.tx_wrreq(usb_tx_wrreq),
+		.tx_data(usb_tx_data),
+
+		.rx_empty(usb_rx_empty),
+		.rx_rdreq(usb_rx_rdreq),
+		.rx_q(usb_rx_data)
 	);
 	
-	reg		[23:0]	rx_counter;
-	reg		[10:0]	tst_counter;	
-
 	reg 			ana_reset [3:0];
 	wire			ana_peak_ready [3:0];
@@ -113,8 +105,7 @@
 
 	reg 			osc_reset [3:0];
+	reg 	[9:0]	osc_addr [3:0];
 	wire	[9:0]	osc_start_addr [3:0];
-	reg 	[9:0]	osc_addr [3:0];
 	wire	[15:0]	osc_q [3:0];
-	reg		[15:0]	osc_q_mux;
 
 	reg 			hst_reset [3:0];
@@ -122,15 +113,13 @@
 	wire	[23:0]	hst_q [3:0];
 
-	reg 			mux_reset, mux_type;
-	reg 	[1:0]	mux_chan, mux_byte, mux_max_byte;
-	reg 	[15:0]	mux_addr, mux_min_addr, mux_max_addr, mux_num_addr;
+	wire 			mux_reset, mux_type;
+	wire 	[1:0]	mux_chan, mux_byte;
+	wire 	[15:0]	mux_addr;
+	
 	reg		[7:0]	mux_q;
-
-	reg		[3:0]	state1, state2;
-	reg				adc_fifo_aclr;
+	reg 	[1:0]	mux_max_byte;
+	reg 	[15:0]	mux_min_addr, mux_max_addr;
 
 	wire			adc_clk [3:0];
-
-
 	wire			adc_data_ready [3:0];
 	wire 	[11:0]	adc_data [3:0];
@@ -146,23 +135,23 @@
     assign	adc_clk[3] = CON_B[0];
     assign	adc_data[3] = CON_B[12:1];
- 
+/* 
 	wire			adc_pll_clk;
 
-	wire			tst_adc_clk;
-	reg 	[11:0]	tst_adc_data;
-/*
-    assign	adc_clk[3] = tst_adc_clk;
-    assign	adc_data[3] = tst_adc_data;
-*/
-/*
 	adc_pll adc_pll_unit(
 		.inclk0(ADC_FCO),
 		.c0(adc_pll_clk));
 */
-
-	pll pll_unit(
+/*
+	wire			tst_adc_clk;
+	wire 	[11:0]	tst_adc_data;
+
+	test test_unit(
 		.inclk0(CLK_50MHz),
-		.c0(tst_adc_clk));
-
+		.tst_clk(tst_adc_clk),
+		.tst_data(tst_adc_data));
+
+    assign	adc_clk[3] = tst_adc_clk;
+    assign	adc_data[3] = tst_adc_data;
+*/
 /*
 	altserial_flash_loader #(
@@ -184,17 +173,16 @@
 		.lvds_fco(ADC_FCO),
 		.lvds_d(ADC_D),
-		.adc_db(adc_data[0]),
+		.adc_db(adc_data[2]),
 		.adc_dc(adc_data[1]),
-		.adc_dd(adc_data[2]));
+		.adc_dd(adc_data[0]));
  
 	genvar i;
 	generate
-		for (i = 0; i < 4; i = i + 1)
+		for (i = 2; i < 4; i = i + 1)
 		begin : MCA_CHAIN
 			adc_fifo adc_fifo_unit (
 				.adc_clk(adc_clk[i]),
 				.adc_data(adc_data[i]),
-				.aclr(adc_fifo_aclr),
-				.rdclk(CLK_50MHz),
+				.clk(CLK_50MHz),
 				.ready(adc_data_ready[i]),
 				.raw_data(raw_data[i]),
@@ -253,5 +241,5 @@
 		end
 
-		case({mux_type,mux_chan})
+		case({mux_type, mux_chan})
 			3'b000, 3'b001, 3'b010, 3'b011:
 			begin
@@ -260,5 +248,5 @@
 				mux_max_byte = 2'd1;	
 				mux_min_addr = {6'd0, osc_start_addr[mux_chan]};
-				mux_num_addr = 16'd1023;
+				mux_max_addr = 16'd1023;
 			end
 
@@ -269,5 +257,5 @@
 				mux_max_byte = 2'd2;	
 				mux_min_addr = 16'd0;
-				mux_num_addr = 16'd4095;
+				mux_max_addr = 16'd4095;
 			end
 		endcase
@@ -276,11 +264,11 @@
 	always @*
 	begin
-		case ({mux_type,mux_byte})
-			5'b000: mux_q = osc_q[mux_chan][7:0];
-			5'b001: mux_q = osc_q[mux_chan][15:8];
-
-			5'b100: mux_q = hst_q[mux_chan][7:0];
-			5'b101: mux_q = hst_q[mux_chan][15:8];
-			5'b110: mux_q = hst_q[mux_chan][23:16];
+		case ({mux_type, mux_byte})
+			3'b000: mux_q = osc_q[mux_chan][7:0];
+			3'b001: mux_q = osc_q[mux_chan][15:8];
+
+			3'b100: mux_q = hst_q[mux_chan][7:0];
+			3'b101: mux_q = hst_q[mux_chan][15:8];
+			3'b110: mux_q = hst_q[mux_chan][23:16];
 
 			default: mux_q = 8'd0;
@@ -288,192 +276,22 @@
 	end
 
-
-	always @(posedge CLK_50MHz)
-	begin
-		if (~usb_fifo_rx_empty)
-		begin
-			led_reg <= 1'b0;
-			rx_counter <= 24'd0;
-		end
-		else
-		begin
-			if (&rx_counter)
-			begin
-				led_reg <= 1'b1;
-			end
-			else
-			begin
-				rx_counter <= rx_counter + 24'd1;
-			end
-		end
-
-		case(state1)
-			1:
-			begin
-				usb_fifo_rx_rdreq <= 1'b1;
-				usb_fifo_tx_wrreq <= 1'b0;
-				mux_type <= 1'b0;
-				mux_chan <= 2'd0;
-				mux_byte <= 2'd0;	
-				mux_reset <= 1'b0;
-				state1 <= 4'd2;
-			end
-
-			2: 
-			begin
-				if (~usb_fifo_rx_empty)
-				begin
-					case (usb_fifo_rx_data)
-						8'h40, 8'h41, 8'h42, 8'h43, 8'h50, 8'h51, 8'h52, 8'h53:
-						begin
-							usb_fifo_rx_rdreq <= 1'b0;
-							mux_type <= usb_fifo_rx_data[4];
-							mux_chan <= usb_fifo_rx_data[1:0];
-							mux_reset <= 1'b1;
-							state1 <= 4'd1;
-						end
-
-						8'h60, 8'h61, 8'h62, 8'h63, 8'h70, 8'h71, 8'h72, 8'h73:
-						begin
-							usb_fifo_rx_rdreq <= 1'b0;
-							mux_type <= usb_fifo_rx_data[4];
-							mux_chan <= usb_fifo_rx_data[1:0];
-							state1 <= 4'd3;
-						end
-
-						8'h30:
-						begin
-							usb_fifo_rx_rdreq <= 1'b0;
-							state1 <= 4'd1;
-						end
-
-						8'h31:
-						begin
-							usb_fifo_rx_rdreq <= 1'b0;
-							tst_counter <= 11'd0;	
-							state1 <= 4'd6;
-						end
-					endcase
-				end
-			end
-			// mux transfer
-			3:
-			begin
-				mux_addr <= mux_min_addr;
-				mux_max_addr <= mux_min_addr + mux_num_addr;
-				mux_byte <= 2'd0;	
-				state1 <= 4'd4;
-			end
-	
-			4:
-			begin
-				usb_fifo_tx_wrreq <= 1'b0;
-				state1 <= 4'd5;
-			end
-
-			5:
-			begin
-				if (~usb_fifo_tx_full)
-				begin
-					usb_fifo_tx_data <= mux_q;
-					usb_fifo_tx_wrreq <= 1'b1;
-					if ((mux_byte == mux_max_byte) && (mux_addr == mux_max_addr))
-					begin
-						state1 <= 4'd1;
-					end
-					else
-					begin
-						state1 <= 4'd4;
-						if (mux_byte == mux_max_byte)
-						begin
-							mux_addr <= mux_addr + 16'd1;
-							mux_byte <= 2'd0;
-						end
-						else
-						begin
-							mux_byte <= mux_byte + 2'd1;
-						end
-					end
-				end
-			end
-
-			// tst transfer
-			6:
-			begin
-				usb_fifo_tx_data <= tst_counter;
-				usb_fifo_tx_wrreq <= 1'b1;
-				tst_counter <= tst_counter + 11'd1;
-				state1 <= 4'd8;
-			end
-			7:
-			begin
-				if (~usb_fifo_tx_full)
-				begin
-					usb_fifo_tx_data <= tst_counter;
-					if (tst_counter == 11'd0)
-					begin
-						state1 <= 4'd9;
-					end
-					else
-					begin
-						tst_counter <= tst_counter + 11'd1;
-					end
-				end
-			end
-			8:
-			begin
-				if (~usb_fifo_tx_full)
-				begin
-					usb_fifo_tx_wrreq <= 1'b0;
-					state1 <= 4'd1;
-				end
-			end
-						
-			default:
-			begin
-				state1 <= 4'd1;
-			end
-		endcase
-	end
-
-	always @ (posedge tst_adc_clk)
-	begin
-		case (state2)
-			1: 
-			begin
-				tst_adc_data <= 12'd0;
-				state2 <= 4'd2;
-			end
-			
-			2:
-			begin
-				tst_adc_data <= 12'd1024;
-				state2 <= 4'd3;
-			end
-
-			3:
-			begin
-				tst_adc_data <= 12'd2048;
-				state2 <= 4'd4;
-			end
-
-			4:
-			begin
-				tst_adc_data <= 12'd3072;
-				state2 <= 4'd5;
-			end
-
-			5:
-			begin
-				tst_adc_data <= 12'd4095;
-				state2 <= 4'd1;
-			end
-
-			default:
-			begin
-				state2 <= 4'd1;
-			end
-		endcase
-	end
+	control control_unit (
+		.clk(CLK_50MHz),
+		.rx_empty(usb_rx_empty),
+		.tx_full(usb_tx_full),
+		.rx_data(usb_rx_data),
+		.mux_max_byte(mux_max_byte),
+		.mux_min_addr(mux_min_addr),
+		.mux_max_addr(mux_max_addr),
+		.mux_q(mux_q),
+		.mux_reset(mux_reset),
+		.mux_type(mux_type),
+		.mux_chan(mux_chan),
+		.mux_byte(mux_byte),
+		.mux_addr(mux_addr),
+		.rx_rdreq(usb_rx_rdreq),
+		.tx_wrreq(usb_tx_wrreq),
+		.tx_data(usb_tx_data),
+		.led(LED));
 
 endmodule
