Optron #41
19 ноября 2000

Likbez - assembler - a view from afar: working with graphics.

<b>Likbez</b> - assembler - a view from afar: working with graphics.
        Assembler - a view from afar.


               Continued.

  Beginning at | | 20, 21, 24, 25, 28-30,

              32, 33, 35, 36


  {} Infarh, 2000


  Here we meet again! After a story about memory allocation 
Speccy you certainly are eager to use the information for 
legitimate purposes, writing any program, in which I will try 
to help. 

  It is no secret that every programmer wants to
see how it runs the program. For
He has to organize its program interface to communicate with 
the user. Nachaat be output from the means, namely -

from the screen. So ...


         The general organization of the screen


  It is not excluded that the BASIC you are already familiar 
with the division on the screen line, familiarity, etc. But if 
you do not know, then read this section carefully.


  Minimum unit of the screen is a point of it - pixel (a 
portmanteau. "Picture element ", pixel - the image element).


  Collected in a matrix of 8x8 points form
familiarity. It houses a standard
symbol. For familiarity is also determined
attribute - the specified pixel color and
pixel color dumped in familiarity.

  The screen contains 24 rows of 32 each familiarity. If you 
need to specify the coordinates of a certain familiarity, 
countdown is from the upper left corner of the screen.

Location where familiarity has coordinates 0, 0. Familiarity to 
his right is coordinates 1, 0, and bottom - 0, 1. Familiarity 
in the lower right corner has kordinaty 31, 23.


  We now turn to the chart. At the lower
level screen is divided into columns (vertical) and line 
(horizontal). Total screen contains a 192x256 pixel. Top left 
pixel has coordinates 0, 0, and the right at the bottom - 255, 
191. The younger three bits of X coordinate determines which 
bits in a byte address corresponds to a given pixel. 

  Now let's see how it is displayed.

  In the Spectrum screen, it corresponds to a memory area 
starting at address # 4000. In its early schedule is 
determined, then - Attributes. Each byte of graphics

determines the state of the eight pixels screen. Pixel will be 
displayed on the screen if in a byte will be set appropriate

bits. For example, take the byte% 10011101. Send it to the 
address # 4000 and see screen. You see, at the very top line at 
the left edge of the screen appeared the point? They have now 
look like this: 



    - There is no point

    - There is a point


  It is easy to understand bits and compliance points
comparing sent beforehand to screen bit and the image: each 
byte in the screen memory area corresponds to the 8-point and

is equal to the width of familiarity.

  Knowing that the screen consists of


         32 x 24 = 768 familiarity,

