Deja Vu #05: Techniques for Stack Use with Interrupts on ZX Spectrum

Article authors

Mentioned teams/groups

CRG
SoundTrack: SECTOR/SERIOUS S.G.'97 (CMG98)
__________________________________________

(C) RLA/CRG
__________________________________________

STACK USAGE
WITH ENABLED INTERRUPTS

It is no secret that stack manipulation commands are widely used for fast graphics operations in games and, especially, in demos. It is with their help that the highest speed of graphics processing can be achieved. However, the more impressive the effect, the more time it takes. There may be cases when the effect does not complete its 69888 cycles and an interrupt occurs. The return address is pushed onto the stack and irreversibly corrupts the information contained beneath it. But all is not lost. Let’s consider a few cases on how to preserve this data and work with the stack with second type interrupts enabled.

1. If the screen is cleared or filled with any data, it is not so critical, since upon returning from the interrupt, the PUSH HL cycle will continue and the return address will be overwritten with the necessary data.

1.1 It is possible that the stack will equal 4000h and the return address will be lost, but this case can be controlled if you know the addresses of those points where this can happen, and jump there not by RET, but by JP ADDRESS.

Example:
LD SP,#4020 ;clear the very
LD HL,#0000 ;top line
PUSH HL ;in the screen
PUSH HL
PUSH HL
... *16 times
PUSH HL
PUSH HL
ADR1 LD SP,(SAVESP)
RET

The last case is possible if the interrupt interrupts between the PUSH HL and LD SP,(SAVESP) commands. The handler for 2 (!) interrupts in this case would look like this:

ORG #BFBF
INT2 DI
LD (TOSP+1),SP
LD SP,INT2-1
PUSH HL
PUSH DE
PUSH BC
PUSH AF
PUSH IX
EXX
EX AF,AF
PUSH HL
PUSH DE
PUSH BC
PUSH AF
PUSH IY

LD HL,(TOSP+1)
LD DE,-#4000
ADD HL,DE
LD A,#C9 ; RET
JR C,OK
LD A,#C3 ; JP
LD (TO_RET),A

... interrupt processing
POP IY
POP AF
POP BC
POP DE
POP HL
EXX
EX AF,AF
POP IX
POP AF
POP BC
POP DE
POP HL
EI
TOSP LD SP,0
TO_RET DB 0
DW ADR1

