Terminal program for MailStation devices
1; vim:syntax=z8a:ts=8
2;
3; msTERM
4; getchar and other keyboard routines
5;
6; Copyright (c) 2019 joshua stein <jcs@jcs.org>
7;
8; Permission to use, copy, modify, and distribute this software for any
9; purpose with or without fee is hereby granted, provided that the above
10; copyright notice and this permission notice appear in all copies.
11;
12; THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13; WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14; MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15; ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16; WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17; ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18; OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19;
20
21 .module getchar
22
23 .include "mailstation.inc"
24 .globl _lptrecv
25
26 .area _DATA
27
28 ; scancode_table holds three tables of ascii characters which
29 ; '_getchar' uses to determine which character to return, depending on
30 ; the scancode pressed and the state of the shift and caps lock keys.
31 .include "scancodes.inc"
32
33keyboardbuffer:
34 .ds 2 ; scancode buffer for _getchar
35capslock:
36 .db #0
37
38 .area _CODE
39
40; unsigned char peekkey(void)
41; check for a scancode using the firmware, then look it up in the scancode
42; table (respecting the shift key and caps lock as control) and return the
43; ascii value of the key in the l register
44_peekkey::
45 ld de, #keyboardbuffer
46 push de
47 call get_keycode_from_buffer
48 pop de
49 ld a, (keyboardbuffer) ; check for caps lock first
50 cp #0x60
51 jr z, is_caps_lock
52 jr not_caps_lock
53is_caps_lock:
54 ld a, (keyboardbuffer + 1) ; check flags
55 bit 0, a ; set=pressed, reset=released
56 jp nz, caps_down
57 ld a, #0 ; caps lock released
58 ld (capslock), a
59 jr nokey
60caps_down:
61 ld a, #1
62 ld (capslock), a
63 jr nokey
64not_caps_lock:
65 ld a, (keyboardbuffer + 1) ; check flags
66 bit 0, a ; set=pressed, reset=released
67 jp z, nokey ; key was released, bail
68 bit 6, a ; when set, shift was held down
69 jr z, lowercase
70capital:
71 ld hl, #scancode_table_uppercase
72 jr char_offset
73lowercase:
74 ld a, (capslock)
75 cp #1
76 jr z, as_control
77 ld hl, #scancode_table
78 jr char_offset
79as_control:
80 ld hl, #scancode_table_control
81 jr char_offset
82char_offset:
83 push hl
84 ld hl, #50
85 push hl
86 call _delay
87 pop hl
88 pop hl
89 ld a, (keyboardbuffer)
90 ld b, #0
91 ld c, a
92 add hl, bc
93 ld a, (hl)
94 ld h, #0
95 ld l, a
96 ret
97nokey:
98 ld h, #0
99 ld l, #0
100 ret
101
102
103; unsigned char getkey(void)
104; peekkey() but loops until a key is available
105_getkey::
106 call _peekkey
107 ld a, l
108 cp #0
109 jp z, _getkey
110 ret
111
112
113; int getchar(void)
114; uses _getkey and filters out non-printables, returns in l register
115_getchar::
116 call _getkey
117 ld a, l
118 cp a, #META_KEY_BEGIN
119 jr nc, _getchar ; a >=
120 ret