# Some useful helpers for dealing with text (arrays of characters) # to-text-line gets called implicitly in various places # define it to be identical to 'to-text' by default def to-text-line x:_elem -> y:address:array:character [ local-scope load-ingredients y <- to-text x ] # variant for arrays (since we can't pass them around otherwise) def array-to-text-line x:address:array:_elem -> y:address:array:character [ local-scope load-ingredients y <- to-text *x ] def equal a:address:array:character, b:address:array:character -> result:boolean [ local-scope load-ingredients a-len:number <- length *a b-len:number <- length *b # compare lengths { trace 99, [text-equal], [comparing lengths] length-equal?:boolean <- equal a-len, b-len break-if length-equal? return 0 } # compare each corresponding character trace 99, [text-equal], [comparing characters] i:number <- copy 0 { done?:boolean <- greater-or-equal i, a-len break-if done? a2:character <- index *a, i b2:character <- index *b, i { chars-match?:boolean <- equal a2, b2 break-if chars-match? return 0 } i <- add i, 1 loop } return 1 ] scenario text-equal-reflexive [ run [ local-scope x:address:array:character <- new [abc] 10:boolean/raw <- equal x, x ] memory-should-contain [ 10 <- 1 # x == x for all x ] ] scenario text-equal-identical [ run [ local-scope x:address:array:character <- new [abc] y:address:array:character <- new [abc] 10:boolean/raw <- equal x, y ] memory-should-contain [ 10 <- 1 # abc == abc ] ] scenario text-equal-distinct-lengths [ run [ local-scope x:address:array:character <- new [abc] y:address:array:character <- new [abcd] 10:boolean/raw <- equal x, y ] memory-should-contain [ 10 <- 0 # abc != abcd ] trace-should-contain [ text-equal: comparing lengths ] trace-should-not-contain [ text-equal: comparing characters ] ] scenario text-equal-with-empty [ run [ local-scope x:address:array:character <- new [] y:address:array:character <- new [abcd] 10:boolean/raw <- equal x, y ] memory-should-contain [ 10 <- 0 # "" != abcd ] ] scenario text-equal-common-lengths-but-distinct [ run [ local-scope x:address:array:character <- new [abc] y:address:array:character <- new [abd] 10:boolean/raw <- equal x, y ] memory-should-contain [ 10 <- 0 # abc != abd ] ] # A new type to help incrementally construct texts. container buffer [ length:number data:address:array:character ] def new-buffer capacity:number -> result:address:buffer [ local-scope load-ingredients result <- new buffer:type *result <- put *result, length:offset, 0 data:address:array:character <- new character:type, capacity *result <- put *result, data:offset, data return result ] def grow-buffer in:address:buffer -> in:address:buffer [ local-scope load-ingredients # double buffer size olddata:address:array:character <- get *in, data:offset oldlen:number <- length *olddata newlen:number <- multiply oldlen, 2 newdata:address:array:character <- new ch
Copyright (c) 2009, 2010 hut <hut@lavabit.com>

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
a, 122/z 10:array:character/raw <- copy *x ] memory-should-contain [ 10:array:character <- [zbc] ] ] scenario replace-character-at-end [ run [ local-scope x:address:array:character <- new [abc] x <- replace x, 99/c, 122/z 10:array:character/raw <- copy *x ] memory-should-contain [ 10:array:character <- [abz] ] ] scenario replace-character-missing [ run [ local-scope x:address:array:character <- new [abc] x <- replace x, 100/d, 122/z 10:array:character/raw <- copy *x ] memory-should-contain [ 10:array:character <- [abc] ] ] scenario replace-all-characters [ run [ local-scope x:address:array:character <- new [banana] x <- replace x, 97/a, 122/z 10:array:character/raw <- copy *x ] memory-should-contain [ 10:array:character <- [bznznz] ] ] # replace underscores in first with remaining args def interpolate template:address:array:character -> result:address:array:character [ local-scope load-ingredients # consume just the template # compute result-len, space to allocate for result tem-len:number <- length *template result-len:number <- copy tem-len { # while ingredients remain a:address:array:character, arg-received?:boolean <- next-ingredient break-unless arg-received? # result-len = result-len + arg.length - 1 (for the 'underscore' being replaced) a-len:number <- length *a result-len <- add result-len, a-len result-len <- subtract result-len, 1 loop } rewind-ingredients _ <- next-ingredient # skip template result <- new character:type, result-len # repeatedly copy sections of template and 'holes' into result result-idx:number <- copy 0 i:number <- copy 0 { # while arg received a:address:array:character, arg-received?:boolean <- next-ingredient break-unless arg-received? # copy template into result until '_' { # while i < template.length tem-done?:boolean <- greater-or-equal i, tem-len break-if tem-done?, +done:label # while template[i] != '_' in:character <- index *template, i underscore?:boolean <- equal in, 95/_ break-if underscore? # result[result-idx] = template[i] *result <- put-index *result, result-idx, in i <- add i, 1 result-idx <- add result-idx, 1 loop } # copy 'a' into result j:number <- copy 0 { # while j < a.length arg-done?:boolean <- greater-or-equal j, a-len break-if arg-done? # result[result-idx] = a[j] in:character <- index *a, j *result <- put-index *result, result-idx, in j <- add j, 1 result-idx <- add result-idx, 1 loop } # skip '_' in template i <- add i, 1 loop # interpolate next arg } +done # done with holes; copy rest of template directly into result { # while i < template.length tem-done?:boolean <- greater-or-equal i, tem-len break-if tem-done? # result[result-idx] = template[i] in:character <- index *template, i *result <- put-index *result, result-idx, in i <- add i, 1 result-idx <- add result-idx, 1 loop } ] scenario interpolate-works [ run [ local-scope x:address:array:character <- new [abc_ghi] y:address:array:character <- new [def] z:address:array:character <- interpolate x, y 10:array:character/raw <- copy *z ] memory-should-contain [ 10:array:character <- [abcdefghi] ] ] scenario interpolate-at-start [ run [ local-scope x:address:array:character <- new [_, hello!] y:address:array:character <- new [abc] z:address:array:character <- interpolate x, y 10:array:character/raw <- copy *z ] memory-should-contain [ 10:array:character <- [abc, hello!] 22 <- 0 # out of bounds ] ] scenario interpolate-at-end [ run [ x:address:array:character <- new [hello, _] y:address:array:character <- new [abc] z:address:array:character <- interpolate x, y 10:array:character/raw <- copy *z ] memory-should-contain [ 10:array:character <- [hello, abc] ] ] # result:boolean <- space? c:character def space? c:character -> result:boolean [ local-scope load-ingredients # most common case first result <- equal c, 32/space return-if result result <- equal c, 10/newline return-if result result <- equal c, 9/tab return-if result result <- equal c, 13/carriage-return return-if result # remaining uncommon cases in sorted order # http://unicode.org code-points in unicode-set Z and Pattern_White_Space result <- equal c, 11/ctrl-k return-if result result <- equal c, 12/ctrl-l return-if result result
# A minimal bootable image that:
#   - loads more sectors past the first boot sector (using BIOS primitives)
#   - switches to 32-bit mode (giving up access to BIOS primitives)
#   - sets up a keyboard handler to print '1' at the top-left of screen when '1' is typed
#
# When it's ready to accept keys, it prints 'H' to the top-left of the screen.
#
# If the initial load fails, it prints 'D' to the top-left of the screen and
# halts.
#
# To convert to a disk image, first prepare a realistically sized disk image:
#   dd if=/dev/zero of=code.img count=20160  # 512-byte sectors, so 10MB
# Now fill in sectors:
#   bootstrap/bootstrap run hex < boot0.hex > boot.bin
#   dd if=boot.bin of=code.img conv=notrunc
# To run:
#   qemu-system-i386 code.img
# Or:
#   bochs -f bochsrc  # bochsrc loads code.img
#
# Since we start out in 16-bit mode, we need instructions SubX doesn't
# support.
# This file contains just lowercase hex bytes and comments. Zero
# error-checking. Make liberal use of:
#   - comments documenting expected offsets
#   - size checks on the emitted file (currently: 512 bytes)
#   - xxd to eyeball that offsets contain expected bytes

