Mouse: Difference between revisions

From NESdev Wiki
Jump to navigationJump to search
(→‎SNES Mouse: removing "read in IRQ" example (feels more like a superflous/confusing combinatorial example to me rather than a practical device))
(→‎SNES Mouse: add note that hyperkin did not implement software sensitivity cycling, reorganizing a bit for better structure)
Line 7: Line 7:


As with the [[standard controller]], the mouse is read by turning the latch ($4016.d0) on and off, and then reading bit 0 or bit 1 of $4016 or $4017 several times, but its report is 32 bits long as opposed to 8 bits.
As with the [[standard controller]], the mouse is read by turning the latch ($4016.d0) on and off, and then reading bit 0 or bit 1 of $4016 or $4017 several times, but its report is 32 bits long as opposed to 8 bits.
Bit 0 goes to the standard controller ports on an NES or AV Famicom; bit 1 goes to the Famicom 4-player adapter.


Some documents about interfacing with the mouse recommend reading the first 16 bits at one speed, delaying a while, and reading the other 16 bits at another speed, following logic analyzer traces from a Super NES console.
On an NES or AV Famicom, the mouse may be connected to bit 0 through the front controller ports. On the original Famicom, it would normally have to be connected to bit 1 instead through the expansion port.
However, these different speeds are merely an artifact of the main loop of ''Mario Paint'', and the mouse will give a correct report when read at any reasonable speed.
For example, a program could read 8 bits, wait a couple thousand cycles, and then read the other 24.


