//---------------------------------------------------------------------------- // user_logic.v - module //---------------------------------------------------------------------------- // // *************************************************************************** // ** Copyright (c) 1995-2012 Xilinx, Inc. All rights reserved. ** // ** ** // ** Xilinx, Inc. ** // ** XILINX IS PROVIDING THIS DESIGN, CODE, OR INFORMATION "AS IS" ** // ** AS A COURTESY TO YOU, SOLELY FOR USE IN DEVELOPING PROGRAMS AND ** // ** SOLUTIONS FOR XILINX DEVICES. BY PROVIDING THIS DESIGN, CODE, ** // ** OR INFORMATION AS ONE POSSIBLE IMPLEMENTATION OF THIS FEATURE, ** // ** APPLICATION OR STANDARD, XILINX IS MAKING NO REPRESENTATION ** // ** THAT THIS IMPLEMENTATION IS FREE FROM ANY CLAIMS OF INFRINGEMENT, ** // ** AND YOU ARE RESPONSIBLE FOR OBTAINING ANY RIGHTS YOU MAY REQUIRE ** // ** FOR YOUR IMPLEMENTATION. XILINX EXPRESSLY DISCLAIMS ANY ** // ** WARRANTY WHATSOEVER WITH RESPECT TO THE ADEQUACY OF THE ** // ** IMPLEMENTATION, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OR ** // ** REPRESENTATIONS THAT THIS IMPLEMENTATION IS FREE FROM CLAIMS OF ** // ** INFRINGEMENT, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS ** // ** FOR A PARTICULAR PURPOSE. ** // ** ** // *************************************************************************** // //---------------------------------------------------------------------------- // Filename: user_logic.v // Version: 1.00.a // Description: User logic module. // Date: Wed Jan 20 16:06:06 2016 (by Create and Import Peripheral Wizard) // Verilog Standard: Verilog-2001 //---------------------------------------------------------------------------- // Naming Conventions: // active low signals: "*_n" // clock signals: "clk", "clk_div#", "clk_#x" // reset signals: "rst", "rst_n" // generics: "C_*" // user defined types: "*_TYPE" // state machine next state: "*_ns" // state machine current state: "*_cs" // combinatorial signals: "*_com" // pipelined or register delay signals: "*_d#" // counter signals: "*cnt*" // clock enable signals: "*_ce" // internal version of output port: "*_i" // device pins: "*_pin" // ports: "- Names begin with Uppercase" // processes: "*_PROCESS" // component instantiations: "<ENTITY_>I_<#|FUNC>" //---------------------------------------------------------------------------- `uselib lib=unisims_ver `uselib lib=proc_common_v3_00_a module user_logic ( // -- ADD USER PORTS BELOW THIS LINE --------------- // --USER ports added here // -- ADD USER PORTS ABOVE THIS LINE --------------- uart_rx_i, uart_tx_o, // -- DO NOT EDIT BELOW THIS LINE ------------------ // -- Bus protocol ports, do not add to or delete Bus2IP_Clk, // Bus to IP clock Bus2IP_Resetn, // Bus to IP reset Bus2IP_Data, // Bus to IP data bus Bus2IP_BE, // Bus to IP byte enables Bus2IP_RdCE, // Bus to IP read chip enable Bus2IP_WrCE, // Bus to IP write chip enable IP2Bus_Data, // IP to Bus data bus IP2Bus_RdAck, // IP to Bus read transfer acknowledgement IP2Bus_WrAck, // IP to Bus write transfer acknowledgement IP2Bus_Error // IP to Bus error response // -- DO NOT EDIT ABOVE THIS LINE ------------------ ); // user_logic // -- ADD USER PARAMETERS BELOW THIS LINE ------------ // --USER parameters added here // -- ADD USER PARAMETERS ABOVE THIS LINE ------------ // -- DO NOT EDIT BELOW THIS LINE -------------------- // -- Bus protocol parameters, do not add to or delete parameter C_NUM_REG = 1; parameter C_SLV_DWIDTH = 32; // -- DO NOT EDIT ABOVE THIS LINE -------------------- // -- ADD USER PORTS BELOW THIS LINE ----------------- // --USER ports added here // -- ADD USER PORTS ABOVE THIS LINE ----------------- input uart_rx_i; output uart_tx_o; // -- DO NOT EDIT BELOW THIS LINE -------------------- // -- Bus protocol ports, do not add to or delete input Bus2IP_Clk; input Bus2IP_Resetn; input [C_SLV_DWIDTH-1 : 0] Bus2IP_Data; input [C_SLV_DWIDTH/8-1 : 0] Bus2IP_BE; input [C_NUM_REG-1 : 0] Bus2IP_RdCE; input [C_NUM_REG-1 : 0] Bus2IP_WrCE; output [C_SLV_DWIDTH-1 : 0] IP2Bus_Data; output IP2Bus_RdAck; output IP2Bus_WrAck; output IP2Bus_Error; // -- DO NOT EDIT ABOVE THIS LINE -------------------- //---------------------------------------------------------------------------- // Implementation //---------------------------------------------------------------------------- // --USER nets declarations added here, as needed for user logic // Nets for user logic slave model s/w accessible register example reg [C_SLV_DWIDTH-1 : 0] slv_reg0; wire [0 : 0] slv_reg_write_sel; wire [0 : 0] slv_reg_read_sel; reg [C_SLV_DWIDTH-1 : 0] slv_ip2bus_data; wire slv_read_ack; wire slv_write_ack; integer byte_index, bit_index; // USER logic implementation added here reg [13:0] BAUD_DIV; reg [13:0] baud_div1=0; //波特率设置计数器 reg baud_bps1=0; //数据采样点信号 reg bps_start=0; //波特率启动标志 always@(posedge Bus2IP_Clk) begin if(baud_div1==(BAUD_DIV>>1)) //当波特率计数器计数到采样点时,产生采样信号baud_bps begin baud_bps1<=1'b1; baud_div1<=baud_div1+1'b1; end else if(baud_div1<BAUD_DIV && bps_start)//当波特率计数器启动时,计数器累加 begin baud_div1<=baud_div1+1'b1; baud_bps1<=0; end else begin baud_bps1<=0; baud_div1<=0; end end reg [4:0] uart_rx_i_r=5'b11111; //数据接收缓存器 always@(posedge Bus2IP_Clk) begin uart_rx_i_r<={uart_rx_i_r[3:0],uart_rx_i}; end //数据接收缓存器,当连续接收到五个低电平时,即uart_rx_int=0时,作为接收到起始信号 wire uart_rx_int=uart_rx_i_r[4] | uart_rx_i_r[3] | uart_rx_i_r[2] | uart_rx_i_r[1] | uart_rx_i_r[0]; reg [3:0] bit_num1=0; //接收数据个数计数器 reg uart_rx_done_r=0; //数据接收完成寄存器 reg state=1'b0; reg [7:0] uart_rx_data_o_r0=0;//数据接收过程中,数据缓存器 reg [7:0] uart_rx_data_o_r1=0;//数据接收完成,数据寄存器 wire [7:0] uart_rx_data_o; wire uart_rx_done; always@(posedge Bus2IP_Clk) begin uart_rx_done_r<=1'b0; case(state) 1'b0 : if(!uart_rx_int)//当连续接收到五个低电平时,即uart_rx_int=0时,作为接收到起始信号,启动波特率时钟 begin bps_start<=1'b1; state<=1'b1; end 1'b1 : if(baud_bps1) //每次等待波特率采样中心时,接收数据,放入数据缓存器中 begin bit_num1<=bit_num1+1'b1; if(bit_num1<4'd9) //接收1bit起始信号,8bit有效信号,1bit结束信号 uart_rx_data_o_r0[bit_num1-1]<=uart_rx_i; end else if(bit_num1==4'd10) //接收完成时候,接收数据个数计数器清零,产生接收完成标志位,并将数据写入数据寄存器,关闭波特率时候 begin bit_num1<=0; uart_rx_done_r<=1'b1; uart_rx_data_o_r1<=uart_rx_data_o_r0; state<=1'b0;//进入状态0,再次循环检测 bps_start<=0; end default:; endcase end assign uart_rx_data_o=uart_rx_data_o_r1; assign uart_rx_done=uart_rx_done_r; reg uart_tx_en_i; reg [7:0] uart_tx_data_i; reg [13:0] baud_div2=0; //波特率设置计数器 reg baud_bps2=0; //数据发送点信号,高有效 reg [9:0] send_data=10'b1111111111;//待发送数据寄存器,1bit起始信号+8bit有效信号+1bit结束信号 reg [3:0] bit_num2=0; //发送数据个数计数器 reg uart_send_flag=0; //数据发送标志位 reg uart_tx_o_r=1; //发送数据寄存器,初始状态位高 always@(posedge Bus2IP_Clk) begin if(baud_div2==(BAUD_DIV>>1)) //当波特率计数器计数到数据发送中点时,产生采样信号baud_bps,用来发送数据 begin baud_bps2<=1'b1; baud_div2<=baud_div2+1'b1; end else if(baud_div2<BAUD_DIV && uart_send_flag)//数据发送标志位有效期间,波特率计数器累加,以产生波特率时钟 begin baud_div2<=baud_div2+1'b1; baud_bps2<=0; end else begin baud_bps2<=0; baud_div2<=0; end end always@(posedge Bus2IP_Clk) begin if(uart_tx_en_i) //接收数据发送使能信号时,产生数据发送标志信号 begin uart_send_flag<=1'b1; send_data<={1'b1,uart_tx_data_i,1'b0};//待发送数据寄存器装填,1bit起始信号0+8bit有效信号+1bit结束信号 end else if(bit_num2==4'd10) //发送结束时候,清楚发送标志信号,并清楚待发送数据寄存器内部信号 begin uart_send_flag<=1'b0; send_data<=10'b1111_1111_11; end end always@(posedge Bus2IP_Clk) begin if(uart_send_flag) //发送有效时候 begin if(baud_bps2)//检测发送点信号 begin if(bit_num2<=4'd9) begin uart_tx_o_r<=send_data[bit_num2]; //发送待发送寄存器内数据,从低位到高位 bit_num2<=bit_num2+1'b1; end end else if(bit_num2==4'd10) bit_num2<=4'd0; end else begin uart_tx_o_r<=1'b1; //空闲状态时,保持发送端位高电平,以备发送时候产生低电平信号 bit_num2<=0; end end assign uart_tx_o=uart_tx_o_r; // ------------------------------------------------------ // Example code to read/write user logic slave model s/w accessible registers // // Note: // The example code presented here is to show you one way of reading/writing // software accessible registers implemented in the user logic slave model. // Each bit of the Bus2IP_WrCE/Bus2IP_RdCE signals is configured to correspond // to one software accessible register by the top level template. For example, // if you have four 32 bit software accessible registers in the user logic, // you are basically operating on the following memory mapped registers: // // Bus2IP_WrCE/Bus2IP_RdCE Memory Mapped Register // "1000" C_BASEADDR + 0x0 // "0100" C_BASEADDR + 0x4 // "0010" C_BASEADDR + 0x8 // "0001" C_BASEADDR + 0xC // // ------------------------------------------------------ assign slv_reg_write_sel = Bus2IP_WrCE[0:0], slv_reg_read_sel = Bus2IP_RdCE[0:0], slv_write_ack = Bus2IP_WrCE[0], slv_read_ack = Bus2IP_RdCE[0]; // implement slave model register(s) always @( posedge Bus2IP_Clk ) begin if ( Bus2IP_Resetn == 1'b0 ) begin slv_reg0 <= 0; end else begin BAUD_DIV<=slv_reg0[31:18]; uart_tx_en_i<=slv_reg0[17]; uart_tx_data_i<=slv_reg0[16:9]; if(uart_rx_done) begin slv_reg0[8]<=uart_rx_done; slv_reg0[7:0]<=uart_rx_data_o; end else case ( slv_reg_write_sel ) 1'b1 : for ( byte_index = 0; byte_index <= (C_SLV_DWIDTH/8)-1; byte_index = byte_index+1 ) if ( Bus2IP_BE[byte_index] == 1 ) slv_reg0[(byte_index*8) +: 8] <= Bus2IP_Data[(byte_index*8) +: 8]; default : begin slv_reg0 <= slv_reg0; end endcase end end // SLAVE_REG_WRITE_PROC // implement slave model register read mux always @( slv_reg_read_sel or slv_reg0 ) begin case ( slv_reg_read_sel ) 1'b1 : slv_ip2bus_data <= slv_reg0; default : slv_ip2bus_data <= 0; endcase end // SLAVE_REG_READ_PROC // ------------------------------------------------------------ // Example code to drive IP to Bus signals // ------------------------------------------------------------ assign IP2Bus_Data = (slv_read_ack == 1'b1) ? slv_ip2bus_data : 0 ; assign IP2Bus_WrAck = slv_write_ack; assign IP2Bus_RdAck = slv_read_ack; assign IP2Bus_Error = 0; endmodule |