Atlas - SDL_dos.c

Home / ext / SDL / src / core / dos Lines: 1 | Size: 5227 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)]
[FILE BEGIN]
1/* 2 Simple DirectMedia Layer 3 Copyright (C) 1997-2026 Sam Lantinga <[email protected]> 4 5 This software is provided 'as-is', without any express or implied 6 warranty. In no event will the authors be held liable for any damages 7 arising from the use of this software. 8 9 Permission is granted to anyone to use this software for any purpose, 10 including commercial applications, and to alter it and redistribute it 11 freely, subject to the following restrictions: 12 13 1. The origin of this software must not be misrepresented; you must not 14 claim that you wrote the original software. If you use this software 15 in a product, an acknowledgment in the product documentation would be 16 appreciated but is not required. 17 2. Altered source versions must be plainly marked as such, and must not be 18 misrepresented as being the original software. 19 3. This notice may not be removed or altered from any source distribution. 20*/ 21#include "SDL_internal.h" 22 23#if defined(SDL_PLATFORM_DOS) 24 25#include "SDL_dos.h" 26 27void *DOS_AllocateConventionalMemory(const int len, _go32_dpmi_seginfo *seginfo) 28{ 29 seginfo->size = (len + 15) / 16; // this is in "paragraphs" 30 if (_go32_dpmi_allocate_dos_memory(seginfo) != 0) { 31 SDL_OutOfMemory(); 32 return NULL; 33 } 34 // No need to lock: DPMI 0.9 §3.3 guarantees the first megabyte is always 35 // committed and locked. CWSDPMI (the DJGPP DPMI host) doesn't support 36 // virtual memory at all, so conventional memory is never paged out. 37 return DOS_PhysicalToLinear(seginfo->rm_segment * 16); 38} 39 40void *DOS_AllocateDMAMemory(const int len, _go32_dpmi_seginfo *seginfo) 41{ 42 // ISA DMA transfers cannot cross a 64 KB physical page boundary (hardware 43 // limitation of the 8237 DMA controller). Allocating 2× the requested size 44 // guarantees at least one contiguous `len`-byte region that doesn't straddle 45 // a boundary. This is the standard technique used by Allegro, MIDAS, and 46 // every other DOS audio library; allocate-check-retry would add complexity 47 // for zero benefit. 48 uint8_t *ptr = (uint8_t *)DOS_AllocateConventionalMemory(len * 2, seginfo); 49 if (!ptr) { 50 return NULL; 51 } 52 53 // if we're past the end of a page, use the second half of the block. 54 const uint32_t physical = (seginfo->rm_segment * 16); 55 if ((physical >> 16) != ((physical + len) >> 16)) { 56 ptr += len; 57 } 58 return ptr; 59} 60 61void DOS_FreeConventionalMemory(_go32_dpmi_seginfo *seginfo) 62{ 63 _go32_dpmi_free_dos_memory(seginfo); 64} 65 66char *DOS_GetFarPtrCString(const Uint32 segoffset) 67{ 68 if (!segoffset) { // let's just treat this as a NULL pointer. 69 return NULL; 70 } 71 72 const unsigned long ofs = (unsigned long)(((segoffset & 0xFFFF0000) >> 12) + (segoffset & 0xFFFF)); 73 size_t len; 74 75 for (len = 0; _farpeekb(_dos_ds, ofs + len) != '\0'; len++) { 76 } 77 78 len++; // null terminator. 79 char *retval = SDL_malloc(len); 80 if (!retval) { 81 return NULL; 82 } 83 84 for (size_t i = 0; i < len; i++) { 85 retval[i] = (char)_farpeekb(_dos_ds, ofs + i); 86 } 87 return retval; 88} 89 90void DOS_HookInterrupt(int irq, DOS_InterruptHookFn fn, DOS_InterruptHook *hook) 91{ 92 SDL_assert(irq >= 0 && irq <= 15); 93 SDL_assert(fn != NULL); 94 SDL_assert(hook != NULL); 95 hook->fn = fn; 96 hook->irq = irq; 97 hook->interrupt_vector = DOS_IRQToVector(irq); 98 hook->irq_handler_seginfo.pm_selector = _go32_my_cs(); 99 hook->irq_handler_seginfo.pm_offset = (uint32_t)fn; 100 _go32_dpmi_get_protected_mode_interrupt_vector(hook->interrupt_vector, &hook->original_irq_handler_seginfo); 101 _go32_dpmi_chain_protected_mode_interrupt_vector(hook->interrupt_vector, &hook->irq_handler_seginfo); 102 103 // enable interrupt on the correct PIC 104 if (irq > 7) { 105 outportb(PIC2_DATA, inportb(PIC2_DATA) & ~(1 << (irq - 8))); // unmask on slave PIC 106 outportb(PIC1_DATA, inportb(PIC1_DATA) & ~(1 << 2)); // ensure cascade (IRQ2) is unmasked 107 } else { 108 outportb(PIC1_DATA, inportb(PIC1_DATA) & ~(1 << irq)); // unmask on master PIC 109 } 110} 111 112void DOS_UnhookInterrupt(DOS_InterruptHook *hook, bool disable_interrupt) 113{ 114 if (!hook || !hook->fn) { 115 return; 116 } 117 118 SDL_assert(hook->interrupt_vector > 0); 119 SDL_assert(hook->irq > 0); 120 121 if (disable_interrupt) { 122 if (hook->irq > 7) { 123 outportb(PIC2_DATA, inportb(PIC2_DATA) | (1 << (hook->irq - 8))); // mask on slave PIC 124 } else { 125 outportb(PIC1_DATA, inportb(PIC1_DATA) | (1 << hook->irq)); // mask on master PIC 126 } 127 } 128 129 _go32_dpmi_set_protected_mode_interrupt_vector(hook->interrupt_vector, &hook->original_irq_handler_seginfo); 130 131 // Note: _go32_dpmi_chain_protected_mode_interrupt_vector internally 132 // allocates a wrapper, but it is NOT safe to free it with 133 // _go32_dpmi_free_iret_wrapper — that function expects a wrapper 134 // allocated by _go32_dpmi_allocate_iret_wrapper, and calling it on 135 // a chain-allocated wrapper causes a page fault on exit. The wrapper 136 // is a few bytes of conventional memory that the OS reclaims when the 137 // process terminates, so the leak is harmless. 138 139 SDL_zerop(hook); 140} 141 142#endif // defined(SDL_PLATFORM_DOS) 143
[FILE END]
(C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.