about summary refs log tree commit diff stats
path: root/apps/ex10.mu.
blob: 35fea653d2ffc943326a4a8ecca4a7cf9d7e1be9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# 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
#?   }
#? }