Copyright (C) Phantom Lord
Music by Phantom Lord
I am glad to welcome you to our little
School programmistov.Segodnya we talk about
fast calculation of addresses in two koordinatam.Eto such an
important thing that you can not write a strong program,
whether game, demo, or a system program without
nee.No above all, it is necessary to
calculation worked quickly. That is why
programmers come up with a variety of perverse (cunning)
techniques that allow calculate the address for a few cycles.
So let us assume that we are doing the flying
stars with the changing coordinates of B and C.
We need to calculate the address of a star with the coordinates
of B (X) and C (Y).
1.Raschitaem address a star with the coordinates
(0, C). This will create at tabl_1
address table with domain C:
[0.191], and the range [# 4000, # 5800)
LD HL, # 4000; Home Screen
LD B, 192; cycle of 192 positions
LD IX, tabl_1; Home Table
A_1 LD (IX +0), L; The table bed
LD (IX +1), H; address
INC IX;
INC IX;
CALL DOWN; Sub on
DJNZ A_1; pixel below
...
tabl_1 DEFS 192 * 2, length of table 192 * 2
, Because 1 address
, It takes 2 bytes
DOWN INC H
LD A, H
AND 7
RET NZ
LD A, L
ADD A, 32
LD L, A
RET C
LD A, H
SUB 8
LD H, A
RET
Now choose from a table of addresses to the address
coordinates (0, C):
LD L, C;
LD H, 0;
ADD HL, HL; HL Multiply by 2
LD DE, tabl_1;
ADD HL, DE; in HL-mail to coordinate
LD E, (HL); natoy, put it in
INC HL; DE
LD D, (HL); 67 TAKTS
...
Usually this is enough for beginners, but with experience you
realize that This protsedurka frequent ispolzyvanii for
one tick (one on-screen clock. 1 second = 50
ticks), make the latter into a double
interrupt. For example, when a large number of stars (300-350
pieces).
Probably many of you know about the wonderful
properties of numbers that are multiples of 256.Naprimer,
to select from a table of 256 bytes
at # 7000 = 256 * 112 number, located
on C'om place enough to make the following
LD L, C
LD H, # 1970
LD A, (HL)
And in A we have the right number. But can we
perform such an operation with a 16-bit'nymi
chislami.Mozhno, but there is one small
poser. Addresses of all we must have
192, since the height of the screen 192 pixels, and
in a table of 256 bytes is placed
256 / 2 = 128 addresses. So it is necessary
create in addition to 128 addresses at # 7000
64 more addresses screen at # 7100:
LD IX, # 7000
LD HL, # 4000
LD B, 128
A_2 LD (IX), L
INC LX
LD (IX), H
INC LX
CALL DOWN (see above)
DJNZ A_2; FOLLOWING PROCEDURE IX =
LD B, 64; = # 7000, HL = # 5000
INC HX
A_3 LD (IX), L
INC LX
LD (IX), H
INC LX
CALL DOWN
DJNZ A_3
...
Now, take ADDRESS with coordinates (0, A)
CP 127;
JR C, SECOND;
ADD A, A; 4
LD L, A; 4
LD H, # 70; 7
LD E, (HL); 7
INC L; 4
LD D, (HL); 7 = 33 TAKTS
RET
SECOND
ADD A, A
LD L, A
LD H, # 1971
LD E, (HL)
INC L
LD D, (HL)
RET
As you can see yourself superior to the previous calculation
is almost doubled. Approximately 20 cycles spent on testing
supplies. Therefore, You can use simple 2-digit display, and
the third segment of the score image.
We discussed how to make the calculation of addresses
the points with zero X-coordinates. Consider how to calculate X.
Assume that the above method
we calculated the address of the coordinate Y, and now we find
the distance from the left border screen. Let B = X:
LD L, B;
SRL L; Divide L by 8.
SRL L;
SRL L;
LD H, 0; In DE we have to address
ADD HL, DE; first calculation.
In HL we address, calculated by
at coordinates (B, C), ie that both
wanted.
It is noteworthy that this is not the fastest calculation, but
if you poraskinete good brains, then changing a couple of
commands you will get more interesting protsedurku. It's up to
you.
See you at the next lesson assembler
Where I'm going to tell you something more interesting.
__________________________________________