OpenCores
URL https://opencores.org/ocsvn/rf6809/rf6809/trunk

Subversion Repositories rf6809

[/] [rf6809/] [trunk/] [software/] [boot/] [keyboard.asm] - Blame information for rev 4

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 4 robfinch
; ============================================================================
2
;        __
3
;   \\__/ o\    (C) 2013-2022  Robert Finch, Waterloo
4
;    \  __ /    All rights reserved.
5
;     \/_//     robfinch@opencores.org
6
;       ||
7
;
8
;
9
;       Keyboard driver routines to interface to a PS2 style keyboard
10
; Converts the scancode to ascii
11
;
12
; This source file is free software: you can redistribute it and/or modify
13
; it under the terms of the GNU Lesser General Public License as published
14
; by the Free Software Foundation, either version 3 of the License, or
15
; (at your option) any later version.
16
;
17
; This source file is distributed in the hope that it will be useful,
18
; but WITHOUT ANY WARRANTY; without even the implied warranty of
19
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
; GNU General Public License for more details.
21
;
22
; You should have received a copy of the GNU General Public License
23
; along with this program.  If not, see .
24
;
25
; ============================================================================
26
;
27
SC_F12  EQU     $07
28
SC_C    EQU             $21
29
SC_T    EQU         $2C
30
SC_Z            EQU     $1A
31
SC_DEL  EQU                     $71     ; extend
32
SC_KEYUP        EQU             $F0     ; should be $f0
33
SC_EXTEND EQU     $E0
34
SC_CTRL EQU                     $14
35
SC_RSHIFT               EQU     $59
36
SC_NUMLOCK      EQU     $77
37
SC_SCROLLLOCK           EQU     $7E
38
SC_CAPSLOCK             EQU             $58
39
SC_ALT  EQU                     $11
40
 
41
;#define SC_LSHIFT      EQU             $12
42
;SC_DEL         EQU             $71             ; extend
43
;SC_LCTRL       EQU             $58
44
 
45
SC_TAB  EQU     $0D
46
 
47
; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
48
; Recieve a byte from the keyboard, used after a command is sent to the
49
; keyboard in order to wait for a response.
50
;
51
; Parameters: none
52
; Returns: accd = recieved byte ($00 to $FF), -1 on timeout
53
; Modifies: acc
54
; Stack Space: 2 words
55
; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
56
 
57
KeybdRecvByte:
58
        pshs    x
59
        ldx             #100                                            ; wait up to 1s
60
krb3:
61
        bsr             KeybdGetStatus  ; wait for response from keyboard
62
        tstb
63
        bmi             krb4                                            ; is input buffer full ? yes, branch
64
        bsr             Wait10ms                                ; wait a bit
65
        leax    -1,x
66
        bne             krb3                                            ; go back and try again
67
        ldd             #-1                                                     ; return -1
68
        puls    x,pc
69
krb4:
70
        bsr             KeybdGetScancode
71
        puls    x,pc
72
 
73
; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
74
; Send a byte to the keyboard.
75
;
76
; Parameters: accb byte to send
77
; Returns: none
78
; Modifies: none
79
; Stack Space: 0 words
80
; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
81
 
82
KeybdSendByte:
83
        stb             KEYBD
84
        rts
85
 
86
; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
87
; Wait until the keyboard transmit is complete
88
;
89
; Parameters: none
90
; Returns: r1 = 0 if successful, r1 = -1 timeout
91
; Modifies: r1
92
; Stack Space: 3 words
93
; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
94
 
95
KeybdWaitTx:
96
        pshs    x
97
        ldx             #100                            ; wait a max of 1s
98
kwt1:
99
        bsr             KeybdGetStatus
100
        andb    #$40                            ; check for transmit complete bit; branch if bit set
101
        bne             kwt2
102
        bsr             Wait10ms                ; delay a little bit
103
        leax    -1,x
104
        bne             kwt1                            ; go back and try again
105
        ldd             #-1                                     ; timed out, return -1
106
        puls    x,pc
107
kwt2:
108
        clra                                                    ; wait complete, return 0
109
        clrb
110
        puls    x,pc
111
 
