Import Ebitor 0.1.1 upstream source snapshot
Original author: Pierre Sarrazin. Source: http://sarrazip.com/dev/ebitor-0.1.1.tar.gz Archive SHA-256: 6911a9d2ee80925622f1d82db1b77710a1f896865e63d29a04dc16489b150e83
This commit is contained in:
commit
53bf6abdd5
19 files changed
+5400
No files matched your search
@@ -0,0 +1,318 @@
|
||||
/* sg4term.c - CoCo 32x16 SG4 mode (6847) terminal functions.
|
||||
|
||||
Copyright (C) 2021-2025 Pierre Sarrazin <http://sarrazip.com/>
|
||||
|
||||
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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "sg4term.h"
|
||||
|
||||
#include "Ebitor_private.h"
|
||||
|
||||
|
||||
enum
|
||||
{
|
||||
sg4term_SPACE_CODE = 0x60,
|
||||
};
|
||||
|
||||
|
||||
struct Ebitor;
|
||||
|
||||
|
||||
static byte sg4term_width, sg4term_height; // max 32x16
|
||||
static byte sg4term_winLeftCol, sg4term_winTopRow; // 0..31, 0..15
|
||||
static char *sg4term_screenBuffer; // address of 32x16 screen buffer
|
||||
static byte sg4term_cursorCol, sg4term_cursorRow; // lower than sg4term_winLeftCol, sg4term_winTopRow
|
||||
static byte (*sg4term_inkeyFuncPtr)(void *cookie); // returns 0 if no key pressed
|
||||
static void *sg4term_inkeyFuncCookie; // argument passed to sg4term_inkeyFuncPtr
|
||||
static word *sg4term_timerPtr; // points to a word that gets incremented 60 times per second
|
||||
static char sg4term_attributeInversionFlag;
|
||||
|
||||
|
||||
static char *
|
||||
getPosInWindow(byte col, byte row)
|
||||
{
|
||||
return sg4term_screenBuffer + ((word) (sg4term_winTopRow + row) << 5) + (sg4term_winLeftCol + col);
|
||||
}
|
||||
|
||||
|
||||
#define getCursorAddress() (getPosInWindow(sg4term_cursorCol, sg4term_cursorRow))
|
||||
|
||||
|
||||
static void
|
||||
sg4term_clearRowsAt(char *rowStartDest, byte numRows)
|
||||
{
|
||||
char value = sg4term_SPACE_CODE ^ sg4term_attributeInversionFlag;
|
||||
for (byte i = numRows; i--; rowStartDest += 32) // for each window row to clear
|
||||
memset(rowStartDest, value, sg4term_width);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
sg4term_clear(void)
|
||||
{
|
||||
sg4term_clearRowsAt(getPosInWindow(0, 0), sg4term_height);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
sg4term_init(char *screenBuffer, const ebitor_Window *window,
|
||||
byte (*inkeyFuncPtr)(void *), void *inkeyFuncCookie, word *timerPtr)
|
||||
{
|
||||
sg4term_screenBuffer = screenBuffer;
|
||||
sg4term_width = window->width;
|
||||
sg4term_height = window->height;
|
||||
sg4term_winLeftCol = window->leftCol;
|
||||
sg4term_winTopRow = window->topRow;
|
||||
sg4term_cursorCol = window->cursorCol;
|
||||
sg4term_cursorRow = window->cursorRow;
|
||||
sg4term_inkeyFuncPtr = inkeyFuncPtr;
|
||||
sg4term_inkeyFuncCookie = inkeyFuncCookie;
|
||||
sg4term_timerPtr = timerPtr;
|
||||
sg4term_attributeInversionFlag = 0;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
sg4term_locate(byte column, byte row)
|
||||
{
|
||||
sg4term_cursorCol = column;
|
||||
sg4term_cursorRow = row;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
sg4term_getCursorPos(byte *column, byte *row)
|
||||
{
|
||||
*column = sg4term_cursorCol;
|
||||
*row = sg4term_cursorRow;
|
||||
}
|
||||
|
||||
|
||||
static BOOL
|
||||
sg4term_newline(void)
|
||||
{
|
||||
sg4term_cursorCol = 0; // back to start of window row
|
||||
if (++sg4term_cursorRow >= sg4term_height)
|
||||
{
|
||||
--sg4term_cursorRow;
|
||||
sg4term_cursorCol = 0; // back to start of last row (avoid writing outside window)
|
||||
return FALSE; // reached end of window
|
||||
}
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
||||
asm char
|
||||
sg4term_convertASCIIToScreenByte(char c)
|
||||
{
|
||||
asm
|
||||
{
|
||||
ldb 3,s // low byte of 'c' (big endian)
|
||||
cmpb #64
|
||||
blo @typo // typographical char
|
||||
cmpb #96
|
||||
blo @done // upper-case letter
|
||||
cmpb #128
|
||||
bhs @done // graphical char
|
||||
subb #96 // lower-case letter
|
||||
bra @store
|
||||
@typo
|
||||
addb #64
|
||||
@store
|
||||
stb 3,s
|
||||
@done
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// CAUTION: This function does NOT scroll when it reaches the end of the window.
|
||||
// Character 13 does a carriage return and line feed AND clears
|
||||
// the rest of the row it is applied to (with spaces).
|
||||
// No other control code is supported.
|
||||
//
|
||||
static BOOL
|
||||
sg4term_putchar(int c)
|
||||
{
|
||||
char *cursor = getCursorAddress();
|
||||
if (c == '\r')
|
||||
{
|
||||
char charToWrite = sg4term_SPACE_CODE ^ sg4term_attributeInversionFlag;
|
||||
for ( ; sg4term_cursorCol < sg4term_width; ++sg4term_cursorCol)
|
||||
*cursor++ = charToWrite;
|
||||
return sg4term_newline();
|
||||
}
|
||||
|
||||
*cursor++ = sg4term_convertASCIIToScreenByte((char) c) ^ sg4term_attributeInversionFlag;
|
||||
if (++sg4term_cursorCol >= sg4term_width) // if past end of window row
|
||||
return sg4term_newline();
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
||||
BOOL
|
||||
sg4term_print(const char *str)
|
||||
{
|
||||
for ( ; *str; ++str)
|
||||
if (!sg4term_putchar(*str))
|
||||
return FALSE; // reached end of window
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
||||
BOOL
|
||||
sg4term_printPrefix(const char *str, size_t prefixLength)
|
||||
{
|
||||
for (size_t i = 0; i < prefixLength; ++i)
|
||||
if (!sg4term_putchar(str[i]))
|
||||
return FALSE; // reached end of window
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
sg4term_invertColors(void)
|
||||
{
|
||||
sg4term_attributeInversionFlag ^= 0x40;
|
||||
}
|
||||
|
||||
|
||||
// Does not move the cursor.
|
||||
//
|
||||
void
|
||||
sg4term_clrtoeol(void)
|
||||
{
|
||||
memset(getCursorAddress(), sg4term_SPACE_CODE ^ sg4term_attributeInversionFlag, sg4term_width - sg4term_cursorCol);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
sg4term_scrollWindowUp(byte numRows)
|
||||
{
|
||||
if (numRows == 0)
|
||||
return;
|
||||
|
||||
char *rowStartDest = getPosInWindow(0, 0); // top window row
|
||||
const char *rowStartSrc = rowStartDest + ((word) numRows << 5);
|
||||
|
||||
for (byte i = sg4term_height - numRows; i--; rowStartSrc += 32, rowStartDest += 32) // for each window row to scroll
|
||||
memmove(rowStartDest, rowStartSrc, sg4term_width);
|
||||
sg4term_clearRowsAt(rowStartDest, numRows);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
sg4term_scrollWindowDown(byte numRows)
|
||||
{
|
||||
if (numRows == 0)
|
||||
return;
|
||||
|
||||
char *rowStartDest = getPosInWindow(0, sg4term_height - 1); // bottom window row
|
||||
const char *rowStartSrc = rowStartDest - ((word) numRows << 5);
|
||||
|
||||
for (byte i = sg4term_height - numRows; i--; rowStartSrc -= 32, rowStartDest -= 32) // for each window row to scroll
|
||||
memmove(rowStartDest, rowStartSrc, sg4term_width);
|
||||
byte c = sg4term_SPACE_CODE ^ sg4term_attributeInversionFlag;
|
||||
for (byte i = numRows; i--; rowStartDest -= 32) // for each window row to clear
|
||||
memset(rowStartDest, c, sg4term_width);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
sg4term_eraseCurrentLine(void)
|
||||
{
|
||||
memset(getPosInWindow(0, sg4term_cursorRow), sg4term_SPACE_CODE ^ sg4term_attributeInversionFlag, sg4term_width);
|
||||
sg4term_cursorCol = 0;
|
||||
}
|
||||
|
||||
|
||||
word
|
||||
sg4term_waitEditorKey(void)
|
||||
{
|
||||
char *cursor = getCursorAddress();
|
||||
for (;;)
|
||||
{
|
||||
byte origByteOnScreen = *cursor;
|
||||
|
||||
// Wait for key with animated cursor if sg4term_timerPtr is not null.
|
||||
// Otherwise, wait for key with inverted character at cursor.
|
||||
byte key;
|
||||
if (sg4term_timerPtr == NULL)
|
||||
{
|
||||
*cursor = origByteOnScreen ^ 0x40;
|
||||
do
|
||||
key = (*sg4term_inkeyFuncPtr)(sg4term_inkeyFuncCookie);
|
||||
while (key == 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
for (word startTime = *sg4term_timerPtr; ; )
|
||||
{
|
||||
key = (*sg4term_inkeyFuncPtr)(sg4term_inkeyFuncCookie);
|
||||
if (key)
|
||||
break;
|
||||
*cursor = origByteOnScreen ^ (((*sg4term_timerPtr - startTime) & 0x38) == 0x38 ? 0x00 : 0x40);
|
||||
}
|
||||
}
|
||||
|
||||
*cursor = origByteOnScreen;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static char
|
||||
sg4term_convertScreenByteToASCII(char b)
|
||||
{
|
||||
if ((byte) b >= 128)
|
||||
return 0; // 128..255: no equivalent in ASCII
|
||||
if (b < 32)
|
||||
return b + 96;
|
||||
if (b < 64) // 32..63
|
||||
return 0; // no equivalent in ASCII
|
||||
if (b < 96) // 64..95
|
||||
return b;
|
||||
return b - 64; // 96..127
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
sg4term_getChars(byte column, byte row, char *dest, size_t len)
|
||||
{
|
||||
assert(column < sg4term_width && row < sg4term_height);
|
||||
const char *p = getPosInWindow(column, row);
|
||||
for (size_t i = len; i--; )
|
||||
*dest++ = sg4term_convertScreenByteToASCII(*p++);
|
||||
}
|
||||
Reference in new issue
Block a user