X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?a=blobdiff_plain;f=src%2Fboot%2Fboot.s;h=f17766bce2174072c420bb8523cdefe011a735bf;hb=16ffb9d5c70a64285cd84b3599698ee0297d1d0c;hp=22028005bf05bb09cbd62caf9af0e78e0539b119;hpb=7fe9b061ba7556ca9617cd061d16e2bc8e040f63;p=bootcensus diff --git a/src/boot/boot.s b/src/boot/boot.s index 2202800..f17766b 100644 --- a/src/boot/boot.s +++ b/src/boot/boot.s @@ -1,16 +1,35 @@ +# pcboot - bootable PC demo/game kernel +# Copyright (C) 2018 John Tsiombikas +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY, without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + .code16 .section .boot,"a" + .set stack_top, 0x7be0 + .set read_retries, 0x7be8 + .set drive_number, 0x7bec + .set cursor_x, 0x7bee .set scratchbuf, 0x7bf0 .set scratchbuf_size, 16 boot: cli - cld # move stack to just below the code xor %ax, %ax mov %ax, %ss - mov $0x7bf0, %sp + mov $stack_top, %sp # use the code segment for data access mov %cs, %ax mov %ax, %ds @@ -20,15 +39,16 @@ boot: call setup_serial + call get_drive_chs + mov $loading_msg, %si call print_str # load the second stage boot loader and jump to it mov $_boot2_size, %eax call print_num - mov $loading_msg2, %si - call print_str + mov $_boot2_size, %eax mov %eax, %ebx shr $9, %eax and $0x1ff, %ebx @@ -45,13 +65,11 @@ boot: call read_sectors jmp boot2_addr - .set SECT_PER_TRACK, 18 - .set ARG_NSECT, 6 .set ARG_SIDX, 4 -loading_msg: .asciz "Loading " -loading_msg2: .asciz " bytes\n" +loading_msg: .asciz "\nLoad " +driveno_msg: .asciz "Drv:" sect_per_track: .short 18 num_cylinders: .short 80 @@ -59,26 +77,45 @@ num_heads: .short 2 heads_mask: .byte 1 get_drive_chs: + mov $driveno_msg, %si + call print_str + xor %eax, %eax movb drive_number, %dl + mov %dl, %al + call print_num + mov $10, %al + call print_char + mov $8, %ah int $0x13 - jnc .Lok + jnc ok ret -.Lok: mov %ch, %al +ok: xor %eax, %eax + mov %ch, %al mov %cl, %ah rol $2, %ah + inc %ax and $0x3ff, %ax mov %ax, num_cylinders + and $0x3f, %cx mov %cx, sect_per_track - andw $0x3f, sect_per_track shr $8, %dx - mov %dx, num_heads - dec %dl mov %dl, heads_mask + inc %dx + mov %dx, num_heads + call print_num + mov $47, %al + call print_char + mov %dx, %ax + call print_num + mov $47, %al + call print_char + mov %cx, %ax + call print_num ret # read_sectors(first, num) @@ -101,19 +138,16 @@ read_sectors: pop %bp ret - .set VAR_ATTEMPTS, -2 - # read_sector(sidx) read_sector: push %bp mov %sp, %bp - sub $2, %sp push %cx push %dx - movw $3, VAR_ATTEMPTS(%bp) + movw $3, read_retries -.Lread_try: +read_try: # calculate the track (sidx / sectors_per_track) mov 4(%bp), %ax @@ -145,22 +179,22 @@ read_sector: mov $0x0201, %ax movb drive_number, %dl int $0x13 - jnc .Lread_ok + jnc read_ok # abort after 3 attempts - decw VAR_ATTEMPTS(%bp) - jz .Lread_fail + decw read_retries + jz read_fail # error detected, reset controller and retry xor %ah, %ah int $0x13 - jmp .Lread_try + jmp read_try -.Lread_fail: +read_fail: mov 4(%bp), %ax jmp abort_read -.Lread_ok: +read_ok: mov $46, %ax call print_char @@ -173,33 +207,19 @@ read_sector: 0: pop %dx pop %cx - add $2, %sp pop %bp ret -str_read_error: .asciz "read error, sector: " +str_read_error: .asciz "rderr:" abort_read: mov $str_read_error, %si - call print_str_num16 - hlt - - # prints a string (ds:si) followed by a number (eax) -print_str_num: - push %eax call print_str - pop %eax + and $0xffff, %eax call print_num mov $10, %al - call ser_putchar - ret - -print_str_num16: - push %eax - and $0xffff, %eax - call print_str_num - pop %eax - ret + call print_char + hlt # expects string pointer in ds:si print_str: @@ -207,18 +227,24 @@ print_str: 0: mov (%si), %al cmp $0, %al - jz .Lend + jz end call print_char inc %si jmp 0b -.Lend: popa +end: popa ret # expects character in al print_char: push %es - pushw $0xb800 + + push %ax + cmp $10, %ax + jnz 0f + mov $32, %ax + +0: pushw $0xb800 pop %es movw cursor_x, %di shl $1, %di @@ -227,12 +253,11 @@ print_char: movb $7, %es:1(%di) incw cursor_x + pop %ax call ser_putchar pop %es ret -cursor_x: .short 0 - # expects number in eax .global print_num print_num: @@ -242,14 +267,14 @@ print_num: movw $scratchbuf + scratchbuf_size, %si movb $0, (%si) mov $10, %ebx -.Lconvloop: +convloop: xor %edx, %edx div %ebx add $48, %dl dec %si mov %dl, (%si) cmp $0, %eax - jnz .Lconvloop + jnz convloop call print_str @@ -267,12 +292,8 @@ print_num: .set DIV_9600, 115200 / 9600 .set LCTL_8N1, 0x03 .set LCTL_DLAB, 0x80 - .set FIFO_ENABLE, 0x01 - .set FIFO_SEND_CLEAR, 0x04 - .set FIFO_RECV_CLEAR, 0x02 - .set MCTL_DTR, 0x01 - .set MCTL_RTS, 0x02 - .set MCTL_OUT2, 0x08 + .set FIFO_ENABLE_CLEAR, 0x07 + .set MCTL_DTR_RTS_OUT2, 0x0b .set LST_TREG_EMPTY, 0x20 setup_serial: @@ -291,21 +312,16 @@ setup_serial: mov $UART_LCTL, %dx out %al, %dx # clear and enable fifo - mov $FIFO_ENABLE, %al - or $FIFO_SEND_CLEAR, %al - or $FIFO_RECV_CLEAR, %al + mov $FIFO_ENABLE_CLEAR, %al mov $UART_FIFO, %dx out %al, %dx # assert RTS and DTR - mov $MCTL_DTR, %al - or $MCTL_RTS, %al - or $MCTL_OUT2, %al + mov $MCTL_DTR_RTS_OUT2, %al mov $UART_MCTL, %dx out %al, %dx ret # expects a character in al - .global ser_putchar ser_putchar: push %dx @@ -319,9 +335,9 @@ ser_putchar: 0: mov %al, %ah # wait until the transmit register is empty mov $UART_LSTAT, %dx -.Lwait: in %dx, %al +wait: in %dx, %al and $LST_TREG_EMPTY, %al - jz .Lwait + jz wait mov $UART_DATA, %dx mov %ah, %al out %al, %dx @@ -329,10 +345,7 @@ ser_putchar: pop %dx ret - -drive_number: .byte 0 .org 510 .byte 0x55 .byte 0xaa - boot2_addr: