mirror of
https://github.com/sehugg/8bitworkshop.git
synced 2024-11-26 10:49:17 +00:00
129 lines
7.4 KiB
C
129 lines
7.4 KiB
C
|
|
#include <string.h>
|
|
#include "aclib.h"
|
|
|
|
#define EXIT_CLIPDEST(addr) if ((((word)addr)&0xfff) >= 0xe10) return
|
|
|
|
// clear screen and set graphics mode
|
|
void clrscr() {
|
|
memset(vidmem, 0, VHEIGHT*VBWIDTH); // clear page 1
|
|
}
|
|
|
|
// draw vertical line
|
|
void vline(byte x, byte y1, byte y2, byte col, byte op) {
|
|
byte xb = x>>2; // divide x by 4
|
|
byte* dest = &vmagic[y1][xb]; // destination address
|
|
byte y;
|
|
hw_magic = M_SHIFT(x) | op; // set magic register
|
|
col <<= 6; // put color in high pixel
|
|
for (y=y1; y<=y2; y++) {
|
|
EXIT_CLIPDEST(dest);
|
|
*dest = col; // shift + xor color
|
|
dest += VBWIDTH; // dest address to next scanline
|
|
}
|
|
}
|
|
|
|
// draw a pixel
|
|
void pixel(byte x, byte y, byte col, byte op) {
|
|
vline(x, y, y, col, op); // draw line with 1-pixel height
|
|
}
|
|
|
|
// render a sprite with the given graphics operation
|
|
void render_sprite(const byte* src, byte x, byte y, byte op) {
|
|
byte i,j;
|
|
byte w = *src++; // get width from 1st byte of sprite
|
|
byte h = *src++; // get height from 2nd byte of sprite
|
|
byte xb = x>>2; // divide x by 4
|
|
byte* dest = &vmagic[y][xb]; // destination address
|
|
hw_magic = M_SHIFT(x) | op; // set magic register
|
|
for (j=0; j<h; j++) {
|
|
EXIT_CLIPDEST(dest);
|
|
for (i=0; i<w; i++) {
|
|
*dest++ = *src++;
|
|
}
|
|
*dest = 0; // rest of shifted byte
|
|
dest += VBWIDTH-w; // dest address to next scanline
|
|
}
|
|
}
|
|
|
|
// erase a sprite
|
|
void erase_sprite(const byte* src, byte x, byte y) {
|
|
byte i,j;
|
|
byte w = *src++; // get width from 1st byte of sprite
|
|
byte h = *src++; // get height from 2nd byte of sprite
|
|
byte xb = x>>2; // divide x by 4
|
|
byte* dest = &vidmem[y][xb]; // destination address
|
|
for (j=0; j<h; j++) {
|
|
EXIT_CLIPDEST(dest);
|
|
for (i=0; i<w; i++) {
|
|
*dest++ = 0;
|
|
}
|
|
*dest = 0; // rest of shifted byte
|
|
dest += VBWIDTH-w; // dest address to next scanline
|
|
}
|
|
}
|
|
|
|
// FONT FUNCTIONS
|
|
|
|
const char FONT[HICHAR-LOCHAR+1][FONT_HEIGHT*FONT_BWIDTH] = {
|
|
{ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, },{ 0x00,0x00,0x00,0x20,0x20,0x20,0x00,0x20, },{ 0x00,0x00,0x50,0x50,0x50,0x00,0x00,0x00, },{ 0x00,0x00,0x00,0x50,0xF8,0x50,0xF8,0x50, },{ 0x00,0x00,0x00,0xF8,0xA0,0xF8,0x28,0xF8, },{ 0x00,0x00,0x00,0xC8,0xD0,0x20,0x58,0x98, },{ 0x00,0x00,0x00,0xE0,0xA8,0xF8,0x90,0xF8, },{ 0x00,0x00,0x40,0x40,0x40,0x00,0x00,0x00, },{ 0x00,0x00,0x30,0x20,0x20,0x20,0x20,0x20, },{ 0x00,0x00,0x60,0x20,0x20,0x20,0x20,0x20, },{ 0x00,0x00,0x00,0x20,0xA8,0x70,0xA8,0x20, },{ 0x00,0x00,0x00,0x20,0x20,0xF8,0x20,0x20, },{ 0x00,0x00,0x00,0x00,0x00,0x00,0x60,0x60, },{ 0x00,0x00,0x00,0x00,0x00,0xF8,0x00,0x00, },{ 0x00,0x00,0x00,0x00,0x00,0x00,0x60,0x60, },{ 0x00,0x00,0x00,0x08,0x10,0x20,0x40,0x80, },{ 0x00,0x00,0x00,0xF8,0x88,0xE8,0x88,0xF8, },{ 0x00,0x00,0x00,0x10,0x30,0x50,0x10,0x10, },{ 0x00,0x00,0x00,0xF8,0x08,0xF8,0x80,0xF8, },{ 0x00,0x00,0x00,0xF8,0x08,0xF8,0x08,0xF8, },{ 0x00,0x00,0x00,0x38,0x48,0x88,0xF8,0x08, },{ 0x00,0x00,0x00,0xF8,0x80,0xF8,0x08,0xF8, },{ 0x00,0x00,0x00,0xF8,0x80,0xF8,0x88,0xF8, },{ 0x00,0x00,0x00,0xF8,0x08,0x10,0x20,0x40, },{ 0x00,0x00,0x00,0xF8,0x88,0xF8,0x88,0xF8, },{ 0x00,0x00,0x00,0xF8,0x88,0xF8,0x08,0xF8, },{ 0x00,0x00,0x00,0x30,0x30,0x00,0x30,0x30, },{ 0x00,0x00,0x00,0x30,0x30,0x00,0x30,0x30, },{ 0x00,0x00,0x08,0x10,0x20,0x40,0x20,0x10, },{ 0x00,0x00,0x00,0x00,0xF8,0x00,0xF8,0x00, },{ 0x00,0x00,0x40,0x20,0x10,0x08,0x10,0x20, },{ 0x00,0x00,0x00,0xF8,0x08,0x78,0x00,0x60, },{ 0x00,0x00,0x00,0xF8,0xA8,0xB8,0x80,0xF8, },{ 0x00,0x00,0x00,0xF8,0x88,0xF8,0x88,0x88, },{ 0x00,0x00,0x00,0xF0,0x90,0xF8,0x88,0xF8, },{ 0x00,0x00,0x00,0xF8,0x80,0x80,0x80,0xF8, },{ 0x00,0x00,0x00,0xE0,0x90,0x88,0x88,0xF8, },{ 0x00,0x00,0x00,0xF8,0x80,0xF8,0x80,0xF8, },{ 0x00,0x00,0x00,0xF8,0x80,0xF8,0x80,0x80, },{ 0x00,0x00,0x00,0xF8,0x80,0xB8,0x88,0xF8, },{ 0x00,0x00,0x00,0x88,0x88,0xF8,0x88,0x88, },{ 0x00,0x00,0x00,0x40,0x40,0x40,0x40,0x40, },{ 0x00,0x00,0x00,0x08,0x08,0x88,0x88,0xF8, },{ 0x00,0x00,0x00,0x88,0x90,0xA0,0x90,0x88, },{ 0x00,0x00,0x00,0x80,0x80,0x80,0x80,0xF8, },{ 0x00,0x00,0x00,0xFE,0x92,0x92,0x92,0x92, },{ 0x00,0x00,0x00,0x88,0xC8,0xA8,0x98,0x88, },{ 0x00,0x00,0x00,0xF8,0x88,0x88,0x88,0xF8, },{ 0x00,0x00,0x00,0xF8,0x88,0x88,0xF8,0x80, },{ 0x00,0x00,0x00,0xF8,0x88,0xA8,0xA8,0xF8, },{ 0x00,0x00,0x00,0xF8,0x88,0xF8,0x90,0x88, },{ 0x00,0x00,0x00,0xF8,0x80,0xF8,0x08,0xF8, },{ 0x00,0x00,0x00,0xF8,0x20,0x20,0x20,0x20, },{ 0x00,0x00,0x00,0x88,0x88,0x88,0x88,0xF8, },{ 0x00,0x00,0x00,0x88,0x88,0x90,0xA0,0xC0, },{ 0x00,0x00,0x00,0x92,0x92,0x92,0x92,0xFE, },{ 0x00,0x00,0x00,0x88,0x50,0x20,0x50,0x88, },{ 0x00,0x00,0x00,0x88,0x88,0xF8,0x08,0xF8, },{ 0x00,0x00,0x00,0xF8,0x10,0x20,0x40,0xF8, },{ 0x00,0x00,0x38,0x20,0x20,0x20,0x20,0x20, },{ 0x00,0x00,0x00,0x80,0x40,0x20,0x10,0x08, },{ 0x00,0x00,0x70,0x10,0x10,0x10,0x10,0x10, },{ 0x00,0x00,0x00,0x20,0x50,0x88,0x00,0x00, },{ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, },{ 0x00,0x00,0x40,0x20,0x10,0x00,0x00,0x00, },{ 0x00,0x00,0x00,0xF8,0x88,0xF8,0x88,0x88, },{ 0x00,0x00,0x00,0xF0,0x90,0xF8,0x88,0xF8, },{ 0x00,0x00,0x00,0xF8,0x80,0x80,0x80,0xF8, },{ 0x00,0x00,0x00,0xE0,0x90,0x88,0x88,0xF8, },{ 0x00,0x00,0x00,0xF8,0x80,0xF8,0x80,0xF8, },{ 0x00,0x00,0x00,0xF8,0x80,0xF8,0x80,0x80, },{ 0x00,0x00,0x00,0xF8,0x80,0xB8,0x88,0xF8, },{ 0x00,0x00,0x00,0x88,0x88,0xF8,0x88,0x88, },{ 0x00,0x00,0x00,0x40,0x40,0x40,0x40,0x40, },{ 0x00,0x00,0x00,0x08,0x08,0x88,0x88,0xF8, },{ 0x00,0x00,0x00,0x88,0x90,0xA0,0x90,0x88, },{ 0x00,0x00,0x00,0x80,0x80,0x80,0x80,0xF8, },{ 0x00,0x00,0x00,0xFE,0x92,0x92,0x92,0x92, },{ 0x00,0x00,0x00,0x88,0xC8,0xA8,0x98,0x88, },{ 0x00,0x00,0x00,0xF8,0x88,0x88,0x88,0xF8, },{ 0x00,0x00,0x00,0xF8,0x88,0x88,0xF8,0x80, },{ 0x00,0x00,0x00,0xF8,0x88,0xA8,0xA8,0xF8, },{ 0x00,0x00,0x00,0xF8,0x88,0xF8,0x90,0x88, },{ 0x00,0x00,0x00,0xF8,0x80,0xF8,0x08,0xF8, },{ 0x00,0x00,0x00,0xF8,0x20,0x20,0x20,0x20, },{ 0x00,0x00,0x00,0x88,0x88,0x88,0x88,0xF8, },{ 0x00,0x00,0x00,0x88,0x88,0x90,0xA0,0xC0, },{ 0x00,0x00,0x00,0x92,0x92,0x92,0x92,0xFE, },{ 0x00,0x00,0x00,0x88,0x50,0x20,0x50,0x88, },{ 0x00,0x00,0x00,0x88,0x88,0xF8,0x08,0xF8, },{ 0x00,0x00,0x00,0xF8,0x10,0x20,0x40,0xF8, },{ 0x00,0x00,0x38,0x20,0x20,0xE0,0x20,0x20, },{ 0x00,0x00,0x20,0x20,0x20,0x20,0x20,0x20, },{ 0x00,0x00,0xE0,0x20,0x20,0x38,0x20,0x20, },{ 0x00,0x00,0x00,0xE8,0xB8,0x00,0x00,0x00, },{ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, },};
|
|
|
|
// draw a letter
|
|
void draw_char(byte ch, byte x, byte y, byte op) {
|
|
const byte* src = &FONT[(ch-LOCHAR)][0];
|
|
byte xb = x>>2; // divide x by 4
|
|
byte* dest = &vmagic[y][xb]; // destination address
|
|
hw_magic = M_SHIFT(x) | M_XPAND | op;
|
|
for (byte i=0; i<8; i++) {
|
|
char b = *src++;
|
|
EXIT_CLIPDEST(dest);
|
|
*dest++ = b; // expand lower nibble -> 1st byte
|
|
*dest++ = b; // expand upper nibble -> 2nd byte
|
|
*dest++ = 0; // leftover -> 3rd byte
|
|
*dest = 0; // reset upper/lower flag
|
|
dest += VBWIDTH-3; // we incremented 3 bytes for this line
|
|
}
|
|
}
|
|
|
|
void draw_string(const char* str, byte x, byte y) {
|
|
do {
|
|
byte ch = *str++;
|
|
if (!ch) break;
|
|
draw_char(ch, x, y, M_MOVE);
|
|
x += 8;
|
|
} while (1);
|
|
}
|
|
|
|
void draw_bcd_word(word bcd, byte x, byte y, byte op) {
|
|
byte j;
|
|
x += 3*8;
|
|
for (j=0; j<4; j++) {
|
|
draw_char('0'+(bcd&0xf), x, y, op);
|
|
x -= 8;
|
|
bcd >>= 4;
|
|
}
|
|
}
|
|
|
|
// add two 16-bit BCD values
|
|
word bcd_add(word a, word b) {
|
|
a; b; // to avoid warning
|
|
__asm
|
|
ld hl,#4
|
|
add hl,sp
|
|
ld iy,#2
|
|
add iy,sp
|
|
ld a,0 (iy)
|
|
add a, (hl)
|
|
daa
|
|
ld c,a
|
|
ld a,1 (iy)
|
|
inc hl
|
|
adc a, (hl)
|
|
daa
|
|
ld b,a
|
|
ld l, c
|
|
ld h, b
|
|
__endasm;
|
|
}
|
|
|