home *** CD-ROM | disk | FTP | other *** search
/ PC Underground / UNDERGROUND.ISO / graphic / modexlib.asm < prev    next >
Assembly Source File  |  1995-07-28  |  14KB  |  425 lines

  1. .286
  2.   w equ word ptr
  3.   b equ byte ptr
  4. data segment public
  5.   extrn vscreen:dword
  6.   extrn vpage:word
  7.   extrn palette:dataptr
  8. data ends
  9.  
  10.  
  11. code segment public
  12. assume cs:code,ds:data
  13. public init_modex, p13_2_modex, squeeze, copyscreen, double
  14. public clrx, split, setpal, getpal, switch, setstart, enter400
  15. public waitretrace, fade_out, fade_to, copy_block, pal_rot
  16.  
  17. switch proc far                 ;switches between both video pages
  18.   mov bx,vpage                  ;load start address
  19.   mov dx,3d4h
  20.   mov al,0ch                    ;distribute to CRTC register 0dh/0ch
  21.   mov ah,bh
  22.   out dx,ax                     ;set highbyte (register 0dh)
  23.   inc al
  24.   mov ah,bl
  25.   out dx,ax                     ;set lowbyte (register 0ch)
  26.   cmp bx,16000d                 ;switch start address (0/16000)
  27.   je set0
  28.   mov vpage,16000
  29.   ret
  30. set0:
  31.   mov vpage,0
  32.   ret
  33. switch endp
  34.  
  35. Init_ModeX proc pascal far      ;enables Mode X
  36.   mov ax,0013h                  ;set Mode 13h
  37.   int 10h
  38.  
  39.   mov dx,3c4h                   ;timing sequencer
  40.   mov al,4                      ;register 4 (memory mode):
  41.   out dx,al                     ;clear bit 3 -> chain4 off
  42.   inc dx
  43.   in al,dx
  44.   and al,0f7h
  45.   or al,4h                      ;set bit 2 -> odd/even mode off
  46.   out dx,al
  47.   dec dx
  48.  
  49.   mov ax,0f02h                  ;register 2 (write plane mask):
  50.   out dx,ax                     ;0fh: all planes on during write
  51.   mov ax,0a000h                 ;clear video RAM
  52.   mov es,ax
  53.   xor di,di
  54.   xor ax,ax
  55.   mov cx,0ffffh
  56.   cld
  57.   rep stosw
  58.  
  59.   mov dx,3d4h                   ;CRTC
  60.   mov al,14h                    ;register 14h  (underline row address):
  61.   out dx,al
  62.   inc dx
  63.   in al,dx                      ;clear bit 6 -> doubleword address off
  64.   and al,0bfh
  65.   out dx,al
  66.   dec dx
  67.   mov al,17h                    ;register 17h (CRTC mode):
  68.   out dx,al                     ;set bit 6 -> byte mode on
  69.   inc dx
  70.   in al,dx
  71.   or al,40h
  72.   out dx,al
  73.   ret
  74. Endp
  75.  
  76. Enter400 proc pascal far        ;switches from mode X (200 rows)
  77.   mov dx,3d4h                   ;to extended 400 row mode
  78.   mov al,9                      ;CRTC register 9 (maximum row address)
  79.   out dx,al                     ;select
  80.   inc dx                        ;read out value 2
  81.   in al,dx
  82.   and al,01110000b              ;clear bits 7 and 3:0
  83.   out dx,al                     ;and write back
  84.   ret
  85. Enter400 endp
  86.  
  87. plane_l: db 0
  88. plane_pos: dw 0
  89.  
  90. p13_2_modex proc pascal far start,pic_size:word
  91.     mov dx,03ceh                ;set write mode 0
  92.     mov ax,4005h                ;via GDC register 5 (GDC mode)
  93.     out dx,ax
  94.  
  95.     mov b plane_l,1             ;store plane mask
  96.     push ds
  97.     lds si,dword ptr ds:vscreen ;load source address
  98.     mov w plane_pos,si          ;and store
  99.     mov ax,0a000h               ;set destination address
  100.     mov es,ax
  101.     mov di,start
  102.     mov cx,pic_size             ;get number
  103. @lpplane:
  104.     mov al,02h                  ;TS register 2 (write plane mask)
  105.     mov ah,b plane_l            ;mask corresponding plane
  106.     mov dx,3c4h
  107.     out dx,ax
  108.  
  109.   @lp1:
  110.     movsb                       ;copy byte
  111.     add si,3                    ;position at next source byte
  112.     loop @lp1
  113.  
  114.     mov di,start                ;get destination address again
  115.     inc w plane_pos             ;source address to new start
  116.     mov si,w plane_pos
  117.     mov cx,pic_size             ;get size
  118.     shl b plane_l,1             ;mask next plane
  119.     cmp b plane_l,10h           ;all 4 planes copied ?
  120.     jne @lpplane
  121.  
  122.     pop ds
  123.     ret
  124. Endp
  125. Split proc pascal far row:byte  ;screen splitting in "row" row
  126.   mov bl,row
  127.   xor bh,bh
  128.   shl bx,1                      ;*2 because of row duplication
  129.   mov cx,bx
  130.  
  131.   mov dx,3d4h                   ;CRTC
  132.   mov al,07h                    ;register 7 (overflow low)
  133.   out dx,al
  134.   inc dx
  135.   in al,dx
  136.   and al,11101111b              ;load bit 4 with bit 8 of the row
  137.   shr cx,4
  138.   and cl,16
  139.   or al,cl
  140.   out dx,al                     ;and set
  141.  
  142.   dec dx
  143.   mov al,09h                    ;register 9 (maximum row address)
  144.   out dx,al
  145.   inc dx
  146.   in al,dx
  147.   and al,10111111b              ;load bit 6 with bit 9 of the row
  148.   shr bl,3
  149.   and bl,64
  150.   or al,bl
  151.   out dx,al                     ;and set
  152.  
  153.   dec dx
  154.   mov al,18h                    ;register 18h (line compare/split screen)
  155.   mov ah,row                    ;set remaining 8 bits
  156.   shl ah,1
  157.   out dx,ax
  158.   ret
  159. Endp
  160. SetStart proc pascal far t:word ;sets screen start to specified address
  161.   mov dx,3d4h                   ;CRTC
  162.   mov al,0ch                    ;register 0ch(linear starting address middle)
  163.   mov ah,byte ptr t + 1         ;set bits 15:8
  164.   out dx,ax                     ;register 0dh(LSA low)
  165.   mov al,0dh                    ;set bits 7:0
  166.   mov ah,byte ptr t
  167.   out dx,ax
  168.   ret
  169. Endp
  170. WaitRetrace proc pascal far
  171.   mov dx,3dah                   ;input status register 1
  172. @wait1:
  173.   in al,dx                      ;bit 3 becomes 0 when beam busy redrawing image
  174.   test al,08h
  175.   jnz @wait1
  176. @wait2:
  177.   in al,dx                      ;bit 3 becomes 1 when retrace
  178.   test al,08h
  179.   jz @wait2
  180.   ret                           ;now the beam is at the bottom of the screen
  181. Endp
  182. public squeeze
  183. squeeze proc pascal far         ;squeezes screen together
  184.   mov si,200*80                 ;initial value for start address
  185.   mov di,199                    ;initial value for split row
  186. sqlp:                           ;main loop
  187.   call waitretrace              ;wait for retrace
  188.   call split pascal, di         ;set lower half by splitting
  189.   call setstart pascal, si      ;set upper half by scrolling
  190.   sub si,80                     ;one row further, so go down
  191.   dec di                        ;split one row down, so
  192.   cmp di,99d                    ;move lower half up
  193.   jae sqlp                      ;finished ?
  194.   ret
  195. squeeze endp
  196. clrx proc pascal far pmask:byte ;clears mode X - pages
  197.   mov al,02h
  198.   mov ah,pmask
  199.   mov dx,3c4h
  200.   out dx,ax
  201.   mov ax,0a000h                 ;get start address and length
  202.   mov es,ax
  203.   mov di,vpage
  204.   xor ax,ax
  205.   mov cx,8000
  206.   rep  stosw                    ;and clear
  207.   ret
  208. clrx endp
  209. copyscreen proc pascal far destination,source:word
  210.   mov dx,3c4h           ;select all planes
  211.   mov ax,0f02h
  212.   out dx,ax
  213.   mov dx,3ceh                   ;write-mode 1 (copy)
  214.   mov ax,4105h
  215.   out dx,ax
  216.  
  217.   push ds
  218.   mov ax,0a000h                                    ;source and destination segment in VGA
  219.   mov es,ax
  220.   mov ds,ax
  221.   mov si,source                 ;load source and destination offset
  222.   mov di,destination
  223.   mov cx,16000d                 ;copy 16000 bytes (=64000 pixels)
  224.   rep movsb
  225.   pop ds
  226.   mov dx,3ceh                   ;write-mode 0
  227.   mov ax,4005h
  228.   out dx,ax
  229.   ret
  230. copyscreen endp
  231. SetPal proc pascal far
  232.   push si
  233.   mov si,offset palette         ;get address
  234.   mov cx,256*3                  ;get number of colors
  235.   xor al,al
  236.   mov dx,03c8h                  ;external palette RAM, pixel write address
  237.   out dx,al                     ;set from color 0
  238.   inc dx                        ;pixel color value
  239.  
  240.   rep outsb                     ;send all colors to VGA
  241.   pop si
  242.   ret
  243. Endp
  244.  
  245. getpal proc pascal far
  246.   push di
  247.   mov di,offset palette         ;get address
  248.   mov cx,256*3                  ;get number of colors
  249.   xor al,al
  250.   mov dx,03c7h                  ;external palette RAM, pixel read address
  251.   out dx,al                     ;read from color 0
  252.   mov dx,3c9h                   ;pixel color value
  253.  
  254.   rep insb                      ;send all colors to VGA
  255.   pop di
  256.   ret
  257. Endp
  258.  
  259. double proc pascal far
  260.   mov dx,3d4h                   ;CRTC register 13h (row offset)
  261.   mov ax,5013h                  ;set to 80 (double width1)
  262.   out dx,ax                     ;and write
  263.   ret
  264. double endp
  265.  
  266.  
  267. fade_out proc pascal far        ;fades out image, video-mode independent
  268. local greatest:word             ;contains maximum possible color value
  269.   mov greatest,63
  270.   mov ax,ds                     ;load destination segment
  271.   mov es,ax
  272. main_loop:                      ;main loop, run once per image
  273.   lea si,palette                ;source and destination offset to palette
  274.   mov di,si
  275.   mov cx,768                    ;modify 768 bytes
  276. lp:
  277.   lodsb                         ;get value
  278.   dec al                        ;decrement
  279.   jns set                       ;if not yet negative -> set
  280.   xor al,al                     ;otherwise 0
  281. set:
  282.   stosb                         ;write destination value in "palette"
  283.  
  284.   dec cx                        ;loop counter
  285.   jne lp
  286.  
  287.   call waitretrace              ;synchronize to retrace
  288.   call setpal                   ;set calculated palette
  289.   dec greatest                  ;decrement outer loop
  290.   jne main_loop                 ;still not finished ? then continue
  291.  
  292.   ret
  293. fade_out endp
  294.  
  295.  
  296. fade_to proc pascal far destinationpal:dword, length1:word, step:byte
  297. ;fades "palette" to "destinationpal", passed by Pascal as array of byte !
  298. local greatest:word
  299.   mov ax,63                     ;calculate number of passes
  300.   div step                      ;necessary to reach 63
  301.   xor ah,ah
  302.   mov greatest,ax               ;set number of loop passes
  303. next_frame:
  304.   les di,destinationpal         ;get offset holen, Pascal passes arrays far !
  305.   lea si,palette                ;get offset of "palette"
  306.   mov cx,768                    ;process 768 bytes
  307.  
  308. continue:
  309.   mov al,[si]                   ;get value from current palette
  310.   mov ah,[di]                   ;get value from destinationpal
  311.  
  312.   mov bl,ah
  313.   sub bl,al                     ;difference to destination value
  314.   cmp bl,step                   ;more than one step over ?
  315.   jg up                         ;-> decrement
  316.   neg bl                        ;difference
  317.   cmp bl,step                   ;greater than negative step
  318.   jg down
  319.  
  320.   mov al,ah                     ;destination reached, finally set
  321.  
  322. write:
  323.   dec cx                        ;decrement color loop
  324.   je finished                   ;0 ? -> finished
  325.   mov [si],al                   ;write value in palette
  326.   inc si                        ;select next value
  327.   inc di
  328.   jmp continue                  ;and continue
  329. down:
  330.   sub al,step                   ;decrement
  331.   jmp write
  332. up:
  333.   add al,step                   ;increment
  334.   jmp write
  335. finished:                       ;palette calculated
  336.   call waitretrace              ;synchronization
  337.   call setpal                   ;set palette
  338.   dec greatest                  ;all 63 passes finished ?
  339.   jne next_frame                ;no -> continue
  340.  
  341.   ret
  342. fade_to endp
  343.  
  344. copy_block proc pascal far destination,source,width1,height1:word
  345. local jump:word
  346.   mov dx,3ceh                   ;GDC
  347.   mov ax,4105h                  ;readmode 0, writemode 1
  348.   out dx,ax                     ;on register 5 : GDC mode
  349.   mov dx,3c4h                   ;TS
  350.   mov ax,0f02h                  ;enable all planes
  351.   out dx,ax                     ;on register 2 : write plane mask
  352.  
  353.   push ds
  354.   mov ax,0a000h                 ;copy within VGA
  355.   mov es,ax                     ;-> both segments to 0a000h
  356.   mov ds,ax
  357.   mov si,source                 ;source data from source
  358.   mov di,destination            ;copy to destination
  359.   mov dx,height1                ;copy high rows
  360.  
  361.   mov ax,80                     ;calculate jump between two rows
  362.   sub ax,width1                 ;(= 80-width1)
  363.   mov jump,ax
  364.  
  365. line_lp:
  366.   mov cx,width1                 ;load width1
  367.   rep movsb                     ;copy a line
  368.   add si,jump
  369.   add di,jump
  370.  
  371.   dec dx                        ;continue line counter
  372.   jne line_lp
  373.  
  374.   pop ds
  375.   ret
  376. copy_block endp
  377.  
  378. Pal_Rot proc pascal far Start,End1:Word
  379.                                 ;rotates palette part by 1 from start to end1
  380.                                 ;if start < end1 : rotation down
  381.                                 ;if start > end1 : rotation up
  382.  
  383.   mov ax,ds                     ;es to data segment
  384.   mov es,ax
  385.  
  386.   lea si,palette                ;set palette offset
  387.   mov di,si                     ;also to di
  388.  
  389.   mov ax,3                      ;convert "start" to palette offset  '
  390.   mul start
  391.   add si,ax                     ;and add to si
  392.   mov ax,3                      ;same for destination
  393.   mul end1
  394.   add di,ax                     ;add to di
  395.  
  396.   mov bx,[si]                   ;store bytes of start color
  397.   mov dl,[si+2]
  398.  
  399.   mov cx,di                     ;difference between start and end is number
  400.   sub cx,si                     ;bytes to be copied
  401.  
  402.   mov di,si                     ;start color as destination offset
  403.   add si,3                      ;one color above as source offset
  404.                                 ;ready for forward copying
  405.   cld                           ;default: forward copy
  406.   or cx,cx                      ;if cx negative (start > end1)
  407.   jns forward
  408.   std                           ;then backwards copy
  409.   neg cx                        ;correct cx
  410.   sub si,4                      ;si to 2nd byte of penultimate color
  411.   add di,2                      ;di to 2nd byte of last color
  412.   add cx,2                      ;copy 2 bytes more,
  413. forward:                        ;so that position is correct after copy loop
  414.  
  415.   rep movsb                     ;copy colors
  416.   mov [di],bx                   ;bytes of old start color
  417.   mov [di+2],dl                 ;write as last color
  418.  
  419.   cld                           ;clear direction-flag again
  420.   ret
  421. Pal_Rot Endp
  422.  
  423. code ends
  424. end
  425.