-
Notifications
You must be signed in to change notification settings - Fork 0
/
968551.asm
3096 lines (2620 loc) · 99.1 KB
/
968551.asm
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
;//////////////////////////////////////////////////////////////////////
;// //
;// Tandberg TDV-5000 series 968551 v2.1 //
;// //
;// 8051-based Firmware, running the TDV-5010 122-key keyboard //
;// from Tandber Data. This provides a MF2-type keyboard for the //
;// PS/2 interface, with support for some extra features like key- //
;// click, key-locks and an AT/XT/Terminal mode toggle switch. //
;// //
;// Dissasembled by Frodevan, 2023 //
;// //
;//////////////////////////////////////////////////////////////////////
;////////////////////////////////////////
;//
;// Ports
;//
ROW_DATA DATA P0
ROW_SELECT DATA P1
LEDS_AND_JUMPERS DATA P2
DATA_AND_CLOCK DATA P3
;////////////////////////////////////////
;//
;// Port bitfields
;//
SPEAKER BIT P2.0
NUM_LOCK_INDICATOR BIT P2.1
CAPS_LOCK_INDICATOR BIT P2.2
SCROLL_LOCK_INDICATOR BIT P2.3
KEYBOARD_MODE_JUMPER_ST1 BIT P2.4
KEYBOARD_MODE_JUMPER_ST2 BIT P2.5
DEFAULT_BEEP_JUMPER_ST3 BIT P2.6
NAVIGATION_NUMCASE_IGNORE_FLAG BIT P2.7
SERIAL_CLOCK_TX BIT P3.0
SERIAL_DATA_TX BIT P3.1
SERIAL_CLOCK_RX BIT P3.2
SERIAL_DATA_RX BIT P3.3
DISABLE_SCANNING_FLAG BIT P3.5
R_SHIFT_UP_PENDING BIT P3.6
L_SHIFT_UP_PENDING BIT P3.7
;//////////////////////////////////////////////////////////////////////
;//
;// Internal RAM layout
;//
DSEG AT 00h
;////////////////////////////////////////
;//
;// Main-loop registers
;//
scancode_tx_buffer_head_ptr: DS 1
scancode_tx_buffer_tail_ptr: DS 1
scancode_tx_queue_length: DS 1
secondary_loop_counter: DS 1
temporary_counter: DS 1
previously_sent_byte: DS 1
size_of_unsent_msg: DS 1
keytype_flags: DS 1
;////////////////////////////////////////
;//
;// Scanning registers
;//
keystate_row_current_bitmap_ptr: DS 1
keystate_row_held_bitmap_ptr: DS 1
row_counter: DS 1
number_of_keys_held: DS 1
keystate_row_previous_bitmap: DS 1
scancode_table_ptr_hi: DS 1
latest_held_key_index: DS 1
beep_duration: DS 1
;////////////////////////////////////////
;//
;// Stack
;//
stack_space: DS 17
;////////////////////////////////////////
;//
;// Variables
;//
keylocks_state: DS 1
keylocks_prev_state: DS 1
keyup_key_index: DS 1
t1_ticks_since_last_tx: DS 1
typematic_count: DS 1
keydown_key_index: DS 1
scancode_table_ptr_lo: DS 1
typematic_delay: DS 1
typematic_repeat_rate: DS 1
prioritized_tx_byte: DS 1
rx_byte: DS 1
;////////////////////////////////////////
;//
;// Data-tables
;//
bit_flags: DS 3
scancode_tx_queue: DS 17
current_keystate_bitmap: DS 16
held_keystate_bitmap: DS 16
key_mode_table: DS 32
;//////////////////////////////////////////////////////////////////////
;//
;// Internal Bitfield layout
;//
;////////////////////////////////////////
;//
;// bit_flags
;//
BSEG AT 8*(bit_flags - 20h)
change_mode_of_backspace: DBIT 1
xt_mode_flag: DBIT 1
tx_byte_pending: DBIT 1
release_enabled: DBIT 1
scancode_queue_overflowed: DBIT 1
is_scancode_set_1: DBIT 1
is_scancode_set_3: DBIT 1
l_shift_key_held: DBIT 1
r_shift_key_held: DBIT 1
ctrl_key_held: DBIT 1
alt_key_held: DBIT 1
num_lock_flag: DBIT 1
typematic_enabled: DBIT 1
typematic_ongoing: DBIT 1
typematic_armed_flag: DBIT 1
typematic_ready_flag: DBIT 1
proccessing_keys_up_flag: DBIT 1
beep_enable_flag: DBIT 1
sound_beeper: DBIT 1
rx_tx_parity_bit: DBIT 1
tx_and_rx_done: DBIT 1
rx_and_tx_flag: DBIT 1
tx_single_byte_flag: DBIT 1
in_init_flag: DBIT 1
;//////////////////////////////////////////////////////////////////////
;//
;// Program entry
;//
CSEG AT 0000h
LJMP init
DB 00h, 00h, 00h, 00h, 00h, 00h, 00h, 00h
;//////////////////////////////////////////////////////////////////////
;//
;// Interrupt Timers
;//
;////////////////////////////////////////
;//
;// Secondary timer
;//
;// Handles key scanning.
;// Triggers about 2.20KHz
;//
timer0_int:
MOV TL0,#60h ; Reset timer
MOV TH0,#0FEh
JNB DISABLE_SCANNING_FLAG,timer_routine
RETI
DB 00h, 00h, 00h, 00h, 00h, 00h
;////////////////////////////////////////
;//
;// Primary timer
;//
;// Mainly used for beeping the speaker.
;// Triggers about 4.47KHz, Prioritized
;//
timer1_int:
INC t1_ticks_since_last_tx
JNB sound_beeper,skip_flipping_speaker
DJNZ beep_duration,flip_speaker
MOV beep_duration,#18h
CLR SPEAKER
CLR sound_beeper
skip_flipping_speaker:
RETI
flip_speaker:
CPL SPEAKER
RETI
;//////////////////////////////////////////////////////////////////////
;//
;// Key Scanning routines
;//
;// Scans the keyboard matrix, one row per timer-interrupt. The full
;// round therefore takes 17 interrupts, since there is one extra
;// interrupt where pointers are reset and the trigger for typematic
;// repeat is being handled. That is, around 130 complete scan-cycles
;// are done every second. Since there's one cycle delay to sort out
;// debounce, the typical latency will be in the range of 8-15ms.
;//
;// R0 Pointer to bitmap of row's current immediate key state
;// R1 Pointer to bitmap of row's current key-held state
;// R2 Row counter
;// R3 Total number of keys held in all rows
;// R4 Bitmap of row's previous immediate key state
;// R4 Temporary key-held bitmap (in check_for_typematic_arming)
;//
timer_routine:
PUSH ACC ; Save CPU state, swap registers
PUSH PSW
PUSH DPL
PUSH DPH
SETB RS0
INC R2 ; Advance to next row
CJNE R2,#10h,read_next_row
LJMP reset_row_counter ; Reset scan if all rows done
read_next_row:
MOV A,ROW_SELECT ; Select current row
ANL A,#0F0h
ADD A,R2
MOV ROW_SELECT,A
NOP ; Small delay
NOP
NOP
NOP
NOP
MOV A,ROW_DATA ; Read row data
CJNE R2,#0Ch,check_for_keysup
ANL A,#0FCh
;////////////////////////////////////////
check_for_keysup:
XCh A,@R0 ; Save new immediate key-state
MOV R4,A ; Keep previous immediate key-state
ORL A,@R0 ; Get keys not held in either
CPL A
ANL A,@R1 ; See if any prev. held key is up
JNZ new_keys_up
LJMP check_for_keysdown
new_keys_up:
SETB proccessing_keys_up_flag ; Determine which key is up in row
JB ACC.0,new_key_col_0_up
JB ACC.1,new_key_col_1_up
JB ACC.2,new_key_col_2_up
JB ACC.3,new_key_col_3_up
JB ACC.4,new_key_col_4_up
JB ACC.5,new_key_col_5_up
JB ACC.6,new_key_col_6_up
MOV A,@R1 ; Column 7 up, update held keys
ANL A,#7Fh
MOV @R1,A
MOV A,#70h
SJMP handle_key_up
new_key_col_6_up:
MOV A,@R1 ; Column 6 up, update held keys
ANL A,#0BFh
MOV @R1,A
MOV A,#60h
SJMP handle_key_up
new_key_col_5_up:
MOV A,@R1 ; Column 5 up, update held keys
ANL A,#0DFh
MOV @R1,A
MOV A,#50h
SJMP handle_key_up
new_key_col_4_up:
MOV A,@R1 ; Column 4 up, update held keys
ANL A,#0EFh
MOV @R1,A
MOV A,#40h
SJMP handle_key_up
new_key_col_3_up:
MOV A,@R1 ; Column 3 up, update held keys
ANL A,#0F7h
MOV @R1,A
MOV A,#30h
SJMP handle_key_up
new_key_col_2_up:
MOV A,@R1 ; Column 2 up, update held keys
ANL A,#0FBh
MOV @R1,A
MOV A,#20h
SJMP handle_key_up
new_key_col_1_up:
MOV A,@R1 ; Column 1 up, update held keys
ANL A,#0FDh
MOV @R1,A
MOV A,#10h
SJMP handle_key_up
new_key_col_0_up:
MOV A,@R1 ; Column 0 up, update held keys
ANL A,#0FEh
MOV @R1,A
CLR A
;////////////////////////////////////////
handle_key_up:
ADD A,R2 ; Combine col and row for key index
MOV keyup_key_index,A ; Save it for scancode event
MOV DPTR,#keytype_flags_table ; Get flags for key
MOVC A,@A+DPTR
MOV keytype_flags,A ; Save it for event and proccessing
MOV size_of_unsent_msg,#00h ; Do scancode event
LCALL encode_and_tx_scancode
DEC R3 ; Scancode sent, one less held key
MOV A,keytype_flags ; Handle any changes to shift state
ANL A,#38h ; Key shift-type in bit 3-5
RL A
SWAP A
JZ normal_key_keyup ; Determine shift-type of key
DEC A
JZ l_shift_key_keyup
DEC A
JZ r_shift_key_keyup
DEC A
JZ alt_key_keyup
DEC A
JZ ctrl_key_keyup
SJMP normal_key_keyup
l_shift_key_keyup:
CLR l_shift_key_held ; Left shift let go
JNB scancode_queue_overflowed,skip_holdoff_shifts
SETB L_SHIFT_UP_PENDING ; Set flag for overflow recovery
SJMP normal_key_keyup
r_shift_key_keyup:
CLR r_shift_key_held ; Right shift let go
JNB scancode_queue_overflowed,skip_holdoff_shifts
SETB R_SHIFT_UP_PENDING ; Set flag for overflow recovery
SJMP normal_key_keyup
alt_key_keyup:
CLR alt_key_held ; Alt let go
SJMP normal_key_keyup
ctrl_key_keyup:
CLR ctrl_key_held ; Ctrl let go
normal_key_keyup:
CLR scancode_queue_overflowed
skip_holdoff_shifts:
CLR typematic_ongoing ; Any key-up stops tymeatic repeat
;////////////////////////////////////////
check_for_keysdown:
CLR proccessing_keys_up_flag
MOV A,R4 ; Get previous immediate key state
ANL A,@R0 ; Get keys which are still held
CPL A ; Prune keys already proccessed
ORL A,@R1
CPL A
JNZ new_keys_down
LJMP check_for_typematic_arming
new_keys_down:
JB ACC.0,new_key_col_0_down ; Determine which key is held
JB ACC.1,new_key_col_1_down
JB ACC.2,new_key_col_2_down
JB ACC.3,new_key_col_3_down
JB ACC.4,new_key_col_4_down
JB ACC.5,new_key_col_5_down
JB ACC.6,new_key_col_6_down
MOV A,@R1 ; Column 7 down, update held keys
ORL A,#80h
MOV @R1,A
MOV A,#70h
SJMP handle_key_down
new_key_col_6_down:
MOV A,@R1 ; Column 6 down, update held keys
ORL A,#40h
MOV @R1,A
MOV A,#60h
SJMP handle_key_down
new_key_col_5_down:
MOV A,@R1 ; Column 5 down, update held keys
ORL A,#20h
MOV @R1,A
MOV A,#50h
SJMP handle_key_down
new_key_col_4_down:
MOV A,@R1 ; Column 4 down, update held keys
ORL A,#10h
MOV @R1,A
MOV A,#40h
SJMP handle_key_down
new_key_col_3_down:
MOV A,@R1 ; Column 3 down, update held keys
ORL A,#08h
MOV @R1,A
MOV A,#30h
SJMP handle_key_down
new_key_col_2_down:
MOV A,@R1 ; Column 2 down, update held keys
ORL A,#04h
MOV @R1,A
MOV A,#20h
SJMP handle_key_down
new_key_col_1_down:
MOV A,@R1 ; Column 1 down, update held keys
ORL A,#02h
MOV @R1,A
MOV A,#10h
SJMP handle_key_down
new_key_col_0_down:
MOV A,@R1 ; Column 0 down, update held keys
ORL A,#01h
MOV @R1,A
CLR A
;////////////////////////////////////////
handle_key_down:
ADD A,R2 ; Combine col and row for key index
MOV keydown_key_index,A ; Save it for scancode event
JNB xt_mode_flag,skip_update_scroll_lock
CJNE A,#3Eh,skip_update_scroll_lock
CPL SCROLL_LOCK_INDICATOR ; Key 3E is hardcoded to ScrollLock
skip_update_scroll_lock:
MOV DPTR,#keytype_flags_table ; Get flags for key
MOVC A,@A+DPTR
MOV keytype_flags,A ; Save it for event and proccessing
MOV size_of_unsent_msg,#00h ; Do scancode event
LCALL encode_and_tx_scancode
CLR scancode_queue_overflowed
INC R3 ; Scancode sent, one more held key
MOV typematic_count,typematic_delay
CLR typematic_ongoing ; Reset typematic repeat system
MOV A,keytype_flags ; Set default for scancode set 1/2
ANL A,#07h ; Get key-type
JB is_scancode_set_3,keydown_process_modifier
CJNE A,#05h,arm_default_typematic
CLR typematic_enabled ; No typematic rep. for Pause/Break
SJMP keydown_process_modifier
arm_default_typematic:
SETB typematic_enabled ; Otherwise, yes typematic repeat
keydown_process_modifier:
MOV A,keytype_flags ; Handle any changes to shift state
ANL A,#38h ; Key shift-type in bit 3-5
RL A
SWAP A
JZ normal_key_keydown ; Determine shift-type of key
DEC A
JZ l_shift_key_keydown
DEC A
JZ r_shift_key_keydown
DEC A
JZ alt_key_keydown
DEC A
JZ ctrl_key_keydown
DEC A
JZ num_lock_key_keydown
DEC A
JZ caps_lock_key_keydown
SJMP normal_key_keydown
num_lock_key_keydown:
JNB xt_mode_flag,normal_key_keydown
CPL num_lock_flag
CPL NUM_LOCK_INDICATOR
SJMP normal_key_keydown
l_shift_key_keydown:
SETB l_shift_key_held
SJMP normal_key_keydown
r_shift_key_keydown:
SETB r_shift_key_held
SJMP normal_key_keydown
alt_key_keydown:
SETB alt_key_held
SJMP normal_key_keydown
ctrl_key_keydown:
SETB ctrl_key_held
SJMP normal_key_keydown
caps_lock_key_keydown:
JNB xt_mode_flag,normal_key_keydown
CPL CAPS_LOCK_INDICATOR
normal_key_keydown:
MOV A,keydown_key_index
CJNE A,#0Dh,keydown_arm_beep ; Key 0D is hardcoded to Arrow Down
JNB alt_key_held,keydown_arm_beep
JNB ctrl_key_held,keydown_arm_beep
CPL beep_enable_flag ; Toggle beep if Ctrl+Alt+Down
keydown_arm_beep:
JNB beep_enable_flag,done_keydown
SETB sound_beeper ; Trigger beep if enabled
done_keydown:
SJMP key_row_end
;////////////////////////////////////////
check_for_typematic_arming:
JNB typematic_ready_flag,key_row_end
JB typematic_armed_flag,key_row_end
MOV A,@R1 ; Scan if most recent key is held
JZ key_row_end
search_for_next_typematic_key:
JB ACC.0,key_col_0_held ; Check any held key in row
JB ACC.1,key_col_1_held
JB ACC.2,key_col_2_held
JB ACC.3,key_col_3_held
JB ACC.4,key_col_4_held
JB ACC.5,key_col_5_held
JB ACC.6,key_col_6_held
ANL A,#7Fh ; Column 7 held, select
MOV R4,A
MOV A,#70h
SJMP held_key_found
key_col_6_held:
ANL A,#0BFh ; Column 6 down, select
MOV R4,A
MOV A,#60h
SJMP held_key_found
key_col_5_held:
ANL A,#0DFh ; Column 5 down, select
MOV R4,A
MOV A,#50h
SJMP held_key_found
key_col_4_held:
ANL A,#0EFh ; Column 4 down, select
MOV R4,A
MOV A,#40h
SJMP held_key_found
key_col_3_held:
ANL A,#0F7h ; Column 3 down, select
MOV R4,A
MOV A,#30h
SJMP held_key_found
key_col_2_held:
ANL A,#0FBh ; Column 2 down, select
MOV R4,A
MOV A,#20h
SJMP held_key_found
key_col_1_held:
ANL A,#0FDh ; Column 1 down, select
MOV R4,A
MOV A,#10h
SJMP held_key_found
key_col_0_held:
ANL A,#0FEh ; Column 0 down, select
MOV R4,A
CLR A
;////////////////////////////////////////
held_key_found:
ADD A,R2 ; Combine col and row for key index
MOV latest_held_key_index,A
CJNE A,keydown_key_index,not_latest_held_key
SETB typematic_armed_flag ; Arm typematic if the key is found
SJMP key_row_end
not_latest_held_key:
MOV A,R4 ; Otherwise keep scanning held keys
JNZ search_for_next_typematic_key
;////////////////////////////////////////
key_row_end:
INC R0 ; Point to next row
INC R1
timer_int_end:
POP DPH ; Restore CPU state
POP DPL
POP PSW
POP ACC
RETI
;////////////////////////////////////////
reset_row_counter:
MOV R2,#-1 ; Reset row counter
MOV R0,#current_keystate_bitmap ; Reset keystate bitmap pointers
MOV R1,#held_keystate_bitmap
JB typematic_ongoing,check_for_typematic_trigger
JBC typematic_ready_flag,typematic_arm_check
MOV A,R3 ; Get number of held keys
JZ typematic_skip_ready ; Don't ready typematic if no keys
DJNZ typematic_count,typematic_skip_ready
SETB typematic_ready_flag ; Typematic delay almost over: Ready
CLR typematic_armed_flag ; Arming is done on next scan-cycle
typematic_skip_ready:
SJMP read_keylocks
;////////////////////////////////////////
typematic_arm_check:
SETB typematic_ongoing ; Start typematic anyways
JNB typematic_armed_flag,read_keylocks
MOV typematic_count,#05h ; If armed, add rest of delay first
LJMP read_keylocks
;////////////////////////////////////////
typematic_disarm:
CLR typematic_armed_flag ; Trigger will skip if not armed
SJMP read_keylocks
;////////////////////////////////////////
check_for_typematic_trigger:
DJNZ typematic_count,read_keylocks
JNB typematic_armed_flag,read_keylocks
JNB typematic_enabled,typematic_disarm
JNB SERIAL_CLOCK_RX,typematic_triggered
MOV typematic_count,#01h
LJMP typematic_end
typematic_triggered:
MOV DPTR,#keytype_flags_table ; Get typematic key flags
MOV A,latest_held_key_index
MOVC A,@A+DPTR
MOV keytype_flags,A ; Save for evt. scancode event
JNB is_scancode_set_3,typematic_scancode_set_12
CPL A ; Check if Notis-key in set 3
ANL A,#07h
JNZ typematic_normal_key ; If not, send scancode
MOV A,#80h ; Else, send Notis-key prefix
MOV size_of_unsent_msg,#00h
SJMP typematic_send_scode_prefix
typematic_scancode_set_12:
MOV size_of_unsent_msg,#00h ; Scancode set 1/2 typematic...
MOV A,keytype_flags ; Get flags back to check shift
ANL A,#07h
JZ typematic_normal_key ; Send normal keys clean
JNB alt_key_held,typematic_set_12_extended_no_alt
CJNE A,#04h,typematic_set_12_extended_no_alt
MOV A,#84h ; Typematic on Alt+PrtScr in set 2
JNB is_scancode_set_1,typematic_send_scancode
MOV A,#54h ; Typematic on Alt+PrtScr in set 1
SJMP typematic_send_scancode
typematic_set_12_extended_no_alt:
MOV A,keytype_flags ; Get flags back to check Notis
CPL A
ANL A,#07h
JZ typematic_notis_key ; Send Notis prefix if so
MOV A,#0E0h
SJMP typematic_send_scode_prefix ; Else send extended prefix
typematic_notis_key:
MOV A,#80h
typematic_send_scode_prefix:
LCALL queue_scancode ; Send any prefix
typematic_normal_key:
MOV DPL,scancode_table_ptr_lo
MOV DPH,scancode_table_ptr_hi
MOV A,latest_held_key_index ; Get normal scancode
MOVC A,@A+DPTR
typematic_send_scancode:
LCALL queue_scancode ; Send main scancode
MOV typematic_count,typematic_repeat_rate
typematic_end:
CLR scancode_queue_overflowed
JNB beep_enable_flag,typematic_only_beep_on_normal_key
SETB sound_beeper ; Sound beeper if enabled...
typematic_only_beep_on_normal_key:
MOV A,keytype_flags
ANL A,#38h
JZ read_keylocks
CLR sound_beeper ; ...but ONLY on normal keys!
;////////////////////////////////////////
read_keylocks:
MOV A,ROW_SELECT ; Read keylocks on row 0Ch
ANL A,#0F0h ; Select row
ADD A,#0Ch
MOV ROW_SELECT,A
NOP ; Wait a little
NOP
NOP
NOP
NOP
MOV A,ROW_DATA ; Data in lower 2 columns
ANL A,#03h
CJNE A,keylocks_prev_state,keylocks_end
MOV keylocks_state,keylocks_prev_state
AJMP timer_int_end
keylocks_end:
MOV keylocks_prev_state,A ; Save immediate state for debounce
AJMP timer_int_end
;//////////////////////////////////////////////////////////////////////
;//
;// Main Loop
;//
;// Handles communication to and from the keyboard, as well as
;// reacting to any incoming commands from the computer.
;//
main_loop:
JB L_SHIFT_UP_PENDING,main_enrty_pending_shift_up
JB R_SHIFT_UP_PENDING,main_enrty_pending_shift_up
main_loop_no_pending_shift_up:
LCALL push_tx ; Send any pending scancodes
main_loop_wait_for_rx:
LCALL poll_rx ; Poll for incoming commands
JNB tx_and_rx_done,main_loop
main_loop_repeat_prev_cmd:
CLR tx_and_rx_done
MOV A,rx_byte ; Check command
CJNE A,#-20,check_command_in_range
check_command_in_range:
JC command_not_in_range
MOV DPTR,#keyboard_commands ; Execute valid command [EC -> FF]
SUBB A,#-20
RL A
JMP @A+DPTR
command_not_in_range:
LJMP invalid_operation ; Else handle invalid command
;////////////////////////////////////////
;//
;// Recover shift release events after overflow
;//
main_enrty_pending_shift_up:
JB is_scancode_set_1,encode_pending_shift_up_set_1
MOV A,#0F0h ; Queue break-code for set 2
LCALL queue_scancode
MOV A,#12h ; Left Shift scancode for set 2
JB L_SHIFT_UP_PENDING,send_pending_shift_up
MOV A,#59h ; Right Shift scancode for set 2
SJMP send_pending_shift_up
encode_pending_shift_up_set_1:
MOV A,#0AAh ; Left Shift break-code for set 1
JB L_SHIFT_UP_PENDING,send_pending_shift_up
MOV A,#0B6h ; Right Shift break-code for set 1
send_pending_shift_up:
LCALL queue_scancode ; Queue scancode
JBC scancode_queue_overflowed,main_loop_no_pending_shift_up
JBC L_SHIFT_UP_PENDING,main_loop_no_pending_shift_up
CLR R_SHIFT_UP_PENDING
SJMP main_loop_no_pending_shift_up
;////////////////////////////////////////
keyboard_commands:
AJMP op_get_keylocks ; Jump-table to all valid commands
AJMP op_set_indicators
AJMP op_echo
AJMP invalid_operation
AJMP op_set_scancode_set
AJMP invalid_operation
AJMP op_identify_kbd
AJMP op_set_typematic
AJMP op_en_scanning
AJMP op_restore_defaults
AJMP op_restore_defaults
AJMP op_typematic_all
AJMP op_make_release_all
AJMP op_make_only_all
AJMP op_typematic_make_release_all
AJMP op_typematic_key
AJMP op_typematic_make_release_key
AJMP op_make_only_key
AJMP op_resend
AJMP op_reset_and_selftest
;////////////////////////////////////////
;//
;// Command: Get keylock state
;//
;// Input:
;// [EC]
;//
;// Output:
;// [FA]
;// Keylock state
;// -> [F6]: Both keylocks open
;// -> [F7]: Keylock col 1 closed
;// -> [F8]: Keylock col 0 closed
;// -> [F9]: Both keylocks closed
;//
op_get_keylocks:
CLR ET0 ; Data will be sent, so no scanning
SETB rx_and_tx_flag ; Send Ack
MOV prioritized_tx_byte,#0FAh
SETB tx_single_byte_flag
LCALL push_tx
MOV A,keylocks_state ; Get lock state
PUSH DPL ; Translate it to return byte
PUSH DPH
MOV DPTR,#keylocks_status_enum
MOVC A,@A+DPTR
POP DPH
POP DPL
MOV prioritized_tx_byte,A ; Send byte
SETB tx_single_byte_flag
CLR rx_and_tx_flag
LCALL push_tx
AJMP main_loop ; Command done
keylocks_status_enum:
DB 0F6h, 0F8h, 0F7h, 0F9h
;////////////////////////////////////////
;//
;// Command: Set keyboard indicators
;//
;// Input:
;// [ED]
;// Indicator state
;// -> [00]: All off
;// -> [01]: Scroll-Lock on
;// -> [02]: Num-Lock on
;// -> [03]: Scroll-Lock + Num-Lock on
;// -> [04]: Caps-Lock on
;// -> [05]: Caps-Lock + Scroll-Lock on
;// -> [06]: Caps-Lock + Num-Lock on
;// -> [07]: All on
;//
;// Output:
;// [FA]
;// [FA]
;//
op_set_indicators:
CLR ET0 ; Data will be sent, so no scanning
MOV prioritized_tx_byte,#0FAh ; Send Ack
SETB tx_single_byte_flag
SETB rx_and_tx_flag ; But also expect data
set_indicators_tx_loop:
LCALL push_tx
set_indicators_rx_loop:
LCALL poll_rx
JB tx_byte_pending,set_indicators_tx_loop
JNB tx_and_rx_done,set_indicators_rx_loop
CLR tx_and_rx_done
MOV A,rx_byte ; Get received data
CJNE A,#08h,check_indicator_range
check_indicator_range:
JNC invalid_indicator_data ; Check data
CPL A ; Set indicators from valid data
MOV C,ACC.0
MOV SCROLL_LOCK_INDICATOR,C
MOV C,ACC.2
MOV CAPS_LOCK_INDICATOR,C
MOV C,ACC.1
MOV NUM_LOCK_INDICATOR,C
CPL C
MOV num_lock_flag,C
MOV prioritized_tx_byte,#0FAh ; Ack valid data
SETB tx_single_byte_flag
CLR rx_and_tx_flag
LCALL push_tx
AJMP main_loop ; Command done
invalid_indicator_data:
CLR rx_and_tx_flag ; Invalid data, wait for more
SETB ET0 ; Enable scanning before retry
AJMP main_loop_repeat_prev_cmd
;////////////////////////////////////////
;//
;// Command: Echo
;//
;// Input:
;// [EE]
;//
;// Output:
;// [EE]
;//
op_echo:
MOV prioritized_tx_byte,#0EEh ; Send Echo response
SETB tx_single_byte_flag
AJMP main_loop ; Command done
;////////////////////////////////////////
;//
;// Invalid command
;//
;// Output:
;// [FE]
;//
invalid_operation:
MOV previously_sent_byte,prioritized_tx_byte
MOV prioritized_tx_byte,#0FEh ; Send Nack
SETB tx_single_byte_flag
AJMP main_loop ; Command done
;////////////////////////////////////////
;//
;// Command: Set Scancode Set
;//
;// Input:
;// [F0]
;// Scancode set
;// -> [00]: Get selected scancode set
;// -> [01]: Selected scancode set 1
;// -> [02]: Selected scancode set 2
;// -> [03]: Selected scancode set 3
;//
;// Output:
;// [FA]
;// [FA]
;// Scancode set selected (if "Get selected" Input)
;// -> [01]: Scancode set 1