ponydos/ponydos.asm

261 lines
3.3 KiB
NASM
Raw Normal View History

2022-02-11 16:16:49 +00:00
cpu 8086
bits 16
org 0x7c00
2023-02-06 15:45:26 +00:00
COLUMNS equ 80
ROWS equ 25
2022-02-11 16:16:49 +00:00
jmp 0:start
start:
cld
cli
; Set up segments and stack
mov ax, cs
mov ds, ax
mov es, ax
mov ss, ax
mov sp, $$
sti
2023-02-06 15:15:47 +00:00
; Clear BSS
mov di, _bss_start
mov cx, _bss_end - _bss_start
xor al, al
rep stosb
2023-01-30 16:56:16 +00:00
initialize_mouse:
; Initialize mouse
; https://www.ctyme.com/intr/rb-1601.htm
mov ax, 0xc205
mov bh, 3 ; TODO: This is the usual PS/2 mouse packet size, but is it correct here?
int 0x15
jc .error
; Set handler address
; https://www.ctyme.com/intr/rb-1603.htm
mov ax, 0xc207
; es is already set correctly
mov bx, mouse_handler
int 0x15
jc .error
; Enable mouse
; https://www.ctyme.com/intr/rb-1596.htm
mov ax, 0xc200
mov bh, 1
int 0x15
jc .error
2023-02-06 15:45:26 +00:00
jmp .done
2023-01-30 16:56:16 +00:00
.error:
; https://www.ctyme.com/intr/rb-1601.htm
mov ax, 0xc201
int 0x15
jmp initialize_mouse
2022-02-11 16:16:49 +00:00
2023-02-06 15:45:26 +00:00
.done:
initialize_screen:
mov ax, 0xb800 ; VGA text video memory
mov es, ax
; Clear screen with asterisks and 15-0 background-foreground
mov ax, 0xf000 + '*'
mov cx, 25*80
xor di, di
rep stosw
; Disable text cursor
mov dx, 0x3d4
mov al, 0xa
out dx, al
inc dx
mov al, 0x20
out dx, al
xor ax, ax
call flip_mouse_cursor
2022-02-11 16:16:49 +00:00
mainloop:
2023-02-06 15:45:26 +00:00
mov bx, [mouse_x]
mov cx, [mouse_y]
cmp [mouse_x_old], bx
jne .update_cursor
cmp [mouse_y_old], cx
jne .update_cursor
2022-02-11 16:16:49 +00:00
hlt
2023-02-06 15:45:26 +00:00
jmp mainloop
.update_cursor:
mov ah, [mouse_x_old]
mov al, [mouse_y_old]
call flip_mouse_cursor
mov ah, [mouse_x]
mov al, [mouse_y]
call flip_mouse_cursor
mov [mouse_x_old], bx
mov [mouse_y_old], cx
jmp mainloop
; in:
; ah = column
; al = row
flip_mouse_cursor:
push ax
push bx
push cx
push es
mov bx, 0xb800
mov es, bx
; Column
xor bh, bh
mov bl, ah
shl bx, 1
; Row
mov cl, COLUMNS*2
mul cl
add bx, ax
; Swap foreground and background colours
inc bx
mov al, [es:bx]
mov cl, 4
ror al, cl
mov [es:bx], al
pop es
pop cx
pop bx
pop ax
ret
2022-02-11 16:16:49 +00:00
2023-02-06 15:15:47 +00:00
Y_OVERFLOW equ 0x80
X_OVERFLOW equ 0x40
Y_NEGATIVE equ 0x20
X_NEGATIVE equ 0x10
BUTTONS equ 0x03
X_MAX_VALUE equ COLUMNS-1
Y_MAX_VALUE equ ROWS-1
2023-01-30 16:56:16 +00:00
mouse_handler:
2022-02-11 16:16:49 +00:00
push ax
push bx
2023-01-30 16:56:16 +00:00
push bp
2023-02-06 15:15:47 +00:00
push ds
2023-01-30 16:56:16 +00:00
2023-02-06 15:15:47 +00:00
mov ax, cs
mov ds, ax
2023-01-30 16:56:16 +00:00
2023-02-06 15:15:47 +00:00
mov bp, sp
2023-01-30 16:56:16 +00:00
2023-02-06 15:15:47 +00:00
mov bx, [bp+18] ; status
test bl, X_OVERFLOW
jnz .x_end
.x:
mov ax, [bp+16] ; X
test bl, X_NEGATIVE
jnz .x_negative
.x_nonnegative:
add [mouse_x], ax
cmp word [mouse_x], X_MAX_VALUE
jb .x_end
mov word [mouse_x], X_MAX_VALUE
jmp .x_end
.x_negative:
neg al
sub [mouse_x], ax
jnc .x_end
mov word [mouse_x], 0
.x_end:
test bl, Y_OVERFLOW
jnz .y_end
.y:
mov ax, [bp+14] ; Y
test bl, Y_NEGATIVE
jnz .y_negative
.y_nonnegative:
; Y-axis is inverted
sub [mouse_y], ax
jnc .y_end
mov word [mouse_y], 0
jmp .y_end
.y_negative:
neg al
add [mouse_y], ax
cmp word [mouse_y], Y_MAX_VALUE
jb .y_end
mov word [mouse_y], Y_MAX_VALUE
.y_end
and bl, BUTTONS
mov [mouse_buttons], bl
pop ds
2023-01-30 16:56:16 +00:00
pop bp
2022-02-11 16:16:49 +00:00
pop bx
pop ax
2023-01-30 16:56:16 +00:00
retf
hexprint16:
xchg ah, al
call hexprint8
xchg ah, al
2022-02-11 16:16:49 +00:00
hexprint8:
push ax
push cx
mov cl, 4
shr al, cl
call hexprint4
pop cx
pop ax
hexprint4:
push ax
and al, 0xf
cmp al, 10
jb .digit09
add al, 'a' - '0' - 10
.digit09:
add al, '0'
mov ah, 0x0e
int 0x10
pop ax
ret
times 510-($-$$) db 0
db 0x55
db 0xaa
2023-02-06 15:15:47 +00:00
section .bss
_bss_start:
mouse_x resw 1
mouse_y resw 1
mouse_buttons resb 1
2023-02-06 15:45:26 +00:00
mouse_x_old resw 1
mouse_y_old resw 1
2023-02-06 15:15:47 +00:00
_bss_end: