Advertisement
NovaYoshi

drawing.asm

Jul 6th, 2013
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; part of Nova's experimental OS
  2.  
  3. %macro XY_SlideEDI 2
  4.   add EDI %1 + ( %2 * 320 )
  5. %endmacro
  6.  
  7. PlotText:                        ; Prints a string onto the screen in the cute font
  8. .loop1: mov al, [esi]            ; Fetch the current character
  9.         cmp al, 0                ; This routine fails when I don't do this compare right here
  10.         jz .exit                 ; And if it's null, then abort the loop
  11.         inc esi                  ; The next iteration will read the next string character
  12.         push esi                 ; PointToChar and PutMonoTile modify ESI. We're pushing it out of the way so it isn't altered.
  13.         call PointToChar         ; Point ESI at the character data now.
  14.         mov bl, dh               ; Copy over the color so PutMonoTile knows which one to use
  15.         call PutMonoTile         ; Print the character
  16.         pop esi                  ; Restore ESI's value
  17.         add edi, 8               ; The next string character will be written eight pixels right of the last one
  18.         jmp .loop1               ;
  19. .exit:  ret                      ;
  20. PlotText_nomask:                 ; Prints a string onto the screen in the cute font
  21. .loop1: mov al, [esi]            ; Fetch the current character
  22.         cmp al, 0                ; This routine fails when I don't do this compare right here
  23.         jz .exit                 ; And if it's null, then abort the loop
  24.         inc esi                  ; The next iteration will read the next string character
  25.         push esi                 ; PointToChar and PutMonoTile modify ESI. We're pushing it out of the way so it isn't altered.
  26.         call PointToChar         ; Point ESI at the character data now.
  27.         mov bl, dh               ; Copy over the color so PutMonoTile knows which one to use
  28.         mov bh, dl
  29.         call PutMonoTile_nomask  ; Print the character
  30.         pop esi                  ; Restore ESI's value
  31.         add edi, 8               ; The next string character will be written eight pixels right of the last one
  32.         jmp .loop1               ;
  33. .exit:  ret                      ;
  34. PointToChar:                     ; Points ESI at an ASCII character specified
  35.         mov esi, CuteFont        ; Start at the top of CuteFont
  36.         and eax, 255             ; Only the lowest byte of EAX matters right now
  37.         shl eax, 3               ; Multiply by 8
  38.         add esi, eax             ; Offset ESI
  39.         ret                      ;
  40.  
  41. PlotByte:
  42.       ror eax, 4
  43.       call PlotNybble
  44.       rol eax, 4
  45.       call PlotNybble
  46.       ret
  47.  
  48. PlotNybble:
  49.       push eax
  50.       push ebx
  51.       push ecx
  52.       push esi
  53.         add edi, 8                ; step to the next char
  54.  
  55.         and al, $0f
  56.         xor ecx, ecx
  57.         mov cl, al
  58.         add ecx, HexDigits
  59.  
  60.         mov al, [ecx]
  61.         call PointToChar
  62.         call PutMonoTile_nomask
  63.       pop esi
  64.       pop ecx
  65.       pop ebx
  66.       pop eax
  67.         ret
  68.  
  69. PlotWord:
  70.         shl edx, 4
  71.         call .Nyb
  72.         call .Nyb
  73.         call .Nyb
  74.         call .Nyb
  75.         ret
  76. .Nyb:   shr edx, 4
  77.         push edx
  78.         add edi, 8
  79.         mov esi, HexDigits
  80.        
  81.         xor eax, eax
  82.         mov al, dl
  83.         and al, 0fh
  84.         add esi, eax
  85.        
  86.         mov al, [esi]
  87.         call PointToChar
  88.         mov bl, 2
  89.         mov bh, 0
  90.         call PutMonoTile_nomask
  91.         pop edx
  92.         ret
  93. HexDigits: db "0123456789ABCDEF"
  94.  
  95. PutMonoTile:                    ; Instead of wasting space by reading the font with a byte per pixel, we're doing it in 1bpp
  96.         push edi                ; Just in case
  97.         mov ch, 8               ; Load our higher counter. We're using it to count rows.
  98. .loop1: mov cl, 8            ; Load our lower counter, which will count our columns. We will do this for each row.
  99.         mov al, [esi]           ; Get the current row in the font
  100. .loop2: shl al, 1            ; Shift left...
  101.         jnc .skip               ; ...which updates carry. I'm essentially doing what Tepples did in NAME to display NES tiles.
  102.         mov [edi], bl           ; If carry is zero, the color won't be poked in
  103. .skip:  inc edi               ; Next pixel!
  104.         dec cl                  ; Decrement the column counter
  105.         jnz .loop2              ; If it isn't the last column, then shift again!
  106.         add edi, 312            ; Move down now
  107.         inc esi                 ; The font is stored with one row per byte. We only need to step the font pointer each row.
  108.         dec ch                  ; Decrement the row counter
  109.         jnz .loop1              ; If it isn't the last row, go read the next
  110.         pop edi                 ; Just in case
  111.         ret                     ;
  112. PutMonoTile_nomask:             ; Instead of wasting space by reading the font with a byte per pixel, we're doing it in 1bpp
  113.         push edi                ; Just in case
  114.         mov ch, 8               ; Load our higher counter. We're using it to count rows.
  115. .loop1: mov cl, 8            ; Load our lower counter, which will count our columns. We will do this for each row.
  116.         mov al, [esi]           ; Get the current row in the font
  117. .loop2: shl al, 1            ; Shift left...
  118.         mov [edi], bh           ; Poke background color
  119.         jnc .skip               ; ...which updates carry. I'm essentially doing what Tepples did in NAME to display NES tiles.
  120.         mov [edi], bl           ; Poke in foreground color if needed
  121. .skip:  inc edi               ; Next pixel!
  122.         dec cl                  ; Decrement the column counter
  123.         jnz .loop2              ; If it isn't the last column, then shift again!
  124.         mov eax, 312            ; If it is, adjust EDI for the next row of pixels
  125.         add edi, eax            ; (We can't apparently add a 16bit constant to it)
  126.         inc esi                 ; The font is stored with one row per byte. We only need to step the font pointer each row.
  127.         dec ch                  ; Decrement the row counter
  128.         jnz .loop1              ; If it isn't the last row, go read the next
  129.         pop edi                 ; Just in case
  130.         ret                     ;
  131.  
  132. RectFill:                       ; Really slow. Humps the stack a lot...
  133.         push ebx                ;
  134.         push edx                ;
  135.         push edi
  136. .loop:
  137.         push edi                ;
  138.         push eax                ;
  139.         push ebx                ;
  140.         push ecx                ;
  141.         push edx                ;
  142.         mov bx, 1               ;
  143.         call OrthiRun           ;
  144.         pop edx                 ;
  145.         pop ecx                 ;
  146.         pop ebx                 ;
  147.         pop eax                 ;
  148.         pop edi                 ;
  149.  
  150.         add edi, 320            ;
  151.         dec dx                  ;
  152.         jnz .loop               ;
  153.  
  154.         pop edi
  155.         pop edx                 ;
  156.         pop ebx                 ;
  157.         ret                     ;
  158. PositionXY:                     ; Sets up EDI to point to the pixel the prorammer wants
  159.         mov cx, 16              ; IT LOOKS LIKE YOU'RE TRYING TO DRAW TO THE SCREEN, WANT SOME HELP?
  160.         mov es, cx              ; ES, like the 6502's stack pointer, can't be set directly. We're having it go through BX.
  161.         mov edi, MemScreen      ; Start at the top-left in EDI
  162.         and eax, 0ffffh         ; Get rid of any bits hiding in the upper half of EAX but keep the rest of EAX the same
  163.         add edi, eax            ; Offset EDI horizontally by EAX
  164.         mov ax,  bx             ; Put BX into AX, because the MUL opcode *automatically* uses EAX
  165.         xor ebx, ebx            ; Clear out EBX. We're gonna use it to hold ScreenW now
  166.         mov bx, [ScreenW]       ; We're gonna multiply by the screen height to get the offset for the Y position
  167.         mul ebx                 ; Multiply!
  168.         add edi, eax            ; Add the vertcial offset to EDI
  169.         ret                     ; EDI's ready!
  170. ClearScreenToColor:             ; Clears the whole screen to a color specified by the programmer
  171.         mov bx, 16              ; We're gonna be drawing to the screen after this routine, so set up the ES segment for that
  172.         mov es, bx              ; ES, like the 6502's stack pointer, can't be set directly. We're having it go through BX.
  173.         mov edi, MemScreen      ; Start at the top-left in EDI
  174.         mov cx, 320*200         ; The whole entire freaking screen :D
  175.         mov bx, 1               ; Might as well fill it horizontally
  176. OrthiRun:                       ; Meant for horizontal and vertical lines, but works okay for diagonals too
  177.         push edi
  178.         and ebx, 0ffffh         ; Get rid of any bits hiding in the upper half of EBX but keep the rest of EBX the same
  179. .run:    mov [es:edi], al       ;
  180.         add edi, ebx            ;
  181.         dec cx                  ;
  182.         jnz .run                ;
  183.         pop edi                 ;
  184.         ret                     ;
  185. PutIcon:                    ; Displays an 8 by 8 tile on the screen, with one nybble per pixel
  186.         mov cl, 0               ; Start at column zero
  187.         mov ch, 8               ; We're drawing 8 rows
  188. .loop1: mov al, [esi]        ; Load a byte of the icon
  189.         and al, 0f0h            ; The first pixel is encoded in the first nybble
  190.         shr al, 4               ; Shift it over so it maps to the right colors
  191.         mov [edi], al           ; Stash it on the screen
  192.         inc edi                 ; Next pixel on the screen!
  193.         mov al, [esi]           ; Fetch the byte again,
  194.         and al, 00fh            ;  this time for the other nybble!
  195.         mov [edi], al           ; Stash it on the screen
  196.         inc edi                 ; Next pixel on the screen!
  197.         inc esi                 ; Okay, we wrote two pixels stored in the icon. Time for the next byte
  198.         inc cl                  ; Next column
  199.         cmp cl, 4               ; 4th byte read in the row?
  200.         jnz .loop1              ; Continue if it isn't
  201. .loop2: mov cl, 0            ; Reset the columns counter
  202.         add edi, 320-8          ; Move EDI so that it points to where the next line of the icon should go
  203.         dec ch                  ; Decrement the row counter,
  204.         jnz .loop1              ;  and draw another row if there are still rows left.
  205.         ret
  206. IconTest:                    ; It's a smiley face!
  207.     db 0ffh,000h,000h,0ffh
  208.     db 0f0h,0eeh,0eeh,00fh
  209.     db 00eh,00eh,0e0h,0e0h
  210.     db 00eh,00eh,0e0h,0e0h
  211.     db 00eh,0eeh,0eeh,0e0h
  212.     db 00eh,00eh,0e0h,0e0h
  213.     db 0f0h,0e0h,00eh,00fh
  214.     db 0ffh,000h,000h,0ffh
  215.  
  216.  
  217. ;db 0ffh,000h,000h,0ffh \ db 0f0h,0eeh,0eeh,00fh \ db 00eh,00eh,0e0h,0e0h \ db 00eh,00eh,0e0h,0e0h \ db 00eh,0eeh,0eeh,0e0h \ db 00eh,00eh,0e0h,0e0h \ db 0f0h,0e0h,00eh,00fh \ db 0ffh,000h,000h,0ffh
  218.  
  219. IconEmpty:
  220.     db 000h,000h,000h,000h
  221.     db 000h,000h,000h,000h
  222.     db 000h,000h,000h,000h
  223.     db 000h,000h,000h,000h
  224.     db 000h,000h,000h,000h
  225.     db 000h,000h,000h,000h
  226.     db 000h,000h,000h,000h
  227.     db 000h,000h,000h,000h
  228. RepeatedlyPokingEAX:
  229.     mov cx, dx
  230. RepeatedlyPokingEAX_Alt:
  231.     mov [edi], eax
  232.     add edi, 4
  233.     dec cx
  234.     jnz RepeatedlyPokingEAX_Alt
  235.     ret
  236. ShadowPrint:        ; Not size optimized yet
  237.         push cx
  238.         push dx
  239.         mov cx, 5
  240. .loop:  push esi
  241.         push edi
  242.         loop .loop, cx
  243.  
  244.         mov dh, 007
  245.         pop edi
  246.         sub edi, 1
  247.         pop esi
  248.         call PlotText
  249.  
  250.         pop edi
  251.         add edi, 1
  252.         pop esi
  253.         call PlotText
  254.  
  255.         pop edi
  256.         add edi, 320
  257.         pop esi
  258.         call PlotText
  259.  
  260.         pop edi
  261.         sub edi, 320
  262.         pop esi
  263.         call PlotText
  264.  
  265.         pop edi
  266.         pop esi
  267.         mov dh, 0x1f
  268.         call PlotText
  269.         pop dx
  270.         pop cx
  271.         ret
  272. Repeat4InEAX:
  273.         xor ebx, ebx
  274.         mov bl, al
  275.         shl ax, 8
  276.         or  ax, bx
  277.         mov bx, ax
  278.         shl ebx, 16
  279.         or eax, ebx
  280.         ret
  281.  
  282. PlotSmallChar2:
  283.         push edi
  284.         push esi
  285.  
  286.         mov ax, 'A'
  287.         shl ax, 1            ; Multiply by 2
  288.  
  289.         pop esi
  290.         pop edi
  291.         ret
  292.  
  293. PlotSmallChar:
  294.         push edi
  295.         push esi
  296.         mov ch, 5
  297.         xor eax, eax
  298.         mov al, [esi]
  299.         cmp al, ' '
  300.         jz .SmallSpace
  301.         sub al, 'A'
  302.         shl ax, 1            ; Multiply by 2
  303.  
  304.         mov esi, TinyFont1
  305.         add esi, eax
  306.  
  307.         mov ax, [esi]
  308.         shl ax, 1            ; Skip first bit for now...
  309. .loop1: mov cl, 3
  310. .loop2: mov dh, 0x00 ; Foreground
  311.         mov dl, 0x0f ; Background
  312.         mov [edi], dl
  313.         shl ax, 1
  314.         jnc .loop3
  315.         mov [edi], dh
  316. .loop3: inc edi
  317.         dec cl
  318.         jnz .loop2
  319.  
  320.         add edi, 320-3
  321.         dec ch
  322.         jnz .loop1
  323.         pop esi
  324.         pop edi
  325.         ret
  326. .SmallSpace:
  327.         mov ax, 0
  328.         jmp .loop1
  329.         ret
  330.  
  331. PlotSmallText:
  332. .loop:  call PlotSmallChar
  333.         inc esi
  334.         add edi, 4
  335.         mov al, [esi]
  336.         cmp al, 0
  337.         jnz .loop
  338.         ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement