diff options
-rw-r--r-- | baremetal/120allocate.subx | 2 | ||||
-rw-r--r-- | baremetal/121new-stream.subx | 127 | ||||
-rw-r--r-- | baremetal/501draw-text.mu | 42 | ||||
-rw-r--r-- | baremetal/shell/sandbox.mu | 19 |
4 files changed, 186 insertions, 4 deletions
diff --git a/baremetal/120allocate.subx b/baremetal/120allocate.subx index 4fa42743..38c4110b 100644 --- a/baremetal/120allocate.subx +++ b/baremetal/120allocate.subx @@ -217,7 +217,7 @@ test-allocate-raw-success: # clean up c7 0/subop/copy 0/mod/indirect 5/rm32/.disp32 . . . Next-alloc-id/disp32 0x100/imm32 # copy to *Next-alloc-id # . reclaim locals - 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0x10/imm32 # add to esp + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0x20/imm32 # add to esp # . epilogue 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp 5d/pop-to-ebp diff --git a/baremetal/121new-stream.subx b/baremetal/121new-stream.subx new file mode 100644 index 00000000..241f162e --- /dev/null +++ b/baremetal/121new-stream.subx @@ -0,0 +1,127 @@ +# Helper to allocate a stream on the heap. + +== code +# instruction effective address register displacement immediate +# . op subop mod rm32 base index scale r32 +# . 1-3 bytes 3 bits 2 bits 3 bits 3 bits 3 bits 2 bits 2 bits 0/1/2/4 bytes 0/1/2/4 bytes + +new-stream: # ad: (addr allocation-descriptor), length: int, elemsize: int, out: (addr handle stream _) + # . prologue + 55/push-ebp + 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp + # . save registers + 50/push-eax + 52/push-edx + # var size/edx: int = elemsize*length (clobbering eax) + # . eax = elemsize + 8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 0/r32/eax 0x10/disp8 . # copy *(ebp+16) to eax + # . eax *= length + 31/xor 3/mod/direct 2/rm32/edx . . . 2/r32/edx . . # clear edx + f7 4/subop/multiply 1/mod/*+disp8 5/rm32/ebp . . 0xc/disp8 . # multiply *(ebp+12) into edx:eax + # . if overflow abort + 81 7/subop/compare 3/mod/direct 2/rm32/edx . . . . . 0/imm32 # compare edx + 75/jump-if-!= $new-stream:abort/disp8 + # . edx = elemsize*length + 89/copy 3/mod/direct 2/rm32/edx . . . 0/r32/eax . . # copy eax to edx + # var n/eax: int = size + 12 (for read, write and size) + 05/add-to-eax 0xc/imm32 + # allocate(ad, n, out) + # . . push args + ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 0x14/disp8 . # push *(ebp+20) + 50/push-eax + ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 8/disp8 . # push *(ebp+8) + # . . call + e8/call allocate/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp + # eax = out->payload + 8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 0/r32/eax 0x14/disp8 . # copy *(ebp+20) to eax + 8b/copy 1/mod/*+disp8 0/rm32/eax . . . 0/r32/eax 4/disp8 . # copy *(eax+4) to eax + # skip payload->allocid + 05/add-to-eax 4/imm32 + # eax->size = size + 89/copy 1/mod/*+disp8 0/rm32/eax . . . 2/r32/edx 8/disp8 . # copy edx to *(eax+8) + # clear-stream(eax) + # . . push args + 50/push-eax + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp +$new-stream:end: + # . restore registers + 5a/pop-to-edx + 58/pop-to-eax + # . epilogue + 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp + 5d/pop-to-ebp + c3/return + +$new-stream:abort: + (draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0 "new-stream: size too large" 3 0) # 3=cyan + { + eb/jump loop/disp8 + } + # never gets here + +test-new-stream: + # . prologue + 55/push-ebp + 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp + # var ad/ecx: allocation-descriptor containing 16 bytes + # . var end/ecx: (addr byte) + 89/<- %ecx 4/r32/esp + # . var start/edx: (addr byte) = end - 32 + 81 5/subop/subtract %esp 0x20/imm32 + 89/<- %edx 4/r32/esp + # . ad = {start, end} + 51/push-ecx + 52/push-edx + 89/copy 3/mod/direct 1/rm32/ecx . . . 4/r32/esp . . # copy esp to ecx + # var start/edx = ad->curr + 8b/copy 0/mod/indirect 1/rm32/ecx . . . 2/r32/edx . . # copy *ecx to edx + # var h/ebx: (handle stream byte) + 68/push 0/imm32 + 68/push 0/imm32 + 89/copy 3/mod/direct 3/rm32/ebx . . . 4/r32/esp . . # copy esp to ebx + # new-stream(ad, 3, 2, h) + # . . push args + 53/push-ebx + 68/push 2/imm32 + 68/push 3/imm32 + 51/push-ecx + # . . call + e8/call new-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0x10/imm32 # add to esp + # eax = out->payload + 8b/copy 1/mod/*+disp8 3/rm32/ebx . . . 0/r32/eax 4/disp8 . # copy *(ebx+4) to eax + # check-ints-equal(eax, edx, msg) + # . . push args + 68/push "F - test-new-stream: returns current pointer of allocation descriptor"/imm32 + 52/push-edx + 50/push-eax + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp + # skip payload->allocid + 05/add-to-eax 4/imm32 + # check-ints-equal(eax->size, 6, msg) + # . . push args + 68/push "F - test-new-stream: sets size correctly"/imm32 + 68/push 6/imm32 + ff 6/subop/push 1/mod/*+disp8 0/rm32/eax . . . . . 8/disp8 # push *(eax+8) + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp + # the rest is delegated to clear-stream() so we won't bother checking it + # . reclaim locals + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0x30/imm32 # add to esp + # . epilogue + 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp + 5d/pop-to-ebp + c3/return + +# . . vim:nowrap:textwidth=0 diff --git a/baremetal/501draw-text.mu b/baremetal/501draw-text.mu index 1e7d93a7..b2c2136f 100644 --- a/baremetal/501draw-text.mu +++ b/baremetal/501draw-text.mu @@ -82,6 +82,14 @@ fn draw-text-rightward screen: (addr screen), text: (addr array byte), x: int, x var stream-storage: (stream byte 0x100) var stream/esi: (addr stream byte) <- address stream-storage write stream, text + var xcurr/eax: int <- draw-stream-rightward screen, stream, x, xmax, y, color, background-color + return xcurr +} + +# draw a single-line stream from x, y to xmax +# return the next 'x' coordinate +# if there isn't enough space, truncate +fn draw-stream-rightward screen: (addr screen), stream: (addr stream byte), x: int, xmax: int, y: int, color: int, background-color: int -> _/eax: int { var xcurr/ecx: int <- copy x { var g/eax: grapheme <- read-grapheme stream @@ -137,10 +145,21 @@ fn render-grapheme screen: (addr screen), g: grapheme, xmin: int, ymin: int, xma # return the next (x, y) coordinate in raster order where drawing stopped # that way the caller can draw more if given the same min and max bounding-box. # if there isn't enough space, truncate -fn draw-text-wrapping-right-then-down screen: (addr screen), text: (addr array byte), xmin: int, ymin: int, xmax: int, ymax: int, x: int, y: int, color: int, background-color: int -> _/eax: int, _/ecx: int { +fn draw-text-wrapping-right-then-down screen: (addr screen), text: (addr array byte), xmin: int, ymin: int, xmax: int, ymax: int, _x: int, _y: int, color: int, background-color: int -> _/eax: int, _/ecx: int { var stream-storage: (stream byte 0x100) var stream/esi: (addr stream byte) <- address stream-storage write stream, text + var x/eax: int <- copy _x + var y/ecx: int <- copy _y + x, y <- draw-stream-wrapping-right-then-down screen, stream, xmin, ymin, xmax, ymax, x, y, color, background-color + return x, y +} + +# draw a stream in the rectangle from (xmin, ymin) to (xmax, ymax), starting from (x, y), wrapping as necessary +# return the next (x, y) coordinate in raster order where drawing stopped +# that way the caller can draw more if given the same min and max bounding-box. +# if there isn't enough space, truncate +fn draw-stream-wrapping-right-then-down screen: (addr screen), stream: (addr stream byte), xmin: int, ymin: int, xmax: int, ymax: int, x: int, y: int, color: int, background-color: int -> _/eax: int, _/ecx: int { var xcurr/eax: int <- copy x var ycurr/ecx: int <- copy y var g/ebx: grapheme <- copy 0 @@ -319,6 +338,14 @@ fn draw-text-downward screen: (addr screen), text: (addr array byte), x: int, y: var stream-storage: (stream byte 0x100) var stream/esi: (addr stream byte) <- address stream-storage write stream, text + var ycurr/eax: int <- draw-stream-downward screen, stream, x, y, ymax, color, background-color + return ycurr +} + +# draw a single-line stream vertically from x, y to ymax +# return the next 'y' coordinate +# if there isn't enough space, truncate +fn draw-stream-downward screen: (addr screen), stream: (addr stream byte), x: int, y: int, ymax: int, color: int, background-color: int -> _/eax: int { var ycurr/ecx: int <- copy y { var g/eax: grapheme <- read-grapheme stream @@ -343,10 +370,21 @@ fn draw-text-downward-from-cursor screen: (addr screen), text: (addr array byte) # return the next (x, y) coordinate in raster order where drawing stopped # that way the caller can draw more if given the same min and max bounding-box. # if there isn't enough space, truncate -fn draw-text-wrapping-down-then-right screen: (addr screen), text: (addr array byte), xmin: int, ymin: int, xmax: int, ymax: int, x: int, y: int, color: int, background-color: int -> _/eax: int, _/ecx: int { +fn draw-text-wrapping-down-then-right screen: (addr screen), text: (addr array byte), xmin: int, ymin: int, xmax: int, ymax: int, _x: int, _y: int, color: int, background-color: int -> _/eax: int, _/ecx: int { var stream-storage: (stream byte 0x100) var stream/esi: (addr stream byte) <- address stream-storage write stream, text + var x/eax: int <- copy _x + var y/ecx: int <- copy _y + x, y <- draw-stream-wrapping-down-then-right screen, stream, xmin, ymin, xmax, ymax, x, y, color, background-color + return x, y +} + +# draw a stream down and right in the rectangle from (xmin, ymin) to (xmax, ymax), starting from (x, y), wrapping as necessary +# return the next (x, y) coordinate in raster order where drawing stopped +# that way the caller can draw more if given the same min and max bounding-box. +# if there isn't enough space, truncate +fn draw-stream-wrapping-down-then-right screen: (addr screen), stream: (addr stream byte), xmin: int, ymin: int, xmax: int, ymax: int, x: int, y: int, color: int, background-color: int -> _/eax: int, _/ecx: int { var xcurr/edx: int <- copy x var ycurr/ecx: int <- copy y { diff --git a/baremetal/shell/sandbox.mu b/baremetal/shell/sandbox.mu index 4ca1082c..8ee04f0a 100644 --- a/baremetal/shell/sandbox.mu +++ b/baremetal/shell/sandbox.mu @@ -1,5 +1,6 @@ type sandbox { data: (handle gap-buffer) + value: (handle stream byte) } fn initialize-sandbox _self: (addr sandbox) { @@ -8,6 +9,8 @@ fn initialize-sandbox _self: (addr sandbox) { allocate data-ah var data/eax: (addr gap-buffer) <- lookup *data-ah initialize-gap-buffer data, 0x1000/4KB + var value-ah/eax: (addr handle stream byte) <- get self, value + populate-stream value-ah, 0x1000/4KB } ## some helpers for tests @@ -47,9 +50,14 @@ fn render-sandbox screen: (addr screen), _self: (addr sandbox), x: int, y: int { var data-ah/eax: (addr handle gap-buffer) <- get self, data var data/eax: (addr gap-buffer) <- lookup *data-ah var dummy/eax: int <- render-gap-buffer screen, data, x, y, 1/true + increment y + var value-ah/eax: (addr handle stream byte) <- get self, value + var value/eax: (addr stream byte) <- lookup *value-ah + var dummy/eax: int <- draw-stream-rightward screen, value, x, 0x30/xmax, y, 7/fg=grey, 0/bg } -fn edit-sandbox self: (addr sandbox), key: byte { +fn edit-sandbox _self: (addr sandbox), key: byte { + var self/esi: (addr sandbox) <- copy _self var g/edx: grapheme <- copy key { compare g, 8/backspace @@ -57,5 +65,14 @@ fn edit-sandbox self: (addr sandbox), key: byte { delete-grapheme-before-cursor self return } + { + compare g, 0x13/ctrl-s + break-if-!= + # ctrl-s: run sandbox(es) + var value-ah/eax: (addr handle stream byte) <- get self, value + var value/eax: (addr stream byte) <- lookup *value-ah + write value, "=> LISP" + return + } add-grapheme-to-sandbox self, g } |