home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / sys / ibm / pc / hardware / 24138 < prev    next >
Encoding:
Text File  |  1992-09-13  |  13.3 KB  |  496 lines

  1. Newsgroups: comp.sys.ibm.pc.hardware
  2. Path: sparky!uunet!utcsri!torn!maccs!mcshub!csx.cciw.ca!hcp
  3. From: hcp@csx.cciw.ca (H.C. Pulley)
  4. Subject: Re: CGA 160x100x16
  5. Organization: Canada Centre for Inland Waters
  6. Date: Sun, 13 Sep 1992 16:19:27 GMT
  7. Message-ID: <1992Sep13.161927.19097@csx.cciw.ca>
  8. References: <1992Sep11.120323.16528@nuscc.nus.sg>
  9. Lines: 485
  10.  
  11. In article <1992Sep11.120323.16528@nuscc.nus.sg> isc30108@nusunix3.nus.sg (AMBAT SASI S/O PALISSERY P) writes:
  12. >can some1 please post some code (c/asm/pas)
  13. >describing how to arrive at this simulated "graphics"
  14. >mode with 16 colors 160x100 resolution.
  15. >thanks.
  16. >
  17. >sasi.
  18. ><last request posted expired>
  19.  
  20. Alright, but you asked for it!
  21.  
  22. Like I said before, it doesn't work properly.
  23.  
  24. Please don't give me any comments on my assembly style.  This was an 
  25. experimental program only.
  26.  
  27. If you manage to get it to work properly, please send me the modified source.
  28.  
  29. This program simply goes into 160x100x16 mode (or tries to anyways) and draws
  30. 200 lines changing colors as it goes.
  31.  
  32. --- CUT HERE ---
  33.  
  34. ; This program goes into text/graphics mode (160*100) in 16 colors
  35. ; It draws 200 lines and then exits
  36. ; By Harry C. Pulley, IV.  MAR92.
  37.  
  38.      .MODEL small
  39.  
  40.      .STACK 200h
  41.  
  42.      .DATA
  43. Current_pos_x DW 159                    ; to hold current pixel x value
  44. Current_pos_y DW 0                      ; to hold current pixel y value
  45. Last_pos_x DW 0                         ; to hold last pixel x value
  46. Last_pos_y DW 99                        ; to hold last pixel y value
  47. Saved_video_mode DB ?                   ; to hold the old video mode for return to DOS
  48. Set_video_mode DB 5                     ; new video mode=5
  49. Plot_mode DB 0                          ; plot mode: pixel color
  50. signed_rise DW ?                        ; signed rise
  51. signed_run DW ?                         ; signed run
  52. rise_sign DB ?                          ; rise + | -
  53. run_sign DB ?                           ; run + | -
  54. Hold_x DW ?                             ; starting x value
  55. Hold_y DW ?                             ; starting y value
  56.  
  57.      .CODE
  58. Start:                                  ; start of program
  59.      mov ax,@data                       ; set up ds to point to .DATA segment
  60.      mov ds,ax                          ; use ax since mov ds,@data is illegal
  61.  
  62.      mov ah,0fh                         ; video function to get video parameters
  63.      int 10h                            ; video interrupt
  64.      mov [Saved_video_mode],al          ; save video mode in variable for later
  65.  
  66.      sub al,al                          ; set al to zero
  67.      mov dx,3d8h                        ; cga mode control register
  68.  
  69.      out dx,al                          ; disable display
  70.  
  71.      mov dx,3d4h
  72.  
  73.      out dx,al                          ; set CRTC reg 0
  74.  
  75.      mov al,83
  76.      mov dx,3d5h
  77.  
  78.      out dx,al                          ; set horizontal total to 83
  79.  
  80.      mov al,1
  81.      mov dx,3d4h
  82.  
  83.      out dx,al                          ; set CRTC reg 1
  84.  
  85.      mov al,80
  86.      mov dx,3d5h
  87.  
  88.      out dx,al                          ; set horizontal displayed to 80
  89.  
  90.      mov al,2
  91.      mov dx,3d4h
  92.  
  93.      out dx,al                          ; set CRTC reg 2
  94.  
  95.      mov al,81
  96.      mov dx,3d5h
  97.  
  98.      out dx,al                          ; set horizontal sync to 81
  99.  
  100.      mov al,3
  101.      mov dx,3d4h
  102.  
  103.      out dx,al                          ; set CRTC reg 3
  104.  
  105.      mov al,1
  106.      mov dx,3d5h
  107.  
  108.      out dx,al                          ; set horizontal sync width 1
  109.  
  110.      mov al,4
  111.      mov dx,3d4h
  112.  
  113.      out dx,al                          ; set CRTC reg 4
  114.  
  115.      mov al,107
  116.      mov dx,3d5h
  117.  
  118.      out dx,al                          ; set vertical total to 107
  119.  
  120.      mov al,5
  121.      mov dx,3d4h
  122.  
  123.      out dx,al                          ; set CRTC reg 5
  124.  
  125.      sub al,al                          ; al=0
  126.      mov dx,3d5h
  127.  
  128.      out dx,al                          ; set vertical adjust 0
  129.  
  130.      mov al,6
  131.      mov dx,3d4h
  132.  
  133.      out dx,al                          ; set CRTC reg 6
  134.  
  135.      mov al,107
  136.      mov dx,3d5h
  137.  
  138.      out dx,al                          ; set vertical displayed 107
  139.  
  140.      mov al,7
  141.      mov dx,3d4h
  142.  
  143.      out dx,al                          ; set CRTC reg 7
  144.  
  145.      mov al,100
  146.      mov dx,3d5h
  147.  
  148.      out dx,al                          ; set vertical sync 100
  149.  
  150.      mov al,8
  151.      mov dx,3d4h
  152.  
  153.      out dx,al                          ; set CRTC reg 8
  154.  
  155.      mov al,2
  156.      mov dx,3d5h
  157.  
  158.      out dx,al                          ; set interlace 2
  159.  
  160.      mov al,9
  161.      mov dx,3d4h
  162.  
  163.      out dx,al                          ; set CRTC reg 9
  164.  
  165.      mov al,7
  166.      mov dx,3d5h
  167.  
  168.      out dx,al                          ; set max scan 7
  169.  
  170.      mov al,10
  171.      mov dx,3d4h
  172.  
  173.      out dx,al                          ; set CRTC reg 10
  174.  
  175.      mov al,6
  176.      mov dx,3d5h
  177.  
  178.      out dx,al                          ; set cursor start 6
  179.  
  180.      mov al,11
  181.      mov dx,3d4h
  182.  
  183.      out dx,al                          ; set CRTC reg 11
  184.  
  185.      mov al,7
  186.      mov dx,3d5h
  187.  
  188.      out dx,al                          ; set cursor end 7
  189.  
  190.      mov al,12
  191.      mov dx,3d4h
  192.  
  193.      out dx,al                          ; set CRTC reg 12
  194.  
  195.      sub al,al
  196.      mov dx,3d5h
  197.  
  198.      out dx,al                          ; set high start addr 0
  199.  
  200.      mov al,13
  201.      mov dx,3d4h
  202.  
  203.      out dx,al                          ; set CRTC reg 13
  204.  
  205.      sub al,al
  206.      mov dx,3d5h
  207.  
  208.      out dx,al                          ; set low start addr 0
  209.  
  210.      mov al,14
  211.      mov dx,3d4h
  212.  
  213.      out dx,al                          ; set CRTC reg 14
  214.  
  215.      sub al,al
  216.      mov dx,3d5h
  217.  
  218.      out dx,al                          ; set high end addr 0
  219.  
  220.      mov al,15
  221.      mov dx,3d4h
  222.  
  223.      out dx,al                          ; set CRTC reg 15
  224.  
  225.      sub al,al
  226.      mov dx,3d5h
  227.  
  228.      out dx,al                          ; set low end addr 0
  229.  
  230.      mov al,01000001b
  231.      mov dx,3d8h
  232.  
  233.      out dx,al                          ; set psuedographics mode 160x100x16
  234.  
  235.      mov ax,0b800h
  236.      mov es,ax                          ; set es to video memory
  237.  
  238.      cld
  239.  
  240.      mov ax,00deh                       ; set even bytes to deh character
  241.      mov cx,8000                        ; 8000 words long
  242.      mov di,0                           ; set di to offset of start of video buffer
  243.  
  244.      rep stosw                          ; fill video memory
  245.  
  246.      mov al,01001001b
  247.      mov dx,3d8h
  248.  
  249.      out dx,al                          ; enable display
  250.  
  251. Mainloop:
  252.  
  253.      Call Plot_line
  254.  
  255.      dec [Current_pos_x]
  256.  
  257.      inc [Current_pos_y]
  258.  
  259.      inc [Last_pos_x]
  260.  
  261.      dec [Last_pos_y]                   ; adjust point values
  262.  
  263.      cmp [Last_pos_x],100
  264.      jge OutofLoop
  265.  
  266.      inc [Plot_mode]
  267.      and [Plot_mode],0fh                ; get Plot_mode%16
  268.  
  269. ;    mov ax,10h
  270. ;    int 16h                            ; wait for key
  271.  
  272.      jmp Mainloop                       ; get next character/mouse action
  273.  
  274. OutofLoop:
  275.      mov ah,0                           ; set video mode
  276.      mov al,[Saved_video_mode]          ; restore video mode
  277.      int 10h                            ; video interrupt
  278.  
  279.      mov ah,4ch                         ; DOS terminate program function
  280.      int 21h                            ; DOS interrupt
  281.  
  282. ; Procedure to plot a pixel using direct memory writes
  283. ; Input: Plot_mode, Current_pos_x, Current_pos_y
  284. ; Registers destroyed: ax,bx,cx,dx
  285. ; Output: none
  286.  
  287. Plot_pixel     PROC
  288.  
  289.      mov ax,[Current_pos_y]             ; set ax to current # of lines
  290.      mov dx,80                          ; mul by 160
  291.      mul dx                             ; get current pixel count in lines
  292.      add ax,[Current_pos_x]             ; add x pos to count
  293.      mov dx,ax
  294.      and dx,1                           ; get odd bit of count
  295.      and ax,0fffeh                      ; get even count
  296.      inc ax                             ; make count odd bye
  297.  
  298.      mov bx,ax                          ; quotient is byte count in bx
  299.  
  300.      mov cl,2
  301.      sal dl,cl                          ; multiply dl by 4 for correct shift
  302.      add dl,4                           ; add 4 to dl to compensate for endianess
  303.      mov cl,dl                          ; cl is the shift count, which is the bit count
  304.  
  305.      mov dl,[Plot_mode]                 ; make dl pixel color
  306.      mov dh,0f0h                        ; prepare to erase
  307.  
  308.      ror dl,cl                          ; rotate the bit to the correct position in dl
  309.      ror dh,cl                          ; rotate erasure window to correct pos
  310.  
  311.      and BYTE PTR es:[bx],dh            ; clear pixel
  312.  
  313.      or BYTE PTR es:[bx],dl             ; draw pixel
  314.      ret
  315.  
  316. Plot_pixel     ENDP
  317.  
  318. ; Procedure to plot a line from Last_pos_? to Current_pos_? using Plot_pixel
  319. ; Input: Last_pos_x,Last_pos_y,Current_pos_x,Current_pos_y
  320. ; Registers destroyed: ax,bx,cx,dx
  321. ; Output: none
  322.  
  323. Plot_line      PROC
  324.  
  325.      mov cx,[Current_pos_y]
  326.      mov dx,[Current_pos_x]             ; put current pos in registers cx,dx
  327.  
  328.      mov ax,cx
  329.      sub ax,[Last_pos_y]
  330.      mov [signed_rise],ax               ; get signed rise
  331.  
  332.      mov ax,dx
  333.      sub ax,[Last_pos_x]
  334.      mov [signed_run],ax                ; get signed run
  335.  
  336.      mov ax,[Last_pos_y]
  337.      push ax
  338.      mov ax,[Last_pos_x]
  339.      push ax                            ; preserve last x,y
  340.  
  341.      cmp cx,[Last_pos_y]                ; we want a positive rise
  342.      jl Last_y_bigger
  343.      mov [rise_sign],1                  ; remember rise sign +
  344.      jmp Do_run
  345.  
  346. Last_y_bigger:
  347.      xchg cx,[Last_pos_y]               ; swap current y and last y
  348.      mov [rise_sign],-1                 ; remember rise sign -
  349.  
  350. Do_run:
  351.      sub cx,[Last_pos_y]                ; get +rise
  352.  
  353.      cmp dx,[Last_pos_x]                ; want a pos rise
  354.      jl Last_x_bigger
  355.      mov [run_sign],1                   ; remember run sign +
  356.      jmp Do_line
  357.  
  358. Last_x_bigger:
  359.      xchg dx,[Last_pos_x]               ; swap current x and last x
  360.      mov [run_sign],-1                  ; remember run sign -
  361.  
  362. Do_line:
  363.      sub dx,[Last_pos_x]                ; want pos run
  364.  
  365.      pop ax
  366.      mov [Last_pos_x],ax
  367.      pop ax
  368.      mov [Last_pos_y],ax                ; restore last x,y
  369.  
  370.      mov ax,cx
  371.      add ax,dx
  372.      cmp ax,0                           ; if cx==dx==0 no draw needed
  373.      jne Continue
  374.      ret
  375.  
  376. Continue:
  377.      mov ax,[Current_pos_y]
  378.      push ax
  379.      mov ax,[Current_pos_x]
  380.      push ax                            ; preserve current position x & y
  381.  
  382.      cmp cx,dx                          ; compare rise to run
  383.      jg Skip
  384.      jmp Run_bigger                     ; ignore = case, doesn't matter y | x if equal
  385.  
  386. Skip:
  387.      cmp [rise_sign],1
  388.      je Start_last                      ; determine starting point, last or current
  389.      jmp Start_next
  390. Start_last:
  391.      mov ax,[Last_pos_y]
  392.      mov [Current_pos_y],ax             ; set last y to start
  393.      mov ax,[Last_pos_x]
  394.      mov [Current_pos_x],ax             ; set last x to start
  395. Start_next:
  396.      mov ax,[Current_pos_y]
  397.      mov [Hold_y],ax                    ; keep start y
  398.  
  399. Rise_loop:
  400.      mov ax,[Current_pos_y]
  401.      sub ax,[Hold_y]                    ; ax=delta y
  402.      mov bx,[signed_run]
  403.      imul bx                            ; mul y*run
  404.      mov bx,[signed_rise]
  405.      idiv bx                            ; div (y*run)/rise
  406.  
  407.      add ax,[Current_pos_x]             ; add ((y*run)/rise)+b
  408.  
  409.      xchg ax,[Current_pos_x]            ; save x
  410.  
  411.      push ax                            ; preserve ax for Current_pos_x
  412.      push cx                            ; preserve cx for Loop counter
  413.  
  414.      call Plot_pixel
  415.  
  416.      pop cx                             ; restore cx
  417.      pop ax                             ; restore ax
  418.  
  419.      mov [Current_pos_x],ax             ; restore x
  420.  
  421.      inc [Current_pos_y]                ; go to next y pos
  422.  
  423.      loop Rise_loop                     ; do next y pos
  424.  
  425.      jmp Restore                        ; exit procedure
  426.  
  427. Run_bigger:
  428.  
  429.      mov cx,dx                          ; put run into counter
  430.  
  431.      cmp [run_sign],1
  432.      je Start_lst                       ; determine starting point, last or current
  433.      jmp Start_nxt
  434. Start_lst:
  435.      mov ax,[Last_pos_y]
  436.      mov [Current_pos_y],ax             ; set last y to start
  437.      mov ax,[Last_pos_x]
  438.      mov [Current_pos_x],ax             ; set last x to start
  439. Start_nxt:
  440.      mov ax,[Current_pos_x]
  441.      mov [Hold_x],ax                    ; keep starting x value
  442.  
  443. Run_loop:
  444.      mov ax,[Current_pos_x]
  445.      sub ax,[Hold_x]                    ; ax=delta x
  446.      mov bx,[signed_rise]
  447.      imul bx                            ; mul x*rise
  448.      mov bx,[signed_run]
  449.      idiv bx                            ; div (x*rise)/run
  450.  
  451.      add ax,[Current_pos_y]             ; add ((x*rise)/run)+b
  452.  
  453.      xchg ax,[Current_pos_y]            ; save y
  454.  
  455.      push ax                            ; preserve ax for Current_pos_y
  456.      push cx                            ; preserve cx for Loop counter
  457.  
  458.      call Plot_pixel
  459.  
  460.      pop cx                             ; restore cx
  461.      pop ax                             ; restore ax
  462.  
  463.      mov [Current_pos_y],ax             ; restore y
  464.  
  465.      inc [Current_pos_x]                ; go to next x pos
  466.  
  467.      loop Run_loop                      ; do next x pos
  468.  
  469.      jmp Restore
  470.  
  471. Restore:
  472.      pop ax
  473.      mov [Current_pos_x],ax             ; restore x
  474.      pop ax
  475.      mov [Current_pos_y],ax             ; restore y
  476.  
  477.      ret
  478.  
  479. Plot_single:
  480.      call Plot_pixel
  481.  
  482.      ret
  483. Plot_line      ENDP
  484.  
  485.      END Start
  486.  
  487. --- CUT HERE ---
  488.  
  489. Harry
  490.  
  491. -- 
  492.           hcp@csx.cciw.ca           | This message    | It takes all kinds,
  493.     hcpiv@grumpy.cis.uoguelph.ca    | released to the | and to each his own.
  494. ------------------------------------| PUBLIC DOMAIN.  | This thought in mind,
  495.  Stay away from the DOS side, Luke! |                 | I walk alone.
  496.