aboutsummaryrefslogtreecommitdiffstats
path: root/boxv.c
blob: 1f2387ff937518ac3a79e035da1788e50af54d60 (plain) (blame)
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
/*****************************************************************************

Copyright (c) 2012-2022  Michal Necasek
                   2023  Philip Kelley

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

*****************************************************************************/

/* Core boxv implementation. */

#include "boxv.h"       /* Public interface. */
#include "boxvint.h"    /* Implementation internals. */
#define NEEDVIDEO
#include "io.h"    /* I/O access layer, host specific. */

#pragma code_seg( _INIT )

/* Write a single value to an indexed register at a specified
 * index. Suitable for the CRTC or graphics controller.
 */
inline void vid_wridx( void *cx, int idx_reg, int idx, v_byte data )
{
    vid_outw( cx, idx_reg, idx | (data << 8) );
}

/* Set an extended non-VGA mode with given parameters. 8bpp and higher only.
 * Returns non-zero value on failure.
 */
int BOXV_ext_mode_set( void *cx, int xres, int yres, int bpp, int v_xres, int v_yres )
{
    /* Do basic parameter validation. */
    if( v_xres < xres || v_yres < yres )
        return( -1 );

    /* Put the hardware into a state where the mode can be safely set. */
    vid_inb( cx, VGA_STAT_ADDR );                   /* Reset flip-flop. */
    vid_outb( cx, VGA_ATTR_W, 0 );                  /* Disable palette. */
    vid_wridx( cx, VGA_SEQUENCER, VGA_SR_RESET, VGA_SR_RESET );

    /* Disable the extended display registers. */
    vid_outw( cx, VBE_DISPI_IOPORT_INDEX, VBE_DISPI_INDEX_ENABLE );
    vid_outw( cx, VBE_DISPI_IOPORT_DATA, VBE_DISPI_DISABLED );

    /* Program the extended non-VGA registers. */

    /* Set X resoultion. */
    vid_outw( cx, VBE_DISPI_IOPORT_INDEX, VBE_DISPI_INDEX_XRES );
    vid_outw( cx, VBE_DISPI_IOPORT_DATA, xres );
    /* Set Y resoultion. */
    vid_outw( cx, VBE_DISPI_IOPORT_INDEX, VBE_DISPI_INDEX_YRES );
    vid_outw( cx, VBE_DISPI_IOPORT_DATA, yres );
    /* Set bits per pixel. */
    vid_outw( cx, VBE_DISPI_IOPORT_INDEX, VBE_DISPI_INDEX_BPP );
    vid_outw( cx, VBE_DISPI_IOPORT_DATA, bpp );
    /* Set the virtual resolution. */
    vid_outw( cx, VBE_DISPI_IOPORT_INDEX, VBE_DISPI_INDEX_VIRT_WIDTH );
    vid_outw( cx, VBE_DISPI_IOPORT_DATA, v_xres );
    vid_outw( cx, VBE_DISPI_IOPORT_INDEX, VBE_DISPI_INDEX_VIRT_HEIGHT );
    vid_outw( cx, VBE_DISPI_IOPORT_DATA, v_yres );
    /* Reset the current bank. */
    vid_outw( cx, VBE_DISPI_IOPORT_INDEX, VBE_DISPI_INDEX_BANK );
    vid_outw( cx, VBE_DISPI_IOPORT_DATA, 0 );
    /* Set the X and Y display offset to 0. */
    vid_outw( cx, VBE_DISPI_IOPORT_INDEX, VBE_DISPI_INDEX_X_OFFSET );
    vid_outw( cx, VBE_DISPI_IOPORT_DATA, 0 );
    vid_outw( cx, VBE_DISPI_IOPORT_INDEX, VBE_DISPI_INDEX_Y_OFFSET );
    vid_outw( cx, VBE_DISPI_IOPORT_DATA, 0 );
    /* Enable the extended display registers. */
    vid_outw( cx, VBE_DISPI_IOPORT_INDEX, VBE_DISPI_INDEX_ENABLE );
#ifdef QEMU
    vid_outw( cx, VBE_DISPI_IOPORT_DATA, VBE_DISPI_ENABLED | VBE_DISPI_8BIT_DAC | VBE_DISPI_LFB_ENABLED );
#else
    vid_outw( cx, VBE_DISPI_IOPORT_DATA, VBE_DISPI_ENABLED | VBE_DISPI_8BIT_DAC );
#endif

    /* Re-enable the sequencer. */
    vid_wridx( cx, VGA_SEQUENCER, VGA_SR_RESET, VGA_SR0_NORESET );

    /* Reset flip-flop again and re-enable palette. */
    vid_inb( cx, VGA_STAT_ADDR );
    vid_outb( cx, VGA_ATTR_W, 0x20 );

    return( 0 );
}

