aboutsummaryrefslogtreecommitdiff
path: root/src/vga.c
blob: ad12c9d568a1e94763e93c3ef9aab11c8c27bf1b (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#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;
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, &regs);
    if (regs.h.al != 0x1a)
        return 0;

    memset(&regs, 0, sizeof(regs));
    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();
    }

    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;
    const uint16_t inc = 320 - dst->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;
        }
        dtarget += inc;
    }
}

void blit_c(const uint8_t *sprite, const Rect *dst, uint8_t c)
{
    uint8_t *dtarget = target + dst->x + dst->y * 320;

    asm volatile (
        "push %%ebp\n\t"
        "movl $320, %%ebp\n\t"
        "subl %%ecx, %%ebp\n\t"
        "blit_c_w:\n\t"
        "movl %%ecx, %%eax\n\t"
        "blit_c_w_loop:\n\t"
        "movb (%%esi), %%ah\n\t"
        "cmpb $%p5, %%ah\n\t"
        "je blit_c_tr\n\t"
        "orb %%ah, %%ah\n\t"
        "je blit_c_store\n\t"
        "movb %%dl, %%ah\n\t"
        "blit_c_store:\n\t"
        "movb %%ah, (%%edi)\n\t"
        "blit_c_tr:\n\t"
        "incl %%esi\n\t"
        "incl %%edi\n\t"
        "decb %%al\n\t"
        "jne blit_c_w_loop\n\t"
        "addl %%ebp, %%edi\n\t"
        "decl %%ebx\n\t"
        "jne blit_c_w\n\t"
        "pop %%ebp\n\t"
        : /*no output */
        : "S" (sprite), "D" (dtarget),
        "b" ((uint32_t)dst->h), "d" ((uint32_t)c),
        "c" ((uint32_t)dst->w), "i" (TRANSPARENT)
        : "eax"
    );
}

void blitrc(const uint8_t *sprite, const Rect *src, const Rect *dst)
{
    uint8_t *dtarget = target + dst->x + dst->y * 320;

    sprite += src->x + src->y * src->w;

    asm volatile (
        "push %%ebp\n\t"
        "movl $320, %%ebp\n\t"
        "subl %%ecx, %%ebp\n\t"
        "blitrc_w:\n\t"
        "movl %%ecx, %%eax\n\t"
        "blitrc_w_loop:\n\t"
        "movb (%%esi), %%ah\n\t"
        "cmpb $%p5, %%ah\n\t"
        "je blitrc_tr\n\t"
        "movb %%ah, (%%edi)\n\t"
        "blitrc_tr:\n\t"
        "incl %%esi\n\t"
        "incl %%edi\n\t"
        "decb %%al\n\t"
        "jne blitrc_w_loop\n\t"
        "addl %%edx, %%esi\n\t"
        "addl %%ebp, %%edi\n\t"
        "decl %%ebx\n\t"
        "jne blitrc_w\n\t"
        "pop %%ebp\n\t"
        : /*no output */
        : "S" (sprite), "D" (dtarget),
        "b" ((uint32_t)dst->h), "d" ((uint32_t)(src->w - dst->w)),
        "c" ((uint32_t)dst->w), "i" (TRANSPARENT)
        : "eax"
    );
}

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

void blit_copy16(const Rect *dst)
{
    uint8_t *src = buffer + dst->x + dst->y * 320;
    uint8_t *dtarget = screen + dst->x + dst->y * 320;

    asm volatile (
        "movl $0x130, %%ebx\n\t"
        "cld\n\t"
        "blit_copy16_loop:\n\t"
        "movsl\n\t"
        "movsl\n\t"
        "movsl\n\t"
        "movsl\n\t"
        "addl %%ebx, %%esi\n\t"
        "addl %%ebx, %%edi\n\t"
        "decl %%eax\n\t"
        "orl %%eax, %%eax\n\t"
        "jne blit_copy16_loop\n\t"
        : /* no output */
        : "S" (src), "D" (dtarget), "a" ((uint32_t)dst->h)
        : "ecx", "ebx"
    );
}