00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include <map>
00027 #include <sstream>
00028 #include <stdlib.h>
00029
00030 #include "../avrerror.h"
00031 #include "ui.h"
00032 using namespace std;
00033
00034 map<int,int> xToNumber;
00035 map<int, const int* > keynumberToScancode1;
00036 map<int, const int* > keynumberToScancode2;
00037 map<int, const int* > keynumberToScancode3;
00038
00039
00040
00041 #include "keyboard.h"
00042
00043 int Keyboard::Step(bool &trueHwStep, SystemClockOffset *timeToNextStepIn_ns) {
00044
00045
00046
00047
00048 static enum {
00049 IDLE,
00050 WRITE_CHANGE_DATA,
00051 WRITE_CHANGE_CLOCK_LOW,
00052 WRITE_CHANGE_CLOCK_HIGH,
00053 READ
00054 } myState = IDLE;
00055
00056
00057
00058 switch (myState ) {
00059 case IDLE:
00060 {
00061 if (bufferWriteIndex!=bufferReadIndex) {
00062 myState=WRITE_CHANGE_DATA;
00063 actualChar= buffer[bufferReadIndex];
00064 bufferReadIndex=(bufferReadIndex+1)&(KBD_BUFFER_SIZE-1);
00065 }
00066 }
00067 break;
00068
00069 case WRITE_CHANGE_DATA:
00070 {
00071 bool d;
00072 static bool parity;
00073
00074
00075 if (bitCnt==0) {
00076 d=0;
00077 parity=0;
00078 } else if ((bitCnt >0 ) && ( bitCnt <=8 )) {
00079 d= (((actualChar)>>(bitCnt-1))) & 0x01;
00080 } else if (bitCnt == 9 ) {
00081 d= parity;
00082 } else {
00083 d= 1;
00084 }
00085
00086 if (d!=0) {
00087 data= 'H';
00088 } else {
00089 data='L';
00090 }
00091
00092 parity^=d;
00093
00094 *timeToNextStepIn_ns=10000;
00095
00096 bitCnt++;
00097
00098 if(bitCnt>11) {
00099 bitCnt=0;
00100 *timeToNextStepIn_ns=50000;
00101 myState= IDLE;
00102 } else {
00103 *timeToNextStepIn_ns=10000;
00104 myState=WRITE_CHANGE_CLOCK_LOW;
00105 }
00106 }
00107 break;
00108
00109
00110 case WRITE_CHANGE_CLOCK_LOW:
00111 clk='L';
00112 *timeToNextStepIn_ns=30000;
00113 myState=WRITE_CHANGE_CLOCK_HIGH;
00114 break;
00115
00116 case WRITE_CHANGE_CLOCK_HIGH:
00117 clk='H';
00118 *timeToNextStepIn_ns=40000;
00119 myState=WRITE_CHANGE_DATA;
00120 break;
00121
00122
00123
00124 case READ:
00125 {
00126
00127 }
00128
00129 break;
00130
00131 default:
00132 avr_error("Default state in kbd ????????????????????");
00133 }
00134
00135 if (lastPortValue!= myPortValue) {
00136 lastPortValue=myPortValue;
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178 }
00179 if(timeToNextStepIn_ns!=0) *timeToNextStepIn_ns=myClockFreq;
00180 return 0;
00181 }
00182 Keyboard::Keyboard(UserInterface *ui, const char *name, const char *baseWindow):
00183 clk( &myPortValue, 1),
00184 data( &myPortValue, 2),
00185 bufferWriteIndex(0), bufferReadIndex(0)
00186 {
00187 myPortValue=0;
00188 bitCnt=0;
00189
00190
00191 ostringstream os;
00192 os << "create Kbd " << name << " .x " << endl;
00193 ui->Write(os.str());
00194
00195 ui->AddExternalType(name, this);
00196 lastPortValue=0;
00197 #include "keytrans.h"
00198 }
00199
00200 Keyboard::~Keyboard() { }
00201
00202 void Keyboard::SetClockFreq(SystemClockOffset f){
00203 myClockFreq=f;
00204 }
00205
00206 int Keyboard::InsertScanCodeToBuffer( unsigned char scan) {
00207 if (((bufferWriteIndex+1)&(KBD_BUFFER_SIZE-1)) == bufferReadIndex) return -1;
00208 buffer[bufferWriteIndex]= scan;
00209 bufferWriteIndex=(bufferWriteIndex+1)&(KBD_BUFFER_SIZE-1);
00210
00211 return 0;
00212 }
00213
00214 void Keyboard::InsertMakeCodeToBuffer( int key) {
00215 int keynum= xToNumber[key];
00216 if (keynum==0) return;
00217
00218
00219
00220 const int *scan= keynumberToScancode2[keynum];
00221 do {
00222 if (*scan==0xffff) return ;
00223 if ( InsertScanCodeToBuffer((unsigned char)(*scan)) < 0) return ;
00224 scan++;
00225
00226 } while (1);
00227 }
00228
00229 void Keyboard::InsertBreakCodeToBuffer( int key) {
00230 int sendF0Pos=0;
00231
00232 int keynum= xToNumber[key];
00233 if (keynum==0) return;
00234
00235 const int *scan= keynumberToScancode2[keynum];
00236 if (*scan==0x00e0) {
00237 sendF0Pos=1;
00238 }
00239
00240 do {
00241 if (*scan==0xffff) return ;
00242 if (sendF0Pos==0) {
00243 if ( InsertScanCodeToBuffer((unsigned char)(0xf0)) < 0) return ;
00244 sendF0Pos--;
00245 } else {
00246 if ( InsertScanCodeToBuffer((unsigned char)(*scan)) < 0) return ;
00247 scan++;
00248 sendF0Pos--;
00249 }
00250
00251 } while (1);
00252 }
00253
00254 void Keyboard::SetNewValueFromUi(const string& s) {
00255 switch (s[0]) {
00256 case 'B':
00257 InsertBreakCodeToBuffer(atoi(s.substr(1).c_str()));
00258 break;
00259
00260 case 'M':
00261 InsertMakeCodeToBuffer(atoi(s.substr(1).c_str()));
00262 break;
00263
00264 default:
00265 cerr << "Unknown message for kbd-handler received from gui :-(" << endl;
00266 }
00267 }
00268