// File: /workspace/github_c_makefile_star2_O3/aap[P]pdp11/1140.c
// Function: main
int
xmain(int argc, char *argv[])
{
	memset(&cpu, 0, sizeof(KD11A));
	memset(&bus, 0, sizeof(Bus));
	cpu.bus = &bus;
	busadddev(&bus, &membusdev);
	busadddev(&bus, &kwbusdev);
	busadddev(&bus, &klbusdev);
	busadddev(&bus, &rkbusdev);
	busadddev(&bus, &dcbusdev);
//	busadddev(&bus, &rombusdev);	// mutually exclusive with the first M9312 ROM
#ifndef AUTODIAG	// breaks some test
	busadddev(&bus, &rombusdev9312);
	busadddev(&bus, &rombusdev9312X);
#endif

	ARGBEGIN{
	}ARGEND;

	if(argc < 0)
		usage();

	loadmem("mem.txt");

	reset(&cpu);

	ttyopen(&kl11.tty);

#ifdef AUTODIAG
	printf("running diags\n");
	/* basic tests */
	if(rundiag(&cpu, "maindec/ZKAAA0.BIN")) return 0;	// branch
	if(rundiag(&cpu, "maindec/ZKABA0.BIN")) return 0;	// con branch
	if(rundiag(&cpu, "maindec/ZKACA0.BIN")) return 0;	// unary
	if(rundiag(&cpu, "maindec/ZKADA0.BIN")) return 0;	// binary
	if(rundiag(&cpu, "maindec/ZKAEA0.BIN")) return 0;	// rot/shf
	if(rundiag(&cpu, "maindec/ZKAFA0.BIN")) return 0;	// cmp
	if(rundiag(&cpu, "maindec/ZKAGA0.BIN")) return 0;	// cmp not
	if(rundiag(&cpu, "maindec/ZKAHA0.BIN")) return 0;	// move
	if(rundiag(&cpu, "maindec/ZKAIA0.BIN")) return 0;	// bis bic bit
	if(rundiag(&cpu, "maindec/ZKAJA0.BIN")) return 0;	// add
	if(rundiag(&cpu, "maindec/ZKAKA0.BIN")) return 0;	// sub
	if(rundiag(&cpu, "maindec/ZKALA0.BIN")) return 0;	// jmp
	if(rundiag(&cpu, "maindec/ZKAMA0.BIN")) return 0;	// rts rti jsr

	/* 11/40 traps */
	if(rundiag(&cpu, "maindec/BKDMD0.BIC")) return 0;

	/* 11/40 instructions */
	if(rundiag(&cpu, "maindec/CKBAB0.BIC")) return 0;	// sxt
	if(rundiag(&cpu, "maindec/CKBBB0.BIC")) return 0;	// sob
	if(rundiag(&cpu, "maindec/CKBCC0.BIC")) return 0;	// xor
	if(rundiag(&cpu, "maindec/CKBDC0.BIC")) return 0;	// mark
	if(rundiag(&cpu, "maindec/CKBEC0.BIC")) return 0;	// rtt
	if(rundiag(&cpu, "maindec/CKBFD0.BIC")) return 0;	// stack limit

	/* EIS */
	if(rundiag(&cpu, "maindec/CKBIB0.BIC")) return 0;	// ash
	if(rundiag(&cpu, "maindec/CKBJA0.BIC")) return 0;	// ashc
	if(rundiag(&cpu, "maindec/CKBKA0.BIC")) return 0;	// mul
	if(rundiag(&cpu, "maindec/CKBLA0.BIC")) return 0;	// div

	/* KT11 */
	if(rundiag(&cpu, "maindec/BKTAD1.BIC")) return 0;	// basic logic
	if(rundiag(&cpu, "maindec/BKTBB0.BIC")) return 0;	// access keys
	if(rundiag(&cpu, "maindec/BKTCB0.BIC")) return 0;	// moves
	if(rundiag(&cpu, "maindec/BKTDC0.BIC")) return 0;	// states
	if(rundiag(&cpu, "maindec/BKTFD0.BIC")) return 0;	// abort

	/* system exerciser with KT11 */
/*	// no idea how this works
	cpu.sw = 0100001;
	cpu.sw |= 0370;	// disable a couple of devices
	if(rundiag(&cpu, "maindec/BKTGD1.BIC")) return 0;	// exerciser
*/
	printf("passed all diagnostics\n");
		return 0;
#endif

	// to boot UNIX v6
	attach_rk05(&rk11, 0, "unix6/disk0.rk");
	attach_rk05(&rk11, 1, "unix6/disk1.rk");
	attach_rk05(&rk11, 2, "unix6/disk2.rk");

	// RT11
//	attach_rk05(&rk11, 0, "rt11/rtv40rk.dsk");
//	attach_rk05(&rk11, 0, "dos11/dos_rk.dsk");

//	cpu.r[7] = 0173100;
//	cpu.sw = 0177406;	// RK11 boot

	cpu.r[7] = 0165020;	// M9312 boot
	cpu.sw = 0;

//	cpu.sw = 0000001;
//	cpu.sw = 0173030;
//	cpu.sw = 0104000;
	run(&cpu);

	return 0;
}