and each has the familiarity of vertical 8 lines (8 bytes), we 
get the size of the graphics in bytes:



     32 x 24 x 8 = 6144 (# 1800) bytes


  Now - about the attributes. Their area of ​​memory
located just outside the graphics area
starting at address # 5800. Because attribute is set for a 
familiarity (and familiarity with us - 768) and each attribute 
takes one bytes, it is easy to deduce the size of the 
attributes - 768 bytes. 

  Here's how this byte defines the state
Screen:

  - Three least significant bits (D2, D1, D0) define
color ink (INK, ustanovlennye pixels);

  - Bits D5, D4, D3 - paper color (PAPER,
pixels are discarded);

  - D6 bit determines the brightness (BRIGHT);
if it is installed, the on-screen information for this 
familiarity is displayed with brightness.


  Remained bits D7 - bit flicker (FLASH).
If it is enabled, then every 16/50 split second ULA swaps color 
paper and ink for this familiarity.


  Consider the attribute byte in the general form:


           Paper



    D7 D6 D5 D4 D3 D2 D1 D0



         Bright Ink

      Flash


  Color screen Spectrum shall have the following line of codes:


   0 black

   1 Blue

   2 red

   3 Magenta

   4 green

   5 cyan

   6 yellow

   7 white



       Organization of on-screen memory


  After reading the screen memory of the Spectrum, do not think 
that it is organized linearly! Its structure is more 
complicated than in we now verify. Type in and run

such a program:


        LD HL, # 4000; Clear screen

        LD DE, # 4001

        LD BC, # 1800

        LD (HL), L

        LDIR

        LD BC, # 2FF; and set

                        , Its attribute

        LD (HL), 7; ink = 7, paper = 0

        LDIR


        LD HL, # 4000; Home Screen

                        ; Memory

        LD DE, # 1800; Size

                        ; Graphics

                        ; Area
SCR

        LD (HL), # FF; Forwards

                        ; The screen

        INC HL; next byte

                        ; Screen

; Delay 1 / 10 seconds for clarity


        LD B, 5
PAUSE

        HALT

        DJNZ PAUSE

, Repeat for the entire field of graphics


        DEC DE

        LD A, D

        OR E

        JR NZ, SCR


        RET


  The purpose of this very simple program - consistently fill 
the memory graphics screen value # FF, which corresponds to a 
shaded line familiarity. By the way, if you ispolzuete

assember XAS, run this program,
using a combination of keys CS + R, - and you
able after completion of the program
contemplate the resulting image as much as anything. Only after 
clicking "Any key" (not to be confused with RESET) you return

the editor.

  Now consider the specifics of svezhenabrannoy program in more 
detail. 

  So, the first 32 bytes of screen memory
correspond to the first line of the first row
familiarity. But you saw it. Perhaps you think that what 
follows is the second line of the first row? And here and 
there! Next be the first line of the second row! And so

successively filled with all 8 lines -
from first to eighth.

  We can assume that the next byte
will match the first line of the ninth
Line ... But again this is not true. Queue fill returns to the 
first row, this time - the second line. AND

this way, through the familiarity of each row, fill all eight 
rows. 

  As you may have noticed, the screen is divided into thirds, 
and the transition to the next third occurs only after filling

the previous one. Of course, such an organization screen 
creates certain difficulties for programmer, but some dexterity

hand it can be easily solved. On the technical details of the 
talk afterwards, but now study the region attribute. 

  To be honest, especially to do it and do not
necessary. Unlike graphics, attributes
organized in a completely linear. First
sequentially (from left to right) is filled with a zero row, 
then turn passes the first, and so on until the end of the 
screen. In order to illustrate the foregoing, we turn again to 
the program. 

  In the previous example, remove the last
team (RET), and from this position, type the following lines:


        LD DE, # 300, size of the

                        ; Attributes
ATTR

        LD (HL), 2

        INC HL

        LD B, 5
PAUSE1

        HALT

        DJNZ PAUSE1

        DEC DE

        LD A, D

        OR E

        JR NZ, ATTR

        RET


  Here's a visual demonstration of the above! As you can see, 
working with attributes is very simple. 

  And now digress for a while
from the development of screen's look more closely
on the program which you just
scored. Naturally, it's not perfect.
It is only an example in which visibility - the most important 
thing. But let's try to write it so that it was like can be 
more compact. Especially because all assembly instructions you 
already know. 

  So ...


        LD HL, # 4000

        PUSH HL

        LD DE, # 4001

        LD BC, # 1800

        PUSH BC

        LD (HL), L

        LDIR

        LD BC, # 2FF

        LD (HL), 7

        LDIR

        POP BC

        POP HL

        LD E, # FF

        CALL FILL

        LD E, 2

        LD BC, # 300
FILL

        LD (HL), E

        INC HL

        LD D, 5
WAIT

        HALT

        DEC D

        JR NZ, WAIT

        DEC BC

        LD A, B

        OR C

        JR NZ, FILL

        RET


  After analyzing the files of this at your leisure,
you certainly understand its working principle.

  And we will return to operation screen.

  Learning method of distribution screen
memory, we should learn and how to find the screen address of 
the byte (Only bytes to pixels we'll get to later).


  Compute the address of the first byte of the first
familiarity in a row, the number of which is passed through 
register A, you will allow such Program:



        LD H, A; in the "A" is

                        ; Line number

        RRCA

        RRCA

        RRCA

        AND # E0

        LD L, A

        LD A, H

        AND # 18

        OR # 40

        LD H, A

; The output of the HL address the first byte
; A given line


  That's the whole complexity. Do not forget to only
if you want to use the procedure
as a subroutine, in the end must
add the command RET. If you follow
the implementation of the procedure step by step, and,
Calling attention to the contents of the battery in a binary 
form, then easily understand principle.


  Now try some change
For example, to be able to calculate
addresses, taking into account also the column. If in the 
previous example, we as an argument used the line number in the 
register A, then there will need to use another number and

column in the register L. See:


        LD H, A

        RRCA

        RRCA

        RRCA

        AND # E0

        ADD A, L; Adding Rows

        LD L, A

        LD A, H

        AND # 18

        OR # 40

        LD H, A


  As you can see, by adding just one
Example command was modified to more
complex. Now try to do so,
was calculated to address any line screen (0 .. 191). Surely 
this would be a useful exercise. 

  Well, let's go further ...

  Very rarely need anything
done within a single line of pixels.
Typically, the image is vertically
much more space. And in order to
go to the next line, not necessarily to re-calculate its 
address. For example, if you need the following line in the 
Within familiarity (printing characters) enough to hold the 
growth rate of older address byte.


  In case, if the transition should be
conducted without reference to the familiarity and
just on the next line within the screen to your attention a 
little routine:


DOWN_HL

        INC H

        LD A, H

        AND 7

        RET NZ

        LD A, L

        ADD A, # 20

        LD L, A

        RET C

        LD A, H

        SUB 8

        LD H, A

        RET


  And this at the entrance procedure obtains the address
byte in the screen, and the output shows the address
bytes, which is located underneath. Note
This is not to be the first byte of the line. If you prefer not 
to use Register of HL, but something else, then just replace 
the appropriate registers in the procedure. But at the same 
time, once the conversation turned to the topic, look at the 
procedure that performs the opposite effect - calculates the 
address line of the previous: 

UP_HL

        DEC H

        LD A, H

        AND 7

        CP 7

        RET NZ

        LD A, L

        SUB # 20

        LD L, A

        RET C

        LD A, H

        ADD A, 8

        LD H, A

        RET


  Sometimes it is necessary to recalculate the address in
screen at the address corresponding to this
familiarity of the attributes. Make
This can be as follows:

SCR_ATR

        LD A, H

        RRCA

        RRCA

        RRCA

        AND 3

        OR # 58

        LD H, A

        RET


  Well, this time until enough.

  Until the next lesson, try to work out writing programs to 
draw graphical primitives - for the practice is

very useful.

  Good luck!


          Prodlzhenie should ...







Other articles:

4 kilobytes - room is dedicated to Shaos Constructions'2000.

Category X - arguments about the Chaos Constructions'000 and review of dem.

ZX-train - Party Life (ending).

Likbez - assembler - a view from afar: working with graphics.

Poems - "Choice".

Advertising - Message for the city.


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

Similar articles:
Expertise - analysis of unique game "Captain Blood", which represent the a fusion of space quest, and a detective.
Games of Millennium - Games based on the movie Renegade (Renegade).
Gameland - Description of the Game Ball Quest.

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