#include #include #include #include #include #include #include #include "vga.h" static uint8_t buffer[320 * 200]; static uint8_t *screen = NULL; static uint8_t *target = NULL; uint8_t open_framebuffer() { if (__djgpp_nearptr_enable() == 0) return 0; target = screen = (uint8_t *)(0xa0000 + __djgpp_conventional_base); return 1; } void blit_target(uint8_t t) { target = t ? buffer : screen; } void close_framebuffer() { __djgpp_nearptr_disable(); } uint8_t set_mode(uint8_t mode) { __dpmi_regs regs = { 0 }; /* detect VGA card */ regs.x.ax = 0x1a00; __dpmi_int(0x10, ®s); if (regs.h.al != 0x1a) return 0; memset(®s, 0, sizeof(regs)); regs.x.ax = mode; __dpmi_int(0x10, ®s); if (mode == 0x13) { /* setup the VGA: 320x200 @ 60Hz */ /* from: https://gist.github.com/juj/34306e6da02a8a7043e393f01e013f24 */ disable(); /* XXX: probably not neeed */ wait_vsync(); outportw(0x3d4, 0x0011); /* Turn off write protect to CRTC registers */ outportw(0x3d4, 0x0b06); /* New vertical total=525 lines, bits 0-7 */ outportw(0x3d4, 0x3e07); /* New vertical total=525 lines, bits 8-9 */ outportw(0x3d4, 0xb910); /* Vsync start=scanline 185 */ outportw(0x3d4, 0x8f12); /* Vertical display end=scanline 399, bits 0-7 */ outportw(0x3d4, 0xb815); /* Vertical blanking start=scanline 440, bits 0-7 */ outportw(0x3d4, 0xe216); /* Adjust vblank end position */ outportw(0x3d4, 0x8511); /* Vsync length=2 lines + turn write protect back on */ /* XXX: probably not neeed */ wait_vsync(); outportw(0x3d4, 0x2813); /* 8 pixel chars */ outportw(0x3d4, 0x8e11); /* restore retrace */ enable(); } return 1; } void wait_vsync() { while (inportb(0x3da) & 8); while (!(inportb(0x3da) & 8)); } void wait_frames(uint16_t frames) { /* wait some time */ for (uint16_t i = 0; i < frames; i++) wait_vsync(); } void set_palette(const uint8_t *palette) { outportb(0x3c8, 0); for (int i = 0; i < 768; i++) outportb(0x3c9, palette[i] >> 2); } void blit(const uint8_t *sprite, const Rect *dst) { uint8_t b; uint8_t *dtarget = target + dst->x + dst->y * 320; for (uint16_t j = dst->h; j > 0; j--) { for (uint16_t i = dst->w; i > 0; i--) { b = *sprite++; /* transparent */ if (b == TRANSPARENT) { dtarget++; continue; } *dtarget++ = b; } dtarget += 320 - dst->w; } } void blit_c(const uint8_t *sprite, const Rect *dst, uint8_t c) { uint8_t b; uint8_t *dtarget = target + dst->x + dst->y * 320; for (uint16_t j = dst->h; j > 0; j--) { for (uint16_t i = dst->w; i > 0; i--) { b = *sprite++; /* transparent */ if (b == TRANSPARENT) { dtarget++; continue; } *dtarget++ = b ? c : b; } dtarget += 320 - dst->w; } } void blitrc(const uint8_t *sprite, const Rect *src, const Rect *dst) { uint8_t b; uint8_t *dtarget = target + dst->x + dst->y * 320; sprite += src->x + src->y * src->w; for (uint16_t j = dst->h; j > 0; j--) { for (uint16_t i = dst->w; i > 0; i--) { b = *sprite++; /* transparent */ if (b == TRANSPARENT) { dtarget++; continue; } *dtarget++ = b; } sprite += src->w - dst->w; dtarget += 320 - dst->w; } } void blit_erase(uint8_t c) { memset(target, c, 320 * 200); } void blit_copy_all() { memcpy(screen, buffer, 320 * 200); } void blit_copy(const Rect *dst) { uint8_t *src = buffer + dst->x + dst->y * 320; uint8_t *dtarget = screen + dst->x + dst->y * 320; for (uint16_t j = dst->h; j > 0; j--) { memcpy(dtarget, src, dst->w); src += 320; dtarget += 320; } }