QL Monitor
Secrets of the QL memory
The organisation of ROM and RAM provides an insight into the new machine’s abilities. Eric Cowsill provides the answers
SINCLAIR has a winning combination – a new more powerful computer, a revolutionary operating system, a new dialect of Basic, and, most important, has taken away the drudgery of wading through a manual to try to discover how it works.
It seemed that the best plan would be to delve into the QL memory and find which addresses are used by the system and where the program area lies. A short program such as that shown in listing one is all that is needed to discover that the memory is divided between ROM and RAM, roughly as shown in figure one.
That program could, of course, be changed to given a more detailed look at key parts of the QL memory. Listing two shows a single Basic monitor program which evolved from the desire to do something useful while idling away the hours discovering the syntax of SuperBasic by trial and error.
The screen layout of the program will be the same, whether F1 or F2 is selected on power up. Line 90 alters the dimensions of the default output channel – channel one – so as to give the best display. Three options are provided from the menu display: an area of memory, alter an area of memory – placing the same value in each location, and quit to leave the program. The selection is made by pressing the key corresponding to the first letter of the required option. Notice the use of SELect to determine appropriate action.
If the ‘display’ option is chosen you will be asked to give the address from which you want the display to start. The PROCedure address will accept an input either in decimal or prefixed by ‘$’, in hex. The procedure is not entirely mug-trapped but will eliminate the most likely causes of invalid inputs.
Listing 1
5 CLS
10 i=1024
20 FOR j= 0 TO 256
25 x=j*i
30 a=PEEK(x):POKE x,65
35 PRINT j;"K ";PEEK(x);" ";
40 POKE x,a
45 NEXT j
This routine provided an interesting exercise in discovering how the QL handles strings. In its turn, address calls up another procedure to evaluate the string as a number, which is then returned as a value in the variable i.
The next action is a call to the PROCedure display. That will show 24 rows, each displaying the contents of 16 memory locations. Those values are shown in hex. Where they correspond to a printable character that is also displayed.
The display of printable characters makes it a relatively easy task to find the keywords in ROM, the Basic program area and the RAM used for string storage, buffers and system variables.
When the display is complete, three options are available. ‘Up arrow’ will show the area of memory immediately preceding that displayed. Similarly, ‘down arrow’ will show the block of memory immediately following. ‘L’ returns to the main menu.
If ‘Alter’ is selected, three values are requested. They are the start and end addresses and the value to be placed in those locations. The area which has been altered is then displayed automatically as soon as ‘Alter’ has been executed.
Working systematically through the QL memory will reveal more about the machine but figure two will highlight the areas which are likely to prove of interest.
The other purpose in writing the program was to try to discover how some of the SuperBasic works. Four of the programming techniques used will be of particular interest.
The string-handling capabilities are very interesting but not yet fully-implemented. Some of the string-handling functions lead to error messages or even program crashes, although on another program RUN they may operate perfectly well. That serves to demonstrate the temporary and incomplete nature of the existing ROM. It seems that if string slicing is to be effected it is necessary to DIMension a string array. The DEFined PROCedure address demonstrates the use of string slicing operations to eliminate invalid inputs.
The use of DEFine PROCedure in place of the usual form of subroutine has the advantage that the routine can be called by name – whether from the main program or from another procedure – rather than by, e.g., ‘GOSUB 1000’. That means that the program to some extent is self-documenting provided names are chosen sensibly.
The procedure begins with DEFine PROCedure name and terminates with END DEFine instead of RETURN. The disadvantage of that simple structure is that because it can be called simply by name, SuperBasic will often fail to detect errors in entering keywords, assuming instead that a procedure is to be called.
Notice the way in which parameters – values – can be passed to, and for that matter from, PROCedures. The PROCedure address expects a value to be passed to it which will be the value of the variable digit – see line 1410. That value is passed when the PROCedure is called by placing the relevant value after the PROCedure name – see lines 1320, 1340 and so on.
REPeat name is used at the beginning of a program loop. Execution of the program will continue with the instruction following the REPeat instruction when END REPeat name is reached. That eliminates many occurrences of GOTO. For example, in line 235 of the monitor program END REPeat main could be replaced by GOTO 107. The program will break out of the loop when EXIT name is encountered – see line 210. The writer found that the REPeat name had to be on a line of its own, otherwise the program seemed to end in continuous loop.
SELect n introduces a powerful structure to enable multiple branches to be selected. The statement is followed by a number of conditions. A sequence of instructions may follow each condition but only if the condition is true will the instructions be executed. Execution of the program will then continue at the instruction following END SELect. Notice the mopping-up condition ON n – REMAINDER. The instructions following that condition will be executed if the value of the variable is not one for which an alternative course has been specified.
The SELect n structure corresponds in certain respects with IF . . . THEN . . . ELSE IF . . . THEN. It has, however, some advantages over this alternative. The sequence of instructions to be executed can continue on separate lines which again reduces the need for GOTO statements.
That structure is used several times in the monitor program, for example in lines 190 to 230. It appears to work only with a numeric variable and where the condition is expressed in the form ON n = n1 rather than ON n < n1 or ON n > n1.
Time, no doubt, will provide the answers as to how best to use the no doubt very powerful SuperBasic commands and structures.
10 REMark **********************
15 REMark BASIC MONITOR PROGRAM
20 REMark FOR SINCLAIR QL
25 REMark Eric Cowsill
30 REMark MAY 1984
35 REMark **********************
90 WINDOW #1,472,256,30,0
100 CSIZE 0,0:MODE 0
102 DIM hx$(6):DIM in$(10)
105 REPeat main
107 CLS #1
110 PRINT " BASIC MONITOR";
112 PRINT " PROGRAM"
120 PRINT:PRINT
130 PRINT "Select from:-"
140 PRINT " Display"
145 PRINT " Alter"
150 PRINT " Quit"
155 REPeat kbd1
160 inp$=INKEY$
162 IF inp$<>"" THEN EXIT kbd1
165 END REPeat kbd1
170 inp=CODE(inp$)
180 IF inp >95 THEN inp=inp-32
190 SElect inp
200 ON inp = 68
201 CLS #1:PRINT
202 PRINT "Start address ?"
203 address 6:display
205 ON inp = 65:alter:display
210 ON inp = 81:EXIT main
230 END SElect
235 END REPeat main
240 STOP
1000 REMark ********************
1010 DEFine PROCedure display
1015 REPeat show
1020 FOR j=i TO i+368 STEP 16
1030 x=j:dechex 5
1040 PRINT hx$(1 TO );" ";
1050 FOR k=0 TO 7
1060 x=PEEK(j+k)
1065 dechex 2
1070 PRINT hx$(1 TO 2);" ";
1080 END FOR k
1085 PRINT " ";
1090 FOR k=8 TO 15
1100 x=PEEK(j+k)
1105 dechex 2
1110 PRINT hx$(1 TO 2);" ";
1120 END FOR k
1125 PRINT" ";
1130 FOR k=0 TO 15
1140 x=PEEK(j+k):ptchar x
1150 END FOR k
1160 PRINT
1170 END FOR j
1180 PRINT "up- preceding ";
1181 PRINT "block down-next";
1182 PRINT " block L-leave";
1183 PRINT " function"
1184 REPeat kbd2
1185 q$=INKEY$
1190 q=CODE(q$)
1195 IF q=108 THEN q=76
1200 SElect q
1210 ON q=208:i=i-384
1215 EXIT kbd2
1220 ON q=216:i=i+384
1225 EXIT kbd2
1230 ON q=76: EXIT show
1250 END SElect
1260 END REPeat kbd2
1280 END REPeat show
1290 END DEFine
1300 REMark *******************
1310 DEFine PROCedure alter
1320 CLS:PRINT
1322 PRINT "Alter memory from?"
1324 address 6
1330 a1=i
1340 PRINT
1342 PRINT "End address?"
1344 address 6
1350 a2=i
1358 PRINT
1360 PRINT "Value to insert?"
1362 address 3
1370 val=i
1380 FOR a=a1 TO a2
1382 POKE a,val
1383 END FOR a
1385 i=a1
1390 END DEFine
1400 REMark ********************
1410 DEFine PROCedure address (digit)
1415 PRINT "Enter address in ";
1420 PRINT"decimal or ";
1422 PRINT "prefix with '$'";
1425 PRINT " and enter in hex."
1426 NPUT in$
1427 l=LEN(in$):hd=0
1428 k=1
1429 REPeat lead
1430 IF l<=digit THEN EXIT lead
1431 IF k>digit THEN EXIT lead
1432 m=l+k-digit
1433 LET in$(k)=in$(m)
1434 k=k+1
1435 END REPeat lead
1436 in$=in$(1 TO 6)
1437 FOR k=1 TO LEN(in$)
1438 in=CODE(in$(k))
1439 IF in=36 THEN hd=1
1440 IF in>96 THEN in=in-32
1441 IF in>70 THEN in=48
1442 IF in<48 THEN in=48
1443 in$(k)=CHR$(in)
1444 END FOR k
1445 SElect hd
1446 ON hd = 0:eval
1447 ON hd = 1:hexdec
1450 END SElect
1460 REMark Digit specifies max
1462 REMark length of in$
1490 END DEFine
1500 REMark ********************
1510 DEFine PROCedure dechex (places)
1520 hx$=""
1530 FOR z=places-1 TO 0 STEP -1
1540 y=INT(x/16^z)
1550 x=x-y*16^z
1560 IF y>9 THEN y=y+7
1570 y=y+48
1580 hx$(places-z)=CHR$(y)
1590 END FOR z
1592 REMark Places specifies
1593 REMark number of hex
1594 REMark digits to be
1595 REMark returned
1597 END DEFine
1600 REMark *****************
1610 DEFine PROCedure hexdec
1620 i=0
1630 j=LEN(in$)
1635 FOR k=j TO 1 STEP -1
1640 a=CODE(in$(k))
1650 IF a>57 THEN a=a-7
1660 a=a-48:i=i+a*16^(j-k)
1670 END FOR k
1690 END DEFine
1700 REMark ******************
1710 DEFine PROCedure eval
1720 i=0:j=LEN(in$)
1730 FOR k=j TO 1 STEP -1
1732 a=CODE(in$(k))
1734 IF a<48 OR a>57 THEN a=48
1740 i=i+(a-48)*10^(j-k)
1750 END FOR k
1760 END DEFine
1800 REMark ********************
1810 DEFine PROCedure ptchar (x)
1830 IF x<32 THEN x=46
1840 IF x>128 THEN x=46
1850 PRINT CHR$(x);
1860 END DEFine
1870 REMark ********************
Since the article was written the writer has received a copy of the QL manual. One or two minor amendments to the program have been incorporated but there are still no solutions to the string-handling problems referred to. It seems we will have to wait until the dongle in the cartridge port is replaced by the final version of the ROM.
SINCLAIR USER September 1984
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.