## 16-bit entry point

# Upon reset, the IBM PC
#   loads the first sector (512 bytes)
#   from some bootable image (see the boot sector marker at the end of this file)
#   to the address range [0x7c00, 0x7e00)

# offset 00 (address 0x7c00):
  # disable interrupts for this initialization
  fa  # cli

  # initialize segment registers
  # this isn't always needed, but the recommendation is to not make assumptions
  b8 00 00  # ax <- 0
  8e d8  # ds <- ax
  8e d0  # ss <- ax
  8e c0  # es <- ax
  8e e0  # fs <- ax
  8e e8  # gs <- ax

  # We don't read or write the stack before we get to 32-bit mode. No function
  # calls, so we don't need to initialize the stack.

# 0e:
  # load more sectors from disk
  b4 02  # ah <- 2  # read sectors from disk
  # dl comes conveniently initialized at boot time with the index of the device being booted
  b5 00  # ch <- 0  # cylinder 0
  b6 00  # dh <- 0  # track 0
  b1 02  # cl <- 2  # second sector, 1-based
  b0 01  # al <- 1  # number of sectors to read
  # address to write sectors to = es:bx = 0x7e00, contiguous with boot segment
  bb 00 00  # bx <- 0
  8e c3  # es <- bx
  bb 00 7e  # bx <- 0x7e00
  cd 13  # int 13h, BIOS disk service
  0f 82 76 00  # jump-if-carry disk-error

