https://github.com/akkartik/mu/blob/main/boot.subx
   1 # Code for the first few disk sectors that all programs in this directory need:
   2 #   - load sectors past the first (using BIOS primitives) since only the first is available by default
   3 #     - if this fails, print 'D' at top-left of screen and halt
   4 #   - initialize a minimal graphics mode
   5 #   - switch to 32-bit mode (giving up access to BIOS primitives)
   6 #   - set up a handler for keyboard events
   7 #   - jump to start of program
   8 
   9 # Code in this file needs to be more deliberate about the SubX facilities it
  10 # uses:
  11 #   - sigils only support 32-bit general-purpose registers, so don't work with segment registers or 16-bit or 8-bit registers
  12 #   - metadata like rm32 and r32 can sometimes misleadingly refer to only the bottom 16 bits of the register; pay attention to the register name
  13 #
  14 # While most of Mu is thoroughly tested, this file is not. I don't yet
  15 # understand hardware interfaces well enough to explain to others.
  16 
  17 # Memory map of a Mu computer:
  18 #   code: currently 4 tracks loaded from the primary disk to [0x00007c00, 0x00048600)
  19 #   stack: grows down from 0x02000000 to 0x01000000
  20 #   heap: [0x02000000, 0x08000000)
  21 #     see 120allocate.subx; Qemu initializes with 128MB RAM by default
  22 # Consult https://wiki.osdev.org/Memory_Map_(x86) before modifying any of
  23 # this. And don't forget to keep *stack-debug.subx in sync.
  24 
  25 == code
  26 
  27 ## 16-bit entry point: 0x7c00
  28 
  29 # Upon reset, the IBM PC:
  30 #   - loads the first sector (512 bytes)
  31 #     from some bootable image (look for the boot-sector-marker further down this file)
  32 #     to the address range [0x7c00, 0x7e00)
  33 #   - starts executing code at address 0x7c00
  34 
  35   fa/disable-interrupts
  36 
  37   # initialize segment registers
  38   b8/copy-to-ax 0/imm16
  39   8e/->seg 3/mod/direct 0/rm32/ax 3/r32/ds
  40   8e/->seg 3/mod/direct 0/rm32/ax 0/r32/es
  41   8e/->seg 3/mod/direct 0/rm32/ax 4/r32/fs
  42   8e/->seg 3/mod/direct 0/rm32/ax 5/r32/gs
  43 
  44   # Temporarily initialize stack to 0x00070000 in real mode.
  45   # We don't read or write the stack before we get to 32-bit mode, but BIOS
  46   # calls do. We need to move the stack in case BIOS initializes it to some
  47   # low address that we want to write code into.
  48   b8/copy-to-ax 0x7000/imm16
  49   8e/->seg 3/mod/direct 0/rm32/ax 2/r32/ss
  50   bc/copy-to-esp 0/imm16
  51 
  52   # undo the A20 hack: https://en.wikipedia.org/wiki/A20_line
  53   # this is from https://github.com/mit-pdos/xv6-public/blob/master/bootasm.S
  54   {
  55     e4/read-port-into-al 0x64/imm8
  56     a8/test-bits-in-al 0x02/imm8  # set zf if bit 1 (second-least significant) is not set
  57     75/jump-if-!zero loop/disp8
  58     b0/copy-to-al 0xd1/imm8
  59     e6/write-al-into-port 0x64/imm8
  60   }
  61   {
  62     e4/read-port-into-al 0x64/imm8
  63     a8/test-bits-in-al 0x02/imm8  # set zf if bit 1 (second-least significant) is not set
  64     75/jump-if-!zero loop/disp8
  65     b0/copy-to-al 0xdf/imm8
  66     e6/write-al-into-port 0x64/imm8
  67   }
  68 
  69   # load remaining sectors from first two tracks of disk into addresses [0x7e00, 0x17800)
  70   b4/copy-to-ah 2/imm8/read-drive
  71   # dl comes conveniently initialized at boot time with the index of the device being booted
  72   b5/copy-to-ch 0/imm8/cylinder
  73   b6/copy-to-dh 0/imm8/head
  74   b1/copy-to-cl 2/imm8/sector  # 1-based
  75   b0/copy-to-al 0x7d/imm8/num-sectors  # 2*63 - 1 = 125
  76   # address to write sectors to = es:bx = 0x7e00, contiguous with boot segment
  77   bb/copy-to-bx 0/imm16
  78   8e/->seg 3/mod/direct 3/rm32/bx 0/r32/es
  79   bb/copy-to-bx 0x7e00/imm16
  80   cd/syscall 0x13/imm8/bios-disk-services
  81   0f 82/jump-if-carry disk_error/disp16
  82 
  83   # load two more tracks of disk into addresses [0x17800, 0x27400)
  84   b4/copy-to-ah 2/imm8/read-drive
  85   # dl comes conveniently initialized at boot time with the index of the device being booted
  86   b5/copy-to-ch 0/imm8/cylinder
  87   b6/copy-to-dh 2/imm8/head
  88   b1/copy-to-cl 1/imm8/sector  # 1-based
  89   b0/copy-to-al 0x7e/imm8/num-sectors  # 2*63 = 126
  90   # address to write sectors to = es:bx = 0x17800, contiguous with boot segment
  91   bb/copy-to-bx 0x1780/imm16
  92   8e/->seg 3/mod/direct 3/rm32/bx 0/r32/es
  93   bb/copy-to-bx 0/imm16
  94   cd/syscall 0x13/imm8/bios-disk-services
  95   0f 82/jump-if-carry disk_error/disp16
  96 
  97   # load two more tracks of disk into addresses [0x27400, 0x37000)
  98   b4/copy-to-ah 2/imm8/read-drive
  99   # dl comes conveniently initialized at boot time with the index of the device being booted
 100   b5/copy-to-ch 0/imm8/cylinder
 101   b6/copy-to-dh 4/imm8/head
 102   b1/copy-to-cl 1/imm8/sector  # 1-based
 103   b0/copy-to-al 0x7e/imm8/num-sectors  # 2*63 = 126
 104   # address to write sectors to = es:bx = 0x27400, contiguous with boot segment
 105   bb/copy-to-bx 0x2740/imm16
 106   8e/->seg 3/mod/direct 3/rm32/bx 0/r32/es
 107   bb/copy-to-bx 0/imm16
 108   cd/syscall 0x13/imm8/bios-disk-services
 109   0f 82/jump-if-carry disk_error/disp16
 110 
 111   # load two more tracks of disk into addresses [0x37000, 0x46c00)
 112   b4/copy-to-ah 2/imm8/read-drive
 113   # dl comes conveniently initialized at boot time with the index of the device being booted
 114   b5/copy-to-ch 0/imm8/cylinder
 115   b6/copy-to-dh 6/imm8/head
 116   b1/copy-to-cl 1/imm8/sector  # 1-based
 117   b0/copy-to-al 0x7e/imm8/num-sectors  # 2*63 = 126
 118   # address to write sectors to = es:bx = 0x37000, contiguous with boot segment
 119   bb/copy-to-bx 0x3700/imm16
 120   8e/->seg 3/mod/direct 3/rm32/bx 0/r32/es
 121   bb/copy-to-bx 0/imm16
 122   cd/syscall 0x13/imm8/bios-disk-services
 123   0f 82/jump-if-carry disk_error/disp16
 124 
 125   # reset es
 126   bb/copy-to-bx 0/imm16
 127   8e/->seg 3/mod/direct 3/rm32/bx 0/r32/es
 128 
 129   # adjust video mode
 130   b4/copy-to-ah 0x4f/imm8  # VBE commands
 131   b0/copy-to-al 2/imm8  # set video mode
 132   bb/copy-to-bx 0x4105/imm16  # 0x0105 | 0x4000
 133                               # 0x0105 = graphics mode 1024x768x256
 134                               #  (alternative candidate: 0x0101 for 640x480x256)
 135                               # 0x4000 bit = configure linear frame buffer in Bochs emulator; hopefully this doesn't hurt anything when running natively
 136   cd/syscall 0x10/imm8/bios-video-services
 137 
 138   # load information for the (hopefully) current video mode
 139   # mostly just for the address to the linear frame buffer
 140   b4/copy-to-ah 0x4f/imm8  # VBE commands
 141   b0/copy-to-al 1/imm8  # get video mode info
 142   b9/copy-to-cx 0x0105/imm16  # mode we requested
 143   bf/copy-to-di Video-mode-info/imm16
 144   cd/syscall 0x10/imm8/bios-video-services
 145 
 146   ## switch to 32-bit mode
 147   # load global descriptor table
 148   # We can't refer to the label directly because SubX doesn't do the right
 149   # thing for lgdt, so rather than make errors worse in most places we instead
 150   # pin gdt_descriptor below.
 151   0f 01 2/subop/lgdt 0/mod/indirect 6/rm32/use-disp16 0x7ce0/disp16/gdt_descriptor
 152   # enable paging
 153   0f 20/<-cr 3/mod/direct 0/rm32/eax 0/r32/cr0
 154   66 83 1/subop/or 3/mod/direct 0/rm32/eax 1/imm8  # eax <- or 0x1
 155   0f 22/->cr 3/mod/direct 0/rm32/eax 0/r32/cr0
 156   # far jump to initialize_32bit_mode that sets cs to offset 8 in the gdt in the process
 157   # We can't refer to the label directly because SubX doesn't have syntax for
 158   # segment selectors. So we instead pin initialize_32bit_mode below.
 159   ea/jump-far-absolute 0x00087d00/disp32  # address 0x7d00 in offset 8 of the gdt
 160 
 161 disk_error:
 162   # print 'D' to top-left of screen to indicate disk error
 163   # *0xb8000 <- 0x0f44
 164   bb/copy-to-bx 0xb800/imm16
 165   8e/->seg 3/mod/direct 3/rm32/bx 3/r32/ds
 166   b0/copy-to-al 0x44/imm8/D
 167   b4/copy-to-ah 0x0f/imm8/white-on-black
 168   bb/copy-to-bx 0/imm16
 169   89/<- 0/mod/indirect 7/rm32/bx 0/r32/ax  # *ds:bx <- ax
 170   # loop forever
 171   {
 172     eb/jump loop/disp8
 173   }
 174 
 175 ## GDT: 3 records of 8 bytes each
 176 == data 0x7ce0
 177 gdt_descriptor:
 178   0x17/imm16  # final index of gdt = size of gdt - 1
 179   gdt_start/imm32/start
 180 
 181 gdt_start:
 182 # offset 0: gdt_null:  mandatory null descriptor
 183   00 00 00 00 00 00 00 00
 184 # offset 8: gdt_code
 185   ff ff  # limit[0:16]
 186   00 00 00  # base[0:24]
 187   9a  # 1/present 00/privilege 1/descriptor type = 1001b
 188       # 1/code 0/conforming 1/readable 0/accessed = 1010b
 189   cf  # 1/granularity 1/32-bit 0/64-bit-segment 0/AVL = 1100b
 190       # limit[16:20] = 1111b
 191   00  # base[24:32]
 192 # offset 16: gdt_data
 193   ff ff  # limit[0:16]
 194   00 00 00  # base[0:24]
 195   92  # 1/present 00/privilege 1/descriptor type = 1001b
 196       # 0/data 0/conforming 1/readable 0/accessed = 0010b
 197   cf  # same as gdt_code
 198   00  # base[24:32]
 199 # gdt_end:
 200 
 201 ## 32-bit code from this point
 202 
 203 == code 0x7d00
 204 initialize_32bit_mode:
 205   66 b8/copy-to-ax 0x10/imm16  # offset 16 from gdt_start
 206   8e/->seg 3/mod/direct 0/rm32/ax 3/r32/ds
 207   8e/->seg 3/mod/direct 0/rm32/ax 2/r32/ss
 208   8e/->seg 3/mod/direct 0/rm32/ax 0/r32/es
 209   8e/->seg 3/mod/direct 0/rm32/ax 4/r32/fs
 210   8e/->seg 3/mod/direct 0/rm32/ax 5/r32/gs
 211 
 212   bc/copy-to-esp 0x02000000/imm32
 213 
 214   ## load interrupt handlers
 215   # We can't refer to the label directly because SubX doesn't do the right
 216   # thing for lidt, so rather than make errors worse in most places we instead
 217   # pin idt_descriptor below.
 218   0f 01 3/subop/lidt 0/mod/indirect 5/rm32/use-disp32 0x7e00/disp32/idt_descriptor
 219 
 220   # For now, not bothering reprogramming the IRQ to not conflict with software
 221   # exceptions.
 222   #   https://wiki.osdev.org/index.php?title=8259_PIC&oldid=24650#Protected_Mode
 223   #
 224   # Interrupt 1 (keyboard) conflicts with debugger faults. We don't use a
 225   # debugger.
 226   # Reference:
 227   #   https://wiki.osdev.org/Exceptions
 228 
 229   # enable timer IRQ0 and keyboard IRQ1
 230   b0/copy-to-al 0xfc/imm8  # disable mask for IRQ0 and IRQ1
 231   e6/write-al-into-port 0x21/imm8
 232 
 233   fb/enable-interrupts
 234 
 235   (initialize-mouse)
 236 
 237   ## enable floating point
 238   db/floating-point-coprocessor e3/initialize
 239   # eax <- cr4
 240   0f 20/<-cr 3/mod/direct 0/rm32/eax 4/r32/cr4
 241   # eax <- or bit 9
 242   0f ba/bit-test 5/subop/bit-test-and-set 3/mod/direct 0/rm32/eax 9/imm8
 243   # cr4 <- eax
 244   0f 22/->cr 3/mod/direct 0/rm32/eax 4/r32/cr4
 245 
 246   e9/jump Entry/disp32
 247 
 248 == boot-sector-marker 0x7dfe
 249 # final 2 bytes of boot sector
 250 55 aa
 251 
 252 ## sector 2 onwards loaded by load_disk, not automatically on boot
 253 
 254 == data 0x7e00
 255 idt_descriptor:
 256   ff 03  # final index of idt = size of idt - 1
 257   idt_start/imm32/start
 258 
 259 +-- 55 lines: # interrupt descriptor table ----------------------------------------------------------------------------------------------------------------------------------------------
 314 
 315 == code
 316 
 317 null-interrupt-handler:
 318   # prologue
 319   # Don't disable interrupts; the timer has the highest priority anyway,
 320   # and this interrupt triggers extremely frequently.
 321   fa/disable-interrupts
 322   60/push-all-registers
 323   9c/push-flags
 324   # acknowledge interrupt
 325   b0/copy-to-al 0x20/imm8
 326   e6/write-al-into-port 0x20/imm8
 327   31/xor %eax 0/r32/eax
 328 $null-interrupt-handler:epilogue:
 329   # epilogue
 330   9d/pop-flags
 331   61/pop-all-registers
 332   fb/enable-interrupts
 333   cf/return-from-interrupt
 334 
 335 timer-interrupt-handler:
 336   # prologue
 337   # Don't disable interrupts; the timer has the highest priority anyway,
 338   # and this interrupt triggers extremely frequently.
 339   fa/disable-interrupts
 340   60/push-all-registers
 341   9c/push-flags
 342   # acknowledge interrupt
 343   b0/copy-to-al 0x20/imm8
 344   e6/write-al-into-port 0x20/imm8
 345   31/xor %eax 0/r32/eax
 346 $timer-interrupt-handler:epilogue:
 347   # epilogue
 348   9d/pop-flags
 349   61/pop-all-registers
 350   fb/enable-interrupts
 351   cf/return-from-interrupt
 352 
 353 keyboard-interrupt-handler:
 354   # prologue
 355   fa/disable-interrupts
 356   60/push-all-registers
 357   9c/push-flags
 358   # acknowledge interrupt
 359   b0/copy-to-al 0x20/imm8
 360   e6/write-al-into-port 0x20/imm8
 361   31/xor %eax 0/r32/eax
 362   # check output buffer of 8042 keyboard controller (https://web.archive.org/web/20040604041507/http://panda.cs.ndsu.nodak.edu/~achapwes/PICmicro/keyboard/atkeyboard.html)
 363   e4/read-port-into-al 0x64/imm8
 364   a8/test-bits-in-al 0x01/imm8  # set zf if bit 0 (least significant) is not set
 365   0f 84/jump-if-not-set $keyboard-interrupt-handler:epilogue/disp32
 366   # - if keyboard buffer is full, return
 367   # var dest-addr/ecx: (addr byte) = (keyboard-buffer + *keyboard-buffer:write)
 368   31/xor %ecx 1/r32/ecx
 369   8a/byte-> *Keyboard-buffer:write 1/r32/cl
 370   81 0/subop/add %ecx Keyboard-buffer:data/imm32
 371   # al = *dest-addr
 372   8a/byte-> *ecx 0/r32/al
 373   # if (al != 0) return
 374   3c/compare-al-and 0/imm8
 375   0f 85/jump-if-!= $keyboard-interrupt-handler:epilogue/disp32
 376   # - read keycode
 377   e4/read-port-into-al 0x60/imm8
 378   # - key released
 379   # if (al == 0xaa) shift = false  # left shift is being lifted
 380   {
 381     3c/compare-al-and 0xaa/imm8
 382     75/jump-if-!= break/disp8
 383     # *shift = 0
 384     c7 0/subop/copy *Keyboard-shift-pressed? 0/imm32
 385   }
 386   # if (al == 0xb6) shift = false  # right shift is being lifted
 387   {
 388     3c/compare-al-and 0xb6/imm8
 389     75/jump-if-!= break/disp8
 390     # *shift = 0
 391     c7 0/subop/copy *Keyboard-shift-pressed? 0/imm32
 392   }
 393   # if (al == 0x9d) ctrl = false  # ctrl is being lifted
 394   {
 395     3c/compare-al-and 0x9d/imm8
 396     75/jump-if-!= break/disp8
 397     # *ctrl = 0
 398     c7 0/subop/copy *Keyboard-ctrl-pressed? 0/imm32
 399   }
 400   # if (al & 0x80) a key is being lifted; return
 401   50/push-eax
 402   24/and-al-with 0x80/imm8
 403   3c/compare-al-and 0/imm8
 404   58/pop-to-eax
 405   75/jump-if-!= $keyboard-interrupt-handler:epilogue/disp8
 406   # - key pressed
 407   # if (al == 0x2a) shift = true, return  # left shift pressed
 408   {
 409     3c/compare-al-and 0x2a/imm8
 410     75/jump-if-!= break/disp8
 411     # *shift = 1
 412     c7 0/subop/copy *Keyboard-shift-pressed? 1/imm32
 413     # return
 414     eb/jump $keyboard-interrupt-handler:epilogue/disp8
 415   }
 416   # if (al == 0x36) shift = true, return  # right shift pressed
 417   {
 418     3c/compare-al-and 0x36/imm8
 419     75/jump-if-!= break/disp8
 420     # *shift = 1
 421     c7 0/subop/copy *Keyboard-shift-pressed? 1/imm32
 422     # return
 423     eb/jump $keyboard-interrupt-handler:epilogue/disp8
 424   }
 425   # if (al == 0x1d) ctrl = true, return
 426   {
 427     3c/compare-al-and 0x1d/imm8
 428     75/jump-if-!= break/disp8
 429     # *ctrl = 1
 430     c7 0/subop/copy *Keyboard-ctrl-pressed? 1/imm32
 431     # return
 432     eb/jump $keyboard-interrupt-handler:epilogue/disp8
 433   }
 434   # - convert key to character
 435   # if (shift) use keyboard shift map
 436   {
 437     81 7/subop/compare *Keyboard-shift-pressed? 0/imm32
 438     74/jump-if-= break/disp8
 439     # sigils don't currently support labels inside *(eax+label)
 440     05/add-to-eax Keyboard-shift-map/imm32
 441     8a/byte-> *eax 0/r32/al
 442     eb/jump $keyboard-interrupt-handler:select-map-done/disp8
 443   }
 444   # if (ctrl) al = *(ctrl map + al)
 445   {
 446     81 7/subop/compare *Keyboard-ctrl-pressed? 0/imm32
 447     74/jump-if-= break/disp8
 448     05/add-to-eax Keyboard-ctrl-map/imm32
 449     8a/byte-> *eax 0/r32/al
 450     eb/jump $keyboard-interrupt-handler:select-map-done/disp8
 451   }
 452   # otherwise al = *(normal map + al)
 453   05/add-to-eax Keyboard-normal-map/imm32
 454   8a/byte-> *eax 0/r32/al
 455 $keyboard-interrupt-handler:select-map-done:
 456   # - if there's no character mapping, return
 457   {
 458     3c/compare-al-and 0/imm8
 459     74/jump-if-= break/disp8
 460     # - store al in keyboard buffer
 461     88/<- *ecx 0/r32/al
 462     # increment index
 463     fe/increment-byte *Keyboard-buffer:write
 464     # clear top nibble of index (keyboard buffer is circular)
 465     80 4/subop/and-byte *Keyboard-buffer:write 0x0f/imm8
 466   }
 467 $keyboard-interrupt-handler:epilogue:
 468   # epilogue
 469   9d/pop-flags
 470   61/pop-all-registers
 471   fb/enable-interrupts
 472   cf/return-from-interrupt
 473 
 474 == data
 475 Keyboard-shift-pressed?:  # boolean
 476   0/imm32
 477 
 478 Keyboard-ctrl-pressed?:  # boolean
 479   0/imm32
 480 
 481 # var keyboard circular buffer
 482 Keyboard-buffer:write:  # nibble
 483   0/imm32
 484 Keyboard-buffer:read:  # nibble
 485   0/imm32
 486 Keyboard-buffer:data:  # byte[16]
 487   00 00 00 00
 488   00 00 00 00
 489   00 00 00 00
 490   00 00 00 00
 491 
 492 +-- 95 lines: # Keyboard maps for translating keys to ASCII -----------------------------------------------------------------------------------------------------------------------------
 587 
 588 Video-mode-info:
 589 +-- 53 lines: # video mode info ---------------------------------------------------------------------------------------------------------------------------------------------------------
 642 
 643 Font:
 644 +--236 lines: # Bitmaps for some ASCII characters (soon Unicode) ------------------------------------------------------------------------------------------------------------------------
 880 
 881 ## Controlling IDE (ATA) hard disks
 882 # Uses 28-bit PIO mode.
 883 # Inspired by https://colorforth.github.io/ide.html
 884 #
 885 # Resources:
 886 #   https://wiki.osdev.org/ATA_PIO_Mode
 887 #   https://forum.osdev.org/viewtopic.php?f=1&p=167798
 888 #   read-sector, according to https://www.scs.stanford.edu/11wi-cs140/pintos/specs/ata-3-std.pdf
 889 
 890 == data
 891 
 892 # code disk
 893 # All ports are 8-bit except data-port, which is 16-bit.
 894 Primary-bus-primary-drive:
 895   # command-port: int (write)
 896   0x1f7/imm32
 897   # status-port: int (read)
 898   0x1f7/imm32
 899   # alternative-status-port: int (read)
 900   0x3f6/imm32
 901   # error-port: int (read)
 902   0x1f1/imm32
 903   # drive-and-head-port: int
 904   0x1f6/imm32
 905   # sector-count-port: int
 906   0x1f2/imm32
 907   # lba-low-port: int
 908   0x1f3/imm32
 909   # lba-mid-port: int
 910   0x1f4/imm32
 911   # lba-high-port: int
 912   0x1f5/imm32
 913   # data-port: int
 914   0x1f0/imm32
 915   # drive-code: byte                # only drive-specific field
 916   0xe0/imm32  # LBA mode also enabled
 917 
 918 # data disk
 919 # All ports are 8-bit except data-port, which is 16-bit.
 920 Primary-bus-secondary-drive:
 921   # command-port: int (write)
 922   0x1f7/imm32
 923   # status-port: int (read)
 924   0x1f7/imm32
 925   # alternative-status-port: int (read)
 926   0x3f6/imm32
 927   # error-port: int (read)
 928   0x1f1/imm32
 929   # drive-and-head-port: int
 930   0x1f6/imm32
 931   # sector-count-port: int
 932   0x1f2/imm32
 933   # lba-low-port: int
 934   0x1f3/imm32
 935   # lba-mid-port: int
 936   0x1f4/imm32
 937   # lba-high-port: int
 938   0x1f5/imm32
 939   # data-port: int
 940   0x1f0/imm32
 941   # drive-code: byte                # only drive-specific field
 942   0xf0/imm32  # LBA mode also enabled
 943 
 944 == code
 945 
 946 load-sectors:  # disk: (addr disk), lba: int, n: int, out: (addr stream byte)
 947   # . prologue
 948   55/push-ebp
 949   89/<- %ebp 4/r32/esp
 950   # . save registers
 951   50/push-eax
 952   51/push-ecx
 953   52/push-edx
 954   # check for drive
 955   (drive-exists? *(ebp+8))  # => eax
 956   3d/compare-eax-and 0/imm32/false
 957   0f 84/jump-if-= $load-sectors:end/disp32
 958   # kick off read
 959   (ata-drive-select *(ebp+8) *(ebp+0xc))
 960   (clear-ata-error *(ebp+8))
 961   (ata-sector-count *(ebp+8) *(ebp+0x10))
 962   (ata-lba *(ebp+8) *(ebp+0xc))
 963   (ata-command *(ebp+8) 0x20)  # read sectors with retries
 964   # for each sector
 965   {
 966     # poll for results
 967 #?     (draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0 "waiting for sector.." 7 0)
 968 #?     (draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0 "." 7 0)
 969     (while-ata-busy *(ebp+8))
 970     (until-ata-data-available *(ebp+8))
 971 #?     (draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0 "reading\n" 7 0)
 972     # var data-port/edx = disk->data-port
 973     8b/-> *(ebp+8) 0/r32/eax
 974     8b/-> *(eax+0x24) 2/r32/edx
 975     # emit results
 976     31/xor %eax 0/r32/eax
 977     b9/copy-to-ecx 0x200/imm32  # 512 bytes per sector
 978     {
 979       81 7/subop/compare %ecx 0/imm32
 980       74/jump-if-= break/disp8
 981       66 ed/read-port-dx-into-ax
 982       # write 2 bytes to stream one at a time
 983       (append-byte *(ebp+0x14) %eax)
 984       49/decrement-ecx
 985       c1/shift 5/subop/right-padding-zeroes %eax 8/imm8
 986       (append-byte *(ebp+0x14) %eax)
 987       49/decrement-ecx
 988       eb/jump loop/disp8
 989     }
 990     # next sector
 991     ff 1/subop/decrement *(ebp+0x10)
 992 #?     (draw-int32-decimal-wrapping-right-then-down-from-cursor-over-full-screen 0 *(ebp+0x10) 0xc 0)
 993     81 7/subop/compare *(ebp+0x10) 0/imm32
 994     7e/jump-if-<= break/disp8
 995     (wait-400ns *(ebp+8))
 996 #?     (draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0 "next sector\n" 7 0)
 997     e9/jump loop/disp32
 998   }
 999 $load-sectors:end:
