     1                                  
     2                                  %if 0
     3                                  
     4                                  lDOS iniload packed payload
     5                                   by E. 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 OPTIMISE_INPUT_SMALL,	0
    57                                  	numdef OPTIMISE_OUYPUT_SMALL,	0
    58                                  	numdef ALLOW_OVERLAPPING,	1	; allow overlapping src and dst
    59                                  	numdef TEST_PROGRAM,		0
    60                                  	numdef TEST_PROGRAM_DECOMPRESSED_SIZE,		0,0
    61                                  %if _TEST_PROGRAM && ! _TEST_PROGRAM_DECOMPRESSED_SIZE
    62                                   %error Test program has to learn of decompressed size.
    63                                  %endif
    64                                  	numdef TEST_PROGRESS,	0
    65                                  
    66                                  	numdef EXEC_OFFSET,	0
    67                                  	numdef EXEC_SEGMENT,	0
    68                                  	numdef IMAGE_EXE,	0
    69                                  %if ! _IMAGE_EXE && _TEST_PROGRAM
    70                                   %error Test program can only be used as EXE.
    71                                  %endif
    72                                  	numdef IMAGE_EXE_CS,	-16	; relative-segment for CS
    73                                  	numdef IMAGE_EXE_IP,	256 +64	; value for IP
    74                                  		; The next two are only used if _IMAGE_EXE_AUTO_STACK is 0.
    75                                  	numdef IMAGE_EXE_SS,	-16	; relative-segment for SS
    76                                  	numdef IMAGE_EXE_SP,	0FFFEh	; value for SP (0 underflows)
    77                                  	numdef IMAGE_EXE_AUTO_STACK,	0, 2048	; use stack behind image
    78                                  		; _IMAGE_EXE_AUTO_STACK here differs from iniload's def of
    79                                  		;  the same name. This one is only used as a flag; if non-zero,
    80                                  		;  keep the stack given to us by iniload; if zero, set up the
    81                                  		;  stack specified by _IMAGE_EXE_SS and _IMAGE_EXE_SP.
    82                                  	numdef IMAGE_EXE_GET_COM_STACK, 0
    83                                  					; set up stack for flat .COM style entry
    84                                  	numdef DEVICE,			0
    85                                  	gendef DEVICE_NAME,		""
    86                                  	numdef DEVICE_ATTRIBUTE,	8000h
    87                                  	numdef DEVICE_ZERO_ENTRYPOINT,	0
    88                                  	numdef BOOTLDR,		1	; support boot loaded entrypoint
    89                                  
    90                                  	numdef BRIEFLZ,		0
    91                                  	numdef LZ4,		0
    92                                  	numdef SNAPPY,		0
    93                                  	numdef EXODECR,		0
    94                                  	numdef X,		0
    95                                  	numdef HEATSHRINK,	0
    96                                  	numdef LZD,		0
    97                                  	numdef LZO,		0
    98                                  	numdef LZSA2,		0
    99                                  	numdef APL,		0
   100                                  	numdef BZP,		0
   101                                  	numdef ZEROCOMP,	0
   102                                  	numdef MVCOMP,		0
   103                                  	numdef LZEXEDAT,	0
   104                                  %if (!!_BRIEFLZ + !!_LZ4 + !!_SNAPPY + !!_EXODECR + !!_X + !!_HEATSHRINK 	+ !!_LZD + !!_LZO + !!_LZSA2 + !!_APL + !!_BZP + !!_ZEROCOMP 	+ !!_MVCOMP + !!_LZEXEDAT) != 1
   107                                   %fatal Exactly one compression method must be selected.
   108                                  %endif
   109                                  %assign ADDITIONAL_MEMORY 0
   110                                  %if _BRIEFLZ
   111                                  	strdef PAYLOAD_FILE,	"lDOSLOAD.BLZ"
   112                                  %elif _LZ4
   113                                  	strdef PAYLOAD_FILE,	"lDOSLOAD.LZ4"
   114                                  %elif _SNAPPY
   115                                  	strdef PAYLOAD_FILE,	"lDOSLOAD.SZ"
   116                                  %elif _EXODECR
   117                                  	strdef PAYLOAD_FILE,	"lDOSLOAD.EXO"
   118                                  %elif _X
   119                                  	strdef PAYLOAD_FILE,	"lDOSLOAD.X"
   120                                  %elif _HEATSHRINK
   121                                  	strdef PAYLOAD_FILE,	"lDOSLOAD.HS"
   122                                  %elif _LZD
   123                                  	strdef PAYLOAD_FILE,	"lDOSLOAD.LZ"
   124                                  %elif _LZO
   125                                  	strdef PAYLOAD_FILE,	"lDOSLOAD.LZO"
   126                                  %elif _LZSA2
   127                                  	strdef PAYLOAD_FILE,	"lDOSLOAD.SA2"
   128                                  %elif _APL
   129                                  	strdef PAYLOAD_FILE,	"lDOSLOAD.APL"
   130                                  %elif _BZP
   131                                  	strdef PAYLOAD_FILE,	"lDOSLOAD.BZP"
   132                                  %elif _ZEROCOMP
   133                                  	strdef PAYLOAD_FILE,	"lDOSLOAD.ZER"
   134                                  %elif _MVCOMP
   135                                  	strdef PAYLOAD_FILE,	"lDOSLOAD.MV"
   136                                  %elif _LZEXEDAT
   137                                  	strdef PAYLOAD_FILE,	"lDOSLOAD.LZE"
   138                                  %endif
   139                                  	numdef PAYLOAD_KERNEL_MAX_PARAS,	0, 0
   140                                  %if _PAYLOAD_KERNEL_MAX_PARAS && ! (_IMAGE_EXE || _DEVICE)
   141                                   %error Kernel mode max paras requires building dual-mode or triple-mode executable
   142                                  %endif
   143                                  	numdef COUNTER,		0, 32
   144                                  %if (_COUNTER - 1) & _COUNTER
   145                                   %error COUNTER must be a power of two
   146                                  %endif
   147                                  	numdef PROGRESS,	0, 1
   148                                  	numdef PROGRESSAMOUNT,	0, 0
   149                                  	numdef PROGRESSMULTI,	1
   150                                  	numdef PROGRESSVAR,	1
   151                                  	numdef PROGRESSVARNAME, ""
   152                                  	numdef PROGRESSDEFAULT,	0
   153                                  	numdef PROGRESSDEFAULTAPP,	_PROGRESSDEFAULT
   154                                  	numdef PROGRESSDEFAULTDEVICE,	_PROGRESSDEFAULT
   155                                  	numdef PROGRESSDEFAULTBOOT,1
   156                                  	numdef PASSLCFG, 0
   157                                  	numdef LCFG, (_PROGRESS && _PROGRESSMULTI) || _PASSLCFG
   158                                  
   159                                  
   160                                  %define MODULE inicomp
   161                                  	strdef INILOAD_CFG, ""
   162                                  %ifnidn _INILOAD_CFG, ""
   163                                   %include _INILOAD_CFG
     1                              <1> ; File auto-generated, do not edit.
     2                              <1> strdef INILOAD_SIGNATURE, "bC"
     3                              <1> %assign _DEVICE 1
     4                              <1> %assign _DEVICE_ZERO_ENTRYPOINT 1
     5                              <1> %assign _DEVICE_ATTRIBUTE 8800h
     6                              <1> %define _DEVICE_NAME "LDEBUG$$"
     7                              <1> %define _PROGRESSVARNAME "LDEBUGPROGRESS"
   164                                  %endif
   165                                  	strdef LCFG_CFG, ""
   166                                  
   167                                  %if _DEVICE
   168                                   %ifidn _DEVICE_NAME, ""
   169                                    %error Device name must be set
   170                                   %endif
   171                                  %endif
   172                                  
   173                                  
   174                                  	cpu 8086
   175                                  	addsection INIT0, start=0 vstart=0
   176                                  init0_start:
   177                                  %if _DEVICE
   178                                  		; The device header is of a fixed format.
   179                                  		;  For our purposes, the 4-byte code for
   180                                  		;  each the strategy entry and the
   181                                  		;  interrupt entry is part of this format.
   182                                  		; (DOS may read the attributes or entrypoint
   183                                  		;  offsets before calling either, so in the inicomp
   184                                  		;  stage we need to recreate in the entrypoints part
   185                                  		;  exactly what the application has here.)
   186                                  		; _PASSLCFG: The lCFG block is passed in the dword
   187                                  		;  behind the interrupt entry.
   188                                  device_header:
   189                                  .next:
   190                                   %if _DEVICE_ZERO_ENTRYPOINT
   191 00000000 EB3E                    	fill 2, -1, jmp strict short j_zero_entrypoint
   192 00000002 FFFF                    	dw -1
   193                                   %else
   194                                  	dd -1				; link to next device
   195                                   %endif
   196                                  .attributes:
   197 00000004 0088                    	dw _DEVICE_ATTRIBUTE		; attributes
   198                                  .strategy:
   199 00000006 [1200]                  	dw .strategy_entry		; -> strategy entry
   200                                  .interrupt:
   201 00000008 [1600]                  	dw .interrupt_entry		; -> interrupt entry
   202                                  .name:
   203 0000000A 4C44454255472424        	fill 8, 32, db _DEVICE_NAME	; character device name,
   204                                  					;  or block device number of units
   205                                  					;  + optional name
   206                                  .strategy_entry:
   207 00000012 E91B0190                	fill 4, 90h, jmp device_entrypoint
   208                                  .interrupt_entry:
   209 00000016 CB90<rep 3h>            	fill 4, 90h, retf
   210                                  .pass_lcfg:
   211                                  %endif
   212                                  
   213                                  
   214                                  %if _IMAGE_EXE || _DEVICE
   215 0000001A 90                      	nop
   216 0000001B 90<rep 5h>              	align 32, nop
   217                                  init0_kernel_entry:
   218                                  		; cs:ip = load seg : 32 here
   219                                  %if ($ - $$) != 32
   220                                   %error Wrong kernel mode entrypoint
   221                                  %endif
   222                                  %if _TEST_PROGRAM
   223                                  @@:
   224 00000020 CC                      	int3
   225 00000021 FB                      	sti
   226 00000022 F4                      	hlt
   227 00000023 EBFB                    	jmp @B
   228                                  %endif
   229                                  %if _BOOTLDR
   230                                  %if ! _IMAGE_EXE
   231                                  j_zero_entrypoint:
   232                                  %endif
   233 00000025 31DB                    	xor bx, bx		; 0 = kernel mode
   234 00000027 50                      	push ax
   235 00000028 8CC8                    	mov ax, cs
   236 0000002A 83C040                  	add ax, (init0_end - init0_start) >> 4
   237 0000002D 8B56E2                  	mov dx, word [ bp + ldLoadTop ]	; => after end of available space
   238 00000030 BE6000                  	mov si, 60h		; => destination
   239 00000033 EB7E                    	jmp init0_common
   240                                  %endif
   241                                  
   242                                   %if _IMAGE_EXE
   243 00000035 90<rep Bh>              	align 64, nop
   244                                  j_zero_entrypoint:
   245                                  init0_exe_entry:
   246                                  		; NOTE:	This part is called with ip = 256 + 64, cs = PSP.
   247                                  %if ($ - $$) != 64
   248                                   %error Wrong EXE mode entrypoint
   249                                  %endif
   250                                  %if _IMAGE_EXE_GET_COM_STACK
   251                                  	mov bx, ds		; => PSP
   252                                  	dec bx			; => MCB
   253                                  	mov ds, bx
   254                                  	add bx, word [3]	; add size of memory block
   255                                  	sub bx, -1 + paras(512)	; add one, then subtract enough paragraphs
   256                                  	cli
   257                                  	mov ss, bx
   258                                  	mov sp, 512		; -> new stack
   259                                  	sti
   260                                  %endif
   261 00000040 BB0100                  	mov bx, 1		; EXE mode
   262 00000043 50                      	push ax
   263                                    %if _TEST_PROGRAM
   264 00000044 BE8100                  	mov si, 81h
   265 00000047 31D2                    	xor dx, dx
   266 00000049 31C0                    	xor ax, ax
   267                                  cmdline:
   268                                  .:
   269 0000004B AC                      	lodsb
   270 0000004C 3C09                    	cmp al, 9
   271 0000004E 74FB                    	je .
   272 00000050 3C20                    	cmp al, 32
   273 00000052 74F7                    	je .
   274 00000054 3C0D                    	cmp al, 13
   275 00000056 744E                    	je .end
   276 00000058 3C30                    	cmp al, '0'
   277 0000005A 721E                    	jb .notdigit
   278 0000005C 3C39                    	cmp al, '9'
   279 0000005E 771A                    	ja .notdigit
   280                                  .digit:
   281 00000060 2C30                    	sub al, '0'
   282 00000062 89D1                    	mov cx, dx		; times 1
   283 00000064 01D2                    	add dx, dx
   284 00000066 722A                    	jc .invalid
   285 00000068 01D2                    	add dx, dx		; times 4
   286 0000006A 7226                    	jc .invalid
   287 0000006C 01CA                    	add dx, cx		; times 5
   288 0000006E 7222                    	jc .invalid
   289 00000070 01D2                    	add dx, dx		; times 10
   290 00000072 721E                    	jc .invalid
   291 00000074 01C2                    	add dx, ax		; add in low digit
   292 00000076 721A                    	jc .invalid
   293 00000078 EBD1                    	jmp .
   294                                  
   295                                  .notdigit:
   296 0000007A 3C41                    	cmp al, 'A'
   297 0000007C 7419                    	je .a
   298 0000007E 3C61                    	cmp al, 'a'
   299 00000080 7415                    	je .a
   300 00000082 3C42                    	cmp al, 'B'
   301 00000084 7416                    	je .b
   302 00000086 3C62                    	cmp al, 'b'
   303 00000088 7412                    	je .b
   304 0000008A 3C43                    	cmp al, 'C'
   305 0000008C 7413                    	je .c
   306 0000008E 3C63                    	cmp al, 'c'
   307 00000090 740F                    	je .c
   308                                  .invalid:
   309 00000092 BAFFFF                  	mov dx, -1		; invalid
   310 00000095 EB0F                    	jmp .end
   311                                  
   312                                  .a:
   313 00000097 80E77F                  	and bh, ~ 80h		; A mode. determine min alloc size
   314 0000009A EBAF                    	jmp .
   315                                  
   316                                  .b:
   317 0000009C 80CF80                  	or bh, 80h		; B mode. repeat depack, counter in command line
   318 0000009F EBAA                    	jmp .
   319                                  
   320                                  .c:
   321 000000A1 80CF40                  	or bh, 40h		; C mode. how often would the counter be called
   322 000000A4 EBA5                    	jmp .
   323                                  
   324                                  .end:
   325 000000A6 52                      	push dx
   326                                    %endif
   327 000000A7 8CD2                    	mov dx, ss		; => after end of available space
   328 000000A9 8CC8                    	mov ax, cs
   329 000000AB 83C050                  	add ax, (256 + (init0_end - init0_start)) >> 4	; => source
   330 000000AE 8CCE                    	mov si, cs
   331 000000B0 83C610                  	add si, 256 >> 4	; => destination
   332                                   %endif
   333                                  
   334                                  init0_common:
   335                                  		; REM:	This part must be position-independent, as it is
   336                                  		;	 called either with ip = init0_common (kernel mode
   337                                  		;	 or device mode)
   338                                  		;	 or ip = init0_common + 256 (application mode).
   339                                  		; INP:	si => depack destination
   340                                  		;	ax => source of payload + INIT1
   341                                  		;	dx => behind available memory
   342                                  		; OUT:	cs:ip -> init1_start:
   343                                  		;	ax => INIT1
   344                                  		;	dx => payload source
   345                                  		;	si => depack destination
   346                                  		;	bx = mode
   347 000000B3 FC                      	cld
   348                                  %if _TEST_PROGRAM
   349 000000B4 81EAC71A                	sub dx, ( (payload_end - payload) ) >> 4
   350 000000B8 721A                    	jc .error
   351 000000BA B99E3D                  	mov cx, ( 		+ (init0b_end - init0b_start) 		+ (payload_end - payload) 		+ (init1_end - init1_start) 		+ (init2_end - init2_start) 		) >> 4
   357 000000BD 29CA                    	sub dx, cx
   358 000000BF 7213                    	jc .error
   359 000000C1 39C2                    	cmp dx, ax
   360 000000C3 720F                    	jb .error
   361                                  %else
   362                                  	mov cx, ( 		+ (init0b_end - init0b_start) 		+ (payload_end - payload) 		+ (init1_end - init1_start) 		) >> 4
   367                                  				; cx = amount paragraphs in INIT0B + payload + INIT1
   368                                  	sub dx, cx		; => destination for INIT0B + payload + INIT1
   369                                  				;  (highest possible spot in available memory)
   370                                  %endif
   371 000000C5 56                      	push si			; preserve destination
   372 000000C6 E83D01                  	call init0_movp		; move up payload + INIT1
   373 000000C9 5E                      	pop si
   374                                  %else
   375                                  	cld
   376                                  	mov ax, cs
   377                                  	add ax, (init0_end - init0_start) >> 4
   378                                  	mov cx, ( 		+ (init0b_end - init0b_start) 		+ (payload_end - payload) 		+ (init1_end - init1_start) 		) >> 4
   383                                  	mov dx, word [ bp + ldLoadTop ]
   384                                  	sub dx, cx
   385                                  	call init0_movp
   386                                  %endif
   387                                  
   388                                  %if _LCFG && ! _TEST_PROGRAM
   389                                  	push cs
   390                                  	call @F			; position-independent far pointer
   391                                  @@:
   392                                  	pop ax
   393                                  	add ax, lcfg - @B	; -> lCFG block
   394                                  	push ax
   395                                  %endif
   396                                  
   397 000000CA 89D0                    	mov ax, dx		; => INIT0B
   398                                  				;  (same as => payload if INIT0B is empty)
   399                                  %if (_LCFG || _PROGRESS) && ! _TEST_PROGRAM
   400                                  	add dx, ( 		+ (init0b_end - init0b_start) 		) >> 4		; => payload
   403                                  %else
   404                                   %if _TEST_PROGRAM
   405 000000CC 05321B                  	add ax, ( 		+ (init0b_end - init0b_start) 		+ (payload_end - payload) 		+ (init1_end - init1_start) 		) >> 4		; => INIT2
   410                                   %else
   411                                  	add ax, ( 		+ (init0b_end - init0b_start) 		+ (payload_end - payload) 		) >> 4		; => INIT1
   415                                   %endif
   416                                  %endif
   417 000000CF 31C9                    	xor cx, cx
   418 000000D1 50                      	push ax
   419 000000D2 51                      	push cx
   420 000000D3 CB                      	retf			; jump to relocated
   421                                  				;  INIT0B:init0b_start
   422                                  				;  or INIT1:init1_start
   423                                  				;  or INIT2:init2_start
   424                                  
   425                                  
   426                                  %if _TEST_PROGRAM
   427                                  .error:
   428 000000D4 89DF                    	mov di, bx		; whichtest
   429                                  
   430 000000D6 0E                      	push cs
   431 000000D7 1F                      	pop ds
   432 000000D8 BA[8803]                	mov dx, init0_msg.error_stderr + 256
   433 000000DB B92500                  	mov cx, init0_msg.error_stderr.length
   434 000000DE BB0200                  	mov bx, 2
   435 000000E1 B440                    	mov ah, 40h
   436 000000E3 CD21                    	int 21h
   437                                  
   438 000000E5 F7C70040                	test di, 4000h		; whichtest
   439 000000E9 7408                    	jz @F
   440 000000EB BA[AD03]                	mov dx, init0_msg.error_c_stdout + 256
   441 000000EE B90300                  	mov cx, init0_msg.error_c_stdout.length
   442 000000F1 EB0A                    	jmp @FF
   443                                  @@:
   444 000000F3 85FF                    	test di, di		; whichtest
   445 000000F5 780D                    	js @FF
   446 000000F7 BA[AD03]                	mov dx, init0_msg.error_stdout + 256
   447 000000FA B90300                  	mov cx, init0_msg.error_stdout.length
   448                                  @@:
   449 000000FD BB0100                  	mov bx, 1
   450 00000100 B440                    	mov ah, 40h
   451 00000102 CD21                    	int 21h
   452                                  @@:
   453 00000104 B8FF4C                  	mov ax, 4CFFh
   454 00000107 CD21                    	int 21h
   455                                  %endif
   456                                  
   457                                  
   458                                  %if _LCFG
   459                                  	%imacro lcfgoption 3+.nolist
   460                                  	_fill %1, 0, lcfg
   461                                  %%start:
   462                                  	%2
   463                                  	%3
   464                                  %%end:
   465                                  %assign %%index %%start - lcfg
   466                                  %rep %%end - %%start
   467                                  %assign LCFGINUSE LCFGINUSE | (1 << %%index)
   468                                   %assign %%index %%index + 1
   469                                  %endrep
   470                                  	%endmacro
   471                                  %assign LCFGINUSE 0
   472                                  
   473 00000109 90<rep 7h>              	align 16
   474                                  lcfg:
   475 00000110 EB1E                    .:		jmp strict short .end
   476 00000112 6C434647                lcfgoption 2,,	db "lCFG"
   477 00000116 3030                    lcfgoption 6,,	db "00"
   478 00000118 FCFF070000000000        lcfgoption 8,,	dq .inuse
   479                                   %if _PROGRESS && _PROGRESSMULTI
   480                                    %if _IMAGE_EXE
   481 00000120 00                      lcfgoption 16, .progress_app:, db _PROGRESSDEFAULTAPP
   482                                    %endif
   483                                    %if _DEVICE
   484 00000121 00                      lcfgoption 17, .progress_dev:, db _PROGRESSDEFAULTDEVICE
   485                                    %endif
   486                                    %if _BOOTLDR
   487 00000122 01                      lcfgoption 18, .progress_boot:, db _PROGRESSDEFAULTBOOT
   488                                    %endif
   489                                   %endif
   490                                  
   491                                  %ifnidn _LCFG_CFG, ""
   492                                   %include _LCFG_CFG
   493                                  %endif
   494                                  
   495 00000123 00<rep Dh>              lcfgoption 32,,
   496                                  .end:
   497                                  .size: equ .end - .
   498                                  .inuse equ LCFGINUSE
   499                                  %endif
   500                                  
   501                                  
   502                                  %if _DEVICE
   503                                  device_entrypoint:
   504                                  %if _TEST_PROGRAM
   505                                  @@:
   506 00000130 CC                      	int3
   507 00000131 FB                      	sti
   508 00000132 F4                      	hlt
   509 00000133 EBFB                    	jmp @B
   510                                  %endif
   511 00000135 26807F0200              	cmp byte [es:bx + 2], 0		; command code 0 (init) ?
   512 0000013A 7407                    	je @F
   513                                  
   514 0000013C 26C747030381            	mov word [es:bx + 3], 8103h	; error, done, code: unknown command
   515 00000142 CB                      	retf
   516                                  
   517                                  @@:
   518 00000143 2E830E[0000]FF          	or word [cs:device_header.next], -1
   519 00000149 0E                      	push cs
   520 0000014A 2EFF36[0600]            	push word [cs:device_header.strategy]
   521                                  					; -> far return to payload's strategy
   522 0000014F 55                      	push bp
   523 00000150 1E                      	push ds
   524 00000151 56                      	push si
   525 00000152 57                      	push di
   526 00000153 52                      	push dx
   527 00000154 51                      	push cx
   528 00000155 50                      	push ax
   529 00000156 53                      	push bx
   530 00000157 06                      	push es
   531                                  
   532 00000158 268B570E                	mov dx, word [es:bx + 14]
   533 0000015C B104                    	mov cl, 4
   534 0000015E D3EA                    	shr dx, cl		; = how many full paragraphs (rounded down)
   535 00000160 26035710                	add dx, word [es:bx + 14 + 2]
   536                                  				; ax => behind end
   537                                  
   538 00000164 8CCF                    	mov di, cs
   539                                  image_size: equ  ( 		+ (init0_end - init0_start) 		+ (init0b_end - init0b_start) 		+ (payload_end - payload) 		+ (init1_end - init1_start) 		)
   545 00000166 81C7721B                	add di, image_size >> 4		; di => end of image
   546                                  
   547 0000016A 39FA                    	cmp dx, di			; enough memory to hold all of us ?
   548 0000016C 720E                    	jb @F				; no -->
   549                                  				; yes
   550                                  .have_some_memory:
   551                                  				; dx => after end of memory
   552 0000016E BB0200                  	mov bx, 2		; device mode
   553 00000171 50                      	push ax
   554 00000172 8CC8                    	mov ax, cs
   555 00000174 83C040                  	add ax, (init0_end - init0_start) >> 4
   556 00000177 8CCE                    	mov si, cs		; => destination
   557 00000179 E937FF                  	jmp init0_common
   558                                  
   559                                  
   560                                  @@:
   561 0000017C 0E                      	push cs
   562 0000017D 1F                      	pop ds
   563 0000017E BA[BD01]                	mov dx, .msg_no_memory
   564 00000181 B409                    	mov ah, 09h
   565 00000183 CD21                    	int 21h
   566                                  
   567 00000185 B80030                  	mov ax, 3000h
   568 00000188 CD21                    	int 21h
   569 0000018A 3C05                    	cmp al, 5
   570 0000018C 7307                    	jae @F
   571 0000018E BA[DC01]                	mov dx, .msg_dos_below_5
   572 00000191 B409                    	mov ah, 09h
   573 00000193 CD21                    	int 21h
   574                                  @@:
   575                                  
   576 00000195 BA[0302]                	mov dx, .msg_linebreak
   577 00000198 B409                    	mov ah, 09h
   578 0000019A CD21                    	int 21h
   579                                  
   580                                  		; tear down the stack frame and modify the request header
   581 0000019C 07                      	pop es
   582 0000019D 5B                      	pop bx
   583 0000019E 26C747030381            	mov word [es:bx + 3], 8103h	; set error, done, invalid command
   584 000001A4 26C6470D00              	mov byte [es:bx + 13], 0	; set number of units = 0
   585 000001A9 268C4F10                	mov word [es:bx + 14 + 2], cs
   586 000001AD 2683670E00              	and word [es:bx + 14], 0	; -> after end of memory to allocate
   587 000001B2 58                      	pop ax
   588 000001B3 59                      	pop cx
   589 000001B4 5A                      	pop dx
   590 000001B5 5F                      	pop di
   591 000001B6 5E                      	pop si
   592 000001B7 1F                      	pop ds
   593 000001B8 5D                      	pop bp
   594 000001B9 83C404                  	add sp, 4		; discard far return to payload's strategy
   595 000001BC CB                      	retf			; return to DOS
   596                                  
   597                                  
   598 000001BD 4C6F6164206572726F-     .msg_no_memory:		ascic "Load error: Not enough memory."
   598 000001C6 723A204E6F7420656E-
   598 000001CF 6F756768206D656D6F-
   598 000001D8 72792E24           
   599 000001DC 204E6F74653A20444F-     .msg_dos_below_5:	ascic " Note: DOS must be at least version 5."
   599 000001E5 53206D757374206265-
   599 000001EE 206174206C65617374-
   599 000001F7 2076657273696F6E20-
   599 00000200 352E24             
   600 00000203 0D0A24                  .msg_linebreak:		ascic 13,10
   601                                  %endif
   602                                  
   603                                  
   604                                  		; Move paragraphs
   605                                  		;
   606                                  		; INP:	ax => source
   607                                  		;	dx => destination
   608                                  		;	cx = number of paragraphs
   609                                  		; CHG:	cx, ds, si, es, di
   610                                  		; OUT:	ax and dx unchanged
   611                                  		; Note:	Doesn't work correctly on HMA; doesn't always wrap to LMA either.
   612                                  		;	Do not provide a wrapped/HMA source or destination!
   613                                  init0_movp:
   614 00000206 39D0                    	cmp ax, dx		; source above destination ?
   615 00000208 770A                    	ja .up			; yes, move up (forwards) -->
   616 0000020A 747B                    	je .return		; same, no need to move -->
   617 0000020C 50                      	push ax
   618 0000020D 01C8                    	add ax, cx		; (expected not to carry)
   619 0000020F 39D0                    	cmp ax, dx		; end of source is above destination ?
   620 00000211 58                      	pop ax
   621 00000212 7730                    	ja .down		; yes, move from top down -->
   622                                  	; Here, the end of source is below-or-equal the destination,
   623                                  	;  so they do not overlap. In this case we prefer moving up.
   624                                  
   625                                  .up:
   626 00000214 50                      	push ax
   627 00000215 52                      	push dx
   628                                  .uploop:
   629 00000216 8ED8                    	mov ds, ax
   630 00000218 8EC2                    	mov es, dx
   631 0000021A 31FF                    	xor di, di
   632 0000021C 31F6                    	xor si, si		; -> start of segment
   633 0000021E 81E90010                	sub cx, 1000h		; 64 KiB left ?
   634 00000222 7610                    	jbe .uplast		; no -->
   635 00000224 51                      	push cx
   636 00000225 B90080                  	mov cx, 10000h /2
   637 00000228 F3A5                    	rep movsw		; move 64 KiB
   638 0000022A 59                      	pop cx
   639 0000022B 050010                  	add ax, 1000h
   640 0000022E 81C20010                	add dx, 1000h		; -> next segment
   641 00000232 EBE2                    	jmp short .uploop	; proceed for more -->
   642                                  .uplast:
   643 00000234 81C10010                	add cx, 1000h		; restore counter
   644 00000238 D1E1                    	shl cx, 1
   645 0000023A D1E1                    	shl cx, 1
   646 0000023C D1E1                    	shl cx, 1		; *8, paragraphs to words
   647 0000023E F3A5                    	rep movsw		; move last part
   648 00000240 5A                      	pop dx
   649 00000241 58                      	pop ax
   650 00000242 EB43                    	jmp short .return
   651                                  
   652                                  .down:
   653 00000244 FD                      	std			; _AMD_ERRATUM_109_WORKAROUND as below
   654                                  .dnloop:
   655 00000245 81E90010                	sub cx, 1000h		; 64 KiB left ?
   656 00000249 761A                    	jbe .dnlast		; no -->
   657 0000024B 50                      	push ax
   658 0000024C 52                      	push dx
   659 0000024D 01C8                    	add ax, cx
   660 0000024F 01CA                    	add dx, cx
   661 00000251 8ED8                    	mov ds, ax		; -> 64 KiB not yet moved
   662 00000253 8EC2                    	mov es, dx
   663 00000255 5A                      	pop dx
   664 00000256 58                      	pop ax
   665 00000257 BFFEFF                  	mov di, -2
   666 0000025A 89FE                    	mov si, di		; moved from last word down
   667 0000025C 51                      	push cx
   668 0000025D B90080                  	mov cx, 10000h /2
   669 00000260 F3A5                    	rep movsw		; move 64 KiB
   670 00000262 59                      	pop cx
   671 00000263 EBE0                    	jmp short .dnloop	; proceed for more -->
   672                                  .dnlast:
   673 00000265 81C10010                	add cx, 1000h		; restore counter
   674 00000269 D1E1                    	shl cx, 1
   675 0000026B D1E1                    	shl cx, 1
   676 0000026D D1E1                    	shl cx, 1		; *8, paragraphs to words
   677 0000026F 89CF                    	mov di, cx
   678 00000271 4F                      	dec di
   679 00000272 D1E7                    	shl di, 1		; words to offset, -> last word
   680 00000274 89FE                    	mov si, di
   681 00000276 8ED8                    	mov ds, ax
   682 00000278 8EC2                    	mov es, dx		; first segment correct
   683                                  
   684                                  
   685                                  	numdef AMD_ERRATUM_109_WORKAROUND, 1
   686                                  %if 0
   687                                  
   688                                  Jack R. Ellis pointed out this erratum:
   689                                  
   690                                  Quoting from https://www.amd.com/system/files/TechDocs/25759.pdf page 69:
   691                                  
   692                                  109   Certain Reverse REP MOVS May Produce Unpredictable Behavior
   693                                  
   694                                  Description
   695                                  
   696                                  In certain situations a REP MOVS instruction may lead to
   697                                  incorrect results. An incorrect address size, data size
   698                                  or source operand segment may be used or a succeeding
   699                                  instruction may be skipped. This may occur under the
   700                                  following conditions:
   701                                  
   702                                  * EFLAGS.DF=1 (the string is being moved in the reverse direction).
   703                                  
   704                                  * The number of items being moved (RCX) is between 1 and 20.
   705                                  
   706                                  * The REP MOVS instruction is preceded by some microcoded instruction
   707                                    that has not completely retired by the time the REP MOVS begins
   708                                    execution. The set of such instructions includes BOUND, CLI, LDS,
   709                                    LES, LFS, LGS, LSS, IDIV, and most microcoded x87 instructions.
   710                                  
   711                                  Potential Effect on System
   712                                  
   713                                  Incorrect results may be produced or the system may hang.
   714                                  
   715                                  Suggested Workaround
   716                                  
   717                                  Contact your AMD representative for information on a BIOS update.
   718                                  
   719                                  %endif
   720                                  
   721                                  %if _AMD_ERRATUM_109_WORKAROUND
   722 0000027A E308                    	jcxz @FF
   723 0000027C 83F914                  	cmp cx, 20
   724 0000027F 7703                    	ja @FF
   725                                  @@:
   726 00000281 A5                      	movsw
   727 00000282 E2FD                    	loop @B
   728                                  @@:
   729                                  %endif
   730 00000284 F3A5                    	rep movsw		; move first part
   731 00000286 FC                      	cld
   732                                  .return:
   733 00000287 C3                      	retn
   734                                  
   735                                  
   736                                  	addsection INIT0B, align=16 vstart=0 follows=INIT0
   737                                  		; INP:	ax:cx = cs:ip = cs:0
   738                                  		;	ax => INIT0B
   739                                  		;	dx => payload source
   740                                  		;	ss:sp -> (dword pointer to lCFG block)
   741                                  		;	if app/device mode supported:
   742                                  		;	 si => depack destination
   743                                  		;	 bx = mode
   744                                  init0b_start:
   745                                  %if _LCFG || _PROGRESS
   746                                  
   747 00000000 05D81A                  	add ax, ( 		+ (init0b_end - init0b_start) 		+ (payload_end - payload) 		) >> 4		; => INIT1
   751                                  
   752                                  %if _LCFG && _PASSLCFG
   753                                   %if (_IMAGE_EXE || _DEVICE)
   754                                  	mov cx, si		; preserve si
   755                                   %endif
   756                                  	pop si
   757                                  	pop ds			; -> lCFG block
   758                                  	mov es, ax
   759                                  	mov di, transfer_lcfg_block
   760                                  				; -> destination
   761                                   %if (_IMAGE_EXE || _DEVICE)
   762                                  	push cx
   763                                   %endif
   764                                  	push di
   765                                  	mov cx, words(lcfg.size)
   766                                  	rep movsw		; cx = 0
   767                                  	pop di			; es:di -> relocated lCFG block, same difference
   768                                   %if (_IMAGE_EXE || _DEVICE)
   769                                  	pop si			; preserve si
   770                                   %endif
   771                                  %elif _LCFG
   772 00000003 5F                      	pop di
   773 00000004 07                      	pop es			; es:di -> (original) lCFG block
   774                                  %endif
   775                                  
   776 00000005 50                      	push ax
   777 00000006 51                      	push cx			; -> far return address to init1_start
   778                                  
   779                                  %if _PROGRESS && _PROGRESSMULTI
   780                                   %if (_IMAGE_EXE || _DEVICE)
   781 00000007 56                      	push si
   782 00000008 53                      	push bx
   783                                   %endif
   784                                   %if _BOOTLDR
   785                                    %if _LCFG
   786 00000009 31C0                    	xor ax, ax
   787 0000000B 268A4512                	mov al, byte [es:di + lcfg.progress_boot - lcfg]
   788 0000000F 97                      	xchg di, ax		; di = boot default progress
   789                                  				; es:ax -> lCFG block
   790                                    %else			; _LCFG
   791                                  	mov di, _PROGRESSDEFAULTBOOT
   792                                    %endif		; _LCFG
   793                                   %endif			; _BOOTLDR
   794                                   %if (_IMAGE_EXE || _DEVICE)
   795                                    %if _BOOTLDR
   796 00000010 85DB                    	test bx, bx
   797 00000012 7503E9AC00              	jz .default
   798                                    %endif		; _BOOTLDR
   799                                    %if _LCFG
   800 00000017 06                      	push es
   801                                     %if _BOOTLDR
   802 00000018 50                      	push ax			; -> lCFG block
   803                                     %else		; _BOOTLDR
   804                                  	push di
   805                                     %endif		; _BOOTLDR
   806                                    %endif		; _LCFG
   807                                    %if _PROGRESSVAR
   808 00000019 B451                    	mov ah, 51h
   809 0000001B CD21                    	int 21h
   810                                  @@:
   811 0000001D 8EDB                    	mov ds, bx
   812 0000001F A12C00                  	mov ax, word [2Ch]
   813 00000022 85C0                    	test ax, ax
   814 00000024 7510                    	jnz .gotenv
   815 00000026 A11600                  	mov ax, word [16h]
   816 00000029 40                      	inc ax
   817 0000002A 7469                    	jz .default_nonboot
   818 0000002C 48                      	dec ax
   819 0000002D 7466                    	jz .default_nonboot
   820 0000002F 39C3                    	cmp bx, ax
   821 00000031 7462                    	je .default_nonboot
   822 00000033 93                      	xchg bx, ax
   823 00000034 EBE7                    	jmp @B
   824                                  
   825                                  .gotenv:
   826 00000036 0E                      	push cs
   827 00000037 1F                      	pop ds				; => cs
   828 00000038 8EC0                    	mov es, ax			; => environment
   829                                   %ifnidni _PROGRESSVARNAME, ""
   830 0000003A BB[F000]                	mov bx, progressvarname1
   831 0000003D E80C00                  	call .findenv
   832 00000040 732E                    	jnc .foundenv			; match -->
   833 00000042 BB[0001]                	mov bx, progressvarname2
   834                                   %endif
   835 00000045 E80400                  	call .findenv
   836 00000048 7326                    	jnc .foundenv			; match -->
   837 0000004A EB49                    	jmp .default_nonboot
   838                                  
   839                                  .findenv:
   840 0000004C 31FF                    	xor di, di
   841                                  @@:
   842                                   %ifnidni _PROGRESSVARNAME, ""
   843 0000004E 89DE                    	mov si, bx			; -> variable name
   844                                   %else
   845                                  	mov si, progressvarname2	; -> variable name
   846                                   %endif
   847 00000050 31C0                    	xor ax, ax
   848 00000052 AC                      	lodsb				; get length
   849 00000053 91                      	xchg cx, ax			; cx = length
   850 00000054 57                      	push di
   851 00000055 F3A6                    	repe cmpsb			; match ?
   852 00000057 7415                    	je .findret_pop			; (NC) yes -->
   853 00000059 5F                      	pop di
   854 0000005A B90080                  	mov cx, 8000h			; maximum env size
   855 0000005D 29F9                    	sub cx, di			; any left ?
   856 0000005F 760B                    	jbe .findret_CY			; no -->
   857 00000061 B000                    	mov al, 0
   858 00000063 F2AE                    	repne scasb			; find next NUL
   859 00000065 7505                    	jne .findret_CY
   860 00000067 263805                  	cmp byte [es:di], al		; double NUL ?
   861 0000006A 75E2                    	jne @B				; no, loop -->
   862                                  .findret_CY:
   863 0000006C F9                      	stc				; not found
   864 0000006D C3                      	retn
   865                                  
   866                                  .findret_pop:
   867 0000006E 59                      	pop cx
   868 0000006F C3                      	retn
   869                                  
   870                                  .foundenv:
   871                                  %if _LCFG
   872 00000070 5E                      	pop si
   873 00000071 5E                      	pop si				; discard far pointer to lCFG
   874                                  %endif
   875 00000072 89FE                    	mov si, di			; es:si -> var content
   876 00000074 31FF                    	xor di, di			; = 0
   877 00000076 31C0                    	xor ax, ax			; ah = 0
   878                                  @@:
   879 00000078 26AC                    	es lodsb			; next candidate digit
   880 0000007A 2C30                    	sub al, '0'			; digit ?
   881 0000007C 7212                    	jb @F				; no -->
   882 0000007E 3C09                    	cmp al, 9
   883 00000080 770E                    	ja @F				; no -->
   884 00000082 01FF                    	add di, di			; times 2
   885 00000084 89FB                    	mov bx, di
   886 00000086 01FF                    	add di, di			; times 4
   887 00000088 01FF                    	add di, di			; times 8
   888 0000008A 01DF                    	add di, bx			; times 8 + times 2 = times 10
   889 0000008C 01C7                    	add di, ax			; add in next digit
   890 0000008E EBE8                    	jmp @B				; and loop
   891                                  
   892                                  @@:
   893 00000090 83E71F                  	and di, 31			; mask to low 5 bits
   894 00000093 EB10                    	jmp @F
   895                                    %endif	; _PROGRESSVAR
   896                                  
   897                                  .default_nonboot:
   898                                  %if _LCFG
   899 00000095 5F                      	pop di
   900 00000096 07                      	pop es			; -> lCFG block
   901 00000097 5B                      	pop bx			; = 1 if app, = 2 if device
   902 00000098 53                      	push bx
   903 00000099 268B4510                	mov ax, word [es:di + lcfg.progress_app - lcfg]
   904                                   %if _DEVICE
   905                                    %if lcfg.progress_app + 1 != lcfg.progress_dev
   906                                     %error Unexpected layout
   907                                    %endif
   908                                  				; al = app, ah = device
   909 0000009D 4B                      	dec bx
   910 0000009E 7402                    	jz .default_app
   911 000000A0 86C4                    	xchg al, ah		; al = device
   912                                  .default_app:
   913                                   %endif			; _DEVICE
   914 000000A2 B400                    	mov ah, 0		; ax = default
   915 000000A4 97                      	xchg di, ax		; di = default
   916                                  %else			; _LCFG
   917                                  	mov di, _PROGRESSDEFAULT
   918                                  %endif			; _LCFG
   919                                  @@:
   920 000000A5 F7C71000                	test di, 16
   921 000000A9 7405                    	jz @F
   922 000000AB 83E7EF                  	and di, ~16
   923 000000AE EB13                    	jmp .outputdevice	; if 16 set skip device check
   924                                  @@:
   925 000000B0 52                      	push dx
   926 000000B1 B80044                  	mov ax, 4400h		; IOCTL get device information
   927 000000B4 BB0100                  	mov bx, 1		; StdOut
   928 000000B7 B283                    	mov dl, 83h		; default if 21.4400 fails
   929 000000B9 CD21                    	int 21h
   930 000000BB F6C280                  	test dl, 80h
   931 000000BE 5A                      	pop dx
   932 000000BF 7502                    	jnz .outputdevice
   933 000000C1 31FF                    	xor di, di		; stdout is a file: force no progress marker
   934                                  .outputdevice:
   935                                   %endif			; (_IMAGE_EXE || _DEVICE)
   936                                  
   937                                  .default:
   938                                   %if (_IMAGE_EXE || _DEVICE)
   939 000000C3 5B                      	pop bx
   940 000000C4 5E                      	pop si
   941                                   %endif
   942 000000C5 83FF05                  	cmp di, step_progress_table.amount
   943                                  					; valid ?
   944 000000C8 7202                    	jb @F				; yes -->
   945 000000CA 31FF                    	xor di, di			; disable
   946                                  @@:
   947 000000CC D1E7                    	shl di, 1			; = word index
   948 000000CE 59                      	pop cx				; = 0
   949 000000CF 1F                      	pop ds				; ds => INIT1
   950                                  
   951 000000D0 2E8B85[E600]            	mov ax, word [cs:step_progress_table + di]
   952                                  					; ax = rel16 for step progress handler
   953 000000D5 1E                      	push ds
   954 000000D6 51                      	push cx				; -> init1_start
   955 000000D7 A3[BA00]                	mov word [step_progress.handler], ax
   956                                  					; patch it
   957                                  
   958 000000DA 83FF04                  	cmp di, 2 * 2			; none or dots ?
   959 000000DD 7305                    	jae @F				; no, skip -->
   960 000000DF C606[0600]A9            	mov byte [..@empty_step_progress], __TEST_IMM16
   961                                  					; for none and dots do not call here
   962                                  @@:
   963                                  %endif			;  _PROGRESS && _PROGRESSMULTI
   964 000000E4 CB                      	retf
   965                                  %endif
   966                                  
   967                                  %if _PROGRESS && _PROGRESSMULTI
   968 000000E5 00                      		align 2, db 0
   969                                  step_progress_table:
   970                                  .:
   971 000000E6 1600                    		dw step_progress.0_step - step_progress.base
   972 000000E8 3B00                    		dw step_progress.1_step - step_progress.base
   973 000000EA 0000                    		dw step_progress.2_step - step_progress.base
   974 000000EC 4100                    		dw step_progress.3_step - step_progress.base
   975 000000EE 8600                    		dw step_progress.4_step - step_progress.base
   976                                  .amount equ ($ - .) / 2
   977                                  
   978                                   %if _PROGRESSVAR && (_IMAGE_EXE || _DEVICE)
   979                                    %ifnidni _PROGRESSVARNAME, ""
   980 000000F0 0F4C44454255475052-     progressvarname1:	counted _PROGRESSVARNAME, '='
   980 000000F9 4F47524553533D     
   981                                    %endif
   982 00000100 0D4C444F5350524F47-     progressvarname2:	counted "LDOSPROGRESS="
   982 00000109 524553533D         
   983                                   %endif
   984                                  %endif
   985                                  
   986 0000010E 90<rep 2h>              	align 16
   987                                  init0b_end:
   988                                  
   989                                  
   990                                  	addsection PAYLOAD, align=16 follows=INIT0B
   991                                  payload:
   992 00000000 <bin 1AC67h>            	incbin _PAYLOAD_FILE
   993                                  .end:
   994 0001AC67 26<rep 9h>              	align 16, db 38
   995                                  payload_end:
   996                                  
   997                                  %if _OPTIMISE_INPUT_SMALL
   998                                   %ifn (payload_end - payload) <= 65_520
   999                                    %error Input declared small is not small
  1000                                   %endif
  1001                                  %else
  1002                                   %if (payload_end - payload) <= 65_520
  1003                                    %warning Input could be declared small
  1004                                   %endif
  1005                                  %endif
  1006                                  
  1007                                  
  1008                                  	addsection INIT1, align=16 follows=PAYLOAD vstart=0
  1009                                  init1_start:
  1010                                  		; INP:	ax = cs = INIT1
  1011                                  		;	dx = cs - (payload_end - payload) >> 4 => source data
  1012                                  		;	if kernel mode:
  1013                                  		;	 ss:bp -> LOADDATA and LOADSTACKVARS
  1014                                  		;	 ss:sp -> valid stack above [bp + ldLoadTop]
  1015                                  		;	 60h => destination
  1016                                  		;	any mode:
  1017                                  		;	 ss:sp -> word value for ax
  1018                                  		;	 bx = 2 if device mode, 1 if EXE mode, 0 if kernel mode
  1019                                  		;	 si => destination (60h for kernel mode,
  1020                                  		;		after PSP for EXE mode,
  1021                                  		;		at device header for device mode)
  1022                                  		;		(not set if only kernel mode supported)
  1023                                  		;	 if EXE mode:
  1024                                  		;	  ss:sp -> valid stack above INIT1
  1025                                  		;	  bp = unset
  1026                                  		;	 if device mode:
  1027                                  		;	  ss:sp -> device entrypoint stack
  1028                                  		;	  holds: es, bx, ax, cx, dx, di, si, ds, bp,
  1029                                  		;		  far address of payload strategy entrypoint,
  1030                                  		;		  far return address to DOS
  1031                                  		;	  bp = unset
  1032                                  		; STT:	UP
  1033                                  		; CHG:	ax, bx, cx, dx, es, ds, si, di
  1034                                  %if _IMAGE_EXE || _DEVICE
  1035                                  	lframe
  1036 00000000 5589E5                  	lenter
  1037                                  	lvar word,	exemode		; must be bp - 2!
  1038 00000003 53                      	 push bx
  1039                                   %if ?exemode != -2
  1040                                    %error exemode variable must be directly below bp
  1041                                   %endif
  1042 00000004 56                      	push si
  1043                                   %if _PROGRESS
  1044 00000005 F8                      	clc			; NC
  1045                                   %endif
  1046                                  %else
  1047                                  	xor bx, bx		; always tell them it is kernel mode (NC)
  1048                                  %endif
  1049                                  
  1050                                  %if _PROGRESS
  1051                                  	; clc
  1052                                  ..@empty_step_progress:		; SMC, byte patched to __TEST_IMM16 if none or dots
  1053                                   %if _TEST_PROGRAM
  1054 00000006 90<rep 3h>              	times 3 nop
  1055                                   %else
  1056                                  	call step_progress.CF	; display empty / 0.0%
  1057                                   %endif
  1058                                  @@:
  1059                                  %endif
  1060                                  
  1061                                  %if _IMAGE_EXE || _DEVICE
  1062 00000009 8EC6                    	mov es, si		; es => destination
  1063                                  %else
  1064                                  	mov ax, 60h
  1065                                  	mov es, ax		; es => destination
  1066                                  %endif
  1067 0000000B 31FF                    	xor di, di		; -> destination
  1068                                  
  1069 0000000D 8EDA                    	mov ds, dx
  1070 0000000F 31F6                    	xor si, si		; -> source
  1071                                  
  1072 00000011 B967AC                  	mov cx, (payload.end - payload) & 0FFFFh
  1073                                  %if (payload.end - payload) >> 16
  1074 00000014 BA0100                  	mov dx, (payload.end - payload) >> 16	; = length of source
  1075                                  %else
  1076                                  	xor dx, dx
  1077                                  %endif
  1078                                  %if _PAYLOAD_KERNEL_MAX_PARAS
  1079                                  	mov ax, _PAYLOAD_KERNEL_MAX_PARAS
  1080                                  	test bx, bx
  1081                                  	jz @F
  1082                                  %endif
  1083 00000017 B8FFFF                  	mov ax, -1
  1084                                  @@:
  1085                                  
  1086                                  		; INP:	ds:si -> source
  1087                                  		;	dx:cx = length of source
  1088                                  		;	es:di -> destination (below source)
  1089                                  		;	bx = EXE mode flag
  1090                                  		;	 (1 if EXE mode, 0 if kernel mode)
  1091                                  		;	 (always 0 if this is a build without EXE mode)
  1092                                  		;	if _PAYLOAD_KERNEL_MAX_PARAS:
  1093                                  		;	 ax = maximum amount in paragraphs of destination needed
  1094                                  		;	 (-1 if full source should be decompressed)
  1095                                  		;	else:
  1096                                  		;	 ax = -1
  1097                                  		; Note:	The destination reaches up to below the source.
  1098 0000001A E85602                  	call depack
  1099                                  %ifn _TEST_PROGRAM
  1100                                  	jc strict short error	; -2 (replaced by retf \ nop)
  1101                                  %endif
  1102                                  %if _IMAGE_EXE || _DEVICE
  1103 0000001D 5E                      	pop si			; si
  1104                                   %if _TEST_PROGRAM
  1105 0000001E 58                      	pop ax			; (discard ?exemode, leave bx as returned)
  1106                                   %else
  1107                                  	pop bx			; ?exemode
  1108                                   %endif
  1109 0000001F 5D                      	pop bp			; bp
  1110 00000020 58                      	pop ax			; ax
  1111                                  	lleave ctx
  1112                                  %endif
  1113                                  %if _TEST_PROGRAM
  1114 00000021 CB                      	retf			; +1 (replace jc error)
  1115 00000022 90                      	nop			; +1
  1116                                  %endif
  1117                                  
  1118                                  %if _DEVICE
  1119 00000023 F6C302                  	test bl, 2
  1120 00000026 7411                    	jz .jmp_exe_or_kernel_mode
  1121                                  
  1122                                  .jmp_device_mode:
  1123 00000028 8EDE                    	mov ds, si
  1124 0000002A 830E[0000]FF            	or word [device_header.next], -1
  1125                                  %if _LCFG && _PASSLCFG
  1126                                  	mov word [device_header.pass_lcfg], transfer_lcfg_block
  1127                                  	mov word [device_header.pass_lcfg + 2], cs
  1128                                  %endif
  1129 0000002F 07                      	pop es
  1130 00000030 5B                      	pop bx
  1131 00000031 58                      	pop ax
  1132 00000032 59                      	pop cx
  1133 00000033 5A                      	pop dx
  1134 00000034 5F                      	pop di
  1135 00000035 5E                      	pop si
  1136 00000036 1F                      	pop ds
  1137 00000037 5D                      	pop bp
  1138 00000038 CB                      	retf			; transfer to payload strategy entrypoint
  1139                                  				; still on stack: far return address to DOS
  1140                                  
  1141                                  .jmp_exe_or_kernel_mode:
  1142                                  %endif
  1143                                  
  1144                                  %if _IMAGE_EXE
  1145 00000039 F6C301                  	test bl, 1
  1146 0000003C 7410                    	jz .jmp_kernel_mode
  1147                                  
  1148                                  .jmp_exe_mode:
  1149                                  %if ! _IMAGE_EXE_AUTO_STACK
  1150                                  	mov cx, cs
  1151                                  	lea dx, [si + _IMAGE_EXE_SS]
  1152                                  	push dx			; stack = relocated ss value
  1153                                  	add dx, (_IMAGE_EXE_SP + 2 + 15) >> 4
  1154                                  	cmp cx, dx		; INIT1 code is above intended stack ?
  1155                                  	jae @F			; yes -->
  1156                                  
  1157                                  	lframe
  1158                                  	lenter
  1159                                  	lvar	word, exemode
  1160                                  	 push bx
  1161                                   %if ?exemode != -2
  1162                                    %error exemode variable must be directly below bp
  1163                                   %endif
  1164                                  	mov bx, -1		; unimplemented, return error
  1165                                  	jmp error
  1166                                  	lleave ctx
  1167                                  @@:
  1168                                  	cli
  1169                                  	pop ss			; = relocated ss value
  1170                                  	mov sp, (_IMAGE_EXE_SP + 2) & 0FFFFh	; change stack
  1171                                  	sti
  1172                                  %endif
  1173                                  
  1174 0000003E 31C9                    	xor cx, cx
  1175 00000040 51                      	push cx			; put zero on top of stack
  1176                                  
  1177                                  %if _IMAGE_EXE_IP < 256
  1178                                  	mov cl, _IMAGE_EXE_IP
  1179                                  %elif (_IMAGE_EXE_IP & 255) == 0
  1180                                  	mov ch, _IMAGE_EXE_IP >> 8
  1181                                  %else
  1182 00000041 B94001                  	mov cx, _IMAGE_EXE_IP
  1183                                  %endif
  1184                                  
  1185                                  %if _IMAGE_EXE_CS == -16
  1186 00000044 83C6F0                  	add si, -16
  1187 00000047 8EDE                    	mov ds, si
  1188 00000049 8EC6                    	mov es, si
  1189                                  %else
  1190                                  	lea dx, [si - 10h]	; => PSP
  1191                                  	mov ds, dx
  1192                                  	mov es, dx		; ds = es => PSP
  1193                                   %if _IMAGE_EXE_CS
  1194                                  	add si, _IMAGE_EXE_CS	; = relocated cs value
  1195                                   %endif
  1196                                  %endif
  1197 0000004B 56                      	push si
  1198 0000004C 51                      	push cx
  1199                                  %if _LCFG && _PASSLCFG
  1200                                  	mov di, transfer_lcfg_block
  1201                                  	mov cx, cs
  1202                                  %endif
  1203 0000004D CB                      	retf			; jump to EXE mode of image
  1204                                  
  1205                                  %endif
  1206                                  .jmp_kernel_mode:
  1207                                  %if _LCFG && _PASSLCFG
  1208                                  	mov di, transfer_lcfg_block
  1209                                  	mov cx, cs
  1210                                  %endif
  1211 0000004E EA20006000              	jmp 60h + _EXEC_SEGMENT:_EXEC_OFFSET
  1212                                  
  1213                                  
  1214                                  %if _TEST_PROGRAM
  1215                                  disp_al_counter:
  1216 00000053 2EFF06[5900]            	inc word [cs:init1_c_mode_counter]	; 5 bytes
  1217 00000058 C3                      	retn					; 1 byte
  1218                                  
  1219 00000059 0000                    init1_c_mode_counter:		dw 0		; 2 bytes, total 8
  1220                                  %else
  1221                                   %if _PROGRESS
  1222                                  disp_al_counter equ step_progress
  1223                                   %else
  1224                                  disp_al_counter equ disp_al
  1225                                   %endif
  1226                                  %endif
  1227                                  
  1228                                  
  1229                                  error:
  1230                                   %ifn _TEST_PROGRAM		; 2 bytes
  1231                                  	push cs
  1232                                  	pop ds
  1233                                   %endif
  1234                                  %if _DEBUG0
  1235                                  	mov si, msg.error_begin
  1236                                  	call disp_error
  1237                                  	xchg ax, bx			; ax = error code
  1238                                  	call disp_ax_hex
  1239                                   %ifn _TEST_PROGRAM		; 3 bytes
  1240                                  	mov si, msg.error_end
  1241                                   %endif
  1242                                  %else
  1243                                   %ifn _TEST_PROGRAM		; 3 bytes
  1244                                  	mov si, msg.error
  1245                                   %endif
  1246                                  %endif
  1247                                   %ifn _TEST_PROGRAM		; 3 bytes, total 2 + 3 + 3 = 8
  1248                                  	call disp_error
  1249                                   %endif
  1250                                  %if _DEVICE
  1251 0000005B F646FE02                	test byte [bp - 2], 2
  1252 0000005F 7435                    	jz .exit_app_or_kernel
  1253                                  
  1254 00000061 5E                      	pop si				; => device segment
  1255 00000062 5B                      	pop bx				; mode word
  1256 00000063 5D                      	pop bp
  1257 00000064 58                      	pop ax
  1258 00000065 8EC6                    	mov es, si
  1259 00000067 31FF                    	xor di, di			; -> device header
  1260 00000069 0E                      	 push cs
  1261 0000006A 1F                      	 pop ds
  1262 0000006B BE[0002]                	mov si, device_header_copy	; -> to reset header
  1263 0000006E B91100                  	mov cx, words(device_header_copy.length)
  1264 00000071 F3A5                    	rep movsw		; overwrite device header with default
  1265                                  				;  (reset to a valid state after unsuccessful
  1266                                  				;  decompression, which may have partially
  1267                                  				;  written the header already)
  1268 00000073 06                      	 push es
  1269 00000074 1F                      	 pop ds				; -> device segment
  1270 00000075 07                      	pop es
  1271 00000076 5B                      	pop bx
  1272 00000077 26C747030381            	mov word [es:bx + 3], 8103h	; set error, done, invalid command
  1273 0000007D 26C6470D00              	mov byte [es:bx + 13], 0	; set number of units = 0
  1274 00000082 268C5F10                	mov word [es:bx + 14 + 2], ds
  1275 00000086 2683670E00              	and word [es:bx + 14], 0	; -> after end of memory to allocate
  1276 0000008B 58                      	pop ax
  1277 0000008C 59                      	pop cx
  1278 0000008D 5A                      	pop dx
  1279 0000008E 5F                      	pop di
  1280 0000008F 5E                      	pop si
  1281 00000090 1F                      	pop ds
  1282 00000091 5D                      	pop bp
  1283 00000092 83C404                  	add sp, 4		; discard far return to payload's strategy
  1284 00000095 CB                      	retf			; return to DOS
  1285                                  
  1286                                  
  1287                                  .exit_app_or_kernel:
  1288                                  %endif
  1289                                  
  1290                                  %if _IMAGE_EXE
  1291                                  %if _BOOTLDR
  1292 00000096 F646FE01                	test byte [bp - 2], 1
  1293 0000009A 7405                    	jz .exit_kernel_mode
  1294                                  %endif
  1295                                  
  1296 0000009C B87F4C                  	mov ax, 4C7Fh
  1297 0000009F CD21                    	int 21h
  1298                                  
  1299                                  .exit_kernel_mode:
  1300                                  %endif
  1301                                  %if _BOOTLDR
  1302 000000A1 31C0                    	xor ax, ax
  1303 000000A3 CD16                    	int 16h
  1304 000000A5 CD19                    	int 19h
  1305                                  %endif
  1306                                  
  1307                                  
  1308                                  %if _PROGRESS
  1309                                  step_progress:
  1310 000000A7 F9                      	stc
  1311                                  .CF:
  1312 000000A8 53                      	push bx
  1313 000000A9 51                      	push cx
  1314 000000AA 52                      	push dx
  1315 000000AB 1E                      	push ds
  1316 000000AC 56                      	push si
  1317 000000AD 57                      	push di
  1318 000000AE 50                      	push ax
  1319                                  
  1320 000000AF 0E                      	push cs
  1321 000000B0 1F                      	pop ds
  1322                                  
  1323 000000B1 8316[E201]00            	adc word [.counter], 0
  1324 000000B6 A1[E201]                	mov ax, word [.counter]
  1325                                  
  1326                                   %if _PROGRESSMULTI
  1327                                    %if _IMAGE_EXE || _DEVICE
  1328                                     %assign defaultprogresshandler _PROGRESSDEFAULT
  1329                                    %else
  1330                                     %assign defaultprogresshandler _PROGRESSDEFAULTBOOT
  1331                                    %endif
  1332 000000B9 E91600                  	jmp strict near . %+ defaultprogresshandler %+ _step
  1333                                  .handler: equ $ - 2		; SMC, patched to dispatch to handler
  1334                                  .base:
  1335                                   %endif
  1336                                  
  1337                                  .2_step:
  1338 000000BC BAE803                  	mov dx, 1000
  1339 000000BF F7E2                    	mul dx			; dx:ax = counter times 1000
  1340                                  %if _PROGRESSAMOUNT
  1341 000000C1 B90100                  	mov cx, _PROGRESSAMOUNT
  1342 000000C4 F7F1                    	div cx			; ax = 0..1000
  1343                                  %else
  1344                                   %warning Progress amount is zero
  1345                                  	mov ax, 1
  1346                                  %endif
  1347                                  
  1348                                  %if 0
  1349                                  	cmp word [.display], ax
  1350                                  	je .ret
  1351                                  	mov word [.display], ax
  1352                                  %endif
  1353 000000C6 BE[EB01]                	mov si, .message_number_end - 1
  1354 000000C9 E80E00                  	call .format_percentage
  1355                                  
  1356 000000CC BE[E401]                	mov si, .message
  1357 000000CF E85201                  	call disp_error
  1358                                  
  1359                                  .ret:
  1360 000000D2 58                      	pop ax
  1361                                  .ret_ax:
  1362 000000D3 5F                      	pop di
  1363 000000D4 5E                      	pop si
  1364 000000D5 1F                      	pop ds
  1365 000000D6 5A                      	pop dx
  1366 000000D7 59                      	pop cx
  1367 000000D8 5B                      	pop bx
  1368                                  .retn:
  1369 000000D9 C3                      	retn
  1370                                  
  1371                                  
  1372                                  .format_percentage:
  1373 000000DA BB0200                  	mov bx, 2		; skip dot
  1374                                  
  1375 000000DD B90A00                  	mov cx, 10		; base
  1376                                  @@:
  1377 000000E0 31D2                    	xor dx, dx		; dx:ax = current number
  1378 000000E2 F7F1                    	div cx			; dl = remainder, ax = next number
  1379 000000E4 80C230                  	add dl, '0'		; make decit
  1380 000000E7 8814                    	mov byte [si], dl	; store
  1381 000000E9 29DE                    	sub si, bx		; -> next digit slot
  1382 000000EB 80FB02                  	cmp bl, 2		; did we just store fractional ?
  1383 000000EE B301                    	mov bl, 1		; ! next iteration do not skip dot
  1384 000000F0 74EE                    	je @B			; yes, loop always -->
  1385 000000F2 85C0                    	test ax, ax		; nonzero left ?
  1386 000000F4 75EA                    	jnz @B			; yes, loop if so -->
  1387 000000F6 C3                      	retn
  1388                                  
  1389                                  
  1390                                   %if _PROGRESSMULTI
  1391                                  .0_step: equ .ret
  1392                                  
  1393                                  .1_step:
  1394 000000F7 58                      	pop ax
  1395 000000F8 E83701                  	call disp_al
  1396 000000FB EBD6                    	jmp .ret_ax
  1397                                  
  1398                                  .3_step:
  1399 000000FD BB4B00                  	mov bx, 80 - 1 - 4
  1400 00000100 F7E3                    	mul bx			; dx:ax = counter times 75
  1401                                  %if _PROGRESSAMOUNT
  1402 00000102 B90100                  	mov cx, _PROGRESSAMOUNT
  1403 00000105 F7F1                    	div cx			; ax = 0..75
  1404                                  %else
  1405                                   %warning Progress amount is zero
  1406                                  	mov ax, 1
  1407                                  %endif
  1408                                  
  1409 00000107 3906[E001]              	cmp word [.display], ax
  1410 0000010B 74C5                    	je .ret
  1411 0000010D A3[E001]                	mov word [.display], ax
  1412                                  
  1413 00000110 50                      	push ax
  1414 00000111 91                      	xchg cx, ax
  1415 00000112 B00D                    	mov al, 13
  1416 00000114 E81B01                  	call disp_al
  1417 00000117 B05B                    	mov al, '['
  1418 00000119 E81601                  	call disp_al
  1419 0000011C B020                    	mov al, 32
  1420 0000011E E81101                  	call disp_al
  1421                                  
  1422 00000121 B023                    	mov al, '#'
  1423 00000123 E305                    	jcxz @FF
  1424                                  @@:
  1425 00000125 E80A01                  	call disp_al
  1426 00000128 E2FB                    	loop @B
  1427                                  @@:
  1428                                  
  1429 0000012A B020                    	mov al, 32
  1430 0000012C 59                      	pop cx
  1431 0000012D 29CB                    	sub bx, cx
  1432 0000012F 7607                    	jbe @FF
  1433 00000131 89D9                    	mov cx, bx
  1434                                  @@:
  1435 00000133 E8FC00                  	call disp_al
  1436 00000136 E2FB                    	loop @B
  1437                                  @@:
  1438                                  
  1439                                  .m4done:
  1440 00000138 E8F700                  	call disp_al
  1441 0000013B B05D                    	mov al, ']'
  1442 0000013D E8F200                  	call disp_al
  1443 00000140 EB90                    	jmp .ret
  1444                                  
  1445                                  
  1446                                  .4_step:
  1447 00000142 50                      	push ax
  1448                                  
  1449 00000143 BAE803                  	mov dx, 1000
  1450 00000146 F7E2                    	mul dx			; dx:ax = counter times 1000
  1451                                  %if _PROGRESSAMOUNT
  1452 00000148 B90100                  	mov cx, _PROGRESSAMOUNT
  1453 0000014B F7F1                    	div cx			; ax = 0..1000
  1454                                  %else
  1455                                   %warning Progress amount is zero
  1456                                  	mov ax, 1
  1457                                  %endif
  1458                                  
  1459 0000014D BE[F501]                	mov si, .message_4_number_end - 1
  1460 00000150 E887FF                  	call .format_percentage
  1461                                  
  1462 00000153 58                      	pop ax
  1463 00000154 BB4B00                  	mov bx, 80 - 1 - 4
  1464 00000157 F7E3                    	mul bx			; dx:ax = counter times 75
  1465                                  %if _PROGRESSAMOUNT
  1466 00000159 B90100                  	mov cx, _PROGRESSAMOUNT
  1467 0000015C F7F1                    	div cx			; ax = 0..75
  1468                                  %else
  1469                                   %warning Progress amount is zero
  1470                                  	mov ax, 1
  1471                                  %endif
  1472                                  
  1473 0000015E 50                      	push ax
  1474 0000015F 91                      	xchg cx, ax
  1475 00000160 B00D                    	mov al, 13
  1476 00000162 E8CD00                  	call disp_al
  1477 00000165 B05B                    	mov al, '['
  1478 00000167 E8C800                  	call disp_al
  1479 0000016A B020                    	mov al, 32
  1480 0000016C E8C300                  	call disp_al
  1481                                  
  1482 0000016F 31FF                    	xor di, di
  1483 00000171 BA2200                  	mov dx, (80 - 1 - 4 - .message_4_length) / 2
  1484 00000174 803E[F101]20            	cmp byte [.message_4_number], 32
  1485 00000179 7401                    	je @F
  1486 0000017B 47                      	inc di
  1487                                  @@:
  1488 0000017C 29FA                    	sub dx, di
  1489                                  
  1490 0000017E B023                    	mov al, '#'
  1491 00000180 E306                    	jcxz @FF
  1492                                  @@:
  1493 00000182 E8AD00                  	call disp_al
  1494 00000185 4A                      	dec dx
  1495 00000186 E0FA                    	loopnz @B
  1496                                  @@:
  1497                                  
  1498 00000188 85D2                    	test dx, dx
  1499 0000018A 752E                    	jnz .notyetm4
  1500                                  
  1501 0000018C BE[F101]                	mov si, .message_4
  1502 0000018F 29FE                    	sub si, di
  1503 00000191 E89000                  	call disp_error
  1504                                  
  1505 00000194 29F9                    	sub cx, di
  1506 00000196 760E                    	jbe @FF
  1507 00000198 83E907                  	sub cx, .message_4_length
  1508 0000019B 7609                    	jbe @FF
  1509 0000019D 89CA                    	mov dx, cx
  1510 0000019F B023                    	mov al, '#'
  1511                                  @@:
  1512 000001A1 E88E00                  	call disp_al
  1513 000001A4 E2FB                    	loop @B
  1514                                  @@:
  1515 000001A6 59                      	pop cx
  1516 000001A7 B020                    	mov al, 32
  1517 000001A9 83EB29                  	sub bx, (80 - 1 - 4 - .message_4_length) / 2 + .message_4_length
  1518 000001AC 29D3                    	sub bx, dx
  1519 000001AE 7607                    	jbe @FF
  1520 000001B0 89D9                    	mov cx, bx
  1521                                  @@:
  1522 000001B2 E87D00                  	call disp_al
  1523 000001B5 E2FB                    	loop @B
  1524                                  @@:
  1525 000001B7 E97EFF                  	jmp .m4done
  1526                                  
  1527                                  .notyetm4:
  1528 000001BA B020                    	mov al, 32
  1529 000001BC 59                      	pop cx
  1530 000001BD 29CB                    	sub bx, cx
  1531 000001BF 7608                    	jbe @FF
  1532 000001C1 89D9                    	mov cx, bx
  1533                                  @@:
  1534 000001C3 E86C00                  	call disp_al
  1535 000001C6 4A                      	dec dx
  1536 000001C7 E0FA                    	loopnz @B
  1537                                  @@:
  1538                                  
  1539 000001C9 BE[F101]                	mov si, .message_4
  1540 000001CC 29FE                    	sub si, di
  1541 000001CE E85300                  	call disp_error
  1542                                  
  1543 000001D1 B020                    	mov al, 32
  1544 000001D3 29F9                    	sub cx, di
  1545 000001D5 83E907                  	sub cx, .message_4_length
  1546                                  	; jbe @FF
  1547                                  @@:
  1548 000001D8 E85700                  	call disp_al
  1549 000001DB E2FB                    	loop @B
  1550                                  @@:
  1551 000001DD E958FF                  	jmp .m4done
  1552                                  %endif
  1553                                  
  1554                                  	align 2, db 0
  1555                                   %if _PROGRESSMULTI
  1556 000001E0 FFFF                    .display:	dw -1
  1557                                   %endif
  1558 000001E2 0000                    .counter:	dw 0
  1559 000001E4 0D5B20                  .message:	db 13,"[ "
  1560 000001E7 2020312E30              .message_number:db "  1.0"	; enough digit slots for 100.0%,
  1561                                  				;  blanks are pre-init until written
  1562                                  .message_number_end:
  1563 000001EC 25205D00                		db "% ]",0
  1564                                   %if _PROGRESSMULTI
  1565 000001F0 20                      		db " "
  1566                                  .message_4:
  1567 000001F1 2020312E30              .message_4_number:db "  1.0"	; enough digit slots for 100.0%,
  1568                                  				;  blanks are pre-init until written
  1569                                  .message_4_number_end:
  1570 000001F6 252000                  		db "% ",0
  1571                                  .message_4_length: equ $ - 1 - .message_4
  1572                                   %endif
  1573                                  %endif
  1574                                  
  1575                                  
  1576                                  %if _DEVICE
  1577 000001F9 90<rep 7h>              	align 16
  1578                                  device_header_copy:
  1579                                  .:
  1580                                  .next:
  1581 00000200 FFFFFFFF                	dd -1				; already initialised
  1582                                  .attributes:
  1583 00000204 0088                    	dw _DEVICE_ATTRIBUTE
  1584                                  .strategy:
  1585 00000206 1200                    	dw .strategy_entry - .		; -> strategy entry
  1586                                  .interrupt:
  1587 00000208 1600                    	dw .interrupt_entry - .		; -> interrupt entry
  1588                                  .name:
  1589 0000020A 4C44454255472424        	fill 8, 32, db _DEVICE_NAME
  1590                                  
  1591                                  .strategy_entry:
  1592 00000212 EB0690<rep 2h>          	fill 4, 90h, jmp .set_error	; rel8 or rel16 jump, not minus .
  1593                                  .interrupt_entry:
  1594 00000216 CB90<rep 3h>            	fill 4, 90h, retf
  1595                                  
  1596                                  .set_error:
  1597 0000021A 26C747030381            	mov word [es:bx + 3], 8103h	; set error, done, invalid command
  1598 00000220 CB                      	retf
  1599                                  .length: equ $ - .
  1600                                  %endif
  1601                                  
  1602                                  
  1603                                  disp_error.loop:
  1604 00000221 E80E00                  	call disp_al
  1605                                  disp_error:
  1606 00000224 AC                      	lodsb
  1607 00000225 84C0                    	test al, al
  1608 00000227 75F8                    	jnz .loop
  1609 00000229 C3                      	retn
  1610                                  
  1611                                  %if _DEBUG0 || _DEBUG1 || _DEBUG2 || _DEBUG3
  1612                                  disp_ax_hex:			; ax
  1613                                  		xchg al,ah
  1614                                  		call disp_al_hex		; display former ah
  1615                                  		xchg al,ah			;  and fall trough for al
  1616                                  disp_al_hex:			; al
  1617                                  		push cx
  1618                                  		mov cl,4
  1619                                  		ror al,cl
  1620                                  		call disp_al_lownibble_hex	; display former high-nibble
  1621                                  		rol al,cl
  1622                                  		pop cx
  1623                                  						;  and fall trough for low-nibble
  1624                                  disp_al_lownibble_hex:
  1625                                  		push ax			 ; save ax for call return
  1626                                  		and al,00001111b		; high nibble must be zero
  1627                                  		add al,'0'			; if number is 0-9, now it's the correct character
  1628                                  		cmp al,'9'
  1629                                  		jna .decimalnum		 ; if we get decimal number with this, ok -->
  1630                                  		add al,7			;  otherwise, add 7 and we are inside our alphabet
  1631                                   .decimalnum:
  1632                                  		call disp_al
  1633                                  		pop ax
  1634                                  		retn
  1635                                  %endif
  1636                                  
  1637                                  
  1638                                  disp_al_for_progress:
  1639                                  %if _PROGRESS && _PROGRESSMULTI
  1640 0000022A 2E833E[BA00]16          	cmp word [cs:step_progress.handler], step_progress.0_step - step_progress.base
  1641 00000230 741B                    	je disp_al.retn
  1642                                  %endif
  1643                                  disp_al:
  1644                                  %if _TEST_PROGRAM
  1645 00000232 C3                      	retn
  1646                                  %else
  1647                                  	push ax
  1648                                  %endif
  1649                                  %if _BOOTLDR
  1650 00000233 53                      	push bx
  1651 00000234 55                      	push bp
  1652 00000235 B40E                    	mov ah, 0Eh
  1653 00000237 BB0700                  	mov bx, 7
  1654                                  %endif
  1655                                  %if _IMAGE_EXE || _DEVICE
  1656 0000023A 52                      	push dx
  1657                                  
  1658                                  %if _BOOTLDR
  1659 0000023B F646FE03                	test byte [bp - 2], 1 | 2
  1660 0000023F 7406                    	jz .display_kernel_mode
  1661                                  %endif
  1662                                  
  1663 00000241 92                      	xchg dx, ax				; dl = input al
  1664 00000242 B402                    	mov ah, 02h
  1665 00000244 CD21                    	int 21h
  1666                                  %if _BOOTLDR
  1667 00000246 A9                      	db __TEST_IMM16				; (skip int)
  1668                                  %endif
  1669                                  
  1670                                  .display_kernel_mode:
  1671                                  %endif
  1672                                  %if _BOOTLDR
  1673 00000247 CD10                    	int 10h
  1674                                  %endif
  1675                                  %if _IMAGE_EXE
  1676 00000249 5A                      	pop dx
  1677                                  %endif
  1678                                  %if _BOOTLDR
  1679 0000024A 5D                      	pop bp
  1680 0000024B 5B                      	pop bx
  1681                                  %endif
  1682 0000024C 58                      	pop ax
  1683                                  .retn:
  1684                                  disp_error.ret:
  1685 0000024D C3                      	retn
  1686                                  
  1687                                  
  1688                                  msg:
  1689                                  %if _DEBUG0
  1690                                  .error_begin:	db "Load error: Decompression failure, code ",0
  1691                                  .error_end:	db "h.",13,10,0
  1692                                  %else
  1693 0000024E 4C6F6164206572726F-     .error:		db "Load error: Decompression failure.",13,10,0
  1693 00000257 723A204465636F6D70-
  1693 00000260 72657373696F6E2066-
  1693 00000269 61696C7572652E0D0A-
  1693 00000272 00                 
  1694                                  %endif
  1695                                  
  1696                                  
  1697                                  %assign NEED_NORMALISE_POINTER_WITH_DISPLACEMENT 1
  1698                                  %assign NEED_NORMALISE_BOTH_POINTERS 1
  1699                                  %assign CHECK_POINTERS_VARIABLE_SRC 1
  1700                                  %assign CHECK_POINTERS_VARIABLE_DST 1
  1701                                  
  1702                                  		; Specific depacker's file is included within label msg.
  1703                                  		; In the file, lframe is used and lleave ctx is not used.
  1704                                  
  1705                                  %if _BRIEFLZ
  1706                                  	%include "brieflz.asm"
  1707                                  %endif
  1708                                  
  1709                                  
  1710                                  %if _LZ4
  1711                                  	%include "lz4.asm"
     1                              <1> 
     2                              <1> %if 0
     3                              <1> 
     4                              <1> 8086 Assembly lDOS iniload payload LZ4 depacker
     5                              <1>  by E. C. Masloch, 2018
     6                              <1> 
     7                              <1> Usage of the works is permitted provided that this
     8                              <1> instrument is retained with the works, so that any entity
     9                              <1> that uses the works is notified of this instrument.
    10                              <1> 
    11                              <1> DISCLAIMER: THE WORKS ARE WITHOUT WARRANTY.
    12                              <1> 
    13                              <1> %endif
    14                              <1> 
    15                              <1> 
    16                              <1> lz4_flg_version_mask:	equ 1100_0000b
    17                              <1> lz4_flg_version:	equ 0100_0000b
    18                              <1> lz4_flg_b_indep:	equ 0010_0000b
    19                              <1> lz4_flg_b_checksum:	equ 0001_0000b
    20                              <1> lz4_flg_c_size:		equ 0000_1000b
    21                              <1> lz4_flg_c_checksum:	equ 0000_0100b
    22                              <1> lz4_flg_reserved:	equ 0000_0010b
    23                              <1> lz4_flg_dict_id:	equ 0000_0001b
    24                              <1> 
    25                              <1> lz4_bd_reserved_mask:	equ 1000_1111b
    26                              <1> 		; BD block max size is not used by us
    27                              <1> 
    28                              <1> 
    29                              <1> 		; INP:	ds:si -> source
    30                              <1> 		;	dx:cx = length of source
    31                              <1> 		;	es:di -> destination (below source)
    32                              <1> 		;	if _IMAGE_EXE:
    33                              <1> 		;	 bx = EXE mode flag (bit 0)
    34                              <1> 		;	else:
    35                              <1> 		;	 bx = 0
    36                              <1> 		;	if _PAYLOAD_KERNEL_MAX_PARAS:
    37                              <1> 		;	 ax = maximum amount in paragraphs of destination needed
    38                              <1> 		;	 (-1 if full source should be decompressed)
    39                              <1> 		;	else:
    40                              <1> 		;	 ax = -1
    41                              <1> 		; OUT:	NC if success
    42                              <1> 		;	CY if error,
    43                              <1> 		;	 bx = ?errordata (if _DEBUG0)
    44                              <1> 		; CHG:	ax, (bx), cx, dx, es, ds, si, di
    45                              <1> 		; STT:	UP
    46                              <1> 		;
    47                              <1> 		; Note:	The destination reaches up to below the source.
    48                              <1> 		; Note:	The input pointers need not be normalised yet.
    49                              <1> 		;	 Normalised means that the offset part is below 16.
    50                              <1> depack:
    51                              <1> 	lframe near
    52                              <1> %if _IMAGE_EXE || _DEBUG0 || _COUNTER
    53 00000273 5589E5              <1> 	lenter early
    54                              <1>  %if _IMAGE_EXE
    55                              <1> 	lvar word,	exemode	; must be bp - 2!
    56 00000276 53                  <1> 	 push bx
    57                              <1>   %if ?exemode != -2
    58                              <1>    %error exemode variable must be directly below bp
    59                              <1>   %endif
    60                              <1>  %endif
    61                              <1>  %if _DEBUG0 || _COUNTER
    62 00000277 31DB                <1> 	xor bx, bx
    63                              <1>  %endif
    64                              <1>  %if _DEBUG0
    65                              <1> 	lvar word,	errordata
    66                              <1> 	 push bx
    67                              <1>  %endif
    68                              <1> %endif
    69                              <1> 	lvar word,	token_and_counter
    70                              <1> 	lequ ?token_and_counter, token
    71                              <1> 	lequ ?token_and_counter + 1, counter
    72                              <1> %if _COUNTER
    73 00000279 53                  <1> 	 push bx		; initialise counter (high byte) to zero
    74                              <1> %endif
    75                              <1> 	lvar dword,	src_behind_block
    76                              <1> 	lvar word,	frame_descriptor_flg_and_bd
    77 0000027A 8D66F6              <1> 	lenter
    78                              <1> 
    79 0000027D E8A302              <1> 	call normalise_both_pointers
    80                              <1> 
    81                              <1> 	lvar dword,	src
    82 00000280 1E                  <1> 	 push ds
    83 00000281 56                  <1> 	 push si
    84                              <1> 	lvar dword,	original_dst
    85 00000282 06                  <1> 	 push es
    86 00000283 57                  <1> 	 push di
    87                              <1> 	lvar dword,	dst
    88 00000284 06                  <1> 	 push es
    89 00000285 57                  <1> 	 push di
    90                              <1> 	lvar dword,	length_of_source
    91 00000286 52                  <1> 	 push dx
    92 00000287 51                  <1> 	 push cx
    93                              <1> 
    94                              <1> %if _PAYLOAD_KERNEL_MAX_PARAS
    95                              <1> 	cmp ax, -1		; no maximum specified ?
    96                              <1> 	je @FF			; retain -1 in ax -->
    97                              <1> 	test di, di		; do we need an additional paragraph ?
    98                              <1> 	jz @F			; no -->
    99                              <1> 	inc ax			; es + ax => paragraph after necessary part
   100                              <1> @@:
   101                              <1> 	mov dx, es
   102                              <1> 	add ax, dx		; => paragraph after necessary part
   103                              <1> 		; If the normalised destination pointer's segment grows
   104                              <1> 		;  to this segment then enough has been decompressed.
   105                              <1> d0	mov byte [bp + ?errordata], 40h
   106                              <1> 	jc .error		; should not carry
   107                              <1> @@:
   108                              <1> 	lvar word,	dst_max_segment
   109                              <1> 	 push ax
   110                              <1> %endif
   111                              <1> 
   112 00000288 1E                  <1> 	 push ds
   113 00000289 56                  <1> 	 push si
   114 0000028A E8F602              <1> 	call pointer_to_linear
   115                              <1> 
   116 0000028D 89D3                <1> 	mov bx, dx
   117 0000028F 91                  <1> 	xchg cx, ax		; bx:cx = source linear
   118                              <1> 
   119 00000290 06                  <1> 	 push es
   120 00000291 57                  <1> 	 push di
   121 00000292 E8EE02              <1> 	call pointer_to_linear
   122                              <1> 
   123                              <1> %if _ALLOW_OVERLAPPING
   124 00000295 034EE6              <1> 	add cx, word [bp + ?length_of_source]
   125 00000298 135EE8              <1> 	adc bx, word [bp + ?length_of_source + 2]
   126                              <1> 		; In case of allowing overlapping source and destination,
   127                              <1> 		;  the ?length_of_destination variable is set to
   128                              <1> 		;  ?src + ?length_of_source - ?dst, allowing to write to
   129                              <1> 		;  all of the source buffer (with the checks already in place
   130                              <1> 		;  from the default handling). Additional checks are done by
   131                              <1> 		;  calling check_pointers_not_overlapping. This is done after
   132                              <1> 		;  every change of ?dst to verify that the write pointer stays
   133                              <1> 		;  below-or-equal the read pointer.
   134                              <1> 		; This means the remaining source data may be corrupted by a
   135                              <1> 		;  write, but nothing after the source data is written to,
   136                              <1> 		;  so the error handling (in INIT1 after the data) still works.
   137                              <1> %endif
   138                              <1> 
   139 0000029B 29C1                <1> 	sub cx, ax
   140 0000029D 19D3                <1> 	sbb bx, dx		; bx:cx = source linear - destination linear
   141                              <1> 
   142                              <1> 	lvar dword,	length_of_destination
   143 0000029F 53                  <1> 	 push bx		; push into [bp + ?length_of_destination + 2]
   144 000002A0 51                  <1> 	 push cx		; push into [bp + ?length_of_destination]
   145                              <1> 
   146                              <1> %if _ALLOW_OVERLAPPING
   147 000002A1 E86302              <1> 	call check_pointers_not_overlapping
   148                              <1> 		; Note:	We initially check here that the write pointer is
   149                              <1> 		;	 low enough, ie below-or-equal the read pointer.
   150                              <1> 		;	 Doing this check here (as well as after any
   151                              <1> 		;	 copied match) allows us to drop the check done
   152                              <1> 		;	 after moving literal bytes.
   153                              <1> d0	mov byte [bp + ?errordata], 1Bh
   154 000002A4 720D                <1> 	jc .error
   155                              <1> %endif
   156                              <1> 
   157                              <1> .loop_frame:
   158 000002A6 31C9                <1> 	xor cx, cx
   159 000002A8 394EE8              <1> 	cmp word [bp + ?length_of_source + 2], cx
   160 000002AB 7517                <1> 	jne @F
   161 000002AD 394EE6              <1> 	cmp word [bp + ?length_of_source], cx
   162 000002B0 7512                <1> 	jne @F
   163                              <1> 
   164                              <1> .end:
   165 000002B2 A8                  <1> 	db __TEST_IMM8			; (NC)
   166                              <1> .error:
   167 000002B3 F9                  <1> 	stc
   168                              <1> 
   169                              <1> %if _COUNTER
   170 000002B4 9F                  <1> 	lahf
   171 000002B5 B00D                <1> 	mov al, 13
   172 000002B7 E870FF              <1> 	call disp_al_for_progress
   173 000002BA B00A                <1> 	mov al, 10
   174 000002BC E86BFF              <1> 	call disp_al_for_progress
   175 000002BF 9E                  <1> 	sahf
   176                              <1> %endif
   177                              <1> d0	mov bx, word [bp + ?errordata]
   178 000002C0 89EC5D              <1> 	lleave code
   179 000002C3 C3                  <1> 	lret
   180                              <1> 
   181                              <1> 
   182                              <1> @@:
   183 000002C4 B104                <1> 	mov cl, 4
   184 000002C6 E81B02              <1> 	call read_le_data_src
   185                              <1> d0	mov byte [bp + ?errordata], 1
   186 000002C9 72E8                <1> 	jc .error
   187                              <1> 
   188 000002CB 81FA4D18            <1> 	cmp dx, 184Dh			; signature match ?
   189                              <1> d0	mov byte [bp + ?errordata], 2
   190 000002CF 75E2                <1> 	jne .error			; no -->
   191 000002D1 3D0422              <1> 	cmp ax, 2204h			; usual frame ?
   192 000002D4 7427                <1> 	je @F				; yes -->
   193                              <1> 
   194 000002D6 24F0                <1> 	and al,  ~0Fh
   195 000002D8 3D502A              <1> 	cmp ax, 2A50h			; skippable frame ?
   196                              <1> d0	mov byte [bp + ?errordata], 3
   197 000002DB 75D6                <1> 	jne .error			; no -->
   198                              <1> 
   199                              <1> 		; cx = 4 still here
   200 000002DD E80402              <1> 	call read_le_data_src		; read length of skippable frame
   201                              <1> d0	mov byte [bp + ?errordata], 4
   202 000002E0 72D1                <1> 	jc .error
   203                              <1> 
   204 000002E2 1E                  <1> 	 push ds
   205 000002E3 56                  <1> 	 push si
   206 000002E4 89C1                <1> 	mov cx, ax
   207 000002E6 89D3                <1> 	mov bx, dx			; displacement = skippable frame length
   208 000002E8 E86902              <1> 	call normalise_pointer_with_displacement_bxcx
   209 000002EB 8F46F2              <1> 	 pop word [bp + ?src]
   210 000002EE 8F46F4              <1> 	 pop word [bp + ?src + 2]	; ?src += length
   211                              <1> d0	mov byte [bp + ?errordata], 1Dh
   212 000002F1 72C0                <1> 	jc .error
   213                              <1> 
   214 000002F3 2946E6              <1> 	sub word [bp + ?length_of_source], ax
   215 000002F6 1956E8              <1> 	sbb word [bp + ?length_of_source + 2], dx
   216                              <1> 					; ?length_of_source -= length
   217                              <1> d0	mov byte [bp + ?errordata], 5
   218 000002F9 72B8                <1> 	jc .error
   219 000002FB EBA9                <1> 	jmp .loop_frame
   220                              <1> 
   221                              <1> 
   222                              <1> @@:
   223 000002FD B102                <1> 	mov cl, 2
   224 000002FF E8E201              <1> 	call read_le_data_src		; read FLG (low byte), BD (high byte)
   225                              <1> d0	mov byte [bp + ?errordata], 6
   226                              <1> .error_CY_1:
   227 00000302 72AF                <1> 	jc .error
   228                              <1> 
   229 00000304 8946F6              <1> 	mov word [bp + ?frame_descriptor_flg_and_bd], ax
   230 00000307 89C3                <1> 	mov bx, ax
   231 00000309 80E3C0              <1> 	and bl, lz4_flg_version_mask
   232 0000030C 80FB40              <1> 	cmp bl, lz4_flg_version		; version correct ?
   233                              <1> d0	mov byte [bp + ?errordata], 7
   234                              <1> .error_NZ_1:
   235 0000030F 75A2                <1> 	jne .error			; no -->
   236                              <1> 
   237 00000311 A9038F              <1> 	test ax, (lz4_bd_reserved_mask << 8) 		 | lz4_flg_reserved | lz4_flg_dict_id	; unsupported bits set?
   239                              <1> d0	mov byte [bp + ?errordata], 8
   240 00000314 75F9                <1> 	jnz .error_NZ_1			; yes -->
   241                              <1> 
   242                              <1> 		; not checked: lz4_flg_b_indep (linked is always supported),
   243                              <1> 		;  lz4_flg_b_checksum, lz4_flg_c_checksum (both checked later)
   244                              <1> 
   245 00000316 A808                <1> 	test al, lz4_flg_c_size		; content size field present ?
   246 00000318 7407                <1> 	jz @F				; no -->
   247 0000031A B108                <1> 	mov cl, 8
   248 0000031C E8C501              <1> 	call read_le_data_src		; (skip content size field)
   249                              <1> d0	mov byte [bp + ?errordata], 9
   250 0000031F 72E1                <1> 	jc .error_CY_1
   251                              <1> @@:
   252 00000321 B101                <1> 	mov cl, 1
   253 00000323 E8BE01              <1> 	call read_le_data_src		; (skip header checksum field)
   254                              <1> d0	mov byte [bp + ?errordata], 0Ah
   255 00000326 72DA                <1> 	jc .error_CY_1
   256                              <1> 
   257                              <1> .loop_block:
   258                              <1> %if _COUNTER && !_PROGRESS
   259                              <1> 	mov al, '#'
   260                              <1> 	call disp_al
   261                              <1> %endif
   262 00000328 B90400              <1> 	mov cx, 4
   263 0000032B E8B601              <1> 	call read_le_data_src		; read block size field
   264                              <1> 					; leaves ds:si -> source
   265                              <1> d0	mov byte [bp + ?errordata], 0Bh
   266 0000032E 72D2                <1> 	jc .error_CY_1
   267                              <1> 
   268 00000330 85D2                <1> 	test dx, dx			; nonzero ?
   269 00000332 7512                <1> 	jnz @FF
   270 00000334 85C0                <1> 	test ax, ax
   271 00000336 750E                <1> 	jnz @FF				; yes -->
   272                              <1> 
   273 00000338 F646F604            <1> 	test byte [bp + ?frame_descriptor_flg_and_bd], lz4_flg_c_checksum
   274 0000033C 7405                <1> 	jz @F
   275                              <1> 		; cx = 4 already
   276 0000033E E8A301              <1> 	call read_le_data_src		; (skip c checksum field)
   277                              <1> d0	mov byte [bp + ?errordata], 0Ch
   278                              <1> .error_CY_2:
   279 00000341 72BF                <1> 	jc .error_CY_1
   280                              <1> @@:
   281 00000343 E960FF              <1> 	jmp .loop_frame			; next frame, if any -->
   282                              <1> 
   283                              <1> 
   284                              <1> @@:
   285 00000346 1E                  <1> 	 push ds
   286 00000347 56                  <1> 	 push si			; -> source
   287 00000348 89D3                <1> 	mov bx, dx
   288 0000034A 80E77F              <1> 	and bh, ~80h			; clear "uncompressed" flag
   289 0000034D 89C1                <1> 	mov cx, ax			; bx:cx = block size
   290 0000034F E80202              <1> 	call normalise_pointer_with_displacement_bxcx
   291                              <1> 					; -> source after block
   292 00000352 8F46F8              <1> 	 pop word [bp + ?src_behind_block]
   293 00000355 8F46FA              <1> 	 pop word [bp + ?src_behind_block + 2]
   294                              <1> d0	mov byte [bp + ?errordata], 1Eh
   295 00000358 7303E956FF          <1> 	jc .error
   296 0000035D F6C680              <1> 	test dh, 80h			; is it uncompressed ?
   297 00000360 7448                <1> 	jz .compressed			; no -->
   298                              <1> 
   299                              <1> .uncompressed:
   300 00000362 87DA                <1> 	xchg bx, dx			; get size without "uncompressed" flag
   301 00000364 E8FC00              <1> 	call lz4_copy_literal_or_uncompressed
   302                              <1> d0	mov byte [bp + ?errordata], 0Dh
   303 00000367 72D8                <1> 	jc .error_CY_2
   304                              <1> 
   305                              <1> .end_block:
   306 00000369 FF76FA              <1> 	 push word [bp + ?src_behind_block + 2]
   307 0000036C FF76F8              <1> 	 push word [bp + ?src_behind_block]
   308 0000036F E81102              <1> 	call pointer_to_linear
   309 00000372 89C1                <1> 	mov cx, ax
   310 00000374 89D3                <1> 	mov bx, dx			; bx:cx = linear ?src_behind_block
   311                              <1> 
   312 00000376 FF76F4              <1> 	 push word [bp + ?src + 2]
   313 00000379 FF76F2              <1> 	 push word [bp + ?src]
   314 0000037C E80402              <1> 	call pointer_to_linear		; dx:ax = linear ?src
   315                              <1> 
   316 0000037F 29C1                <1> 	sub cx, ax
   317 00000381 19D3                <1> 	sbb bx, dx			; ?src_behind_block - ?src
   318                              <1> 					;  = amount to skip
   319                              <1> d0	mov byte [bp + ?errordata], 0Fh
   320 00000383 72BC                <1> 	jc .error_CY_2
   321                              <1> 
   322 00000385 294EE6              <1> 	sub word [bp + ?length_of_source], cx
   323 00000388 195EE8              <1> 	sbb word [bp + ?length_of_source + 2], bx
   324                              <1> d0	mov byte [bp + ?errordata], 10h
   325                              <1> .error_CY_3:
   326 0000038B 72B4                <1> 	jc .error_CY_2			; not enough data to skip -->
   327                              <1> 
   328 0000038D FF76FA              <1> 	push word [bp + ?src_behind_block + 2]
   329 00000390 FF76F8              <1> 	push word [bp + ?src_behind_block]
   330 00000393 8F46F2              <1> 	pop word [bp + ?src]
   331 00000396 8F46F4              <1> 	pop word [bp + ?src + 2]	; set ?src -> behind block
   332                              <1> 
   333 00000399 F646F610            <1> 	test byte [bp + ?frame_descriptor_flg_and_bd], lz4_flg_b_checksum
   334 0000039D 7408                <1> 	jz @F				; no block checksum -->
   335 0000039F B90400              <1> 	mov cx, 4
   336 000003A2 E83F01              <1> 	call read_le_data_src		; (skip block checksum field)
   337                              <1> d0	mov byte [bp + ?errordata], 0Eh
   338 000003A5 72E4                <1> 	jc .error_CY_3
   339                              <1> @@:
   340                              <1> 
   341 000003A7 E97EFF              <1> 	jmp .loop_block
   342                              <1> 
   343                              <1> 
   344                              <1> .compressed:
   345                              <1> .loop_sequence:
   346                              <1> 
   347                              <1> %if _COUNTER
   348 000003AA FE46FD              <1> 	inc byte [bp + ?counter]
   349 000003AD F646FD1F            <1> 	test byte [bp + ?counter], _COUNTER - 1
   350 000003B1 7505                <1> 	jnz @F
   351 000003B3 B02E                <1> 	mov al, '.'
   352 000003B5 E89BFC              <1> 	call disp_al_counter
   353                              <1> @@:
   354                              <1> %endif
   355                              <1> 
   356 000003B8 BB[3604]            <1> 	mov bx, .lodsb
   357 000003BB E8EE00              <1> 	call lz4_func_bx_with_src	; load token
   358                              <1> d0	mov byte [bp + ?errordata], 11h
   359 000003BE 72CB                <1> 	jc .error_CY_3
   360 000003C0 8846FC              <1> 	mov byte [bp + ?token], al
   361                              <1> 
   362 000003C3 B104                <1> 	mov cl, 4
   363 000003C5 D2E8                <1> 	shr al, cl
   364 000003C7 E8F300              <1> 	call lz4_get_length		; dx:cx = length of literals
   365                              <1> d0	mov byte [bp + ?errordata], 12h
   366 000003CA 72BF                <1> 	jc .error_CY_3
   367                              <1> 
   368 000003CC 89C8                <1> 	mov ax, cx			; dx:ax = length of literals (may be 0)
   369 000003CE E89200              <1> 	call lz4_copy_literal_or_uncompressed
   370                              <1> 					; copy over the literals
   371                              <1> d0	mov byte [bp + ?errordata], 13h
   372 000003D1 72B8                <1> 	jc .error_CY_3
   373                              <1> 
   374 000003D3 E87800              <1> 	call lz4_check_behind_block_higher_than_src
   375                              <1> 					; do we have more data in this block ?
   376 000003D6 725B                <1> 	jc .end_sequence		; no -->
   377                              <1> 
   378 000003D8 BB[4204]            <1> 	mov bx, .lodsw
   379 000003DB E8CE00              <1> 	call lz4_func_bx_with_src	; load match offset
   380                              <1> d0	mov byte [bp + ?errordata], 14h
   381                              <1> .error_CY_4:
   382 000003DE 72AB                <1> 	jc .error_CY_3
   383                              <1> 
   384 000003E0 85C0                <1> 	test ax, ax			; offset is 0 ?
   385                              <1> d0	mov byte [bp + ?errordata], 15h
   386 000003E2 F9                  <1> 	stc
   387 000003E3 74F9                <1> 	jz .error_CY_4			; yes, error -->
   388                              <1> 
   389 000003E5 89C1                <1> 	mov cx, ax
   390 000003E7 F7D9                <1> 	neg cx				; -1 .. -65535
   391 000003E9 BBFFFF              <1> 	mov bx, -1			; sign-extend (always negative sense)
   392                              <1> 
   393 000003EC FF76EC              <1> 	 push word [bp + ?dst + 2]
   394 000003EF FF76EA              <1> 	 push word [bp + ?dst]
   395 000003F2 E85F01              <1> 	call normalise_pointer_with_displacement_bxcx
   396 000003F5 5E                  <1> 	 pop si
   397 000003F6 1F                  <1> 	 pop ds				; ds:si -> source of matching
   398                              <1> d0	mov byte [bp + ?errordata], 1Fh
   399 000003F7 7203E9B7FE          <1> 	jnc .error
   400                              <1> 
   401 000003FC 8CD8                <1> 	mov ax, ds			; is source of matching within ?dst ?
   402 000003FE 3946F0              <1> 	cmp word [bp + ?original_dst + 2], ax
   403                              <1> d0	mov byte [bp + ?errordata], 16h
   404                              <1> .error_A_1:
   405 00000401 7603E9ADFE          <1> 	ja .error
   406 00000406 7205                <1> 	jb @F
   407 00000408 3976EE              <1> 	cmp word [bp + ?original_dst], si
   408                              <1> d0	mov byte [bp + ?errordata], 17h
   409 0000040B 77F4                <1> 	ja .error_A_1			; no, ?original_dst is above it -->
   410                              <1> @@:
   411                              <1> 
   412 0000040D 8A46FC              <1> 	mov al, byte [bp + ?token]
   413 00000410 240F                <1> 	and al, 0Fh			; get match length from token
   414 00000412 E8A800              <1> 	call lz4_get_length		; dx:cx = length of match - 4
   415                              <1> d0	mov byte [bp + ?errordata], 18h
   416 00000415 72C7                <1> 	jc .error_CY_4
   417 00000417 89C8                <1> 	mov ax, cx			; dx:ax = length of match - 4
   418 00000419 83C004              <1> 	add ax, 4
   419 0000041C 83D200              <1> 	adc dx, 0			; dx:ax = length of match
   420                              <1> d0	mov byte [bp + ?errordata], 19h
   421 0000041F 72BD                <1> 	jc .error_CY_4
   422 00000421 E85400              <1> 	call lz4_copy_data		; give ?dst -> dest, ds:si -> source
   423                              <1> 		; returns: ?dst incremented, ds:si -> after match source
   424                              <1> d0	mov byte [bp + ?errordata], 1Ah
   425 00000424 72B8                <1> 	jc .error_CY_4
   426                              <1> 
   427                              <1> %if _ALLOW_OVERLAPPING
   428 00000426 E8DE00              <1> 	call check_pointers_not_overlapping
   429                              <1> d0	mov byte [bp + ?errordata], 1Ch
   430 00000429 72B3                <1> 	jc .error_CY_4
   431                              <1> %endif
   432                              <1> 
   433                              <1> .next_sequence:
   434 0000042B E82000              <1> 	call lz4_check_behind_block_higher_than_src
   435 0000042E 7203E977FF          <1> 	jnc .loop_sequence		; process next sequence -->
   436                              <1> 
   437                              <1> .end_sequence:
   438 00000433 E933FF              <1> 	jmp .end_block
   439                              <1> 
   440                              <1> 
   441                              <1> 		; INP:	ds:si -> source data
   442                              <1> 		;	?length_of_source
   443                              <1> 		; OUT:	CY if error (source buffer too small)
   444                              <1> 		;	NC if success,
   445                              <1> 		;	 al = value read
   446                              <1> 		;	 ds:si incremented (NOT normalised)
   447                              <1> 		;	 ?length_of_source decremented
   448                              <1> .lodsb:
   449 00000436 836EE601            <1> 	sub word [bp + ?length_of_source], 1
   450 0000043A 835EE800            <1> 	sbb word [bp + ?length_of_source + 2], 0
   451 0000043E 7201                <1> 	jb .retn
   452 00000440 AC                  <1> 	lodsb
   453                              <1> .retn:
   454 00000441 C3                  <1> 	retn
   455                              <1> 
   456                              <1> 
   457                              <1> 		; INP:	ds:si -> source data
   458                              <1> 		;	?length_of_source
   459                              <1> 		; OUT:	CY if error (source buffer too small)
   460                              <1> 		;	NC if success,
   461                              <1> 		;	 ax = value read
   462                              <1> 		;	 ds:si incremented (NOT normalised)
   463                              <1> 		;	 ?length_of_source decremented
   464                              <1> .lodsw:
   465 00000442 836EE602            <1> 	sub word [bp + ?length_of_source], 2
   466 00000446 835EE800            <1> 	sbb word [bp + ?length_of_source + 2], 0
   467 0000044A 72F5                <1> 	jb .retn
   468 0000044C AD                  <1> 	lodsw
   469 0000044D C3                  <1> 	retn
   470                              <1> 
   471                              <1> 
   472                              <1> 		; INP:	?src_behind_block -> block end (normalised)
   473                              <1> 		;	?src -> current address in source buffer
   474                              <1> 		; OUT:	CY if ?src is >= (above-or-equal) ?src_behind_block
   475                              <1> 		;	NC else (?src is below ?src_behind_block, more data)
   476                              <1> 		; CHG:	ax, dx
   477                              <1> 		;
   478                              <1> 		; Note:	Although ?src should always equal ?src_behind_block
   479                              <1> 		;	 if CY here, we actually check for above-or-equal.
   480                              <1> lz4_check_behind_block_higher_than_src:
   481 0000044E 8B56FA              <1> 	mov dx, word [bp + ?src_behind_block + 2]
   482 00000451 8B46F8              <1> 	mov ax, word [bp + ?src_behind_block]
   483                              <1> 					; dx:ax -> behind block
   484                              <1> 
   485 00000454 3956F4              <1> 	cmp word [bp + ?src + 2], dx	; compare segment
   486 00000457 7708                <1> 	ja .error			; ?src > dx:ax -->
   487 00000459 7205                <1> 	jb .success			; ?src < dx:ax -->
   488 0000045B 3946F2              <1> 	cmp word [bp + ?src], ax	; segment is same, compare offset
   489 0000045E 7301                <1> 	jae .error			; ?src >= dx:ax -->
   490                              <1> 
   491                              <1> .success:
   492 00000460 A8                  <1> 	db __TEST_IMM8			; (NC)
   493                              <1> .error:
   494 00000461 F9                  <1> 	stc
   495 00000462 C3                  <1> 	retn
   496                              <1> 
   497                              <1> 
   498                              <1> 		; INP:	?dst -> destination (normalised)
   499                              <1> 		;	?src -> source (normalised)
   500                              <1> 		;	dx:ax = how long the literal data is (0 is valid)
   501                              <1> 		; OUT:	?dst incremented (normalised)
   502                              <1> 		;	?src incremented (normalised)
   503                              <1> 		;	?length_of_destination shortened
   504                              <1> 		;	?length_of_source shortened
   505                              <1> 		;	CY if error (either buffer too small)
   506                              <1> 		;	NC if success
   507                              <1> 		; CHG:	cx, dx, ax, es, di, ds, si
   508                              <1> lz4_copy_literal_or_uncompressed:
   509 00000463 C576F2              <1> 	lds si, [bp + ?src]
   510                              <1> 
   511 00000466 2946E6              <1> 	sub word [bp + ?length_of_source], ax
   512 00000469 1956E8              <1> 	sbb word [bp + ?length_of_source + 2], dx
   513                              <1> 					; enough data exists ?
   514 0000046C 7209                <1> 	jb .return			; no --> (CY)
   515                              <1> 
   516 0000046E E80700              <1> 	call lz4_copy_data		; CY/NC depending on status
   517                              <1> 
   518 00000471 8976F2              <1> 	mov word [bp + ?src], si
   519 00000474 8C5EF4              <1> 	mov word [bp + ?src + 2], ds
   520                              <1> 					; (CF preserved)
   521                              <1> .return:
   522 00000477 C3                  <1> 	retn
   523                              <1> 
   524                              <1> 
   525                              <1> 		; INP:	?dst -> destination (normalised)
   526                              <1> 		;	ds:si -> source (normalised)
   527                              <1> 		;	dx:ax = how long the data is (0 is valid)
   528                              <1> 		; OUT:	?dst incremented (normalised)
   529                              <1> 		;	ds:si incremented (normalised)
   530                              <1> 		;	?length_of_destination shortened
   531                              <1> 		;	CY if error (buffer too small)
   532                              <1> 		;	NC if success
   533                              <1> 		;	Instead of returning, this may jump to depack.end
   534                              <1> 		;	 if the destination segment grows up to the value
   535                              <1> 		;	 stored in the ?dst_max_segment variable. It is
   536                              <1> 		;	 assumed that this will use the stack frame to leave
   537                              <1> 		;	 the function, therefore discarding any of the stack
   538                              <1> 		;	 contents between the frame and the stack top.
   539                              <1> 		; CHG:	cx, dx, ax, es, di, ds, si
   540                              <1> lz4_copy_data:
   541                              <1> d0	inc byte [bp + ?errordata + 1]
   542 00000478 2946E2              <1> 	sub word [bp + ?length_of_destination], ax
   543 0000047B 1956E4              <1> 	sbb word [bp + ?length_of_destination + 2], dx
   544                              <1> 					; enough space left ?
   545 0000047E 722A                <1> 	jb .error			; no -->
   546                              <1> 
   547 00000480 C47EEA              <1> 	les di, [bp + ?dst]
   548                              <1> .loop:
   549 00000483 B9F0FF              <1> 	mov cx, 64 * 1024 - 16		; block size
   550                              <1> 		; If both pointers are normalised, moving 0FFF0h bytes is
   551                              <1> 		;  valid and results in a maximum offset of 0FFFFh in either.
   552                              <1> 
   553 00000486 85D2                <1> 	test dx, dx			; >= 1_0000h ?
   554 00000488 7508                <1> 	jnz @F				; yes, full block -->
   555 0000048A 85C0                <1> 	test ax, ax			; == 0 ?
   556 0000048C 7415                <1> 	jz .end				; yes, done -->
   557 0000048E 39C8                <1> 	cmp ax, cx			; can move in one (last) block ?
   558 00000490 7607                <1> 	jbe .last			; yes -->
   559                              <1> @@:
   560                              <1> 					; no, move one block and continue
   561 00000492 29C8                <1> 	sub ax, cx
   562 00000494 83DA00              <1> 	sbb dx, 0			; left over remaining
   563 00000497 EB03                <1> 	jmp .copy
   564                              <1> 
   565                              <1> .last:
   566 00000499 91                  <1> 	xchg cx, ax			; cx = remaining length
   567 0000049A 31C0                <1> 	xor ax, ax			; no more remaining
   568                              <1> 
   569                              <1> .copy:
   570 0000049C F3A4                <1> 	rep movsb			; move one block (full or partial)
   571                              <1> 
   572 0000049E E88200              <1> 	call normalise_both_pointers
   573 000004A1 EBE0                <1> 	jmp .loop
   574                              <1> 
   575                              <1> .end:
   576 000004A3 897EEA              <1> 	mov word [bp + ?dst], di
   577 000004A6 8C46EC              <1> 	mov word [bp + ?dst + 2], es
   578                              <1> %if _PAYLOAD_KERNEL_MAX_PARAS
   579                              <1> 	mov ax, es
   580                              <1> 	cmp ax, word [bp + ?dst_max_segment]
   581                              <1> 	jae depack.end
   582                              <1> %endif
   583                              <1> 
   584 000004A9 A8                  <1> 	db __TEST_IMM8			; (NC)
   585                              <1> .error:
   586 000004AA F9                  <1> 	stc
   587 000004AB C3                  <1> 	retn
   588                              <1> 
   589                              <1> 
   590                              <1> 		; INP:	?src -> source data
   591                              <1> 		;	bx = function to call
   592                              <1> 		; OUT:	ds:si = ?src -> behind data (normalised)
   593                              <1> 		;	CY if error (buffer too small)
   594                              <1> 		;	NC if success
   595                              <1> 		;	(CF is passed from what the function at bx returns)
   596                              <1> 		;
   597                              <1> 		; Protocol of function in bx called by this:
   598                              <1> 		; INP:	ds:si -> source data
   599                              <1> 		;	?length_of_source
   600                              <1> 		;	(may take more inputs depending on function)
   601                              <1> 		; OUT:	ds:si incremented (NOT normalised)
   602                              <1> 		;	?length_of_source decremented
   603                              <1> 		;	CY if error (buffer too small or other error)
   604                              <1> 		;	NC if success
   605                              <1> 		;	(may give more outputs depending on function)
   606                              <1> lz4_func_bx_with_src:
   607 000004AC C576F2              <1> 	lds si, [bp + ?src]
   608 000004AF FFD3                <1> 	call bx
   609 000004B1 9C                  <1> 	pushf
   610 000004B2 E87500              <1> 	call normalise_dssi_pointer
   611 000004B5 8976F2              <1> 	mov word [bp + ?src], si
   612 000004B8 8C5EF4              <1> 	mov word [bp + ?src + 2], ds
   613 000004BB 9D                  <1> 	popf
   614 000004BC C3                  <1> 	retn
   615                              <1> 
   616                              <1> 
   617                              <1> 		; INP:	?src -> source data
   618                              <1> 		;	?length_of_source
   619                              <1> 		;	al = first field data (0 to 15)
   620                              <1> 		; OUT:	dx:cx = length value
   621                              <1> 		;	?src incremented
   622                              <1> 		;	?length_of_source decremented
   623                              <1> 		;	CY if error (too high or out of source data)
   624                              <1> 		;	NC if success
   625                              <1> 		; CHG:	ax, bx
   626                              <1> lz4_get_length:
   627 000004BD 1E                  <1> 	push ds
   628 000004BE 56                  <1> 	push si
   629                              <1> 
   630 000004BF 31C9                <1> 	xor cx, cx
   631 000004C1 31D2                <1> 	xor dx, dx			; dx:cx = length field
   632 000004C3 B40F                <1> 	mov ah, 15			; first byte maximum value
   633                              <1> .loop:
   634 000004C5 00C1                <1> 	add cl, al
   635 000004C7 80D500              <1> 	adc ch, 0
   636 000004CA 83D200              <1> 	adc dx, 0			; add in this byte
   637 000004CD 7211                <1> 	jc .error			; if too high -->
   638 000004CF 38E0                <1> 	cmp al, ah			; is this the maximum value ?
   639 000004D1 750C                <1> 	jne .end			; no -->
   640 000004D3 BB[3604]            <1> 	mov bx, depack.lodsb
   641 000004D6 E8D3FF              <1> 	call lz4_func_bx_with_src	; load next byte
   642 000004D9 7205                <1> 	jc .error
   643 000004DB B4FF                <1> 	mov ah, 255			; next maximum value is 255
   644 000004DD EBE6                <1> 	jmp .loop			; -->
   645                              <1> 
   646                              <1> .end:
   647 000004DF A8                  <1> 	db __TEST_IMM8			; (NC)
   648                              <1> .error:
   649 000004E0 F9                  <1> 	stc
   650                              <1> 
   651 000004E1 5E                  <1> 	pop si
   652 000004E2 1F                  <1> 	pop ds
   653 000004E3 C3                  <1> 	retn
   654                              <1> 
   655                              <1> 
   656                              <1> 		; INP:	?src -> source data (normalised)
   657                              <1> 		;	cx = size to read, 1 to 4 or higher (at most 0FFF0h)
   658                              <1> 		;	?length_of_source
   659                              <1> 		; OUT:	If cx >= 4,
   660                              <1> 		;	 dx:ax = value read as little-endian
   661                              <1> 		;	If cx == 3,
   662                              <1> 		;	 dl:ax = value read
   663                              <1> 		;	If cx == 2,
   664                              <1> 		;	 ax = value read
   665                              <1> 		;	If cx == 1,
   666                              <1> 		;	 al = value read
   667                              <1> 		;	If cx == 0,
   668                              <1> 		;	 invalid, CY
   669                              <1> 		;	ds:si = ?src = incremented source (normalised)
   670                              <1> 		;	?length_of_source decremented
   671                              <1> 		;	CY if error (source buffer too small, or cx zero)
   672                              <1> 		;	NC if success
   673                              <1> 		; CHG:	ds, si, dx, ax
   674                              <1> 		;
   675                              <1> 		; Note:	Always reads four bytes at INP:ds:si -> data.
   676                              <1> read_le_data_src:
   677 000004E4 E31F                <1> 	jcxz .error
   678                              <1> 
   679 000004E6 294EE6              <1> 	sub word [bp + ?length_of_source], cx
   680 000004E9 835EE800            <1> 	sbb word [bp + ?length_of_source + 2], 0
   681 000004ED 7216                <1> 	jb .error
   682                              <1> 
   683 000004EF C576F2              <1> 	lds si, [bp + ?src]
   684                              <1> 
   685 000004F2 AD                  <1> 	lodsw
   686 000004F3 92                  <1> 	xchg ax, dx		; dx = low word (first)
   687 000004F4 AD                  <1> 	lodsw			; ax = high word (second)
   688 000004F5 92                  <1> 	xchg ax, dx		; fix order, dx:ax = dword
   689                              <1> 
   690 000004F6 83EE04              <1> 	sub si, 4
   691 000004F9 01CE                <1> 	add si, cx
   692                              <1> 
   693 000004FB E82C00              <1> 	call normalise_dssi_pointer
   694 000004FE 8976F2              <1> 	mov word [bp + ?src], si
   695 00000501 8C5EF4              <1> 	mov word [bp + ?src + 2], ds
   696                              <1> 
   697 00000504 A8                  <1> 	db __TEST_IMM8		; (NC)
   698                              <1> .error:
   699 00000505 F9                  <1> 	stc
   700 00000506 C3                  <1> 	retn
   701                              <1> 
  1712                                  %endif
  1713                                  
  1714                                  
  1715                                  %if _SNAPPY
  1716                                  	%include "snappy.asm"
  1717                                  %endif
  1718                                  
  1719                                  
  1720                                  %if _EXODECR
  1721                                  	%include "exodecr.asm"
  1722                                  %endif
  1723                                  
  1724                                  
  1725                                  %if _X
  1726                                  	%include "x.asm"
  1727                                  %endif
  1728                                  
  1729                                  
  1730                                  %if _HEATSHRINK
  1731                                  	%include "heatshr.asm"
  1732                                  %endif
  1733                                  
  1734                                  
  1735                                  %if _LZD
  1736                                  	%include "lzd.asm"
  1737                                  %endif
  1738                                  
  1739                                  
  1740                                  %if _LZO
  1741                                  	%include "lzo.asm"
  1742                                  %endif
  1743                                  
  1744                                  
  1745                                  %if _LZSA2
  1746                                  	%include "lzsa2.asm"
  1747                                  %endif
  1748                                  
  1749                                  
  1750                                  %if _APL
  1751                                  	%include "apl.asm"
  1752                                  %endif
  1753                                  
  1754                                  
  1755                                  %if _BZP
  1756                                  	%include "bzp.asm"
  1757                                  %endif
  1758                                  
  1759                                  
  1760                                  %if _ZEROCOMP
  1761                                  	%include "zerocomp.asm"
  1762                                  %endif
  1763                                  
  1764                                  
  1765                                  %if _MVCOMP
  1766                                  	%include "mvcomp.asm"
  1767                                  %endif
  1768                                  
  1769                                  
  1770                                  %if _LZEXEDAT
  1771                                  	%include "lzexedat.asm"
  1772                                  %endif
  1773                                  
  1774                                  
  1775                                  %if _ALLOW_OVERLAPPING
  1776                                  		; INP:	?src, ?dst
  1777                                  		; OUT:	CY if error (?src < ?dst)
  1778                                  		;	NC if success
  1779                                  		; CHG:	ax, bx, cx, dx
  1780                                  check_pointers_not_overlapping:
  1781                                  %if CHECK_POINTERS_VARIABLE_DST
  1782 00000507 FF76EC                  	 push word [bp + ?dst + 2]
  1783 0000050A FF76EA                  	 push word [bp + ?dst]
  1784                                  %else
  1785                                  	 push es
  1786                                  	 push di
  1787                                  %endif
  1788 0000050D E87300                  	call pointer_to_linear
  1789                                  
  1790 00000510 91                      	xchg cx, ax
  1791 00000511 87DA                    	xchg bx, dx			; bx:cx = linear ?dst after write
  1792                                  
  1793                                  %if CHECK_POINTERS_VARIABLE_SRC
  1794 00000513 FF76F4                  	 push word [bp + ?src + 2]
  1795 00000516 FF76F2                  	 push word [bp + ?src]
  1796                                  %else
  1797                                  	 push ds
  1798                                  	 push si
  1799                                  %endif
  1800 00000519 E86700                  	call pointer_to_linear		; dx:ax = linear ?src before next read
  1801                                  
  1802 0000051C 39DA                    	cmp dx, bx			; ?src >= ?dst ?
  1803 0000051E 7502                    	jne @F
  1804 00000520 39C8                    	cmp ax, cx
  1805                                  @@:
  1806                                  					; (CY) if error (src < dst)
  1807                                  					; (NC) if no error
  1808 00000522 C3                      	retn
  1809                                  %endif
  1810                                  
  1811                                  	; This leaves the lframe context created within the
  1812                                  	;  specific depacker's file. The above function
  1813                                  	;  check_pointers_not_overlapping uses the frame.
  1814                                  	lleave ctx
  1815                                  
  1816                                  
  1817                                  		; INP:	ds:si = pointer
  1818                                  		;	es:di = pointer
  1819                                  		; OUT:	ds:si normalised
  1820                                  		;	es:di normalised
  1821                                  %if NEED_NORMALISE_BOTH_POINTERS
  1822                                  normalise_both_pointers:
  1823 00000523 06                      	 push es
  1824 00000524 57                      	 push di
  1825 00000525 E80A00                  	call normalise_pointer
  1826 00000528 5F                      	 pop di
  1827 00000529 07                      	 pop es
  1828                                  %endif
  1829                                  
  1830                                  normalise_dssi_pointer:
  1831 0000052A 1E                      	 push ds
  1832 0000052B 56                      	 push si
  1833 0000052C E80300                  	call normalise_pointer
  1834 0000052F 5E                      	 pop si
  1835 00000530 1F                      	 pop ds
  1836 00000531 C3                      	retn
  1837                                  
  1838                                  
  1839                                  		; INP:	word [ss:sp + 2] = segment
  1840                                  		;	word [ss:sp] = offset
  1841                                  		;
  1842                                  		; Note:	Does not work correctly with pointers that point to
  1843                                  		;	 a HMA location. Do not use then!
  1844                                  normalise_pointer:
  1845                                  %if NEED_NORMALISE_POINTER_WITH_DISPLACEMENT
  1846                                  	lframe near
  1847                                  	lpar word,	segment
  1848                                  	lpar word,	offset
  1849                                  	lpar_return
  1850 00000532 5589E5                  	lenter
  1851                                  
  1852 00000535 837E040F                	cmp word [bp + ?offset], 15
  1853 00000539 7617                    	jbe .ret
  1854                                  
  1855 0000053B 53                      	push bx
  1856 0000053C 51                      	push cx
  1857                                  
  1858 0000053D 31DB                    	xor bx, bx
  1859 0000053F 31C9                    	xor cx, cx
  1860 00000541 FF7606                  	 push word [bp + ?segment]
  1861 00000544 FF7604                  	 push word [bp + ?offset]
  1862 00000547 E80A00                  	call normalise_pointer_with_displacement_bxcx
  1863 0000054A 8F4604                  	 pop word [bp + ?offset]
  1864 0000054D 8F4606                  	 pop word [bp + ?segment]
  1865                                  
  1866 00000550 59                      	pop cx
  1867 00000551 5B                      	pop bx
  1868                                  .ret:
  1869 00000552 5D                      	lleave
  1870 00000553 C3                      	lret
  1871                                  
  1872                                  
  1873                                  		; INP:	word [ss:sp + 2] = segment
  1874                                  		;	word [ss:sp] = offset
  1875                                  		;	bx:cx = add/sub displacement
  1876                                  		; OUT:	CY if the displacement carries
  1877                                  		;	NC if not
  1878                                  normalise_pointer_with_displacement_bxcx:
  1879                                  %endif
  1880                                  	lframe near
  1881                                  	lpar word,	segment
  1882                                  	lpar word,	offset
  1883                                  	lpar_return
  1884 00000554 5589E5                  	lenter
  1885                                  
  1886                                  %ifn NEED_NORMALISE_POINTER_WITH_DISPLACEMENT
  1887                                  	cmp word [bp + ?offset], 15
  1888                                  	jbe .ret
  1889                                  %endif
  1890                                  
  1891 00000557 50                      	push ax
  1892 00000558 51                      	push cx
  1893 00000559 52                      	push dx
  1894                                  
  1895 0000055A FF7606                  	 push word [bp + ?segment]
  1896 0000055D FF7604                  	 push word [bp + ?offset]
  1897 00000560 E82000                  	call pointer_to_linear
  1898                                  
  1899                                  %if NEED_NORMALISE_POINTER_WITH_DISPLACEMENT
  1900                                  	; push bx
  1901                                  	; 				; sign-extend cx into bx:cx
  1902                                  	; cmp cx, 8000h			; CY if < 8000h (NC if negative)
  1903                                  	; cmc				; NC if positive
  1904                                  	; sbb bx, bx			; 0 if was NC, -1 if was CY
  1905                                  
  1906 00000563 01C1                    	add cx, ax
  1907 00000565 11DA                    	adc dx, bx			; dx:cx = dx:ax + bx:cx
  1908                                  	; pop bx
  1909 00000567 9F                      	lahf				; ah = flags
  1910                                  %else
  1911                                  	xchg ax, cx
  1912                                  %endif
  1913                                  
  1914                                  %if 0
  1915                                  		; Adds in HMA support for this function. Not currently used.
  1916                                  	cmp dx, 10h			; dx:ax >= 10_0000h ?
  1917                                  	jb @F				; no, linear-to-pointer normally -->
  1918                                  	; ja .error
  1919                                  
  1920                                  	add cx, 10h
  1921                                  	; jc .error
  1922                                  	mov word [bp + ?offset], cx
  1923                                  	or word [bp + ?segment], -1
  1924                                  	jmp .return
  1925                                  @@:
  1926                                  %endif
  1927                                  
  1928 00000568 53                      	push bx
  1929 00000569 89CB                    	mov bx, cx
  1930 0000056B 83E10F                  	and cx, 15
  1931 0000056E 894E04                  	mov word [bp + ?offset], cx
  1932                                  
  1933 00000571 B104                    	mov cl, 4
  1934                                  @@:
  1935 00000573 D1EA                    	shr dx, 1
  1936 00000575 D1DB                    	rcr bx, 1
  1937 00000577 E2FA                    	loop @B
  1938                                  
  1939 00000579 895E06                  	mov word [bp + ?segment], bx
  1940 0000057C 5B                      	pop bx
  1941                                  
  1942                                  	; test dx, dx
  1943                                  	; jnz .error
  1944                                  
  1945                                  .return:
  1946                                  
  1947                                  %if NEED_NORMALISE_POINTER_WITH_DISPLACEMENT
  1948 0000057D 9E                      	sahf				; restore flags from ah
  1949                                  %endif
  1950 0000057E 5A                      	pop dx
  1951 0000057F 59                      	pop cx
  1952 00000580 58                      	pop ax
  1953                                  %ifn NEED_NORMALISE_POINTER_WITH_DISPLACEMENT
  1954                                  .ret:
  1955                                  %endif
  1956 00000581 5D                      	lleave
  1957 00000582 C3                      	lret
  1958                                  
  1959                                  
  1960                                  		; INP:	word [ss:sp + 2] = segment
  1961                                  		;	word [ss:sp] = offset
  1962                                  		; OUT:	dx:ax = linear address
  1963                                  pointer_to_linear:
  1964                                  	lframe near
  1965                                  	lpar word,	segment
  1966                                  	lpar word,	offset
  1967 00000583 5589E5                  	lenter
  1968                                  
  1969 00000586 8B4606                  	mov ax, word [bp + ?segment]
  1970 00000589 31D2                    	xor dx, dx
  1971 0000058B 51                      	push cx
  1972 0000058C B90400                  	mov cx, 4
  1973                                  @@:
  1974 0000058F D1E0                    	shl ax, 1
  1975 00000591 D1D2                    	rcl dx, 1
  1976 00000593 E2FA                    	loop @B
  1977                                  
  1978 00000595 034604                  	add ax, word [bp + ?offset]
  1979 00000598 11CA                    	adc dx, cx			; cx = 0 here
  1980 0000059A 59                      	pop cx
  1981                                  
  1982 0000059B 5D                      	lleave
  1983 0000059C C20400                  	lret
  1984                                  
  1985                                  
  1986                                  %if _PASSLCFG
  1987                                  	align 16
  1988                                  transfer_lcfg_block:
  1989                                  	times lcfg.size nop
  1990                                  %endif
  1991 0000059F 90                      	align 16
  1992                                  init1_end:
  1993                                  
  1994                                  
  1995                                  	usesection INIT0
  1996                                  %if _TEST_PROGRAM
  1997                                  init0_msg:
  1998 00000288 4572726F723A204E6F-     .error_stderr:	db "Error: Not enough memory allocated.",13,10
  1998 00000291 7420656E6F75676820-
  1998 0000029A 6D656D6F727920616C-
  1998 000002A3 6C6F63617465642E0D-
  1998 000002AC 0A                 
  1999                                  .error_stderr.length: equ $ - .error_stderr
  2000                                  .error_c_stdout:
  2001 000002AD 30                      .error_stdout:	db "0"
  2002                                  %if 0
  2003                                  		_autodigits paras(_TEST_PROGRAM_DECOMPRESSED_SIZE 				+ (payload_end - payload) 				+ (init1_end - init1_start) 				+ ADDITIONAL_MEMORY 				)
  2008                                  %endif
  2009 000002AE 0D0A                    		db 13,10
  2010                                  .error_c_stdout.length: equ $ - .error_c_stdout
  2011                                  .error_stdout.length: equ $ - .error_stdout
  2012                                  
  2013 000002B0 90<rep 150h>            	align 1024
  2014                                  %if ($ - $$) != 1024
  2015                                   %error Wrong INIT0 size
  2016                                  %endif
  2017                                  %endif
  2018                                  
  2019                                  	align 16
  2020                                  init0_end:
  2021                                  
  2022                                  
  2023                                  %assign num (init1_end - init1_start) + (init0_end - init0_start)
  2024                                  %if _BRIEFLZ
  2025                                  %define which iniblz
  2026                                  %elif _LZ4
  2027                                  %define which inilz4
  2028                                  %elif _SNAPPY
  2029                                  %define which inisz
  2030                                  %elif _EXODECR
  2031                                  %define which iniexo
  2032                                  %elif _X
  2033                                  %define which inix
  2034                                  %elif _HEATSHRINK
  2035                                  %define which inihs
  2036                                  %elif _LZD
  2037                                  %define which inilz
  2038                                  %elif _LZO
  2039                                  %define which inilzo
  2040                                  %elif _LZSA2
  2041                                  %define which inilzsa2
  2042                                  %elif _APL
  2043                                  %define which iniapl
  2044                                  %elif _BZP
  2045                                  %define which inibzp
  2046                                  %elif _ZEROCOMP
  2047                                  %define which inizero
  2048                                  %elif _MVCOMP
  2049                                  %define which inimv
  2050                                  %elif _LZEXEDAT
  2051                                  %define which inilzexe
  2052                                  %endif
  2053                                  %warning which: num bytes used for depacker
  2053          ******************       warning: inilz4: 2464 bytes used for depacker [-w+user]
  2054                                  
  2055                                  
  2056                                  %if _TEST_PROGRAM
  2057                                  	addsection INIT2, align=16 follows=INIT1 vstart=0
  2058                                  init2_start:
  2059                                  		; si => after PSP
  2060                                  		; cs => INIT2
  2061                                  		; psp, free, payload, init1, init2, payload space, stack
  2062                                  		;
  2063                                  		; The correct allocation for the test program
  2064                                  		;  is image size (init0, payload, init1, init2)
  2065                                  		;  minus init0 plus compressed payload size
  2066                                  		;  plus decompressed size plus stack. For
  2067                                  		;  simplicity, init0 subtraction may be skipped.
  2068                                  
  2069                                  		; ss:sp -> word amount repetitions, word ax
  2070                                  		; bx = 1 if A mode test,
  2071                                  		;	8001h if B mode test,
  2072                                  		;	4001h if C mode test
  2073 00000000 53                      	push bx
  2074                                  
  2075                                  	lframe 0
  2076                                  	lpar word,	repetitions
  2077                                  	lpar word,	whichtest
  2078 00000001 5589E5                  	lenter
  2079                                  
  2080 00000004 8B5604                  	mov dx, word [bp + ?repetitions]
  2081 00000007 83FAFF                  	cmp dx, -1
  2082 0000000A 7509                    	jne @F
  2083                                  
  2084                                  .cmdline_error:
  2085                                  %if _DEBUG0
  2086                                  	mov bx, -1
  2087                                  %endif
  2088 0000000C BA[D602]                	mov dx, init2_msg.cmdline_error
  2089 0000000F B92400                  	mov cx, init2_msg.cmdline_error.length
  2090 00000012 E98401                  	jmp init2_error
  2091                                  
  2092                                  
  2093                                  @@:
  2094 00000015 F6460340                	testopt [bp + ?whichtest], 4000h
  2095 00000019 750C                    	jnz .a_or_c
  2096 0000001B F6460380                	testopt [bp + ?whichtest], 8000h
  2097 0000001F 7406                    	jz .a_or_c
  2098                                  .b:
  2099 00000021 85D2                    	test dx, dx
  2100 00000023 74E7                    	jz .cmdline_error
  2101 00000025 EB04                    	jmp @F
  2102                                  
  2103                                  .a_or_c:
  2104 00000027 85D2                    	test dx, dx
  2105 00000029 75E1                    	jnz .cmdline_error
  2106                                  @@:
  2107                                  
  2108 0000002B 8CCA                    	mov dx, cs
  2109 0000002D 81C26C22                	add dx, (init2_end - init2_start) >> 4
  2110                                  				; => payload saving area
  2111 00000031 8CC8                    	mov ax, cs
  2112 00000033 2D211B                  	sub ax, ( (init1_end - init1_start) 		+ (payload_end - payload) 		) >> 4		; => payload source for first run
  2115 00000036 B9C71A                  	mov cx, (payload_end - payload) >> 4
  2116 00000039 E8C102                  	call init2_movp		; copy payload to payload saving area
  2117                                  		; We save away the payload here because a failure
  2118                                  		;  to decompress generally overwrites part of that
  2119                                  		;  payload which was used as source.
  2120                                  
  2121 0000003C 89C2                    	mov dx, ax		; dx => payload source to use
  2122 0000003E 8CC8                    	mov ax, cs
  2123 00000040 83E85A                  	sub ax, (init1_end - init1_start) >> 4
  2124                                  				; ax => INIT1
  2125 00000043 BB0100                  	mov bx, 1		; say we're in EXE mode
  2126 00000046 52                      	push dx
  2127 00000047 50                      	push ax
  2128 00000048 56                      	push si			; si => target
  2129 00000049 0E                      	 push cs
  2130 0000004A E80D01                  	 call .transfer		; call decompression
  2131 0000004D 5E                      	pop si
  2132 0000004E 58                      	pop ax
  2133 0000004F 5A                      	pop dx
  2134                                  		; Returns here after decompression.
  2135                                  		; CY if error.
  2136 00000050 7309                    	jnc @F
  2137                                  
  2138 00000052 BA[2702]                	mov dx, init2_msg.initial_error
  2139 00000055 B93C00                  	mov cx, init2_msg.initial_error.length
  2140 00000058 E93E01                  	jmp init2_error
  2141                                  
  2142                                  @@:
  2143                                  	numdef INCLUDE_UNCOMPRESSED
  2144                                  	numdef WRITE_WRONG_FILE
  2145                                  %if _INCLUDE_UNCOMPRESSED
  2146                                  payload_uncompressed_size equ payload_uncompressed.end - payload_uncompressed
  2147                                  
  2148 0000005B E80201                  	call checkdecompressed
  2149 0000005E 7409                    	je @F
  2150                                  
  2151                                  %if _WRITE_WRONG_FILE
  2152                                  	call writefiles
  2153                                  %endif
  2154                                  
  2155                                  %if _DEBUG0
  2156                                  	mov bx, -1
  2157                                  %endif
  2158 00000060 BA[6302]                	mov dx, init2_msg.initial_error_2
  2159 00000063 B93C00                  	mov cx, init2_msg.initial_error_2.length
  2160 00000066 E93001                  	jmp init2_error
  2161                                  
  2162                                  @@:
  2163                                  %endif
  2164                                  
  2165 00000069 F6460340                	testopt [bp + ?whichtest], 4000h
  2166 0000006D 741D                    	jz @F
  2167 0000006F 8CC8                    	mov ax, cs
  2168 00000071 83E85A                  	sub ax, (init1_end - init1_start) >> 4
  2169                                  				; ax => INIT1
  2170 00000074 8EC0                    	mov es, ax
  2171 00000076 26A1[5900]              	mov ax, word [es:init1_c_mode_counter]
  2172 0000007A E85601                  	call init2_disp_ax_dec
  2173 0000007D B00D                    	mov al, 13
  2174 0000007F E84601                  	call init2_disp_al
  2175 00000082 B00A                    	mov al, 10
  2176 00000084 E84101                  	call init2_disp_al
  2177 00000087 B8004C                  	mov ax, 4C00h
  2178 0000008A CD21                    	int 21h
  2179                                  
  2180                                  @@:
  2181                                  	lvar word,	upperbound
  2182 0000008C 52                      	 push dx
  2183                                  	lvar word,	lowerbound
  2184 0000008D 56                      	 push si
  2185                                  	lvar word,	current_init1
  2186 0000008E 50                      	 push ax
  2187                                  
  2188                                  %if _TEST_PROGRESS
  2189 0000008F BA[1C02]                	mov dx, init2_msg.progress.1
  2190 00000092 B90700                  	mov cx, init2_msg.progress.1.length
  2191                                  .loop:
  2192 00000095 0E                      	push cs
  2193 00000096 1F                      	pop ds
  2194 00000097 BB0200                  	mov bx, 2
  2195 0000009A B440                    	mov ah, 40h
  2196 0000009C CD21                    	int 21h
  2197                                  %else
  2198                                  .loop:
  2199                                  %endif
  2200                                  
  2201 0000009E 8B56FE                  	mov dx, [bp + ?upperbound]
  2202                                  
  2203 000000A1 F6460380                	testopt [bp + ?whichtest], 8000h
  2204 000000A5 7419                    	jz @F
  2205                                  
  2206 000000A7 FF4E04                  	dec word [bp + ?repetitions]
  2207 000000AA 751E                    	jnz .test_b_skip_bound
  2208                                  
  2209                                  %if _TEST_PROGRESS
  2210 000000AC 0E                      	push cs
  2211 000000AD 1F                      	pop ds
  2212 000000AE BA[2502]                	mov dx, init2_msg.progress.linebreak
  2213 000000B1 B90200                  	mov cx, init2_msg.progress.linebreak.length
  2214 000000B4 BB0200                  	mov bx, 2
  2215 000000B7 B440                    	mov ah, 40h
  2216 000000B9 CD21                    	int 21h
  2217                                  %endif
  2218 000000BB B8004C                  	mov ax, 4C00h
  2219 000000BE CD21                    	int 21h
  2220                                  
  2221                                  
  2222                                  @@:
  2223 000000C0 2B56FC                  	sub dx, [bp + ?lowerbound]
  2224 000000C3 746C                    	jz .found
  2225 000000C5 D1EA                    	shr dx, 1
  2226                                  		; Rounding down, so that we never retry upper bound.
  2227                                  		;  The upper bound is known to be working.
  2228 000000C7 0356FC                  	add dx, [bp + ?lowerbound]
  2229                                  
  2230                                  .test_b_skip_bound:
  2231 000000CA 52                      	push dx
  2232 000000CB 81C2C71A                	add dx, (payload_end - payload) >> 4
  2233 000000CF 8B46FA                  	mov ax, [bp + ?current_init1]
  2234 000000D2 B95A00                  	mov cx, (init1_end - init1_start) >> 4
  2235 000000D5 E82502                  	call init2_movp
  2236 000000D8 8956FA                  	mov [bp + ?current_init1], dx
  2237 000000DB 5A                      	pop dx
  2238                                  
  2239 000000DC 8CC8                    	mov ax, cs
  2240 000000DE 056C22                  	add ax, (init2_end - init2_start) >> 4
  2241                                  				; => payload in saving area
  2242 000000E1 B9C71A                  	mov cx, (payload_end - payload) >> 4
  2243 000000E4 E81602                  	call init2_movp		; copy payload from payload saving area
  2244                                  
  2245                                  		; dx => source
  2246 000000E7 8B46FA                  	mov ax, [bp + ?current_init1]
  2247 000000EA BB0100                  	mov bx, 1
  2248 000000ED 52                      	push dx
  2249 000000EE 56                      	push si
  2250 000000EF 0E                      	 push cs
  2251 000000F0 E86700                  	 call .transfer
  2252 000000F3 5E                      	pop si
  2253 000000F4 5A                      	pop dx
  2254                                  		; Returns here after decompression.
  2255                                  		; CY if error.
  2256 000000F5 7312                    	jnc @F
  2257                                  
  2258 000000F7 F6460380                	testopt [bp + ?whichtest], 8000h
  2259 000000FB 7511                    	jnz .test_b_fail
  2260                                  
  2261                                  		; error: this attempt is one below the new lower bound
  2262 000000FD 42                      	inc dx
  2263 000000FE 8956FC                  	mov word [bp + ?lowerbound], dx
  2264                                  
  2265                                  %if _TEST_PROGRESS
  2266 00000101 BA[2302]                	mov dx, init2_msg.progress.fail
  2267 00000104 B90100                  	mov cx, init2_msg.progress.fail.length
  2268                                  %endif
  2269 00000107 EB8C                    	jmp .loop
  2270                                  
  2271                                  @@:
  2272                                  %if _INCLUDE_UNCOMPRESSED
  2273 00000109 E85400                  	call checkdecompressed
  2274 0000010C 7417                    	je @F
  2275                                  
  2276                                  %if _WRITE_WRONG_FILE
  2277                                  	call writefiles
  2278                                  %endif
  2279                                  
  2280                                  .test_b_fail:
  2281                                  %if _TEST_PROGRESS
  2282 0000010E 0E                      	push cs
  2283 0000010F 1F                      	pop ds
  2284 00000110 BA[2502]                	mov dx, init2_msg.progress.linebreak
  2285 00000113 B90200                  	mov cx, init2_msg.progress.linebreak.length
  2286 00000116 BB0200                  	mov bx, 2
  2287 00000119 B440                    	mov ah, 40h
  2288 0000011B CD21                    	int 21h
  2289                                  %endif
  2290                                  %if _DEBUG0
  2291                                  	mov bx, -2
  2292                                  %endif
  2293 0000011D BA[9F02]                	mov dx, init2_msg.subsequent_error_2
  2294 00000120 B93700                  	mov cx, init2_msg.subsequent_error_2.length
  2295 00000123 EB74                    	jmp init2_error
  2296                                  
  2297                                  @@:
  2298                                  %endif
  2299                                  		; success: this attempt is the new upper bound
  2300 00000125 8956FE                  	mov word [bp + ?upperbound], dx
  2301                                  %if _TEST_PROGRESS
  2302 00000128 BA[2402]                	mov dx, init2_msg.progress.success
  2303 0000012B B90100                  	mov cx, init2_msg.progress.success.length
  2304                                  %endif
  2305 0000012E E964FF                  	jmp .loop
  2306                                  
  2307                                  .found:
  2308                                  %if _TEST_PROGRESS
  2309 00000131 0E                      	push cs
  2310 00000132 1F                      	pop ds
  2311 00000133 BA[2502]                	mov dx, init2_msg.progress.linebreak
  2312 00000136 B90200                  	mov cx, init2_msg.progress.linebreak.length
  2313 00000139 BB0200                  	mov bx, 2
  2314 0000013C B440                    	mov ah, 40h
  2315 0000013E CD21                    	int 21h
  2316                                  %endif
  2317 00000140 8B46FE                  	mov ax, word [bp + ?upperbound]
  2318 00000143 29F0                    	sub ax, si		; = how many paragraphs in buffer before source
  2319 00000145 05211B                  	add ax, paras( (init1_end - init1_start) 			+ (payload_end - payload) )
  2321                                  				; = how many paragraphs needed for process
  2322 00000148 E88800                  	call init2_disp_ax_dec
  2323 0000014B B00D                    	mov al, 13
  2324 0000014D E87800                  	call init2_disp_al
  2325 00000150 B00A                    	mov al, 10
  2326 00000152 E87300                  	call init2_disp_al
  2327 00000155 B8004C                  	mov ax, 4C00h
  2328 00000158 CD21                    	int 21h
  2329                                  
  2330                                  
  2331                                  .transfer:
  2332 0000015A 31FF                    	xor di, di
  2333 0000015C 57                      	push di			; placeholder ax value on stack
  2334 0000015D 50                      	push ax			; INIT1 segment
  2335 0000015E 57                      	push di			; zero = init1_start
  2336 0000015F CB                      	retf			; transfer to INIT1:init1_start
  2337                                  
  2338                                  
  2339                                  
  2340                                  %if _INCLUDE_UNCOMPRESSED
  2341                                  
  2342                                  
  2343                                  %if _WRITE_WRONG_FILE
  2344                                  writefiles:
  2345                                  	push cs
  2346                                  	pop ds
  2347                                  	mov dx, init2_msg.wrong_file_name
  2348                                  	xor cx, cx
  2349                                  	mov ah, 3Ch
  2350                                  	int 21h
  2351                                  	jc .notfile
  2352                                  
  2353                                  	mov bx, ax
  2354                                  	mov cx, payload_uncompressed_size >> 4
  2355                                  	xor dx, dx
  2356                                  .loopfile:
  2357                                  	mov ds, si
  2358                                  	mov ah, 40h
  2359                                  	push cx
  2360                                  	mov cx, 16
  2361                                  	int 21h
  2362                                  	pop cx
  2363                                  	inc si
  2364                                  	loop .loopfile
  2365                                  
  2366                                  	mov ds, si
  2367                                  	mov cx, payload_uncompressed_size & 15
  2368                                  	mov ah, 40h
  2369                                  	int 21h
  2370                                  
  2371                                  	mov ah, 3Eh
  2372                                  	int 21h
  2373                                  
  2374                                  .notfile:
  2375                                  
  2376                                  	push cs
  2377                                  	pop ds
  2378                                  	mov dx, init2_msg.wrong_file_name2
  2379                                  	xor cx, cx
  2380                                  	mov ah, 3Ch
  2381                                  	int 21h
  2382                                  	jc .notfile2
  2383                                  
  2384                                  	mov bx, ax
  2385                                  	mov cx, payload_uncompressed_size >> 4
  2386                                  	mov si, cs
  2387                                  	add si, (payload_uncompressed - init2_start) >> 4
  2388                                  	xor dx, dx
  2389                                  .loopfile2:
  2390                                  	mov ds, si
  2391                                  	mov ah, 40h
  2392                                  	push cx
  2393                                  	mov cx, 16
  2394                                  	int 21h
  2395                                  	pop cx
  2396                                  	inc si
  2397                                  	loop .loopfile2
  2398                                  
  2399                                  	mov ds, si
  2400                                  	mov cx, payload_uncompressed_size & 15
  2401                                  	mov ah, 40h
  2402                                  	int 21h
  2403                                  
  2404                                  	mov ah, 3Eh
  2405                                  	int 21h
  2406                                  
  2407                                  .notfile2:
  2408                                  	retn
  2409                                  %endif
  2410                                  
  2411                                  		; INP:	si => decompressed image
  2412                                  		; OUT:	ZR if matching
  2413                                  		;	NZ if mismatching
  2414                                  		; CHG:	es, ds, di, bx, cx
  2415                                  		; STT:	UP
  2416                                  checkdecompressed:
  2417 00000160 50                      	push ax
  2418 00000161 52                      	push dx
  2419 00000162 56                      	push si
  2420                                  %if _PAYLOAD_KERNEL_MAX_PARAS
  2421                                  	mov cx, _PAYLOAD_KERNEL_MAX_PARAS
  2422                                  	mov bx, 0
  2423                                  %else
  2424 00000163 B93222                  	mov cx, payload_uncompressed_size >> 4
  2425 00000166 BB0000                  	mov bx, payload_uncompressed_size & 15
  2426                                  %endif
  2427 00000169 8CCF                    	mov di, cs
  2428 0000016B 83C739                  	add di, (payload_uncompressed - init2_start) >> 4
  2429 0000016E E319                    	jcxz .end
  2430                                  .loop:
  2431 00000170 51                      	push cx
  2432 00000171 B90800                  	mov cx, 8
  2433 00000174 8EDE                    	mov ds, si
  2434 00000176 8EC7                    	mov es, di
  2435 00000178 46                      	inc si
  2436 00000179 47                      	inc di
  2437 0000017A 56                      	push si
  2438 0000017B 57                      	push di
  2439 0000017C 31F6                    	xor si, si
  2440 0000017E 31FF                    	xor di, di
  2441 00000180 F3A7                    	repe cmpsw
  2442 00000182 5F                      	pop di
  2443 00000183 5E                      	pop si
  2444 00000184 59                      	pop cx
  2445 00000185 750E                    	jne .ret
  2446 00000187 E2E7                    	loop .loop
  2447                                  .end:
  2448 00000189 8EDE                    	mov ds, si
  2449 0000018B 8EC7                    	mov es, di
  2450 0000018D 31F6                    	xor si, si
  2451 0000018F 31FF                    	xor di, di		; (ZR)
  2452 00000191 89D9                    	mov cx, bx
  2453 00000193 F3A6                    	repe cmpsb
  2454                                  .ret:
  2455 00000195 5E                      	pop si
  2456 00000196 5A                      	pop dx
  2457 00000197 58                      	pop ax
  2458 00000198 C3                      	retn
  2459                                  %endif
  2460                                  
  2461                                  
  2462                                  init2_error:
  2463                                  %if _DEBUG0
  2464                                  	push bx
  2465                                  %endif
  2466 00000199 0E                      	push cs
  2467 0000019A 1F                      	pop ds
  2468 0000019B BB0200                  	mov bx, 2
  2469 0000019E B440                    	mov ah, 40h
  2470 000001A0 CD21                    	int 21h
  2471                                  
  2472                                  %if _DEBUG0
  2473                                  	mov dx, init2_msg.rc
  2474                                  	mov cx, init2_msg.rc.length
  2475                                  	mov bx, 2
  2476                                  	mov ah, 40h
  2477                                  	int 21h
  2478                                  	pop ax
  2479                                  	call init2_error_disp_ax_hex
  2480                                  	mov dx, init2_msg.linebreak
  2481                                  	mov cx, init2_msg.linebreak.length
  2482                                  	mov bx, 2
  2483                                  	mov ah, 40h
  2484                                  	int 21h
  2485                                  %endif
  2486                                  
  2487 000001A2 F6460340                	testopt [bp + ?whichtest], 4000h
  2488 000001A6 7408                    	jz @F
  2489 000001A8 BA[FA02]                	mov dx, init2_msg.error_c_stdout
  2490 000001AB B90300                  	mov cx, init2_msg.error_c_stdout.length
  2491 000001AE EB0C                    	jmp @FF
  2492                                  @@:
  2493 000001B0 F6460380                	testopt [bp + ?whichtest], 8000h
  2494 000001B4 750D                    	jnz @FF
  2495 000001B6 BA[FA02]                	mov dx, init2_msg.error_stdout
  2496 000001B9 B90300                  	mov cx, init2_msg.error_stdout.length
  2497                                  @@:
  2498 000001BC BB0100                  	mov bx, 1
  2499 000001BF B440                    	mov ah, 40h
  2500 000001C1 CD21                    	int 21h
  2501                                  @@:
  2502 000001C3 B8FF4C                  	mov ax, 4CFFh
  2503 000001C6 CD21                    	int 21h
  2504                                  
  2505                                  	lleave ctx
  2506                                  
  2507                                  
  2508                                  %if _DEBUG0
  2509                                  init2_error_disp_ax_hex:	; ax
  2510                                  		xchg al,ah
  2511                                  		call init2_error_disp_al_hex	; display former ah
  2512                                  		xchg al,ah			;  and fall trough for al
  2513                                  init2_error_disp_al_hex:	; al
  2514                                  		push cx
  2515                                  		mov cl,4
  2516                                  		ror al,cl
  2517                                  		call init2_error_disp_al_lownibble_hex
  2518                                  						; display former high-nibble
  2519                                  		rol al,cl
  2520                                  		pop cx
  2521                                  						;  and fall trough for low-nibble
  2522                                  init2_error_disp_al_lownibble_hex:
  2523                                  		push ax			 ; save ax for call return
  2524                                  		and al,00001111b		; high nibble must be zero
  2525                                  		add al,'0'			; if number is 0-9, now it's the correct character
  2526                                  		cmp al,'9'
  2527                                  		jna .decimalnum		 ; if we get decimal number with this, ok -->
  2528                                  		add al,7			;  otherwise, add 7 and we are inside our alphabet
  2529                                   .decimalnum:
  2530                                  		call init2_error_disp_al
  2531                                  		pop ax
  2532                                  		retn
  2533                                  
  2534                                  
  2535                                  init2_error_disp_al:
  2536                                  	push dx
  2537                                  	push cx
  2538                                  	push bx
  2539                                  	push ax
  2540                                  	mov dx, sp
  2541                                  	push ds
  2542                                  	 push ss
  2543                                  	 pop ds
  2544                                  	mov cx, 1
  2545                                  	mov bx, 2
  2546                                  	mov ah, 40h
  2547                                  	int 21h
  2548                                  	pop ds
  2549                                  	pop ax
  2550                                  	pop bx
  2551                                  	pop cx
  2552                                  	pop dx
  2553                                  	retn
  2554                                  %endif
  2555                                  
  2556                                  
  2557                                  init2_disp_al:
  2558 000001C8 52                      	push dx
  2559 000001C9 50                      	push ax
  2560 000001CA 88C2                    	mov dl, al
  2561 000001CC B402                    	mov ah, 2
  2562 000001CE CD21                    	int 21h
  2563 000001D0 58                      	pop ax
  2564 000001D1 5A                      	pop dx
  2565 000001D2 C3                      	retn
  2566                                  
  2567                                  
  2568                                  		; Display number in ax decimal
  2569                                  		;
  2570                                  		; INP:	ax = number
  2571                                  		; OUT:	displayed using Int21.02
  2572                                  		; CHG:	none
  2573                                  init2_disp_ax_dec:			; ax (no leading zeros)
  2574 000001D3 53                      		push bx
  2575 000001D4 31DB                    		xor bx, bx
  2576                                  .pushax:
  2577 000001D6 52                      		push dx
  2578 000001D7 50                      		push ax
  2579 000001D8 08DB                    		or bl, bl
  2580 000001DA 7405                    		jz .nobl
  2581 000001DC 80EB05                  		sub bl, 5
  2582 000001DF F6DB                    		neg bl
  2583                                  .nobl:
  2584 000001E1 51                      		push cx
  2585 000001E2 B91027                  		mov cx, 10000
  2586 000001E5 E81B00                  		call .divide_out
  2587 000001E8 B9E803                  		mov cx, 1000
  2588 000001EB E81500                  		call .divide_out
  2589 000001EE B96400                  		mov cx, 100
  2590 000001F1 E80F00                  		call .divide_out
  2591 000001F4 B10A                    		mov cl, 10
  2592 000001F6 E80A00                  		call .divide_out
  2593                                  							; (Divisor 1 is useless)
  2594 000001F9 0430                    		add al, '0'
  2595 000001FB E8CAFF                  		call init2_disp_al
  2596 000001FE 59                      		pop cx
  2597 000001FF 58                      		pop ax
  2598 00000200 5A                      		pop dx
  2599 00000201 5B                      		pop bx					; Caller's register
  2600 00000202 C3                      		retn
  2601                                  
  2602                                  
  2603                                  		; INP:	ax = number
  2604                                  		;	cx = divisor
  2605                                  		; OUT:	ax = remainder of operation
  2606                                  		;	result displayed
  2607                                  .divide_out:
  2608 00000203 52                      		push dx
  2609 00000204 31D2                    		xor dx, dx
  2610 00000206 F7F1                    		div cx				; 0:ax / cx
  2611 00000208 52                      		push dx				; remainder
  2612 00000209 FECB                    		dec bl
  2613 0000020B 7503                    		jnz .nobl2
  2614 0000020D 80CF01                  		or bh, 1
  2615                                  .nobl2:
  2616 00000210 08C7                    		or bh, al
  2617 00000212 7405                    		jz .leadingzero
  2618 00000214 0430                    		add al, '0'
  2619 00000216 E8AFFF                  		call init2_disp_al		; display result
  2620                                   .leadingzero:
  2621 00000219 58                      		pop ax				; remainder
  2622 0000021A 5A                      		pop dx
  2623 0000021B C3                      		retn
  2624                                  
  2625                                  
  2626                                  init2_msg:
  2627                                  %if _TEST_PROGRESS
  2628 0000021C 496E666F3A2031          .progress.1:		db "Info: 1"
  2629                                  .progress.1.length: equ $ - .progress.1
  2630 00000223 46                      .progress.fail:		db "F"
  2631                                  .progress.fail.length: equ $ - .progress.fail
  2632 00000224 53                      .progress.success:	db "S"
  2633                                  .progress.success.length: equ $ - .progress.success
  2634 00000225 0D0A                    .progress.linebreak:	db 13,10
  2635                                  .progress.linebreak.length: equ $ - .progress.linebreak
  2636                                  %endif
  2637                                  %if _WRITE_WRONG_FILE
  2638                                  .wrong_file_name:	asciz "WRONG.BIN"
  2639                                  .wrong_file_name2:	asciz "WRONG2.BIN"
  2640                                  %endif
  2641 00000227 4572726F723A205465-     .initial_error:	db "Error: Test program failed to decompress with full buffer."
  2641 00000230 73742070726F677261-
  2641 00000239 6D206661696C656420-
  2641 00000242 746F206465636F6D70-
  2641 0000024B 726573732077697468-
  2641 00000254 2066756C6C20627566-
  2641 0000025D 6665722E           
  2642 00000261 0D0A                    .linebreak:	db 13,10
  2643                                  .initial_error.length: equ $ - .initial_error
  2644                                  .linebreak.length: equ $ - .linebreak
  2645                                  %if _DEBUG0
  2646                                  .rc:	db "Error: Failure code="
  2647                                  .rc.length: equ $ - .rc
  2648                                  %endif
  2649                                  %if _INCLUDE_UNCOMPRESSED
  2650 00000263 4572726F723A205465-     .initial_error_2:	db "Error: Test program decompressed wrongly with full buffer.",13,10
  2650 0000026C 73742070726F677261-
  2650 00000275 6D206465636F6D7072-
  2650 0000027E 65737365642077726F-
  2650 00000287 6E676C792077697468-
  2650 00000290 2066756C6C20627566-
  2650 00000299 6665722E0D0A       
  2651                                  .initial_error_2.length: equ $ - .initial_error_2
  2652 0000029F 4572726F723A205465-     .subsequent_error_2:	db "Error: Test program decompressed wrongly during test.",13,10
  2652 000002A8 73742070726F677261-
  2652 000002B1 6D206465636F6D7072-
  2652 000002BA 65737365642077726F-
  2652 000002C3 6E676C792064757269-
  2652 000002CC 6E6720746573742E0D-
  2652 000002D5 0A                 
  2653                                  .subsequent_error_2.length: equ $ - .subsequent_error_2
  2654                                  %endif
  2655 000002D6 4572726F723A20496E-     .cmdline_error:	db "Error: Invalid command line input.",13,10
  2655 000002DF 76616C696420636F6D-
  2655 000002E8 6D616E64206C696E65-
  2655 000002F1 20696E7075742E0D0A 
  2656                                  .cmdline_error.length: equ $ - .cmdline_error
  2657                                  
  2658                                  .error_c_stdout:
  2659 000002FA 30                      .error_stdout:	db "0"
  2660                                  %if 0
  2661                                  		_autodigits paras(_TEST_PROGRAM_DECOMPRESSED_SIZE 				+ (payload_end - payload) 				+ (init1_end - init1_start) 				+ ADDITIONAL_MEMORY 				)
  2666                                  %endif
  2667 000002FB 0D0A                    		db 13,10
  2668                                  .error_c_stdout.length: equ $ - .error_c_stdout
  2669                                  .error_stdout.length: equ $ - .error_stdout
  2670                                  
  2671                                  
  2672                                  		; Move paragraphs
  2673                                  		;
  2674                                  		; INP:	ax => source
  2675                                  		;	dx => destination
  2676                                  		;	cx = number of paragraphs
  2677                                  		; CHG:	-
  2678                                  		; OUT:	ax and dx unchanged
  2679                                  		; Note:	Doesn't work correctly on HMA; doesn't always wrap to LMA either.
  2680                                  		;	Do not provide a wrapped/HMA source or destination!
  2681                                  init2_movp:
  2682 000002FD 51                      	push cx
  2683 000002FE 1E                      	push ds
  2684 000002FF 56                      	push si
  2685 00000300 06                      	push es
  2686 00000301 57                      	push di
  2687                                  
  2688 00000302 39D0                    	cmp ax, dx		; source above destination ?
  2689 00000304 770A                    	ja .up			; yes, move up (forwards) -->
  2690 00000306 747B                    	je .return		; same, no need to move -->
  2691 00000308 50                      	push ax
  2692 00000309 01C8                    	add ax, cx		; (expected not to carry)
  2693 0000030B 39D0                    	cmp ax, dx		; end of source is above destination ?
  2694 0000030D 58                      	pop ax
  2695 0000030E 7730                    	ja .down		; yes, move from top down -->
  2696                                  	; Here, the end of source is below-or-equal the destination,
  2697                                  	;  so they do not overlap. In this case we prefer moving up.
  2698                                  
  2699                                  .up:
  2700 00000310 50                      	push ax
  2701 00000311 52                      	push dx
  2702                                  .uploop:
  2703 00000312 8ED8                    	mov ds, ax
  2704 00000314 8EC2                    	mov es, dx
  2705 00000316 31FF                    	xor di, di
  2706 00000318 31F6                    	xor si, si		; -> start of segment
  2707 0000031A 81E90010                	sub cx, 1000h		; 64 KiB left ?
  2708 0000031E 7610                    	jbe .uplast		; no -->
  2709 00000320 51                      	push cx
  2710 00000321 B90080                  	mov cx, 10000h /2
  2711 00000324 F3A5                    	rep movsw		; move 64 KiB
  2712 00000326 59                      	pop cx
  2713 00000327 050010                  	add ax, 1000h
  2714 0000032A 81C20010                	add dx, 1000h		; -> next segment
  2715 0000032E EBE2                    	jmp short .uploop	; proceed for more -->
  2716                                  .uplast:
  2717 00000330 81C10010                	add cx, 1000h		; restore counter
  2718 00000334 D1E1                    	shl cx, 1
  2719 00000336 D1E1                    	shl cx, 1
  2720 00000338 D1E1                    	shl cx, 1		; *8, paragraphs to words
  2721 0000033A F3A5                    	rep movsw		; move last part
  2722 0000033C 5A                      	pop dx
  2723 0000033D 58                      	pop ax
  2724 0000033E EB43                    	jmp short .return
  2725                                  
  2726                                  .down:
  2727 00000340 FD                      	std			; _AMD_ERRATUM_109_WORKAROUND as below
  2728                                  .dnloop:
  2729 00000341 81E90010                	sub cx, 1000h		; 64 KiB left ?
  2730 00000345 761A                    	jbe .dnlast		; no -->
  2731 00000347 50                      	push ax
  2732 00000348 52                      	push dx
  2733 00000349 01C8                    	add ax, cx
  2734 0000034B 01CA                    	add dx, cx
  2735 0000034D 8ED8                    	mov ds, ax		; -> 64 KiB not yet moved
  2736 0000034F 8EC2                    	mov es, dx
  2737 00000351 5A                      	pop dx
  2738 00000352 58                      	pop ax
  2739 00000353 BFFEFF                  	mov di, -2
  2740 00000356 89FE                    	mov si, di		; moved from last word down
  2741 00000358 51                      	push cx
  2742 00000359 B90080                  	mov cx, 10000h /2
  2743 0000035C F3A5                    	rep movsw		; move 64 KiB
  2744 0000035E 59                      	pop cx
  2745 0000035F EBE0                    	jmp short .dnloop	; proceed for more -->
  2746                                  .dnlast:
  2747 00000361 81C10010                	add cx, 1000h		; restore counter
  2748 00000365 D1E1                    	shl cx, 1
  2749 00000367 D1E1                    	shl cx, 1
  2750 00000369 D1E1                    	shl cx, 1		; *8, paragraphs to words
  2751 0000036B 89CF                    	mov di, cx
  2752 0000036D 4F                      	dec di
  2753 0000036E D1E7                    	shl di, 1		; words to offset, -> last word
  2754 00000370 89FE                    	mov si, di
  2755 00000372 8ED8                    	mov ds, ax
  2756 00000374 8EC2                    	mov es, dx		; first segment correct
  2757                                  
  2758                                  		; Refer to comment in init0_movp.
  2759                                  %if _AMD_ERRATUM_109_WORKAROUND
  2760 00000376 E308                    	jcxz @FF
  2761 00000378 83F914                  	cmp cx, 20
  2762 0000037B 7703                    	ja @FF
  2763                                  @@:
  2764 0000037D A5                      	movsw
  2765 0000037E E2FD                    	loop @B
  2766                                  @@:
  2767                                  %endif
  2768 00000380 F3A5                    	rep movsw		; move first part
  2769 00000382 FC                      	cld
  2770                                  .return:
  2771 00000383 5F                      	pop di
  2772 00000384 07                      	pop es
  2773 00000385 5E                      	pop si
  2774 00000386 1F                      	pop ds
  2775 00000387 59                      	pop cx
  2776 00000388 C3                      	retn
  2777                                  
  2778                                  
  2779 00000389 90<rep 7h>              	align 16
  2780                                  
  2781                                  	strdef UNCOMPRESSED_FILE, "lDOSLOAD.BIN"
  2782                                  %if _INCLUDE_UNCOMPRESSED
  2783                                  payload_uncompressed:
  2784 00000390 <bin 22320h>            	incbin _UNCOMPRESSED_FILE
  2785                                  .end:
  2786 000226B0 26                      	db 38
  2787 000226B1 26<rep Fh>              	align 16, db 38
  2788                                  %endif
  2789                                  init2_end:
  2790                                  %endif
  2791                                  
