aboutsummaryrefslogtreecommitdiff
path: root/src/vga.c
blob: e90ebbac2e7cd0978a7cab7f00b6b8380152b06d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <dpmi.h>
#include <sys/nearptr.h>
#include <pc.h>
#include <dos.h>

#include "vga.h"

static uint8_t buffer[320 * 200];
static uint8_t *screen = NULL;

uint8_t open_framebuffer()
{
    if (__djgpp_nearptr_enable() == 0)
        return 0;

    screen = (uint8_t *)(0xa0000 + __djgpp_conventional_base);
    return 1;
}

void close_framebuffer()
{
    __djgpp_nearptr_disable();
}

void set_mode(uint8_t mode)
{
    __dpmi_regs regs = { 0 };
    regs.x.ax = mode;
    __dpmi_int(0x10, &regs);

    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();
    }
}

void wait_vsync()
{
    while (inportb(0x3da) & 8);
    while (!(inportb(0x3da) & 8));
}

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)
{
    for (int32_t j = dst->y * 320; j < (dst->y + dst->h) * 320; j += 320)
        for (int16_t i = dst->x; i < dst->x + dst->w; i++)
        {
            uint8_t b = *sprite++;

            /* transparent */
            if (b == TRANSPARENT)
                continue;

            /* clipping */
            if (i < 0 || i >= 320 || j < 0 || j >= 200 * 320)
                continue;

            buffer[i + j] = b;
        }
}

void blit_erase(uint8_t c)
{
    memset(buffer, c, 320 * 200);
}

void blit_update()
{
    memcpy(screen, buffer, 320 * 200);
}