SoundTrack: DEVOUR OF BRAIN BY DX-1969 1998
__________________________________________
(C) Card! Nal / PGC / BD
__________________________________________
Hello, dear readers of our magazine!
Daniel then asked me to write an article
by coding'u, and I'm here for a text editor. This article has
no definite purpose, I just talk about the optimization and
all that I have accumulated with the release of
number four Deja Vu.
To begin with talk about the withdrawal of sprites
without attributes. That's standard procedure:
LD HL, adr; address sprite
LD DE, # 4000; address on the screen
LD B, 192; height in pixels
LD C, 32; width of familiarity
LOOP2 PUSH DE
PUSH BC
LD B, 0
LDIR
POP BC
POP DE
INC D
LD A, D
AND 7
JP NZ, METKA1
LD A, E
ADD A, 32
LD E, A
JP C, METKA1
LD A, D
SUB 8
LD D, A
METKA1 DJNZ LOOP2
RET
Now let's count how many
cycles it works. Without going into details, as I thought, I
will say that It employs about 146,000 strokes in the
derivation of the sprite-sized screen. On average, spends 23.7
cycles per byte. Generally, it is good procedure for deriving a
static picture, well, if you need a fast, generic procedure,
then see the bottom listing. The word I'm Universal implied
that one can arbitrarily set the height, width, sprite and its
coordinates on the screen.
CALL DECRUN; this program makes
, Address table
; Screen
LD HL, adr; address sprite
LD D, 3; coord. X in the familiarity
LD E, 8; coord. Y in pixels
LD B, 100; height in pixels
LD C, 20; width of familiarity
WIWOD DI
LD A, D
LD (WIW1 +1), A
LD A, C
ADD A, A
SUB 64
NEG
LD C, A
LD A, B
LD B, 0
LD IX, WIW2
ADD IX, BC
EX DE, HL
LD H, B
ADD HL, HL
LD BC, BUFER
ADD HL, BC
LD (STACK +1), SP
LD SP, HL
EX DE, HL
LD B, A
LOOP1 POP DE
LD A, E
WIW1 ADD A, 0
LD E, A
LD C, D
JP (IX)
WIW2
DUP 1932
LDI; team LDI repeated
EDUP; 32 times
DJNZ LOOP1
STACK LD SP, 0
EI
RET
DECRUN LD HL, BUFER
LD DE, # 4000
LD B, 192
LOOP LD (HL), E
INC HL
LD (HL), D
INC HL
INC D
LD A, D
AND 7
JR NZ, METKA
LD A, E
ADD A, 32
LD E, A
JR C, METKA
LD A, D
SUB 8
LD D, A
METKA DJNZ LOOP
RET
BUFER DEFS 384
It works for 108,076 cycles
withdrawal of sprite the size of a screen that
1.35 fold faster than the previous one. On average,
spends 17.6 cycles per byte. Subroutine
DECRUN runs only once, and then you can use subroutine WIWOD
indefinitely. I can only say that it is necessary to prohibit
the interruption, as yuzaetsya stack. Try yourself to find out
how everything works. I just want what will prompt the command
LD C, D before the team JP (IX). As known, LDI at work reduces
the register-pair BC, and so, in order not to spoil Register B,
we need to keep C register number is greater than the maximum
width of the sprite that is larger than 32, and register
D always contains a number greater than 32.
Well all sorted out with the conclusion of sprites,
talk about turning a sprite at any angle
(0 - 360). In the fourth Deja Vu was the source Kolotova "TURN
SPRITES". Honestly, I saw this program from earlier and I will
say that the quality of the program me not very happy :-(. I
tried turn-drenched square 5 by 5 familiarity
45 degrees, and saw a nasty picture, the square was already
looks like not a square, and on the square lattice, and use this
program was difficult. And the thing
in the algorithm of rotation and accounting structure
screen. I implemented a slightly different algorithm and the
result is simply stunning. The picture is almost (considering
lower resolution screen) is not distorted. If, for example,
turn filled circle radius of 50 to 45
degrees around its center, then get
the same circle. True, this algorithm is not me
came up, and took it out of the magazine ZX NEWS 3.
There was a little article about turning sprites, and
was given to program in BASIC. Programm
This has worked for over an hour, turning the whole
screen. Analog in the code of this program is not
was, as the author confessed that he does not make friends with
BASIC built-in calculator. Honestly
speaking, I am also not friendly with built-in
calculator, and therefore implement this
algorithm in the code without using a calculator. The first
version of my program worked 3.5 minutes, turning the whole
screen is not it is important to any angle. I thought it was a
full SUXX and slightly to optimize it. In finally got around 40
seconds, which is about 5 times faster than the original
version, and 100 times faster than BASIC, and this, mind you,
without compromising image quality. Further optimize was lazy,
though the prospect of time to bring up to 10-15 seconds, and
then faster, but this joy I I can give you, SERZH, you
understand me :-). The annex will find a source in format ALASM
3.8c. Source contains comments, so it will be dismantled. Do
not look to poor code, no time to lick it.
The origin is lower left
corner of the screen, if I'm not mistaken. Rotating a sprite
(do not let yourself wither!) Should already be on your screen.
The program uses a lot of memory (6144 + approx 2kg) bytes for
the buffer, see there ... I almost forgot to tell the algorithm
of rotation. Take the point with coordinates (x0, y0), rotated
at an angle and the coordinates of (x1, y1), from coordinates
(x1, y1) point is taken (if any) and transferred in the
coordinate (x0, y0), then is taken next. (X0, y0), etc. Check
out beyond screen, OF COZZZ, are made so that all nama :-).
So what? Let's go on! I was there a
CRANK asked for help to understand the "arcane xor'kah", and
therefore I will say this. Learn to understand the xor'kah you
must own (Through STS v6.2, for example). Try
understand that it rasxorivaet, trace, followed closely by the
registers, the resident STS'a endure, if necessary, to a safe
place. The simplest xor'ka looks as follows:
LD HL, adr
LD BC, lenght
LOOP LD A, (HL)
XOR 23
LD (HL), A
INC HL
DEC BC
LD A, B
OR C
JR NZ, LOOP
RET
If you have a pC, where you can dial
program in assembly language or in hiew (if you
perverse :-)).
mov dx, adr
mov bx, lenght
loop mov si, dx
mov al, [si]
xor al, 1923
mov [si], al
inc dx
dec bx
jnz loop
retn
Of course I do not care where kodit, but
SPECCY, OF COZZZ, RULEZ FOREVER!
With xor'kami like figured out how to
Spectrum, and on PISI, we have decided
more :-). I would like to say a few
words about switching pages. For example, what I saw that
program from SERZH'a in one of his ruleznyh (as always) stateek:
LD BC, # 7FFD
OUT (C), A
LD (BANK), A
RET
BANK DEFB 0
Or something like that. What do we see? It seems everything
is correct ... But no! Consider a situation that interrupts the
teams came out (c), a and ld (bank), a. Interrupts will work
out and come back. But on exit to restore the old value
BANKi and eventually colored squares on the screen, and
therefore need to preserve the value of front out (c), a.
Because Deja Vu will now go out without
protection, I would like to talk about classroom
method of copy protection invented
I (drive can not be copied even in pC
and Amiga), and even wanted to throw in the source
application (formatter and reader format), but
then I thought that would be too bold! AND
abandoned the idea. Although, if I
well be asked, then maybe tell you.
Now we have gone further.
I want to say a few words about the game Black
Raven, cool I must say igruha. But some levels are simply
suicidal (I because of the almost impenetrable), but since I do
not gamer toys are not fond helluva lot, so make your life
easier, put a couple of POKES and saw FINAL CUT! If zero
three bytes from the address # D5CE in RAM0, then you
will no longer run out of money. Well, a number
current level is located at # 79D8.
For example, enter the next room there
level, do the "retry" and you
on another level. If there Record
# 10, see FINAL CUT. "Everything's fine," you might say - "But
what about protection?" Here You're right, even the shadow
businesses will not help. And so the twist yourself, break your
own. I have no moral right to teach you breaking commercial
products. I will only say, I spent three days to insert POKES!
Next, let's talk about the multiplication algorithm.
The simplest procedure for multiplication SPECCY
looks like this: HL = A * E
LD H, A
LD L, 0
LD D, L
LD B, 8
LOOP ADD HL, HL
JR NC, $ +4
ADD HL, DE
DJNZ LOOP
RET
Not difficult to count the number of cycles:
at least 310, a maximum of 358 without RET.
Of course, if you expand the loop, we obtain
at least 206, and a maximum of 254 bar. A possible
Does faster? Yes, of cozzz! But for this
to remember the initial algebra. A more precise formulas of
abridged multiplication:
The square sum of: (a + b) ^ 2 = a ^ 2 +2 ab + b ^ 2
Square difference: (ab) ^ 2 = a ^ 2-2ab + b ^ 2
Take these formulas into service. For
In order to write the procedure of multiplication,
need to form a plate of squares of numbers from 0 to 255,
inclusive. Table squares will be the main part of the program.
Address table must be a multiple of 256, ie
Low byte = 0. This makes the following
programm.
SET_TBL LD DE, TABL
LD H, E
LD L, E
LD B, E
LD C, E
SET_T1 LD A, L
LD (DE), A
INC D
LD A, H
LD (DE), A
DEC D
ADD HL, BC
INC C
ADD HL, BC
INC E
JR NZ, SET_T1
RET
TABL DEFS 512
And here is the procedure for multiplication. Numbers
from 0 to 255 are set in the registers L and E.
Response in HL.
MULS LD H, 'TABL
LD C, (HL)
INC H
LD B, (HL)
LD A, L
ADD A, E
JP NC, MUL4
JP M, MUL1
SUB E
SUB E
JP NC, MUL2
LD A, E
SUB L
MUL2 LD L, E
LD D, (HL)
DEC H
LD E, (HL)
LD L, A
LD A, (HL)
INC H
LD H, (HL)
LD L, A
SBC HL, BC
OR A
SBC HL, DE
LD A, L
CPL
LD L, A
LD A, H
CPL
LD H, A
INC HL
SRL H
RR L
RET
MUL4 LD L, E
LD D, (HL)
DEC H
LD E, (HL)
LD L, A
LD A, (HL)
INC H
LD H, (HL)
LD L, A
SBC HL, BC
OR A
SBC HL, DE
SRL H
RR L
RET
MUL1 LD A, L
SUB E
JP NC, MUL3
LD A, E
SUB L
MUL3 LD L, E
LD D, (HL)
DEC H
LD E, (HL)
LD L, A
LD A, (HL)
INC H
LD H, (HL)
LD L, A
SBC HL, BC
OR A
SBC HL, DE
SRL H
RR L
LD A, L
CPL
LD L, A
LD A, H
CPL
LD H, A
INC HL
RET
As a result, the number of cycles at least 141,
a maximum of 207, which will agree, a little bit.
This algorithm was borrowed from the game
BATTLE COMMAND 128. Also write about this in the electronic
journal of Spectrum Expert 1. However, there are a few other
programs that are marks when multiplied, and there was limiting
the numbers of -128 To +127, which is not always convenient.
Now let's talk about the calculation
square root. In ZX FORMATE 7 was some kind of programm for
calculating the roots, but I just say that not very much. And
the thing speed. I poprobyval computed using
of the programs from the root of 1024. Potrassiroval STS'om and
found that program 5 (Five) times does the division, and in
calculating the square root of 65,535 makes 10 (desyat!)
divisions. Moreover, the procedure division holds about 1000
cycles, and even more. I then thought for a moment, and wrote
his own. Its listing is given below. My program does only one
division, if the number falls in interval from 2 to 16383, and
holds two division, if the number in the range of 16,384 -
-65535. When the numbers 0 and 1 division are not met. The
program runs on a modified Newton algorithm is me. Try
themselves to understand how everything works. Dam hint: in the
table are so-called secondary roots of numbers from 1 to 2 ^ 2
^ 15. Calculated by the formula:
(Sqr (2 ^ n) + sqr (2 ^ (n +1))) / 2
SQRT LD A, H; HL = SQR (HL)
OR L
RET Z
LD D, H
LD E, L
LD B, 15
ADD HL, HL
JR C, SQRT2
DEC B
ADD HL, HL
JR C, SQRT2
DEC B
SQRTL ADD HL, HL
JR C, SQRT1
DJNZ SQRTL
LD HL, 1
RET
SQRT1 LD HL, TABL
LD C, B
LD B, 0
ADD HL, BC
LD C, (HL)
SQRT_ PUSH BC
CALL DIV
POP BC
ADD HL, BC
SRL H
RR L
RET
SQRT2 PUSH DE
CALL SQRT1
POP DE
LD B, H
LD C, L
JR SQRT_
DIV PUSH BC; HL = DE / BC to rounding
LD A, B; response
CPL
LD B, A
LD A, C
CPL
LD C, A
INC BC
LD HL, 0
LD A, E
ADD A, A
RL D
DUP 1916
ADC HL, HL
ADD HL, BC
JR C, $ +4
SBC HL, BC
RLA
RL D
EDUP
LD E, A
POP BC
SRL B
RR C
OR A
SBC HL, BC
EX DE, HL
RET C
INC HL
RET
TABL DEFB 1,2,2,3,5,7,10,14,19,27
DEFB 39,55,77,109,155,219
Run time, I did not think, but it
amounts to approximately 1500 - 2500 cycles.
The program can be accelerated if a more rapid procedure
division. But You can do without division, if
use a different algorithm. I have
to accept this, but there is not enough
free time to investigate it thoroughly and write a program :-(
There my story ends. I can only say that the listings used
assembler directives ALASM. For example:
DUP 1916
body program
EDUP
means that the body of the program assembled 16 times.
Finally I would like to inform you that I want to prepare
for the second number, a long article on 3D-graphics on SPECCY.
It will be considered: the formula turning points in space, the
algorithm of drawing the cast of the triangle decomposition,
the algorithm to hide invisible faces, the algorithm for
constructing complex nonconvex object known, algorithms fill
the faces depending on the location of the light source and
something else. Naturally we give examples in assembler. Was
have the desire and free time. On this until everything. BYE.