// File: /workspace/github_c_makefile_star2_O1/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 0x482c5(%rip), %rdi     # 0x4abf0
movl $0x158, %edx            # imm = 0x158
xorl %esi, %esi
callq 0x20e0
xorps %xmm0, %xmm0
movups %xmm0, 0x48407(%rip)    # 0x4ad48
movq $0x0, 0x4840c(%rip) # 0x4ad58
leaq 0x483f5(%rip), %rax     # 0x4ad48
movq %rax, 0x482be(%rip)     # 0x4ac18
movq %rax, %rcx
movq (%rax), %rax
testq %rax, %rax
jne 0x295a
leaq 0x9c54(%rip), %rax      # 0xc5c0
movq %rax, (%rcx)
movq $0x0, 0x9c46(%rip) # 0xc5c0
leaq 0x483c7(%rip), %rcx     # 0x4ad48
movq %rcx, %rax
movq (%rcx), %rcx
testq %rcx, %rcx
jne 0x2981
leaq 0x9d6d(%rip), %rcx      # 0xc700
movq %rcx, (%rax)
movq $0x0, 0x9d5f(%rip) # 0xc700
leaq 0x483a0(%rip), %rcx     # 0x4ad48
movq %rcx, %rax
movq (%rcx), %rcx
testq %rcx, %rcx
jne 0x29a8
leaq 0x9d06(%rip), %rcx      # 0xc6c0
movq %rcx, (%rax)
movq $0x0, 0x9cf8(%rip) # 0xc6c0
leaq 0x48379(%rip), %rcx     # 0x4ad48
movq %rcx, %rax
movq (%rcx), %rcx
testq %rcx, %rcx
jne 0x29cf
leaq 0x9d5f(%rip), %rcx      # 0xc740
movq %rcx, (%rax)
movq $0x0, 0x9d51(%rip) # 0xc740
leaq 0x48352(%rip), %rcx     # 0x4ad48
movq %rcx, %rax
movq (%rcx), %rcx
testq %rcx, %rcx
jne 0x29f6
leaq 0x9d78(%rip), %rcx      # 0xc780
movq %rcx, (%rax)
movq $0x0, 0x9d6a(%rip) # 0xc780
leaq 0x4832b(%rip), %rcx     # 0x4ad48
movq %rcx, %rax
movq (%rcx), %rcx
testq %rcx, %rcx
jne 0x2a1d
leaq 0x9c11(%rip), %rcx      # 0xc640
movq %rcx, (%rax)
movq $0x0, 0x9c03(%rip) # 0xc640
leaq 0x48304(%rip), %rcx     # 0x4ad48
movq %rcx, %rax
movq (%rcx), %rcx
testq %rcx, %rcx
jne 0x2a44
leaq 0x9c2a(%rip), %rcx      # 0xc680
movq %rcx, (%rax)
movq $0x0, 0x9c1c(%rip) # 0xc680
cmpq $0x0, 0x4817c(%rip)     # 0x4abe8
jne 0x2a78
movq (%rbx), %rax
movq %rax, 0x48170(%rip)     # 0x4abe8
leal -0x1(%r14), %eax
movq 0x8(%rbx), %rsi
testq %rsi, %rsi
je 0x2acd
cmpb $0x2d, (%rsi)
jne 0x2acd
leaq 0x8(%rbx), %rcx
leaq 0x1(%rsi), %rdx
movzbl 0x1(%rsi), %edi
cmpl $0x2d, %edi
je 0x2aa1
testl %edi, %edi
jne 0x2aa7
jmp 0x2acd
cmpb $0x0, 0x2(%rsi)
je 0x2abe
cmpb $0x0, (%rdx)
leaq 0x1(%rdx), %rdx
jne 0x2aa7
movb $0x1, %dl
movq %rcx, %rbx
movl %eax, %r14d
testb %dl, %dl
jne 0x2a78
jmp 0x2acd
addl $-0x2, %r14d
addq $0x10, %rbx
xorl %edx, %edx
movl %r14d, %eax
jmp 0x2ab5
testl %eax, %eax
js 0x2b60
leaq 0x6551(%rip), %rdi      # 0x902d
callq 0x257b
leaq 0x48108(%rip), %rbx     # 0x4abf0
movq %rbx, %rdi
callq 0x2b68
leaq 0x47d05(%rip), %rdi     # 0x4a7fc
callq 0x82e3
leaq 0x47d35(%rip), %r14     # 0x4a838
leaq 0x652b(%rip), %rdx      # 0x9035
movq %r14, %rdi
xorl %esi, %esi
callq 0x7fe3
leaq 0x6529(%rip), %rdx      # 0x9044
movq %r14, %rdi
movl $0x1, %esi
callq 0x7fe3
leaq 0x6524(%rip), %rdx      # 0x9053
movq %r14, %rdi
movl $0x2, %esi
callq 0x7fe3
movw $0xea10, 0x480b9(%rip)  # imm = 0xEA10
movw $0x0, 0x48122(%rip)     # 0x4ac70
movq %rbx, %rdi
callq 0x6d0e
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r14
retq
callq 0x2260
nopl (%rax)