[comp.sources.atari.st] v02i043: decky -- Connect DEC keyboard to ST

koreth@ssyx.ucsc.edu (Steven Grimm) (06/14/89)

Submitted-by: mark@ncsuvx.ncsu.edu (Mark Boyd)
Posting-number: Volume 2, Issue 43
Archive-name: decky

	Here is the sources version of the DEC keyboard stuff.
If you haven't seen my note with the binary version, please
read that before dealing with this.

#!/bin/sh
# shar:	Shell Archiver  (v1.22)
#
#	Run the following text with /bin/sh to create:
#	  DECKEY.LIS
#	  MIDIKB.S
#	  SERIALKB.S
#
sed 's/^X//' << 'SHAR_EOF' > DECKEY.LIS &&
XDECKeybd.doc                                               June, 1989
X  
XIntroduction:
X
XI'm typing this on a DEC LK201 keyboard connected to an Atari 1040ST
Xthrough the midi port.  Now I won't have to get a Mega to have a
Xdecent, detachable, keyboard!  Besides, this keyboard has a better
Xlayout and ( IMHO ) feel than the one that comes with a Mega.  
X
XOne of reason that I started using ST's was that its keyboard layout is
Xalmost identical to a DEC keyboard.  Since my microcomputers spend a lot
Xof time acting as terminals talking to VAX computer systems, the
Xcompatible keyboard layout is very important to me.  On the other hand,
XI have always hated the feel of  the ST keyboard.  I think it is the
XST's worst feature.  Now I have been able to keep the layout and get
Xmuch better feel in a detached keyboard.
X
XLK201s keyboards are used on all current DEC products, including
Xterminals and workstations. Moreover, they were used on the DEC Rainbow
XPC's and on the previous generation of DEC terminals.  As a result,
Xthey are fairly common at ham and computer fests. You could also get a
Xnew one from DEC as a replacement part, albeit for too much money!
X
XThe obvious choice for an external ST keyboard would be an extended PC
Xkeyboard.  Its layout is fairly close to the ST's, and it is relative
Xcheap and very available.  Unfortuately, its electrical interface is
Xquite incompatible with any of the Atari's ports. PC keyboard use with
Xthe ST would require a complicated external interface adapter.  I don't
Xknow if anyone has done it, but I decided it wasn't worth the trouble.
XOn the other hand, the LK201 keyboard can be wired directly to either
Xthe serial or midi ports. 
X
XLK201s conform to RS-423 standards at 4800 baud, 8 bits, no parity, one
Xstop bit.  They send a single byte of code ( a scan code ) for most key
Xpresses, only the shift and control keys send codes on both down and
Xup. When a key or keys is help down, a metronome ( repeat ) code is
Xsent at a fixed rate.
X
XThe ST keyboards use a serial interface at 7.8125 K baud. They send a
Xscan code when a key is depressed or released. The software require to
Xconvert input from the DEC keyboard to that used by the ST is straight
Xforward.
X
XIf you are willing to use your serial port, no modifications to the ST
Xor keyboard are required.  All it takes is a small, external, power
Xsupply and a RJ11 ( modular phone plug ) to DB25 adapter.  The
Xlimitation when using the serial port is that you can't use the ST as a
Xterminal with the external keyboard.  The midi port interface doesn't
Xhave this limitation but it does require internal modifications to the
XST.
X
XThe power supply requirement is 200ma. at 12 V.  The voltage is
Xregulated internally so it isn't very critical.  A range from 10 V. to
X15 V. seems OK, although you might have internal heat problems in the
Xkeyboard at the upper end of that range. Suitable wall transformer type
Xsupplies are available for less than $10.  Since I'm not going to buy a
XMega ST, my ST's power supply will have to handle 4 megabytes of memory
Xin the near future. I didn't want to further load the ST's already
Xmarginal power supply with the extra keyboard.  
X
XThe midi port problem is that its baud rate is is hardwired into the
Xcomputer.  Changing the midi port's baud rate to 4800 baud is
Xstraightforward, but it should only be done by someone with good
Xsoldering skills and PC ( that's Printed Circuit, not Personal
XComputer!  ) experience.
X
XThe modification uses one TTL IC, half a dozen short pieces of wire,
Xtwo solder connections to pins on existing IC's and one trace cut on
Xthe top of the ST circuit board.  It isn't even necessary to remove the
Xmetal shield on a 1040 ST, because everything you need to get at is
Xlocated under the disk drive.  Since this change will affect the
Xoperation of the midi port, you may wish to mount a switch ( SPDT )
Xthat selects either 4800 or the normal ( 31.25K )baud rate.
X
XNo external keyboard, unless it is simply an ST keyboard mounted in a
Xseparate case, can replace the built in keyboard.  STs need the
Xmouse, joystick, and clock functions of the original keyboard.  An
Xexternal keyboard must simply work in parallel with the original
Xkeyboard.
X
XSoftware:
X
XWhen you strike a key on the ST's keyboard, a scancode is sent from it
Xto a serial port on the main board.  This causes the serial port to
Xgenerate an interrupt.  The interrupt is serviced by a routine that:
X
X        1. Gets the scan code sent by the keyboard
X        2. Determines wether it is dealing with
X           regular keys, shift keys or the mouse
X        3. Performs the appropriate function.
X
XAs a result of a regular key press, the scancode and an ascii code  
X( looked up in a set of tables ) are placed in a queue.  Shift codes
X( right shift, left shift, control, caps lock, and alternate ) simply
Xcause a change the contents of a flag byte.  The contents of this byte
Xdetermine which of three tables is used for looking up the scan code
Xand if control or ALT are used as modifiers.
X
XAn external keyboard must duplicate, as much as is possible, these
Xregular key and shift functions and not disrupt the other ( mouse,
Xclock, and joystick ) keyboard functions.  Since the builtin keyboard
Xfunctions involve modification of data structures ( keyboard queue and
Xshift byte ) in memory, the external keyboard software can simply
Xmodify the same data structures.  If the keyboard software was not
Xinterrupt driven with these intermediate data stuctures, it would be
Xmuch more dificult to add an external keyboard.
X
XThe serial port, the keyboard port, and the midi port all use similar
Xinterfaces.  This makes it easy to write an interrupt handler for the
Xserial or midi port that duplicate the  services provide by keyboard
Xport interrupt handler.  Most of the needed subroutines are already in
Xthe ST's ROMs.  The code needed to add a keyboard is quite small, about
X700 bytes. I'm sending uuencoded sources and binaries to
Xcomp.{sources|binaries}. atari.st.
X
XBugs/Differences from the ST keyboard:
X
XWell this an 'alpha' release, so it probably has a few unintended
X'features'!  On the other hand, it is a small, simple, and straight
Xforward program.  I have been using the midi version for several weeks
Xon a 1040 ST and a friend has been using the serial port version on a
X520 ST for two weeks.  We haven't found any problems.
X
XI did have trouble when I tried using the external keyboard to replace
Xmy mouse buttons.  I had to eliminate that 'feature' since it regularly
Xcrashed the ST! The serial version won't work with a screen blanker
Xthat monitors the keyboard/midi interrupt vector since it doesn't use
Xthat vector!  The midi version works with idle 1.2, but it must be run
Xbefore idle is run.
X
XI didn't put a lot of effort into handling the alt key because I very
Xrarely use it.  On the LK201, the alt key says 'Compose Character' and,
Xbecause of the way I handled it, it only affects the next key pressed. 
XThe only program I use that has a problem with alt is Uniterm.
XUnfortunately I use Uniterm more than any other program ( thank you
XSimon Poole!  ).  Uniterm doesn't use the normal alt flag, so I just do
Xthe Uniterm alt stuff from my regular keyboard.
X
XThe LK201 keyboard is programmable, but I have not made any use of this
Xfeature.  The midi interface is only 'RS423 compatible' for input, so
Xextra hardware would be required there.  The serial interface should be
Xable to talk to the keyboard, but I had no luck in my limited attempts
Xto do so.  Since I am comfortable with the keyboards default settings,
XI made my interface input only.
X
XNow let's consider the keyboard remappings: There is only one shift
Xcode, so there is no way to set only one of two shift bits.  I don't
Xhave any programs that distinguish between the the two shift keys,
Xbut you may.  It can't be done with the DEC keyboard.  Another DEC
Xlimitation, DEC keyboards don't have a backspace key or an escape
Xkey in the main keypad.  I 'fixed' both these problems.
X
XDelete is the key where backspace was and shift-delete acts as
Xbackspace.  This works well when I use the ST as a terminal and with
Xemacs like editors.  Since some ST programs use backspace as delete
Xleft, I also put backspace and delete on the two extra keys in the
Xmiddle of the pad above the cursor keys. 
X
XDEC has this wierd < > key down between the z and the left shift, I
Xmoved ~` down there, and put esc up in the top left hand corner where
X~` was.  Note that < and > are shift-, and shift-.  respectively.
XActually DEC supports this change in their new 300 series of terminals.
XThey will even sell you a couple of new keytops to make it look
Xpretty. I just marked < and > on the ',' and '.' keycaps. 
X
XA few other keys have moved around a bit, but that is about it.  There
Xare 20 function keys, so I mapped the top ten to the shifted function
Xkey codes.  The keypad is identical, except for the notation on the top
Xfour keys ( Pf1 - Pf4 instead of ( ) / * ) and the kp+ key that says
X',' . There really is very little difference, and I find no problem,
Xother that feel, in switching between the two keyboards.  After typing
Xon the DEC keyboard, the ST feels very bad!  Now, on to the hardware
Xdetails.
X
XHardware:
X
XThe RJ11 ( standard modular telephone connector ) plug on the DEC
Xkeyboard has four wires.  They are color coded black, red, green, and
Xyellow.  The red wire supplies power ( +12 V.  ) to the keyboard.  It
Xshould be connected to the positive side of the power supply.  The
Xgreen wire is ground for both the power supply and the communications
Xlines.  It must be connnected to the negative side of the power supply.
XThe black wire is the RS-423 output from the keyboard.  The yellow wire
Xis the RS-423 input to the keyboard.  This interface does not use the
Xyellow wire.
X
XFor the serial port, I used an RJ11 to DB25 adapter that looks like an
Xoversized DB25 shell. Our computer center uses them for all our
Xterminals so that they can use RJ11 wall jacks.  I brought the power
Xwires in through a small hole that I melted in the shell, and did all
Xthe wiring inside.  There were originally four wires connected to pins
X2, 3, 7, and 20.  I cut all the wires and reconnected the ones for 3 (
Xblack wire from keyboard) and  7 ( green wire from keyboard and power
Xsupply negative lead ). Plug the keyboard into the RJ11 jack. 
X
XPlug the DB25 into the serial port, power the keyboard, and run
Xserialkb.prg.  Serialkb.prg installs itself as the serial interrupt
Xdriver the first time it is run.  It deinstalls itself when it is run
Xagain and restores the original serial port interrupt handler.  The
Xsmall amount of memory used ( < 1KB ) will not be released until you
Xreboot.  It will create a new version of itself every other time you
Xrun it.
X
XThe midi port (IN) connector is a five pin DIN male.  I actually used
Xthe end of a cable from a defuct IBM PC keyboard. Connect the black
Xwire from the keyboard to pin 4 and the green wire to pin 5.  Watch out
Xfor the wierd ordering, it isn't 1 2 3 4 5!  The keyboard power
Xconnections are the same as for the serial port ( positive voltage to
Xred wire, negative to green ).  Note that the midi interface is not
XRS-423 compatible, but that a RS-423 output should drive a midi input. 
XIt works well on my system, but 'your mileage may vary'.  Now lets go
Xthrough the hard part, changing the midi port's baud rate to 4800 baud.
X
XBefore starting this modification, reflect on the fact you will be
Xviolating any Atari warantee and that you could, with suitable
Xcarelessness or ignorance, trash your ST.  Don't try this, or any
Xother hardware modification, unless you know what you are doing and
Xare able to handle any damage you may cause.  I obviously can't help
Xyou if you damage your system.  I hereby disclaim any and all
Xresponsiblility for the correctness of the following instructions.
XYou should assume that they are designed to destroy your system 8^)!
XThese instructions are not a cookbook, they assume that you know
Xwhat you are doing.  If you don't, find somebody who does and get
Xthem to help you!
X
XThe midi port uses an ACIA.  In its stock form the clock input to the
XACIA is 500 KHz and the ACIA is programmed to divide that clock by 16
Xgiving a 31.25 K baud rate.  The keyboard ACIA uses the same clock, but
Xit is programmed to divide it by 64, giving 7.185 K baud. To get 4800
Xbaud we need 16 or 64 times 4800 Hz at the midi ACIA clock input.  The
Xsystem has 8, 4, 2, and .5 Mhz clock signal available.  
X
XA note of credit here, most of this information and the ROM listings
Xneeded to complete this project came from 'Atari ST Internals',
Xpublished by Abacus.  This is, by far, the most useful ST book I own. 
X
XA little playing around with a calculator shows 4MHz, divided by 13,
Xthen divided by 64, gives 4807.7 Hz.  Good enough!  There is a 4 MHz
Xclock input to the 68901 MFP chip which is located near the ACIAs.  Now
Xall we need is a ciruit to divide that 4 Mhz clock by thirteen.
X
XDividing a TTL level clock signal by 13 is easy to do with a 4 bit
Xbinary TTL counter chip with a parallel load function.  After a little
Xreading in my well worn 'TTL Cookbook' by Don Lancaster, and a few
Xminutes spent sorting through the chips I had available, I came up with
Xthe following circuit:
X
X                            ____________
X                    grnd --|1    7    16|-- + 5V.
X                         --|2    4    15|-- + 5V.
X                         --|3    L    14|----------- 4 Mhz. INPUT
X                    grnd --|4    S    13|------  ( pin 35, 68901 )
X                   + 5V. --|5    1    12|--   | <- connect 13 and 11
X                         --|6    9    11|------ 
X307.6923 KHz OUTPUT -------|7    1    10|-- + 5V.
X(pin 3, top 6850)   grnd --|8   TOP    9|-- + 5V.
X                            ------------
X
XI wired this circuit directly on the pins of the 74LS191 IC using
Xwire-wrap wire.  I soldered it after checking to make sure that it
Xdid indeed divide by 13.  At first it didn't because I had wired pin
X11 to pin 12, intead of to pin 13!  I always check and double check
Xevery step of my work.  Careful checking along the way is tedious,
Xbut it prevents much pain and suffering later.
X
X
XNow, for the work inside the ST.  Take it apart and remove the floppy
Xdrive ( this is for a 1040, I think the relevant chips are located in
Xthe same area on the 520, near the serial port connector, but I
Xhaven't made this modification to a 520.  Locate the MFP and the two
XACIAs.  On my 1040, the upper ( nearest the back ) ACIA is used for
Xthe midi port.  There is a small trace connecting pins 3 and 4 ( the
Xclock inputs ) of both ACIA chips.  Cut it somewhere between the two
XACIAs.  Be careful to completely sever that trace and not to damage
Xany other traces.
X
XTo mount the 74LS191, bend pins 1 and 16 up above the top of the chip
Xand solder them to the leads of a bypass capacitor located above and
Xbetween the ACIA and MFP chips.  This holds the chip in place ( upside
Xdown ) and provides power and ground connections.  It is easy to break
Xthe pins, so be gentle.  Then carefully connect the INPUT by soldering
Xwire wrap wire directly to pin 35 of the MFP.  This pin is located
Ximmediately below the Vss ( ground ) pin.  The ground is easy to
Xidentify because it is a thick trace. Finally, connect the OUTPUT to
Xpin 3 of the midi ACIA using wire wrap wire.
X
XCheck and double check the wiring and the mounting of the TTL IC. Make
Xsure that none of its leads can touch anything conductive, for example,
Xthe bottom shield on the disk drive!  Then put it all back together,
Xand boot it.  Be prepared to go back inside and fix any errors!  
X
XConnect the keyboard to the midi port, power the keyboard, run the
Xprogram midikb.prg, and type on your new keyboard!  Midikb installs
Xitself as the midi port interrupt handler.  It uses 1 K of memory.
XSince it does not interfere with the normal use of the system (as
Xfar as I have been able to determine 8*)), I always have it
Xinstalled, but only power up the keyboard when I am using it.  To
Xdeinstall, you must reboot the system.
X  
X
X	Mark Boyd, Computer Science
X	Univ. of North Carolina at Asheville
X	Asheville, NC 28804
X	mbunca@ecsvax.uncecs.edu
X	mark@ncsuvx.ncsu.edu
X
SHAR_EOF
chmod 0600 DECKEY.LIS || echo "restore of DECKEY.LIS fails"
sed 's/^X//' << 'SHAR_EOF' > MIDIKB.S &&
Xstart:
X         pea      install(pc)      ; superexec to set int vector
X         move.w   #38,-(a7)        ; and initialize midi ACIA
X         trap     #14
X         addq.l   #6,a7
X         clr.w    -(a7)
X         move.l   #$400,-(a7)      ; reserve space, keep process
X         move.w   #$31,-(a7)
X         trap     #1               ; done
Xinstall:
X         pea      handler(pc)
X         move.l   (a7)+,$118       ; pointer keyboard/midi intrpt
X         move.b   #3,$fffffc04     ; master reset midi acia
X         move.b   #$96,$fffffc04   ; set for /64, 8bit, 1 s, no parity
X         rts
Xhandler: 
X         movem.l  d0-d7/a0-a6,-(sp); save registers
X         lea      $0,a5            ; clear a5 ( base page pointer )
Xmore:    jsr      midisrv
X         move.l   $dec(a5),a2      ; get keyboard addr
X         jsr      (a2)             ; service keyboard
X         btst     #4,$fffffa01     ; still interrupt
X         beq.s    more             ; yes
X         bclr     #6,$fffffa11     ; clear int service bit
X         movem.l  (sp)+,d0-d7/a0-a6
X         rte
Xmidisrv: move.b   d0,-(sp)
X         move.l   $de8(a5),a2      ; get midi addr
X         jsr      (a2)             ; service midi
X         move.b   (sp)+,d3         ; see if we got something
X         cmp.b    d0,d3            ; kludge to check for input
X         bne.s    doit
X         rts
Xdoit:    andi.l   #$ff,d0
X         move.l   d0,d3
X         cmp.b    #$55,d0          ; ignore codes below $56
X         bls      done
X         cmp.b    #$8d,d0          ; prevent alt insert
X         bne.s    noinsr           ; to avoid crashing!
X         btst     #3,$e1b          ; is it alt?
X         beq      lookup           ; nope
X         bra      done             ; yes, toss it
Xnoinsr:  cmp.b    #$b4,d0          ; is it metronome?
X         bne.s    norept
X         move.l   oldkey,d0        ; repeat last key
X         bra      almost
Xnorept:  cmp.b    #$bf,d3          ; is it tilde - modify keyboard
X         bne.s    notilde
X         move.w   #$01,d0          ; make it esc
X         bra      almost
Xnotilde: cmp.b    #$c9,d3          ; is it <>
X         bne.s    nogtlt
X         move.w   #$29,d0          ; make it tilde
X         bra      almost
Xnogtlt:  cmp.b    #$bc,d3          ; is it backsp
X         bne.s    notdel
X         btst     #0,$e1b          ; is it shifted
X         bne.s    notdel
X         move.w   #$53,d0          ; make it del
X         bra      almost
Xnotdel:  cmp.b    #$ad,d3          ; handle shift codes
X         bmi      lookup
X         cmp.b    #$bb,d3
X         bpl      lookup           ; nope
X         move.b   $e1b,d2          ; get shift mask
X         cmp.b    #$af,d3          ; is it shift
X         bpl.s    arnd1
X         eori.b   #3,d2            ; toggle shift bits
X         bra.s    arnd5
Xarnd1:   cmp.b    #$b0,d3
X         bpl.s    arnd2
X         bchg     #2,d2            ; toggle ctrl bit
X         bra.s    arnd5
Xarnd2:   cmp.b    #$b1,d3
X         bpl.s    arnd3
X         bchg     #4,d2            ; toggle caps lock bit
X         bra.s    arnd5
Xarnd3:   cmp.b    #$b2,d3
X         bpl.s    arnd4
X         bset     #3,d2            ; set alt bit
X         bra.s    arnd5
Xarnd4:   cmp.b    #$b4,d3          ; all ups
X         bpl.s    arnd5
X         andi.b   #$f0,d2          ; clear shift,ctrl, and alt bits
Xarnd5:   move.b   d2,$e1b          ; save new shift status
X         bra.s    done
Xlookup:  lea      table(pc),a0     ; convert dec to st scan code
X         sub.w    #$56,d3
X         and.l    #$ff,d3
X         asl.l    #1,d3
X         adda.l   d3,a0
X         move.w   (a0),d0
Xalmost:  move.l   #$db0,a0         ; point to kbd iorec addr
X         lea      $0,a5
X         move.l   d0,oldkey
X         jsr      $fc2aa6          ; get scan code in buffer
X         bclr     #3,$e1b          ; clear alt shift
Xdone:    rts
Xtable:   dc.w     $3b,$3c,$3d,$3e,$3f,0,0,0,0,0,0,0,0,0 ; function keys
X         dc.w     $40,$41,$42,$43,$44,0,0,0,0,0,0,0,0
X         dc.w     $54,$55,$56,$57,0,0,0,0,0,0,0   ; shifted funct keys
X         dc.w     $58,$59,0,0,$5a,$5b,$5c,$5d,0,0,0,0,0,0
X         dc.w     $62,$0e,$61,$52,$0e,$47,0,0     ; keys abv cursor pad
X         dc.w     $70,0,$71,$72,$6d,$6e,$6f,$6a   ; numeric keypad
X         dc.w     $6b,$6c,$4e,$67,$68,$69,$4a
X         dc.w     $63,$64,$65,$66,0,0             ; pf keys
X         dc.w     $4b,$4d,$50,$48                 ; cursor keys
X         dc.w     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
X         dc.w     $0e,$1c,$0f,$29,$02,$10,$1e,$2c,0 ; main keyboard
X         dc.w     $03,$11,$1f,$2d,0,0,$04,$12,$20,$2e
X         dc.w     0,$05,$13,$21,$2f,$39,0,$06,$14,$22,$30
X         dc.w     0,$07,$15,$23,$31,0,$08,$16,$24,$32,0
X         dc.w     $09,$17,$25,$33,0,$0a,$18,$26,$34,0
X         dc.w     $0b,$19,0,$27,$35,0,$0d,$1b,$2b,0
X         dc.w     $0c,$1a,$28,0,0,0
Xoldkey:  dc.l     0
X         end
X
SHAR_EOF
chmod 0600 MIDIKB.S || echo "restore of MIDIKB.S fails"
sed 's/^X//' << 'SHAR_EOF' > SERIALKB.S &&
Xstart:
X         pea      install(pc)      ; superexec to set int vector
X         move.w   #38,-(a7)        ; and check for prior installation
X         trap     #14
X         addq.l   #6,a7
X         cmp.b    #1,d0
X         bne.s    bye
X         move.w   #-1,-(sp)        ; setup rs232 port
X         move.w   #-1,-(sp)
X         move.w   #-1,-(sp)
X         move.w   #-1,-(sp)
X         move.w   #0,-(sp)         ; no handshake
X         move.w   #2,-(sp)         ; 4800 baud
X         move.w   #15,-(sp)
X         trap     #14
X         add.l    #14,sp
Xbye:     clr.w    -(a7)
X         move.l   #$400,-(a7)     ; reserve space, keep process
X         move.w   #$31,-(a7)
X         trap     #1
Xinstall:
X         move.l   $130,a0         ; get the serial port int vector
X         movea.l  -4(a0),a1       ; check to see if serialkb installed
X         cmp.l    #$6b796264,a1
X         bne.s    hookit          ; no, install it
X         move.l   -8(a0),$130     ; yes, restore original vector
X         move.b   #0,d0
X         rts
Xhookit:  move.l   a0,oldvec       ; save the original vector
X         pea      handler(pc)     ; and set the new vector
X         move.l   (a7)+,$130      ; pointer to rs232 in buf full intrpt
X         move.b   #1,d0
X         rts
Xoldvec:  dc.l     0
Xmagic:   dc.b     'kybd'
Xhandler: 
X         movem.l  d0-d3/a0-a5,-(sp); save registers
X         lea      $fffffa01,a1     ; pointer to mfp
X         btst     #7,42(a1)        ; is it receive int
X         beq      done
X         clr.l    d0
X         move.b   46(a1),d0        ; get data
X         move.l   d0,d3
X         cmp.b    #$55,d0          ; ignore codes below $56
X         bls      done
X         cmp.b    #$8d,d0          ; prevent alt insert
X         bne.s    noinsr           ; to avoid crashing!
X         btst     #3,$e1b          ; is it alt?
X         beq      lookup           ; nope
X         bra      done             ; yes, toss it
Xnoinsr:  cmp.b    #$b4,d0          ; is it metronome?
X         bne.s    norept
X         move.l   oldkey,d0        ; repeat last key
X         bra      almost
Xnorept:  cmp.b    #$bf,d3          ; is it tilde - modify keyboard
X         bne.s    notilde
X         move.w   #$01,d0          ; make it esc
X         bra      almost
Xnotilde: cmp.b    #$c9,d3          ; is it <>
X         bne.s    nogtlt
X         move.w   #$29,d0          ; make it tilde
X         bra      almost
Xnogtlt:  cmp.b    #$bc,d3          ; is it backsp
X         bne.s    notdel
X         btst     #0,$e1b          ; is it shifted
X         bne.s    notdel
X         move.w   #$53,d0          ; make it del
X         bra      almost
Xnotdel:  cmp.b    #$ad,d3          ; handle shift codes
X         bmi      lookup
X         cmp.b    #$bb,d3
X         bpl      lookup           ; nope
X         move.b   $e1b,d2          ; get shift mask
X         cmp.b    #$af,d3          ; is it shift
X         bpl.s    arnd1
X         eori.b   #3,d2            ; toggle shift bits
X         bra.s    arnd5
Xarnd1:   cmp.b    #$b0,d3
X         bpl.s    arnd2
X         bchg     #2,d2            ; toggle ctrl bit
X         bra.s    arnd5
Xarnd2:   cmp.b    #$b1,d3
X         bpl.s    arnd3
X         bchg     #4,d2            ; toggle caps lock bit
X         bra.s    arnd5
Xarnd3:   cmp.b    #$b2,d3
X         bpl.s    arnd4
X         bset     #3,d2            ; set alt bit
X         bra.s    arnd5
Xarnd4:   cmp.b    #$b4,d3          ; all ups
X         bpl.s    arnd5
X         andi.b   #$f0,d2          ; clear shift,ctrl, and alt bits
Xarnd5:   move.b   d2,$e1b          ; save new shift status
X         bra.s    done
Xlookup:  lea      table(pc),a0     ; convert dec to st scan code
X         sub.w    #$56,d3
X         and.l    #$ff,d3
X         asl.l    #1,d3
X         adda.l   d3,a0
X         move.w   (a0),d0
Xalmost:  move.l   #$db0,a0         ; point to kbd iorec addr
X         lea      $0,a5
X         move.l   d0,oldkey
X         jsr      $fc2aa6          ; get scan code in buffer
X         bclr     #3,$e1b          ; clear alt shift
Xdone:    bclr     #4,14(a1)        ; clear int service bit
X         movem.l  (sp)+,d0-d3/a0-a5; restore registers
X         rte
Xtable:   dc.w     $3b,$3c,$3d,$3e,$3f,0,0,0,0,0,0,0,0,0 ; function keys
X         dc.w     $40,$41,$42,$43,$44,0,0,0,0,0,0,0,0
X         dc.w     $54,$55,$56,$57,0,0,0,0,0,0,0   ; shifted funct keys
X         dc.w     $58,$59,0,0,$5a,$5b,$5c,$5d,0,0,0,0,0,0
X         dc.w     $62,$0e,$61,$52,$53,$47,0,0     ; keys abv cursor pad
X         dc.w     $70,0,$71,$72,$6d,$6e,$6f,$6a   ; numeric keypad
X         dc.w     $6b,$6c,$4e,$67,$68,$69,$4a
X         dc.w     $63,$64,$65,$66,0,0             ; pf keys
X         dc.w     $4b,$4d,$50,$48                 ; cursor keys
X         dc.w     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
X         dc.w     $0e,$1c,$0f,$29,$02,$10,$1e,$2c,0 ; main keyboard
X         dc.w     $03,$11,$1f,$2d,0,0,$04,$12,$20,$2e
X         dc.w     0,$05,$13,$21,$2f,$39,0,$06,$14,$22,$30
X         dc.w     0,$07,$15,$23,$31,0,$08,$16,$24,$32,0
X         dc.w     $09,$17,$25,$33,0,$0a,$18,$26,$34,0
X         dc.w     $0b,$19,0,$27,$35,0,$0d,$1b,$2b,0
X         dc.w     $0c,$1a,$28,0,0,0
Xoldkey:  dc.l     0
X         end
X
SHAR_EOF
chmod 0600 SERIALKB.S || echo "restore of SERIALKB.S fails"
exit 0