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