/*
 * $Header: /smsa/bsd:include/dev/kb_protocol.h:bsd  1.5  $
 * $Source: /smsa/bsd:include/dev/kb_protocol.h: $
 *
 * Copyright (c) 1988 Acorn Computers Ltd., Cambridge, England
 */

#ifndef __dev_kb_protocol_h
#define __dev_kb_protocol_h

/* kb_protocol.h
 *
 * Description of A500/Archimedes keyboard interface protocols.
 * The prefix K5_ is used for A500-specific codes, KA_ for the
 * equivalent Archimedes ones, and KB_ for common codes.
 *
 * Note - we have not bothered to define all the funny codes for
 * things like software protection devices.
 */

/* Keyboard Matrix information */

#define K5_KM_LROWS		16		/* Logical rows possible */
#define K5_KM_NROWS		10		/* normal key rows 0..9 only */
#define K5_KM_COLS		10		/* 10 columns, 0..9 */

/*
 * A500 keyboard matrix is a little irregular - only column 0 has
 * more than 10 rows..
 */

#define K5_SPECIAL__COL		0		/* specials: col 0, rows 10-15 */
#define K5_SPECIAL_SHIFT	10		/* SHIFT key */
#define K5_SPECIAL_LOOKS	11		/* LOOKS key */
#define K5_SPECIAL_CMD		12		/* CMD (control) key */
#define K5_SPECIAL_MB_R		13		/* right mouse button */
#define K5_SPECIAL_MB_M		14		/* middle mouse button */
#define K5_SPECIAL_MB_L		15		/* left mouse button */
#define K5_KM_SPECIALS		 6		/* how many keys outside 10x10 matrix */

#define K5_MAP_SIZE	(K5_KM_NROWS * K5_KM_COLS + K5_KM_SPECIALS)

/* Archimedes keyboard matrix is much easier */

#define KA_KM_ROWS		8		/* (includes mouse buttons) */
#define KA_KM_COLS		16		/* 16 columns (0-0xF) */

#define KA_MAP_SIZE	(KA_KM_ROWS * KA_KM_COLS)


/* Keyboard protocol values */

/* Reset sequence codes are common to both directions (and keyboard types) */

#define KB_RESET	0x80        /* + 7 bits (reset type) */
#define KB_HARD_RESET	(KB_RESET + 0x7F)
#define KB_RESET_ACK_1	(KB_RESET + 0x7E)
#define KB_RESET_ACK_2	(KB_RESET + 0x7D)

/* Codes sent from a keyboard to the main unit */

/* The keyboard identification code (used to distinguish keyboard types) */

#define K5_KEYBOARD_ID		0x10		/* + 4 bits (twice: lo, hi) */
#define K5_KEYBOARD_ID_BITS	4
#define K5_KEYBOARD_ID_MASK	0x0F
#define KA_KEYBOARD_ID		0x80		/* + 6 bits, in this one byte */
#define KA_KEYBOARD_ID_BITS	6
#define KA_KEYBOARD_ID_MASK	0x3F

/*
 * Note: the actual keyboard IDs (as issued from the hardware) are defined
 * in vcioctl.h, because they are user-visible.
 */

/*
 * Keyboard transitions - both keyboard types send two bytes for each transition;
 * the first byte contains the row code, the second the column code: 4 bits each.
 * The top nibble of each byte in a pair is the transition type code (up or down).
 */
#define K5_KEY_DOWN		0x20
#define K5_KEY_UP		0x30
#define KA_KEY_DOWN		0xC0
#define KA_KEY_UP		0xD0
#define KB_KEY_MASK		0x0F		/* Common mask for row/col info */

/* Information from the keyboard about relative movement of the mouse */
#define K5_MOUSE_POS		0x40		/* + 6 bits (twice: X, Y) */
#define K5_MOUSE_POS_BITS	6		/* how many bits of info */
#define K5_MOUSE_POS_MASK	0x3F		/* how to get at them */
#define KA_MOUSE_POS		0x00		/* + 7 bits (twice: X, Y) */
#define KA_MOUSE_POS_BITS	7		/* how many bits of info */
#define KA_MOUSE_POS_MASK	0x7F		/* how to get at them */

/* Codes sent from the main unit to a keyboard */

/* Codes to set/reset the state of the LED(s) */
#define K5_LED_ON		0x00		/* + 4 bits (ignored) */
#define K5_LED_OFF		0x10		/* + 4 bits (ignored) */
#define KA_LEDS_SET		0x00		/* + 3 bits state data */

/* ARM -> KB request types (codes are common to both keyboards) */
#define KB_REQ			0x20		/* + 4 bits (request type) */
#define KB_REQ_KEYBOARD_ID	(KB_REQ + 0x0)
#define KB_REQ_MOUSE_POS	(KB_REQ + 0x2)

/*
 * ARM -> KB acknowledge types: the basic "acknowledge" byte is 0011xxxx on
 * both keyboard types, but the individual codes differ slightly between them.
 */
#define KB_ACK			0x30
#define K5_ACK_BYTE		(KB_ACK + 0x0)	/* A500: ack first byte of two */
#define KA_ACK_BYTE		(KB_ACK + 0xF)	/* Arch: ack first byte of two */
#define KB_ACK_NULL		(KB_ACK + 0x0)	/* common: ack and disable events */
#define KB_ACK_KBS		(KB_ACK + 0x1)	/* common: allow k/b events only */
#define KA_ACK_MOUSE		(KB_ACK + 0x2)	/* Arch: allow mouse events only */
#define K5_ACK_MOUSE		(KB_ACK + 0x2)	/* A500: allow mouse events only */
#define KA_ACK_KBS_MOUSE	(KB_ACK + 0x3)	/* Arch: allow keyboard & mouse events */
#define K5_ACK_KBS_MOUSE	(KB_ACK + 0x3)	/* A500: allow keyboard & mouse events */

#endif/*__dev_kb_protocol_h*/

/* EOF kb_protocol.h */
