; start loader for BGE (begining from v 3.12)  / based of 'linkpack.asm'

    device zxspectrum128
    
            INCLUDE "export.asm"
    
            ORG #4000
            
            
beginbasic  DISPLAY "d0 " ,/a, $
          DISP #5D3B
          
          DISPLAY "basic : ",/a, $
          
          DB 0,10
          DW len10
line10  
          DB #EA,13   ; REM and ASM CODE
          
runload               ; запуск загрузчика          
          ld (iy+1),#CC
          ld a,#10
          ld bc,#7FFD
          ld (#5B5C),a
          out (c),a
          xor a
          ld (#5CF9),a   ; set operation load
          ld hl,#6000
         push hl
          ld b,lnbge_l
          ld c,5
          ld de,(#5CF4)
         jp #3D13 
len10=$-line10
      
          DB 0,20
          DW len20
line20
          DB #FB,":",#FD,"0",#0E,0,0,#FE,#5F,0,":"
             ;cls : clear 5fff :
          DB #F9,#C0,"0",#0E,0,0 : DW runload : DB 0         
             ;randomize usr 'runload'
             
          DB 13
len20=$-line20

          ENT
lenbasic EQU $-beginbasic
          DISPLAY "LenBasic : ",/a, lenbasic

lnbastrk=($-beginbasic+255)/256          
            
    ;   SAVEBIN "bge_b.hst",beginbasic,lnbastrk*256
    
           ORG beginbasic+lnbastrk*256                   
;---------------------------------------------------------------
loader      DISPLAY "d1 " ,/a, $
            DISP #6000
s_load 
            DISPLAY "s_load : ",/a, $
;    замена цвета на стандартный для BGE (белый на чёрном)
            ld a,#07
            ld (#5C8D),a
            ld (#5C8F),a
            ld (#5C48),a   ; border
            ld hl,#4000
            ld de,#4001
            ld bc,6144
            ld (hl),l
            ld a,e
            ld (#5C3C),a    ; печать снизу
            ldir
            ld (hl),7
            ld bc,767
            ldir
            xor a
            out (#FE),a
            ld a,#CC
            ld (#5C3B),a
            
            
            ld hl,StartTXT
            call Print_
            
           ; проверка наличия 128K 
            ld hl,#C000
            xor a
            call setpg
            ld e,(hl)
            ld (hl),a
            inc a
            call setpg
            ld (hl),a
            dec a
            call setpg
            ld a,(hl)
            cp #10
           jr z,2F  ; есть
                                            
            ld hl,Not128txt  ; нет
            call Print_
1           halt
           jr 1B

2           ld hl,txtLoad_
            call Print_

            ld hl,Link
            ld b,lntLink
            call load
            
            call setDep                  
            
            ld a,3
            ld de,#C000
            call dep_
            
            ld a,6          
            ld bc,Link.lnbge_6
            ld hl,Link.bge_6
            ld de,#C000
            call dep
            
            ld a,7
            ld bc,Link.lnbge_7
            ld hl,Link.bge_7
            ld de,#DB00
            call dep
            
            ld hl,Link    ; Link=Link2
            ld b,lntLink2
            call load
            
            call setDep 
            
            ld a,#C3   ; 'jp'
            ld (dehJ),a
            xor a
            ld hl,InitBGE  ; #A500
            push hl
        ;    ld bc,Link2.lnbge_mn
        ;    ld hl,Link
            ld de,#6000
           jr dep_ 
dep         push de
            ld de,Link            
            ldir   
            pop de                 
dep_        ld hl,Link
            call setpg
dehJ        call DEHRUST
            xor a
setpg       or #10
            push bc
            ld bc,#7FFD
            ld (#5B5C),a
            out (c),a
            pop bc
           ret
load
            ld c,5
            ld de,(#5CF4)
           jp #3D13
           
setDep      ld hl,LD_dehr   ; установка распаковщика по адресу #5E00
            ld de,#5E00
            ld bc,lnLD_dehr
            ldir
           ret         
              
Print_      ld a,(hl)
            inc hl
            cp #FF
           ret z
            rst #10            
           jr Print_

Not128txt   DB 13,#13,1,#10,2,"Sorry !",13,"You need 128 Kb minimum.",#FF  


StartTXT    DB #11,0,#10,5,#13,0,22,0,4,"Burial Graphics Editor",13
            DB #10,7,"________________________________",13,13,#10,6
            DB "Version: ",#10,7

            INCLUDE "Version.asm"
            
            DB 13,13,13,#10,4            
            DB "Based on the source code of the",13,13,#10,5
            DB "  Burial  Gfx  Editor  3.o5",13
            DB "  (c) 2oo1 Delirium Tremens",13
            DB "           from Freedom",13,13,#10,4
            DB "  - Sinn (coding, etc.)",13
            DB "  - Others of dtr - support",13,#10,7
            DB "________________________________",13,13,#FF
            
txtLoad_    DB "Loading and Depacking ...",13,#FF           
             
LD_dehr    
             ENT                 
             DISP #5E00   
ld_dehr     INCLUDE "DEHRUST@.asm"  
             
lnLD_dehr   EQU $-ld_dehr 
             ENT
lnbge_l=($-loader+255)/256


   ;    SAVEBIN "bge_l.hst",s_load,lnbge_l*256
   
            ORG loader+lnbge_l*256
LinkADR_            
            DISPLAY "d2 " ,/a, $        
;---------------------------------------------------------------
            DISP s_load+lnbge_l*256
             DISPLAY " "
Link
            DISPLAY "Link : ",/a, $
.bge_3     INCBIN "bge_3.hst"
.lnbge_3=$-.bge_3

.bge_6     INCBIN "bge_6.hst"
.lnbge_6=$-.bge_6

.bge_7     INCBIN "bge_7.hst"
.lnbge_7=$-.bge_7

           ENT
lntLink=($-LinkADR_+255)/256 
           
           
           DISPLAY "d3 " ,/a, $
          
                     
           ORG LinkADR_+lntLink*256
Link2ADR_         
           DISP s_load+lnbge_l*256
           DISPLAY " "
           DISPLAY "Link2 : ",/a, $
Link2           
.bge_mn    INCBIN "bge_main.hst"
.lnbge_mn=$-.bge_mn
     
           ENT
lntLink2=($-Link2ADR_+255)/256
  
           
           
           DISPLAY "d4 " ,/a, $ 
           ASSERT  $ < #FFFF
           DISPLAY "len trk : ",/a,($-beginbasic+255)/256

      ;     SAVEBIN "bge_d.hst",Link,lntLink*256
           
;---------------------------------------------------------------

        EMPTYTRD "bge_xxx.trd", "BGE 3.12"
           
  ;       ORG #4000   ;  собираем монолоидер / create monoloader
         
  ;       INCBIN "bge_b.hst"
  ;       INCBIN "bge_l.hst"
  ;       INCBIN "bge_d.hst"
              
      SAVETRD "bge_xxx.trd","boot.B",beginbasic,$-beginbasic
    ;  SAVEBIN  "bge_B.bin",beginbasic,$-beginbasic

   ; Lua -- коррекция монолоэдера 'boot.B'
    LUA
  -- monoloader 'boot.B' correction 
  
     print " Lua monoloader boot correction "  
     bln = tostring (sj.get_label("lenbasic")) 
     sj.shellexec (("trdetz bge_xxx.trd fprop boot.B start ")..(bln))
     sj.shellexec (("trdetz bge_xxx.trd fprop boot.B flen ")..(bln))
      
    ENDLUA         
                    
     DISPLAY " " 