	org 512
__START_PROGRAM:
	di
	push ix
	push iy
	exx
	push hl
	exx
	ld hl, 0
	add hl, sp
	ld (__CALL_BACK__), hl
	ei
	jp __LABEL__end01
__LABEL__charmap01:
#line 2
		defb %00011010,%00001010,%00111000,%00101000
		defb %00111011,%00001000,%00101100,%00100000
		defb %00011010,%00001000,%00100100,%00100000
		defb %00111010,%00001010,%00101100,%00100000
		defb %00111111,%00111111,%00111111,%00111111
		defb %01111111,%01111111,%01111111,%01111111
		defb %10111111,%10111111,%10111111,%10111111
		defb %11111111,%11111111,%11111111,%11111111
		defb %00100110,%00100110,%00011001,%00011001
		defb %01100110,%01100110,%01011001,%01011001
		defb %10100110,%10100110,%10011001,%10011001
		defb %11100110,%11100110,%11011001,%11011001
		defb %00100010,%00001000,%00001000,%00100010
		defb %01100010,%01001000,%01001000,%01100010
		defb %10100010,%10001000,%10001000,%10100010
		defb %11100010,%11001000,%11001000,%11100010
		defb %00011101,%00110111,%00110111,%00011101
		defb %01011101,%01110111,%01110111,%01011101
		defb %10011101,%10110111,%10110111,%10011101
		defb %11011101,%11110111,%11110111,%11011101
#line 22
__LABEL__end01:
	ld a, 98
	call _mc1000screen
	ld a, 2
	push af
	call _mc1000cls
	ld hl, 50
	ld (_rseed), hl
	push hl
	call _mc1000rnd
	call __FTOU32REG
	push de
	push hl
	ld de, 0
	ld hl, 255
	call __BAND32
	ld (_rseed), hl
	ld de, 3
	call __MUL16_FAST
	ld de, 0
	ld (_p1), hl
	ld hl, (_rseed)
	push hl
	call _mc1000rnd
	call __FTOU32REG
	push de
	push hl
	ld de, 0
	ld hl, 255
	call __BAND32
	ld (_rseed), hl
	ld de, 8
	call __DIVU16
	ld de, 0
	push de
	push hl
	ld de, 0
	ld hl, 8
	pop bc
	add hl, bc
	ex de, hl
	pop bc
	adc hl, bc
	ex de, hl
	ld (_s1), hl
	ld hl, (_rseed)
	push hl
	call _mc1000rnd
	call __FTOU32REG
	push de
	push hl
	ld de, 0
	ld hl, 255
	call __BAND32
	ld (_rseed), hl
	ld de, 3
	call __MUL16_FAST
	ld de, 0
	ld (_p2), hl
	ld hl, (_rseed)
	push hl
	call _mc1000rnd
	call __FTOU32REG
	push de
	push hl
	ld de, 0
	ld hl, 255
	call __BAND32
	ld (_rseed), hl
	ld de, 8
	call __DIVU16
	ld de, 0
	push de
	push hl
	ld de, 0
	ld hl, 8
	pop bc
	add hl, bc
	ex de, hl
	pop bc
	adc hl, bc
	ex de, hl
	ld (_s2), hl
__LABEL__lp01:
	ld hl, (_rseed)
	push hl
	call _mc1000rnd
	call __FTOU32REG
	ld (_rseed), hl
	ex de, hl
	ld hl, 0
	push de
	push hl
	ld de, 1
	ld hl, 39321
	call __DIVF16
	ex de, hl
	ld a, h
	add a, a
	sbc a, a
	ld e, a
	ld d, a
	ld bc, 0
	push bc
	ld bc, 128
	push bc
	pop bc
	add hl, bc
	ex de, hl
	pop bc
	adc hl, bc
	push hl
	push de
	ld de, 0
	ld hl, 255
	call __BAND32
	ld (_vl), hl
	ld hl, 0
	push hl
	ld hl, __LABEL__charmap01
	push hl
	ld hl, (_vl)
	ld de, 20
	call __MODI16
	push hl
	ld hl, (_p1)
	push hl
	call _mc1000vpokechr4x6mode96
	ld hl, (_p1)
	inc hl
	ld h, 0
	ld a, l
	and 127
	ld l, a
	ld (_p1), hl
	ld hl, (_s1)
	dec hl
	ld (_s1), hl
	ld de, 1
	ld hl, (_s1)
	call __LTI16
	or a
	jp z, __LABEL1
	ld hl, (_rseed)
	push hl
	call _mc1000rnd
	call __FTOU32REG
	push de
	push hl
	ld de, 0
	ld hl, 255
	call __BAND32
	ld (_rseed), hl
	ld de, 3
	call __MUL16_FAST
	ld de, 0
	ld (_p1), hl
	ld hl, (_rseed)
	push hl
	call _mc1000rnd
	call __FTOU32REG
	push de
	push hl
	ld de, 0
	ld hl, 255
	call __BAND32
	ld (_rseed), hl
	ld de, 8
	call __DIVU16
	ld de, 0
	push de
	push hl
	ld de, 0
	ld hl, 8
	pop bc
	add hl, bc
	ex de, hl
	pop bc
	adc hl, bc
	ex de, hl
	ld (_s1), hl
