     1                                  
     2                                  %if 0
     3                                  
     4                                  lDOS iniload packed payload
     5                                   by C. Masloch, 2018-2020
     6                                  
     7                                  Usage of the works is permitted provided that this
     8                                  instrument is retained with the works, so that any entity
     9                                  that uses the works is notified of this instrument.
    10                                  
    11                                  DISCLAIMER: THE WORKS ARE WITHOUT WARRANTY.
    12                                  
    13                                  
    14                                  Includes one of several depackers, some of which are under
    15                                  separate usage conditions. Refer to the individual depacker
    16                                  source files for the applicable usage conditions.
    17                                  
    18                                  %endif
    19                                  
    20                                  %include "lmacros3.mac"
     1                              <1> [list -]
    14                              <2> [list -]
    14                              <1> [list -]
    21                                  
    22                                  	struc LOADSTACKVARS, -10h
    23 FFFFFFF0 ????????                lsvFirstCluster:	resd 1
    24 FFFFFFF4 ????????                lsvFATSector:		resd 1
    25 FFFFFFF8 ????                    lsvFATSeg:		resw 1
    26 FFFFFFFA ????                    lsvLoadSeg:		resw 1
    27 FFFFFFFC ????????                lsvDataStart:		resd 1
    28                                  	endstruc
    29                                  
    30                                  	struc LOADDATA, LOADSTACKVARS - 10h
    31 FFFFFFE0 ????                    ldMemoryTop:	resw 1
    32 FFFFFFE2 ????                    ldLoadTop:	resw 1
    33 FFFFFFE4 ????                    ldSectorSeg:	resw 1
    34 FFFFFFE6 ??                      ldFATType:	resb 1
    35 FFFFFFE7 ??                      ldHasLBA:	resb 1
    36 FFFFFFE8 ????                    ldClusterSize:	resw 1
    37 FFFFFFEA ????                    ldParaPerSector:resw 1
    38 FFFFFFEC ????                    ldLoadingSeg:	resw 1
    39 FFFFFFEE ????                    ldLoadUntilSeg:	resw 1
    40                                  	endstruc
    41                                  
    42                                  
    43                                  %ifndef _MAP
    44                                  %elifempty _MAP
    45                                  %else	; defined non-empty, str or non-str
    46                                  	[map all _MAP]
    47                                  %endif
    48                                  
    49                                  	defaulting
    50                                  
    51                                  	numdef DEBUG0		; use errordata to generate an error code
    52                                  	numdef DEBUG1		; dump_stack_frame after an error occurred
    53                                  	numdef DEBUG2		; dump_stack_frame before blz_depack_safe call
    54                                  	numdef DEBUG3		; dump_stack_frame at start of blz_depack_safe
    55                                  
    56                                  	numdef ALLOW_OVERLAPPING,	1	; allow overlapping src and dst
    57                                  	numdef TEST_PROGRAM,		0
    58                                  	numdef TEST_PROGRAM_DECOMPRESSED_SIZE,		0,0
    59                                  %if _TEST_PROGRAM && ! _TEST_PROGRAM_DECOMPRESSED_SIZE
    60                                   %error Test program has to learn of decompressed size.
    61                                  %endif
    62                                  	numdef TEST_PROGRESS,	0
    63                                  
    64                                  	numdef EXEC_OFFSET,	0
    65                                  	numdef EXEC_SEGMENT,	0
    66                                  	numdef IMAGE_EXE,	0
    67                                  %if ! _IMAGE_EXE && _TEST_PROGRAM
    68                                   %error Test program can only be used as EXE.
    69                                  %endif
    70                                  	numdef IMAGE_EXE_CS,	-16	; relative-segment for CS
    71                                  	numdef IMAGE_EXE_IP,	256 +64	; value for IP
    72                                  		; The next two are only used if _IMAGE_EXE_AUTO_STACK is 0.
    73                                  	numdef IMAGE_EXE_SS,	-16	; relative-segment for SS
    74                                  	numdef IMAGE_EXE_SP,	0FFFEh	; value for SP (0 underflows)
    75                                  	numdef IMAGE_EXE_AUTO_STACK,	0, 2048	; use stack behind image
    76                                  		; _IMAGE_EXE_AUTO_STACK here differs from iniload's def of
    77                                  		;  the same name. This one is only used as a flag; if non-zero,
    78                                  		;  keep the stack given to us by iniload; if zero, set up the
    79                                  		;  stack specified by _IMAGE_EXE_SS and _IMAGE_EXE_SP.
    80                                  	numdef IMAGE_EXE_GET_COM_STACK, 0
    81                                  					; set up stack for flat .COM style entry
    82                                  	numdef DEVICE,			0
    83                                  	gendef DEVICE_NAME,		""
    84                                  	numdef DEVICE_ATTRIBUTE,	8000h
    85                                  	numdef DEVICE_ZERO_ENTRYPOINT,	0
    86                                  	numdef BOOTLDR,		1	; support boot loaded entrypoint
    87                                  
    88                                  	numdef BRIEFLZ,		0
    89                                  	numdef LZ4,		0
    90                                  	numdef SNAPPY,		0
    91                                  	numdef EXODECR,		0
    92                                  	numdef X,		0
    93                                  	numdef HEATSHRINK,	0
    94                                  	numdef LZD,		0
    95                                  	numdef LZO,		0
    96                                  	numdef LZSA2,		0
    97                                  	numdef APL,		0
    98                                  	numdef BZP,		0
    99                                  %if (!!_BRIEFLZ + !!_LZ4 + !!_SNAPPY + !!_EXODECR + !!_X + !!_HEATSHRINK 	+ !!_LZD + !!_LZO + !!_LZSA2 + !!_APL + !!_BZP) != 1
   101                                   %fatal Exactly one compression method must be selected.
   102                                  %endif
   103                                  %assign ADDITIONAL_MEMORY 0
   104                                  %if _BRIEFLZ
   105                                  	strdef PAYLOAD_FILE,	"lDOSLOAD.BLZ"
   106                                  %elif _LZ4
   107                                  	strdef PAYLOAD_FILE,	"lDOSLOAD.LZ4"
   108                                  %elif _SNAPPY
   109                                  	strdef PAYLOAD_FILE,	"lDOSLOAD.SZ"
   110                                  %elif _EXODECR
   111                                  	strdef PAYLOAD_FILE,	"lDOSLOAD.EXO"
   112                                  %elif _X
   113                                  	strdef PAYLOAD_FILE,	"lDOSLOAD.X"
   114                                  %elif _HEATSHRINK
   115                                  	strdef PAYLOAD_FILE,	"lDOSLOAD.HS"
   116                                  %elif _LZD
   117                                  	strdef PAYLOAD_FILE,	"lDOSLOAD.LZ"
   118                                  %elif _LZO
   119                                  	strdef PAYLOAD_FILE,	"lDOSLOAD.LZO"
   120                                  %elif _LZSA2
   121                                  	strdef PAYLOAD_FILE,	"lDOSLOAD.SA2"
   122                                  %elif _APL
   123                                  	strdef PAYLOAD_FILE,	"lDOSLOAD.APL"
   124                                  %elif _BZP
   125                                  	strdef PAYLOAD_FILE,	"lDOSLOAD.BZP"
   126                                  %endif
   127                                  	numdef PAYLOAD_KERNEL_MAX_PARAS,	0, 0
   128                                  %if _PAYLOAD_KERNEL_MAX_PARAS && ! _IMAGE_EXE
   129                                   %error Kernel mode max paras requires building dual-mode executable
   130                                  %endif
   131                                  	numdef COUNTER,		0, 32
   132                                  %if (_COUNTER - 1) & _COUNTER
   133                                   %error COUNTER must be a power of two
   134                                  %endif
   135                                  
   136                                  	strdef INILOAD_CFG, ""
   137                                  %ifnidn _INILOAD_CFG, ""
   138                                   %include _INILOAD_CFG
     1                              <1> ; File auto-generated, do not edit.
     2                              <1> strdef INILOAD_SIGNATURE, "eb"
     3                              <1> %assign _DEVICE 1
     4                              <1> %assign _DEVICE_ZERO_ENTRYPOINT 1
     5                              <1> %assign _DEVICE_ATTRIBUTE 8800h
     6                              <1> %define _DEVICE_NAME "LDEBUG$$"
   139                                  %endif
   140                                  
   141                                  %if _DEVICE
   142                                   %ifidn _DEVICE_NAME, ""
   143                                    %error Device name must be set
   144                                   %endif
   145                                  %endif
   146                                  
   147                                  
   148                                  	cpu 8086
   149                                  	section INIT0 start=0 vstart=0
   150                                  init0_start:
   151                                  %if _DEVICE
   152                                  		; The device header is of a fixed format.
   153                                  		;  For our purposes, the 4-byte code for
   154                                  		;  each the strategy entry and the
   155                                  		;  interrupt entry is part of this format.
   156                                  		; (DOS may read the attributes or entrypoint
   157                                  		;  offsets before calling either, so in the inicomp
   158                                  		;  stage we need to recreate in the entrypoints part
   159                                  		;  exactly what the application has here.)
   160                                  device_header:
   161                                  .next:
   162                                   %if _DEVICE_ZERO_ENTRYPOINT
   163 00000000 EB3E                    	fill 2, -1, jmp strict short j_zero_entrypoint
   164 00000002 FFFF                    	dw -1
   165                                   %else
   166                                  	dd -1				; link to next device
   167                                   %endif
   168                                  .attributes:
   169 00000004 0088                    	dw _DEVICE_ATTRIBUTE		; attributes
   170                                  .strategy:
   171 00000006 [1200]                  	dw .strategy_entry		; -> strategy entry
   172                                  .interrupt:
   173 00000008 [1600]                  	dw .interrupt_entry		; -> interrupt entry
   174                                  .name:
   175 0000000A 4C44454255472424        	fill 8, 32, db _DEVICE_NAME	; character device name,
   176                                  					;  or block device number of units
   177                                  					;  + optional name
   178                                  .strategy_entry:
   179 00000012 EB5190<rep 2h>          	fill 4, 90h, jmp device_entrypoint
   180                                  .interrupt_entry:
   181 00000016 CB90<rep 3h>            	fill 4, 90h, retf
   182                                  %endif
   183                                  
   184                                  
   185                                  %if _IMAGE_EXE || _DEVICE
   186 0000001A 90                      	nop
   187 0000001B 90<rep 5h>              	align 32, nop
   188                                  init0_kernel_entry:
   189                                  		; cs:ip = load seg : 32 here
   190                                  %if ($ - $$) != 32
   191                                   %error Wrong kernel mode entrypoint
   192                                  %endif
   193                                  %if _TEST_PROGRAM
   194                                  @@:
   195                                  	int3
   196                                  	sti
   197                                  	hlt
   198                                  	jmp @B
   199                                  %endif
   200                                  %if _BOOTLDR
   201                                  %if ! _IMAGE_EXE
   202                                  j_zero_entrypoint:
   203                                  %endif
   204 00000020 31DB                    	xor bx, bx		; 0 = kernel mode
   205 00000022 50                      	push ax
   206 00000023 8CC8                    	mov ax, cs
   207 00000025 83C01D                  	add ax, (init0_end - init0_start) >> 4
   208 00000028 8B56E2                  	mov dx, word [ bp + ldLoadTop ]	; => after end of available space
   209 0000002B BE6000                  	mov si, 60h		; => destination
   210 0000002E EB20                    	jmp init0_common
   211                                  %endif
   212                                  
   213                                   %if _IMAGE_EXE
   214 00000030 90<rep 10h>             	align 64, nop
   215                                  j_zero_entrypoint:
   216                                  init0_exe_entry:
   217                                  		; NOTE:	This part is called with ip = 256 + 64, cs = PSP.
   218                                  %if ($ - $$) != 64
   219                                   %error Wrong EXE mode entrypoint
   220                                  %endif
   221                                  %if _IMAGE_EXE_GET_COM_STACK
   222                                  	mov bx, ds		; => PSP
   223                                  	dec bx			; => MCB
   224                                  	mov ds, bx
   225                                  	add bx, word [3]	; add size of memory block
   226                                  	sub bx, -1 + paras(512)	; add one, then subtract enough paragraphs
   227                                  	cli
   228                                  	mov ss, bx
   229                                  	mov sp, 512		; -> new stack
   230                                  	sti
   231                                  %endif
   232 00000040 BB0100                  	mov bx, 1		; EXE mode
   233 00000043 50                      	push ax
   234                                    %if _TEST_PROGRAM
   235                                  	mov si, 81h
   236                                  	xor dx, dx
   237                                  	xor ax, ax
   238                                  cmdline:
   239                                  .:
   240                                  	lodsb
   241                                  	cmp al, 9
   242                                  	je .
   243                                  	cmp al, 32
   244                                  	je .
   245                                  	cmp al, 13
   246                                  	je .end
   247                                  	cmp al, '0'
   248                                  	jb .notdigit
   249                                  	cmp al, '9'
   250                                  	ja .notdigit
   251                                  .digit:
   252                                  	sub al, '0'
   253                                  	mov cx, dx		; times 1
   254                                  	add dx, dx
   255                                  	jc .invalid
   256                                  	add dx, dx		; times 4
   257                                  	jc .invalid
   258                                  	add dx, cx		; times 5
   259                                  	jc .invalid
   260                                  	add dx, dx		; times 10
   261                                  	jc .invalid
   262                                  	add dx, ax		; add in low digit
   263                                  	jc .invalid
   264                                  	jmp .
   265                                  
   266                                  .notdigit:
   267                                  	cmp al, 'A'
   268                                  	je .a
   269                                  	cmp al, 'a'
   270                                  	je .a
   271                                  	cmp al, 'B'
   272                                  	je .b
   273                                  	cmp al, 'b'
   274                                  	je .b
   275                                  .invalid:
   276                                  	mov dx, -1		; invalid
   277                                  	jmp .end
   278                                  
   279                                  .a:
   280                                  	and bh, ~ 80h
   281                                  	jmp .
   282                                  
   283                                  .b:
   284                                  	or bh, 80h
   285                                  	jmp .
   286                                  
   287                                  .end:
   288                                  	push dx
   289                                    %endif
   290 00000044 8CD2                    	mov dx, ss		; => after end of available space
   291 00000046 8CC8                    	mov ax, cs
   292 00000048 83C02D                  	add ax, (256 + (init0_end - init0_start)) >> 4	; => source
   293 0000004B 8CCE                    	mov si, cs
   294 0000004D 83C610                  	add si, 256 >> 4	; => destination
   295                                   %endif
   296                                  
   297                                  init0_common:
   298                                  		; REM:	This part must be position-independent, as it is
   299                                  		;	 called either with ip = init0_common (kernel mode
   300                                  		;	 or device mode)
   301                                  		;	 or ip = init0_common + 256 (application mode).
   302                                  		; INP:	si => depack destination
   303                                  		;	ax => source of payload + INIT1
   304                                  		;	dx => behind available memory
   305                                  		; OUT:	cs:ip -> init1_start:
   306                                  		;	ax => INIT1
   307                                  		;	dx => payload source
   308                                  		;	si => depack destination
   309                                  		;	bx = mode
   310 00000050 FC                      	cld
   311                                  %if _TEST_PROGRAM
   312                                  	sub dx, ( (payload_end - payload) ) >> 4
   313                                  	jc .error
   314                                  	mov cx, ( (payload_end - payload) 		+ (init1_end - init1_start) 		+ (init2_end - init2_start) 		) >> 4
   318                                  	sub dx, cx
   319                                  	jc .error
   320                                  	cmp dx, ax
   321                                  	jb .error
   322                                  %else
   323 00000051 B9EB14                  	mov cx, ( (payload_end - payload) 		+ (init1_end - init1_start) 		) >> 4
   326                                  				; cx = amount paragraphs in payload + INIT1
   327 00000054 29CA                    	sub dx, cx		; => destination for payload + INIT1
   328                                  				;  (highest possible spot in available memory)
   329                                  %endif
   330 00000056 56                      	push si			; preserve destination
   331 00000057 E8F300                  	call init0_movp		; move up payload + INIT1
   332 0000005A 5E                      	pop si
   333                                  %else
   334                                  	cld
   335                                  	mov ax, cs
   336                                  	add ax, (init0_end - init0_start) >> 4
   337                                  	mov cx, ( (payload_end - payload) 		+ (init1_end - init1_start) 		) >> 4
   340                                  	mov dx, word [ bp + ldLoadTop ]
   341                                  	sub dx, cx
   342                                  	call init0_movp
   343                                  %endif
   344                                  
   345 0000005B 89D0                    	mov ax, dx		; => payload
   346                                  %if _TEST_PROGRAM
   347                                  	add ax, ( 		+ (payload_end - payload) 		+ (init1_end - init1_start) 		) >> 4		; => INIT2
   351                                  %else
   352 0000005D 05AF14                  	add ax, ( 		+ (payload_end - payload) 		) >> 4		; => INIT1
   355                                  %endif
   356 00000060 31C9                    	xor cx, cx
   357 00000062 50                      	push ax
   358 00000063 51                      	push cx
   359 00000064 CB                      	retf			; jump to relocated INIT1:init1_start
   360                                  				;  or INIT2:init2_start
   361                                  
   362                                  
   363                                  %if _TEST_PROGRAM
   364                                  .error:
   365                                  	mov di, bx
   366                                  
   367                                  	push cs
   368                                  	pop ds
   369                                  	mov dx, init0_msg.error_stderr + 256
   370                                  	mov cx, init0_msg.error_stderr.length
   371                                  	mov bx, 2
   372                                  	mov ah, 40h
   373                                  	int 21h
   374                                  
   375                                  	test di, 8000h
   376                                  	jnz @F
   377                                  	mov dx, init0_msg.error_stdout + 256
   378                                  	mov cx, init0_msg.error_stdout.length
   379                                  	mov bx, 1
   380                                  	mov ah, 40h
   381                                  	int 21h
   382                                  @@:
   383                                  	mov ax, 4CFFh
   384                                  	int 21h
   385                                  %endif
   386                                  
   387                                  
   388                                  %if _DEVICE
   389                                  device_entrypoint:
   390                                  %if _TEST_PROGRAM
   391                                  @@:
   392                                  	int3
   393                                  	sti
   394                                  	hlt
   395                                  	jmp @B
   396                                  %endif
   397 00000065 26807F0200              	cmp byte [es:bx + 2], 0		; command code 0 (init) ?
   398 0000006A 7407                    	je @F
   399                                  
   400 0000006C 26C747030381            	mov word [es:bx + 3], 8103h	; error, done, code: unknown command
   401 00000072 CB                      	retf
   402                                  
   403                                  @@:
   404 00000073 2E830E[0000]FF          	or word [cs:device_header.next], -1
   405 00000079 0E                      	push cs
   406 0000007A 2EFF36[0600]            	push word [cs:device_header.strategy]
   407                                  					; -> far return to payload's strategy
   408 0000007F 55                      	push bp
   409 00000080 1E                      	push ds
   410 00000081 56                      	push si
   411 00000082 57                      	push di
   412 00000083 52                      	push dx
   413 00000084 51                      	push cx
   414 00000085 50                      	push ax
   415 00000086 53                      	push bx
   416 00000087 06                      	push es
   417                                  
   418 00000088 268B4710                	mov ax, word [es:bx + 14 + 2]
   419 0000008C 31D2                    	xor dx, dx
   420 0000008E B90400                  	mov cx, 4
   421                                  @@:
   422 00000091 D1E0                    	shl ax, 1
   423 00000093 D1D2                    	rcl dx, 1
   424 00000095 E2FA                    	loop @B
   425 00000097 2603470E                	add ax, word [es:bx + 14]
   426 0000009B 83D200                  	adc dx, 0		; dx:ax = linear address behind end
   427                                  
   428 0000009E B104                    	mov cl, 4
   429                                  @@:
   430 000000A0 D1EA                    	shr dx, 1
   431 000000A2 D1D8                    	rcr ax, 1
   432 000000A4 E2FA                    	loop @B			; ax => behind end
   433                                  
   434 000000A6 8CCF                    	mov di, cs
   435                                  image_size: equ  ( (init0_end - init0_start) 		+ (payload_end - payload) 		+ (init1_end - init1_start) 		)
   439 000000A8 81C70815                	add di, image_size >> 4		; di => end of image
   440                                  
   441 000000AC 39F8                    	cmp ax, di			; enough memory to hold all of us ?
   442 000000AE 7203E98A00              	jae .have_some_memory		; yes -->
   443                                  
   444 000000B3 0E                      	push cs
   445 000000B4 1F                      	pop ds
   446 000000B5 BA[F400]                	mov dx, .msg_no_memory
   447 000000B8 B409                    	mov ah, 09h
   448 000000BA CD21                    	int 21h
   449                                  
   450 000000BC B80030                  	mov ax, 3000h
   451 000000BF CD21                    	int 21h
   452 000000C1 3C05                    	cmp al, 5
   453 000000C3 7307                    	jae @F
   454 000000C5 BA[1301]                	mov dx, .msg_dos_below_5
   455 000000C8 B409                    	mov ah, 09h
   456 000000CA CD21                    	int 21h
   457                                  @@:
   458                                  
   459 000000CC BA[3A01]                	mov dx, .msg_linebreak
   460 000000CF B409                    	mov ah, 09h
   461 000000D1 CD21                    	int 21h
   462                                  
   463                                  		; tear down the stack frame and modify the request header
   464 000000D3 07                      	pop es
   465 000000D4 5B                      	pop bx
   466 000000D5 26C747030381            	mov word [es:bx + 3], 8103h	; set error, done, invalid command
   467 000000DB 26C6470D00              	mov byte [es:bx + 13], 0	; set number of units = 0
   468 000000E0 268C4F10                	mov word [es:bx + 14 + 2], cs
   469 000000E4 2683670E00              	and word [es:bx + 14], 0	; -> after end of memory to allocate
   470 000000E9 58                      	pop ax
   471 000000EA 59                      	pop cx
   472 000000EB 5A                      	pop dx
   473 000000EC 5F                      	pop di
   474 000000ED 5E                      	pop si
   475 000000EE 1F                      	pop ds
   476 000000EF 5D                      	pop bp
   477 000000F0 83C404                  	add sp, 4		; discard far return to payload's strategy
   478 000000F3 CB                      	retf			; return to DOS
   479                                  
   480 000000F4 4C6F6164206572726F-     .msg_no_memory:		ascic "Load error: Not enough memory."
   480 000000FD 723A204E6F7420656E-
   480 00000106 6F756768206D656D6F-
   480 0000010F 72792E24           
   481 00000113 204E6F74653A20444F-     .msg_dos_below_5:	ascic " Note: DOS must be at least version 5."
   481 0000011C 53206D757374206265-
   481 00000125 206174206C65617374-
   481 0000012E 2076657273696F6E20-
   481 00000137 352E24             
   482 0000013A 0D0A24                  .msg_linebreak:		ascic 13,10
   483                                  
   484                                  
   485                                  .have_some_memory:
   486 0000013D 89C2                    	mov dx, ax		; => after end of memory
   487 0000013F BB0200                  	mov bx, 2		; device mode
   488 00000142 50                      	push ax
   489 00000143 8CC8                    	mov ax, cs
   490 00000145 83C01D                  	add ax, (init0_end - init0_start) >> 4
   491 00000148 8CCE                    	mov si, cs		; => destination
   492 0000014A E903FF                  	jmp init0_common
   493                                  %endif
   494                                  
   495                                  
   496                                  		; Move paragraphs
   497                                  		;
   498                                  		; INP:	ax => source
   499                                  		;	dx => destination
   500                                  		;	cx = number of paragraphs
   501                                  		; CHG:	cx, ds, si, es, di
   502                                  		; OUT:	ax and dx unchanged
   503                                  		; Note:	Doesn't work correctly on HMA; doesn't always wrap to LMA either.
   504                                  		;	Do not provide a wrapped/HMA source or destination!
   505                                  init0_movp:
   506 0000014D 39D0                    	cmp ax, dx		; source above destination ?
   507 0000014F 770A                    	ja .up			; yes, move up (forwards) -->
   508 00000151 747B                    	je .return		; same, no need to move -->
   509 00000153 50                      	push ax
   510 00000154 01C8                    	add ax, cx		; (expected not to carry)
   511 00000156 39D0                    	cmp ax, dx		; end of source is above destination ?
   512 00000158 58                      	pop ax
   513 00000159 7730                    	ja .down		; yes, move from top down -->
   514                                  	; Here, the end of source is below-or-equal the destination,
   515                                  	;  so they do not overlap. In this case we prefer moving up.
   516                                  
   517                                  .up:
   518 0000015B 50                      	push ax
   519 0000015C 52                      	push dx
   520                                  .uploop:
   521 0000015D 8ED8                    	mov ds, ax
   522 0000015F 8EC2                    	mov es, dx
   523 00000161 31FF                    	xor di, di
   524 00000163 31F6                    	xor si, si		; -> start of segment
   525 00000165 81E90010                	sub cx, 1000h		; 64 KiB left ?
   526 00000169 7610                    	jbe .uplast		; no -->
   527 0000016B 51                      	push cx
   528 0000016C B90080                  	mov cx, 10000h /2
   529 0000016F F3A5                    	rep movsw		; move 64 KiB
   530 00000171 59                      	pop cx
   531 00000172 050010                  	add ax, 1000h
   532 00000175 81C20010                	add dx, 1000h		; -> next segment
   533 00000179 EBE2                    	jmp short .uploop	; proceed for more -->
   534                                  .uplast:
   535 0000017B 81C10010                	add cx, 1000h		; restore counter
   536 0000017F D1E1                    	shl cx, 1
   537 00000181 D1E1                    	shl cx, 1
   538 00000183 D1E1                    	shl cx, 1		; *8, paragraphs to words
   539 00000185 F3A5                    	rep movsw		; move last part
   540 00000187 5A                      	pop dx
   541 00000188 58                      	pop ax
   542 00000189 EB43                    	jmp short .return
   543                                  
   544                                  .down:
   545 0000018B FD                      	std			; _AMD_ERRATUM_109_WORKAROUND as below
   546                                  .dnloop:
   547 0000018C 81E90010                	sub cx, 1000h		; 64 KiB left ?
   548 00000190 761A                    	jbe .dnlast		; no -->
   549 00000192 50                      	push ax
   550 00000193 52                      	push dx
   551 00000194 01C8                    	add ax, cx
   552 00000196 01CA                    	add dx, cx
   553 00000198 8ED8                    	mov ds, ax		; -> 64 KiB not yet moved
   554 0000019A 8EC2                    	mov es, dx
   555 0000019C 5A                      	pop dx
   556 0000019D 58                      	pop ax
   557 0000019E BFFEFF                  	mov di, -2
   558 000001A1 89FE                    	mov si, di		; moved from last word down
   559 000001A3 51                      	push cx
   560 000001A4 B90080                  	mov cx, 10000h /2
   561 000001A7 F3A5                    	rep movsw		; move 64 KiB
   562 000001A9 59                      	pop cx
   563 000001AA EBE0                    	jmp short .dnloop	; proceed for more -->
   564                                  .dnlast:
   565 000001AC 81C10010                	add cx, 1000h		; restore counter
   566 000001B0 D1E1                    	shl cx, 1
   567 000001B2 D1E1                    	shl cx, 1
   568 000001B4 D1E1                    	shl cx, 1		; *8, paragraphs to words
   569 000001B6 89CF                    	mov di, cx
   570 000001B8 4F                      	dec di
   571 000001B9 D1E7                    	shl di, 1		; words to offset, -> last word
   572 000001BB 89FE                    	mov si, di
   573 000001BD 8ED8                    	mov ds, ax
   574 000001BF 8EC2                    	mov es, dx		; first segment correct
   575                                  
   576                                  
   577                                  	numdef AMD_ERRATUM_109_WORKAROUND, 1
   578                                  %if 0
   579                                  
   580                                  Jack R. Ellis pointed out this erratum:
   581                                  
   582                                  Quoting from https://www.amd.com/system/files/TechDocs/25759.pdf page 69:
   583                                  
   584                                  109   Certain Reverse REP MOVS May Produce Unpredictable Behavior
   585                                  
   586                                  Description
   587                                  
   588                                  In certain situations a REP MOVS instruction may lead to
   589                                  incorrect results. An incorrect address size, data size
   590                                  or source operand segment may be used or a succeeding
   591                                  instruction may be skipped. This may occur under the
   592                                  following conditions:
   593                                  
   594                                  * EFLAGS.DF=1 (the string is being moved in the reverse direction).
   595                                  
   596                                  * The number of items being moved (RCX) is between 1 and 20.
   597                                  
   598                                  * The REP MOVS instruction is preceded by some microcoded instruction
   599                                    that has not completely retired by the time the REP MOVS begins
   600                                    execution. The set of such instructions includes BOUND, CLI, LDS,
   601                                    LES, LFS, LGS, LSS, IDIV, and most microcoded x87 instructions.
   602                                  
   603                                  Potential Effect on System
   604                                  
   605                                  Incorrect results may be produced or the system may hang.
   606                                  
   607                                  Suggested Workaround
   608                                  
   609                                  Contact your AMD representative for information on a BIOS update.
   610                                  
   611                                  %endif
   612                                  
   613                                  %if _AMD_ERRATUM_109_WORKAROUND
   614 000001C1 E308                    	jcxz @FF
   615 000001C3 83F914                  	cmp cx, 20
   616 000001C6 7703                    	ja @FF
   617                                  @@:
   618 000001C8 A5                      	movsw
   619 000001C9 E2FD                    	loop @B
   620                                  @@:
   621                                  %endif
   622 000001CB F3A5                    	rep movsw		; move first part
   623 000001CD FC                      	cld
   624                                  .return:
   625 000001CE C3                      	retn
   626                                  
   627                                  
   628                                  	section PAYLOAD align=16 follows=INIT0
   629                                  payload:
   630 00000000 <bin 14AEBh>            	incbin _PAYLOAD_FILE
   631                                  .end:
   632 00014AEB 26<rep 5h>              	align 16, db 38
   633                                  payload_end:
   634                                  
   635                                  
   636                                  	section INIT1 align=16 follows=PAYLOAD vstart=0
   637                                  init1_start:
   638                                  		; INP:	ax = cs = INIT1
   639                                  		;	dx = cs - (payload_end - payload) >> 4 => source data
   640                                  		;	if kernel mode:
   641                                  		;	 ss:bp -> LOADDATA and LOADSTACKVARS
   642                                  		;	 ss:sp -> valid stack above [bp + ldLoadTop]
   643                                  		;	 60h => destination
   644                                  		;	any mode:
   645                                  		;	 word [ss:sp] = value for ax
   646                                  		;	 bx = 2 if device mode, 1 if EXE mode, 0 if kernel mode
   647                                  		;	 si => destination (60h for kernel mode,
   648                                  		;		after PSP for EXE mode,
   649                                  		;		at device header for device mode)
   650                                  		;		(not set if only kernel mode supported)
   651                                  		;	 if EXE mode:
   652                                  		;	  ss:sp -> valid stack above INIT1
   653                                  		;	  bp = unset
   654                                  		;	 if device mode:
   655                                  		;	  ss:sp -> device entrypoint stack
   656                                  		;	  holds: es, bx, ax, cx, dx, di, si, ds, bp,
   657                                  		;		  far address of payload strategy entrypoint,
   658                                  		;		  far return address to DOS
   659                                  		;	  bp = unset
   660                                  		; STT:	UP
   661                                  		; CHG:	ax, bx, cx, dx, es, ds, si, di
   662                                  %if _IMAGE_EXE || _DEVICE
   663                                  	lframe
   664 00000000 5589E5                  	lenter
   665                                  	lvar word,	exemode		; must be bp - 2!
   666 00000003 53                      	 push bx
   667                                   %if ?exemode != -2
   668                                    %error exemode variable must be directly below bp
   669                                   %endif
   670 00000004 56                      	push si
   671                                  %else
   672                                  	xor bx, bx		; always tell them it is kernel mode
   673                                  %endif
   674                                  
   675                                  %if _IMAGE_EXE
   676 00000005 8EC6                    	mov es, si		; es => destination
   677                                  %else
   678                                  	mov ax, 60h
   679                                  	mov es, ax		; es => destination
   680                                  %endif
   681 00000007 31FF                    	xor di, di		; -> destination
   682                                  
   683 00000009 8EDA                    	mov ds, dx
   684 0000000B 31F6                    	xor si, si		; -> source
   685                                  
   686 0000000D B9EB4A                  	mov cx, (payload.end - payload) & 0FFFFh
   687                                  %if (payload.end - payload) >> 16
   688 00000010 BA0100                  	mov dx, (payload.end - payload) >> 16	; = length of source
   689                                  %else
   690                                  	xor dx, dx
   691                                  %endif
   692                                  %if _PAYLOAD_KERNEL_MAX_PARAS
   693                                  	mov ax, _PAYLOAD_KERNEL_MAX_PARAS
   694                                  	test bx, bx
   695                                  	jz @F
   696                                  %endif
   697 00000013 B8FFFF                  	mov ax, -1
   698                                  @@:
   699                                  		; INP:	ds:si -> source
   700                                  		;	dx:cx = length of source
   701                                  		;	es:di -> destination (below source)
   702                                  		;	bx = EXE mode flag
   703                                  		;	 (1 if EXE mode, 0 if kernel mode)
   704                                  		;	 (always 0 if this is a build without EXE mode)
   705                                  		;	if _PAYLOAD_KERNEL_MAX_PARAS:
   706                                  		;	 ax = maximum amount in paragraphs of destination needed
   707                                  		;	 (-1 if full source should be decompressed)
   708                                  		;	else:
   709                                  		;	 ax = -1
   710                                  		; Note:	The destination reaches up to below the source.
   711 00000016 E80201                  	call depack
   712                                  %ifn _TEST_PROGRAM
   713 00000019 7234                    	jc strict short error
   714                                  %endif
   715                                  %if _IMAGE_EXE
   716 0000001B 5E                      	pop si			; si
   717                                   %if _TEST_PROGRAM
   718                                  	pop ax			; (discard ?exemode, leave bx as returned)
   719                                   %else
   720 0000001C 5B                      	pop bx			; ?exemode
   721                                   %endif
   722 0000001D 5D                      	pop bp			; bp
   723 0000001E 58                      	pop ax			; ax
   724                                  	lleave ctx
   725                                  %endif
   726                                  %if _TEST_PROGRAM
   727                                  	retf
   728                                  	nop
   729                                  %endif
   730                                  
   731                                  %if _DEVICE
   732 0000001F F6C302                  	test bl, 2
   733 00000022 7411                    	jz .jmp_exe_or_kernel_mode
   734                                  
   735                                  .jmp_device_mode:
   736 00000024 8EDE                    	mov ds, si
   737 00000026 830E[0000]FF            	or word [device_header.next], -1
   738 0000002B 07                      	pop es
   739 0000002C 5B                      	pop bx
   740 0000002D 58                      	pop ax
   741 0000002E 59                      	pop cx
   742 0000002F 5A                      	pop dx
   743 00000030 5F                      	pop di
   744 00000031 5E                      	pop si
   745 00000032 1F                      	pop ds
   746 00000033 5D                      	pop bp
   747 00000034 CB                      	retf			; transfer to payload strategy entrypoint
   748                                  				; still on stack: far return address to DOS
   749                                  
   750                                  .jmp_exe_or_kernel_mode:
   751                                  %endif
   752                                  
   753                                  %if _IMAGE_EXE
   754 00000035 F6C301                  	test bl, 1
   755 00000038 7410                    	jz .jmp_kernel_mode
   756                                  
   757                                  .jmp_exe_mode:
   758                                  %if ! _IMAGE_EXE_AUTO_STACK
   759                                  	mov cx, cs
   760                                  	lea dx, [si + _IMAGE_EXE_SS]
   761                                  	push dx			; stack = relocated ss value
   762                                  	add dx, (_IMAGE_EXE_SP + 2 + 15) >> 4
   763                                  	cmp cx, dx		; INIT1 code is above intended stack ?
   764                                  	jae @F			; yes -->
   765                                  
   766                                  	lframe
   767                                  	lenter
   768                                  	lvar	word, exemode
   769                                  	 push bx
   770                                   %if ?exemode != -2
   771                                    %error exemode variable must be directly below bp
   772                                   %endif
   773                                  	mov bx, -1		; unimplemented, return error
   774                                  	jmp error
   775                                  	lleave ctx
   776                                  @@:
   777                                  	cli
   778                                  	pop ss			; = relocated ss value
   779                                  	mov sp, (_IMAGE_EXE_SP + 2) & 0FFFFh	; change stack
   780                                  	sti
   781                                  %endif
   782                                  
   783 0000003A 31C9                    	xor cx, cx
   784 0000003C 51                      	push cx			; put zero on top of stack
   785                                  
   786                                  %if _IMAGE_EXE_IP < 256
   787                                  	mov cl, _IMAGE_EXE_IP
   788                                  %elif (_IMAGE_EXE_IP & 255) == 0
   789                                  	mov ch, _IMAGE_EXE_IP >> 8
   790                                  %else
   791 0000003D B94001                  	mov cx, _IMAGE_EXE_IP
   792                                  %endif
   793                                  
   794                                  %if _IMAGE_EXE_CS == -16
   795 00000040 83C6F0                  	add si, -16
   796 00000043 8EDE                    	mov ds, si
   797 00000045 8EC6                    	mov es, si
   798                                  %else
   799                                  	lea dx, [si - 10h]	; => PSP
   800                                  	mov ds, dx
   801                                  	mov es, dx		; ds = es => PSP
   802                                  	add si, _IMAGE_EXE_CS	; = relocated cs value
   803                                  %endif
   804 00000047 56                      	push si
   805 00000048 51                      	push cx
   806 00000049 CB                      	retf			; jump to EXE mode of image
   807                                  
   808                                  %endif
   809                                  .jmp_kernel_mode:
   810 0000004A EA20006000              	jmp 60h + _EXEC_SEGMENT:_EXEC_OFFSET
   811                                  
   812                                  
   813                                  error:
   814 0000004F 0E                      	push cs
   815 00000050 1F                      	pop ds
   816                                  %if _DEBUG0
   817                                  	mov si, msg.error_begin
   818                                  	call disp_error
   819                                  	xchg ax, bx			; ax = error code
   820                                  	call disp_ax_hex
   821                                  	mov si, msg.error_end
   822                                  %else
   823 00000051 BE[F600]                	mov si, msg.error
   824                                  %endif
   825 00000054 E87D00                  	call disp_error
   826                                  
   827                                  %if _DEVICE
   828 00000057 F646FE02                	test byte [bp - 2], 2
   829 0000005B 7435                    	jz .exit_app_or_kernel
   830                                  
   831 0000005D 5E                      	pop si				; => device segment
   832 0000005E 5B                      	pop bx				; mode word
   833 0000005F 5D                      	pop bp
   834 00000060 58                      	pop ax
   835 00000061 8EC6                    	mov es, si
   836 00000063 31FF                    	xor di, di			; -> device header
   837 00000065 0E                      	 push cs
   838 00000066 1F                      	 pop ds
   839 00000067 BE[B000]                	mov si, device_header_copy	; -> to reset header
   840 0000006A B91100                  	mov cx, words(device_header_copy.length)
   841 0000006D F3A5                    	rep movsw		; overwrite device header with default
   842                                  				;  (reset to a valid state after unsuccessful
   843                                  				;  decompression, which may have partially
   844                                  				;  written the header already)
   845 0000006F 06                      	 push es
   846 00000070 1F                      	 pop ds				; -> device segment
   847 00000071 07                      	pop es
   848 00000072 5B                      	pop bx
   849 00000073 26C747030381            	mov word [es:bx + 3], 8103h	; set error, done, invalid command
   850 00000079 26C6470D00              	mov byte [es:bx + 13], 0	; set number of units = 0
   851 0000007E 268C5F10                	mov word [es:bx + 14 + 2], ds
   852 00000082 2683670E00              	and word [es:bx + 14], 0	; -> after end of memory to allocate
   853 00000087 58                      	pop ax
   854 00000088 59                      	pop cx
   855 00000089 5A                      	pop dx
   856 0000008A 5F                      	pop di
   857 0000008B 5E                      	pop si
   858 0000008C 1F                      	pop ds
   859 0000008D 5D                      	pop bp
   860 0000008E 83C404                  	add sp, 4		; discard far return to payload's strategy
   861 00000091 CB                      	retf			; return to DOS
   862                                  
   863                                  
   864                                  .exit_app_or_kernel:
   865                                  %endif
   866                                  
   867                                  %if _IMAGE_EXE
   868                                  %if _BOOTLDR
   869 00000092 F646FE01                	test byte [bp - 2], 1
   870 00000096 7405                    	jz .exit_kernel_mode
   871                                  %endif
   872                                  
   873 00000098 B87F4C                  	mov ax, 4C7Fh
   874 0000009B CD21                    	int 21h
   875                                  
   876                                  .exit_kernel_mode:
   877                                  %endif
   878                                  %if _BOOTLDR
   879 0000009D 31C0                    	xor ax, ax
   880 0000009F CD16                    	int 16h
   881 000000A1 CD19                    	int 19h
   882                                  %endif
   883                                  
   884                                  
   885                                  %if _DEVICE
   886 000000A3 90<rep Dh>              	align 16
   887                                  device_header_copy:
   888                                  .:
   889                                  .next:
   890 000000B0 FFFFFFFF                	dd -1				; already initialised
   891                                  .attributes:
   892 000000B4 0088                    	dw _DEVICE_ATTRIBUTE
   893                                  .strategy:
   894 000000B6 1200                    	dw .strategy_entry - .		; -> strategy entry
   895                                  .interrupt:
   896 000000B8 1600                    	dw .interrupt_entry - .		; -> interrupt entry
   897                                  .name:
   898 000000BA 4C44454255472424        	fill 8, 32, db _DEVICE_NAME
   899                                  .strategy_entry:
   900 000000C2 EB0690<rep 2h>          	fill 4, 90h, jmp .set_error	; rel8 or rel16 jump, not minus .
   901                                  .interrupt_entry:
   902 000000C6 CB90<rep 3h>            	fill 4, 90h, retf
   903                                  
   904                                  .set_error:
   905 000000CA 26C747030381            	mov word [es:bx + 3], 8103h	; set error, done, invalid command
   906 000000D0 CB                      	retf
   907                                  .length: equ $ - .
   908                                  %endif
   909                                  
   910                                  
   911                                  disp_error.loop:
   912 000000D1 E80600                  	call disp_al
   913                                  disp_error:
   914 000000D4 AC                      	lodsb
   915 000000D5 84C0                    	test al, al
   916 000000D7 75F8                    	jnz .loop
   917 000000D9 C3                      	retn
   918                                  
   919                                  %if _DEBUG0 || _DEBUG1 || _DEBUG2 || _DEBUG3
   920                                  disp_ax_hex:			; ax
   921                                  		xchg al,ah
   922                                  		call disp_al_hex		; display former ah
   923                                  		xchg al,ah			;  and fall trough for al
   924                                  disp_al_hex:			; al
   925                                  		push cx
   926                                  		mov cl,4
   927                                  		ror al,cl
   928                                  		call disp_al_lownibble_hex	; display former high-nibble
   929                                  		rol al,cl
   930                                  		pop cx
   931                                  						;  and fall trough for low-nibble
   932                                  disp_al_lownibble_hex:
   933                                  		push ax			 ; save ax for call return
   934                                  		and al,00001111b		; high nibble must be zero
   935                                  		add al,'0'			; if number is 0-9, now it's the correct character
   936                                  		cmp al,'9'
   937                                  		jna .decimalnum		 ; if we get decimal number with this, ok -->
   938                                  		add al,7			;  otherwise, add 7 and we are inside our alphabet
   939                                   .decimalnum:
   940                                  		call disp_al
   941                                  		pop ax
   942                                  		retn
   943                                  %endif
   944                                  
   945                                  disp_al:
   946                                  %if _TEST_PROGRAM
   947                                  	retn
   948                                  %else
   949 000000DA 50                      	push ax
   950                                  %endif
   951                                  %if _BOOTLDR
   952 000000DB 53                      	push bx
   953 000000DC 55                      	push bp
   954 000000DD B40E                    	mov ah, 0Eh
   955 000000DF BB0700                  	mov bx, 7
   956                                  %endif
   957                                  %if _IMAGE_EXE || _DEVICE
   958 000000E2 52                      	push dx
   959                                  
   960                                  %if _BOOTLDR
   961 000000E3 F646FE03                	test byte [bp - 2], 1 | 2
   962 000000E7 7406                    	jz .display_kernel_mode
   963                                  %endif
   964                                  
   965 000000E9 92                      	xchg dx, ax				; dl = input al
   966 000000EA B402                    	mov ah, 02h
   967 000000EC CD21                    	int 21h
   968                                  %if _BOOTLDR
   969 000000EE A9                      	db __TEST_IMM16				; (skip int)
   970                                  %endif
   971                                  
   972                                  .display_kernel_mode:
   973                                  %endif
   974                                  %if _BOOTLDR
   975 000000EF CD10                    	int 10h
   976                                  %endif
   977                                  %if _IMAGE_EXE
   978 000000F1 5A                      	pop dx
   979                                  %endif
   980                                  %if _BOOTLDR
   981 000000F2 5D                      	pop bp
   982 000000F3 5B                      	pop bx
   983                                  %endif
   984 000000F4 58                      	pop ax
   985                                  disp_error.ret:
   986 000000F5 C3                      	retn
   987                                  
   988                                  
   989                                  msg:
   990                                  %if _DEBUG0
   991                                  .error_begin:	db "Load error: Decompression failure, code ",0
   992                                  .error_end:	db "h.",13,10,0
   993                                  %else
   994 000000F6 4C6F6164206572726F-     .error:		db "Load error: Decompression failure.",13,10,0
   994 000000FF 723A204465636F6D70-
   994 00000108 72657373696F6E2066-
   994 00000111 61696C7572652E0D0A-
   994 0000011A 00                 
   995                                  %endif
   996                                  
   997                                  
   998                                  %assign NEED_NORMALISE_POINTER_WITH_DISPLACEMENT 1
   999                                  %assign CHECK_POINTERS_VARIABLE_SRC 1
  1000                                  %assign CHECK_POINTERS_VARIABLE_DST 1
  1001                                  
  1002                                  		; Specific depacker's file is included within label msg.
  1003                                  		; In the file, lframe is used and lleave ctx is not used.
  1004                                  
  1005                                  %if _BRIEFLZ
  1006                                  	%include "brieflz.asm"
  1007                                  %endif
  1008                                  
  1009                                  
  1010                                  %if _LZ4
  1011                                  	%include "lz4.asm"
  1012                                  %endif
  1013                                  
  1014                                  
  1015                                  %if _SNAPPY
  1016                                  	%include "snappy.asm"
  1017                                  %endif
  1018                                  
  1019                                  
  1020                                  %if _EXODECR
  1021                                  	%include "exodecr.asm"
  1022                                  %endif
  1023                                  
  1024                                  
  1025                                  %if _X
  1026                                  	%include "x.asm"
  1027                                  %endif
  1028                                  
  1029                                  
  1030                                  %if _HEATSHRINK
  1031                                  	%include "heatshr.asm"
  1032                                  %endif
  1033                                  
  1034                                  
  1035                                  %if _LZD
  1036                                  	%include "lzd.asm"
  1037                                  %endif
  1038                                  
  1039                                  
  1040                                  %if _LZO
  1041                                  	%include "lzo.asm"
  1042                                  %endif
  1043                                  
  1044                                  
  1045                                  %if _LZSA2
  1046                                  	%include "lzsa2.asm"
     1                              <1> 
     2                              <1> ; 8086 Assembly lDOS iniload payload LZSA2 depacker
     3                              <1> ;  by C. Masloch, 2021
     4                              <1> ;
     5                              <1> ;  based on:
     6                              <1> ;  decompress_small.S - space-efficient decompressor implementation for 8088
     7                              <1> ;
     8                              <1> ;  Copyright (C) 2019 Emmanuel Marty
     9                              <1> ;
    10                              <1> ;  This software is provided 'as-is', without any express or implied
    11                              <1> ;  warranty.  In no event will the authors be held liable for any damages
    12                              <1> ;  arising from the use of this software.
    13                              <1> ;
    14                              <1> ;  Permission is granted to anyone to use this software for any purpose,
    15                              <1> ;  including commercial applications, and to alter it and redistribute it
    16                              <1> ;  freely, subject to the following restrictions:
    17                              <1> ;
    18                              <1> ;  1. The origin of this software must not be misrepresented; you must not
    19                              <1> ;     claim that you wrote the original software. If you use this software
    20                              <1> ;     in a product, an acknowledgment in the product documentation would be
    21                              <1> ;     appreciated but is not required.
    22                              <1> ;  2. Altered source versions must be plainly marked as such, and must not be
    23                              <1> ;     misrepresented as being the original software.
    24                              <1> ;  3. This notice may not be removed or altered from any source distribution.
    25                              <1> 
    26                              <1> 
    27                              <1> 	numdef COUNTER,		0, 128
    28                              <1> %if (_COUNTER - 1) & _COUNTER
    29                              <1>  %error COUNTER must be a power of two
    30                              <1> %endif
    31                              <1> 
    32                              <1> %assign CHECK_POINTERS_VARIABLE_SRC 0
    33                              <1> %assign CHECK_POINTERS_VARIABLE_DST 0
    34                              <1> 
    35                              <1> 
    36                              <1> 		; INP:	ds:si -> source
    37                              <1> 		;	dx:cx = length of source
    38                              <1> 		;	es:di -> destination (below source)
    39                              <1> 		;	if _IMAGE_EXE:
    40                              <1> 		;	 bx = EXE mode flag (bit 0)
    41                              <1> 		;	else:
    42                              <1> 		;	 bx = 0
    43                              <1> 		;	if _PAYLOAD_KERNEL_MAX_PARAS:
    44                              <1> 		;	 ax = maximum amount in paragraphs of destination needed
    45                              <1> 		;	 (-1 if full source should be decompressed)
    46                              <1> 		;	else:
    47                              <1> 		;	 ax = -1
    48                              <1> 		; OUT:	NC if success
    49                              <1> 		;	CY if error,
    50                              <1> 		;	 bx = ?errordata (if _DEBUG0)
    51                              <1> 		; CHG:	ax, (bx), cx, dx, es, ds, si, di
    52                              <1> 		; STT:	UP
    53                              <1> 		;
    54                              <1> 		; Note:	The destination reaches up to below the source.
    55                              <1> 		; Note:	The input pointers need not be normalised yet.
    56                              <1> 		;	 Normalised means that the offset part is below 16.
    57                              <1> depack:
    58                              <1> 	lframe near
    59                              <1> %if _IMAGE_EXE || _DEBUG0 || _COUNTER
    60 0000011B 5589E5              <1> 	lenter early
    61                              <1>  %if _IMAGE_EXE
    62                              <1> 	lvar word,	exemode	; must be bp - 2!
    63 0000011E 53                  <1> 	 push bx
    64                              <1>   %if ?exemode != -2
    65                              <1>    %error exemode variable must be directly below bp
    66                              <1>   %endif
    67                              <1>  %endif
    68                              <1>  %if _DEBUG0 || _COUNTER
    69                              <1> 	xor bx, bx
    70                              <1>  %endif
    71                              <1>  %if _DEBUG0
    72                              <1> 	lvar word,	errordata
    73                              <1> 	 push bx
    74                              <1>  %endif
    75                              <1> %endif
    76                              <1> 	lvar word,	counter
    77                              <1> %if _COUNTER
    78                              <1> 	 push bx		; initialise counter (high byte) to zero
    79                              <1> %endif
    80                              <1> 	lvar dword,	length_of_source_after_block
    81                              <1> 	lvar word,	matchlength
    82 0000011F 8D66F6              <1> 	lenter
    83                              <1> 
    84 00000122 E81502              <1> 	call normalise_both_pointers
    85                              <1> 
    86                              <1> 	lvar dword,	src
    87 00000125 1E                  <1> 	 push ds
    88 00000126 56                  <1> 	 push si
    89                              <1> 	lvar dword,	original_dst
    90 00000127 06                  <1> 	 push es
    91 00000128 57                  <1> 	 push di
    92                              <1> 	lvar dword,	dst
    93 00000129 06                  <1> 	 push es
    94 0000012A 57                  <1> 	 push di
    95                              <1> 	lvar dword,	length_of_source
    96 0000012B 52                  <1> 	 push dx
    97 0000012C 51                  <1> 	 push cx
    98                              <1> 
    99                              <1> %if 0 ; && _PAYLOAD_KERNEL_MAX_PARAS
   100                              <1> 	cmp ax, -1		; no maximum specified ?
   101                              <1> 	je @FF			; retain -1 in ax -->
   102                              <1> 	test di, di		; do we need an additional paragraph ?
   103                              <1> 	jz @F			; no -->
   104                              <1> 	inc ax			; es + ax => paragraph after necessary part
   105                              <1> @@:
   106                              <1> 	mov dx, es
   107                              <1> 	add ax, dx		; => paragraph after necessary part
   108                              <1> 		; If the normalised destination pointer's segment grows
   109                              <1> 		;  to this segment then enough has been decompressed.
   110                              <1> d0	mov byte [bp + ?errordata], 40h
   111                              <1> 	jc .error		; should not carry
   112                              <1> @@:
   113                              <1> 	lvar word,	dst_max_segment
   114                              <1> 	 push ax
   115                              <1> %endif
   116                              <1> 
   117 0000012D 1E                  <1> 	 push ds
   118 0000012E 56                  <1> 	 push si
   119 0000012F E86802              <1> 	call pointer_to_linear
   120                              <1> 
   121 00000132 89D3                <1> 	mov bx, dx
   122 00000134 91                  <1> 	xchg cx, ax		; bx:cx = source linear
   123                              <1> 
   124 00000135 06                  <1> 	 push es
   125 00000136 57                  <1> 	 push di
   126 00000137 E86002              <1> 	call pointer_to_linear
   127                              <1> 
   128                              <1> %if _ALLOW_OVERLAPPING
   129 0000013A 034EE6              <1> 	add cx, word [bp + ?length_of_source]
   130 0000013D 135EE8              <1> 	adc bx, word [bp + ?length_of_source + 2]
   131                              <1> 		; In case of allowing overlapping source and destination,
   132                              <1> 		;  the ?length_of_destination variable is set to
   133                              <1> 		;  ?src + ?length_of_source - ?dst, allowing to write to
   134                              <1> 		;  all of the source buffer (with the checks already in place
   135                              <1> 		;  from the default handling). Additional checks are done by
   136                              <1> 		;  calling check_pointers_not_overlapping. This is done after
   137                              <1> 		;  every change of ?dst to verify that the write pointer stays
   138                              <1> 		;  below-or-equal the read pointer.
   139                              <1> 		; This means the remaining source data may be corrupted by a
   140                              <1> 		;  write, but nothing after the source data is written to,
   141                              <1> 		;  so the error handling (in INIT1 after the data) still works.
   142                              <1> %endif
   143                              <1> 
   144 00000140 29C1                <1> 	sub cx, ax
   145 00000142 19D3                <1> 	sbb bx, dx		; bx:cx = source linear - destination linear
   146                              <1> 
   147                              <1> 	lvar dword,	length_of_destination
   148 00000144 53                  <1> 	 push bx		; push into [bp + ?length_of_destination + 2]
   149 00000145 51                  <1> 	 push cx		; push into [bp + ?length_of_destination]
   150                              <1> 
   151                              <1> %if _ALLOW_OVERLAPPING
   152 00000146 E8DD01              <1> 	call check_pointers_not_overlapping
   153                              <1> 		; Note:	We initially check here that the write pointer is
   154                              <1> 		;	 low enough, ie below-or-equal the read pointer.
   155                              <1> 		;	 Doing this check here (as well as after any
   156                              <1> 		;	 copied match) allows us to drop the check done
   157                              <1> 		;	 after moving literal bytes.
   158                              <1> d0	mov byte [bp + ?errordata], 1Bh
   159 00000149 7268                <1> 	jc .error
   160                              <1> %endif
   161                              <1> 
   162 0000014B E87600              <1> 	call .lodsw
   163 0000014E 3D7B9E              <1> 	cmp ax, 7Bh | (9Eh << 8)	; signature
   164 00000151 7560                <1> 	jne .error
   165 00000153 E86200              <1> 	call .lodsb
   166 00000156 3C20                <1> 	cmp al, 1 << 5			; traits byte, check for lzsa2
   167 00000158 7559                <1> 	jne .error
   168                              <1> 
   169                              <1> .loop:
   170 0000015A E8DD01              <1> 	call normalise_both_pointers
   171                              <1> 
   172 0000015D 31D2                <1> 	xor dx, dx
   173 0000015F E86200              <1> 	call .lodsw			; bits 0 to 15 of length
   174 00000162 92                  <1> 	xchg ax, dx
   175 00000163 E85200              <1> 	call .lodsb
   176 00000166 A87F                <1> 	test al, 0111_1111b		; reserved bits + bit 16 of length
   177 00000168 7549                <1> 	jnz .error			; error if nonzero -->
   178                              <1> 
   179 0000016A E8D401              <1> 	call normalise_dssi_pointer
   180                              <1> 
   181                              <1> 		; This first check is only really relevant
   182                              <1> 		;  for uncompressed blocks.
   183 0000016D 89FB                <1> 	mov bx, di
   184 0000016F 01D3                <1> 	add bx, dx			; fits in destination segment ?
   185 00000171 7240                <1> 	jc .error			;  (assuming bit 16 = 0)
   186                              <1> 		; This second check is to insure we don't
   187                              <1> 		;  have to normalise the source pointer
   188                              <1> 		;  until after the end of the block.
   189 00000173 89F3                <1> 	mov bx, si
   190 00000175 01D3                <1> 	add bx, dx			; fits in source segment ?
   191 00000177 723A                <1> 	jc .error
   192                              <1> 
   193 00000179 84C0                <1> 	test al, al			; uncompressed ?
   194 0000017B 92                  <1> 	xchg ax, dx
   195 0000017C 7905                <1> 	jns .compressed			; no -->
   196                              <1> .uncompressed:
   197 0000017E 91                  <1> 	xchg ax, cx
   198 0000017F F3A4                <1> 	rep movsb			; copy this
   199 00000181 EBD7                <1> 	jmp .loop
   200                              <1> 
   201                              <1> .compressed:
   202 00000183 8B4EE6              <1> 	mov cx, word [bp + ?length_of_source]
   203 00000186 8B5EE8              <1> 	mov bx, word [bp + ?length_of_source + 2]
   204                              <1> 
   205 00000189 85C0                <1> 	test ax, ax
   206 0000018B 7421                <1> 	jz .end
   207                              <1> 
   208 0000018D 29C1                <1> 	sub cx, ax
   209 0000018F 83DB00              <1> 	sbb bx, 0
   210 00000192 721F                <1> 	jc .error
   211 00000194 894EF8              <1> 	mov word [bp + ?length_of_source_after_block], cx
   212 00000197 895EFA              <1> 	mov word [bp + ?length_of_source_after_block + 2], bx
   213                              <1> 
   214 0000019A 06                  <1> 	push es
   215 0000019B 57                  <1> 	push di
   216 0000019C E83100              <1> 	call lzsa2_decompress
   217 0000019F 83F801              <1> 	cmp ax, 1			; CY if ax == 0
   218 000001A2 91                  <1> 	xchg cx, ax
   219 000001A3 19DB                <1> 	sbb bx, bx			; -1 if cx == 0, else 0
   220 000001A5 F7DB                <1> 	neg bx				; 1 if cx == 0, else 0
   221 000001A7 E8C101              <1> 	call normalise_pointer_with_displacement_bxcx
   222 000001AA 5F                  <1> 	pop di
   223 000001AB 07                  <1> 	pop es
   224 000001AC EBAC                <1> 	jmp .loop
   225                              <1> 
   226                              <1> .end:
   227 000001AE 09CB                <1> 	or bx, cx
   228 000001B0 7501                <1> 	jnz .error
   229                              <1> 
   230                              <1> .ret:
   231 000001B2 A8                  <1> 	db __TEST_IMM8		; (skip stc, NC)
   232                              <1> .error:
   233 000001B3 F9                  <1> 	stc
   234                              <1> 
   235                              <1> %if _COUNTER
   236                              <1> 	lahf
   237                              <1> 	mov al, 13
   238                              <1> 	call disp_al
   239                              <1> 	mov al, 10
   240                              <1> 	call disp_al
   241                              <1> 	sahf
   242                              <1> %endif
   243                              <1> d0	mov bx, word [bp + ?errordata]
   244 000001B4 89EC5D              <1> 	lleave code
   245 000001B7 C3                  <1> 	lret
   246                              <1> 
   247                              <1> 
   248                              <1> 		; INP:	ds:si -> source data
   249                              <1> 		;	?length_of_source
   250                              <1> 		; OUT:	CY if error (source buffer too small)
   251                              <1> 		;	NC if success,
   252                              <1> 		;	 al = value read
   253                              <1> 		;	 ds:si incremented (NOT normalised)
   254                              <1> 		;	 ?length_of_source decremented
   255                              <1> .lodsb:
   256 000001B8 836EE601            <1> 	sub word [bp + ?length_of_source], 1
   257 000001BC 835EE800            <1> 	sbb word [bp + ?length_of_source + 2], 0
   258 000001C0 72F1                <1> 	jb .error
   259 000001C2 AC                  <1> 	lodsb
   260                              <1> .retn:
   261 000001C3 C3                  <1> 	retn
   262                              <1> 
   263                              <1> 
   264                              <1> 		; INP:	ds:si -> source data
   265                              <1> 		;	?length_of_source
   266                              <1> 		; OUT:	CY if error (source buffer too small)
   267                              <1> 		;	NC if success,
   268                              <1> 		;	 ax = value read
   269                              <1> 		;	 ds:si incremented (NOT normalised)
   270                              <1> 		;	 ?length_of_source decremented
   271                              <1> .lodsw:
   272 000001C4 836EE602            <1> 	sub word [bp + ?length_of_source], 2
   273 000001C8 835EE800            <1> 	sbb word [bp + ?length_of_source + 2], 0
   274 000001CC 72E5                <1> 	jb .error
   275 000001CE AD                  <1> 	lodsw
   276 000001CF C3                  <1> 	retn
   277                              <1> 
   278                              <1> 
   279                              <1> 		; Decompress LZSA2 block
   280                              <1> 		;
   281                              <1> 		; INP:	ds:si -> LZSA2 block
   282                              <1> 		;	es:di -> output buffer
   283                              <1> 		;	?length_of_source
   284                              <1> 		;	?length_of_source_after_block = block end target
   285                              <1> 		;	?original_dst -> lowest valid back reference
   286                              <1> 		; OUT:	ax = decompressed size (0 means 64 KiB)
   287                              <1> 		;	ds:si -> behind source data
   288                              <1> 		;	jumps to depack.error if error condition detected
   289                              <1> 		; CHG:	all except bp
   290                              <1> lzsa2_decompress:
   291                              <1> .lodsb equ depack.lodsb
   292                              <1> .lodsw equ depack.lodsw
   293                              <1> 
   294 000001D0 57                  <1>    push di                 ; remember decompression offset
   295                              <1> 
   296 000001D1 BB0001              <1>    mov bx,0100H
   297 000001D4 8366F600            <1>    and word [bp + ?matchlength], 0
   298                              <1> 
   299                              <1> .decode_token_clear_cx:
   300 000001D8 31C9                <1>    xor cx,cx
   301                              <1> .decode_token:
   302                              <1> %if _COUNTER
   303                              <1> 	inc byte [bp + ?counter]
   304                              <1> 	test byte [bp + ?counter], _COUNTER - 1
   305                              <1> 	jnz @F
   306                              <1> 	mov al, '.'
   307                              <1> 	call disp_al
   308                              <1> @@:
   309                              <1> %endif
   310 000001DA 8B46E6              <1> 	mov ax, word [bp + ?length_of_source]
   311 000001DD 8B56E8              <1> 	mov dx, word [bp + ?length_of_source + 2]
   312                              <1> 
   313 000001E0 3956FA              <1> 	cmp word [bp + ?length_of_source_after_block + 2], dx
   314 000001E3 7503                <1> 	jne @F
   315 000001E5 3946F8              <1> 	cmp word [bp + ?length_of_source_after_block], ax
   316                              <1> @@:
   317 000001E8 7503E91501          <1> 	je .end
   318 000001ED 77C4                <1> 	ja depack.error
   319                              <1> 
   320 000001EF 89C8                <1>    mov ax,cx               ; clear ah - cx is zero from above or from after rep movsb in .copy_match
   321 000001F1 E8C4FF              <1> 	call .lodsb		; read token byte: XYZ|LL|MMMM
   322 000001F4 89C2                <1>    mov dx,ax               ; keep token in dl
   323                              <1>    
   324 000001F6 2418                <1>    and al,018H             ; isolate literals length in token (LL)
   325 000001F8 B103                <1>    mov cl,3
   326 000001FA D2E8                <1>    shr al,cl               ; shift literals length into place
   327                              <1> 
   328 000001FC 3C03                <1>    cmp al,03H              ; LITERALS_RUN_LEN_V2?
   329 000001FE 7513                <1>    jne .got_literals       ; no, we have the full literals count from the token, go copy
   330                              <1> 
   331 00000200 E81101              <1>    call .get_nibble        ; get extra literals length nibble
   332 00000203 00C8                <1>    add al,cl               ; add len from token to nibble 
   333 00000205 3C12                <1>    cmp al,012H             ; LITERALS_RUN_LEN_V2 + 15 ?
   334 00000207 750A                <1>    jne .got_literals       ; if not, we have the full literals count, go copy
   335                              <1> 
   336 00000209 E8ACFF              <1> 	call .lodsb		; grab extra length byte
   337 0000020C 0412                <1>    add al,012H             ; overflow?
   338 0000020E 7303                <1>    jnc .got_literals       ; if not, we have the full literals count, go copy
   339                              <1> 
   340 00000210 E8B1FF              <1> 	call .lodsw		; grab 16-bit extra length
   341                              <1> 
   342                              <1> .got_literals:
   343 00000213 91                  <1>    xchg cx,ax
   344 00000214 294EE6              <1> 	sub word [bp + ?length_of_source], cx
   345 00000217 835EE800            <1> 	sbb word [bp + ?length_of_source + 2], 0
   346 0000021B 7296                <1> 	jb depack.error
   347 0000021D 294EE2              <1> 	sub word [bp + ?length_of_destination], cx
   348 00000220 835EE400            <1> 	sbb word [bp + ?length_of_destination + 2], 0
   349 00000224 728D                <1> 	jb depack.error
   350 00000226 F3A4                <1>    rep movsb               ; copy cx literals from ds:si to es:di
   351                              <1> 
   352 00000228 52                  <1> 	push dx
   353 00000229 50                  <1> 	push ax
   354 0000022A 8B46E6              <1> 	mov ax, word [bp + ?length_of_source]
   355 0000022D 8B56E8              <1> 	mov dx, word [bp + ?length_of_source + 2]
   356                              <1> 
   357 00000230 3956FA              <1> 	cmp word [bp + ?length_of_source_after_block + 2], dx
   358 00000233 7503                <1> 	jne @F
   359 00000235 3946F8              <1> 	cmp word [bp + ?length_of_source_after_block], ax
   360                              <1> @@:
   361 00000238 58                  <1> 	pop ax
   362 00000239 5A                  <1> 	pop dx
   363 0000023A 7503E9C300          <1> 	je .end
   364 0000023F 7603E96FFF          <1> 	ja depack.error
   365                              <1> 
   366 00000244 F6C2C0              <1>    test dl,0C0h            ; check match offset mode in token (X bit)
   367 00000247 7819                <1>    js .rep_match_or_large_offset
   368                              <1> 
   369                              <1>    ;;cmp dl,040H             ; check if this is a 5 or 9-bit offset (Y bit)
   370                              <1>                            ; discovered via the test with bit 6 set
   371 00000249 91                  <1>    xchg cx,ax              ; clear ah - cx is zero from the rep movsb above
   372 0000024A 7508                <1>    jne .offset_9_bit
   373                              <1> 
   374                              <1>                            ; 5 bit offset
   375 0000024C 80FA20              <1>    cmp dl,020H             ; test bit 5
   376 0000024F E8B500              <1>    call .get_nibble_x
   377 00000252 EB0A                <1>    jmp short .dec_offset_top
   378                              <1> 
   379                              <1> .offset_9_bit:             ; 9 bit offset
   380 00000254 E861FF              <1> 	call .lodsb		; get 8 bit offset from stream in A
   381 00000257 FECC                <1>    dec ah                  ; set offset bits 15-8 to 1
   382 00000259 F6C220              <1>    test dl,020H            ; test bit Z (offset bit 8)
   383 0000025C 741F                <1>    je .get_match_length
   384                              <1> .dec_offset_top:
   385 0000025E FECC                <1>    dec ah                  ; clear bit 8 if Z bit is clear
   386                              <1>                            ; or set offset bits 15-8 to 1
   387 00000260 EB1B                <1>    jmp short .get_match_length
   388                              <1> 
   389                              <1> .rep_match_or_large_offset:
   390                              <1>    ;;cmp dl,0c0H             ; check if this is a 13-bit offset or a 16-bit offset/rep match (Y bit)
   391 00000262 7A0C                <1>    jpe .rep_match_or_16_bit
   392                              <1> 
   393                              <1>                            ; 13 bit offset
   394                              <1> 
   395 00000264 80FAA0              <1>    cmp dl,0A0H             ; test bit 5 (knowing that bit 7 is also set)
   396 00000267 86E0                <1>    xchg ah,al
   397 00000269 E89B00              <1>    call .get_nibble_x
   398 0000026C 2C02                <1>    sub al,2                ; substract 512
   399 0000026E EB08                <1>    jmp short .get_match_length_1
   400                              <1> 
   401                              <1> .rep_match_or_16_bit:
   402 00000270 F6C220              <1>    test dl,020H            ; test bit Z (offset bit 8)
   403 00000273 750B                <1>    jne .repeat_match       ; rep-match
   404                              <1> 
   405                              <1>                            ; 16 bit offset
   406 00000275 E840FF              <1> 	call .lodsb		; Get 2-byte match offset
   407                              <1> 
   408                              <1> .get_match_length_1:
   409 00000278 86E0                <1>    xchg ah,al
   410 0000027A E83BFF              <1> 	call .lodsb		; load match offset bits 0-7
   411                              <1> 
   412                              <1> .get_match_length:
   413 0000027D 8746F6              <1> 	xchg word [bp + ?matchlength], ax
   414                              <1> 			; bp: offset
   415                              <1> .repeat_match:
   416 00000280 92                  <1>    xchg ax,dx              ; ax: original token
   417 00000281 2407                <1>    and al,07H              ; isolate match length in token (MMM)
   418 00000283 0402                <1>    add al,2                ; add MIN_MATCH_SIZE_V2
   419                              <1> 
   420 00000285 3C09                <1>    cmp al,09H              ; MIN_MATCH_SIZE_V2 + MATCH_RUN_LEN_V2?
   421 00000287 7518                <1>    jne .got_matchlen       ; no, we have the full match length from the token, go copy
   422                              <1> 
   423 00000289 E88800              <1>    call .get_nibble        ; get extra literals length nibble
   424 0000028C 00C8                <1>    add al,cl               ; add len from token to nibble 
   425 0000028E 3C18                <1>    cmp al,018H             ; MIN_MATCH_SIZE_V2 + MATCH_RUN_LEN_V2 + 15?
   426 00000290 750F                <1>    jne .got_matchlen       ; no, we have the full match length from the token, go copy
   427                              <1> 
   428 00000292 E823FF              <1> 	call .lodsb		; grab extra length byte
   429 00000295 0418                <1>    add al,018H             ; overflow?
   430 00000297 7308                <1>    jnc .got_matchlen       ; if not, we have the entire length
   431 00000299 7503E915FF          <1> 	je depack.error		; detect EOD code
   432                              <1> 
   433 0000029E E823FF              <1> 	call .lodsw		; grab 16-bit length
   434                              <1> 
   435                              <1> .got_matchlen:
   436 000002A1 91                  <1>    xchg cx,ax              ; copy match length into cx
   437 000002A2 1E                  <1>    push ds                 ; save ds:si (current pointer to compressed data)
   438 000002A3 96                  <1>    xchg si,ax          
   439 000002A4 06                  <1>    push es
   440 000002A5 1F                  <1>    pop ds
   441 000002A6 89FE                <1> 	mov si, di
   442 000002A8 0376F6              <1> 	add si, word [bp + ?matchlength]
   443                              <1> 			; ds:si now points at back reference in output data
   444 000002AB 7225                <1> 	jc @FF		; simple case branches here -->
   445 000002AD 53                  <1> 	push bx
   446 000002AE 51                  <1> 	push cx
   447 000002AF 8B4EF6              <1> 	mov cx, word [bp + ?matchlength]
   448 000002B2 BBFFFF              <1> 	mov bx, -1
   449 000002B5 06                  <1> 	 push es
   450 000002B6 57                  <1> 	 push di
   451 000002B7 E8B100              <1> 	call normalise_pointer_with_displacement_bxcx
   452 000002BA 5E                  <1> 	 pop si
   453 000002BB 5B                  <1> 	 pop bx
   454 000002BC 7203E9F2FE          <1> 	jnc depack.error
   455 000002C1 3B5EF0              <1> 	cmp bx, word [bp + ?original_dst + 2]
   456 000002C4 7503                <1> 	jne @F
   457 000002C6 3B76EE              <1> 	cmp si, word [bp + ?original_dst]
   458                              <1> @@:
   459 000002C9 7303E9E5FE          <1> 	jb depack.error
   460 000002CE 8EDB                <1> 	mov ds, bx
   461 000002D0 59                  <1> 	pop cx
   462 000002D1 5B                  <1> 	pop bx
   463                              <1> @@:
   464 000002D2 294EE2              <1> 	sub word [bp + ?length_of_destination], cx
   465 000002D5 835EE400            <1> 	sbb word [bp + ?length_of_destination + 2], 0
   466 000002D9 7303E9D5FE          <1> 	jb depack.error
   467 000002DE F3A4                <1>    rep movsb               ; copy match
   468 000002E0 96                  <1>    xchg si,ax              ; restore ds:si
   469 000002E1 1F                  <1>    pop ds
   470                              <1> %if _ALLOW_OVERLAPPING
   471 000002E2 53                  <1> 	push bx
   472 000002E3 85FF                <1> 	test di, di
   473 000002E5 740C                <1> 	jz .zero
   474 000002E7 E83C00              <1> 	call check_pointers_not_overlapping
   475                              <1> .waszero:
   476 000002EA 5B                  <1> 	pop bx
   477 000002EB 7303E9C3FE          <1> 	jc depack.error
   478 000002F0 E9E5FE              <1> 	jmp .decode_token_clear_cx
   479                              <1> 				; go decode another token
   480                              <1> 
   481                              <1> .zero:
   482 000002F3 06                  <1> 	push es
   483 000002F4 8CC3                <1> 	mov bx, es		; => our block
   484 000002F6 81C30010            <1> 	add bx, 1000h		; => next 64 KiB block
   485 000002FA 8EC3                <1> 	mov es, bx		; es:di -> after our block
   486 000002FC E82700              <1> 	call check_pointers_not_overlapping
   487 000002FF 07                  <1> 	pop es
   488 00000300 EBE8                <1> 	jmp .waszero
   489                              <1> %else
   490                              <1> 	jmp .decode_token
   491                              <1> 				; go decode another token
   492                              <1> %endif
   493                              <1> 
   494                              <1> .end:
   495 00000302 58                  <1>    pop ax                  ; retrieve the original decompression offset
   496 00000303 97                  <1>    xchg di,ax              ; compute decompressed size
   497 00000304 29F8                <1>    sub ax,di
   498 00000306 C3                  <1>    ret                     ; done
   499                              <1> 
   500                              <1> .get_nibble_x:
   501 00000307 F5                  <1>    cmc                     ; carry set if bit 4 was set
   502 00000308 D0D8                <1>    rcr al,1
   503 0000030A E80700              <1>    call .get_nibble        ; get nibble for offset bits 0-3
   504 0000030D 08C8                <1>    or al,cl                ; merge nibble
   505 0000030F D0C0                <1>    rol al,1
   506 00000311 34E1                <1>    xor al,0E1H             ; set offset bits 7-5 to 1
   507 00000313 C3                  <1>    ret
   508                              <1> 
   509                              <1> .get_nibble:
   510 00000314 F6DF                <1>    neg bh                  ; nibble ready?
   511 00000316 7905                <1>    jns .has_nibble
   512                              <1>    
   513 00000318 93                  <1>    xchg bx,ax
   514 00000319 E89CFE              <1> 	call .lodsb		; load two nibbles
   515 0000031C 93                  <1>    xchg bx,ax
   516                              <1> 
   517                              <1> .has_nibble:
   518 0000031D B104                <1>    mov cl,4                ; swap 4 high and low bits of nibble
   519 0000031F D2CB                <1>    ror bl,cl
   520 00000321 B10F                <1>    mov cl,0FH
   521 00000323 20D9                <1>    and cl,bl
   522 00000325 C3                  <1>    ret
  1047                                  %endif
  1048                                  
  1049                                  
  1050                                  %if _APL
  1051                                  	%include "apl.asm"
  1052                                  %endif
  1053                                  
  1054                                  
  1055                                  %if _BZP
  1056                                  	%include "bzp.asm"
  1057                                  %endif
  1058                                  
  1059                                  
  1060                                  %if _ALLOW_OVERLAPPING
  1061                                  		; INP:	?src, ?dst
  1062                                  		; OUT:	CY if error (?src < ?dst)
  1063                                  		;	NC if success
  1064                                  		; CHG:	ax, bx, cx, dx
  1065                                  check_pointers_not_overlapping:
  1066                                  %if CHECK_POINTERS_VARIABLE_DST
  1067                                  	 push word [bp + ?dst + 2]
  1068                                  	 push word [bp + ?dst]
  1069                                  %else
  1070 00000326 06                      	 push es
  1071 00000327 57                      	 push di
  1072                                  %endif
  1073 00000328 E86F00                  	call pointer_to_linear
  1074                                  
  1075 0000032B 91                      	xchg cx, ax
  1076 0000032C 87DA                    	xchg bx, dx			; bx:cx = linear ?dst after write
  1077                                  
  1078                                  %if CHECK_POINTERS_VARIABLE_SRC
  1079                                  	 push word [bp + ?src + 2]
  1080                                  	 push word [bp + ?src]
  1081                                  %else
  1082 0000032E 1E                      	 push ds
  1083 0000032F 56                      	 push si
  1084                                  %endif
  1085 00000330 E86700                  	call pointer_to_linear		; dx:ax = linear ?src before next read
  1086                                  
  1087 00000333 39DA                    	cmp dx, bx			; ?src >= ?dst ?
  1088 00000335 7502                    	jne @F
  1089 00000337 39C8                    	cmp ax, cx
  1090                                  @@:
  1091                                  					; (CY) if error (src < dst)
  1092                                  					; (NC) if no error
  1093 00000339 C3                      	retn
  1094                                  %endif
  1095                                  
  1096                                  	; This leaves the lframe context created within the
  1097                                  	;  specific depacker's file. The above function
  1098                                  	;  check_pointers_not_overlapping uses the frame.
  1099                                  	lleave ctx
  1100                                  
  1101                                  
  1102                                  		; INP:	ds:si = pointer
  1103                                  		;	es:di = pointer
  1104                                  		; OUT:	ds:si normalised
  1105                                  		;	es:di normalised
  1106                                  normalise_both_pointers:
  1107 0000033A 06                      	 push es
  1108 0000033B 57                      	 push di
  1109 0000033C E80A00                  	call normalise_pointer
  1110 0000033F 5F                      	 pop di
  1111 00000340 07                      	 pop es
  1112                                  
  1113                                  normalise_dssi_pointer:
  1114 00000341 1E                      	 push ds
  1115 00000342 56                      	 push si
  1116 00000343 E80300                  	call normalise_pointer
  1117 00000346 5E                      	 pop si
  1118 00000347 1F                      	 pop ds
  1119 00000348 C3                      	retn
  1120                                  
  1121                                  
  1122                                  		; INP:	word [ss:sp + 2] = segment
  1123                                  		;	word [ss:sp] = offset
  1124                                  		;
  1125                                  		; Note:	Does not work correctly with pointers that point to
  1126                                  		;	 a HMA location. Do not use then!
  1127                                  normalise_pointer:
  1128                                  %if NEED_NORMALISE_POINTER_WITH_DISPLACEMENT
  1129                                  	lframe near
  1130                                  	lpar word,	segment
  1131                                  	lpar word,	offset
  1132                                  	lpar_return
  1133 00000349 5589E5                  	lenter
  1134                                  
  1135 0000034C 837E040F                	cmp word [bp + ?offset], 15
  1136 00000350 7617                    	jbe .ret
  1137                                  
  1138 00000352 53                      	push bx
  1139 00000353 51                      	push cx
  1140                                  
  1141 00000354 31DB                    	xor bx, bx
  1142 00000356 31C9                    	xor cx, cx
  1143 00000358 FF7606                  	 push word [bp + ?segment]
  1144 0000035B FF7604                  	 push word [bp + ?offset]
  1145 0000035E E80A00                  	call normalise_pointer_with_displacement_bxcx
  1146 00000361 8F4604                  	 pop word [bp + ?offset]
  1147 00000364 8F4606                  	 pop word [bp + ?segment]
  1148                                  
  1149 00000367 59                      	pop cx
  1150 00000368 5B                      	pop bx
  1151                                  .ret:
  1152 00000369 5D                      	lleave
  1153 0000036A C3                      	lret
  1154                                  
  1155                                  
  1156                                  		; INP:	word [ss:sp + 2] = segment
  1157                                  		;	word [ss:sp] = offset
  1158                                  		;	bx:cx = add/sub displacement
  1159                                  		; OUT:	CY if the displacement carries
  1160                                  		;	NC if not
  1161                                  normalise_pointer_with_displacement_bxcx:
  1162                                  %endif
  1163                                  	lframe near
  1164                                  	lpar word,	segment
  1165                                  	lpar word,	offset
  1166                                  	lpar_return
  1167 0000036B 5589E5                  	lenter
  1168                                  
  1169                                  %ifn NEED_NORMALISE_POINTER_WITH_DISPLACEMENT
  1170                                  	cmp word [bp + ?offset], 15
  1171                                  	jbe .ret
  1172                                  %endif
  1173                                  
  1174 0000036E 50                      	push ax
  1175 0000036F 51                      	push cx
  1176 00000370 52                      	push dx
  1177                                  
  1178 00000371 FF7606                  	 push word [bp + ?segment]
  1179 00000374 FF7604                  	 push word [bp + ?offset]
  1180 00000377 E82000                  	call pointer_to_linear
  1181                                  
  1182                                  %if NEED_NORMALISE_POINTER_WITH_DISPLACEMENT
  1183                                  	; push bx
  1184                                  	; 				; sign-extend cx into bx:cx
  1185                                  	; cmp cx, 8000h			; CY if < 8000h (NC if negative)
  1186                                  	; cmc				; NC if positive
  1187                                  	; sbb bx, bx			; 0 if was NC, -1 if was CY
  1188                                  
  1189 0000037A 01C1                    	add cx, ax
  1190 0000037C 11DA                    	adc dx, bx			; dx:cx = dx:ax + bx:cx
  1191                                  	; pop bx
  1192 0000037E 9F                      	lahf				; ah = flags
  1193                                  %else
  1194                                  	xchg ax, cx
  1195                                  %endif
  1196                                  
  1197                                  %if 0
  1198                                  		; Adds in HMA support for this function. Not currently used.
  1199                                  	cmp dx, 10h			; dx:ax >= 10_0000h ?
  1200                                  	jb @F				; no, linear-to-pointer normally -->
  1201                                  	; ja .error
  1202                                  
  1203                                  	add cx, 10h
  1204                                  	; jc .error
  1205                                  	mov word [bp + ?offset], cx
  1206                                  	or word [bp + ?segment], -1
  1207                                  	jmp .return
  1208                                  @@:
  1209                                  %endif
  1210                                  
  1211 0000037F 53                      	push bx
  1212 00000380 89CB                    	mov bx, cx
  1213 00000382 83E10F                  	and cx, 15
  1214 00000385 894E04                  	mov word [bp + ?offset], cx
  1215                                  
  1216 00000388 B104                    	mov cl, 4
  1217                                  @@:
  1218 0000038A D1EA                    	shr dx, 1
  1219 0000038C D1DB                    	rcr bx, 1
  1220 0000038E E2FA                    	loop @B
  1221                                  
  1222 00000390 895E06                  	mov word [bp + ?segment], bx
  1223 00000393 5B                      	pop bx
  1224                                  
  1225                                  	; test dx, dx
  1226                                  	; jnz .error
  1227                                  
  1228                                  .return:
  1229                                  
  1230                                  %if NEED_NORMALISE_POINTER_WITH_DISPLACEMENT
  1231 00000394 9E                      	sahf				; restore flags from ah
  1232                                  %endif
  1233 00000395 5A                      	pop dx
  1234 00000396 59                      	pop cx
  1235 00000397 58                      	pop ax
  1236                                  %ifn NEED_NORMALISE_POINTER_WITH_DISPLACEMENT
  1237                                  .ret:
  1238                                  %endif
  1239 00000398 5D                      	lleave
  1240 00000399 C3                      	lret
  1241                                  
  1242                                  
  1243                                  		; INP:	word [ss:sp + 2] = segment
  1244                                  		;	word [ss:sp] = offset
  1245                                  		; OUT:	dx:ax = linear address
  1246                                  pointer_to_linear:
  1247                                  	lframe near
  1248                                  	lpar word,	segment
  1249                                  	lpar word,	offset
  1250 0000039A 5589E5                  	lenter
  1251                                  
  1252 0000039D 8B4606                  	mov ax, word [bp + ?segment]
  1253 000003A0 31D2                    	xor dx, dx
  1254 000003A2 51                      	push cx
  1255 000003A3 B90400                  	mov cx, 4
  1256                                  @@:
  1257 000003A6 D1E0                    	shl ax, 1
  1258 000003A8 D1D2                    	rcl dx, 1
  1259 000003AA E2FA                    	loop @B
  1260                                  
  1261 000003AC 034604                  	add ax, word [bp + ?offset]
  1262 000003AF 11CA                    	adc dx, cx			; cx = 0 here
  1263 000003B1 59                      	pop cx
  1264                                  
  1265 000003B2 5D                      	lleave
  1266 000003B3 C20400                  	lret
  1267                                  
  1268                                  
  1269 000003B6 90<rep Ah>              	align 16
  1270                                  init1_end:
  1271                                  
  1272                                  
  1273                                  	section INIT0
  1274                                  %if _TEST_PROGRAM
  1275                                  init0_msg:
  1276                                  .error_stderr:	db "Error: Not enough memory allocated.",13,10
  1277                                  .error_stderr.length: equ $ - .error_stderr
  1278                                  .error_stdout:	_autodigits paras(_TEST_PROGRAM_DECOMPRESSED_SIZE 				+ (payload_end - payload) 				+ (init1_end - init1_start) 				+ ADDITIONAL_MEMORY 				)
  1283                                  		db 13,10
  1284                                  .error_stdout.length: equ $ - .error_stdout
  1285                                  
  1286                                  	align 1024
  1287                                  %if ($ - $$) != 1024
  1288                                   %error Wrong INIT0 size
  1289                                  %endif
  1290                                  %endif
  1291                                  
  1292 000001CF 90                      	align 16
  1293                                  init0_end:
  1294                                  
  1295                                  
  1296                                  %assign num (init1_end - init1_start) + (init0_end - init0_start)
  1297                                  %if _BRIEFLZ
  1298                                  %define which iniblz
  1299                                  %elif _LZ4
  1300                                  %define which inilz4
  1301                                  %elif _SNAPPY
  1302                                  %define which inisz
  1303                                  %elif _EXODECR
  1304                                  %define which iniexo
  1305                                  %elif _X
  1306                                  %define which inix
  1307                                  %elif _HEATSHRINK
  1308                                  %define which inihs
  1309                                  %elif _LZD
  1310                                  %define which inilz
  1311                                  %elif _LZO
  1312                                  %define which inilzo
  1313                                  %elif _LZSA2
  1314                                  %define which inilzsa2
  1315                                  %elif _APL
  1316                                  %define which iniapl
  1317                                  %elif _BZP
  1318                                  %define which inibzp
  1319                                  %endif
  1320                                  %warning which: num bytes used for depacker
  1320          ******************       warning: inilzsa2: 1424 bytes used for depacker [-w+user]
  1321                                  
  1322                                  
  1323                                  %if _TEST_PROGRAM
  1324                                  	section INIT2 align=16 follows=INIT1 vstart=0
  1325                                  init2_start:
  1326                                  		; si => after PSP
  1327                                  		; cs => INIT2
  1328                                  		; psp, free, payload, init1, init2, payload space, stack
  1329                                  		;
  1330                                  		; The correct allocation for the test program
  1331                                  		;  is image size (init0, payload, init1, init2)
  1332                                  		;  minus init0 plus compressed payload size
  1333                                  		;  plus decompressed size plus stack. For
  1334                                  		;  simplicity, init0 subtraction may be skipped.
  1335                                  
  1336                                  		; ss:sp -> word amount repetitions, word ax
  1337                                  		; bx = 1 if A test, 8001h if B test
  1338                                  	push bx
  1339                                  
  1340                                  	lframe 0
  1341                                  	lpar word,	repetitions
  1342                                  	lpar word,	whichtest
  1343                                  	lenter
  1344                                  
  1345                                  	mov dx, word [bp + ?repetitions]
  1346                                  	cmp dx, -1
  1347                                  	jne @F
  1348                                  
  1349                                  .cmdline_error:
  1350                                  %if _DEBUG0
  1351                                  	mov bx, -1
  1352                                  %endif
  1353                                  	mov dx, init2_msg.cmdline_error
  1354                                  	mov cx, init2_msg.cmdline_error.length
  1355                                  	jmp init2_error
  1356                                  
  1357                                  
  1358                                  @@:
  1359                                  	testopt [bp + ?whichtest], 8000h
  1360                                  	jz .a
  1361                                  .b:
  1362                                  	test dx, dx
  1363                                  	jz .cmdline_error
  1364                                  	jmp @F
  1365                                  
  1366                                  .a:
  1367                                  	test dx, dx
  1368                                  	jnz .cmdline_error
  1369                                  @@:
  1370                                  
  1371                                  	mov dx, cs
  1372                                  	add dx, (init2_end - init2_start) >> 4
  1373                                  				; => payload saving area
  1374                                  	mov ax, cs
  1375                                  	sub ax, ( (init1_end - init1_start) 		+ (payload_end - payload) 		) >> 4		; => payload source for first run
  1378                                  	mov cx, (payload_end - payload) >> 4
  1379                                  	call init2_movp		; copy payload to payload saving area
  1380                                  		; We save away the payload here because a failure
  1381                                  		;  to decompress generally overwrites part of that
  1382                                  		;  payload which was used as source.
  1383                                  
  1384                                  	mov dx, ax		; dx => payload source to use
  1385                                  	mov ax, cs
  1386                                  	sub ax, (init1_end - init1_start) >> 4
  1387                                  				; ax => INIT1
  1388                                  	mov bx, 1		; say we're in EXE mode
  1389                                  	push dx
  1390                                  	push ax
  1391                                  	push si			; si => target
  1392                                  	 push cs
  1393                                  	 call .transfer		; call decompression
  1394                                  	pop si
  1395                                  	pop ax
  1396                                  	pop dx
  1397                                  		; Returns here after decompression.
  1398                                  		; CY if error.
  1399                                  	jnc @F
  1400                                  
  1401                                  	mov dx, init2_msg.initial_error
  1402                                  	mov cx, init2_msg.initial_error.length
  1403                                  	jmp init2_error
  1404                                  
  1405                                  @@:
  1406                                  	numdef INCLUDE_UNCOMPRESSED
  1407                                  	numdef WRITE_WRONG_FILE
  1408                                  %if _INCLUDE_UNCOMPRESSED
  1409                                  payload_uncompressed_size equ payload_uncompressed.end - payload_uncompressed
  1410                                  
  1411                                  	call checkdecompressed
  1412                                  	je @F
  1413                                  
  1414                                  %if _WRITE_WRONG_FILE
  1415                                  	call writefiles
  1416                                  %endif
  1417                                  
  1418                                  %if _DEBUG0
  1419                                  	mov bx, -1
  1420                                  %endif
  1421                                  	mov dx, init2_msg.initial_error_2
  1422                                  	mov cx, init2_msg.initial_error_2.length
  1423                                  	jmp init2_error
  1424                                  
  1425                                  @@:
  1426                                  %endif
  1427                                  
  1428                                  	lvar word,	upperbound
  1429                                  	 push dx
  1430                                  	lvar word,	lowerbound
  1431                                  	 push si
  1432                                  	lvar word,	current_init1
  1433                                  	 push ax
  1434                                  
  1435                                  %if _TEST_PROGRESS
  1436                                  	mov dx, init2_msg.progress.1
  1437                                  	mov cx, init2_msg.progress.1.length
  1438                                  .loop:
  1439                                  	push cs
  1440                                  	pop ds
  1441                                  	mov bx, 2
  1442                                  	mov ah, 40h
  1443                                  	int 21h
  1444                                  %else
  1445                                  .loop:
  1446                                  %endif
  1447                                  
  1448                                  	mov dx, [bp + ?upperbound]
  1449                                  
  1450                                  	testopt [bp + ?whichtest], 8000h
  1451                                  	jz @F
  1452                                  
  1453                                  	dec word [bp + ?repetitions]
  1454                                  	jnz .test_b_skip_bound
  1455                                  
  1456                                  %if _TEST_PROGRESS
  1457                                  	push cs
  1458                                  	pop ds
  1459                                  	mov dx, init2_msg.progress.linebreak
  1460                                  	mov cx, init2_msg.progress.linebreak.length
  1461                                  	mov bx, 2
  1462                                  	mov ah, 40h
  1463                                  	int 21h
  1464                                  %endif
  1465                                  	mov ax, 4C00h
  1466                                  	int 21h
  1467                                  
  1468                                  
  1469                                  @@:
  1470                                  	sub dx, [bp + ?lowerbound]
  1471                                  	jz .found
  1472                                  	shr dx, 1
  1473                                  		; Rounding down, so that we never retry upper bound.
  1474                                  		;  The upper bound is known to be working.
  1475                                  	add dx, [bp + ?lowerbound]
  1476                                  
  1477                                  .test_b_skip_bound:
  1478                                  	push dx
  1479                                  	add dx, (payload_end - payload) >> 4
  1480                                  	mov ax, [bp + ?current_init1]
  1481                                  	mov cx, (init1_end - init1_start) >> 4
  1482                                  	call init2_movp
  1483                                  	mov [bp + ?current_init1], dx
  1484                                  	pop dx
  1485                                  
  1486                                  	mov ax, cs
  1487                                  	add ax, (init2_end - init2_start) >> 4
  1488                                  				; => payload in saving area
  1489                                  	mov cx, (payload_end - payload) >> 4
  1490                                  	call init2_movp		; copy payload from payload saving area
  1491                                  
  1492                                  		; dx => source
  1493                                  	mov ax, [bp + ?current_init1]
  1494                                  	mov bx, 1
  1495                                  	push dx
  1496                                  	push si
  1497                                  	 push cs
  1498                                  	 call .transfer
  1499                                  	pop si
  1500                                  	pop dx
  1501                                  		; Returns here after decompression.
  1502                                  		; CY if error.
  1503                                  	jnc @F
  1504                                  
  1505                                  	testopt [bp + ?whichtest], 8000h
  1506                                  	jnz .test_b_fail
  1507                                  
  1508                                  		; error: this attempt is one below the new lower bound
  1509                                  	inc dx
  1510                                  	mov word [bp + ?lowerbound], dx
  1511                                  
  1512                                  %if _TEST_PROGRESS
  1513                                  	mov dx, init2_msg.progress.fail
  1514                                  	mov cx, init2_msg.progress.fail.length
  1515                                  %endif
  1516                                  	jmp .loop
  1517                                  
  1518                                  @@:
  1519                                  %if _INCLUDE_UNCOMPRESSED
  1520                                  	call checkdecompressed
  1521                                  	je @F
  1522                                  
  1523                                  %if _WRITE_WRONG_FILE
  1524                                  	call writefiles
  1525                                  %endif
  1526                                  
  1527                                  .test_b_fail:
  1528                                  %if _TEST_PROGRESS
  1529                                  	push cs
  1530                                  	pop ds
  1531                                  	mov dx, init2_msg.progress.linebreak
  1532                                  	mov cx, init2_msg.progress.linebreak.length
  1533                                  	mov bx, 2
  1534                                  	mov ah, 40h
  1535                                  	int 21h
  1536                                  %endif
  1537                                  %if _DEBUG0
  1538                                  	mov bx, -2
  1539                                  %endif
  1540                                  	mov dx, init2_msg.subsequent_error_2
  1541                                  	mov cx, init2_msg.subsequent_error_2.length
  1542                                  	jmp init2_error
  1543                                  
  1544                                  @@:
  1545                                  %endif
  1546                                  		; success: this attempt is the new upper bound
  1547                                  	mov word [bp + ?upperbound], dx
  1548                                  %if _TEST_PROGRESS
  1549                                  	mov dx, init2_msg.progress.success
  1550                                  	mov cx, init2_msg.progress.success.length
  1551                                  %endif
  1552                                  	jmp .loop
  1553                                  
  1554                                  .found:
  1555                                  %if _TEST_PROGRESS
  1556                                  	push cs
  1557                                  	pop ds
  1558                                  	mov dx, init2_msg.progress.linebreak
  1559                                  	mov cx, init2_msg.progress.linebreak.length
  1560                                  	mov bx, 2
  1561                                  	mov ah, 40h
  1562                                  	int 21h
  1563                                  %endif
  1564                                  	mov ax, word [bp + ?upperbound]
  1565                                  	sub ax, si		; = how many paragraphs in buffer before source
  1566                                  	add ax, paras( (init1_end - init1_start) 			+ (payload_end - payload) )
  1568                                  				; = how many paragraphs needed for process
  1569                                  	call init2_disp_ax_dec
  1570                                  	mov al, 13
  1571                                  	call init2_disp_al
  1572                                  	mov al, 10
  1573                                  	call init2_disp_al
  1574                                  	mov ax, 4C00h
  1575                                  	int 21h
  1576                                  
  1577                                  
  1578                                  .transfer:
  1579                                  	xor di, di
  1580                                  	push di			; dummy ax value on stack
  1581                                  	push ax			; INIT1 segment
  1582                                  	push di			; zero = init1_start
  1583                                  	retf
  1584                                  
  1585                                  
  1586                                  
  1587                                  %if _INCLUDE_UNCOMPRESSED
  1588                                  
  1589                                  
  1590                                  %if _WRITE_WRONG_FILE
  1591                                  writefiles:
  1592                                  	push cs
  1593                                  	pop ds
  1594                                  	mov dx, init2_msg.wrong_file_name
  1595                                  	xor cx, cx
  1596                                  	mov ah, 3Ch
  1597                                  	int 21h
  1598                                  	jc .notfile
  1599                                  
  1600                                  	mov bx, ax
  1601                                  	mov cx, payload_uncompressed_size >> 4
  1602                                  	xor dx, dx
  1603                                  .loopfile:
  1604                                  	mov ds, si
  1605                                  	mov ah, 40h
  1606                                  	push cx
  1607                                  	mov cx, 16
  1608                                  	int 21h
  1609                                  	pop cx
  1610                                  	inc si
  1611                                  	loop .loopfile
  1612                                  
  1613                                  	mov ds, si
  1614                                  	mov cx, payload_uncompressed_size & 15
  1615                                  	mov ah, 40h
  1616                                  	int 21h
  1617                                  
  1618                                  	mov ah, 3Eh
  1619                                  	int 21h
  1620                                  
  1621                                  .notfile:
  1622                                  
  1623                                  	push cs
  1624                                  	pop ds
  1625                                  	mov dx, init2_msg.wrong_file_name2
  1626                                  	xor cx, cx
  1627                                  	mov ah, 3Ch
  1628                                  	int 21h
  1629                                  	jc .notfile2
  1630                                  
  1631                                  	mov bx, ax
  1632                                  	mov cx, payload_uncompressed_size >> 4
  1633                                  	mov si, cs
  1634                                  	add si, (payload_uncompressed - init2_start) >> 4
  1635                                  	xor dx, dx
  1636                                  .loopfile2:
  1637                                  	mov ds, si
  1638                                  	mov ah, 40h
  1639                                  	push cx
  1640                                  	mov cx, 16
  1641                                  	int 21h
  1642                                  	pop cx
  1643                                  	inc si
  1644                                  	loop .loopfile2
  1645                                  
  1646                                  	mov ds, si
  1647                                  	mov cx, payload_uncompressed_size & 15
  1648                                  	mov ah, 40h
  1649                                  	int 21h
  1650                                  
  1651                                  	mov ah, 3Eh
  1652                                  	int 21h
  1653                                  
  1654                                  .notfile2:
  1655                                  	retn
  1656                                  %endif
  1657                                  
  1658                                  		; INP:	si => decompressed image
  1659                                  		; OUT:	ZR if matching
  1660                                  		;	NZ if mismatching
  1661                                  		; CHG:	es, ds, di, bx, cx
  1662                                  		; STT:	UP
  1663                                  checkdecompressed:
  1664                                  	push ax
  1665                                  	push dx
  1666                                  	push si
  1667                                  %if _PAYLOAD_KERNEL_MAX_PARAS
  1668                                  	mov cx, _PAYLOAD_KERNEL_MAX_PARAS
  1669                                  	mov bx, 0
  1670                                  %else
  1671                                  	mov cx, payload_uncompressed_size >> 4
  1672                                  	mov bx, payload_uncompressed_size & 15
  1673                                  %endif
  1674                                  	mov di, cs
  1675                                  	add di, (payload_uncompressed - init2_start) >> 4
  1676                                  	jcxz .end
  1677                                  .loop:
  1678                                  	push cx
  1679                                  	mov cx, 8
  1680                                  	mov ds, si
  1681                                  	mov es, di
  1682                                  	inc si
  1683                                  	inc di
  1684                                  	push si
  1685                                  	push di
  1686                                  	xor si, si
  1687                                  	xor di, di
  1688                                  	repe cmpsw
  1689                                  	pop di
  1690                                  	pop si
  1691                                  	pop cx
  1692                                  	jne .ret
  1693                                  	loop .loop
  1694                                  .end:
  1695                                  	mov ds, si
  1696                                  	mov es, di
  1697                                  	xor si, si
  1698                                  	xor di, di		; (ZR)
  1699                                  	mov cx, bx
  1700                                  	repe cmpsb
  1701                                  .ret:
  1702                                  	pop si
  1703                                  	pop dx
  1704                                  	pop ax
  1705                                  	retn
  1706                                  %endif
  1707                                  
  1708                                  
  1709                                  init2_error:
  1710                                  %if _DEBUG0
  1711                                  	push bx
  1712                                  %endif
  1713                                  	push cs
  1714                                  	pop ds
  1715                                  	mov bx, 2
  1716                                  	mov ah, 40h
  1717                                  	int 21h
  1718                                  
  1719                                  %if _DEBUG0
  1720                                  	mov dx, init2_msg.rc
  1721                                  	mov cx, init2_msg.rc.length
  1722                                  	mov bx, 2
  1723                                  	mov ah, 40h
  1724                                  	int 21h
  1725                                  	pop ax
  1726                                  	call init2_error_disp_ax_hex
  1727                                  	mov dx, init2_msg.linebreak
  1728                                  	mov cx, init2_msg.linebreak.length
  1729                                  	mov bx, 2
  1730                                  	mov ah, 40h
  1731                                  	int 21h
  1732                                  %endif
  1733                                  
  1734                                  	testopt [bp + ?whichtest], 8000h
  1735                                  	jnz @F
  1736                                  
  1737                                  	mov dx, init2_msg.error_stdout
  1738                                  	mov cx, init2_msg.error_stdout.length
  1739                                  	mov bx, 1
  1740                                  	mov ah, 40h
  1741                                  	int 21h
  1742                                  @@:
  1743                                  	mov ax, 4CFFh
  1744                                  	int 21h
  1745                                  
  1746                                  	lleave ctx
  1747                                  
  1748                                  
  1749                                  %if _DEBUG0
  1750                                  init2_error_disp_ax_hex:	; ax
  1751                                  		xchg al,ah
  1752                                  		call init2_error_disp_al_hex	; display former ah
  1753                                  		xchg al,ah			;  and fall trough for al
  1754                                  init2_error_disp_al_hex:	; al
  1755                                  		push cx
  1756                                  		mov cl,4
  1757                                  		ror al,cl
  1758                                  		call init2_error_disp_al_lownibble_hex
  1759                                  						; display former high-nibble
  1760                                  		rol al,cl
  1761                                  		pop cx
  1762                                  						;  and fall trough for low-nibble
  1763                                  init2_error_disp_al_lownibble_hex:
  1764                                  		push ax			 ; save ax for call return
  1765                                  		and al,00001111b		; high nibble must be zero
  1766                                  		add al,'0'			; if number is 0-9, now it's the correct character
  1767                                  		cmp al,'9'
  1768                                  		jna .decimalnum		 ; if we get decimal number with this, ok -->
  1769                                  		add al,7			;  otherwise, add 7 and we are inside our alphabet
  1770                                   .decimalnum:
  1771                                  		call init2_error_disp_al
  1772                                  		pop ax
  1773                                  		retn
  1774                                  
  1775                                  
  1776                                  init2_error_disp_al:
  1777                                  	push dx
  1778                                  	push cx
  1779                                  	push bx
  1780                                  	push ax
  1781                                  	mov dx, sp
  1782                                  	push ds
  1783                                  	 push ss
  1784                                  	 pop ds
  1785                                  	mov cx, 1
  1786                                  	mov bx, 2
  1787                                  	mov ah, 40h
  1788                                  	int 21h
  1789                                  	pop ds
  1790                                  	pop ax
  1791                                  	pop bx
  1792                                  	pop cx
  1793                                  	pop dx
  1794                                  	retn
  1795                                  %endif
  1796                                  
  1797                                  
  1798                                  init2_disp_al:
  1799                                  	push dx
  1800                                  	push ax
  1801                                  	mov dl, al
  1802                                  	mov ah, 2
  1803                                  	int 21h
  1804                                  	pop ax
  1805                                  	pop dx
  1806                                  	retn
  1807                                  
  1808                                  
  1809                                  		; Display number in ax decimal
  1810                                  		;
  1811                                  		; INP:	ax = number
  1812                                  		; OUT:	displayed using Int21.02
  1813                                  		; CHG:	none
  1814                                  init2_disp_ax_dec:			; ax (no leading zeros)
  1815                                  		push bx
  1816                                  		xor bx, bx
  1817                                  .pushax:
  1818                                  		push dx
  1819                                  		push ax
  1820                                  		or bl, bl
  1821                                  		jz .nobl
  1822                                  		sub bl, 5
  1823                                  		neg bl
  1824                                  .nobl:
  1825                                  		push cx
  1826                                  		mov cx, 10000
  1827                                  		call .divide_out
  1828                                  		mov cx, 1000
  1829                                  		call .divide_out
  1830                                  		mov cx, 100
  1831                                  		call .divide_out
  1832                                  		mov cl, 10
  1833                                  		call .divide_out
  1834                                  							; (Divisor 1 is useless)
  1835                                  		add al, '0'
  1836                                  		call init2_disp_al
  1837                                  		pop cx
  1838                                  		pop ax
  1839                                  		pop dx
  1840                                  		pop bx					; Caller's register
  1841                                  		retn
  1842                                  
  1843                                  
  1844                                  		; INP:	ax = number
  1845                                  		;	cx = divisor
  1846                                  		; OUT:	ax = remainder of operation
  1847                                  		;	result displayed
  1848                                  .divide_out:
  1849                                  		push dx
  1850                                  		xor dx, dx
  1851                                  		div cx				; 0:ax / cx
  1852                                  		push dx				; remainder
  1853                                  		dec bl
  1854                                  		jnz .nobl2
  1855                                  		or bh, 1
  1856                                  .nobl2:
  1857                                  		or bh, al
  1858                                  		jz .leadingzero
  1859                                  		add al, '0'
  1860                                  		call init2_disp_al		; display result
  1861                                   .leadingzero:
  1862                                  		pop ax				; remainder
  1863                                  		pop dx
  1864                                  		retn
  1865                                  
  1866                                  
  1867                                  init2_msg:
  1868                                  %if _TEST_PROGRESS
  1869                                  .progress.1:		db "Info: 1"
  1870                                  .progress.1.length: equ $ - .progress.1
  1871                                  .progress.fail:		db "F"
  1872                                  .progress.fail.length: equ $ - .progress.fail
  1873                                  .progress.success:	db "S"
  1874                                  .progress.success.length: equ $ - .progress.success
  1875                                  .progress.linebreak:	db 13,10
  1876                                  .progress.linebreak.length: equ $ - .progress.linebreak
  1877                                  %endif
  1878                                  %if _WRITE_WRONG_FILE
  1879                                  .wrong_file_name:	asciz "WRONG.BIN"
  1880                                  .wrong_file_name2:	asciz "WRONG2.BIN"
  1881                                  %endif
  1882                                  .initial_error:	db "Error: Test program failed to decompress with full buffer."
  1883                                  .linebreak:	db 13,10
  1884                                  .initial_error.length: equ $ - .initial_error
  1885                                  .linebreak.length: equ $ - .linebreak
  1886                                  %if _DEBUG0
  1887                                  .rc:	db "Error: Failure code="
  1888                                  .rc.length: equ $ - .rc
  1889                                  %endif
  1890                                  %if _INCLUDE_UNCOMPRESSED
  1891                                  .initial_error_2:	db "Error: Test program decompressed wrongly with full buffer.",13,10
  1892                                  .initial_error_2.length: equ $ - .initial_error_2
  1893                                  .subsequent_error_2:	db "Error: Test program decompressed wrongly during test.",13,10
  1894                                  .subsequent_error_2.length: equ $ - .subsequent_error_2
  1895                                  %endif
  1896                                  .cmdline_error:	db "Error: Invalid command line input.",13,10
  1897                                  .cmdline_error.length: equ $ - .cmdline_error
  1898                                  .error_stdout:	_autodigits paras(_TEST_PROGRAM_DECOMPRESSED_SIZE 				+ (payload_end - payload) 				+ (init1_end - init1_start) 				+ ADDITIONAL_MEMORY 				)
  1903                                  		db 13,10
  1904                                  .error_stdout.length: equ $ - .error_stdout
  1905                                  
  1906                                  
  1907                                  		; Move paragraphs
  1908                                  		;
  1909                                  		; INP:	ax => source
  1910                                  		;	dx => destination
  1911                                  		;	cx = number of paragraphs
  1912                                  		; CHG:	-
  1913                                  		; OUT:	ax and dx unchanged
  1914                                  		; Note:	Doesn't work correctly on HMA; doesn't always wrap to LMA either.
  1915                                  		;	Do not provide a wrapped/HMA source or destination!
  1916                                  init2_movp:
  1917                                  	push cx
  1918                                  	push ds
  1919                                  	push si
  1920                                  	push es
  1921                                  	push di
  1922                                  
  1923                                  	cmp ax, dx		; source above destination ?
  1924                                  	ja .up			; yes, move up (forwards) -->
  1925                                  	je .return		; same, no need to move -->
  1926                                  	push ax
  1927                                  	add ax, cx		; (expected not to carry)
  1928                                  	cmp ax, dx		; end of source is above destination ?
  1929                                  	pop ax
  1930                                  	ja .down		; yes, move from top down -->
  1931                                  	; Here, the end of source is below-or-equal the destination,
  1932                                  	;  so they do not overlap. In this case we prefer moving up.
  1933                                  
  1934                                  .up:
  1935                                  	push ax
  1936                                  	push dx
  1937                                  .uploop:
  1938                                  	mov ds, ax
  1939                                  	mov es, dx
  1940                                  	xor di, di
  1941                                  	xor si, si		; -> start of segment
  1942                                  	sub cx, 1000h		; 64 KiB left ?
  1943                                  	jbe .uplast		; no -->
  1944                                  	push cx
  1945                                  	mov cx, 10000h /2
  1946                                  	rep movsw		; move 64 KiB
  1947                                  	pop cx
  1948                                  	add ax, 1000h
  1949                                  	add dx, 1000h		; -> next segment
  1950                                  	jmp short .uploop	; proceed for more -->
  1951                                  .uplast:
  1952                                  	add cx, 1000h		; restore counter
  1953                                  	shl cx, 1
  1954                                  	shl cx, 1
  1955                                  	shl cx, 1		; *8, paragraphs to words
  1956                                  	rep movsw		; move last part
  1957                                  	pop dx
  1958                                  	pop ax
  1959                                  	jmp short .return
  1960                                  
  1961                                  .down:
  1962                                  	std			; _AMD_ERRATUM_109_WORKAROUND as below
  1963                                  .dnloop:
  1964                                  	sub cx, 1000h		; 64 KiB left ?
  1965                                  	jbe .dnlast		; no -->
  1966                                  	push ax
  1967                                  	push dx
  1968                                  	add ax, cx
  1969                                  	add dx, cx
  1970                                  	mov ds, ax		; -> 64 KiB not yet moved
  1971                                  	mov es, dx
  1972                                  	pop dx
  1973                                  	pop ax
  1974                                  	mov di, -2
  1975                                  	mov si, di		; moved from last word down
  1976                                  	push cx
  1977                                  	mov cx, 10000h /2
  1978                                  	rep movsw		; move 64 KiB
  1979                                  	pop cx
  1980                                  	jmp short .dnloop	; proceed for more -->
  1981                                  .dnlast:
  1982                                  	add cx, 1000h		; restore counter
  1983                                  	shl cx, 1
  1984                                  	shl cx, 1
  1985                                  	shl cx, 1		; *8, paragraphs to words
  1986                                  	mov di, cx
  1987                                  	dec di
  1988                                  	shl di, 1		; words to offset, -> last word
  1989                                  	mov si, di
  1990                                  	mov ds, ax
  1991                                  	mov es, dx		; first segment correct
  1992                                  
  1993                                  		; Refer to comment in init0_movp.
  1994                                  %if _AMD_ERRATUM_109_WORKAROUND
  1995                                  	jcxz @FF
  1996                                  	cmp cx, 20
  1997                                  	ja @FF
  1998                                  @@:
  1999                                  	movsw
  2000                                  	loop @B
  2001                                  @@:
  2002                                  %endif
  2003                                  	rep movsw		; move first part
  2004                                  	cld
  2005                                  .return:
  2006                                  	pop di
  2007                                  	pop es
  2008                                  	pop si
  2009                                  	pop ds
  2010                                  	pop cx
  2011                                  	retn
  2012                                  
  2013                                  
  2014                                  	align 16
  2015                                  
  2016                                  	strdef UNCOMPRESSED_FILE, "lDOSLOAD.BIN"
  2017                                  %if _INCLUDE_UNCOMPRESSED
  2018                                  payload_uncompressed:
  2019                                  	incbin _UNCOMPRESSED_FILE
  2020                                  .end:
  2021                                  	db 38
  2022                                  	align 16, db 38
  2023                                  %endif
  2024                                  init2_end:
  2025                                  %endif
  2026                                  
