EEC180/181AB Tutorial: Using a PS/2 Mouse with the DE1-SoC

EEC180/181AB, Digital Systems Design

  1. Overview
  2. This tutorial provides and describes verilog to allow the DE1-SoC FPGA board to interface with a PS/2 mouse. The PS/2 mouse controller module supports three button inputs - left click, right click, and middle click - along with mouse movements in the X (horizontal) and Y (vertical) directions.

    The design interfaces with the DE1-SoC's PS/2 port, so a USB mouse will require a small inexpensive USB-to-PS/2 converter.

    The verilog code provided comprises two files: the mouse controller module and a testbench module to quickly and easily get the mouse working.

  3. Verilog Code
  4. mouse.v

    The mouse module can be placed into a top-level module or submodule with access to the PS/2 ports and a 50 MHz clock. Two additional control signals need to be connected for START and RESET functionality.

    The mouse controller module has the following inputs, outputs, and inouts:
      iSTART  – input, active xxxx, for transmitting instructions to device
      iRST_n  – input, active low?, FSM reset signal
      iCLK_50 – input, 50 MHz clock signal from DE1-SoC
      PS2_CLK – inout, for ps2_clock signal to/from the PS2 device
      PS2_DAT – inout, for ps2_data signal to/from the PS2 device
      oLEFBUT – output, active high, left mouse button press
      oMIDBUT – output, active high, middle mouse button press
      oRIGBUT – output, active high, right mouse button press
      oX[7:0] – output, active xxxx, mouse horizontal displacement
      oY[7:0] – output, active xxxx, mouse vertical displacement

    Mouse displacement signals oX and oY are encoded as 8-bit unsigned values that "wrap" when the value exceeds 255 or drops below zero.

    The PS/2 port utilizes two wires for bi-directional communication to send initialization signals to the device and return mouse command data to the board.

  5. Instructions for Use
  6. Select the PS/2 option when generating a pin assignment file using the DE1-SoC SystemBuilder. The generated top level module contains two pairs of "inout PS2_*" ports: PS2_DAT, PS2_DAT2, PS2_CLK, and PS2_CLK2. The mouse.v module utilizes the pair of signals without a "2" at the end of their names.

    Use of the PS/2 port requires the PS/2 option to be selected when generating a pin assignment file using the DE1-SoC SystemBuilder program. Once the assignment is generated, the corresponding top level module contains two sets of "inout" DATA and CLOCK ports, named PS2_DAT, PS2_DAT2 and PS2_CLK, PS2_CLK2 respectively, as the PS/2 standard supports both a mouse and keyboard using a physical adapter. This module only utilizes the first set of ports as the controller only supports mouse functionality.

    When implementing PS/2 Mouse functionality, ensure the PS/2 pin assignments are included when generating a pin assignment file with the SystemBuilder and the PS/2 inout ports are included in the top-level module ports.

  7. A Simple Testbench
  8. mouse_test.v

    The Testbench module is intended to be placed into a top-level module with KEY[1], KEY[0], and SW[0] used for control signal input and data outputs going to the LEDR and HEX devices on the DE1-SoC. The left, middle, and right mouse button clicks will be displayed on segment 0 of HEX2, HEX1, and HEX0, respectively. LEDR[7:0] will display the raw mouse displacement data with SW[0] selecting which axis is shown. When SW[0] is high, the Y (vertical) axis is displayed. When SW[0] is low, the X (horizontal) axis is displayed.

    The Testbench module can be instantiated in a top-level module with the following:

    mouse_test U1 (
      .iKEYS     (KEY[1:0]),
      .iSW0      (SW[0]),
      .iCLK_50   (CLOCK_50),
      .ioPS2_CLK (PS2_CLK),
      .ioPS2_DAT (PS2_DAT),
      .oHEX0     (HEX0),
      .oHEX1     (HEX1),
      .oHEX2     (HEX2),
      .oLEDR     (LEDR[7:0])
      );
    

    The testbench module has the following inputs and outputs:
         iKEYS - KEY[1:0]    -> Push buttons for reset/instruction transmission
                         --> KEY[1] configured as reset
                         --> KEY[0] configured as instruction transmission
         iSW0  - SW[0]       -> SW to determine if X or Y movement value is shown on LEDR
         ioPS2_CLK - PS2_CLK -> clock inout signal for PS2 mouse device
         ioPS2_DAT - PS2_DAT -> data inout signal for PS2 mouse device
         oHEX0 - HEX0        -> 7-Segment output used to show Right Mouse Button click
         oHEX1 - HEX1        -> 7-Segment output used to show Middle Mouse Button click
         oHEX1 - HEX1        -> 7-Segment output used to show Left Mouse Button click
         oLEDR - LEDR[7:0]   -> LEDR output used to show X/Y movement value
    

  9. Reference
  10. Another reference design can be found here: http://www-ug.eecg.utoronto.ca/desl/MSO_de1_tutorials.html at a website at the University of Toronto.



EEC 181 | B. Baas | ECE Dept. | UC Davis
2022/06/08  Written
2024/04/15  Re-written
Author: Derek Li