/* SPDX-License-Identifier: GPL-3.0-or-later */ /* Brian-approved C graphics exception: docs/DECISIONS.md, 2026-11-10. * Hardware reference: Chris Lomont, CoCo Hardware v0.82, pp. 11-13, 67-69. * The four BASIC graphics banks $30-$33 exist in the 128 KB machine. * Only Disk BASIC task 0's $8000-$9FFF mapping changes. SAM ROM/RAM selection, * video registers, BASIC system RAM, code and stack are never changed here. */ #include "coco3_video.h" #define REG(address) (*(byte *)(address)) word c3_mapping_errors; byte c3_video_ready(void) { /* Stock Disk BASIC uses MMU task 0 and HSCREEN 2 page $60000. * GIME control registers are not a dependable readback contract. The * caller has already initialized standard CoCo support and HSCREEN 2; * this renderer neither detects nor supports arbitrary custom mappings. */ return isCoCo3; } /* CMOC does not implement volatile. A separate C call is an optimization * boundary for MMU readback; audit the emitted reads/stores for each candidate. */ static byte read_bank(void) { return REG(0xFFA4) & 63; } static void copy_chunk(word offset, const byte *source, byte count) { byte *bank_register; byte *destination; byte original, bank, i; bank = (byte)(0x30 + (offset >> 13)); destination = (byte *)(0x8000 + (offset & 0x1FFF)); /* These are unmodified public coco.h interrupt APIs, not authored assembly. * Limit each interval to at most 32 copied bytes. No standard API that * needs BASIC/ROM is called until the prior mapping has been restored. */ disableInterrupts(); bank_register = (byte *)0xFFA4; original = read_bank(); *bank_register = bank; for (i = 0; i < count; ++i) destination[i] = source[i]; *bank_register = original; if (read_bank() != original) ++c3_mapping_errors; enableInterrupts(); } byte c3_blit(word x, byte y, word width, byte height, const byte *packed) { word row_bytes, offset, remaining, until_boundary, source_address; byte row, chunk; if (!width || !height || x >= 320 || width > 320-x || y >= 192 || height > 192-y) return FALSE; if ((x | width) & 1) return FALSE; row_bytes = width / 2; source_address = (word)packed; if (source_address >= 0x8000 || (word)(row_bytes * height) > 0x8000-source_address) return FALSE; for (row = 0; row < height; ++row) { offset = (word)(y+row) * 160 + x/2; remaining = row_bytes; while (remaining) { until_boundary = 8192 - (offset & 8191); chunk = remaining > 32 ? 32 : (byte)remaining; if (until_boundary < chunk) chunk = (byte)until_boundary; copy_chunk(offset,packed,chunk); packed += chunk; offset += chunk; remaining -= chunk; } } return TRUE; } byte c3_fill(word x, byte y, word width, byte height, byte color) { byte fill[160], row; if (!width || !height || color > 15 || x >= 320 || width > 320-x || y >= 192 || height > 192-y) return FALSE; /* Keep uncommon odd edges on the public API, outside every mapped interval. */ if (x & 1) { for (row = 0; row < height; ++row) hset(x,y+row,color); ++x; --width; } if (width & 1) { for (row = 0; row < height; ++row) hset(x+width-1,y+row,color); --width; } if (!width) return TRUE; memset(fill,(color<<4)|color,sizeof(fill)); for(row=0;row