Computus Interruptus, Part 6

See all articles from SINCUS v4 n3

Hello again! It’s time to take another look at the wonderful world of interrupts. This installment will be of most use to those who’ve built the NMI Switch and the Universal LROS/AROS Development Board, shown in part 5 of this series. If you haven’t done so yet, warm up that soldering iron and start wiring!

As mentioned last time, this hardware is not compatiable with other add-ons that try to use the same memory space. This includes Doug Dewey’s excellent EMU series. This also includes my own “Extra Simple Spectrum Emulator”,(Sincus News, Feb 1985) which isn’t a true LROS, but uses the LROS memory area. Actually, we shouldn’t mind temporarily losing either of these, as we’ll see how to use the LROS/AROS Board as its own Spectrum Emulator in this installment.

Another incompatiable item that’s been brought to my attention is the Aerco disk interface, which uses the entire cartridge bank of memory. Cem Barut, with whom I’ve corresponded in the past, has used the Aerco memory to implement a “TS2068 Emulator” similiar to the one we’ll discuss in a moment, so the Aerco might actually be able to replace the Universal LROS/AROS Development Board, if the memeory loading programs shown here are rewritten to accommodate it. Unfortunately, Cem also tells me that the Aerco Interface uses the NMI signal. This means that my NMI switch may not work with it, or if it does, repeated use could eventually damage the Aerco.

Since I don’t have one of these units available to me I can’t recommend your using the NMI switches with it. If any of you readers can shed some light on this matter, I’ll pass the information on and give you the credit. Note however that you’ll have to say more than “I tried the NMI switch with my Aerco, and it works.” You’ll need to provide some information that shows that using the switch is also safe to use in the long run.

Next, before we get into the meat of things, I should point out that there are some typographical errors in the last installment. This is not unusual, since I tend to submit my articles in a handwritten “chicken scratch” which Paul has to interpret before he can type anything in. He also has to type pretty fast, because he usually gets my articles about a week AFTER the deadline, and then only after a lot of pleading and threats. So, the responsibility for errors is mine. If you notice an occasional spot where my articles lapse into incoherency (What do you mean, all the time? Twenty lashes with a tangled cassette tape for you!) that’s the reason. My handwriting, which rarely resembles English, will occasionally lapse from something resembling Greek into something more like Chinese. Now, on to the typos.

First of all, the fourth paragraph, first column should say that the “TS2068 code was NOT written to be run in the cartridge bank.” (We’ll do it anyway, in a moment.) At the end of the third paragraph, second column, it should read, ” a reset switch which can be disabled by software.” In the fouth paragraph, second column, it should say that my board was “designed to accommodate RAM, EPROM or EEPROM chips.” There’s no ROM or reason for wanting anything else. (Ouch, sorry.)

Now, we’ll be using the LROS/AROS Board with the READ ENABLE and WRITE ENABLE switches closed. Also, the LROS switch must be closed, and the AROS switch must be open. Those who’ve traced through the circuit will know that when the board is setup like this, it’s read from starting at address 0000H in the cartridge bank, but written to starting at C000H in the same bank. This allows us to do some fancy tricks from BASIC, which we’ll see in a moment.

Now, it’s time to fire up your TS2068. (If real fire is observed, it’s time for a new computer.) Type in and RUN Listing 1. This must be done with the orginial Timex ROMs in control, so if you’ve got a Spectrum ROM installed in board, just hold on until we tallk about Listing 4, later. Alternately, you can RUN the program on a friend’s computer.

When the program runs, it will SAVE to tape a “TS2068 Emulator” program. (Why in the world we’d want such a thing will be explained later.) The program will take a fairly long time to run before the SAVE message appears, but you only have to do this once to produce the new program, so this is not a real problem.

Once the new program is LOADed in, (and the LROS/AROS Board installed) it will insert a copy of the TS2068 code into the LROS/AROS Board, but it won’t run it, yet. The TS2068 Emulator program includes a complete copy of the TS2068 home ROM code. While this way may seem a waste of time when reloading, since there’s already a copy of the ROM code inside the TS2068, this also makes it possible for this program to be used by those who’ve installed a Spectrum ROM inside their computer. (Provided they have access to a computer that allows them to produce the TS2068 Emulator program, in the first place.) So this program will tolerate a much wider variety of computer set ups than will the simpler, faster way of just copying the TS2068 ROM directly into the LROS/AROS board.

For those who want to produce their own emulator programs, you should note that the code isn’t LOADed right into the LROS/AROS Board memory. It turns out that the TS2068 bank switching hardware gets in your way if you try. Instead, the code is first LOADed into RAM in the home bank, and then moved to the LROS/AROS Board with a short machine code program.

