// SPDX-License-Identifier: GPL-3.0-or-later #include typedef unsigned char byte; #define REG(addr) (*(byte *)(addr)) #define FIRST_PAGE 0x20 #define SECOND_PAGE 0x24 #define WINDOW 0x8000 static byte back; // Zero when closed; otherwise an 8 KiB block number. static byte palette[16]; static const byte *textImage; // program_end is provided by CMOC's linker script. While bank 4 is mapped, // neither an instruction fetch nor a stack access may land in the window. static asm byte lowProgramAndStack(void) { asm { program_end import ldx #program_end cmpx #$8000 bhi @reject cmps #$8000 bhi @reject tfr cc,b bitb #$10 bne @reject ldb #1 rts @reject clrb } } // A real 128 KiB GIME accepts the same MMU register writes as a 512 KiB // machine, but blocks $20 and $30 alias. Change just one byte reversibly. // An inactive window (for instance, wrong task) also fails this test. static asm byte independentBanks(void) { asm { pshs cc orcc #$50 lda >$ffa4 pshs a lda #$30 sta >$ffa4 lda >$9555 pshs a lda #$20 sta >$ffa4 lda >$9555 pshs a coma sta >$9555 cmpa >$9555 bne @alias lda #$30 sta >$ffa4 ldb >$9555 cmpb 1,s bne @alias ldb #1 bra @restore @alias clrb @restore lda #$20 sta >$ffa4 puls a sta >$9555 leas 1,s puls a sta >$ffa4 puls cc } } // BASIC's 16-bit TIMER is incremented by its vertical interrupt handler. // Poll the complete word, including through wraparound, with interrupts live. // Aligned 32 KiB images share a zero low offset; flipping needs one store. // The countdown makes missing IRQs a recoverable error. static byte field(byte offsetHigh, byte flip) { asm { ldd >$0112 ldx #$2000 @wait cmpd >$0112 bne @blank leax -1,x bne @wait clrb bra @done @blank tst :flip beq @success ldb :offsetHigh stb >$ff9d @success ldb #1 @done stb :flip } return flip; } // Paint at most one scan-line fragment in one bank. Preserve the other nibble // at either edge. Mask interrupts only across the short-lived mapping: BASIC // can take its normal 60 Hz interrupt between scan lines. static void span(byte bank, unsigned address, byte lowFirst, byte pairs, byte highLast, byte color) { byte packed = (color << 4) | color; asm { pshs cc orcc #$50 lda >$ffa4 pshs a lda :bank sta >$ffa4 ldx :address tst :lowFirst beq @pairs lda ,x anda #$f0 ora :color sta ,x+ @pairs ldb :pairs beq @last lda :packed @loop sta ,x+ decb bne @loop @last tst :highLast beq @done lda :packed anda #$f0 ldb ,x andb #$0f pshs b ora ,s+ sta ,x @done puls a sta >$ffa4 puls cc } } void gime_fill_rect(unsigned x, unsigned y, unsigned w, unsigned h, byte color) { unsigned rowPixel; if (!back || color > 15 || x >= GIME_WIDTH || y >= GIME_HEIGHT || !w || !h) return; if (w > GIME_WIDTH - x) w = GIME_WIDTH - x; if (h > GIME_HEIGHT - y) h = GIME_HEIGHT - y; rowPixel = y * GIME_WIDTH + x; while (h--) { unsigned pixel = rowPixel; unsigned remaining = w; while (remaining) { unsigned inBank = pixel & 0x3fff; unsigned count = 0x4000 - inBank; byte first = (byte)(pixel & 1); if (count > remaining) count = remaining; span((byte)(back + (pixel >> 14)), WINDOW + (inBank >> 1), first, (byte)((count - first) >> 1), (byte)((count - first) & 1), color); pixel += count; remaining -= count; } rowPixel += GIME_WIDTH; } } void gime_plot(unsigned x, unsigned y, byte color) { gime_fill_rect(x, y, 1, 1, color); } void gime_clear(byte color) { gime_fill_rect(0, 0, GIME_WIDTH, GIME_HEIGHT, color); } void gime_set_palette(byte slot, byte rgb) { if (back && slot < 16 && rgb < 64) REG(0xffb0 + slot) = rgb; } byte gime_open(void) { byte i, width; // Super Extended BASIC defines HRMODE at $E6 and HRWIDTH at $E7. if (back || !lowProgramAndStack() || *(unsigned *)0xfff8 != 0xfef7 || REG(0xe6) != 0 || REG(0xe7) > 2) return 0; for (i = 0; i < 16; ++i) { byte bank = REG(0xffa0 + i) & 0x3f; if (bank >= FIRST_PAGE && bank < SECOND_PAGE + 4) return 0; } if (!field(0, 0) || !independentBanks()) return 0; width = REG(0xe7); textImage = (const byte *)0xe032 + width * 9; for (i = 0; i < 16; ++i) palette[i] = REG(0xffb0 + i) & 0x3f; back = FIRST_PAGE; gime_clear(0); back = SECOND_PAGE; gime_clear(0); if (!field(0, 0)) { back = 0; return 0; } // Super Extended BASIC keeps editable images of the write-only registers // at $E032 (text tables) and $E070 (graphics). Keep the ROM's color phase. REG(0xff90) = REG(0xe070); REG(0xff98) = REG(0xe071); REG(0xff99) = 0x1e; // 192 lines, 160 bytes per row, 16 colors. REG(0xff9c) = 0; REG(0xff9d) = FIRST_PAGE << 2; REG(0xff9e) = 0; REG(0xff9f) = 0; return 1; } byte gime_present(void) { if (!back || !field(back << 2, 1)) return 0; back = (back == SECOND_PAGE ? FIRST_PAGE : SECOND_PAGE); return 1; } void gime_close(void) { byte i; if (!back) return; field(0, 0); // Even if the timer stops, always give the display back. REG(0xff90) = textImage[0]; REG(0xff98) = textImage[1]; REG(0xff99) = textImage[2]; REG(0xff9c) = textImage[5]; REG(0xff9d) = textImage[6]; REG(0xff9e) = textImage[7]; REG(0xff9f) = textImage[8]; for (i = 0; i < 16; ++i) REG(0xffb0 + i) = palette[i]; back = 0; }