Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Andrew Jenner December 15, 2013
file 565 lines (472 sloc) 9.217 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564
%include "../defaults_bin.asm"

  jmp codeStart

  db '20131215a-com1-115200',0

codeStart:
  sub di,0x500
  cmp di,kernelEnd
  jne lengthOk1
  or byte[cs:flags],1
lengthOk1:

  call findSP
findSP:
  pop si
  sub si,findSP


  mov ah,0
  mov cx,checkSum
; mov [cs:kernelLength],cx
; mov [cs:kernelOffset],si

checksumLoop:
  lodsb
  add ah,al
  loop checksumLoop
; mov [cs:computedChecksum],ah
  cmp ah,[cs:checkSum]
  je checksumOk1
  or byte[cs:flags],2
checksumOk1:

; call findSP2
;findSP2:
; pop si
; sub si,findSP2
; mov [cs:kernelOffset],si
; mov word[cs:kernelLength],checkSum

  ; Don't want any stray interrupts interfering with the serial port accesses.
  cli

  ; Set 40-column text mode
  initCGA 8

  ; Clear the video memory
  mov ax,0xb800
  mov es,ax
  mov cx,40*25
  mov ax,0x0700
  xor di,di
  cld
  rep stosw

  ; Set up the timer interrupt
  mov al,TIMER0 | BOTH | MODE3 | BINARY
  out 0x43,al
  mov al,0 ; rate = 13125000/11/2^16 ~= 18.2Hz
  out 0x40,al
  out 0x40,al

  ; Set up interrupt masks.
  mov al,0xbc ; Enable IRQs 0 (timer), 1 (keyboard) and 6 (floppy disk).
  out 0x21,al ; Leave disabled 2 (EGA/VGA/slave 8259) 3 (COM2/COM4), 4 (COM1/COM3), 5 (hard drive, LPT2) and 7 (LPT1)

  ; Set up interrupt table
  xor ax,ax
  mov es,ax
  mov ax,0xf000
  mov ds,ax
  mov cx,8
  mov si,0xfef3
  mov di,0x20
interruptSetupLoop:
  movsw
  inc di
  inc di
  loop interruptSetupLoop
  mov word[0x02*4], 0xf85f ; nmi_int
  mov word[0x05*4], 0xff54 ; print_screen
  mov word[0x18*4 + 2], 0xf600 ; segment for BASIC

  ; Disable NMI
  xor al,al
  out 0xa0,al

  ; Move the stack right at the end of main RAM.
  mov ax,0x9000
  mov ss,ax
  xor sp,sp

  mov di,0x50 ;Target segment (TODO: make this 0060:0000 as FreeDOS does?)
  mov bx,cs
  call main
main:
  pop si
  sub si,main ; Offset of our start within CS
  jnz checkDestinationClear
  cmp bx,di
  jz noRelocationNeeded
checkDestinationClear:
  ; Check that (start of our code) >= (end of destination code)
  ; Normalize our address
  mov cl,4
  mov ax,si
  shr ax,cl
  add ax,bx
  and si,0x0f ; Our start address in normalized xxxx:000x form is now in AX:SI
  ; compute end of destination as a physical address
  mov dx,di
  add dx,(kernelEnd + 15) >> 4 ; end of destination segment
  cmp ax,dx
  jge doMove
  ; We can't relocate to the final destination directly, since our code
  ; overlaps that space. We need to move to a point that is higher than both
  ; the end of our code and the end of the final destination.
  mov di,ax
  add di,(kernelEnd + 15) >> 4 ; end of current code
  cmp di,dx
  jge doMove
  ; We are overlapping the start of the final destination - relocate to after
  ; the end of the final destination.
  mov di,dx
doMove:
  push di ; Push return segment
  ; Move kernelEnd bytes from AX:SI to DI:0
  mov cx,kernelEnd
  mov ds,ax
  mov es,di
  xor di,di
  push di ; Push return offset
  rep movsb
  retf