/* Program the DAC. Each of the 'count' entries is 4 bytes in size,
 * red/green/blue/unused.
 * Returns non-zero on failure.
 */
int BOXV_dac_set( void *cx, unsigned start, unsigned count, void *pal )
{
    v_byte      *prgbu = pal;

    /* Basic argument validation. */
    if( start + count > 256 )
        return( -1 );

    /* Write the starting index. */
    vid_outb( cx, VGA_DAC_W_INDEX, start );
    /* Load the RGB data. */
    while( count-- ) {
        vid_outb( cx, VGA_DAC_DATA, *prgbu++ );
        vid_outb( cx, VGA_DAC_DATA, *prgbu++ );
        vid_outb( cx, VGA_DAC_DATA, *prgbu++ );
        ++prgbu;
    }
    return( 0 );
}

/* Detect the presence of a supported adapter and amount of installed
 * video memory. Returns zero if not found.
 */
int BOXV_detect( void *cx, unsigned long *vram_size )
{
    v_word      boxv_id;

    vid_outw( cx, VBE_DISPI_IOPORT_INDEX, VBE_DISPI_INDEX_ID );

    boxv_id = vid_inw( cx, VBE_DISPI_IOPORT_DATA );
#ifdef QEMU
    if( boxv_id < VBE_DISPI_ID0 || boxv_id > VBE_DISPI_ID6 )
        return( 0 );

    if( vram_size ) {
        vid_outw( cx, VBE_DISPI_IOPORT_INDEX, VBE_DISPI_INDEX_VIDEO_MEMORY_64K );
        *vram_size = (unsigned long)vid_inw( cx, VBE_DISPI_IOPORT_DATA ) << 16;
    }

    return( boxv_id );
#else
    if( vram_size ) {
        *vram_size = vid_ind( cx, VBE_DISPI_IOPORT_DATA );
    }

    if( boxv_id >= VBE_DISPI_ID0 && boxv_id <= VBE_DISPI_ID4 )
        return( boxv_id );
    else
        return( 0 );
#endif
}

/* Disable extended mode and place the hardware into a VGA compatible state.
 * Returns non-zero on failure.
 */
int BOXV_ext_disable( void *cx )
{
    /* Disable the extended display registers. */
    vid_outw( cx, VBE_DISPI_IOPORT_INDEX, VBE_DISPI_INDEX_ENABLE );
    vid_outw( cx, VBE_DISPI_IOPORT_DATA, VBE_DISPI_DISABLED );
    return( 0 );
}

/* Return the physical base address of the framebuffer. Needed in environments
 * that do not query the base through PCI.
 */
unsigned long BOXV_get_lfb_base( void *cx )
{
    unsigned long   fb_base;

    /* Ask the virtual hardware for the high 16 bits. */
    vid_outw( cx, VBE_DISPI_IOPORT_INDEX, VBE_DISPI_INDEX_FB_BASE_HI );
    fb_base = vid_inw( cx, VBE_DISPI_IOPORT_DATA );

    /* Old versions didn't support that, so use the default
     * if the value looks like garbage.
     */
    if( fb_base != 0 && fb_base != 0xffff )
        fb_base = fb_base << 16;
    else
        fb_base = VBE_DISPI_LFB_PHYSICAL_ADDRESS;

    return( fb_base );
}