112
; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
113
; Wait for 10 ms
114
;
115
; Parameters: none
116
; Returns: none
117
; Modifies: none
118
; Stack Space: 2 words
119
; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
120
 
121
Wait10ms:
122
        pshs    d
123
        lda             MSCOUNT+3
124
W10_0001:
125
        tfr             a,b
126
        subb    MSCOUNT+3
127
        cmpb    #$FFA
128
        bhi             W10_0001
129
        puls    d,pc
130
 
131
; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
132
; Wait for 300 ms (256 ms)
133
;
134
; Parameters: none
135
; Returns: none
136
; Modifies: none
137
; Stack Space: 2 words
138
; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
139
 
140
Wait300ms:
141
        pshs    d
142
        lda             MSCOUNT+3
143
W300_0001:
144
        tfr             a,b
145
        subb    MSCOUNT+3
146
        cmpb    #$F00
147
        bhi     W300_0001
148
        puls    d,pc
149
 
150
; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
151
; Get the keyboard status
152
;
153
; Parameters: none
154
; Returns: d = status
155
; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
156
 
157
KeybdGetStatus:
158
kbgs3:
159
        ldb             KEYBD+1
160
        bitb    #$80
161
        bne             kbgs1
162
        bitb    #$01            ; check parity error flag
163
        bne             kbgs2
164
        clra
165
        rts
166
kbgs2:
167
        ldb             #$FE            ; request resend
168
        bsr             KeybdSendByte
169
        bsr             KeybdWaitTx
170
        bra             kbgs3
171
kbgs1:                                  ; return negative status
172
        orb             #$F00
173
        lda             #-1
174
        rts
175
 
176
; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
177
; Get the scancode from the keyboard port
178
;
179
; Parameters: none
180
; Returns: acca = scancode
181
; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
182
 
183
KeybdGetScancode:
184
        clra
185
        ldb             KEYBD                           ; get the scan code
186
        clr             KEYBD+1                 ; clear receive register (write $00 to status reg)
187
        ; The following useful during debug.
188
;       lbsr    DispByteAsHex
189
;       pshs    b
190
;       ldb             #' '
191
;       lbsr    OUTCH
192
;       puls    b
193
        rts
194
 
195
; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
196
; Set the LEDs on the keyboard.
197
;
198
; Parameters: d LED status to set
199
; Returns: none
200
; Modifies: none
201
; Stack Space: 2 words
202
; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
203
 
204
KeybdSetLED:
205
        pshs    b
206
        ldb             #$ED                                            ; set LEDs command
207
        bsr             KeybdSendByte
208
        bsr             KeybdWaitTx
209
        bsr             KeybdRecvByte   ; should be an ack
210
        puls    b
211
        bsr             KeybdSendByte
212
        bsr             KeybdWaitTx
213
        bsr             KeybdRecvByte   ; should be an ack
214
        rts
215
 
216
; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
217
; Get ID - get the keyboards identifier code.
218
;
219
; Parameters: none
220
; Returns: d = $AB83, $00 on fail
221
; Modifies: d, KeybdID updated
222
; Stack Space: 2 words
223
; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
224
 
225
KeybdGetID:
226
        ldb             #$F2
227
        bsr             KeybdSendByte
228
        bsr             KeybdWaitTx
229
        bsr             KeybdRecvByte
230
        bitb    #$80
231
        bne             kgnotKbd
232
        cmpb    #$AB
233
        bne             kgnotKbd
234
        bsr             KeybdRecvByte
235
        bitb    #$80
236
        bne             kgnotKbd
237
        cmpb    #$83
238
        bne             kgnotKbd
239
        ldd             #$AB83
240
kgid1:
241
        std             KeybdID
242
        rts
243
kgnotKbd:
244
        clra
245
        clrb
246
        bra             kgid1
247
 
248
; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
249
; Initialize the keyboard.
250
;
251
; Parameters:
252
;               none
253
;       Modifies:
254
;               none
255
; Returns:
256
;               none
257
; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
258
 
259
KeybdInit:
260
        pshs    d,y
261
        ldy             #5
262
kbdi0002:
263
        bsr             Wait10ms
