// File: /workspace/github_c_makefile_star2_O0/simh[P]simtools/converters/sfmtcvt/sfmtcvt.c
// Function: main
int main (int argc, char *argv[])
{
int i, d, d1, j, k, astrt, dstrt, addr, maxaddr[MAXR];
int numr, numf;
unsigned char data[MAXR][MAXA];
char *s, *ppos, *cptr, line[256], oname[256];
FILE *ifile, *ofile;

if ((argc < 2) || (argv[0] == NULL)) {
	printf ("Usage is: verb file [file...]\n");
	exit (0);  }

s = argv[1];
if ((s != NULL) && (*s++ == '-')) {
	++argv; --argc;
	switch (*s) {
 	case '1':
	    numr = 1; break;
	case '2':
	    numr = 2; break;
	case '4':
	    numr = 4; break;
	default:
	    fprintf (stderr, "Bad option %c\n", *s);
	return 0;  }
	}
else numr = 1;

for (i = 1, numf = 0; i < argc; i++) {
	ifile = fopen (argv[i], "r");
	if (ifile == NULL) {
		printf ("Error opening file: %s\n", argv[i]);
		exit (0);  }

	printf ("Processing file %s\n", argv[i]);
	astrt = 4;
	maxaddr[numf] = 0;
	for (;;) {
		cptr = fgets (line, 256, ifile);
		if (cptr == NULL) break;
		if (line[0] != 'S') continue;
		if (line[1] == '1') dstrt = 8;
		else if (line[1] == '2') dstrt = 10;
		else continue;
		for (k = astrt, addr = 0; k < dstrt; k++) {
			d = HEX (line[k]);
			addr = (addr << 4) + d;  }
		if (addr >= MAXA) {
			printf ("Address %o out of range\n", addr);
			break;  }
		for (k = dstrt; k < (dstrt + 32); k = k + 2, addr++) {
			d = HEX (line[k]);
			d1 = HEX (line[k+1]);
			data[numf][addr] = (d << 4) + d1;  }
		if (addr > maxaddr[numf]) maxaddr[numf] = addr;
		}
	fclose (ifile);
	numf++;
	if (numf >= numr) {
	    for (k = 0; k < numr; k++) {
		if (maxaddr[k] != maxaddr[0]) {
		    printf ("Rom lengths don't match, file 1 = %d, file %d = %d\n",
			maxaddr[0], k, maxaddr[k]);
		    return 0;  }  }
	    strcpy (oname, argv[i]);
            if (ppos = strrchr (oname, '.')) strcpy (ppos, ".bin");
            else strcat (oname, ".bin");
	    ofile = fopen (oname, "wb");
	    if (ofile == NULL) {
		printf ("Error opening file: %s\n", oname);
		exit (0);  }
	    printf ("Output file: %s, ROM size is %d\n", oname, maxaddr[0]);
	    for (k = 0; k < maxaddr[0]; k++) {
		for (j = numr - 1; j >= 0; j--) {
		    fwrite (&data[j][k], 1, 1, ofile);  }  }
	    fclose (ofile);
	    numf = 0;
	    }
	}
if (numf) printf ("Unprocessed files\n");
return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x20290, %rsp          # imm = 0x20290
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x2, -0x8(%rbp)
jl 0x11f9
movq -0x10(%rbp), %rax
cmpq $0x0, (%rax)
jne 0x120e
leaq 0xe04(%rip), %rdi       # 0x2004
movb $0x0, %al
callq 0x1050
xorl %edi, %edi
callq 0x10b0
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x20058(%rbp)
cmpq $0x0, -0x20058(%rbp)
je 0x12dc
movq -0x20058(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x20058(%rbp)
movsbl (%rax), %eax
cmpl $0x2d, %eax
jne 0x12dc
movq -0x10(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x10(%rbp)
movl -0x8(%rbp), %eax
decl %eax
movl %eax, -0x8(%rbp)
movq -0x20058(%rbp), %rax
movsbl (%rax), %eax
movl %eax, -0x20284(%rbp)
subl $0x31, %eax
je 0x1291
jmp 0x1277
movl -0x20284(%rbp), %eax
subl $0x32, %eax
je 0x129a
jmp 0x1284
movl -0x20284(%rbp), %eax
subl $0x34, %eax
je 0x12a3
jmp 0x12ac
movl $0x1, -0x44(%rbp)
jmp 0x12da
movl $0x2, -0x44(%rbp)
jmp 0x12da
movl $0x4, -0x44(%rbp)
jmp 0x12da
movq 0x2d45(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movq -0x20058(%rbp), %rax
movsbl (%rax), %edx
leaq 0xd5c(%rip), %rsi       # 0x2023
movb $0x0, %al
callq 0x1080
movl $0x0, -0x4(%rbp)
jmp 0x1748
jmp 0x12e3
movl $0x1, -0x44(%rbp)
movl $0x1, -0x14(%rbp)
movl $0x0, -0x48(%rbp)
movl -0x14(%rbp), %eax
cmpl -0x8(%rbp), %eax
jge 0x172d
movq -0x10(%rbp), %rax
movslq -0x14(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0xd22(%rip), %rsi       # 0x2032
callq 0x1090
movq %rax, -0x20278(%rbp)
cmpq $0x0, -0x20278(%rbp)
jne 0x1347
movq -0x10(%rbp), %rax
movslq -0x14(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
leaq 0xcfb(%rip), %rdi       # 0x2034
movb $0x0, %al
callq 0x1050
xorl %edi, %edi
callq 0x10b0
movq -0x10(%rbp), %rax
movslq -0x14(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
leaq 0xcf2(%rip), %rdi       # 0x204c
movb $0x0, %al
callq 0x1050
movl $0x4, -0x28(%rbp)
movslq -0x48(%rbp), %rax
movl $0x0, -0x40(%rbp,%rax,4)
leaq -0x20170(%rbp), %rdi
movq -0x20278(%rbp), %rdx
movl $0x100, %esi            # imm = 0x100
callq 0x1070
movq %rax, -0x20068(%rbp)
cmpq $0x0, -0x20068(%rbp)
jne 0x13a2
jmp 0x157f
movsbl -0x20170(%rbp), %eax
cmpl $0x53, %eax
je 0x13b0
jmp 0x1374
movsbl -0x2016f(%rbp), %eax
cmpl $0x31, %eax
jne 0x13c5
movl $0x8, -0x2c(%rbp)
jmp 0x13de
movsbl -0x2016f(%rbp), %eax
cmpl $0x32, %eax
jne 0x13da
movl $0xa, -0x2c(%rbp)
jmp 0x13dc
jmp 0x1374
jmp 0x13de
movl -0x28(%rbp), %eax
movl %eax, -0x24(%rbp)
movl $0x0, -0x30(%rbp)
movl -0x24(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jge 0x1453
movslq -0x24(%rbp), %rax
movsbl -0x20170(%rbp,%rax), %eax
cmpl $0x41, %eax
jl 0x141e
movslq -0x24(%rbp), %rax
movsbl -0x20170(%rbp,%rax), %eax
subl $0x41, %eax
addl $0xa, %eax
movl %eax, -0x20288(%rbp)
jmp 0x1433
movslq -0x24(%rbp), %rax
movsbl -0x20170(%rbp,%rax), %eax
subl $0x30, %eax
movl %eax, -0x20288(%rbp)
movl -0x20288(%rbp), %eax
movl %eax, -0x18(%rbp)
movl -0x30(%rbp), %eax
shll $0x4, %eax
addl -0x18(%rbp), %eax
movl %eax, -0x30(%rbp)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x13eb
cmpl $0x8000, -0x30(%rbp)    # imm = 0x8000
jl 0x1472
movl -0x30(%rbp), %esi
leaq 0xbfa(%rip), %rdi       # 0x2060
movb $0x0, %al
callq 0x1050
jmp 0x157f
movl -0x2c(%rbp), %eax
movl %eax, -0x24(%rbp)
movl -0x24(%rbp), %eax
movl -0x2c(%rbp), %ecx
addl $0x20, %ecx
cmpl %ecx, %eax
jge 0x1562
movslq -0x24(%rbp), %rax
movsbl -0x20170(%rbp,%rax), %eax
cmpl $0x41, %eax
jl 0x14b4
movslq -0x24(%rbp), %rax
movsbl -0x20170(%rbp,%rax), %eax
subl $0x41, %eax
addl $0xa, %eax
movl %eax, -0x2028c(%rbp)
jmp 0x14c9
movslq -0x24(%rbp), %rax
movsbl -0x20170(%rbp,%rax), %eax
subl $0x30, %eax
movl %eax, -0x2028c(%rbp)
movl -0x2028c(%rbp), %eax
movl %eax, -0x18(%rbp)
movl -0x24(%rbp), %eax
addl $0x1, %eax
cltq
movsbl -0x20170(%rbp,%rax), %eax
cmpl $0x41, %eax
jl 0x1505
movl -0x24(%rbp), %eax
addl $0x1, %eax
cltq
movsbl -0x20170(%rbp,%rax), %eax
subl $0x41, %eax
addl $0xa, %eax
movl %eax, -0x20290(%rbp)
jmp 0x151e
movl -0x24(%rbp), %eax
addl $0x1, %eax
cltq
movsbl -0x20170(%rbp,%rax), %eax
subl $0x30, %eax
movl %eax, -0x20290(%rbp)
movl -0x20290(%rbp), %eax
movl %eax, -0x1c(%rbp)
movl -0x18(%rbp), %eax
shll $0x4, %eax
addl -0x1c(%rbp), %eax
movb %al, %dl
movslq -0x48(%rbp), %rcx
leaq -0x20050(%rbp), %rax
shlq $0xf, %rcx
addq %rcx, %rax
movslq -0x30(%rbp), %rcx
movb %dl, (%rax,%rcx)
movl -0x24(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x24(%rbp)
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x1478
movl -0x30(%rbp), %eax
movslq -0x48(%rbp), %rcx
cmpl -0x40(%rbp,%rcx,4), %eax
jle 0x157a
movl -0x30(%rbp), %ecx
movslq -0x48(%rbp), %rax
movl %ecx, -0x40(%rbp,%rax,4)
jmp 0x1374
movq -0x20278(%rbp), %rdi
callq 0x1040
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
movl -0x48(%rbp), %eax
cmpl -0x44(%rbp), %eax
jl 0x171d
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
cmpl -0x44(%rbp), %eax
jge 0x15f1
movslq -0x24(%rbp), %rax
movl -0x40(%rbp,%rax,4), %eax
cmpl -0x40(%rbp), %eax
je 0x15e4
movl -0x40(%rbp), %esi
movl -0x24(%rbp), %edx
movslq -0x24(%rbp), %rax
movl -0x40(%rbp,%rax,4), %ecx
leaq 0xaa8(%rip), %rdi       # 0x2079
movb $0x0, %al
callq 0x1050
movl $0x0, -0x4(%rbp)
jmp 0x1748
jmp 0x15e6
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x15a7
leaq -0x20270(%rbp), %rdi
movq -0x10(%rbp), %rax
movslq -0x14(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
callq 0x1030
leaq -0x20270(%rbp), %rdi
movl $0x2e, %esi
callq 0x1060
movq %rax, -0x20060(%rbp)
cmpq $0x0, %rax
je 0x163c
movq -0x20060(%rbp), %rdi
leaq 0xa78(%rip), %rsi       # 0x20ad
callq 0x1030
jmp 0x164f
leaq -0x20270(%rbp), %rdi
leaq 0xa63(%rip), %rsi       # 0x20ad
callq 0x10a0
leaq -0x20270(%rbp), %rdi
leaq 0xa55(%rip), %rsi       # 0x20b2
callq 0x1090
movq %rax, -0x20280(%rbp)
cmpq $0x0, -0x20280(%rbp)
jne 0x168f
leaq -0x20270(%rbp), %rsi
leaq 0x9b3(%rip), %rdi       # 0x2034
movb $0x0, %al
callq 0x1050
xorl %edi, %edi
callq 0x10b0
leaq -0x20270(%rbp), %rsi
movl -0x40(%rbp), %edx
leaq 0xa15(%rip), %rdi       # 0x20b5
movb $0x0, %al
callq 0x1050
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
cmpl -0x40(%rbp), %eax
jge 0x170a
movl -0x44(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
jl 0x16fd
movslq -0x20(%rbp), %rax
leaq -0x20050(%rbp), %rdi
shlq $0xf, %rax
addq %rax, %rdi
movslq -0x24(%rbp), %rax
addq %rax, %rdi
movq -0x20280(%rbp), %rcx
movl $0x1, %edx
movq %rdx, %rsi
callq 0x10c0
movl -0x20(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x16bf
jmp 0x16ff
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x16ae
movq -0x20280(%rbp), %rdi
callq 0x1040
movl $0x0, -0x48(%rbp)
jmp 0x171f
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x12f1
cmpl $0x0, -0x48(%rbp)
je 0x1741
leaq 0x99c(%rip), %rdi       # 0x20d6
movb $0x0, %al
callq 0x1050
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x20290, %rsp          # imm = 0x20290
popq %rbp
retq