2. There is another tricky case when data is not written to the stack but read from it. Most often this is used when a large part of the screen is moving (TRASH DEMO, ART'98, various game intros). A large sprite is created in memory beforehand and displayed on the screen with a byte chain like this:

POP HL
LD (#4000),HL
POP HL
LD (#4002),HL
...
POP HL
LD (#57FE),HL
LD SP,(SAVESP)
RET

In the provided programs, part of the screen manages to transfer during one interrupt, and this problem does not arise, but there have been cases when one effect works, and then a window with wishes or GTX'es needs to be overlaid. If an interrupt occurs, the return address will again be pushed onto the stack and the sprite will be "irreparably" corrupted. It was with such a case that one of my coder friends approached me. After some time, the problem was solved. His effects worked with interrupts enabled, and the graphics were not corrupted.

Handler:

ORG #BFBF
INT2 DI
LD (TOSP+1),SP
LD SP,INT2-1
PUSH HL
PUSH DE
PUSH BC
PUSH AF
PUSH IX
EXX
EX AF,AF
PUSH HL
PUSH DE
PUSH BC
PUSH AF
PUSH IY
LD HL,(TOSP+1)
LD E,(HL)
INC HL
LD D,(HL)
LD (_JP+1),DE
... interrupt processing
POP IY
POP AF
POP BC
POP DE
POP HL
EXX
EX AF,AF
POP IX
POP AF
POP BC
POP DE
POP HL
TOSP LD SP,0
PUSH HL ;Writing
POP HL ;Stack correction
EI
_JP JP 0

Let’s consider possible cases when an interrupt may occur:

a) between the commands POP HL and LD (NN),HL the read bytes are in registers HL and will be saved by the command PUSH HL before returning.

b) between the commands LD (NN),HL and POP HL the data is again in HL and will be restored in the same way. It is worth noting that the return from this handler will always be made by a direct jump. I should mention that I have not encountered such workaround tricks with the stack in demos yet. I hope this article will reduce the workload of some of your effects by an extra 10-20 thousand cycles, which so hindered making your effect faster in the intro.

Contents of the publication: Deja Vu #05

  • Аперативчик - Max
    Detailed instructions on managing the DEJA VU interface, highlighting different input methods and navigation commands. Explanation of the new and old interfaces for enhanced user experience. Discussion on additional features like frame scrolling and music management.
  • Аперативчик - Max
    Discussion on supporting machines with more than 128k memory, leading to separate shells for 128k and 256k systems. Testing was mainly done on Scorpion and Profi, with functionality on other models anticipated. Article includes guidance on unpacking source files and insights on using improved algorithms.
  • Тема - M.M.A
    This article explores the theory behind digitizing sound on ZX Spectrum, focusing on sampling and quantization processes. It provides practical insights into converting sound files using specific hardware and software. Additionally, it offers methods to enhance sound quality while working within the hardware limitations.
  • Theme
    The article discusses the Save Our Scene initiative aimed at uniting Spectrum users and developers to promote software distribution and enhance the scene's development.
  • Charter of the Amazing Soft Making Association
    Discussion of the founding charter of the Amazing Soft Making association, detailing its goals, membership criteria, and operational principles.
  • Theory of Magazine Creation
    The article provides a detailed guide for aspiring magazine creators, focusing on technical aspects such as interface design, memory management, text formatting, and music integration for ZX Spectrum publications.
  • Solder Drop
    The article provides a personal account of purchasing and using the General Sound device for ZX Spectrum, detailing installation and sound performance. It discusses the initial issues encountered and praises the enhanced audio experience in compatible games. The author encourages further software adaptation for the device and reflects on multimedia capabilities with simultaneous hardware use.
  • Solder Drop
    The article discusses the capabilities of Sound Forge 4.0c for professional audio processing on PCs, highlighting its extensive features such as sound editing, effects, and restoration tools.
  • SOFTWARE
    The article reviews the latest software developments for the ZX Spectrum from Samara, including updates to MAXSOFT SCREEN PACKER, File Commander, and new applications like S-Terminal.
  • SOFTWARE - Card!nal
    Review and walkthrough of the logical graphic adventure game 'Operation R.R.' with detailed level instructions. Discussion on game elements like music choice and graphic design. Mentions new coder MAX/CYBERAX/BINARY DIMENSION's involvement.
  • SOFTWARE
    Discussion on the current state and evolution of the demoscene, highlighting the rise of 4K intros and upcoming competitions like FUNTOP'98.
  • CODING
    Article discusses assembly language coding techniques for optimizing screen scrolling on ZX Spectrum, featuring example code and performance analysis.
  • CODING - RLA
    The article explores stack manipulation techniques during second type interrupts for graphical effects on ZX Spectrum. It discusses solutions for preserving data integrity when interrupts disrupt graphical operations. Practical examples are provided to handle stack issues efficiently.
  • CODING
    The article describes the MS-PACK packer and its DEPACKER, detailing usage scenarios and providing BASIC and assembly code examples for handling packed files. It emphasizes optimizing performance by allowing unpacking with interrupts enabled and separating the DEPACKER from packed files. Additionally, it includes insights on programming techniques for loading and executing BASIC files on ZX Spectrum.
  • CODING
    The article discusses various coding techniques for ZX Spectrum, focusing on sprite rendering, rotation algorithms, and optimization methods to enhance performance.
  • ANOTHER WORLD
    Discussion on the evolution of multimedia technologies and their impact on various fields, including education and entertainment. It covers advances in computer hardware and software that have facilitated the integration of audio, video, and text. The article reflects on past developments and speculates on the future of multimedia systems.
  • ANOTHER WORLD
    Comparison of PC and Amiga systems highlighting performance, software costs, and user experience with multimedia capabilities.
  • Honor Roll
    Interview with PROGRESS discusses their creative journey on ZX Spectrum and AMIGA, addressing challenges in demomaking and the current state of the scene.
  • Honor Roll
    The article details the activities and future projects of the Eternity Industry team, based in Kovrov, including successful releases and collaborations with other groups.
  • Honor Roll
    Discussion of the Artcomp'98 festival, focusing on its mail-in format and guidelines for various competitions, including demo, graphics, and music categories.
  • Honor Roll
    The article provides a glossary of terms used in the demo scene, explaining roles such as musician, coder, and graphician, as well as different types of demos and effects. It serves as a useful resource for understanding the terminology and dynamics of the community. This is a descriptive piece aimed at educating readers about the jargon of the demo scene.
  • Honor Roll
    The article discusses the issues with mouse support in various ZX Spectrum magazines and the frustrations of users when encountering compatibility problems. It critiques developers for not adhering to standards, leading to poor user experiences. The author expresses the importance of consistent improvements in software for the ZX Spectrum community.
  • Honor Board
    The article discusses the process of creating tricolor images for ZX Spectrum using Photoshop and a simplified approach. It outlines how to divide an image into RGB channels and convert them for use on the Spectrum. Additionally, it provides tips on how to manage the files for optimal results.
  • Honor Roll
    The article discusses the comparison and perspectives on various computer systems, particularly emphasizing the strengths of AMIGA over PC and advocating for appreciation of all machines.
  • Seven and a Half
    Article discusses the humorous absurdities and peculiarities of military training and academia, blending satire with real anecdotes and witty observations.
  • Seven and a Half
    The article provides a satirical manual on programming methodologies, mocking the rigidity of formal programming practices and advocating for a more creative approach to coding.
  • Seven and a Half
    Instructions on safe sex practices, including guidelines on eligibility, preparation, and actions during and after the sexual session, along with handling emergency situations.
  • Seven and a Half
    The article discusses a call for a talented artist in Krasnodar for a ZX Spectrum group, raises concerns about the unethical practices of Scorpion regarding software rights, and critiques a video review of E'97.
  • Seven and a Half
    The article 'Семь и 1/2' narrates a humorous picnic adventure involving the editorial team of Deja Vu, highlighting their camaraderie and mishaps while preparing a barbecue.
  • Trial of the Pen
    The article is a humorous take on the fictional adventures of Winnie the Pooh as he interacts with computers and friends, discussing the absurdities of technology and daily life.
  • First Pen
    The article discusses the new section in Deja Vu dedicated to fantasy and science fiction literature, featuring book reviews and reader participation in content creation.
  • Advertisement
    The article is an advertisement section from Deja Vu #05, promoting collaborations with designers and musicians for future issues, and offering various software and hardware for ZX Spectrum.
  • News
    The article announces the launch of a new magazine, AMIGA RULES, focused on the AMIGA computer, addressing the lack of quality Russian-language publications. It aims to provide information on programming, hardware, software, and gaming, while fostering a community among AMIGA enthusiasts. The magazine will include contributions from readers and regular updates on the AMIGA scene.