264
        clr             KEYBD+1                 ; clear receive register (write $00 to status reg)
265
        ldb             #-1                                     ; send reset code to keyboard
266
        stb             KEYBD+1                 ; write $FF to status reg to clear TX state
267
        bsr             KeybdSendByte   ; now write to transmit register
268
        bsr             KeybdWaitTx             ; wait until no longer busy
269
        bsr             KeybdRecvByte   ; look for an ACK ($FA)
270
        cmpb    #$FA
271
        bne             kbdiTryAgain
272
        bsr             KeybdRecvByte   ; look for BAT completion code ($AA)
273
        cmpb    #$FC                            ; reset error ?
274
        beq             kbdiTryAgain
275
        cmpb    #$AA                            ; reset complete okay ?
276
        bne             kbdiTryAgain
277
 
278
        ; After a reset, scan code set #2 should be active
279
.config:
280
        ldb             #$F0                    ; send scan code select
281
        stb             LEDS
282
        bsr             KeybdSendByte
283
        bsr             KeybdWaitTx
284
        tstb
285
        bmi             kbdiTryAgain
286
        bsr             KeybdRecvByte   ; wait for response from keyboard
287
        tsta
288
        bmi             kbdiTryAgain
289
        cmpb    #$FA                                    ; ACK
290
        beq             kbdi0004
291
kbdiTryAgain:
292
        dey
293
        bne       kbdi0002
294
.keybdErr:
295
        ldd             #msgBadKeybd
296
        lbsr    DisplayStringCRLF
297
        bra             ledxit
298
kbdi0004:
299
        ldb             #2                      ; select scan code set #2
300
        bsr             KeybdSendByte
301
        bsr             KeybdWaitTx
302
        tstb
303
        bmi             kbdiTryAgain
304
        bsr             KeybdRecvByte   ; wait for response from keyboard
305
        tsta
306
        bmi             kbdiTryAgain
307
        cmpb    #$FA
308
        bne             kbdiTryAgain
309
        bsr             KeybdGetID
310
ledxit:
311
        ldb             #$07
312
        bsr             KeybdSetLED
313
        bsr             Wait300ms
314
        ldb             #$00
315
        bsr             KeybdSetLED
316
        puls    d,y,pc
317
 
318
msgBadKeybd:
319
        fcb             "Keyboard error",0
320
 
321
; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
322
; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
323
 
324
DBGCheckForKey:
325
        bra             KeybdGetStatus
326
 
327
 
328
; KeyState2 variable bit meanings
329
;1176543210
330
; ||||||||+ = shift
331
; |||||||+- = alt
332
; ||||||+-- = control
333
; |||||+--- = numlock
334
; ||||+---- = capslock
335
; |||+----- = scrolllock
336
; ||+------ = 
337
; |+------- =    "
338
; |         =    "
339
; |         =    "
340
; |         =    "
341
; +-------- = extended
342
 
343
; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
344
; Debug versison of keyboard get routine.
345
;
346
; Parameters:
347
;               b:      0 = non blocking, otherwise blocking
348
; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
349
 
350
DBGGetKey:
351
        pshs    x
352
dbgk2:
353
        pshs    b
354
        bsr             KeybdGetStatus
355
        andb    #$80                                                    ; is key available?
356
        puls    b
357
        bne             dbgk1                                                   ; branch if key
358
        tstb                                                                            ; block?
359
        bne             dbgk2                                                   ; If no key and blocking - loop
360
        ldd             #-1                                                             ; return -1 if no block and no key
361
        puls    x,pc
362
dbgk1:
363
        bsr             KeybdGetScancode
364
;       lbsr    DispByteAsHex
365
        ; Make sure there is a small delay between scancode reads
366
        ldx             #20
367
dbgk3:
368
        dex
369
        bne             dbgk3
370
        ; switch on scan code
371
        cmpb    #SC_KEYUP
372
        bne             dbgk4
373
        clr             KeyState1                                       ; make KeyState1 = -1
374
        neg             KeyState1
375
        bra             dbgk2                                                   ; loop back
376
dbgk4:
377
        cmpb    #SC_EXTEND
378
        bne             dbgk5
379
        lda             KeyState2
380
        ora             #$800
381
        sta             KeyState2
382
        bra             dbgk2
383
dbgk5:
384
        cmpb    #SC_CTRL
385
        bne             dbgkNotCtrl
386
        tst             KeyState1
387
        bmi             dbgk7
388
        lda             KeyState2
389
        ora             #4
390
        sta             KeyState2
391
        bra             dbgk8
392
dbgk7:
393
        lda             KeyState2
394
        anda    #~4
395
        sta             KeyState2
396
dbgk8:
397
        clr             KeyState1
398
        bra             dbgk2
399
dbgkNotCtrl:
400
        cmpb    #SC_RSHIFT
401
        bne             dbgkNotRshift
402
        tst             KeyState1
403
        bmi             dbgk9
404
        lda             KeyState2
405
        ora             #1
406
        sta             KeyState2
407
        bra             dbgk10
408
dbgk9:
409
        lda             KeyState2
410
        anda    #~1
411
        sta             KeyState2
412
dbgk10:
413
        clr             KeyState1
414
        bra             dbgk2
415
dbgkNotRshift:
416
        cmpb    #SC_NUMLOCK
417
        bne             dbgkNotNumlock
418
        lda             KeyState2
419
        eora    #16
420
        sta             KeyState2
421
        lda             KeyLED
422
        eora    #2
423
        sta             KeyLED
424
        tfr             a,b
425
        clra
426
        bsr             KeybdSetLED
427
        bra             dbgk2
428
dbgkNotNumlock:
429
        cmpb    #SC_CAPSLOCK
430
        bne             dbgkNotCapslock
431
        lda             KeyState2
432
        eora    #32
433
        sta             KeyState2
434
        lda             KeyLED
435
        eora    #4
436
        sta             KeyLED
437
        tfr             a,b
438
        clra
439
        bsr             KeybdSetLED
440
        bra             dbgk2
441
dbgkNotCapslock:
442
        cmpb    #SC_SCROLLLOCK
443
        bne             dbgkNotScrolllock
444
        lda             KeyState2
445
        eora    #64
446
        sta             KeyState2
447
        lda             KeyLED
448
        eora    #1
449
        sta             KeyLED
450
        tfr             a,b
451
        clra
452
        bsr             KeybdSetLED
453
        bra             dbgk2
454
dbgkNotScrolllock:
455
        cmpb    #SC_ALT
456
        bne             dbgkNotAlt
457
        tst             KeyState1
458
        bmi             dbgk11
459
        lda             KeyState2
460
        ora             #2
461
        sta             KeyState2
462
        bra             dbgk12
463
dbgk11:
464
        lda             KeyState2
465
        anda    #~2
466
        sta             KeyState2
467
dbgk12:
468
        clr             KeyState1
469
        bra             dbgk2
470
dbgkNotAlt:
471
        tst             KeyState1
472
        beq             dbgk13
473
        clr             KeyState1
474
        bra             dbgk2
475
dbgk13:
476
        lda             KeyState2               ; Check for CTRL-ALT-DEL
477
        anda    #6
478
        cmpa    #6
479
        bne             dbgk14
480
        cmpb    #SC_DEL
481
        bne             dbgk14
482
        jmp             [$FFFFFE]               ; jump to reset vector
483
dbgk14:
484
        tst             KeyState2               ; extended code?
485
        bpl             dbgk15
486
        lda             KeyState2
487
        anda    #$7FF
488
        sta             KeyState2
489
        ldx             #keybdExtendedCodes
490
        bra             dbgk18
491
dbgk15:
492
        lda             KeyState2               ; Is CTRL down?
493
        bita    #4
494
        beq             dbgk16
495
        ldx             #keybdControlCodes
496
        bra             dbgk18
497
dbgk16:
498
        bita    #1                                      ; Is shift down?
499
        beq             dbgk17
500
        ldx             #shiftedScanCodes
501
        bra             dbgk18
502
dbgk17:
503
        ldx             #unshiftedScanCodes
504
dbgk18:
505
        abx
506
        ldb             ,x
507
        clra
508
        puls    x,pc                            ; and return
509
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.