Advertisement
Mysoft

Untitled

Sep 7th, 2016
567
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. cpu 386
  2. org 100h
  3.  
  4. _BDACurMode equ 0x449
  5. _FontSeg equ 0xF000
  6. _FontOff equ 0xFA6E
  7. _LineSz equ (640/8)
  8. _AttReg  equ 3C0h
  9. _SeqReg  equ 3C4h
  10. __SeqPlaneMask equ 02h
  11. _SeqPal  equ 3C8h
  12. _GfxReg  equ 3CEh
  13. __GfxLogicOp   equ 03h
  14. __GfxMode      equ 05h
  15. __GfxMem       equ 06h
  16. __GfxBitMask   equ 08h
  17. _StatReg equ 3DAh
  18. ; --- [wFlags] ---
  19. _bUseMouse   equ 1
  20. _bNeedUpdate equ 2
  21.  
  22. %macro If8 1+  
  23.     %1
  24. %endmacro
  25. %macro If16 1+
  26.     ;%1
  27. %endmacro
  28. %macro If1632 1+
  29.     ;%1
  30. %endmacro
  31. %macro If32 1+
  32.     ;%1
  33. %endmacro
  34.  
  35. %define SetFlag(_flag_) or byte [wFlags], (_flag_)
  36. %define TestFlag(_flag_) test byte [wFlags], (_flag_)
  37. %define ResetFlag(_flag_) and byte [wFlags], (~(_flag_))
  38.  
  39. section .text
  40. Start:      
  41.    
  42.     call InitApp
  43.     call InitPalette    
  44.     call LinearMode
  45.    
  46.     mov dx, sBmpFile
  47. .ReloadBitmap: 
  48.     call LoadBitmap
  49.  
  50.     SetFlag(_bNeedUpdate)
  51. .MainLoop:
  52.    
  53.     TestFlag(_bNeedUpdate)
  54.     jz .NoPalUpdate
  55.     xor si,si
  56.     xor cx, cx    
  57.     call CalculatePaletteConversion
  58. .NoPalUpdate:
  59.  
  60.     call Blit256to16x2
  61.        
  62.     mov si, sMessage+5
  63.     mov ax, [iShowFPS]
  64.     call IntToStr
  65.     mov byte [si], 0    
  66.     mov si, sMessage    
  67.     xor di, di
  68.     call DrawText
  69.    
  70.     mov al, [iCurPage]
  71.     mov ah, 05h
  72.     int 10h  
  73.    
  74.     inc word [iCountFPS]
  75.     mov ah, 02h
  76.     int 1Ah
  77.     cmp [iSecondsBCD], dh
  78.     je .SkipAdjust
  79.     mov ax, [iCountFPS]
  80.     mov [iShowFPS], ax
  81.     mov word [iCountFPS], 0
  82.     mov [iSecondsBCD], dh
  83. .SkipAdjust:    
  84.    
  85.     ;mov ax, 0C07h ;clear keyboard / wait for key
  86.  
  87.     ResetFlag(_bNeedUpdate)           ; non zero when it's to update
  88. ;---------- Checking Keyboard ----------
  89.     .NextKey:
  90.     mov ah,0Bh    
  91.     int 21h
  92.     test al, al
  93.     jz .NoMoreKeys
  94.     mov ah,07
  95.     int 21h
  96.     test al, al
  97.     jnz .GotNormalKey
  98.     mov ah,07
  99.     int 21h
  100. .GotExtendedKey:
  101.     jmp .NextKey
  102. .GotNormalKey:
  103.     cmp al, 27
  104.     je .Done
  105.     cmp al, 'A'
  106.     jb .NotUpper
  107.     cmp al, 'Z'
  108.     ja .NotUpper
  109.     mov [sBmpLoad+7],al
  110.     mov dx, sBmpLoad
  111.     jmp .ReloadBitmap
  112. .NotUpper:
  113.     cmp al, 'a'
  114.     jb .NotLower
  115.     cmp al, 'z'
  116.     ja .NotLower
  117.     mov [sBmpLoad+7],al
  118.     mov dx, sBmpLoad
  119.     jmp .ReloadBitmap
  120. .NotLower:    
  121.     jmp .NextKey
  122. .NoMoreKeys:
  123. ;----------- Checking Mouse ----------
  124.     TestFlag(_bUseMouse)
  125.     jz .NoMouseEvents
  126.     mov ax, 3
  127.     int 33h
  128.     test bl, 1
  129.     jz .NoMouseEvents
  130.     cmp [iMouseX], cx
  131.     jne .MouseMoved
  132.     cmp [iMouseY], dx
  133.     je .NoMouseEvents
  134. .MouseMoved:
  135.     SetFlag(_bNeedUpdate)
  136.     mov [iMouseX], cx
  137.     mov [iMouseY], dx
  138.  
  139.     cmp dx, 10
  140.     mov ax, 63    
  141.     jl .SetBrightness  
  142.     shl dx, 7          
  143.     mov ax, dx
  144.     xor dx, dx    
  145. .InLimitY:    
  146.     div word [iMaxY]   
  147. .SetBrightness:    
  148.     mov dx, 640
  149.     sub dx, cx
  150.     shr dx, 5
  151.     add dx, 4
  152.     cmp ax, dx
  153.     jg .BrightInLimit
  154.     mov ax, dx
  155. .BrightInLimit:    
  156.     mov [iBrightness], ax
  157.     cmp cx,10
  158.     mov ax, -1
  159.     jl .SetContrast
  160.     mov bx, cx 
  161.     and bx, (~1)
  162.     mov ax,[ContrastTab+bx]
  163.     test cx, 1
  164.     jz .NoOddContrast
  165.     add ax,[ContrastTab+bx+2]
  166.     shr ax, 1
  167. .NoOddContrast:
  168. .SetContrast:
  169.     mov [iContrast], ax
  170.    
  171. .NoMouseEvents:
  172.     TestFlag(_bNeedUpdate)
  173.     ;jnz .MainLoop            
  174.     ;call ScreenSync
  175.     jmp .MainLoop
  176.     jmp .NextKey    
  177.  
  178. .Done:      
  179.     pop ax        ;return previous mode
  180.     int 10h
  181.     pop ax
  182.     mov sp,ax
  183.     add sp, 2
  184.     push 0
  185.     ret ;done
  186.  
  187. ; -------------------------------------------------------------------------------------------------
  188. ; -------------------------------------------------------------------------------------------------    
  189. ; -------------------------------------------------------------------------------------------------
  190. InitApp:    
  191.     mov ah,0Fh    ;get current mode
  192.     int 10h
  193.     xor ah,ah
  194.    
  195.     cli
  196.     pop cx    
  197.     mov dx, sp
  198.     mov sp, EndStack
  199.     push dx
  200.     push ax
  201.     push cx
  202.     sti
  203.    
  204.     mov al,10h   ;gfx mode (640x350x16)
  205.     int 10h
  206.    
  207.     ;push ds
  208.     ;mov ds, [EgaRgb] ;0000:xxxx
  209.     ;mov byte [_BDACurMode], 0x13  ; Current Video Mode = 0x13 (hack)    
  210.     ;pop ds
  211.    
  212.     push bp
  213.     push es
  214.     mov ax, 1130h
  215.     mov bh, 3h ;8x8 font
  216.     int 10h
  217.     mov ax, es    
  218.     mov [cs:pFontSeg], ax    
  219.     mov [cs:iFontOff], bp
  220.     pop es
  221.     pop bp
  222.    
  223.     ;mov ax, 0C0Bh ;clear keyboard '(07 would be to wait)
  224.     ;int 21h
  225.    
  226.     mov ax, ds
  227.     mov dx, Buffer  
  228.     shr dx, 4
  229.     add ax, dx    
  230.     mov [pBuffSeg], ax
  231.    
  232.     xor ax, ax
  233.     int 33h
  234.     test ax,ax
  235.     jz .NoMouse
  236.     SetFlag(_bUseMouse)
  237.     mov ax,8            ;**
  238.     mov CX,0            ;** Mouse Min/Max
  239.     mov DX,349          ;** 0 to 349
  240.     int 33h             ;**
  241.    
  242.     ;mov ax,1            ;   Show Cursor
  243.     ;int 33h             ;
  244. .NoMouse:
  245.        
  246.     ret
  247. ; -------------------------------------------------------------------------------------------------
  248. ScreenSync:
  249.     push ax
  250.     push dx
  251.     mov dx, 0x3DA
  252. .vBlank:    
  253.     in al, dx
  254.     test al, 8
  255.     jz .vBlank
  256. .vSync:
  257.     in al, dx
  258.     test al, 8
  259.     jnz .vSync
  260.     pop dx
  261.     pop ax
  262.     ret
  263. ; -------------------------------------------------------------------------------------------------
  264. InitPalette:
  265.     pusha
  266.     ;mov dx, _SeqReg
  267.     ;mov ax, __SeqPlaneMask+0x0F00
  268.     ;out dx, ax
  269.     ;mov dx, _GfxReg
  270.     ;mov ax, 0x0001
  271.     ;out dx, ax
  272.     ;mov ax, 0x0002
  273.     ;out dx, ax
  274.     ;mov ax, 0x0003
  275.     ;out dx, ax
  276.     ;mov ax, 0xFF08
  277.     ;out dx, ax
  278.     ;mov ax, 0x0F07
  279.     ;out dx, ax
  280.     ;mov ax, 0x0005
  281.     ;out dx, ax
  282.    
  283.     mov bx, _AttReg
  284.     mov dx, _StatReg        ; reading from this register
  285.     in al, dx               ; Attribute in index mode    
  286.     mov si, EgaPal          ; ega Pal array (out of 64 colors)
  287.     xchg bx, dx             ; attribute Register att,color,att,color,...
  288.     xor ax,ax               ; counter 0 to 15
  289. .NextAtt:    
  290.     out dx, al              ; write Attribute #
  291.     out dx, al
  292.     ;outsb                   ; write next color from DS:SI
  293.     add ax, 0x1001          ; point to next att (also add 16 to high byte)
  294.     jnc .NextAtt            ; when high byte overflows it's done (16/256)    
  295.     mov al, 0x20            ;
  296.     out dx, al              ; PAS = 1 (for normal operation)
  297.     xchg bx, dx             ; status register
  298.     in al, dx               ; Attribute in index mode    
  299.     popa
  300.     ret
  301. ; -------------------------------------------------------------------------------------------------
  302. ; -------------------------------------------------------------------------------------------------
  303. LinearMode:
  304.     pusha
  305.    
  306.     mov dx, _GfxReg
  307.     mov al, __GfxMode
  308.     out dx, al
  309.     inc dx
  310.     in al, dx
  311.     and al, (~3)  ;Write Mode 0
  312.     or al, 64     ;CHAIN-4 ON :]
  313.     out dx, al 
  314.    
  315.     mov si, EgaRgb
  316.     mov dx, _SeqPal
  317.     xor al, al
  318.     out dx, al
  319.     inc dx
  320.     mov cx, (16*3)
  321. .NextPAL:  
  322.     lodsb
  323.     shr al, 2
  324.     out dx, al
  325.     loop .NextPAL
  326.     sub si, (16*3)
  327.     mov cl, 0x10   
  328.     xor bx, bx
  329.     xor ax, ax
  330. .NextColor:
  331.     mov bl, cl
  332.     and bl, 15
  333.     mov di, bx 
  334.     shl di, 1
  335.     lea si, [bx+di+EgaRgb]
  336.     mov bl, cl
  337.     shr bl, 4
  338.     mov di, bx 
  339.     shl di, 1
  340.     lea di, [bx+di+EgaRgb]     
  341.     mov al, [si]
  342.     mov bl, [di]
  343.     add ax, bx
  344.     shr ax, 3
  345.     out dx, al             ;R
  346.     mov al, [si+1]
  347.     mov bl, [di+1]
  348.     add ax, bx
  349.     shr ax, 3
  350.     out dx, al             ;G
  351.     mov al, [si+2]
  352.     mov bl, [di+2]
  353.     add ax, bx
  354.     shr ax, 3
  355.     out dx, al             ;B
  356.     inc cl
  357.     jnz .NextColor
  358.    
  359.     popa
  360.     ret
  361.    
  362. ; -------------------------------------------------------------------------------------------------
  363. DrawText: ; SS:SI = zstring,  VidSeg:DI = target
  364.     pusha
  365.     push ds
  366.     push es
  367.    
  368.     mov ax,[iCurPage]
  369.     mov cx,[iFontOff]
  370.     mov bp, si
  371.     shl ax, 15    
  372.     mov es, [pVideoSeg]
  373.     add di, ax
  374.     mov ds, [pFontSeg]
  375.    
  376.     mov dx, _SeqReg              ;   Select plane (0-3)
  377.     mov al, __SeqPlaneMask       ;   will be used from now on
  378.     out dx, al                   ;   to write pixel per pixel
  379.     inc dx    
  380.        
  381. .NextChar:    
  382.     xor bx, bx 
  383.     add bl, [bp]
  384.     mov si, cx
  385.     jz .EndString
  386.     shl bx, 3
  387.     add si, bx 
  388.     mov bx, 0x08FF 
  389. .NextLine:
  390.    
  391.     mov al, 0xFF
  392.     out dx, al
  393.     mov word [es:di+_LineSz*0],0
  394.     mov word [es:di+_LineSz*1],0
  395.    
  396.     mov ah,[si]
  397.     shr ah, 1
  398.     adc al, al
  399.     shr ah, 1
  400.     adc al, al
  401.     shr ah, 1
  402.     adc al, al
  403.     shr ah, 1
  404.     adc al, al
  405.     shr ah, 1
  406.     adc al, al
  407.     shr ah, 1
  408.     adc al, al
  409.     shr ah, 1
  410.     adc al, al
  411.     shr ah, 1
  412.     adc al, al
  413.     out dx, al 
  414.     mov [es:di+_LineSz*0+0],bl
  415.     shr al, 4
  416.     mov [es:di+_LineSz*1+0],bl
  417.     out dx, al
  418.     mov [es:di+_LineSz*0+1],bl 
  419.     inc si
  420.     mov [es:di+_LineSz*1+1],bl
  421.     add di, (_LineSz*2)
  422.     dec bh
  423.     jnz .NextLine  
  424.     sub di, ((_LineSz*16)-2)   
  425.     inc bp
  426.     jmp .NextChar
  427. .EndString:
  428.     pop es
  429.     pop ds
  430.     popa
  431.     ret
  432. ; -------------------------------------------------------------------------------------------------
  433. IntToStr: ;AX = number ; SS:SI target ; (SI updated on return)
  434.     push ax
  435.     push cx
  436.     push dx
  437.     inc si
  438.     cmp ax, 10
  439.     jb .WriteNum   
  440.     inc si
  441.     cmp ax, 100
  442.     jb .WriteNum   
  443.     inc si
  444.     cmp ax, 1000   
  445.     jb .WriteNum
  446.     inc si
  447.     cmp ax, 10000
  448.     jb .WriteNum
  449.     inc si
  450. .WriteNum:
  451.     mov cx, 10
  452.     push si
  453. .NextDig:  
  454.     xor dx,dx
  455.     dec si
  456.     div cx
  457.     add dl, '0'
  458.     test ax,ax
  459.     mov [ss:si], dl
  460.     jnz .NextDig
  461.     pop si
  462.     pop dx
  463.     pop cx
  464.     pop ax
  465.     ret
  466.    
  467. ; -------------------------------------------------------------------------------------------------
  468. LoadBitmap: ;DS:DX = bitmap name
  469.     pusha
  470.  
  471.     mov ax, 3D00h ;Open File for read
  472.     int 21h
  473.     jc .Done
  474.  
  475.     mov bx,ax
  476.     mov ax,4200h  ;Seek from start
  477.     xor cx,cx
  478.     mov dx,54     ;palette start?
  479.     int 21h
  480.  
  481.     mov dx, pal256
  482.     mov cx,1024
  483.     mov ah,3Fh    ;read palette
  484.     int 21h
  485.        
  486.     mov ax, 0x3F3F
  487.     mov si, 1024
  488. .Pal256to64:  
  489.     shr word [pal256+si], 2    
  490.     shr word [pal256+si+2], 2
  491.     and word [pal256+si], ax
  492.     and word [pal256+si+2], ax
  493.     sub si, 4
  494.     jns .Pal256to64
  495.  
  496.     push ds
  497.     mov ds, [pBuffSeg]
  498.     mov dx, 174*320
  499.     mov cx, 320
  500. .NextLine:
  501.     mov ah, 3Fh   ;read a line
  502.     int 21h
  503.     sub dx, cx
  504.     jnc .NextLine
  505.     pop ds
  506.    
  507.     mov ah, 3Eh   ;close file
  508.     int 21h
  509.    
  510. .Done:    
  511.     popa
  512.     ret
  513. ; -------------------------------------------------------------------------------------------------
  514. %push
  515. CalculatePaletteConversion: ; SI = iStart ; CX = iCount
  516.         %define %$CNT bp+0
  517.         %define %$IR  bp+1
  518.         %define %$IG  bp+2
  519.         %define %$IB  bp+3
  520.         %define %$RD  bp+4
  521.         %define %$GD  bp+5
  522.         %define %$BD  bp+6
  523.        
  524.     pusha             ;-
  525.     sub sp, 8         ;| Prologue
  526.     mov bp, sp        ;-
  527.        
  528.     mov ax, si        ;-
  529.     test ah,ah        ;|
  530.     jnz .EndSub       ;| if cuint(iStart) > 255 then exit sub
  531.     add ax, cx        ;| if cuint(iStart+iCount) > 255 then exit sub
  532.     test ah, ah       ;|
  533.     jnz .EndSub       ;-
  534.     mov [%$CNT], cl
  535.    
  536.     ; IIGGRRBBiiggrrbb    
  537.     mov di, si
  538.     shl di, 6         ;di = iStart*16*2 (16 dither * 4 planes * 2 rows * 2 pixels) (bytes)
  539.     shl si, 2         ;si = iStart*4    (RGBZ666 palette array)
  540.     add di, ColorPat
  541.     add si, pal256
  542.    
  543. .StartEntry:
  544.     mov cl, 85                 ; CL = color step
  545.  
  546.     ;xor dh,dh                   ; -
  547.     ;xor al,al                   ; |
  548.     ;mov ah, [si+0]              ; |  AX = (pPal->R)*255
  549.     ;mov dl, ah                  ; |
  550.     ;sub ax, dx                  ; -
  551.  
  552. ; ==== adjust brightness/contrast of RGB and get dither difference ====
  553.     mov al, 255                  ; AX = (pPal->R)*255
  554.     mul byte [si+2]              ;
  555.     xor dx, dx                   ; AX /= iBrightness
  556.     div word [iBrightness]       ;
  557.     cmp word [iContrast],0       ; if iContrast >= 0 then
  558.     jle .NoContrastR             ;
  559.     mul ax                       ; AX = (AX*AX) / iContrast
  560.     div word [iContrast]         ;
  561. .NoContrastR:                    ;
  562.     test ah,ah                   ; if AX > 255 then AX = 255
  563.     jz .NoOverflowR              ;
  564.     mov ax,255                   ;
  565. .NoOverflowR:                    ;
  566.     mov ch, al                   ; RD = (AL mod &h85): IR = AL-RD
  567.     div cl                       ;
  568.     mov [%$RD], ah               ;                  
  569.     sub ch, ah                   ;
  570.     mov [%$IR], ch               ;
  571.    
  572.     mov al, 255                  ; AX = (pPal->G)*255
  573.     mul byte [si+1]              ;
  574.     xor dx, dx                   ; AX /= iBrightness
  575.     div word [iBrightness]       ;
  576.     cmp word [iContrast],0       ; if iContrast >= 0 then
  577.     jle .NoContrastG             ;
  578.     mul ax                       ; AX = (AX*AX) / iContrast
  579.     div word [iContrast]         ;
  580. .NoContrastG:                    ;
  581.     test ah,ah                   ; if AX > 255 then AX = 255
  582.     jz .NoOverflowG              ;
  583.     mov ax,255                   ;
  584. .NoOverflowG:                    ;    
  585.     mov ch, al                   ; GD = (AL mod &h85): IG = AL-GD
  586.     div cl                       ;
  587.     mov [%$GD], ah               ;                  
  588.     sub ch, ah                   ;
  589.     mov [%$IG], ch               ;
  590.  
  591.     mov al, 255                  ; AX = (pPal->B)*255
  592.     mul byte [si+0]              ;
  593.     xor dx, dx                   ; AX /= iBrightness
  594.     div word [iBrightness]       ;
  595.     cmp word [iContrast],0       ; if iContrast >= 0 then
  596.     jle .NoContrastB             ;
  597.     mul ax                       ; AX = (AX*AX) / iContrast
  598.     div word [iContrast]         ;
  599. .NoContrastB:                    ;
  600.     test ah,ah                   ; if AX > 255 then AX = 255
  601.     jz .NoOverflowB              ;
  602.     mov ax,255                   ;
  603. .NoOverflowB:                    ;    
  604.     mov ch, al                   ; BD = (AL mod &h85): IB = AL-BD
  605.     div cl                       ;
  606.     mov [%$BD], ah               ;                  
  607.     sub ch, ah                   ;
  608.     mov [%$IB], ch               ;
  609.  
  610. ; ==== Dither Loops ====
  611.     xor bx,bx                    ; Start Point of dither tab (iY = 0 to 63 step 2)  
  612.     xor cx,cx
  613.     push si                      ; SI needed to access palette table
  614. .NextDither:
  615.     mov dx, [bx+DitTab]          ; Dither Pixels 0/1
  616.     cmp dl, [%$RD]               ; extra level on Red?
  617.     sbb cl, cl                   ; cl = (RD>DitTab) and 85
  618.     and cl, 85                   ;
  619.     add cl, [%$IR]               ; R0 = IR+Dither
  620.     shr cl, 2                    ; N1 = RGBTab( R0 shr 6 , ... , ... )
  621.     and cl, 0b110000             ;
  622.     mov si, cx                   ;
  623.    
  624.     cmp dh, [%$GD]               ; extra level on Green?
  625.     sbb cl, cl                   ; cl = (GD>DitTab+1) and 85
  626.     and cl, 85                   ;
  627.     add cl, [%$IG]               ; G0 = IG+Dither
  628.     shr cl, 4                    ; N1 = RGBTab( ... , G1 shr 6 , ... )
  629.     and cl, 0b001100             ;
  630.     add si, cx                   ;
  631.  
  632.     cmp dl, [%$BD]               ; extra level on Blue?
  633.     sbb cl, cl                   ; cl = (BD>DitTab) and 85  
  634.     and cl, 85                   ;
  635.     add cl, [%$IB]               ; B0 = IB+Dither
  636.     shr cl, 6                    ; N1 = RGBTab( ... , ... , B0 shr 6 )
  637.     add si, cx                   ;
  638.  
  639.     mov al, [RGBTab+si]          ; var N1 = RGBTab(R0 shr 6,G1 shr 6,B0 shr 6)
  640.     ;mov [di],al                  ; tColorTab(IDX+0) = N1    
  641.     ;inc di                       ; 'IDX += 2'
  642.    
  643.     mov dx, [bx+DitTab+8]        ; Dither Pixels 8/9
  644.     cmp dl, [%$RD]               ; extra level on Red?
  645.     sbb cl, cl                   ; cl = (RD>DitTab) and 85
  646.     and cl, 85                   ;
  647.     add cl, [%$IR]               ; R2 = IR+Dither
  648.     shr cl, 2                    ; N3 = RGBTab( R2 shr 6 , ... , ... )
  649.     and cl, 0b110000             ;
  650.     mov si, cx                   ;
  651.    
  652.     cmp dh, [%$GD]               ; extra level on Green?
  653.     sbb cl, cl                   ; cl = (GD>DitTab+1) and 85
  654.     and cl, 85                   ;
  655.     add cl, [%$IG]               ; G3 = IG+Dither
  656.     shr cl, 4                    ; N3 = RGBTab( ... , G3 shr 6 , ... )
  657.     and cl, 0b001100             ;
  658.     add si, cx                   ;
  659.  
  660.     cmp dl, [%$BD]               ; extra level on Blue?
  661.     sbb cl, cl                   ; cl = (BD>DitTab) and 85  
  662.     and cl, 85                   ;
  663.     add cl, [%$IB]               ; B2 = IB+Dither
  664.     shr cl, 6                    ; N3 = RGBTab( ... , ... , B2 shr 6 )
  665.     add si, cx                   ;
  666.    
  667.     mov ah, [RGBTab+si]          ; var N1 = RGBTab(R0 shr 6,G1 shr 6,B0 shr 6)
  668.     ;mov [di], ah                 ; tColorTab(IDX+1) = N1    
  669.     ;inc di                       ; 'IDX += 1'
  670.     mov [di], ax
  671.     add di, 2
  672.    
  673.     add bl, 2                    ; if ((iY+2) and 7)=0 then iY += 8
  674.     test bl, 7                   ; every row finished must skip another row
  675.     jnz .NextDither              ;
  676.     add bl, 8                    ;
  677.     cmp bl, 63                   ; 'next iY'
  678.     jle .NextDither              ; that only need to be checked each row
  679.    
  680.     dec byte [%$CNT]             ; next entry
  681.     pop si                       ; restore saved SI
  682.     lea si, [si+4]
  683.     jnz .StartEntry
  684.    
  685. .EndSub:
  686.     add sp, 8         ;-
  687.     popa              ;| Epilogue
  688.     ret               ;-
  689. %pop
  690. ; -------------------------------------------------------------------------------------------------
  691. %push
  692. Blit256to16x2:    
  693.     pusha
  694.     push ds
  695.     push es
  696.    
  697.     ;xor di, di                   ; es:di is the 640x350 planar screen
  698.     xor bx, bx                    ; ds:bx is the source bitmap
  699.     xor byte [iCurPage], 1
  700.     mov di,[iCurPage]
  701.     shl di, 15
  702.      
  703.     mov es, [pVideoSeg]
  704.     mov ds, [pBuffSeg]
  705.        
  706.     mov dx, _GfxReg              ;**
  707.     mov al, __GfxMode            ;**
  708.     out dx, al                   ;**
  709.     inc dx                       ;**
  710.     in al,dx                     ;** Set EGA Mode X / Write 0
  711.     and al, (~3)                 ;**
  712.     or al, 64                    ;**
  713.     out dx, al                   ;**
  714.    
  715.     mov dx, _SeqReg              ;   Select plane (0-3)
  716.     mov al, __SeqPlaneMask       ;   will be used from now on
  717.     out dx, al                   ;   to write pixel per pixel    
  718.    
  719.     mov bp, ColorPat             ; bp is used to access ColorPat on SS=CS        
  720.     mov word [cs:bPlane], 0x1001    
  721. .NextPlane:
  722.     push bp                      ; save dither X too
  723.     push di                      ; save pointers to restart
  724.     push bx                      ; src and target :)
  725.     mov dx, (_SeqReg+1)          ; Sequencer data (PlaneMask)
  726.     mov cl,175                   ; 175 input lines
  727.     mov al, [cs:bPlane]          ; Set New plane
  728.     out dx,al                    ; on PlaneMask register (already selected)
  729. .NextRow:
  730.     mov ch,20    
  731. .NextBlk:
  732.        
  733.     movzx si, byte [bx+8]        ; pixel 8 from 256 color source
  734.     shl si,5                     ; si = pIN[X] shl 5    
  735.     mov dx,[bp+si]               ; pixels pattern for line 1/2 (dither column 0-1, row 0-1)    
  736.     If8 mov [es:di+2], dl        ; - 8 bit (first unroll the write)
  737.    
  738.     movzx si, byte [bx+12]       ; pixel 12 from 256 color source
  739.     shl si,5                     ; si = pIN[X] shl 5
  740.     If8 mov [es:di+82], dh       ; - 8 bit (first unroll the write)    
  741.     mov ax,[bp+si]               ; pixels pattern for line 1/2 (dither column 0-1, row 0-1)    
  742.     If1632 xchg al, dh           ; --- 16/32 bit (swap row/col order)
  743.    
  744.     If32 shl edx, 16             ; ---- 32 bit (just rotate to do single 32bit write)
  745.     If32 shl eax, 16             ; ---- 32 bit (just rotate to do single 32bit write)
  746.     If16 mov [es:di+2], dx       ; -- 16 bit (first unroll the write)    
  747.     If8 mov [es:di+3], al        ; - 8 bit (second unroll the write)
  748.        
  749.     movzx si, byte [bx+0]        ; pixel 0 from 256 color source    
  750.     shl si,5                     ; si = pIN[X] shl 5
  751.     If16 mov [es:di+82], ax      ; -- 16 bit (second unroll the write)
  752.     If8 mov [es:di+83], ah       ; - 8 bit (second unroll the write)    
  753.     mov dx,[bp+si]               ; pixels pattern for line 1/2 (dither column 0-1, row 0-1)    
  754.     If8 mov [es:di+0], dl        ; - 8 bit (third unroll the write)
  755.        
  756.     movzx si, byte [bx+4]        ; pixel 4 from 256 color source
  757.     shl si,5                     ; si = pIN[X] shl 5
  758.     If8 mov [es:di+80], dh       ; - 8 bit (third unroll the write)    
  759.     mov ax,[bp+si]               ; pixels pattern for line 1/2 (dither column 0-1, row 0-1)    
  760.     If1632 xchg al, dh           ; --- 16/32 bit (swap row/col order)
  761.    
  762.     If32 mov [es:di], edx        ; ---- 32 bit (write the final 32bit)    
  763.     If16 mov [es:di], dx         ; -- 16 bit (write the last 16 bit unroll)    
  764.     If8 mov [es:di+1], al        ; - 8 bit (last unroll the write)
  765.            
  766.     add bx, 16                   ; end of unroll... 4 input pixels read
  767.     add di, 4                    ; and 4 output pixels (1 byte : 2 lines of a plane)
  768.     dec ch                       ; next block (20 blocks = 80 pixels of 320 on 4 planes)
  769.     If32 mov [es:di+80-4], eax   ; ---- 32 bit (write the final 32bit)    
  770.     If16 mov [es:di+80-4], ax    ; -- 16 bit (write the last 8 bit unroll)    
  771.     If8 mov [es:di+81-4], ah     ; - 8 bit (last unroll the write)
  772.     jnz .NextBlk                 ; continue until all blocks are done
  773.     add bp, 8                    ; end of a row so advance dither row...
  774.     add di, 80                   ; two output lines are done so skip one
  775.     and bp, (~32)                ; and mod the dither row 0-7. (&31 preservering >=128)
  776.     dec cl                       ; there's more lines to read?
  777.     jnz .NextRow                 ; keep going until all 175 is read
  778.    
  779.     pop bx                       ; Restore initial source
  780.     pop di                       ; Restore initial target
  781.     pop bp                       ; Restore current dither X    
  782.     inc bx                       ; Source on next plane
  783.     add bp,2                     ; Next Dither (2 pixels)
  784.     shl word [cs:bPlane], 1      ; Next Plane / Counter
  785.     jnc .NextPlane               ; and go back until all 4 planes is done
  786.    
  787. .EndSub:
  788.     pop es
  789.     pop ds
  790.    
  791. TestFlag(_bUseMouse)
  792.     jz .NoMouse
  793.     ;mov ax,2
  794.     ;int 33h
  795.     ;mov ax,1Dh
  796.     ;mov bx,[iCurPage]
  797.     ;int 33h    
  798.     ;mov ax,1
  799.     ;int 33h
  800.     jmp .DoneFlip
  801. .NoMouse:  
  802. .DoneFlip:
  803.    
  804.     popa    
  805.     ret
  806. %pop
  807.  
  808. ; -------------------------------------------------------------------------------------------------
  809. ; -------------------------------------------------------------------------------------------------
  810. ; -------------------------------------------------------------------------------------------------
  811.  
  812. section .data
  813. sBmpLoad:    db "BMP\VGAA.BMP",0
  814. sBmpFile:    db "vga.bmp",0
  815. sMessage:    db "Fps:     ",0
  816. wFlags:      db 0
  817. iSecondsBCD: db -1
  818. bPlane       db 0                  ;Write on 4 planes of 16 colors
  819. align 4
  820. iShowFPS:    dw 0
  821. iCountFPS:   dw 0
  822. iCurPage:    dw 0
  823. iMouseX:     dw -1
  824. iMouseY:     dw -1
  825. iMaxY:       dw 349
  826. iBrightness: dw 63
  827. iContrast:   dw -1
  828. pBuffSeg:    dw 0
  829. pVideoSeg:   dw 0xA000
  830. pFontSeg:    dw _FontSeg
  831. iFontOff:    dw _FontOff
  832. align 4
  833. EgaPal:
  834. db 00,08,01,09,32,33,41,04
  835. db 05,13,36,37,45,24,10,26
  836. DitTab: ;8x8 ordered dither mask (0-84)
  837. db 00,64,16,80,04,68,20,84
  838. db 43,21,59,37,47,25,63,41
  839. db 11,75,05,69,15,79,09,73
  840. db 53,32,48,27,57,36,52,31
  841. db 03,67,19,83,01,65,17,81
  842. db 45,24,61,40,44,23,60,39
  843. db 13,77,08,72,12,76,07,71
  844. db 56,35,51,29,55,33,49,28
  845. EgaRgb:
  846. db 000,000,000, 000,000,085
  847. db 000,000,170, 000,000,255
  848. db 085,000,000, 085,000,170
  849. db 085,000,255, 170,000,000
  850. db 170,000,170, 170,000,255
  851. db 255,000,000, 255,000,170
  852. db 255,000,255, 000,085,085
  853. db 000,170,085, 000,255,085
  854. RGBTab:
  855. db 000,001,002,003,208,208,210,211
  856. db 224,224,226,227,240,240,242,243
  857. db 004,020,005,006,212,212,213,214
  858. db 228,228,229,230,244,244,245,246
  859. db 007,023,008,009,215,215,216,217
  860. db 231,231,232,233,247,247,248,249
  861. db 010,026,011,012,218,218,219,220
  862. db 234,234,235,236,250,250,251,252
  863. ContrastTab:
  864. dw 11, 11, 12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 15, 15, 15
  865. dw 15, 15, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 19, 19, 19, 19, 20, 20, 20
  866. dw 21, 21, 21, 22, 22, 22, 23, 23, 23, 24, 24, 24, 25, 25, 26, 26, 26, 27, 27, 28
  867. dw 28, 28, 29, 29, 30, 30, 31, 31, 32, 32, 33, 33, 34, 34, 35, 35, 36, 36, 37, 37
  868. dw 38, 38, 39, 40, 40, 41, 42, 42, 43, 43, 44, 45, 45, 46, 47, 48, 48, 49, 50, 51
  869. dw 51, 52, 53, 54, 55, 55, 56, 57, 58, 59, 60, 61, 62, 63, 63, 64, 65, 66, 67, 68
  870. dw 70, 71, 72, 73, 74, 75, 76, 77, 78, 80, 81, 82, 83, 85, 86, 87, 89, 90, 91, 93
  871. dw 94, 96, 97, 99, 100, 102, 103, 105, 106, 108, 110, 111, 113, 115, 116, 118, 120
  872. dw 122, 124, 126, 127, 129, 131, 133, 135, 138, 140, 142, 144, 146, 148, 151, 153
  873. dw 155, 158, 160, 162, 165, 167, 170, 173, 175, 178, 181, 183, 186, 189, 192, 195
  874. dw 198, 201, 204, 207, 210, 213, 217, 220, 223, 227, 230, 234, 237, 241, 245, 248
  875. dw 252, 256, 260, 264, 268, 272, 276, 280, 285, 289, 293, 298, 303, 307, 312, 317
  876. dw 321, 326, 331, 336, 342, 347, 352, 357, 363, 368, 374, 380, 386, 391, 397, 404
  877. dw 410, 416, 422, 429, 435, 442, 449, 456, 463, 470, 477, 484, 491, 499, 507, 514
  878. dw 522, 530, 538, 546, 555, 563, 572, 581, 590, 599, 608, 617, 626, 636, 646, 656
  879. dw 666, 676, 686, 697, 707, 718, 729, 740, 751, 763, 775, 786, 798, 811, 823, 836
  880. dw 848, 861, 874, 888, 901, 915, 929, 943, 958, 972, 987, 1002, 1018, 1033, 1049
  881. dw 1065, 1081, 1098, 1114, 1131, 1149, 1166, 1184, 1202, 1221, 1239, 1258, 1277
  882. dw 1297, 1317, 1337, 1357, 1378, 1399, 1420
  883.  
  884. ;ColorPat: incbin  "ColorTab.bin"
  885. ;ColorPat: times 16384 db 255
  886.  
  887. section .bss
  888. alignb 64
  889. stack:    resb 256 ;times 256 db 0
  890. EndStack:
  891. ColorPat: resb 8192 ;times 16384 db 0
  892. pal256:   resb 1024 ;times 1024 db 0
  893. Buffer:   resb 64000 ;times 64000 db 0
  894. EOF:
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement