gbdk-2020 | GameBoy Development Kit |
| download: https://git.y1.nz/archives/gbdk.tar.gz | |
| README | Files | Log | Refs | LICENSE |
commit a2aa923973cf26d1d1ef9a40e480f2d91ff5cc44 parent 2997e027c606f2254f433b0d3283d4d03dbbc8a1 Author: bbbbbr <bbbbbr@users.noreply.github.com> Date: Thu, 13 Mar 2025 01:59:15 -0700 Merge pull request #749 from bbbbbr/megaduck/laptop_io_updates MegaDuck: Updated on duck laptop IO commands, expose a few vars - Printer command labeled and some query commands for the init result - Run cart command labeled Diffstat:
| M | gbdk-lib/examples/megaduck/laptop_keyboard/src/megaduck_key2ascii.c | 2 | +- |
| M | gbdk-lib/examples/megaduck/laptop_keyboard/src/megaduck_keyboard.c | 12 | ++++++++++-- |
| M | gbdk-lib/examples/megaduck/laptop_keyboard/src/megaduck_keyboard.h | 8 | ++++---- |
| M | gbdk-lib/include/duck/laptop_io.h | 41 | ++++++++++++++++++++++++++++++++++++++++- |
| M | gbdk-lib/libc/targets/sm83/duck/megaduck_laptop_io.c | 29 | ++++++++++++++++++++--------- |
5 files changed, 75 insertions(+), 17 deletions(-)
diff --git a/gbdk-lib/examples/megaduck/laptop_keyboard/src/megaduck_key2ascii.c b/gbdk-lib/examples/megaduck/laptop_keyboard/src/megaduck_key2ascii.c @@ -254,7 +254,7 @@ const char scancode_to_ascii_LUT_spanish[] = { NO_KEY, // 0xDB SYS_KBD_CODE_PIANO_RE_2 '-', // 0xDC SYS_KBD_CODE_DASH KEY_ARROW_DOWN, // 0xDD SYS_KBD_CODE_ARROW_DOWN // TODO: Temporary - NO_KEY, // 0xDE SYS_KBD_CODE_PRINTSCREEN_RIGHT + KEY_PRINTSCREEN, // 0xDE SYS_KBD_CODE_PRINTSCREEN_RIGHT // TODO: Temporary NO_KEY, // 0xDF KEY_DELETE, // 0xE0 SYS_KBD_CODE_DELETE diff --git a/gbdk-lib/examples/megaduck/laptop_keyboard/src/megaduck_keyboard.c b/gbdk-lib/examples/megaduck/laptop_keyboard/src/megaduck_keyboard.c @@ -16,6 +16,7 @@ bool key_repeat_allowed = false; uint8_t key_repeat_timeout = REPEAT_OFF; uint8_t key_pressed = NO_KEY; +uint8_t key_repeat_previous = NO_KEY; uint8_t key_previous = NO_KEY; @@ -58,6 +59,8 @@ bool duck_io_poll_keyboard(duck_keyboard_data_t * key_data) { // Returns translated key (if no key pressed, invalid, etc value will be NO_KEY) char duck_io_process_key_data(duck_keyboard_data_t * key_data, uint8_t megaduck_model) { + key_previous = key_pressed; + // Optional program layer of key repeat on top of // hardware key repeat (which is too fast, mostly) // @@ -74,7 +77,7 @@ char duck_io_process_key_data(duck_keyboard_data_t * key_data, uint8_t megaduck_ } else { // If there is a repeat then send the previous pressed key // and set a small delay until next repeat - key_pressed = key_previous; + key_pressed = key_repeat_previous; key_repeat_timeout = REPEAT_CONTINUE_THRESHOLD; } } @@ -93,6 +96,11 @@ char duck_io_process_key_data(duck_keyboard_data_t * key_data, uint8_t megaduck_ if ((key_pressed >= 'a') && (key_pressed <= 'z')) key_pressed -= ('a' - 'A'); + // Allow PrintScreen flag to override any other key pressed + // (Left printscreen is a flag, Right one is an actual key) + if (key_data->flags & DUCK_IO_KEY_FLAG_PRINTSCREEN_LEFT) + key_pressed = KEY_PRINTSCREEN; + // Only allow repeat for the range from: // - ASCII 32 (space) and higher // - As well as arrow keys @@ -104,7 +112,7 @@ char duck_io_process_key_data(duck_keyboard_data_t * key_data, uint8_t megaduck_ key_repeat_allowed = false; // Save key for repeat - key_previous = key_pressed; + key_repeat_previous = key_pressed; } return key_pressed; diff --git a/gbdk-lib/examples/megaduck/laptop_keyboard/src/megaduck_keyboard.h b/gbdk-lib/examples/megaduck/laptop_keyboard/src/megaduck_keyboard.h @@ -15,6 +15,7 @@ #define KEY_ARROW_RIGHT 3u #define KEY_ARROW_LEFT 4u #define KEY_HELP 5u +#define KEY_PRINTSCREEN 6u // Use ascii values for these keys @@ -24,7 +25,7 @@ #define KEY_DELETE 127u -// RTC data +// Keyboard data typedef struct duck_keyboard_data_t { uint8_t flags; uint8_t scancode; @@ -32,9 +33,8 @@ typedef struct duck_keyboard_data_t { // Post-Processed key data -extern char megaduck_key_pressed; -extern char megaduck_key_previous; -extern uint8_t megaduck_key_flags; +extern uint8_t key_pressed; +extern uint8_t key_previous; bool duck_io_poll_keyboard(duck_keyboard_data_t * key_data); diff --git a/gbdk-lib/include/duck/laptop_io.h b/gbdk-lib/include/duck/laptop_io.h @@ -1,5 +1,6 @@ #include <gbdk/platform.h> #include <stdint.h> +#include <stdbool.h> /** @file duck/laptop_io.h @@ -34,9 +35,10 @@ #define DUCK_IO_CMD_ABORT_OR_FAIL 0x04u #define DUCK_IO_CMD_PLAY_SPEECH 0x05u #define DUCK_IO_CMD_RUN_CART_IN_SLOT 0x08u -#define DUCK_IO_CMD_INIT_UNKNOWN_0x09 0x09u /**< May also be PrintScreen related */ +#define DUCK_IO_CMD_PRINT_INIT_MAYBE_EXT_IO 0x09u /**< Command to init the printer and return status + model type */ #define DUCK_IO_CMD_SET_RTC 0x0Bu /**< Command to set hardware RTC by sending a multi-byte packet */ #define DUCK_IO_CMD_GET_RTC 0x0Cu /**< Command to get hardware RTC by receiving a a multi-byte packet */ +#define DUCK_IO_CMD_PRINT_SEND_BYTES 0x11u /**< Send printer data */ // #define FF60_REG_BEFORE_XFER 0x00u @@ -52,6 +54,7 @@ #define DUCK_IO_LEN_RTC_SET 8u /**< Set RTC payload size: 8 bytes Payload (excludes 1 length header byte, 1 byte Checksum) */ #define DUCK_IO_LEN_PLAY_SPEECH 1u /**< Play Speech payload size: 1 byte Payload (excludes 1 length header byte, 1 byte Checksum) */ +#define DUCK_IO_REPLY_NO_CART_IN_SLOT 06u // #define MEGADUCK_KBD_BYTE_1_EXPECT 0x0Eu // #define MEGADUCK_SIO_BOOT_OK 0x01u @@ -85,6 +88,23 @@ #define DUCK_IO_KBD_KEYCODE 1u +// Printer init reply related +// Init Reply Bit.0 +#define DUCK_IO_PRINTER_INIT_MASK 0x01u // Bit.0 +#define DUCK_IO_PRINTER_INIT_OK 0x01u +#define DUCK_IO_PRINTER_INIT_FAIL 0x00u + +// Init Reply Bit.1 +#define DUCK_IO_PRINTER_TYPE_MASK 0x02u // Bit.1 +#define DUCK_IO_PRINTER_TYPE_2_PASS 0x00u // Bit.1 = 0 // 13 x 12 byte packets + 1 x 5 or 6 byte packet (with CR and/or LF) +#define DUCK_IO_PRINTER_TYPE_1_PASS 0x02u // Bit.1 = 1 // 3 x 12 byte packets + 118 non-packet bytes + + + +extern volatile bool duck_io_rx_byte_done; +extern volatile uint8_t duck_io_rx_byte; + + // TODO: change these to user supplied buffers? extern uint8_t duck_io_rx_buf[DUCK_IO_LEN_RX_MAX]; extern uint8_t duck_io_rx_buf_len; @@ -148,6 +168,25 @@ void duck_io_enable_read_byte(void); bool duck_io_laptop_init(void); +/** Returns whether the MegaDuck Printer was detected during duck_io_laptop_init() + + Returns `true` if successful, otherwise `false` + + @ref duck_io_laptop_init() must be called first +*/ +bool duck_io_printer_detected(void); + + +/** Returns which type of MegaDuck Printer was detected during duck_io_laptop_init() + + Return value should be one of the following: + \li @ref DUCK_IO_PRINTER_TYPE_1_PASS (single pass monochrome) + \li @ref DUCK_IO_PRINTER_TYPE_2_PASS (two pass possibly supports shades of grey) + + @ref duck_io_laptop_init() must be called first +*/ +uint8_t duck_io_printer_type(void); + // ===== Higher level IO functions ===== diff --git a/gbdk-lib/libc/targets/sm83/duck/megaduck_laptop_io.c b/gbdk-lib/libc/targets/sm83/duck/megaduck_laptop_io.c @@ -10,16 +10,16 @@ // volatile SFR __at(0xFF60) FF60_REG; // TODO: namespace to megaduck -static volatile bool duck_io_rx_byte_done; -static volatile uint8_t duck_io_rx_byte; +volatile bool duck_io_rx_byte_done; +volatile uint8_t duck_io_rx_byte; - uint8_t duck_io_rx_buf[DUCK_IO_LEN_RX_MAX]; - uint8_t duck_io_rx_buf_len; + uint8_t duck_io_rx_buf[DUCK_IO_LEN_RX_MAX]; + uint8_t duck_io_rx_buf_len; - uint8_t duck_io_tx_buf[DUCK_IO_LEN_TX_MAX]; - uint8_t duck_io_tx_buf_len; + uint8_t duck_io_tx_buf[DUCK_IO_LEN_TX_MAX]; + uint8_t duck_io_tx_buf_len; - uint8_t serial_cmd_0x09_reply_data; // In original hardware it's requested, but used for nothing? + uint8_t duck_io_priniter_init_result = DUCK_IO_PRINTER_INIT_FAIL; // Default printer to not connected static void _delay_1_msec(void); @@ -305,13 +305,13 @@ bool duck_io_laptop_init(void) { if (duck_io_init_ok) { // Save response from some command // (so far not seen being used in System ROM 32K Bank 0) - duck_io_send_byte(DUCK_IO_CMD_INIT_UNKNOWN_0x09); + duck_io_send_byte(DUCK_IO_CMD_PRINT_INIT_MAYBE_EXT_IO); // TODO: This wait with no timeout is how the System ROM does it, // but it can probably be changed to a long delay and // attempt to fail somewhat gracefully. duck_io_read_byte_no_timeout(); - serial_cmd_0x09_reply_data = duck_io_rx_byte; + duck_io_priniter_init_result = duck_io_rx_byte; } // Ignore the RTC init check for now @@ -321,3 +321,14 @@ bool duck_io_laptop_init(void) { return (duck_io_init_ok); } + + +bool duck_io_printer_detected(void) { + return (duck_io_priniter_init_result & DUCK_IO_PRINTER_INIT_OK); +} + + +uint8_t duck_io_printer_type(void) { + return (duck_io_priniter_init_result & DUCK_IO_PRINTER_TYPE_MASK); +} +
This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.