From 32476f2a4ed39d9e761277b574e1c2f3fb87a59d Mon Sep 17 00:00:00 2001 From: Karol Stasiak Date: Wed, 26 Jun 2019 15:51:09 +0200 Subject: [PATCH] Move readkey to a separate module --- CHANGELOG.md | 2 +- docs/README.md | 2 +- docs/index.md | 2 +- docs/stdlib/frequent.md | 20 ------ docs/stdlib/other.md | 29 +++++++++ examples/crossplatform/guess.mfk | 12 ++-- include/a8_kernel.mfk | 10 --- include/c128_kernal.mfk | 8 --- include/c264_kernal.mfk | 8 --- include/c64_kernal.mfk | 7 --- include/cpc.mfk | 2 - include/keyboard.mfk | 101 +++++++++++++++++++++++++++++++ include/pc88.mfk | 2 - include/vic20_kernal.mfk | 7 --- include/zxspectrum.mfk | 10 --- 15 files changed, 141 insertions(+), 81 deletions(-) create mode 100644 include/keyboard.mfk diff --git a/CHANGELOG.md b/CHANGELOG.md index 00c22190..c39dec7a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,7 +14,7 @@ * Added `memory_barrier` macro. -* Added `readkey` function. +* Added `keyboard` module. * Added `random` module. diff --git a/docs/README.md b/docs/README.md index 858e1404..b7628a76 100644 --- a/docs/README.md +++ b/docs/README.md @@ -45,7 +45,7 @@ * [Modules for reading input devices](stdlib/input.md) -* [Other cross-platform modules (`err`, `random`)](stdlib/other.md) +* [Other cross-platform modules (`keyboard`, `err`, `random`)](stdlib/other.md) * [Definitions available on only some platforms](stdlib/frequent.md) diff --git a/docs/index.md b/docs/index.md index 858e1404..b7628a76 100644 --- a/docs/index.md +++ b/docs/index.md @@ -45,7 +45,7 @@ * [Modules for reading input devices](stdlib/input.md) -* [Other cross-platform modules (`err`, `random`)](stdlib/other.md) +* [Other cross-platform modules (`keyboard`, `err`, `random`)](stdlib/other.md) * [Definitions available on only some platforms](stdlib/frequent.md) diff --git a/docs/stdlib/frequent.md b/docs/stdlib/frequent.md index d00fa26a..6b2ce78e 100644 --- a/docs/stdlib/frequent.md +++ b/docs/stdlib/frequent.md @@ -29,26 +29,6 @@ Moves the cursor to the next line. Available for: all computer targets. Uses ROM routines, so requires the appropriate ROM to be enabled if applicable. -#### `byte readkey()` - -Waits for and reads a single keypress. - -The returning values may vary between platforms: - -* letters may be uppercase or lowercase - -* modifier keys may be applied or ignored - -Available for: -Commodore 64 (requires KERNAL), -Commodore 16 or Plus/4 (requires KERNAL), -Commodore 128 (requires KERNAL), -VIC 20 (requires KERNAL), -Atari, -Amstrad CPC, -ZX Spectrum, -NEC PC-88. - #### `pointer readline()` Reads a line from the console and returns a pointer to a null-terminated string. diff --git a/docs/stdlib/other.md b/docs/stdlib/other.md index 0fc92a59..4513ba16 100644 --- a/docs/stdlib/other.md +++ b/docs/stdlib/other.md @@ -1,5 +1,34 @@ [< back to index](../index.md) +## keyboard + +The `keyboard` module provides support for reading keypresses from the keyboard. +Not supported on all targets. + +#### `byte readkey()` + +Waits for and reads a single keypress. + +The returning values may vary between platforms: + +* letters may be uppercase or lowercase + +* modifier keys may be applied or ignored + +Available for: +Commodore 64 (requires KERNAL), +Commodore 16 or Plus/4 (requires KERNAL), +Commodore 128 (requires KERNAL), +VIC 20 (requires KERNAL), +Atari, +Amstrad CPC, +ZX Spectrum, +NEC PC-88. + +#### `const byte KEY_ENTER` + +Key code for the Enter/Return key. Usually 13, but not always. + ## err #### `enum error_number` diff --git a/examples/crossplatform/guess.mfk b/examples/crossplatform/guess.mfk index e4e85ca9..89b90e8d 100644 --- a/examples/crossplatform/guess.mfk +++ b/examples/crossplatform/guess.mfk @@ -10,7 +10,11 @@ import c64_basic import c264_basic #endif -#define READKEY = CBM_64 | CBM_VIC | CBM_264 | CBM_128 | ZX_SPECTRUM | NEC_PC_88 | ATARI_8 +#define READKEY = CBM_64 | CBM_264 | ZX_SPECTRUM | NEC_PC_88 + +#if READKEY +import keyboard +#endif void main () { init_rand_seed() @@ -63,9 +67,9 @@ void play_round() { putstrz(" attempts!"z) new_line() #if READKEY - putstrz("Press any key to play again."z) - readkey() - new_line() + putstrz("Press any key to play again."z) + readkey() + new_line() #endif new_line() } \ No newline at end of file diff --git a/include/a8_kernel.mfk b/include/a8_kernel.mfk index a24a7e56..4c047297 100644 --- a/include/a8_kernel.mfk +++ b/include/a8_kernel.mfk @@ -13,16 +13,6 @@ noinline asm void putchar(byte a) { rts } -noinline asm byte readkey() { - lda #$C - sta $2A - lda $E425 - pha - lda $E424 - pha - rts -} - inline void new_line() { putchar($9b) } diff --git a/include/c128_kernal.mfk b/include/c128_kernal.mfk index 4cbed02f..767c7bb3 100644 --- a/include/c128_kernal.mfk +++ b/include/c128_kernal.mfk @@ -15,14 +15,6 @@ asm void putchar(byte a) { ? RTS } -noinline asm byte readkey() { -__read_key__start: - LDA $D0 - BEQ __read_key__start - LDX #0 - JMP $C244 -} - inline void new_line() { putchar(13) } diff --git a/include/c264_kernal.mfk b/include/c264_kernal.mfk index 3f918581..912e2e7f 100644 --- a/include/c264_kernal.mfk +++ b/include/c264_kernal.mfk @@ -14,14 +14,6 @@ asm void putchar(byte a) { ? RTS } -noinline asm byte readkey() { -__read_key__start: - LDA $EF - BEQ __read_key__start - LDX #0 - JMP $D8D5 -} - inline void new_line() { putchar(13) } diff --git a/include/c64_kernal.mfk b/include/c64_kernal.mfk index d1ecde68..82abf81e 100644 --- a/include/c64_kernal.mfk +++ b/include/c64_kernal.mfk @@ -31,13 +31,6 @@ asm void putchar(byte a) { ? RTS } -noinline asm byte readkey() { -__read_key__start: - LDA $C6 - BEQ __read_key__start - JMP $E5B4 -} - inline void new_line() { chrout(13) } diff --git a/include/cpc.mfk b/include/cpc.mfk index 0bfce2d1..65c86791 100644 --- a/include/cpc.mfk +++ b/include/cpc.mfk @@ -5,8 +5,6 @@ asm void putchar(byte a) @$BB5A extern -asm byte readkey() @$BB06 extern - inline void new_line() { putchar(13) putchar(10) diff --git a/include/keyboard.mfk b/include/keyboard.mfk new file mode 100644 index 00000000..7d51372b --- /dev/null +++ b/include/keyboard.mfk @@ -0,0 +1,101 @@ +#if ATARI_8 +const byte KEY_ENTER = 155 +#else +const byte KEY_ENTER = 13 +#endif + + +#if CBM_64 +noinline asm byte readkey() { +__read_key__start: + LDA $C6 + BEQ __read_key__start + JMP $E5B4 +} +#define OK = 1 +#endif + + +#if CBM_128 +noinline asm byte readkey() { +__read_key__start: + LDA $D0 + BEQ __read_key__start + LDX #0 + JMP $C244 +} +#define OK = 1 +#endif + + +#if CBM_264 +noinline asm byte readkey() { +__read_key__start: + LDA $EF + BEQ __read_key__start + LDX #0 + JMP $D8D5 +} +#define OK = 1 +#endif + + +#if CBM_VIC +noinline asm byte readkey() { +__read_key__start: + LDA $C6 + BEQ __read_key__start + JMP $E5CF +} +#define OK = 1 +#endif + + +#if ATARI_8 +noinline asm byte readkey() { + lda #$C + sta $2A + lda $E425 + pha + lda $E424 + pha + rts +} +#define OK = 1 +#endif + + +#if NEC_PC_88 +asm byte readkey() @$3583 extern +#define OK = 1 +#endif + + +#if AMSTRAD_CPC +asm byte readkey() @$BB06 extern +#define OK = 1 +#endif + + +#if ZX_SPECTRUM +#pragma zilog_syntax +asm byte readkey() { + ld hl,23560 + ld (hl),0 +__readkey__start: + ld a,(hl) + or a + jr z,__readkey__start + ? ret +} +#define OK = 1 +#endif + + +#if not(OK) + #if KEYBOARD + #warn keyboard module is not yet supported + #else + #warn keyboard module is not supported on targets without keyboard + #endif +#endif \ No newline at end of file diff --git a/include/pc88.mfk b/include/pc88.mfk index 28a5e374..3970b7bd 100644 --- a/include/pc88.mfk +++ b/include/pc88.mfk @@ -6,8 +6,6 @@ asm void putchar(byte a) @$3e0d extern -asm byte readkey() @$3583 extern - inline void new_line() { putchar(13) putchar(10) diff --git a/include/vic20_kernal.mfk b/include/vic20_kernal.mfk index 4d6153cc..1de29e51 100644 --- a/include/vic20_kernal.mfk +++ b/include/vic20_kernal.mfk @@ -15,13 +15,6 @@ asm void putchar(byte a) { ? RTS } -noinline asm byte readkey() { -__read_key__start: - LDA $C6 - BEQ __read_key__start - JMP $E5CF -} - inline void new_line() { putchar(13) } diff --git a/include/zxspectrum.mfk b/include/zxspectrum.mfk index 4e8da414..1fcd63db 100644 --- a/include/zxspectrum.mfk +++ b/include/zxspectrum.mfk @@ -14,16 +14,6 @@ inline void new_line() { putchar(13) } -asm byte readkey() { - ld hl,23560 - ld (hl),0 -__readkey__start: - ld a,(hl) - or a - jr z,__readkey__start - ? ret -} - inline asm void set_border(byte a) { out (254),a ? ret