home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / progmisc / bs_hwscr.zip / EXPLAIN.DOC < prev    next >
Text File  |  1993-10-17  |  20KB  |  448 lines

  1.   ▓▓▒▒░░  Hardware Scrollies .... Everything You Might Dream Of .... ░░▒▒▓▓
  2.  
  3.  Released   : 17/10/93
  4.  Doc & Code : Binary Slave
  5.  Last Minute: Living Nature
  6.  
  7.  Binary Slave presents his first source-code to Public Domain.
  8.  
  9.  It mainly consist of the so called Hardware scroll... .
  10.  
  11.  A Hardware scroll is a characteristic of the standard VGA-card.
  12.  It does not take hudge CPU time just a few port writes and the rest is up
  13.  to another VGA characteristic nl. Vertical retrace which does all the
  14.  synchronisation.
  15.  
  16.  I will give you an idea/example how to make a hardware scrolly... .
  17.  So you'll be able to make very fast scrollers (I sure hope so) on
  18.  your PC even on an XT! Just like in some demos.
  19.  All you need is some knowledge about programming pascal/assembly,
  20.  graphic environments,image processing.
  21.  
  22.  The code is written in TP & ASM, and may easyly be converted into C
  23.  language or similar languages. The complete source is given and you need
  24.  at least TP version 6.0.
  25.  
  26.  For the assembly procedures I just saved the DS if it's changed, but if
  27.  you want to work 100% properly this doesn't fit. Cause if you don't  ....
  28.  God may be with you...lazy me.
  29.  You propably need to preserve the following registers:
  30.   -  Base pointer  BP.
  31.   -  Stack Segment SS.
  32.   -  Stack pointer SP.
  33.   -  Data Segment  DS.
  34.  
  35.  An asm statement can freely modify these registers:
  36.   -  AX,BX,CX,DX
  37.   -  SI,DI
  38.   -  ES
  39.   -  Flags
  40.  
  41.  On entry to the asm statement, the built-in assembler knows only the BP, SP,
  42.  SS, and DS registers; it can assume nothing else about register contents.
  43.  
  44.  I've chosen those languages for several reasons.
  45.   Turbo Pascal : - one of the most popular languages...
  46.                  - very easy to read/write/learn and understand some techniques.
  47.   ASM : For the fast actions... such page flipping/filling/drawing routines.
  48.  
  49.  It was not the intention to make a super fast scrolling, no, it is
  50.  intended to make Hardware scrolling a fact and understandable to all coders,
  51.  not just the asm-speed freaks (who already know the hardware scrollies).
  52.  The speed of the actual scroll is not so important due to the synchronisations
  53.  Learning to make smooth animations is perhaps more important than the speed of
  54.  the animation!
  55.  Also try not to make too much procedure calls because TP's internal memory
  56.  management slows (due to the activation records) down the animation.
  57.  
  58.  The scrolling is performed in tweaked video mode, it is also perfectly
  59.  possible that you make your scrollers in normal graphics mode (13H -
  60.  320*200*256colors), but I think mode X has got its charms...
  61.  
  62.  First Let me tell you something about your PC...
  63.  On the base level of your PC there exist things called I/O ports.
  64.  These ports makes it possible to rebuild/change the entire
  65.  graphic evironment.
  66.  
  67. I   Some general functions of the VGA
  68.     =================================
  69.  
  70.   I.1 The Graphics Controller
  71.   ---------------------------
  72.  
  73.   The graphics controller is a VLSI circuit that resides in the data path
  74.   between the processor and the display memory. In its default state, the
  75.   graphics controller is transparent. As a programmer you should be able to
  76.   modify this port in order to let it assist in drawing operations. Like
  77.   logically combine,rotate processor data with the display memory data
  78.   (PROCESSOR READ LATCHES, which can be verry usefull...).
  79.   Color compare functions (rep scasb ex.sprites),Data serializer,Logical Unit.
  80.  
  81.  
  82.   I.2 The Crt Controller Registers
  83.   ---------------------------------
  84.  
  85.   The CRT controller controls the crt (Cathode Ray Tube) by generating the
  86.   syncing and blanking signals that define the display raster.
  87.   It displays colors that are generated by a beam of electrons that strike
  88.   the phosphorus coating on the back of the crt screen and cause it to glow.
  89.   The electron beam is swept across the display screen from the left to the
  90.   right in a series of horizontal lines. At the same time, it's intensity is
  91.   modulated to produce display patterns. The electron beam must continiously
  92.   redraw the pattern on the screen 50,60,70 or more times a second, depending
  93.   on the display used (=REFRESH TIME). The sweep pattern of the electron
  94.   beam is called RASTER LINES.
  95.   The beam begins in the upper left corner of the display and sweeps right.
  96.   When it reaches the right edge of the screen, the beam is shut off (HORIZONTAL
  97.   BLANKING) and then rapidly brought back to the left edge of the screen
  98.   (HORIZONTAL RETRACE) to begin the next horizontal scan just below the previous
  99.   one. After all the horizontal scans have been completed (HORIZONTAL TOTAL),
  100.   the electron beam will end up in the lower right corner of the screen.
  101.   At this point the beam is shut off once again (VERTIKAL BLANKING) and then
  102.   rapidly brought back up to the upper left corner (VERTIKAL RETRACE) so the
  103.   next raster can begin.
  104.  
  105.   This last effect is normally very synchronic on all VGA-cards, so this
  106.   effect should be very good to synchronize all our animation...Ok?
  107.  
  108.   I.3 Display Memory
  109.   ------------------
  110.  
  111.   The nowadays VGA contains up to 256Kb or more display memory, divided into
  112.   four independent 64Kb sections of memory! These are called color planes.
  113.   These color planes all reside in the same processor memory space.
  114.   Which color planes are being written to or read from at any time is
  115.   determined by several I/O ports.
  116.  
  117.   I.4 Attribute Controller
  118.   ------------------------
  119.  
  120.   The attribute controller determines wich color will be displayed for both text
  121.   and graphic mode. The heart of the controller is the color look-up table that
  122.   translate four bit information from display memory into 6 bit color information
  123.   mation (EGA) or 12 bit color information (VGA).
  124.  
  125.   I.5 Sequencer
  126.   -------------
  127.  
  128.   The sequencer controls the sequencing of all functions. It generates the dot
  129.   and character clocks that control display refresh timing. It controls the
  130.   timing of display memory read and write cycles, and generate wait-states
  131.   if necessary.
  132.  
  133.  
  134. II  The VGA Mode
  135.     ============
  136.  
  137.   The standard (MCGA) or bios 13H mode has a resolution of 320*200 and
  138.   256 colors. You can call this mode by a simple BIOS-call. The BIOS takes
  139.   care of all the additional settings, no prob.
  140.   To get into tweaked video mode which has a resolution of 320*200*4 and
  141.   256 colors, we have to do some old tricks.
  142.   -->"unchain" the vga mode, disable the "odd/even adressing".<--
  143.   These setting enable us to use 256kb of display memory.
  144.   Yes, 4 * 64Kb color planes are available, this results in a smooth page
  145.   flipping animation scenes...(if handled properly ofcoz).
  146.   As you all know the VGA memory adress area starts from 0A000:0000H.
  147.   In tweaked mode we have:
  148.  
  149.   0A000:0000 equals 1th pixel + 2nd pixel + 3th pixel + 4th pixel!
  150.   0A000:0001 equals 5th pixel + 6th pixel + 7th pixel + 8th pixel!
  151.   etc...
  152.  
  153.   The only thing distinguishing the pixels is the memory adress and the color
  154.   planes which they are in.
  155.   As we know we have 4 planes ... 4 pixel
  156.  
  157.   0A000:0000  1th plane ....  1th pixel
  158.   0A000:0000  2nd plane ....  2nd pixel
  159.   0A000:0000  3th plane ....  3th pixel
  160.   0A000:0000  4th plane ....  4th pixel
  161.   0A000:0001  1th plane ....  5th pixel
  162.   0A000:0001  2nd plane ....  6th pixel
  163.   0A000:0001  3th plane ....  7th pixel
  164.   0A000:0001  4th plane ....  8th pixel
  165.   etc etc... ok?
  166.  
  167.   Remark:  In our case this means that the physical screen is only 80 memory
  168.            adress spaces (pixels) in width!! No longer 320!!
  169.            200 in height remains the same.
  170.  
  171.  
  172. III The used i/o ports:
  173.     ===================
  174.  
  175.   III.1 Plane reading/writing
  176.   ---------------------------
  177.  
  178.   The Graphics controller: port 3CEH
  179.   index  00  : Set/reset register
  180.          01  : Set/reset enable register
  181.          02  : Color compare register
  182.          03  : Data rotate & Function Select
  183.          04  : Read plane Select register
  184.          05  : Mode register
  185.          06  : Miscellaneous register
  186.          07  : Color Don't care register
  187.          08  : Bit mask register...
  188.  
  189.   Writing on the screen...
  190.   Write to plane ... plane enable (port 3CEH index 01).
  191.  
  192.         p.e.: mov   DX,3ceH    ; select port 3CEH
  193.               mov   al,01      ; select index 01
  194.               out   dx,al      ; write selections to port
  195.               inc   dx
  196.  
  197.                                 d0 d1 d2 d3  *  *  *  *
  198. port 3CEH index 1              ╚══╩══╩══╩══╩══╩══╩══╩══╝
  199.                                 0  1  2  3  4  5  6  7
  200.                                   * = Not used by plane switching.
  201.                                   d0=1 : plane 0 write enbale
  202.                                   d0=0 : plane 0 write disabled!
  203.                                   d1...  plane 1.
  204.                                   d2...  plane 2.
  205.                                   d3...  plane 3.
  206.              Yes, it is possible to write with one movsb to write 4 pixels at
  207.              once... (yes even eight if you enable double word mode).
  208.              Such routines are used to clear verry fast the screen in graphics
  209.              mode just imagine 4 64Kb display memory screen cleared at the
  210.              speed of clearing 2 screens (4K) in text mode !!!
  211.  
  212.                           OR
  213.               mov   al,00 --> in  al,dx
  214.                               and al,11110000B force all planes on write disabled.
  215.                               or  al,1   force bit 0 on 1 to make plane 0
  216.                                             write enable..
  217.  
  218.               out   dx,al    ; write changes to port.
  219.  
  220.               For planes 1..3 the same code san be used.
  221.                   plane 1: mov al,02H
  222.                   plane 2: mov al,04H
  223.                   plane 3: mov al,08H
  224.               all planes: mov al,0FH
  225.  
  226.       Reading from memory/screen:
  227.  
  228.       Read plane select register... (port 3CEH index 04)
  229.       This one differs from the plane enable :-)
  230.  
  231.                                 d0 d1  *  *  *  *  *  *
  232. port 3CEH index 04             ╚══╩══╩══╩══╩══╩══╩══╩══╝
  233.                                 0  1  2  3  4  5  6  7
  234.                                   * = Not used by plane switching.
  235.                                   d0&d1=00 : plane 0 read enbale.
  236.                                         01 : plane 1 read enable.
  237.                                         10 : plane 2 read enable.
  238.                                         11 : plane 3 read enbale.
  239.      you are not allowed to read from more than one plane at a time since
  240.      this would not be meaningfull.
  241.      So, in this way you are able to read from the screen.
  242.  
  243.  
  244.   III.2  The Logical Screen Width
  245.   -------------------------------
  246.  
  247.      In graphics mode, it defines the logical distance, in either 16 bit
  248.      memory words or 32 bit double words between successive scan lines.
  249.      In all standard operating modes,the logical screen width is equal to
  250.      the physical screen width.
  251.      You can change the width of the screen to make you screen wider so you
  252.      can update you scrolly (animation) out of the screen so nobody will
  253.      see it!!
  254.      In the example we use 40*4=160 words this means 320 bytes in width.
  255.      Actually *4 =1280 bytes wide!!!!
  256.      You can change all these settings for example zooming or the hardware
  257.      scroll ( Panning) page flipping animation.
  258.  
  259.      You can alther the screen width with the CRT controller... .
  260.  
  261.      PORT 3B4/3D4H
  262.      -------------
  263.  
  264.      index 00H  Horizontal total           0CH  Start adress high
  265.            01H  Horizontal display end     0DH  Start adress low
  266.            02H  Start horizontal blank     0EH  Cursor location high
  267.            03H  End horizontal blank       0FH  Cursor location low
  268.            04H  Start horizontal retrace   10H  Vertikal retrace start
  269.            05H  End horizontal retrace     11H  Vertikal Retrace end
  270.            06H  Vertical total             12H  Vertikal Display end
  271.            07H  Overflow                   13H  Logical Screen width
  272.            08H  Preset row scan            14H  Underline location
  273.            09H  Maximum scan line          15H  Start Vertikal Blank
  274.            0AH  Cursor Start               16H  End Vertikal Blank
  275.            0BH  Cursor end                 17H  Mode Control register
  276.                                            18H  Line Compare register
  277.  
  278.      Handle these ports with care because some of them can damage your
  279.      monitor screen!!!
  280.  
  281.      Well the index we are intersted in is 13H:
  282.      ex. in  TP:
  283.        port[$3d4]:=$13    ;select port 3D4H index 13H = logical screen width.
  284.        port[$3d5]:=.....  length of the screen ... in our case 40*4 =160 words.
  285.  
  286.  
  287. IV   The Actual Hardware Scroll
  288.      ==========================
  289.  
  290.      a) Byte Panning
  291.      ---------------
  292.  
  293.      If we take a look at the CRT controller port 3D4H we see the index
  294.      0CH and 0DH.
  295.      The start adress high and Start adress low... .
  296.      This is a 16 bit read/write register that defines the adress in
  297.      display memory of the data that will be displayed in the upper left
  298.      corner of the screen (starting position).
  299.      This register can be used to byte-panning an image on the screen or to do
  300.      the page-flipping.
  301.  
  302.      1 word equals    High  Low
  303.                      ╚════╩════╝
  304.                      0CH = Start addres (high byte).
  305.                      0DH = Start addres (low  byte).
  306.  
  307.      b) But Panning
  308.      --------------
  309.  
  310.      For smooth scrolling in text mode for example we can add the index 08H
  311.      Preset row scan, so that characters can be scrolled up or down one pixel
  312.      at a time! See also Horizontal Pel Pan register.
  313.  
  314.                                 d0 d1 d2 d3 d4  *  *  *
  315. port 3CEH index 08             ╚══╩══╩══╩══╩══╩══╩══╩══╝
  316.                                 0  1  2  3  4  5  6  7
  317.  
  318.             * = Not used by panning.
  319.               d4-d0: Preset Row scan for panning one pixel at a time.
  320.  
  321.               Note we wont use this because this realy isn't nessecary in
  322.               graphics mode to perform smooth panning.
  323.  
  324.       Horizontal Pel Pan Register
  325.       ---------------------------
  326.  
  327.       First of all this register is situated at port 3C0H (= Attribute Contr-
  328.       oller Register.)
  329.  
  330.       PORT 3C0H:
  331.       ----------
  332.       register  00H Color palette Register 0   0AH Color palete Register 10
  333.                 01H Color palette Register 1   0BH Color palete Register 11
  334.                 02H Color palette Register 2   0CH Color palete Register 12
  335.                 03H Color palette Register 3   0DH Color palete Register 13
  336.                 04H Color palette Register 4   0EH Color palete Register 14
  337.                 05H Color palette Register 5   0FH Color palete Register 15
  338.                 06H Color palette Register 6   10H Mode Control Register
  339.                 07H Color palette Register 7   11H Screen Border color
  340.                 08H Color palette Register 8   12H Color plane enable register
  341.                 09H Color palette Register 9   13H Horizontal Panning regsiter
  342.                                                14H Color Select register
  343.  
  344.  
  345.  
  346.                                 d0 d1 d2 d3  *  *  *  *
  347. port 3C0H index 13H            ╚══╩══╩══╩══╩══╩══╩══╩══╝
  348.                                 0  1  2  3  4  5  6  7
  349.                            * = Reserved.
  350.               d3-d0: Allows the display to be shifted horizontally one pixel at
  351.                      a time. Up to 16 pixels the rest is performed by the start
  352.                      address register increasing 2 times or 2 words. or 2*8=16!
  353.  
  354.            This might all sound pretty confusing but the following pic should
  355.            clear things up...
  356.  
  357.  
  358.         ex. in text mode: 1 ░░░▓▓░░░
  359.                           2 ░░▓░░▓░░
  360.                           3 ░▓░░░░▓░
  361.                           4 ░▓░░░░▓░
  362.                           5 ░▓°▓▓▓▓░   °= left upper corner.
  363.                           6 ░▓░░░░▓░   Preset row Scan = 5   -- VERTICAL.
  364.                           7 ░▓░░░░▓░   Horiz. Pel Pan  = 3   -- HORIZONTAL.
  365.                           8 ░░░░░░░░   These pixel per pixel.
  366.                             12345678
  367.                                        While Start adress register byte per
  368.                                        byte... .
  369.  
  370.          if ° (the upper left corner) reaches the 8,8 then it is time to clear
  371.          the preset row scan and the Hor.pel pan reg. and increase the Start
  372.          adress register. OK?
  373.  
  374.     Note: If you make a split screen you are able to put an image on the screen
  375.           and let the Hardware scrolly scroll on top above the split screen.
  376.           The linecounter ignores the split screen and so the VGA is fooled.
  377.           So the panning has now effect on the splitscreen!
  378.           Just like in the MN2-Dentro...
  379.  
  380.           The Line compare register enables us to use a split screen.
  381.           The line compare register has several overflow bits seperated
  382.           in different ports, what a genius plan!!
  383.           Just put the correct value in the line compare and off we go... .
  384.  
  385.           Upper left corner :    --> ┌────────────────────────┐
  386.           Start adress register      │-----                   │
  387.                                      │----------              │
  388.                                      │--------------          │
  389.                                      │                        │
  390.           Line compare register: --->│========================│
  391.           Begin of the display       │---- ---- ---- ---- ----│
  392.           memory.                    │---- ---- ---- ---- ----│
  393.                                      │--------------          │
  394.                                      │---- ---- .             │
  395.                                      │                        │
  396.                                      │                        │
  397.                                      └────────────────────────┘
  398.  
  399.           In the MN2-dentro I used the upper part of the screen as hardware
  400.           scrolly. The scrolly is on the buttom of each page .
  401.           So I could leave the big pix on the top of page 2 and made
  402.           it pan the way you saw it.
  403.  
  404.  
  405.   Well if you find something interesting, or if you make some scrollies yourself
  406.   with this source, I would be pleased if you upload them at MN2.
  407.   Also if you have an idea/comment please upload it at MN2 in the
  408.   Binary Slave conference...
  409.   Still having trouble?
  410.   Leave a msg at MN2 and I'll see what I can do...
  411.  
  412.   Files:
  413.   ------
  414.         - Cijf.pcx
  415.         - Multa-u.pcx
  416.         - Multv-.pcx        {GFX files made by Wolf. My personal gfx man!}
  417.         - Example.hdw       {"Modulated" scroll text}
  418.         - Example.txt       {Scroll text}
  419.         - Explain.doc       {This file}
  420.         - Hscroll.exe       {Compiled example file}
  421.         - Hscroll.pas       {Example source}
  422.         - Meat.exe          {MN2 publicement file}
  423.         - Meat.pas          {Source from this file}
  424.         - Txt2scr.exe       {Converts example.txt 2 example.hdw ascii# 33 - 100}
  425.         - Txt2scr.pas       {Source for this file
  426.  
  427.  
  428.   Note: I don't have the official documents of the Vga, only some books.
  429.         I'm not responsable for any faults in these docs!
  430.         Take care with some ports because they might cause damage!
  431.         The only method to learn such things is trail and error!
  432.         Be sure you get the whole and correct source and D/L it from MN2!
  433.  
  434.   | MultiNet 2: +32 (0)50/36.33.96 [HST 24/24]             |
  435.   | FidoNet   : 2:291/1800.12 [Bart Vanderostyne]          |
  436.   | F A X     : +32 (0)50/34.73.79 [Write to Binary Slave] |
  437.  
  438.   Hi's and Ho's  to - D.H.
  439.   -------------     - Tasmaniac
  440.                     - Shadowlord
  441.                     - Overlord 0f House
  442.                     - Living Nature
  443.                     - El Presidento
  444.  
  445.     Greetings to all wich I humbly forgot on the MN2 greetingslist...
  446.  
  447. <EOF>
  448.