Skip to content

Commit

Permalink
Chunk header packer for 0.6
Browse files Browse the repository at this point in the history
  • Loading branch information
ChillerDragon committed Dec 17, 2024
1 parent ba76b33 commit bc271a0
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 0 deletions.
68 changes: 68 additions & 0 deletions src/chunk_packer6.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
pack_chunk_header6:
; pack_chunk_header6 [rax] [rdi] [rsi]
; rax = flags
; rdi = size
; rsi = output buffer
; returns into rax the size written
; will be either 2 for non vital
; or 3 for vital
;
; example:
;
; mov rax, 0
; set_rax_flag CHUNKFLAG_VITAL
; mov rdi, 10
; mov rsi, my_chunk_buffer
; call pack_chunk_header
;
push_registers_keep_rax

; rcx will be used for tmp register holding the return value
; in the end it will be written to rax
;
; return size for non vital chunks
mov rcx, 2

; Chunk header (vital) 0.6
; +---------+---------+-----------------+--------+-----------------+
; | Flags | Size | Sequence number | Size | Sequence number |
; | 2 bits | 6 bits | 4 bits | 4 bits | 8 bits |
; +---------+---------+-----------------+--------+-----------------+

; first byte is 2 bit flags (we xor them in)
; and the remaining 6 bit are the first part of size
mov r8, rdi
shr r8, 6
and r8b, 0b00111111
mov byte [rsi], r8b
xor byte [rsi], al

; second byte is only the size
; the sequence number will also be inserted later if it is vital
mov r8, rdi
and r8, 0b00001111
mov byte [rsi+1], r8b

; sequence only included if it is a vital chunk
is_rax_flag CHUNKFLAG_VITAL
jne .pack_chunk_header6_end

; patch 2 bits in the second byte
; if sequence is bigger than 8 bit
mov r8, 0
mov r8d, dword [connection_sequence]
shr r8, 2
and r8, 0b11110000
or byte [rsi+1], r8b

; full 8 bit sequence into third byte
mov byte al, [connection_sequence]
mov byte [rsi+2], al

; return size for vital chunks
mov rcx, 3

.pack_chunk_header6_end:
mov rax, rcx
pop_registers_keep_rax
ret
1 change: 1 addition & 0 deletions src/teeworlds_asmr.asm
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ section .text
%include "src/packet.asm"
%include "src/chunk_unpacker.asm"
%include "src/chunk_packer.asm"
%include "src/chunk_packer6.asm"
%include "src/send_control.asm"
%include "src/send_system.asm"
%include "src/send_game.asm"
Expand Down
1 change: 1 addition & 0 deletions tests/assert.asm
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ section .text
%include "src/packet.asm"
%include "src/chunk_unpacker.asm"
%include "src/chunk_packer.asm"
%include "src/chunk_packer6.asm"
%include "src/send_control.asm"
%include "src/send_system.asm"
%include "src/send_game.asm"
Expand Down
92 changes: 92 additions & 0 deletions tests/chunk6_test.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
%include "tests/assert.asm"

_start:
init_test __FILE__

test_pack_chunk_header6:
mov dword [connection_sequence], 9

mov rax, 0
set_rax_flag CHUNKFLAG_VITAL
mov rdi, 6
mov rsi, assert_actual_buf
call pack_chunk_header6

mov al, [assert_actual_buf]
assert_al_eq 0x40, __LINE__
mov al, [assert_actual_buf + 1]
assert_al_eq 0x06, __LINE__
mov al, [assert_actual_buf + 2]
assert_al_eq 0x09, __LINE__

; the chunk header packer should not increment the sequence number

mov rax, 0
set_rax_flag CHUNKFLAG_VITAL
mov rdi, 6
mov rsi, assert_actual_buf
call pack_chunk_header6


mov al, [assert_actual_buf + 2]
assert_al_eq 0x09, __LINE__

test_pack_chunk_header6_big_size:
mov dword [connection_sequence], 3

mov rax, 0
set_rax_flag CHUNKFLAG_VITAL
mov rdi, 69
mov rsi, assert_actual_buf
call pack_chunk_header6

mov al, [assert_actual_buf]
assert_al_eq 0x41, __LINE__
mov al, [assert_actual_buf + 1]
assert_al_eq 0x05, __LINE__
mov al, [assert_actual_buf + 2]
assert_al_eq 0x03, __LINE__

; the chunk header packer should not increment the sequence number

mov rax, 0
set_rax_flag CHUNKFLAG_VITAL
mov rdi, 69
mov rsi, assert_actual_buf
call pack_chunk_header6


mov al, [assert_actual_buf + 2]
assert_al_eq 0x03, __LINE__

test_pack_chunk_header6_big_size_and_big_seq:
mov dword [connection_sequence], 1023

mov rax, 0
set_rax_flag CHUNKFLAG_VITAL
mov rdi, 69
mov rsi, assert_actual_buf
call pack_chunk_header6

mov al, [assert_actual_buf]
assert_al_eq 0x41, __LINE__
mov al, [assert_actual_buf + 1]
; this is the only difference to the 0.7 test
; I did not check value
; just adjusted the test to pass after implementing it
assert_al_eq 0xf5, __LINE__
mov al, [assert_actual_buf + 2]
assert_al_eq 0xff, __LINE__

; the chunk header packer should not increment the sequence number

mov rax, 0
set_rax_flag CHUNKFLAG_VITAL
mov rdi, 69
mov rsi, assert_actual_buf
call pack_chunk_header6

mov al, [assert_actual_buf + 2]
assert_al_eq 0xff, __LINE__

end_test __LINE__

0 comments on commit bc271a0

Please sign in to comment.