MouseP.v.html 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. `timescale 1ns / 1ps // PS/2 Logitech mouse PDR 14.10.2013 / 8.9.2015
  2. module MouseP(
  3. input clk, rst,
  4. inout msclk, msdat,
  5. output [27:0] out);
  6. reg [9:0] x, y;
  7. reg [2:0] btns;
  8. reg Q0, Q1, run;
  9. reg [31:0] shreg;
  10. wire shift, endbit, reply;
  11. wire [9:0] dx, dy;
  12. // 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 bit
  13. // 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
  14. // ===============================================================
  15. // p y y y y y y y y 0 1 p x x x x x x x x 0 1 p Y X t s 1 M R L 0 normal
  16. // ---------------------------------------------------------------
  17. // p ----response--- 0 1 --InitBuf echoed--- 1 1 1 1 1 1 1 1 1 1 1 init
  18. // ---------------------------------------------------------------
  19. // p = parity (ignored); X, Y = overflow; s, t = x, y sign bits
  20. // initially need to send F4 cmd (start reporting); add start and parity bits
  21. localparam InitBuf = 32'b11111111111111111111110_11110100_0;
  22. assign msclk = ~rst ? 0 : 1'bz; // initial drive clock low
  23. assign msdat = ~run & ~shreg[0] ? 0 : 1'bz;
  24. assign shift = Q1 & ~Q0; // falling edge detector
  25. assign reply = ~run & ~shreg[11]; // start bit of echoed InitBuf, if response
  26. assign endbit = run & ~shreg[0]; // normal packet received
  27. assign dx = {{2{shreg[5]}}, shreg[7] ? 8'b0 : shreg[19:12]}; //sign+overfl
  28. assign dy = {{2{shreg[6]}}, shreg[8] ? 8'b0 : shreg[30:23]}; //sign+overfl
  29. assign out = {run, btns, 2'b0, y, 2'b0, x};
  30. always @ (posedge clk) begin
  31. run <= rst & (reply | run); Q0 <= msclk; Q1 <= Q0;
  32. shreg <= ~rst ? InitBuf : (endbit | reply) ? -1 : shift ? {msdat,
  33. shreg[31:1]} : shreg;
  34. x <= ~rst ? 0 : endbit ? x + dx : x; y <= ~rst ? 0 : endbit ? y + dy
  35. : y;
  36. btns <= ~rst ? 0 : endbit ? {shreg[1], shreg[3], shreg[2]} : btns;
  37. end
  38. endmodule