Skip to content

Commit

Permalink
ps2: Added mouse and keyboard stacked decoders
Browse files Browse the repository at this point in the history
Mouse decoder interprets the 3-byte packets for movement and clicking, but
ignores the configuration commands from the host.
Keyboard decoder interprets press and release messages for each key but does not keep track of state (capslock, shift, etc).
  • Loading branch information
kamocat committed Apr 28, 2023
1 parent 7d8409c commit 110d23e
Show file tree
Hide file tree
Showing 5 changed files with 363 additions and 0 deletions.
25 changes: 25 additions & 0 deletions decoders/ps2_keyboard/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
##
## This file is part of the libsigrokdecode project.
##
## Copyright (C) 2023 Marshal Horn <[email protected]>
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program. If not, see <http://www.gnu.org/licenses/>.
##

'''
This protocol decoder can decode PS/2 keyboard commands.
It should be stacked on the PS/2 packet decoder.
'''

from .pd import Decoder
90 changes: 90 additions & 0 deletions decoders/ps2_keyboard/pd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
##
## This file is part of the libsigrokdecode project.
##
## Copyright (C) 2023 Marshal Horn <[email protected]>
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, see <http://www.gnu.org/licenses/>.
##

import sigrokdecode as srd
from .sc import key_decode

class Ps2Packet:
def __init__(self, val, host=False, pok=False, ack=False):
self.val = val #byte value
self.host = host #Host transmissions
self.pok = pok #Parity ok
self.ack = ack #Acknowlege ok for host transmission.

class Decoder(srd.Decoder):
api_version = 3
id = 'keyboard'
name = 'PS/2 Keyboard'
longname = 'PS/2 Keyboard'
desc = 'PS/2 keyboard interface.'
license = 'gplv2+'
inputs = ['ps2_packet']
outputs = []
tags = ['PC']
binary = (
('Keys', 'Key presses'),
)
annotations = (
('Press', 'Key pressed'),
('Release', 'Key released'),
('Ack', 'Acknowledge'),
)
annotation_rows = (
('keys', 'key presses and releases',(0,1,2)),
)

def __init__(self):
self.reset()

def reset(self):
self.sw = 0 #for switch statement
self.ann = 0 #defualt to Make code (keypress)
self.extended = False

def start(self):
self.out_binary = self.register(srd.OUTPUT_BINARY)
self.out_ann = self.register(srd.OUTPUT_ANN)

def decode(self,startsample,endsample,data):
if data.host:
# Ignore host commands or interrupted keycodes
self.reset()
return
if self.sw < 1:
self.ss = startsample
self.sw = 1
if self.sw < 2:
if data.val == 0xF0: #Break code
self.ann = 1
return
elif data.val == 0xE0: #Extended character
self.extended = True
return
elif data.val == 0xFA: #Acknowledge code
c = ['Acknowledge','ACK']
self.ann = 2
self.sw = 4
if self.sw < 3:
c = key_decode(data.val, self.extended)

self.put(self.ss,endsample,self.out_ann,[self.ann,c])
if self.ann == 0: #Make code
self.put(self.ss,endsample,self.out_binary,[0,c[-1].encode('UTF-8')])
self.reset()

99 changes: 99 additions & 0 deletions decoders/ps2_keyboard/sc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#A list of scancodes for the PS2 keyboard
ext = {
0x1F:'L Sup',
0x14:'R Ctrl',
0x27:'R Sup',
0x11:'R Alt',
0x2F:'Menu',
0x12:'PrtScr',
0x7C:'SysRq',
0x70:'Insert',
0x6C:'Home',
0x7D:'Pg Up',
0x71:'Delete',
0x69:'End',
0x7A:'Pg Dn',
0x75:['Up arrow','^'],
0x6B:['Left arrow','Left','<-'],
0x74:['Right arrow','Right','->'],
0x72:['Down arrow','Down','v'],
0x4A:['KP /','/'],
0x5A:['KP Ent','\n'],
}

std = {
0x1C:'A',
0x32:'B',
0x21:'C',
0x23:'D',
0x24:'E',
0x2B:'F',
0x34:'G',
0x33:'H',
0x43:'I',
0x3B:'J',
0x42:'K',
0x4B:'L',
0x3A:'M',
0x31:'N',
0x44:'O',
0x4D:'P',
0x15:'Q',
0x2D:'R',
0x1B:'S',
0x2C:'T',
0x3C:'U',
0x2A:'V',
0x1D:'W',
0x22:'X',
0x35:'Y',
0x1A:'Z',
0x45:'0)',
0x16:'1!',
0x1E:'2@',
0x26:'3#',
0x25:'4$',
0x2E:'5%',
0x36:'6^',
0x3D:'7&',
0x3E:'8*',
0x46:'9(',
0x0E:'`~',
0x4E:'-_',
0x55:'=+',
0x5D:'\|',
0x66:'Backsp',
0x29:['Space',' '],
0x0D:['Tab','\t'],
0x58:'CapsLk',
0x12:'L Shft',
0x14:'L Ctrl',
0x11:'L Alt',
0x59:'R Shft',
0x5A:['Enter','\n'],
0x76:'Esc',
0x5:'F1',
0x6:'F2',
0x4:'F3',
0x0C:'F4',
0x3:'F5',
0x0B:'F6',
0x83:'F7',
0x0A:'F8',
0x1:'F9',
0x9:'F10',
0x78:'F11',
0x7:'F12',
0x7E:'ScrLck',
}


