about summary refs log blame commit diff stats
path: root/apps/ex10.mu.
blob: 35fea653d2ffc943326a4a8ecca4a7cf9d7e1be9 (plain) (tree)















































































































































































                                                                                                                                             
# Demo of mouse support.
#
# To build a disk image:
#   ./translate ex10.mu            # emits disk.img
# To run:
#   qemu-system-i386 disk.img
# Or:
#   bochs -f bochsrc               # bochsrc loads disk.img

fn main screen: (addr screen), keyboard: (addr keyboard), data-disk: (addr disk) {
#?   var x/esi: int <- copy 0x200
#?   var y/edi: int <- copy 0x180
#?   render-grid x, y
  $main:event-loop: {
    # read deltas from mouse
    var dx/eax: int <- copy 0
    var dy/ecx: int <- copy 0
    dx, dy <- read-mouse-event
    # loop if deltas are both 0
    {
      compare dx, 0
      break-if-!=
      compare dy, 0
      break-if-!=
      loop $main:event-loop
    }
    # render unclamped deltas
#?     render-grid x, y
    draw-int32-decimal-wrapping-right-then-down-from-cursor-over-full-screen screen, dx, 7/fg, 0/bg
    draw-text-wrapping-right-then-down-from-cursor-over-full-screen screen, " ", 7/fg, 0/bg
    draw-int32-decimal-wrapping-right-then-down-from-cursor-over-full-screen screen, dy, 7/fg, 0/bg
    move-cursor-to-left-margin-of-next-line screen
#?     {
#?       var dummy1/eax: int <- copy 0
#?       var dummy2/ecx: int <- copy 0
#?       dummy1, dummy2 <- draw-text-wrapping-right-then-down-over-full-screen screen, "         ", 0/x, 0x10/y, 0x31/fg, 0/bg
#?     }
#?     {
#?       var ephemeral-dx/eax: int <- copy dx
#?       var dummy/ecx: int <- copy 0
#?       ephemeral-dx, dummy <- draw-int32-decimal-wrapping-right-then-down-over-full-screen screen, ephemeral-dx, 0/x, 0x10/y, 0x31/fg, 0/bg
#?     }
#?     {
#?       var dummy/eax: int <- copy 0
#?       var ephemeral-dy/ecx: int <- copy dy
#?       dummy, ephemeral-dy <- draw-int32-decimal-wrapping-right-then-down-over-full-screen screen, ephemeral-dy, 5/x, 0x10/y, 0x31/fg, 0/bg
#?     }
#?     # clamp deltas
#?     $clamp-dx: {
#?       compare dx, -0xa
#?       {
#?         break-if->
#?         dx <- copy -0xa
#?         break $clamp-dx
#?       }
#?       compare dx, 0xa
#?       {
#?         break-if-<
#?         dx <- copy 0xa
#?         break $clamp-dx
#?       }
#?       dx <- copy 0
#?     }
#?     $clamp-dy: {
#?       compare dy, -0xa
#?       {
#?         break-if->
#?         dy <- copy -0xa
#?         break $clamp-dy
#?       }
#?       compare dy, 0xa
#?       {
#?         break-if-<
#?         dy <- copy 0xa
#?         break $clamp-dy
#?       }
#?       dy <- copy 0
#?     }
#?     # render clamped deltas
#?     {
#?       var dummy1/eax: int <- copy 0
#?       var dummy2/ecx: int <- copy 0
#?       dummy1, dummy2 <- draw-text-wrapping-right-then-down-over-full-screen screen, "         ", 0/x, 0x20/y, 0x31/fg, 0/bg
#?     }
#?     {
#?       var save-dx/eax: int <- copy dx
#?       var dummy/ecx: int <- copy 0
#?       save-dx, dummy <- draw-int32-decimal-wrapping-right-then-down-over-full-screen screen, save-dx, 0/x, 0x20/y, 0x31/fg, 0/bg
#?     }
#?     {
#?       var dummy/eax: int <- copy 0
#?       var save-dy/ecx: int <- copy dy
#?       dummy, save-dy <- draw-int32-decimal-wrapping-right-then-down-over-full-screen screen, save-dy, 5/x, 0x20/y, 0x31/fg, 0/bg
#?     }
#?     # loop if deltas are both 0
#?     {
#?       compare dx, 0
#?       break-if-!=
#?       compare dy, 0
#?       break-if-!=
#?       loop $main:event-loop
#?     }
#?     # accumulate deltas and clamp result within screen bounds
#?     x <- add dx
#?     compare x, 0
#?     {
#?       break-if->=
#?       x <- copy 0
#?     }
#?     compare x, 0x400
#?     {
#?       break-if-<
#?       x <- copy 0x3ff
#?     }
#?     y <- subtract dy  # mouse y coordinates are reverse compared to screen
#?     compare y, 0
#?     {
#?       break-if->=
#?       y <- copy 0
#?     }
#?     compare y, 0x300
#?     {
#?       break-if-<
#?       y <- copy 0x2ff
#?     }
    loop
  }
}

#? fn render-grid curr-x: int, curr-y: int {
#?   and-with curr-x, 0xfffffffc
#?   and-with curr-y, 0xfffffffc
#?   var y/eax: int <- copy 0
#?   {
#?     compare y, 0x300/screen-height=768
#?     break-if->=
#?     var x/edx: int <- copy 0
#?     {
#?       compare x, 0x400/screen-width=1024
#?       break-if->=
#?       var color/ecx: int <- copy 0
#?       # set color if either x or y is divisible by 4
#?       var tmp/ebx: int <- copy y
#?       tmp <- and 3
#?       compare tmp, 0
#?       {
#?         break-if-!=
#?         color <- copy 3
#?       }
#?       tmp <- copy x
#?       tmp <- and 3
#?       compare tmp, 0
#?       {
#?         break-if-!=
#?         color <- copy 3
#?       }
#?       # highlight color if x and y match curr-x and curr-y (quantized)
#?       {
#?         var xq/edx: int <- copy x
#?         xq <- and 0xfffffffc
#?         var yq/eax: int <- copy y
#?         yq <- and 0xfffffffc
#?         compare xq, curr-x
#?         break-if-!=
#?         compare yq, curr-y
#?         break-if-!=
#?         color <- copy 0xc
#?       }
#?       pixel-on-real-screen x, y, color
#?       x <- increment
#?       loop
#?     }
#?     y <- increment
#?     loop
#?   }
#? }