; Insert...
	bsr setwriter	;after calling jsetcopper.

; Insert...
	bsr writeit	;In the usual position
		;after maincalls.
***************************************************************************
* Set-up routines
***************************************************************************

setwriter:	move #0,wdelay	;No clear screen coming up.
	lea message,a0	;Start of message.
	move.l a0,messagepointer
	move.b #0,curx	;Top left of screen
	move.b #0,cury
	move.l #-2,oldcursor	;No cursors were drawn in the
	move.l #-2,lastcursor	;last two frames.
	move.l #-1,lastadds	;No letter was drawn last frame.
	rts

*****************************************************************************
* Main loop codes
*****************************************************************************

writeit:	tst pausing	;Pretty obviously,
	beq nodelay	;jump if not waiting.
	tst wdelay	;Are we waiting to clear?
	beq flashcursor	;Nope!
	sub #1,wdelay
	cmp #1,wdelay	;Clear screen.
	beq clearscreen
	bra flashcursor	;Exit to cursor routine.

nodelay:	move.b curx,d0	;Cursor coordinates.
	move.b cury,d1
	add.b #1,d0	;Move right.
	cmp.b #41,d0	;Off line?
	blt wisameline	;Nope.

	moveq #1,d0	;Next line.
	add.b #1,d1
	cmp.b #32,d1	;Off page?
	blt wisameline	;Nope.

	move.b #40,curx	;Set to bottom right.
	move.b #31,cury

newscreen:	move #75,wdelay	;1.5 seconds at 50 frames/sec.
	move #75,pausing	;These must be the same.
	bra nonewletter	;Jump to exit routine.

wisameline:	move.b d0,curx	;If everything was ok then
	move.b d1,cury	;back we come to here.

donextletter:	move.l messagepointer,a1 ;Get next ascii code.
	move.b (a1)+,d2
checkcodes:	tst.b d2	;Check for 0 = end of message.
	bne nomeswrap
	lea message,a1
	move.b (a1)+,d2

nomeswrap:	move.l a1,messagepointer ;We`re still in the message.
	cmp.b #32,d2	  ;Ascii 32 is a space.
	bcc realletters	  ;Jump if space or above.

;
; Now we check for control codes.
;

	cmp.b #1,d2	  ;1,x = wait for x frames
	bne nopause
	move.b (a1)+,pausing+1	  ;Byte.
	move.l a1,messagepointer ;Extra code taken.
	bra nonewletter	  ;Exit and draw last letter.

nopause:	cmp.b #12,d2	;12 = clear screen.
	beq newscreen	;Erm....

nocls:	cmp.b #16,d2	;16 = delete.
	bne nodel
	move.b #32,d2	;Space.

	cmp.b #1,curx	;Check not on far left.
	bne ccdelok

	tst.b cury	;Check not at top.
	beq newscreen	;Time to clear the screen.
	bsr realletters	;Otherwise draw space and cursor
	move.b #39,curx	;And reset cursor.
	sub.b #1,cury
	rts

ccdelok:	bsr realletters	;If cursor in sensible place...
	sub.b #2,curx	;plot space then move cursor back
	rts	;(back two because it`ll go
		;forward one next frame.)

nodel:	cmp.b #8,d2	;8 = back one character.
	bne noback
	cmp.b #1,curx	;This is just the same for delete,
	bne ccbackok	;but without plotting the space.

	tst.b cury
	beq newscreen
	bsr nonewletter
	move.b #39,curx
	sub.b #1,cury
	rts

ccbackok:	bsr nonewletter
	sub.b #2,curx
	rts

noback:	cmp.b #9,d2	;9 = forward one character.
	bne nofor
	bra nonewletter	;Cursor has already moved forward
		;so just plot cursor.
nofor:	cmp.b #10,d2	;10 = down one character.
	bne nodown
	bsr nonewletter	;Plot last letter and cursor.
	sub.b #1,curx
	cmp.b #31,cury
	bne ccdownok	;Can`t move off screen.
	rts

ccdownok:	add.b #1,cury	;Move down one line.
	rts	;Cursor at right position.

nodown:	cmp.b #11,d2	;11 = up one character.
	bne noup
	bsr nonewletter	;Plot last letter and cursor.
	sub.b #1,curx
	tst.b cury
	bne ccupok	;Can`t move off screen.
	rts

