// File: mdpkh[P]retro6k/src/main.cpp
// Function: PaintCell(unsigned char, unsigned char, unsigned char, unsigned char*)
void PaintCell(unsigned char col, unsigned char row, unsigned char glyph, unsigned char* att)
{
	unsigned char* faddr = sysram;
	faddr += 0x1000 | (glyph << 4);
	unsigned char* paddr = sysram;
	paddr += 0x0FE0;
	for (int gy = 0, cy = 0, sy = row << 5; gy < 8; ++gy)
	{
		unsigned char pix[8];
		pix[0] = paddr[att[(*faddr & 0x3)]];
		pix[1] = paddr[att[(*faddr & 0xC) >> 2]];
		pix[2] = paddr[att[(*faddr & 0x30) >> 4]];
		pix[3] = paddr[att[(*faddr & 0xC0) >> 6]];
		faddr++;
		pix[4] = paddr[att[(*faddr & 0x3)]];
		pix[5] = paddr[att[(*faddr & 0xC) >> 2]];
		pix[6] = paddr[att[(*faddr & 0x30) >> 4]];
		pix[7] = paddr[att[(*faddr & 0xC0) >> 6]];
		faddr++;
		for (int ry = 0; ry < config.screen.pixheight; ++ry, ++cy)
		{
			unsigned char* dst = (unsigned char*)cellcanvas->pixels + (long long)cellcanvas->pitch * cy;
			for (int gx = 0, cx = 0; gx < 8; ++gx)
			{
				for (int rx = 0; rx < config.screen.pixwidth; ++rx, ++dst)
				{
					*dst = pix[gx];
				}
			}
		}
		scanlinedirty[sy++] = true;
		scanlinedirty[sy++] = true;
		scanlinedirty[sy++] = true;
		scanlinedirty[sy++] = true;
	}
	SDL_Rect dstrect;
	dstrect.x = col * config.screen.pixwidth * 8;
	dstrect.y = row * config.screen.pixheight * 8;
	SDL_BlitSurface(cellcanvas, nullptr, framebuffer, &dstrect);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movb %dl, %al
movb %sil, %dl
movb %dil, %sil
movb %sil, -0x1(%rbp)
movb %dl, -0x2(%rbp)
movb %al, -0x3(%rbp)
movq %rcx, -0x10(%rbp)
movq 0x1d9bb(%rip), %rax     # 0x2d560
movq %rax, -0x18(%rbp)
movzbl -0x3(%rbp), %ecx
shll $0x4, %ecx
orl $0x1000, %ecx           # imm = 0x1000
movq -0x18(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x18(%rbp)
movq 0x1d995(%rip), %rax     # 0x2d560
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
addq $0xfe0, %rax            # imm = 0xFE0
movq %rax, -0x20(%rbp)
movl $0x0, -0x24(%rbp)
movl $0x0, -0x28(%rbp)
movzbl -0x2(%rbp), %eax
shll $0x5, %eax
movl %eax, -0x2c(%rbp)
cmpl $0x8, -0x24(%rbp)
jge 0xfe45
movq -0x20(%rbp), %rax
movq -0x10(%rbp), %rcx
movq -0x18(%rbp), %rdx
movzbl (%rdx), %edx
andl $0x3, %edx
movslq %edx, %rdx
movzbl (%rcx,%rdx), %ecx
movb (%rax,%rcx), %al
movb %al, -0x34(%rbp)
movq -0x20(%rbp), %rax
movq -0x10(%rbp), %rcx
movq -0x18(%rbp), %rdx
movzbl (%rdx), %edx
andl $0xc, %edx
sarl $0x2, %edx
movslq %edx, %rdx
movzbl (%rcx,%rdx), %ecx
movb (%rax,%rcx), %al
movb %al, -0x33(%rbp)
movq -0x20(%rbp), %rax
movq -0x10(%rbp), %rcx
movq -0x18(%rbp), %rdx
movzbl (%rdx), %edx
andl $0x30, %edx
sarl $0x4, %edx
movslq %edx, %rdx
movzbl (%rcx,%rdx), %ecx
movb (%rax,%rcx), %al
movb %al, -0x32(%rbp)
movq -0x20(%rbp), %rax
movq -0x10(%rbp), %rcx
movq -0x18(%rbp), %rdx
movzbl (%rdx), %edx
andl $0xc0, %edx
sarl $0x6, %edx
movslq %edx, %rdx
movzbl (%rcx,%rdx), %ecx
movb (%rax,%rcx), %al
movb %al, -0x31(%rbp)
movq -0x18(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x18(%rbp)
movq -0x20(%rbp), %rax
movq -0x10(%rbp), %rcx
movq -0x18(%rbp), %rdx
movzbl (%rdx), %edx
andl $0x3, %edx
movslq %edx, %rdx
movzbl (%rcx,%rdx), %ecx
movb (%rax,%rcx), %al
movb %al, -0x30(%rbp)
movq -0x20(%rbp), %rax
movq -0x10(%rbp), %rcx
movq -0x18(%rbp), %rdx
movzbl (%rdx), %edx
andl $0xc, %edx
sarl $0x2, %edx
movslq %edx, %rdx
movzbl (%rcx,%rdx), %ecx
movb (%rax,%rcx), %al
movb %al, -0x2f(%rbp)
movq -0x20(%rbp), %rax
movq -0x10(%rbp), %rcx
movq -0x18(%rbp), %rdx
movzbl (%rdx), %edx
andl $0x30, %edx
sarl $0x4, %edx
movslq %edx, %rdx
movzbl (%rcx,%rdx), %ecx
movb (%rax,%rcx), %al
movb %al, -0x2e(%rbp)
movq -0x20(%rbp), %rax
movq -0x10(%rbp), %rcx
movq -0x18(%rbp), %rdx
movzbl (%rdx), %edx
andl $0xc0, %edx
sarl $0x6, %edx
movslq %edx, %rdx
movzbl (%rcx,%rdx), %ecx
movb (%rax,%rcx), %al
movb %al, -0x2d(%rbp)
movq -0x18(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x18(%rbp)
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl 0x1d81d(%rip), %eax     # 0x2d554
jge 0xfdd3
movq 0x8216ac(%rip), %rax    # 0x8313f0
movq 0x20(%rax), %rax
movq 0x8216a1(%rip), %rcx    # 0x8313f0
movslq 0x18(%rcx), %rcx
movslq -0x28(%rbp), %rdx
imulq %rdx, %rcx
addq %rcx, %rax
movq %rax, -0x40(%rbp)
movl $0x0, -0x44(%rbp)
movl $0x0, -0x48(%rbp)
cmpl $0x8, -0x44(%rbp)
jge 0xfdba
movl $0x0, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
cmpl 0x1d7ca(%rip), %eax     # 0x2d550
jge 0xfdad
movslq -0x44(%rbp), %rax
movb -0x34(%rbp,%rax), %cl
movq -0x40(%rbp), %rax
movb %cl, (%rax)
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0xfd7d
jmp 0xfdaf
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0xfd70
jmp 0xfdbc
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0xfd2e
movl -0x2c(%rbp), %eax
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, -0x2c(%rbp)
movslq %eax, %rcx
leaq 0x1d798(%rip), %rax     # 0x2d580
movb $0x1, (%rax,%rcx)
movl -0x2c(%rbp), %eax
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, -0x2c(%rbp)
movslq %eax, %rcx
leaq 0x1d77f(%rip), %rax     # 0x2d580
movb $0x1, (%rax,%rcx)
movl -0x2c(%rbp), %eax
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, -0x2c(%rbp)
movslq %eax, %rcx
leaq 0x1d766(%rip), %rax     # 0x2d580
movb $0x1, (%rax,%rcx)
movl -0x2c(%rbp), %eax
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, -0x2c(%rbp)
movslq %eax, %rcx
leaq 0x1d74d(%rip), %rax     # 0x2d580
movb $0x1, (%rax,%rcx)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0xfbf5
movzbl -0x1(%rbp), %eax
imull 0x1d700(%rip), %eax     # 0x2d550
shll $0x3, %eax
movl %eax, -0x5c(%rbp)
movzbl -0x2(%rbp), %eax
imull 0x1d6f3(%rip), %eax     # 0x2d554
shll $0x3, %eax
movl %eax, -0x58(%rbp)
movq 0x821582(%rip), %rdi    # 0x8313f0
movq 0x821583(%rip), %rdx    # 0x8313f8
xorl %eax, %eax
movl %eax, %esi
leaq -0x5c(%rbp), %rcx
callq 0x73b0
addq $0x60, %rsp
popq %rbp
retq
nopl (%rax,%rax)