# 26:
  # undo the A20 hack: https://en.wikipedia.org/wiki/A20_line
  # this is from https://github.com/mit-pdos/xv6-public/blob/master/bootasm.S
  # seta20.1:
  e4 64  # al <- port 0x64
  a8 02  # set zf if bit 1 (second-least significant) is not set
  75 fa  # if zf not set, goto seta20.1 (-6)

  b0 d1  # al <- 0xd1
  e6 64  # port 0x64 <- al

# 30:
  # seta20.2:
  e4 64  # al <- port 0x64
  a8 02  # set zf if bit 1 (second-least significant) is not set
  75 fa  # if zf not set, goto seta20.2 (-6)

  b0 df  # al <- 0xdf
  e6 64  # port 0x64 <- al

# 3a:
  # switch to 32-bit mode
  0f 01 16  # lgdt 00/mod/indirect 010/subop 110/rm/use-disp16
    80 7c  # *gdt_descriptor
# 3f:
  0f 20 c0  # eax <- cr0
  66 83 c8 01  # eax <- or 0x1
  0f 22 c0  # cr0 <- eax
  ea c0 7c 08 00  # far jump to initialize_32bit_mode after setting cs to the record at offset 8 in the gdt (gdt_code)

# padding
# 4e:
                                          00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

## GDT: 3 records of 8 bytes each

# 60:
# gdt_start:
# gdt_null:  mandatory null descriptor
  00 00 00 00 00 00 00 00
# gdt_code:  (offset 8 from gdt_start)
  ff ff  # limit[0:16]
  00 00 00  # base[0:24]
  9a  # 1/present 00/privilege 1/descriptor type = 1001b
      # 1/code 0/conforming 1/readable 0/accessed = 1010b
  cf  # 1/granularity 1/32-bit 0/64-bit-segment 0/AVL = 1100b
      # limit[16:20] = 1111b
  00  # base[24:32]
# gdt_data:  (offset 16 from gdt_start)
  ff ff  # limit[0:16]
  00 00 00  # base[0:24]
  92  # 1/present 00/privilege 1/descriptor type = 1001b
      # 0/data 0/conforming 1/readable 0/accessed = 0010b
  cf  # same as gdt_code
  00  # base[24:32]
# gdt_end:

# padding
# 78:
                        00 00 00 00 00 00 00 00

# 80:
# gdt_descriptor:
  17 00  # final index of gdt = gdt_end - gdt_start - 1
  60 7c 00 00  # start = gdt_start

# padding
# 85:
                  00 00 00 00 00 00 00 00 00 00

# 90:
# disk_error:
  # print 'D' to top-left of screen to indicate disk error
  # *0xb8000 <- 0x0f44
  # bx <- 0xb800
  bb 00 b8
  # ds <- bx
  8e db  # 11b/mod 011b/reg/ds 011b/rm/bx
  # al <- 'D'
  b0 44
  # ah <- 0x0f  # white on black
  b4 0f
  # bx <- 0
  bb 00 00
  # *ds:bx <- ax
  89 07  # 00b/mod/indirect 000b/reg/ax 111b/rm/bx

e9 fb ff  # loop forever

# padding
# a1:
   00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

## 32-bit code from this point (still some instructions not in SubX)

# c0:
# initialize_32bit_mode:
  66 b8 10 00  # ax <- offset 16 from gdt_start
  8e d8  # ds <- ax
  8e d0  # ss <- ax
  8e c0  # es <- ax
  8e e0  # fs <- ax
  8e e8  # gs <- ax

  # load interrupt handlers
  0f 01 1d  # lidt 00/mod/indirect 011/subop 101/rm32/use-disp32
    00 7f 00 00  # *idt_descriptor

  # enable keyboard IRQ
  b0 fd  # al <- 0xfd  # enable just IRQ1
  e6 21  # port 0x21 <- al

  # initialization is done; enable interrupts
  fb
  e9 21 00 00 00  # jump to 0x7d00

