This month I am presenting a simple little Basic program that takes you into the heart of the QL ROM. Superbasic is extensible. You can add machine language Procedures and Functions which act just like they were in ROM. The way the machine handles this is to create a list of the Functions & Procedures and update it. Because the machine is multitasking we have to find the Basic Job, (which is always Job#0.) The pointer to this area of memory is kept in the System Variable SV_Basic.
SV_BASIC -> BASIC JOB HEADER ($68 BYTES) $00 BV_START POINTER BASE . . $10 BV_PFBAS PROGRAM FILE $14 BV_PFP TOP OF PROG FILE $18 BV_NBAS NAME TABLE $1C BV_NTP TOP NAME TABLE $20 BV_NLBAS NAME LIST $24 BV_NLP TOP NAME LIST $28 BV_VVBAS VARIABLE VALUES $2C BV_VVP TOP VAR. VALUES . . $100 BV_END TOP OF BASIC VARS.
Once we know how this section is set up we can write a simple basic program which will pull these names, values & addresses from the tables. In particular, the basic listing given will print the name of each Procedure, Function and its address in ROM; the name of each variable and its value, if it has one. Note that the size of the Basic area changes while the program runs & so it sometimes generates garbage. What happens is that the name will come out as two or three lines of garbage &/or other names. One way around this would be to rewrite the procedures as functions, so that each time the values of offsets used are dynamically updated rather than what they were when the program firt started running. I believe the classic cop out is, this is left as an exercise for the student.
NOTE: Adrian Dickens incorrectly says (Page 330) that SV_basic is a pointer to the base of the Basic stack. Also while involved in other work I noticed that he has (page 329) RI_SUB as TOS-NOS, when it is NOS-TOS. This is the kind of thing that gives you grey hairs if you don’t catch it.
If this should happen into the hands of any QL’er out of Vancouver, why don’t you drop me a line. I’d like to hear from you.
100 REMark sys_var Dec/85 by H.Taylor
120 layout
140 printer
160 SV_BASE=HEX('28000')
180 HDR=104
200 PRINT 'SYSTEM VARIABLES : ';HEX$(SV_BASE,24)
220 BAS_AREA=PEEK_L(SV_BASE + 16)
240 PRINT 'BASIC AREA BASE: ';HEX$(BAS_AREA,24)
260 REMark
280 BAS_END=PEEK_L(SV_BASE + 20)
300 PRINT ' END: ';HEX$(BAS_END,24)
320 PNTR_BAS=BAS_AREA + HDR
340 PRINT 'POINTERS BASE: ';HEX$(PNTR_BAS,24)
360 REMark
380 BV_TOP=PNTR_BAS + 256
400 PRINT 'BASIC VARIABLES END: ';HEX$(BV_TOP,24)
420 REMark
440 PROG_FILE=PNTR_BAS+ 16
460 OFFSET=PEEK_L(PROG_FILE)
480 PF_START=PNTR_BAS + OFFSET
500 PRINT\ 'PROGRAM FILE BASE: ';HEX$(PF_START,24)
520 REMark
540 FILE_END=PNTR_BAS + 20
560 OFFSET=PEEK_L(FILE_END)
580 PF_END=PNTR_BAS + OFFSET
600 PRINT ' END: ';HEX$(PF_END,24)
620 REMark
640 NAM_TABL=PNTR_BAS + 24
660 OFFSET=PEEK_L(NAM_TABL)
680 NT_START=PNTR_BAS+ OFFSET
700 PRINT 'NAME TABLE BASE: ';HEX$(NT_START,24)
720 REMark
740 NAMTABL_END=PNTR_BAS + 28
760 OFFSET=PEEK_L(NAMTABL_END)
780 NT_END=PNTR_BAS + OFFSET
800 PRINT ' END: ';HEX$(NT_END,24)
820 REMark
840 NAM_LIST=PNTR_BAS+ 32
860 OFFSET=PEEK_L(NAM_LIST)
880 NL_START=PNTR_BAS+ OFFSET
900 PRINT 'NAME LIST BASE: ';HEX$(NL_START,24)
920 REMark
940 NAMLIST_END=PNTR_BAS + 36
960 OFFSET=PEEK_L(NAMLIST_END)
980 NL_END=PNTR_BAS + OFFSET
1000 PRINT ' END: ';HEX$(NL_END,24)
1020 REMark
1040 VAR_VALUES=PNTR_BAS+ 40
1060 OFFSET=PEEK_L(VAR_VALUES)
1080 vv_start=PNTR_BAS+ OFFSET
1100 PRINT 'VARIABLE VALUES BASE: ';HEX$(vv_start,24)
1120 REMark
1140 VARVAL_END=PNTR_BAS + 44
1160 OFFSET=PEEK_L(VARVAL_END)
1180 VV_END=PNTR_BAS + OFFSET
1200 PRINT ' END: ';HEX$(VV_END,24)
1220 test$='this is a test example'
1240 REMark
1260 PRINT#0,'HIT ANY KEY TO CONTINUE' : PAUSE : CLS#0
1280 SHOW_TABLE 1
1300 IF pr :
1320 CLS : CLS#0 : PRINT#0,'Hit any key when Printer ready'
1340 PAUSE : SHOW_TABLE 3
1360 ELSE
1380 STOP
1400 END IF
1420 DEFine PROCedure layout
1440 BORDER #0,8,0
1460 WINDOW #2,512,216,0,0 : BORDER #2,5,0
1480 WINDOW #1,512,216,0,0 : BORDER #1,5,0
1500 PAPER #2,0 : INK #2,4
1520 PAPER 0 : INK 6
1540 PAPER#0,0 : INK#0,2 : MODE 4
1560 END DEFine layout
1580 DEFine PROCedure SHOW_TABLE(chan)
1600 LOCal addr,fin,off,ch
1620 addr=NT_START
1640 fin=NT_END
1660 ch=chan
1680 PRINT#ch, \
1700 FOR a=addr TO fin STEP 8
1720 type=PEEK_W(a)
1740 PRINT#ch, 'Code:';HEX$(type,16);
1760 SELect ON type
1780 =1 : a$='Unset string'
1800 =2 : a$='Unset fp #'
1820 =3 : a$='Unset Integer'
1840 =257: a$='Str.expr.'
1860 =258: a$='FP expr.'
1880 =259: a$='Integer expr.'
1900 =513: a$='String'
1920 =514: a$='F.Point Number'
1940 =515: a$='Integer'
1960 =768: a$='Substring'
1980 =769: a$='String array'
2000 =770: a$='FP array'
2020 =771: a$='Integer array'
2040 =1024: a$='Super Basic Proc'
2060 =1026: a$='Super Basic Proc'
2080 =1281: a$='SupBas Str Func'
2100 =1282: a$='SupBas FP Func'
2120 =1283: a$='SupBas Int Func'
2140 =1538: a$='Rep Loop Index'
2160 =1794: a$='For Loop Index'
2180 =2048: a$='Mach Code Proc'
2200 =2304: a$='Mach Code Func'
2220 =REMAINDER : a$='Type error'
2240 END SELect
2260 PRINT#ch, ' '; a$;' ';
2280 off=PEEK_W(a+2)
2300 flag=0
2320 SELect ON off
2340 =-1 : c$='No Name ': flag=1
2360 = 0 TO 32767 : c$='Name Pntr '
2380 =-2 TO -32767 : c$='Copy '
2400 END SELect
2420 PRINT#ch, TO 28
2440 PRINT#ch, c$;HEX$(off,16);
2460 nam_addr=NL_START + off
2480 Nam_len=PEEK(nam_addr)
2500 PRINT#ch, TO 40
2520 IF flag : GO TO 2680
2540 FOR c=1 TO Nam_len
2560 dat= PEEK(nam_addr +c)
2580 SELect ON dat
2600 =32 TO 126 : PRINT#ch, CHR$(dat);
2620 =REMAINDER : PRINT#ch. HEX$(dat,8);' ';
2640 END SELect
2660 END FOR c
2680 flag=0
2700 SELect ON type
2720 =1 TO 3,513 TO 515,769 TO 771,1538,1794 : b$='Value pntr'
2740 =257 TO 259,768 : b$='RI Stk pntr'
2760 =1024,1026,1281 TO 1283 : b$='Line Number':flag=1
2780 =2048,2304 : b$='Abs Addr' : flag=2
2800 END SELect
2820 PRINT#ch, TO 60
2840 PRINT#ch, b$;
2860 val=PEEK_L(a+4)
2880 val_flag=0
2900 SELect ON flag
2920 =0:IF val=-1 :
2940 d$='Undefined'
2960 ELSE val_addr=vv_start + val
2980 d$=val
3000 v$=HEX$(PEEK_L(val_addr),32) & HEX$(PEEK_L(val_addr+4),32)
3020 val_flag=1
3040 END IF
3060 =1: d$=PEEK_W(a+4)
3080 =2: d$=HEX$(val,24) : IF val>786432 : INK 2
3100 =REMAINDER : d$='Flag Error'
3120 END SELect
3140 PRINT#ch, TO 70
3160 PRINT#ch, d$
3180 IF val_flag :
3200 PRINT#ch, 'Value Table shows : ';
3220 SELect ON type
3240 =513 : str_len=PEEK_W(val_addr)
3260 FOR e=val_addr+2 TO val_addr+str_len+1 : PRINT#ch,CHR$(PEEK(e));
3280 PRINT#ch,
3300 =514 : PRINT#ch, v$;' = ';: conv_fp v$
3320 =515 : PRINT#ch, HEX$(PEEK_W(val_addr),16)
3340 =REMAINDER : PRINT#ch, v$
3360 END SELect
3380 END IF
3400 INK 6
3420 END FOR a
3440 END DEFine SHOW_TABLE
3460 DEFine PROCedure printer
3480 pr=0
3500 REMark sets up a printer at 9600 Baud on SER1hr : modify as required
3520 PRINT#0,'If you wish printer output toggle <CTRL><P>,'\'Else hit any key
3540 a$=INKEY$(-1) : c=CODE(a$) : CLS#0
3560 IF c=16 :
3580 pr=1
3600 OPEN#3,ser1hr
3610 PRINT#0,'Printer channel opened. SER1hr at 9600 Baud'
3620 BAUD 9600
3640 END IF
3660 END DEFine printer
3680 DEFine PROCedure conv_fp(fp$)
3700 LOCal fp,expon,man
3720 expon=HEX(fp$(2 TO 4))
3740 man=HEX(fp$(5 TO 12 ))
3760 fp = man * (2^(expon-2048-31))
3780 PRINT fp
3800 END DEFine
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.