#include "stdafx.h"
#include "common.h"

#define END_OF_STREAM  256
#define BUMP_CODE  257
#define FLUSH_CODE  258
#define FIRST_CODE  259

//int _dict;
/*unsigned char _decode_stack;
int _next_code;
unsigned char _current_code_bits;
int _new_code;
int _old_code;
unsigned char _character;
int _RAMPtr;
unsigned char _RAMRack;
unsigned char _RAMMask;
int _numdecode;
*/
int temp;



SLONG asmexpand(void *buf, SLONG amount)
{
	int rv;
	__asm
	{	
		pushad
			mov	ecx,amount
			mov	edi,buf
			mov	esi,RAMPtr
			mov	dl,RAMRack
			mov	dh,RAMMask
			mov	temp,edi
outer:
		mov	ebx,numdecode
			or	ebx,ebx
			jz	inner
outer2:
		dec	ebx
			mov	al,decode_stack[ebx]
			stosb
			dec	ecx
			jz	done
			or	ebx,ebx
			jnz	outer2
inner:
		call	readbit
			mov	new_code,eax
			cmp	eax,END_OF_STREAM
			je	done
			cmp	eax,FLUSH_CODE
			je	doflush
			cmp	eax,BUMP_CODE
			je	dobump
			xor	ebp,ebp
			cmp	eax,next_code
			jb	nofix
			mov	al,byte ptr character
			mov   byte ptr decode_stack,al
			inc	ebp
			mov	eax,old_code
nofix:
		cmp	eax,255
			jbe	doneit
again:
		lea	ebx,[eax*8]
			add	ebx,dict
			mov	eax,[ebx+4]
			mov	bl,[ebx]
			mov	decode_stack[ebp],bl
			inc	ebp
			cmp	eax,255
			ja	again
doneit:
		mov	decode_stack[ebp],al
			mov	byte ptr character,al
			inc	ebp
			mov	numdecode,ebp
			mov	ebx,next_code
			shl	ebx,3
			add	ebx,dict
			mov	[ebx],al
			mov	eax,old_code
			mov	[ebx+4],eax
			inc	next_code
			mov	eax,new_code
			mov	old_code,eax
			jmp	outer
			
readbit:	; mask in dh, rack in dl, preserves ecx
			mov	bl,cl
			mov	cl,byte ptr current_code_bits
			xor	eax,eax
			mov	ebp,80000000h
			rol	ebp,cl
			mov	cl,bl
getloop:    cmp	dh,80h
			jne	noget
			mov	dl,[esi]
			inc	esi
noget:	test	dl,dh
		jz	noor
		or	eax,ebp
noor:		ror	dh,1
			shr	ebp,1
			jnz	getloop
			ret
			jmp theend
			
			
done:
		mov	numdecode,ebx
			sub	temp,edi
			mov	RAMPtr,esi
			mov	RAMRack,dl
			mov	RAMMask,dh
			popad
			mov	eax,temp
			neg	eax
			jmp theend
dobump:
		inc	current_code_bits
			jmp	inner
doflush:
		mov	next_code,FIRST_CODE
			mov	current_code_bits,9
			call	readbit
			mov	old_code,eax
			mov	byte ptr character,al
			mov	decode_stack,al
			mov	numdecode,1
			jmp	outer
theend:
		mov rv,eax
		}
		;
		
		
		return rv;
		
}

///////////////////////////////////////// fx2.asm

;// asm1.asm



/*
unsigned char _cliptab128[256];

int  _texptr;
int  _fadeptr;
int  _ghostptr;



int  _screenbuf;
int  _reciptab;
int  _multab;

unsigned char _zmaptab1;
unsigned char _zmaptab2;
unsigned char _zmaptab3;

int _cval;

unsigned char _linecol;


int _miny;
int _maxy;
*/

#define xsize 320