The first byte of the report is all zeroes and may be ignored.  The other three bytes are sent most significant bit first:
The first byte of the report is all zeroes and may be ignored.  The other three bytes are sent most significant bit first:
Line 29: Line 26:
+-------- Direction (1: left; 0: right)
+-------- Direction (1: left; 0: right)
</pre>
</pre>
=== Motion ===
Motion of the mouse is given as a displacement since the last mouse read, delivered in the third and fourth bytes of the report.
The displacements are in [[wikipedia:Signed number representations#Sign-and-magnitude method|sign-and-magnitude]], not [[wikipedia:Signed number representations#Two's complement|two's complement]].
The displacements are in [[wikipedia:Signed number representations#Sign-and-magnitude method|sign-and-magnitude]], not [[wikipedia:Signed number representations#Two's complement|two's complement]].
For example, $05 represents five mickeys (movement units) in one direction and $85 represents five mickeys in the other.
For example, $05 represents five mickeys (movement units) in one direction and $85 represents five mickeys in the other.
Line 50: Line 52:
   sta x_velocity
   sta x_velocity
</pre>
</pre>
=== Sensitivity ===


The mouse can be set to low, medium, or high sensitivity.
The mouse can be set to low, medium, or high sensitivity.
To change the sensitivity, send a clock while the latch ($4016.d0) is turned on:
 
On the original SNES mouse this can be changed by sending a clock while the latch ($4016.d0) is turned on:
<pre>
<pre>
   ldy #1
   ldy #1
Line 60: Line 65:
   sty $4016
   sty $4016
</pre>
</pre>
Some revisions of the mouse's microcontroller [http://problemkaputt.de/fullsnes.htm#snescontrollersmousetwobuttonmouse reportedly] power up in an unknown state and may return useless values before the sensitivity is changed for the first time.
Some revisions of the mouse's microcontroller [http://problemkaputt.de/fullsnes.htm#snescontrollersmousetwobuttonmouse reportedly] power up in an unknown state and may return useless values before the sensitivity is changed for the first time.
A mouse detection routine should cycle through the sensitivities anyway to ensure that the connected device is in fact a mouse.
Failure to do so is likely to cause things that are not a mouse to be detected as a mouse, such as half of a Four Score if player 3 or 4 is holding Right.


Using more than two mice on an AV Famicom is not recommended for two reasons:
The clone Hyperkin "Hyper Click Retro Style" mouse will not cycle its sensitivity this way. Instead it has a manual button on the underside that must be pressed by the user to cycle sensitivity. For this reason, it is not advised to use the software sensitivity cycling to automatically detect the presence of a mouse.
* A mouse draws 50 mA, which is much more current than the standard controller draws. Drawing too much current is likely to cause the voltage regulator to overheat.
 
* Changing player 1's sensitivity also affects player 3's, and changing player 2's sensitivity also affects player 4's.
=== Other notes ===


A program that reads the mouse while playing [[APU DMC|DPCM samples]] must proceed very carefully.
A program that reads the mouse while playing [[APU DMC|DPCM samples]] must proceed very carefully.
Line 73: Line 77:
Only the [[Standard controller#APU DMC conflict glitch|OAM DMA sync trick]] reliably avoids these glitches.
Only the [[Standard controller#APU DMC conflict glitch|OAM DMA sync trick]] reliably avoids these glitches.


Example games:
Using more than two mice on an AV Famicom is not recommended for two reasons:
* A mouse draws 50 mA, which is much more current than the standard controller draws. Drawing too much current is likely to cause the voltage regulator to overheat.
* Changing player 1's sensitivity also affects player 3's, and changing player 2's sensitivity also affects player 4's.
 
Some documents about interfacing with the mouse recommend reading the first 16 bits at one speed, delaying a while, and reading the other 16 bits at another speed, following logic analyzer traces from a Super NES console.
However, these different speeds are merely an artifact of the main loop of ''Mario Paint'', and the mouse will give a correct report when read at any reasonable speed.
For example, a program could read 8 bits, wait a couple thousand cycles, and then read the other 24.
 
=== Example games ===
* ''[[Thwaite]]''
* ''[[Thwaite]]''
* ''[[Action 53|Sliding Blaster]]''
* ''[[Action 53|Theremin]]''
* ''[[Emulator Tests#Input Tests|allpads]]''


== Hori Track ==
== Hori Track ==

Revision as of 03:11, 6 January 2019

Recent homebrew has adapted the SNES mouse to be used with the NES. Hori released a trackball for Famicom called the Hori Track. A famiclone made by the Subor company also had a mouse. All three use different protocols.

SNES Mouse

The Super NES Mouse (SNS-016) is a peripheral for the Super NES that was originally bundled with Mario Paint. It can be used with an NES through an adapter, made from an NES controller extension cord and a Super NES controller extension cord, that connects the respective power, ground, clock, latch, and data pins.

As with the standard controller, the mouse is read by turning the latch ($4016.d0) on and off, and then reading bit 0 or bit 1 of $4016 or $4017 several times, but its report is 32 bits long as opposed to 8 bits.

On an NES or AV Famicom, the mouse may be connected to bit 0 through the front controller ports. On the original Famicom, it would normally have to be connected to bit 1 instead through the expansion port.

The first byte of the report is all zeroes and may be ignored. The other three bytes are sent most significant bit first:

76543210  Second byte of report
||||++++- Signature: 0001
||++----- Current sensitivity (0: low; 1: medium; 2: high)
|+------- Left button (1: pressed)
+-------- Right button (1: pressed)

76543210  Third byte of report
|+++++++- Vertical displacement since last read
+-------- Direction (1: up; 0: down)

76543210  Fourth byte of report
|+++++++- Horizontal displacement since last read
+-------- Direction (1: left; 0: right)

Motion

Motion of the mouse is given as a displacement since the last mouse read, delivered in the third and fourth bytes of the report.

The displacements are in sign-and-magnitude, not two's complement. For example, $05 represents five mickeys (movement units) in one direction and $85 represents five mickeys in the other. To convert these to two's complement, use negation:

  ; Convert to two's complement
  lda third_byte
  bpl :+
  eor #$7F
  sec
  adc #$00
:
  sta y_velocity

  lda fourth_byte
  bpl :+
  eor #$7F
  sec
  adc #$00
:
  sta x_velocity

Sensitivity

The mouse can be set to low, medium, or high sensitivity.

On the original SNES mouse this can be changed by sending a clock while the latch ($4016.d0) is turned on:

  ldy #1
  sty $4016
  lda $4016
  dey
  sty $4016

Some revisions of the mouse's microcontroller reportedly power up in an unknown state and may return useless values before the sensitivity is changed for the first time.

The clone Hyperkin "Hyper Click Retro Style" mouse will not cycle its sensitivity this way. Instead it has a manual button on the underside that must be pressed by the user to cycle sensitivity. For this reason, it is not advised to use the software sensitivity cycling to automatically detect the presence of a mouse.

Other notes

A program that reads the mouse while playing DPCM samples must proceed very carefully. Sample playback causes occasional double reads on $4016 and $4017, deleting a bit from the stream read back. The re-reading solution that can be used for the standard controller fails here because each latch pulse sent to a mouse will clear its count of accumulated movement, and there is no known way to reliably detect corrupted data. Only the OAM DMA sync trick reliably avoids these glitches.

Using more than two mice on an AV Famicom is not recommended for two reasons:

  • A mouse draws 50 mA, which is much more current than the standard controller draws. Drawing too much current is likely to cause the voltage regulator to overheat.
  • Changing player 1's sensitivity also affects player 3's, and changing player 2's sensitivity also affects player 4's.

Some documents about interfacing with the mouse recommend reading the first 16 bits at one speed, delaying a while, and reading the other 16 bits at another speed, following logic analyzer traces from a Super NES console. However, these different speeds are merely an artifact of the main loop of Mario Paint, and the mouse will give a correct report when read at any reasonable speed. For example, a program could read 8 bits, wait a couple thousand cycles, and then read the other 24.

Example games

Hori Track

Hori produced a trackball compatible with Moero Pro Soccer, Moero Pro Soccer, Putt Putt Golf, and US Championship V'Ball. It was released in Japan, and what appears to be a prototype U.S. version was exhibited behind glass in Nintendo World, but the U.S. version never reached stores.

Report byte 1 is the same as an NES.

Byte 2, MSB first:

7654 3210
|||| ++++- Axis 2, signed 4 bit, XOR with $F
++++------ Axis 1, signed 4 bit, XOR with $F

Byte 3, MSB first:

7654 3210
|||| ++++- Unknown (read and unused by games)
||++------ ID byte (1 or 2 depending on version)
|+-------- Unknown (speculated by nocash to be the speed switch)
+--------- Rotation mode switch (0: R, 1: L)

In rotation mode L, Up on the Control Pad points up, axis 1 points down, and axis 2 points right. In rotation mode R, Up on the Control Pad points right, axis 1 points left, and axis 2 points down.

Subor Mouse

This came with Subor's SB2000 famiclone system.

Example games:

  • Educational Computer 2000

Input ($4016 write)

7  bit  0
---- ----
xxxx xxxS
        |
        +- Strobe

Output ($4017 read)

7  bit  0
---- ----
xxxx xxxD
        |
        +- Serial data

The mouse returns either 1-byte or 3-byte responses based on the size of the x/y axis movement since the last read. If the movement value for both the X and Y axis is between -1 and 1 (inclusively), the mouse returns a 1-byte response. Otherwise, a 3-byte response is sent. In this case, the strobe bit must be toggled 3 times in a row to read the entire response (e.g read $4017 8 times, toggle strobe bit, read 8 more times, etc.)

1-byte response format

LRXXYY00
||||||||
||||||++- Always 0
||||++--- Y movement (0: no movement, 1 or 2 = went down 1 unit, 3 = went up 1 unit)
||++----- X movement (0: no movement, 1 or 2 = went right 1 unit, 3 = went left 1 unit)
|+------- Right mouse button (1: pressed)
+-------- Left mouse button (1: pressed)

3-byte response format

Byte 1

LRSXTY01
||||||||
||||||++- Always 01
|||||+--- Y movement (Bit 4)
||||+---- Y movement direction (1: up, 0: down)
|||+----- X movement (Bit 4)
||+------ X movement direction (1: left, 0: right)
|+------- Right mouse button (1: pressed)
+-------- Left mouse button (1: pressed)

Byte 2

--XXXX10
  ||||||
  ||||++- Always 10
  ++++--- X movement (Bits 0-3)

Byte 3

--YYYY11
  ||||||
  ||||++- Always 11
  ++++--- Y movement (Bits 0-3)

References