*		      NPR2 v1.0. 28th September 1990
*		      ------------------------------

* The Problem
* -----------

*	PAL Amigas occasionally boot up with an NTSC 200-line screen.
*	This is rather annoying, as windows won't open to full size,
*	and some programs won't run. The machine has to be reset - and
*	there's always the possibility of the problem recurring on
*	reboot.

* The Solution
* ------------

*	Declan Mc Ardle wrote a neat little program called NoPalReset
*	which checked the DisplayFlags in GfxBase. If the screen was
*	NTSC, the machine was reset using the proper Commodore Reset
*	Code.

* The Other Problem
* -----------------

*	NoPalReset worked fine on most Amigas (including mine) but
*	users of newer machines (mostly B2000s) reported repeated Guru
*	Meditations. Some unfortunates even got their hard disks
*	scrambled.¹

*				--------
*  ¹: This is purely on hearsay, so I cannot prove it.
*     No disrespect is implied in any way towards Mr. Mc Ardle.
*				--------

*	This seemed odd, because the source code supplied with
*	NoPalReset seemed correct. There was one tiny mistake, but
*	that would probably not cause any problems (and it would only
*	be executed if your machine was in a *bad* way anyway).

*	Disassembling the executable image provided a new lead; the
*	source and the object code didn't match. There were a couple
*	of subtle nuances which could lead to problems.