ccupok:	sub.b #1,cury	;Move up one line.
	rts	;Cursor at right position.

noup:	cmp.b #13,d2	;13 = carriage return.
	bne noreturn
	cmp.b #31,cury
	beq newscreen
	bsr nonewletter	;I'm getting bored....
	move.b #0,curx

ccretok:	add.b #1,cury	;Move down one line.

	rts

noreturn:	cmp.b #31,d2	;31 = move cursor to x,y.
	bne realletters	;(i.e. dc.b 31,x,y).
	bsr nonewletter	;Now what has this done before?
	move.b (a1)+,curx	;x = 0 to 39, y = 0 to 31.
	move.b (a1)+,cury
	move.l a1,messagepointer ;Save new pointer.
	rts

realletters:	move.l drawscreen,a0	;Now we get down to plotting
	ext.w d0	;those letters. d0 is now a word.
	ext.w d1	;d1 is now a word.
	ext.l d0	;d0 is now a longword.
	subq #1,d0
	mulu #40*8,d1	;d1 hold offset due to y.
	add.l d0,d1	;Add offset due to x.
	adda.l d1,a0	;Add offset to base address.

	lea font,a2	;Base address of font.

	sub.b #32,d2	;Calculate offset and find
	ext.w d2	;letter's address.
	ext.l d2
	asl.l #3,d2
	adda.l d2,a2

	move #7,d3	;8 lines to plot.

putlastletter:	cmp.l #-1,lastadds
	beq nolastletter	;Skip plotting the last letter if
	move.l lastadds,a4	;there weren't one. Lastadds holds
	adda.l drawscreen,a4	;the offset of the last letter.
	move.l lastfont,a3
plloop2:	move.b (a3)+,(a4)	;Plot letter.
	adda.l #40,a4
	dbf d3,plloop2

nolastletter:	move #7,d3
	move.l d1,lastadds	;Save this letter's offset
	move.l a2,lastfont	;and the letter's data address.
plotletterloop:	move.b (a2)+,(a0)	;Plot letter.
	adda.l #40,a0
	dbf d3,plotletterloop

movecursor:	move.b #$ff,cursorphase ;Enable cursor.
flashcursor:	cmp.l #-2,oldcursor
	beq nooldcursor	;Jump if no old cursor to erase.
	move.l oldcursor,d1	;Jump if old cursor already erased
	cmp.l lastadds,d1	;by the last letter overwriting it
	beq nooldcursor

	move.b oldcursorphase,d2 ;Erase old cursor.
	bsr putcursor

nooldcursor:	move.l lastcursor,oldcursor           ;Shunt last frame's 
		               ;cursor into next
	move.b lastcursorphase,oldcursorphase ;frame's erase.

	move.b curx,d0
	move.b cury,d1
	tst.w pausing	;Jump if printing.
	beq printing
;
; If the cursor is still,
; then make it flash.
;
	sub #1,pausing	;Decrement count.
	tst cpcount
	bne noflash
	move #5,cpcount
	eor.b #$ff,cursorphase	;Toggle cursor on/off.
	noflash:sub #1,cpcount
printing:	ext.w d0	;Calculate and save offsets for
	ext.w d1	;the screen addresses just like
	ext.l d0	;with the letters.
	subq #1,d0
	mulu #40*8,d1
	add.l d0,d1
	move.l d1,lastcursor

	move.b cursorphase,d2	;Store this too for erasing.
	move.b d2,lastcursorphase

putcursor:	moveq #7,d0	;Add the offset to the base addr,
	move.l drawscreen,a0	;and plot the cursor on the screen
	adda.l d1,a0

putcursorloop:	eor.b d2,(a0)	;Invert the cell.
	adda.l #40,a0
	dbf d0,putcursorloop

writerdone:	tst.b lastwascleared	;Check to see if the screen was
	bne clearsecondscreen	;cleared last frame.
	rts

clearsecondscreen:bsr clearscreen	  ;Clear this screen if the last
	move.b #0,lastwascleared ;one was, but not the next!
	rts

