// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/bsave.c
// Function: do_char_1screen
static void do_char_1screen(deark *c, lctx *d, struct de_char_screen *screen, i64 pgnum,
	i64 pg_offset_in_data, i64 width, i64 height)
{
	i64 i, j;
	unsigned int ch;
	u8 fgcol, bgcol;
	i64 offset;
	u8 b0, b1;
	struct de_encconv_state es;

	screen->width = width;
	screen->height = height;
	screen->cell_rows = de_mallocarray(c, height, sizeof(struct de_char_cell*));
	de_encconv_init(&es, DE_ENCODING_CP437_G);

	for(j=0; j<height; j++) {
		screen->cell_rows[j] = de_mallocarray(c, width, sizeof(struct de_char_cell));

		for(i=0; i<width; i++) {
			// 96 padding bytes per page?
			offset = BSAVE_HDRSIZE + pg_offset_in_data + j*(width*2) + i*2;

			b0 = de_getbyte(offset);
			b1 = de_getbyte(offset+1);

			ch = b0;
			//"The attribute byte stores the foreground color in the low nibble and the background color and blink attribute in the high nibble."
			//TODO: "blink" attribute?
			fgcol = (b1 & 0x0f);
			bgcol = (b1 & 0xf0) >> 4;

			screen->cell_rows[j][i].fgcol = (de_color)fgcol;
			screen->cell_rows[j][i].bgcol = (de_color)bgcol;
			screen->cell_rows[j][i].codepoint = (i32)ch;
			screen->cell_rows[j][i].codepoint_unicode = de_char_to_unicode_ex((i32)ch, &es);
		}
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq 0x10(%rbp), %rax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movq -0x30(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
movq 0x10(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x8(%rbp), %rdi
movq 0x10(%rbp), %rsi
movl $0x8, %edx
callq 0x15c450
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x10(%rax)
leaq -0x78(%rbp), %rdi
movl $0xd, %esi
callq 0x160150
movq $0x0, -0x40(%rbp)
movq -0x40(%rbp), %rax
cmpq 0x10(%rbp), %rax
jge 0x4cee6
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rsi
movl $0x14, %edx
callq 0x15c450
movq %rax, %rdx
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x40(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x30(%rbp), %rax
jge 0x4ced3
movq -0x28(%rbp), %rax
addq $0x7, %rax
movq -0x40(%rbp), %rcx
movq -0x30(%rbp), %rdx
shlq %rdx
imulq %rdx, %rcx
addq %rcx, %rax
movq -0x38(%rbp), %rcx
shlq %rcx
addq %rcx, %rax
movq %rax, -0x50(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x50(%rbp), %rsi
callq 0x165060
movb %al, -0x51(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x50(%rbp), %rsi
addq $0x1, %rsi
callq 0x165060
movb %al, -0x52(%rbp)
movzbl -0x51(%rbp), %eax
movl %eax, -0x44(%rbp)
movzbl -0x52(%rbp), %eax
andl $0xf, %eax
movb %al, -0x45(%rbp)
movzbl -0x52(%rbp), %eax
andl $0xf0, %eax
sarl $0x4, %eax
movb %al, -0x46(%rbp)
movzbl -0x45(%rbp), %ecx
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x40(%rbp), %rdx
movq (%rax,%rdx,8), %rax
imulq $0x14, -0x38(%rbp), %rdx
addq %rdx, %rax
movl %ecx, 0x8(%rax)
movzbl -0x46(%rbp), %ecx
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x40(%rbp), %rdx
movq (%rax,%rdx,8), %rax
imulq $0x14, -0x38(%rbp), %rdx
addq %rdx, %rax
movl %ecx, 0xc(%rax)
movl -0x44(%rbp), %ecx
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x40(%rbp), %rdx
movq (%rax,%rdx,8), %rax
imulq $0x14, -0x38(%rbp), %rdx
addq %rdx, %rax
movl %ecx, (%rax)
movl -0x44(%rbp), %edi
leaq -0x78(%rbp), %rsi
callq 0x160a30
movl %eax, %ecx
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x40(%rbp), %rdx
movq (%rax,%rdx,8), %rax
imulq $0x14, -0x38(%rbp), %rdx
addq %rdx, %rax
movl %ecx, 0x4(%rax)
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x4cdbc
jmp 0x4ced5
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x4cd81
addq $0x80, %rsp
popq %rbp
retq
nop