# Mu - boot.subx

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   # load two more tracks of disk into addresses [0x46c00, 0x56800)
     126   b4/copy-to-ah 2/imm8/read-drive
     127   # dl comes conveniently initialized at boot time with the index of the device being booted
     128   b5/copy-to-ch 0/imm8/cylinder
     129   b6/copy-to-dh 8/imm8/head                   # <====
     130   b1/copy-to-cl 1/imm8/sector  # 1-based
     131   b0/copy-to-al 0x7e/imm8/num-sectors  # 2*63 = 126
     132   # address to write sectors to = es:bx = 0x46c00, contiguous with boot segment
     133   bb/copy-to-bx 0x46c0/imm16                  # <====
     134   8e/->seg 3/mod/direct 3/rm32/bx 0/r32/es
     135   bb/copy-to-bx 0/imm16
     136   cd/syscall 0x13/imm8/bios-disk-services
     137   0f 82/jump-if-carry disk_error/disp16
     138
     139   # reset es
     140   bb/copy-to-bx 0/imm16
     141   8e/->seg 3/mod/direct 3/rm32/bx 0/r32/es
     142
     143   # adjust video mode
     144   b4/copy-to-ah 0x4f/imm8  # VBE commands
     145   b0/copy-to-al 2/imm8  # set video mode
     146   bb/copy-to-bx 0x4105/imm16  # 0x0105 | 0x4000
     147                               # 0x0105 = graphics mode 1024x768x256
     148                               #  (alternative candidate: 0x0101 for 640x480x256)
     149                               # 0x4000 bit = configure linear frame buffer in Bochs emulator; hopefully this doesn't hurt anything when running natively
     150   cd/syscall 0x10/imm8/bios-video-services
     151
     152   # load information for the (hopefully) current video mode
     153   # mostly just for the address to the linear frame buffer
     154   b4/copy-to-ah 0x4f/imm8  # VBE commands
     155   b0/copy-to-al 1/imm8  # get video mode info
     156   b9/copy-to-cx 0x0105/imm16  # mode we requested
     157   bf/copy-to-di Video-mode-info/imm16
     158   cd/syscall 0x10/imm8/bios-video-services
     159
     160   ## switch to 32-bit mode
     161   # load global descriptor table
     162   # We can't refer to the label directly because SubX doesn't do the right
     163   # thing for lgdt, so rather than make errors worse in most places we instead
     164   # pin gdt_descriptor below.
     165   0f 01 2/subop/lgdt 0/mod/indirect 6/rm32/use-disp16 0x7ce0/disp16/gdt_descriptor
     166   # enable paging
     167   0f 20/<-cr 3/mod/direct 0/rm32/eax 0/r32/cr0
     168   66 83 1/subop/or 3/mod/direct 0/rm32/eax 1/imm8  # eax <- or 0x1
     169   0f 22/->cr 3/mod/direct 0/rm32/eax 0/r32/cr0
     170   # far jump to initialize_32bit_mode that sets cs to offset 8 in the gdt in the process
     171   # We can't refer to the label directly because SubX doesn't have syntax for
     172   # segment selectors. So we instead pin initialize_32bit_mode below.
     173   ea/jump-far-absolute 0x00087d00/disp32  # address 0x7d00 in offset 8 of the gdt
     174
     175 disk_error:
     176   # print 'D' to top-left of screen to indicate disk error
     177   # *0xb8000 <- 0x0f44
     178   bb/copy-to-bx 0xb800/imm16
     179   8e/->seg 3/mod/direct 3/rm32/bx 3/r32/ds
     180   b0/copy-to-al 0x44/imm8/D
     181   b4/copy-to-ah 0x0f/imm8/white-on-black
     182   bb/copy-to-bx 0/imm16
     183   89/<- 0/mod/indirect 7/rm32/bx 0/r32/ax  # *ds:bx <- ax
     184   # loop forever
     185   {
     186     eb/jump loop/disp8
     187   }
     188
     189 ## GDT: 3 records of 8 bytes each
     190 == data 0x7ce0
     191 gdt_descriptor:
     192   0x17/imm16  # final index of gdt = size of gdt - 1
     193   gdt_start/imm32/start
     194
     195 gdt_start:
     196 # offset 0: gdt_null:  mandatory null descriptor
     197   00 00 00 00 00 00 00 00
     198 # offset 8: gdt_code
     199   ff ff  # limit[0:16]
     200   00 00 00  # base[0:24]
     201   9a  # 1/present 00/privilege 1/descriptor type = 1001b
     202       # 1/code 0/conforming 1/readable 0/accessed = 1010b
     203   cf  # 1/granularity 1/32-bit 0/64-bit-segment 0/AVL = 1100b
     204       # limit[16:20] = 1111b
     205   00  # base[24:32]
     206 # offset 16: gdt_data
     207   ff ff  # limit[0:16]
     208   00 00 00  # base[0:24]
     209   92  # 1/present 00/privilege 1/descriptor type = 1001b
     210       # 0/data 0/conforming 1/readable 0/accessed = 0010b
     211   cf  # same as gdt_code
     212   00  # base[24:32]
     213 # gdt_end:
     214
     215 ## 32-bit code from this point
     216
     217 == code 0x7d00
     218 initialize_32bit_mode:
     219   66 b8/copy-to-ax 0x10/imm16  # offset 16 from gdt_start
     220   8e/->seg 3/mod/direct 0/rm32/ax 3/r32/ds
     221   8e/->seg 3/mod/direct 0/rm32/ax 2/r32/ss
     222   8e/->seg 3/mod/direct 0/rm32/ax 0/r32/es
     223   8e/->seg 3/mod/direct 0/rm32/ax 4/r32/fs
     224   8e/->seg 3/mod/direct 0/rm32/ax 5/r32/gs
     225
     226   bc/copy-to-esp 0x02000000/imm32
     227
     228   ## load interrupt handlers
     229   # We can't refer to the label directly because SubX doesn't do the right
     230   # thing for lidt, so rather than make errors worse in most places we instead
     231   # pin idt_descriptor below.
     232   0f 01 3/subop/lidt 0/mod/indirect 5/rm32/use-disp32 0x7e00/disp32/idt_descriptor
     233
     234   # For now, not bothering reprogramming the IRQ to not conflict with software
     235   # exceptions.
     236   #   https://wiki.osdev.org/index.php?title=8259_PIC&oldid=24650#Protected_Mode
     237   #
     238   # Interrupt 1 (keyboard) conflicts with debugger faults. We don't use a
     239   # debugger.
     240   # Reference:
     241   #   https://wiki.osdev.org/Exceptions
     242
     243   # enable timer IRQ0 and keyboard IRQ1
     244   b0/copy-to-al 0xfc/imm8  # disable mask for IRQ0 and IRQ1
     245   e6/write-al-into-port 0x21/imm8
     246
     247   fb/enable-interrupts
     248
     249   (initialize-mouse)
     250
     251   ## enable floating point
     252   db/floating-point-coprocessor e3/initialize
     253   # eax <- cr4
     254   0f 20/<-cr 3/mod/direct 0/rm32/eax 4/r32/cr4
     255   # eax <- or bit 9
     256   0f ba/bit-test 5/subop/bit-test-and-set 3/mod/direct 0/rm32/eax 9/imm8
     257   # cr4 <- eax
     258   0f 22/->cr 3/mod/direct 0/rm32/eax 4/r32/cr4
     259
     260   e9/jump Entry/disp32
     261
     262 == boot-sector-marker 0x7dfe
     263 # final 2 bytes of boot sector
     264 55 aa
     265
     266 ## sector 2 onwards loaded by load_disk, not automatically on boot
     267
     268 == data 0x7e00
     269 idt_descriptor:
     270   ff 03  # final index of idt = size of idt - 1
     271   idt_start/imm32/start
     272
     273 +-- 55 lines: # interrupt descriptor table ----------------------------------------------------------------------------------------------------------------------------------------------
     328
     329 == code
     330
     331 null-interrupt-handler:
     332   # prologue
     333   # Don't disable interrupts; the timer has the highest priority anyway,
     334   # and this interrupt triggers extremely frequently.
     335   fa/disable-interrupts
     336   60/push-all-registers
     337   9c/push-flags
     338   # acknowledge interrupt
     339   b0/copy-to-al 0x20/imm8
     340   e6/write-al-into-port 0x20/imm8
     341   31/xor %eax 0/r32/eax
     342 $null-interrupt-handler:epilogue:
     343   # epilogue
     344   9d/pop-flags
     345   61/pop-all-registers
     346   fb/enable-interrupts
     347   cf/return-from-interrupt
     348
     349 timer-interrupt-handler:
     350   # prologue
     351   # Don't disable interrupts; the timer has the highest priority anyway,
     352   # and this interrupt triggers extremely frequently.
     353   fa/disable-interrupts
     354   60/push-all-registers
     355   9c/push-flags
     356   # acknowledge interrupt
     357   b0/copy-to-al 0x20/imm8
     358   e6/write-al-into-port 0x20/imm8
     359   31/xor %eax 0/r32/eax
     360 $timer-interrupt-handler:epilogue:
     361   # epilogue
     362   9d/pop-flags
     363   61/pop-all-registers
     364   fb/enable-interrupts
     365   cf/return-from-interrupt
     366
     367 keyboard-interrupt-handler:
     368   # prologue
     369   fa/disable-interrupts
     370   60/push-all-registers
     371   9c/push-flags
     372   # acknowledge interrupt
     373   b0/copy-to-al 0x20/imm8
     374   e6/write-al-into-port 0x20/imm8
     375   31/xor %eax 0/r32/eax
     376   # check output buffer of 8042 keyboard controller (https://web.archive.org/web/20040604041507/http://panda.cs.ndsu.nodak.edu/~achapwes/PICmicro/keyboard/atkeyboard.html)
     377   e4/read-port-into-al 0x64/imm8
     378   a8/test-bits-in-al 0x01/imm8  # set zf if bit 0 (least significant) is not set
     379   0f 84/jump-if-not-set $keyboard-interrupt-handler:epilogue/disp32
     380   # - if keyboard buffer is full, return
     381   # var dest-addr/ecx: (addr byte) = (keyboard-buffer + *keyboard-buffer:write)
     382   31/xor %ecx 1/r32/ecx
     383   8a/byte-> *Keyboard-buffer:write 1/r32/cl
     384   81 0/subop/add %ecx Keyboard-buffer:data/imm32
     385   # al = *dest-addr
     386   8a/byte-> *ecx 0/r32/al
     387   # if (al != 0) return
     388   3c/compare-al-and 0/imm8
     389   0f 85/jump-if-!= $keyboard-interrupt-handler:epilogue/disp32
     390   # - read keycode
     391   e4/read-port-into-al 0x60/imm8
     392   # - key released
     393   # if (al == 0xaa) shift = false  # left shift is being lifted
     394   {
     395     3c/compare-al-and 0xaa/imm8
     396     75/jump-if-!= break/disp8
     397     # *shift = 0
     398     c7 0/subop/copy *Keyboard-shift-pressed? 0/imm32
     399   }
     400   # if (al == 0xb6) shift = false  # right shift is being lifted
     401   {
     402     3c/compare-al-and 0xb6/imm8
     403     75/jump-if-!= break/disp8
     404     # *shift = 0
     405     c7 0/subop/copy *Keyboard-shift-pressed? 0/imm32
     406   }
     407   # if (al == 0x9d) ctrl = false  # ctrl is being lifted
     408   {
     409     3c/compare-al-and 0x9d/imm8
     410     75/jump-if-!= break/disp8
     411     # *ctrl = 0
     412     c7 0/subop/copy *Keyboard-ctrl-pressed? 0/imm32
     413   }
     414   # if (al & 0x80) a key is being lifted; return
     415   50/push-eax
     416   24/and-al-with 0x80/imm8
     417   3c/compare-al-and 0/imm8
     418   58/pop-to-eax
     419   75/jump-if-!= $keyboard-interrupt-handler:epilogue/disp8
     420   # - key pressed
     421   # if (al == 0x2a) shift = true, return  # left shift pressed
     422   {
     423     3c/compare-al-and 0x2a/imm8
     424     75/jump-if-!= break/disp8
     425     # *shift = 1
     426     c7 0/subop/copy *Keyboard-shift-pressed? 1/imm32
     427     # return
     428     eb/jump $keyboard-interrupt-handler:epilogue/disp8
     429   }
     430   # if (al == 0x36) shift = true, return  # right shift pressed
     431   {
     432     3c/compare-al-and 0x36/imm8
     433     75/jump-if-!= break/disp8
     434     # *shift = 1
     435     c7 0/subop/copy *Keyboard-shift-pressed? 1/imm32
     436     # return
     437     eb/jump $keyboard-interrupt-handler:epilogue/disp8
     438   }
     439   # if (al == 0x1d) ctrl = true, return
     440   {
     441     3c/compare-al-and 0x1d/imm8
     442     75/jump-if-!= break/disp8
     443     # *ctrl = 1
     444     c7 0/subop/copy *Keyboard-ctrl-pressed? 1/imm32
     445     # return
     446     eb/jump $keyboard-interrupt-handler:epilogue/disp8
     447   }
     448   # - convert key to character
     449   # if (shift) use keyboard shift map
     450   {
     451     81 7/subop/compare *Keyboard-shift-pressed? 0/imm32
     452     74/jump-if-= break/disp8
     453     # sigils don't currently support labels inside *(eax+label)
     454     05/add-to-eax Keyboard-shift-map/imm32
     455     8a/byte-> *eax 0/r32/al
     456     eb/jump $keyboard-interrupt-handler:select-map-done/disp8
     457   }
     458   # if (ctrl) al = *(ctrl map + al)
     459   {
     460     81 7/subop/compare *Keyboard-ctrl-pressed? 0/imm32
     461     74/jump-if-= break/disp8
     462     05/add-to-eax Keyboard-ctrl-map/imm32
     463     8a/byte-> *eax 0/r32/al
     464     eb/jump $keyboard-interrupt-handler:select-map-done/disp8
     465   }
     466   # otherwise al = *(normal map + al)
     467   05/add-to-eax Keyboard-normal-map/imm32
     468   8a/byte-> *eax 0/r32/al
     469 $keyboard-interrupt-handler:select-map-done:
     470   # - if there's no character mapping, return
     471   {
     472     3c/compare-al-and 0/imm8
     473     74/jump-if-= break/disp8
     474     # - store al in keyboard buffer
     475     88/<- *ecx 0/r32/al
     476     # increment index
     477     fe/increment-byte *Keyboard-buffer:write
     478     # clear top nibble of index (keyboard buffer is circular)
     479     80 4/subop/and-byte *Keyboard-buffer:write 0x0f/imm8
     480   }
     481 $keyboard-interrupt-handler:epilogue:
     482   # epilogue
     483   9d/pop-flags
     484   61/pop-all-registers
     485   fb/enable-interrupts
     486   cf/return-from-interrupt
     487
     488 == data
     489 Keyboard-shift-pressed?:  # boolean
     490   0/imm32
     491
     492 Keyboard-ctrl-pressed?:  # boolean
     493   0/imm32
     494
     495 # var keyboard circular buffer
     496 Keyboard-buffer:write:  # nibble
     497   0/imm32
     498 Keyboard-buffer:read:  # nibble
     499   0/imm32
     500 Keyboard-buffer:data:  # byte[16]
     501   00 00 00 00
     502   00 00 00 00
     503   00 00 00 00
     504   00 00 00 00
     505
     506 +-- 95 lines: # Keyboard maps for translating keys to ASCII -----------------------------------------------------------------------------------------------------------------------------
     601
     602 Video-mode-info:
     603 +-- 53 lines: # video mode info ---------------------------------------------------------------------------------------------------------------------------------------------------------
     656
     657 Font:
     658 +--236 lines: # Bitmaps for some ASCII characters (soon Unicode) ------------------------------------------------------------------------------------------------------------------------
     894
     895 ## Controlling IDE (ATA) hard disks
     896 # Uses 28-bit PIO mode.
     897 # Inspired by https://colorforth.github.io/ide.html
     898 #
     899 # Resources:
     900 #   https://wiki.osdev.org/ATA_PIO_Mode
     901 #   https://forum.osdev.org/viewtopic.php?f=1&p=167798
     902 #   read-sector, according to https://www.scs.stanford.edu/11wi-cs140/pintos/specs/ata-3-std.pdf
     903
     904 == data
     905
     906 # code disk
     907 # All ports are 8-bit except data-port, which is 16-bit.
     908 Primary-bus-primary-drive:
     909   # command-port: int (write)
     910   0x1f7/imm32
     911   # status-port: int (read)
     912   0x1f7/imm32
     913   # alternative-status-port: int (read)
     914   0x3f6/imm32
     915   # error-port: int (read)
     916   0x1f1/imm32
     917   # drive-and-head-port: int
     918   0x1f6/imm32
     919   # sector-count-port: int
     920   0x1f2/imm32
     921   # lba-low-port: int
     922   0x1f3/imm32
     923   # lba-mid-port: int
     924   0x1f4/imm32
     925   # lba-high-port: int
     926   0x1f5/imm32
     927   # data-port: int
     928   0x1f0/imm32
     929   # drive-code: byte                # only drive-specific field
     930   0xe0/imm32  # LBA mode also enabled
     931
     932 # data disk
     933 # All ports are 8-bit except data-port, which is 16-bit.
     934 Primary-bus-secondary-drive:
     935   # command-port: int (write)
     936   0x1f7/imm32
     937   # status-port: int (read)
     938   0x1f7/imm32
     939   # alternative-status-port: int (read)
     940   0x3f6/imm32
     941   # error-port: int (read)
     942   0x1f1/imm32
     943   # drive-and-head-port: int
     944   0x1f6/imm32
     945   # sector-count-port: int
     946   0x1f2/imm32
     947   # lba-low-port: int
     948   0x1f3/imm32
     949   # lba-mid-port: int
     950   0x1f4/imm32
     951   # lba-high-port: int
     952   0x1f5/imm32
     953   # data-port: int
     954   0x1f0/imm32
     955   # drive-code: byte                # only drive-specific field
     956   0xf0/imm32  # LBA mode also enabled
     957
     958 == code
     959
     960 load-sectors:  # disk: (addr disk), lba: int, n: int, out: (addr stream byte)
     961   # . prologue
     962   55/push-ebp
     963   89/<- %ebp 4/r32/esp
     964   # . save registers
     965   50/push-eax
     966   51/push-ecx
     967   52/push-edx
     968   # check for drive
     969   (drive-exists? *(ebp+8))  # => eax
     970   3d/compare-eax-and 0/imm32/false
     971   0f 84/jump-if-= $load-sectors:end/disp32
     972   # kick off read
     973   (ata-drive-select *(ebp+8) *(ebp+0xc))
     974   (clear-ata-error *(ebp+8))
     975   (ata-sector-count *(ebp+8) *(ebp+0x10))
     976   (ata-lba *(ebp+8) *(ebp+0xc))
     977   (ata-command *(ebp+8) 0x20)  # read sectors with retries
     978   # for each sector
     979   {
     980     # poll for results
     981 #?     (draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0 "waiting for sector.." 7 0)
     982 #?     (draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0 "." 7 0)
     983     (while-ata-busy *(ebp+8))
     984     (until-ata-data-available *(ebp+8))
     985 #?     (draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0 "reading\n" 7 0)
     986     # var data-port/edx = disk->data-port
     987     8b/-> *(ebp+8) 0/r32/eax
     988     8b/-> *(eax+0x24) 2/r32/edx
     989     # emit results
     990     31/xor %eax 0/r32/eax
     991     b9/copy-to-ecx 0x200/imm32  # 512 bytes per sector
     992     {
     993       81 7/subop/compare %ecx 0/imm32
     994       74/jump-if-= break/disp8
     995       66 ed/read-port-dx-into-ax
     996       # write 2 bytes to stream one at a time
     997       (append-byte *(ebp+0x14) %eax)
     998       49/decrement-ecx
     999       c1/shift 5/subop/right-padding-zeroes %eax 8/imm8
    1000       (append-byte *(ebp+0x14) %eax)
    1001       49/decrement-ecx
    1002       eb/jump loop/disp8
    1003     }
    1004     # next sector
    1005     ff 1/subop/decrement *(ebp+0x10)
    1006 #?     (draw-int32-decimal-wrapping-right-then-down-from-cursor-over-full-screen 0 *(ebp+0x10) 0xc 0)
    1007     81 7/subop/compare *(ebp+0x10) 0/imm32
    1008     7e/jump-if-<= break/disp8
    1009     (wait-400ns *(ebp+8))
    1010 #?     (draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0 "next sector\n" 7 0)
    1011     e9/jump loop/disp32
    1012   }
    1013 $load-sectors:end:
    1014   # . restore registers
    1015   5a/pop-to-edx
    1016   59/pop-to-ecx
    1017   58/pop-to-eax
    1018   # . epilogue
    1019   89/<- %esp 5/r32/ebp
    1020   5d/pop-to-ebp
    1021   c3/return
    1022
    1023 store-sectors:  # disk: (addr disk), lba: int, n: int, in: (addr stream byte)
    1024   # . prologue
    1025   55/push-ebp
    1026   89/<- %ebp 4/r32/esp
    1027   # . save registers
    1028   50/push-eax
    1029   51/push-ecx
    1030   52/push-edx
    1031   53/push-ebx
    1032   # check for drive
    1033   (drive-exists? *(ebp+8))  # => eax
    1034   3d/compare-eax-and 0/imm32/false
    1035   0f 84/jump-if-= $store-sectors:end/disp32
    1036   # kick off write
    1037   (ata-drive-select *(ebp+8) *(ebp+0xc))
    1038   (clear-ata-error *(ebp+8))
    1039   (ata-sector-count *(ebp+8) *(ebp+0x10))
    1040   (ata-lba *(ebp+8) *(ebp+0xc))
    1041   (ata-command *(ebp+8) 0x30)  # write sectors with retries
    1042   # for each sector
    1043 #?   (set-cursor-position 0 0 0)
    1044 #?   (draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0 "0" 7 0)
    1045   {
    1046     # wait
    1047     (while-ata-busy *(ebp+8))
    1048     (until-ata-ready-for-data *(ebp+8))
    1049     # var data-port/edx = disk->data-port
    1050     8b/-> *(ebp+8) 0/r32/eax
    1051     8b/-> *(eax+0x24) 2/r32/edx
    1052     # send data
    1053     b9/copy-to-ecx 0x200/imm32  # 512 bytes per sector
    1054     # . var first-byte/ebx: byte
    1055     # . when it's more than 0xff, we're at an even-numbered byte
    1056     bb/copy-to-ebx 0xffff/imm32
    1057 #?     (draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0 "D" 7 0)
    1058 $store-sectors:store-sector:
    1059     {
    1060       81 7/subop/compare %ecx 0/imm32
    1061       74/jump-if-= break/disp8
    1062       # this loop is slow, but the ATA spec also requires a small delay
    1063       (stream-empty? *(ebp+0x14))  # => eax
    1064       3d/compare-eax-and 0/imm32/false
    1065       75/jump-if-!= break/disp8
    1066       # read byte from stream
    1067       (read-byte *(ebp+0x14))  # => eax
    1068       # if we're at an odd-numbered byte, save it to first-byte
    1069       81 7/subop/compare %ebx 0xff/imm32
    1070       {
    1071         7e/jump-if-<= break/disp8
    1072         89/<- %ebx 0/r32/eax
    1073         eb/jump $store-sectors:store-sector/disp8
    1074       }
    1075       # otherwise OR it with first-byte and write it out
    1076       c1/shift 4/subop/left %eax 8/imm8
    1077       09/or %eax 3/r32/ebx
    1078       66 ef/write-ax-into-port-dx
    1079       49/decrement-ecx
    1080       49/decrement-ecx
    1081       # reset first-byte
    1082       bb/copy-to-ebx 0xffff/imm32
    1083       eb/jump loop/disp8
    1084     }
    1085     # write out final first-byte if necessary
    1086 #?     (draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0 "I" 7 0)
    1087     81 7/subop/compare %ebx 0xff/imm32
    1088     {
    1089       7f/jump-if-> break/disp8
    1090       89/<- %eax 3/r32/ebx
    1091       66 ef/write-ax-into-port-dx
    1092       49/decrement-ecx
    1093       49/decrement-ecx
    1094     }
    1095     # pad zeroes
    1096 #?     (draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0 "P" 7 0)
    1097     31/xor %eax 0/r32/eax
    1098     {
    1099       81 7/subop/compare %ecx 0/imm32
    1100       74/jump-if-= break/disp8
    1101       66 ef/write-ax-into-port-dx
    1102       49/decrement-ecx
    1103       49/decrement-ecx
    1104       eb/jump loop/disp8
    1105     }
    1106     # next sector
    1107 #?     (draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0 "N" 7 0)
    1108     ff 1/subop/decrement *(ebp+0x10)
    1109     81 7/subop/compare *(ebp+0x10) 0/imm32
    1110     7e/jump-if-<= break/disp8
    1111 #?     (draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0 "W" 7 0)
    1112     (wait-400ns *(ebp+8))
    1113 #?     (draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0 "L" 7 0)
    1114     e9/jump loop/disp32
    1115   }
    1116 #?   (draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0 "F" 7 0)
    1117   (flush-ata-cache *(ebp+8))
    1118 #?   (draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0 "Y" 7 0)
    1119 $store-sectors:end:
    1120   # . restore registers
    1121   5b/pop-to-ebx
    1122   5a/pop-to-edx
    1123   59/pop-to-ecx
    1124   58/pop-to-eax
    1125   # . epilogue
    1126   89/<- %esp 5/r32/ebp
    1127   5d/pop-to-ebp
    1128   c3/return
    1129
    1130 +--289 lines: # disk helpers ------------------------------------------------------------------------------------------------------------------------------------------------------------
    1419
    1420 ## Controlling a PS/2 mouse
    1421 # Uses no IRQs, just polling.
    1422 # Thanks Dave Long: https://github.com/jtauber/cleese/blob/master/necco/kernel/bochs/py8042.py
    1423 #
    1424 # Resources:
    1425 #   https://wiki.osdev.org/Mouse_Input
    1426
    1427 # results x/eax, y/ecx range from -256 to +255
    1428 # See https://wiki.osdev.org/index.php?title=Mouse_Input&oldid=25663#Format_of_First_3_Packet_Bytes
    1429 read-mouse-event:  # -> _/eax: int, _/ecx: int
    1430   # . prologue
    1431   55/push-ebp
    1432   89/<- %ebp 4/r32/esp
    1433   # . save registers
    1434   52/push-edx
    1435   53/push-ebx
    1436   # if no event, return 0, 0
    1437   b8/copy-to-eax 0/imm32
    1438   b9/copy-to-ecx 0/imm32
    1439   (any-mouse-event?)  # => eax
    1440   3d/compare-eax-and 0/imm32/false
    1441   74/jump-if-= $read-mouse-event:end/disp8
    1442   # var f1/edx: byte = inb(0x60)
    1443   31/xor %eax 0/r32/eax
    1444   e4/read-port-into-al 0x60/imm8
    1445   89/<- %edx 0/r32/eax
    1446   (wait-for-mouse-event)
    1447   # var dx/ebx: byte = inb(0x60)
    1448   31/xor %eax 0/r32/eax
    1449   e4/read-port-into-al 0x60/imm8
    1450   89/<- %ebx 0/r32/eax
    1451   (wait-for-mouse-event)
    1452   # var dy/ecx: byte = inb(0x60)
    1453   31/xor %eax 0/r32/eax
    1454   e4/read-port-into-al 0x60/imm8
    1455   89/<- %ecx 0/r32/eax
    1456   # eax = dx
    1457   89/<- %eax 3/r32/ebx
    1458   # if (f1 & 0x10) dx = -dx
    1459   {
    1460     f6 0/subop/test-bits %dl 0x10/imm8
    1461     74/jump-if-zero break/disp8
    1462     0d/or-eax-with 0xffffff00/imm32
    1463   }
    1464   # if (f1 & 0x20) dy = -dy
    1465   {
    1466     f6 0/subop/test-bits %dl 0x20/imm8
    1467     74/jump-if-zero break/disp8
    1468     81 1/subop/or %ecx 0xffffff00/imm32
    1469   }
    1470 $read-mouse-event:end:
    1471   # . restore registers
    1472   5b/pop-to-ebx
    1473   5a/pop-to-edx
    1474   # . epilogue
    1475   89/<- %esp 5/r32/ebp
    1476   5d/pop-to-ebp
    1477   c3/return
    1478
    1479 +--147 lines: # mouse helpers -----------------------------------------------------------------------------------------------------------------------------------------------------------
    1626
    1627 # vim:ft=subx