/*
KBRDEMDV.c
Copyright (C) 2006 Philip Cummins, Paul C. Pratt
You can redistribute this file and/or modify it under the terms
of version 2 of the GNU General Public License as published by
the Free Software Foundation. You should have received a copy
of the license along with this file; see the file COPYING.
This file 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
license for more details.
*//*
KeyBoaRD EMulated DeVice
Emulation of the keyboard in the Mac Plus.
This code adapted from "Keyboard.c" in vMac by Philip Cummins.
*/#ifndef AllFiles#include"SYSDEPNS.h"#include"MYOSGLUE.h"#include"EMCONFIG.h"#include"GLOBGLUE.h"#endif#include"KBRDEMDV.h"#ifdef _VIA_Debug#include<stdio.h>#endif/*
ReportAbnormalID unused 0x0B03 - 0x0BFF
*/IMPORTPROCKYBD_ShiftOutData(ui3bv);IMPORTFUNCui3bKYBD_ShiftInData(void);enum{kKybdStateIdle,kKybdStateRecievingCommand,kKybdStateRecievedCommand,kKybdStateRecievingEndCommand,kKybdStates};LOCALVARintKybdState=kKybdStateIdle;LOCALVARblnrHaveKeyBoardResult=falseblnr;LOCALVARui3bKeyBoardResult;LOCALPROCGotKeyBoardData(ui3bv){if(KybdState!=kKybdStateIdle){HaveKeyBoardResult=trueblnr;KeyBoardResult=v;}else{KYBD_ShiftOutData(v);VIA1_iCB2=1;}}LOCALVARui3bInstantCommandData=0x7B;LOCALFUNCblnrAttemptToFinishInquiry(void){inti;blnrKeyDown;ui3bKeyboard_Data;if(FindKeyEvent(&i,&KeyDown)){if(i<64){Keyboard_Data=i<<1;if(!KeyDown){Keyboard_Data+=128;}}else{Keyboard_Data=121;InstantCommandData=(i-64)<<1;if(!KeyDown){InstantCommandData+=128;}}GotKeyBoardData(Keyboard_Data);returntrueblnr;}else{returnfalseblnr;}}#define MaxKeyboardWait 16 /* in 60ths of a second *//*
Code in the mac rom will reset the keyboard if
it hasn't been heard from in 32/60th of a second.
So time out and send something before that
to keep connection.
*/LOCALVARintInquiryCommandTimer=0;GLOBALPROCDoKybd_ReceiveCommand(void){if(KybdState!=kKybdStateRecievingCommand){ReportAbnormalID(0x0B01,"KybdState != kKybdStateRecievingCommand");}else{ui3bin=KYBD_ShiftInData();KybdState=kKybdStateRecievedCommand;switch(in){case0x10:/* Inquiry Command */if(!AttemptToFinishInquiry()){InquiryCommandTimer=MaxKeyboardWait;}break;case0x14:/* Instant Command */GotKeyBoardData(InstantCommandData);InstantCommandData=0x7B;break;case0x16:/* Model Command */GotKeyBoardData(0x0b/* 0x01 */);/* Test value, means Model 0, no extra devices *//*
Fixed by Hoshi Takanori -
it uses the proper keyboard type now
*/break;case0x36:/* Test Command */GotKeyBoardData(0x7D);break;case0x00:GotKeyBoardData(0);break;default:/* Debugger(); */GotKeyBoardData(0);break;}}}GLOBALPROCDoKybd_ReceiveEndCommand(void){if(KybdState!=kKybdStateRecievingEndCommand){ReportAbnormalID(0x0B02,"KybdState != kKybdStateRecievingEndCommand");}else{KybdState=kKybdStateIdle;#ifdef _VIA_Debugfprintf(stderr,"enter DoKybd_ReceiveEndCommand\n");#endifif(HaveKeyBoardResult){#ifdef _VIA_Debugfprintf(stderr,"HaveKeyBoardResult: %d\n",KeyBoardResult);#endifHaveKeyBoardResult=falseblnr;KYBD_ShiftOutData(KeyBoardResult);VIA1_iCB2=1;}}}GLOBALPROCKybd_DataLineChngNtfy(void){switch(KybdState){casekKybdStateIdle:if(VIA1_iCB2==0){KybdState=kKybdStateRecievingCommand;#ifdef _VIA_Debugfprintf(stderr,"posting kICT_Kybd_ReceiveCommand\n");#endifICT_add(kICT_Kybd_ReceiveCommand,6800UL*kCycleScale/64*kMyClockMult);if(InquiryCommandTimer!=0){InquiryCommandTimer=0;/* abort Inquiry */}}break;casekKybdStateReciev