home .. forth .. colorforth mail list archive ..

[ColorForth] USB and other serial bit boffing


On Wednesday 05 December 2001 11:57 pm, Arthur Green wrote:
> I would be interested in the source for that, too. If it isn't completely
> out of line, why don't you post in plain-text on this list?

OK, I hope this isn't too long for some. I have included other files 
necessary to understand what is going on. The actual protected mode 
application (NOS.BIN) is missing and left to individual choice.

Myron Plichota

***A2C.BAT is the NASM script used to make BYEHELLO.COM

nasm -w+orphan-labels -f bin %1.asm -l %1.lst -o %1.com -s > %1.err
edit %1.err


***BYEHELLO.ASM follows

; NoS install program

  org 0x100
  bits 16
  jmp doit

prompt$:
  db 0x0D, 0x0A
  db 'You are about to dedicate your entire C: drive to a NoS system.'
  db 0x0D, 0x0A
  db 'All previous contents including partitioning information will be lost.'
  db 0x0D, 0x0A
  db 'Press "Y" to continue, any other key to abort installation.'
  db 0x0D, 0x0A, '$'

diskerr$:
  db 0x0D, 0x0A
  db 'Error loading NoS binary file!'
  db 0x0D, 0x0A
  db 'Aborting with NoS installation incomplete.'
  db 0x0D, 0x0A, '$'

openmbr$:
  db 0x0D, 0x0A
  db 'Opening A:\MBR.BIN.'
  db 0x0D, 0x0A, '$'

opennos$:
  db 0x0D, 0x0A
  db 'Opening A:\NOS.BIN.'
  db 0x0D, 0x0A, '$'

read$:
  db 'R$'

write$:
  db 'W$'

verify$:
  db 'V $'

success$:
  db 0x0D, 0x0A
  db 'NoS installation complete.'
  db 0x0D, 0x0A
  db 'Remove installation diskette and reset your PC to run NoS.'
  db 0x0D, 0x0A
  db 'Welcome to NoS and enjoy.'
  db 0x0D, 0x0A, '$'

mbr0:
  db 'MBR.BIN', 0

nos0:
  db 'NOS.BIN', 0

;
await_hd_not_busy:
;
  mov dx,0x1F7
.L1:
    in al,dx
    test al,0x80
    jnz .L1
  ret

;
await_hd_drq:
;
  mov dx,0x1F7
.L1:
    in al,dx
    test al,0x8
    jz .L1
  ret

;
set_hd_lba:
;
  mov al,1 ; sector count
  mov dx,0x1F2
  out dx,al
  mov al,[lba] ; LBA0..7
  inc dx
  out dx,al
  mov al,[lba+1] ; LBA8..15
  inc dx
  out dx,al
  mov al,[lba+2] ; LBA16..23
  inc dx
  out dx,al
  mov al,[lba+3] ; LBA24..27
  and al,0xF
  or al,0xE0
  inc dx
  out dx,al
  ret

;
write_hd_sector:
;
  call set_hd_lba
  mov al,0x30 ; write
  mov dx,0x1F7
  out dx,al
  call await_hd_not_busy
  call await_hd_drq
  mov si,sector_buf
  mov cx,0x100
  mov dx,0x1F0
  cld
  rep outsw
  ret

;
verify_hd_sector:
;
  call set_hd_lba
  mov al,0x40 ; read verify
  mov dx,0x1F7
  out dx,al
  call await_hd_not_busy
  ret

;
xfer_sector:
;
  mov ah,0x09 ; display string
  mov dx,read$
  int 0x21
  mov ah,0x3F ; read A:
  mov dx,sector_buf
  mov cx,0x200
  mov bx,[handle]
  int 0x21
  jnc xfer_sector1
    jmp diskerr
xfer_sector1:
  mov [bytes_xfered],ax ; save actual
  cmp ax,0x200
  je xfer_sector2 ; exit if not full sector
    ret
;
xfer_sector2:
;
  mov ah,0x09 ; display string
  mov dx,write$
  int 0x21
  call write_hd_sector
  mov dx,0x1F7
  in al,dx
  test al,0x21
  jz xfer_sector3
    jmp diskerr
xfer_sector3:
  mov ah,0x09 ; display string
  mov dx,verify$
  int 0x21
  call verify_hd_sector
  mov dx,0x1F7
  in al,dx
  test al,0x21
  jz xfer_sector4
    jmp diskerr
xfer_sector4:
  ret

;
doit:
;
  mov ah,0x09 ; display string
  mov dx,prompt$
  int 0x21
  mov ah,0x01 ; read keyboard with echo
  int 0x21
  cmp al,'Y'
  je doit1
    jmp bye
doit1:
  mov ah,0x09 ; display string
  mov dx,openmbr$
  int 0x21
  mov ax,0x3D00 ; open MBR.BIN for reading
  mov dx,mbr0
  int 0x21
  jnc doit2
    jmp diskerr
doit2:
  mov [handle],ax
  mov dword [lba],0x00000000 ; 0th C: LBA
  call xfer_sector
  mov ah,0x09 ; display string
  mov dx,opennos$
  int 0x21
  mov ax,0x3D00 ; open NOS.BIN for reading
  mov dx,nos0
  int 0x21
  jnc doit3
    jmp diskerr
doit3:
  mov [handle],ax
doit4:
    inc dword [lba] ; Nth C: LBA
    call xfer_sector
    cmp word [bytes_xfered],0x200
    je doit4
  test word [bytes_xfered],0xFFFF
  jz doit5
    call xfer_sector2 ; last sector
doit5:
  mov ah,0x09 ; display string
  mov dx,success$
  int 0x21
  jmp bye

