-
Notifications
You must be signed in to change notification settings - Fork 375
/
bios.cpp
10606 lines (9339 loc) · 437 KB
/
bios.cpp
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
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2002-2021 The DOSBox Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <assert.h>
#include "control.h"
#include "dosbox.h"
#include "mem.h"
#include "cpu.h"
#include "bios.h"
#include "regs.h"
#include "timer.h"
#include "cpu.h"
#include "callback.h"
#include "inout.h"
#include "pic.h"
#include "hardware.h"
#include "pci_bus.h"
#include "joystick.h"
#include "mouse.h"
#include "callback.h"
#include "setup.h"
#include "bios_disk.h"
#include "serialport.h"
#include "mapper.h"
#include "vga.h"
#include "jfont.h"
#include "shiftjis.h"
#include "pc98_gdc.h"
#include "pc98_gdc_const.h"
#include "regionalloctracking.h"
#include "build_timestamp.h"
extern bool PS1AudioCard;
#include "parport.h"
#include "dma.h"
#include "shell.h"
#include "render.h"
#include "sdlmain.h"
#include <time.h>
#include <sys/stat.h>
#if defined(DB_HAVE_CLOCK_GETTIME) && ! defined(WIN32)
//time.h is already included
#else
#include <sys/timeb.h>
#endif
#if C_EMSCRIPTEN
# include <emscripten.h>
#endif
#if defined(_MSC_VER)
# pragma warning(disable:4244) /* const fmath::local::uint64_t to double possible loss of data */
# pragma warning(disable:4305) /* truncation from double to float */
#endif
#if defined(WIN32) && !defined(S_ISREG)
# define S_ISREG(x) ((x & S_IFREG) == S_IFREG)
#endif
std::string ibm_rom_basic;
size_t ibm_rom_basic_size = 0;
uint32_t ibm_rom_basic_base = 0;
/* NTS: The "Epson check" code in Windows 2.1 only compares up to the end of "NEC Corporation" */
const std::string pc98_copyright_str = "Copyright (C) 1983 by NEC Corporation / Microsoft Corp.\x0D\x0A";
/* more strange data involved in the "Epson check" */
const unsigned char pc98_epson_check_2[0x27] = {
0x26,0x8A,0x05,0xA8,0x10,0x75,0x11,0xC6,0x06,0xD6,0x09,0x1B,0xC6,0x06,0xD7,0x09,
0x4B,0xC6,0x06,0xD8,0x09,0x48,0xEB,0x0F,0xC6,0x06,0xD6,0x09,0x1A,0xC6,0x06,0xD7 ,
0x09,0x70,0xC6,0x06,0xD8,0x09,0x71
};
bool enable_pc98_copyright_string = false;
/* mouse.cpp */
extern bool pc98_40col_text;
extern bool en_bios_ps2mouse;
extern bool rom_bios_8x8_cga_font;
extern bool pcibus_enable;
extern bool enable_fpu;
uint32_t Keyb_ig_status();
bool VM_Boot_DOSBox_Kernel();
uint32_t MEM_get_address_bits();
Bitu bios_post_parport_count();
Bitu bios_post_comport_count();
void pc98_update_cpu_page_ptr(void);
bool KEYBOARD_Report_BIOS_PS2Mouse();
bool gdc_5mhz_according_to_bios(void);
void pc98_update_display_page_ptr(void);
void pc98_update_palette(void);
bool MEM_map_ROM_alias_physmem(Bitu start,Bitu end);
void MOUSE_Startup(Section *sec);
void change_output(int output);
void runBoot(const char *str);
void SetIMPosition(void);
bool isDBCSCP();
Bitu INT60_Handler(void);
Bitu INT6F_Handler(void);
#if defined(USE_TTF)
void ttf_switch_on(bool ss), ttf_switch_off(bool ss), ttf_setlines(int cols, int lins);
#endif
/* Rate limit log entries regarding APM AH=05h CPU IDLE because Windows 98's APM driver likes to call it way too much per second */
pic_tickindex_t APM_log_cpu_idle_next_report = 0;
unsigned long APM_log_cpu_idle = 0;
unsigned long APM_WakeupKeys = 0;
/* APM events (eventually ACPI as well) */
unsigned long PowerButtonClicks = 0;
bool APM_ResumeNotificationFromSuspend = false;
bool APM_ResumeNotificationFromStandby = false;
bool APM_PowerButtonSendsSuspend = true;
bool bochs_port_e9 = false;
bool isa_memory_hole_512kb = false;
bool int15_wait_force_unmask_irq = false;
int unhandled_irq_method = UNHANDLED_IRQ_SIMPLE;
unsigned int reset_post_delay = 0;
Bitu call_irq_default = 0;
uint16_t biosConfigSeg=0;
Bitu BIOS_DEFAULT_IRQ0_LOCATION = ~0u; // (RealMake(0xf000,0xfea5))
Bitu BIOS_DEFAULT_IRQ1_LOCATION = ~0u; // (RealMake(0xf000,0xe987))
Bitu BIOS_DEFAULT_IRQ07_DEF_LOCATION = ~0u; // (RealMake(0xf000,0xff55))
Bitu BIOS_DEFAULT_IRQ815_DEF_LOCATION = ~0u; // (RealMake(0xf000,0xe880))
Bitu BIOS_DEFAULT_HANDLER_LOCATION = ~0u; // (RealMake(0xf000,0xff53))
Bitu BIOS_DEFAULT_INT5_LOCATION = ~0u; // (RealMake(0xf000,0xff54))
Bitu BIOS_VIDEO_TABLE_LOCATION = ~0u; // RealMake(0xf000,0xf0a4)
Bitu BIOS_VIDEO_TABLE_SIZE = 0u;
Bitu BIOS_DEFAULT_RESET_LOCATION = ~0u; // RealMake(0xf000,0xe05b)
Bitu BIOS_DEFAULT_RESET_CODE_LOCATION = ~0u;
bool allow_more_than_640kb = false;
unsigned int APM_BIOS_connected_minor_version = 0;// what version the OS connected to us with. default to 1.0
unsigned int APM_BIOS_minor_version = 2; // what version to emulate e.g to emulate 1.2 set this to 2
static bool apm_realmode_connected = false;
/* default bios type/version/date strings */
const char* const bios_type_string = "IBM COMPATIBLE BIOS for DOSBox-X";
const char* const bios_version_string = "DOSBox-X BIOS v1.0";
const char* const bios_date_string = "01/01/92";
bool APM_inactivity_timer = true;
RegionAllocTracking rombios_alloc;
Bitu rombios_minimum_location = 0xF0000; /* minimum segment allowed */
Bitu rombios_minimum_size = 0x10000;
bool MEM_map_ROM_physmem(Bitu start,Bitu end);
bool MEM_unmap_physmem(Bitu start,Bitu end);
static std::string bochs_port_e9_line;
static void bochs_port_e9_flush() {
if (!bochs_port_e9_line.empty()) {
LOG_MSG("Bochs port E9h: %s",bochs_port_e9_line.c_str());
bochs_port_e9_line.clear();
}
}
void bochs_port_e9_write(Bitu port,Bitu val,Bitu /*iolen*/) {
(void)port;//UNUSED
if (val == '\n' || val == '\r') {
bochs_port_e9_flush();
}
else {
bochs_port_e9_line += (char)val;
if (bochs_port_e9_line.length() >= 256)
bochs_port_e9_flush();
}
}
LoopHandler *DOSBOX_GetLoop(void);
static Bitu APM_SuspendedLoopFunc(void);
static RealPt APM_SuspendedLoopRptr=0;
/* "wakeup" keys were pressed and released */
void APM_Suspend_Wakeup_Key(void) {
APM_WakeupKeys++;
}
void APM_SuspendedLoopLeaveState() {
APM_WakeupKeys = 0;
PowerButtonClicks = 0;
/* Turn on the VGA display, assuming it was on when suspended */
if (IS_VGA_ARCH) {
Bitu crtc;
Bitu tmp;
IO_Write(0x3C4,0x01); // clocking mode
tmp = IO_Read(0x3C5);
IO_Write(0x3C5,tmp & (~0x20)); // turn on screen
// NTS: vga_crtc.cpp/vga_draw.cpp does not emulate blanking the display upon disabling sync signals
crtc = (IO_Read(0x3CC) & 1) ? 0x3D4 : 0x3B4;
IO_Write(crtc,0x17); // mode control
tmp = IO_Read(crtc+1);
IO_Write(crtc+1,tmp | 0x80); // enable sync
}
}
/* callback for APM suspended loop routine in BIOS */
static Bitu APM_SuspendedLoopFunc(void) {
if (APM_WakeupKeys != 0 || PowerButtonClicks != 0) {
APM_SuspendedLoopLeaveState();
LOG_MSG("APM: leaving suspended state");
reg_eip += 3; /* skip over HLT+JMP to get to RET */
return CBRET_NONE;
}
return CBRET_NONE;
}
bool PowerManagementEnabledButton() {
if (IS_PC98_ARCH) /* power management not yet known or implemented */
return false;
if (apm_realmode_connected) /* guest has connected to the APM BIOS */
return true;
return false;
}
void APM_BeginSuspendedMode(void) {
/* Enable interrupts, dumbass. We use HLT here. */
CPU_STI();
/* WARNING: This assumes, like all callbacks, that they reside in the same segment.
* The APM BIOS entry point can be called from real mode, 16-bit or 32-bit protected mode,
* therefore this code cannot assume any particular segment value and can only safely
* change reg_(e)ip to direct CPU execution. The most likely scenario is that the
* return address pushed here will point at the IRET in the INT 15 handler. */
if (cpu.stack.big) CPU_Push32(reg_eip);
else CPU_Push16(reg_ip);
reg_ip = APM_SuspendedLoopRptr & 0xFFFF; /* offset only */
/* reset counters */
PowerButtonClicks = 0;
APM_WakeupKeys = 0;
/* Turn off the VGA display */
if (IS_VGA_ARCH) {
Bitu crtc;
Bitu tmp;
IO_Write(0x3C4,0x01); // clocking mode
tmp = IO_Read(0x3C5);
IO_Write(0x3C5,tmp | 0x20); // turn off screen
// NTS: vga_crtc.cpp/vga_draw.cpp does not emulate blanking the display upon disabling sync signals
crtc = (IO_Read(0x3CC) & 1) ? 0x3D4 : 0x3B4;
IO_Write(crtc,0x17); // mode control
tmp = IO_Read(crtc+1);
IO_Write(crtc+1,tmp & (~0x80)); // disable sync
}
LOG_MSG("System is now in APM suspend mode");
}
void ROMBIOS_DumpMemory() {
rombios_alloc.logDump();
}
void ROMBIOS_SanityCheck() {
rombios_alloc.sanityCheck();
}
Bitu ROMBIOS_MinAllocatedLoc() {
Bitu r = rombios_alloc.getMinAddress();
if (r > (0x100000u - rombios_minimum_size))
r = (0x100000u - rombios_minimum_size);
return r & ~0xFFFu;
}
void ROMBIOS_FreeUnusedMinToLoc(Bitu phys) {
Bitu new_phys;
if (rombios_minimum_location & 0xFFF) E_Exit("ROMBIOS: FreeUnusedMinToLoc minimum location not page aligned");
phys &= ~0xFFFUL;
new_phys = rombios_alloc.freeUnusedMinToLoc(phys) & (~0xFFFUL);
assert(new_phys >= phys);
if (phys < new_phys) MEM_unmap_physmem(phys,new_phys-1);
rombios_minimum_location = new_phys;
ROMBIOS_SanityCheck();
ROMBIOS_DumpMemory();
}
bool ROMBIOS_FreeMemory(Bitu phys) {
return rombios_alloc.freeMemory(phys);
}
Bitu ROMBIOS_GetMemory(Bitu bytes,const char *who,Bitu alignment,Bitu must_be_at) {
return rombios_alloc.getMemory(bytes,who,alignment,must_be_at);
}
void ROMBIOS_InitForCustomBIOS(void) {
rombios_alloc.initSetRange(0xD8000,0xE0000);
}
static IO_Callout_t dosbox_int_iocallout = IO_Callout_t_none;
static unsigned char dosbox_int_register_shf = 0;
static uint32_t dosbox_int_register = 0;
static unsigned char dosbox_int_regsel_shf = 0;
static uint32_t dosbox_int_regsel = 0;
static bool dosbox_int_error = false;
static bool dosbox_int_busy = false;
static const char *dosbox_int_version = "DOSBox-X integration device v1.0";
static const char *dosbox_int_ver_read = NULL;
struct dosbox_int_saved_state {
unsigned char dosbox_int_register_shf;
uint32_t dosbox_int_register;
unsigned char dosbox_int_regsel_shf;
uint32_t dosbox_int_regsel;
bool dosbox_int_error;
bool dosbox_int_busy;
};
#define DOSBOX_INT_SAVED_STATE_MAX 4
struct dosbox_int_saved_state dosbox_int_saved[DOSBOX_INT_SAVED_STATE_MAX];
int dosbox_int_saved_sp = -1;
/* for use with interrupt handlers in DOS/Windows that need to save IG state
* to ensure that IG state is restored on return in order to not interfere
* with anything userspace is doing (as an alternative to wrapping all access
* in CLI/STI or PUSHF/CLI/POPF) */
bool dosbox_int_push_save_state(void) {
if (dosbox_int_saved_sp >= (DOSBOX_INT_SAVED_STATE_MAX-1))
return false;
struct dosbox_int_saved_state *ss = &dosbox_int_saved[++dosbox_int_saved_sp];
ss->dosbox_int_register_shf = dosbox_int_register_shf;
ss->dosbox_int_register = dosbox_int_register;
ss->dosbox_int_regsel_shf = dosbox_int_regsel_shf;
ss->dosbox_int_regsel = dosbox_int_regsel;
ss->dosbox_int_error = dosbox_int_error;
ss->dosbox_int_busy = dosbox_int_busy;
return true;
}
bool dosbox_int_pop_save_state(void) {
if (dosbox_int_saved_sp < 0)
return false;
struct dosbox_int_saved_state *ss = &dosbox_int_saved[dosbox_int_saved_sp--];
dosbox_int_register_shf = ss->dosbox_int_register_shf;
dosbox_int_register = ss->dosbox_int_register;
dosbox_int_regsel_shf = ss->dosbox_int_regsel_shf;
dosbox_int_regsel = ss->dosbox_int_regsel;
dosbox_int_error = ss->dosbox_int_error;
dosbox_int_busy = ss->dosbox_int_busy;
return true;
}
bool dosbox_int_discard_save_state(void) {
if (dosbox_int_saved_sp < 0)
return false;
dosbox_int_saved_sp--;
return true;
}
extern bool user_cursor_locked;
extern int user_cursor_x,user_cursor_y;
extern int user_cursor_sw,user_cursor_sh;
extern int master_cascade_irq,bootdrive;
extern bool enable_slave_pic;
extern bool bootguest, use_quick_reboot;
bool bootvm = false, bootfast = false;
static std::string dosbox_int_debug_out;
void VGA_SetCaptureStride(uint32_t v);
void VGA_SetCaptureAddress(uint32_t v);
void VGA_SetCaptureState(uint32_t v);
SDL_Rect &VGA_CaptureRectCurrent(void);
SDL_Rect &VGA_CaptureRectFromGuest(void);
uint32_t VGA_QueryCaptureAddress(void);
uint32_t VGA_QueryCaptureState(void);
uint32_t VGA_QuerySizeIG(void);
uint32_t Mixer_MIXQ(void);
uint32_t Mixer_MIXC(void);
void Mixer_MIXC_Write(uint32_t v);
PhysPt Mixer_MIXWritePos(void);
void Mixer_MIXWritePos_Write(PhysPt np);
void Mixer_MIXWriteBegin_Write(PhysPt np);
void Mixer_MIXWriteEnd_Write(PhysPt np);
/* read triggered, update the regsel */
void dosbox_integration_trigger_read() {
dosbox_int_error = false;
switch (dosbox_int_regsel) {
case 0: /* Identification */
dosbox_int_register = 0xD05B0740;
break;
case 1: /* test */
break;
case 2: /* version string */
if (dosbox_int_ver_read == NULL)
dosbox_int_ver_read = dosbox_int_version;
dosbox_int_register = 0;
for (Bitu i=0;i < 4;i++) {
if (*dosbox_int_ver_read == 0) {
dosbox_int_ver_read = dosbox_int_version;
break;
}
dosbox_int_register += ((uint32_t)((unsigned char)(*dosbox_int_ver_read++))) << (uint32_t)(i * 8);
}
break;
case 3: /* version number */
dosbox_int_register = (0x01U/*major*/) + (0x00U/*minor*/ << 8U) + (0x00U/*subver*/ << 16U) + (0x01U/*bump*/ << 24U);
break;
case 4: /* current emulator time as 16.16 fixed point */
dosbox_int_register = (uint32_t)(PIC_FullIndex() * 0x10000);
break;
case 0x5158494D: /* query mixer output 'MIXQ' */
/* bits [19:0] = sample rate in Hz or 0 if mixer is not mixing AT ALL
* bits [23:20] = number of channels (at this time, always 2 aka stereo)
* bits [29:29] = 1=swap stereo 0=normal
* bits [30:30] = 1=muted 0=not muted
* bits [31:31] = 1=sound 0=nosound */
dosbox_int_register = Mixer_MIXQ();
break;
case 0x4358494D: /* query mixer output 'MIXC' */
dosbox_int_register = Mixer_MIXC();
break;
case 0x5058494D: /* query mixer output 'MIXP' */
dosbox_int_register = Mixer_MIXWritePos();
break;
case 0x4258494D: /* query mixer output 'MIXB' */
break;
case 0x4558494D: /* query mixer output 'MIXE' */
break;
case 0x6845C0: /* query VGA display size */
dosbox_int_register = VGA_QuerySizeIG();
break;
case 0x6845C1: /* query VGA capture state */
dosbox_int_register = VGA_QueryCaptureState();
break;
case 0x6845C2: /* query VGA capture address (what is being captured to NOW) */
dosbox_int_register = VGA_QueryCaptureAddress();
break;
case 0x6845C3: /* query VGA capture current crop rectangle (position) will not reflect new rectangle until VGA capture finishes capture. */
{
const SDL_Rect &r = VGA_CaptureRectCurrent();
dosbox_int_register = ((uint32_t)r.y << (uint32_t)16ul) + (uint32_t)r.x;
}
break;
case 0x6845C4: /* query VGA capture current crop rectangle (size). will not reflect new rectangle until VGA capture finishes capture. */
{
const SDL_Rect &r = VGA_CaptureRectCurrent();
dosbox_int_register = ((uint32_t)r.h << (uint32_t)16ul) + (uint32_t)r.w;
}
break;
case 0x825901: /* PIC configuration */
/* bits [7:0] = cascade interrupt or 0xFF if none
* bit [8:8] = primary PIC present
* bit [9:9] = secondary PIC present */
if (master_cascade_irq >= 0)
dosbox_int_register = ((unsigned int)master_cascade_irq & 0xFFu);
else
dosbox_int_register = 0xFFu;
dosbox_int_register |= 0x100; // primary always present
if (enable_slave_pic) dosbox_int_register |= 0x200;
break;
case 0x823780: /* ISA DMA injection, single byte/word (read from memory) */
break;
// case 0x804200: /* keyboard input injection -- not supposed to read */
// break;
case 0x804201: /* keyboard status */
dosbox_int_register = Keyb_ig_status();
break;
case 0x434D54: /* read user mouse status */
dosbox_int_register =
(user_cursor_locked ? (1UL << 0UL) : 0UL); /* bit 0 = mouse capture lock */
break;
case 0x434D55: /* read user mouse cursor position */
dosbox_int_register = (uint32_t((uint16_t)user_cursor_y & 0xFFFFUL) << 16UL) | uint32_t((uint16_t)user_cursor_x & 0xFFFFUL);
break;
case 0x434D56: { /* read user mouse cursor position (normalized for Windows 3.x) */
signed long long x = ((signed long long)user_cursor_x << 16LL) / (signed long long)(user_cursor_sw-1);
signed long long y = ((signed long long)user_cursor_y << 16LL) / (signed long long)(user_cursor_sh-1);
if (x < 0x0000LL) x = 0x0000LL;
if (x > 0xFFFFLL) x = 0xFFFFLL;
if (y < 0x0000LL) y = 0x0000LL;
if (y > 0xFFFFLL) y = 0xFFFFLL;
dosbox_int_register = ((unsigned int)y << 16UL) | (unsigned int)x;
} break;
case 0xC54010: /* Screenshot/capture trigger */
/* TODO: This should also be hidden behind an enable switch, so that rogue DOS development
* can't retaliate if the user wants to capture video or screenshots. */
#if (C_SSHOT)
dosbox_int_register = 0x00000000; // available
if (CaptureState & CAPTURE_IMAGE)
dosbox_int_register |= 1 << 0; // Image capture is in progress
if (CaptureState & CAPTURE_VIDEO)
dosbox_int_register |= 1 << 1; // Video capture is in progress
if (CaptureState & CAPTURE_WAVE)
dosbox_int_register |= 1 << 2; // WAVE capture is in progress
#else
dosbox_int_register = 0xC0000000; // not available (bit 31 set), not enabled (bit 30 set)
#endif
break;
case 0xAA55BB66UL: /* interface reset result */
break;
default:
dosbox_int_register = 0xAA55AA55;
dosbox_int_error = true;
break;
}
LOG(LOG_MISC,LOG_DEBUG)("DOSBox-X integration read 0x%08lx got 0x%08lx (err=%u)\n",
(unsigned long)dosbox_int_regsel,
(unsigned long)dosbox_int_register,
dosbox_int_error?1:0);
}
bool watchdog_set = false;
void Watchdog_Timeout_Event(Bitu /*val*/) {
LOG_MSG("Watchdog timeout occurred");
CPU_Raise_NMI();
}
void Watchdog_Timer_Clear(void) {
if (watchdog_set) {
PIC_RemoveEvents(Watchdog_Timeout_Event);
watchdog_set = false;
}
}
void Watchdog_Timer_Set(uint32_t timeout_ms) {
Watchdog_Timer_Clear();
if (timeout_ms != 0) {
watchdog_set = true;
PIC_AddEvent(Watchdog_Timeout_Event,(double)timeout_ms);
}
}
unsigned int mouse_notify_mode = 0;
// 0 = off
// 1 = trigger as PS/2 mouse interrupt
/* write triggered */
void dosbox_integration_trigger_write() {
dosbox_int_error = false;
LOG(LOG_MISC,LOG_DEBUG)("DOSBox-X integration write 0x%08lx val 0x%08lx\n",
(unsigned long)dosbox_int_regsel,
(unsigned long)dosbox_int_register);
switch (dosbox_int_regsel) {
case 1: /* test */
break;
case 2: /* version string */
dosbox_int_ver_read = NULL;
break;
case 0xDEB0: /* debug output (to log) */
for (unsigned int b=0;b < 4;b++) {
unsigned char c = (unsigned char)(dosbox_int_register >> (b * 8U));
if (c == '\n' || dosbox_int_debug_out.length() >= 200) {
LOG_MSG("Client debug message: %s\n",dosbox_int_debug_out.c_str());
dosbox_int_debug_out.clear();
}
else if (c != 0) {
dosbox_int_debug_out += ((char)c);
}
else {
break;
}
}
dosbox_int_register = 0;
break;
case 0xDEB1: /* debug output clear */
dosbox_int_debug_out.clear();
break;
case 0x6845C1: /* query VGA capture state */
VGA_SetCaptureState(dosbox_int_register);
break;
case 0x6845C2: /* set VGA capture address, will be applied to next capture */
VGA_SetCaptureAddress(dosbox_int_register);
break;
case 0x6845C3: /* set VGA capture crop rectangle (position), will be applied to next capture */
{
SDL_Rect &r = VGA_CaptureRectFromGuest();
r.x = (int)(dosbox_int_register & 0xFFFF);
r.y = (int)(dosbox_int_register >> 16ul);
}
break;
case 0x6845C4: /* set VGA capture crop rectangle (size), will be applied to next capture */
{
SDL_Rect &r = VGA_CaptureRectFromGuest();
r.w = (int)(dosbox_int_register & 0xFFFF);
r.h = (int)(dosbox_int_register >> 16ul);
}
break;
case 0x6845C5: /* set VGA capture stride (bytes per scan line) */
VGA_SetCaptureStride(dosbox_int_register);
break;
case 0x52434D: /* release mouse capture 'MCR' */
void GFX_ReleaseMouse(void);
GFX_ReleaseMouse();
break;
case 0x5158494D: /* query mixer output 'MIXQ' */
break;
case 0x4358494D: /* query mixer output 'MIXC' */
Mixer_MIXC_Write(dosbox_int_register);
break;
case 0x5058494D: /* query mixer output 'MIXP' */
Mixer_MIXWritePos_Write(dosbox_int_register);
break;
case 0x4258494D: /* query mixer output 'MIXB' */
Mixer_MIXWriteBegin_Write(dosbox_int_register);
break;
case 0x4558494D: /* query mixer output 'MIXE' */
Mixer_MIXWriteEnd_Write(dosbox_int_register);
break;
case 0x57415444: /* Set/clear watchdog timer 'WATD' */
Watchdog_Timer_Set(dosbox_int_register);
break;
case 0x808602: /* NMI (INT 02h) interrupt injection */
{
dosbox_int_register_shf = 0;
dosbox_int_regsel_shf = 0;
CPU_Raise_NMI();
}
break;
case 0x825900: /* PIC interrupt injection */
{
dosbox_int_register_shf = 0;
dosbox_int_regsel_shf = 0;
/* bits [7:0] = IRQ to signal (must be 0-15)
* bit [8:8] = 1=raise 0=lower IRQ */
uint8_t IRQ = dosbox_int_register&0xFFu;
bool raise = (dosbox_int_register>>8u)&1u;
if (IRQ < 16) {
if (raise)
PIC_ActivateIRQ(IRQ);
else
PIC_DeActivateIRQ(IRQ);
}
}
break;
case 0x823700: /* ISA DMA injection, single byte/word (write to memory) */
{
dosbox_int_register_shf = 0;
dosbox_int_regsel_shf = 0;
/* bits [7:0] = data byte if 8-bit DNA
* bits [15:0] = data word if 16-bit DMA
* bits [18:16] = DMA channel to send to */
DmaChannel *ch = GetDMAChannel(((unsigned int)dosbox_int_register>>16u)&7u);
if (ch != NULL) {
unsigned char tmp[2];
tmp[0] = (unsigned char)( dosbox_int_register & 0xFFu);
tmp[1] = (unsigned char)((dosbox_int_register >> 8u) & 0xFFu);
/* NTS: DMA channel write will write tmp[0] if 8-bit, tmp[0]/tmp[1] if 16-bit */
if (ch->Write(1/*one unit of transfer*/,tmp) == 1) {
dosbox_int_register = 0;
dosbox_int_error = false;
}
else {
dosbox_int_register = 0x823700;
dosbox_int_error = true;
}
}
else {
dosbox_int_register = 0x8237FF;
dosbox_int_error = true;
}
}
break;
case 0x823780: /* ISA DMA injection, single byte/word (read from memory) */
{
dosbox_int_register_shf = 0;
dosbox_int_regsel_shf = 0;
/* bits [18:16] = DMA channel to send to */
DmaChannel *ch = GetDMAChannel(((unsigned int)dosbox_int_register>>16u)&7u);
if (ch != NULL) {
unsigned char tmp[2];
/* NTS: DMA channel write will write tmp[0] if 8-bit, tmp[0]/tmp[1] if 16-bit */
tmp[0] = tmp[1] = 0;
if (ch->Read(1/*one unit of transfer*/,tmp) == 1) {
dosbox_int_register = ((unsigned int)tmp[1] << 8u) + (unsigned int)tmp[0];
dosbox_int_error = false;
}
else {
dosbox_int_register = 0x823700;
dosbox_int_error = true;
}
}
else {
dosbox_int_register = 0x8237FF;
dosbox_int_error = true;
}
}
break;
case 0x804200: /* keyboard input injection */
void Mouse_ButtonPressed(uint8_t button);
void Mouse_ButtonReleased(uint8_t button);
void pc98_keyboard_send(const unsigned char b);
void Mouse_CursorMoved(float xrel,float yrel,float x,float y,bool emulate);
void KEYBOARD_AUX_Event(float x,float y,Bitu buttons,int scrollwheel);
void KEYBOARD_AddBuffer(uint16_t data);
switch ((dosbox_int_register>>8)&0xFF) {
case 0x00: // keyboard
if (IS_PC98_ARCH)
pc98_keyboard_send(dosbox_int_register&0xFF);
else
KEYBOARD_AddBuffer(dosbox_int_register&0xFF);
break;
case 0x01: // AUX
if (!IS_PC98_ARCH)
KEYBOARD_AddBuffer((dosbox_int_register&0xFF)|0x100/*AUX*/);
else // no such interface in PC-98 mode
dosbox_int_error = true;
break;
case 0x08: // mouse button injection
if (dosbox_int_register&0x80) Mouse_ButtonPressed(dosbox_int_register&0x7F);
else Mouse_ButtonReleased(dosbox_int_register&0x7F);
break;
case 0x09: // mouse movement injection (X)
Mouse_CursorMoved(((dosbox_int_register>>16UL) / 256.0f) - 1.0f,0,0,0,true);
break;
case 0x0A: // mouse movement injection (Y)
Mouse_CursorMoved(0,((dosbox_int_register>>16UL) / 256.0f) - 1.0f,0,0,true);
break;
case 0x0B: // mouse scrollwheel injection
// TODO
break;
default:
dosbox_int_error = true;
break;
}
break;
// case 0x804201: /* keyboard status do not write */
// break;
/* this command is used to enable notification of mouse movement over the windows even if the mouse isn't captured */
case 0x434D55: /* read user mouse cursor position */
case 0x434D56: /* read user mouse cursor position (normalized for Windows 3.x) */
mouse_notify_mode = dosbox_int_register & 0xFF;
LOG(LOG_MISC,LOG_DEBUG)("Mouse notify mode=%u",mouse_notify_mode);
break;
case 0xC54010: /* Screenshot/capture trigger */
#if (C_SSHOT)
void CAPTURE_ScreenShotEvent(bool pressed);
void CAPTURE_VideoEvent(bool pressed);
#endif
void CAPTURE_WaveEvent(bool pressed);
/* TODO: It would be wise to grant/deny access to this register through another dosbox-x.conf option
* so that rogue DOS development cannot shit-spam the capture folder */
#if (C_SSHOT)
if (dosbox_int_register & 1)
CAPTURE_ScreenShotEvent(true);
if (dosbox_int_register & 2)
CAPTURE_VideoEvent(true);
#endif
if (dosbox_int_register & 4)
CAPTURE_WaveEvent(true);
break;
default:
dosbox_int_register = 0x55AA55AA;
dosbox_int_error = true;
break;
}
}
/* PORT 0x28: Index
* 0x29: Data
* 0x2A: Status(R) or Command(W)
* 0x2B: Not yet assigned
*
* Registers are 32-bit wide. I/O to index and data rotate through the
* bytes of the register depending on I/O length, meaning that one 32-bit
* I/O read will read the entire register, while four 8-bit reads will
* read one byte out of 4. */
static Bitu dosbox_integration_port00_index_r(Bitu port,Bitu iolen) {
(void)port;//UNUSED
Bitu retb = 0;
Bitu ret = 0;
while (iolen > 0) {
ret += ((dosbox_int_regsel >> (dosbox_int_regsel_shf * 8)) & 0xFFU) << (retb * 8);
if ((++dosbox_int_regsel_shf) >= 4) dosbox_int_regsel_shf = 0;
iolen--;
retb++;
}
return ret;
}
static void dosbox_integration_port00_index_w(Bitu port,Bitu val,Bitu iolen) {
(void)port;//UNUSED
while (iolen > 0) {
uint32_t msk = 0xFFU << (dosbox_int_regsel_shf * 8);
dosbox_int_regsel = (dosbox_int_regsel & ~msk) + ((val & 0xFF) << (dosbox_int_regsel_shf * 8));
if ((++dosbox_int_regsel_shf) >= 4) dosbox_int_regsel_shf = 0;
val >>= 8U;
iolen--;
}
}
static Bitu dosbox_integration_port01_data_r(Bitu port,Bitu iolen) {
(void)port;//UNUSED
Bitu retb = 0;
Bitu ret = 0;
while (iolen > 0) {
if (dosbox_int_register_shf == 0) dosbox_integration_trigger_read();
ret += ((dosbox_int_register >> (dosbox_int_register_shf * 8)) & 0xFFU) << (retb * 8);
if ((++dosbox_int_register_shf) >= 4) dosbox_int_register_shf = 0;
iolen--;
retb++;
}
return ret;
}
static void dosbox_integration_port01_data_w(Bitu port,Bitu val,Bitu iolen) {
(void)port;//UNUSED
while (iolen > 0) {
uint32_t msk = 0xFFU << (dosbox_int_register_shf * 8);
dosbox_int_register = (dosbox_int_register & ~msk) + ((val & 0xFF) << (dosbox_int_register_shf * 8));
if ((++dosbox_int_register_shf) >= 4) dosbox_int_register_shf = 0;
if (dosbox_int_register_shf == 0) dosbox_integration_trigger_write();
val >>= 8U;
iolen--;
}
}
static Bitu dosbox_integration_port02_status_r(Bitu port,Bitu iolen) {
(void)iolen;//UNUSED
(void)port;//UNUSED
/* status */
/* 7:6 = regsel byte index
* 5:4 = register byte index
* 3:2 = reserved
* 1 = error
* 0 = busy */
return
((unsigned int)dosbox_int_regsel_shf << 6u) + ((unsigned int)dosbox_int_register_shf << 4u) +
(dosbox_int_error ? 2u : 0u) + (dosbox_int_busy ? 1u : 0u);
}
static void dosbox_integration_port02_command_w(Bitu port,Bitu val,Bitu iolen) {
(void)port;
(void)iolen;
switch (val) {
case 0x00: /* reset latch */
dosbox_int_register_shf = 0;
dosbox_int_regsel_shf = 0;
break;
case 0x01: /* flush write */
if (dosbox_int_register_shf != 0) {
dosbox_integration_trigger_write();
dosbox_int_register_shf = 0;
}
break;
case 0x20: /* push state */
if (dosbox_int_push_save_state()) {
dosbox_int_register_shf = 0;
dosbox_int_regsel_shf = 0;
dosbox_int_error = false;
dosbox_int_busy = false;
dosbox_int_regsel = 0xAA55BB66;
dosbox_int_register = 0xD05B0C5;
LOG(LOG_MISC,LOG_DEBUG)("DOSBOX-X IG state saved");
}
else {
LOG(LOG_MISC,LOG_DEBUG)("DOSBOX-X IG unable to push state, stack overflow");
dosbox_int_error = true;
}
break;
case 0x21: /* pop state */
if (dosbox_int_pop_save_state()) {
LOG(LOG_MISC,LOG_DEBUG)("DOSBOX-X IG state restored");
}
else {
LOG(LOG_MISC,LOG_DEBUG)("DOSBOX-X IG unable to pop state, stack underflow");
dosbox_int_error = true;
}
break;
case 0x22: /* discard state */
if (dosbox_int_discard_save_state()) {
LOG(LOG_MISC,LOG_DEBUG)("DOSBOX-X IG state discarded");
}
else {
LOG(LOG_MISC,LOG_DEBUG)("DOSBOX-X IG unable to discard state, stack underflow");
dosbox_int_error = true;
}
break;
case 0x23: /* discard all state */
while (dosbox_int_discard_save_state());
break;
case 0xFE: /* clear error */
dosbox_int_error = false;
break;
case 0xFF: /* reset interface */
dosbox_int_busy = false;
dosbox_int_error = false;
dosbox_int_regsel = 0xAA55BB66;
dosbox_int_register = 0xD05B0C5;