clearscreen:	move.b #0,curx	;Wipe the screen and reset cursor.
	move.b #0,cury
	move.b #1,lastwascleared
	move #2559,d0	;2560 longwords per bitplane.
	move.l drawscreen,a0
	csloop:move.l #0,(a0)+
	dbf d0,csloop
	move.l #-1,lastadds	;No last letter.
	move.l #-2,oldcursor	;No last cursor.
	rts

nonewletter:	moveq #7,d3	;This routine prints the last
	cmp.l #-1,lastadds	;letter then forgets it, so that
	beq flashcursor	;we can play at control codes. 
		;Quit if already done.

	move.l lastadds,a4	;Plot the letter.
	adda.l drawscreen,a4
	move.l lastfont,a3
plloop4:	move.b (a3)+,(a4)
	adda.l #40,a4
	dbf d3,plloop4
	move.l #-1,lastadds

	bra flashcursor	;Now print cursor.

***************************************************************************

cpcount:	dc.w 0	;And now for something completely
lastadds:	dc.l 0	;different......
lastfont:	dc.l 0
lastcursor:	dc.l 0
oldcursor:	dc.l 0
cursorphase:	dc.b 0
lastcursorphase:	dc.b 0
oldcursorphase:	dc.b 0
lastwascleared:	dc.b 0
wdelay:	dc.w 0
pausing:	dc.w 0
curx:	dc.b 1
cury:	dc.b 1

	even

messagepointer:	dc.l message

message:

dc.b "THIS IS A PRETTY BOG STANDARD TEXT",13,"WRITER...."
dc.b 1,75
dc.b 13
dc.b "WHAT YOU JUST SAW WAS A PAUSE"
dc.b 1,15
dc.b "FOLLOWED",13,"BY A RETURN."
dc.b 13,13
dc.b "I YOU TYPE IN A LOT OF TEXT THEN IT'LL",13,"WRAP JUST "
dc.b "LIKE A BASIC PRINT STATEMENT."
dc.b 13
dc.b "YOU CAN ALSO LOCATE TEXT...."
dc.b 31,16,18
dc.b "BUT...."
dc.b 31,0,31
dc.b "WHEN YOU REACH THE BOTTOM OF THE SCREEN,"
dc.b "THIS HAPPENS!"
dc.b 13,13
dc.b "YOU CAN ALSO CLEAR THE SCREEN YOURSELF."
dc.b 12
dc.b "IF YOU TTYPE IN"
dc.b 16,16,16,16,16,16,16,16
dc.b "TYPE IN SOMETHING WRONG THEN YOU CAN DELETE IT."
dc.b 13,13
dc.b "ALSO YOU CAN MOVE THE CURSOR AROUND",13,"WITHOUT " 
dc.b "SPOILING TEXT UNDERNEATH."
dc.b 9,9,9,9,9,9
dc.b 10,10,10,10,10,10
dc.b 8,8,8,8,8,8
dc.b 11,11,11,11,11,11
dc.b 13
dc.b "THAT WAS FUN...."
dc.b 9,9,9,9,9,9
dc.b 10,10,10,10,10,10
dc.b 8,8,8,8,8,8
dc.b 11,11,11,11,11,11
dc.b 13,13
dc.b "BUT NOW IT IS TIME TO GO...."
dc.b 1,50
dc.b "BYE!"
dc.b 12,0		;Don't forget the 0!

	even

; This is the font (8 bytes per letter) in ascii order.
; Capitals only, I'm afraid.
; You can always make up one of your own....

font:

dc.b $00,$00,$00,$00,$00,$00,$00,$00
dc.b $30,$30,$30,$30,$00,$30,$30,$00,$6c,$6c,$48,$00,$00,$00,$00,$00
dc.b $14,$3e,$14,$3e,$14,$00,$00,$00,$88,$50,$58,$7c,$7c,$38,$38,$00
dc.b $30,$30,$30,$30,$00,$30,$30,$00,$60,$f6,$96,$62,$94,$fc,$72,$00
dc.b $30,$30,$10,$00,$00,$00,$00,$00,$18,$38,$20,$20,$20,$38,$18,$00
dc.b $30,$38,$08,$08,$08,$38,$30,$00,$10,$38,$fe,$7c,$38,$6c,$82,$00
dc.b $00,$10,$10,$7c,$10,$10,$00,$00,$00,$00,$00,$00,$00,$18,$30,$00
dc.b $00,$00,$00,$7c,$00,$00,$00,$00,$00,$00,$00,$00,$00,$18,$18,$00
dc.b $02,$04,$08,$10,$20,$40,$80,$00,$7c,$fe,$82,$ba,$82,$fe,$7c,$00

