diff --git a/src/drawing.asm b/src/drawing.asm index ce35471..e915ffd 100644 --- a/src/drawing.asm +++ b/src/drawing.asm @@ -14,6 +14,8 @@ section .bss simulation_running: RESB 1 next_frame_ptr: RESQ 1 +section .data + extern simulation_speed section .rodata clear: db ESC_CHAR, "[2J", 0 @@ -24,8 +26,9 @@ section .rodata home_cursor: db ESC_CHAR, "[H", 0 - statusbar: db ESC_CHAR, "[32;100m", "Use arrow keys to move cursor, enter to invert cell j/k to change simulation speed, p to simulation", 0 - START_STOP_pos: equ $-statusbar-17 + statusbar: db ESC_CHAR, "[32;100m", "Use arrow keys to move cursor, enter to invert cell j/k to change simulation speed, p to simulation. SPEED:", 0 + statusbarLen: equ $-statusbar + START_STOP_pos: equ statusbarLen-25 start_str: db "START", 0 @@ -49,6 +52,7 @@ extern string_copy extern memory_set extern memory_copy extern alloc +extern unsigned_int_to_ascii global init_gameboard init_gameboard: @@ -95,6 +99,7 @@ print_game_ui: push rdi add rdi, [gameboard_size] sub di, [term_cols] + push rdi add rdi, START_STOP_pos mov cl, [simulation_running] @@ -107,6 +112,34 @@ print_game_ui: .end_simulation_running_check: call string_copy + pop rdi + add rdi, statusbarLen + + movss xmm0, [simulation_speed] + + cvttss2si rsi, xmm0 + push rsi + push rdi + call unsigned_int_to_ascii + pop rdi + add rdi, rax + + mov byte [rdi], '.' + inc rdi + + pop rax + cvtsi2ss xmm1, rax + + subss xmm0, xmm1; get only value after decimal point + mov rax, 10 + cvtsi2ss xmm1, rax + + mulss xmm0, xmm1 + cvttss2si rsi, xmm0; get first decimal point as int + + call unsigned_int_to_ascii; rdi already set + + pop rdi call print_str diff --git a/src/input.asm b/src/input.asm index 761934f..4ef712f 100644 --- a/src/input.asm +++ b/src/input.asm @@ -15,6 +15,10 @@ section .bss section .data cursor_rows: dw 1 cursor_cols: dw 1 + + + global simulation_speed + simulation_speed dd 1.0 section .rodata extern reset @@ -25,6 +29,8 @@ section .rodata cursor_color: db ESC_CHAR, "[45m", 0 + speed_multiplier dd 50.0 + arrow_switch_statement: dq handle_user_input.arrow_up dq handle_user_input.arrow_down @@ -63,7 +69,10 @@ handle_user_input:; main loop of the program mov word [r12+4], POLLIN mov rdi, r12 mov rsi, 1; only one file descriptor is provided - mov rdx, 50; no timeout. maybe use this for final sleep but run if user inputs something TODO + movss xmm0, [speed_multiplier] + movss xmm1, [simulation_speed] + mulss xmm0, xmm1; callculate sleep lenght + cvttss2si rdx, xmm0; truncate and copy to rdx syscall test rax, rax; SYS_POLL returns 0 when no change happens within timeout @@ -179,15 +188,31 @@ handle_user_input:; main loop of the program cmp al, 'j' jne .check_k - ; TODO implement simulation speed + movss xmm0, [simulation_speed] + mov eax, 0x3DCCCCCD; 0.1f + vmovd xmm1, eax + + addss xmm0, xmm1 + ; wont check for overflows since the user would need be crazy to reach that number + movss [simulation_speed], xmm0 jmp .end_input_handling .check_k: cmp al, 'k' jne .check_q + + movss xmm0, [simulation_speed] + mov eax, 0x3DCCCCCD; 0.1f + vmovd xmm1, eax - ; TODO implement simulation speed + subss xmm0, xmm1 + + ucomiss xmm0, xmm1; check if number is smaller than 0.1 + jb .end_input_handling + movss [simulation_speed], xmm0 + + jmp .end_input_handling .check_q: cmp al, 'q' @@ -206,15 +231,6 @@ handle_user_input:; main loop of the program mov r13b, 1 .dont_step: - %ifdef COMMENT - lea rdi, [multipurpuse_buf] - mov qword [multipurpuse_buf], 0 - mov qword [multipurpuse_buf+8], 50000000; 50ms - mov rax, SYS_NANOSLEEP - mov rsi, 0 - syscall - %endif - test r13b, r13b jz .main_loop call print_game_ui diff --git a/src/main.asm b/src/main.asm index 26ad5a7..a594e38 100644 --- a/src/main.asm +++ b/src/main.asm @@ -95,6 +95,8 @@ _start: call disable_canonical_mode_and_echo + call print_game_ui + call handle_user_input call reset_terminal