	; (c) 2007 Karel 'Clock' Kulhavy,
	; This program is free software licensed under GPL v2 or later as you
	; want.

	org 65000
	; This is gonna actually start at 23773.
	ld	hl,entry-65000+23773
	ld	de,entry
	ld	bc,end-entry
	ldir
	jp	entry
entry
	ld	sp,27000
	;	Clear the screen with white paper&ink and no bright
	ld	hl,16384+3*8*256
	ld	de,16384+3*8*256+1
	ld	bc,767
	ld	(hl),0x3f
	ldir
	;	The pixel data of Twibright Labs logo
	ld	hl,16384+256*8
	ld	bc,256*8
	call	loader
	;	The attributes of Twibright Labs logo
	ld	hl,16384+3*8*256+32*7
	ld	bc,32*10
	call	loader
	;	Most of the game
	ld	hl,27000
	ld	bc,38000
	call	loader
	;	The remaining 536 bytes
	ld	hl,23296
	ld	bc,536
	call	loader
	;	Setup code trampoline
	ld	hl,trampoline
	ld	de,23832
	ld	bc,endtrampoline-trampoline
	ldir
	;	Setup the copying of the last 536 bytes
	ld	hl,23296
	ld	de,65000
	ld	bc,536
	;	Launch the trampoline
	jp 23832

trampoline:
	ldir
	jp 62262
endtrampoline:

loader:
	; Differential manchester loader.
	; input: HL address
	;        BC count
required	equ 21
	; number of preamble bits required (all zeros, except the last one)

	di

	;	Mic on, white border
	ld	a,0xf
	out	(254),a

	push	bc
	exx
	pop	bc
	exx
	ld	e,64

	call	preamble

	nop			; 4

	; The cycle length is 238T.
	; edge-jp nc, nobyte-newbit-edge must have it (238)
	; edge-jp nz,newbyte-newbit-edge must have it (238)
newbyte:
	ld	b,1		; 4
newbit:
	; Timing for 16000 bps begins
	defb	0x18, 0x00	;12
	defb	0x18, 0x00	;12
	ld	a,r		;9
	;	33T
	; Timing for 16000 bps ends

	; Begin of 11025 bps timing
	; 107 T stuffing
;	ld	a,r
;	ld	a,0
;	ld	a,0
;	ld	a,0
;	ld	a,0
;	ld	a,0
;	ld	a,0
;	ld	a,0
;	ld	a,0
;	ld	a,0
;	ld	a,0
;	ld	a,0
;	ld	a,0
;	ld	a,0
;	ld	a,0		; 107
	; End of 11025 bps timing



	in	a,(254)		; 11
	ld	d,a		; 4
edge:	in	a,(254)		; 11
	xor	d		; 4
	and	e		; 4
	jp	z,edge		; 10
	xor	d		; 4
	xor	c		; 4
	ld	c,d		; 4
	rlca			; 4
	rlca			; 4
	rl	b		; 8
	jp	nc,nobyte	; 10
	ld	(hl),b		; 7
	inc	hl		; 6
	exx			; 4
	dec	bc		; 6
	ld	a,b		; 4
	or	c		; 4
	exx			; 4
	jp	nz,newbyte	; 10

	ei
	ret

nobyte:
	nop
	ld	a,0
	ld	a,0
	ld	a,0
	ld	a,0
	ld	a,0		; 39
	jp	newbit		; 10

preamble
	; b-counter of zero bits
	; c-last value
	; e must be 64 on entry.

	; The cycle must be 238T
	; pedge-jp z,onebit-jp nc,pagain-pbit must be 238T (238)
	; pedge-jp pbit-pedge must be 238T (238)
	; pedge-jp z,onebit-ret-newbyte-newbit-edge must be also 238T (238)

	ld	c,0

pagain:	; Got a one byte which wasn't preceded by enough zero
	; bytes, repeat.
	ld	b,0			; 7
	; Counter of zero bits
pbit:
	; 16000 bps timing begin. Needs 44 ticks instead of 118
	defb	0x18, 0x00		;12
	defb	0x18, 0x00		;12
	defb	0x18, 0x00		;12
	nop				;4
	nop				;4
	; 16000 bps timing end

	; 11025 bps timing beginning
	; Timing stuffing
;	inc	ix
;	dec	ix
;
;	inc	ix
;	dec	ix
;
;	inc	ix
;	dec	ix
;
;	inc	ix
;	dec	ix
;
;	inc	ix
;	dec	ix
;
;	ld	a,r
;	ld	a,r			; 118
	; 11025 bps timing beginning

	in	a,(254)			; 11
	ld	d,a			; 4

pedge:	in	a,(254)			; 11
	xor	d			; 4
	and	e			; 4
	jp	z,pedge			; 10
	;	Here we have an edge

	; increent b but just up to 255
	ld	a,b			; 4
	add	a,1			; 7
	sbc	a,0			; 7
	ld	b,a			; 4

	; xor with last received bit
	ld	a,d			; 4
	xor	c			; 4
	ld	c,d			; 4
	and	e			; 4

	; Now if NZ, we received a zero bit.
	jp	z,onebit		; 10
	ld	a,0			; 7
	ld	a,0			; 7
	nop				; 4
	jp	pbit			; 10
onebit:
	ld	a,required		; 7
	cp	b			; 4
	jp	nc,pagain		; 10
	; 	Here we got a one bit after
	;       the required amount of zeroes.

	ret				; 10
end



	

	; Cycle len 131 T - 20.038 kHz
	; 11.025 kHz needs 238 T