// Assembly:
pushq %r14
pushq %rbx
pushq %rax
movq %rsi, %rbx
movl %edi, %r14d
leaq 0x483c6(%rip), %rdi     # 0x4abf0
movl $0x158, %edx            # imm = 0x158
xorl %esi, %esi
callq 0x20e0
xorps %xmm0, %xmm0
movups %xmm0, 0x48510(%rip)    # 0x4ad50
leaq 0x48501(%rip), %rax     # 0x4ad48
movq %rax, 0x483ca(%rip)     # 0x4ac18
leaq 0x9d6b(%rip), %rcx      # 0xc5c0
movq %rcx, 0x484ec(%rip)     # 0x4ad48
movq $0x0, 0x9d59(%rip) # 0xc5c0
movq %rcx, %rdx
movq (%rcx), %rcx
testq %rcx, %rcx
jne 0x2867
leaq 0x9e87(%rip), %rcx      # 0xc700
movq %rcx, (%rdx)
movq $0x0, 0x9e79(%rip) # 0xc700
movq %rax, %rcx
movq (%rax), %rax
testq %rax, %rax
jne 0x2887
leaq 0x9e27(%rip), %rax      # 0xc6c0
movq %rax, (%rcx)
movq $0x0, 0x9e19(%rip) # 0xc6c0
leaq 0x4849a(%rip), %rcx     # 0x4ad48
movq %rcx, %rax
movq (%rcx), %rcx
testq %rcx, %rcx
jne 0x28ae
leaq 0x9e80(%rip), %rcx      # 0xc740
movq %rcx, (%rax)
movq $0x0, 0x9e72(%rip) # 0xc740
leaq 0x48473(%rip), %rcx     # 0x4ad48
movq %rcx, %rax
movq (%rcx), %rcx
testq %rcx, %rcx
jne 0x28d5
leaq 0x9e99(%rip), %rcx      # 0xc780
movq %rcx, (%rax)
movq $0x0, 0x9e8b(%rip) # 0xc780
leaq 0x4844c(%rip), %rcx     # 0x4ad48
movq %rcx, %rax
movq (%rcx), %rcx
testq %rcx, %rcx
jne 0x28fc
leaq 0x9d32(%rip), %rcx      # 0xc640
movq %rcx, (%rax)
movq $0x0, 0x9d24(%rip) # 0xc640
leaq 0x48425(%rip), %rcx     # 0x4ad48
movq %rcx, %rax
movq (%rcx), %rcx
testq %rcx, %rcx
jne 0x2923
leaq 0x9d4b(%rip), %rcx      # 0xc680
movq %rcx, (%rax)
movq $0x0, 0x9d3d(%rip) # 0xc680
movq 0x4829e(%rip), %rdi     # 0x4abe8
testq %rdi, %rdi
jne 0x2959
movq (%rbx), %rdi
movq %rdi, 0x4828f(%rip)     # 0x4abe8
leal -0x1(%r14), %ecx
movq 0x8(%rbx), %rdx
testq %rdx, %rdx
je 0x29b3
addq $0x8, %rbx
movl %ecx, %eax
cmpb $0x2d, (%rdx)
jne 0x29a8
leaq 0x1(%rdx), %rcx
movzbl 0x1(%rdx), %esi
cmpl $0x2d, %esi
je 0x2984
testl %esi, %esi
jne 0x298a
jmp 0x29a8
cmpb $0x0, 0x2(%rdx)
je 0x29ac
cmpb $0x0, (%rcx)
leaq 0x1(%rcx), %rcx
jne 0x298a
leal -0x1(%rax), %ecx
movq 0x8(%rbx), %rdx
addq $0x8, %rbx
movl %eax, %r14d
testq %rdx, %rdx
jne 0x296a
jmp 0x29b3
movl %eax, %ecx
jmp 0x29b3
addl $-0x2, %r14d
movl %r14d, %ecx
testl %ecx, %ecx
js 0x2a46
leaq 0x666b(%rip), %rdi      # 0x902d
callq 0x247b
leaq 0x48222(%rip), %rbx     # 0x4abf0
movq %rbx, %rdi
callq 0x2a4c
leaq 0x47e1f(%rip), %rdi     # 0x4a7fc
callq 0x840f
leaq 0x47e4f(%rip), %r14     # 0x4a838
leaq 0x6645(%rip), %rdx      # 0x9035
movq %r14, %rdi
xorl %esi, %esi
callq 0x8110
leaq 0x6643(%rip), %rdx      # 0x9044
movq %r14, %rdi
movl $0x1, %esi
callq 0x8110
leaq 0x663e(%rip), %rdx      # 0x9053
movq %r14, %rdi
movl $0x2, %esi
callq 0x8110
movw $0xea10, 0x481d3(%rip)  # imm = 0xEA10
movw $0x0, 0x4823c(%rip)     # 0x4ac70
movq %rbx, %rdi
callq 0x6dba
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r14
retq
callq 0x2260
nop