Peeking the Option ROM From a BASIC Program
The question came up on Facebook as to whether you can read the contents of an Option ROM from BASIC. The short answer is not really, or at least, not without some work.
Why? The 8085 has a 65536 byte address space. That space is apportioned between ROM at the bottom and RAM at the top. The 32K Option ROM, when enabled, occupies the same address space as the 32K main ROM. When the Option ROM is running, the BASIC ROM isn’t, and vice versa,, because they all their jumps, internal data references and interrupt handlers are located to execute in the same space: the lower 32,768 bytes of the 8085 CPU’s address space. The upper 32K of the address space is permanently devoted to the RAM.
OK, well, BASIC has an OUT I/O routine and BASIC has a PEEK routine. To switch to the option ROM, you OUT 1 to I/O register 224. To switch back you OUT 0 to I/O register 224. So knowing just enough to get ourselves in trouble, a naive approach to PEEKing the option ROM would be:
OUT 224,1:?PEEK(0): OUT224,0
to print the first byte of the Option ROM, and because I'm almost clever, I of course switch back to the Main ROM at the end of it like a good BASIC citizen.
Workey? No. No workey.
The problem is that the BASIC interpreter will abruptly disappear with OUT 224,1. Boom. Poof. Gone. So the BASIC interpreter loop that executes your BASIC program line by line, token by token, will never get to anything after the first OUT... it will instead lock-up or, perhaps, crash spectacularly.
Because of that, I don't think there's any way to PEEK the option ROM from pure BASIC. But rather than let that hurdle stop us, instead, we need to contrive some machine code with no ROM dependencies and embed it in our BASIC program. This code will run completely from RAM, which always runs from the same place, so it can swap the standard ROM out and the Option ROM in without causing a crash.
So we’re writing a machine language (ML) subroutine. We can interface ML with BASIC. BASIC gives us a way to invoke a ML subroutine, the “CALL” function. We will design a routine we can CALL, passing along the address of the Option ROM location we want, and get back the byte value that is at that offset in the Option ROM.
How does CALL work? The CALL statement takes three parameters: a subroutine entry point, an 8-bit parameter, and a 16-bit parameter. The subroutine is called, and the parameters are passed in the A and HL registers, respectively. At the end of the subroutine, CALL’ed code must effectuate return to the BASIC interpreter, leaving the stack as it found it!.
Clearly, we need to pass the address to PEEK. The 16-bit parameter could be good for that. We don’t have any use for an 8-bit input to our subroutine. But what about passing back the byte "PEEKed"? That's an "out" parameter. It sure would have been great if Microsoft had made CALL return a 8 or 16-bit value.
But it is not so, and the BASIC ROM is literally etched in stone for many of our machines.
Oh well. No point in crying into our cereal about it... we just need to put our thinking cap back on. What we can do instead is pass a parameter array of integers as our 16-bit value. That is, instead of passing one value, we pass a value that refers (a reference) to a modifiable object.
A single array reference can convey an arbitrary number of values and pointers. And since we're passing a reference to the array object, the variables at each index of the array can be modified by the ML routine.
DIM P%(2) ' P%(0): return value P%(1) = 1000 : ' Address in Option ROM to peek
So assuming our subroutine is at address PK% (we’ll get to that later), an invocation of our subroutine to find out what’s at address 1000 of the Option ROM would look like:
P%(1) = 1000: CALL PK%, VARPTR(P%(0)): ?P%(0)
So now that we know what we want the routine to do, and how we will invoke it, let's code it.
For each line I will show the assembly instruction, as well as the hex and what I call Funny Hex for each instruction. Funny Hex is like hex but ABCDEF are represented by :;<=>? , for simplified conversion. Any character in Funny Hex, you can subtract 48 (ASCII encoding of zero) and it will give you a value between 0 and 15
First I need to get resolve a level of indirection on the value of HL to load the “address” parameter into the HL register.
; HL starts as the address of the integer parameter array, in C-speak, ¶m[0] ; Load HL with param[1] since it's the address to peek XCHG ; $EB >; INX D ; $13 13 ; uh oh, need a listable alternative INX D ; $13 13 ; again LHLI ; $ED >=
That was my first cut at that... and it would work, but $13 is an unprintable ASCII control character (^S / DC3/ XOFF). We have an opportunity here to craft code that can be embedded directly into a BASIC string, execute in place, and have no LIST or EDIT issues. That makes for much shorter BASIC code, with no "poke loop" overhead and no need to run from ALT-LCD. To do that, we need all instructions (and operands!) we use to be 32 (printable space) or higher.
So to avoid problems we instead:
PUSH H ; $E5 >5 INX H ; $23 23 INX H ; $23 23 XCHG ; $EB >; LHLI ; $ED >=
; we're going to set the 224 ($E0) register to 1 to switch in the option rom XRA A ; $AF :? INR A ; $3C 3< ; disable interrupts so the hardware won't interrupt us. I’m not sure if this is actually necessary, since Option ROMs have their own interrupt handlers. DI ; $F3 ?3 ; Switch in the option ROM OUT 224 ; $D3 $E0 / =3 >0 ; L <- option ROM byte referenced by HL MOV L,M ; $6E 6> ; Switch back to the standard ROM (same I/O register as we used to enable, but with A=0 instead of A=1) XRA A ; $AF :? OUT 224 ; $D3 $E0 / =3 >0 ; restore interrupts EI ; $FB ?; ; load the peeked value into HL (high byte is always zero) XRA A ; $AF :? MOV H,A ; $67 67 ; Address of parameter array (references the first parameter) is on stack, restore it POP D ; $D1 =1 ; pass the peeked value as the return value SHLI ; $D9 =9 ; return to the BASIC interpreter RET ; $C9 <9
The full assembly:
PUSH H ; $E5 >5 INX H ; $23 23 INX H ; $23 23 XCHG ; $EB >; LHLI ; $ED >= XRA A ; $AF :? INR A ; $3C 3< DI ; $F3 ?3 OUT 224 ; $D3 $E0 / =3 >0 MOV L,M ; $6E 6> XRA A ; $AF :? OUT 224 ; $D3 $E0 / =3 >0 EI ; $FB ?; XRA A ; $AF :? MOV H,A ; $67 67 POP D ; $D1 =1 SHLI ; $D9 =9 RET ; $C9 <9
That works out to 20 bytes of machine code.
Here’s a 7-bit ASCII encoded BASIC program that modifies an embedded string to hold the subroutine. Run it once, and it will do the work of converting the Funny Hex into a safe binary string.