Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

tunnel

brewskibrewski Posts: 18Member
; simple tunnel-effect in real mode. for tasm. (use tlink/t)
; by brewski
; (brewski@graffiti.net)
;
; Will assemble below 256 bytes, don't remember
; it's old stuff...perhaps fun for someone?-Make it smaller =)
;
;

.MODEL TINY
.386
.CODE
IDEAL
ORG 0100h


START:

mov al,13h ; assume ah=0
int 10h

mov ax,cs
mov ds,ax ; codesegment is datasegment
add ah,10h
mov es,ax ; ugly texture
add ah,10h
mov fs,ax ; u tunnel coordinates
add ah,10h
mov gs,ax ; v tunnel coordinates
add ah,10h
mov [offscreen],ax ; offscreen buffer for later use

mov dx,3c8h
xor al,al
out dx,al
inc dx
mov cx,256
xor al,al
@@black:
out dx,al
out dx,al
out dx,al
inc al
loop SHORT @@black


xor di,di
xor bx,bx
@@texture:
mov al,bl
xor al,bh
stosb
dec bl
jne SHORT @@texture
dec bh
jne SHORT @@texture

calcTunnel:

mov di,64000-1
mov cx,100
@rX:
mov bx,160
@@capl:
; u = lowByteOf: (int)( 8192/Math.sqrt(i*i+j*j) )
mov [i],bx
mov [j],cx
finit
fild [constant1]
fild [i]
fmul st(0),st(0)
fild [j]
fmul st(0),st(0)
faddp st(1),st

fsqrt
fdivp st(1),st
fistp [word ptr temp]
mov ax,[temp]
mov [fs:di],al
; v = lowByteOf: (int)( atan2(i,j)*128/Math.PI )
fild [i]
fild [j]
fpatan
fimul [constant2]
fldpi
fdivp st(1),st
fistp [temp]
mov ax,[temp]
mov [gs:di],al

dec di
dec bx
cmp bx,-160
jne SHORT @@capl
dec cx
cmp cx,-100
jne @rX

tAnimate:

inc [displace]
mov dl,[displace]

; do Static Texture Mapping
; uses four segment registers for a mean and lean little loop
; ds=offscreen buffer segment
; es=texture segment
; fs=tunnel u's
; gs=tunnel v's
; registers:
; dl=displacement for u and v


push ds

mov ax,[offscreen]
mov ds,ax

mov di,64000-1
xor ah,ah
@@static:
xor bh,bh
mov bl,[gs:di]
add bl,dl
sal bx,8
mov al,[fs:di]; index to texture is now in bx
add al,dl
add bx,ax
mov al,[es:bx]
mov [ds:di-1],al
dec di
jne SHORT @@static


; wait for vertical raster retrace

; mov dx,03dah
; @@one:
; in al,dx
; and al,8
; jz SHORT @@one



; ( offscreen segment is still in ds )
push es ; save
xor di,di
xor si,si
mov cx,64000
push 0a000h
pop es
rep movsb
pop es ; restore texture segment
pop ds ; restore data segment (value is cs)


mov ah,11h
int 16h
jnz SHORT @@finish
jmp tAnimate

@@finish:

mov ax,0003h
int 10h
ret

constant1 dw 8192
constant2 dw 128
i dw ?
j dw ?
z dw ?
temp dw ?

mup db ?

displace db ?
offscreen dw ?

END START

