home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / archives / packetdrivers.tar.gz / pd.tar / src / pixel.asm < prev    next >
Assembly Source File  |  1995-06-25  |  7KB  |  380 lines

  1. ;put into the public domain by Russell Nelson, nelson@crynwr.com
  2. ;code to do graphics at 320x200x256 on both EGAs and VGAs.
  3.  
  4. ; we want to be able to call these routines as if they were real addresses,
  5. ; and we don't want to have to index into a table.  Therefore, the init_vid
  6. ; routine will copy the correct set of addresses into the following locations.
  7.  
  8. address_list    label    word
  9. ;After calling open_vid, you can't change es, di, dx, or ah.
  10. ;al and bx will get trashed, but they need not be preserved.
  11. open_vid    dw    ?    ;enter with cx,dx=initial point.
  12. move_right    dw    ?    ;move the point right.
  13. move_left    dw    ?    ;move the point left.
  14. move_up        dw    ?    ;move the point up.
  15. move_down    dw    ?    ;move the point down.
  16. set_bit        dw    ?    ;set the pixel at the point to al.
  17. set_line    dw    ?    ;set the row of pixels at the point to
  18.                 ;the list of cx pixels at ds:si.
  19. close_vid    dw    ?    ;clean up after open_vid
  20. uninit_vid    dw    ?    ;clean up after init_vid, return to text mode.
  21. address_end    label    word
  22.  
  23. ega_adrs    dw    init_vid_ega, open_vid_ega, move_right_ega, move_left_ega
  24.         dw    move_up_ega, move_down_ega, set_bit_ega, set_line_ega
  25.         dw    close_vid_ega, uninit_vid_ega
  26.  
  27. vga_adrs    dw    init_vid_vga, open_vid_vga, move_right_vga, move_left_vga
  28.         dw    move_up_vga, move_down_vga, set_bit_vga, set_line_vga
  29.         dw    close_vid_vga, uninit_vid_vga
  30.  
  31.  
  32. init_vid:
  33. ;test for VGA.
  34.     mov    ax,1a00h
  35.     int    10h
  36.     cmp    al,1ah
  37.     mov    si,offset vga_adrs
  38.   ifndef FORCE_EGA
  39.     je    have_vga
  40.   endif
  41.     mov    si,offset ega_adrs
  42. have_vga:
  43.     lodsw                ;get the init_vid_* routine.
  44.     push    ds
  45.     pop    es
  46.     mov    di,offset address_list
  47.     mov    cx,(offset address_end - offset address_list) / 2
  48.     rep    movsw
  49.     jmp    ax            ;jump to the proper init_vid routine.
  50.  
  51.  
  52. init_vid_vga:
  53.     mov    ax,13h            ;put the screen into vga mode.
  54.     int    10h
  55.  
  56.     call    pal_save        ;save the palette
  57.     call    pal_setgrey        ;set to a gray palette.
  58.  
  59.     ret
  60.  
  61. uninit_vid_vga:
  62.     call    pal_restore
  63.  
  64.     mov    ax,3h            ;put the screen into text mode.
  65.     int    10h
  66.  
  67.     ret
  68.  
  69.  
  70. init_vid_ega:
  71.     mov    ax,10h            ;put the screen into ega mode.
  72.     int    10h
  73.  
  74.     mov    ax,1000h        ;change the palette entry for 7
  75.     mov    bx,77Q*256 + 7        ;  to 77 octal.
  76.     int    10h
  77.  
  78.     mov    ax,1000h        ;. .
  79.     mov    bx,007Q*256 + 08h
  80.     int    10h
  81.  
  82.     mov    ax,1000h        ;. .
  83.     mov    bx,010Q*256 + 09h
  84.     int    10h
  85.  
  86.     mov    ax,1000h        ;. .
  87.     mov    bx,020Q*256 + 0ah
  88.     int    10h
  89.  
  90.     mov    ax,1000h        ;. .
  91.     mov    bx,030Q*256 + 0bh
  92.     int    10h
  93.  
  94.     mov    ax,1000h        ;. .
  95.     mov    bx,040Q*256 + 0ch
  96.     int    10h
  97.  
  98.     mov    ax,1000h        ;. .
  99.     mov    bx,050Q*256 + 0dh
  100.     int    10h
  101.  
  102.     mov    ax,1000h        ;. .
  103.     mov    bx,060Q*256 + 0eh
  104.     int    10h
  105.  
  106.     mov    ax,1000h        ;. .
  107.     mov    bx,070Q*256 + 0fh
  108.     int    10h
  109.  
  110.     ret
  111.  
  112.  
  113. uninit_vid_ega:
  114.     mov    ax,3h            ;put the screen into text mode.
  115.     int    10h
  116.  
  117.     ret
  118.  
  119.  
  120. pal_save:
  121. ;Save existing palette
  122.     mov    ax,1017h        ;get all palette registers.
  123.     mov    bx,0            ;first palette register.
  124.     mov    cx,256            ;read all palette registers.
  125.     push    ds
  126.     pop    es
  127.     mov    dx,offset save_dacs
  128.     int    10h
  129.  
  130.     ret
  131.  
  132.  
  133. pal_setgrey:
  134.     mov    di,offset gray_dacs
  135.     mov    al,0
  136. pal_setgrey_1:
  137.     mov    cx,3 * 256 / 64        ;3 colors, 256 grays, 64 slots.
  138.     rep    stosb
  139.  
  140.     inc    al
  141.     cmp    al,64
  142.     jb    pal_setgrey_1
  143.  
  144.     mov    dx,offset gray_dacs
  145.     jmp    short pal_set
  146.  
  147. pal_restore:
  148.     mov    dx,offset save_dacs
  149. pal_set:
  150.     mov    ax,1012h        ;set all palette registers.
  151.     mov    bx,0            ;first palette register.
  152.     mov    cx,256            ;read all palette registers.
  153.     push    ds
  154.     pop    es
  155.     int    10h
  156.  
  157.     ret
  158.  
  159.  
  160. open_vid_vga:
  161. ;enter with cx,dx = point on screen.
  162. ;exit with es:di,ah -> byte/bit on screen.
  163.     mov    ax,0a000h
  164.     mov    es,ax
  165.     mov    ax,320            ;width of screen.
  166.     mul    dx
  167.     add    ax,cx            ;add the offset in.
  168.     mov    di,ax            ;remember our pointer.
  169.     ret
  170.  
  171.  
  172. move_right_vga:
  173.     inc    di
  174.     ret
  175.  
  176. move_left_vga:
  177.     dec    di
  178.     ret
  179.  
  180. move_up_vga:
  181.     sub    di,320            ;width of screen.
  182.     ret
  183.  
  184. move_down_vga:
  185.     add    di,320            ;width of screen.
  186.     ret
  187.  
  188. set_line_vga:
  189. ;enter with the current pixel set up, ds:si -> row of pixels, cx=number of
  190. ;pixels.  Exit with the same point set up.
  191.     push    di
  192.     shr    cx,1
  193.     rep    movsw
  194.     jnc    set_line_vga_1
  195.     movsb
  196. set_line_vga_1:
  197.     pop    di
  198.     ret
  199.  
  200.  
  201. set_bit_vga:
  202.     mov    es:[di],al
  203.     ret
  204.  
  205. close_vid_vga:
  206.     ret
  207.  
  208.  
  209. ; the EGA code follows.  It uses a group of four pixels to achieve the
  210. ; appearance of 14 different graylevels.
  211.  
  212. open_vid_ega:
  213. ;enter with cx,dx = point on screen.
  214. ;exit with es:di,ah -> byte/bit on screen.
  215.     mov    ax,0a000h
  216.     mov    es,ax
  217.     mov    ax,640/8*2        ;bytes per scan line * scan lines per pixel
  218.     mul    dx
  219.     mov    di,ax
  220.     mov    ax,cx            ;/8*2
  221.     shr    ax,1
  222.     shr    ax,1
  223.     add    di,ax
  224.  
  225.     mov    dx,03ceh        ;graphics controller
  226.     mov    ax,0205h        ;write mode 2.
  227.     out    dx,al
  228.     inc    dx
  229.     mov    al,ah
  230.     out    dx,al
  231.     dec    dx
  232.     mov    al,08h            ;select bitmap register.
  233.     out    dx,al
  234.     inc    dx
  235.  
  236.     shl    cx,1            ;compute the bit.
  237.     and    cl,7
  238.     mov    ah,80h
  239.     shr    ah,cl
  240.     ret
  241.  
  242.  
  243. move_right_ega:
  244.     shr    ah,1            ;move right by a bit.
  245.     ror    ah,1            ;move right by another bit,
  246.     adc    di,0            ;  and handle byte increment.
  247.     ret
  248.  
  249.  
  250. move_left_ega:
  251.     rol    ah,1            ;move left by a bit,
  252.     adc    di,0            ;  and handle byte increment.
  253.     shl    ah,1            ;move left by another bit.
  254.     ret
  255.  
  256.  
  257. move_up_ega:
  258.     sub    di,80*2            ;width of screen.
  259.     ret
  260.  
  261.  
  262. move_down_ega:
  263.     add    di,80*2            ;width of screen.
  264.     ret
  265.  
  266.  
  267. set_line_ega:
  268. ;enter with the current pixel set up, ds:si -> row of pixels, cx=number of
  269. ;pixels.  Exit with the same point set up.
  270.     push    ax
  271.     push    di
  272. set_line_ega_1:
  273.     lodsb
  274.     call    set_bit
  275. ;beginning of in-line move_right
  276.     shr    ah,1            ;move right by a bit.
  277.     ror    ah,1            ;move right by another bit,
  278.     adc    di,0            ;  and handle byte increment.
  279. ;end of in-line move_right
  280.     loop    set_line_ega_1
  281.     pop    di
  282.     pop    ax
  283.     ret
  284.  
  285.  
  286. set_bit_ega:
  287.     mov    bl,al
  288.     xor    bh,bh
  289.  
  290.     mov    al,ah
  291.     out    dx,al
  292.     mov    al,ulpal[bx]
  293.     xchg    es:[di],al        ;store the palette value.
  294.     mov    al,llpal[bx]
  295.     xchg    es:[di+80],al        ;store the palette value.
  296.  
  297.     shr    ah,1            ;move right by a bit.
  298.  
  299.     mov    al,ah
  300.     out    dx,al
  301.     mov    al,urpal[bx]
  302.     xchg    es:[di],al        ;store the palette value.
  303.     mov    al,lrpal[bx]
  304.     xchg    es:[di+80],al        ;store the palette value.
  305.  
  306.     shl    ah,1            ;restore the bit.
  307.  
  308.     ret
  309.  
  310.  
  311. close_vid_ega:
  312.     mov    al,0ffh            ;mask = all ones.
  313.     out    dx,al
  314.     dec    dx
  315.     mov    ax,0005h        ;write mode 0
  316.     out    dx,al
  317.     inc    dx
  318.     mov    al,ah
  319.     out    dx,al
  320.  
  321.     ret
  322.  
  323. ulpal    db    20 dup(00h)
  324.     db    20 dup(09h)
  325.     db    20 dup(0bh)
  326.     db    20 dup(0fh)
  327.     db    20 dup(0fh)
  328.     db    20 dup(0fh)
  329.     db    20 dup(0fh)
  330.     db    20 dup(08h)
  331.     db    20 dup(08h)
  332.     db    20 dup(07h)
  333.     db    20 dup(07h)
  334.     db    20 dup(07h)
  335.     db    20 dup(07h)
  336. urpal    db    20 dup(00h)
  337.     db    20 dup(00h)
  338.     db    20 dup(0ch)
  339.     db    20 dup(0eh)
  340.     db    20 dup(0fh)
  341.     db    20 dup(08h)
  342.     db    20 dup(08h)
  343.     db    20 dup(08h)
  344.     db    20 dup(08h)
  345.     db    20 dup(08h)
  346.     db    20 dup(08h)
  347.     db    20 dup(08h)
  348.     db    20 dup(07h)
  349. llpal    db    20 dup(00h)
  350.     db    20 dup(0ch)
  351.     db    20 dup(0ch)
  352.     db    20 dup(0dh)
  353.     db    20 dup(0fh)
  354.     db    20 dup(0fh)
  355.     db    20 dup(08h)
  356.     db    20 dup(08h)
  357.     db    20 dup(08h)
  358.     db    20 dup(08h)
  359.     db    20 dup(08h)
  360.     db    20 dup(07h)
  361.     db    20 dup(07h)
  362. lrpal    db    20 dup(00h)
  363.     db    20 dup(0ah)
  364.     db    20 dup(0bh)
  365.     db    20 dup(0bh)
  366.     db    20 dup(0fh)
  367.     db    20 dup(0fh)
  368.     db    20 dup(0fh)
  369.     db    20 dup(0fh)
  370.     db    20 dup(08h)
  371.     db    20 dup(08h)
  372.     db    20 dup(07h)
  373.     db    20 dup(07h)
  374.     db    20 dup(07h)
  375.  
  376. save_dacs    db    256 * 3 dup(?)
  377. gray_dacs    db    256 * 3 dup(?)
  378.  
  379.  
  380.