0% found this document useful (0 votes)
15 views1 page

Uk 1985010401

Elektor Articles

Uploaded by

HecOs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views1 page

Uk 1985010401

Elektor Articles

Uploaded by

HecOs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

JSR SWAP

elektor january 1985


Whenever two (or more) large programs must exist at the same time
in a 6502's memory there is bound to be a conflict as regards page 0
and the stack (page 1). The situation could arise if a BASIC
interpreter and a DOS (disk operating system), or both of these and a
video handler, are being used simultaneously. One of the accepted
methods of solving this problem involves reserving two areas in
random access memory where these pages are 'duplicated', giving,
for example, E000...EOFF for page 0 and E100...E1FF for the stack.
Every time the computer changes from one program to the other the
contents of these areas of RAM are swapped with the appropriate
contents of pages 0 and 1. This removes any possibility of corrupting
the pointers on page zero or the contents of the stack.

software to
JSR SWAP
One of the notable characteristics of the
6502 microprocessor is the way it uses
change of the parameters saved on these
two pages will upset the operation of a
interchange pages 0 and 1. The 256 bytes from program that is being run — usually with
pages 0 and 1 0000HEX to OOFFHEX can be addressed no possibility of correcting the error.
using commands specific to this zone. When two programs are run in parallel it
in a 6502-based This is known as page zero addressing; is essential that they do not destroy each
system the most significant address byte is not other's page 0 and 1 parameters. This is
specified as it is implicit in the operation an extra worry for the programmer, par-
code. The same 256 bytes can be used as ticularly as it can be an insoluble prob-
16-bit pointers for indirect indexed lem. As soon as the programs that are
addressing of the rest of the memory. The being run reach a certain size it is better
Table 1. The processor 256 bytes from 0100HEX to 01FFHEX form to find a way to break away from the
leaves the SWAP routine the 6502's stack. This is a register shackles governing the use of pages 0
(which it entered using
JSR) not by RTS but by
generated by the processor itself to and 1.
JSR! The return address enable it to store certain information. It The routine proposed here is used to
is 'stacked' at the start of operates on the principle of 'last in first move the contents of page zero and page
the routine, then cor- out' so the processor can only work with one to another area of RAM where they
rected (return address =
the last item stored on the stack. An can be changed at will. At the same time
address left + 1) and
then placed after the
internal stack pointer continually indicates the contents of the RAM area in question
jump instruction ident- the address of this last item. is transferred to pages 0 and 1. The name
ified by the label It is obvious that the slightest careless of the routine is SWAP, for obvious
JMPINS. reasons.
040:
Using this routine means that the program-
...***
050: *SWAP* mer no longer has to worry about the con-
060: A)0EX.*
070:
tents of pages zero and one when leaving
080: one program to carry out another. All he
090: SWAP PAGE 0 AND PAGE 1 WITH E000 AND E100
l00: has to do is run the SWAP routine. Page
110: zero (0000HEX...00FFHEx) and page one
120:
130: E200 PZ $0000 PAGE ZERO (0100HEX...01FFHEX) of the first program
140: E200 STACK $0100 STACK AREA
150: E200 SPZ EE $E000 SWAPPED PAGE ZERO are saved at E000HEX...E1FFHEX, and
160: E200 SSTACK * $E100 SWAPPED STACK AREA the contents of pages 0 and 1 for the sec-
170:
180: ond program, which had been stored at
190:
200: E200 18 SWAP CLC
E000HEX...EIFFHEX, are transferred to
210: E201 68 PLA 0000HEX...01FFHEX. When returning
228: E202 69 01 ADCIM $81 PUT RETURN ADDRESS
230: E204 8D 2C E2 STA JMPINS .01 JUST BEHIND A JUMP OP-CODE from the second to the first program the
240:
258:
E207
E208
68
AA
PLA
TAX
SWAP routine is again executed and the
260: E209 90 81 BCC SW same procedure is carried out again to
270: E208 E8 INX return the two pairs of pages zero and
280:
290: E200 BE 2D E2 SW STX JMPINS +02 one to their original locations. The lo-
300: E2OF A2 00 LDXIM $00 RESET INDEX
310: cations we have used to store pages 0 and
320: E211 BD 00 01 SWAPST LDAX STACK GET BYTE FROM PAGE 1 1 (E000HEX...E1FFHEX) can, of course,
330: E2I4 BC 00 El LDYX SSTACK GET BYTE FROM SWAP AREA
348: E217 9D 00 El STAX SSTACK SAVE BYTE FROM PAGE 1 IN SWAP AREA be changed to suit the system with which
350: E21A 98 TYA the SWAP routine is used, provided the
360: E2113 9D 00 81 STAX STACK SAVE BYTE FROM SWAP AREA IN PAGE 1
370: E2IE 85 00 LDAX PZ GET BYTE FROM PAGE 8 area reserved is in random access
380: E220 BC 00 E0 LDYX SPZ GET BYTE FROM SWAP- AREA
398: E223 90 00 E8 STAX SPZ SAVE BYTE FROM PAGE 0 IN SWAP AREA
memory. Similarly the SWAP routine itself
400: E226 94 00 STYZX PZ SAVE BYTE FROM SWAP AREA IN PAGE 8 must be run in RAM. A look at the last
410: E228 EB INX BUMP THE OFFSET
428: E229 DO E6 BNE SWAPST NOT DONE, KEEP ON line of the listing will show why this is
430:
448: E228 4C FF FF JMPINS JMP $FFFF SELF MODIFYING CODE'
necessary. Indexing and swapping are the
450: two procedures that make this routine
possible so bear this in mind.

1-40

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy