-
Notifications
You must be signed in to change notification settings - Fork 0
/
kb.c
1059 lines (939 loc) · 23.4 KB
/
kb.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// (C) 2007 Mike Brent aka Tursi aka HarmlessLion.com
// This software is provided AS-IS. No warranty
// express or implied is provided.
//
// This notice defines the entire license for this software.
// All rights not explicity granted here are reserved by the
// author.
//
// You may redistribute this software provided the original
// archive is UNCHANGED and a link back to my web page,
// http://harmlesslion.com, is provided as the author's site.
// It is acceptable to link directly to a subpage at harmlesslion.com
// provided that page offers a URL for that purpose
//
// Source code, if available, is provided for educational purposes
// only. You are welcome to read it, learn from it, mock
// it, and hack it up - for your own use only.
//
// Please contact me before distributing derived works or
// ports so that we may work out terms. I don't mind people
// using my code but it's been outright stolen before. In all
// cases the code must maintain credit to the original author(s).
//
// -COMMERCIAL USE- Contact me first. I didn't make
// any money off it - why should you? ;) If you just learned
// something from this, then go ahead. If you just pinched
// a routine or two, let me know, I'll probably just ask
// for credit. If you want to derive a commercial tool
// or use large portions, we need to talk. ;)
//
// Commercial use means ANY distribution for payment, whether or
// not for profit.
//
// If this, itself, is a derived work from someone else's code,
// then their original copyrights and licenses are left intact
// and in full force.
//
// http://harmlesslion.com - visit the web page for contact info
//
#include <avr/io.h>
#include <avr/interrupt.h>
#include <string.h>
#include "gpr.h"
#include "kb.h"
#include "scancodes.h"
#include "serial.h"
#include "ti.h"
#define BUFF_SIZE 64
//#define DEBUG_KB_ERRS
unsigned char edge, bitcount;// 0 = neg. 1 = pos.
unsigned char kb_buffer[BUFF_SIZE];
unsigned char *inpt, *outpt;
unsigned char buffcnt;
// used to count down between bits
volatile int timeout;
#define TIMEOUTCOUNT 0xffff
// Note: this means we can never resend the Diagnostic echo command
#define DUMMY_OUT 0xEE
// keyboard commands out
volatile unsigned char outcmd;
volatile unsigned char outparity;
volatile signed char outcount;
volatile unsigned char lastout=DUMMY_OUT;
volatile unsigned char lastpar;
volatile unsigned char outstarted;
volatile unsigned char ack_cmd, ack_parity, ack_valid;
volatile unsigned char kbtestok=0;
// keyboard state (note commented zeros indicate vars that are assumed initialized to zero!)
unsigned char is_up /*=0*/, isextended /*= 0*/, shiftstate/*=0*/, capslock=1, lockedshiftstate/*=0*/;
unsigned char scrolllock/*=0*/, numlock=1;
unsigned char fctnrefcount,shiftrefcount,ctrlrefcount;
unsigned char ignorecount; // used to ignore the break key
unsigned char ticols[8]={ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
unsigned char cheatcode[10] /*={0,0,0,0,0,0,0,0,0,0};*/;
// cheat code is up,up,down,down,left,right,left,right,b,a (trigger with enter)
// because we don't save the meta information, it's also 88224646ba (digits on numpad)
unsigned const char cheatmatch[10]={ 0x75,0x75,0x72,0x72,0x6B,0x74,0x6B,0x74,0x32,0x1c };
volatile const unsigned char *pCheat/*=0*/;
volatile int abortCheat/*=0*/;
int cheatidx/*=0*/; // cheat code buffer is a ring buffer to reduce overhead
// Track whether the last key required a fctn, shift, or control press
unsigned char fLastMeta/*=0*/;
unsigned char nLastRow=-1, nLastCol=-1;
unsigned char outputclear/*=0*/; // used for controlling external pins
enum LASTMETA {
METANONE = 0,
METAFCTN,
METACTRL,
METASHIFT
};
#define SHIFT_COL 7
#define FCTN_ROW 4
#define SHIFT_ROW 5
#define CTRL_ROW 6
void init_kb(void)
{
inpt = kb_buffer; // Initialize buffer
outpt = kb_buffer;
buffcnt = 0;
outcount = -1;
ack_valid=0; // no acknowledge byte pending
fctnrefcount=0;
shiftrefcount=0;
ctrlrefcount=0;
ignorecount=0;
DATA_HIGH; // high is idle
CLOCK_NORMAL; // activates input interrupts
}
void resetstate() {
// called after no activity from the kb for a while,
// we just reset our counters to correct any potential misalignments
// else, let's reset our state machine
// but don't reset the TI's state machine
if (outstarted) {
// abort our command
CLOCK_LOW;
}
putserialchar('*');
inpt = kb_buffer; // Initialize buffer
outpt = kb_buffer;
buffcnt = 0;
outcount = -1;
ignorecount=0;
// ack_valid=0; // if that ack ever comes in, we will need something to feed the keyboard
bitcount = 11;
if (outstarted) {
delay(1);
CLOCK_NORMAL;
outstarted=0;
}
}
void StartCmd() {
// To send a command, we start by pulling the data line low, then we just send data
// with the clocks. To prevent racing with the keyboard, it's correct to pull the clock
// line low for 60uS or more first, and release the clock after lowering data
volatile char k,j;
j=0;
if (outstarted) {
return;
}
// This does the clock reset (I think)
CLOCK_LOW; // inhibit clock
for (j=0; j<5; j++) {
for (k=0; k<90; k++);
}
DATA_LOW; // start bit ready
for (j=0; j<5; j++) {
for (k=0; k<90; k++);
}
outstarted=1;
lastout=outcmd;
lastpar=outparity;
PORTB=0xdf;
CLOCK_NORMAL; // allow clock again
}
// returns 0 if the previous command is still sending
int send_cmd(unsigned char cmd, unsigned char par)
{
#ifdef DEBUGMODE
print_hexbyte(cmd);
#endif
outcmd=cmd;
outparity=par;
outstarted=0;
outcount=10;
StartCmd();
return 1;
}
//SIGNAL(SIG_INTERRUPT0)
ISR(INT0_vect)
{
static unsigned char data; // Holds the received scan code or output data
flipled(6);
timeout=TIMEOUTCOUNT; // we got a bit, make sure we wait for another
if (outstarted) {
// we're sending data!
if (edge) // Routine entered at rising edge
{
MCUCR = 2; // Set next interrupt on falling edge
edge = 0;
} else { // Routine entered at falling edge
MCUCR = 3; // Set next interrupt on rising edge
edge = 1;
if (outcount >= 0) {
// load our data bit
if (outcount > 2) {
flipled(0);
if (outcmd&0x01) {
DATA_HIGH;
} else {
DATA_LOW;
}
outcmd>>=1;
} else {
if (outcount == 2) {
PORTB&=0xfd;
// parity
if (outparity) {
DATA_HIGH;
} else {
DATA_LOW;
}
} else {
if (outcount == 1) {
// stop bit
DATA_HIGH;
} else {
if (outcount == 0) {
// otherwise, we're waiting for an ack
if (0 == (PIND & 8)) {
outstarted=0;
bitcount=11;
PORTB|=0x20;
} else {
outcount++;
}
}
}
}
}
outcount--;
}
}
} else {
// we're receiving data!
if (!edge) // Routine entered at falling edge
{
if(bitcount < 11 && bitcount > 2) // Bit 3 to 10 is data. Parity bit,
{ // start and stop bits are ignored.
data = (data >> 1);
if (PIND & 8) {
data = data | 0x80; // Store a '1'
}
}
bitcount--;
MCUCR = 3; // Set next interrupt on rising edge
edge = 1;
} else { // Routine entered at rising edge
if(bitcount == 0) // All bits received
{
// if the cheat code was running, we need to finish it off at the end of the current
// line - we can't touch the state till it's done
if (pCheat) {
abortCheat=1;
} else {
decode(data);
}
bitcount = 11;
// Check for pending output data
if (outcount>0) {
StartCmd();
}
}
MCUCR = 2; // Set next interrupt on falling edge
edge = 0;
}
}
}
unsigned char getextbit(unsigned char sc) {
unsigned char bit=0x00;
CheckTIPolling();
switch (sc) {
case 0x01: // F9
bit=0x10;
putserialchar('%');
putserialchar('4');
break;
case 0x09: // F10
bit=0x20;
putserialchar('%');
putserialchar('5');
break;
case 0x78: // F11
bit=0x40;
putserialchar('%');
putserialchar('6');
break;
case 0x07: // F12
bit=0x80;
putserialchar('%');
putserialchar('7');
break;
}
return bit;
}
void handleexton(unsigned char sc) {
// set an external pin (low) based on scancode passed (F9-F12)
unsigned char bit;
bit=getextbit(sc);
if (0 != bit) {
// set bit to low output and set clear code
PORTD&=~bit;
DDRD|=bit;
outputclear|=bit;
}
}
void handleextoff(unsigned char sc) {
// clear an external pin (tristate) based on scancode passed (F9-F12)
unsigned char bit;
bit=getextbit(sc);
if (0 != bit) {
// set bit to input
DDRD&=~bit;
PORTD|=bit;
outputclear&=~bit;
putserialchar('%');
}
}
void ParseRowCol(signed char row, signed char col, unsigned char fup) {
if ((-1 == row) || (-1 == col)) {
return;
}
if (col == SHIFT_COL) {
if (row == FCTN_ROW) {
// FCTN
if (fup) {
if (fctnrefcount) fctnrefcount--;
} else {
fctnrefcount++;
}
if (fctnrefcount) {
ticols[col]&=(unsigned char)~(1<<row);
} else {
ticols[col]|=(unsigned char)(1<<row);
}
return;
}
if (row == SHIFT_ROW) {
// Shift
if (fup) {
if (shiftrefcount) shiftrefcount--;
} else {
shiftrefcount++;
}
if (shiftrefcount) {
ticols[col]&=(unsigned char)~(1<<row);
} else {
ticols[col]|=(unsigned char)(1<<row);
}
return;
}
if (row == CTRL_ROW) {
// Ctrl
if (fup) {
if (ctrlrefcount) ctrlrefcount--;
} else {
ctrlrefcount++;
}
if (ctrlrefcount) {
ticols[col]&=(unsigned char)~(1<<row);
} else {
ticols[col]|=(unsigned char)(1<<row);
}
return;
}
}
if (!fup) {
ticols[col]&=(unsigned char)~(1<<row);
} else {
ticols[col]|=(unsigned char)(1<<row);
}
}
// new decode for TI
void decode(unsigned char sc)
{
static unsigned short nLastChar=0x00ff; // used to detect autorepeat and ignore it
const signed char *pDat;
#ifdef DEBUGMODE
print_hexbyte(sc);
#endif
// block ALT-= unless it's ALT-CTRL-=
if (sc == 0x55) { // '='
if ((ctrlrefcount <= 0) && (fctnrefcount > 0)) {
// return if FCTN is pressed but not CTRL
return;
}
}
if (ignorecount > 0) {
if (sc != 0xf0) {
ignorecount--;
}
return;
}
// else parse the key
switch (sc) {
case 0xFA: // keyboard acknowledge
if (ack_valid != 0) {
send_cmd(ack_cmd,ack_parity);
}
ack_valid=0;
ack_parity=0;
// putserialchar('A');
// putserialchar('C');
// putserialchar('K');
break;
case 0xAA: // keyboard self test passed
kbtestok=1;
fctnrefcount=0;
shiftrefcount=0;
ctrlrefcount=0;
#ifdef DEBUG_KB_ERRS
old=PORTB;
PORTB=0x00;
delay(25);
PORTB=old;
delay(50);
// putserialchar('O');
// putserialchar('K');
#endif
break;
case 0xEE: // echo response
break;
case 0xFE: // resend command
if (lastout != DUMMY_OUT) {
send_cmd(lastout, lastpar);
}
#ifdef DEBUG_KB_ERRS
old=PORTB;
PORTB=0x00;
delay(25);
PORTB=0xff;
delay(25);
PORTB=0x00;
delay(25);
PORTB=old;
delay(50);
// putserialchar('R');
// putserialchar('e');
// putserialchar('s');
#endif
break;
case 0xfc: // keyboard error - keyboard is disabled, so we need to reset it
kbtestok=0;
send_cmd(KEY_RESET);
break;
case 0x00:
case 0xff: // both of these are errors or buffer overflows
// TODO: we should take some action here
#ifdef DEBUG_KB_ERRS
old=PORTB;
PORTB=0x00;
delay(25);
PORTB=0xff;
delay(25);
PORTB=0x00;
delay(25);
PORTB=0xff;
delay(25);
PORTB=0x00;
delay(25);
PORTB=old;
delay(50);
// putserialchar('E');
// putserialchar('r');
// putserialchar('r');
#endif
break;
// special keys
case 0xF0: // key up
is_up=1;
break;
// these cases mean that just pressing SHIFT can not be detected on the TI
// this is a minor problem for the sake of bypassing ramdisk startup
// The reason is that a shifted key on the PC may not be shifted on the TI,
// so we don't want to activate it. The left and right Windows keys have been remapped to
// shift for that case, assuming the keyboard boots faster than the ramdisk ;)
case 0x12: // left SHIFT
case 0x59: // right SHIFT
if (!isextended) {
if (is_up) {
shiftstate=0;
} else {
shiftstate=1;
}
}
isextended=0;
is_up=0;
break;
case 0xe0: // extended code follows
isextended=1;
break;
case 0xe1: // break sequence - ignore next two keys
ignorecount=2;
break;
case 0x58: // caps lock
if (!isextended) {
if (is_up) {
is_up=0;
} else {
if (!ack_valid) {
// don't tromp on top of another change
capslock=!capslock;
UpdateKbLEDs();
}
}
}
isextended=0;
break;
case 0x7e: // scroll lock
if (isextended) {
// this is control-break, then, parse normally
goto dodefault;
}
if (is_up) {
is_up=0;
} else {
if (!ack_valid) {
scrolllock=!scrolllock;
UpdateKbLEDs();
}
}
break;
case 0x77: // num lock
if (!isextended) {
if (is_up) {
is_up=0;
} else {
if (!ack_valid) {
numlock=!numlock;
UpdateKbLEDs();
}
}
}
isextended=0;
break;
#ifdef DEBUGMODE
case 0x76: // ESC override (note: code may crash if you hit this too fast!)
if (!is_up) {
int idx;
putserialchar('\r');
putserialchar('\n');
putserialchar('S');
print_hexbyte(shiftrefcount);
putserialchar('\r');
putserialchar('\n');
putserialchar('C');
print_hexbyte(ctrlrefcount);
putserialchar('\r');
putserialchar('\n');
putserialchar('F');
print_hexbyte(fctnrefcount);
putserialchar('\r');
putserialchar('\n');
// and print the TI CRU map
for (idx=0; idx<8; idx++) {
print_hexbyte(ticols[idx]);
putserialchar('\r');
putserialchar('\n');
}
}
break;
#endif
// Check for Ctrl+Alt+Delete (reset the keyboard chip itself!)
case 0x71:
if (isextended) {
// Delete key
if (is_up) {
// activate on release (note this uses the TI versions!)
if ((ctrlrefcount > 0) && (fctnrefcount > 0)) {
// Perform a reset
void (*reset)()=NULL;
cli();
putserialchar('X');
putserialchar('X');
putserialchar('X');
reset();
// never returns
}
}
}
// if we make it here, then process normally
goto dodefault;
break;
case 0x83:
sc=0x7f; // special case for F7, remap to less than 0x80
goto dodefault;
// check for magic hardware sequence Alt+F9 through Alt+F12
// Must stay right above the default case so we can fall through!
case 0x01: // F9
case 0x09: // F10
case 0x78: // F11
case 0x07: // F12
if (is_up) {
if (outputclear) {
handleextoff(sc);
// clear the otherwise missed up handling
lockedshiftstate=0;
nLastChar=0x00ff; // clear last char - now that we've released we can press again ;)
isextended=0;
is_up=0;
break;
}
} else {
if (fctnrefcount > 0) {
// this is an external key request
handleexton(sc);
// clear potential flag that we'd otherwise miss
isextended=0;
break;
}
}
// else, fall through to default and handle it
default: // any other key
dodefault:
// certain keys are remapped for numlock and scroll lock
if (!numlock) {
sc=remapnumlock(sc);
}
if (scrolllock) {
sc=remapscrolllock(sc);
}
CheckTIPolling();
if (sc < 0x80) {
unsigned short nThisChar;
if (isextended) {
pDat=scan2ti994aextend[sc];
} else if ((shiftstate)||(lockedshiftstate)) {
pDat=scan2ti994ashift[sc];
lockedshiftstate=1;
} else {
pDat=scan2ti994aflat[sc];
}
// check for and ignore repeated characters
// to avoid screwing up the meta key refcounts
if (isextended) {
nThisChar=0xe000|sc;
} else {
nThisChar=sc;
}
// Up codes don't autorepeat, so don't check them
if ((is_up)||(nThisChar != nLastChar)) {
signed char row1,col1=-1;
signed char row2,col2=-1;
nLastChar=nThisChar;
row1=pgm_read_byte_near(pDat);
if (-1 != row1) {
col1=pgm_read_byte_near(pDat+1);
}
pDat+=2;
row2=pgm_read_byte_near(pDat);
if (-1 != row2) {
col2=pgm_read_byte_near(pDat+1);
}
// fLastMeta tracks whether the last keypress
// added a shift-style key that the user did not
// explicitly press it, so we can turn it off it
// we don't need it now. It can't help a string
// of three keypresses. ;) The extra up event won't
// cause a problem as the refcounting code can cope
// with that.
if (!is_up) {
if ((METANONE != fLastMeta) && (col1 != SHIFT_COL) && (col2 != SHIFT_COL)) {
switch (fLastMeta) {
case METAFCTN: // FCTN was added last
if ((row1 != FCTN_ROW)&&(row2 != FCTN_ROW)) {
// turn off last key (prevents errors on up event)
ParseRowCol(FCTN_ROW,SHIFT_COL,1);
ParseRowCol(nLastRow,nLastCol,1);
}
break;
case METACTRL: // CTRL was added last
if ((row1 != CTRL_ROW)&&(row2 != CTRL_ROW)) {
// turn off last key (prevents errors on up event)
ParseRowCol(CTRL_ROW,SHIFT_COL,1);
ParseRowCol(nLastRow,nLastCol,1);
}
break;
case METASHIFT: // SHIFT was added last
if ((row1 != SHIFT_ROW) && (row2 != SHIFT_ROW)) {
// turn off last key (prevents errors on up event)
ParseRowCol(SHIFT_ROW,SHIFT_COL,1);
ParseRowCol(nLastRow,nLastCol,1);
}
break;
}
}
fLastMeta=METANONE;
}
if (-1 != row1) {
ParseRowCol(row1,col1,is_up);
}
if (-1 != row2) {
ParseRowCol(row2,col2,is_up);
if (!is_up) {
// Update meta - it may be either one, but it only counts when both were used!
if (col1 == SHIFT_COL) {
switch (row1) {
case FCTN_ROW: fLastMeta=METAFCTN; break;
case SHIFT_ROW: fLastMeta=METASHIFT; break;
case CTRL_ROW: fLastMeta=METACTRL; break;
}
if (METANONE != fLastMeta) {
putserialchar(';');
nLastRow=row2;
nLastCol=col2;
}
} else if (col2 == 7) {
switch (row2) {
case FCTN_ROW: fLastMeta=METAFCTN; break;
case SHIFT_ROW: fLastMeta=METASHIFT; break;
case CTRL_ROW: fLastMeta=METACTRL; break;
}
if (METANONE != fLastMeta) {
putserialchar(';');
nLastRow=row1;
nLastCol=col1;
}
}
}
}
}
// check cheat codes - this is just to catch
// people who dump the binary and copy it to resell,
// and also for a bit of ego boost. Please do not
// remove it. :)
if (0 == pCheat) {
if (!is_up) {
if (0x5a != sc) { // not Enter down
cheatcode[cheatidx++]=sc;
if (cheatidx>9) cheatidx=0;
}
} else {
if (0x5a == sc) { // is Enter up
int x;
int y;
x=cheatidx;
y=0;
#ifdef DEBUGMODE
putserialchar('\r');
putserialchar('\n');
putserialchar('>');
#endif
while (y < 10) {
#ifdef DEBUGMODE
print_hexbyte(cheatcode[x]);
putserialchar(' ');
putserialchar('=');
putserialchar(' ');
print_hexbyte(cheatmatch[y]);
putserialchar('\r');
putserialchar('\n');
#endif
if (cheatcode[x] != cheatmatch[y]) {
break;
}
x++;
if (x>9) x=0;
y++;
}
if (y > 9) {
// this is it - start the playback
#ifdef DEBUGMODE
putserialchar('!');
putserialchar('!');
#endif
memset(cheatcode, 0, 10);
pCheat=copyright;
abortCheat=0;
}
#ifdef DEBUGMODE
putserialchar('\r');
putserialchar('\n');
#endif
}
}
} else {
if ((abortCheat)&&(is_up)&&(0x5a == sc)) {
pCheat=0;
abortCheat=0;
}
}
CheckTIPolling();
}
if (is_up) {
lockedshiftstate=0;
nLastChar=0x00ff; // clear last char - now that we've released we can press again ;)
}
isextended=0;
is_up=0;
break;
}
#ifdef DEBUGMODE
// debug on LEDs
PORTB&=(unsigned char)0xf0;
PORTB|=(unsigned char)((unsigned char)~(is_up|(isextended<<1)|(shiftstate<<2)|(capslock<<3)))&0x0f;
#endif
}
void put_kbbuff(unsigned char c)
{
if (buffcnt<BUFF_SIZE) // If buffer not full
{
*inpt = c; // Put character into buffer
inpt++; // Increment pointer
buffcnt++;
if (inpt >= kb_buffer + BUFF_SIZE) { // Pointer wrapping
inpt = kb_buffer;
}
}
}
int getps2char(void)
{
int byte;
while(buffcnt == 0) { // Wait for data
CheckTIPolling();
delay(0); // just to handle the timeout
}
byte = *outpt; // Get byte
outpt++; // Increment pointer
if (outpt >= kb_buffer + BUFF_SIZE) { // Pointer wrapping
outpt = kb_buffer;
}
buffcnt--; // Decrement buffer count
return byte;
}
void UpdateKbLEDs() {
char nCnt=0;
unsigned char nCmd=0;
// This function may take a while, and it is unlikely the user is
// also pushing any relevant keys. Therefore, we clear our output
// to the TI in case there's old data there
PORTA=0xff;
// As a workaround for ref counting errors, toggling any LED
// will also reset all shift meta keys
if (fctnrefcount) {
fctnrefcount=0;
ticols[7]&=(unsigned char)~(1<<4);
}
if (shiftrefcount) {
shiftrefcount=0;
ticols[7]&=(unsigned char)~(1<<5);
}
if (ctrlrefcount) {
ctrlrefcount=0;
ticols[7]&=(unsigned char)~(1<<6);
}
//delay(1);
// now work out which lights are active
if (capslock) {
nCnt++;
nCmd|=LED_CAPS;
}
if (scrolllock) {
nCnt++;
nCmd|=LED_SCROLL;
}
if (numlock) {
nCnt++;
nCmd|=LED_NUM;
}
if (nCnt&0x01) {
ack_parity=0;
} else {
ack_parity=1;
}
ack_cmd=nCmd;
ack_valid=5; // valid for up to 5 timeout periods
// Although this will weaken the TI response time, the user
// is most likely waiting for the lights to change anyway
while (0 == send_cmd(KEY_LEDS)) {
delay(1);
}
#ifdef DEBUG_MODE
putserialchar('~');
#endif
while (ack_valid != 0) {
delay(1);
}
}
// when numlock is off, the numeric keypad has alternate functions
unsigned char remapnumlock(unsigned char in) {
// this is nice - the scan codes map the same as the real keys,
// only they become extended
if (isextended) {
return in;
}
switch (in) {//Numpad digit
case 0x69: // 1
case 0x6b: // 4
case 0x6c: // 7
case 0x70: // 0
case 0x71: // Period
case 0x72: // 2
case 0x74: // 6
case 0x75: // 8