uart_ctrl.v 3.1 KB
Newer Older
饶先宏's avatar
饶先宏 已提交
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

module uart_ctrl(
	input wClk,
	input nwReset,
	input wRead,
	input [31:0] bReadAddr,
	input wWrite, 
	input [31:0] bWriteAddr,
	input [31:0] bWriteData,
	output [31:0] bReadData,
	output uart_tx,
	input  uart_rx
	);

	wire [31:0] ctl_state;

	wire [7:0]  send_buf_data, send_buf_q;
	wire send_buf_full;
	wire send_buf_empty;
	wire [9:0] send_buf_used;
	reg send_buf_read;
	reg send_buf_write;
	assign send_buf_data = bWriteData[7:0];
	assign ctl_state[15:0] = {5'b0, send_buf_used, send_buf_full};

    uart_fifo uart_send_buf(
		.clock(wClk),
		.data(send_buf_data),
		.rdreq(send_buf_read),
		.wrreq(send_buf_write),
		.almost_full(send_buf_full),
		.empty(send_buf_empty),
		.full(),
		.q(send_buf_q),
		.usedw(send_buf_used));


	wire [7:0]  recv_buf_data, recv_buf_q;
	wire recv_buf_empty;
	wire recv_buf_full;
	wire [9:0] recv_buf_used;
	reg recv_buf_read;
	reg recv_buf_write;
	assign ctl_state[31:16] = {5'b0, recv_buf_used, recv_buf_empty};

    uart_fifo uart_recv_buf(
		.clock(wClk),
		.data(recv_buf_data),
		.rdreq(recv_buf_read),
		.wrreq(recv_buf_write),
		.almost_full(recv_buf_full),
		.empty(recv_buf_empty),
		.full(),
		.q(recv_buf_q),
		.usedw(recv_buf_used));

	reg [2:0] uartaddr;
	reg  uart_read, uart_write;
	reg  [15:0] uart_write_data;
	wire [15:0] uart_read_data;
	wire uart_has_data;
	wire uart_can_send;
	altera_uart uart(
                       // inputs:
                        .address(uartaddr),
                        .begintransfer(1'b1),
                        .chipselect(1'b1),
                        .clk(wClk),
                        .read_n(~uart_read),
                        .reset_n(nwReset),
                        .rxd(uart_rx),
                        .write_n(uart_write),
                        .writedata(uart_write_data),

                       // outputs:
                        .dataavailable(uart_has_data),
                        .irq(),
                        .readdata(uart_read_data),
                        .readyfordata(uart_can_send),
                        .txd(uart_tx)
                     );

	always @(posedge wClk)	
	if (~nwReset) begin
		uart_read <= 1'b0;
		uart_write < = 1'b0;
		uart_addr <= 3'b0;
		recv_buf_write <= 1'b0;
	end else begin
		uart_read <= 1'b0;
		uart_write < = 1'b0;
		uart_addr <= 3'b0;
		recv_buf_write <= 1'b0;
		if (uart_has_data && ~recv_buf_full) begin
			recv_buf_write <= 1'b1;
			recv_buf_data <= 
		end
	end

	/*  */
	reg [31:0] bReadData;
	wire [1:0] readaddr = bReadAddr[3:2];
	always @(posedge wClk) 
	if (~nwReset) begin
		bReadData <= 32'h0;
		recv_buf_read <= 1'b0;
	end else begin
		recv_buf_read <= 1'b0;
		if (wRead) begin
			if (readaddr == 0) begin /* state */
				bReadData <= ctl_state;
			end else if (readaddr == 1) begin
				bReadData <= {24'b0, uartrecvdata};
				recv_buf_read <= ~ctl_state[16]; /* empty */
			end
		end
	end

	/* д */
	wire [1:0] writeaddr = bWriteAddr[3:2];
	always @(posedge wClk)
	if (~nwReset) begin
		send_buf_write <= 1'b0;
	end else begin
		send_buf_write <= 1'b0;
		if (wWrite && (writeaddr == 2)) begin
			send_buf_write <= 1'b1;
		end
	end

endmodule