wok-tiny rev 152

Add plop
author Pascal Bellard <pascal.bellard@slitaz.org>
date Thu Sep 19 20:20:35 2019 +0200 (2019-09-19)
parents f93fa966f210
children bd7b94f2c7d0
files base-tiny/receipt plop/receipt plop/stuff/bootloader.S plop/stuff/pack plop/stuff/unlzma.S plop/stuff/unpack.S
line diff
     1.1 --- a/base-tiny/receipt	Sun Sep 15 19:49:24 2019 +0200
     1.2 +++ b/base-tiny/receipt	Thu Sep 19 20:20:35 2019 +0200
     1.3 @@ -119,6 +119,8 @@
     1.4  	sed "s|\$(echo -en '\x64\x66\x8F\x47\x78')|\$(echo -en '\x5E\x5E\x90\x90\x90')|" | \\
     1.5  	sed "s|\$(echo -en '\x66\x8F\x47\x78')|\$(echo -en '\x5E\x5E\x90\x90')|" | \\
     1.6  	sed "s|\$(echo -en '\x77\x02\xB0\x01')|\$(echo -en '\x90\x90\xB0\x01')|" | \\
     1.7 +	sed "s|\$(echo -en '\x98\xCD\x13')|\$(echo -en '\x89\x90\x90')|" | \\
     1.8 +	sed "s|\$(echo -en '\x60\xCD\x13')|\$(echo -en '\x60\x90\x90')|" | \\
     1.9  	sed "s|\$(echo -en '\xB0\x31\x1C\x03\xB4\x0E')|\$(echo -en '\xB0\x31\x14\xFD\xB4\x0E')|" | \\
    1.10  	dd of=\$i conv=notrunc
    1.11  done
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/plop/receipt	Thu Sep 19 20:20:35 2019 +0200
     2.3 @@ -0,0 +1,34 @@
     2.4 +# SliTaz package receipt.
     2.5 +
     2.6 +PACKAGE="plop"
     2.7 +VERSION="5.0.15"
     2.8 +CATEGORY="base-system"
     2.9 +SHORT_DESC="USB / IDE-CD / PCMCIA CardBus and others boot utility."
    2.10 +MAINTAINER="pascal.bellard@slitaz.org"
    2.11 +LICENSE="other"
    2.12 +WEB_SITE="https://www.plop.at/en/bootmanagers.html"
    2.13 +TARBALL="$PACKAGE-$VERSION.zip"
    2.14 +WGET_URL="https://download.plop.at/files/bootmngr/$TARBALL"
    2.15 +TARGET="i486"
    2.16 +
    2.17 +BUILD_DEPENDS="xz"
    2.18 +
    2.19 +# Rules to configure and make the package.
    2.20 +compile_rules()
    2.21 +{
    2.22 +	cp $stuff/*.S $stuff/pack .
    2.23 +	for i in bootloader unpack ; do
    2.24 +		cc -o $i.o -Wa,-a=$i.lst -c $i.S
    2.25 +		objcopy -O binary $i.o $i.bin
    2.26 +	done
    2.27 +	sed -i "s/VERSION/$VERSION/" pack
    2.28 +	./pack --build bootloader.bin unpack.bin
    2.29 +	./pack plpbt.bin plop
    2.30 +}
    2.31 +
    2.32 +# Rules to gen a SliTaz package suitable for Tazpkg.
    2.33 +genpkg_rules()
    2.34 +{
    2.35 +	mkdir -p $fs/boot
    2.36 +	cp $src/plop $fs/boot/
    2.37 +}
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/plop/stuff/bootloader.S	Thu Sep 19 20:20:35 2019 +0200
     3.3 @@ -0,0 +1,653 @@
     3.4 +// Image/zImage & tiny bzImage linux kernel boot sector, (C) SliTaz, GPL2.
     3.5 +
     3.6 +SYSSEG		= 0x1000
     3.7 +setup_sects	= 497
     3.8 +syssize		= 500
     3.9 +cmd_line_ptr	= 0x228
    3.10 +
    3.11 +	.text
    3.12 +	.code16
    3.13 +	.org	0
    3.14 +	.globl	_start
    3.15 +_start:
    3.16 +
    3.17 +#define CODESZ 512		/* patched by installer */
    3.18 +
    3.19 +// Default kernel format is 386 Image/zImage
    3.20 +//#define BZIMAGE 0x207		/* setup version ; for bzImage < 512 Kb only */
    3.21 +//#define ELKS			/* 8086/286 linux port */
    3.22 +
    3.23 +/* some extra features */
    3.24 +#define EXE_SUPPORT		real mode dos .exe file support	+208/264
    3.25 +#define  CMDLINE		dos kernel cmdline support	+45
    3.26 +#define  VCPI			VCPI 4.0 support (386+)		+109
    3.27 +#define  SHUTDOWNDOS		shutdown DOS services		+29
    3.28 +
    3.29 +/* some contraints to reduce the code size */
    3.30 +//#define FLOPPY_1440K_ONLY	1.44M floppies support only	-26
    3.31 +#define FLOPPY_HAS_2_SIDES	hardcoded heads count to 2	-15
    3.32 +//#define NO_CMDLINE_SHRINK	remove heading spaces ?		-6-21
    3.33 +//#define  NO_CMDLINE_FILE	remove @cmdline file support ?	-21
    3.34 +//#define NO_DOTS		show progression dots ?		-5
    3.35 +#ifndef BZIMAGE
    3.36 +#define TINY_ZIMAGE		system < 64Kb ?			-11
    3.37 +#defime NO_MINSETUP		default setup (dos only) ?	-4
    3.38 +//#define NO_CURSOR_DEFINITION					-8
    3.39 +#endif
    3.40 +
    3.41 +#ifdef ELKS
    3.42 +	.arch	i8086
    3.43 +INITSEG		= 0x0100
    3.44 +SETUPSEG	= 0x0120
    3.45 +#define ONLY8086
    3.46 +#undef BZIMAGE
    3.47 +#undef VCPI
    3.48 +#else
    3.49 +INITSEG		= 0x9000
    3.50 +SETUPSEG	= 0x9020
    3.51 +#endif
    3.52 +
    3.53 +.macro	shlclw cnt,obj
    3.54 +#ifdef ONLY8086
    3.55 +	movb	\cnt,%cl
    3.56 +	shlw	%cl,\obj
    3.57 +#else
    3.58 +	shlw	\cnt,\obj
    3.59 +#endif
    3.60 +.endm
    3.61 +
    3.62 +.macro	shrclw cnt,obj
    3.63 +#ifdef ONLY8086
    3.64 +	movb	\cnt,%cl
    3.65 +	shrw	%cl,\obj
    3.66 +#else
    3.67 +	shrw	\cnt,\obj
    3.68 +#endif
    3.69 +.endm
    3.70 +
    3.71 +#ifdef EXE_SUPPORT
    3.72 +#define EXEADRS(x)	(x+0xE0)
    3.73 +#define FLAT20(x)	(x+16*INITSEG)
    3.74 +
    3.75 +.macro	trace_int19
    3.76 +#ifdef ONLY8086
    3.77 +	xorw	%si, %si
    3.78 +	movw	%si, %ds
    3.79 +	pushw	4+2(%si)
    3.80 +	pushw	4(%si)
    3.81 +	movw	$step19, 4(%si)
    3.82 +	movw	$INITSEG, 4+2(%si)
    3.83 +#else
    3.84 +	pushl	$4
    3.85 +	popw	%si
    3.86 +	popw	%ds
    3.87 +	pushl	(%si)
    3.88 +	movl	$step19+(INITSEG<<16), (%si)
    3.89 +#endif
    3.90 +	pushfw
    3.91 +	popw	%ax
    3.92 +	incb	%ah			# set TF
    3.93 +	pushw	%ax
    3.94 +	popfw
    3.95 +	ljmp	*4*0x19-4(%si)
    3.96 +.endm
    3.97 +
    3.98 +stacktop	= 0x9E00		# in 0x8000 .. 0xA000
    3.99 +	decw	%bp			// Magic number: MZ
   3.100 +	popw	%dx
   3.101 +	jmp	start			// Bytes on last page of file
   3.102 +	.word	(CODESZ+511)/512	// Pages in file			INSTALLER
   3.103 +	.word	0			// Relocations
   3.104 +	.word	(end_header-_start)/16	// Size of header in paragraphs
   3.105 +	.word	4096			// Minimum extra paragraphs needed
   3.106 +	.word	-1			// Maximum extra paragraphs needed
   3.107 +	.word	(CODESZ+15)/16		// Initial (relative) SS value		INSTALLER
   3.108 +	.word	stacktop		// Initial SP value
   3.109 +	.word	0			// Checksum				INSTALLER?
   3.110 +	.word	EXEADRS(comstart)	// Initial IP value
   3.111 +	.word	0xFFF0			// Initial (relative) CS value
   3.112 +//	.word	0x001C			// File address of relocation table
   3.113 +//	.word	0,0,0			// Overlay number
   3.114 +#else
   3.115 +#undef VCPI
   3.116 +#endif
   3.117 +start:
   3.118 +	xorw	%ax, %ax		# %ax = 0
   3.119 +zeroed		= 12			# zeroed registers
   3.120 +	movw	$zeroed/2, %cx		# clear gdt + offset, %ds, limits
   3.121 +stacktop	= 0x9E00		# in 0x8000 .. 0xA000 (+zeroed+12)
   3.122 +#ifdef ONLY8086
   3.123 +	movw	$INITSEG, %bx
   3.124 +#else
   3.125 +	pushw	$INITSEG
   3.126 +#endif
   3.127 +end_header:
   3.128 +	cld				# assume nothing
   3.129 +#if defined(BZIMAGE) && BZIMAGE >= 0x202
   3.130 +	popw	%es			# %es contain INITSEG
   3.131 +	movw	%es, %di
   3.132 +#else
   3.133 +					# cmdline offset at 0x22
   3.134 +	movw	$stacktop, %di		# stacktop is an arbitrary value >=
   3.135 +					# length of bootsect + length of
   3.136 +					# setup + room for stack;
   3.137 +					# 12 is disk parm size.
   3.138 +# ifdef ONLY8086
   3.139 +	pushw	%bx
   3.140 +# endif
   3.141 +	popw	%es			# %es contain INITSEG
   3.142 +#endif
   3.143 +	pushw	%es
   3.144 +	popw	%ss			# %es = %ss = INITSEG
   3.145 +	movw	%di, %sp		# put stack at INITSEG:stacktop-...
   3.146 +#ifdef EXE_SUPPORT
   3.147 +	cwd				# force %dx = 0 (floppy only)
   3.148 +#endif
   3.149 +
   3.150 +# Many BIOS's default disk parameter tables will not recognize
   3.151 +# multi-sector reads beyond the maximum sector number specified
   3.152 +# in the default diskette parameter tables - this may mean 7
   3.153 +# sectors in some cases.
   3.154 +#
   3.155 +# Since single sector reads are slow and out of the question,
   3.156 +# we must take care of this by creating new parameter tables
   3.157 +# (for the first disk) in RAM.  We can set the maximum sector
   3.158 +# count to 36 - the most we will encounter on an ED 2.88.  
   3.159 +#
   3.160 +# High doesn't hurt.  Low does.  Let's use the max: 63
   3.161 +
   3.162 +	rep				# don't worry about cld
   3.163 +	stosw				# already done above
   3.164 +	popw	%bx			# offset = 0
   3.165 +	popw	%ds			# %ds = 0
   3.166 +
   3.167 +	movb	setup_sects+0x7C00, %al	# read bootsector + setup (%ds = 0)
   3.168 +	incw	%ax
   3.169 +
   3.170 +	ldsw	0x78(%bx), %si		# %ds:%bx+0x78 is parameter table address
   3.171 +	pushw	%es
   3.172 +	pushw	%di
   3.173 +	movb	$6, %cl			# copy 12 bytes
   3.174 +	rep				# don't worry about cld
   3.175 +	movsw				# already done above
   3.176 +	movw	%cx, %ds		# %ds = 0
   3.177 +#ifdef ONLY8086
   3.178 +	popw	0x78(%bx)		# update parameter table address
   3.179 +	popw	0x78+2(%bx)
   3.180 +#else
   3.181 +	popl	0x78(%bx)		# update parameter table address
   3.182 +#endif
   3.183 +	pushw	%ss
   3.184 +	popw	%ds			# now %ds = %es = %ss = INITSEG
   3.185 +	movb	$63, 0x4-12(%di)	# patch sector count, %di = stacktop
   3.186 +
   3.187 +	xchg	%ax, %di		# sector count
   3.188 +	popw	%ax			# limits = 0
   3.189 +	incw	%cx			# cylinder 0, sector 1, clear Z
   3.190 +	call	read_first_sectors	# read setup
   3.191 +
   3.192 +# This routine loads the system at address LOADSEG, making sure
   3.193 +# no 64kB boundaries are crossed. We try to load it as fast as
   3.194 +# possible, loading whole tracks whenever we can.
   3.195 +
   3.196 +	movw	syssize, %di
   3.197 +	decw	%di
   3.198 +	shrclw	$9-4, %di
   3.199 +	incw	%di
   3.200 +	movw	$SYSSEG, %cx
   3.201 +#ifdef BZIMAGE
   3.202 +	push	%cx
   3.203 +#endif
   3.204 +	call	read_sectorsCX
   3.205 +
   3.206 +# This procedure turns off the floppy drive motor, so
   3.207 +# that we enter the kernel in a known state, and
   3.208 +# don't have to worry about it later.
   3.209 +
   3.210 +kill_motor:
   3.211 +	xchgw	%ax, %di		# reset FDC (%di < 128)
   3.212 +	int	$0x13
   3.213 +
   3.214 +# After that (everything loaded), we jump to the setup-routine
   3.215 +# loaded directly after the bootblock:
   3.216 +# Segments are as follows: %ds = %ss = INITSEG
   3.217 +
   3.218 +#ifdef BZIMAGE
   3.219 +	popw	%bx
   3.220 +	popw	%si			// SYSSEG:0
   3.221 +movesys:				// %bx = DS, %si
   3.222 +	movw	$16, %ax
   3.223 +	mulw	%bx
   3.224 +	addw	%si, %ax
   3.225 +	adcw	$0x9300, %dx		// %dx:%ax src flat address
   3.226 +	movw	$9, %cx
   3.227 +zero1:
   3.228 +	pushw	$0			// 2E..1E
   3.229 +	loop	zero1
   3.230 +	//pushl	$0x93100000		// 1A: dest
   3.231 +	pushw	$0x9310
   3.232 +	pushw	%cx
   3.233 +	pushw	$-1			// 18
   3.234 +	pushw	%cx			// 16
   3.235 +	pushw	%dx			// src
   3.236 +	pushw	%ax
   3.237 +	pushw	$-1			// 10
   3.238 +	movb	$8, %cl
   3.239 +	movw	%cx, %bx		// will move 8*64 = 512Kb
   3.240 +zero2:
   3.241 +	pushw	$0			// 0E..00
   3.242 +	loop	zero2
   3.243 +	movw	%sp, %si
   3.244 +	pushw	%ss
   3.245 +	popw	%es
   3.246 +	pushw	%es
   3.247 +	popw	%ds
   3.248 +syslp:
   3.249 +	movb	$0x80, %ch	
   3.250 +	movb	$0x87, %ah
   3.251 +	int	$0x15
   3.252 +	incb	0x14(%si)
   3.253 +	incb	0x1C(%si)
   3.254 +	decw	%bx
   3.255 +	jne	syslp
   3.256 +#endif
   3.257 +jmp_setup:
   3.258 +	cli
   3.259 +	ljmp	$SETUPSEG, $0
   3.260 +
   3.261 +#ifdef EXE_SUPPORT
   3.262 +#ifdef SHUTDOWNDOS
   3.263 +doiret:
   3.264 +	iret
   3.265 +step19:
   3.266 +	pushw	%si
   3.267 +	pushw	%ds
   3.268 +	movw	%sp, %si
   3.269 +	ldsw	%ss:4(%si), %si
   3.270 +	cmpw	$0x19CD, (%si)
   3.271 +	popw    %ds
   3.272 +	popw    %si
   3.273 +	jne	doiret
   3.274 +	xorw	%si, %si
   3.275 +	movw	%si, %ds
   3.276 +	pushw	%cs
   3.277 +	popw	%ss
   3.278 +	movw	$stacktop-4-16, %sp
   3.279 +#ifdef ONLY8086
   3.280 +	popw	4(%si)
   3.281 +	popw	4+2(%si)
   3.282 +	popw	%bp
   3.283 +	popw	%di
   3.284 +	popw	%si
   3.285 +	popw	%dx
   3.286 +	popw	%cx
   3.287 +	popw	%bx
   3.288 +	popw	%ax
   3.289 +#else
   3.290 +	popl	4(%si)
   3.291 +	popaw
   3.292 +#endif
   3.293 +#ifdef BZIMAGE
   3.294 +	jmp	movesys
   3.295 +#endif
   3.296 +#endif
   3.297 +#ifndef BZIMAGE
   3.298 +movesys:				// %ax = SYSSEG, %bx = DS, %si
   3.299 +	shrclw	$4, %si
   3.300 +	addw	%si, %bx
   3.301 +#ifdef TINY_ZIMAGE
   3.302 +	movw	$0xFFFF, %cx
   3.303 +	xorw	%si, %si
   3.304 +	xorw	%di, %di
   3.305 +	cmpw	%ax, %bx
   3.306 +	jc	forward
   3.307 +	decw	%si
   3.308 +	decw	%di
   3.309 +	std
   3.310 +forward:
   3.311 +	movw	%ax, %es
   3.312 +	movw	%bx, %ds
   3.313 +	rep
   3.314 +	  movsb
   3.315 +	cld
   3.316 +#else
   3.317 +	subw	%ax, %bx
   3.318 +	jnc	forward
   3.319 +	movb	$0x90, %ah
   3.320 +	;decw	%ax
   3.321 +forward:
   3.322 +	movw	%ax, %es
   3.323 +	movw	%ax, %di
   3.324 +	addw	%bx, %di
   3.325 +	movw	%di, %ds
   3.326 +	sbbw	%di, %di		// %di = 0 : -1
   3.327 +	cmc				//  C  = 1 :  0
   3.328 +	adcw	%di, %ax
   3.329 +	xorw	%si, %si
   3.330 +	xorw	%di, %di
   3.331 +	movb	$0x10, %cl
   3.332 +	cmpb	%cl, %ah		// move 512k
   3.333 +	rep
   3.334 +	  movsb
   3.335 +	jns	forward
   3.336 +#endif
   3.337 +#ifndef NO_CURSOR_DEFINITION
   3.338 +	movb	$1, %ah
   3.339 +	movb	$0, %bh
   3.340 +	movb	$0x20, %ch	// 0x2000
   3.341 +	int	$0x10
   3.342 +#endif
   3.343 +	pushw	%ss
   3.344 +	popw	%ds
   3.345 +	jmp	jmp_setup
   3.346 +#endif
   3.347 +comstart:
   3.348 +	cld				# assume nothing
   3.349 +#ifdef ONLY8086
   3.350 +	movw	$INITSEG, %ax
   3.351 +	pushw	%ax
   3.352 +#else
   3.353 +	pushw	$INITSEG
   3.354 +#endif
   3.355 +	popw	%es
   3.356 +#ifdef CMDLINE
   3.357 +	movw	%sp, %di
   3.358 +	movw	$0x80, %si
   3.359 +	lodsb
   3.360 +	cbw
   3.361 +	xchgw	%ax, %cx
   3.362 +	jcxz	nocmdline
   3.363 +# if defined(BZIMAGE) && BZIMAGE >= 0x202
   3.364 +	movw	$INITSEG/16+stacktop/256, EXEADRS(cmd_line_ptr+1)
   3.365 +# else
   3.366 +	movw	$0xA33F, 0x7F(%si)
   3.367 +# endif
   3.368 +# ifndef NO_CMDLINE_SHRINK
   3.369 +skipspace:
   3.370 +	lodsb
   3.371 +	cmpb	$0x20, %al
   3.372 +	je	skipspace
   3.373 +#  ifndef NO_CMDLINE_FILE
   3.374 +	movw	%si,%dx
   3.375 +	decw	%si
   3.376 +	subb	$'@',%al
   3.377 +	jne	notafile
   3.378 +	movb	$0x3D,%ah
   3.379 +	int	$0x21
   3.380 +	jc	notafile
   3.381 +	xchgw	%ax,%bx
   3.382 +	//movw	%si,%dx		// ~320 bytes max
   3.383 +	movw	$EXEADRS(notafile)-130,%cx
   3.384 +	movb	$0x3F,%ah
   3.385 +	int	$0x21
   3.386 +	xchgw	%ax,%cx
   3.387 +notafile:
   3.388 +#  else
   3.389 +	decw	%si
   3.390 +#  endif
   3.391 +# endif
   3.392 +	rep
   3.393 +	  movsb
   3.394 +nocmdline:
   3.395 +	orb	EXEADRS(setup_sects), %ch
   3.396 +# ifndef NO_MINSETUP
   3.397 +#  ifndef BZIMAGE
   3.398 +	jnz	setupok
   3.399 +	mov	$4, %ch
   3.400 +setupok:
   3.401 +#  endif
   3.402 +# endif
   3.403 +#else
   3.404 +	movb	EXEADRS(setup_sects), %ch
   3.405 +#endif
   3.406 +	movb	$(512-(end_header-_start))/2, %cl
   3.407 +	movw	$0x100, %si
   3.408 +	movw	$end_header, %di
   3.409 +	rep
   3.410 +	  movsw
   3.411 +	movw	$SYSSEG, %ax
   3.412 +	movw	%ds, %bx
   3.413 +	pushw	%es
   3.414 +	popw	%ss
   3.415 +#ifndef SHUTDOWNDOS
   3.416 +	pushw	%es
   3.417 +	pushw	$movesys
   3.418 +#endif
   3.419 +#ifdef VCPI
   3.420 +	pushaw
   3.421 +	smsww	%ax
   3.422 +	andb	$1, %al
   3.423 +	je	isrm
   3.424 +	movw	$EXEADRS(gdt_vcpi),%si
   3.425 +	movw	$pagebuf-0x90000,%di	// %es = 0x9000
   3.426 +	movl	$pagebuf+3,%es:0x1000(%di)
   3.427 +call_vcpi:
   3.428 +	movb	$0xDE,%ah	// DE01, EBX = getiface(DS:SI, ES:DI)
   3.429 +	int	$0x67
   3.430 +	movl	$FLAT20(sw2pm_params),%esi
   3.431 +	movb	$0x0C,%al	// DE0C switchpm(ESI)
   3.432 +	jmp	call_vcpi
   3.433 +pm_code:
   3.434 +	movl	%cr0,%eax
   3.435 +	andl	$0x7FFFFFFE,%eax
   3.436 +	movl	%eax,%cr0
   3.437 +	movl	%eax,%cr3
   3.438 +isrm:
   3.439 +# ifdef SHUTDOWNDOS
   3.440 +	trace_int19
   3.441 +# else
   3.442 +	lssw	%cs:EXEADRS(saved_ss_sp),%sp	
   3.443 +	popaw
   3.444 +	retf
   3.445 +# endif
   3.446 +#else
   3.447 +# ifdef SHUTDOWNDOS
   3.448 +#  ifdef ONLY8086
   3.449 +	pushw	%ax
   3.450 +	pushw	%bx
   3.451 +	pushw	%cx
   3.452 +	pushw	%dx
   3.453 +	pushw	%si
   3.454 +	pushw	%di
   3.455 +	pushw	%bp
   3.456 +#  else
   3.457 +	pushaw
   3.458 +#  endif
   3.459 +	trace_int19
   3.460 +# endif
   3.461 +	retf
   3.462 +#endif
   3.463 +#endif
   3.464 +
   3.465 +# read_sectors reads %di sectors into %es:0 buffer.
   3.466 +# %es:0 is updated to the next memory location.
   3.467 +# First, sectors are read sector by sector until
   3.468 +# sector per track count is known. Then they are
   3.469 +# read track by track.
   3.470 +# Assume no error on first track.
   3.471 +
   3.472 +#ifdef FLOPPY_1440K_ONLY
   3.473 +#define FLOPPY_HAS_2_SIDES	hardcore heads count to 2
   3.474 +#define FLOPPY_SECTORS		18	/* 18 sectors */
   3.475 +#else
   3.476 +#define FLOPPY_HEADS		2	/* 2 heads minimum */
   3.477 +#endif
   3.478 +
   3.479 +return:
   3.480 +#ifndef NO_DOTS
   3.481 +	movw	$0xE2E,%ax
   3.482 +	int	$0x10
   3.483 +#endif
   3.484 +	ret
   3.485 +
   3.486 +check_limits:
   3.487 +#ifndef FLOPPY_1440K_ONLY
   3.488 +	popw	%dx
   3.489 +        cmpb    %al, %cl		# max sector known ?
   3.490 +        ja	next_head		#   no -> store it
   3.491 +#ifndef FLOPPY_HAS_2_SIDES
   3.492 +#ifdef FLOPPY_HEADS
   3.493 +	cmpb	$FLOPPY_HEADS, %dh	# 2 heads minimum
   3.494 +	jb	check_cylinder
   3.495 +#endif
   3.496 +        cmpb    %ah, %dh		# max head known ?
   3.497 +        ja	next_cylinder		#   no -> store it
   3.498 +check_cylinder:
   3.499 +#endif
   3.500 +	pushw	%ax
   3.501 +	cbw				# %ah = 0
   3.502 +#else
   3.503 +	pushw	%dx
   3.504 +#endif
   3.505 +        int     $0x13			# reset controler
   3.506 +#ifndef FLOPPY_1440K_ONLY
   3.507 +	popw	%ax
   3.508 +	movb	$1, %al			# sector by sector...
   3.509 +#else
   3.510 +	movw	$1, %ax
   3.511 +	jmp	more1trk
   3.512 +#endif
   3.513 +read_sectorslp:
   3.514 +	pushw	%dx			# some bios break dx...
   3.515 +#ifndef FLOPPY_1440K_ONLY
   3.516 +        pushw   %ax			# limits
   3.517 +	subb	%cl, %al		# sectors remaining in track
   3.518 +	ja	tolastsect
   3.519 +	movb	$1, %al			# first track sector by sector
   3.520 +tolastsect:
   3.521 +#else
   3.522 +	movb	$FLOPPY_SECTORS+1, %al
   3.523 +	subb	%cl, %al		# sectors remaining in track
   3.524 +#endif
   3.525 +	cbw
   3.526 +	cmpw	%di, %ax
   3.527 +	jb	more1trk
   3.528 +	movw	%di, %ax		# sectors to read
   3.529 +more1trk:
   3.530 +	pushw	%ax			# save context
   3.531 +	movb	$2, %ah			# cmd: read chs
   3.532 +        int     $0x13
   3.533 +#ifndef FLOPPY_1440K_ONLY
   3.534 +	popw	%dx			# save %ax
   3.535 +        popw    %ax			# limits
   3.536 +#else
   3.537 +	popw	%ax			# restore context
   3.538 +	popw	%dx
   3.539 +#endif
   3.540 +	jc	check_limits
   3.541 +#ifndef FLOPPY_1440K_ONLY
   3.542 +	xchgw	%ax, %bp
   3.543 +	addw	%dx,%cx			# next sector
   3.544 +	movw	%cx, %si
   3.545 +	pushw	%dx
   3.546 +	shlclw	$5, %dx
   3.547 +	movw	%es, %cx
   3.548 +	addw	%dx, %cx
   3.549 +	popw	%dx
   3.550 +	subw	%dx,%di			# update sector counter
   3.551 +	popw	%dx
   3.552 +#else
   3.553 +	addw	%ax,%cx			# next sector
   3.554 +	movw	%cx, %si
   3.555 +	pushw	%ax
   3.556 +	shlclw	$5, %ax
   3.557 +	movw	%es, %cx
   3.558 +	addw	%ax, %cx
   3.559 +	popw	%ax
   3.560 +	subw	%ax,%di			# update sector counter
   3.561 +#endif
   3.562 +read_sectorsCX:
   3.563 +	movw	%cx, %es		# next location
   3.564 +	jz	return
   3.565 +read_sectors:
   3.566 +	movw	%si, %cx
   3.567 +#ifndef FLOPPY_1440K_ONLY
   3.568 +#   al is last sector+1
   3.569 +#   ah is last head+1
   3.570 +	xchgw	%ax, %bp
   3.571 +#endif
   3.572 +#ifndef FLOPPY_1440K_ONLY
   3.573 +        cmpb    %al,%cl			# reach sector limit ?
   3.574 +        jne     bdendlp
   3.575 +next_head:
   3.576 +        movb    %cl,%al
   3.577 +#else
   3.578 +        cmpb    $FLOPPY_SECTORS+1,%cl	# reach sector limit ?
   3.579 +        jne     bdendlp
   3.580 +#endif
   3.581 +        movb    $1,%cl			# first sector
   3.582 +#ifndef FLOPPY_HAS_2_SIDES
   3.583 +        incb    %dh			# next head
   3.584 +        cmpb    %ah, %dh		# reach head limit ?
   3.585 +        jne     bdendlp
   3.586 +next_cylinder:
   3.587 +        movb    %dh,%ah
   3.588 +        movb    $0,%dh			# first head
   3.589 +#else
   3.590 +        xorb    %cl,%dh			# next head
   3.591 +        jne	bdendlp			# reach head limit ?
   3.592 +#endif
   3.593 +# NOTE : support 256 cylinders max
   3.594 +        incb    %ch			# next cylinder
   3.595 +read_first_sectors:
   3.596 +bdendlp:
   3.597 +        jmp	read_sectorslp
   3.598 +
   3.599 +#ifdef VCPI
   3.600 +pagebuf		=	0x98000
   3.601 +tss		=	gdt_abs-40
   3.602 +gdt		=	gdt_abs-32
   3.603 +gdt_null	=	gdt_abs-32
   3.604 +gdt_vcpi	=	gdt_abs-24
   3.605 +gdt_vcpi2	=	gdt_abs-16
   3.606 +gdt_vcpi3	=	gdt_abs-8
   3.607 +gdt_abs:
   3.608 +	.word	0xFFFF
   3.609 +	.long	0x92000000
   3.610 +	.byte	0xCF,0
   3.611 +gdt_code:
   3.612 +	.word	0xFFFF
   3.613 +gdt_code_base:
   3.614 +	.long	0x9A000000+FLAT20(0)
   3.615 +	.byte	0x8F,0
   3.616 +gdt_tss:
   3.617 +	.word	0x00FF
   3.618 +gdt_tss_base:
   3.619 +	.long	0x89000000+FLAT20(tss)
   3.620 +	.byte	0,0
   3.621 +gdtr:
   3.622 +gdt_lim:
   3.623 +	.word	0xFFFF
   3.624 +gdt_base:
   3.625 +	.long	FLAT20(gdt)
   3.626 +sw2pm_params:
   3.627 +sw2pm_cr3:
   3.628 +	.long	pagebuf+0x1000
   3.629 +sw2pm_gdtr_ptr:
   3.630 +	.long	FLAT20(gdtr)
   3.631 +sw2pm_idtr_ptr:
   3.632 +	.long	FLAT20(idtr)
   3.633 +sw2pm_ldtr:
   3.634 +	.word	0
   3.635 +sw2pm_tr:
   3.636 +SEL_TSS		= gdt_tss-gdt_null
   3.637 +	.word	SEL_TSS
   3.638 +sw2pm_jumpaddr:
   3.639 +	.long	pm_code
   3.640 +SEL_CODE	= gdt_code-gdt_null
   3.641 +	.word	SEL_CODE
   3.642 +idtr:
   3.643 +idt_lim:
   3.644 +	.word	0x03FF
   3.645 +idt_base:
   3.646 +	.long	0
   3.647 +# ifndef SHUTDOWNDOS
   3.648 +saved_ss_sp:
   3.649 +	.word	stacktop-4-16-4,INITSEG
   3.650 +# endif
   3.651 +#endif
   3.652 +#ifdef ELKS
   3.653 +	.org	0x1E3
   3.654 +	.byte	13,10,7
   3.655 +	.asciz	"ELKS Boot"
   3.656 +#endif
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/plop/stuff/pack	Thu Sep 19 20:20:35 2019 +0200
     4.3 @@ -0,0 +1,76 @@
     4.4 +#!/bin/sh
     4.5 +if [ "$1" == "--build" ]; then
     4.6 +	set -- ${2:-bootloader.bin} ${3:-unpack.bin}
     4.7 +	x=$(grep -s packed_syssize$ ${2/.bin/.lst}|sed '$!d;s/.*t:\([^ ]*\).*/\1/')
     4.8 +	[ -n "$x" ] && sed -i "s/XXXP/$((513+0x$x))/" $0 || sed -i "/XXXP$/d" $0
     4.9 +	x=$(grep -s setup_byte$ ${2/.bin/.lst}|sed '$!d;s/.*t:\([^ ]*\).*/\1/')
    4.10 +	[ -n "$x" ] && sed -i "s/XXXSB/$((513+0x$x))/" $0 || sed -i "/XXXSB/d" $0
    4.11 +	x=$(grep -s setup_word$ ${2/.bin/.lst}|sed '$!d;s/.*t:\([^ ]*\).*/\1/')
    4.12 +	[ -n "$x" ] && sed -i "s/XXXSW/$((514+0x$x))/" $0 || sed -i "/XXXSW/d" $0
    4.13 +	x=$(grep -s setup_para$ ${2/.bin/.lst}|sed '$!d;s/.*t:\([^ ]*\).*/\1/')
    4.14 +	[ -n "$x" ] && sed -i "s/XXXSP/$((514+0x$x))/" $0 || sed -i "/XXXSP/d" $0
    4.15 +	x=$(grep -s setup_seg$ ${2/.bin/.lst}|sed '$!d;s/.*t:\([^ ]*\).*/\1/')
    4.16 +	[ -n "$x" ] && sed -i "s/XXXSS/$((513+0x$x))/" $0 || sed -i "/XXXSS/d" $0
    4.17 +	x=$(grep -s setup_keep$ ${2/.bin/.lst}|sed '$!d;s/.*t:\([^ ]*\).*/\1/')
    4.18 +	[ -n "$x" ] && sed -i "s/XXXK1/1/" $0 || sed -i "/XXXK1/d" $0
    4.19 +	[ -n "$x" ] && sed -i "s/skip=XXXK2 //" $0 || sed -i "s/XXXK2/1/" $0
    4.20 +	[ -n "$x" ] && sed -i "s/+XXXK3//" $0 || sed -i "s/XXXK3/1/" $0
    4.21 +	grep -q BFF401 ${2/.bin/.lst} && sed -i 's/512 skip/500 skip/;s/1000 -/&32 - /' $0
    4.22 +	grep -q 83C60D ${2/.bin/.lst} && sed -i 's/ | dd bs=1 skip=13//' $0
    4.23 +	helpmsg=$(grep -s helpmsg$ ${1/.bin/.lst}|sed '$!d;s/.*t:\([^ ]*\).*/\1/')
    4.24 +	helpend=$(grep -s helpend$ ${1/.bin/.lst}|sed '$!d;s/.*t:\([^ ]*\).*/\1/')
    4.25 +	if [ -n "$helpmsg" ]; then
    4.26 +		sed -i "s/YYY/$((0x$helpmsg))/" $0
    4.27 +		sed -i "s/ZZZ/$((0x$helpend-0x$helpmsg))/" $0
    4.28 +	else
    4.29 +		sed -i '/YYY/d' $0
    4.30 +	fi
    4.31 +	cat >> $0 <<EOM
    4.32 +$(dd if=$1 bs=512 count=1 conv=sync 2> /dev/null | cat - $2 | gzip -9 | uuencode -m -)
    4.33 +EOT
    4.34 +EOM
    4.35 +	sed -i '/--build/,/^fi/d' $0
    4.36 +	exit
    4.37 +fi
    4.38 +
    4.39 +store()
    4.40 +{
    4.41 +	n=$1
    4.42 +	for i in $(seq 1 ${4:-2}); do
    4.43 +		printf '\\\\x%02X' $(($n & 255))
    4.44 +		n=$(($n >> 8))
    4.45 +	done | xargs echo -en | dd conv=notrunc bs=1 of=$2 seek=$3
    4.46 +}
    4.47 +
    4.48 +compress()
    4.49 +{
    4.50 +	xz -z -e --format=lzma --lzma1=mf=bt2 --stdout | dd bs=1 skip=13
    4.51 +}
    4.52 +
    4.53 +main()
    4.54 +{
    4.55 +	uudecode | gunzip >$2
    4.56 +	dd if=$1 bs=1 skip=497 seek=497 count=15 of=$2 conv=notrunc
    4.57 +	setup="$(echo $(od -j 497 -N 1 -dAn $1))"
    4.58 +	[ $setup -eq 0 ] && setup=4
    4.59 +	setup=$(($setup+XXXK1))
    4.60 +	store $setup $2 XXXSB 1
    4.61 +	store $setup $2 XXXSW 1
    4.62 +	store $((32 * $setup)) $2 XXXSP
    4.63 +	store $((0x1000 - 32*$setup)) $2 XXXSS 
    4.64 +	dd if=$1 bs=512 skip=XXXK2 | compress >>$2
    4.65 +	size=$(stat -c %s $2)
    4.66 +	syssize=$((($size-512*($setup+XXXK3)+15)/16))
    4.67 +	store $(($syssize + 0x1000)) $2 XXXP
    4.68 +	store $syssize $2 500
    4.69 +	store 4 $2 497 1
    4.70 +	store $((($size+511)/512)) $2 4
    4.71 +	store $((($size+15)/16)) $2 14
    4.72 +	echo -e "$3" | dd conv=notrunc bs=1 seek=YYY of=$2 count=ZZZ
    4.73 +}
    4.74 +
    4.75 +HELP="$(unix2dos <<EOT
    4.76 +Plop Boot Manager VERSION
    4.77 +EOT
    4.78 +)"
    4.79 +main $1 ${2:-$1.packed} "${3:-$HELP}" 2>/dev/null <<EOT
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/plop/stuff/unlzma.S	Thu Sep 19 20:20:35 2019 +0200
     5.3 @@ -0,0 +1,1019 @@
     5.4 +// #define RC_NORMALIZE if (Range < kTopValue) { Range <<= 8; Code = (Code << 8) | RC_READ_BYTE; }
     5.5 +//
     5.6 +// #define IfBit0(p) RC_NORMALIZE; bound = (Range >> kNumBitModelTotalBits) * *(p); if (Code < bound)
     5.7 +// #define UpdateBit0(p) Range = bound; *(p) += (kBitModelTotal - *(p)) >> kNumMoveBits;
     5.8 +// #define UpdateBit1(p) Range -= bound; Code -= bound; *(p) -= (*(p)) >> kNumMoveBits;
     5.9 +//
    5.10 +//#define RC_GET_BIT2(p, mi, A0, A1) IfBit0(p) \
    5.11 +//  { UpdateBit0(p); mi <<= 1; A0; } else \
    5.12 +//  { UpdateBit1(p); mi = (mi + mi) + 1; A1; }
    5.13 +//
    5.14 +// #define RC_GET_BIT(p, mi) RC_GET_BIT2(p, mi, ; , ;)
    5.15 +//
    5.16 +// #define RangeDecoderBitTreeDecode(probs, numLevels, res) \
    5.17 +//  { int i = numLevels; res = 1; \
    5.18 +//  do { CProb *p = probs + res; RC_GET_BIT(p, res) } while(--i != 0); \
    5.19 +//  res -= (1 << numLevels); }
    5.20 +/*
    5.21 + * Compression with : lzma e src dst -eos -pb2 -lp0 -lc3
    5.22 + */
    5.23 +
    5.24 +#define PROP_PB 2
    5.25 +#define PROP_LP 0
    5.26 +#define PROP_LC 3
    5.27 +#define PROPS (PROP_LC+(PROP_LP*9)+(PROP_PB*45))
    5.28 +
    5.29 +// static const Byte *Buffer;
    5.30 +// static UInt32 bound, Code, Range;
    5.31 +
    5.32 +/*
    5.33 + * Buffer register DS:SI
    5.34 + * all var based ws=ss:bp
    5.35 + */
    5.36 +
    5.37 +rep0		=	-4		// long
    5.38 +rep1		=	rep0-4		// long
    5.39 +rep2		=	rep0-8		// long
    5.40 +rep3		=	rep0-12		// long
    5.41 +state		=	-17		// byte, 0..11
    5.42 +posState 	=	state-1		// byte, 0..15
    5.43 +posState2 	=	posState-1	// byte, 0..15
    5.44 +scratched	=	rep0-16		// byte = 1
    5.45 +Code		=	-24		// long
    5.46 +outStream	=	-28		// long
    5.47 +nowPos		=	outStream	// long
    5.48 +Range		=	Code-8		// long
    5.49 +#define LOCALS		32
    5.50 +
    5.51 +// int LzmaDecode(CLzmaDecoderState *vs,
    5.52 +//     const unsigned char *inStream, 
    5.53 +//     unsigned char *outStream)
    5.54 +// {
    5.55 +//   CProb *p = vs->Probs;
    5.56 +//   SizeT nowPos = 0;
    5.57 +//   #define posStateMask = (1 << (vs->Properties.pb)) - 1;
    5.58 +//   #define literalPosMask = (1 << (vs->Properties.lp)) - 1;
    5.59 +//   int lc = vs->Properties.lc, state = 0, len = 0;
    5.60 +//   UInt32 rep0 = 1, rep1 = 1, rep2 = 1, rep3 = 1;
    5.61 +// 
    5.62 +//   {
    5.63 +//     UInt32 i, numProbs = Literal /*1846*/
    5.64 +// 	    + ((UInt32)LZMA_LIT_SIZE /*768*/ << (lc + vs->Properties.lp));
    5.65 +//     for (i = 0; i < numProbs; i++) p[i] = kBitModelTotal /*2048*/ >> 1;
    5.66 +
    5.67 +#define WS (1846+(768<<(PROP_LC+PROP_LP)))
    5.68 +#if (WS+WS+LOCALS) >= 65000
    5.69 +/* MAX WS = (1846+(768<<(8+4))) > 3MB! */
    5.70 +#error invalid (lc,lp,pb) : out of memory
    5.71 +#endif
    5.72 +
    5.73 +ws1	=	WS
    5.74 +ws2	=	ws1*2
    5.75 +ws	=	ws2+LOCALS+15
    5.76 +
    5.77 +#ifndef FLAT32
    5.78 +#define	AX	%ax
    5.79 +#define	BX	%bx
    5.80 +#define	CX	%cx
    5.81 +#define	DX	%dx
    5.82 +#define	SI	%si
    5.83 +#define	DI	%di
    5.84 +#define	BP	%bp
    5.85 +#define	SP	%sp
    5.86 +#define CWD	cwd
    5.87 +#else
    5.88 +#define	AX	%eax
    5.89 +#define	BX	%ebx
    5.90 +#define	CX	%ecx
    5.91 +#define	DX	%edx
    5.92 +#define	SI	%esi
    5.93 +#define	DI	%edi
    5.94 +#define	BP	%ebp
    5.95 +#define	SP	%esp
    5.96 +#define CWD	cdq
    5.97 +#endif
    5.98 +/*
    5.99 + * LzmaDecode:
   5.100 +#ifndef FLAT32
   5.101 + *   input   ds:si=inStream, es:di=outStream
   5.102 + *   output  outStream[], ds:si, es:di
   5.103 + 	.code 16
   5.104 +#else
   5.105 + *   input   esi=inStream, edi=outStream
   5.106 + *   output  outStream[], esi, edi
   5.107 + 	.code 32
   5.108 +#endif
   5.109 + */
   5.110 + 
   5.111 +	mov	$ws1, CX
   5.112 +#ifdef ONLY8086
   5.113 +	movw	$2048/2, %ax
   5.114 +lzd1:
   5.115 +	pushw	%ax
   5.116 +#else
   5.117 +lzd1:
   5.118 +	pushw	$2048/2
   5.119 +#endif
   5.120 +	loop	lzd1
   5.121 +	mov	SP, BP
   5.122 +	movb	$((LOCALS+3)/4)*2, %cl
   5.123 +#ifdef ONLY8086
   5.124 +	movw	$1, %ax
   5.125 +	cwd
   5.126 +initlocals:
   5.127 +	pushw	%dx
   5.128 +	pushw	%ax
   5.129 +#else
   5.130 +initlocals:
   5.131 +	pushl	$1
   5.132 +#endif
   5.133 +	loop	initlocals
   5.134 +
   5.135 +#if !defined(FLAT32) && !defined(FLAT16OUT)
   5.136 +	movb	$4, %cl
   5.137 +	movw	%es, %bx
   5.138 +	shrw	%cl, %bx
   5.139 +	movw	%es, %dx
   5.140 +	shlw	%cl, %dx
   5.141 +	addw	%dx, %di
   5.142 +	movw	%di, outStream(%bp)
   5.143 +	adcb	%bh, outStream+2(%bp)
   5.144 +	incw	%cx
   5.145 +#else
   5.146 +	movb	$5, %cl
   5.147 +	mov	DI, outStream(BP)
   5.148 +#endif
   5.149 +
   5.150 +//   Byte previousByte = 0;
   5.151 +	xor	BX, BX
   5.152 +
   5.153 +// #define RC_INIT(buffer) 
   5.154 +//    Buffer = buffer; Code = 0; Range = 0xFFFFFFFF; 
   5.155 +//    { int i; for(i=0; i<5; i++) { Code = (Code<<8) | RC_READ_BYTE; }}
   5.156 +//   }
   5.157 +//   RC_INIT(inStream);
   5.158 +
   5.159 +#ifndef NO_LZMA_HEADER
   5.160 +#ifdef CHECK_LZMA_HEADER
   5.161 +	cmp.w	$0x5A4C, (SI)	// lzip header ('LZIP' version:1 dicobits:1)
   5.162 +	je	lzip_header
   5.163 +	cmp.w	$0x5D, (SI)	// lzma header (0x5D dicosz:4 orgsz:8)
   5.164 +	jne	no_header
   5.165 +	add	$13-6, SI	// skip lzma header
   5.166 +lzip_header:
   5.167 +	add	$6, SI		// skip lzip header
   5.168 +no_header:
   5.169 +#else
   5.170 +	add	$13, SI		// skip lzma header (0x5D dicosz:4 orgsz:8)
   5.171 +#endif
   5.172 +#endif
   5.173 +setrep:
   5.174 +	call	RC_LOAD_BYTE
   5.175 +	decb	Range(BP)
   5.176 +	loop	setrep
   5.177 +
   5.178 +lzdmainlp:
   5.179 +//   while(1) {
   5.180 +//     CProb *prob;
   5.181 +//     int posState = (int)((nowPos) & posStateMask);
   5.182 +// 
   5.183 +//     prob = p + IsMatch /*0*/ + (state << kNumPosBitsMax /*4*/) + posState;
   5.184 +//     if (Bit0(prob)) { /* char */
   5.185 +
   5.186 +	xor	DX, DX
   5.187 +	call	Bit1state	// Bit1(dx + (state << kNumPosBitsMax /*4*/) + posState)
   5.188 +	mov	$state, DI
   5.189 +	jc	lzdstring
   5.190 +
   5.191 +//       prob = p + Literal /*1846*/ + (LZMA_LIT_SIZE /*768*/ * 
   5.192 +// 	((((nowPos) & literalPosMask) << lc) + (previousByte >> (8 - lc))));
   5.193 +
   5.194 +#if PROP_LC != 0
   5.195 +# ifdef ONLY8086
   5.196 +	movb	$8-PROP_LC, %cl
   5.197 +	shrb	%cl, %bl
   5.198 +# else
   5.199 +	shrb	$8-PROP_LC, %bl
   5.200 +# endif
   5.201 +#endif
   5.202 +
   5.203 +#if PROP_LP != 0
   5.204 +	movb	posState2(BP), %dl
   5.205 +# ifdef ONLY8086
   5.206 +	movb	$PROP_LC, %cl
   5.207 +	shl	%cl, DX
   5.208 +# else
   5.209 +	shl	$PROP_LC, DX
   5.210 +# endif
   5.211 +	movb	$0, %bh
   5.212 +	add	BX, DX
   5.213 +#endif
   5.214 +
   5.215 +	movb	$3, %ah
   5.216 +	mul	BX		// dx = 3*bh
   5.217 +	add	$1846, AX
   5.218 +
   5.219 +//       int symbol = 1;
   5.220 +
   5.221 +	CWD
   5.222 +	inc	DX		// symbol = 1
   5.223 +	xchg	AX, CX		// save prob
   5.224 +
   5.225 +//       if (state >= kNumLitStates /*7*/) { /* previous was string */
   5.226 +//       if (state < 4) state = 0;
   5.227 +
   5.228 +lzd6z:
   5.229 +	subb	$3, (BP, DI)
   5.230 +
   5.231 +//       if (state < 4) state = 0;
   5.232 +
   5.233 +	jnc	lzd6
   5.234 +	movb	%dh, (BP, DI)	// %dh = 0
   5.235 +
   5.236 +lzd6:
   5.237 +//       else if (state < 10) state -= 3;
   5.238 +
   5.239 +	cmpb	$10-3, (BP, DI)
   5.240 +	
   5.241 +//       else state -= 6;
   5.242 +
   5.243 +	jnb	lzd6z
   5.244 +	cmpb	$7-3-1, (BP, DI)
   5.245 +	jbe	lzd3
   5.246 +	
   5.247 +//         int matchByte = outStream[nowPos - rep0];
   5.248 +
   5.249 +	call	DicoRep02ESDI	// %bl = outStream[nowPos - rep0];
   5.250 +	
   5.251 +//         do {
   5.252 +//           int bit;
   5.253 +//           CProb *probLit;
   5.254 +//           matchByte <<= 1; bit = (matchByte & 0x100);
   5.255 +
   5.256 +	movb	$1, %bh
   5.257 +lzd4:
   5.258 +	shlb	$1, %bl			// matchByte <<= 1
   5.259 +	sbb	DI, DI			// save bit=C
   5.260 +
   5.261 +//           probLit = prob + 0x100 + bit + symbol;
   5.262 +
   5.263 +	mov	CX, AX			// restore prob
   5.264 +	adcb	%bh, %ah		// + bit + 0x100
   5.265 +	
   5.266 +//           RC_GET_BIT2(probLit, symbol, if (bit) break, if (!bit) break)
   5.267 +
   5.268 +	call	Bit1axdx		// C,%ax = Bit1(prob+%ax)
   5.269 +	rclb	$1, %dl			// symbol <<= 1; symbol |= C
   5.270 +	jc	lzd5			// if symbol >= 0x100
   5.271 +	cmp	DI, AX
   5.272 +	jz	lzd4			// if bit == Bit1(prob+%ax)
   5.273 +
   5.274 +//         } while (symbol < 0x100);
   5.275 +//       }
   5.276 +lzd3:
   5.277 +//       while (symbol < 0x100) {
   5.278 +//         CProb *probLit = prob + symbol;
   5.279 +//         RC_GET_BIT(probLit, symbol)
   5.280 +//       }
   5.281 +
   5.282 +	xor	BX, BX
   5.283 +	jmp	lzd4
   5.284 +lzd5:
   5.285 +
   5.286 +//       outStream[nowPos++] = previousByte = (Byte)symbol;
   5.287 +
   5.288 +	xchg	AX, DX
   5.289 +	call	outchar		// %bl = outStream[nowPos++] = %al;
   5.290 +	jmp	lzdmainlp
   5.291 +
   5.292 +//     }
   5.293 +
   5.294 +lzdstring:
   5.295 +	mov	$1, CX
   5.296 +
   5.297 +//     else { /* string */
   5.298 +//       prob = p + IsRep /*192*/ + state;
   5.299 +
   5.300 +	movb	$192, %dl
   5.301 +	addb	(BP, DI), %dl
   5.302 +	mov	$rep0, DI
   5.303 +
   5.304 +//       if (Bit0(prob)) {
   5.305 +
   5.306 +	call	Bit1dx		// Bit1(prob)
   5.307 + 	jc	lzd8
   5.308 +
   5.309 +//         rep3 = rep2; rep2 = rep1; rep1 = rep0;
   5.310 +//         state = (state < kNumLitStates /*7*/) ? 0 : 3;
   5.311 +
   5.312 +	stc
   5.313 +
   5.314 +//         prob = p + LenCoder /*818*/;
   5.315 +
   5.316 +	mov	$818, DX
   5.317 +
   5.318 +//       }
   5.319 +
   5.320 +	jmp	lzd11a
   5.321 +
   5.322 +//       else {
   5.323 +lzd8:
   5.324 +//         prob += kNumStates /*12*/;
   5.325 +//         if (Bit0(prob)) {
   5.326 +	call	Bit1dx12	// prob += 12; Bit1(prob)
   5.327 +	jc	lzd11
   5.328 +//           prob = p + IsRep0Long /*240*/ + (state << kNumPosBitsMax /*4*/) 
   5.329 +// 		   + posState;
   5.330 +	movb	$240, %dl	// dh=0
   5.331 +
   5.332 +//           if (Bit0(prob)) {
   5.333 +
   5.334 +	call	Bit1state	// Bit1(dx + (state << kNumPosBitsMax /*4*/) + posState)
   5.335 +	jc	lzd12
   5.336 +
   5.337 +//             // if (nowPos == 0) return LZMA_RESULT_DATA_ERROR;
   5.338 +//             state = (state < kNumLitStates /*7*/) ? 9 : 11;
   5.339 +
   5.340 +	movb	$9, %dl
   5.341 +
   5.342 +//             len++; goto string;
   5.343 +	jmp	lzd13string	// ax = 0
   5.344 +//           }
   5.345 +//         }
   5.346 +//         else {
   5.347 +lzd11:
   5.348 +//           UInt32 distance = rep1;
   5.349 +//           prob += kNumStates /*12*/;
   5.350 +//           if (!Bit0(prob)) {
   5.351 +
   5.352 +	call	Bit1dx12	// prob += 12; Bit1(prob)
   5.353 +	jnc	lzd11z
   5.354 +
   5.355 +//             prob += kNumStates /*12*/;
   5.356 +//             if (Bit0(prob)) distance = rep2;
   5.357 +
   5.358 +	call	Bit1dx12	// prob += 12; Bit1(prob)
   5.359 +lzd11a:
   5.360 +	adcb	%cl, %cl
   5.361 +	
   5.362 +//             else { distance = rep3; rep3 = rep2; }
   5.363 +//             rep2 = rep1;
   5.364 +//           }
   5.365 +//           rep1 = rep0; rep0 = distance;
   5.366 +
   5.367 +lzd11z:
   5.368 +# ifdef ONLY8086
   5.369 +	shl	$1, CX
   5.370 +	shl	$1, CX		// 8->32 bits
   5.371 +	sub	CX, DI		// &rep[cx]
   5.372 +	movw	(BP, DI), %ax
   5.373 +	pushw	2(BP, DI)
   5.374 +rotreplp:
   5.375 +	movb	4(BP, DI), %bl
   5.376 +	movb	%bl, (BP, DI)
   5.377 +	inc	DI
   5.378 +	loop	rotreplp
   5.379 +	popw	%bx
   5.380 +	testb	%dh, %dh
   5.381 +	jnz	lzd10
   5.382 +	movw	%ax, (BP, DI)
   5.383 +	movw	%bx, 2(BP, DI)
   5.384 +# else
   5.385 +	shl	$2, CX		// 8->32 bits
   5.386 +	sub	CX, DI		// &rep[cx]
   5.387 +	movl	(BP, DI), %eax
   5.388 +rotreplp:
   5.389 +	movb	4(BP, DI), %bl
   5.390 +	movb	%bl, (BP, DI)
   5.391 +	inc	DI
   5.392 +	loop	rotreplp
   5.393 +	testb	%dh, %dh
   5.394 +	jnz	lzd10
   5.395 +	movl	%eax, (BP, DI)
   5.396 +# endif
   5.397 +
   5.398 +//         }
   5.399 +lzd12:
   5.400 +//         state = (state < kNumLitStates /*7*/) ? 8 : 11;
   5.401 +
   5.402 +	movb	$0x08, %cl
   5.403 +
   5.404 +//         prob = p + RepLenCoder /*1332*/;
   5.405 +
   5.406 +	mov	$1332, DX
   5.407 +
   5.408 +//       }
   5.409 +lzd10:
   5.410 +	push	CX		// CX = 0
   5.411 +
   5.412 +//       { /* get len */
   5.413 +//         int numBits, offset;
   5.414 +//         CProb *probLen = prob + LenChoice /*0*/;
   5.415 +//         numBits = kLenNumLowBits /*3*/;
   5.416 +
   5.417 +	movb	$8, %cl		// numBits : 3,3,8
   5.418 +
   5.419 +//         if (Bit0(probLen)) {
   5.420 +
   5.421 +	call	Bit1dx		// Bit1(prob)
   5.422 +	xchg	AX, BX
   5.423 +	inc	DX
   5.424 +	jnc	lzd15		// bx=0
   5.425 +
   5.426 +//           probLen = prob + LenLow/*2*/ + (posState << kLenNumLowBits/*3*/);
   5.427 +//           offset = 0;
   5.428 +//         }
   5.429 +//         else {
   5.430 +//           probLen = prob + LenChoice2 /*1*/;
   5.431 +
   5.432 +	call	Bit1dx		// Bit1(prob)
   5.433 +	add	AX, BX
   5.434 +
   5.435 +#if PROP_PB != 0
   5.436 +	inc	AX		// ah=0
   5.437 +#endif
   5.438 +	jc	lzd16		// %ax=0, %bx=-2 
   5.439 +lzd15:
   5.440 +#if PROP_PB != 0
   5.441 +	movb	$8, %al
   5.442 +	mulb	posState(BP)
   5.443 +#endif
   5.444 +
   5.445 +//           if (Bit0(probLen)) {
   5.446 +//             probLen = prob + LenMid/*130*/ + (posState << kLenNumMidBits/*3*/);
   5.447 +
   5.448 +	movb	$3, %cl		// numBits : 3,3,8
   5.449 +lzd16:
   5.450 +#if PROP_PB != 0
   5.451 +	add	$2-128-1, AX	// probLen : 2,130,258
   5.452 +#else
   5.453 +	mov	$2-128-1, AX	// probLen : 2,130,258
   5.454 +#endif
   5.455 +	add	DX, AX
   5.456 +	mov	$-8+1, DX	// offset  : 0,8,16
   5.457 +lzdargslp:
   5.458 +	add	$8, DX
   5.459 +	add	$128, AX
   5.460 +	inc	BX
   5.461 +	jle	lzdargslp	// leave with bx=1
   5.462 +
   5.463 +//             offset = kLenNumLowSymbols /*8*/;
   5.464 +//             //numBits = kLenNumMidBits /*3*/;
   5.465 +//           }
   5.466 +//           else {
   5.467 +//             probLen = prob + LenHigh /*258*/;
   5.468 +//             offset = kLenNumLowSymbols /*8*/ + kLenNumMidSymbols /*8*/;
   5.469 +//             numBits = kLenNumHighBits /*8*/;
   5.470 +//           }
   5.471 +//         }
   5.472 +//         RangeDecoderBitTreeDecode(probLen, numBits, len); len += offset;
   5.473 +
   5.474 +	push	DX
   5.475 +	call	RangeDecoder	// %ax=probs, %cx=numLevels, %ax=res
   5.476 +	pop	DX
   5.477 +	add	DX, AX		// offset
   5.478 +	pop	DX		// 0
   5.479 +lzd13string:
   5.480 +	push	AX
   5.481 +
   5.482 +// state = (state < kNumLitStates /*7*/) ? dl : dl|3;
   5.483 +
   5.484 +	movb	$7, %cl
   5.485 +	cmpb	%cl, state(BP)
   5.486 +	jb	new_state
   5.487 +	orb	$3, %dl
   5.488 +new_state:
   5.489 +	movb	%dl, state(BP)
   5.490 +
   5.491 +//       } /* get len */
   5.492 +//       if (state < 4) {
   5.493 +
   5.494 +	cmpb	$4-1, %dl
   5.495 +	ja	lzd19
   5.496 +
   5.497 +//         int posSlot;
   5.498 +//         state += kNumLitStates /*7*/;
   5.499 +
   5.500 +	addb	%cl, state(BP)
   5.501 +
   5.502 +//         prob = p + PosSlot /*432*/ + (((len < kNumLenToPosStates /*4*/) ? 
   5.503 +// 		len : kNumLenToPosStates - 1) << kNumPosSlotBits /*6*/);
   5.504 +
   5.505 +	cmp	$4+1, AX
   5.506 +	jb	lzd21
   5.507 +	mov	$3+1, AX
   5.508 +
   5.509 +lzd21:
   5.510 +
   5.511 +	dec	CX		// cx = 6
   5.512 +	shl	%cl, AX
   5.513 +	add	$432-64, AX
   5.514 +
   5.515 +//         RangeDecoderBitTreeDecode(prob, kNumPosSlotBits /*6*/, posSlot);
   5.516 +
   5.517 +	call	RangeDecoder	// %ax=probs, %cx=numLevels, %ax=res
   5.518 +
   5.519 +//         if (posSlot >= kStartPosModelIndex /*4*/) {
   5.520 +//           int numDirectBits = ((posSlot >> 1) - 1);
   5.521 +
   5.522 +#ifndef FLAT32
   5.523 +	movw	%cx, 2(%bp, %di)	// %cx = 0
   5.524 +#endif
   5.525 +	mov	AX, (BP, DI)
   5.526 +	mov	AX, CX
   5.527 +	shrw	$1, CX
   5.528 +	dec	CX
   5.529 +	cmpb	$4, %al
   5.530 +	jb	lzd22
   5.531 +
   5.532 +//           rep0 = (2 | ((UInt32)posSlot & 1));
   5.533 +
   5.534 +	andb	%bl, (BP, DI)		// %bx=1
   5.535 +	orb	$2, (BP, DI)
   5.536 +
   5.537 +//           if (posSlot < kEndPosModelIndex /*14*/) {
   5.538 +
   5.539 +	cmpb	$14, %al
   5.540 +	jnb	lzd23
   5.541 +
   5.542 +//             rep0 <<= numDirectBits;
   5.543 +
   5.544 +	neg	AX
   5.545 +# ifdef ONLY8086
   5.546 +	pushw	%cx
   5.547 +	movb	$0, %ch
   5.548 +shllrep0:
   5.549 +	shlw	$1, (BP, DI)
   5.550 +	rclw	$1, 2(BP, DI)
   5.551 +	loop	shllrep0
   5.552 +	popw	%cx
   5.553 +# else
   5.554 +	shll	%cl, (BP, DI)
   5.555 +# endif
   5.556 +	add	(BP, DI), AX
   5.557 +
   5.558 +//             prob = p + SpecPos /*688*/ + rep0 - posSlot - 1;
   5.559 +
   5.560 +	add	$687, AX
   5.561 +	jmp	lzd24
   5.562 +
   5.563 +//           }
   5.564 +//           else {
   5.565 +lzd23:
   5.566 +//             numDirectBits -= kNumAlignBits /*4*/;
   5.567 +//             do {
   5.568 +//               RC_NORMALIZE; Range >>= 1; rep0 <<= 1;
   5.569 +//               if (Code >= Range) { Code -= Range; rep0 |= 1; }
   5.570 +
   5.571 +lzd23z:
   5.572 +	call	RC_NORMALIZE
   5.573 +# ifdef ONLY8086
   5.574 +	pushw	%dx
   5.575 +	shrw	$1, Range+2(BP)
   5.576 +	rcrw	$1, Range(BP)
   5.577 +	movw	Range(BP), %ax
   5.578 +	movw	Range+2(BP), %dx
   5.579 +	cmpw	Code+2(BP), %dx
   5.580 +	ja	lzd25
   5.581 +	jb	lzd25x
   5.582 +	cmpw	Code(BP), %ax
   5.583 +	ja	lzd25
   5.584 +lzd25x:
   5.585 +	subw	%ax, Code(BP)
   5.586 +	sbbw	%dx, Code+2(BP)
   5.587 +	stc
   5.588 +lzd25:
   5.589 +	popw	%dx
   5.590 +	rclw	$1, (BP, DI)
   5.591 +	rclw	$1, 2(BP, DI)
   5.592 +# else
   5.593 +	shrl	$1, Range(BP)
   5.594 +	movl	Range(BP), %eax
   5.595 +	cmpl	Code(BP), %eax
   5.596 +	ja	lzd25
   5.597 +	subl	%eax, Code(BP)
   5.598 +	stc
   5.599 +lzd25:
   5.600 +	rcll	$1, (BP, DI)
   5.601 +# endif
   5.602 +
   5.603 +//             } while (--numDirectBits != 0);
   5.604 +
   5.605 +	cmpb	$4+1, %cl
   5.606 +	loopne	lzd23z
   5.607 +
   5.608 +//             prob = p + Align /* 802 */; numDirectBits = kNumAlignBits /*4*/;
   5.609 +//             rep0 <<= numDirectBits;
   5.610 +
   5.611 +# ifdef ONLY8086
   5.612 +	pushw	%cx
   5.613 +	movb	$0, %ch
   5.614 +shlrep0:
   5.615 +	shlw	$1, (BP, DI)
   5.616 +	rclw	$1, 2(BP, DI)
   5.617 +	loop	shlrep0
   5.618 +	popw	%cx
   5.619 +# else
   5.620 +	shll	%cl, (BP, DI)
   5.621 +# endif
   5.622 +	mov	$802, AX
   5.623 +//           }
   5.624 +
   5.625 +lzd24:
   5.626 +	call	RangeDecoder	// %ax=probs, %cx=numLevels, %ax=res
   5.627 +
   5.628 +//           {
   5.629 +//             int i = 1, mi = 1;
   5.630 +//             do {
   5.631 +//               CProb *prob3 = prob + mi;
   5.632 +//               RC_GET_BIT2(prob3, mi, ; , rep0 |= i);
   5.633 +
   5.634 +	orb	%dh, (BP, DI)	// update rep0 with DirectBits
   5.635 +
   5.636 +//               i <<= 1;
   5.637 +//             } while(--numDirectBits != 0);
   5.638 +//           }
   5.639 +//         } else rep0 = posSlot;
   5.640 +lzd22:
   5.641 +//         if (++rep0 == (UInt32)(0)) break; /* EOF */
   5.642 +
   5.643 +# ifdef ONLY8086
   5.644 +	incw	(BP, DI)
   5.645 +	jnz	lzd19
   5.646 +	incw	2(BP, DI)
   5.647 +# else
   5.648 +	incl	(BP, DI)
   5.649 +# endif
   5.650 +
   5.651 +lzd19:
   5.652 +	pop	CX
   5.653 +	jz	lzdone
   5.654 +
   5.655 +//       }
   5.656 +//       len += kMatchMinLen;/*2*/
   5.657 +
   5.658 +	inc	CX
   5.659 +
   5.660 +//     string: // if (rep0 > nowPos) return LZMA_RESULT_DATA_ERROR;
   5.661 +//       do {
   5.662 +lzd13z:
   5.663 +//         previousByte = outStream[nowPos - rep0];
   5.664 +//         outStream[nowPos++] = previousByte;
   5.665 +
   5.666 +	call	outcharDico 	// %bl = outStream[nowPos++] = outStream[nowPos - rep0]
   5.667 +
   5.668 +//       } while(--len != 0);
   5.669 +
   5.670 +	loop	lzd13z
   5.671 +
   5.672 +//     } /* char/string */
   5.673 +//   }
   5.674 +
   5.675 +	jmp	lzdmainlp
   5.676 +
   5.677 +lzdone:
   5.678 +//   //RC_NORMALIZE;
   5.679 +//   //*inSizeProcessed = (SizeT)(Buffer - inStream); *outSizeProcessed = nowPos;
   5.680 +//   return LZMA_RESULT_OK;
   5.681 +	call	Dico2ESDI	// set es & di (rep0 = 0)
   5.682 +	lea	ws2(BP), SP	// dealloc
   5.683 +	ret	
   5.684 +// }
   5.685 +
   5.686 +// al = outStream[nowPos - rep0];
   5.687 +
   5.688 +/*
   5.689 + * output  es:di, al
   5.690 + * scratch bh, cl, flags
   5.691 + */
   5.692 +
   5.693 +DicoRep02ESDI:
   5.694 +	stc
   5.695 +
   5.696 +// bl = outStream[nowPos];
   5.697 +
   5.698 +/*
   5.699 + * output  es:di, bl
   5.700 + * scratch bh, cl, flags
   5.701 + */
   5.702 + 
   5.703 +Dico2ESDI:
   5.704 +#if !defined(FLAT32) && !defined(FLAT16OUT)
   5.705 +# ifdef ONLY8086
   5.706 +	pushw	%ax
   5.707 +	movw	nowPos(%bp), %bx
   5.708 +	movw	nowPos+2(%bp), %ax
   5.709 +	jnc	Dico2ESDIz
   5.710 +	subw	rep0(%bp), %bx
   5.711 +	sbbw	rep0+2(%bp), %ax
   5.712 +Dico2ESDIz:
   5.713 +	movw	$0xF, %di
   5.714 +	andw	%bx, %di
   5.715 +	pushw	%cx
   5.716 +	movb	$4, %cl
   5.717 +	shrw	%cl, %bx
   5.718 +	shlw	%cl, %ax
   5.719 +	popw	%cx
   5.720 +	addb	%al, %bh
   5.721 +	popw	%ax
   5.722 +# else
   5.723 +	movl	nowPos(%bp), %ebx
   5.724 +	jnc	Dico2ESDIz
   5.725 +	subl	rep0(%bp), %ebx
   5.726 +Dico2ESDIz:
   5.727 +	movw	%bx, %di
   5.728 +	xorw	%bx, %bx
   5.729 +	shrl	$4, %ebx
   5.730 +# endif
   5.731 +	movw	%bx, %es
   5.732 +#else
   5.733 +	mov	nowPos(BP), DI
   5.734 +	jnc	Dico2ESDIz
   5.735 +	sub	rep0(BP), DI
   5.736 +Dico2ESDIz:
   5.737 +#endif
   5.738 +#ifdef FLAT32
   5.739 +	movb	(DI), %bl
   5.740 +#else
   5.741 +	movb	%es:(%di), %bl
   5.742 +#endif
   5.743 +	ret
   5.744 +
   5.745 +outcharDico:
   5.746 +
   5.747 +// bl = outStream[nowPos++] = outStream[nowPos - rep0]
   5.748 +
   5.749 +/*
   5.750 + * output  es:di, bl
   5.751 + * update  nowPos
   5.752 + * scratch ax, dx, bh, cl, flags
   5.753 + */
   5.754 +
   5.755 +	call	DicoRep02ESDI	// %bl = outStream[nowPos - rep0]
   5.756 +	xchg	AX, BX
   5.757 +outchar:
   5.758 +
   5.759 +// bl = outStream[nowPos++] = previousByte = al;
   5.760 +
   5.761 +/*
   5.762 + * output  bl
   5.763 + * update  nowPos
   5.764 + * scratch ax, dx, bh, di, cl, flags
   5.765 + */
   5.766 +
   5.767 +	clc
   5.768 +	call	Dico2ESDI
   5.769 +	stosb
   5.770 +	xchg	AX, BX		// previous byte
   5.771 +
   5.772 +//	int posState = (int)((nowPos) & posStateMask);
   5.773 +
   5.774 +#if PROP_PB != 0 && PROP_LP != 0
   5.775 +	addw	$0x0101, posState2(BP)
   5.776 +	andb	$(((1 << PROP_PB) -1)<<8)+((1 << PROP_LP) -1), posState2(BP)
   5.777 +#else
   5.778 +# if PROP_PB != 0
   5.779 +	incb	posState(BP)
   5.780 +	andb	$((1 << PROP_PB) -1), posState(BP)
   5.781 +# endif
   5.782 +# if PROP_LP != 0
   5.783 +	incb	posState2(BP)
   5.784 +	andb	$((1 << PROP_LP) -1), posState2(BP)
   5.785 +# endif
   5.786 +#endif
   5.787 +#ifdef ONLY8086
   5.788 +	incw	nowPos(BP)
   5.789 +	jnz	incnowPosDone
   5.790 +	incw	nowPos+2(BP)
   5.791 +incnowPosDone:
   5.792 +#else
   5.793 +	incl	nowPos(BP)
   5.794 +#endif
   5.795 +	ret
   5.796 +
   5.797 +//  
   5.798 +// #define RC_NORMALIZE if (Range < kTopValue) 
   5.799 +//    { Range <<= 8; Code = (Code << 8) | RC_READ_BYTE; }
   5.800 +
   5.801 +/*
   5.802 + * update  Range, Code, ds:si
   5.803 + * scratch flags
   5.804 + */
   5.805 +
   5.806 +RC_NORMALIZE:
   5.807 +	cmpb	$0, Range+3(BP)
   5.808 +	jne	RC_NORMALIZE_1
   5.809 +RC_LOAD_BYTE:
   5.810 +	push	AX
   5.811 +#ifdef ONLY8086
   5.812 +	movw	Range+1(BP), %ax
   5.813 +	movw	%ax, Range+2(BP)
   5.814 +	movw	Code+1(BP), %ax
   5.815 +	movw	%ax, Code+2(BP)
   5.816 +	xorw	%ax, %ax
   5.817 +	movb	Range(BP), %ah
   5.818 +	movw	%ax, Range(BP)
   5.819 +	movb	Code(BP), %ah
   5.820 +	movw	%ax, Code(BP)
   5.821 +#else
   5.822 +	shll	$8, Range(BP)
   5.823 +	shll	$8, Code(BP)
   5.824 +#endif
   5.825 +#if !defined(FLAT16) && !defined(FLAT32)
   5.826 +	testw	%si, %si
   5.827 +	jns	RC_READ_BYTE
   5.828 +	movw	%ds, %ax
   5.829 +	incw	%ax
   5.830 +	movw	%ax, %ds
   5.831 +	addw	$-16, %si
   5.832 +RC_READ_BYTE:
   5.833 +#endif
   5.834 +	lodsb
   5.835 +	movb	%al, Code(BP)
   5.836 +	pop	AX
   5.837 +RC_NORMALIZE_1:
   5.838 +	ret
   5.839 +
   5.840 +// Bit1(dx + (state << kNumPosBitsMax /*4*/) + posState)
   5.841 +
   5.842 +Bit1state:
   5.843 +	movb	$16, %al
   5.844 +	mulb	state(BP)
   5.845 +# if PROP_PB != 0
   5.846 +	addb	posState(BP), %al
   5.847 +# endif
   5.848 +Bit1axdx:
   5.849 +	add	DX, AX
   5.850 +	jmp	Bit1
   5.851 +
   5.852 +// prob += 12; Bit1(prob)
   5.853 +
   5.854 +Bit1dx12:
   5.855 +	add	$12, DX
   5.856 +Bit1dx:
   5.857 +	mov	DX, AX
   5.858 +
   5.859 +// static int Bit1(CProb *p)
   5.860 +
   5.861 +Bit1:
   5.862 +/*
   5.863 + * input   ax=p
   5.864 + * output  C, ax
   5.865 + * update  bound, Range, Code, ds:si
   5.866 + * scratch flags
   5.867 + */
   5.868 + 
   5.869 +// {
   5.870 +// 	RC_NORMALIZE;
   5.871 +
   5.872 +	call  RC_NORMALIZE		// kill %ax, update %si
   5.873 +
   5.874 +#ifdef ONLY8086
   5.875 +	pushw	%ax
   5.876 +	pushw	%cx
   5.877 +	pushw	%dx
   5.878 +	pushw	%di
   5.879 +#else
   5.880 +	pushal
   5.881 +#endif
   5.882 +
   5.883 +	xchg	AX, DI
   5.884 +	add	DI, DI			// short *
   5.885 +	
   5.886 +
   5.887 +// 	bound = (Range>>kNumBitModelTotalBits /*11*/) * *(p);
   5.888 +
   5.889 +#ifdef ONLY8086
   5.890 +	movw	Range(BP), %dx
   5.891 +	movw	Range+2(BP), %ax
   5.892 +	movw	$11, %cx
   5.893 +shr11lp:
   5.894 +	shrw	$1, %ax
   5.895 +	rcrw	$1, %dx
   5.896 +	loop	shr11lp
   5.897 +	movw	%dx, %cx
   5.898 +	mulw	(BP, DI)
   5.899 +	xchgw	%ax, %cx
   5.900 +	mulw	(BP, DI)
   5.901 +	addw	%cx, %dx
   5.902 +#else
   5.903 +	movl	Range(BP), %eax
   5.904 +	shrl	$11, %eax
   5.905 +	movzwl	(BP, DI), %edx
   5.906 +	mull	%edx
   5.907 +#endif
   5.908 +
   5.909 +// 	if (Code < bound) {
   5.910 +
   5.911 +#ifdef ONLY8086
   5.912 +	cmpw	Code+2(BP), %dx
   5.913 +	jb	Bit1_1
   5.914 +	ja	Bit1_1x
   5.915 +	cmpw	Code(BP), %ax
   5.916 +	jbe	Bit1_1
   5.917 +Bit1_1x:
   5.918 +
   5.919 +//    		Range = bound;
   5.920 +
   5.921 +	movw	%ax, Range(BP)
   5.922 +	movw	%dx, Range+2(BP)
   5.923 +#else
   5.924 +	cmpl	Code(BP), %eax
   5.925 +	jbe	Bit1_1
   5.926 +
   5.927 +//    		Range = bound;
   5.928 +
   5.929 +	movl	%eax, Range(BP)
   5.930 +#endif
   5.931 +
   5.932 +// 		*(p) += (kBitModelTotal /*2048*/ - *(p)) >> kNumMoveBits /*5*/;
   5.933 +
   5.934 +	movw	$2048, %ax
   5.935 +
   5.936 +// 		return 0;
   5.937 +
   5.938 +	jmp	Bit1_2
   5.939 +
   5.940 +//	}
   5.941 +// 	else {
   5.942 +
   5.943 +Bit1_1:
   5.944 +
   5.945 +//    		Range -= bound; Code -= bound;
   5.946 +
   5.947 +#ifdef ONLY8086
   5.948 +	subw	%ax, Range(BP)
   5.949 +	sbbw	%dx, Range+2(BP)
   5.950 +	subw	%ax, Code(BP)
   5.951 +	sbbw	%dx, Code+2(BP)
   5.952 +#else
   5.953 +	subl	%eax, Range(BP)
   5.954 +	subl	%eax, Code(BP)
   5.955 +#endif
   5.956 +
   5.957 +// 		*(p) -= (*(p)) >> kNumMoveBits /*5*/;
   5.958 +
   5.959 +	movw	$31, %ax
   5.960 +
   5.961 +// 		return 1;
   5.962 +
   5.963 +	stc
   5.964 +Bit1_2:
   5.965 +	pushf
   5.966 +	subw	(BP, DI), %ax
   5.967 +#ifdef ONLY8086
   5.968 +	movb	$5, %cl
   5.969 +	sarw	%cl, %ax
   5.970 +#else
   5.971 +	sarw	$5, %ax
   5.972 +#endif
   5.973 +	addw	%ax, (BP, DI)
   5.974 +	popf
   5.975 +#ifdef ONLY8086
   5.976 +	popw	%di
   5.977 +	popw	%dx
   5.978 +	popw	%cx
   5.979 +	popw	%ax
   5.980 +#else
   5.981 +	popal
   5.982 +#endif
   5.983 +	sbb	AX, AX
   5.984 +
   5.985 +// 	}
   5.986 +// }
   5.987 +
   5.988 +	ret
   5.989 +
   5.990 +RangeDecoder:
   5.991 +
   5.992 +/*
   5.993 + * input   ax=probs cx=numLevels (< 8) bx=1
   5.994 + * output  ax=res (backward), dh (forward)
   5.995 + * update  bound, Range, Code, ds:si
   5.996 + * scratch flags, cx=0, dl
   5.997 + */
   5.998 + 
   5.999 +	push	BX
  5.1000 +	
  5.1001 +//   { int i = numLevels; res = 1; 
  5.1002 +	mov	BX, DX		// res = 1
  5.1003 +	
  5.1004 +//   do { CProb *p = probs + res; RC_GET_BIT(p, res) } while(--i != 0); 
  5.1005 +
  5.1006 +RangeDecoder_1:
  5.1007 +	push	AX
  5.1008 +	call	Bit1axdx		// C,%ax = Bit1(prob+%ax)
  5.1009 +	rclb	$1, %dl			// res <<= 1; res |= C
  5.1010 +	andb	%bl, %al		// current bit
  5.1011 +	orb	%al, %bh		// store in bh
  5.1012 +	shlb	$1, %bl			// update max
  5.1013 +	pop	AX
  5.1014 +	loop	RangeDecoder_1
  5.1015 +
  5.1016 +//   res -= (1 << numLevels); }
  5.1017 +
  5.1018 +	xchg	AX, BX			// move bh to dh
  5.1019 +	xchg	AX, DX			// and dl to al
  5.1020 +	sub	%dl, %al		// sub max
  5.1021 +	pop	BX
  5.1022 +	ret
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/plop/stuff/unpack.S	Thu Sep 19 20:20:35 2019 +0200
     6.3 @@ -0,0 +1,364 @@
     6.4 +#define SYSTEM		0x10000
     6.5 +#define SETUP		14
     6.6 +#define SYSSIZE		0x1F4
     6.7 +#define SETUPSIZE	0x1F1
     6.8 +
     6.9 +#define KEEP_BOOTSECTOR		1
    6.10 +#define LINUX_HEADER		0
    6.11 +#define UPDATE_SYSSIZE		1
    6.12 +#define HARDCODED_SYSSIZE	1
    6.13 +#define HARDCODED_SETUPSIZE	1
    6.14 +#define FLAT16			1
    6.15 +#define FLAT16OUT		1
    6.16 +#define SAVEREGS		0 
    6.17 +#define ELKS			0
    6.18 +
    6.19 +	.text
    6.20 +	.code16
    6.21 +	.org	0
    6.22 +
    6.23 +#if KEEP_BOOTSECTOR
    6.24 +#undef UPDATE_SYSSIZE
    6.25 +#define UPDATE_SYSSIZE		0
    6.26 +#endif
    6.27 +#if defined(FLAT16OUT) && FLAT16OUT == 0
    6.28 +#undef FLAT16OUT
    6.29 +#endif
    6.30 +#if ELKS
    6.31 +	.arch	i8086
    6.32 +#define FLAT16			1
    6.33 +#define LINUX_HEADER		0
    6.34 +#define SAVEREGS		0 
    6.35 +#define TOPSEG		0x2000
    6.36 +#else
    6.37 +#define TOPSEG		0x8000
    6.38 +#endif
    6.39 +
    6.40 +	.globl	_start
    6.41 +_start:
    6.42 +#if LINUX_HEADER
    6.43 +	jmp	start2
    6.44 +	.ascii	"HdrS"		// 202 magic
    6.45 +	.word	0x200		// 206 version 2.00
    6.46 +	.long	0		// 208 realmode_swtch
    6.47 +	.word	SYSTEM/16	// 20C start_sys_seg
    6.48 +	.word	0		// 20E kernel_version
    6.49 +	.byte	0		// 210 type_of_loader
    6.50 +	.byte	0		// 211 loadflags
    6.51 +	.word	0		// 212 setup_move_size
    6.52 +	.long	SYSTEM		// 214 code32_start
    6.53 +	.long	0		// 218 ramdisk_image
    6.54 +	.long	0		// 21C ramdisk_size
    6.55 +	.long	0		// 220 bootsect_kludge
    6.56 +start2:
    6.57 +#endif
    6.58 +#if SAVEREGS
    6.59 +	pushfw
    6.60 +	pushw	%cs
    6.61 +	pushw	$0
    6.62 +	pushal
    6.63 +	pushw	%ds
    6.64 +	cld
    6.65 +#else
    6.66 +	pushw	%cs
    6.67 +# if FLAT16
    6.68 +	xorw	%di, %di
    6.69 +	pushw	%di
    6.70 +# else
    6.71 +	pushw	$0
    6.72 +# endif
    6.73 +#endif
    6.74 +#if ELKS
    6.75 +	movw	$0x100, %ax
    6.76 +	pushw	%ax
    6.77 +#else
    6.78 +# if UPDATE_SYSSIZE || KEEP_BOOTSECTOR
    6.79 +	pushw	%ds			// <A>
    6.80 +# else
    6.81 +	pushw	%cs			// <A>
    6.82 +# endif
    6.83 +#endif
    6.84 +#if FLAT16
    6.85 +# 1- move 9000/9020..9020+stp -> TOPSEG
    6.86 +#if ELKS
    6.87 +	movb	$TOPSEG/256, %ah
    6.88 +	movw	%ax, %es
    6.89 +#else
    6.90 +	pushw	$TOPSEG
    6.91 +	popw	%es
    6.92 +#endif
    6.93 +#if KEEP_BOOTSECTOR
    6.94 +	xorw	%si, %si
    6.95 +#else
    6.96 +	movw	$0x200, %si
    6.97 +#endif
    6.98 +#if SAVEREGS == 0
    6.99 +	pushw	%es
   6.100 +#if ELKS
   6.101 +# if KEEP_BOOTSECTOR
   6.102 +	movw	$cont+0x200, %ax
   6.103 +# else
   6.104 +	movw	$cont, %ax
   6.105 +# endif
   6.106 +	pushw	%ax
   6.107 +#else
   6.108 +# if KEEP_BOOTSECTOR
   6.109 +	pushw	$cont+0x200
   6.110 +# else
   6.111 +	// pushw	$cont
   6.112 +	.byte	0x6A, cont
   6.113 +# endif
   6.114 +#endif
   6.115 +	jmp	jumpinto
   6.116 +#else
   6.117 +# if HARDCODED_SETUPSIZE
   6.118 +setup_word:
   6.119 +#  if KEEP_BOOTSECTOR
   6.120 +#  else
   6.121 +	movw	$SETUP*256, %cx
   6.122 +#  endif
   6.123 +# else
   6.124 +#  if KEEP_BOOTSECTOR
   6.125 +	movw	$0x100, %cx
   6.126 +	addb	SETUPSIZE, %ch
   6.127 +#  else
   6.128 +	xorw	%cx, %cx
   6.129 +	movb	SETUPSIZE, %ch
   6.130 +#  endif
   6.131 +	movw	%cx, %dx
   6.132 +# endif
   6.133 +	pushw	%cx			// <B>
   6.134 +	pushw	%es
   6.135 +	pushw	%di
   6.136 +	rep
   6.137 +	  movsw
   6.138 +	lret
   6.139 +#endif
   6.140 +cont:
   6.141 +# 2- move 1000..1000+sys -> TOPSEG+stp
   6.142 +#if ELKS
   6.143 +	movw	$0x1000, %ax
   6.144 +	movw	%ax, %ds
   6.145 +#else
   6.146 +	pushw	$0x1000
   6.147 +	popw	%ds
   6.148 +#endif
   6.149 +	xorw	%si, %si
   6.150 +	subw	%di, %cx
   6.151 +	rep
   6.152 +	  movsb
   6.153 +# 3- unlz(TOPSEG+stp:end, 1000-stp:0)
   6.154 +	pushw	%es		// <C>
   6.155 +# if HARDCODED_SETUPSIZE
   6.156 +setup_seg:
   6.157 +#if ELKS
   6.158 +#  if KEEP_BOOTSECTOR
   6.159 +#   if UPDATE_SYSSIZE
   6.160 +	movw	$0x1000-(SETUP*32)-32-32, %ax
   6.161 +#   else
   6.162 +	movw	$0x1000-(SETUP*32)-32, %ax
   6.163 +#   endif
   6.164 +#  else
   6.165 +#   if UPDATE_SYSSIZE
   6.166 +	movw	$0x1000-(SETUP*32)-32, %ax
   6.167 +#   else
   6.168 +	movw	$0x1000-(SETUP*32), %ax
   6.169 +#   endif
   6.170 +#  endif
   6.171 +	movw	%ax, %es
   6.172 +#else
   6.173 +#  if KEEP_BOOTSECTOR
   6.174 +#   if UPDATE_SYSSIZE
   6.175 +	pushw	$0x1000-(SETUP*32)-32-32
   6.176 +#   else
   6.177 +	pushw	$0x1000-(SETUP*32)-32
   6.178 +#   endif
   6.179 +#  else
   6.180 +#   if UPDATE_SYSSIZE
   6.181 +	pushw	$0x1000-(SETUP*32)-32
   6.182 +#   else
   6.183 +	pushw	$0x1000-(SETUP*32)
   6.184 +#   endif
   6.185 +#  endif
   6.186 +	popw	%es
   6.187 +#endif
   6.188 +# else
   6.189 +#  if UPDATE_SYSSIZE
   6.190 +	incb	%dh
   6.191 +#  endif
   6.192 +#if ELKS
   6.193 +	shrw	$1, %dx
   6.194 +	shrw	$1, %dx
   6.195 +	shrw	$1, %dx
   6.196 +#else
   6.197 +	shrw	$3, %dx
   6.198 +#endif
   6.199 +	movw	%ds, %ax
   6.200 +	subw	%dx, %ax
   6.201 +	movw	%ax, %es
   6.202 +# endif
   6.203 +#else		// FLAT16
   6.204 +# 1- move 1000..1000+sys -> 9000-sys
   6.205 +# if HARDCODED_SYSSIZE
   6.206 +packed_syssize:
   6.207 +	movw	$0x1000+0, %ax
   6.208 +# else
   6.209 +	movw	SYSSIZE, %ax
   6.210 +	addw	$0x1000, %ax
   6.211 +# endif
   6.212 +	movw	%ds, %dx
   6.213 +mvsys:
   6.214 +	decw	%ax
   6.215 +	decw	%dx
   6.216 +	movw	%ax, %ds
   6.217 +	movw	%dx, %es
   6.218 +	xorw	%si, %si
   6.219 +	xorw	%di, %di
   6.220 +	movw	$8, %cx
   6.221 +	rep
   6.222 +	  movsw
   6.223 +	cmpw	$0x1000, %ax
   6.224 +	jne	mvsys
   6.225 +# 2- move 9000/9020..9020+stp -> 9000-sys-stp
   6.226 +	
   6.227 +# if HARDCODED_SETUPSIZE
   6.228 +#  if KEEP_BOOTSECTOR
   6.229 +setup_byte:
   6.230 +	movb	$SETUP+1, %ch
   6.231 +setup_para:
   6.232 +	subw	$SETUP*32+32, %dx
   6.233 +#  else
   6.234 +setup_byte:
   6.235 +	movb	$SETUP, %ch
   6.236 +setup_para:
   6.237 +	subw	$SETUP*32, %dx
   6.238 +#  endif
   6.239 +# else
   6.240 +	movb	%ss:SETUPSIZE, %ch
   6.241 +#  if KEEP_BOOTSECTOR
   6.242 +	incb	$ch
   6.243 +#  endif
   6.244 +	movw	%cx, %ax
   6.245 +#if ELKS
   6.246 +	shrw	$1, %ax
   6.247 +	shrw	$1, %ax
   6.248 +	shrw	$1, %ax
   6.249 +#else
   6.250 +	shrw	$3, %ax
   6.251 +#endif
   6.252 +	subw	%ax, %dx
   6.253 +# endif
   6.254 +	pushw	%cx			// <B>
   6.255 +	movw	%dx, %es
   6.256 +	xorw	%si, %si
   6.257 +	xorw	%di, %di
   6.258 +	rep
   6.259 +	  movsw	%cs:(%si),%es:(%di)
   6.260 +	pushw	%es
   6.261 +# 3- reloc itself in 0x7C00
   6.262 +	pushw	$0x07C0
   6.263 +	popw	%es
   6.264 +	movw	$cont, %si
   6.265 +	movw	%si, %di
   6.266 +	movw	$end-_start, %cx
   6.267 +	pushw	%es
   6.268 +	pushw	%di
   6.269 +	rep
   6.270 +	  movsb	%cs:(%si),%es:(%di)
   6.271 +	lret
   6.272 +cont:
   6.273 +# 4- unlz(9000-sys-stp:end, 1000-stp:0)
   6.274 +# if HARDCODED_SETUPSIZE
   6.275 +setup_seg:
   6.276 +#  if KEEP_BOOTSECTOR
   6.277 +#   if UPDATE_SYSSIZE
   6.278 +	pushw	$0x1000-(SETUP*32)-32-32
   6.279 +#   else
   6.280 +	pushw	$0x1000-(SETUP*32)-32
   6.281 +#   endif
   6.282 +#  else
   6.283 +#   if UPDATE_SYSSIZE
   6.284 +	pushw	$0x1000-(SETUP*32)-32
   6.285 +#   else
   6.286 +	pushw	$0x1000-(SETUP*32)
   6.287 +#   endif
   6.288 +#  endif
   6.289 +	popw	%es
   6.290 +# else
   6.291 +	negw	%ax
   6.292 +#  if UPDATE_SYSSIZE
   6.293 +	addw	$0x1000-32, %ax
   6.294 +#  else
   6.295 +	addw	$0x1000, %ax
   6.296 +#  endif
   6.297 +	movw	%ax, %es
   6.298 +# endif
   6.299 +#endif		// FLAT16
   6.300 +	popw	%ds			// <C>
   6.301 +#if KEEP_BOOTSECTOR
   6.302 +	movw	$0x200+end-_start, %si
   6.303 +#else
   6.304 +	movw	$end-_start, %si
   6.305 +#endif
   6.306 +#if UPDATE_SYSSIZE
   6.307 +	movw	$SYSSIZE, %di
   6.308 +#else
   6.309 +	xorw	%di, %di
   6.310 +#endif
   6.311 +	pushw	%di
   6.312 +	pushw	%es
   6.313 +	call	unpack
   6.314 +	popw	%ds
   6.315 +	popw	%si
   6.316 +#   if KEEP_BOOTSECTOR
   6.317 +setup_keep:
   6.318 +	xorw	%di, %di
   6.319 +#   else
   6.320 +	movw	%si, %di
   6.321 +#   endif
   6.322 +#   if SAVEREGS != 0
   6.323 +	popw	%cx			// <B>
   6.324 +#if UPDATE_SYSSIZE
   6.325 +// not need with memtest	movb	$(512-SYSSIZE)/2, %cl
   6.326 +#endif
   6.327 +	popw	%es			// <A>
   6.328 +# 5- move 1000-stp..1000 -> 9020
   6.329 +#   else
   6.330 +	popw	%es			// <A>
   6.331 +jumpinto:
   6.332 +# if HARDCODED_SETUPSIZE
   6.333 +setup_word:
   6.334 +#  if KEEP_BOOTSECTOR
   6.335 +	movw	$SETUP*256+256, %cx
   6.336 +#  else
   6.337 +	movw	$SETUP*256, %cx
   6.338 +#  endif
   6.339 +# else
   6.340 +#  if KEEP_BOOTSECTOR
   6.341 +	movw	$0x100, %cx
   6.342 +	addb	%ss:SETUPSIZE, %ch
   6.343 +#  else
   6.344 +	xorw	%cx, %cx
   6.345 +	movb	%ss:SETUPSIZE, %ch
   6.346 +#  endif
   6.347 +	movw	%cx, %dx
   6.348 +# endif
   6.349 +#  endif
   6.350 +	rep
   6.351 +	  movsw
   6.352 +#if SAVEREGS
   6.353 +	popw	%ds
   6.354 +	popal
   6.355 +	iret
   6.356 +#else
   6.357 +	lret
   6.358 +#endif
   6.359 +
   6.360 +unpack:
   6.361 +#define NO_LZMA_HEADER
   6.362 +#if ELKS
   6.363 +#define ONLY8086	1
   6.364 +#endif
   6.365 +#include "unlzma.S"
   6.366 +
   6.367 +end: