Skip to content

Commit

Permalink
bios: implement comm_send.../receive..., minor fixes, add comm_echo t…
Browse files Browse the repository at this point in the history
…ests
  • Loading branch information
asiekierka committed Jul 27, 2024
1 parent fd33d6e commit 96c7e34
Show file tree
Hide file tree
Showing 13 changed files with 566 additions and 8 deletions.
89 changes: 89 additions & 0 deletions bios/comm/__comm_wait_timeout.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/**
* Copyright (c) 2023, 2024 Adrian "asie" Siekierka
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

.arch i186
.code16
.intel_syntax noprefix

#include "../common.inc"

// Input:
// - BH = awaited mask (any bit)
// - CX = timeout
// Output:
// - AH = 0 if success, 1 if timeout, 2 if overrun, 3 if cancel
// - AL = (serial status & BH)
// Clobber: AX
//
// TODO: Halt CPU while waiting for serial input.
// Note that the serial send/receive interrupts will loop
// indefinitely until the byte is sent/received.

.global __comm_wait_timeout
__comm_wait_timeout:
pushf
push dx
sti

ss mov dx, [tick_count]
add dx, cx // DX = final tick count

1:
xor ax, ax
in al, IO_SERIAL_STATUS
and al, bh
jnz 8f // received mask?

cmp cx, 0xFFFF
je 2f // skip timeout?

test cx, cx
jz 7f // zero timeout?
ss cmp dx, [tick_count]
jle 7f // timeout?

2:
// check for cancel key
ss mov ax, [comm_cancel_key]
test ax, ax
jz 1b // cancel key not set?
ss and ax, [keys_held]
ss cmp ax, [comm_cancel_key]
jne 1b // cancel key combo not matched?

mov ah, 3 // return cancel
jmp 9f

7:
mov ah, 1 // return timeout
jmp 9f

8:
test al, SERIAL_OVERRUN
jz 9f // not overrun? (only checked if overrun in mask)
mov ah, 2 // return overrun

9:
pop dx
popf
ret

16 changes: 8 additions & 8 deletions bios/comm/comm.s
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@
irq_comm_handlers:
.word comm_open
.word comm_close
.word error_handle_irq20 // TODO: comm_send_char
.word error_handle_irq20 // TODO: comm_receive_char
.word error_handle_irq20 // TODO: comm_receive_with_timeout
.word error_handle_irq20 // TODO: comm_send_string
.word error_handle_irq20 // TODO: comm_send_block
.word error_handle_irq20 // TODO: comm_receive_block
.word comm_send_char
.word comm_receive_char
.word comm_receive_with_timeout
.word comm_send_string
.word comm_send_block
.word comm_receive_block
.word comm_set_timeout
.word comm_set_baudrate
.word comm_get_baudrate
Expand All @@ -49,13 +49,13 @@ irq_comm_handler:
iret

.section ".data"
.global comm_baudrate
comm_baudrate: .byte 1 // 38400 bps
.global comm_recv_timeout
comm_recv_timeout: .word 0xFFFF
.global comm_send_timeout
comm_send_timeout: .word 0xFFFF

.section ".bss"
.global comm_baudrate
comm_baudrate: .byte 0
.global comm_cancel_key
comm_cancel_key: .word 0
68 changes: 68 additions & 0 deletions bios/comm/comm_receive_block.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* Copyright (c) 2023, 2024 Adrian "asie" Siekierka
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

.arch i186
.code16
.intel_syntax noprefix

#include "../common.inc"

/**
* INT 14h AH=07h - comm_receive_block
* Input:
* - CX = Length, in bytes
* - DS:DX = Input buffer
* Output:
* - AX = Status
* - DX = Number of bytes received
*/
.global comm_receive_block
comm_receive_block:
push cx
push di
push es
push ds
pop es
mov di, dx

// check for CX == 0
xor ax, ax
test cx, cx
jz 9f

mov dx, ax // DX = 0

1:
call comm_receive_char
test ah, ah
jnz 9f // if error, return error

stosb // write received character
inc dx
loop 1b

9:
pop es
pop di
pop cx
ret

42 changes: 42 additions & 0 deletions bios/comm/comm_receive_char.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* Copyright (c) 2023, 2024 Adrian "asie" Siekierka
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

.arch i186
.code16
.intel_syntax noprefix

#include "../common.inc"

/**
* INT 14h AH=03h - comm_receive_char
* Input:
* Output:
* - AX = Character or status
*/
.global comm_receive_char
comm_receive_char:
push cx
ss mov cx, [comm_recv_timeout]
call comm_receive_with_timeout
pop cx
ret

63 changes: 63 additions & 0 deletions bios/comm/comm_receive_with_timeout.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* Copyright (c) 2023, 2024 Adrian "asie" Siekierka
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

.arch i186
.code16
.intel_syntax noprefix

#include "../common.inc"

/**
* INT 14h AH=04h - comm_receive_with_timeout
* Input:
* - CX = Timeout, in frames
* Output:
* - AX = Character or status
*/
.global comm_receive_with_timeout
comm_receive_with_timeout:
push bx

// Clear overrun, in case it occured before.
in al, IO_SERIAL_STATUS
or al, SERIAL_OVERRUN_RESET
out IO_SERIAL_STATUS, al

mov bh, (SERIAL_OVERRUN | SERIAL_RX_READY)
call __comm_wait_timeout
test ah, ah
jnz 8f

// receive character
xor ax, ax
in al, IO_SERIAL_DATA
jmp 9f

8:
// convert __comm_wait_timeout result to error code
xchg ah, al
mov ah, 0x81

9:
pop bx
ret

60 changes: 60 additions & 0 deletions bios/comm/comm_send_block.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* Copyright (c) 2023, 2024 Adrian "asie" Siekierka
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

.arch i186
.code16
.intel_syntax noprefix

#include "../common.inc"

/**
* INT 14h AH=06h - comm_send_block
* Input:
* - CX = Length, in bytes
* - DS:DX = Input buffer
* Output:
* - AX = Status
*/
.global comm_send_block
comm_send_block:
push cx
push si
mov si, dx

// check for CX == 0
xor ax, ax
test cx, cx
jz 9f

1:
lodsb
mov bl, al
call comm_send_char
test ax, ax
jnz 9f // if error, return error
loop 1b

9:
pop si
pop cx
ret

Loading

0 comments on commit 96c7e34

Please sign in to comment.