comment +

For compilation  use:

Turbo  Assembler 2.02
tasm /m2 ditherat.asm
tlink /t ditherat.obj

code: bitl/7dump

comment end! +

mode60  = 0        ; enable tweaked 60hz VGA mode

.model tiny
.data
;v128    DW 128

pattern DB 0,8,2,10, 12,4,14,6, 3,11,1,9, 15,7,13,5

IF mode60
vga60 DW 0b06h, 3e07h, 0c310h, 8c11h, 8f12h, 9015h, 0b16h
ENDIF

.code
.486
org 0100h

start:

mov al,13h
int 10h

IF mode60
pusha
mov dx, 3d4h               ; Set tweaked 60hz VGA mode 320x200
push dx
mov al, 11h
out dx, al
inc dx
in al, dx
and al, 7fh
out dx, al
mov dx, 3C2h
mov al, 0e3h
out dx, al
pop dx
lea si, vga60
mov cx, 7
rep outsw
popa
ENDIF

add dh, al                 ; cs + 1300h
mov ds, dx
add dh, al
mov fs, dx
add dh, al
mov gs, dx


mov bh, 60    ; bh=60
mov dx, 000Ch ; dl=12
@r:
xor ch, ch
   @g:
   xor cl, cl
          @b:
          mov ax, 1010h
          int 10h
          inc bx
          add cl, dl;12
          cmp cl, bh;60
          jbe @b
   add ch, dl;12
   cmp ch, bh;60
   jbe @g
add dh, dl;12
cmp dh, bh;60
jbe @r


;**********************************************************************
; MAIN LOOP
;**********************************************************************

@mainloop:

push ds

mov dh, 3
@bl3:
push ds
pop es

in ax, 40h
xadd [si], ax
xor ah, al
mov bl,48
test al, 7
jnz @sk
shr bl, 2
@sk:
xchg di, ax

mov cl,31
A:mov al,31
B:pusha
  sub al,16
  imul al
  xchg ax,cx
  sub al,16
  imul al
  add al,cl
  popa
  jc S
  mov byte ptr ds:[di],bl
S:inc di
  dec al
  jnz B
  add di,320-31
  loop A

;---------------- BLUR----------------------------
     xor si, si
     mov di, 1
     mov ch, 3fh ;320*200/4+640
     @blur:
        lodsd
        add al, [si]
        add al, [si+320]
        add al, [si+640]
        shr al, 2
        stosd
     loop @blur
;--------------------------------------------------

mov ax, ds
add ah, 13h
mov ds, ax
dec dh
jnz @bl3

pop ds

push 0a000h
pop es

xor di, di ;n:=0;
mov si, 200
@y:  ;for y:=0 to 199 do for x:=0 to 319 do begin
mov bp, 319
@x:
   ; bayer:= pattern[(x and 3)+(y and 3)*4]*4;
   mov bx, bp
   and bx, 3
   mov ax, si
   and al, 3
   shl al, 2
   add bl, al
   mov cl, ss:[bx+pattern]

   mov al, ds:[di]
   mov bl, fs:[di]
   mov dl, gs:[di]

   add al, cl
   add bl, cl
   add dl, cl

   ;col:=byte( (r*6 div 64)*36 + (g*6 div 64)*6 + (b*6 div 64) );
   mov cl, 6
   mul cl
   imul bx, cx
   imul dx, cx

   shr ax, cl
   shr bx, cl
   shr dx, cl

   imul ax, ax, 36
   imul bx, cx

   add al, dl
   add al, bl

   stosb ;mem[vga:n]:=col; inc(n);

dec bp
jns @x

dec si
jnz @y


     mov  dx, 3DAh           ; Wait for vertical retrace
IF mode60
@w1:
     in   al, dx
     test al, 8
     jnz  @w1
ENDIF
@w2:
     in   al, dx
     test al, 8
     jz   @w2

in al, 60h                    ; wait for ESC
dec ax
jnz @mainloop                 ; none received, start over


;**********************************************************************
; EXIT
;**********************************************************************
;IF mode60
   mov ax, 3                ; back to text
   int 10h                  ; mode
;ENDIF

;int 20h                  ; exit
ret
end start

