; /-----------------------------------------------------------------------/
; file:		uue.s
; function:	Encode / decode a single line
; opcode:		
; part of:	Infitra
;
; comment:	Experimental
; author:		E Kuik
; /-----------------------------------------------------------------------/

; Decoding can be done in one buffer. The binairy result
; will alway be smaller then the encoded data.
;
; d5 -> binairy filesize counter
; a0 -> Source
; a1 -> Dest
Line_decode:
; get the line length
	moveq	#0,d3
	move.b	(a0)+,d3
	sub.b	#$20,d3		; length of binairy line
	beq	Lready
	bmi	Lready
	add.l	d3,d5		; increase data size
Decode:	moveq	#3,d2		; get 4 encoded byte
.Decode:	move.b	(a0)+,d1
	beq	.Decode		; null???
	bmi	.Decode		; illigal char, should be replaced
				; with table lookup sequence
	cmp.b	#$20,d1
	blt	.Decode		; illigal char!
	rol.l	#8,d0
	move.b	d1,d0
	dbra	d2,.Decode
	sub.l	#$20202020,d0	; remove mask
	moveq	#0,d1
; byte 1
	rol.l	#8,d0
	move.b	d0,d1		; 6 bit byte 1
	lsl.l	#6,d1
; byte 1 & 2
	rol.l	#8,d0
	or.b	d0,d1		; 2 bit byte 1 + 4 bit byte 2
	lsl.l	#6,d1
; byte 2 & 3
	rol.l	#8,d0
	or.b	d0,d1		; 4 bit byte 2 + 2 bit byte 3
	lsl.l	#6,d1
; byte 3
	rol.l	#8,d0
	or.b	d0,d1		; 6 bit byte 3
	rol.l	#8,d1
.rept 3
	rol.l	#8,d1
	move.b	d1,(a1)+
.endm
	subq.l	#3,d3
	beq	Lready		; this line is ready
	bpl	Decode
Lready:	rts


; encode x byte to one single line
; d3 holds binairy bytes to encode
; a0 -> Source pointer
; a1 -> dest pointer

uuelwidth	= 45
; to produce UUD.TTP compatible encoding

Line_encode:
	move.l	d3,d2
	add.l	#$20,d2
	move.b	d2,(a1)+
; encode in groups of 3 byte to 4 encoded bytes
Encode:	moveq	#0,d0		; dest reg
	moveq	#0,d1		; temp reg
; encoded byte 1
	move.b	(a0),d0
	rol.l	#6,d0		; use upper 6 bit
	move.b	#0,d0
; encoded byte 2
	move.b	(a0)+,d1
	and.b	#3,d1		; use lower 2 bit
	lsl.b	#4,d1		; 4 bit to left
	or.b	d1,d0
	move.b	(a0),d1		; use upper 4 bit
	rol.b	#4,d1
	and.b	#$0f,d1
	or.b	d1,d0
; encoded byte 3
	rol.l	#8,d0		; create space
	move.b	(a0)+,d1
	and.b	#$0f,d1
	lsl.b	#2,d1
	or.b	d1,d0
	move.b	(a0),d1
	rol.b	#2,d1
	and.b	#3,d1
	or.b	d1,d0
; encoded byte 4
	rol.l	#8,d0		; create space
	move.b	(a0)+,d0
	and.b	#$3f,d0
	add.l	#$20202020,d0
; and store in destination buffer
.rept 4
	rol.l	#8,d0
	move.b	d0,(a1)+
.endm
	sub.w	#3,d3
	beq	lineend
	bpl	Encode
lineend:	lea	uuelend,a2
	move.b	(a2),(a1)+
	sub.b	#1,(a2)
	cmp.b	#"a",(a2)
	bge	endchar
	move.b	#"z",(a2)
endchar:	move.b	#13,(a1)+
	move.b	#10,(a1)+
	move.b	#0,(a1)
	rts

.data
uuelend:	dc.b	"z",0
