Nicron #125
19 декабря 2002

Programming - The procedure for calculating the next and previous line of the screen. Optimization.

<b>Programming</b> - The procedure for calculating the next and previous line of the screen. Optimization.
(C) EB Golyakov
(Spencer Winset, Diamond group, Moscow)
500:95 / 462.8 @ ZXNet
2:5020 / 2065.608 @ Fidonet



     Procedure for calculating the next and previous lines of 
the screen. 

                        Optimization.


  This publication is the result of optimization algorithms
calculating the next and previous screen line and the desire to 
convey A new method to as many programmers because

so far I have not met with similar modifications in the current
programs or competitive works with various computer
Festivals. The author is counting on the reader's knowledge 
assembler Z80 [2], and also address device memory view Spectrum 
[3,4] or compatible computers.


            *


  The original algorithm has been considered previously by the 
author [1] so I do not think it necessary to pay attention to 
the description of the principles of his work, limited to only 
a slight historical narratives. Since 1982, this method of 
calculating the memory address the on-screen and not widely 
used, due to uncertainty or unpopularity of Subprogramme ROM on 
the Spectrum. 


  In Memory ROM (ZX Spectrum 48/128 (c) Sinclair Research Ltd)
function raspolagaetsyasya in addresses 3769-3784 in the 
in-line form. (Note edition: in-line - a structural term, 
implying embeddedness functions in the body of the program in a 
certain place, excluding its challenge to the team CALL).



  Below is a comparison of the original (A) with advanced
and still widely used procedure DOWN_HL (B)
authorship of which could not be established.



     A. (Logic_HL) BA (DOWN_HL)


    HEX OP CODE string HEX OP CODE

    _____ ________________ _____ ________________

    24 INC H INC H January 1924

    7C LD A, H 2 7C LD A, H

    E607 AND # 3 July 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 June C620 ADD A, # 20

    6F LD L, A 7 6F LD L, A

    3F CCF August 3804 JR C, BREAK

    9F SBC A, A 9 7C LD A, H

    E6F8 AND # F8 10 D608 SUB # 08

    84 ADD A, H November 1967 LD H, A

    67 LD H, A 1912 BREAK ...

          BREAK ...

    _____________________ _____________________

     16b - 27/60t 16b - 27/49/59t



  It is seen that for the same length in the sample (B) was 
isolated overflow event register L, signifying the transition 
to another third of the screen (line 8), and the execution time 
can reduced from 60 to 49 cycles, this can be considered a 
success, but presence on the screen only two of these lines 
significantly reduces the the probability of cost savings. Just 
shorten the calculation transition to the next position on a 
clock, it is not essential, although and is valid for every 
eighth line of the screen (except for two mentioned above).



  We can say that just one stroke decided the fate of the first
algorithm, which was simply forgotten, in spite of all
ornateness logical structures; hence the name Logic.
Analyzing pulikatsiyu [1], it was found that the CCF team
(Complement Carry Flag), amending the state carry flag CY
the opposite (Line 8), whose application here
means that the action taken earlier (Line 6) have affected
flag is exactly the opposite. However, simple addition can be
replaced by subtracting the number of completed and CY flag will
respectively inverted automatically. Mentioned in
Article VI Roshchina [5]. Replacing the command ADD A, # 20 on 
SUB # E0, we find that the arithmetic ekvivalentny, and the 
team CCF no longer needed. Below is the result - a new

procedure DOWN_HL +, the associated with the previously 
optimized DOWN_HL:




      BA (DOWN_HL) V. (DOWN_HL +)


    HEX OP CODE LINE HEX OP CODE

    _____ ________________ ______ _______________

    24 INC H INC H January 1924

    7C LD A, H 02 7C LD A, H

    E607 AND # July 2003 E607 AND # 07

    200A JR NZ, BREAK April 2009 JR NZ, BREAK

    7D LD A, L 05 7D LD A, L

    C620 ADD A, # 20 June 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 2009 E6F8 AND # F8

    D608 SUB # 10 August 1984 ADD A, H

    67 LD H, A November 1967 LD H, A

          BREAK ... BREAK ...

    _____________________ _____________________

     16b - 27/49/59t 15b - 27/56t



  Of course, success here can be regarded as reducing the code 
length 1 byte for such a relatively small size of bytes - it is

more than 6%. Also achieved gains in speed for 3 cycles for
every eighth line of the screen. Comparing the execution speed
rascheta (in cycles) for all lines of the screen, assuming the 
first line given, we obtain:



      Procedure (A) 27 * 167 +60 * 24 = 5949

                (B) 27 * 167 +49 * 2 +59 * 22 = 5905 (-44)

                (B) 27 * 167 56 * 24 = 5853 (-96)


  Speed ​​advantage of the procedure (A) was as follows: for
(B) 44 cycles, and for (B) 96 cycles, which is 2.2 times 
greater. Thus, both onscreen sprites 50 to 40 lines, derived 
ekonoiya not 450, but already 1000 cycles.


            *


  The following are procedures for calculation of the overlying 
line Popular UP_HL (F) and similarly optimized UP_HL + (D), with

identical performance optimimizatsii 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 DEC H February 1925

    E607 AND # July 2003 E607 AND # 07

    200A JR NZ, BREAK April 2009 JR NZ, BREAK

    7D LD A, L 05 7D LD A, L

    D620 SUB # 20 June 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 # 10 August 1984 ADD A, H

    67 LD H, A November 1967 LD H, A

          BREAK ... BREAK ...

    _____________________ _____________________

     16b - 27/49/59t 15b - 27/56t



  The author hopes that the material described will be useful
as creators 256/512 byte intro, and the authors of large-scale
game or demo projects, where the first place there is no
number of effects, umeschennyh in polkilobayta and Beauty
visualization, multiplied by the elegance and poignancy of code.



 Literature
________________________________________________________________

1. Golyakov EB Support operations in the derivation of the 
sprite in 

   area of ​​the display memory. - "Nicron", 1998, N119.

2. Personal computer "ZX-SPECTRUM". Programming in

   machine code and assembly language: in 3 hours-M., Inforkom,

   1993.

3. Graphics ZX SPECTRUM. -M., VA PRINT, 1994. s.168-171.

4. Hardman, J., E. Hyuzon top 40 procedures. - "ZX-REVIEW, 1992,

   N1, 2.

5. Roshchin I. More about the programming of arithmetic 
operations. 

   - Hams. Your computer ", 2000, N12, 2001, N1-4.







Other articles:

Entry - the contents of rooms.

Events - demopati in Kazan: CAFe'2002 - Wlodek'a story.

Events - demopati in Kazan: CAFe'2002 - story Tigrr / Brainwave.

Events - CAFe'2002 - Results ZX-contests.

Programming - The procedure for calculating the next and previous line of the screen. Optimization.

Feedback - addresses of the publisher.


Темы: Игры, Программное обеспечение, Пресса, Аппаратное обеспечение, Сеть, Демосцена, Люди, Программирование

Similar articles:
Epilogue - the last article number.
News from landfill - News from the E-mage and the ACL, a newspaper C_Net Week 19.

В этот день...   23 November