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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
|
/**************************************************************************
Copyright (c) 2025 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 VESA
#include "winhack.h"
#include "vmm.h"
#include "vxd.h"
#include "vxd_lib.h"
#include "3d_accel.h"
#include "boxvint.h" /* VGA regitry */
#include "pci.h" /* re-use PCI functions from SVGA */
#include "vesa.h"
#define IO_IN8
#define IO_OUT8
#include "io32.h"
#include "code32.h"
#define ISA_LFB 0xE0000000UL
extern FBHDA_t *hda;
extern LONG fb_lock_cnt;
extern DWORD ThisVM;
static char vesa_vxd_name[] = "vesamini.vxd";
typedef struct vesa_mode
{
DWORD width;
DWORD height;
DWORD bpp;
DWORD pitch;
DWORD phy;
WORD mode_id;
WORD flags;
} vesa_mode_t;
static vesa_mode_t *vesa_modes = NULL;
static DWORD vesa_modes_cnt = 0;
WORD vesa_version = 0;
static DWORD vesa_caps = 0;
static int act_mode = -1;
#include "vxd_strings.h"
void vesa_bios(CRS_32 *V86regs)
{
// sizeof(CRS_32) = 108
_asm mov ebx, [ThisVM]
_asm mov ecx, [V86regs]
/* save state */
_asm sub esp, 108
_asm mov edi, esp
VMMCall(Save_Client_State);
/* switch to V86regs */
_asm mov esi, ecx
VMMCall(Restore_Client_State)
/* V86 INT */
VMMCall(Begin_Nest_V86_Exec);
_asm mov eax, 10h
VMMCall(Exec_Int);
VMMCall(End_Nest_Exec);
/* copy result state to V86regs */
_asm mov edi, ecx
VMMCall(Save_Client_State);
/* restore state regs */
_asm mov esi, esp
VMMCall(Restore_Client_State)
_asm add esp, 108
}
void load_client_state(CRS_32 *V86regs)
{
_asm mov edi, [V86regs]
VMMCall(Save_Client_State);
}
static DWORD vesa_buf_v86;
static void *vesa_buf;
static DWORD vesa_buf_phy;
static vesa_palette_entry_t *vesa_pal;
static int vesa_pal_bits = 6;
static BOOL vesa_valid = FALSE;
#define SCREEN_EMULATED_CENTER 0
#define SCREEN_EMULATED_COPY 1
#define SCREEN_FLIP 2
#define SCREEN_FLIP_VSYNC 3
static DWORD screen_mode = SCREEN_EMULATED_COPY;
#define MODE_OFFSET 1024
#define CRTC_OFFSET 2048
#define PAL_OFFSET 3072 // pal size = 4*256
#define V86_SEG(_lin) ((_lin) >> 4)
#define V86_OFF(_lin) ((_lin) & 0xF)
#define LIN_FROM_V86(_flatptr) ((((_flatptr) >> 12) & 0xFFFF0UL) + ((_flatptr) & 0xFFFFUL))
#define VESA_SUCC(_r) (((_r).Client_EAX & 0xFFFF)==0x004F)
static void offset_calc(DWORD offset, DWORD *ox, DWORD *oy)
{
DWORD ps = ((hda->bpp+7)/8);
*oy = offset/hda->pitch;
*ox = (offset % hda->pitch)/ps;
}
static void alloc_modes_info(DWORD cnt)
{
DWORD vesa_modes_pages = ((sizeof(vesa_mode_t) * cnt) + P_SIZE - 1) / P_SIZE;
vesa_modes = (vesa_mode_t*)_PageAllocate(vesa_modes_pages, PG_SYS, 0, 0x0, 0, 0x100000, NULL, PAGEZEROINIT);
dbg_printf("vesa_modes = %lX\n", vesa_modes);
vesa_modes_cnt = 0;
}
BOOL VESA_init_hw()
{
DWORD flat;
dbg_printf("VESA init begin...\n");
flat = _PageAllocate(1, PG_SYS, 0, 0x0, 0, 0x100000, &vesa_buf_phy, PAGEUSEALIGN | PAGECONTIG | PAGEFIXED);
vesa_buf = (void*)flat;
if(vesa_buf)
{
DWORD modes_count = 0;
#if 0
/* JH: bad idea, memory must be above 640k! */
DWORD lp = _GetLastV86Page();
DWORD v86_page = lp+1;
_SetLastV86Page(v86_page, 0);
if(_PhysIntoV86(vesa_buf_phy >> 12, ThisVM, v86_page, 1, 0))
{
vesa_buf_v86 = v86_page*4096;
}
dbg_printf("last page was=%lX\n", lp);
dbg_printf("VESA V86 addr=0x%lX, phy=0x%lX\n", vesa_buf_v86, vesa_buf_phy);
#else
/* FIXME: check if page is free! */
DWORD v86_page = 0xB0;
if(_PhysIntoV86(vesa_buf_phy >> 12, ThisVM, v86_page, 1, 0))
{
vesa_buf_v86 = v86_page*4096;
}
dbg_printf("VESA V86 addr=0x%lX, phy=0x%lX\n", vesa_buf_v86, vesa_buf_phy);
#endif
if(vesa_buf_v86)
{
DWORD i;
DWORD fb_phy = 0xFFFFFFFFF;
CRS_32 regs;
vesa_info_block_t *info = vesa_buf;
vesa_mode_info_t *modeinfo = (vesa_mode_info_t*)((char*)vesa_buf + MODE_OFFSET);
memset(info, 0, sizeof(vesa_info_block_t));
memset(modeinfo, 0, sizeof(vesa_mode_info_t));
memcpy(&info->VESASignature[0], "VBE2", 4);
load_client_state(®s);
regs.Client_EAX = VESA_CMD_ADAPTER_INFO;
regs.Client_ES = V86_SEG(vesa_buf_v86);
regs.Client_EDI = V86_OFF(vesa_buf_v86);
vesa_bios(®s);
if((regs.Client_EAX & 0xFFFF) == 0x004F &&
memcmp(info->VESASignature, "VESA", 4) == 0)
{
WORD *modes = NULL;
dbg_printf("VESA bios result=%X, vesa_version=%X, modes_ptr=%lX vesa_caps=%lX\n",
regs.Client_EAX & 0xFFFF, info->VESAVersion, info->VideoModePtr, info->Capabilities);
if(info->VESAVersion < VESA_VBE_2_0)
{
dbg_printf("We need VBE 2.0 as minimum, abort\n");
return FALSE;
}
modes = (WORD*)LIN_FROM_V86(info->VideoModePtr);
if(modes != NULL)
{
while(*modes != 0xFFFF)
{
//dbg_printf("modes: 0x%04X\n", *modes);
modes++;
modes_count++;
}
}
alloc_modes_info(modes_count);
modes = (WORD*)LIN_FROM_V86(info->VideoModePtr);
for(i = 0; i < modes_count; i++)
{
regs.Client_EAX = VESA_CMD_MODE_INFO;
regs.Client_ECX = modes[i];
regs.Client_ES = V86_SEG(vesa_buf_v86+MODE_OFFSET);
regs.Client_EDI = V86_OFF(vesa_buf_v86+MODE_OFFSET);
vesa_bios(®s);
//dbg_printf("mode=%X atrs=0x%lX eax=0x%lX\n", modes[i], modeinfo->ModeAttributes, regs.Client_EAX);
if(VESA_SUCC(regs))
{
if(modeinfo->ModeAttributes &
(VESA_MODE_HW_SUPPORTED | VESA_MODE_COLOR | VESA_MODE_GRAPHICS | VESA_MODE_LFB))
{
vesa_mode_t *m = &vesa_modes[vesa_modes_cnt];
m->width = modeinfo->XResolution;
m->height = modeinfo->YResolution;
m->bpp = modeinfo->BitsPerPixel;
m->pitch = modeinfo->BytesPerScanLine;
m->phy = modeinfo->PhysBasePtr;
m->mode_id = modes[i];
m->flags = modeinfo->ModeAttributes;
vesa_modes_cnt++;
if(m->phy)
{
if(m->phy < fb_phy)
{
fb_phy = m->phy;
}
}
dbg_printf("Mode 0x%X = (%ld x %ld x %ld) = phy:%lX\n",
m->mode_id, m->width, m->height, m->bpp, m->phy);
}
}
} // for
if(vesa_modes_cnt == 0)
{
return FALSE;
}
if(!FBHDA_init_hw())
{
return FALSE;
}
vesa_version = info->VESAVersion;
vesa_caps = info->Capabilities;
memcpy(hda->vxdname, vesa_vxd_name, sizeof(vesa_vxd_name));
hda->vram_size = info->TotalMemory * 0x10000UL;
hda->vram_bar_size = hda->vram_size;
if(fb_phy == 0xFFFFFFFF)
{
fb_phy = ISA_LFB;
}
hda->vram_pm32 = (void*)_MapPhysToLinear(fb_phy, hda->vram_size, 0);
hda->flags |= FB_SUPPORT_FLIPING | FB_VESA_MODES;
vesa_pal = (vesa_palette_entry_t*)(((BYTE*)vesa_buf)+PAL_OFFSET);
vesa_valid = TRUE;
//VESA_load_vbios_pm();
FBHDA_memtest();
dbg_printf("VESA_init_hw(vram_size=%ld) = TRUE\n", hda->vram_size);
return TRUE;
}
}
}
return FALSE;
}
BOOL VESA_valid()
{
return vesa_valid;
}
BOOL VESA_validmode(DWORD w, DWORD h, DWORD bpp)
{
DWORD i;
for(i = 0; i < vesa_modes_cnt; i++)
{
if(vesa_modes[i].width == w && vesa_modes[i].height == h && vesa_modes[i].bpp == bpp)
{
return TRUE;
}
}
//dbg_printf("fail to valid mode: %ld %ld %ld\n", w, h, bpp);
return FALSE;
}
void VESA_clear()
{
memset((BYTE*)hda->vram_pm32+hda->system_surface, 0, hda->pitch*hda->height);
}
BOOL VESA_setmode(DWORD w, DWORD h, DWORD bpp, DWORD rr_min, DWORD rr_max)
{
DWORD i;
dbg_printf("VESA_validmode()\n");
//if(!VESA_validmode(w, h, bpp)) return FALSE;
for(i = 0; i < vesa_modes_cnt; i++)
{
if(vesa_modes[i].width == w && vesa_modes[i].height == h && vesa_modes[i].bpp == bpp)
{
CRS_32 regs;
load_client_state(®s);
// set mode
regs.Client_EAX = VESA_CMD_MODE_SET;
regs.Client_EBX = vesa_modes[i].mode_id | VESA_SETMODE_LFB;
regs.Client_ES = 0;
regs.Client_EDI = 0;
vesa_bios(®s);
if(VESA_SUCC(regs))
{
DWORD test_x;
DWORD test_y;
dbg_printf("SET success: %d %d %d\n", w, h, bpp);
hda->width = w;
hda->height = h;
hda->bpp = bpp;
hda->pitch = vesa_modes[i].pitch;
hda->stride = h * hda->pitch;
hda->surface = 0;
/* set DAC to 8BIT if supported */
if(bpp <= 8)
{
if((vesa_caps & VESA_CAP_DAC8BIT) != 0)
{
/* set DAC to 8 bpp */
regs.Client_EAX = VESA_CMD_PALETTE_FORMAT;
regs.Client_EBX = VESA_DAC_SETFORMAT | VESA_DAC_SET_8BIT;
vesa_bios(®s);
if(VESA_SUCC(regs))
{
vesa_pal_bits = (regs.Client_EBX >> 8) & 0xFF;
}
else
{
vesa_pal_bits = 6;
}
}
else
{
vesa_pal_bits = 6;
}
}
/* test if HW flip supported */
offset_calc(hda->stride, &test_x, &test_y);
regs.Client_EAX = VESA_CMD_DISPLAY_START;
regs.Client_EBX = VESA_DISPLAYSTART_VTRACE;
regs.Client_ECX = test_x;
regs.Client_EDX = test_y;
vesa_bios(®s);
if(VESA_SUCC(regs))
{
screen_mode = SCREEN_FLIP_VSYNC;
regs.Client_EAX = VESA_CMD_DISPLAY_START;
regs.Client_EBX = VESA_DISPLAYSTART_SET;
regs.Client_ECX = 0;
regs.Client_EDX = 0;
vesa_bios(®s);
}
else
{
offset_calc(hda->stride, &test_x, &test_y);
regs.Client_EAX = VESA_CMD_DISPLAY_START;
regs.Client_EBX = VESA_DISPLAYSTART_SET;
regs.Client_ECX = test_x;
regs.Client_EDX = test_y;
vesa_bios(®s);
if(VESA_SUCC(regs))
{
screen_mode = SCREEN_FLIP;
regs.Client_EAX = VESA_CMD_DISPLAY_START;
regs.Client_EBX = VESA_DISPLAYSTART_SET;
regs.Client_ECX = test_x;
regs.Client_EDX = test_y;
vesa_bios(®s);
}
else
{
screen_mode = SCREEN_EMULATED_COPY;
}
}
if(screen_mode <= SCREEN_EMULATED_COPY)
{
hda->system_surface = hda->stride;
hda->surface = hda->stride;
//hda->system_surface = 0;
//hda->flags &= ~FB_SUPPORT_VSYNC;
}
else
{
hda->system_surface = 0;
hda->flags |= FB_SUPPORT_VSYNC;
}
VESA_clear();
mouse_invalidate();
FBHDA_update_heap_size(FALSE);
act_mode = i;
dbg_printf("mode set, mode_id=%d, screen_mode=%d\n", i, screen_mode);
return TRUE;
}
else
{
dbg_printf("vbios fail: eax=0x%lX\n", regs.Client_EAX);
}
}
} // for
dbg_printf("fail to set %ld %ld %ld\n", w, h, bpp);
return FALSE;
}
void FBHDA_palette_set(unsigned char index, DWORD rgb)
{
dbg_printf("PAL: %d:0x%lX,bpp:%d\n", index, rgb, vesa_pal_bits);
if((vesa_caps & VESA_CAP_NONVGA) == 0)
{
/* if mode is VGA compatible, we can use VGA regitry */
outp(VGA_DAC_W_INDEX, index); /* Set starting index. */
if(vesa_pal_bits == 8)
{
outp(VGA_DAC_DATA, (rgb >> 16) & 0xFF);
outp(VGA_DAC_DATA, (rgb >> 8) & 0xFF);
outp(VGA_DAC_DATA, rgb & 0xFF);
}
else
{
outp(VGA_DAC_DATA, (rgb >> 18) & 0x3F);
outp(VGA_DAC_DATA, (rgb >> 10) & 0x3F);
outp(VGA_DAC_DATA, (rgb >> 2) & 0x3F);
}
}
else
{
CRS_32 regs;
DWORD v86_ptr = vesa_buf_v86+PAL_OFFSET+4*index;
if(vesa_pal_bits == 8)
{
vesa_pal[index].Red = (rgb >> 16) & 0xFF;
vesa_pal[index].Green = (rgb >> 8) & 0xFF;
vesa_pal[index].Blue = rgb & 0xFF;
}
else
{
vesa_pal[index].Red = (rgb >> 18) & 0x3F;
vesa_pal[index].Green = (rgb >> 10) & 0x3F;
vesa_pal[index].Blue = (rgb >> 2) & 0x3F;
}
load_client_state(®s);
regs.Client_EAX = VESA_CMD_PALETTE_DATA;
regs.Client_EBX = VESA_RAMDAC_DATA_SET;
regs.Client_ECX = 1;
regs.Client_EDX = index;
regs.Client_ES = V86_SEG(v86_ptr);
regs.Client_EDI = V86_OFF(v86_ptr);
vesa_bios(®s);
}
}
DWORD FBHDA_palette_get(unsigned char index)
{
if((vesa_caps & VESA_CAP_NONVGA) == 0)
{
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);
if(vesa_pal_bits == 8)
{
return (r << 16) | (g << 8) | b;
}
else
{
return (r << 18) | (g << 10) | (b << 2);
}
}
else
{
if(vesa_pal_bits == 8)
{
return
((DWORD)(vesa_pal[index].Red) << 16) |
((DWORD)(vesa_pal[index].Green) << 8) |
(DWORD)(vesa_pal[index].Blue);
}
else
{
return
((DWORD)(vesa_pal[index].Red) << 18) |
((DWORD)(vesa_pal[index].Green) << 10) |
((DWORD)(vesa_pal[index].Blue) << 2);
}
}
return 0;
}
BOOL FBHDA_swap(DWORD offset)
{
if((offset + hda->stride) < hda->vram_size && offset >= hda->system_surface)
{
switch(screen_mode)
{
case SCREEN_EMULATED_CENTER:
case SCREEN_EMULATED_COPY:
FBHDA_access_begin(0);
hda->surface = offset;
FBHDA_access_end(0);
break;
case SCREEN_FLIP:
case SCREEN_FLIP_VSYNC:
{
CRS_32 regs;
DWORD off_x;
DWORD off_y;
if(fb_lock_cnt == 0)
{
mouse_erase();
}
offset_calc(offset, &off_x, &off_y);
load_client_state(®s);
regs.Client_EAX = VESA_CMD_DISPLAY_START;
regs.Client_EBX =
(screen_mode == SCREEN_FLIP_VSYNC) ? VESA_DISPLAYSTART_VTRACE : VESA_DISPLAYSTART_SET;
regs.Client_ECX = off_x;
regs.Client_EDX = off_y;
hda->surface = offset;
vesa_bios(®s);
if(fb_lock_cnt == 0)
{
mouse_blit();
}
break;
}
}
return TRUE;
}
return FALSE;
}
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();
switch(screen_mode)
{
case SCREEN_EMULATED_CENTER:
case SCREEN_EMULATED_COPY:
memcpy(hda->vram_pm32, ((BYTE*)hda->vram_pm32)+hda->surface, hda->stride);
break;
}
// 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)
{
}
|