wok rev 23919

Add plop
author Pascal Bellard <pascal.bellard@slitaz.org>
date Wed Aug 19 13:03:10 2020 +0000 (2020-08-19)
parents 17bb3a5bf500
children 6f2ca254dc9c
files plop/receipt plop/stuff/bootloader.S plop/stuff/pack plop/stuff/unlzma.S plop/stuff/unpack.S
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/plop/receipt	Wed Aug 19 13:03:10 2020 +0000
     1.3 @@ -0,0 +1,56 @@
     1.4 +# SliTaz package receipt.
     1.5 +
     1.6 +PACKAGE="plop"
     1.7 +VERSION="5.0.15"
     1.8 +CATEGORY="base-system"
     1.9 +SHORT_DESC="USB / IDE-CD / PCMCIA CardBus and others boot utility."
    1.10 +MAINTAINER="pascal.bellard@slitaz.org"
    1.11 +LICENSE="other"
    1.12 +WEB_SITE="https://www.plop.at/en/bootmanagers.html"
    1.13 +TARBALL="plpbt-$VERSION.zip"
    1.14 +WGET_URL="https://download.plop.at/files/bootmngr/$TARBALL"
    1.15 +
    1.16 +BUILD_DEPENDS="xz"
    1.17 +
    1.18 +# Rules to configure and make the package.
    1.19 +compile_rules()
    1.20 +{
    1.21 +	while read ofs data name; do
    1.22 +		echo -en "\x$data" | \
    1.23 +		dd conv=notrunc of=plpbt.bin bs=1 seek=$((0x$ofs))
    1.24 +	done <<EOT
    1.25 +0288	00	videoMode=text
    1.26 +0295	00	stars=off
    1.27 +0299	01	font=bios
    1.28 +029A	00	zoomanimation=off
    1.29 +EOT
    1.30 +	cp $stuff/*.S $stuff/pack .
    1.31 +	for i in bootloader unpack ; do
    1.32 +		cc -o $i.o -Wa,-a=$i.lst -c $i.S
    1.33 +		objcopy -O binary $i.o $i.bin
    1.34 +	done
    1.35 +	sed -i "s/VERSION/$VERSION/" pack
    1.36 +	./pack --build bootloader.bin unpack.bin
    1.37 +	./pack plpbt.bin plop
    1.38 +}
    1.39 +
    1.40 +# Rules to gen a SliTaz package suitable for Tazpkg.
    1.41 +genpkg_rules()
    1.42 +{
    1.43 +	mkdir -p $fs/boot $install/usr/share
    1.44 +	cp $src/plop $fs/boot/plop.exe
    1.45 +	cp -a $src/doc $install/usr/share
    1.46 +	cp -a $src/*.txt $src/*.TXT $install/usr/share/doc
    1.47 +}
    1.48 +
    1.49 +# Pre and post install commands for Tazpkg.
    1.50 +post_install()
    1.51 +{
    1.52 +	[ -s $1/boot/isolinux/isolinux.cfg ] &&
    1.53 +	! grep -qs 'USB/PCMCIA boot' $1/boot/isolinux/isolinux.cfg && sed -i \
    1.54 +'s|LABEL md5|LABEL plop usb pcmcia\
    1.55 +	MENU LABEL USB/PCMCIA boot\
    1.56 +	KERNEL /boot/plop.exe\
    1.57 +\n&|' $1/boot/isolinux/isolinux.cfg
    1.58 +	true
    1.59 +}
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/plop/stuff/bootloader.S	Wed Aug 19 13:03:10 2020 +0000
     2.3 @@ -0,0 +1,655 @@
     2.4 +// Image/zImage & tiny bzImage linux kernel boot sector, (C) SliTaz, GPL2.
     2.5 +
     2.6 +SYSSEG		= 0x1000
     2.7 +setup_sects	= 497
     2.8 +syssize		= 500
     2.9 +cmd_line_ptr	= 0x228
    2.10 +
    2.11 +	.text
    2.12 +	.code16
    2.13 +	.org	0
    2.14 +	.globl	_start
    2.15 +_start:
    2.16 +
    2.17 +#define CODESZ 512		/* patched by installer */
    2.18 +
    2.19 +// Default kernel format is 386 Image/zImage
    2.20 +//#define BZIMAGE 0x207		/* setup version ; for bzImage < 512 Kb only */
    2.21 +//#define ELKS			/* 8086/286 linux port */
    2.22 +
    2.23 +/* some extra features */
    2.24 +#define EXE_SUPPORT		real mode dos .exe file support	+208/264
    2.25 +#define  CMDLINE		dos kernel cmdline support	+45
    2.26 +#define  VCPI			VCPI 4.0 support (386+)		+109
    2.27 +#define  SHUTDOWNDOS		shutdown DOS services		+29
    2.28 +
    2.29 +/* some contraints to reduce the code size */
    2.30 +//#define FLOPPY_1440K_ONLY	1.44M floppies support only	-26
    2.31 +#define FLOPPY_HAS_2_SIDES	hardcoded heads count to 2	-15
    2.32 +//#define NO_CMDLINE_SHRINK	remove heading spaces ?		-6-21
    2.33 +//#define  NO_CMDLINE_FILE	remove @cmdline file support ?	-21
    2.34 +//#define NO_DOTS		show progression dots ?		-5
    2.35 +#ifndef BZIMAGE
    2.36 +#define TINY_ZIMAGE		system < 64Kb ?			-11
    2.37 +#define NO_MINSETUP		default setup (dos only) ?	-4
    2.38 +//#define NO_CURSOR_DEFINITION					-8
    2.39 +#endif
    2.40 +
    2.41 +#ifdef ELKS
    2.42 +	.arch	i8086
    2.43 +INITSEG		= 0x0100
    2.44 +SETUPSEG	= 0x0120
    2.45 +#define ONLY8086
    2.46 +#undef BZIMAGE
    2.47 +#undef VCPI
    2.48 +#else
    2.49 +INITSEG		= 0x9000
    2.50 +SETUPSEG	= 0x9020
    2.51 +#endif
    2.52 +
    2.53 +.macro	shlclw cnt,obj
    2.54 +#ifdef ONLY8086
    2.55 +	movb	\cnt,%cl
    2.56 +	shlw	%cl,\obj
    2.57 +#else
    2.58 +	shlw	\cnt,\obj
    2.59 +#endif
    2.60 +.endm
    2.61 +
    2.62 +.macro	shrclw cnt,obj
    2.63 +#ifdef ONLY8086
    2.64 +	movb	\cnt,%cl
    2.65 +	shrw	%cl,\obj
    2.66 +#else
    2.67 +	shrw	\cnt,\obj
    2.68 +#endif
    2.69 +.endm
    2.70 +
    2.71 +#ifdef EXE_SUPPORT
    2.72 +#define EXEADRS(x)	(x+0xE0)
    2.73 +#define FLAT20(x)	(x+16*INITSEG)
    2.74 +
    2.75 +.macro	trace_int19
    2.76 +#ifdef ONLY8086
    2.77 +	xorw	%si, %si
    2.78 +	movw	%si, %ds
    2.79 +	pushw	4+2(%si)
    2.80 +	pushw	4(%si)
    2.81 +	movw	$step19, 4(%si)
    2.82 +	movw	$INITSEG, 4+2(%si)
    2.83 +#else
    2.84 +	pushl	$4
    2.85 +	popw	%si
    2.86 +	popw	%ds
    2.87 +	pushl	(%si)
    2.88 +	movl	$step19+(INITSEG<<16), (%si)
    2.89 +#endif
    2.90 +	pushfw
    2.91 +	popw	%ax
    2.92 +	incb	%ah			# set TF
    2.93 +	pushw	%ax
    2.94 +	popfw
    2.95 +	ljmp	*4*0x19-4(%si)
    2.96 +.endm
    2.97 +
    2.98 +stacktop	= 0x9E00		# in 0x8000 .. 0xA000
    2.99 +	decw	%bp			// Magic number: MZ
   2.100 +	popw	%dx
   2.101 +	jmp	start			// Bytes on last page of file
   2.102 +	.word	(CODESZ+511)/512	// Pages in file			INSTALLER
   2.103 +	.word	0			// Relocations
   2.104 +	.word	(end_header-_start)/16	// Size of header in paragraphs
   2.105 +	.word	4096			// Minimum extra paragraphs needed
   2.106 +	.word	-1			// Maximum extra paragraphs needed
   2.107 +	.word	(CODESZ+15)/16		// Initial (relative) SS value		INSTALLER
   2.108 +	.word	stacktop		// Initial SP value
   2.109 +	.word	0			// Checksum				INSTALLER?
   2.110 +	.word	EXEADRS(comstart)	// Initial IP value
   2.111 +	.word	0xFFF0			// Initial (relative) CS value
   2.112 +//	.word	0x001C			// File address of relocation table
   2.113 +//	.word	0,0,0			// Overlay number
   2.114 +#else
   2.115 +#undef VCPI
   2.116 +#endif
   2.117 +start:
   2.118 +	xorw	%ax, %ax		# %ax = 0
   2.119 +zeroed		= 12			# zeroed registers
   2.120 +	movw	$zeroed/2, %cx		# clear gdt + offset, %ds, limits
   2.121 +stacktop	= 0x9E00		# in 0x8000 .. 0xA000 (+zeroed+12)
   2.122 +#ifdef ONLY8086
   2.123 +	movw	$INITSEG, %bx
   2.124 +#else
   2.125 +	pushw	$INITSEG
   2.126 +#endif
   2.127 +end_header:
   2.128 +	cld				# assume nothing
   2.129 +#if defined(BZIMAGE) && BZIMAGE >= 0x202
   2.130 +	popw	%es			# %es contain INITSEG
   2.131 +	movw	%es, %di
   2.132 +#else
   2.133 +					# cmdline offset at 0x22
   2.134 +	movw	$stacktop, %di		# stacktop is an arbitrary value >=
   2.135 +					# length of bootsect + length of
   2.136 +					# setup + room for stack;
   2.137 +					# 12 is disk parm size.
   2.138 +# ifdef ONLY8086
   2.139 +	pushw	%bx
   2.140 +# endif
   2.141 +	popw	%es			# %es contain INITSEG
   2.142 +#endif
   2.143 +	pushw	%es
   2.144 +	popw	%ss			# %es = %ss = INITSEG
   2.145 +	movw	%di, %sp		# put stack at INITSEG:stacktop-...
   2.146 +#ifdef EXE_SUPPORT
   2.147 +	cwd				# force %dx = 0 (floppy only)
   2.148 +#endif
   2.149 +
   2.150 +# Many BIOS's default disk parameter tables will not recognize
   2.151 +# multi-sector reads beyond the maximum sector number specified
   2.152 +# in the default diskette parameter tables - this may mean 7
   2.153 +# sectors in some cases.
   2.154 +#
   2.155 +# Since single sector reads are slow and out of the question,
   2.156 +# we must take care of this by creating new parameter tables
   2.157 +# (for the first disk) in RAM.  We can set the maximum sector
   2.158 +# count to 36 - the most we will encounter on an ED 2.88.  
   2.159 +#
   2.160 +# High doesn't hurt.  Low does.  Let's use the max: 63
   2.161 +
   2.162 +	rep				# don't worry about cld
   2.163 +	stosw				# already done above
   2.164 +	popw	%bx			# offset = 0
   2.165 +	popw	%ds			# %ds = 0
   2.166 +
   2.167 +	movb	setup_sects+0x7C00, %al	# read bootsector + setup (%ds = 0)
   2.168 +	incw	%ax
   2.169 +
   2.170 +	ldsw	0x78(%bx), %si		# %ds:%bx+0x78 is parameter table address
   2.171 +	pushw	%es
   2.172 +	pushw	%di
   2.173 +	movb	$6, %cl			# copy 12 bytes
   2.174 +	rep				# don't worry about cld
   2.175 +	movsw				# already done above
   2.176 +	movw	%cx, %ds		# %ds = 0
   2.177 +#ifdef ONLY8086
   2.178 +	popw	0x78(%bx)		# update parameter table address
   2.179 +	popw	0x78+2(%bx)
   2.180 +#else
   2.181 +	popl	0x78(%bx)		# update parameter table address
   2.182 +#endif
   2.183 +	pushw	%ss
   2.184 +	popw	%ds			# now %ds = %es = %ss = INITSEG
   2.185 +	movb	$63, 0x4-12(%di)	# patch sector count, %di = stacktop
   2.186 +
   2.187 +	xchg	%ax, %di		# sector count
   2.188 +	popw	%ax			# limits = 0
   2.189 +	incw	%cx			# cylinder 0, sector 1, clear Z
   2.190 +	call	read_first_sectors	# read setup
   2.191 +
   2.192 +# This routine loads the system at address LOADSEG, making sure
   2.193 +# no 64kB boundaries are crossed. We try to load it as fast as
   2.194 +# possible, loading whole tracks whenever we can.
   2.195 +
   2.196 +	movw	syssize, %di
   2.197 +	decw	%di
   2.198 +	shrclw	$9-4, %di
   2.199 +	incw	%di
   2.200 +	movw	$SYSSEG, %cx
   2.201 +#ifdef BZIMAGE
   2.202 +	push	%cx
   2.203 +#endif
   2.204 +	call	read_sectorsCX
   2.205 +
   2.206 +# This procedure turns off the floppy drive motor, so
   2.207 +# that we enter the kernel in a known state, and
   2.208 +# don't have to worry about it later.
   2.209 +
   2.210 +kill_motor:
   2.211 +	xchgw	%ax, %di		# reset FDC (%di < 128)
   2.212 +	int	$0x13
   2.213 +
   2.214 +# After that (everything loaded), we jump to the setup-routine
   2.215 +# loaded directly after the bootblock:
   2.216 +# Segments are as follows: %ds = %ss = INITSEG
   2.217 +
   2.218 +#ifdef BZIMAGE
   2.219 +	popw	%bx
   2.220 +	popw	%si			// SYSSEG:0
   2.221 +movesys:				// %bx = DS, %si
   2.222 +	movw	$16, %ax
   2.223 +	mulw	%bx
   2.224 +	addw	%si, %ax
   2.225 +	adcw	$0x9300, %dx		// %dx:%ax src flat address
   2.226 +	movw	$9, %cx
   2.227 +zero1:
   2.228 +	pushw	$0			// 2E..1E
   2.229 +	loop	zero1
   2.230 +	//pushl	$0x93100000		// 1A: dest
   2.231 +	pushw	$0x9310
   2.232 +	pushw	%cx
   2.233 +	pushw	$-1			// 18
   2.234 +	pushw	%cx			// 16
   2.235 +	pushw	%dx			// src
   2.236 +	pushw	%ax
   2.237 +	pushw	$-1			// 10
   2.238 +	movb	$8, %cl
   2.239 +	movw	%cx, %bx		// will move 8*64 = 512Kb
   2.240 +zero2:
   2.241 +	pushw	$0			// 0E..00
   2.242 +	loop	zero2
   2.243 +	movw	%sp, %si
   2.244 +	pushw	%ss
   2.245 +	popw	%es
   2.246 +	pushw	%es
   2.247 +	popw	%ds
   2.248 +syslp:
   2.249 +	movb	$0x80, %ch	
   2.250 +	movb	$0x87, %ah
   2.251 +	int	$0x15
   2.252 +	incb	0x14(%si)
   2.253 +	incb	0x1C(%si)
   2.254 +	decw	%bx
   2.255 +	jne	syslp
   2.256 +#endif
   2.257 +jmp_setup:
   2.258 +	cli
   2.259 +	ljmp	$SETUPSEG, $0
   2.260 +
   2.261 +#ifdef EXE_SUPPORT
   2.262 +#ifdef SHUTDOWNDOS
   2.263 +doiret:
   2.264 +	iret
   2.265 +step19:
   2.266 +	pushw	%si
   2.267 +	pushw	%ds
   2.268 +	movw	%sp, %si
   2.269 +	ldsw	%ss:4(%si), %si
   2.270 +	cmpw	$0x19CD, (%si)
   2.271 +	popw    %ds
   2.272 +	popw    %si
   2.273 +	jne	doiret
   2.274 +	xorw	%si, %si
   2.275 +	movw	%si, %ds
   2.276 +	pushw	%cs
   2.277 +	popw	%ss
   2.278 +	movw	$stacktop-4-16, %sp
   2.279 +#ifdef ONLY8086
   2.280 +	popw	4(%si)
   2.281 +	popw	4+2(%si)
   2.282 +	popw	%bp
   2.283 +	popw	%di
   2.284 +	popw	%si
   2.285 +	popw	%dx
   2.286 +	popw	%cx
   2.287 +	popw	%bx
   2.288 +	popw	%ax
   2.289 +#else
   2.290 +	popl	4(%si)
   2.291 +	popaw
   2.292 +#endif
   2.293 +#ifdef BZIMAGE
   2.294 +	jmp	movesys
   2.295 +#endif
   2.296 +#endif
   2.297 +#ifndef BZIMAGE
   2.298 +movesys:				// %ax = SYSSEG, %bx = DS, %si
   2.299 +	shrclw	$4, %si
   2.300 +	addw	%si, %bx
   2.301 +#ifdef TINY_ZIMAGE
   2.302 +	movw	$0xFFFF, %cx
   2.303 +	xorw	%si, %si
   2.304 +	xorw	%di, %di
   2.305 +	cmpw	%ax, %bx
   2.306 +	jnc	forward
   2.307 +	decw	%si
   2.308 +	decw	%di
   2.309 +	std
   2.310 +forward:
   2.311 +	movw	%ax, %es
   2.312 +	movw	%bx, %ds
   2.313 +	rep
   2.314 +	  movsb
   2.315 +	cld
   2.316 +#else
   2.317 +	subw	%ax, %bx
   2.318 +	jnc	forward
   2.319 +	movb	$0x90, %ah
   2.320 +	;decw	%ax
   2.321 +forward:
   2.322 +	movw	%ax, %es
   2.323 +	movw	%ax, %di
   2.324 +	addw	%bx, %di
   2.325 +	movw	%di, %ds
   2.326 +	sbbw	%di, %di		// %di = 0 : -1
   2.327 +	cmc				//  C  = 1 :  0
   2.328 +	adcw	%di, %ax
   2.329 +	xorw	%si, %si
   2.330 +	xorw	%di, %di
   2.331 +	movb	$0x10, %cl
   2.332 +	cmpb	%cl, %ah		// move 512k
   2.333 +	rep
   2.334 +	  movsb
   2.335 +	jns	forward
   2.336 +#endif
   2.337 +#ifndef NO_CURSOR_DEFINITION
   2.338 +	movb	$1, %ah
   2.339 +	movb	$0, %bh
   2.340 +	movb	$0x20, %ch	// 0x2000
   2.341 +	int	$0x10
   2.342 +#endif
   2.343 +	pushw	%ss
   2.344 +	popw	%ds
   2.345 +	jmp	jmp_setup
   2.346 +#endif
   2.347 +comstart:
   2.348 +	cld				# assume nothing
   2.349 +#ifdef ONLY8086
   2.350 +	movw	$INITSEG, %ax
   2.351 +	pushw	%ax
   2.352 +#else
   2.353 +	pushw	$INITSEG
   2.354 +#endif
   2.355 +	popw	%es
   2.356 +#ifdef CMDLINE
   2.357 +	movw	%sp, %di
   2.358 +	movw	$0x80, %si
   2.359 +	lodsb
   2.360 +	cbw
   2.361 +	xchgw	%ax, %cx
   2.362 +	jcxz	nocmdline
   2.363 +# if defined(BZIMAGE) && BZIMAGE >= 0x202
   2.364 +	movw	$INITSEG/16+stacktop/256, EXEADRS(cmd_line_ptr+1)
   2.365 +# else
   2.366 +	movw	$0xA33F, 0x7F(%si)
   2.367 +# endif
   2.368 +# ifndef NO_CMDLINE_SHRINK
   2.369 +skipspace:
   2.370 +	lodsb
   2.371 +	cmpb	$0x20, %al
   2.372 +	je	skipspace
   2.373 +#  ifndef NO_CMDLINE_FILE
   2.374 +	movw	%si,%dx
   2.375 +	decw	%si
   2.376 +	subb	$'@',%al
   2.377 +	jne	notafile
   2.378 +	movb	$0x3D,%ah
   2.379 +	int	$0x21
   2.380 +	jc	notafile
   2.381 +	xchgw	%ax,%bx
   2.382 +	//movw	%si,%dx		// ~320 bytes max
   2.383 +	movw	$EXEADRS(notafile)-130,%cx
   2.384 +	movb	$0x3F,%ah
   2.385 +	int	$0x21
   2.386 +	xchgw	%ax,%cx
   2.387 +notafile:
   2.388 +#  else
   2.389 +	decw	%si
   2.390 +#  endif
   2.391 +# endif
   2.392 +	rep
   2.393 +	  movsb
   2.394 +nocmdline:
   2.395 +	orb	EXEADRS(setup_sects), %ch
   2.396 +# ifndef NO_MINSETUP
   2.397 +#  ifndef BZIMAGE
   2.398 +	jnz	setupok
   2.399 +	mov	$4, %ch
   2.400 +setupok:
   2.401 +#  endif
   2.402 +# endif
   2.403 +#else
   2.404 +	movb	EXEADRS(setup_sects), %ch
   2.405 +#endif
   2.406 +	movb	$(512-(end_header-_start))/2, %cl
   2.407 +	movw	$0x100, %si
   2.408 +	movw	$end_header, %di
   2.409 +	rep
   2.410 +	  movsw
   2.411 +	movw	$SYSSEG, %ax
   2.412 +	movw	%ds, %bx
   2.413 +	pushw	%es
   2.414 +	popw	%ss
   2.415 +#ifndef SHUTDOWNDOS
   2.416 +	pushw	%es
   2.417 +	pushw	$movesys
   2.418 +#endif
   2.419 +#ifdef VCPI
   2.420 +	pushaw
   2.421 +	smsww	%ax
   2.422 +	andb	$1, %al
   2.423 +	je	isrm
   2.424 +	movw	$EXEADRS(gdt_vcpi),%si
   2.425 +	movw	$pagebuf-0x90000,%di	// %es = 0x9000
   2.426 +	movl	$pagebuf+3,%es:0x1000(%di)
   2.427 +call_vcpi:
   2.428 +	movb	$0xDE,%ah	// DE01, EBX = getiface(DS:SI, ES:DI)
   2.429 +	int	$0x67
   2.430 +	movl	$FLAT20(sw2pm_params),%esi
   2.431 +	movb	$0x0C,%al	// DE0C switchpm(ESI)
   2.432 +	jmp	call_vcpi
   2.433 +pm_code:
   2.434 +	movl	%cr0,%eax
   2.435 +	andl	$0x7FFFFFFE,%eax
   2.436 +	movl	%eax,%cr0
   2.437 +	movl	%eax,%cr3
   2.438 +isrm:
   2.439 +# ifdef SHUTDOWNDOS
   2.440 +	trace_int19
   2.441 +# else
   2.442 +	lssw	%cs:EXEADRS(saved_ss_sp),%sp	
   2.443 +	popaw
   2.444 +	retf
   2.445 +# endif
   2.446 +#else
   2.447 +# ifdef SHUTDOWNDOS
   2.448 +#  ifdef ONLY8086
   2.449 +	pushw	%ax
   2.450 +	pushw	%bx
   2.451 +	pushw	%cx
   2.452 +	pushw	%dx
   2.453 +	pushw	%si
   2.454 +	pushw	%di
   2.455 +	pushw	%bp
   2.456 +#  else
   2.457 +	pushaw
   2.458 +#  endif
   2.459 +	trace_int19
   2.460 +# endif
   2.461 +	retf
   2.462 +#endif
   2.463 +#endif
   2.464 +
   2.465 +# read_sectors reads %di sectors into %es:0 buffer.
   2.466 +# %es:0 is updated to the next memory location.
   2.467 +# First, sectors are read sector by sector until
   2.468 +# sector per track count is known. Then they are
   2.469 +# read track by track.
   2.470 +# Assume no error on first track.
   2.471 +
   2.472 +#ifdef FLOPPY_1440K_ONLY
   2.473 +#ifndef FLOPPY_HAS_2_SIDES
   2.474 +#define FLOPPY_HAS_2_SIDES	hardcore heads count to 2
   2.475 +#endif
   2.476 +#define FLOPPY_SECTORS		18	/* 18 sectors */
   2.477 +#else
   2.478 +#define FLOPPY_HEADS		2	/* 2 heads minimum */
   2.479 +#endif
   2.480 +
   2.481 +return:
   2.482 +#ifndef NO_DOTS
   2.483 +	movw	$0xE2E,%ax
   2.484 +	int	$0x10
   2.485 +#endif
   2.486 +	ret
   2.487 +
   2.488 +check_limits:
   2.489 +#ifndef FLOPPY_1440K_ONLY
   2.490 +	popw	%dx
   2.491 +        cmpb    %al, %cl		# max sector known ?
   2.492 +        ja	next_head		#   no -> store it
   2.493 +#ifndef FLOPPY_HAS_2_SIDES
   2.494 +#ifdef FLOPPY_HEADS
   2.495 +	cmpb	$FLOPPY_HEADS, %dh	# 2 heads minimum
   2.496 +	jb	check_cylinder
   2.497 +#endif
   2.498 +        cmpb    %ah, %dh		# max head known ?
   2.499 +        ja	next_cylinder		#   no -> store it
   2.500 +check_cylinder:
   2.501 +#endif
   2.502 +	pushw	%ax
   2.503 +	cbw				# %ah = 0
   2.504 +#else
   2.505 +	pushw	%dx
   2.506 +#endif
   2.507 +        int     $0x13			# reset controler
   2.508 +#ifndef FLOPPY_1440K_ONLY
   2.509 +	popw	%ax
   2.510 +	movb	$1, %al			# sector by sector...
   2.511 +#else
   2.512 +	movw	$1, %ax
   2.513 +	jmp	more1trk
   2.514 +#endif
   2.515 +read_sectorslp:
   2.516 +	pushw	%dx			# some bios break dx...
   2.517 +#ifndef FLOPPY_1440K_ONLY
   2.518 +        pushw   %ax			# limits
   2.519 +	subb	%cl, %al		# sectors remaining in track
   2.520 +	ja	tolastsect
   2.521 +	movb	$1, %al			# first track sector by sector
   2.522 +tolastsect:
   2.523 +#else
   2.524 +	movb	$FLOPPY_SECTORS+1, %al
   2.525 +	subb	%cl, %al		# sectors remaining in track
   2.526 +#endif
   2.527 +	cbw
   2.528 +	cmpw	%di, %ax
   2.529 +	jb	more1trk
   2.530 +	movw	%di, %ax		# sectors to read
   2.531 +more1trk:
   2.532 +	pushw	%ax			# save context
   2.533 +	movb	$2, %ah			# cmd: read chs
   2.534 +        int     $0x13
   2.535 +#ifndef FLOPPY_1440K_ONLY
   2.536 +	popw	%dx			# save %ax
   2.537 +        popw    %ax			# limits
   2.538 +#else
   2.539 +	popw	%ax			# restore context
   2.540 +	popw	%dx
   2.541 +#endif
   2.542 +	jc	check_limits
   2.543 +#ifndef FLOPPY_1440K_ONLY
   2.544 +	xchgw	%ax, %bp
   2.545 +	addw	%dx,%cx			# next sector
   2.546 +	movw	%cx, %si
   2.547 +	pushw	%dx
   2.548 +	shlclw	$5, %dx
   2.549 +	movw	%es, %cx
   2.550 +	addw	%dx, %cx
   2.551 +	popw	%dx
   2.552 +	subw	%dx,%di			# update sector counter
   2.553 +	popw	%dx
   2.554 +#else
   2.555 +	addw	%ax,%cx			# next sector
   2.556 +	movw	%cx, %si
   2.557 +	pushw	%ax
   2.558 +	shlclw	$5, %ax
   2.559 +	movw	%es, %cx
   2.560 +	addw	%ax, %cx
   2.561 +	popw	%ax
   2.562 +	subw	%ax,%di			# update sector counter
   2.563 +#endif
   2.564 +read_sectorsCX:
   2.565 +	movw	%cx, %es		# next location
   2.566 +	jz	return
   2.567 +read_sectors:
   2.568 +	movw	%si, %cx
   2.569 +#ifndef FLOPPY_1440K_ONLY
   2.570 +#   al is last sector+1
   2.571 +#   ah is last head+1
   2.572 +	xchgw	%ax, %bp
   2.573 +#endif
   2.574 +#ifndef FLOPPY_1440K_ONLY
   2.575 +        cmpb    %al,%cl			# reach sector limit ?
   2.576 +        jne     bdendlp
   2.577 +next_head:
   2.578 +        movb    %cl,%al
   2.579 +#else
   2.580 +        cmpb    $FLOPPY_SECTORS+1,%cl	# reach sector limit ?
   2.581 +        jne     bdendlp
   2.582 +#endif
   2.583 +        movb    $1,%cl			# first sector
   2.584 +#ifndef FLOPPY_HAS_2_SIDES
   2.585 +        incb    %dh			# next head
   2.586 +        cmpb    %ah, %dh		# reach head limit ?
   2.587 +        jne     bdendlp
   2.588 +next_cylinder:
   2.589 +        movb    %dh,%ah
   2.590 +        movb    $0,%dh			# first head
   2.591 +#else
   2.592 +        xorb    %cl,%dh			# next head
   2.593 +        jne	bdendlp			# reach head limit ?
   2.594 +#endif
   2.595 +# NOTE : support 256 cylinders max
   2.596 +        incb    %ch			# next cylinder
   2.597 +read_first_sectors:
   2.598 +bdendlp:
   2.599 +        jmp	read_sectorslp
   2.600 +
   2.601 +#ifdef VCPI
   2.602 +pagebuf		=	0x98000
   2.603 +tss		=	gdt_abs-40
   2.604 +gdt		=	gdt_abs-32
   2.605 +gdt_null	=	gdt_abs-32
   2.606 +gdt_vcpi	=	gdt_abs-24
   2.607 +gdt_vcpi2	=	gdt_abs-16
   2.608 +gdt_vcpi3	=	gdt_abs-8
   2.609 +gdt_abs:
   2.610 +	.word	0xFFFF
   2.611 +	.long	0x92000000
   2.612 +	.byte	0xCF,0
   2.613 +gdt_code:
   2.614 +	.word	0xFFFF
   2.615 +gdt_code_base:
   2.616 +	.long	0x9A000000+FLAT20(0)
   2.617 +	.byte	0x8F,0
   2.618 +gdt_tss:
   2.619 +	.word	0x00FF
   2.620 +gdt_tss_base:
   2.621 +	.long	0x89000000+FLAT20(tss)
   2.622 +	.byte	0,0
   2.623 +gdtr:
   2.624 +gdt_lim:
   2.625 +	.word	0xFFFF
   2.626 +gdt_base:
   2.627 +	.long	FLAT20(gdt)
   2.628 +sw2pm_params:
   2.629 +sw2pm_cr3:
   2.630 +	.long	pagebuf+0x1000
   2.631 +sw2pm_gdtr_ptr:
   2.632 +	.long	FLAT20(gdtr)
   2.633 +sw2pm_idtr_ptr:
   2.634 +	.long	FLAT20(idtr)
   2.635 +sw2pm_ldtr:
   2.636 +	.word	0
   2.637 +sw2pm_tr:
   2.638 +SEL_TSS		= gdt_tss-gdt_null
   2.639 +	.word	SEL_TSS
   2.640 +sw2pm_jumpaddr:
   2.641 +	.long	pm_code
   2.642 +SEL_CODE	= gdt_code-gdt_null
   2.643 +	.word	SEL_CODE
   2.644 +idtr:
   2.645 +idt_lim:
   2.646 +	.word	0x03FF
   2.647 +idt_base:
   2.648 +	.long	0
   2.649 +# ifndef SHUTDOWNDOS
   2.650 +saved_ss_sp:
   2.651 +	.word	stacktop-4-16-4,INITSEG
   2.652 +# endif
   2.653 +#endif
   2.654 +#ifdef ELKS
   2.655 +	.org	0x1E3
   2.656 +	.byte	13,10,7
   2.657 +	.asciz	"ELKS Boot"
   2.658 +#endif
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/plop/stuff/pack	Wed Aug 19 13:03:10 2020 +0000
     3.3 @@ -0,0 +1,76 @@
     3.4 +#!/bin/sh
     3.5 +if [ "$1" == "--build" ]; then
     3.6 +	set -- ${2:-bootloader.bin} ${3:-unpack.bin}
     3.7 +	x=$(grep -s packed_syssize$ ${2/.bin/.lst}|sed '$!d;s/.*t:\([^ ]*\).*/\1/')
     3.8 +	[ -n "$x" ] && sed -i "s/XXXP/$((513+0x$x))/" $0 || sed -i "/XXXP$/d" $0
     3.9 +	x=$(grep -s setup_byte$ ${2/.bin/.lst}|sed '$!d;s/.*t:\([^ ]*\).*/\1/')
    3.10 +	[ -n "$x" ] && sed -i "s/XXXSB/$((513+0x$x))/" $0 || sed -i "/XXXSB/d" $0
    3.11 +	x=$(grep -s setup_word$ ${2/.bin/.lst}|sed '$!d;s/.*t:\([^ ]*\).*/\1/')
    3.12 +	[ -n "$x" ] && sed -i "s/XXXSW/$((514+0x$x))/" $0 || sed -i "/XXXSW/d" $0
    3.13 +	x=$(grep -s setup_para$ ${2/.bin/.lst}|sed '$!d;s/.*t:\([^ ]*\).*/\1/')
    3.14 +	[ -n "$x" ] && sed -i "s/XXXSP/$((514+0x$x))/" $0 || sed -i "/XXXSP/d" $0
    3.15 +	x=$(grep -s setup_seg$ ${2/.bin/.lst}|sed '$!d;s/.*t:\([^ ]*\).*/\1/')
    3.16 +	[ -n "$x" ] && sed -i "s/XXXSS/$((513+0x$x))/" $0 || sed -i "/XXXSS/d" $0
    3.17 +	x=$(grep -s setup_keep$ ${2/.bin/.lst}|sed '$!d;s/.*t:\([^ ]*\).*/\1/')
    3.18 +	[ -n "$x" ] && sed -i "s/XXXK1/1/" $0 || sed -i "/XXXK1/d" $0
    3.19 +	[ -n "$x" ] && sed -i "s/skip=XXXK2 //" $0 || sed -i "s/XXXK2/1/" $0
    3.20 +	[ -n "$x" ] && sed -i "s/+XXXK3//" $0 || sed -i "s/XXXK3/1/" $0
    3.21 +	grep -q BFF401 ${2/.bin/.lst} && sed -i 's/512 skip/500 skip/;s/1000 -/&32 - /' $0
    3.22 +	grep -q 83C60D ${2/.bin/.lst} && sed -i 's/ | dd bs=1 skip=13//' $0
    3.23 +	helpmsg=$(grep -s helpmsg$ ${1/.bin/.lst}|sed '$!d;s/.*t:\([^ ]*\).*/\1/')
    3.24 +	helpend=$(grep -s helpend$ ${1/.bin/.lst}|sed '$!d;s/.*t:\([^ ]*\).*/\1/')
    3.25 +	if [ -n "$helpmsg" ]; then
    3.26 +		sed -i "s/YYY/$((0x$helpmsg))/" $0
    3.27 +		sed -i "s/ZZZ/$((0x$helpend-0x$helpmsg))/" $0
    3.28 +	else
    3.29 +		sed -i '/YYY/d' $0
    3.30 +	fi
    3.31 +	cat >> $0 <<EOM
    3.32 +$(dd if=$1 bs=512 count=1 conv=sync 2> /dev/null | cat - $2 | gzip -9 | uuencode -m -)
    3.33 +EOT
    3.34 +EOM
    3.35 +	sed -i '/--build/,/^fi/d' $0
    3.36 +	exit
    3.37 +fi
    3.38 +
    3.39 +store()
    3.40 +{
    3.41 +	n=$1
    3.42 +	for i in $(seq 1 ${4:-2}); do
    3.43 +		printf '\\\\x%02X' $(($n & 255))
    3.44 +		n=$(($n >> 8))
    3.45 +	done | xargs echo -en | dd conv=notrunc bs=1 of=$2 seek=$3
    3.46 +}
    3.47 +
    3.48 +compress()
    3.49 +{
    3.50 +	xz -z -e --format=lzma --lzma1=mf=bt2 --stdout | dd bs=1 skip=13
    3.51 +}
    3.52 +
    3.53 +main()
    3.54 +{
    3.55 +	uudecode | gunzip >$2
    3.56 +	dd if=$1 bs=1 skip=497 seek=497 count=15 of=$2 conv=notrunc
    3.57 +	setup="$(echo $(od -j 497 -N 1 -dAn $1))"
    3.58 +	[ $setup -eq 0 ] && setup=4
    3.59 +	setup=$(($setup+XXXK1))
    3.60 +	store $setup $2 XXXSB 1
    3.61 +	store $setup $2 XXXSW 1
    3.62 +	store $((32 * $setup)) $2 XXXSP
    3.63 +	store $((0x1000 - 32*$setup)) $2 XXXSS 
    3.64 +	dd if=$1 bs=512 skip=XXXK2 | compress >>$2
    3.65 +	size=$(stat -c %s $2)
    3.66 +	syssize=$((($size-512*($setup+XXXK3)+15)/16))
    3.67 +	store $(($syssize + 0x1000)) $2 XXXP
    3.68 +	store $syssize $2 500
    3.69 +	store 4 $2 497 1
    3.70 +	store $((($size+511)/512)) $2 4
    3.71 +	store $((($size+15)/16)) $2 14
    3.72 +	echo -e "$3" | dd conv=notrunc bs=1 seek=YYY of=$2 count=ZZZ
    3.73 +}
    3.74 +
    3.75 +HELP="$(unix2dos <<EOT
    3.76 +Plop Boot Manager VERSION
    3.77 +EOT
    3.78 +)"
    3.79 +main $1 ${2:-$1.packed} "${3:-$HELP}" 2>/dev/null <<EOT
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/plop/stuff/unlzma.S	Wed Aug 19 13:03:10 2020 +0000
     4.3 @@ -0,0 +1,1019 @@
     4.4 +// #define RC_NORMALIZE if (Range < kTopValue) { Range <<= 8; Code = (Code << 8) | RC_READ_BYTE; }
     4.5 +//
     4.6 +// #define IfBit0(p) RC_NORMALIZE; bound = (Range >> kNumBitModelTotalBits) * *(p); if (Code < bound)
     4.7 +// #define UpdateBit0(p) Range = bound; *(p) += (kBitModelTotal - *(p)) >> kNumMoveBits;
     4.8 +// #define UpdateBit1(p) Range -= bound; Code -= bound; *(p) -= (*(p)) >> kNumMoveBits;
     4.9 +//
    4.10 +//#define RC_GET_BIT2(p, mi, A0, A1) IfBit0(p) \
    4.11 +//  { UpdateBit0(p); mi <<= 1; A0; } else \
    4.12 +//  { UpdateBit1(p); mi = (mi + mi) + 1; A1; }
    4.13 +//
    4.14 +// #define RC_GET_BIT(p, mi) RC_GET_BIT2(p, mi, ; , ;)
    4.15 +//
    4.16 +// #define RangeDecoderBitTreeDecode(probs, numLevels, res) \
    4.17 +//  { int i = numLevels; res = 1; \
    4.18 +//  do { CProb *p = probs + res; RC_GET_BIT(p, res) } while(--i != 0); \
    4.19 +//  res -= (1 << numLevels); }
    4.20 +/*
    4.21 + * Compression with : lzma e src dst -eos -pb2 -lp0 -lc3
    4.22 + */
    4.23 +
    4.24 +#define PROP_PB 2
    4.25 +#define PROP_LP 0
    4.26 +#define PROP_LC 3
    4.27 +#define PROPS (PROP_LC+(PROP_LP*9)+(PROP_PB*45))
    4.28 +
    4.29 +// static const Byte *Buffer;
    4.30 +// static UInt32 bound, Code, Range;
    4.31 +
    4.32 +/*
    4.33 + * Buffer register DS:SI
    4.34 + * all var based ws=ss:bp
    4.35 + */
    4.36 +
    4.37 +rep0		=	-4		// long
    4.38 +rep1		=	rep0-4		// long
    4.39 +rep2		=	rep0-8		// long
    4.40 +rep3		=	rep0-12		// long
    4.41 +state		=	-17		// byte, 0..11
    4.42 +posState 	=	state-1		// byte, 0..15
    4.43 +posState2 	=	posState-1	// byte, 0..15
    4.44 +scratched	=	rep0-16		// byte = 1
    4.45 +Code		=	-24		// long
    4.46 +outStream	=	-28		// long
    4.47 +nowPos		=	outStream	// long
    4.48 +Range		=	Code-8		// long
    4.49 +#define LOCALS		32
    4.50 +
    4.51 +// int LzmaDecode(CLzmaDecoderState *vs,
    4.52 +//     const unsigned char *inStream, 
    4.53 +//     unsigned char *outStream)
    4.54 +// {
    4.55 +//   CProb *p = vs->Probs;
    4.56 +//   SizeT nowPos = 0;
    4.57 +//   #define posStateMask = (1 << (vs->Properties.pb)) - 1;
    4.58 +//   #define literalPosMask = (1 << (vs->Properties.lp)) - 1;
    4.59 +//   int lc = vs->Properties.lc, state = 0, len = 0;
    4.60 +//   UInt32 rep0 = 1, rep1 = 1, rep2 = 1, rep3 = 1;
    4.61 +// 
    4.62 +//   {
    4.63 +//     UInt32 i, numProbs = Literal /*1846*/
    4.64 +// 	    + ((UInt32)LZMA_LIT_SIZE /*768*/ << (lc + vs->Properties.lp));
    4.65 +//     for (i = 0; i < numProbs; i++) p[i] = kBitModelTotal /*2048*/ >> 1;
    4.66 +
    4.67 +#define WS (1846+(768<<(PROP_LC+PROP_LP)))
    4.68 +#if (WS+WS+LOCALS) >= 65000
    4.69 +/* MAX WS = (1846+(768<<(8+4))) > 3MB! */
    4.70 +#error invalid (lc,lp,pb) : out of memory
    4.71 +#endif
    4.72 +
    4.73 +ws1	=	WS
    4.74 +ws2	=	ws1*2
    4.75 +ws	=	ws2+LOCALS+15
    4.76 +
    4.77 +#ifndef FLAT32
    4.78 +#define	AX	%ax
    4.79 +#define	BX	%bx
    4.80 +#define	CX	%cx
    4.81 +#define	DX	%dx
    4.82 +#define	SI	%si
    4.83 +#define	DI	%di
    4.84 +#define	BP	%bp
    4.85 +#define	SP	%sp
    4.86 +#define CWD	cwd
    4.87 +#else
    4.88 +#define	AX	%eax
    4.89 +#define	BX	%ebx
    4.90 +#define	CX	%ecx
    4.91 +#define	DX	%edx
    4.92 +#define	SI	%esi
    4.93 +#define	DI	%edi
    4.94 +#define	BP	%ebp
    4.95 +#define	SP	%esp
    4.96 +#define CWD	cdq
    4.97 +#endif
    4.98 +/*
    4.99 + * LzmaDecode:
   4.100 +#ifndef FLAT32
   4.101 + *   input   ds:si=inStream, es:di=outStream
   4.102 + *   output  outStream[], ds:si, es:di
   4.103 + 	.code 16
   4.104 +#else
   4.105 + *   input   esi=inStream, edi=outStream
   4.106 + *   output  outStream[], esi, edi
   4.107 + 	.code 32
   4.108 +#endif
   4.109 + */
   4.110 + 
   4.111 +	mov	$ws1, CX
   4.112 +#ifdef ONLY8086
   4.113 +	movw	$2048/2, %ax
   4.114 +lzd1:
   4.115 +	pushw	%ax
   4.116 +#else
   4.117 +lzd1:
   4.118 +	pushw	$2048/2
   4.119 +#endif
   4.120 +	loop	lzd1
   4.121 +	mov	SP, BP
   4.122 +	movb	$((LOCALS+3)/4)*2, %cl
   4.123 +#ifdef ONLY8086
   4.124 +	movw	$1, %ax
   4.125 +	cwd
   4.126 +initlocals:
   4.127 +	pushw	%dx
   4.128 +	pushw	%ax
   4.129 +#else
   4.130 +initlocals:
   4.131 +	pushl	$1
   4.132 +#endif
   4.133 +	loop	initlocals
   4.134 +
   4.135 +#if !defined(FLAT32) && !defined(FLAT16OUT)
   4.136 +	movb	$4, %cl
   4.137 +	movw	%es, %bx
   4.138 +	shrw	%cl, %bx
   4.139 +	movw	%es, %dx
   4.140 +	shlw	%cl, %dx
   4.141 +	addw	%dx, %di
   4.142 +	movw	%di, outStream(%bp)
   4.143 +	adcb	%bh, outStream+2(%bp)
   4.144 +	incw	%cx
   4.145 +#else
   4.146 +	movb	$5, %cl
   4.147 +	mov	DI, outStream(BP)
   4.148 +#endif
   4.149 +
   4.150 +//   Byte previousByte = 0;
   4.151 +	xor	BX, BX
   4.152 +
   4.153 +// #define RC_INIT(buffer) 
   4.154 +//    Buffer = buffer; Code = 0; Range = 0xFFFFFFFF; 
   4.155 +//    { int i; for(i=0; i<5; i++) { Code = (Code<<8) | RC_READ_BYTE; }}
   4.156 +//   }
   4.157 +//   RC_INIT(inStream);
   4.158 +
   4.159 +#ifndef NO_LZMA_HEADER
   4.160 +#ifdef CHECK_LZMA_HEADER
   4.161 +	cmp.w	$0x5A4C, (SI)	// lzip header ('LZIP' version:1 dicobits:1)
   4.162 +	je	lzip_header
   4.163 +	cmp.w	$0x5D, (SI)	// lzma header (0x5D dicosz:4 orgsz:8)
   4.164 +	jne	no_header
   4.165 +	add	$13-6, SI	// skip lzma header
   4.166 +lzip_header:
   4.167 +	add	$6, SI		// skip lzip header
   4.168 +no_header:
   4.169 +#else
   4.170 +	add	$13, SI		// skip lzma header (0x5D dicosz:4 orgsz:8)
   4.171 +#endif
   4.172 +#endif
   4.173 +setrep:
   4.174 +	call	RC_LOAD_BYTE
   4.175 +	decb	Range(BP)
   4.176 +	loop	setrep
   4.177 +
   4.178 +lzdmainlp:
   4.179 +//   while(1) {
   4.180 +//     CProb *prob;
   4.181 +//     int posState = (int)((nowPos) & posStateMask);
   4.182 +// 
   4.183 +//     prob = p + IsMatch /*0*/ + (state << kNumPosBitsMax /*4*/) + posState;
   4.184 +//     if (Bit0(prob)) { /* char */
   4.185 +
   4.186 +	xor	DX, DX
   4.187 +	call	Bit1state	// Bit1(dx + (state << kNumPosBitsMax /*4*/) + posState)
   4.188 +	mov	$state, DI
   4.189 +	jc	lzdstring
   4.190 +
   4.191 +//       prob = p + Literal /*1846*/ + (LZMA_LIT_SIZE /*768*/ * 
   4.192 +// 	((((nowPos) & literalPosMask) << lc) + (previousByte >> (8 - lc))));
   4.193 +
   4.194 +#if PROP_LC != 0
   4.195 +# ifdef ONLY8086
   4.196 +	movb	$8-PROP_LC, %cl
   4.197 +	shrb	%cl, %bl
   4.198 +# else
   4.199 +	shrb	$8-PROP_LC, %bl
   4.200 +# endif
   4.201 +#endif
   4.202 +
   4.203 +#if PROP_LP != 0
   4.204 +	movb	posState2(BP), %dl
   4.205 +# ifdef ONLY8086
   4.206 +	movb	$PROP_LC, %cl
   4.207 +	shl	%cl, DX
   4.208 +# else
   4.209 +	shl	$PROP_LC, DX
   4.210 +# endif
   4.211 +	movb	$0, %bh
   4.212 +	add	BX, DX
   4.213 +#endif
   4.214 +
   4.215 +	movb	$3, %ah
   4.216 +	mul	BX		// dx = 3*bh
   4.217 +	add	$1846, AX
   4.218 +
   4.219 +//       int symbol = 1;
   4.220 +
   4.221 +	CWD
   4.222 +	inc	DX		// symbol = 1
   4.223 +	xchg	AX, CX		// save prob
   4.224 +
   4.225 +//       if (state >= kNumLitStates /*7*/) { /* previous was string */
   4.226 +//       if (state < 4) state = 0;
   4.227 +
   4.228 +lzd6z:
   4.229 +	subb	$3, (BP, DI)
   4.230 +
   4.231 +//       if (state < 4) state = 0;
   4.232 +
   4.233 +	jnc	lzd6
   4.234 +	movb	%dh, (BP, DI)	// %dh = 0
   4.235 +
   4.236 +lzd6:
   4.237 +//       else if (state < 10) state -= 3;
   4.238 +
   4.239 +	cmpb	$10-3, (BP, DI)
   4.240 +	
   4.241 +//       else state -= 6;
   4.242 +
   4.243 +	jnb	lzd6z
   4.244 +	cmpb	$7-3-1, (BP, DI)
   4.245 +	jbe	lzd3
   4.246 +	
   4.247 +//         int matchByte = outStream[nowPos - rep0];
   4.248 +
   4.249 +	call	DicoRep02ESDI	// %bl = outStream[nowPos - rep0];
   4.250 +	
   4.251 +//         do {
   4.252 +//           int bit;
   4.253 +//           CProb *probLit;
   4.254 +//           matchByte <<= 1; bit = (matchByte & 0x100);
   4.255 +
   4.256 +	movb	$1, %bh
   4.257 +lzd4:
   4.258 +	shlb	$1, %bl			// matchByte <<= 1
   4.259 +	sbb	DI, DI			// save bit=C
   4.260 +
   4.261 +//           probLit = prob + 0x100 + bit + symbol;
   4.262 +
   4.263 +	mov	CX, AX			// restore prob
   4.264 +	adcb	%bh, %ah		// + bit + 0x100
   4.265 +	
   4.266 +//           RC_GET_BIT2(probLit, symbol, if (bit) break, if (!bit) break)
   4.267 +
   4.268 +	call	Bit1axdx		// C,%ax = Bit1(prob+%ax)
   4.269 +	rclb	$1, %dl			// symbol <<= 1; symbol |= C
   4.270 +	jc	lzd5			// if symbol >= 0x100
   4.271 +	cmp	DI, AX
   4.272 +	jz	lzd4			// if bit == Bit1(prob+%ax)
   4.273 +
   4.274 +//         } while (symbol < 0x100);
   4.275 +//       }
   4.276 +lzd3:
   4.277 +//       while (symbol < 0x100) {
   4.278 +//         CProb *probLit = prob + symbol;
   4.279 +//         RC_GET_BIT(probLit, symbol)
   4.280 +//       }
   4.281 +
   4.282 +	xor	BX, BX
   4.283 +	jmp	lzd4
   4.284 +lzd5:
   4.285 +
   4.286 +//       outStream[nowPos++] = previousByte = (Byte)symbol;
   4.287 +
   4.288 +	xchg	AX, DX
   4.289 +	call	outchar		// %bl = outStream[nowPos++] = %al;
   4.290 +	jmp	lzdmainlp
   4.291 +
   4.292 +//     }
   4.293 +
   4.294 +lzdstring:
   4.295 +	mov	$1, CX
   4.296 +
   4.297 +//     else { /* string */
   4.298 +//       prob = p + IsRep /*192*/ + state;
   4.299 +
   4.300 +	movb	$192, %dl
   4.301 +	addb	(BP, DI), %dl
   4.302 +	mov	$rep0, DI
   4.303 +
   4.304 +//       if (Bit0(prob)) {
   4.305 +
   4.306 +	call	Bit1dx		// Bit1(prob)
   4.307 + 	jc	lzd8
   4.308 +
   4.309 +//         rep3 = rep2; rep2 = rep1; rep1 = rep0;
   4.310 +//         state = (state < kNumLitStates /*7*/) ? 0 : 3;
   4.311 +
   4.312 +	stc
   4.313 +
   4.314 +//         prob = p + LenCoder /*818*/;
   4.315 +
   4.316 +	mov	$818, DX
   4.317 +
   4.318 +//       }
   4.319 +
   4.320 +	jmp	lzd11a
   4.321 +
   4.322 +//       else {
   4.323 +lzd8:
   4.324 +//         prob += kNumStates /*12*/;
   4.325 +//         if (Bit0(prob)) {
   4.326 +	call	Bit1dx12	// prob += 12; Bit1(prob)
   4.327 +	jc	lzd11
   4.328 +//           prob = p + IsRep0Long /*240*/ + (state << kNumPosBitsMax /*4*/) 
   4.329 +// 		   + posState;
   4.330 +	movb	$240, %dl	// dh=0
   4.331 +
   4.332 +//           if (Bit0(prob)) {
   4.333 +
   4.334 +	call	Bit1state	// Bit1(dx + (state << kNumPosBitsMax /*4*/) + posState)
   4.335 +	jc	lzd12
   4.336 +
   4.337 +//             // if (nowPos == 0) return LZMA_RESULT_DATA_ERROR;
   4.338 +//             state = (state < kNumLitStates /*7*/) ? 9 : 11;
   4.339 +
   4.340 +	movb	$9, %dl
   4.341 +
   4.342 +//             len++; goto string;
   4.343 +	jmp	lzd13string	// ax = 0
   4.344 +//           }
   4.345 +//         }
   4.346 +//         else {
   4.347 +lzd11:
   4.348 +//           UInt32 distance = rep1;
   4.349 +//           prob += kNumStates /*12*/;
   4.350 +//           if (!Bit0(prob)) {
   4.351 +
   4.352 +	call	Bit1dx12	// prob += 12; Bit1(prob)
   4.353 +	jnc	lzd11z
   4.354 +
   4.355 +//             prob += kNumStates /*12*/;
   4.356 +//             if (Bit0(prob)) distance = rep2;
   4.357 +
   4.358 +	call	Bit1dx12	// prob += 12; Bit1(prob)
   4.359 +lzd11a:
   4.360 +	adcb	%cl, %cl
   4.361 +	
   4.362 +//             else { distance = rep3; rep3 = rep2; }
   4.363 +//             rep2 = rep1;
   4.364 +//           }
   4.365 +//           rep1 = rep0; rep0 = distance;
   4.366 +
   4.367 +lzd11z:
   4.368 +# ifdef ONLY8086
   4.369 +	shl	$1, CX
   4.370 +	shl	$1, CX		// 8->32 bits
   4.371 +	sub	CX, DI		// &rep[cx]
   4.372 +	movw	(BP, DI), %ax
   4.373 +	pushw	2(BP, DI)
   4.374 +rotreplp:
   4.375 +	movb	4(BP, DI), %bl
   4.376 +	movb	%bl, (BP, DI)
   4.377 +	inc	DI
   4.378 +	loop	rotreplp
   4.379 +	popw	%bx
   4.380 +	testb	%dh, %dh
   4.381 +	jnz	lzd10
   4.382 +	movw	%ax, (BP, DI)
   4.383 +	movw	%bx, 2(BP, DI)
   4.384 +# else
   4.385 +	shl	$2, CX		// 8->32 bits
   4.386 +	sub	CX, DI		// &rep[cx]
   4.387 +	movl	(BP, DI), %eax
   4.388 +rotreplp:
   4.389 +	movb	4(BP, DI), %bl
   4.390 +	movb	%bl, (BP, DI)
   4.391 +	inc	DI
   4.392 +	loop	rotreplp
   4.393 +	testb	%dh, %dh
   4.394 +	jnz	lzd10
   4.395 +	movl	%eax, (BP, DI)
   4.396 +# endif
   4.397 +
   4.398 +//         }
   4.399 +lzd12:
   4.400 +//         state = (state < kNumLitStates /*7*/) ? 8 : 11;
   4.401 +
   4.402 +	movb	$0x08, %cl
   4.403 +
   4.404 +//         prob = p + RepLenCoder /*1332*/;
   4.405 +
   4.406 +	mov	$1332, DX
   4.407 +
   4.408 +//       }
   4.409 +lzd10:
   4.410 +	push	CX		// CX = 0
   4.411 +
   4.412 +//       { /* get len */
   4.413 +//         int numBits, offset;
   4.414 +//         CProb *probLen = prob + LenChoice /*0*/;
   4.415 +//         numBits = kLenNumLowBits /*3*/;
   4.416 +
   4.417 +	movb	$8, %cl		// numBits : 3,3,8
   4.418 +
   4.419 +//         if (Bit0(probLen)) {
   4.420 +
   4.421 +	call	Bit1dx		// Bit1(prob)
   4.422 +	xchg	AX, BX
   4.423 +	inc	DX
   4.424 +	jnc	lzd15		// bx=0
   4.425 +
   4.426 +//           probLen = prob + LenLow/*2*/ + (posState << kLenNumLowBits/*3*/);
   4.427 +//           offset = 0;
   4.428 +//         }
   4.429 +//         else {
   4.430 +//           probLen = prob + LenChoice2 /*1*/;
   4.431 +
   4.432 +	call	Bit1dx		// Bit1(prob)
   4.433 +	add	AX, BX
   4.434 +
   4.435 +#if PROP_PB != 0
   4.436 +	inc	AX		// ah=0
   4.437 +#endif
   4.438 +	jc	lzd16		// %ax=0, %bx=-2 
   4.439 +lzd15:
   4.440 +#if PROP_PB != 0
   4.441 +	movb	$8, %al
   4.442 +	mulb	posState(BP)
   4.443 +#endif
   4.444 +
   4.445 +//           if (Bit0(probLen)) {
   4.446 +//             probLen = prob + LenMid/*130*/ + (posState << kLenNumMidBits/*3*/);
   4.447 +
   4.448 +	movb	$3, %cl		// numBits : 3,3,8
   4.449 +lzd16:
   4.450 +#if PROP_PB != 0
   4.451 +	add	$2-128-1, AX	// probLen : 2,130,258
   4.452 +#else
   4.453 +	mov	$2-128-1, AX	// probLen : 2,130,258
   4.454 +#endif
   4.455 +	add	DX, AX
   4.456 +	mov	$-8+1, DX	// offset  : 0,8,16
   4.457 +lzdargslp:
   4.458 +	add	$8, DX
   4.459 +	add	$128, AX
   4.460 +	inc	BX
   4.461 +	jle	lzdargslp	// leave with bx=1
   4.462 +
   4.463 +//             offset = kLenNumLowSymbols /*8*/;
   4.464 +//             //numBits = kLenNumMidBits /*3*/;
   4.465 +//           }
   4.466 +//           else {
   4.467 +//             probLen = prob + LenHigh /*258*/;
   4.468 +//             offset = kLenNumLowSymbols /*8*/ + kLenNumMidSymbols /*8*/;
   4.469 +//             numBits = kLenNumHighBits /*8*/;
   4.470 +//           }
   4.471 +//         }
   4.472 +//         RangeDecoderBitTreeDecode(probLen, numBits, len); len += offset;
   4.473 +
   4.474 +	push	DX
   4.475 +	call	RangeDecoder	// %ax=probs, %cx=numLevels, %ax=res
   4.476 +	pop	DX
   4.477 +	add	DX, AX		// offset
   4.478 +	pop	DX		// 0
   4.479 +lzd13string:
   4.480 +	push	AX
   4.481 +
   4.482 +// state = (state < kNumLitStates /*7*/) ? dl : dl|3;
   4.483 +
   4.484 +	movb	$7, %cl
   4.485 +	cmpb	%cl, state(BP)
   4.486 +	jb	new_state
   4.487 +	orb	$3, %dl
   4.488 +new_state:
   4.489 +	movb	%dl, state(BP)
   4.490 +
   4.491 +//       } /* get len */
   4.492 +//       if (state < 4) {
   4.493 +
   4.494 +	cmpb	$4-1, %dl
   4.495 +	ja	lzd19
   4.496 +
   4.497 +//         int posSlot;
   4.498 +//         state += kNumLitStates /*7*/;
   4.499 +
   4.500 +	addb	%cl, state(BP)
   4.501 +
   4.502 +//         prob = p + PosSlot /*432*/ + (((len < kNumLenToPosStates /*4*/) ? 
   4.503 +// 		len : kNumLenToPosStates - 1) << kNumPosSlotBits /*6*/);
   4.504 +
   4.505 +	cmp	$4+1, AX
   4.506 +	jb	lzd21
   4.507 +	mov	$3+1, AX
   4.508 +
   4.509 +lzd21:
   4.510 +
   4.511 +	dec	CX		// cx = 6
   4.512 +	shl	%cl, AX
   4.513 +	add	$432-64, AX
   4.514 +
   4.515 +//         RangeDecoderBitTreeDecode(prob, kNumPosSlotBits /*6*/, posSlot);
   4.516 +
   4.517 +	call	RangeDecoder	// %ax=probs, %cx=numLevels, %ax=res
   4.518 +
   4.519 +//         if (posSlot >= kStartPosModelIndex /*4*/) {
   4.520 +//           int numDirectBits = ((posSlot >> 1) - 1);
   4.521 +
   4.522 +#ifndef FLAT32
   4.523 +	movw	%cx, 2(%bp, %di)	// %cx = 0
   4.524 +#endif
   4.525 +	mov	AX, (BP, DI)
   4.526 +	mov	AX, CX
   4.527 +	shrw	$1, CX
   4.528 +	dec	CX
   4.529 +	cmpb	$4, %al
   4.530 +	jb	lzd22
   4.531 +
   4.532 +//           rep0 = (2 | ((UInt32)posSlot & 1));
   4.533 +
   4.534 +	andb	%bl, (BP, DI)		// %bx=1
   4.535 +	orb	$2, (BP, DI)
   4.536 +
   4.537 +//           if (posSlot < kEndPosModelIndex /*14*/) {
   4.538 +
   4.539 +	cmpb	$14, %al
   4.540 +	jnb	lzd23
   4.541 +
   4.542 +//             rep0 <<= numDirectBits;
   4.543 +
   4.544 +	neg	AX
   4.545 +# ifdef ONLY8086
   4.546 +	pushw	%cx
   4.547 +	movb	$0, %ch
   4.548 +shllrep0:
   4.549 +	shlw	$1, (BP, DI)
   4.550 +	rclw	$1, 2(BP, DI)
   4.551 +	loop	shllrep0
   4.552 +	popw	%cx
   4.553 +# else
   4.554 +	shll	%cl, (BP, DI)
   4.555 +# endif
   4.556 +	add	(BP, DI), AX
   4.557 +
   4.558 +//             prob = p + SpecPos /*688*/ + rep0 - posSlot - 1;
   4.559 +
   4.560 +	add	$687, AX
   4.561 +	jmp	lzd24
   4.562 +
   4.563 +//           }
   4.564 +//           else {
   4.565 +lzd23:
   4.566 +//             numDirectBits -= kNumAlignBits /*4*/;
   4.567 +//             do {
   4.568 +//               RC_NORMALIZE; Range >>= 1; rep0 <<= 1;
   4.569 +//               if (Code >= Range) { Code -= Range; rep0 |= 1; }
   4.570 +
   4.571 +lzd23z:
   4.572 +	call	RC_NORMALIZE
   4.573 +# ifdef ONLY8086
   4.574 +	pushw	%dx
   4.575 +	shrw	$1, Range+2(BP)
   4.576 +	rcrw	$1, Range(BP)
   4.577 +	movw	Range(BP), %ax
   4.578 +	movw	Range+2(BP), %dx
   4.579 +	cmpw	Code+2(BP), %dx
   4.580 +	ja	lzd25
   4.581 +	jb	lzd25x
   4.582 +	cmpw	Code(BP), %ax
   4.583 +	ja	lzd25
   4.584 +lzd25x:
   4.585 +	subw	%ax, Code(BP)
   4.586 +	sbbw	%dx, Code+2(BP)
   4.587 +	stc
   4.588 +lzd25:
   4.589 +	popw	%dx
   4.590 +	rclw	$1, (BP, DI)
   4.591 +	rclw	$1, 2(BP, DI)
   4.592 +# else
   4.593 +	shrl	$1, Range(BP)
   4.594 +	movl	Range(BP), %eax
   4.595 +	cmpl	Code(BP), %eax
   4.596 +	ja	lzd25
   4.597 +	subl	%eax, Code(BP)
   4.598 +	stc
   4.599 +lzd25:
   4.600 +	rcll	$1, (BP, DI)
   4.601 +# endif
   4.602 +
   4.603 +//             } while (--numDirectBits != 0);
   4.604 +
   4.605 +	cmpb	$4+1, %cl
   4.606 +	loopne	lzd23z
   4.607 +
   4.608 +//             prob = p + Align /* 802 */; numDirectBits = kNumAlignBits /*4*/;
   4.609 +//             rep0 <<= numDirectBits;
   4.610 +
   4.611 +# ifdef ONLY8086
   4.612 +	pushw	%cx
   4.613 +	movb	$0, %ch
   4.614 +shlrep0:
   4.615 +	shlw	$1, (BP, DI)
   4.616 +	rclw	$1, 2(BP, DI)
   4.617 +	loop	shlrep0
   4.618 +	popw	%cx
   4.619 +# else
   4.620 +	shll	%cl, (BP, DI)
   4.621 +# endif
   4.622 +	mov	$802, AX
   4.623 +//           }
   4.624 +
   4.625 +lzd24:
   4.626 +	call	RangeDecoder	// %ax=probs, %cx=numLevels, %ax=res
   4.627 +
   4.628 +//           {
   4.629 +//             int i = 1, mi = 1;
   4.630 +//             do {
   4.631 +//               CProb *prob3 = prob + mi;
   4.632 +//               RC_GET_BIT2(prob3, mi, ; , rep0 |= i);
   4.633 +
   4.634 +	orb	%dh, (BP, DI)	// update rep0 with DirectBits
   4.635 +
   4.636 +//               i <<= 1;
   4.637 +//             } while(--numDirectBits != 0);
   4.638 +//           }
   4.639 +//         } else rep0 = posSlot;
   4.640 +lzd22:
   4.641 +//         if (++rep0 == (UInt32)(0)) break; /* EOF */
   4.642 +
   4.643 +# ifdef ONLY8086
   4.644 +	incw	(BP, DI)
   4.645 +	jnz	lzd19
   4.646 +	incw	2(BP, DI)
   4.647 +# else
   4.648 +	incl	(BP, DI)
   4.649 +# endif
   4.650 +
   4.651 +lzd19:
   4.652 +	pop	CX
   4.653 +	jz	lzdone
   4.654 +
   4.655 +//       }
   4.656 +//       len += kMatchMinLen;/*2*/
   4.657 +
   4.658 +	inc	CX
   4.659 +
   4.660 +//     string: // if (rep0 > nowPos) return LZMA_RESULT_DATA_ERROR;
   4.661 +//       do {
   4.662 +lzd13z:
   4.663 +//         previousByte = outStream[nowPos - rep0];
   4.664 +//         outStream[nowPos++] = previousByte;
   4.665 +
   4.666 +	call	outcharDico 	// %bl = outStream[nowPos++] = outStream[nowPos - rep0]
   4.667 +
   4.668 +//       } while(--len != 0);
   4.669 +
   4.670 +	loop	lzd13z
   4.671 +
   4.672 +//     } /* char/string */
   4.673 +//   }
   4.674 +
   4.675 +	jmp	lzdmainlp
   4.676 +
   4.677 +lzdone:
   4.678 +//   //RC_NORMALIZE;
   4.679 +//   //*inSizeProcessed = (SizeT)(Buffer - inStream); *outSizeProcessed = nowPos;
   4.680 +//   return LZMA_RESULT_OK;
   4.681 +	call	Dico2ESDI	// set es & di (rep0 = 0)
   4.682 +	lea	ws2(BP), SP	// dealloc
   4.683 +	ret	
   4.684 +// }
   4.685 +
   4.686 +// al = outStream[nowPos - rep0];
   4.687 +
   4.688 +/*
   4.689 + * output  es:di, al
   4.690 + * scratch bh, cl, flags
   4.691 + */
   4.692 +
   4.693 +DicoRep02ESDI:
   4.694 +	stc
   4.695 +
   4.696 +// bl = outStream[nowPos];
   4.697 +
   4.698 +/*
   4.699 + * output  es:di, bl
   4.700 + * scratch bh, cl, flags
   4.701 + */
   4.702 + 
   4.703 +Dico2ESDI:
   4.704 +#if !defined(FLAT32) && !defined(FLAT16OUT)
   4.705 +# ifdef ONLY8086
   4.706 +	pushw	%ax
   4.707 +	movw	nowPos(%bp), %bx
   4.708 +	movw	nowPos+2(%bp), %ax
   4.709 +	jnc	Dico2ESDIz
   4.710 +	subw	rep0(%bp), %bx
   4.711 +	sbbw	rep0+2(%bp), %ax
   4.712 +Dico2ESDIz:
   4.713 +	movw	$0xF, %di
   4.714 +	andw	%bx, %di
   4.715 +	pushw	%cx
   4.716 +	movb	$4, %cl
   4.717 +	shrw	%cl, %bx
   4.718 +	shlw	%cl, %ax
   4.719 +	popw	%cx
   4.720 +	addb	%al, %bh
   4.721 +	popw	%ax
   4.722 +# else
   4.723 +	movl	nowPos(%bp), %ebx
   4.724 +	jnc	Dico2ESDIz
   4.725 +	subl	rep0(%bp), %ebx
   4.726 +Dico2ESDIz:
   4.727 +	movw	%bx, %di
   4.728 +	xorw	%bx, %bx
   4.729 +	shrl	$4, %ebx
   4.730 +# endif
   4.731 +	movw	%bx, %es
   4.732 +#else
   4.733 +	mov	nowPos(BP), DI
   4.734 +	jnc	Dico2ESDIz
   4.735 +	sub	rep0(BP), DI
   4.736 +Dico2ESDIz:
   4.737 +#endif
   4.738 +#ifdef FLAT32
   4.739 +	movb	(DI), %bl
   4.740 +#else
   4.741 +	movb	%es:(%di), %bl
   4.742 +#endif
   4.743 +	ret
   4.744 +
   4.745 +outcharDico:
   4.746 +
   4.747 +// bl = outStream[nowPos++] = outStream[nowPos - rep0]
   4.748 +
   4.749 +/*
   4.750 + * output  es:di, bl
   4.751 + * update  nowPos
   4.752 + * scratch ax, dx, bh, cl, flags
   4.753 + */
   4.754 +
   4.755 +	call	DicoRep02ESDI	// %bl = outStream[nowPos - rep0]
   4.756 +	xchg	AX, BX
   4.757 +outchar:
   4.758 +
   4.759 +// bl = outStream[nowPos++] = previousByte = al;
   4.760 +
   4.761 +/*
   4.762 + * output  bl
   4.763 + * update  nowPos
   4.764 + * scratch ax, dx, bh, di, cl, flags
   4.765 + */
   4.766 +
   4.767 +	clc
   4.768 +	call	Dico2ESDI
   4.769 +	stosb
   4.770 +	xchg	AX, BX		// previous byte
   4.771 +
   4.772 +//	int posState = (int)((nowPos) & posStateMask);
   4.773 +
   4.774 +#if PROP_PB != 0 && PROP_LP != 0
   4.775 +	addw	$0x0101, posState2(BP)
   4.776 +	andb	$(((1 << PROP_PB) -1)<<8)+((1 << PROP_LP) -1), posState2(BP)
   4.777 +#else
   4.778 +# if PROP_PB != 0
   4.779 +	incb	posState(BP)
   4.780 +	andb	$((1 << PROP_PB) -1), posState(BP)
   4.781 +# endif
   4.782 +# if PROP_LP != 0
   4.783 +	incb	posState2(BP)
   4.784 +	andb	$((1 << PROP_LP) -1), posState2(BP)
   4.785 +# endif
   4.786 +#endif
   4.787 +#ifdef ONLY8086
   4.788 +	incw	nowPos(BP)
   4.789 +	jnz	incnowPosDone
   4.790 +	incw	nowPos+2(BP)
   4.791 +incnowPosDone:
   4.792 +#else
   4.793 +	incl	nowPos(BP)
   4.794 +#endif
   4.795 +	ret
   4.796 +
   4.797 +//  
   4.798 +// #define RC_NORMALIZE if (Range < kTopValue) 
   4.799 +//    { Range <<= 8; Code = (Code << 8) | RC_READ_BYTE; }
   4.800 +
   4.801 +/*
   4.802 + * update  Range, Code, ds:si
   4.803 + * scratch flags
   4.804 + */
   4.805 +
   4.806 +RC_NORMALIZE:
   4.807 +	cmpb	$0, Range+3(BP)
   4.808 +	jne	RC_NORMALIZE_1
   4.809 +RC_LOAD_BYTE:
   4.810 +	push	AX
   4.811 +#ifdef ONLY8086
   4.812 +	movw	Range+1(BP), %ax
   4.813 +	movw	%ax, Range+2(BP)
   4.814 +	movw	Code+1(BP), %ax
   4.815 +	movw	%ax, Code+2(BP)
   4.816 +	xorw	%ax, %ax
   4.817 +	movb	Range(BP), %ah
   4.818 +	movw	%ax, Range(BP)
   4.819 +	movb	Code(BP), %ah
   4.820 +	movw	%ax, Code(BP)
   4.821 +#else
   4.822 +	shll	$8, Range(BP)
   4.823 +	shll	$8, Code(BP)
   4.824 +#endif
   4.825 +#if !defined(FLAT16) && !defined(FLAT32)
   4.826 +	testw	%si, %si
   4.827 +	jns	RC_READ_BYTE
   4.828 +	movw	%ds, %ax
   4.829 +	incw	%ax
   4.830 +	movw	%ax, %ds
   4.831 +	addw	$-16, %si
   4.832 +RC_READ_BYTE:
   4.833 +#endif
   4.834 +	lodsb
   4.835 +	movb	%al, Code(BP)
   4.836 +	pop	AX
   4.837 +RC_NORMALIZE_1:
   4.838 +	ret
   4.839 +
   4.840 +// Bit1(dx + (state << kNumPosBitsMax /*4*/) + posState)
   4.841 +
   4.842 +Bit1state:
   4.843 +	movb	$16, %al
   4.844 +	mulb	state(BP)
   4.845 +# if PROP_PB != 0
   4.846 +	addb	posState(BP), %al
   4.847 +# endif
   4.848 +Bit1axdx:
   4.849 +	add	DX, AX
   4.850 +	jmp	Bit1
   4.851 +
   4.852 +// prob += 12; Bit1(prob)
   4.853 +
   4.854 +Bit1dx12:
   4.855 +	add	$12, DX
   4.856 +Bit1dx:
   4.857 +	mov	DX, AX
   4.858 +
   4.859 +// static int Bit1(CProb *p)
   4.860 +
   4.861 +Bit1:
   4.862 +/*
   4.863 + * input   ax=p
   4.864 + * output  C, ax
   4.865 + * update  bound, Range, Code, ds:si
   4.866 + * scratch flags
   4.867 + */
   4.868 + 
   4.869 +// {
   4.870 +// 	RC_NORMALIZE;
   4.871 +
   4.872 +	call  RC_NORMALIZE		// kill %ax, update %si
   4.873 +
   4.874 +#ifdef ONLY8086
   4.875 +	pushw	%ax
   4.876 +	pushw	%cx
   4.877 +	pushw	%dx
   4.878 +	pushw	%di
   4.879 +#else
   4.880 +	pushal
   4.881 +#endif
   4.882 +
   4.883 +	xchg	AX, DI
   4.884 +	add	DI, DI			// short *
   4.885 +	
   4.886 +
   4.887 +// 	bound = (Range>>kNumBitModelTotalBits /*11*/) * *(p);
   4.888 +
   4.889 +#ifdef ONLY8086
   4.890 +	movw	Range(BP), %dx
   4.891 +	movw	Range+2(BP), %ax
   4.892 +	movw	$11, %cx
   4.893 +shr11lp:
   4.894 +	shrw	$1, %ax
   4.895 +	rcrw	$1, %dx
   4.896 +	loop	shr11lp
   4.897 +	movw	%dx, %cx
   4.898 +	mulw	(BP, DI)
   4.899 +	xchgw	%ax, %cx
   4.900 +	mulw	(BP, DI)
   4.901 +	addw	%cx, %dx
   4.902 +#else
   4.903 +	movl	Range(BP), %eax
   4.904 +	shrl	$11, %eax
   4.905 +	movzwl	(BP, DI), %edx
   4.906 +	mull	%edx
   4.907 +#endif
   4.908 +
   4.909 +// 	if (Code < bound) {
   4.910 +
   4.911 +#ifdef ONLY8086
   4.912 +	cmpw	Code+2(BP), %dx
   4.913 +	jb	Bit1_1
   4.914 +	ja	Bit1_1x
   4.915 +	cmpw	Code(BP), %ax
   4.916 +	jbe	Bit1_1
   4.917 +Bit1_1x:
   4.918 +
   4.919 +//    		Range = bound;
   4.920 +
   4.921 +	movw	%ax, Range(BP)
   4.922 +	movw	%dx, Range+2(BP)
   4.923 +#else
   4.924 +	cmpl	Code(BP), %eax
   4.925 +	jbe	Bit1_1
   4.926 +
   4.927 +//    		Range = bound;
   4.928 +
   4.929 +	movl	%eax, Range(BP)
   4.930 +#endif
   4.931 +
   4.932 +// 		*(p) += (kBitModelTotal /*2048*/ - *(p)) >> kNumMoveBits /*5*/;
   4.933 +
   4.934 +	movw	$2048, %ax
   4.935 +
   4.936 +// 		return 0;
   4.937 +
   4.938 +	jmp	Bit1_2
   4.939 +
   4.940 +//	}
   4.941 +// 	else {
   4.942 +
   4.943 +Bit1_1:
   4.944 +
   4.945 +//    		Range -= bound; Code -= bound;
   4.946 +
   4.947 +#ifdef ONLY8086
   4.948 +	subw	%ax, Range(BP)
   4.949 +	sbbw	%dx, Range+2(BP)
   4.950 +	subw	%ax, Code(BP)
   4.951 +	sbbw	%dx, Code+2(BP)
   4.952 +#else
   4.953 +	subl	%eax, Range(BP)
   4.954 +	subl	%eax, Code(BP)
   4.955 +#endif
   4.956 +
   4.957 +// 		*(p) -= (*(p)) >> kNumMoveBits /*5*/;
   4.958 +
   4.959 +	movw	$31, %ax
   4.960 +
   4.961 +// 		return 1;
   4.962 +
   4.963 +	stc
   4.964 +Bit1_2:
   4.965 +	pushf
   4.966 +	subw	(BP, DI), %ax
   4.967 +#ifdef ONLY8086
   4.968 +	movb	$5, %cl
   4.969 +	sarw	%cl, %ax
   4.970 +#else
   4.971 +	sarw	$5, %ax
   4.972 +#endif
   4.973 +	addw	%ax, (BP, DI)
   4.974 +	popf
   4.975 +#ifdef ONLY8086
   4.976 +	popw	%di
   4.977 +	popw	%dx
   4.978 +	popw	%cx
   4.979 +	popw	%ax
   4.980 +#else
   4.981 +	popal
   4.982 +#endif
   4.983 +	sbb	AX, AX
   4.984 +
   4.985 +// 	}
   4.986 +// }
   4.987 +
   4.988 +	ret
   4.989 +
   4.990 +RangeDecoder:
   4.991 +
   4.992 +/*
   4.993 + * input   ax=probs cx=numLevels (< 8) bx=1
   4.994 + * output  ax=res (backward), dh (forward)
   4.995 + * update  bound, Range, Code, ds:si
   4.996 + * scratch flags, cx=0, dl
   4.997 + */
   4.998 + 
   4.999 +	push	BX
  4.1000 +	
  4.1001 +//   { int i = numLevels; res = 1; 
  4.1002 +	mov	BX, DX		// res = 1
  4.1003 +	
  4.1004 +//   do { CProb *p = probs + res; RC_GET_BIT(p, res) } while(--i != 0); 
  4.1005 +
  4.1006 +RangeDecoder_1:
  4.1007 +	push	AX
  4.1008 +	call	Bit1axdx		// C,%ax = Bit1(prob+%ax)
  4.1009 +	rclb	$1, %dl			// res <<= 1; res |= C
  4.1010 +	andb	%bl, %al		// current bit
  4.1011 +	orb	%al, %bh		// store in bh
  4.1012 +	shlb	$1, %bl			// update max
  4.1013 +	pop	AX
  4.1014 +	loop	RangeDecoder_1
  4.1015 +
  4.1016 +//   res -= (1 << numLevels); }
  4.1017 +
  4.1018 +	xchg	AX, BX			// move bh to dh
  4.1019 +	xchg	AX, DX			// and dl to al
  4.1020 +	sub	%dl, %al		// sub max
  4.1021 +	pop	BX
  4.1022 +	ret
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/plop/stuff/unpack.S	Wed Aug 19 13:03:10 2020 +0000
     5.3 @@ -0,0 +1,364 @@
     5.4 +#define SYSTEM		0x10000
     5.5 +#define SETUP		14
     5.6 +#define SYSSIZE		0x1F4
     5.7 +#define SETUPSIZE	0x1F1
     5.8 +
     5.9 +#define KEEP_BOOTSECTOR		1
    5.10 +#define LINUX_HEADER		0
    5.11 +#define UPDATE_SYSSIZE		1
    5.12 +#define HARDCODED_SYSSIZE	1
    5.13 +#define HARDCODED_SETUPSIZE	1
    5.14 +#define FLAT16			1
    5.15 +#define FLAT16OUT		1
    5.16 +#define SAVEREGS		0 
    5.17 +#define ELKS			0
    5.18 +
    5.19 +	.text
    5.20 +	.code16
    5.21 +	.org	0
    5.22 +
    5.23 +#if KEEP_BOOTSECTOR
    5.24 +#undef UPDATE_SYSSIZE
    5.25 +#define UPDATE_SYSSIZE		0
    5.26 +#endif
    5.27 +#if defined(FLAT16OUT) && FLAT16OUT == 0
    5.28 +#undef FLAT16OUT
    5.29 +#endif
    5.30 +#if ELKS
    5.31 +	.arch	i8086
    5.32 +#define FLAT16			1
    5.33 +#define LINUX_HEADER		0
    5.34 +#define SAVEREGS		0 
    5.35 +#define TOPSEG		0x2000
    5.36 +#else
    5.37 +#define TOPSEG		0x8000
    5.38 +#endif
    5.39 +
    5.40 +	.globl	_start
    5.41 +_start:
    5.42 +#if LINUX_HEADER
    5.43 +	jmp	start2
    5.44 +	.ascii	"HdrS"		// 202 magic
    5.45 +	.word	0x200		// 206 version 2.00
    5.46 +	.long	0		// 208 realmode_swtch
    5.47 +	.word	SYSTEM/16	// 20C start_sys_seg
    5.48 +	.word	0		// 20E kernel_version
    5.49 +	.byte	0		// 210 type_of_loader
    5.50 +	.byte	0		// 211 loadflags
    5.51 +	.word	0		// 212 setup_move_size
    5.52 +	.long	SYSTEM		// 214 code32_start
    5.53 +	.long	0		// 218 ramdisk_image
    5.54 +	.long	0		// 21C ramdisk_size
    5.55 +	.long	0		// 220 bootsect_kludge
    5.56 +start2:
    5.57 +#endif
    5.58 +#if SAVEREGS
    5.59 +	pushfw
    5.60 +	pushw	%cs
    5.61 +	pushw	$0
    5.62 +	pushal
    5.63 +	pushw	%ds
    5.64 +	cld
    5.65 +#else
    5.66 +	pushw	%cs
    5.67 +# if FLAT16
    5.68 +	xorw	%di, %di
    5.69 +	pushw	%di
    5.70 +# else
    5.71 +	pushw	$0
    5.72 +# endif
    5.73 +#endif
    5.74 +#if ELKS
    5.75 +	movw	$0x100, %ax
    5.76 +	pushw	%ax
    5.77 +#else
    5.78 +# if UPDATE_SYSSIZE || KEEP_BOOTSECTOR
    5.79 +	pushw	%ds			// <A>
    5.80 +# else
    5.81 +	pushw	%cs			// <A>
    5.82 +# endif
    5.83 +#endif
    5.84 +#if FLAT16
    5.85 +# 1- move 9000/9020..9020+stp -> TOPSEG
    5.86 +#if ELKS
    5.87 +	movb	$TOPSEG/256, %ah
    5.88 +	movw	%ax, %es
    5.89 +#else
    5.90 +	pushw	$TOPSEG
    5.91 +	popw	%es
    5.92 +#endif
    5.93 +#if KEEP_BOOTSECTOR
    5.94 +	xorw	%si, %si
    5.95 +#else
    5.96 +	movw	$0x200, %si
    5.97 +#endif
    5.98 +#if SAVEREGS == 0
    5.99 +	pushw	%es
   5.100 +#if ELKS
   5.101 +# if KEEP_BOOTSECTOR
   5.102 +	movw	$cont+0x200, %ax
   5.103 +# else
   5.104 +	movw	$cont, %ax
   5.105 +# endif
   5.106 +	pushw	%ax
   5.107 +#else
   5.108 +# if KEEP_BOOTSECTOR
   5.109 +	pushw	$cont+0x200
   5.110 +# else
   5.111 +	// pushw	$cont
   5.112 +	.byte	0x6A, cont
   5.113 +# endif
   5.114 +#endif
   5.115 +	jmp	jumpinto
   5.116 +#else
   5.117 +# if HARDCODED_SETUPSIZE
   5.118 +setup_word:
   5.119 +#  if KEEP_BOOTSECTOR
   5.120 +#  else
   5.121 +	movw	$SETUP*256, %cx
   5.122 +#  endif
   5.123 +# else
   5.124 +#  if KEEP_BOOTSECTOR
   5.125 +	movw	$0x100, %cx
   5.126 +	addb	SETUPSIZE, %ch
   5.127 +#  else
   5.128 +	xorw	%cx, %cx
   5.129 +	movb	SETUPSIZE, %ch
   5.130 +#  endif
   5.131 +	movw	%cx, %dx
   5.132 +# endif
   5.133 +	pushw	%cx			// <B>
   5.134 +	pushw	%es
   5.135 +	pushw	%di
   5.136 +	rep
   5.137 +	  movsw
   5.138 +	lret
   5.139 +#endif
   5.140 +cont:
   5.141 +# 2- move 1000..1000+sys -> TOPSEG+stp
   5.142 +#if ELKS
   5.143 +	movw	$0x1000, %ax
   5.144 +	movw	%ax, %ds
   5.145 +#else
   5.146 +	pushw	$0x1000
   5.147 +	popw	%ds
   5.148 +#endif
   5.149 +	xorw	%si, %si
   5.150 +	subw	%di, %cx
   5.151 +	rep
   5.152 +	  movsb
   5.153 +# 3- unlz(TOPSEG+stp:end, 1000-stp:0)
   5.154 +	pushw	%es		// <C>
   5.155 +# if HARDCODED_SETUPSIZE
   5.156 +setup_seg:
   5.157 +#if ELKS
   5.158 +#  if KEEP_BOOTSECTOR
   5.159 +#   if UPDATE_SYSSIZE
   5.160 +	movw	$0x1000-(SETUP*32)-32-32, %ax
   5.161 +#   else
   5.162 +	movw	$0x1000-(SETUP*32)-32, %ax
   5.163 +#   endif
   5.164 +#  else
   5.165 +#   if UPDATE_SYSSIZE
   5.166 +	movw	$0x1000-(SETUP*32)-32, %ax
   5.167 +#   else
   5.168 +	movw	$0x1000-(SETUP*32), %ax
   5.169 +#   endif
   5.170 +#  endif
   5.171 +	movw	%ax, %es
   5.172 +#else
   5.173 +#  if KEEP_BOOTSECTOR
   5.174 +#   if UPDATE_SYSSIZE
   5.175 +	pushw	$0x1000-(SETUP*32)-32-32
   5.176 +#   else
   5.177 +	pushw	$0x1000-(SETUP*32)-32
   5.178 +#   endif
   5.179 +#  else
   5.180 +#   if UPDATE_SYSSIZE
   5.181 +	pushw	$0x1000-(SETUP*32)-32
   5.182 +#   else
   5.183 +	pushw	$0x1000-(SETUP*32)
   5.184 +#   endif
   5.185 +#  endif
   5.186 +	popw	%es
   5.187 +#endif
   5.188 +# else
   5.189 +#  if UPDATE_SYSSIZE
   5.190 +	incb	%dh
   5.191 +#  endif
   5.192 +#if ELKS
   5.193 +	shrw	$1, %dx
   5.194 +	shrw	$1, %dx
   5.195 +	shrw	$1, %dx
   5.196 +#else
   5.197 +	shrw	$3, %dx
   5.198 +#endif
   5.199 +	movw	%ds, %ax
   5.200 +	subw	%dx, %ax
   5.201 +	movw	%ax, %es
   5.202 +# endif
   5.203 +#else		// FLAT16
   5.204 +# 1- move 1000..1000+sys -> 9000-sys
   5.205 +# if HARDCODED_SYSSIZE
   5.206 +packed_syssize:
   5.207 +	movw	$0x1000+0, %ax
   5.208 +# else
   5.209 +	movw	SYSSIZE, %ax
   5.210 +	addw	$0x1000, %ax
   5.211 +# endif
   5.212 +	movw	%ds, %dx
   5.213 +mvsys:
   5.214 +	decw	%ax
   5.215 +	decw	%dx
   5.216 +	movw	%ax, %ds
   5.217 +	movw	%dx, %es
   5.218 +	xorw	%si, %si
   5.219 +	xorw	%di, %di
   5.220 +	movw	$8, %cx
   5.221 +	rep
   5.222 +	  movsw
   5.223 +	cmpw	$0x1000, %ax
   5.224 +	jne	mvsys
   5.225 +# 2- move 9000/9020..9020+stp -> 9000-sys-stp
   5.226 +	
   5.227 +# if HARDCODED_SETUPSIZE
   5.228 +#  if KEEP_BOOTSECTOR
   5.229 +setup_byte:
   5.230 +	movb	$SETUP+1, %ch
   5.231 +setup_para:
   5.232 +	subw	$SETUP*32+32, %dx
   5.233 +#  else
   5.234 +setup_byte:
   5.235 +	movb	$SETUP, %ch
   5.236 +setup_para:
   5.237 +	subw	$SETUP*32, %dx
   5.238 +#  endif
   5.239 +# else
   5.240 +	movb	%ss:SETUPSIZE, %ch
   5.241 +#  if KEEP_BOOTSECTOR
   5.242 +	incb	$ch
   5.243 +#  endif
   5.244 +	movw	%cx, %ax
   5.245 +#if ELKS
   5.246 +	shrw	$1, %ax
   5.247 +	shrw	$1, %ax
   5.248 +	shrw	$1, %ax
   5.249 +#else
   5.250 +	shrw	$3, %ax
   5.251 +#endif
   5.252 +	subw	%ax, %dx
   5.253 +# endif
   5.254 +	pushw	%cx			// <B>
   5.255 +	movw	%dx, %es
   5.256 +	xorw	%si, %si
   5.257 +	xorw	%di, %di
   5.258 +	rep
   5.259 +	  movsw	%cs:(%si),%es:(%di)
   5.260 +	pushw	%es
   5.261 +# 3- reloc itself in 0x7C00
   5.262 +	pushw	$0x07C0
   5.263 +	popw	%es
   5.264 +	movw	$cont, %si
   5.265 +	movw	%si, %di
   5.266 +	movw	$end-_start, %cx
   5.267 +	pushw	%es
   5.268 +	pushw	%di
   5.269 +	rep
   5.270 +	  movsb	%cs:(%si),%es:(%di)
   5.271 +	lret
   5.272 +cont:
   5.273 +# 4- unlz(9000-sys-stp:end, 1000-stp:0)
   5.274 +# if HARDCODED_SETUPSIZE
   5.275 +setup_seg:
   5.276 +#  if KEEP_BOOTSECTOR
   5.277 +#   if UPDATE_SYSSIZE
   5.278 +	pushw	$0x1000-(SETUP*32)-32-32
   5.279 +#   else
   5.280 +	pushw	$0x1000-(SETUP*32)-32
   5.281 +#   endif
   5.282 +#  else
   5.283 +#   if UPDATE_SYSSIZE
   5.284 +	pushw	$0x1000-(SETUP*32)-32
   5.285 +#   else
   5.286 +	pushw	$0x1000-(SETUP*32)
   5.287 +#   endif
   5.288 +#  endif
   5.289 +	popw	%es
   5.290 +# else
   5.291 +	negw	%ax
   5.292 +#  if UPDATE_SYSSIZE
   5.293 +	addw	$0x1000-32, %ax
   5.294 +#  else
   5.295 +	addw	$0x1000, %ax
   5.296 +#  endif
   5.297 +	movw	%ax, %es
   5.298 +# endif
   5.299 +#endif		// FLAT16
   5.300 +	popw	%ds			// <C>
   5.301 +#if KEEP_BOOTSECTOR
   5.302 +	movw	$0x200+end-_start, %si
   5.303 +#else
   5.304 +	movw	$end-_start, %si
   5.305 +#endif
   5.306 +#if UPDATE_SYSSIZE
   5.307 +	movw	$SYSSIZE, %di
   5.308 +#else
   5.309 +	xorw	%di, %di
   5.310 +#endif
   5.311 +	pushw	%di
   5.312 +	pushw	%es
   5.313 +	call	unpack
   5.314 +	popw	%ds
   5.315 +	popw	%si
   5.316 +#   if KEEP_BOOTSECTOR
   5.317 +setup_keep:
   5.318 +	xorw	%di, %di
   5.319 +#   else
   5.320 +	movw	%si, %di
   5.321 +#   endif
   5.322 +#   if SAVEREGS != 0
   5.323 +	popw	%cx			// <B>
   5.324 +#if UPDATE_SYSSIZE
   5.325 +// not need with memtest	movb	$(512-SYSSIZE)/2, %cl
   5.326 +#endif
   5.327 +	popw	%es			// <A>
   5.328 +# 5- move 1000-stp..1000 -> 9020
   5.329 +#   else
   5.330 +	popw	%es			// <A>
   5.331 +jumpinto:
   5.332 +# if HARDCODED_SETUPSIZE
   5.333 +setup_word:
   5.334 +#  if KEEP_BOOTSECTOR
   5.335 +	movw	$SETUP*256+256, %cx
   5.336 +#  else
   5.337 +	movw	$SETUP*256, %cx
   5.338 +#  endif
   5.339 +# else
   5.340 +#  if KEEP_BOOTSECTOR
   5.341 +	movw	$0x100, %cx
   5.342 +	addb	%ss:SETUPSIZE, %ch
   5.343 +#  else
   5.344 +	xorw	%cx, %cx
   5.345 +	movb	%ss:SETUPSIZE, %ch
   5.346 +#  endif
   5.347 +	movw	%cx, %dx
   5.348 +# endif
   5.349 +#  endif
   5.350 +	rep
   5.351 +	  movsw
   5.352 +#if SAVEREGS
   5.353 +	popw	%ds
   5.354 +	popal
   5.355 +	iret
   5.356 +#else
   5.357 +	lret
   5.358 +#endif
   5.359 +
   5.360 +unpack:
   5.361 +#define NO_LZMA_HEADER
   5.362 +#if ELKS
   5.363 +#define ONLY8086	1
   5.364 +#endif
   5.365 +#include "unlzma.S"
   5.366 +
   5.367 +end: