// File: /workspace/github_c_makefile_star2_O2/salextpuru[P]speccy-toochain/src/spgbld/func.cxx
// Function: load_ini(char*)
void load_ini(char *name)
{
	char t[256];
	char v[256];
	char v1[256];
	char v2[256];
	int a = 0;
	int b = 0;
	int nblk = 0;
	FILE* f = fopen(name, "r");

	if (!f)
		error(INI);

	while (!feof(f)) {
		*t = 0;
		fscanf(f, "%256[^ =\n\r\t]%*[ =]%256[^\n\r]%*c", t, v);

		if (!*t) {
			fscanf(f, "%*c");
			continue;
		}

		if (t[0] == ';')
			continue;

		strlwr(t);

		// description string
		if (!strcmp(t, STR(F_DESC))) {
			strncpy(hdr.desc, v, 32);
			printf("Description:\t%s\n", v);
			continue;
		}

		// stack address
		if (!strcmp(t, STR(F_SP))) {
			hdr.sp = (u16)num(v);
			printf("SP:\t\t#%04X\n", hdr.sp);
			continue;
		}

		// resident address
		if (!strcmp(t, STR(F_RES))) {
			hdr.resid = (u16)num(v);
			printf("Resident at:\t#%04X\n", hdr.resid);
			continue;
		}

		// pager address
		if (!strcmp(t, STR(F_PAGER))) {
			hdr.pager = (u16)num(v);

			if (hdr.pager)
				printf("Pager at:\t#%04X\n", hdr.pager);

			else
				printf("Pager:\t\tNot used\n");

			continue;
		}

		// start address
		if (!strcmp(t, STR(F_START))) {
			hdr.start = (u16)num(v);
			printf("Start:\t\t#%04X\n", hdr.start);
			continue;
		}

		// page3
		if (!strcmp(t, STR(F_PAGE3))) {
			hdr.page3 = (u8)num(v);
			printf("Page #C000:\t#%02X\n", hdr.page3);
			continue;
		}

		// clock
		if (!strcmp(t, STR(F_CLOCK))) {
			hdr.clk = (u8)num(v);
			printf("Clock:\t\t");

			switch (hdr.clk) {
			case 0: {
				printf("3.5");
				break;
			}

			case 1: {
				printf("7");
				break;
			}

			case 2: {
				printf("14");
				break;
			}

			case 3: {
				printf("14+");
				break;
			}
			}

			printf(" MHz\n");
			continue;
		}

		// INT
		if (!strcmp(t, STR(F_INT))) {
			hdr.ei = num(v);
			printf("Interrupts:\t");

			if (hdr.ei)
				printf("EI\n");

			else
				printf("DI\n");

			continue;
		}

		// block
		if (!strcmp(t, STR(F_BLK))) {
			int size;
			int offset = 0;
			sscanf(v, "%[^ ,\t]%*[ ,\t]%[^ ,\t]%*[ ,\t]%s", v1, v2, v);
			a = num(v1);
			b = num(v2);

			if (a & 0x1FF)
				error(ALGN);

			if ((a < 0xC000) || (a > 0xFE00))
				error(ADDR);

			a -= 0xC000;
			printf("Block:\t\tStart: #%04X  Page: #%02X  File: %s\n", a, b, v);
			struct stat st;
			stat(v, &st);
			size = st.st_size;

			if (size < 0) {
				printf("%s: ", v);
				error(FNFD);
			}

			if (size == 0) {
				printf("%s: ", v);
				error(ZERO);
			}

			while (size) {
				int curr_size = min(size, 0x4000 - a);
				strncpy(blk[nblk].fname, v, sizeof(blk[nblk].fname));
				hdr.blk[nblk].addr = a >> 9;
				hdr.blk[nblk].page = b++;
				blk[nblk].offset = offset;
				blk[nblk].size = curr_size;
				a = 0;
				offset += curr_size;
				size -= curr_size;

				if (++nblk == 256)
					goto end_of_ini;
			}

			continue;
		}

		// unknown tag
		printf("%s: ", t);
		error(UNK);
	}

end_of_ini:

	if (!nblk)
		error(0BLK);

	conf.n_blocks = nblk;
	hdr.num_blk = conf.n_blocks;
	hdr.blk[conf.n_blocks - 1].last = 1;
	fclose(f);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x4b8, %rsp            # imm = 0x4B8
leaq 0x1aba(%rip), %rsi      # 0x40e7
callq 0x2150
testq %rax, %rax
je 0x2b54
movq %rax, %rbx
leaq 0x1a3a(%rip), %r14      # 0x407f
leaq 0x20(%rsp), %r15
leaq 0x120(%rsp), %r13
leaq 0x3b27(%rip), %r12      # 0x6180
leaq 0x1a66(%rip), %rbp      # 0x40c6
movl $0x0, 0x4(%rsp)
movq %rbx, %rdi
callq 0x2220
testl %eax, %eax
jne 0x2afd
movb $0x0, 0x20(%rsp)
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
movq %r13, %rcx
xorl %eax, %eax
callq 0x2270
movzbl 0x20(%rsp), %eax
cmpl $0x3b, %eax
je 0x2668
testl %eax, %eax
jne 0x26b1
movq %rbx, %rdi
leaq 0x19f2(%rip), %rsi      # 0x409a
xorl %eax, %eax
callq 0x2270
jmp 0x2668
movq %r15, %rdi
callq 0x239c
movq %r15, %rdi
leaq 0x19db(%rip), %rsi      # 0x409e
pushq $0x5
popq %rdx
callq 0x2130
testl %eax, %eax
je 0x27ca
movq %r15, %rdi
leaq 0x19d7(%rip), %rsi      # 0x40b4
pushq $0x6
popq %rdx
callq 0x2130
testl %eax, %eax
je 0x27ee
movq %r15, %rdi
movq %rbp, %rsi
pushq $0x9
popq %rdx
callq 0x2130
testl %eax, %eax
je 0x2808
movq %r15, %rdi
leaq 0x19d6(%rip), %rsi      # 0x40e3
pushq $0x6
popq %rbp
movq %rbp, %rdx
callq 0x2130
testl %eax, %eax
je 0x282c
movq %r15, %rdi
leaq 0x19d0(%rip), %rsi      # 0x40fa
movq %rbp, %rdx
callq 0x2130
testl %eax, %eax
je 0x284b
movq %r15, %rdi
leaq 0x19cb(%rip), %rsi      # 0x410f
movq %rbp, %rdx
callq 0x2130
testl %eax, %eax
je 0x2865
movq %r15, %rdi
leaq 0x19ca(%rip), %rsi      # 0x4128
movq %rbp, %rdx
callq 0x2130
testl %eax, %eax
je 0x28a7
cmpl $0x746e69, 0x20(%rsp)   # imm = 0x746E69
jne 0x28f3
movq %r13, %rdi
callq 0x25da
movb 0x35(%r12), %cl
shlb $0x2, %al
andb $0x4, %al
andb $-0x5, %cl
orb %al, %cl
movb %cl, 0x35(%r12)
leaq 0x19a3(%rip), %rdi      # 0x4142
xorl %eax, %eax
callq 0x2030
testb $0x4, 0x35(%r12)
leaq 0x1913(%rip), %rbp      # 0x40c6
jne 0x2ab3
leaq 0x1bd7(%rip), %rdi      # 0x4397
callq 0x2210
jmp 0x2668
movq %r12, %rdi
movq %r13, %rsi
pushq $0x20
popq %rdx
callq 0x2080
leaq 0x18c4(%rip), %rdi      # 0x40a3
movq %r13, %rsi
xorl %eax, %eax
callq 0x2030
jmp 0x2668
movq %r13, %rdi
callq 0x25da
movw %ax, 0x32(%r12)
movzwl %ax, %esi
leaq 0x18b4(%rip), %rdi      # 0x40ba
jmp 0x2820
movq %r13, %rdi
callq 0x25da
movw %ax, 0x38(%r12)
movzwl %ax, %esi
leaq 0x18af(%rip), %rdi      # 0x40cf
xorl %eax, %eax
callq 0x2030
jmp 0x2668
movq %r13, %rdi
callq 0x25da
movw %ax, 0x36(%r12)
testw %ax, %ax
je 0x288f
movzwl %ax, %esi
leaq 0x18a0(%rip), %rdi      # 0x40e9
jmp 0x287c
movq %r13, %rdi
callq 0x25da
movw %ax, 0x30(%r12)
movzwl %ax, %esi
leaq 0x189d(%rip), %rdi      # 0x4100
jmp 0x287c
movq %r13, %rdi
callq 0x25da
movb %al, 0x34(%r12)
movzbl %al, %esi
leaq 0x1899(%rip), %rdi      # 0x4115
xorl %eax, %eax
callq 0x2030
leaq 0x183c(%rip), %rbp      # 0x40c6
jmp 0x2668
leaq 0x1aeb(%rip), %rdi      # 0x4381
callq 0x2210
leaq 0x1824(%rip), %rbp      # 0x40c6
jmp 0x2668
movq %r13, %rdi
callq 0x25da
movb 0x35(%r12), %cl
andb $0x3, %al
andb $-0x4, %cl
orb %al, %cl
movb %cl, 0x35(%r12)
leaq 0x1867(%rip), %rdi      # 0x412e
xorl %eax, %eax
callq 0x2030
movzbl 0x35(%r12), %eax
andl $0x3, %eax
leaq 0x1726(%rip), %rcx      # 0x4004
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0x1849(%rip), %rdi      # 0x4137
jmp 0x2ad4
movq %r15, %rdi
leaq 0x1852(%rip), %rsi      # 0x414f
pushq $0x6
popq %rdx
callq 0x2130
testl %eax, %eax
jne 0x2b94
movq %r13, %rdi
leaq 0x183e(%rip), %rsi      # 0x4155
leaq 0x3b0(%rsp), %r14
movq %r14, %rdx
leaq 0x2b0(%rsp), %r15
movq %r15, %rcx
movq %r13, %r8
xorl %eax, %eax
callq 0x2110
movq %r14, %rdi
callq 0x25da
movq %rax, %r14
movq %r15, %rdi
callq 0x25da
testw $0x1ff, %r14w           # imm = 0x1FF
jne 0x2bc5
movq %rax, %r15
movq %r14, %rax
addq $-0xfe01, %rax          # imm = 0xFFFF01FF
cmpq $-0x3e02, %rax          # imm = 0xC1FE
jbe 0x2be3
addl $0xffff4000, %r14d      # imm = 0xFFFF4000
leaq 0x1847(%rip), %rdi      # 0x41c3
movl %r14d, %esi
movl %r15d, %edx
movq %r13, %rcx
xorl %eax, %eax
callq 0x2030
movq %r13, %rdi
leaq 0x220(%rsp), %rsi
callq 0x20e0
movl 0x250(%rsp), %r13d
testl %r13d, %r13d
js 0x2c01
movq %r15, 0x10(%rsp)
movq %rbx, 0x8(%rsp)
movl 0x4(%rsp), %ecx
je 0x2c38
movslq %ecx, %rdx
imulq $0x4408, %rdx, %r15     # imm = 0x4408
addq 0x360e(%rip), %r15      # 0x5fe0
leaq (%rdx,%rdx,2), %rax
leaq (%r12,%rax), %rbx
addq $0x100, %rbx            # imm = 0x100
incq %rdx
xorl %ebp, %ebp
movl %ecx, 0x4(%rsp)
testl %r13d, %r13d
je 0x2a87
movl $0x4000, %r12d          # imm = 0x4000
subl %r14d, %r12d
cmpl %r12d, %r13d
cmovll %r13d, %r12d
movq %rdx, 0x18(%rsp)
movl $0x400, %edx            # imm = 0x400
movq %r15, %rdi
leaq 0x120(%rsp), %rsi
callq 0x2080
shrl $0x9, %r14d
movzwl (%rbx), %eax
andl $0x1f, %r14d
andl $-0x20, %eax
orl %r14d, %eax
movw %ax, (%rbx)
movq 0x10(%rsp), %rdx
movb %dl, 0x2(%rbx)
movl %ebp, 0x400(%r15)
movl %r12d, 0x404(%r15)
addl %r12d, %ebp
subl %r12d, %r13d
movl 0x4(%rsp), %ecx
incl %ecx
addq $0x4408, %r15           # imm = 0x4408
addq $0x3, %rbx
movq 0x18(%rsp), %rsi
movq %rsi, %rax
incq %rax
incb %dl
movq %rdx, 0x10(%rsp)
xorl %r14d, %r14d
cmpl $0x100, %esi            # imm = 0x100
movq %rax, %rdx
jne 0x29e6
jmp 0x2b41
movq 0x8(%rsp), %rbx
leaq 0x36ed(%rip), %r12      # 0x6180
leaq 0x15e5(%rip), %r14      # 0x407f
leaq 0x20(%rsp), %r15
leaq 0x120(%rsp), %r13
leaq 0x1618(%rip), %rbp      # 0x40c6
jmp 0x2668
leaq 0x18e0(%rip), %rdi      # 0x439a
callq 0x2210
jmp 0x2668
leaq 0x1670(%rip), %rdi      # 0x413b
jmp 0x2ad4
leaq 0x166a(%rip), %rdi      # 0x413e
xorl %eax, %eax
callq 0x2030
jmp 0x2ae5
pushq $0x37
popq %rdi
callq 0x21c0
leaq 0x15da(%rip), %rbp      # 0x40c6
leaq 0x189f(%rip), %rdi      # 0x4392
callq 0x2210
jmp 0x2668
movl 0x4(%rsp), %ecx
testl %ecx, %ecx
je 0x2b72
leaq 0x444274(%rip), %rax    # 0x446d80
movl %ecx, 0x18(%rax)
movw %cx, 0x3a(%r12)
decl %ecx
movslq %ecx, %rax
leaq (%rax,%rax,2), %rax
orb $-0x80, 0x100(%r12,%rax)
movq %rbx, %rdi
callq 0x2120
addq $0x4b8, %rsp            # imm = 0x4B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x100, %ecx            # imm = 0x100
movq 0x8(%rsp), %rbx
leaq 0x362e(%rip), %r12      # 0x6180
jmp 0x2b05
leaq 0x1518(%rip), %rdi      # 0x4073
xorl %eax, %eax
callq 0x2030
leaq 0x1767(%rip), %rdi      # 0x42d0
callq 0x2210
pushq $0x2
jmp 0x2b8e
leaq 0x16ad(%rip), %rdi      # 0x4226
xorl %eax, %eax
callq 0x2030
leaq 0x1749(%rip), %rdi      # 0x42d0
callq 0x2210
pushq $0x8
popq %rdi
callq 0x2170
leaq 0x1655(%rip), %rdi      # 0x41f0
leaq 0x20(%rsp), %rsi
xorl %eax, %eax
callq 0x2030
leaq 0x166b(%rip), %rdi      # 0x4219
xorl %eax, %eax
callq 0x2030
leaq 0x1714(%rip), %rdi      # 0x42d0
callq 0x2210
pushq $0x3
jmp 0x2b8e
leaq 0x15a8(%rip), %rdi      # 0x4174
xorl %eax, %eax
callq 0x2030
leaq 0x16f6(%rip), %rdi      # 0x42d0
callq 0x2210
pushq $0x4
jmp 0x2b8e
leaq 0x15af(%rip), %rdi      # 0x4199
xorl %eax, %eax
callq 0x2030
leaq 0x16d8(%rip), %rdi      # 0x42d0
callq 0x2210
pushq $0xd
jmp 0x2b8e
leaq 0x15e8(%rip), %rdi      # 0x41f0
leaq 0x120(%rsp), %rsi
xorl %eax, %eax
callq 0x2030
leaq 0x15d7(%rip), %rdi      # 0x41f5
xorl %eax, %eax
callq 0x2030
leaq 0x16a4(%rip), %rdi      # 0x42d0
callq 0x2210
pushq $0x5
jmp 0x2b8e
leaq 0x15b1(%rip), %rdi      # 0x41f0
leaq 0x120(%rsp), %rsi
xorl %eax, %eax
callq 0x2030
leaq 0x15b0(%rip), %rdi      # 0x4205
xorl %eax, %eax
callq 0x2030
leaq 0x166d(%rip), %rdi      # 0x42d0
callq 0x2210
pushq $0xa
jmp 0x2b8e