extern int val1	;
extern int ival1	;
extern int yval1	;
extern int iebp	;
extern int yebp	;
extern unsigned char val2	;
extern unsigned char ival2	;
extern unsigned char val3	;
extern unsigned char ival3	;
extern unsigned char yval2	;
extern unsigned char yval3	;
extern unsigned char pad1,pad2;
unsigned char scratch[32];
extern int pintexptr;

//int _pin;







void drawpinmap(void *srctex, void *dest)
{
	__asm
	{
		mov eax,srctex
		mov edx,dest
		
		;bl:edx :edx contains u and lo v, bl contains hi v
			;val2:val1 contains u/v incs
			;ch:esi contains colour
			;val3:ebp contains colour inc
			;ival1,ival2,ival3,iebp are delta deltas
			
			pushad
			mov	esi,offset pin
			mov edi,edx
			mov pintexptr,eax
			
			mov ch,25
pinyloop:
		mov cl,40
pinxloop:
		//assume esi:ptr pin
			push esi
			push ecx
			mov eax,[esi+12/*sizeof pin*/+0/*u*/]
			sub eax,[esi+0/*u*/]
			mov edx,[esi+(12/*sizeof pin*/)*42+0/*u*/]
			sub edx,[esi+(12/*sizeof pin*/)*41+0/*u*/]
			sub edx,eax
			sar eax,3
			sar edx,6
			mov val1,eax
			sar eax,31
			mov val2,al			
			mov ival1,edx
			sar edx,31
			mov ival2,dl
			mov eax,[esi+(12/*sizeof pin*/)*41+0/*u*/]
			sub eax,[esi+0/*u*/]
			sar eax,3			
			mov yval1,eax			
			sar eax,31
			mov yval2,al			
			
			mov eax,[esi+12/*sizeof pin*/+4/*u*/]
			sub eax,[esi+4/*u*/]
			mov edx,[esi+(12/*sizeof pin*/)*42+4/*u*/]
			sub edx,[esi+(12/*sizeof pin*/)*41+4/*u*/]
			sub edx,eax
			sar eax,3
			sar edx,6			
			add val2,ah
			shl eax,24
			add val1,eax
			adc val2,0
			add ival2,dh
			shl edx,24
			add ival1,edx
			adc ival2,0
			mov eax,[esi+(12/*sizeof pin*/)*41+4/*u*/]
			sub eax,[esi+4/*u*/]
			sar eax,3			
			add yval2,ah
			shl eax,24
			add yval1,eax
			adc yval2,0
			
			mov eax,[esi+12/*sizeof pin*/+8/*col*/]
			sub eax,[esi+8/*col*/]
			mov edx,[esi+(12/*sizeof pin*/)*42+8/*col*/]
			sub edx,[esi+(12/*sizeof pin*/)*41+8/*col*/]
			sub edx,eax
			sar eax,3
			sar edx,6			
			mov val3,ah
			shl eax,24
			mov ebp,eax						
			mov ival3,dh
			shl edx,24
			mov iebp,edx
			mov eax,[esi+(12/*sizeof pin*/)*41+8/*col*/]
			sub eax,[esi+8/*col*/]
			sar eax,3			
			mov byte ptr yval3,ah
			shl eax,24
			mov yebp,eax
			
			mov ebx,pintexptr
			mov ecx,fadeptr
			mov dl,byte ptr ([esi+4/*u*/])
			shl edx,24
			add edx,800000h
			mov eax,[esi+0/*u*/]
			and eax,65535			
			add edx,eax
			mov bl,byte ptr ([esi+4/*u*/])+1
			mov ch,byte ptr ([esi+8/*col*/])+1
			mov esi,[esi+8/*col*/]
			shl esi,24
			mov scratch,8
			
			;bl:edx :edx contains u and lo v, bl contains hi v
			;val2:val1 contains u/v incs
			;ch:esi contains colour
			;val3:ebp contains colour inc
			;ival1,ival2,ival3,iebp are delta deltas
			
			;mov ch,0
			;mov esi,0
			;mov ebp,0
			;mov val3,0
			;mov iebp,0
			;mov ival3,0
			;mov yebp,0
			;mov yval3,0
			
again:
		
		
		mov scratch[4],bl
			mov scratch[8],ch
			mov dword ptr scratch[12],edx
			mov dword ptr scratch[16],esi
			
			mov bh,dh         ; v
            add edx,val1      ; u
			mov cl,[ebx]      ; v
            mov bh,val2       ; u
            mov al,[ecx]      ; v
            adc bl,bh		;  u
            add esi,ebp		; v
            mov cl,val3		; u
            mov bh,dh		; v
            adc ch,cl		;  u
			
            mov ah,val2       ; v
			add edx,val1      ; u
			mov cl,[ebx]      ; v
			adc bl,ah		;  u
            mov ah,[ecx]      ; v
            mov cl,val3		; u
            add esi,ebp		; v
            adc ch,cl		;  u
            rol eax,16		; v
			
            mov bh,dh
            mov al,val2       ; v
            add edx,val1      ; u
			mov cl,[ebx]      ; v
            adc bl,al		;  u
            mov al,[ecx]      ; v
            mov cl,val3		; u
            add esi,ebp		; v
            adc ch,cl		;  u
			
            mov bh,dh         ; v
            add edx,val1      ; u
			mov cl,[ebx]      ; v
            mov bh,val2       ; u
            mov ah,[ecx]      ; v
            adc bl,bh		;  u
            add esi,ebp		; v
            mov cl,val3		; u
            mov bh,dh		; v
            adc ch,cl		;  u
            rol eax,16		; v
            mov [edi],eax	; u
			
            ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
		
		mov bh,dh         ; v
            add edx,val1      ; u
			mov cl,[ebx]      ; v
            mov bh,val2       ; u
            mov al,[ecx]      ; v
            adc bl,bh		;  u
			add esi,ebp		; v
            mov cl,val3		; u
            mov bh,dh		; v
            adc ch,cl		;  u
			
            mov ah,val2       ; v
            add edx,val1      ; u
			mov cl,[ebx]      ; v
            adc bl,ah		;  u
            mov ah,[ecx]      ; v
            mov cl,val3		; u
            add esi,ebp		; v
            adc ch,cl		;  u
            rol eax,16		; v
			
            mov bh,dh
            mov al,val2       ; v
            add edx,val1      ; u
			mov cl,[ebx]      ; v
            adc bl,al		;  u
            mov al,[ecx]      ; v
            mov cl,val3		; u
            add esi,ebp		; v
            adc ch,cl		;  u
			
            mov bh,dh         ; v
            add edx,val1      ; u
			mov cl,[ebx]      ; v
            mov bh,val2       ; u
            mov ah,[ecx]      ; v
            adc bl,bh		;  u
            add esi,ebp		; v
            mov cl,val3		; u
            mov bh,scratch	; v
            adc ch,cl		;  u
            rol eax,16		; v
            mov [edi+4],eax	; u
			
			mov bl,scratch[4]
			mov ch,scratch[8]
			mov edx,dword ptr scratch[12]
			mov esi,dword ptr scratch[16]
			
			mov eax,yval1
			mov cl,yval2		
			add edx,eax			
			adc bl,cl
			mov eax,yebp
			mov cl,byte ptr yval3
			add esi,eax
			adc ch,cl
			
			mov eax,iebp	; u
            add ebp,eax		; v
            mov al,ival3      ; u			
            adc val3,al       ; u
			add edi,xsize	  ; v
            mov eax,ival1     ; v
            add val1,eax      ; u
            mov al,ival2      ; v
            adc val2,al       ; u
			
            dec bh		; v
            mov scratch,bh      ; u
            jnz again         ; v
			
			
			pop ecx
			pop esi
			add esi,12/*sizeof pin*/
			sub	edi,xsize*8-8
			dec cl
			jnz pinxloop
			add esi,12/*sizeof pin*/
			add edi,xsize*8-320
			dec ch
			jnz pinyloop
			//assume esi:nothing
			
			popad		
			
			
	}
}