*	Commodore's Reset Code MUST be LONGWORD aligned. It was only
*	word aligned in NoPalReset, and this could cause problems.
*	Also a novel (shortcut ? I'm not sure) method was used in
*	determining the display mode; this probably wouldn't cause any
*	damage, but is contrary to Official Policy.

* The Solution
* ------------

*	Enter NPR2 (NoPALReset 2). It does everything that NoPalReset
*	was supposed to do, but strictly by the book. It's been fairly
*	heavily tested; assuming the Relocator keeps its promise to
*	relocate to longword boundaries (as it must) the Commodore
*	Reset Code will be longword aligned. All libraries opened are
*	closed on exit, so no memory will be lost

* Using NPR2
* ----------

*	Copy the NPR2 program into the C: directory of your boot disk
*	- it's only 188 bytes long. Now edit your S:Startup-Sequence,
*	and insert a line at the start reading

*				C:NPR2

*	Resave your S:Startup-Sequence, and the next time you reboot
*	NPR2 will make sure you get a PAL screen.

*	RUNNING NPR2 ON AN NTSC AMIGA IS SINGULARLY POINTLESS and is
*	therefore not recommended.

*	When NPR2 reboots the machine, you get the bright white screen
*	very suddenly. This can be a disconcerting if you're not
*	expecting it. Therefore it's not clever to install NPR2 on
*	someone else's machine without telling them, since nervous
*	users don't appreciate being traumatised.

* About the Source
* ----------------

*	Declan's code was very well commented, so only needed
*	rearranging and patching to fix it up. It used a lot of
*	includes for such a small program, so the relevant details
*	were spliced in to make the source file self-contained. It's
*	also strictly Metacomco ASSEM compatible now, only because I
*	don't have DevPac 2.
*	[Fixing the source wasn't helped by a misprint in a reference
*	 book which said that gb_DisplayFlags is actually one longword
*	on from where it really is. That got me for a while.]

*	To rebuild NPR2, just run this file (NPR2.DOC) through your
*	favourite assembler. It's your assembler's fault (for not
*	being ASSEM compatible) if any errors crop up.

*	For Charlie Gibbs' A68k,

*			A68K NPR2.DOC

*	and then link (using The Software Distillery's Blink)

*			Blink NPR2.o

*	Depending on your assembler's ability to optimise branches,
*	you should get an executable of around 188 bytes.

* Tools used
* ----------

*	All the software tools used to build NPR2 are freely
*	distributable, and available in the Fred Fish Collection.

*     Tool	Name   Version	  Author	      Fish Disk
*   -----------------------------------------------------------------
*   Text Editor DME	1.40	Matt Dillon		 359
*   Assembler	A68K	2.61	Charlie Gibbs		 314
*   Linker	Blink	6.7	The Software Distillery  314
*   Monitor	Mon	1.24	Timo Rossi		 310
*   Calculator	Calc	3.0	Jimmy Yang		 210

* Distribution
* ------------

*	This work (NPR2.DOC and the NPR2 executable) may be freely
*	distributed by any means as long as

*	  - no charge is made for the work itself

*	  - NPR2.DOC and the NPR2 executable are not altered

*	  - NPR2 is not documented in any other way apart from a
*	    verbatim NPR2.DOC

*	You are free to make changes to NPR2.DOC, but you must not
*	distribute the modified forms under the same name. If any
*	changes are made to this document, my name and address MUST be
*	removed too.

*	No guarantee is given that this software fulfils any specific
*	task or purpose. Statements made herein which constitute an
*	infringement of local statutory rights are invalid.

*					Stewart C. Russell
*					 140 Capelrig Road
*					  Newton Mearns
*					   Glasgow
*					    SCOTLAND

*	Length of file NPR2 is 188 bytes.
*	Length of file NPR2.DOC is 06975 bytes.

*	And now, for some source code...

	IDNT NPR2

* NPR2 by Stewart C. Russell, 28th September 1990
* Based on `NoPalReset' by Declan Mc Ardle.
* Detects if Amiga is in NTSC or PAL mode, resets if in NTSC mode.

	SECTION NoPALReset,CODE

_AbsExecBase	 EQU 4
_LVOOpenLibrary  EQU -552
_LVOCloseLibrary EQU -414
_LVOSupervisor	 EQU -30
gb_DisplayFlags  EQU 206
ERROR		 EQU 20
NTSC		 EQU 1

	MOVE.L	_AbsExecBase,a6 	Put ExecBase in a6.
	LEA	GfxLib,a1		Put Gfx name in a1.
	MOVE.L	#33,d0			Oldest Current Version.
	JSR	_LVOOpenLibrary(a6)     Open Graphics Library.
	MOVE.L	d0,_GfxBase		Put pointer in _GfxBase.
	TST.L	d0			Was it 0 (Library Unobtainable) ?
	BEQ	quit			If so exit, else -
	MOVE.L	_GfxBase,a1		Put _GfxBase in a1.
	MOVE.W	gb_DisplayFlags(a1),d0  Put Display Flags in d0.
	CMP.W	#NTSC,d0		Compare it with NTSC (= 1).
	BEQ	reboot			If it's equal, RESET.
	JSR	CloseGfx		Close Graphics library.
	CLR.L	d0			Clear d0. (No error code)
	RTS				Go back to CLI.

quit:
	MOVE.L	 #ERROR,d0		Set d0 to FAIL error code.
	RTS				Go back to CLI.

CloseGfx:
	MOVE.L	_GfxBase,a1		Move address in _GfxBase to a1.
	JSR	_LVOCloseLibrary(a6)    Close the Graphics Library.
	RTS

* What follows is the official Commodore-Amiga Reset Code.
* Accept *no* substitutes.

reboot:
	MOVE.L	_AbsExecBase,a6 	Get a pointer to ExecBase.
	LEA.L	MagicResetCode(pc),a5   Location of code to Trap to.
	JSR	_LVOSupervisor(a6)      Start Code (must use JSR).

	CNOP	0,4		IMPORTANT: Must be longword aligned.
MagicResetCode:
	LEA.L	2,a0		Point to JMP instruction at start of ROM.
	RESET			All RAM goes away now!
	JMP	(a0)            Rely on prefetch to execute this instruction.

* Only data and stuff follows. Technically it should go in a hunk of its own,
* but that makes the program unnecessarily large.

GfxLib:
	DC.B	'graphics.library',0
	CNOP	0,4
_GfxBase:
	DC.L	0
	END