noRelocationNeeded:
  ; Set up some interrupts
  ; int 0x60 == capture screen
  ; int 0x61 == start audio recording
  ; int 0x62 == stop audio recording
  ; int 0x63 == print AX as a 4-digit hex number
  ; int 0x64 == print CX bytes from DS:SI
  ; int 0x65 == print AL as a character
  ; int 0x66 == send file
  ; int 0x67 == finish
  ; int 0x68 == load data from serial port at ES:DI. On completion, ES:DI points to end of loaded data.
  ; int 0x69 == stop screen output
  ; int 0x6a == start screen output
  xor ax,ax
  mov ds,ax
  setInterrupt 0x60, captureScreenRoutine
  setInterrupt 0x61, startAudioRoutine
  setInterrupt 0x62, stopAudioRoutine
  setInterrupt 0x63, printHexRoutine
  setInterrupt 0x64, printStringRoutine
  setInterrupt 0x65, printCharacterRoutine
  setInterrupt 0x66, sendFileRoutine
  setInterrupt 0x67, completeRoutine
  setInterrupt 0x68, loadSerialDataRoutine
  setInterrupt 0x69, stopScreenRoutine
  setInterrupt 0x6a, resumeScreenRoutine

  ; Reset video variables
  mov ax,cs
  mov ds,ax
  xor ax,ax
  mov [column],al
  mov [startAddress],ax

  test byte[flags],1
  jz lengthOk

  mov si,lengthWrong
  mov cx,lengthWrongEnd - lengthWrong
  printString

lengthOk:
  test byte[flags],2
  jz checksumOk

  mov si,checksumWrong
  mov cx,checksumWrongEnd - checksumWrong
  printString

; mov ax,[cs:computedChecksum]
; printHex
; mov ax,[cs:kernelOffset]
; printHex
; mov ax,[cs:kernelLength]
; printHex

checksumOk:

  ; Print the boot message
; mov si,bootMessage
; mov cx,bootMessageEnd - bootMessage
; printString

  ; Push the cleanup address for the program to retf back to.
  mov bx,cs
  push bx
  mov ax,completeRoutine
  push ax

  ; Find the next segment after the end of the kernel. This is where we'll
  ; load our program.
  mov ax,(kernelEnd + 15) >> 4
  add ax,bx
  mov es,ax
  xor di,di

  ; Push the address so we can just retf to it after the load
  push es
  push di

  loadSerialData

  ; Print the OK message
  mov si,okMessage
  mov cx,okMessageEnd - okMessage
  printString

  retf


loadSerialDataRoutine:
  initSerial

  inc dx ; 5
  mov al,'R'
  sendByte
  dec dx

packetLoop:
  ; Activate DTR
  mov al,1
  out dx,al
  inc dx ; 5

  ; Receive packet size in bytes
  receiveByte

  mov cl,al
  mov ch,0

  ; Push a copy to check when we're done and adjust DI for retries
  push cx

; call printCharNoSerial

  ; Init checksum
  mov ah,0

  ; Receive CX bytes and store them at ES:DI
  jcxz noBytes
byteLoop:
  receiveByte
  add ah,al
  stosb
  loop byteLoop
noBytes:

  ; Receive checksum
  receiveByte
  sub ah,al

  ; Deactivate DTR
  dec dx ; 4
  mov al,0
  out dx,al

  cmp ah,0
  jne checkSumFailed

  ; Send success byte
  inc dx ; 5
  mov al,'K'
  sendByte
  dec dx ; 4


  ; Normalize ES:DI
  mov ax,di
  mov cl,4
  shr ax,cl
  mov bx,es
  add bx,ax
  mov es,bx
  and di,0xf

  pop cx
  jcxz transferComplete
  jmp packetLoop

checkSumFailed:
  ; Send fail byte
  inc dx ; 5
  mov al,'F'
  sendByte
  dec dx ; 4

  pop cx
  sub di,cx
  jmp packetLoop

transferComplete:
  iret


completeRoutine:
  disconnect
  jmp 0 ; Restart the kernel


printNybble:
  cmp al,10
  jge printAlphabetic
  add al,'0'
  jmp printGotCharacter
printAlphabetic:
  add al,'A' - 10
printGotCharacter:
  jmp printChar


