Advertisement
Guest User

MouseMasterSM

a guest
Jan 28th, 2020
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.92 KB | None | 0 0
  1. module MouseMasterSM(
  2. input CLK,
  3. input RESET,
  4. //Transmitter Control
  5. output SEND_BYTE,
  6. output [7:0] BYTE_TO_SEND,
  7. input BYTE_SENT,
  8. //Receiver Control
  9. output READ_ENABLE,
  10. input [7:0] BYTE_READ,
  11. input [1:0] BYTE_ERROR_CODE,
  12. input BYTE_READY,
  13. //Data Registers
  14. output [7:0] MOUSE_DX,
  15. output [7:0] MOUSE_DY,
  16. output [7:0] MOUSE_STATUS,
  17. output SEND_INTERRUPT
  18. );
  19. //////////////////////////////////////////////////////////////
  20. // Main state machine - There is a setup sequence
  21. //
  22. // 1) Send FF -- Reset command,
  23. // 2) Read FA -- Mouse Acknowledge,
  24. // 2) Read AA -- Self-Test Pass
  25. // 3) Read 00 -- Mouse ID
  26. // 4) Send F4 -- Start transmitting command,
  27. // 5) Read FA -- Mouse Acknowledge,
  28. //
  29. // If at any time this chain is broken, the SM will restart from
  30. // the beginning. Once it has finished the set-up sequence, the read enable flag
  31. 13
  32. // is raised.
  33. // The host is then ready to read mouse information 3 bytes at a time:
  34. // S1) Wait for first read, When it arrives, save it to Status. Goto S2.
  35. // S2) Wait for second read, When it arrives, save it to DX. Goto S3.
  36. // S3) Wait for third read, When it arrives, save it to DY. Goto S1.
  37. // Send interrupt.
  38. //State Control
  39. reg [3:0] Curr_State, Next_State;
  40. reg [23:0] Curr_Counter, Next_Counter;
  41. //Transmitter Control
  42. reg Curr_SendByte, Next_SendByte;
  43. reg [7:0] Curr_ByteToSend, Next_ByteToSend;
  44. //Receiver Control
  45. reg Curr_ReadEnable, Next_ReadEnable;
  46. //Data Registers
  47. reg [7:0] Curr_Status, Next_Status;
  48. reg [7:0] Curr_Dx, Next_Dx;
  49. reg [7:0] Curr_Dy, Next_Dy;
  50. reg Curr_SendInterrupt, Next_SendInterrupt;
  51. //Sequential
  52. always@(posedge CLK) begin
  53. if(RESET) begin
  54. Curr_State <= 4'h0;
  55. Curr_Counter <= 0;
  56. Curr_SendByte <= 1'b0;
  57. Curr_ByteToSend <= 8'h00;
  58. Curr_ReadEnable <= 1'b0;
  59. Curr_Status <= 8'h00;
  60. Curr_Dx <= 8'h00;
  61. Curr_Dy <= 8'h00;
  62. Curr_SendInterrupt <= 1'b0;
  63. end else begin
  64. Curr_State <= Next_State;
  65. Curr_Counter <= Next_Counter;
  66. Curr_SendByte <= Next_SendByte;
  67. Curr_ByteToSend <= Next_ByteToSend;
  68. Curr_ReadEnable <= Next_ReadEnable;
  69. Curr_Status <= Next_Status;
  70. Curr_Dx <= Next_Dx;
  71. Curr_Dy <= Next_Dy;
  72. Curr_SendInterrupt <= Next_SendInterrupt;
  73. end
  74. end
  75. //Combinatorial
  76. always@* begin
  77. Next_State = Curr_State;
  78. Next_Counter = Curr_Counter;
  79. Next_SendByte = 1'b0;
  80. Next_ByteToSend = Curr_ByteToSend;
  81. Next_ReadEnable = 1'b0;
  82. Next_Status = Curr_Status;
  83. Next_Dx = Curr_Dx;
  84. Next_Dy = Curr_Dy;
  85. Next_SendInterrupt = 1'b0;
  86. case(Curr_State)
  87. //Initialise State - Wait here for 10ms before trying to initialise the mouse.
  88. 4'h0: begin
  89. if(Curr_Counter == 5000000) begin // 1/100th sec at 50MHz clock
  90. Next_State = 4'h1;
  91. Next_Counter = 0;
  92. 14
  93. end else
  94. Next_Counter = Curr_Counter + 1'b1;
  95. end
  96. //Start initialisation by sending FF
  97. 4'h1: begin
  98. Next_State = 4'h2;
  99. Next_SendByte = 1'b1;
  100. Next_ByteToSend = 8'hFF;
  101. end
  102. //Wait for confirmation of the byte being sent
  103. 4'h2: begin
  104. if(BYTE_SENT)
  105. Next_State = 4'h3;
  106. end
  107. //Wait for confirmation of a byte being received
  108. //If the byte is FA goto next state, else re-initialise.
  109. 4'h3: begin
  110. if(BYTE_READY) begin
  111. if((BYTE_READ == 8'hFA) & (BYTE_ERROR_CODE == 2'b00))
  112. Next_State = 4'h4;
  113. else
  114. Next_State = 4'h0;
  115. end
  116. Next_ReadEnable = 1'b1;
  117. end
  118. //Wait for self-test pass confirmation
  119. //If the byte received is AA goto next state, else re-initialise
  120. 4'h4: begin
  121. if(BYTE_READY) begin
  122. if((BYTE_READ == 8'hAA) & (BYTE_ERROR_CODE == 2'b00))
  123. Next_State = 4'h5;
  124. else
  125. Next_State = 4'h0;
  126. end
  127. Next_ReadEnable = 1'b1;
  128. end
  129. //Wait for confirmation of a byte being received
  130. //If the byte is 00 goto next state (MOUSE ID) else re-initialise
  131. 4'h5: begin
  132. if(BYTE_READY) begin
  133. if((BYTE_READ == 8'h00) & (BYTE_ERROR_CODE == 2'b00))
  134. Next_State = 4'h6;
  135. else
  136. Next_State = 4'h0;
  137. end
  138. Next_ReadEnable = 1'b1;
  139. end
  140. //Send F4 - to start mouse transmit
  141. 4'h6: begin
  142. Next_State = 4'h7;
  143. Next_SendByte = 1'b1;
  144. Next_ByteToSend = 8'hF4;
  145. end
  146. //Wait for confirmation of the byte being sent
  147. 4'h7: if(BYTE_SENT) Next_State = 4'h8;
  148. //Wait for confirmation of a byte being received
  149. //If the byte is F4 goto next state, else re-initialise
  150. 4'h8: begin
  151. if(BYTE_READY) begin
  152. if(BYTE_READ == 8'hF4)
  153. Next_State = 4'h9;
  154. else
  155. Next_State = 4'h0;
  156. end
  157. 15
  158. Next_ReadEnable = 1'b1;
  159. end
  160. ///////////////////////////////////////////////////////////
  161. //At this point the SM has initialised the mouse.
  162. //Now we are constantly reading. If at any time
  163. //there is an error, we will re-initialise
  164. //the mouse - just in case.
  165. ///////////////////////////////////////////////////////////
  166. //Wait for the confirmation of a byte being received.
  167. //This byte will be the first of three, the status byte.
  168. //If a byte arrives, but is corrupted, then we re-initialise
  169. 4'h9: begin
  170. if(BYTE_READY & (BYTE_ERROR_CODE == 2'b00)) begin
  171. Next_State = 4'hA;
  172. Next_Status = BYTE_READ;
  173. end else
  174. Next_State = 4'h0;
  175.  
  176. Next_Counter = 0;
  177. Next_ReadEnable = 1'b1;
  178. end
  179. //Wait for confirmation of a byte being received
  180. //This byte will be the second of three, the Dx byte.
  181. 4'hA: begin
  182. ………………..
  183. FILL IN THIS AREA
  184. ……………….
  185. end
  186. //Wait for confirmation of a byte being received
  187. //This byte will be the third of three, the Dy byte.
  188. 4'hB: begin
  189. ………………..
  190. FILL IN THIS AREA
  191. ……………….
  192. end
  193. //Send Interrupt State
  194. 4'hC: begin
  195. Next_State = 4'h9;
  196. Next_SendInterrupt = 1'b1;
  197. end
  198. //Default State
  199. default: begin
  200. Next_State = 4'h0;
  201. Next_Counter = 0;
  202. Next_SendByte = 1'b0;
  203. Next_ByteToSend = 8'hFF;
  204. Next_ReadEnable = 1'b0;
  205. Next_Status = 8'h00;
  206. Next_Dx = 8'h00;
  207. Next_Dy = 8'h00;
  208. Next_SendInterrupt = 1'b0;
  209. end
  210. endcase
  211. end
  212. ///////////////////////////////////////////////////
  213. //Tie the SM signals to the IO
  214. //Transmitter
  215. assign SEND_BYTE = Curr_SendByte;
  216. 16
  217. assign BYTE_TO_SEND = Curr_ByteToSend;
  218. //Receiver
  219. assign READ_ENABLE = Curr_ReadEnable;
  220. //Output Mouse Data
  221. assign MOUSE_DX = Curr_Dx;
  222. assign MOUSE_DY = Curr_Dy;
  223. assign MOUSE_STATUS = Curr_Status;
  224. assign SEND_INTERRUPT = Curr_SendInterrupt;
  225. endmodule
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement