// File: /workspace/github_c_makefile_star2_O0/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 %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
leaq 0x4c143(%rip), %rdi     # 0x4ec00
xorl %esi, %esi
movl $0x158, %edx            # imm = 0x158
callq 0x20e0
leaq 0x4c288(%rip), %rdi     # 0x4ed58
xorl %esi, %esi
movl $0x18, %edx
callq 0x20e0
leaq 0x4c275(%rip), %rax     # 0x4ed58
movq %rax, 0x4c13e(%rip)     # 0x4ec28
leaq 0x4c267(%rip), %rdi     # 0x4ed58
leaq 0xdac8(%rip), %rsi      # 0x105c0
callq 0x2350
leaq 0x4c254(%rip), %rdi     # 0x4ed58
leaq 0xdbf5(%rip), %rsi      # 0x10700
callq 0x2350
leaq 0x4c241(%rip), %rdi     # 0x4ed58
leaq 0xdba2(%rip), %rsi      # 0x106c0
callq 0x2350
leaq 0x4c22e(%rip), %rdi     # 0x4ed58
leaq 0xdc0f(%rip), %rsi      # 0x10740
callq 0x2350
leaq 0x4c21b(%rip), %rdi     # 0x4ed58
leaq 0xdc3c(%rip), %rsi      # 0x10780
callq 0x2350
leaq 0x4c208(%rip), %rdi     # 0x4ed58
leaq 0xdae9(%rip), %rsi      # 0x10640
callq 0x2350
leaq 0x4c1f5(%rip), %rdi     # 0x4ed58
leaq 0xdb16(%rip), %rsi      # 0x10680
callq 0x2350
movb $0x1, %al
cmpq $0x0, 0x4c07f(%rip)     # 0x4ebf8
movb %al, -0x22(%rbp)
jne 0x2b96
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, 0x4c06c(%rip)     # 0x4ebf8
cmpq $0x0, %rax
setne %al
movb %al, -0x22(%rbp)
movb -0x22(%rbp), %al
movq -0x10(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x10(%rbp)
movl -0x8(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x8(%rbp)
movq -0x10(%rbp), %rcx
xorl %eax, %eax
cmpq $0x0, (%rcx)
movb %al, -0x23(%rbp)
je 0x2be5
movq -0x10(%rbp), %rax
movq (%rax), %rax
movsbl (%rax), %ecx
xorl %eax, %eax
cmpl $0x2d, %ecx
movb %al, -0x23(%rbp)
jne 0x2be5
movq -0x10(%rbp), %rax
movq (%rax), %rax
movsbl 0x1(%rax), %eax
cmpl $0x0, %eax
setne %al
movb %al, -0x23(%rbp)
movb -0x23(%rbp), %al
testb $0x1, %al
jne 0x2bf1
jmp 0x2c92
movq -0x10(%rbp), %rax
movq (%rax), %rax
addq $0x1, %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x2d, %eax
jne 0x2c30
movq -0x18(%rbp), %rax
movsbl 0x1(%rax), %eax
cmpl $0x0, %eax
jne 0x2c30
movl -0x8(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x8(%rbp)
movq -0x10(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x10(%rbp)
jmp 0x2c92
movb $0x0, -0x21(%rbp)
movq -0x18(%rbp), %rax
movsbl (%rax), %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, -0x24(%rbp)
je 0x2c65
movq -0x18(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x18(%rbp)
movb (%rax), %al
movb %al, -0x21(%rbp)
movsbl %al, %eax
cmpl $0x0, %eax
setne %al
movb %al, -0x24(%rbp)
movb -0x24(%rbp), %al
testb $0x1, %al
jne 0x2c6e
jmp 0x2c70
jmp 0x2c34
movq $0x0, -0x20(%rbp)
movl -0x8(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x8(%rbp)
movq -0x10(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x10(%rbp)
jmp 0x2bae
cmpl $0x0, -0x8(%rbp)
jge 0x2c9d
callq 0x2a70
leaq 0xa39b(%rip), %rdi      # 0xd03f
callq 0x25c0
leaq 0x4bf50(%rip), %rdi     # 0x4ec00
callq 0x2d30
leaq 0x4bb24(%rip), %rdi     # 0x4e7e0
addq $0x2c, %rdi
callq 0xbcb0
leaq 0x4bb7c(%rip), %rdi     # 0x4e848
xorl %esi, %esi
leaq 0xa372(%rip), %rdx      # 0xd047
callq 0xb5f0
leaq 0x4bb67(%rip), %rdi     # 0x4e848
movl $0x1, %esi
leaq 0xa369(%rip), %rdx      # 0xd056
callq 0xb5f0
leaq 0x4bb4f(%rip), %rdi     # 0x4e848
movl $0x2, %esi
leaq 0xa360(%rip), %rdx      # 0xd065
callq 0xb5f0
movw $0xea10, 0x4befb(%rip)  # imm = 0xEA10
movw $0x0, 0x4bf64(%rip)     # 0x4ec80
leaq 0x4bedd(%rip), %rdi     # 0x4ec00
callq 0x8c90
xorl %eax, %eax
addq $0x30, %rsp
popq %rbp
retq