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 | /* GATE PROJECT LICENSE:
+----------------------------------------------------------------------------+
| Copyright(c) 2018-2025, Stefan Meislinger <sm@opengate.at> |
| All rights reserved. |
| |
| Redistribution and use in source and binary forms, with or without |
| modification, are permitted provided that the following conditions are met:|
| |
| 1. Redistributions of source code must retain the above copyright notice, |
| this list of conditions and the following disclaimer. |
| 2. Redistributions in binary form must reproduce the above copyright |
| notice, this list of conditions and the following disclaimer in the |
| documentation and/or other materials provided with the distribution. |
| |
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"|
| AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| THE POSSIBILITY OF SUCH DAMAGE. |
+----------------------------------------------------------------------------+
*/
#include "gate/platforms.h"
#if defined(GATE_SYS_DOS)
#include "gate/results.h"
#include "gate/strings.h"
#include "gate/coroutines.h"
#include "gate/inputs.h"
#include <errno.h>
#include <string.h>
#include <i86.h>
#include <dos.h>
#include <bios.h>
#include <conio.h>
#include <fcntl.h>
#include <share.h>
#include <signal.h>
#include <stdlib.h>
gate_result_t gate_platform_init(void)
{
gate_dos_clock_init();
return GATE_RESULT_OK;
}
void gate_platform_exit(int exit_code)
{
_Exit(exit_code);
}
void gate_platform_atexit(void(*func)(void))
{
atexit(func);
}
gate_result_t gate_platform_lock()
{
return GATE_RESULT_OK;
}
gate_result_t gate_platform_unlock()
{
return GATE_RESULT_OK;
}
gate_int32_t gate_platform_get_last_error()
{
return (errno);
}
gate_result_t gate_platform_set_last_error(gate_int32_t platform_error_code)
{
errno = (int)platform_error_code;
return GATE_RESULT_OK;
}
static gate_result_t gate_dos_convert_errno(int error_code)
{
gate_result_t ret = GATE_RESULT_FAILED;
switch (error_code)
{
case 0: ret = GATE_RESULT_OK; break;
case E2BIG: ret = GATE_RESULT_OUTOFBOUNDS; break;
case EACCES: ret = GATE_RESULT_ACCESSDENIED; break;
/*case EWOULDBLOCK: ret = GATE_RESULT_BLOCKED; break;*/
case EAGAIN: ret = GATE_RESULT_BLOCKED; break;
case EBADF: ret = GATE_RESULT_INVALIDDATA; break;
case EBUSY: ret = GATE_RESULT_NOTREADY; break;
case ECHILD: ret = GATE_RESULT_EXECUTIONFAILED; break;
case EDEADLK: ret = GATE_RESULT_LOCKED; break;
case EDOM: ret = GATE_RESULT_ARITHMETICERROR; break;
/*case EDQUOT: ret = GATE_RESULT_FAILED; break;*/
case EEXIST: ret = GATE_RESULT_ALREADYEXISTS; break;
case EFAULT: ret = GATE_RESULT_BADADDRESS; break;
case EFBIG: ret = GATE_RESULT_OUTOFBOUNDS; break;
case EILSEQ: ret = GATE_RESULT_INVALIDDATA; break;
case EINTR: ret = GATE_RESULT_EXECUTIONINTERRUPTED; break;
case EINVAL: ret = GATE_RESULT_INVALIDARG; break;
case EIO: ret = GATE_RESULT_INVALIDDATA; break;
case EISDIR: ret = GATE_RESULT_INVALIDSTATE; break;
case EMFILE: ret = GATE_RESULT_OUTOFRESOURCES; break;
case EMLINK: ret = GATE_RESULT_OUTOFRESOURCES; break;
/*case EMULTIHOP: ret = GATE_RESULT_FAILED; break;*/
case ENAMETOOLONG: ret = GATE_RESULT_OUTOFBOUNDS; break;
case ENFILE: ret = GATE_RESULT_OUTOFRESOURCES; break;
#ifdef ENODATA
case ENODATA: ret = GATE_RESULT_INVALIDCONTENT; break;<--- Skipping configuration 'ENODATA;GATE_SYS_DOS' since the value of 'ENODATA' is unknown. Use -D if you want to check it. You can use -U to skip it explicitly.
#endif
case ENODEV: ret = GATE_RESULT_NOTAVAILABLE; break;
case ENOENT: ret = GATE_RESULT_NOTAVAILABLE; break;
case ENOEXEC: ret = GATE_RESULT_INVALIDHEADER; break;
case ENOLCK: ret = GATE_RESULT_NOTAVAILABLE; break;
/*case ENOLINK: ret = GATE_RESULT_; break;*/
case ENOMEM: ret = GATE_RESULT_OUTOFMEMORY; break;
case ENOSPC: ret = GATE_RESULT_OUTOFRESOURCES; break;
#ifdef ENOSR
case ENOSR: ret = GATE_RESULT_NOTAVAILABLE; break;<--- Skipping configuration 'ENOSR;GATE_SYS_DOS' since the value of 'ENOSR' is unknown. Use -D if you want to check it. You can use -U to skip it explicitly.
#endif
#ifdef ENOSTR
case ENOSTR: ret = GATE_RESULT_NOTAVAILABLE; break;<--- Skipping configuration 'ENOSTR;GATE_SYS_DOS' since the value of 'ENOSTR' is unknown. Use -D if you want to check it. You can use -U to skip it explicitly.
#endif
case ENOSYS: ret = GATE_RESULT_NOTSUPPORTED; break;
case ENOTDIR: ret = GATE_RESULT_INVALIDSTATE; break;
case ENOTEMPTY: ret = GATE_RESULT_INVALIDSTATE; break;
case ENOTTY: ret = GATE_RESULT_INCORRECTTYPE; break;
case ENXIO: ret = GATE_RESULT_NOTAVAILABLE; break;
case EPERM: ret = GATE_RESULT_PERMISSIONDENIED; break;
case EPIPE: ret = GATE_RESULT_NOTREADY; break;
case ERANGE: ret = GATE_RESULT_OUTOFBOUNDS; break;
case EROFS: ret = GATE_RESULT_LOCKED; break;
case ESPIPE: ret = GATE_RESULT_INVALIDARG; break;
case ESRCH: ret = GATE_RESULT_NOMATCH; break;
/*case ESTALE: ret = GATE_RESULT_FAILED; break;*/
#ifdef ETIME
case ETIME: ret = GATE_RESULT_TIMEOUT; break;<--- Skipping configuration 'ETIME;GATE_SYS_DOS' since the value of 'ETIME' is unknown. Use -D if you want to check it. You can use -U to skip it explicitly.
#endif
case ETXTBSY: ret = GATE_RESULT_NOTREADY; break;
case EXDEV: ret = GATE_RESULT_NOTSUPPORTED; break;
default: ret = GATE_RESULT_FAILED; break;
}
return ret;
}
gate_result_t gate_platform_print_error(gate_int32_t platform_error_code, char* buffer, gate_size_t buffer_len)
{
char* error_text = NULL;
if ((buffer != NULL) && (buffer_len != 0))
{
error_text = strerror((int)platform_error_code);
gate_str_print_text(buffer, buffer_len, error_text, gate_str_length(error_text));
}
return gate_dos_convert_errno(platform_error_code);
}
gate_bool_t gate_platform_stream_valid(gate_platform_stream_t strm)
{
int fd = (int)(gate_intptr_t)strm;
return (fd >= 0) && (fd < 65535);
}
void gate_platform_stream_set_invalid(gate_platform_stream_t* ptr_strm)
{
*ptr_strm = (gate_platform_stream_t)(gate_intptr_t)(-1);
}
gate_result_t gate_platform_stream_open(char const* path, int open_flags, int native_flags, gate_platform_stream_t* strm)
{
int handle;
unsigned result;
(void)native_flags;
switch (open_flags)
{
case GATE_PLATFORM_STREAM_OPEN_READ:
{
result = _dos_open(path, O_RDONLY, &handle);
break;
}
case GATE_PLATFORM_STREAM_OPEN_WRITE:
{
result = _dos_creat(path, _A_NORMAL, &handle);
break;
}
case GATE_PLATFORM_STREAM_OPEN_READWRITE:
{
result = _dos_open(path, O_RDWR, &handle);
break;
}
default:
{
return GATE_RESULT_NOTSUPPORTED;
}
}
if (0 != result)
{
return gate_platform_print_last_error(NULL, 0);
}
*strm = (gate_platform_stream_t)(gate_intptr_t)handle;
return GATE_RESULT_OK;
}
gate_result_t gate_platform_stream_read(gate_platform_stream_t strm, char* buffer, gate_size_t bufferlength, gate_size_t* returned)
{
int handle = (int)(gate_intptr_t)strm;
unsigned count = (unsigned)((bufferlength > 65535) ? 65535 : bufferlength);
unsigned retrieved = 0;
unsigned result = _dos_read(handle, buffer, count, &retrieved);
if (result != 0)
{
return gate_platform_print_last_error(NULL, 0);
}
if (returned)
{
*returned = retrieved;
}
return GATE_RESULT_OK;
}
gate_result_t gate_platform_stream_write(gate_platform_stream_t strm, char const* buffer, gate_size_t bufferlength, gate_size_t* written)
{
int handle = (int)(gate_intptr_t)strm;
unsigned count = (unsigned)((bufferlength > 65535) ? 65535 : bufferlength);
unsigned transmitted = 0;
unsigned result = _dos_write(handle, buffer, count, &transmitted);
if (result != 0)
{
return gate_platform_print_last_error(NULL, 0);
}
if (written)
{
*written = transmitted;
}
return GATE_RESULT_OK;
}
gate_result_t gate_platform_stream_seek(gate_platform_stream_t strm, gate_int64_t position, int origin, gate_int64_t* final_position)
{
union REGS regs;
int result;
int handle = (int)(gate_intptr_t)strm;
gate_uint32_t new_pos = (gate_uint32_t)(gate_int32_t)position;
regs.h.ah = 0x42;
if (origin == GATE_PLATFORM_STREAM_ORIGIN_END)
{
regs.h.al = 2;
}
else if (origin == GATE_PLATFORM_STREAM_ORIGIN_CURRENT)
{
regs.h.al = 1;
}
else
{
regs.h.al = 0;
}
regs.w.bx = (unsigned short)handle;
regs.w.cx = (unsigned short)(new_pos >> 16);
regs.w.dx = (unsigned short)(new_pos & 0xffff);
result = intdos(®s, ®s);<--- Variable 'result' is assigned a value that is never used.
if (regs.w.cflag & INTR_CF)
{
return GATE_RESULT_FAILED;
}
if (final_position)
{
new_pos = (((gate_uint32_t)regs.w.dx) << 16) | ((gate_uint32_t)regs.w.ax);
*final_position = (gate_int64_t)(gate_int32_t)new_pos;
}
return GATE_RESULT_OK;
}
gate_result_t gate_platform_stream_close(gate_platform_stream_t* strm)
{
int handle = (int)(gate_intptr_t)strm;
unsigned result = _dos_close(handle);
if (result != 0)
{
return gate_platform_print_last_error(NULL, 0);
}
return GATE_RESULT_OK;
}
int gate_platform_convert_gate_signal(int gate_platform_signal)
{
switch (gate_platform_signal)
{
case GATE_PLATFORM_SIGNAL_ABRT: return SIGABRT;
case GATE_PLATFORM_SIGNAL_FPE: return SIGFPE;
case GATE_PLATFORM_SIGNAL_ILL: return SIGILL;
case GATE_PLATFORM_SIGNAL_INT: return SIGINT;
case GATE_PLATFORM_SIGNAL_SEGV: return SIGSEGV;
case GATE_PLATFORM_SIGNAL_TERM: return SIGTERM;
default: return gate_platform_signal;
}
}
int gate_platform_convert_native_signal(int native_signal)
{
switch (native_signal)
{
case SIGABRT: return GATE_PLATFORM_SIGNAL_ABRT;
case SIGFPE: return GATE_PLATFORM_SIGNAL_FPE;
case SIGILL: return GATE_PLATFORM_SIGNAL_ILL;
case SIGINT: return GATE_PLATFORM_SIGNAL_INT;
case SIGSEGV: return GATE_PLATFORM_SIGNAL_SEGV;
case SIGTERM: return GATE_PLATFORM_SIGNAL_TERM;
default: return native_signal;
}
}
gate_result_t gate_platform_set_signal_handler(int sig,
gate_platform_signal_handler_t new_handler,
gate_platform_signal_handler_t* ptr_old_handler)
{
gate_platform_signal_handler_t ret;
int native_signal = gate_platform_convert_gate_signal(sig);
if (native_signal == 0)
{
return GATE_RESULT_NOTSUPPORTED;
}
ret = signal(native_signal, new_handler);
if (SIG_ERR == ret)
{
return GATE_RESULT_FAILED;
}
else
{
if (ptr_old_handler)
{
*ptr_old_handler = ret;
}
return GATE_RESULT_OK;
}
}
gate_result_t gate_platform_raise_signal(int sig)
{
gate_result_t ret = GATE_RESULT_FAILED;
int result;
int native_signal = gate_platform_convert_gate_signal(sig);
if (native_signal == 0)
{
return GATE_RESULT_NOTSUPPORTED;
}
result = raise(native_signal);
if (result == 0)
{
ret = GATE_RESULT_OK;
}
else
{
ret = GATE_RESULT_FAILED;
}
return ret;
}
int gate_dos_intr(unsigned char intnr, gate_dos_gpregs_t* gpregs, gate_dos_memregs_t* memregs)
{
int ret;
union REGS regs_in, regs_out;
struct SREGS regs_seg;
regs_in.w.ax = gpregs->a.x;
regs_in.w.bx = gpregs->b.x;
regs_in.w.cx = gpregs->c.x;
regs_in.w.dx = gpregs->d.x;
regs_in.w.si = gpregs->si;
regs_in.w.di = gpregs->di;
regs_in.w.cflag = gpregs->flags;
if (memregs)
{
regs_seg.es = memregs->es;
regs_seg.ds = memregs->ds;
regs_seg.cs = memregs->cs;
regs_seg.ss = memregs->ss;
#if defined(GATE_SYS_DOS32)
ret = int386x((int)intnr, ®s_in, ®s_out, ®s_seg);
#else
ret = int86x((int)intnr, ®s_in, ®s_out, ®s_seg);
#endif
memregs->es = regs_seg.es;
memregs->ds = regs_seg.ds;
memregs->cs = regs_seg.cs;
memregs->ss = regs_seg.ss;
}
else
{
#if defined(GATE_SYS_DOS32)
ret = int386((int)intnr, ®s_in, ®s_out);
#else
ret = int86((int)intnr, ®s_in, ®s_out);
#endif
}
gpregs->a.x = regs_out.w.ax;
gpregs->b.x = regs_out.w.bx;
gpregs->c.x = regs_out.w.cx;
gpregs->d.x = regs_out.w.dx;
gpregs->si = regs_out.w.si;
gpregs->di = regs_out.w.di;
gpregs->flags = regs_out.w.cflag;
return ret;
}
unsigned int gate_dos_version()
{
gate_dos_gpregs_t regs = GATE_INIT_EMPTY;
regs.a.x = 0x3000;
gate_dos_intr(0x21, ®s, NULL);
return regs.a.x;
}
unsigned char gate_dos_cpu_read_port_byte(unsigned short port_addr)
{
unsigned char ret_value = 0;
_asm
{
mov dx, [port_addr];
in al, dx;
mov[ret_value], al;
};
return ret_value;
}
unsigned short gate_dos_cpu_read_port_word(unsigned short port_addr)
{
unsigned short ret_value = 0;
_asm
{
mov dx, [port_addr]
in ax, dx
mov[ret_value], ax
};
return ret_value;
}
void gate_dos_cpu_write_port_byte(unsigned short port_addr, unsigned char data)
{
_asm
{
mov dx, port_addr
mov al, data
out dx, al
};
}
void gate_dos_cpu_write_port_word(unsigned short port_addr, unsigned short data)
{
_asm
{
mov dx, port_addr
mov ax, data
out dx, ax
};
}
void __far* gate_dos_farptr(unsigned int segment, unsigned int offset)
{
return MK_FP(segment, offset);
}
unsigned gate_dos_farptr_seg(void const __far* ptr)
{
return FP_SEG(ptr);
}
unsigned gate_dos_farptr_off(void const __far* ptr)
{
return FP_OFF(ptr);
}
void gate_dos_delay(unsigned int milliseconds)
{
delay(milliseconds);
}
static volatile unsigned int gate_dos_clock_days = 0;
static volatile unsigned long gate_dos_clock_value = 0;
static volatile unsigned long gate_dos_clock_current_start = 0;
void gate_dos_clock_init()
{
long ticks = 0;
_bios_timeofday(_TIME_GETCLOCK, &ticks);
gate_dos_clock_days = 0;
gate_dos_clock_value = ticks;
}
static gate_uint32_t const milliseconds_per_day = 86400000;
static gate_uint32_t const clock_ticks_per_day = 1573344;
gate_uint32_t gate_dos_clock_ticks_per_day()
{
return (gate_uint32_t)clock_ticks_per_day;
}
gate_uint32_t gate_dos_clock_get()
{
gate_uint32_t milliseconds;
long ticks = 0;
int overflow = _bios_timeofday(_TIME_GETCLOCK, &ticks);
if (overflow)
{
gate_dos_clock_days += overflow;
gate_dos_clock_current_start = milliseconds_per_day * gate_dos_clock_days;
}
else if ((unsigned long)ticks < gate_dos_clock_value)
{
++gate_dos_clock_days;
gate_dos_clock_current_start = milliseconds_per_day * gate_dos_clock_days;
}
gate_dos_clock_value = (unsigned long)ticks;
//milliseconds = gate_dos_clock_current_start + (gate_int32_t)((double)milliseconds_per_day * (double)ticks / (double)clock_ticks_per_day);
milliseconds = gate_dos_clock_current_start + (gate_uint32_t)(100000ULL * (gate_uint64_t)ticks / 1821ULL);
return milliseconds;
}
gate_bool_t gate_dos_has_kbd_event()
{
return kbhit() ? true : false;
}
gate_bool_t gate_dos_get_next_kbd_event(unsigned int* ptr_key)
{
int ch = getch();
if (ch < 0)
{
return false;
}
if (ptr_key)
{
*ptr_key = ch;
}
return true;
}
gate_bool_t gate_dos_read_kbd(unsigned int* ptr_key, gate_bool_t* is_extended)
{
int ch;
gate_bool_t ext = false;
if (!gate_dos_has_kbd_event())
{
return false;
}
ch = getch();
if (ch < 0)
{
return false;
}
if (ch == 0)
{
ext = true;
ch = getch();
}
if (ptr_key)
{
*ptr_key = ch;
}
if (is_extended)
{
*is_extended = ext;
}
return true;
}
gate_bool_t gate_dos_await_kbd(unsigned int* ptr_key, gate_bool_t* is_extended, unsigned timeout_ms)
{
gate_uint32_t now = gate_dos_clock_get();
gate_uint32_t timeout = now + (gate_uint32_t)timeout_ms;
do
{
if (gate_dos_read_kbd(ptr_key, is_extended))
{
return true;
}
now = gate_dos_clock_get();
} while (now >= timeout);
return false;
}
static gate_bool_t gate_dos_try_read_kbd_event(gate_char32_t* ptr_char, gate_result_t* ptr_result)
{
unsigned int chr = 0;
if (gate_dos_has_kbd_event())
{
if (ptr_char)
{
if (gate_dos_get_next_kbd_event(&chr))
{
*ptr_char = chr;
*ptr_result = GATE_RESULT_OK;
}
else
{
*ptr_result = GATE_RESULT_FAILED;
}
}
else
{
*ptr_result = GATE_RESULT_OK;
}
return true;
}
else
{
return false;
}
}
gate_result_t gate_dos_await_and_read_kbd(gate_char32_t* ptr_char, gate_uint32_t timeout_ms)
{
gate_result_t result = GATE_RESULT_TIMEOUT;
const gate_bool_t wait_infinite = (timeout_ms == (gate_uint32_t)-1);
gate_uint64_t timeout_ticks;
if (timeout_ms == 0)
{
if (!gate_dos_try_read_kbd_event(ptr_char, &result))
{
return GATE_RESULT_TIMEOUT;
}
else
{
return result;
}
}
if (wait_infinite)
{
while (!gate_dos_try_read_kbd_event(ptr_char, &result))
{
}
return result;
}
else
{
timeout_ticks = gate_dos_clock_get() + timeout_ms;
do
{
if (gate_dos_try_read_kbd_event(ptr_char, &result))
{
return result;
}
} while (gate_dos_clock_get() < timeout_ticks);
return GATE_RESULT_TIMEOUT;
}
}
static char const* gate_dos_registered_app_path = NULL;
void gate_dos_register_app_path(char const* text)
{
gate_dos_registered_app_path = text;
}
char const* gate_dos_get_app_path()
{
return gate_dos_registered_app_path;
}
gate_result_t gate_dos_alloc(unsigned int paragraphs16, unsigned int* ptr_segment_addr)
{
unsigned result = _dos_allocmem(paragraphs16, ptr_segment_addr);
if (result == 0)
{
return GATE_RESULT_OK;
}
else
{
return GATE_RESULT_FAILED;
}
}
gate_result_t gate_dos_dealloc(unsigned int segment_addr)
{
unsigned result = _dos_freemem(segment_addr);
if (result == 0)
{
return GATE_RESULT_OK;
}
else
{
return GATE_RESULT_FAILED;
}
}
void gate_dos_debug_print(char const* format, ...)
{
va_list ap;
va_start(ap, format);
vfprintf(stderr, format, ap);
va_end(ap);
}
unsigned gate_dos_kbd_scan_code_to_input_code(unsigned bios_scan_code)
{
switch (bios_scan_code)
{
case 0x48: return GATE_KBD_KEY_UP;
case 0x50: return GATE_KBD_KEY_DOWN;
case 0x4b: return GATE_KBD_KEY_LEFT;
case 0x4d: return GATE_KBD_KEY_RIGHT;
case 0x3b: return GATE_KBD_KEY_F1;
case 0x3c: return GATE_KBD_KEY_F2;
case 0x3d: return GATE_KBD_KEY_F3;
case 0x3e: return GATE_KBD_KEY_F4;
case 0x3f: return GATE_KBD_KEY_F5;
case 0x40: return GATE_KBD_KEY_F6;
case 0x41: return GATE_KBD_KEY_F7;
case 0x42: return GATE_KBD_KEY_F8;
case 0x43: return GATE_KBD_KEY_F9;
case 0x44: return GATE_KBD_KEY_F10;
case 0x85: return GATE_KBD_KEY_F11;
case 0x86: return GATE_KBD_KEY_F12;
case 0x52: return GATE_KBD_KEY_INSERT;
case 0x53: return GATE_KBD_KEY_DELETE;
case 0x47: return GATE_KBD_KEY_HOME;
case 0x4f: return GATE_KBD_KEY_END;
case 0x49: return GATE_KBD_KEY_PGUP;
case 0x51: return GATE_KBD_KEY_PGDOWN;
case 0x01: return GATE_KBD_KEY_ESCAPE;
case 0x1c: return GATE_KBD_KEY_RETURN;
case 0x0e: return GATE_KBD_KEY_BACKSPACE;
default: break;
}
return bios_scan_code;
}
gate_result_t gate_platform_semaphore_create(gate_platform_semaphore_t* ptr_sem, unsigned int count)
{
unsigned volatile* sem;
if (!ptr_sem || (count == 0))
{
return GATE_RESULT_INVALIDARG;
}
sem = (unsigned volatile*)ptr_sem;
*sem = count;
return GATE_RESULT_OK;
}
gate_result_t gate_platform_semaphore_acquire(gate_platform_semaphore_t* ptr_sem, gate_uint32_t const* ptr_timeout_ms)
{
unsigned volatile* sem = (unsigned volatile*)ptr_sem;
unsigned value;<--- Unused variable: value
gate_uint64_t timeout_time_ms = gate_dos_clock_get();
if (!sem)
{
return GATE_RESULT_INVALIDARG;
}
if (ptr_timeout_ms)
{
timeout_time_ms += *ptr_timeout_ms;
}
do
{
if (*sem > 0)
{
--(*sem);
return GATE_RESULT_OK;
}
gate_platform_yield();
} while (ptr_timeout_ms && (gate_dos_clock_get() < timeout_time_ms));
return GATE_RESULT_TIMEOUT;
}
gate_result_t gate_platform_semaphore_release(gate_platform_semaphore_t* ptr_sem)
{
unsigned volatile* sem = (unsigned volatile*)ptr_sem;
if (!sem)
{
return GATE_RESULT_INVALIDARG;
}
++(*sem);
return GATE_RESULT_OK;
}
gate_result_t gate_platform_semaphore_destroy(gate_platform_semaphore_t* ptr_sem)
{
unsigned volatile* sem = (unsigned volatile*)ptr_sem;
if (!sem)
{
return GATE_RESULT_INVALIDARG;
}
*sem = 0xffff;
return GATE_RESULT_OK;
}
gate_bool_t gate_platform_yield()
{
if (gate_coroutine_enabled())
{
gate_coroutine_yield();
return true;
}
return false;
}
#endif /* GATE_SYS_DOS */
|