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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
|
/**************************************************************************
Copyright (c) 2024 Jaroslav Hensl <emulator@emulace.cz>
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.
*****************************************************************************/
#define VBE
#include "winhack.h"
#include "vmm.h"
#include "vxd.h"
#include "vxd_lib.h"
#include "3d_accel.h"
#include "boxvint.h"
#include "pci.h" /* re-use PCI functions from SVGA */
#include "code32.h"
#define IO_IN16
#define IO_OUT16
#define IO_IN8
#define IO_OUT8
#ifndef QEMU
# define IO_IN32
#endif
#include "io32.h"
#define PCI_VENDOR_ID_INNOTEK 0x80EE
#define PCI_DEVICE_ID_VBOX_VGA 0xBEEF
#define PCI_VENDOR_ID_BOCHSVBE 0x1234
#define PCI_DEVICE_ID_BOCHSVBE 0x1111
/* (max) 256MB VRAM: max 8K in 16:10 */
#define RES_MAX_X 8192
#define RES_MAX_Y 5120
#define ISA_LFB 0xE0000000UL
static BOOL vbe_is_valid = FALSE;
extern FBHDA_t *hda;
extern LONG fb_lock_cnt;
WORD vbe_chip_id = 0;
#ifdef QEMU
static char vbe_vxd_name[] = "qemumini.vxd";
#else
static char vbe_vxd_name[] = "boxvmini.vxd";
#endif
#include "vxd_strings.h"
static inline void wridx(unsigned short idx_reg, unsigned char idx, unsigned char data)
{
outpw(idx_reg, (unsigned short)idx | (((unsigned short)data) << 8));
}
static WORD VBE_detect(DWORD *vram_size)
{
WORD chip_id;
outpw(VBE_DISPI_IOPORT_INDEX, VBE_DISPI_INDEX_ID);
chip_id = inpw(VBE_DISPI_IOPORT_DATA);
#ifdef QEMU
if(chip_id < VBE_DISPI_ID0 || chip_id > VBE_DISPI_ID6)
return 0;
if(vram_size)
{
outpw(VBE_DISPI_IOPORT_INDEX, VBE_DISPI_INDEX_VIDEO_MEMORY_64K);
*vram_size = ((DWORD)inpw(VBE_DISPI_IOPORT_DATA)) << 16;
}
return chip_id;
#else
if(vram_size)
{
*vram_size = inpd(VBE_DISPI_IOPORT_DATA);
}
if(chip_id >= VBE_DISPI_ID0 && chip_id <= VBE_DISPI_ID4)
return chip_id;
return 0;
#endif
}
DWORD VBE_get_LFB()
{
PCIAddress pciAddr;
/* virtualbox */
if(PCI_FindDevice(PCI_VENDOR_ID_INNOTEK, PCI_DEVICE_ID_VBOX_VGA, &pciAddr))
{
PCI_SetMemEnable(&pciAddr, TRUE);
return PCI_GetBARAddr(&pciAddr, 0);
}
/* qemu */
if(PCI_FindDevice(PCI_VENDOR_ID_BOCHSVBE, PCI_DEVICE_ID_BOCHSVBE, &pciAddr))
{
PCI_SetMemEnable(&pciAddr, TRUE);
return PCI_GetBARAddr(&pciAddr, 0);
}
/* try failback */
return ISA_LFB;
}
BOOL VBE_init_hw()
{
DWORD vram_size;
DWORD vram_phy;
vbe_chip_id = VBE_detect(&vram_size);
if(vbe_chip_id == 0)
{
dbg_printf(dbg_vbe_fail);
return FALSE;
}
vram_phy = VBE_get_LFB();
if(!FBHDA_init_hw())
{
return FALSE;
}
dbg_printf(dbg_vbe_init, vram_phy, vram_size);
hda->vram_size = vram_size;
hda->vram_pm32 = (void*)_MapPhysToLinear(vram_phy, vram_size, 0);
hda->flags |= FB_SUPPORT_FLIPING;
memcpy(hda->vxdname, vbe_vxd_name, sizeof(vbe_vxd_name));
dbg_printf(dbg_vbe_lfb, hda->vram_pm32);
if(hda->vram_pm32 != NULL)
{
vbe_is_valid = TRUE;
}
return TRUE;
}
BOOL VBE_valid()
{
return vbe_is_valid;
}
static DWORD VBE_pitch(DWORD width, DWORD bpp)
{
DWORD bp = (bpp + 7) / 8;
return (bp * width + 3) & 0xFFFFFFFC;
}
BOOL VBE_validmode(DWORD w, DWORD h, DWORD bpp)
{
switch(bpp)
{
case 8:
case 16:
case 24:
case 32:
break;
default:
return FALSE;
}
if(w <= RES_MAX_X)
{
if(h <= RES_MAX_Y)
{
DWORD size = VBE_pitch(w, bpp) * h;
if(size < hda->vram_size)
{
return TRUE;
}
}
}
return FALSE;
}
BOOL VBE_setmode(DWORD w, DWORD h, DWORD bpp)
{
if(!VBE_validmode(w, h, bpp)) return FALSE;
/* Put the hardware into a state where the mode can be safely set. */
inp(VGA_STAT_ADDR); /* Reset flip-flop. */
outp(VGA_ATTR_W, 0); /* Disable palette. */
wridx(VGA_SEQUENCER, VGA_SR_RESET, VGA_SR_RESET);
/* Disable the extended display registers. */
outpw(VBE_DISPI_IOPORT_INDEX, VBE_DISPI_INDEX_ENABLE);
outpw(VBE_DISPI_IOPORT_DATA, VBE_DISPI_DISABLED);
/* Program the extended non-VGA registers. */
/* Set X resoultion. */
outpw(VBE_DISPI_IOPORT_INDEX, VBE_DISPI_INDEX_XRES);
outpw(VBE_DISPI_IOPORT_DATA, (WORD)w);
/* Set Y resoultion. */
outpw(VBE_DISPI_IOPORT_INDEX, VBE_DISPI_INDEX_YRES);
outpw(VBE_DISPI_IOPORT_DATA, (WORD)h);
/* Set bits per pixel. */
outpw(VBE_DISPI_IOPORT_INDEX, VBE_DISPI_INDEX_BPP);
outpw(VBE_DISPI_IOPORT_DATA, (WORD)bpp);
/* Set the virtual resolution. (same as FB resolution) */
outpw(VBE_DISPI_IOPORT_INDEX, VBE_DISPI_INDEX_VIRT_WIDTH);
outpw(VBE_DISPI_IOPORT_DATA, (WORD)w);
outpw(VBE_DISPI_IOPORT_INDEX, VBE_DISPI_INDEX_VIRT_HEIGHT);
outpw(VBE_DISPI_IOPORT_DATA, (WORD)h);
/* Reset the current bank. */
outpw(VBE_DISPI_IOPORT_INDEX, VBE_DISPI_INDEX_BANK);
outpw(VBE_DISPI_IOPORT_DATA, 0);
/* Set the X and Y display offset to 0. */
outpw(VBE_DISPI_IOPORT_INDEX, VBE_DISPI_INDEX_X_OFFSET);
outpw(VBE_DISPI_IOPORT_DATA, 0);
outpw(VBE_DISPI_IOPORT_INDEX, VBE_DISPI_INDEX_Y_OFFSET);
outpw(VBE_DISPI_IOPORT_DATA, 0);
/* Enable the extended display registers. */
outpw(VBE_DISPI_IOPORT_INDEX, VBE_DISPI_INDEX_ENABLE);
#ifdef QEMU
outpw(VBE_DISPI_IOPORT_DATA, VBE_DISPI_ENABLED | VBE_DISPI_8BIT_DAC | VBE_DISPI_LFB_ENABLED | VBE_DISPI_NOCLEARMEM);
#else
outpw(VBE_DISPI_IOPORT_DATA, VBE_DISPI_ENABLED | VBE_DISPI_8BIT_DAC);
#endif
/* Re-enable the sequencer. */
wridx(VGA_SEQUENCER, VGA_SR_RESET, VGA_SR0_NORESET);
/* Reset flip-flop again and re-enable palette. */
inp(VGA_STAT_ADDR);
outp(VGA_ATTR_W, 0x20);
hda->width = w;
hda->height = h;
hda->bpp = bpp;
hda->pitch = VBE_pitch(w, bpp);
hda->stride = h * hda->pitch;
hda->surface = 0;
mouse_invalidate();
return TRUE;
}
void FBHDA_palette_set(unsigned char index, DWORD rgb)
{
outp(VGA_DAC_W_INDEX, index); /* Set starting index. */
outp(VGA_DAC_DATA, (rgb >> 16) & 0xFF);
outp(VGA_DAC_DATA, (rgb >> 8) & 0xFF);
outp(VGA_DAC_DATA, rgb & 0xFF);
}
DWORD FBHDA_palette_get(unsigned char index)
{
DWORD r, g, b;
outp(VGA_DAC_W_INDEX, index);
r = inp(VGA_DAC_DATA);
g = inp(VGA_DAC_DATA);
b = inp(VGA_DAC_DATA);
return (r << 16) | (g << 8) | b;
}
BOOL FBHDA_swap(DWORD offset)
{
DWORD ps = ((hda->bpp+7)/8);
DWORD offset_y = offset/hda->pitch;
DWORD offset_x = (offset % hda->pitch)/ps;
if(offset + hda->stride > hda->vram_size) return FALSE; /* if exceed VRAM */
FBHDA_access_begin(0);
outpw(VBE_DISPI_IOPORT_INDEX, VBE_DISPI_INDEX_Y_OFFSET);
outpw(VBE_DISPI_IOPORT_DATA, (WORD)offset_y);
outpw(VBE_DISPI_IOPORT_INDEX, VBE_DISPI_INDEX_X_OFFSET);
outpw(VBE_DISPI_IOPORT_DATA, (WORD)offset_x);
hda->surface = offset;
FBHDA_access_end(0);
return TRUE;
}
void FBHDA_access_begin(DWORD flags)
{
//Wait_Semaphore(hda_sem, 0);
if(fb_lock_cnt++ == 0)
{
mouse_erase();
}
}
void FBHDA_access_rect(DWORD left, DWORD top, DWORD right, DWORD bottom)
{
FBHDA_access_begin(0);
}
void FBHDA_access_end(DWORD flags)
{
fb_lock_cnt--;
if(fb_lock_cnt < 0) fb_lock_cnt = 0;
if(fb_lock_cnt == 0)
{
mouse_blit();
// cursor
}
//Signal_Semaphore(hda_sem);
}
DWORD FBHDA_overlay_setup(DWORD overlay, DWORD width, DWORD height, DWORD bpp)
{
return 0;
}
void FBHDA_overlay_lock(DWORD left, DWORD top, DWORD right, DWORD bottom)
{
}
void FBHDA_overlay_unlock(DWORD flags)
{
}
|