// File: /workspace/github_c_makefile_star2_O0/mist64[P]cbmbasic/runtime.c
// Function: LOAD
static void
LOAD() {
		FILE *f;
		struct stat st;
		unsigned short start;
		unsigned short end;
		unsigned char savedbyte;

		if (A) {
			printf("UNIMPL: VERIFY\n");
			exit(1);
		}
		if (!kernal_filename_len)
			goto missing_file_name;

/* on special filename $ read directory entries and load they in the basic area memory */
		if( RAM[kernal_filename]=='$' ) {
			DIR *dirp;
			struct dirent *dp;
			int i, file_size;
			unsigned short old_memp, memp = 0x0801;  // TODO hack!

			old_memp = memp;
			memp += 2;
			RAM[memp++] = 0;
			RAM[memp++] = 0;
			RAM[memp++] = 0x12; /* REVERS ON */
			RAM[memp++] = '"';
			for(i=0; i<16; i++)
				RAM[memp+i] = ' ';
			if( (getcwd((char*)&RAM[memp], 256)) == NULL )
				goto device_not_present;
			memp += strlen((char*)&RAM[memp]); /* only 16 on COMMODORE DOS */
			RAM[memp++] = '"';
			RAM[memp++] = ' ';
			RAM[memp++] = '0';
			RAM[memp++] = '0';
			RAM[memp++] = ' ';
			RAM[memp++] = '2';
			RAM[memp++] = 'A';
			RAM[memp++] = 0;

			RAM[old_memp] = (memp) & 0xFF;
			RAM[old_memp+1] = (memp) >> 8;
			
			if ( !(dirp = opendir(".")) )
				goto device_not_present;
			while ((dp = readdir(dirp))) {
				size_t namlen = strlen(dp->d_name);
				stat(dp->d_name, &st);
				file_size = (st.st_size + 253)/254; /* convert file size from num of bytes to num of blocks(254 bytes) */
				if (file_size>0xFFFF)
					file_size = 0xFFFF;
				old_memp = memp;
				memp += 2;
				RAM[memp++] = file_size & 0xFF;
				RAM[memp++] = file_size >> 8;
				if (file_size<1000) {
					RAM[memp++] = ' ';
					if (file_size<100) {
						RAM[memp++] = ' ';
						if (file_size<10) {
							RAM[memp++] = ' ';
						}
					}
				}
				RAM[memp++] = '"';
				if (namlen>16)
					namlen=16; /* TODO hack */
				memcpy(&RAM[memp], dp->d_name, namlen);
				memp += namlen;
				RAM[memp++] = '"';
				for (i=namlen; i<16; i++)
					RAM[memp++] = ' ';
				RAM[memp++] = ' ';
				RAM[memp++] = 'P';
				RAM[memp++] = 'R';
				RAM[memp++] = 'G';
				RAM[memp++] = ' ';
				RAM[memp++] = ' ';
				RAM[memp++] = 0;

				RAM[old_memp] = (memp) & 0xFF;
				RAM[old_memp+1] = (memp) >> 8;
			}
			RAM[memp] = 0;
			RAM[memp+1] = 0;
			(void)closedir(dirp);
			end = memp + 2;
/*
for (i=0; i<255; i++) {
	if (!(i&15))
		printf("\n %04X  ", 0x0800+i);
	printf("%02X ", RAM[0x0800+i]);
}
*/
			goto load_noerr;
		} /* end if( RAM[kernal_filename]=='$' ) */

		savedbyte = RAM[kernal_filename+kernal_filename_len]; /* TODO possible overflow */
		RAM[kernal_filename+kernal_filename_len] = 0;

/* on directory filename chdir on it */
		if( (stat((char*)&RAM[kernal_filename], &st)) == -1 )
			goto file_not_found;
		if(S_ISDIR(st.st_mode)) {
			if( (chdir((char*)&RAM[kernal_filename])) == -1 )
				goto device_not_present;

			RAM[0x0801] = RAM[0x0802] = 0;
			end = 0x0803;
			goto load_noerr;
		}

/* on file load it read it and load in the basic area memory */
		f = fopen((char*)&RAM[kernal_filename], "rb");
		if (!f)
			goto file_not_found;
		start = ((unsigned char)fgetc(f)) | ((unsigned char)fgetc(f))<<8;
		if (!kernal_sec)
			start = X | Y<<8;
		end = start + fread(&RAM[start], 1, 65536-start, f); /* TODO may overwrite ROM */
		printf("LOADING FROM $%04X to $%04X\n", start, end);
		fclose(f);

load_noerr:
		X = end & 0xFF;
		Y = end >> 8;
		C = 0;
		A = KERN_ERR_NONE;
		return;
file_not_found:
		C = 1;
		A = KERN_ERR_FILE_NOT_FOUND;
		return;
device_not_present:
		C = 1;
		A = KERN_ERR_DEVICE_NOT_PRESENT;
		return;
missing_file_name:
		C = 1;
		A = KERN_ERR_MISSING_FILE_NAME;
		return;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xe0, %rsp
leaq 0x1dab3(%rip), %rax     # 0x5aab5
cmpb $0x0, (%rax)
je 0x3d01f
leaq 0x2af3(%rip), %rdi      # 0x3fb01
movb $0x0, %al
callq 0x10b0
movl $0x1, %edi
callq 0x11f0
cmpb $0x0, 0x1db96(%rip)     # 0x5abbc
jne 0x3d02d
jmp 0x3d963
movzwl 0x1db86(%rip), %eax     # 0x5abba
movl %eax, %ecx
leaq 0xda73(%rip), %rax      # 0x4aab0
movzbl (%rax,%rcx), %eax
cmpl $0x24, %eax
jne 0x3d735
movw $0x801, -0xbc(%rbp)     # imm = 0x801
movw -0xbc(%rbp), %ax
movw %ax, -0xba(%rbp)
movzwl -0xbc(%rbp), %eax
addl $0x2, %eax
movw %ax, -0xbc(%rbp)
movw -0xbc(%rbp), %ax
movw %ax, %cx
addw $0x1, %cx
movw %cx, -0xbc(%rbp)
movzwl %ax, %eax
movl %eax, %ecx
leaq 0xda1d(%rip), %rax      # 0x4aab0
movb $0x0, (%rax,%rcx)
movw -0xbc(%rbp), %ax
movw %ax, %cx
addw $0x1, %cx
movw %cx, -0xbc(%rbp)
movzwl %ax, %eax
movl %eax, %ecx
leaq 0xd9f8(%rip), %rax      # 0x4aab0
movb $0x0, (%rax,%rcx)
movw -0xbc(%rbp), %ax
movw %ax, %cx
addw $0x1, %cx
movw %cx, -0xbc(%rbp)
movzwl %ax, %eax
movl %eax, %ecx
leaq 0xd9d3(%rip), %rax      # 0x4aab0
movb $0x12, (%rax,%rcx)
movw -0xbc(%rbp), %ax
movw %ax, %cx
addw $0x1, %cx
movw %cx, -0xbc(%rbp)
movzwl %ax, %eax
movl %eax, %ecx
leaq 0xd9ae(%rip), %rax      # 0x4aab0
movb $0x22, (%rax,%rcx)
movl $0x0, -0xb4(%rbp)
cmpl $0x10, -0xb4(%rbp)
jge 0x3d145
movzwl -0xbc(%rbp), %eax
addl -0xb4(%rbp), %eax
movslq %eax, %rcx
leaq 0xd980(%rip), %rax      # 0x4aab0
movb $0x20, (%rax,%rcx)
movl -0xb4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb4(%rbp)
jmp 0x3d110
movzwl -0xbc(%rbp), %eax
leaq 0xd95d(%rip), %rdi      # 0x4aab0
addq %rax, %rdi
movl $0x100, %esi            # imm = 0x100
callq 0x10f0
cmpq $0x0, %rax
jne 0x3d16b
jmp 0x3d94d
movzwl -0xbc(%rbp), %eax
leaq 0xd937(%rip), %rdi      # 0x4aab0
addq %rax, %rdi
callq 0x1080
movq %rax, %rcx
movzwl -0xbc(%rbp), %eax
addq %rcx, %rax
movw %ax, -0xbc(%rbp)
movw -0xbc(%rbp), %ax
movw %ax, %cx
addw $0x1, %cx
movw %cx, -0xbc(%rbp)
movzwl %ax, %eax
movl %eax, %ecx
leaq 0xd8fa(%rip), %rax      # 0x4aab0
movb $0x22, (%rax,%rcx)
movw -0xbc(%rbp), %ax
movw %ax, %cx
addw $0x1, %cx
movw %cx, -0xbc(%rbp)
movzwl %ax, %eax
movl %eax, %ecx
leaq 0xd8d5(%rip), %rax      # 0x4aab0
movb $0x20, (%rax,%rcx)
movw -0xbc(%rbp), %ax
movw %ax, %cx
addw $0x1, %cx
movw %cx, -0xbc(%rbp)
movzwl %ax, %eax
movl %eax, %ecx
leaq 0xd8b0(%rip), %rax      # 0x4aab0
movb $0x30, (%rax,%rcx)
movw -0xbc(%rbp), %ax
movw %ax, %cx
addw $0x1, %cx
movw %cx, -0xbc(%rbp)
movzwl %ax, %eax
movl %eax, %ecx
leaq 0xd88b(%rip), %rax      # 0x4aab0
movb $0x30, (%rax,%rcx)
movw -0xbc(%rbp), %ax
movw %ax, %cx
addw $0x1, %cx
movw %cx, -0xbc(%rbp)
movzwl %ax, %eax
movl %eax, %ecx
leaq 0xd866(%rip), %rax      # 0x4aab0
movb $0x20, (%rax,%rcx)
movw -0xbc(%rbp), %ax
movw %ax, %cx
addw $0x1, %cx
movw %cx, -0xbc(%rbp)
movzwl %ax, %eax
movl %eax, %ecx
leaq 0xd841(%rip), %rax      # 0x4aab0
movb $0x32, (%rax,%rcx)
movw -0xbc(%rbp), %ax
movw %ax, %cx
addw $0x1, %cx
movw %cx, -0xbc(%rbp)
movzwl %ax, %eax
movl %eax, %ecx
leaq 0xd81c(%rip), %rax      # 0x4aab0
movb $0x41, (%rax,%rcx)
movw -0xbc(%rbp), %ax
movw %ax, %cx
addw $0x1, %cx
movw %cx, -0xbc(%rbp)
movzwl %ax, %eax
movl %eax, %ecx
leaq 0xd7f7(%rip), %rax      # 0x4aab0
movb $0x0, (%rax,%rcx)
movzwl -0xbc(%rbp), %eax
andl $0xff, %eax
movb %al, %dl
movzwl -0xba(%rbp), %eax
movl %eax, %ecx
leaq 0xd7d5(%rip), %rax      # 0x4aab0
movb %dl, (%rax,%rcx)
movzwl -0xbc(%rbp), %eax
sarl $0x8, %eax
movb %al, %dl
movzwl -0xba(%rbp), %eax
addl $0x1, %eax
movslq %eax, %rcx
leaq 0xd7b2(%rip), %rax      # 0x4aab0
movb %dl, (%rax,%rcx)
leaq 0x2809(%rip), %rdi      # 0x3fb11
callq 0x1070
movq %rax, -0xa8(%rbp)
cmpq $0x0, %rax
jne 0x3d31f
jmp 0x3d94d
jmp 0x3d321
movq -0xa8(%rbp), %rdi
callq 0x11a0
movq %rax, -0xb0(%rbp)
cmpq $0x0, %rax
je 0x3d6e7
movq -0xb0(%rbp), %rdi
addq $0x13, %rdi
callq 0x1080
movq %rax, -0xc8(%rbp)
movq -0xb0(%rbp), %rdi
addq $0x13, %rdi
leaq -0x98(%rbp), %rsi
callq 0x1160
movq -0x68(%rbp), %rax
addq $0xfd, %rax
movl $0xfe, %ecx
cqto
idivq %rcx
movl %eax, -0xb8(%rbp)
cmpl $0xffff, -0xb8(%rbp)    # imm = 0xFFFF
jle 0x3d39c
movl $0xffff, -0xb8(%rbp)    # imm = 0xFFFF
movw -0xbc(%rbp), %ax
movw %ax, -0xba(%rbp)
movzwl -0xbc(%rbp), %eax
addl $0x2, %eax
movw %ax, -0xbc(%rbp)
movl -0xb8(%rbp), %eax
andl $0xff, %eax
movb %al, %dl
movw -0xbc(%rbp), %ax
movw %ax, %cx
addw $0x1, %cx
movw %cx, -0xbc(%rbp)
movzwl %ax, %eax
movl %eax, %ecx
leaq 0xd6c7(%rip), %rax      # 0x4aab0
movb %dl, (%rax,%rcx)
movl -0xb8(%rbp), %eax
sarl $0x8, %eax
movb %al, %dl
movw -0xbc(%rbp), %ax
movw %ax, %cx
addw $0x1, %cx
movw %cx, -0xbc(%rbp)
movzwl %ax, %eax
movl %eax, %ecx
leaq 0xd698(%rip), %rax      # 0x4aab0
movb %dl, (%rax,%rcx)
cmpl $0x3e8, -0xb8(%rbp)     # imm = 0x3E8
jge 0x3d4b0
movw -0xbc(%rbp), %ax
movw %ax, %cx
addw $0x1, %cx
movw %cx, -0xbc(%rbp)
movzwl %ax, %eax
movl %eax, %ecx
leaq 0xd664(%rip), %rax      # 0x4aab0
movb $0x20, (%rax,%rcx)
cmpl $0x64, -0xb8(%rbp)
jge 0x3d4ae
movw -0xbc(%rbp), %ax
movw %ax, %cx
addw $0x1, %cx
movw %cx, -0xbc(%rbp)
movzwl %ax, %eax
movl %eax, %ecx
leaq 0xd636(%rip), %rax      # 0x4aab0
movb $0x20, (%rax,%rcx)
cmpl $0xa, -0xb8(%rbp)
jge 0x3d4ac
movw -0xbc(%rbp), %ax
movw %ax, %cx
addw $0x1, %cx
movw %cx, -0xbc(%rbp)
movzwl %ax, %eax
movl %eax, %ecx
leaq 0xd608(%rip), %rax      # 0x4aab0
movb $0x20, (%rax,%rcx)
jmp 0x3d4ae
jmp 0x3d4b0
movw -0xbc(%rbp), %ax
movw %ax, %cx
addw $0x1, %cx
movw %cx, -0xbc(%rbp)
movzwl %ax, %eax
movl %eax, %ecx
leaq 0xd5df(%rip), %rax      # 0x4aab0
movb $0x22, (%rax,%rcx)
cmpq $0x10, -0xc8(%rbp)
jbe 0x3d4ea
movq $0x10, -0xc8(%rbp)
movzwl -0xbc(%rbp), %eax
leaq 0xd5b8(%rip), %rdi      # 0x4aab0
addq %rax, %rdi
movq -0xb0(%rbp), %rsi
addq $0x13, %rsi
movq -0xc8(%rbp), %rdx
callq 0x1170
movq -0xc8(%rbp), %rcx
movzwl -0xbc(%rbp), %eax
addq %rcx, %rax
movw %ax, -0xbc(%rbp)
movw -0xbc(%rbp), %ax
movw %ax, %cx
addw $0x1, %cx
movw %cx, -0xbc(%rbp)
movzwl %ax, %eax
movl %eax, %ecx
leaq 0xd565(%rip), %rax      # 0x4aab0
movb $0x22, (%rax,%rcx)
movq -0xc8(%rbp), %rax
movl %eax, -0xb4(%rbp)
cmpl $0x10, -0xb4(%rbp)
jge 0x3d59b
movw -0xbc(%rbp), %ax
movw %ax, %cx
addw $0x1, %cx
movw %cx, -0xbc(%rbp)
movzwl %ax, %eax
movl %eax, %ecx
leaq 0xd52a(%rip), %rax      # 0x4aab0
movb $0x20, (%rax,%rcx)
movl -0xb4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb4(%rbp)
jmp 0x3d55c
movw -0xbc(%rbp), %ax
movw %ax, %cx
addw $0x1, %cx
movw %cx, -0xbc(%rbp)
movzwl %ax, %eax
movl %eax, %ecx
leaq 0xd4f4(%rip), %rax      # 0x4aab0
movb $0x20, (%rax,%rcx)
movw -0xbc(%rbp), %ax
movw %ax, %cx
addw $0x1, %cx
movw %cx, -0xbc(%rbp)
movzwl %ax, %eax
movl %eax, %ecx
leaq 0xd4cf(%rip), %rax      # 0x4aab0
movb $0x50, (%rax,%rcx)
movw -0xbc(%rbp), %ax
movw %ax, %cx
addw $0x1, %cx
movw %cx, -0xbc(%rbp)
movzwl %ax, %eax
movl %eax, %ecx
leaq 0xd4aa(%rip), %rax      # 0x4aab0
movb $0x52, (%rax,%rcx)
movw -0xbc(%rbp), %ax
movw %ax, %cx
addw $0x1, %cx
movw %cx, -0xbc(%rbp)
movzwl %ax, %eax
movl %eax, %ecx
leaq 0xd485(%rip), %rax      # 0x4aab0
movb $0x47, (%rax,%rcx)
movw -0xbc(%rbp), %ax
movw %ax, %cx
addw $0x1, %cx
movw %cx, -0xbc(%rbp)
movzwl %ax, %eax
movl %eax, %ecx
leaq 0xd460(%rip), %rax      # 0x4aab0
movb $0x20, (%rax,%rcx)
movw -0xbc(%rbp), %ax
movw %ax, %cx
addw $0x1, %cx
movw %cx, -0xbc(%rbp)
movzwl %ax, %eax
movl %eax, %ecx
leaq 0xd43b(%rip), %rax      # 0x4aab0
movb $0x20, (%rax,%rcx)
movw -0xbc(%rbp), %ax
movw %ax, %cx
addw $0x1, %cx
movw %cx, -0xbc(%rbp)
movzwl %ax, %eax
movl %eax, %ecx
leaq 0xd416(%rip), %rax      # 0x4aab0
movb $0x0, (%rax,%rcx)
movzwl -0xbc(%rbp), %eax
andl $0xff, %eax
movb %al, %dl
movzwl -0xba(%rbp), %eax
movl %eax, %ecx
leaq 0xd3f4(%rip), %rax      # 0x4aab0
movb %dl, (%rax,%rcx)
movzwl -0xbc(%rbp), %eax
sarl $0x8, %eax
movb %al, %dl
movzwl -0xba(%rbp), %eax
addl $0x1, %eax
movslq %eax, %rcx
leaq 0xd3d1(%rip), %rax      # 0x4aab0
movb %dl, (%rax,%rcx)
jmp 0x3d321
movzwl -0xbc(%rbp), %eax
movl %eax, %ecx
leaq 0xd3b9(%rip), %rax      # 0x4aab0
movb $0x0, (%rax,%rcx)
movzwl -0xbc(%rbp), %eax
addl $0x1, %eax
movslq %eax, %rcx
leaq 0xd3a1(%rip), %rax      # 0x4aab0
movb $0x0, (%rax,%rcx)
movq -0xa8(%rbp), %rdi
callq 0x1110
movzwl -0xbc(%rbp), %eax
addl $0x2, %eax
movw %ax, -0x9c(%rbp)
jmp 0x3d8f5
movzwl 0x1d47e(%rip), %eax     # 0x5abba
movzbl 0x1d479(%rip), %ecx     # 0x5abbc
addl %ecx, %eax
movslq %eax, %rcx
leaq 0xd361(%rip), %rax      # 0x4aab0
movb (%rax,%rcx), %al
movb %al, -0x9d(%rbp)
movzwl 0x1d45b(%rip), %eax     # 0x5abba
movzbl 0x1d456(%rip), %ecx     # 0x5abbc
addl %ecx, %eax
movslq %eax, %rcx
leaq 0xd33e(%rip), %rax      # 0x4aab0
movb $0x0, (%rax,%rcx)
movzwl 0x1d43d(%rip), %eax     # 0x5abba
leaq 0xd32c(%rip), %rdi      # 0x4aab0
addq %rax, %rdi
leaq -0x98(%rbp), %rsi
callq 0x1160
cmpl $-0x1, %eax
jne 0x3d79d
jmp 0x3d937
movl -0x80(%rbp), %eax
andl $0xf000, %eax           # imm = 0xF000
cmpl $0x4000, %eax           # imm = 0x4000
jne 0x3d7f6
movzwl 0x1d407(%rip), %eax     # 0x5abba
leaq 0xd2f6(%rip), %rdi      # 0x4aab0
addq %rax, %rdi
callq 0x1090
cmpl $-0x1, %eax
jne 0x3d7cc
jmp 0x3d94d
leaq 0xd2dd(%rip), %rax      # 0x4aab0
movb $0x0, 0x802(%rax)
leaq 0xd2cf(%rip), %rax      # 0x4aab0
movb $0x0, 0x801(%rax)
movw $0x803, -0x9c(%rbp)     # imm = 0x803
jmp 0x3d8f5
movzwl 0x1d3bd(%rip), %eax     # 0x5abba
leaq 0xd2ac(%rip), %rdi      # 0x4aab0
addq %rax, %rdi
leaq 0x2305(%rip), %rsi      # 0x3fb13
callq 0x11e0
movq %rax, -0x8(%rbp)
cmpq $0x0, -0x8(%rbp)
jne 0x3d823
jmp 0x3d937
movq -0x8(%rbp), %rdi
callq 0x1100
movzbl %al, %eax
movl %eax, -0xcc(%rbp)
movq -0x8(%rbp), %rdi
callq 0x1100
movl %eax, %ecx
movl -0xcc(%rbp), %eax
movzbl %cl, %ecx
shll $0x8, %ecx
orl %ecx, %eax
movw %ax, -0x9a(%rbp)
cmpb $0x0, 0x1d363(%rip)     # 0x5abbf
jne 0x3d87e
leaq 0x1d24d(%rip), %rax     # 0x5aab2
movzbl (%rax), %eax
leaq 0x1d249(%rip), %rcx     # 0x5aab8
movzbl (%rcx), %ecx
shll $0x8, %ecx
orl %ecx, %eax
movw %ax, -0x9a(%rbp)
movzwl -0x9a(%rbp), %eax
movq %rax, -0xd8(%rbp)
movzwl -0x9a(%rbp), %eax
leaq 0xd216(%rip), %rdi      # 0x4aab0
addq %rax, %rdi
movzwl -0x9a(%rbp), %ecx
movl $0x10000, %eax          # imm = 0x10000
subl %ecx, %eax
movslq %eax, %rdx
movq -0x8(%rbp), %rcx
movl $0x1, %esi
callq 0x1050
movq %rax, %rcx
movq -0xd8(%rbp), %rax
addq %rcx, %rax
movw %ax, -0x9c(%rbp)
movzwl -0x9a(%rbp), %esi
movzwl -0x9c(%rbp), %edx
leaq 0x2231(%rip), %rdi      # 0x3fb16
movb $0x0, %al
callq 0x10b0
movq -0x8(%rbp), %rdi
callq 0x1060
movzwl -0x9c(%rbp), %eax
andl $0xff, %eax
movb %al, %cl
leaq 0x1d1a8(%rip), %rax     # 0x5aab2
movb %cl, (%rax)
movzwl -0x9c(%rbp), %eax
sarl $0x8, %eax
movb %al, %cl
leaq 0x1d199(%rip), %rax     # 0x5aab8
movb %cl, (%rax)
leaq 0x1d18e(%rip), %rax     # 0x5aab6
movb $0x0, (%rax)
leaq 0x1d183(%rip), %rax     # 0x5aab5
movb $0x0, (%rax)
jmp 0x3d977
leaq 0x1d178(%rip), %rax     # 0x5aab6
movb $0x1, (%rax)
leaq 0x1d16d(%rip), %rax     # 0x5aab5
movb $0x4, (%rax)
jmp 0x3d977
leaq 0x1d162(%rip), %rax     # 0x5aab6
movb $0x1, (%rax)
leaq 0x1d157(%rip), %rax     # 0x5aab5
movb $0x5, (%rax)
jmp 0x3d977
leaq 0x1d14c(%rip), %rax     # 0x5aab6
movb $0x1, (%rax)
leaq 0x1d141(%rip), %rax     # 0x5aab5
movb $0x8, (%rax)
addq $0xe0, %rsp
popq %rbp
retq