extern "C" unsigned char mytab[];

void rotocopy(unsigned char *dest, unsigned char *src1, unsigned char *src2)
{
	__asm
	{
		mov eax,dest
		mov edx,src1
		mov ebx,src2
		
		pushad
			mov edi,eax
			mov esi,edx
			mov ebp,ebx
			xor ebx,ebx
			mov ecx,64000/4
again:
		mov bl,[esi]
			mov dl,[ebp]
			add bl,[esi+1]
			rcr bl,1
			shr dl,1
			add bl,dl
			rcr bl,1
			mov al,byte ptr mytab[ebx]
			mov bl,[esi+1]
			mov dl,[ebp+1]
			add bl,[esi+2]
			rcr bl,1
			shr dl,1
			add bl,dl
			rcr bl,1
			mov ah,byte ptr mytab[ebx]
			rol eax,16
			mov bl,[esi+2]
			mov dl,[ebp+2]
			add bl,[esi+3]
			rcr bl,1
			shr dl,1
			add bl,dl
			rcr bl,1
			mov al,byte ptr mytab[ebx]
			mov bl,[esi+3]
			mov dl,[ebp+3]
			add bl,[esi+4]
			rcr bl,1
			shr dl,1
			add bl,dl
			rcr bl,1
			mov ah,byte ptr mytab[ebx]
			rol eax,16
			mov [edi],eax
			add edi,4
			add esi,4
			add ebp,4
			dec ecx
			jnz again
			popad
			
	}
}