# padding
# df:
                                             00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

## 'application' SubX code: print one character to top-left of screen

# offset 100 (address 0x7d00):
# Entry:
  # eax <- *0x7ff4  # random address in second segment containing 'H'
  8b  # copy rm32 to r32
    05  # 00/mod/indirect 000/r32/eax 101/rm32/use-disp32
    # disp32
    f4 7f 00 00
  # *0xb8000 <- eax
  89  # copy r32 to rm32
    05  # 00/mod/indirect 000/r32/eax 101/rm32/use-disp32
    # disp32
    00 80 0b 00

e9 fb ff ff ff  # loop forever

# padding
# 111:
   00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

# 120:
# null interrupt handler:
  cf  # iret

# padding
# 121:
   00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

# 130:
# keyboard interrupt handler:
  # prologue
  fa  # disable interrupts
  60  # push all registers to stack
  # acknowledge interrupt
  b0 20  # al <- 0x20
  e6 20  # port 0x20 <- al
  # check output buffer of 8042 keyboard controller (https://web.archive.org/web/20040604041507/http://panda.cs.ndsu.nodak.edu/~achapwes/PICmicro/keyboard/atkeyboard.html)
  e4 64  # al <- port 0x64
  a8 01  # set zf if bit 0 (least significant) is not set
  74 11  # if bit 0 is not set, skip to epilogue
  # read keycode into eax
  31 c0  # eax <- xor eax;  11/direct 000/r32/eax 000/rm32/eax
  e4 60  # al <- port 0x60
  # map key '1' to ascii; if eax == 2, eax = 0x31
  3d 02 00 00 00  # compare eax with 0x02
  75 0b  # if not equal, goto epilogue
  b8 31 0f 00 00  # eax <- 0x0f31
  # print eax to top-left of screen (*0xb8000)
  89  # copy r32 to rm32
    05  # 00/mod/indirect 000/r32/eax 101/rm32/use-disp32
    # disp32
    00 80 0b 00
  # epilogue
  61  # pop all registers
  fb  # enable interrupts
  cf  # iret

# padding
# 155
               00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00

# final 2 bytes of boot sector
55 aa

## sector 2
# loaded by load_disk, not automatically on boot

# offset 200 (address 0x7e00): interrupt descriptor table
# 32 entries * 8 bytes each = 256 bytes (0x100)
# idt_start:

00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00

# entry 8: clock
  20 7d  # target[0:16] = null interrupt handler
  08 00  # segment selector (gdt_code)
  00  # unused
  8e  # 1/p 00/dpl 0 1110/type/32-bit-interrupt-gate
  00 00  # target[16:32]

# entry 9: keyboard
  30 7d  # target[0:16] = keyboard interrupt handler
  08 00  # segment selector (gdt_code)
  00  # unused
  8e  # 1/p 00/dpl 0 1110/type/32-bit-interrupt-gate
  00 00  # target[16:32]

00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
# idt_end:

# offset 300 (address 0x7f00):
# idt_descriptor:
  ff 00  # idt_end - idt_start - 1
  00 7e 00 00  # start = idt_start

# padding
                  00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 48 0f 00 00 00 00 00 00 00 00 00 00  # spot the 'H' with attributes
# offset 400 (address 0x8000)

# vim:ft=conf
dest-idx, src src-idx <- add src-idx, 1 dest-idx <- add dest-idx, 1 loop } ] scenario text-copy-copies-partial-text [ run [ local-scope x:address:array:character <- new [abc] y:address:array:character <- copy-range x, 1, 3 1:array:character/raw <- copy *y ] memory-should-contain [ 1:array:character <- [bc] ] ] scenario text-copy-out-of-bounds [ run [ local-scope x:address:array:character <- new [abc] y:address:array:character <- copy-range x, 2, 4 1:array:character/raw <- copy *y ] memory-should-contain [ 1:array:character <- [c] ] ] scenario text-copy-out-of-bounds-2 [ run [ local-scope x:address:array:character <- new [abc] y:address:array:character <- copy-range x, 3, 3 1:array:character/raw <- copy *y ] memory-should-contain [ 1:array:character <- [] ] ]