__LABEL1:
	ld hl, 0
	push hl
	ld hl, __LABEL__charmap01
	push hl
	ld hl, 5
	push hl
	ld hl, (_p2)
	push hl
	call _mc1000vpokechr4x6mode96
	ld hl, (_p2)
	inc hl
	ld h, 0
	ld a, l
	and 127
	ld l, a
	ld (_p2), hl
	ld hl, (_s2)
	dec hl
	ld (_s2), hl
	ld de, 1
	ld hl, (_s2)
	call __LTI16
	or a
	jp z, __LABEL3
	ld hl, (_rseed)
	push hl
	call _mc1000rnd
	call __FTOU32REG
	push de
	push hl
	ld de, 0
	ld hl, 255
	call __BAND32
	ld (_rseed), hl
	ld de, 3
	call __MUL16_FAST
	ld de, 0
	ld (_p2), hl
	ld hl, (_rseed)
	push hl
	call _mc1000rnd
	call __FTOU32REG
	push de
	push hl
	ld de, 0
	ld hl, 255
	call __BAND32
	ld (_rseed), hl
	ld de, 8
	call __DIVU16
	ld de, 0
	push de
	push hl
	ld de, 0
	ld hl, 8
	pop bc
	add hl, bc
	ex de, hl
	pop bc
	adc hl, bc
	ex de, hl
	ld (_s2), hl
__LABEL3:
	ld hl, 100
	call _mc1000delayb
	jp __LABEL__lp01
	ld hl, 0
	ld b, h
	ld c, l
__END_PROGRAM:
	di
	ld hl, (__CALL_BACK__)
	ld sp, hl
	exx
	pop hl
	exx
	pop iy
	pop ix
	ei
	ret
__CALL_BACK__:
	DEFW 0
_mc1000screen:
#line 1
		out ($80),a
		ld ($00F5),a
#line 3
_mc1000screen__leave:
	ret
_mc1000cls:
	push ix
	ld ix, 0
	add ix, sp
	ld hl, 0
	push hl
	push hl
	push hl
	inc sp
	ld (ix-3), 0
	ld (ix-2), 2
	ld (ix-1), 0
	ld a, (245)
	push af
	ld h, 248
	pop af
	and h
	sub 128
	sub 1
	jp nc, __LABEL5
	ld (ix-3), 0
	ld (ix-2), 4
__LABEL5:
	ld a, (245)
	push af
	ld h, 252
	pop af
	and h
	sub 136
	sub 1
	jp nc, __LABEL7
	ld (ix-3), 0
	ld (ix-2), 8
__LABEL7:
	ld a, (245)
	push af
	ld h, 252
	pop af
	and h
	sub 140
	sub 1
	jp nc, __LABEL9
	ld (ix-3), 0
	ld (ix-2), 6
__LABEL9:
	ld a, (245)
	push af
	ld h, 248
	pop af
	and h
	sub 144
	sub 1
	jp nc, __LABEL11
	ld (ix-3), 0
	ld (ix-2), 12
__LABEL11:
	ld a, (245)
	push af
	ld h, 248
	pop af
	and h
	sub 152
	sub 1
	jp nc, __LABEL13
	ld (ix-3), 0
	ld (ix-2), 24
__LABEL13:
	ld a, (245)
	push af
	ld h, 132
	pop af
	and h
	sub 128
	sub 1
	jp nc, __LABEL15
	ld a, (ix+5)
	push af
	ld h, 3
	pop af
	and h
	ld h, 85
	call __MUL8_FAST
	ld (ix-1), a
__LABEL15:
	ld a, (245)
	push af
	ld h, 132
	pop af
	and h
	sub 132
	sub 1
	jp nc, __LABEL17
	ld a, (ix+5)
	push af
	ld h, 1
	pop af
	and h
	ld h, 255
	call __MUL8_FAST
	ld (ix-1), a
__LABEL17:
	ld a, (245)
	push af
	ld h, 96
	pop af
	and h
	sub 96
	sub 1
	jp nc, __LABEL19
	ld (ix-1), 0
	ld a, (ix+5)
	push af
	ld h, 7
	pop af
	and h
	push af
	ld h, 4
	pop af
	cp h
	jp nc, __LABEL21
	ld a, (ix+5)
	push af
	ld h, 3
	pop af
	and h
	ld b, 6
__LABEL38:
	add a, a
	djnz __LABEL38
	ld h, a
	ld a, 63
	or h
	ld (ix-1), a