1000   # . restore registers
1001   5a/pop-to-edx
1002   59/pop-to-ecx
1003   58/pop-to-eax
1004   # . epilogue
1005   89/<- %esp 5/r32/ebp
1006   5d/pop-to-ebp
1007   c3/return
1008 
1009 store-sectors:  # disk: (addr disk), lba: int, n: int, in: (addr stream byte)
1010   # . prologue
1011   55/push-ebp
1012   89/<- %ebp 4/r32/esp
1013   # . save registers
1014   50/push-eax
1015   51/push-ecx
1016   52/push-edx
1017   53/push-ebx
1018   # check for drive
1019   (drive-exists? *(ebp+8))  # => eax
1020   3d/compare-eax-and 0/imm32/false
1021   0f 84/jump-if-= $store-sectors:end/disp32
1022   # kick off write
1023   (ata-drive-select *(ebp+8) *(ebp+0xc))
1024   (clear-ata-error *(ebp+8))
1025   (ata-sector-count *(ebp+8) *(ebp+0x10))
1026   (ata-lba *(ebp+8) *(ebp+0xc))
1027   (ata-command *(ebp+8) 0x30)  # write sectors with retries
1028   # for each sector
1029 #?   (set-cursor-position 0 0 0)
1030 #?   (draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0 "0" 7 0)
1031   {
1032     # wait
1033     (while-ata-busy *(ebp+8))
1034     (until-ata-ready-for-data *(ebp+8))
1035     # var data-port/edx = disk->data-port
1036     8b/-> *(ebp+8) 0/r32/eax
1037     8b/-> *(eax+0x24) 2/r32/edx
1038     # send data
1039     b9/copy-to-ecx 0x200/imm32  # 512 bytes per sector
1040     # . var first-byte/ebx: byte
1041     # . when it's more than 0xff, we're at an even-numbered byte
1042     bb/copy-to-ebx 0xffff/imm32
1043 #?     (draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0 "D" 7 0)
1044 $store-sectors:store-sector:
1045     {
1046       81 7/subop/compare %ecx 0/imm32
1047       74/jump-if-= break/disp8
1048       # this loop is slow, but the ATA spec also requires a small delay
1049       (stream-empty? *(ebp+0x14))  # => eax
1050       3d/compare-eax-and 0/imm32/false
1051       75/jump-if-!= break/disp8
1052       # read byte from stream
1053       (read-byte *(ebp+0x14))  # => eax
1054       # if we're at an odd-numbered byte, save it to first-byte
1055       81 7/subop/compare %ebx 0xff/imm32
1056       {
1057         7e/jump-if-<= break/disp8
1058         89/<- %ebx 0/r32/eax
1059         eb/jump $store-sectors:store-sector/disp8
1060       }
1061       # otherwise OR it with first-byte and write it out
1062       c1/shift 4/subop/left %eax 8/imm8
1063       09/or %eax 3/r32/ebx
1064       66 ef/write-ax-into-port-dx
1065       49/decrement-ecx
1066       49/decrement-ecx
1067       # reset first-byte
1068       bb/copy-to-ebx 0xffff/imm32
1069       eb/jump loop/disp8
1070     }
1071     # write out final first-byte if necessary
1072 #?     (draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0 "I" 7 0)
1073     81 7/subop/compare %ebx 0xff/imm32
1074     {
1075       7f/jump-if-> break/disp8
1076       89/<- %eax 3/r32/ebx
1077       66 ef/write-ax-into-port-dx
1078       49/decrement-ecx
1079       49/decrement-ecx
1080     }
1081     # pad zeroes
1082 #?     (draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0 "P" 7 0)
1083     31/xor %eax 0/r32/eax
1084     {
1085       81 7/subop/compare %ecx 0/imm32
1086       74/jump-if-= break/disp8
1087       66 ef/write-ax-into-port-dx
1088       49/decrement-ecx
1089       49/decrement-ecx
1090       eb/jump loop/disp8
1091     }
1092     # next sector
1093 #?     (draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0 "N" 7 0)
1094     ff 1/subop/decrement *(ebp+0x10)
1095     81 7/subop/compare *(ebp+0x10) 0/imm32
1096     7e/jump-if-<= break/disp8
1097 #?     (draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0 "W" 7 0)
1098     (wait-400ns *(ebp+8))
1099 #?     (draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0 "L" 7 0)
1100     e9/jump loop/disp32
1101   }
1102 #?   (draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0 "F" 7 0)
1103   (flush-ata-cache *(ebp+8))
1104 #?   (draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0 "Y" 7 0)
1105 $store-sectors:end:
1106   # . restore registers
1107   5b/pop-to-ebx
1108   5a/pop-to-edx
1109   59/pop-to-ecx
1110   58/pop-to-eax
1111   # . epilogue
1112   89/<- %esp 5/r32/ebp
1113   5d/pop-to-ebp
1114   c3/return
1115 
1116 +--289 lines: # disk helpers ------------------------------------------------------------------------------------------------------------------------------------------------------------
1405 
1406 ## Controlling a PS/2 mouse
1407 # Uses no IRQs, just polling.
1408 # Thanks Dave Long: https://github.com/jtauber/cleese/blob/master/necco/kernel/bochs/py8042.py
1409 #
1410 # Resources:
1411 #   https://wiki.osdev.org/Mouse_Input
1412 
1413 # results x/eax, y/ecx range from -256 to +255
1414 # See https://wiki.osdev.org/index.php?title=Mouse_Input&oldid=25663#Format_of_First_3_Packet_Bytes
1415 read-mouse-event:  # -> _/eax: int, _/ecx: int
1416   # . prologue
1417   55/push-ebp
1418   89/<- %ebp 4/r32/esp
1419   # . save registers
1420   52/push-edx
1421   53/push-ebx
1422   # if no event, return 0, 0
1423   b8/copy-to-eax 0/imm32
1424   b9/copy-to-ecx 0/imm32
1425   (any-mouse-event?)  # => eax
1426   3d/compare-eax-and 0/imm32/false
1427   74/jump-if-= $read-mouse-event:end/disp8
1428   # var f1/edx: byte = inb(0x60)
1429   31/xor %eax 0/r32/eax
1430   e4/read-port-into-al 0x60/imm8
1431   89/<- %edx 0/r32/eax
1432   (wait-for-mouse-event)
1433   # var dx/ebx: byte = inb(0x60)
1434   31/xor %eax 0/r32/eax
1435   e4/read-port-into-al 0x60/imm8
1436   89/<- %ebx 0/r32/eax
1437   (wait-for-mouse-event)
1438   # var dy/ecx: byte = inb(0x60)
1439   31/xor %eax 0/r32/eax
1440   e4/read-port-into-al 0x60/imm8
1441   89/<- %ecx 0/r32/eax
1442   # eax = dx
1443   89/<- %eax 3/r32/ebx
1444   # if (f1 & 0x10) dx = -dx
1445   {
1446     f6 0/subop/test-bits %dl 0x10/imm8
1447     74/jump-if-zero break/disp8
1448     0d/or-eax-with 0xffffff00/imm32
1449   }
1450   # if (f1 & 0x20) dy = -dy
1451   {
1452     f6 0/subop/test-bits %dl 0x20/imm8
1453     74/jump-if-zero break/disp8
1454     81 1/subop/or %ecx 0xffffff00/imm32
1455   }
1456 $read-mouse-event:end:
1457   # . restore registers
1458   5b/pop-to-ebx
1459   5a/pop-to-edx
1460   # . epilogue
1461   89/<- %esp 5/r32/ebp
1462   5d/pop-to-ebp
1463   c3/return
1464 
1465 +--147 lines: # mouse helpers -----------------------------------------------------------------------------------------------------------------------------------------------------------
1612 
1613 # vim:ft=subx