def key_decode(code, extended):
try:
c = ext[code] if extended else std[code]
except KeyError:
fs = '[E0%0X]' if extended else '[%0X]'
c = fs % code
if not isinstance(0,list):
c = [c]
return c
25 changes: 25 additions & 0 deletions decoders/ps2_mouse/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
##
## This file is part of the libsigrokdecode project.
##
## Copyright (C) 2023 Marshal Horn <[email protected]>
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program. If not, see <http://www.gnu.org/licenses/>.
##

'''
This protocol decoder can decode PS/2 mouse commands.
It should be stacked on the PS/2 packet decoder.
'''

from .pd import Decoder
124 changes: 124 additions & 0 deletions decoders/ps2_mouse/pd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
##
## This file is part of the libsigrokdecode project.
##
## Copyright (C) 2023 Marshal Horn <[email protected]>
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, see <http://www.gnu.org/licenses/>.
##

import sigrokdecode as srd

class Ps2Packet:
def __init__(self, val, host=False, pok=False, ack=False):
self.val = val #byte value
self.host = host #Host transmissions
self.pok = pok #Parity ok
self.ack = ack #Acknowlege ok for host transmission.

class Decoder(srd.Decoder):
api_version = 3
id = 'mouse'
name = 'PS/2 Mouse'
longname = 'PS/2 Mouse'
desc = 'PS/2 mouse interface.'
license = 'gplv2+'
inputs = ['ps2_packet']
outputs = []
tags = ['PC']
binary = (
('bytes', 'Bytes without explanation'),
('movement', 'Explanation of mouse movement and clicks'),
)
annotations = (
('Movement', 'Mouse movement packets'),
)
annotation_rows = (
('mov', 'Mouse Movement',(0,)),
)

def __init__(self):
self.reset()

def reset(self):
self.packets = []
self.es = 0
self.ss = 0

def start(self):
self.out_binary = self.register(srd.OUTPUT_BINARY)
self.out_ann = self.register(srd.OUTPUT_ANN)

def metadata(self,key,value):
if key == srd.SRD_CONF_SAMPLERATE:
self.samplerate = value

def mouse_movement(self):
if len(self.packets) >= 3:
if not self.packets[0].host:
msg = ''
[flags,x,y] = [p.val for p in self.packets[:3]]
if flags & 1:
msg += 'L'
if flags & 2:
msg += 'M'
if flags & 4:
msg += 'R'
if flags & 0x10:
x = x-256
if flags & 0x20:
y = y-256
if x != 0:
msg += ' X%+d' % x
if flags & 0x40:
msg += '!!'
if y != 0:
msg += ' Y%+d' % y
if flags & 0x80:
msg += '!!'
if msg == '':
msg = 'No Movement'
ustring = ('\n' + msg).encode('UTF-8')
self.put(self.ss,self.es,self.out_binary, [1,ustring] )
self.put(self.ss,self.es,self.out_ann, [0,[msg]] )


def print_packets(self):
self.mouse_movement()
tag = "Host: " if self.packets[-1].host else "Mouse:"
octets = ' '.join(["%02X" % x.val for x in self.packets])
unicode_string = ("\n"+tag+" "+octets).encode('UTF-8')
self.put(self.ss,self.es,self.out_binary, [0,unicode_string] )
self.reset()

def mouse_ack(self,ss,es):
self.put(ss,es,self.out_binary, [0,b' ACK'] )

def decode(self,startsample,endsample,data):
if len(self.packets) == 0:
self.ss = startsample
elif data.host != self.packets[-1].host:
self.print_packets()
self.ss = startsample #Packets were cleared, need to set startsample again
if data.val == 0xFA and not data.host:
#Special case: acknowledge byte from mouse
self.mouse_ack(startsample,endsample)
self.reset()
return

self.packets.append(data)
self.es = endsample
#Mouse streaming packets are in 3s
#Timing is not guaranteed because host can hold the clock at any point
if len(self.packets)>2:
self.print_packets()

0 comments on commit 110d23e

Please sign in to comment.