__LABEL21:
__LABEL19:
	ld a, (245)
	push af
	ld h, 96
	pop af
	and h
	sub 64
	sub 1
	jp nc, __LABEL23
	ld (ix-1), 0
	ld a, (ix+5)
	push af
	ld h, 15
	pop af
	and h
	push af
	ld h, 8
	pop af
	cp h
	jp nc, __LABEL25
	ld a, (ix+5)
	push af
	ld h, 7
	pop af
	and h
	add a, a
	add a, a
	add a, a
	add a, a
	ld h, a
	ld a, 15
	or h
	ld (ix-1), a
__LABEL25:
__LABEL23:
	ld a, (245)
	push af
	ld h, 252
	pop af
	and h
	sub 1
	jp nc, __LABEL27
	ld a, (ix+5)
	push af
	ld h, 1
	pop af
	and h
	ld b, 7
__LABEL39:
	add a, a
	djnz __LABEL39
	ld h, a
	ld a, 32
	or h
	ld (ix-1), a
__LABEL27:
	ld (ix-5), 0
	ld (ix-4), 0
	jp __LABEL28
__LABEL31:
	ld l, (ix-5)
	ld h, (ix-4)
	ld de, 32768
	add hl, de
	push hl
	ld a, (ix-1)
	pop hl
	ld (hl), a
__LABEL32:
	ld l, (ix-5)
	ld h, (ix-4)
	inc hl
	ld (ix-5), l
	ld (ix-4), h
__LABEL28:
	ld l, (ix-5)
	ld h, (ix-4)
	push hl
	ld l, (ix-3)
	ld h, (ix-2)
	dec hl
	pop de
	or a
	sbc hl, de
	jp nc, __LABEL31
__LABEL30:
_mc1000cls__leave:
	ld sp, ix
	pop ix
	exx
	pop hl
	ex (sp), hl
	exx
	ret
_mc1000delayb:
#line 1
		ld b,h
		ld c,l
		call $C30E
#line 4
_mc1000delayb__leave:
	ret
_mc1000vpokechr4x6mode96:
	push ix
	ld ix, 0
	add ix, sp
	ld hl, 0
	push hl
	inc sp
	ld l, (ix+4)
	ld h, (ix+5)
	ld a, h
	and 3
	ld h, a
	ld (ix+4), l
	ld (ix+5), h
	ld (ix-1), 0
	jp __LABEL33
__LABEL36:
	ld a, (ix-1)
	ld l, a
	ld h, 0
	push hl
	ld l, (ix+10)
	ld h, (ix+11)
	ex de, hl
	pop hl
	add hl, de
	ld de, 32
	call __MUL16_FAST
	ld de, 32768
	add hl, de
	push hl
	ld l, (ix+4)
	ld h, (ix+5)
	ld h, 0
	ld a, l
	and 15
	ld l, a
	add hl, hl
	ex de, hl
	pop hl
	add hl, de
	ld de, 0
	push de
	push hl
	ld l, (ix+4)
	ld h, (ix+5)
	ld de, 16
	call __DIVU16
	ld de, 0
	push de
	push hl
	ld de, 0
	ld hl, 64
	call __MUL32
	pop bc
	add hl, bc
	ex de, hl
	pop bc
	adc hl, bc
	ex de, hl
	push hl
	ld l, (ix+8)
	ld h, (ix+9)
	push hl
	ld l, (ix+6)
	ld h, (ix+7)
	add hl, hl
	add hl, hl
	ex de, hl
	pop hl
	add hl, de
	push hl
	ld a, (ix-1)
	add a, a
	ld l, a
	ld h, 0
	ex de, hl
	pop hl
	add hl, de
	ld b, h
	ld c, l
	ld a, (bc)
	pop hl
	ld (hl), a
	ld a, (ix-1)
	ld l, a
	ld h, 0
	push hl
	ld l, (ix+10)
	ld h, (ix+11)
	ex de, hl
	pop hl
	add hl, de
	ld de, 32
	call __MUL16_FAST
	ld de, 32769
	add hl, de
	push hl
	ld l, (ix+4)
	ld h, (ix+5)
	ld h, 0
	ld a, l
	and 15
	ld l, a
	add hl, hl
	ex de, hl
	pop hl
	add hl, de
	ld de, 0
	push de
	push hl
	ld l, (ix+4)
	ld h, (ix+5)
	ld de, 16
	call __DIVU16
	ld de, 0
	push de
	push hl
	ld de, 0
	ld hl, 64
	call __MUL32
	pop bc
	add hl, bc
	ex de, hl
	pop bc
	adc hl, bc
	ex de, hl
	push hl
	ld l, (ix+8)
	ld h, (ix+9)
	push hl
	ld l, (ix+6)
	ld h, (ix+7)
	add hl, hl
	add hl, hl
	ex de, hl
	pop hl
	add hl, de
	push hl
	ld a, (ix-1)
	add a, a
	ld l, a
	ld h, 0
	ex de, hl
	pop hl
	add hl, de
	inc hl
	ld b, h
	ld c, l
	ld a, (bc)
	pop hl
	ld (hl), a
