290 lines
7.8 KiB
Plaintext
290 lines
7.8 KiB
Plaintext
' ########### start ###########
|
||
' pic:PIC16C84
|
||
' ########### end ###########
|
||
|
||
'----------------------------------------------------------------------
|
||
' Mouse adapter for Atari ST and AMIGA
|
||
' 2018-02-15 (c) Paolo Iocco
|
||
'
|
||
' schematic ATARI ST
|
||
'
|
||
' +°--\ /---+
|
||
' Mouse_V<----|RA2 RA1|<--|10K|<--+5V (data)
|
||
' -|RA3 RA0|<--|10K|<--+5V (clk)
|
||
' -|RA4 O2|<----RC Oscillator
|
||
' +5V--|50K|-->|RES O1|-
|
||
' GND-------->|5 14|<--------+5V
|
||
' RB DB.9<----|RB0 RB7|-
|
||
' LB DB.6<----|RB1 RB6|-
|
||
' YB DB.4<----|RB2 RB5|----->DB.1 XB
|
||
' YA DB.3<----|RB3 RB4|----->DB.2 XA
|
||
' +---------+
|
||
'----------------------------------------------------------------------
|
||
'
|
||
' DB9 STANDARDS
|
||
' ~~~~~~~~~~~~~
|
||
'
|
||
' Atari ST AMIGA
|
||
'
|
||
' +---------> YB +---------> XB
|
||
' | +-------> YA | +-------> YA
|
||
' | | +-----> XA | | +-----> XA
|
||
' | | | +---> XB | | | +---> YB
|
||
' | | | | | | | |
|
||
' _____________ _____________
|
||
' 5 \ x o o o o / 1 5 \ x o o o o / 1
|
||
' \ o o o o / \ o o o o /
|
||
' 9 `~~~~~~~' 6 9 `~~~~~~~' 6
|
||
' | | | | | | | |
|
||
' | | | +----> Left Button | | | +----> Left Button
|
||
' | | +------> Power | | +------> Power
|
||
' | +--------> Ground | +--------> Ground
|
||
' +----------> Right Button +----------> Right Button
|
||
|
||
|
||
'Chip model
|
||
'#chip tiny84a, 8
|
||
#Chip 16C84, 4.19
|
||
#Config OSC = HS, WDT = OFF, PWRTE = ON, CP = OFF
|
||
|
||
#define PS2Clock PORTA.0
|
||
#define PS2Data PORTA.1
|
||
'#define PS2_DELAY 10 ms
|
||
|
||
#define XB PORTB.5
|
||
#define XA PORTB.4
|
||
#define YA PORTB.3
|
||
#define YB PORTB.2
|
||
#define LB PORTB.1
|
||
#define RB PORTB.0
|
||
#define Mouse_V PORTA.2
|
||
|
||
'Power-on Reset:
|
||
|
||
'Mouse: AA Self-test passed
|
||
'Mouse: 00 Mouse ID
|
||
'Host: FF Reset command
|
||
'Mouse: FA Acknowledge
|
||
'Mouse: AA Self-test passed
|
||
'Mouse: 00 Mouse ID
|
||
'Host: FF Reset command
|
||
'Mouse: FA Acknowledge
|
||
'Mouse: AA Self-test passed
|
||
'Mouse: 00 Mouse ID
|
||
'Host: FF Reset command
|
||
'Mouse: FA Acknowledge
|
||
'Mouse: AA Self-test passed
|
||
'Mouse: 00 Mouse ID
|
||
'Host: F3 Set Sample Rate
|
||
'Mouse: FA Acknowledge
|
||
'Host: 0A decimal 10
|
||
'Mouse: FA Acknowledge
|
||
'Host: F2 Read Device Type
|
||
'Mouse: FA Acknowledge
|
||
'Mouse: 00 Mouse ID
|
||
'Host: E8 Set resolution
|
||
'Mouse: FA Acknowledge
|
||
'Host: 03 8 Counts/mm
|
||
'Mouse: FA Acknowledge
|
||
'Host: E6 Set Scaling 1:1
|
||
'Mouse: FA Acknowledge
|
||
'Host: F3 Set Sample Rate
|
||
'Mouse: FA Acknowledge
|
||
'Host: 28 decimal 40
|
||
'Mouse: FA Acknowledge
|
||
'Host: F4 Enable
|
||
'Mouse: FA Acknowledge
|
||
'Initialization complete...
|
||
'If I then press the Left Button...
|
||
'Mouse: 09 1 1 00001001; bit0 = Left button state; bit3 = always 1
|
||
'Mouse: 00 1 1 No X-movement
|
||
'Mouse: 00 1 1 No Y-movement
|
||
'... and release the Left Button:
|
||
'Mouse: 08 0 1 00001000 bit0 = Left button state; bit3 = always 1
|
||
'Mouse: 00 1 1 No X-movement
|
||
'Mouse: 00 1 1 No Y-movement
|
||
|
||
|
||
setup:
|
||
DIR PS2Clock Out
|
||
DIR PS2Data IN
|
||
'Init PS2
|
||
Set PS2Clock Off
|
||
'Init Buttons
|
||
Set LB On
|
||
Set RB On
|
||
|
||
|
||
|
||
'Init Mouse
|
||
mouse = PS2ReadByte ' Self-test passed
|
||
'if mouse <> 0xaa then goto errore
|
||
mouse = PS2ReadByte ' Mouse ID
|
||
'if mouse <> 0x00 then goto errore
|
||
|
||
PS2WriteByte 0xff ' Reset command
|
||
mouse = PS2ReadByte ' Acknowledge
|
||
'if mouse <> 0xfa then goto errore
|
||
mouse = PS2ReadByte ' Self-test passed
|
||
'if mouse <> 0xaa then goto errore
|
||
mouse = PS2ReadByte ' Mouse ID
|
||
'if mouse <> 0x00 then goto errore
|
||
|
||
PS2WriteByte 0xff ' Reset command
|
||
mouse = PS2ReadByte ' Acknowledge
|
||
'if mouse <> 0xfa then goto errore
|
||
mouse = PS2ReadByte ' Self-test passed
|
||
'if mouse <> 0xaa then goto errore
|
||
mouse = PS2ReadByte ' Mouse ID
|
||
'if mouse <> 0x00 then goto errore
|
||
|
||
PS2WriteByte 0xfa ' Enter Stream Mode
|
||
mouse = PS2ReadByte ' Acknowledge
|
||
'if mouse <> 0xfa then goto errore
|
||
|
||
loop:
|
||
Do
|
||
|
||
|
||
' ------------------------
|
||
' D7 D6 D5 D4 D3 D2 D1 D0 (The D0 bit (LSB) is sent first)
|
||
' ------------------------
|
||
'(1) YV XV YS XS 1 0 R L (overflow, sign, buttons)
|
||
'(2) X7 X6 X5 X4 X3 X2 X1 X0 (X movement; -128 to +127)
|
||
'(3) Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y0 (Y movement; -128 to +127)
|
||
|
||
'L = Left Button State (1 = pressed down)
|
||
'R = Right Button State (1 = pressed down)
|
||
'XS = Direction of X movement (1 = LEFT)
|
||
'YS = Direction of Y movement (1 = UP)
|
||
'XV = Overflow of X movement value (1 = X overflow occured)
|
||
'YV = Overflow of Y movement value (1 = Y overflow occured)
|
||
'X7,...,X0 : X movement; 8-bit 2's-complement signed byte (-128 to +127)
|
||
'Y7,...,Y0 : Y movement; 8-bit 2's-complement signed byte (-128 to +127)
|
||
|
||
'Here are examples of data sent to the host (PC):
|
||
'------------------------------------------------
|
||
'(The least-significant bit of each data byte is sent first.)
|
||
'Move Left 1 unit : 0x18, 0xFF, 0x00
|
||
'Move Right 1 unit : 0x08, 0x01, 0x00
|
||
'Move Down 1 unit : 0x28, 0x00, 0xFF
|
||
'Move Up 1 unit : 0x08, 0x00, 0x01
|
||
'Press Left Button : 0x09, 0x00, 0x00
|
||
'Release Left Button : 0x08, 0x00, 0x00
|
||
'Press Right Button : 0x0C, 0x00, 0x00
|
||
'Release Right Button : 0x08, 0x00, 0x00
|
||
|
||
'13. Interpret the direction bits (explained in the Mouse Interface section)
|
||
'14. Interpret the X and Y motion bytes
|
||
'15. Update cumulative X,Y motion and display on LCD
|
||
'16. Repeat steps 1-15 indefinitely
|
||
|
||
mouse = PS2ReadByte
|
||
mouse_x = PS2ReadByte
|
||
mouse_y = PS2ReadByte
|
||
|
||
if mouse.0 then
|
||
gosub LeftButtonON
|
||
else
|
||
gosub LeftButtonOFF
|
||
end if
|
||
|
||
if mouse.1 then
|
||
gosub RightButtonON
|
||
else
|
||
gosub RightButtonOFF
|
||
end if
|
||
|
||
if mouse.2 then
|
||
gosub MidButtonON
|
||
else
|
||
gosub MidButtonOFF
|
||
end if
|
||
|
||
if mouse_x <> 0 then gosub Move_x
|
||
if mouse_y <> 0 then gosub Move_y
|
||
|
||
Loop
|
||
|
||
LeftButtonON:
|
||
set LB off
|
||
return
|
||
LeftButtonOFF:
|
||
set LB on
|
||
return
|
||
|
||
RightButtonON:
|
||
set RB off
|
||
return
|
||
RightButtonOFF:
|
||
set RB on
|
||
return
|
||
|
||
MidButtonON:
|
||
'set MB off
|
||
return
|
||
MidButtonOFF:
|
||
'set MB on
|
||
return
|
||
|
||
Move_x:
|
||
if mouse.4 then
|
||
set XA on
|
||
wait 1 ms
|
||
set XB on
|
||
wait 1 ms
|
||
set XA off
|
||
wait 1 ms
|
||
set XB off
|
||
wait 1 ms
|
||
else
|
||
set XB on
|
||
wait 1 ms
|
||
set XA on
|
||
wait 1 ms
|
||
set XB off
|
||
wait 1 ms
|
||
set XA off
|
||
wait 1 ms
|
||
end if
|
||
return
|
||
|
||
Move_y:
|
||
if mouse.5 then
|
||
set YA on
|
||
wait 1 ms
|
||
set YB on
|
||
wait 1 ms
|
||
set YA off
|
||
wait 1 ms
|
||
set YB off
|
||
wait 1 ms
|
||
else
|
||
set YB on
|
||
wait 1 ms
|
||
set YA on
|
||
wait 1 ms
|
||
set YB off
|
||
wait 1 ms
|
||
set YA off
|
||
wait 1 ms
|
||
end if
|
||
return
|
||
|
||
errore:
|
||
goto errore
|
||
|
||
'Our code (shown further down) attempts to display X and Y coordinate motion of an optical mouse by doing the following:
|
||
'1. Set Clock low
|
||
'2. Wait at least 100 microseconds
|
||
'3. Set Data low
|
||
'4. Release Clock (Default is high on an open collector. Setting Data low and Clock high is the host requesting to send command, and this should cause the mouse to start generating clock signals and to wait for commands from the host)
|
||
'5. Wait for clock to go low (represents beginning of a clock input wave from mouse)
|
||
'6. Send next bit of “Enable Data Reporting” command to mouse (The “Enable Data Reporting” command is 0xF4, or 244 in binary, sent within the 12 bit frame described in the PS/2 Protocol section)
|
||
'7. Repeat steps 5 & 6 until the “Enable Data Reporting” command is completely sent and acknowledged by the mouse
|
||
'8. Release Clock
|
||
'9. Release Data (Clock high and Data high will put the mouse into idle mode and if Data Reporting has been enabled, the mouse should send a Data and Clock signal to the PIC when it detects button changes or motion)
|
||
'10. Wait for clock to go low
|
||
'11. Input Data bit into a 33 member array
|
||
'12. Repeat steps 10 & 11 until the 33 member array is filled with 3 data packets worth of information (three 11 bit data packets converted into an 33 member array of 1’s and 0’s)
|