Setting up LKDOS User-Defined Commands

See all articles from Sinc-Link v9 n5
Author(s): Steve Gunhouse
Publication: Sinc-Link
Pub Details: Sinc-Link v9 n5 (Volume 9, Issue 5)
Date: September/October 1991
Pages: 14-16

In the May issue of Sinc-Link (vol.9 no.3), Bob’s Notebook discusses using the LKDOS user defined command to invoke an NMI-F routine, but points out that he usually gets an error when he does this. There are ways around this, however.

The problem is that the routines end with a RET instruction. This is the right thing to do with an NMI routine, it would then return to the LKDOS NMI routine so that it could exit properly. However, it poses two problems when used as a user defined command. First, there is a system variable (see Appendix D of your User Manual) called CH ADD at 23645 which when the routine is done, should point to the terminator after the command (either a colon or ENTER). Second, it does not disable the cartridge before attempting to return.

It would not be appropriate to just put these into our NMI routine. An NMI routine should leave regular memory as it found it, if possible. So the first thing we will need to do is determine whether our routine was called as an NMI or a user command, then act appropriately. Luckily, there is a way provided in LKDOS to do this.

Two years ago, I published a list of useful locations in the LKDOS cartridge (see vol.7 no.4, pages 14-16). We find at location 8194 in the cartridge something I labelled NMI flag. If the routine that called LKDOS is a Basic command (like RANDOMIZE USR 100: CAT “”, or PRINT #4: DATA 0), this flag will be set to 0. If it was an NMI it will be 1. Certian LKDOS utilities (like the Move program) will set it to 11 when they use the cartridge, but that doesn’t matter much here. The point is that we can read this location to determine how our routine was called, and then act appropriately.

I should note at this point that some of the ml in the LKDOS ROM also reads this location, and will act differently depending on what it finds here. Most significant in this respect are the ml LOAD and SAVE commands (at 00C6 hex and 00CC hex). In particular, by changing this flag, I can make the computer think that it is running an NMI or just turned on, and load the AUTOSTART program without further effort. I take advantage of this in the new version of “autobt.Cx”, which Bob is describing in another article.

That isn’t of much use to someone trying to write a useful routine that works as both an NMI and a user command. If we hope to return to our original program (which autoboot doesn’t want to do), we must not change the NMI flag.

There are a few subroutines provided in LKDOS which may be useful in a user function. These include:

138 (008A hex) read a string to 8226 and check as a valid filename
141 (008D hex) find Basic terminator
144 (0090 hex) read a number, and leave it in BC register
156 (009C hex) read a string to 8226 (at most 10 chars.)
186 (00BA hex) disable LKDOS cartridge and exit

One other fact of note: LKDOS does implement RST 32, but not RST 24. For you non-programmers, RST 24 is used to read the current character as pointed to by CH ADD, while RST 32 reads the next character – ignoring spaces, colours, etc.

If we just want to make a routine that exits properly, and not read what comes after the DATA keyword, we need not worry about that. All we need to do is find the RET in our old NMI routine, and replace it with the following:

LD A,8194       load A register with NMI flag
AND A test to see if flag=0
RET NZ if flag is not 0 (i.e., if NMI) then return
CALL 141 adjust CH ADD to end of DATA statement
JMP 186 disable LKDOS and exit

We used a JMP 186 instead of a CALL because once LKDOS is disabled, it doesn’t make sense to return to our routine. With this, we could fix the old graphics 24 so that it didn’t cause the errors Bob complained about.

But why stop there? The PRINT #4: DATA 0 command always has to have a number (or string) after the DATA. With the other routines I listed above, we could find out what this number or string is, and do something with it!

I brought up RST 32 for a reason, as you may have guessed. When our routine is called, CH ADD will point to the keyword DATA, but before we use any of the subroutines to read a number or string, CH ADD should point at the first character of the number or string. So we should have something like this where we want to read the number:

LD A,8194       (we don't want to read a number if this is an NMI)
AND A (so if it isn't BASIC, do something else!)
JR NZ,SKIPIT
RST 32 set CH ADD to next character
CALL 144 read a number to BC (error if too large or not
a number)

After this we should check to make sure our number is an appropriate value. In the new version of graphics 24 in Bob’s article, I put the indented lines and the checking into a subroutine, just after the printer control codes.

If we were looking for 2 numbers instead of 1, we should check to make sure there is a comma between them. We can’t use RST 24 for this in LKDOS, so we would have to do something else. That would look like this:

read the first number, as above
LD HL,(23645) load some register pair with CH ADD
LD A,(HL) load A with the character
CP 44 compare to 44=CODE ","
JR NZ,NONUM if not a comma, then there is no second number
read the second number, as above

Likewise, we could look for a third number (or string), and a fourth, as many as we need. If there had to be a second number but it was left off, we could include code to generate an error message – RST 08 works normally. I decided not to do that in the new graphics 24, because I didn’t want to make it too hard to use. I could presume if the second number was left off, the programmer meant 24, but some other code may not have that option.

I have not concerned myself with preserving registers in this article. Rest assured, almost any of the LKDOS routines used in this article does alter many of the registers. So any values you need to keep track of should be stored somewhere, or pushed onto the stack, and then retrieved afterwards.

Also note that while I have had both NMI-F and the user command doing almost the same thing, that need not be the case. You could use the NMI flag to branch to a completely different routine for the NMI instead of just skipping reading numbers. So we could have a routine that copied the screen to the printer in response to PRINT #4: DATA 0 but reload the AUTOSTART if NMI-F was pressed. Or use your imagination.

Steven Gunhouse

retyped by GFC
due to faint copy