From a4c94c8b449d5090d2e781f0382a6ba3cdd29c42 Mon Sep 17 00:00:00 2001
From: Karoly Balogh <karoly@freepascal.org>
Date: Sun, 3 Dec 2023 12:12:27 +0100
Subject: [PATCH] human68k: more work on getting tinyheap right. still doesn't
 seem to work, sadly, so still disabled

---
 compiler/parser.pas    |  6 ++++++
 rtl/human68k/si_prc.pp | 18 +++++++++++++++++-
 rtl/human68k/system.pp | 18 ++++++++++++++++--
 3 files changed, 39 insertions(+), 3 deletions(-)

diff --git a/compiler/parser.pas b/compiler/parser.pas
index 739c7b26a6..4abe97225d 100644
--- a/compiler/parser.pas
+++ b/compiler/parser.pas
@@ -125,6 +125,12 @@ implementation
            system_powerpc_morphos,
            system_x86_64_aros:
              include(supported_calling_conventions,pocall_syscall);
+           system_m68k_human68k:
+             begin
+               include(supported_calling_conventions,pocall_syscall);
+               if heapsize=0 then
+                 heapsize:=65536;
+             end;
 {$ifdef i8086}
            system_i8086_embedded:
              begin
diff --git a/rtl/human68k/si_prc.pp b/rtl/human68k/si_prc.pp
index 12fef28f64..ba40bfda91 100644
--- a/rtl/human68k/si_prc.pp
+++ b/rtl/human68k/si_prc.pp
@@ -17,6 +17,8 @@ unit si_prc;
 
 interface
 
+{$.define FPC_HUMAN68K_USE_TINYHEAP}
+
 implementation
 
 {$include h68kdos.inc}
@@ -24,6 +26,10 @@ implementation
 var
   stacktop: pointer; public name '__stktop';
   stklen: longint; external name '__stklen';
+{$ifdef FPC_HUMAN68K_USE_TINYHEAP}
+  initial_heap_start: pointer; external name '__initial_heap_start';
+  initial_heap_end: pointer; external name '__initial_heap_end';
+{$endif FPC_HUMAN68K_USE_TINYHEAP}
 
 var
   h68k_startup: Th68kdos_startup; public name '_h68k_startup';
@@ -45,6 +51,7 @@ end;
 procedure PascalStart(const startparams: Ph68kdos_startup); noreturn;
 var
   bss_start: pbyte;
+  blocksize: longint;
 begin
   with startparams^ do
     begin
@@ -53,8 +60,17 @@ begin
       fillchar(bss_start^,bss_end-bss_start,0);
 
       h68k_psp:=pointer(@mcb[$10]);
-      h68kdos_setblock(h68k_psp,bss_end-pointer(h68k_psp)+stklen);
+{$ifdef FPC_HUMAN68K_USE_TINYHEAP}
+      blocksize:=bss_end-pointer(h68k_psp)+stklen+heapsize;
+{$else FPC_HUMAN68K_USE_TINYHEAP}
+      blocksize:=bss_end-pointer(h68k_psp)+stklen;
+{$endif FPC_HUMAN68K_USE_TINYHEAP}
+      h68kdos_setblock(h68k_psp,blocksize);
       stacktop:=bss_end+stklen;
+{$ifdef FPC_HUMAN68K_USE_TINYHEAP}
+      initial_heap_start:=stacktop;
+      initial_heap_end:=initial_heap_start+heapsize;
+{$endif FPC_HUMAN68K_USE_TINYHEAP}
     end;
 
   h68k_startup:=startparams^;
diff --git a/rtl/human68k/system.pp b/rtl/human68k/system.pp
index 0d87b79143..a9be48483f 100644
--- a/rtl/human68k/system.pp
+++ b/rtl/human68k/system.pp
@@ -112,6 +112,11 @@ var
   h68k_startup: Th68kdos_startup; external name '_h68k_startup';
   h68k_psp: Ph68kdos_psp; external name '_h68k_psp';
 
+{$ifdef FPC_HUMAN68K_USE_TINYHEAP}
+  initial_heap_start: pointer; public name '__initial_heap_start';
+  initial_heap_end: pointer; public name '__initial_heap_end';
+{$endif FPC_HUMAN68K_USE_TINYHEAP}
+
 
 {*****************************************************************************
                              ParamStr
@@ -242,6 +247,17 @@ end;
                          System Unit Initialization
 *****************************************************************************}
 
+{$ifdef FPC_HUMAN68K_USE_TINYHEAP}
+procedure InitHeap;
+var
+  aligned_heap_start: pointer;
+begin
+  aligned_heap_start:=align(initial_heap_start,sizeof(ttinyheapblock));
+  RegisterTinyHeapBlock_Simple_Prealigned(aligned_heap_start, ptruint(initial_heap_end - aligned_heap_start));
+end;
+{$endif FPC_HUMAN68K_USE_TINYHEAP}
+
+
 procedure SysInitStdIO;
 begin
   OpenStdIO(Input,fmInput,StdInputHandle);
@@ -263,10 +279,8 @@ begin
   StackLength := CheckInitialStkLen (InitialStkLen);
 { Initialize ExitProc }
   ExitProc:=Nil;
-{$ifndef FPC_HUMAN68K_USE_TINYHEAP}
 { Setup heap }
   InitHeap;
-{$endif FPC_HUMAN68K_USE_TINYHEAP}
   SysInitExceptions;
 {$ifdef FPC_HAS_FEATURE_UNICODESTRINGS}
   InitUnicodeStringManager;