Rewrite mouse handler to use less code

This commit is contained in:
Juhani Krekelä 2023-03-18 15:10:48 +02:00
parent cb030ccf41
commit 9d3de7483e
1 changed files with 44 additions and 38 deletions

View File

@ -319,8 +319,6 @@ open_file:
Y_OVERFLOW equ 0x80
X_OVERFLOW equ 0x40
Y_NEGATIVE equ 0x20
X_NEGATIVE equ 0x10
BUTTONS equ 0x03
X_MAX_VALUE equ 2*COLUMNS-1
@ -328,62 +326,70 @@ Y_MAX_VALUE equ 4*ROWS-1
mouse_handler:
pusha
push ds
mov ax, cs
mov ds, ax
mov bp, sp
mov bx, [bp+2*9+10] ; status
mov bx, [bp+2*8+10] ; status
test bl, X_OVERFLOW
jnz .x_end
.x:
mov ax, [bp+2*9+8] ; X
mov si, [cs:mouse_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
mov ax, [bp+2*8+8] ; X
.x_negative:
neg al
sub [mouse_x], ax
jnc .x_end
mov word [mouse_x], 0
; X and Y coördinates are stored as 9-bit signed integers
; using two's complement notation. The high bits are called
; "X negative" and "Y negative".
mov ah, bl
shl ah, 3 ; X negative is bit 4, shift it to sign position
sar ah, 7 ; Fill entire byte with sign bit's value
add si, ax
;cmp si, 0
jge .not_x_underflow
xor si, si
.not_x_underflow:
cmp si, X_MAX_VALUE
jle .not_x_overflow
mov si, X_MAX_VALUE
.not_x_overflow:
mov [cs:mouse_x], si
.x_end:
test bl, Y_OVERFLOW
jnz .y_end
.y:
mov ax, [bp+2*9+6] ; Y
mov si, [cs:mouse_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
mov ax, [bp+2*8+6] ; Y
.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
mov ah, bl
shl ah, 2 ; Y negative is bit 5
sar ah, 7
; Y direction is flipped compared to our coöridinate space
sub si, ax
;cmp si, 0
jge .not_y_underflow
xor si, si
.not_y_underflow:
cmp si, Y_MAX_VALUE
jl .not_y_overflow
mov si, Y_MAX_VALUE
.not_y_overflow:
mov [cs:mouse_y], si
.y_end:
and bl, BUTTONS
mov [mouse_buttons], bl
mov [cs:mouse_buttons], bl
pop ds
popa
retf