'///
'/// List 1: NestorBASIC load / sample of NestorPreTer format code
'/// Summarizing: NPR converts this list into a normal MSX-BASIC file.
'///

'*********************************
'*                               *
'*  First we define some macros  *
'*                               *
'*********************************

'---  Constants  ---

@define TRUE  -1
@define FALSE  0
@define ON @TRUE
@define OFF @FALSE

'@REQ_SEGS defines the minimum number of RAM segments we will need,
'including the five first ones, which are used by NB itself:
'When NB is loaded, the availability of at least REQ_SEGS segments
'is checked; if not available, an error message is prompted and
'NB is uninstalled.

@define REQ_SEGS 6

'---  Variables  ---

'We will centralize all variables in an array named D,
'which will be created after loading NB (see tip#2 for more details).
'Exception is made for loop counters, which must be simple variables.
'Also, we define simple variable @ERROR for NB functions error recovering.

@define NUM_VARS 3	'Total number of variables we will use
			'(except @ERROR and loop counters)
@define ERROR e
@define NUM_SEGS d(0)	'Total number of available segments
@define FILE_HANDLE d(1)	'Identifier for any open file
@define ANY_DATA d(2)
'...
'define here all variables you will use
'and don't forget to set NUM_VARS appropriately
'...

'We define also all the loop counters we will use
'It is better to use b, b1, b2... so we can easily initialize all
'with just a DEFINT b

@define LOOP b
@define LOOP2 b2

'---  NestorBASIC functions ---

'Using macros to name NB functions you don't need to remember
'the numbers for all functions (and vice versa, when you find
'a function call in your list, you don't need to remember which is
'the function for that number)

@macro	NB_UNINST e=usr(0)	'Uninstalls NestorBASIC
@macro	NB_INFO e=usr(1)	'Gets info about NestorBASIC
@macro	R_SEG	e=usr(2)	'Reads a byte from a segment

'...
'define here all functions you will use, or define all
'in an external macros file (this is better).
'In my HP you can find a file with all NB functions defined as macros.
'...

'---   Samples of useful macros  ---

'Clear keyboard buffer:

@macro	CLEAR_KEY_BUFFER defusr1=&h156: @ERROR=usr1(0)

'Check if ENTER is being presse (if @PRESS_ENTER then...)

@macro	PRESS_ENTER (peek(&HFBEC) and 128) = 0

'Uninstalls NB freeing BASIC memory, and ends.
'BEWARE: Don't uninstall NB from inside a turbo block!

@macro	FINISH	p(0)=@YES: @NB_UNINST: end

'--- To identify the list ---

@remon

'list1.bas

@remoff


'**********************
'*                    *
'*  NestorBASIC load  *
'*                    *
'**********************

~	maxfiles=0:			'This saves about 250 bytes
	keyoff: screen 0: width 80:
	color: color 15,0,0:
	?"--- Loading NestorBASIC... ---": ?:
	bload"nbasic.bin",r:
	defint @ERROR:		'NB error variable
	@ERROR= 0:
	if p(0) >4 then
	      goto ~LOADOK	'No error if P(0) is at least 5
	else
		?"ERROR: ";

'*** Error? Then show message and finish.

~	if p(0)=0 then
		?"No mapped memory or only 64K found!":
		end

~	if p(0)=1 then
		?"Disk error when loading NestorBASIC!":
		end

~	if p(0)=2 then
		?"No free segments!":
		end

~	if p(0)=3 then
		?"NestorBASIC was already installed.":
		@NB_INFO:
		goto ~ALR_LOAD

~	if p(0)=4 then
		?"Unknown error.":
		end

'*** Jumps here if NestorBASIC loaded successfully.

~LOADOK:	?"NestorBASIC loaded successfully!":
		?"Available segments:"; p(0): ?

'*** Jumps here if NestorBASIC was already loaded.

~ALR_LOAD

'*** Checks if we have at least REQ_SEGS segments, else ends.

~	defint d: dim d(@NUM_VARS):	'Creates variables array
	if p(0)< @REQ_SEGS then
		?"ERROR: Not enough free segments!":
		?"I have"; p(0) ;" segments and I need at least";
		@REQ_SEGS; "segments.":
		@FINISH
	else
		@NUM_SEGS= p(0)
'...
'Put your program from here. Suggestion for the beginning:

_turbo on (p(), d(), e)
dim f$(1): defint @LOOP: @LOOP=0: @LOOP2=0
