DOWN_HL - Optimization
ZXNet echo conference «code.zx»
From Evgeny Goljakov → To All 21 December 2002
Hello All!
═══ Published in Nicron N125 on 12/20/2002 ═══
(c) E.B. Golyakov (Spencer Winset, Diamond group, Moscow)
500:95/462.8@ZXNet 2:5020/2065.608@Fidonet
Procedures for calculating the next and previous screen line.
Optimization.
This publication is a consequence of optimization of algorithms
calculation of the next and previous line of the screen and the desire to convey
new method to as many programmers as possible, because
that so far I have not encountered such modifications in modern
programs or competitive works from various computer
festivals. The author relies on the reader's knowledge of Z80 assembler
[2], as well as the Spectrum screen memory address device [3,4]
or compatible computers.
* * *
The original algorithm was reviewed by the author earlier [1],
therefore, I do not consider it necessary to pay attention to the description of the principles
his work, limiting itself to only a light historical excursion.
Since 1982, this method of calculating screen memory addresses has been
not widely used due to unknown or
the unpopularity of using ROM routines on the Spectrum.
In ROM memory (ZX Spectrum 48/128 (c) Sinclair Research Ltd)
the function is located at addresses 3769-3784 in in-line form.
(editor's note: in-line is a structural term implyingembedding of a function in the body of the program in a certain place,
excluding its call using the CALL command).
Below is a comparison of the original (A) with the improved
and still widely used procedure DOWN_HL (B),
whose authorship could not be established.
A. (Logic_HL) B. (DOWN_HL)
HEX │ OP CODE String HEX │ OP CODE
_____│________________ _____│________________
24 │ INC H 1 24 │ INC H
7C │ LD A,H 2 7C │ LD A,H
E607 │ AND #07 3 E607 │ AND #07
200A │ JR NZ,BREAK 4 200A │ JR NZ,BREAK
7D │ LD A,L 5 7D │ LD A,L
C620 │ ADD A,#20 6 C620 │ ADD A,#20
6F │ LD L,A 7 6F │ LD L,A
3F │ CCF 8 3804 │ JR C,BREAK
9F │ SBC A,A 9 7C │ LD A,H
E6F8 │ AND #F8 10 D608 │ SUB #08
84 │ ADD A,H 11 67 │ LD H,A
67 │ LD H,A 12 BREAK ...
BREAK...
_____________________ _____________________
16b - 27/60t 16b - 27/49/59t
It can be seen that with the same length in example (B) it was possible to isolate
L register overflow event, indicating a transition to anothera third of the screen (line 8), while the execution time succeeds
cut from 60 to 49 bars, this can be considered a success, but
the presence of only two such lines on the screen significantly reduces
probability of savings. Calculation time has also been reduced
moving to the next position for 1 measure; it's not significant though
and is true for every eighth line of the screen (excluding two,
mentioned above).
We can say that just one tact decided the fate of the first
algorithm that was simply forgotten, despite all the
floridness of logical structures; hence the name Logic.
Analyzing the publication [1], it was possible to establish that the CCF command
(Complement Carry Flag), which changes the state of the CY carry flag
to the opposite (Line 8), the application of which is here
means that the action performed earlier (Line 6) affected
the flag is exactly the opposite. However, simple addition can be
replaced by subtracting the complement number, and the CY flag will be
accordingly inverted automatically. This is mentioned in
article by I. Roshchin [5]. Replacing the ADD A,#20 command with SUB #E0,
we find that the arithmetic operations are equivalent, and the CCF command
no longer needed. Below is the result obtained - new
procedure DOWN_HL+, compared with the previously optimized one
DOWN_HL:
B. (DOWN_HL) B. (DOWN_HL+)HEX │ OP CODE LINE HEX │ OP CODE
_____│________________ ______│_______________
24 │ INC H 01 24 │ INC H
7C │ LD A,H 02 7C │ LD A,H
E607 │ AND #07 03 E607 │ AND #07
200A │ JR NZ,BREAK 04 2009 │ JR NZ,BREAK
7D │ LD A,L 05 7D │ LD A,L
C620 │ ADD A,#20 06 C620 │ SUB #E0
6F │ LD L,A 07 6F │ LD L,A
3804 │ JR C,BREAK 08 9F │ SBC A,A
7C │ LD A,H 09 E6F8 │ AND #F8
D608 │ SUB #08 10 84 │ ADD A,H
67 │ LD H,A 11 67 │ LD H,A
BREAK ... BREAK
_____________________ _____________________
16b - 27/49/59h 15b - 27/56h
Either way, it doesn't matter if you're stuck in a bowl
на 1 month; Let's get a smile on our face - это
более 6%. We received a 3 months ago
каждой восьмой линии экрана. The price of the snowflake
расчетa (в тактах) для всех линий экрана, считая первую линию
Pictures, descriptions:
Size (А) 27*167+60*24=
(Б) 27*167+49*2+59*22= 5905 (-44)
(B) 27*167+56*24=5853(-96)The speed advantage over procedure (A) was: for
(B) 44 cycles, and for (C) 96 cycles, which is 2.2 times more. Yes,
when displaying 50 sprites of 40 lines each, the resulting savings
not 450, but already 1000 cycles.
* * *
The following are the procedures for calculating the overlying line,
popular UP_HL (G) and similarly optimized UP_HL+ (D), with
identical optimization indicators described above:
G. (UP_HL) D. (UP_HL+)
HEX │ OP CODE LINE HEX │ OP CODE
_____│________________ ______│_______________
7C │ LD A,H 01 7C │ LD A,H
25 │ DEC H 02 25 │ DEC H
E607 │ AND #07 03 E607 │ AND #07
200A │ JR NZ,BREAK 04 2009 │ JR NZ,BREAK
7D │ LD A,L 05 7D │ LD A,L
D620 │ SUB #20 06 C6E0 │ ADD A,#E0
6F │ LD L,A 07 6F │ LD L,A
3804 │ JR C,BREAK 08 9F │ SBC A,A
7C │ LD A,H 09 E608 │ AND #08
D608 │ SUB #08 10 84 │ ADD A,H
67 │ LD H,A 11 67 │ LD H,A
BREAK...BREAK...
_____________________ _____________________
16b - 27/49/59t 15b - 27/56tThe author expresses the hope that the material presented will be useful
both the creators of 256/512 bytes intro and the authors of large-scale
gaming or demo projects, where the first place is not
the number of effects fit into half a kilobyte, and the beauty
visualization, coupled with the elegance and sharpness of the code.
Literature
_______________________________________________________________
1. Golyakov E.B. Auxiliary operations when displaying a sprite in
screen memory area. - "Nicron", 1998, N119.
2. Personal computer "ZX-SPECTRUM". Programming in
machine codes and in ASSEMBLY language: In 3 hours - M., Inforcom,
1993.
3. ZX SPECTRUM graphics. -M., VA PRINT, 1994. pp. 168-171.
4. Hardman J., Huson E. 40 best procedures. - "ZX-REVIEW", 1992,
N1,2.
5. Roshchin I. More about programming arithmetic operations.
- "Amateur Radio. Your Computer", 2000, N12, 2001, N1-4.
═════════
Thank you for your attention.