Import coco-letters 0.1.0 upstream source snapshot

Original author: Pierre Sarrazin. Preserve upstream GPLv3-or-later and file-specific notices.
Source: http://sarrazip.com/dev/coco-letters-0.1.0.tar.gz
Archive SHA-256: ad7c7771845e1a35014644fb562634a558abee632e1d7738683db6e9e9e46b94
This commit is contained in:
Brian Holman committed 2026-11-09 23:39:21 -07:00
commit 4cedb2d94a
29 files changed
+20169

No files matched your search

+528
View File
@@ -0,0 +1,528 @@
/* graphics.c
Copyright (C) 2026 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 "graphics.h"
// Uncomment to send printf() et al. to the Becker port
// instead of the lower-right text window.
//
//#define TRACES_TO_BECKER_PORT
static const byte rgbPaletteValues[NUM_COLORS] =
{
#define PALETTE_ELEMENT(name, rgbCode, cmpCode) rgbCode,
#include "palette.h"
#undef PALETTE_ELEMENT
};
static const byte cmpPaletteValues[NUM_COLORS] =
{
#define PALETTE_ELEMENT(name, rgbCode, cmpCode) cmpCode,
#include "palette.h"
#undef PALETTE_ELEMENT
};
// Points to either rgbPaletteValues[] or cmpPaletteValues[].
//
static const byte *currentPaletteValues = rgbPaletteValues;
static const byte colorIndexToByte[NUM_COLORS] =
{
0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
};
static struct
{
byte curRow; // 0..TEXT_SCREEN_HEIGHT-1
byte curCol; // 0..TEXT_SCREEN_WIDTH-1
} textWindow;
// color: Palette index (0..15).
//
void
hcls(byte color)
{
byte b = colorIndexToByte[color];
memset16(GRAPHICS_START, ((word) b << 8) | b, NUM_GRAPHICS_BLOCKS * BLOCK_SIZE / 2);
}
void consoleOutHookGraphics(void);
static void
setPaletteFromCurrentArray(void)
{
memcpy((void *) 0xFFB0, currentPaletteValues, NUM_COLORS);
poke(0xFF9A, currentPaletteValues[PAL_SCREEN_BG]); // border color (0..63)
}
void
setPalette(BOOL monitorIsRGB)
{
currentPaletteValues = (monitorIsRGB ? rgbPaletteValues : cmpPaletteValues);
setPaletteFromCurrentArray();
}
void
alternatePalettes(void)
{
setPalette(currentPaletteValues == cmpPaletteValues);
}
void
showGraphicsMode(void)
{
setPaletteFromCurrentArray();
poke(0xFF90, 0x4C); // reset CoCo 2 compatible bit
poke(0xFF98, 0x80); // graphics mode
poke(0xFF99, 0b00011010); // 256x192, 16 colors
// Tell the GIME chip that the memory at FIRST_GRAPHICS_BLOCK is the screen.
pokeWord(0xFF9D, (word) FIRST_GRAPHICS_BLOCK << 10);
// Redirect printf() et al. to the software text screen.
setConsoleOutHook(consoleOutHookGraphics);
}
void
setupGraphicsMode(void)
{
for (byte i = 0; i < NUM_GRAPHICS_BLOCKS; ++i)
mmuSet(FIRST_GRAPHICS_BANK + i, FIRST_GRAPHICS_BLOCK + i);
hcls(PAL_SCREEN_BG); // empty the screen before showing it
textWindow.curRow = TEXT_WIN_TOP;
textWindow.curCol = TEXT_WIN_LEFT;
}
asm byte *
getPixelAddr(byte x, byte y)
{
#if 0 /* Original C code. */
return (byte *) (GRAPHICS_START + y * (word) BYTES_PER_ROW + (x / 2));
#else
asm
{
; 'x' is in B, as per __gcccall.
lsrb ; x / 2
ldx #GRAPHICS_START
abx
ldb 3,s ; y
lda #BYTES_PER_ROW
mul ; D = y * (word) BYTES_PER_ROW
leax d,x ; add to GRAPHICS_START + x / 2
; Return value left in X as per __gcccall.
}
#endif
}
static const byte sixteenColorModeMasks[2] = { 0b00001111, 0b11110000 };
// Expensive if used to set many pixels.
// Assumes 256x192x16.
//
void
setPixel(byte x, byte y, byte color)
{
byte mask = sixteenColorModeMasks[x & 1]; // the zero bits are where 'color' will go
byte *changedByte = getPixelAddr(x, y);
#if 0 //ndef NDEBUG
if (changedByte < GRAPHICS_START || changedByte >= GRAPHICS_END)
{
printf("!setPixel(%u,%u,%u):%p,%p,%p.", x, y, color, changedByte, GRAPHICS_START, GRAPHICS_END);
for (;;)
;
}
#endif
byte colorByte = (x & 1 ? color : (color << 4));
*changedByte = *changedByte & mask | colorByte;
}
byte
getPixel(byte x, byte y)
{
byte mask = sixteenColorModeMasks[x & 1];
byte *targetByte = getPixelAddr(x, y);
return x & 1 ? *targetByte & 0x0F : (*targetByte >> 4);
}
void
hline(byte left, byte right, byte row, byte colorIndex)
{
const byte oddCol = left & 1;
byte mask = sixteenColorModeMasks[oddCol]; // the zero bits are where 'colorIndex' will go
byte *changedByte = getPixelAddr(left, row);
const byte evenColorByte = colorIndex << 4;
const byte oddColorByte = colorIndex;
const byte colorByteXorValue = oddColorByte ^ evenColorByte;
byte colorByte = (oddCol ? oddColorByte : evenColorByte);
for ( ; left <= right; ++left)
{
*changedByte = *changedByte & mask | colorByte;
if (left & 1) // if just processed odd horiz. coord
++changedByte; // then next pixel is in next byte
mask = ~mask; // alternate between 0xF0 and 0x0F
colorByte ^= colorByteXorValue; // alternate betweenoddColorByte and evenColorByte
}
}
void
vline(byte top, byte bottom, byte column, byte colorIndex)
{
assert(top < SCRN_HEIGHT);
assert(bottom < SCRN_HEIGHT);
assert(colorIndex < NUM_COLORS);
const byte oddCol = column & 1;
const byte mask = sixteenColorModeMasks[oddCol]; // the zero bits are where 'colorIndex' will go
const byte colorByte = (oddCol ? colorIndex : (colorIndex << 4));
byte *changedByte = getPixelAddr(column, top);
for ( ; top <= bottom; ++top, changedByte += BYTES_PER_ROW)
*changedByte = *changedByte & mask | colorByte;
}
void
draw6ByteWideRectangle(byte leftPosInBytes,
byte upperRowInPixels,
byte heightInPixels,
byte colorIndex)
{
byte byteToWrite = colorIndexToByte[colorIndex];
word wordToWrite = ((word) byteToWrite << 8) | byteToWrite;
byte *bytePtr = (byte *) (GRAPHICS_START + upperRowInPixels * (word) BYTES_PER_ROW + leftPosInBytes);
for (byte row = 0; row < heightInPixels; ++row, bytePtr += BYTES_PER_ROW)
{
* (word *) bytePtr = wordToWrite;
* (word *) (bytePtr + 2) = wordToWrite;
* (word *) (bytePtr + 4) = wordToWrite;
}
}
void
drawRect(byte leftPosInPixels,
byte upperRowInPixels,
byte widthInPixels,
byte heightInPixels,
byte colorIndex)
{
byte right = leftPosInPixels + widthInPixels - 1;
for ( ; heightInPixels--; ++upperRowInPixels)
hline(leftPosInPixels, right, upperRowInPixels, colorIndex);
}
void
drawBox(byte leftX, byte topY, byte rightX, byte bottomY, byte colorIndex)
{
hline(leftX, rightX, topY, colorIndex);
hline(leftX, rightX, bottomY, colorIndex);
vline(topY, bottomY, leftX, colorIndex);
vline(topY, bottomY, rightX, colorIndex);
}
// Colors currently used by writeCharEx().
//
static byte foreColorByte = PAL_PROMPTS | (PAL_PROMPTS << 4);
static byte backColorByte = PAL_SCREEN_BG | (PAL_SCREEN_BG << 4);
// Uses foreColorByte and backColorByte.
//
static void
writeCharEx(const byte font[], byte numRows, byte numColumns,
byte x, byte y,
word glyphIndex)
{
const byte *glyph = &font[glyphIndex << 3];
const byte firstMaskInRow = sixteenColorModeMasks[x & 1];
byte *changedByte = getPixelAddr(x, y);
for (byte row = 0; row < numRows; ++row)
{
byte glyphByte = glyph[row];
byte mask = firstMaskInRow; // the zero bits are where the 4-bit pixel will go
byte *firstChangedByteInRow = changedByte;
for (byte col = 0; col < numColumns; ++col, glyphByte <<= 1)
{
byte colorByte = (glyphByte & 0x80) ? backColorByte : foreColorByte; // 2 nybbles set
*changedByte = *changedByte & mask | (colorByte & ~mask);
mask = ~mask; // 11110000 <-> 00001111: next pixel will go in other nybble
if (mask == 0b00001111) // if next pixel will go in high nybble
++changedByte; // next pixel will go in next byte
}
changedByte = firstChangedByteInRow + BYTES_PER_ROW;
}
}
void
writeTileCharAtPixelPosWithColors(byte x, byte y, byte tileType, byte foreColorIndex, byte backColorIndex)
{
byte f = foreColorByte, b = backColorByte;
foreColorByte = foreColorIndex | (foreColorIndex << 4);
backColorByte = backColorIndex | (backColorIndex << 4);
writeCharEx((const byte *) FONT_5x8_BUFFER, 7, 5, x, y, tileType - 1);
foreColorByte = f;
backColorByte = b;
}
void
writeCharAtPixelPos(byte x, byte y, char asciiCode)
{
writeCharEx((const byte *) FONT_4x8_BUFFER, CHAR_HEIGHT, CHAR_WIDTH - 1,
x, y, (byte) asciiCode - 32);
}
void
writeStringAt(byte textCol, byte textRow, const char *str)
{
byte startCol = textCol;
for ( ; *str; ++str)
{
if (*str == '\n')
{
textCol = startCol;
++textRow;
}
else
writeCharAt(textCol++, textRow, *str);
}
}
void
writeDecimalAt(byte textCol, byte textRow, unsigned value)
{
char str[6];
utoa10(value, str);
writeStringAt(textCol, textRow, str);
}
#ifndef TRACES_TO_BECKER_PORT
// N.B.: Assumes that the text window is at the right of the screen.
//
static void
scrollTextWindowUp(void)
{
byte homeX = TEXT_WIN_LEFT * CHAR_WIDTH; // in pixels
byte *p = getPixelAddr(homeX, TEXT_WIN_TOP * CHAR_HEIGHT);
byte numBytesToCopyPerRow = (byte) (256 - homeX) / 2; // assumes homeX > 0 and that text win at right of scrn
byte y;
for (y = TEXT_WIN_TOP * CHAR_HEIGHT; y < SCRN_HEIGHT - CHAR_HEIGHT; ++y, p += BYTES_PER_ROW)
{
byte *dest = p;
if (homeX & 1) // if left-most byte of text window is not fully part of text window
{
dest[0] = (dest[0] & 0b11110000) | (dest[BYTES_PER_ROW * (word) CHAR_HEIGHT] & 0b00001111);
++dest; // move on to next byte
}
memcpy(dest, dest + (BYTES_PER_ROW * (word) CHAR_HEIGHT), numBytesToCopyPerRow);
}
byte colorByte = (PAL_SCREEN_BG << 4) | PAL_SCREEN_BG;
for ( ; y < SCRN_HEIGHT; ++y, p += BYTES_PER_ROW) // clear bottom
{
byte *dest = p;
if (homeX & 1)
{
dest[0] = (dest[0] & 0b11110000) | PAL_SCREEN_BG;
++dest; // move on to next byte
}
memset(dest, colorByte, numBytesToCopyPerRow);
}
}
#endif /* TRACES_TO_BECKER_PORT */
// Called by printf() and putchar() when in graphics mode.
// The character to be printed is in register A.
// Must preserve B and X.
//
void
consoleOutHookGraphics(void)
{
#ifdef TRACES_TO_BECKER_PORT
asm
{
sta $FF42
}
#else
char ch;
asm
{
pshs x,b ; must be preserved
sta :ch ; character to write
}
byte goToNewLine = FALSE;
switch (ch)
{
case '\r':
// Write spaces until current line filled.
for (byte col = textWindow.curCol; col < TEXT_WIN_LEFT + TEXT_WIN_WIDTH; ++col)
writeCharAt(col, textWindow.curRow, ' ');
goToNewLine = TRUE;
break;
default:
if ((byte) ch >= ' ' && (byte) ch <= 127) // DEL rendered as filled rectangle
{
writeCharAt(textWindow.curCol, textWindow.curRow, ch);
if (++textWindow.curCol == TEXT_WIN_LEFT + TEXT_WIN_WIDTH) // if end of row reached
goToNewLine = TRUE;
}
}
if (goToNewLine)
{
textWindow.curCol = TEXT_WIN_LEFT;
if (textWindow.curRow < (byte) (TEXT_WIN_TOP + TEXT_WIN_HEIGHT - 1)) // if not on last row
++textWindow.curRow;
else
scrollTextWindowUp();
}
asm
{
puls b,x
}
#endif
}
void
getCurrentTextWindowPos(byte *textCol, byte *textRow)
{
*textCol = textWindow.curCol;
*textRow = textWindow.curRow;
}
static byte *hardwareTextCursor = (byte *) HW_TEXT_SCREEN_BUFFER;
// Called by printf() and putchar() when in hardware text mode.
// The character to be printed is in register A.
// Must preserve B and X.
//
// Does NOT support scrolling. The cursor must not go beyond last row.
//
static asm __norts__ void
hardwareTextScreenConsoleOut(void)
{
asm
{
pshs x,b
ldx :hardwareTextCursor
cmpa #13
beq @cr
sta ,x+
bra @done
@cr
; Begin hardwareTextCursor back to start of current row, then to start of next row.
ldd #-HW_TEXT_SCREEN_BUFFER
leax d,x ; X now offset from HW_TEXT_SCREEN_BUFFER
; Divide X by 40 by subtraction, to get the remainder (0..39)
bra @divBy40Cond
@divBy40Loop
leax -HW_TEXT_SCREEN_WIDTH,x
@divBy40Cond
cmpx #HW_TEXT_SCREEN_WIDTH
bhs @divBy40Loop ; loop if still not in first row
;
tfr x,d ; get remainder (column number) in B
negb ; negate column number
ldx :hardwareTextCursor
leax b,x ; put cursor back at start of row (signed operation)
leax HW_TEXT_SCREEN_WIDTH,x ; put cursor at start of next row
@done
stx :hardwareTextCursor
@ret
puls b,x,pc
}
}
void
showHardwareTextScreen(void)
{
// Set CoCo 3 hi res text mode.
// Must be consistent with HW_TEXT_SCREEN_WIDTH, HW_TEXT_SCREEN_HEIGHT, HW_TEXT_SCREEN_BUFFER.
//
* (byte *) 0xFF90 = 0x4C; // reset CoCo 2 compatible bit
* (byte *) 0xFF98 = 0x03; // text mode
* (byte *) 0xFF99 = 0b00000100; // 192 lines per field, 40x24 grid, NO color attributes
* (byte *) 0xFF9A = currentPaletteValues[PAL_SCREEN_BG];
* (unsigned *) 0xFF9D = 0xFC00; // specifies $7E000 as base address; mapped to HW_TEXT_SCREEN_BUFFER
// Set the foreground and background colors through palette slots 1 and 0.
//
* (byte *) 0xFFB0 = currentPaletteValues[PAL_SCREEN_BG];
* (byte *) 0xFFB1 = currentPaletteValues[PAL_PROMPTS];
setConsoleOutHook(hardwareTextScreenConsoleOut);
}
void
clearHardwareTextScreen(void)
{
memset16((void *) HW_TEXT_SCREEN_BUFFER, 0x2020, HW_TEXT_SCREEN_WIDTH * HW_TEXT_SCREEN_HEIGHT / 2);
hardwareTextCursor = (byte *) HW_TEXT_SCREEN_BUFFER;
}