EECS 270: Lab 7

Real-World Interfacing with a Nintendo Controller

See the lab schedule for due dates.
Value: 275 points, plus up to 20 points of extra credit.

  1. Overview
  2. The purpose of this lab is to learn how to design, develop and implement a sequential digital circuit whose purpose is to interface with an actual hardware device.

  3. Preparation
  4. Design Specification
    1. Overview
    2. For this lab, your top-level module must be done in schematic capture. you may freely use Verilog after that, however we strongly suggest that you take advantage of the visual nature of the schematic tools and use the top-level module to connect other modules.

      The design task of this lab is to develop an interface to an 8-bit Nintendo controller. Your design will cause different LEDs to light up depending upon which button on the controller has been pressed. In addition, you are to design a state machine that observes the keys pressed on the controller. When a certain combination is observed, you are to light a different LED.

    3. Nintendo Controller
    4. The Nintendo controller has eight buttons: left, right, up, down, A, B, select and start. The controller is intended to plug into the Nintendo Entertainment System (NES) via a 7-pin connector. One wire is ground, one Vcc, and two are unused. The other three wires are called "Latch", "Pulse" and "Data". When the controller is connected to the NES, the NES asks it to send the current state of its buttons about 60 times a second (60 Hertz). The NES does this in two stages. First it sends a pulse on the Latch wire. On the rising edge of this pulse, the controller samples all eight buttons. The NES then sends pulses on the Pulse wire. For each rising edge on the Pulse wire, the controller sends information about a different button on the Data wire. Figure 1 provides an illustration of the timing. Additional information can be found with search of the web.

      Figure 1: NES controler transaction

      NES/controller communication
      The NES uses a serialized polling mechanism to query the state of the buttons 60 times a second. First, the NES sends a 12µs high signal to the Latch wire, telling the controller to latch the state of all buttons internally. 6µs later the NES sends 8 high pulses on the Pulse wire, each with a period of 12µs and a 50% duty cycle.

      After detecting the rising edge on Latch, the controller sends the status of the A button on the Data wire; Data is low if the A button is pressed, high if it is not. For each pulse on the Pulse wire, the controller drives the Data wire low if the button corresponding to that pulse was pressed. (The button states on Data are thus active low.) The button order is always the same: A, B, Select, Start, Up, Down, Left, Right. Again, see Figure 1 for more information.

      Generating Latch and Pulse
      You will need to generate the Latch and Pulse signals so that the controller responds correctly. The first step is to divide the clock to an appropriate frequency. After that, you are left with a number of options. You could design a state machine which generates Latch and Pulse. This state machine would restart every time you desired to sample the controller, and its outputs would be the Latch and Pulse signals.

      Another option is to use a counter (which is of course a state machine itself) to generate Latch and Pulse. Again, you would need to be sure you are only generating these signals when sampling the controller.

      Your Latch and Pulse have some restrictions. Each pulse should have a period between 8µs and 16µs and should have a 50% duty cycle. Latch should be high for a duration equal to pulse's period. Further, the time when pulse is actually changing should be a very small fraction of the time. As shown in Figure 2, it should be no more than 5% of the total time.


      Figure 2:Pulse timing

      Reading Data
      Once you correctly generate the Latch and Pulse signals, the controller will transmit the button status on the Data wire. You may wish to use a shift register to read each bit from the Data wire. Give careful consideration to how to clock the shift register.

      Displaying data to the LEDs
      You are to map the button data to the Green LEDs so that when a given button is pushed some corresponding LED is lit. You may map the buttons to the Green LEDs in any way you see fit so long as each button lights a unique LED.


      Figure 3: How your design interacts with the NES controller

    5. Easter Egg
    6. Background
      Programmers sometimes put pieces of code into an application that produce unexpected surprises. Users usually won't see them unless they press a certain key combination. They're usually found by word of mouth and rarely by accident. http://www.pccomputernotes.com/pcterms/glossarye.htm

      Many games have secret codes that cause the game to behave differently. These "Easter Eggs" are activated by pressing a certain sequence of keys. Most NES games have Easter Eggs of one type or another in them.

      Requirements
      Each student will be assigned a different 5-key code. When that code is entered into the NES controller, a single Red LEDs should be lit -- you may choose which one. The user must press the right buttons in order and press no other buttons between correct presses for the entry of the code sequence to be considered correct. The Red LED should remain lit until the start button on the controller is pressed. In all cases, the Green LEDs should continue to function as before. Once you have successfully implemented the required functionality, you can improve upon this Easter Egg feature to earn a few extra credit points. Information is available in the Deliverables section of this lab.

  5. Mapping inputs and outputs
  6. The DE2 board that we use has a fair bit of general purpose I/O (the pins on the far right of the board). We will connect the controllers for you as follows:
    1. Pulse, Blue, to GPIO_0[0] (D25)
    2. Latch, White, to GPIO_0[1] (J22)
    3. Data, Red, to GPIO_0[3] (E25)
    Further, Vcc (5 volts) and Ground (0 volts) are needed. Yellow is connected to pin 10 (Vcc) and Brown to pin 11 (Ground). Power and ground are always found at those pins; you need not modify your qsf file for those two pins. See Figure 4.

    Figure 4: Where the NES controller pins should be placed.

    The mapping of Green LEDs to buttons is done at your discression. You are also free to choose which Red LED to use for the Easter Egg.

    The 50Mhz system clock found on pin N2.

  7. Design Notes and Hints
  8. Deliverables
  9. Pre-Lab (100 Points)
    1. Questions (30 points)
      1. What is the period of the 50MHz system clock? (2 points)
      2. In order to generate a clock that has a period of 12µs, by how much would you have to divide the system clock? (5 points)
      3. Now, assume you can only divide the system clock by a power of two. What would be the best value to divide by if you want a clock period as close to 12µs as possible without going under that value? (5 points)
      4. If you never sent anything on Pulse (i.e., Pulse is always 0), but continued to send high pulses on Latch at a rate of 60 Hz, what would you expect to happen on Data? Explain. (9 points)
      5. In order to read information from the Data wire, you will need to clock a shift register. How will you generate the clock for the shift register? (9 points)
    2. Simulation (60 points)
      You are asked to turn in two simulation printouts. These simulations will need to cover a large amount of time (especially the second one). To get the simulator to do that, you need to modify the default file end time. (Tools → Options → Waveform editor). You need to do this before you create the inputs.
      1. Turn in a simulation which shows your Latch and Pulse signals being generated correctly to perform a single polling operation of the controller. Your simulation should look much like Figure 1 but will not include the data from the controller. You are to provide a 50MHz clock as input.(30 points)
      2. Turn in a simulation which shows your Latch and Pulse signals being generated correctly to perform two polling operation of the controller. Your simulation should look much like Figure 2 but should show Latch and Pulse rather than just Pulse. This simulation may run for quite a while. (30 points)
    3. Supporting documentation (10 points)
      Turn in a printout of your top-level schematic, any Verilog modules you wrote or other macros you designed. These should be printed at scales and orientations that are readable.

    In-lab (130+ points)
    Download the .sof file for the Nintendo Controller to the DE2 board and verify that your implementation is working properly. There are two parts to the in-lab assignment. The first is to demonstrate that your Green LEDs are correctly responding. (70 points)

    The second is to demonstrate that your Easter Egg works correctly. (60 points)

    Further, you may earn up to 20 points of extra credit for doing one or more of the following with the Easter Egg. Note that these assignments can be fairly difficult. Extra credit will not be accepted after the due date for the in-lab.

    Post-Lab (45 points)