/* gimeterm.c - CoCo 3 GIME hardware text mode terminal functions. Copyright (C) 2021-2025 Pierre Sarrazin This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ #include "gimeterm.h" #include "Ebitor.h" struct Ebitor; static byte gimeterm_width, gimeterm_height; // max 80x24 static byte gimeterm_winLeftCol, gimeterm_winTopRow; // 0..79, 0..24 static word *gimeterm_screenBuffer; // address of 40/64/80x24/25 screen buffer static byte gimeterm_screenColumns; // 40, 64 or 80 (regardless of gimeterm_width) static byte gimeterm_cursorCol, gimeterm_cursorRow; // lower than gimeterm_width, gimeterm_cursorRow static byte (*gimeterm_inkeyFuncPtr)(void *cookie); // returns 0 if no key pressed static void *gimeterm_inkeyFuncCookie; // passed to gimeterm_inkeyFuncPtr static word *gimeterm_timerPtr; // points to a word that gets incremented 60 times per second static byte gimeterm_textAttribute; static byte gimeterm_invertedTextAttribute; static word * getPosInWindow(byte col, byte row) { return gimeterm_screenBuffer + ((word) (gimeterm_winTopRow + row) * gimeterm_screenColumns + gimeterm_winLeftCol + col); } #define getCursorAddress() (getPosInWindow(gimeterm_cursorCol, gimeterm_cursorRow)) static void gimeterm_clearRowsAt(word *rowStartDest, byte numRows) { word w = 0x2000 | gimeterm_textAttribute; for (byte i = numRows; i--; rowStartDest += gimeterm_screenColumns) // for each window row to clear memset16(rowStartDest, w, gimeterm_width); } void gimeterm_clear(void) { gimeterm_clearRowsAt(getPosInWindow(0, 0), gimeterm_height); } void gimeterm_init(void *screenBuffer, byte screenMode, const gimeterm_Window *window, byte (*inkeyFuncPtr)(void *), void *inkeyFuncCookie, word *timerPtr) { gimeterm_screenBuffer = (word *) screenBuffer; gimeterm_screenColumns = screenMode; gimeterm_inkeyFuncPtr = inkeyFuncPtr; gimeterm_inkeyFuncCookie = inkeyFuncCookie; gimeterm_timerPtr = timerPtr; gimeterm_setWindow(window); } void gimeterm_setWindow(const gimeterm_Window *window) { gimeterm_width = window->window.width; gimeterm_height = window->window.height; gimeterm_winLeftCol = window->window.leftCol; gimeterm_winTopRow = window->window.topRow; gimeterm_cursorCol = window->window.cursorCol; gimeterm_cursorRow = window->window.cursorRow; gimeterm_textAttribute = window->textAttribute; gimeterm_invertedTextAttribute = window->invertedTextAttribute; } void gimeterm_getWindow(gimeterm_Window *dest) { dest->window.width = gimeterm_width; dest->window.height = gimeterm_height; dest->window.leftCol = gimeterm_winLeftCol; dest->window.topRow = gimeterm_winTopRow; dest->window.cursorCol = gimeterm_cursorCol; dest->window.cursorRow = gimeterm_cursorRow; dest->textAttribute = gimeterm_textAttribute; dest->invertedTextAttribute = gimeterm_invertedTextAttribute; } void gimeterm_locate(byte column, byte row) { gimeterm_cursorCol = column; gimeterm_cursorRow = row; } void gimeterm_getCursorPos(byte *column, byte *row) { *column = gimeterm_cursorCol; *row = gimeterm_cursorRow; } BOOL gimeterm_newline(void) { gimeterm_cursorCol = 0; // back to start of window row if (++gimeterm_cursorRow >= gimeterm_height) { --gimeterm_cursorRow; gimeterm_cursorCol = 0; // back to start of last row (avoid writing outside window) return FALSE; // reached end of window } return TRUE; } BOOL gimeterm_putchar(int c) { word *cursor = getCursorAddress(); if (c == '\r') { word w = 0x2000 | gimeterm_textAttribute; for ( ; gimeterm_cursorCol < gimeterm_width; ++gimeterm_cursorCol) *cursor++ = w; return gimeterm_newline(); } ((byte *) cursor)[0] = (byte) c; ((byte *) cursor)[1] = gimeterm_textAttribute; if (++gimeterm_cursorCol >= gimeterm_width) // if past end of window row return gimeterm_newline(); return TRUE; } BOOL gimeterm_print(const char *str) { for ( ; *str; ++str) if (!gimeterm_putchar(*str)) return FALSE; // reached end of window return TRUE; } BOOL gimeterm_printPrefix(const char *str, size_t prefixLength) { for (size_t i = 0; i < prefixLength; ++i) if (!gimeterm_putchar(str[i])) return FALSE; // reached end of window return TRUE; } // Use inverted colors for the next print operations. // void gimeterm_invertColors(void) { byte temp = gimeterm_textAttribute; gimeterm_textAttribute = gimeterm_invertedTextAttribute; gimeterm_invertedTextAttribute = temp; } // Does not move the cursor. // void gimeterm_clrtoeol(void) { memset16(getCursorAddress(), 0x2000 | gimeterm_textAttribute, gimeterm_width - gimeterm_cursorCol); } void gimeterm_scrollWindowUp(byte numRows) { if (numRows == 0) return; word *rowStartDest = getPosInWindow(0, 0); // top window row const word *rowStartSrc = rowStartDest + ((word) numRows * gimeterm_screenColumns); size_t bytesPerWindowRow = gimeterm_width << 1; for (byte i = gimeterm_height - numRows; i--; rowStartSrc += gimeterm_screenColumns, rowStartDest += gimeterm_screenColumns) // for each window row to scroll memcpy(rowStartDest, rowStartSrc, bytesPerWindowRow); gimeterm_clearRowsAt(rowStartDest, numRows); } void gimeterm_scrollWindowDown(byte numRows) { if (numRows == 0) return; word *rowStartDest = getPosInWindow(0, gimeterm_height - 1); // bottom window row const word *rowStartSrc = rowStartDest - ((word) numRows * gimeterm_screenColumns); size_t bytesPerWindowRow = gimeterm_width << 1; for (byte i = gimeterm_height - numRows; i--; rowStartSrc -= gimeterm_screenColumns, rowStartDest -= gimeterm_screenColumns) // for each window row to scroll memmove(rowStartDest, rowStartSrc, bytesPerWindowRow); const word w = 0x2000 | gimeterm_textAttribute; for (byte i = numRows; i--; rowStartDest -= gimeterm_screenColumns) // for each window row to clear memset16(rowStartDest, w, gimeterm_width); } void gimeterm_eraseCurrentLine(void) { memset16(getPosInWindow(0, gimeterm_cursorRow), 0x2000 | gimeterm_textAttribute, gimeterm_width); gimeterm_cursorCol = 0; } // Animates a cursor using the timerPtr passed to gimeterm_init(). // Uses the inkeyFuncPtr and inkeyFuncCookie passed to gimeterm_init() to poll the keyboard. // attributeByte: Address of a GIME 40/80-column screen attribute byte. // cursorAttribute: Byte to put at *attributeByte to animate the cursor. // Returns the key pressed, without interpretation. // static byte gimeterm_waitKeyAtPosition(byte *attributeByte, byte cursorAttribute) { byte origByteOnScreen = *attributeByte; // Wait for key with animated cursor. byte key; for (word startTime = *gimeterm_timerPtr; ; ) { key = (*gimeterm_inkeyFuncPtr)(gimeterm_inkeyFuncCookie); if (key) break; *attributeByte = (((*gimeterm_timerPtr - startTime) & 0x38) == 0x38 ? origByteOnScreen : cursorAttribute); } *attributeByte = origByteOnScreen; return key; } byte gimeterm_waitKey(void) { byte *cursor = (byte *) getCursorAddress() + 1; // point to attribute byte return gimeterm_waitKeyAtPosition(cursor, gimeterm_invertedTextAttribute); } word gimeterm_waitEditorKey(void) { for (;;) { byte key = gimeterm_waitKey(); switch (key) { case 3: // Break return ebitor_KEY_ESCAPE; case 8: // left arrow return ebitor_KEY_ARROW_LEFT; case 9: // right arrow return ebitor_KEY_ARROW_RIGHT; case '^': // up arrow return ebitor_KEY_ARROW_UP; case 10: // down arrow return ebitor_KEY_ARROW_DOWN; case 12: // Clear used as backspace return '\b'; // backspace case 13: // Enter return '\r'; default: if (key >= ' ' && key != 127) return key; } } } byte gimeterm_setTextAttribute(byte newAttribute) { byte orig = gimeterm_textAttribute; gimeterm_textAttribute = newAttribute; return orig; } byte gimeterm_setInvertedTextAttribute(byte newAttribute) { byte orig = gimeterm_invertedTextAttribute; gimeterm_invertedTextAttribute = newAttribute; return orig; }