29.12.2012 Views

The Programmer's Guide to TRSDOS Version 6 - Tim Mann's Home ...

The Programmer's Guide to TRSDOS Version 6 - Tim Mann's Home ...

The Programmer's Guide to TRSDOS Version 6 - Tim Mann's Home ...

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>The</strong> more complex bank function is function 0. This request is used <strong>to</strong> actually exchange<br />

the current bank with the specified bank. A very important point <strong>to</strong> remember here is that<br />

since a memory transfer will take place in the address range X'8000' <strong>to</strong> X'FFFF', the<br />

transfer cannot proceed correctly if the stack pointer (SP) contains a value that places<br />

the stack in that range. In fact, @BANK will inhibit function 0 and return an SVC<br />

parameter error if the stack pointer violates the condition.<br />

A bank can be used purely as a data s<strong>to</strong>rage buffer. Most likely, the application's<br />

routines for invoking and indexing the bank switching will reside in the user range<br />

X'3000' through X'7FFF' (or possibly in the I/O driver range). As an example<br />

illustration, the following code will invoke a previously tested and reserved bank (via<br />

functions 2 and 3), access the buffer, and then res<strong>to</strong>re the previous bank:<br />

LD C,1 ;Specify bank-1<br />

LD B,0 ;Bring up bank<br />

LD A,@BANK ;Identify the SVC<br />

RST 40<br />

JR NZ,ERROR ;Whatever error trap<br />

PUSH BC ;Save old bank data<br />

.<br />

your code <strong>to</strong> access the buffer region<br />

.<br />

POP BC ;Recover old bank data<br />

LD A,@BANK ;Identify the SVC<br />

RST 40<br />

JR NZ,ERROR ;Whatever error trap<br />

Note that the @BANK function 0 conveniently returns a zero in register B <strong>to</strong> effect a<br />

function 0 later, as well as provides the old bank number in register C. This means that<br />

you only have <strong>to</strong> save register pair BC, pop it when you want <strong>to</strong> res<strong>to</strong>re the previous<br />

bank, and then issue the @BANK SVC.<br />

Say you have a need <strong>to</strong> transfer <strong>to</strong> another bank from a routine that is executing in high<br />

memory. Can this be done? Notice that the only limitation discussed was that the stack<br />

must not be in high memory. <strong>The</strong> @BANK SVC function 0 does provide a technique for<br />

au<strong>to</strong>matically transferring <strong>to</strong> an address in the new bank. This technique is termed the<br />

transfer function. It relies on the assumption that since you are managing the 32K bank,<br />

your application should know exactly where it needs <strong>to</strong> transfer (i.e. where the<br />

application originally placed the code <strong>to</strong> execute). <strong>The</strong> code <strong>to</strong> perform a bank transfer<br />

is similar <strong>to</strong> the above. Register pair HL must be loaded with the transfer address,<br />

Register C, which contains the bank number <strong>to</strong> invoke, must have its high order (bit-7)<br />

set <strong>to</strong> indicate the TRANSFER activity. After the specified bank is enabled, control is<br />

passed <strong>to</strong> the transfer address that was in HL. Upon entry <strong>to</strong> your routine in the new bank<br />

(we will refer <strong>to</strong> it as "PROGRAM-B"), register HL will contain the old RETurn address so<br />

that PROGRAM-B will know where <strong>to</strong> return <strong>to</strong> when it transfers. Register C will also<br />

contain the old bank number with bit-7 set and register B will contain a zero. This<br />

register setup will provide for an easy return <strong>to</strong> the routine in the old bank that<br />

invoked the bank transfer. An illustration of the transfer code is as follows:<br />

LD C,1 ;Specify bank-1<br />

LD B,0 ;Bring up bank 0<br />

LD HL,(TRAADR) ;Set the transfer address<br />

SET 7,C ; & denote a transfer<br />

LD A,@BANK ;Identify the SVC<br />

RST 40<br />

RETADR JR NZ,ERROR<br />

Control will be returned <strong>to</strong> "RETADR" under either of two conditions. If there was an<br />

error in executing the bank transfer (for instance an invalid bank number or the stack<br />

pointer being in high memory), the returned condition will be NZ. If the transfer <strong>to</strong>ok<br />

place and PROGRAM-B transferred back, the returned condition will always have the Z-flag<br />

8-23

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!