In order to switch to the emulator, you need only type OUT 244, 3, but don’t do this, yet. We want to make CHANGES to our TS2068 emulator. (No point in emulating the TS2068 exactly; our emulator will allow us to do things that the old ROM can’t.) This article contains several BASIC programs that make changes to the emulator code. These programs should run when the real ROM is in control. It’s possible to rewrite them for when the emulator is running the show, but for consistency in this article, we will ALWAYS make our patches when the real (home) ROM is in control, as running them will always return control to the home ROM.

All right! Now that you’ve been pummled with a plethora of preposterously polysyllabic paragraphs, it’s finally time to start talking about interrupts. We’ll assume that you’ve installed your LROS/AROS Board and NMI switch, set the switches properly, turned on your TS2068 and LOADed and RUN the TS2068 Emulator program. We’ll also assume you’ve done NOTHING else.

Now, type in and run Listing 2. This is just about the simplest possible program you can use to change code in the emulator, and this changes a single byte. Let’s see what we’re changing, and why, before we try to understand what Listing 2 does.

Listing 3 is a copy of the NMI routine in both the Timex and Sinclair ROMs, and it is the program that’s run when you push the NMI switch. If you’ve tried pushing this switch, you’ll have found that this resets the machine. This is due to a bug in the NMI routine. The machine looks at the contents of locations 5CB0/1 Hex, and if these contents are zero, the machine JUMPS to the address contained in the locations 5CB0/1 Hex. In other words, it jumps to location zero, causing a machine reset. However, if these locations do NOT contain zero, the machine goes right back to what it was doing, and no reset occurs.

Now, type OUT 244,3. Although nothing important seems to have happened, the TS2068 emulator is now in control. Press the NMI switch. If you ran Listing 2 earlier, you’ll find that pressing the switch no longer resets the machine. We’ve corrrected the bug. Type NEW or RANDOMIZE USR 0, or OUT 244,0. Any of these will switch you back to the old ROM. Now, the NMI switch causes the machine to reset again.

What have we done? Look at Listing 2. Line 10 makes sure that nothing important is going on in the upper 16K of the computer. Line 20 opens the upper 16K of memory to the cartridge bank. Now, when we poke something into the top 16K of memory, it will appear in the emulator, and the addresses will always be seperated exactly by 49152. So, in line 30, we POKE a number into location (49152 +109), and expect that it will go into location 109. This is location 006D in listing 3, and it changes the JR NZ, #0070 into JR Z, #0070. Line 40 gives the upper 16K back to the normal memory. It also will switch out the TS2068 emulator, if it’s active, and you won’t get the results we’ve been discussing. This is why all of the listings here should be run when the normal ROM is in control.

After using Listing 2 and switching in the TS2068 emulator, you can cause the computer to run your own custom machine code program at the push of an NMI switch. Just insert it into some unused portion of memory and put it’s address into locations 5CB0/1 Hex. However, if you then want the computer to resume where it left off, you’ll have to follow a couple of additional rules. First, note that the NMI routine saved the values of AF and HL. For this reason, the last 3 lines of your routine must be POP HL, POP AF, RETN, and they must be done in that order! Second, if your program uses any other registers, you must save their original values and restore them before you return. Third, you must not use, EI or DI. The NMI will automaticallydisable the maskable interrupt, and RETN will restore its original status, but using EI or DI can upset this. In a momment, we’ll run a new program that breaks some of these rules, and we’ll see what happens.

Now, if you have a Spectrum ROM installed in your computer, or if you have any variety of Spectrum emulator, type in and RUN Listing 4. This will produce a program similar to the TS2068 emulator, but it inserts the Spectrum ROM code into the LROS/AROS Board. Switching to the Spectrum emulator is a little more complicated. Type in:

RANDOMIZE USR 0   (resets the computer)
OUT 244,3         (Switches banks & crashes computer)
RANDOMIZE USR 0   (resets properly after the crash)

Some users may be tempted to leave out the RANDOMIZE USR 0s, but they’re important; especially the second one. The OUT command crashes the computer and it’s very unwise to go on using it without first resetting it. Those who remember my INTERFACE ZERO schematic,(SINCUS NEWS, March 1985) will notice a reset was included. This is the perfect alternative to typing RANDOMIZE USR 0, all the time.

Next, with the main ROM in control, LOAD in one of the emulator tapes. Then, before switching to the emulator, type in and RUN Listing 5 or 6, depending on the emulator tape. This modifies the NMI routine as shown in Listing 7. (The Spectrum version is shown. The TS2068 version is the same except for a different CALL address.)

The CALL instruction runs the screen-copy routine in the ROM. As such, pressing the NMI switch will cause a copy of the screen to be sent to the TS2040 printer. But look at the program! I’ve modified the B register without first saving it somewhere, and if you trace through the COPY routine (0EAF in the Spectrum, 0A05 in the TS2068) you’ll see that it uses DI and EI. I’ve broken my own rules.

