module i2c2mdio_module ( input wire clk, input wire rst, input wire sda, input wire scl, input wire mdio_in, output wire mdio_out, output wire sda_out );

reg [7:0] slave_address; reg [7:0] reg_address; reg [15:0] data_in; reg [15:0] data_out; reg [3:0] state;

wire sda_out_reg; wire sda_out_wire; wire sda_out_and; wire sda_out_or; wire sda_out_buf; wire sda_out_buf_reg; wire sda_out_buf_wire; wire sda_out_buf_and; wire sda_out_buf_or; wire sda_out_buf_tri; wire sda_out_buf_tri_reg; wire sda_out_buf_tri_wire; wire sda_out_buf_tri_and; wire sda_out_buf_tri_or;

wire scl_reg; wire scl_wire; wire scl_and; wire scl_or;

wire sda_out_buf_tri_reg_int; wire sda_out_buf_tri_wire_int; wire sda_out_buf_tri_and_int; wire sda_out_buf_tri_or_int;

assign sda_out = sda_out_buf_tri_reg_int; assign mdio_out = data_out[15];

assign scl = scl_reg; assign sda = sda_out_wire;

always @(posedge clk or posedge rst) begin if (rst) begin slave_address <= 8'b0; reg_address <= 8'b0; data_in <= 16'b0; data_out <= 16'b0; state <= 4'b0000; end else begin case (state) 4'b0000: begin if (scl_reg && !scl_wire) begin state <= 4'b0001; end end 4'b0001: begin if (!scl_reg && scl_wire) begin state <= 4'b0010; end end 4'b0010: begin if (!scl_reg && scl_wire) begin slave_address <= sda_out_buf_tri_reg_int ? {1'b1, slave_address[6:0]} : {1'b0, slave_address[6:0]}; state <= 4'b0011; end end 4'b0011: begin if (!scl_reg && scl_wire) begin state <= 4'b0100; end end 4'b0100: begin if (!scl_reg && scl_wire) begin reg_address <= sda_out_buf_tri_reg_int ? {1'b1, reg_address[6:0]} : {1'b0, reg_address[6:0]}; state <= 4'b0101; end end 4'b0101: begin if (!scl_reg && scl_wire) begin state <= 4'b0110; end end 4'b0110: begin if (!scl_reg && scl_wire) begin state <= 4'b0111; end end 4'b0111: begin if (!scl_reg && scl_wire) begin if (sda_out_buf_tri_reg_int) begin state <= 4'b1000; end else begin state <= 4'b1111; end end end 4'b1000: begin if (!scl_reg && scl_wire) begin state <= 4'b1001; end end 4'b1001: begin if (!scl_reg && scl_wire) begin data_in <= sda_out_buf_tri_reg_int ? {1'b1, data_in[14:0]} : {1'b0, data_in[14:0]}; state <= 4'b1010; end end 4'b1010: begin if (!scl_reg && scl_wire) begin state <= 4'b1011; end end 4'b1011: begin if (!scl_reg && scl_wire) begin state <= 4'b1100; end end 4'b1100: begin if (!scl_reg && scl_wire) begin state <= 4'b1101; end end 4'b1101: begin if (!scl_reg && scl_wire) begin data_out <= {1'b0, data_in[15:1]}; state <= 4'b1110; end end 4'b1110: begin if (!scl_reg && scl_wire) begin state <= 4'b1111; end end 4'b1111: begin if (!scl_reg && scl_wire) begin state <= 4'b0000; end end endcase end end

assign sda_out_reg = sda_out_buf_tri_reg | sda_out_buf_tri_wire; assign sda_out_wire = sda_out_buf_tri_reg & sda_out_buf_tri_wire; assign sda_out_and = sda_out_buf_tri_reg & sda_out_buf_tri_wire; assign sda_out_or = sda_out_buf_tri_reg | sda_out_buf_tri_wire; assign sda_out_buf = sda_out_buf_reg & sda_out_buf_wire; assign sda_out_buf_reg = sda_out_buf_tri_reg; assign sda_out_buf_wire = sda_out_buf_tri_wire; assign sda_out_buf_and = sda_out_buf_tri_reg & sda_out_buf_tri_wire; assign sda_out_buf_or = sda_out_buf_tri_reg | sda_out_buf_tri_wire; assign sda_out_buf_tri = sda_out_buf_tri_reg_int; assign sda_out_buf_tri_reg = sda_out_buf_tri_reg_int; assign sda_out_buf_tri_wire = sda_out_buf_tri_wire_int; assign sda_out_buf_tri_and = sda_out_buf_tri_reg_int & sda_out_buf_tri_wire_int; assign sda_out_buf_tri_or = sda_out_buf_tri_reg_int | sda_out_buf_tri_wire_int;

assign scl_reg = scl; assign scl_wire = scl; assign scl_and = scl_reg & scl_wire; assign scl_or = scl_reg | scl_wire;

assign sda_out_buf_tri_reg_int = (scl_reg & sda_out_buf_tri_reg) | (!scl_reg & sda_out_buf_tri_reg_int); assign sda_out_buf_tri_wire_int = (scl_reg & sda_out_buf_tri_wire) | (!scl_reg & sda_out_buf_tri_wire_int); assign sda_out_buf_tri_and_int = (scl_reg & sda_out_buf_tri_and) | (!scl_reg & sda_out_buf_tri_and_int); assign sda_out_buf_tri_or_int = (scl_reg & sda_out_buf_tri_or) | (!scl_reg & sda_out_buf_tri_or_int);

endmodule

用Verilog写一个i2c2mdio_module

原文地址: https://www.cveoy.top/t/topic/hWR9 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录