From a6a7debf2e0aa0d7f4f416c6a59f9e1cec5ecc45 Mon Sep 17 00:00:00 2001 From: PoliEcho Date: Sun, 13 Jul 2025 20:23:19 +0200 Subject: [PATCH] break tty while trining to fix tty --- src/input.asm | 40 ++++++++++++++++++++++++++++++++++++---- src/main.asm | 48 ++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 82 insertions(+), 6 deletions(-) diff --git a/src/input.asm b/src/input.asm index 4ef712f..ecef6e7 100644 --- a/src/input.asm +++ b/src/input.asm @@ -12,6 +12,8 @@ section .bss extern gameboard_ptr extern simulation_running + + extern running_in_tty section .data cursor_rows: dw 1 cursor_cols: dw 1 @@ -64,17 +66,47 @@ handle_user_input:; main loop of the program xor rax, rax mov qword [r12], rax; zeroout the buffer + movss xmm0, [speed_multiplier] + movss xmm1, [simulation_speed] + mulss xmm0, xmm1; callculate sleep lenght + cvttss2si rdx, xmm0; truncate and copy to rdx + + mov byte al, [running_in_tty] + test al, al; test if we are running in tty + jz .pts + + mov rax, rdx + xor rdx, rdx + mov rcx, 1000; convert miliseconds to seconds + div rcx + + mov qword [r12], rax; store seconds + + mov rax, rdx + xor rdx, rdx + mov rcx, 1000000 + mul rcx + + mov qword [r12+8], rax; store nanoseconds + mov rax, SYS_NANOSLEEP + mov rdi, r12 + xor rsi, rsi; ignore some remaining time or something + syscall + + mov rax, 1; simulate sys_poll output + jmp .end_input_handling + + .pts: mov rax, SYS_POLL mov dword [r12], STDIN; create pollfd struct mov word [r12+4], POLLIN mov rdi, r12 mov rsi, 1; only one file descriptor is provided - movss xmm0, [speed_multiplier] - movss xmm1, [simulation_speed] - mulss xmm0, xmm1; callculate sleep lenght - cvttss2si rdx, xmm0; truncate and copy to rdx + ; rdi already set syscall + .end_delay_handling: + test rax, rax; SYS_POLL returns 0 when no change happens within timeout jz .no_input diff --git a/src/main.asm b/src/main.asm index a594e38..022a120 100644 --- a/src/main.asm +++ b/src/main.asm @@ -19,7 +19,9 @@ section .bss extern cursor_rows extern cursor_cols - + + global running_in_tty + running_in_tty: RESB 1 section .rodata extern resetLen @@ -27,6 +29,8 @@ section .rodata hide_cursor: db ESC_CHAR, "[?25l", 0 show_cursor: db ESC_CHAR, "[?25h", 0 + help_text: db "asm-game-of-life [args]",0xA,"-h display this help menu",0xA,"Controls:",0xA,"use arrow keys to move around",0xA,"ENTER to invert cell",0xA,"p to START/STOP simulation",0xA,"k to increase simulation speed",0xA,"j to decrese simulation speed",0xA, 0 + section .text extern print_str extern unsigned_int_to_ascii @@ -58,11 +62,50 @@ _start: ; handle args pop rcx; get argc (number of arguments) + pop rax; get rid of program name arugument cmp rcx, 1 jle .no_arguments_provided - ; TODO hanndle arguments + dec rcx + .handle_arg: + pop rax + mov word di, [rax] + + cmp di, 0x682D; check if -h was passed + jne .next_arg + lea rdi, [help_text] + call print_str + jmp .exit_program + + .next_arg: + dec rcx + test rcx, rcx + jnz .handle_arg + .no_arguments_provided: + + pop rax; get rid of null termination of argv + ; handle enviroment vars + + .handle_env: + pop rax + test rax, rax; test if we reached end of envs + jz .no_envs + + mov dword edi, [rax] + cmp edi, 0x4D524554; check for "TERM" inverted becose endiannes + jne .handle_env + mov qword rdi, [rax+5]; remove the TERM= part this should never segfault since there sould allwas be other data behind enviroment vars and i dont mind garbage + mov rsi, 0xffffffffff + and rdi, rsi + mov rsi, 0x78756e696c + cmp rdi, rsi; check for "linux" + jne .no_envs + mov byte [running_in_tty], 1 + + .no_envs: + + call init_alloc xor rax, rax @@ -104,6 +147,7 @@ _start: lea rdi, [show_cursor] call print_str + .exit_program: mov rax, SYS_EXIT mov rdi, 0 ; return code syscall