printHexRoutine:
  push bx
  push cx
  mov bx,ax
  mov al,bh
  mov cl,4
  shr al,cl
  call printNybble
  mov al,bh
  and al,0xf
  call printNybble
  mov al,bl
  shr al,cl
  call printNybble
  mov al,bl
  and al,0xf
  call printNybble
  pop cx
  pop bx
  iret


printStringRoutine:
  lodsb
  call printChar
  loop printStringRoutine
  iret


printCharacterRoutine:
  call printChar
  iret


printCharNoScreen:
  push dx
  mov dx,0x3f8 + 5
  sendByte
  pop dx
  ret


printCharNoSerial:
  push dx
  jmp printCharCommon


printChar:
  push dx

  ; Output the character over serial as well
  mov dx,0x3f8 + 5
  sendByte

printCharCommon:
  cmp word[cs:screenCounter],0
  jne .complete

  push bx
  push cx
  push es
  push di

  mov dx,0xb800
  mov es,dx
  mov dx,0x3d4
  mov cx,[cs:startAddress]
  cmp al,10
  je .newLine
  mov di,cx
  add di,cx
  mov bl,[cs:column]
  xor bh,bh
  mov [es:bx+di+24*40*2],al
  inc bx
  inc bx
  cmp bx,80
  jne .done
.newLine:
  add cx,40
  and cx,0x1fff
  mov [cs:startAddress],cx

  ; Scroll screen
  mov ah,ch
  mov al,0x0c
  out dx,ax
  mov ah,cl
  inc ax
  out dx,ax
  ; Clear the newly scrolled area
  mov di,cx
  add di,cx
  add di,24*40*2
  mov cx,40
  mov ax,0x0700
  rep stosw
  mov cx,[cs:startAddress]

  xor bx,bx
.done:
  mov [cs:column],bl

  ; Move cursor
  shr bx,1
  add bx,cx
  add bx,24*40
  and bx,0x1fff
  mov ah,bh
  mov al,0x0e
  out dx,ax
  mov ah,bl
  inc ax
  out dx,ax

  pop di
  pop es
  pop cx
  pop bx
.complete:
  pop dx
  ret

captureScreenRoutine:
  push ax
  mov al,1
  call printCharNoScreen
  pop ax
  iret

startAudioRoutine:
  push ax
  mov al,2
  call printCharNoScreen
  pop ax
  iret

stopAudioRoutine:
  push ax
  mov al,3
  call printCharNoScreen
  pop ax
  iret

printCharEscaped:
  cmp al,0x5
  ja .checkForComplete
.escapeNeeded:
  push ax
  mov al,0
  call printCharNoScreen
  pop ax
.noEscapeNeeded:
  call printCharNoScreen
  ret
.checkForComplete:
  cmp al,0x1a
  je .escapeNeeded
  jmp .noEscapeNeeded


sendFileRoutine:
  cld
  mov al,4
  call printCharNoScreen
  mov al,cl
  call printCharEscaped
  mov al,ch
  call printCharEscaped
  mov al,dl
  call printCharEscaped
.loopTop:
  cmp cx,0
  jne .doByte
  cmp dl,0
  je .done
.doByte:
  cmp si,0xffff
  jne .normalized
  mov si,0x000f
  mov ax,ds
  add ax,0xfff
  mov ds,ax
.normalized:
  lodsb
  call printCharEscaped
  dec cx
  cmp cx,0xffff
  jne .loopTop
  dec dl
  jmp .loopTop
.done:
  iret


stopScreenRoutine:
  inc word[cs:screenCounter]
  iret


resumeScreenRoutine:
  dec word[cs:screenCounter]
  iret


column:
  db 0
startAddress:
  dw 0
;bootMessage:
; db 'XT Serial Kernel',10
;bootMessageEnd:
lengthWrong:
  db 'Length incorrect',10
lengthWrongEnd:
checksumWrong:
  db 'Checksum incorrect',10
checksumWrongEnd:
okMessage:
  db 'OK',10
okMessageEnd:
screenCounter:
  dw 0

flags:
  db 0

;kernelLength:
; dw 0
;kernelOffset:
; dw 0
;computedChecksum:
; db 0

checkSum:
  db 0 ; This will be overwritten by quickboot

kernelEnd:
Something went wrong with that request. Please try again.