void copy320to256(void *dest, void *src)
{
	__asm
	{
		mov eax,dest
		mov edx,src
		
		pushad
			mov edi,eax
			mov esi,edx
			mov ecx,64000/20
again:
		mov eax,[esi]
			mov ebx,[esi+5]
			mov edx,[esi+10]
			mov ebp,[esi+15]
			mov [edi],eax
			mov [edi+4],ebx
			mov [edi+8],edx
			mov [edi+12],ebp
			add esi,20
			add edi,16
			dec ecx
			jnz again
			popad
			
	}
}

void xfade(void*dest,void *src,int _di, int _si)
{
	
	__asm
	{
		mov eax,dest
		mov edx,src
		mov ebx,_di
		mov ecx,_si

		pushad
			mov edi,eax
			mov esi,edx
			mov eax,ebx
			mov ebx,multab
			mov edx,ebx
			mov bh,cl
			mov dh,al
			mov ecx,640*480/4
again:
		mov bl,[esi+2]
			mov dl,[edi+2]
			mov al,[ebx]
			add al,[edx]
			mov bl,[esi+3]
			mov dl,[edi+3]
			mov ah,[ebx]
			add ah,[edx]
			shl eax,16
			mov bl,[esi]
			mov dl,[edi]
			mov al,[ebx]
			add al,[edx]
			mov bl,[esi+1]
			mov dl,[edi+1]
			mov ah,[ebx]
			add ah,[edx]
			xor eax,80808080h
			mov [edi],eax
			add esi,4
			add edi,4
			dec ecx
			jnz again
			popad
			
	}
}



void ghosto(void *dest, void *src)
{
	__asm
	{
		
		
		mov eax,dest
		mov edx,src

		pushad
			mov edi,eax
			mov esi,edx
			mov ecx,64000/4
			xor ebx,ebx
			xor edx,edx
again:
		mov eax,[edi]
			shr eax,2
			and eax,1f1f1f1fh
			add eax,[esi]
			
			//repeat 2
			mov bl,al
			mov dl,ah
			//externdef _cliptab128:byte
			mov al,cliptab128[ebx]
			mov ah,cliptab128[edx]
			rol eax,16
			//endm
			mov bl,al
			mov dl,ah
			//externdef _cliptab128:byte
			mov al,cliptab128[ebx]
			mov ah,cliptab128[edx]
			rol eax,16
			//endm

			mov [edi],eax
			add edi,4
			add esi,4
			dec ecx
			jnz again
			popad
			
			
			
	}
	
}


void beep()
{
	MessageBeep(-1);
}