__LABEL37:
	ld a, (ix-1)
	inc a
	ld (ix-1), a
__LABEL33:
	ld a, (ix-1)
	push af
	ld a, 1
	pop hl
	cp h
	jp nc, __LABEL36
__LABEL35:
_mc1000vpokechr4x6mode96__leave:
	ld sp, ix
	pop ix
	exx
	pop hl
	pop bc
	pop bc
	pop bc
	ex (sp), hl
	exx
	ret
_mc1000rnd:
	push ix
	ld ix, 0
	add ix, sp
	ld l, (ix+4)
	ld h, (ix+5)
	push hl
	ld de, 255
	pop hl
	call __BAND16
	ld de, 221
	call __MUL16_FAST
	ld de, 53
	add hl, de
	push hl
	ld de, 255
	pop hl
	call __BAND16
	ld de, 0
	call __U32TOFREG
_mc1000rnd__leave:
	ld sp, ix
	pop ix
	exx
	pop hl
	ex (sp), hl
	exx
	ret
#line 1 "mul16.asm"
__MUL16:	; Mutiplies HL with the last value stored into de stack
				; Works for both signed and unsigned
	
			PROC
	
			LOCAL __MUL16LOOP
	        LOCAL __MUL16NOADD
			
			ex de, hl
			pop hl		; Return address
			ex (sp), hl ; CALLEE caller convention
	
;;__MUL16_FAST:	; __FASTCALL ENTRY: HL = 1st operand, DE = 2nd Operand
	;;		ld c, h
	;;		ld a, l	 ; C,A => 1st Operand
	;;
	;;		ld hl, 0 ; Accumulator
	;;		ld b, 16
	;;
;;__MUL16LOOP:
	;;		sra c	; C,A >> 1  (Arithmetic)
	;;		rra
	;;
	;;		jr nc, __MUL16NOADD
	;;		add hl, de
	;;
;;__MUL16NOADD:
	;;		sla e
	;;		rl d
	;;			
	;;		djnz __MUL16LOOP
	
__MUL16_FAST:
	        ld b, 16
	        ld a, d
	        ld c, e
	        ex de, hl
	        ld hl, 0
	
__MUL16LOOP:
	        add hl, hl  ; hl << 1
	        sla c
	        rla         ; a,c << 1
	        jp nc, __MUL16NOADD
	        add hl, de
	
__MUL16NOADD:
	        djnz __MUL16LOOP
	
			ret	; Result in hl (16 lower bits)
	
			ENDP
	
#line 736 "chrdemo_mc1000_01.bas"
#line 1 "mul32.asm"
#line 1 "_mul32.asm"
	
; Ripped from: http://www.andreadrian.de/oldcpu/z80_number_cruncher.html#moztocid784223
	; Used with permission.
	; Multiplies 32x32 bit integer (DEHL x D'E'H'L')
	; 64bit result is returned in H'L'H L B'C'A C
	
	
__MUL32_64START:
			push hl
			exx
			ld b, h
			ld c, l		; BC = Low Part (A)
			pop hl		; HL = Load Part (B)
			ex de, hl	; DE = Low Part (B), HL = HightPart(A) (must be in B'C')
			push hl
	
			exx
			pop bc		; B'C' = HightPart(A)
			exx			; A = B'C'BC , B = D'E'DE
	
				; multiply routine 32 * 32bit = 64bit
				; h'l'hlb'c'ac = b'c'bc * d'e'de
				; needs register a, changes flags
				;
				; this routine was with tiny differences in the
				; sinclair zx81 rom for the mantissa multiply
	
__LMUL:
	        and     a               ; reset carry flag
	        sbc     hl,hl           ; result bits 32..47 = 0
	        exx
	        sbc     hl,hl           ; result bits 48..63 = 0
	        exx
	        ld      a,b             ; mpr is b'c'ac
	        ld      b,33            ; initialize loop counter
	        jp      __LMULSTART  
	
__LMULLOOP:
	        jr      nc,__LMULNOADD  ; JP is 2 cycles faster than JR. Since it's inside a LOOP
	                                ; it can save up to 33 * 2 = 66 cycles
	                                ; But JR if 3 cycles faster if JUMP not taken!
	        add     hl,de           ; result += mpd
	        exx
	        adc     hl,de
	        exx
	
__LMULNOADD:
	        exx
	        rr      h               ; right shift upper
	        rr      l               ; 32bit of result
	        exx
	        rr      h
	        rr      l
	
__LMULSTART:
	        exx
	        rr      b               ; right shift mpr/
	        rr      c               ; lower 32bit of result
	        exx
	        rra                     ; equivalent to rr a
	        rr      c
	        djnz    __LMULLOOP
	
			ret						; result in h'l'hlb'c'ac
	       
#line 2 "mul32.asm"
	
