// File: /workspace/github_c_makefile_star2_O0/aap[P]pdp11/rk11.c
// Function: svc_rk05
void
svc_rk05(RK05 *drive, RK11 *rk, int selected)
{
	if(drive->fp == nil){
		reset_rk05(drive);
		return;
	}

	drive->rkds |= DRY | SOK;

	// movement
	// TODO: do some actual timing here?

	// move head (seek)
	drive->rkds &= ~ARDY;
	if(drive->cyl != drive->newcyl){
		if(drive->cyl_timer)
			drive->cyl_timer--;
		if(drive->cyl_timer == 0){
			if(drive->cyl < drive->newcyl)
				drive->cyl++;
			else
				drive->cyl--;
			// some fake value, should be around 640us
			drive->cyl_timer = 500;	
		}

		// search complete
		if(drive->cyl == drive->newcyl &&
		   drive->func == FR_SEEK){
			rk->rkds = (drive-rk->drives)<<13;
			rk->rkcs |= SCP;
			drive->func = 0;
			rk->done = 1;
		}
	}else if(drive->func == 0)
		drive->rkds |= ARDY;

	drive->surf = (rk->rkda>>4)&1;

	// transfer data to/from sector we're interested in
	if(selected && ISXFER(drive->func) &&
	   drive->cyl == drive->newcyl && (rk->rkda&017) == drive->sc){

		// beginning of new sector
		if(drive->wc == 0){
			int pos = blockaddr(drive->cyl, drive->surf, drive->sc)*512;
			fseek(drive->fp, pos, 0);
			fread(rk->buf, 1, 512, drive->fp);
			// rewind so we can write back easily
			fseek(drive->fp, pos, 0);

			rk->state = 1;
		}

		// transfer one word
		if(rk->state == 1 && rk->rkwc){
			rk->rkwc++;

			if(drive->func == FR_READ){
				drive->dsb = WD(rk->buf[drive->wc*2+1], rk->buf[drive->wc*2]);
				rk->rkdb = drive->dsb;

				// write to bus
				rk->bus->data = rk->rkdb;
				rk->bus->addr = busaddr(rk);
				if(dato_bus(rk->bus))
					rkerr(drive, rk, NXM);
			}else if(drive->func == FR_WRITE ||
				 drive->func == FR_WRITE_CHECK){

				// read from bus
				rk->bus->addr = busaddr(rk);
				if(dati_bus(rk->bus))
					rkerr(drive, rk, NXM);
				rk->rkdb = rk->bus->data;

				drive->dsb = rk->rkdb;
				rk->buf[drive->wc*2] = drive->dsb;
				rk->buf[drive->wc*2+1] = drive->dsb>>8;
			}

			// transfer finished
			if(rk->rkwc == 0)
				// have to keep the function around till
				// the end of the block
				rk->done = 1;
		}
	}

	// rotate
	drive->wc++;
	if(drive->wc >= 256){

		// end of sector
		drive->wc = 0;

		// end of current sector
		if(selected && ISXFER(drive->func) &&
		   drive->cyl == drive->newcyl && (rk->rkda&017) == drive->sc){

			if(rk->state == 1){
				count(drive, rk);

				// write back
				if(drive->func == FR_WRITE ||
				   drive->func == FR_WRITE_CHECK)
					fwrite(rk->buf, 1, 512, drive->fp);
				rk->state = 0;
			}

			// end of transfer
			if(rk->rkwc == 0){
				rk->rkcs |= RDY;
				drive->func = 0;
			}
		}

		drive->rkds &= ~SOK;
		drive->sc++;
		// should be one revolution every 40ms
		if(drive->sc >= 12)
			drive->sc = 0;
	}
	drive->rkds &= ~017;
	drive->rkds |= drive->sc;
	if((rk->rkda&017) == drive->sc)
		drive->rkds |= SC_EQ_SA;
	else
		drive->rkds &= ~SC_EQ_SA;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x20, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq -0x8(%rbp), %rax
cmpq $0x0, (%rax)
jne 0x8d4b
movq -0x8(%rbp), %rdi
callq 0x8b30
jmp 0x9281
movq -0x8(%rbp), %rax
movzwl 0xc(%rax), %ecx
orl $0x180, %ecx            # imm = 0x180
movw %cx, 0xc(%rax)
movq -0x8(%rbp), %rax
movzwl 0xc(%rax), %ecx
andl $-0x41, %ecx
movw %cx, 0xc(%rax)
movq -0x8(%rbp), %rax
movl 0x1c(%rax), %eax
movq -0x8(%rbp), %rcx
cmpl 0x20(%rcx), %eax
je 0x8e44
movq -0x8(%rbp), %rax
cmpl $0x0, 0x14(%rax)
je 0x8d97
movq -0x8(%rbp), %rax
movl 0x14(%rax), %ecx
addl $-0x1, %ecx
movl %ecx, 0x14(%rax)
movq -0x8(%rbp), %rax
cmpl $0x0, 0x14(%rax)
jne 0x8dd8
movq -0x8(%rbp), %rax
movl 0x1c(%rax), %eax
movq -0x8(%rbp), %rcx
cmpl 0x20(%rcx), %eax
jge 0x8dc0
movq -0x8(%rbp), %rax
movl 0x1c(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x1c(%rax)
jmp 0x8dcd
movq -0x8(%rbp), %rax
movl 0x1c(%rax), %ecx
addl $-0x1, %ecx
movl %ecx, 0x1c(%rax)
movq -0x8(%rbp), %rax
movl $0x1f4, 0x14(%rax)      # imm = 0x1F4
movq -0x8(%rbp), %rax
movl 0x1c(%rax), %eax
movq -0x8(%rbp), %rcx
cmpl 0x20(%rcx), %eax
jne 0x8e42
movq -0x8(%rbp), %rax
cmpl $0x4, 0x10(%rax)
jne 0x8e42
movq -0x8(%rbp), %rax
movq -0x10(%rbp), %rcx
addq $0x28, %rcx
subq %rcx, %rax
movl $0x30, %ecx
cqto
idivq %rcx
shlq $0xd, %rax
movw %ax, %cx
movq -0x10(%rbp), %rax
movw %cx, 0x8(%rax)
movq -0x10(%rbp), %rax
movzwl 0xc(%rax), %ecx
orl $0x2000, %ecx           # imm = 0x2000
movw %cx, 0xc(%rax)
movq -0x8(%rbp), %rax
movl $0x0, 0x10(%rax)
movq -0x10(%rbp), %rax
movl $0x1, 0x1c(%rax)
jmp 0x8e5f
movq -0x8(%rbp), %rax
cmpl $0x0, 0x10(%rax)
jne 0x8e5d
movq -0x8(%rbp), %rax
movzwl 0xc(%rax), %ecx
orl $0x40, %ecx
movw %cx, 0xc(%rax)
jmp 0x8e5f
movq -0x10(%rbp), %rax
movzwl 0x12(%rax), %ecx
sarl $0x4, %ecx
andl $0x1, %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0x24(%rax)
cmpl $0x0, -0x14(%rbp)
je 0x90f7
movq -0x8(%rbp), %rax
movl 0x10(%rax), %ecx
movl $0x2e, %eax
sarl %cl, %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x90f7
movq -0x8(%rbp), %rax
movl 0x1c(%rax), %eax
movq -0x8(%rbp), %rcx
cmpl 0x20(%rcx), %eax
jne 0x90f7
movq -0x10(%rbp), %rax
movzwl 0x12(%rax), %eax
andl $0xf, %eax
movq -0x8(%rbp), %rcx
cmpl 0x18(%rcx), %eax
jne 0x90f7
movq -0x8(%rbp), %rax
cmpl $0x0, 0x28(%rax)
jne 0x8f3e
movq -0x8(%rbp), %rax
movl 0x1c(%rax), %edi
movq -0x8(%rbp), %rax
movl 0x24(%rax), %esi
movq -0x8(%rbp), %rax
movl 0x18(%rax), %edx
callq 0x9440
shll $0x9, %eax
movl %eax, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rdi
movslq -0x18(%rbp), %rsi
xorl %edx, %edx
callq 0x2170
movq -0x10(%rbp), %rdi
addq $0x1a8, %rdi            # imm = 0x1A8
movq -0x8(%rbp), %rax
movq (%rax), %rcx
movl $0x1, %esi
movl $0x200, %edx            # imm = 0x200
callq 0x2040
movq -0x8(%rbp), %rax
movq (%rax), %rdi
movslq -0x18(%rbp), %rsi
xorl %edx, %edx
callq 0x2170
movq -0x10(%rbp), %rax
movl $0x1, 0x18(%rax)
movq -0x10(%rbp), %rax
cmpl $0x1, 0x18(%rax)
jne 0x90f5
movq -0x10(%rbp), %rax
movzwl 0xe(%rax), %eax
cmpl $0x0, %eax
je 0x90f5
movq -0x10(%rbp), %rax
movw 0xe(%rax), %cx
addw $0x1, %cx
movw %cx, 0xe(%rax)
movq -0x8(%rbp), %rax
cmpl $0x2, 0x10(%rax)
jne 0x901e
movq -0x10(%rbp), %rax
movq -0x8(%rbp), %rcx
movl 0x28(%rcx), %ecx
shll %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movzbl 0x1a8(%rax,%rcx), %eax
shll $0x8, %eax
movq -0x10(%rbp), %rcx
movq -0x8(%rbp), %rdx
movl 0x28(%rdx), %edx
shll %edx
movslq %edx, %rdx
movzbl 0x1a8(%rcx,%rdx), %ecx
orl %ecx, %eax
movw %ax, %cx
movq -0x8(%rbp), %rax
movw %cx, 0x2c(%rax)
movq -0x8(%rbp), %rax
movw 0x2c(%rax), %cx
movq -0x10(%rbp), %rax
movw %cx, 0x16(%rax)
movq -0x10(%rbp), %rax
movw 0x16(%rax), %cx
movq -0x10(%rbp), %rax
movq (%rax), %rax
movw %cx, 0xc(%rax)
movq -0x10(%rbp), %rdi
callq 0x9460
movl %eax, %ecx
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl %ecx, 0x8(%rax)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x2410
cmpl $0x0, %eax
je 0x9019
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl $0x400, %edx            # imm = 0x400
callq 0x8ad0
jmp 0x90db
movq -0x8(%rbp), %rax
cmpl $0x1, 0x10(%rax)
je 0x9036
movq -0x8(%rbp), %rax
cmpl $0x3, 0x10(%rax)
jne 0x90d9
movq -0x10(%rbp), %rdi
callq 0x9460
movl %eax, %ecx
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl %ecx, 0x8(%rax)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x23a0
cmpl $0x0, %eax
je 0x906e
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl $0x400, %edx            # imm = 0x400
callq 0x8ad0
movq -0x10(%rbp), %rax
movq (%rax), %rax
movw 0xc(%rax), %cx
movq -0x10(%rbp), %rax
movw %cx, 0x16(%rax)
movq -0x10(%rbp), %rax
movw 0x16(%rax), %cx
movq -0x8(%rbp), %rax
movw %cx, 0x2c(%rax)
movq -0x8(%rbp), %rax
movw 0x2c(%rax), %ax
movb %al, %dl
movq -0x10(%rbp), %rax
movq -0x8(%rbp), %rcx
movl 0x28(%rcx), %ecx
shll %ecx
movslq %ecx, %rcx
movb %dl, 0x1a8(%rax,%rcx)
movq -0x8(%rbp), %rax
movzwl 0x2c(%rax), %eax
sarl $0x8, %eax
movb %al, %dl
movq -0x10(%rbp), %rax
movq -0x8(%rbp), %rcx
movl 0x28(%rcx), %ecx
shll %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movb %dl, 0x1a8(%rax,%rcx)
jmp 0x90db
movq -0x10(%rbp), %rax
movzwl 0xe(%rax), %eax
cmpl $0x0, %eax
jne 0x90f3
movq -0x10(%rbp), %rax
movl $0x1, 0x1c(%rax)
jmp 0x90f5
jmp 0x90f7
movq -0x8(%rbp), %rax
movl 0x28(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x28(%rax)
movq -0x8(%rbp), %rax
cmpl $0x100, 0x28(%rax)      # imm = 0x100
jl 0x9229
movq -0x8(%rbp), %rax
movl $0x0, 0x28(%rax)
cmpl $0x0, -0x14(%rbp)
je 0x91f3
movq -0x8(%rbp), %rax
movl 0x10(%rax), %ecx
movl $0x2e, %eax
sarl %cl, %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x91f3
movq -0x8(%rbp), %rax
movl 0x1c(%rax), %eax
movq -0x8(%rbp), %rcx
cmpl 0x20(%rcx), %eax
jne 0x91f3
movq -0x10(%rbp), %rax
movzwl 0x12(%rax), %eax
andl $0xf, %eax
movq -0x8(%rbp), %rcx
cmpl 0x18(%rcx), %eax
jne 0x91f3
movq -0x10(%rbp), %rax
cmpl $0x1, 0x18(%rax)
jne 0x91c7
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x94e0
movq -0x8(%rbp), %rax
cmpl $0x1, 0x10(%rax)
je 0x919b
movq -0x8(%rbp), %rax
cmpl $0x3, 0x10(%rax)
jne 0x91bc
movq -0x10(%rbp), %rdi
addq $0x1a8, %rdi            # imm = 0x1A8
movq -0x8(%rbp), %rax
movq (%rax), %rcx
movl $0x1, %esi
movl $0x200, %edx            # imm = 0x200
callq 0x2200
movq -0x10(%rbp), %rax
movl $0x0, 0x18(%rax)
movq -0x10(%rbp), %rax
movzwl 0xe(%rax), %eax
cmpl $0x0, %eax
jne 0x91f1
movq -0x10(%rbp), %rax
movzwl 0xc(%rax), %ecx
orl $0x80, %ecx
movw %cx, 0xc(%rax)
movq -0x8(%rbp), %rax
movl $0x0, 0x10(%rax)
jmp 0x91f3
movq -0x8(%rbp), %rax
movzwl 0xc(%rax), %ecx
andl $0xfffffeff, %ecx       # imm = 0xFFFFFEFF
movw %cx, 0xc(%rax)
movq -0x8(%rbp), %rax
movl 0x18(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x18(%rax)
movq -0x8(%rbp), %rax
cmpl $0xc, 0x18(%rax)
jl 0x9227
movq -0x8(%rbp), %rax
movl $0x0, 0x18(%rax)
jmp 0x9229
movq -0x8(%rbp), %rax
movzwl 0xc(%rax), %ecx
andl $-0x10, %ecx
movw %cx, 0xc(%rax)
movq -0x8(%rbp), %rax
movl 0x18(%rax), %edx
movq -0x8(%rbp), %rax
movzwl 0xc(%rax), %ecx
orl %edx, %ecx
movw %cx, 0xc(%rax)
movq -0x10(%rbp), %rax
movzwl 0x12(%rax), %eax
andl $0xf, %eax
movq -0x8(%rbp), %rcx
cmpl 0x18(%rcx), %eax
jne 0x9272
movq -0x8(%rbp), %rax
movzwl 0xc(%rax), %ecx
orl $0x10, %ecx
movw %cx, 0xc(%rax)
jmp 0x9281
movq -0x8(%rbp), %rax
movzwl 0xc(%rax), %ecx
andl $-0x11, %ecx
movw %cx, 0xc(%rax)
addq $0x20, %rsp
popq %rbp
retq
nopw (%rax,%rax)