	opt	l-,c-,d-,o+,ow-

; Rob Northen Magic Number Reader
; Trojan of The Medway Boys

Forbid		=	-132
Permit		=	-138
OldOpenLibrary	=	-408
CloseLibrary	=	-414

; dos

Open	=	-30
Close	=	-36
Read	=	-42
Write	=	-48

; open dos library...

	SECTION TEXT

	move.l 	$4.w,a6			; exec base
	lea 	DosName,a1		; name
	jsr 	OldOpenLibrary(a6)
	move.l 	d0,DosBase		; save base
	beq 	error			; not opened

	; open console window...

	move.l d0,a6

	move.l 	#ConName,d1		; console name
	move.l 	#1005,d2		; open mode (old)
	jsr 	Open(a6)
	move.l 	d0,ConHand		; save console handle
	beq 	error			; not opened

; turn of task switching and drives

	move.l	$4.w,a6
	jsr	Forbid(a6)

	move.b	#$87,$bfd100		; switch off drives
	nop
	nop
	move.b	#$78,$bfd100

; print title...

	move.l	dosbase,a6
	bsr 	bold
	move.l 	ConHand,d1
	move.l 	#titletxt,d2
	move.l 	#ttsize,d3
	jsr 	Write(a6)
	bsr 	norm
	bsr	newline

loop:	move.l	ConHand,d1
	move.l	#instxt,d2
	move.l	#inssize,d3
	jsr	Write(a6)
	bsr	newline

	move.l	ConHand,d1
	move.l	#buffer,d2
	move.l	#80,d3
	jsr	Read(a6)

	move.b	buffer,d2
	cmp.b	#"q",d2
	beq	quit
	cmp.b	#"Q",d2
	beq	quit

	move.b	#"N",RNflag
	jsr	robnortnew
	cmp.l	#0,d0
	beq	notRNnew
	bra	gotit

tryold	move.b	#"O",RNflag
	jsr	robnortold

	cmp.l	#0,d0
	beq	notRNold

gotit	move.l	d0,-(a7)
	
	move.l	ConHand,d1
	move.l	#numtxt,d2
	move.l	#numsize,d3
	jsr	Write(a6)

	move.l	(a7)+,d0
	bsr	printhex
	bsr	newline
	bra	loop

notRNold
	move.l	ConHand,d1
	move.l	#notoldtxt,d2
	move.l	#notoldsize,d3
	jsr	Write(a6)
	bsr	newline
	bra	loop

notRNnew
	move.l	ConHand,d1
	move.l	#notnewtxt,d2
	move.l	#notnewsize,d3
	jsr	Write(a6)
	bsr	newline
	bra	tryold

; close the console window...

quit:	move.l 	ConHand,d1
	jsr 	Close(a6)

	move.l 	$4.w,a6
	move.l 	DosBase,a1
	jsr 	CloseLibrary(a6)

	jsr	Permit(a6)

	clr.l 	d0			; no return code
	rts

error:	move.l 	#-1,d0			; return code
	rts

; print hex number in d0...

printhex:
	lea 	store,a0		; temp store for ASCII
	move.w 	#7,d1			; print 8 chars

prnloop:
	clr.l 	d2			; clear total
	rol.l 	#4,d0			; get digit
	move.b 	d0,d2
	andi.b 	#$f,d2			; mask out high nibble
	cmp.b 	#$a,d2			; digit or letter ?
	blt 	p_digit			; digit!
	addi.b 	#"A"-$a,d2		; convert to ASCII
	bra 	storeit			; and store

p_digit:
	addi.b 	#"0",d2			; convert to ASCII

storeit:
	move.b 	d2,(a0)+		; store
	dbf 	d1,prnloop

; now print the store....

	move.l 	DosBase,a6
	move.l 	ConHand,d1
	move.l 	#store,d2		; pointer to ASCII
	move.l 	#8,d3			; 8 chars
	jsr 	Write(a6)

; print (NEW) or (OLD)...

	cmp.b	#"O",RNflag
	beq	printold
	move.l	ConHand,d1
	move.l	#newtxt,d2
	move.l	#newsize,d3
	jsr	Write(a6)
	rts

printold
	move.l	ConHand,d1
	move.l	#oldtxt,d2
	move.l	#oldsize,d3
	jsr	Write(a6)
	rts

; print a newline...

newline:
	move.l 	DosBase,a6
	move.l 	ConHand,d1
	move.l 	#nltxt,d2
	move.l 	#nlsize,d3
	jsr 	Write(a6)
	rts

; set bold...

bold:
	move.l 	DosBase,a6
	move.l 	ConHand,d1
	move.l 	#boldtxt,d2
	move.l 	#btsize,d3
	jsr 	Write(a6)
	rts

; set normal...

norm:
	move.l 	DosBase,a6
	move.l 	ConHand,d1
	move.l 	#normtxt,d2
	move.l 	#ntsize,d3
	jsr 	Write(a6)
	rts

	SECTION DATA


DosName: 	dc.b 	"dos.library",0

ConName: 	dc.b 	"CON:100/20/340/120/MAGIC",0

titletxt: 	dc.b 	"       Rob Northen Magic Number Reader",$a
		dc.b	"          CODED BY THE MEDWAY BOYS"
ttsize	=	*-titletxt

instxt:		dc.b	$a,"Please insert disk in DF0: and hit <CR>",$a,$d
		dc.b	"     ---==>TYPE Q<CR> TO QUIT<==---"
inssize	=	*-instxt

numtxt:		dc.b	"Magic Number: $"
numsize	=	*-numtxt

newtxt		dc.b	" (NEW)"
newsize	=	*-newtxt

oldtxt		dc.b	" (OLD)"
oldsize	=	*-oldtxt

notnewtxt	dc.b	"Disk is NOT Rob Northen (NEW)!"
notnewsize	=	*-notnewtxt

notoldtxt	dc.b	"Disk is NOT Rob Northen (OLD)!"
notoldsize	=	*-notoldtxt

nltxt: 		dc.b 	$a,$d
nlsize	=	*-nltxt

boldtxt: 	dc.b 	$9b,"1;31;40m"
btsize	=	*-boldtxt

normtxt: 	dc.b 	$9b,"0;31;40m"
ntsize	=	*-normtxt

even

DosBase: 	dc.l 	0
ConHand: 	dc.l 	0
store: 		ds.b 	8
buffer:		ds.b 	80

robnortold:	incbin	robnorthen.old
robnortnew:	incbin	robnorthen.new

RNflag:		ds.b	1

	END