;
diskerr:
;
  mov ah,0x09 ; display string
  mov dx,diskerr$
  int 0x21
;
bye:
;
  mov ax,0x4C00
  int 0x21

  align 4
lba:
  resb 4
handle:
  resb 2
bytes_xfered:
  resb 2
sector_buf:


***A2B.BAT is the NASM script used to make MBR.BIN and NOS.BIN

nasm -w+orphan-labels -f bin %1.asm -l %1.lst -o %1.bin -s > %1.err
edit %1.err


***NOS.INC follows

; NoS system constants

; memory map
IDT_BASE                equ 0           ; interrupt descriptor table base@
NUM_INTS                equ 48          ; INTs {0..47}
SERIAL_PARALLEL         equ 0x400       ; inherited COMn, LPTn I/O@s
KEYBOARD_BUF            equ 0x800       ; keyboard buffer
KEYBOARD_BUF_SIZE       equ 0x100       ; size of keyboard buffer
KEYBOARD_BUF_END        equ KEYBOARD_BUF+KEYBOARD_BUF_SIZE
SECTOR_BUF              equ KEYBOARD_BUF_END
STACK_SIZE              equ 0x100       ; for both stacks
INIT_RP                 equ 0xA0000     ; default ESP
PAST_NOS                equ 6           ; version dependent!
%define NOS_ORG 0x100000


***MBR.ASM follows

; NoS MBR must load at 0000:7C00
; Enters 32-bit flat protected mode and loads NoS from LBA 1..?

%include "nos.inc"

  org 0x7C00
  bits 16
  cli                           ; disable interrupts
  ; turn off CRT controller cursor
  mov dx,0x3D4
  mov al,0xA
  out dx,al
  inc edx
  mov al,0x20
  out dx,al
  ; turn off floppy drive motor
  mov dx,03F2h
  mov al,0x4
  out dx,al
  ; set DS = 0x0000
  xor ax,ax
  mov ds,ax
  ; enable A20 by all known means
  mov al,0D1h                   ; via write P2 keyboard MCU cmd
  out 64h,al
  mov al,3
; 7 - kbd serial data
; 6 - kbd clk
; 5 - input buffer empty flag
; 4 - output buffer full flag
; 3,2 - undefined
; 1 - A20 gate: 1 disables stupid A20 gimmick, i.e. makes whole again
; 0 - reset: 0 resets CPU
  out 60h,al
  mov al,2                      ; via chipset
  out 92h,al
  ; enter protected mode but not 32-bit yet
  db 66h
  lgdt [gdtaddr]
  mov eax,cr0
  or al,1
  mov cr0,eax
  jmp short flush_q
flush_q:
  ; load data and stack segment registers
  mov ax,10h                    ; data and stacks selector
  mov ds,ax
  mov es,ax
  mov fs,ax
  mov gs,ax
  mov ss,ax
  mov esp,INIT_RP
  ; enter 32-bit mode via far jump
  db 66h
  db 0EAh
  dd bootload
  dw 8                          ; code selector

  bits 32
;
await_hd_not_busy:
;
  mov edx,0x1F7
.L1:
    in al,dx
    test al,0x80
    jnz .L1
  ret

;
await_hd_drq:
;
  mov edx,0x1F7
.L1:
    in al,dx
    test al,0x8
    jz .L1
  ret

;
set_hd_lba:
;
  mov al,1 ; sector count
  mov edx,0x1F2
  out dx,al
  mov al,[lba] ; LBA0..7
  inc edx
  out dx,al
  mov al,[lba+1] ; LBA8..15
  inc edx
  out dx,al
  mov al,[lba+2] ; LBA16..23
  inc edx
  out dx,al
  mov al,[lba+3] ; LBA24..27
  and al,0xF
  or al,0xE0
  inc edx
  out dx,al
  ret

;
read_hd_sector:
;
  call set_hd_lba
  mov al,0x20
  mov edx,0x1F7
  out dx,al
  call await_hd_not_busy
  call await_hd_drq
  test al,1 ; general error?
  jz read_hd_sector1
    mov word [0xB8000],0xC013 ; red blinking "!!"
    jmp short $
read_hd_sector1:
  mov ecx,0x100
  mov edx,0x1F0
  rep insw
  ret

;
bootload:
;
  lidt [idtaddr]                ; null interrupt descriptor
  cld                           ; increment src and dest ptrs
  mov edi,NOS_ORG               ; dest addr
  push edi                      ; execution start = dest addr
bootload1:
    call read_hd_sector
    inc dword [lba]
    cmp dword [lba],PAST_NOS
    jne bootload1
  ret                           ; execute

  align 4
lba: ; current LBA
  dd 0x00000001

gdtbl: ; Global Descriptor Table (GDT)
; 0: null descriptor
  dw 0
  dw 0
  db 0
  db 0
  db 0
  db 0
; 8: 4G code at 0
  dw 0FFFFh
  dw 0
  db 0
  db 10011010b
  db 11001111b
  db 0
; 10: 4G data and stack at 0
  dw 0FFFFh
  dw 0
  db 0
  db 10010010b
  db 11001111b
  db 0

gdtaddr:                        ; flat segment descriptors
  dw 23                         ; last byte offset
  dd gdtbl                      ; fixup

idtaddr:                        ; null interrupt descriptor
  dw 0
  dd 0

  times 0x1FE-($-$$) db 0 
  db 0x55, 0xAA
------------------------

To Unsubscribe from this list, send mail to Mdaemon@xxxxxxxxxxxxxxxxxx with:
unsubscribe ColorForth
as the first and only line within the message body
Problems   -   List-Admin@xxxxxxxxxxxxxxxxxx
Main ColorForth site   -   http://www.colorforth.com