__MUL32:	; multiplies 32 bit un/signed integer.
				; First operand stored in DEHL, and 2nd onto stack
				; Lowest part of 2nd operand on top of the stack
				; returns the result in DE.HL
			exx
			pop hl	; Return ADDRESS
			pop de	; Low part
			ex (sp), hl ; CALLEE -> HL = High part
			ex de, hl
			call __MUL32_64START
	
__TO32BIT:  ; Converts H'L'HLB'C'AC to DEHL (Discards H'L'HL)
			exx
			push bc
			exx
			pop de
			ld h, a
			ld l, c
			ret
	
	
#line 737 "chrdemo_mc1000_01.bas"
#line 1 "divf16.asm"
#line 1 "div32.asm"
#line 1 "neg32.asm"
__ABS32:
		bit 7, d
		ret z
	
__NEG32: ; Negates DEHL (Two's complement)
		ld a, l
		cpl
		ld l, a
	
		ld a, h
		cpl
		ld h, a
	
		ld a, e
		cpl
		ld e, a
		
		ld a, d
		cpl
		ld d, a
	
		inc l
		ret nz
	
		inc h
		ret nz
	
		inc de
		ret
	
#line 2 "div32.asm"
	
				 ; ---------------------------------------------------------
__DIVU32:    ; 32 bit unsigned division
	             ; DEHL = Dividend, Stack Top = Divisor
	             ; OPERANDS P = Dividend, Q = Divisor => OPERATION => P / Q
				 ;
				 ; Changes A, BC DE HL B'C' D'E' H'L'
				 ; ---------------------------------------------------------
	        exx
	        pop hl   ; return address
	        pop de   ; low part
	        ex (sp), hl ; CALLEE Convention ; H'L'D'E' => Dividend
	
__DIVU32START: ; Performs D'E'H'L' / HLDE
	        ; Now switch to DIVIDEND = B'C'BC / DIVISOR = D'E'DE (A / B)
	        push de ; push Lowpart(Q)
			ex de, hl	; DE = HL
	        ld hl, 0
	        exx
	        ld b, h
	        ld c, l
	        pop hl
	        push de
	        ex de, hl
	        ld hl, 0        ; H'L'HL = 0
	        exx
	        pop bc          ; Pop HightPart(B) => B = B'C'BC
	        exx
	
	        ld a, 32 ; Loop count
	
__DIV32LOOP:
	        sll c  ; B'C'BC << 1 ; Output most left bit to carry
	        rl  b
	        exx
	        rl c
	        rl b
	        exx
	
	        adc hl, hl
	        exx
	        adc hl, hl
	        exx
	
	        sbc hl,de
	        exx
	        sbc hl,de
	        exx
	        jp nc, __DIV32NOADD	; use JP inside a loop for being faster
	
	        add hl, de
	        exx
	        adc hl, de
	        exx
	        dec bc
	
__DIV32NOADD:
	        dec a
	        jp nz, __DIV32LOOP	; use JP inside a loop for being faster
	        ; At this point, quotient is stored in B'C'BC and the reminder in H'L'HL
	
	        push hl
	        exx
	        pop de
	        ex de, hl ; D'E'H'L' = 32 bits modulus
	        push bc
	        exx
	        pop de    ; DE = B'C'
	        ld h, b
	        ld l, c   ; DEHL = quotient D'E'H'L' = Modulus
	
	        ret     ; DEHL = quotient, D'E'H'L' = Modulus
	
	
	
__MODU32:    ; 32 bit modulus for 32bit unsigned division
	             ; DEHL = Dividend, Stack Top = Divisor (DE, HL)
	
	        exx
	        pop hl   ; return address
	        pop de   ; low part
	        ex (sp), hl ; CALLEE Convention ; H'L'D'E' => Dividend
	
	        call __DIVU32START	; At return, modulus is at D'E'H'L'
	
__MODU32START:
	
			exx
			push de
			push hl
	
			exx 
			pop hl
			pop de
	
			ret
	
	
__DIVI32:    ; 32 bit signed division
	             ; DEHL = Dividend, Stack Top = Divisor
	             ; A = Dividend, B = Divisor => A / B
	        exx
	        pop hl   ; return address
	        pop de   ; low part
	        ex (sp), hl ; CALLEE Convention ; H'L'D'E' => Dividend
	
__DIVI32START:
			exx
			ld a, d	 ; Save sign
			ex af, af'
			bit 7, d ; Negative?
			call nz, __NEG32 ; Negates DEHL
	
			exx		; Now works with H'L'D'E'
			ex af, af'
			xor h
			ex af, af'  ; Stores sign of the result for later
	
			bit 7, h ; Negative?
			ex de, hl ; HLDE = DEHL
			call nz, __NEG32
			ex de, hl 
	
			call __DIVU32START
			ex af, af' ; Recovers sign
			and 128	   ; positive?
			ret z
	
			jp __NEG32 ; Negates DEHL and returns from there
			
			
__MODI32:	; 32bits signed division modulus
			exx
	        pop hl   ; return address
	        pop de   ; low part
	        ex (sp), hl ; CALLEE Convention ; H'L'D'E' => Dividend
	
			call __DIVI32START
			jp __MODU32START		
	
#line 2 "divf16.asm"
	
	
__DIVF16:	; 16.16 Fixed point Division (signed)
		
	            ; DE.HL = Dividend, Stack Top = Divisor
	            ; A = Dividend, B = Divisor => A / B
	                exx
	                pop hl   ; return address
	                pop de   ; low part
	                ex (sp), hl ; CALLEE Convention ; H'L'D'E' => Dividend
					ex de, hl   ; D'E'.H'L' Dividend
	
__DIVF16START: ; FAST Entry: DEHL => Dividend, D'E'H'L' => Divisor
			ld a, d	 ; Save sign
			ex af, af'
			bit 7, d ; Negative?
			call nz, __NEG32 ; Negates DEHL
	
			exx		; Now works with D'E'.H'L'
			ex af, af'
			xor d
			ex af, af'  ; Stores sign of the result for later
	
			bit 7, d ; Negative?
			call nz, __NEG32
			exx		 ; Now we have DE.HL => Dividend
	
			ld b, 16
	
__SHIFTALOOP:		; Tries to shift Dividend to the left
	        bit 7, d
			jp nz, __SHIFTB
	        add hl, hl
			ex de, hl
	        adc hl, hl
			ex de, hl
	        djnz __SHIFTALOOP
	        jp __DOF16_DIVRDY
	
__SHIFTB:       ; Cannot shift Dividend more to the left, try to shift Divisor to the right
	        ld a, b
	        exx
	        ld b, a
	                ; Divisor is in DEHL
__SHIFTBLOOP:
	        bit 1, l
			jp nz, __DOF16_DIVIDE
	        sra d
	        rr e
	        rr h
	        rr l
	        djnz __SHIFTBLOOP
	
__DOF16_DIVIDE:
	        ld a, b
	        exx
	        ld b, a
	
__DOF16_DIVRDY:
			exx
			ex de, hl
	        push bc
			call __DIVU32START
			pop bc
	
			xor a
			or b
			jp z, __ENDF16DIV
	
__SHIFTCLOOP:
			add hl, hl	; Shift DECIMAL PART << 1
			ex de, hl		
			adc hl, hl  ; Shift INTEGER PART << 1 Plus Carry
			ex de, hl		
			djnz __SHIFTCLOOP
	
__ENDF16DIV: 	   ; Put the sign on the result
			ex af, af' ; Recovers sign
			and 128	   ; positive?
			ret z
			jp __NEG32 ; Negates DEHL and returns from there
	
#line 738 "chrdemo_mc1000_01.bas"
#line 1 "band16.asm"
; vim:ts=4:et:
	; FASTCALL bitwise and16 version.
	; result in hl 
; __FASTCALL__ version (operands: A, H)
	; Performs 16bit or 16bit and returns the boolean
; Input: HL, DE
; Output: HL <- HL AND DE
	
__BAND16:
		ld a, h
		and d
	    ld h, a
	
	    ld a, l
	    and e
	    ld l, a
	
	    ret 
	
#line 739 "chrdemo_mc1000_01.bas"
#line 1 "mul8.asm"
__MUL8:		; Performs 8bit x 8bit multiplication
		PROC
	
		;LOCAL __MUL8A
		LOCAL __MUL8LOOP
		LOCAL __MUL8B
				; 1st operand (byte) in A, 2nd operand into the stack (AF)
		pop hl	; return address
		ex (sp), hl ; CALLE convention
	
;;__MUL8_FAST: ; __FASTCALL__ entry
	;;	ld e, a
	;;	ld d, 0
	;;	ld l, d
	;;	
	;;	sla h	
	;;	jr nc, __MUL8A
	;;	ld l, e
	;;
;;__MUL8A:
	;;
	;;	ld b, 7
;;__MUL8LOOP:
	;;	add hl, hl
	;;	jr nc, __MUL8B
	;;
	;;	add hl, de
	;;
;;__MUL8B:
	;;	djnz __MUL8LOOP
	;;
	;;	ld a, l ; result = A and HL  (Truncate to lower 8 bits)
	
__MUL8_FAST: ; __FASTCALL__ entry, a = a * h (8 bit mul) and Carry
	
	    ld b, 8
	    ld l, a
	    xor a
	
__MUL8LOOP:
	    add a, a ; a *= 2
	    sla l
	    jp nc, __MUL8B
	    add a, h
	
__MUL8B:
	    djnz __MUL8LOOP
		
		ret		; result = HL
		ENDP
	
#line 740 "chrdemo_mc1000_01.bas"
#line 1 "band32.asm"
	; FASTCALL bitwise and 32 version.
	; Performs 32bit and 32bit and returns the bitwise
	; result in DE,HL
	; First operand in DE,HL 2nd operand into the stack
	
__BAND32:
	    ld b, h
	    ld c, l ; BC <- HL
	
	    pop hl  ; Return address
	    ex (sp), hl ; HL <- Lower part of 2nd Operand
	
		ld a, b
	    and h
	    ld b, a
	
	    ld a, c
	    and l
	    ld c, a ; BC <- BC & HL
	
		pop hl  ; Return dddress
	    ex (sp), hl ; HL <- High part of 2nd Operand
	
	    ld a, d
	    and h
	    ld d, a
	
	    ld a, e
	    and l
	    ld e, a ; DE <- DE & HL
	
	    ld h, b
	    ld l, c ; HL <- BC  ; Always return DE,HL pair regs
	
	    ret
	
#line 741 "chrdemo_mc1000_01.bas"
#line 1 "and8.asm"
	; FASTCALL boolean and 8 version.
	; result in Accumulator (0 False, not 0 True)
; __FASTCALL__ version (operands: A, H)
	; Performs 8bit and 8bit and returns the boolean
	
__AND8:
		or a
		ret z
		ld a, h
		ret 
	
#line 742 "chrdemo_mc1000_01.bas"
#line 1 "lti16.asm"
	
#line 1 "lti8.asm"
	
__LTI8: ; Test 8 bit values A < H
        ; Returns result in A: 0 = False, !0 = True
	        sub h
	
__LTI:  ; Signed CMP
	        PROC
	        LOCAL __PE
	
	        ld a, 0  ; Sets default to false
__LTI2:
	        jp pe, __PE
	        ; Overflow flag NOT set
	        ret p
	        dec a ; TRUE
	
__PE:   ; Overflow set
	        ret m
	        dec a ; TRUE
	        ret
	        
	        ENDP
#line 3 "lti16.asm"
	
__LTI16: ; Test 8 bit values HL < DE
        ; Returns result in A: 0 = False, !0 = True
	        xor a
	        sbc hl, de
	        jp __LTI2
	
#line 743 "chrdemo_mc1000_01.bas"
#line 1 "ftou32reg.asm"
	
	
__FTOU32REG:	; Converts a Float to (un)signed 32 bit integer (NOTE: It's ALWAYS 32 bit signed)
					; Input FP number in A EDCB (A exponent, EDCB mantissa)
				; Output: DEHL 32 bit number (signed)
		PROC
	
		LOCAL __IS_FLOAT
	
		or a
		jr nz, __IS_FLOAT 
		; Here if it is a ZX ROM Integer
	
		ld h, c
		ld l, d
	ld a, e	 ; Takes sign: FF = -, 0 = +
		ld de, 0
		inc a
		jp z, __NEG32	; Negates if negative
		ret
	
__IS_FLOAT:  ; Jumps here if it is a true floating point number
		ld h, e	
		push hl  ; Stores it for later (Contains Sign in H)
	
		push de
		push bc
	
		exx
		pop de   ; Loads mantissa into C'B' E'D' 
		pop bc	 ; 
	
		set 7, c ; Highest mantissa bit is always 1
		exx
	
		ld hl, 0 ; DEHL = 0
		ld d, h
		ld e, l
	
		;ld a, c  ; Get exponent
		sub 128  ; Exponent -= 128
		jr z, __FTOU32REG_END	; If it was <= 128, we are done (Integers must be > 128)
		jr c, __FTOU32REG_END	; It was decimal (0.xxx). We are done (return 0)
	
		ld b, a  ; Loop counter = exponent - 128
	
__FTOU32REG_LOOP:
		exx 	 ; Shift C'B' E'D' << 1, output bit stays in Carry
		sla d
		rl e
		rl b
		rl c
	
	    exx		 ; Shift DEHL << 1, inserting the carry on the right
		rl l
		rl h
		rl e
		rl d
	
		djnz __FTOU32REG_LOOP
	
__FTOU32REG_END:
		pop af   ; Take the sign bit
		or a	 ; Sets SGN bit to 1 if negative
		jp m, __NEG32 ; Negates DEHL
		
		ret
	
		ENDP
	
	
__FTOU8:	; Converts float in C ED LH to Unsigned byte in A
		call __FTOU32REG
		ld a, l
		ret
	
#line 744 "chrdemo_mc1000_01.bas"
#line 1 "div16.asm"
	; 16 bit division and modulo functions 
	; for both signed and unsigned values
	
#line 1 "neg16.asm"
	; Negates HL value (16 bit)
__ABS16:
		bit 7, h
		ret z
	
__NEGHL:
		ld a, l			; HL = -HL
		cpl
		ld l, a
		ld a, h
		cpl
		ld h, a
		inc hl
		ret
	
#line 5 "div16.asm"
	
__DIVU16:    ; 16 bit unsigned division
	             ; HL = Dividend, Stack Top = Divisor
	
		;   -- OBSOLETE ; Now uses FASTCALL convention
		;   ex de, hl
	    ;	pop hl      ; Return address
	    ;	ex (sp), hl ; CALLEE Convention
	
__DIVU16_FAST:
	    ld a, h
	    ld c, l
	    ld hl, 0
	    ld b, 16
	
__DIV16LOOP:
	    sll c
	    rla
	    adc hl,hl
	    sbc hl,de
	    jr  nc, __DIV16NOADD
	    add hl,de
	    dec c
	
__DIV16NOADD:
	    djnz __DIV16LOOP
	
	    ex de, hl
	    ld h, a
	    ld l, c
	
	    ret     ; HL = quotient, DE = Mudulus
	
	
	
__MODU16:    ; 16 bit modulus
	             ; HL = Dividend, Stack Top = Divisor
	
	    ;ex de, hl
	    ;pop hl
	    ;ex (sp), hl ; CALLEE Convention
	
	    call __DIVU16_FAST
	    ex de, hl	; hl = reminder (modulus)
					; de = quotient
	
	    ret
	
	
__DIVI16:	; 16 bit signed division
		;	--- The following is OBSOLETE ---
		;	ex de, hl
		;	pop hl
		;	ex (sp), hl 	; CALLEE Convention
	
__DIVI16_FAST:
		ld a, d
		xor h
		ex af, af'		; BIT 7 of a contains result
	
		bit 7, d		; DE is negative?
		jr z, __DIVI16A	
	
		ld a, e			; DE = -DE
		cpl
		ld e, a
		ld a, d
		cpl
		ld d, a
		inc de
	
__DIVI16A:
		bit 7, h		; HL is negative?
		call nz, __NEGHL
	
__DIVI16B:
		call __DIVU16_FAST
		ex af, af'
	
		or a	
		ret p	; return if positive
	    jp __NEGHL
	
		
__MODI16:    ; 16 bit modulus
	             ; HL = Dividend, Stack Top = Divisor
	
	    ;ex de, hl
	    ;pop hl
	    ;ex (sp), hl ; CALLEE Convention
	
	    call __DIVI16_FAST
	    ex de, hl	; hl = reminder (modulus)
					; de = quotient
	
	    ret
	
#line 745 "chrdemo_mc1000_01.bas"
#line 1 "u32tofreg.asm"
	
__I8TOFREG:
		ld l, a
		rlca
		sbc a, a	; A = SGN(A)
		ld h, a
		ld e, a
		ld d, a
	
__I32TOFREG:	; Converts a 32bit signed integer (stored in DEHL)
					; to a Floating Point Number returned in (A ED CB)
	
		ld a, d
		or a		; Test sign
	
		jp p, __U32TOFREG	; It was positive, proceed as 32bit unsigned
	
		call __NEG32		; Convert it to positive
		call __U32TOFREG	; Convert it to Floating point
	
		set 7, e			; Put the sign bit (negative) in the 31bit of mantissa
		ret
	
__U8TOFREG:
					; Converts an unsigned 8 bit (A) to Floating point
		ld l, a
		ld h, 0
		ld e, h
		ld d, h
	
__U32TOFREG:	; Converts an unsigned 32 bit integer (DEHL)
					; to a Floating point number returned in A ED CB
	
	    PROC
	
	    LOCAL __U32TOFREG_END
	
		ld a, d
		or e
		or h
		or l
	    ld b, d
		ld c, e		; Returns 00 0000 0000 if ZERO
		ret z
	
		push de
		push hl
	
		exx
		pop de  ; Loads integer into B'C' D'E' 
		pop bc
		exx
	
		ld l, 128	; Exponent
		ld bc, 0	; DEBC = 0
		ld d, b
		ld e, c
	
__U32TOFREG_LOOP: ; Also an entry point for __F16TOFREG
		exx
		ld a, d 	; B'C'D'E' == 0 ?
		or e
		or b
		or c
		jp z, __U32TOFREG_END	; We are done
	
		srl b ; Shift B'C' D'E' >> 1, output bit stays in Carry
		rr c
		rr d
		rr e
		exx
	
		rr e ; Shift EDCB >> 1, inserting the carry on the left
		rr d
		rr c
		rr b
	
		inc l	; Increment exponent
		jp __U32TOFREG_LOOP
	
	
__U32TOFREG_END:
		exx
	    ld a, l     ; Puts the exponent in a
		res 7, e	; Sets the sign bit to 0 (positive)
	
		ret
	    ENDP
	
#line 746 "chrdemo_mc1000_01.bas"
	
ZXBASIC_USER_DATA:
_p1:
	DEFB 00, 00
_p2:
	DEFB 00, 00
_s1:
	DEFB 00, 00
_s2:
	DEFB 00, 00
_vl:
	DEFB 00, 00
_rseed:
	DEFB 00, 00
	; Defines DATA END --> HEAP size is 0
ZXBASIC_USER_DATA_END EQU ZXBASIC_MEM_HEAP
	; Defines USER DATA Length in bytes
ZXBASIC_USER_DATA_LEN EQU ZXBASIC_USER_DATA_END - ZXBASIC_USER_DATA
	END