As a result, the programs you interrupt in this manner may act erratic or crash altogether, after the screen is printed out. Don’t worry, the pictures I’ve included show that it still works, and next time we’ll look at a more complex, but corrected version of the NMI screen copy. We’ve just run out of room, this time, but run the programs and see how bizzare things can get, if we don’t write our interrupt handlers properly.

By the way, Doug Dewey of the Triangle Sinclair User Group will be selling a version of his EMU emulator that contains a NMI SAVE routine I’ve written, as well as a version of my 64 column BASIC that will run under a Spectrum emulator. The NMI SAVE, along with the NMI switch we’ve been using, will stop a Spectrum program, and SAVE a “snapshot” of it to tape. When the tape is reloaded, the program will continue running at the point the “snapshot” was taken. The NMI SAVE is too long to include in this article, so I’ve given Doug the rights to duplicate and distribute it. Write to him for details.

As always, you can call me with questions, [phone number omitted], before 9:30pm Eastern time, or write me, Wes Brzozowski, [address omitted]. Please enclose a stamped, self addressed envelop for a reply.

screen copies - CRAZYBUGS; GLASS
screen copies – CRAZYBUGS; GLASS

LISTING 1

 10 REM RUN this program when
the TS2068 ROM is in
control. It will SAVE
the "TS2068 Emulator"
program to tape.
20 CLEAR 32767: FOR j=0 TO 163
83: POKE j+32768,PEEK j: NEXT j
30 SAVE "EMUL-2068" LINE 60
40 SAVE "emul-2068"CODE 32768,
16384
50 STOP
60 CLEAR 32767: LOAD ""CODE 32
768
70 OUT 244,192: RESTORE
80 FOR j=23296 TO 23307
90 READ k: POKE j,k
100 NEXT j
110 DATA 1,0,64,17,0,192,33,0,1
28,237,176,201
120 RANDOMIZE USR 23296: OUT 24
4,0
130 PAUSE 0: RANDOMIZE USR 0

LISTING 2

10 CLEAR 49151
20 OUT 244,192
30 POKE 49152+109,40
40 OUT 244,0

LISTING 3

0066  F5      PUSH  AF
0067 E5 PUSH HL
0068 2AB05C LD HL,(#5CB0)
006B 7C LD A,H
006C B5 OR L
006D 2001 JR NZ,#0070
006F E9 JP (HL)
0070 E1 POP HL
0071 F1 POP AF
0072 ED45 RETN

LISTING 4

 10 REM RUN this program when a
SPECTRUM EMULATOR is in
control. It will SAVE
the "SPECTRUM EMULATOR"
program to tape.
30 SAVE "EMUL-SPECT" LINE 60
40 SAVE "emul-SPECT"CODE 0,163
84
50 STOP
60 CLEAR 32767: LOAD ""CODE 32
768
70 OUT 244,192: RESTORE
80 FOR j=23296 TO 23307
90 READ k: POKE j,k
100 NEXT j
110 DATA 1,0,64,17,0,192,33,0,1
28,237,176,201
120 RANDOMIZE USR 23296
130 FOR j=49152 TO 49159
140 READ k: POKE j,k
150 NEXT j
160 DATA 243,175,17,255,255,195
,203,17
170 OUT 244,0
180 PAUSE 0: RANDOMIZE USR 0

LISTING 5

10 REM NMI Screen COPY-TS2068
20 CLEAR 49151: OUT 244,192
30 POKE 49152+104,6
31 POKE 49152+105,192
32 POKE 49152+106,205
33 POKE 49152+107,5
34 POKE 49152+108,10
35 POKE 49152+109,24
40 OUT 244,0

LISTING 6

10 REM NMI ScreenCOPY-Spectrum
20 CLEAR 49151: OUT 244,192
30 POKE 49152+104,6
31 POKE 49152+105,192
32 POKE 49152+106,205
33 POKE 49152+107,175
34 POKE 49152+108,14
35 POKE 49152+109,24
40 OUT 244,0

LISTING 7

0066  F5      PUSH  AF
0067 E5 PUSH HL
0068 06C0 LD B,#C0
006A CDAF0E CALL #0EAF
006D 1801 JR #0070
006F E9 JP (HL)
0070 E1 POP HL
0071 F1 POP AF
0072 ED45 RETN
screen copies - PSION Flight Simulation; PYJAMARAMA-About to be darted; PENETRATOR-About to buy the farm; TIR NA NOG
screen copies – PSION Flight Simulation; PYJAMARAMA-About to be darted; PENETRATOR-About to buy the farm; TIR NA NOG