Comments

  • espydudeespydude Posts: 7Member
    Thank you for being the first person to add some code. I'am hoping that this type of message board works. Any body with comments please feel free to post or send a message to me.


    Thanks!



    [blue][size=3]Question [b][italic]everything![/italic][/b][/size][/blue]

    Espydude

  • blipblip Posts: 756Member
    ;Smaller code that also pipelines better:

    [code]code segment byte public use16 'code'
    assume cs:code,ds:Code,es:code,ss:code
    org 100h
    386p
    ideal
    start:
    mov ax,13h ;Do NOT assume AH=0, it's 09h when I run my own proggies!
    int 10h

    mov ax,cs
    mov ds,ax
    add ah,10h
    mov es,ax ;ugly texture
    add ah,10h
    mov fs,ax ;u tunnel coordinates
    add ah,10h
    mov gs,ax ;v tunnel coordinates
    add ah,10h
    mov [offscreen],ax ;offscreen buffer for later use

    mov dx,3c8h ;Oh no! A palette change! ;-)
    xor al,al
    out dx,al
    inc dx
    xor al,al
    mov cx,256 ;By swapping this with XOR AL,AL a pipe stall was prevented.
    @@black:
    out dx,al
    out dx,al
    out dx,al
    inc ax ;INC AX is smaller than INC AL.
    loop SHORT @@black

    xor di,di
    xor bx,bx
    @@texture:
    mov al,bl
    xor al,bh
    stosb
    dec bl
    jne SHORT @@texture
    dec bh
    jne SHORT @@texture

    calcTunnel:
    mov di,64000-1
    mov cx,100
    @rX:
    mov bx,160
    @@capl:
    ;u = lowByteOf(int)( 8192/Math.sqrt(i*i+j*j) )
    mov [i],bx
    mov [j],cx
    finit
    fild [constant1]
    fild [i]
    fmul st(0),st(0)
    fild [j]
    fmul st(0),st(0)
    faddp st(1),st

    fsqrt
    fdivp st(1),st
    fistp [word ptr temp]
    mov ax,[temp]
    mov [fs:di],al
    ; v = lowByteOf(int)( atan2(i,j)*128/Math.PI )
    fild [i]
    fild [j]
    fpatan
    fimul [constant2]
    fldpi
    fdivp st(1),st
    fistp [temp]
    mov ax,[temp]
    mov [gs:di],al

    dec di
    dec bx
    cmp bx,-160
    jne SHORT @@capl
    dec cx
    cmp cx,-100
    jne @rX

    tAnimate:
    inc [displace]
    mov dl,[displace]

    ; do Static Texture Mapping
    ; uses four segment registers for a mean and lean little loop
    ; ds=offscreen buffer segment
    ; es=texture segment
    ; fs=tunnel u's
    ; gs=tunnel v's
    ; registers:
    ; dl=displacement for u and v


    push ds
    mov ds,[offscreen] ;This is better than MOV AX,[offscreen] / MOV DS,AX.

    mov di,64000-1
    xor ah,ah
    @@static:
    movzx bx,[gs:di] ;This is better than XOR BH,BH / MOV BL,[GS:DI]
    add bl,dl
    sal bx,8
    mov al,[fs:di]; index to texture is now in bx
    add al,dl
    add bx,ax
    mov al,[es:bx]
    mov [ds:di-1],al
    dec di
    jne SHORT @@static


    ; wait for vertical raster retrace

    ; mov dx,03dah
    ; @@one:
    ; in al,dx
    ; and al,8
    ; jz SHORT @@one



    ; ( offscreen segment is still in ds )
    push es ; save
    mov cx,64000
    push 0a000h
    xor si,si ;Prevent some pipeline stalls by separating a PUSH and a POP.
    xor di,di
    pop es
    rep movsb
    pop es ; restore texture segment
    pop ds ; restore data segment (value is cs)


    mov ah,11h
    int 16h
    jz tAnimate ;This is better than JNZ SHORT @@finish / JMP tAnimate.

    @@finish:

    mov ax,0003h
    int 10h
    ret

    constant1 dw 8192
    constant2 dw 128
    i dw ?
    j dw ?
    z dw ?
    temp dw ?

    mup db ?

    displace db ?
    offscreen dw ?

    code ends
    END START[/code]
  • brewskibrewski Posts: 18Member
  • angelusMortisangelusMortis Posts: 141Member
    coud anyone explain the effect to me, please ????
  • blipblip Posts: 756Member
    I haven't compiled it, so I couldn't tell you but I do have two other great tunnel programs (I don't have the sources, though).
  • angelusMortisangelusMortis Posts: 141Member
    i mean source code sharing is a very good idea. but what do i need it
    for if i cant undestand the source?
    so if there is someone who can explain the effect to me, please reply.





    : I haven't compiled it, so I couldn't tell you but I do have two other great tunnel programs (I don't have the sources, though).
    :

  • brewskibrewski Posts: 18Member
    [b][red]This message was edited by the brewski at 2002-6-23 0:7:8[/red][/b][hr]
    [b][red]This message was edited by the brewski at 2002-6-23 0:4:59[/red][/b][hr]
    [b][red]This message was edited by the brewski at 2002-6-23 0:2:14[/red][/b][hr]
    : i mean source code sharing is a very good idea. but what do i need it
    : for if i cant undestand the source?
    : so if there is someone who can explain the effect to me, please reply.
    :
    :
    :
    :
    :
    : : I haven't compiled it, so I couldn't tell you but I do have two other great tunnel programs (I don't have the sources, though).
    : :
    :
    :
    Hi AngelusMortis!

    I can explain what I remember of how this kind of effect is working, with some c-code, hope
    that's ok.
    Lots of look up tables:

    One that precalculates a texture which is 256x256 pixels large.
    In this case, by xor'ing in a loop, produces an image with squares.
    This texture is where pixels or texels will be fetched from later.
    In c code would be:

    int index=0;
    for(int y=0;y<256;y++){
    for(int x=0;x<256;x++){
    texture[y+x*256]=(char)(x^y);
    index++;
    }}

    Next, precalculate the u and v buffers.
    In the source there's one 64k segment for the u's, and another one for the v's.
    These buffers contains memory offsets which points to the texture-segment.
    For every coordinate position on screen there's a corresponding offset where to
    fetch the texel in the texture-buffer.
    So these buffers equals the size of the screen, 320x200.
    The u's contains offsets for the x-coordinates.
    The v's contains offsets for the y-coordinates.
    So in the render loop, ( tAnimate-label in the asm-source) something like:

    int index=0;
    for(int y=0;y<200;y++){
    for(int x=0;x<320;x++){
    PixelOnScreen[index]=texture[ uvBuffer [x+(y *256 )] ];
    index++;
    }}

    ( But in the asm-src there's separate buffers for u and v so it's more like:
    u=uBuffer[index];
    v=vBuffer[index];
    PixelOnScreen[index]=texture[u+v<<8]

    But this would produce a static picture, to get motion, add another dynamic variable and
    add it to the offsets, and the tunnel will move:

    u=uBuffer[index]+displace;
    v=vBuffer[index]+displace;
    PixelOnScreen[index]=texture[u+v<<8]



    ( And once every frame increase displace-variable )

    The 'calcTunnel' label in the source, is where these uv offsets are calculated, with
    some trigonometry-function.
    In c would be somewhat like:

    int index=0;
    for(int y=0;y<200;y++){
    for(int x=0;x<320;x++){
    int i=x-160; // (SCREENWIDTH/2)
    int j=y-100; // (SCREENHEIGHT/2)
    v [index] = (int) ( atan2 (i,j) * 128/ 3.14159265 ); // the angle
    u [index] = (int) ( 8192 / sqrt (i*i+j*j) ); // distance from origin
    index++;
    }}

    (Varying the constant 8192 will produce different perspective.
    Constant 128 is half of size of the textureBuffer width or height.)

    I just converted this to assembler, and tried optimize a bit for size.
    So the asm-code is messier than these c-snippets.

    Hope this help.
    (I wrote this off memory, so if someone spots error, please
    forgive...)

    To Blip: Could you please explain about pipeline, what is it? and how to prevent bad pipelining! )







  • blipblip Posts: 756Member
    Pipelining is like having a few mini-processors in one that do jobs in parallel to speed up the whole machine considerably. There is one catch, though: they must do jobs that don't require the result of another that's in another pipeline or else you'll have a pipeline stall and lose some speed in the process. There are many ways to prevent pipeline stalls and they can differ between processors. I'd rather not explain it because this guide does much better than I would be able to and more: http://www.programmersheaven.com/file.asp?FileID=6344
Sign In or Register to comment.