dc.b $10,$30,$10,$10,$10,$38,$38,$00,$7c,$fe,$02,$7c,$80,$fe,$fe,$00
dc.b $fe,$fe,$02,$3c,$02,$fe,$fc,$00,$e8,$e8,$48,$7c,$08,$1c,$1c,$00
dc.b $fe,$fe,$80,$fc,$02,$fe,$fc,$00,$7c,$fe,$80,$fc,$82,$fe,$7c,$00
dc.b $fe,$fe,$84,$08,$10,$38,$38,$00,$7c,$fe,$82,$7c,$82,$fe,$7c,$00
dc.b $7c,$fe,$82,$7e,$02,$fe,$7c,$00,$00,$30,$30,$00,$00,$30,$30,$00
dc.b $00,$30,$30,$00,$00,$30,$60,$00,$00,$0c,$30,$c0,$30,$0c,$00,$00
dc.b $00,$00,$7c,$00,$7c,$00,$00,$00,$00,$c0,$30,$0c,$30,$c0,$00,$00
dc.b $7c,$fe,$02,$3c,$00,$30,$30,$00,$7c,$82,$b2,$9a,$aa,$bc,$40,$00

dc.b $fe,$fe,$44,$7c,$44,$ee,$ee,$00,$fc,$fe,$42,$7c,$42,$fe,$fc,$00
dc.b $7c,$fe,$82,$80,$80,$fe,$7c,$00,$fc,$fe,$42,$42,$42,$fe,$fc,$00
dc.b $fe,$fe,$40,$7c,$40,$fe,$fe,$00,$fe,$fe,$40,$7c,$40,$e0,$e0,$00
dc.b $7c,$fe,$80,$be,$82,$fe,$7c,$00,$ee,$ee,$44,$7c,$44,$ee,$ee,$00
dc.b $38,$38,$10,$10,$10,$38,$38,$00,$fe,$fe,$02,$02,$02,$fe,$7c,$00
dc.b $ee,$ee,$44,$78,$44,$ee,$ee,$00,$e0,$e0,$40,$40,$42,$fe,$fe,$00
dc.b $ee,$ee,$54,$54,$44,$ee,$ee,$00,$ce,$ee,$64,$54,$4c,$ee,$e6,$00
dc.b $7c,$fe,$82,$82,$82,$fe,$7c,$00,$fc,$fe,$42,$7c,$40,$e0,$e0,$00

dc.b $78,$fc,$84,$b4,$8c,$fc,$7a,$00,$fc,$fe,$42,$7c,$44,$ee,$ee,$00
dc.b $7e,$fe,$80,$7c,$02,$fe,$7c,$00,$fe,$fe,$92,$10,$10,$38,$38,$00
dc.b $ee,$ee,$44,$44,$44,$7c,$38,$00,$ee,$ee,$44,$28,$28,$10,$10,$00
dc.b $ee,$ee,$44,$54,$54,$28,$28,$00,$ee,$ee,$44,$38,$44,$ee,$ee,$00
dc.b $ee,$ee,$44,$38,$08,$70,$70,$00,$fe,$fe,$02,$7c,$80,$fe,$fe,$00
dc.b $30,$30,$78,$7c,$7c,$38,$38,$00,$00,$00,$01,$07,$1f,$30,$fc,$00
dc.b $3c,$7c,$fc,$1e,$fe,$1e,$3f,$00,$0f,$30,$4d,$bf,$bb,$49,$30,$0f
dc.b $f0,$0c,$b2,$fd,$dd,$92,$0c,$f0,$7c,$92,$ba,$fe,$c6,$ba,$7c,$00
