Fixed MZ executable memory allocation during EXEC
authorRobert Pengelly <robertapengelly@hotmail.com>
Tue, 20 Oct 2026 18:32:56 +0000 (19:32 +0100)
committerRobert Pengelly <robertapengelly@hotmail.com>
Tue, 20 Oct 2026 18:32:56 +0000 (19:32 +0100)
interrupts.c
loader.c
loader.h

index 26f0aaa13922c4106efaae145ed529d159ca407e..7ee3b05762dc38c7b826316edab29b96d69ab619 100644 (file)
@@ -7578,7 +7578,7 @@ static void int_21 (CPU8086 *cpu) {
         
             char name[260], path[520], args[128];
             
-            uint16_t paragraphs, child_psp, source_env, env_size;
+            uint16_t paragraphs, max_paragraphs, child_psp, source_env, env_size;
             uint16_t command_off, command_seg;
             uint16_t tail_length;
             
@@ -7602,7 +7602,7 @@ static void int_21 (CPU8086 *cpu) {
             dos_read_asciiz (cpu, cpu->ds, cpu->regs[REG_DX], name, sizeof (name));
             dos_resolve_path (name, path, sizeof (path));
             
-            if (!dos_executable_memory (path, &paragraphs, &is_mz)) {
+            if (!dos_executable_memory (path, &paragraphs, &max_paragraphs, &is_mz)) {
             
                 dos_set_error (cpu, 0x0002);
                 break;
@@ -7622,7 +7622,7 @@ static void int_21 (CPU8086 *cpu) {
             
             }
             
-            if (!is_mz) {
+            if (!is_mz || max_paragraphs > paragraphs) {
             
                 uint16_t mcb_seg = 0x004F;
                 uint16_t largest = 0;
@@ -7651,6 +7651,10 @@ static void int_21 (CPU8086 *cpu) {
                 }
                 
                 paragraphs = (uint16_t) (largest - env_size - 1);
+                
+                if (is_mz && paragraphs > max_paragraphs) {
+                    paragraphs = max_paragraphs;
+                }
             
             }
             
index b9d75d9aa9c243b4d78094b3ee7098f2a45a2350..cb2c8b648417f31f57944e0c7749e4085f91bae1 100644 (file)
--- a/loader.c
+++ b/loader.c
@@ -81,7 +81,7 @@ typedef struct __attribute__ ((packed)) {
 
 } MZHeader;
 
-int dos_executable_memory (const char *path, uint16_t *paragraphs, int *is_mz) {
+int dos_executable_memory (const char *path, uint16_t *paragraphs, uint16_t *max_paragraphs, int *is_mz) {
 
     FILE *fp;
     MZHeader hdr;
@@ -92,9 +92,11 @@ int dos_executable_memory (const char *path, uint16_t *paragraphs, int *is_mz) {
     uint32_t total_size;
     uint32_t image_start;
     uint32_t image_size;
+    
     uint32_t required;
+    uint32_t maximum;
     
-    if (path == NULL || paragraphs == NULL || is_mz == NULL) {
+    if (path == NULL || paragraphs == NULL || max_paragraphs == NULL || is_mz == NULL) {
         return 0;
     }
     
@@ -128,7 +130,13 @@ int dos_executable_memory (const char *path, uint16_t *paragraphs, int *is_mz) {
         }
         
         image_size = total_size - image_start;
+        
         required = 0x10 + (image_size + 15) / 16 + hdr.min_alloc;
+        maximum = 0x10 + (image_size + 15) / 16 + hdr.max_alloc;
+        
+        if (maximum < required) {
+            maximum = required;
+        }
     
     } else {
     
@@ -138,6 +146,7 @@ int dos_executable_memory (const char *path, uint16_t *paragraphs, int *is_mz) {
         
         /* A COM process needs a PSP and the conventional 64 KiB segment. */
         required = 0x1000;
+        maximum = 0xFFFF;
     
     }
     
@@ -145,6 +154,7 @@ int dos_executable_memory (const char *path, uint16_t *paragraphs, int *is_mz) {
         return 0;
     }
     
+    *max_paragraphs = (uint16_t) (maximum > 0xFFFF ? 0xFFFF : maximum);
     *paragraphs = (uint16_t) required;
     *is_mz = mz;
     
index d7e9e9bda59a25cee30b3caf7b12d5e6cfec3096..e23ba4c40b3774bb114058018abe76f6cb6fa5c9 100644 (file)
--- a/loader.h
+++ b/loader.h
@@ -9,7 +9,7 @@
 
 #include    "cpu.h"
 
-int dos_executable_memory (const char *path, uint16_t *paragraphs, int *is_mz);
+int dos_executable_memory (const char *path, uint16_t *paragraphs, uint16_t *max_paragraphs, int *is_mz);
 int load_com_file (const char *path, CPU8086 *cpu, uint8_t *mem, size_t mem_size, uint16_t load_addr, size_t *out_size);
 int load_mz_file (const char *path, CPU8086 *cpu, uint16_t base_seg, uint16_t available_paragraphs);