// File: /workspace/github_c_makefile_star2_O2/Joy-Division[P]tools-zoe/mdx-parser.c
// Function: main
int main( int argc, char **argv ) {
	
	if ( argc < 3 ) {
		printf("Not enough args!\nUse: %s MDX-file game\n", argv[0]);
		printf("supported games are:\n%d (MGS1)\n%d (ZoE1 and MGS2HD)\n", mgs1, zoe1);
		return 1;
	}
	
	
	FILE *f, *o;
	char outputname[512] = {0};
	unsigned int game, tracklistsize, numtracks;
	unsigned int cursong, curtrack;
	unsigned int event, param0, param1, param2, note, velocity, length, snap;
	uint32_t numsongs, songoffset, *tracklist;
	uint32_t curtoken;
	game = strtoul(argv[2], NULL, 10);
	if(!(game<NUMWVXGAMES)) {
		printf("Unsupported game %d!\nRun without args to see supported types.\n", game);
		return 1;
	}
	
	
	switch(game) {
		case mgs1: {
			tracklistsize = 0x60;
			tracklist = malloc(0x60);
			break;
		}
		case zoe1: {
			tracklistsize = 0x80;
			tracklist = malloc(0x80);
			break;
		}
	}
	numtracks = tracklistsize / 4;
	
	if( !(f = fopen( argv[1], "rb" ))) {
		printf("Couldnt open file %s\n", argv[1]);
		return 1;
	}
	
	fseek(f, 0, SEEK_SET);
	fread(&numsongs, 4, 1, f);
	printf("Number of songs in MDX: %d\n", numsongs);
	
	for(cursong = 0;cursong < numsongs;cursong++) {
		fseek(f, 4+(4*cursong), SEEK_SET);
		fread(&songoffset, 4, 1, f);
		printf("Song %d - Offset: %08x\n", cursong, songoffset);
		fseek(f, songoffset, SEEK_SET);
		fread(tracklist, tracklistsize, 1, f);
		for(curtrack = 0;curtrack < numtracks;curtrack++) {
			sprintf(outputname, "song%05d-track%05d.bin", cursong, curtrack);
			curtoken = 0;
			if( !(o = fopen( outputname, "wb" ))) {
				printf("Couldnt open file %s\n", outputname);
				return 1;
			}
			printf("Song %d - Track %02d - Offset: %08x\n", cursong, curtrack, tracklist[curtrack]);
			fseek(f, tracklist[curtrack], SEEK_SET);
			
			/* smf stuffs */
			//~ track = smf_track_new();
			
			while(curtoken != 0xFFFE0000) {
				printf("\t");
				fread(&curtoken, 4, 1, f);
				
				event = curtoken >> 24;
				param0 = (curtoken >> 16) & 0xFF;
				param1 = (curtoken >> 8) & 0xFF;
				param2 = curtoken & 0xFF;
				
				if(event < 0x80 ) {
					/* possible midi note */
					note = event;
					snap = param0;
					length = param1;
					velocity = param2;
					printf("Encountered note %x(%s)\n", note, midinotes[note]);
					printf("\tsnap %x - length %x - velocity %x\n", snap, length, velocity);
				}
				else {
					switch(event) {
						case 0xD0: {
							printf("Encountered Tempo event at %08x, parameters %02x %02x %02x\n", (unsigned int)ftell(f)-4, param0, param1, param2);
							break;
						}
						
						case 0xD1: {
							printf("Encountered Decrease event at %08x, parameters %02x %02x %02x\n", (unsigned int)ftell(f)-4, param0, param1, param2);
							break;
						}
						
						case 0xD2: {
							printf("Encountered Instrument A event at %08x, parameters %02x %02x %02x\n", (unsigned int)ftell(f)-4, param0, param1, param2);
							break;
						}
						
						case 0xD3: {
							printf("Encountered Instrument B event at %08x, parameters %02x %02x %02x\n", (unsigned int)ftell(f)-4, param0, param1, param2);
							break;
						}
						
						case 0xD4: {
							printf("Encountered Instrument C event at %08x, parameters %02x %02x %02x\n", (unsigned int)ftell(f)-4, param0, param1, param2);
							break;
						}
						
						case 0xD5: {
							printf("Encountered Change Master Volume event at %08x, parameters %02x %02x %02x\n", (unsigned int)ftell(f)-4, param0, param1, param2);
							break;
						}
						
						case 0xD6: {
							printf("Encountered Fade out event at %08x, parameters %02x %02x %02x\n", (unsigned int)ftell(f)-4, param0, param1, param2);
							break;
						}
						
						case 0xD7: {
							printf("Encountered Change Channel Volume event at %08x, parameters %02x %02x %02x\n", (unsigned int)ftell(f)-4, param0, param1, param2);
							break;
						}
						
						case 0xD8: {
							printf("Encountered Fade out note event at %08x, parameters %02x %02x %02x\n", (unsigned int)ftell(f)-4, param0, param1, param2);
							break;
						}
						
						case 0xDD: {
							printf("Encountered Pan event at %08x, complete event %08x\n", (unsigned int)ftell(f)-4, curtoken);
							break;
						}
						
						/*
						case 0xDE: {
							printf("Encountered Pan1 event at %08x, complete event %08x\n", (unsigned int)ftell(f)-4, curtoken);
							break;
						}
						*/
						
						case 0xDF: {
							printf("Encountered Pitch event at %08x, parameters %02x %02x %02x\n", (unsigned int)ftell(f)-4, param0, param1, param2);
							break;
						}
						
						case 0xE0: {
							printf("Encountered Pitch note when changed event at %08x, parameters %02x %02x %02x\n", (unsigned int)ftell(f)-4, param0, param1, param2);
							break;
						}
						
						case 0xE3: {
							printf("Encountered Modulation event at %08x, parameters %02x %02x %02x\n", (unsigned int)ftell(f)-4, param0, param1, param2);
							break;
						}
						
						case 0xE5: {
							printf("Encountered Swirl up note event at %08x, parameters %02x %02x %02x\n", (unsigned int)ftell(f)-4, param0, param1, param2);
							break;
						}
						
						case 0xE6: {
							printf("Encountered Pitch bend on change event at %08x, parameters %02x %02x %02x\n", (unsigned int)ftell(f)-4, param0, param1, param2);
							break;
						}
						
						case 0xE7: {
							printf("Encountered Set Loop Start (MEMORY A) event at %08x, complete event %08x\n", (unsigned int)ftell(f)-4, curtoken);
							break;
						}
						
						case 0xE8: {
							printf("Encountered Set Loop End (MEMORY A) (repeat PARAM0 times) event at %08x, parameters %02x %02x %02x\n", (unsigned int)ftell(f)-4, param0, param1, param2);
							break;
						}
						
						case 0xE9: {
							printf("Encountered Set Loop Start (MEMORY B) event at %08x, complete event %08x\n", (unsigned int)ftell(f)-4, curtoken);
							break;
						}
						
						case 0xEA: {
							printf("Encountered Set Loop End (MEMORY B) (repeat PARAM0 times) event at %08x, parameters %02x %02x %02x\n", (unsigned int)ftell(f)-4, param0, param1, param2);
							break;
						}
						
						case 0xEB: {
							printf("Encountered Set Loop Start event at %08x, complete event %08x\n", (unsigned int)ftell(f)-4, curtoken);
							break;
						}
						
						case 0xEC: {
							printf("Encountered Set Loop End event at %08x, complete event %08x\n", (unsigned int)ftell(f)-4, curtoken);
							break;
						}
						
						case 0xED: {
							printf("Encountered Set Loop Start (0xED) event at %08x, complete event %08x\n", (unsigned int)ftell(f)-4, curtoken);
							break;
						}
						
						case 0xEE: {
							printf("Encountered Set Loop End (0xEE) (two per 0xED) event at %08x, complete event %08x\n", (unsigned int)ftell(f)-4, curtoken);
							break;
						}
						
						case 0xF2: {
							printf("Encountered Silence A event at %08x, parameters %02x %02x %02x\n", (unsigned int)ftell(f)-4, param0, param1, param2);
							break;
						}
						
						case 0xF3: {
							printf("Encountered Silence B event at %08x, parameters %02x %02x %02x\n", (unsigned int)ftell(f)-4, param0, param1, param2);
							break;
						}
						
						case 0xFF: {
							printf("Encountered End Of Track event at %08x, complete event %08x\n", (unsigned int)ftell(f)-4, curtoken);
							break;
						}
						
						default: {
							printf("Unknown event %x at %08x\n", curtoken, (unsigned int)ftell(f)-4);
							break;
						}
					}
				}
				fwrite(&curtoken, 4, 1, o);
			}
			fclose(o);
		}
	}
	
	printf("Done.\n");
	
	fclose(f);
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x258, %rsp            # imm = 0x258
movq %rsi, %rbx
cmpl $0x2, %edi
jg 0x25b9
movq (%rbx), %rsi
leaq 0xd74(%rip), %rdi       # 0x330c
xorl %eax, %eax
callq 0x2070
leaq 0xd8e(%rip), %rdi       # 0x3334
pushq $0x1
popq %rbx
xorl %esi, %esi
movl %ebx, %edx
xorl %eax, %eax
callq 0x2070
jmp 0x29e2
leaq 0x50(%rsp), %rdi
movl $0x200, %edx            # imm = 0x200
xorl %esi, %esi
callq 0x2080
movq 0x10(%rbx), %rdi
pushq $0xa
popq %rdx
xorl %esi, %esi
callq 0x20d0
cmpl $0x2, %eax
jae 0x25f7
testb $0x1, %al
je 0x260c
movl $0x80, %edi
callq 0x20a0
movq %rax, 0x8(%rsp)
movl $0x80, %ebp
jmp 0x261c
leaq 0xd6b(%rip), %rdi       # 0x3369
movl %eax, %esi
xorl %eax, %eax
callq 0x2070
jmp 0x29df
pushq $0x60
popq %rdi
callq 0x20a0
movq %rax, 0x8(%rsp)
pushq $0x60
popq %rbp
movq 0x8(%rbx), %rdi
leaq 0xd81(%rip), %rsi       # 0x33a8
callq 0x20c0
testq %rax, %rax
je 0x29cd
movq %rax, %r15
movl %ebp, %eax
shrl $0x2, %eax
movq %rax, 0x30(%rsp)
movq %r15, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x20b0
leaq 0x4c(%rsp), %rbx
pushq $0x4
popq %r12
pushq $0x1
popq %r13
movq %rbx, %rdi
movq %r12, %rsi
movq %r13, %rdx
movq %r15, %rcx
callq 0x2050
movl (%rbx), %ebx
leaq 0xd36(%rip), %rdi       # 0x33ab
movl %ebx, %esi
xorl %eax, %eax
callq 0x2070
movl %ebp, %eax
movq %rax, 0x20(%rsp)
leaq 0x4(%rsp), %r14
xorl %ebp, %ebp
movq %r15, 0x40(%rsp)
movq %rbx, 0x18(%rsp)
cmpq %rbx, %rbp
jae 0x29f6
leal 0x4(,%rbp,4), %esi
movq %r15, %rdi
xorl %edx, %edx
callq 0x20b0
movq %r14, %rdi
movq %r12, %rsi
movq %r13, %rdx
movq %r15, %rcx
callq 0x2050
movl 0x4(%rsp), %r14d
leaq 0xcfa(%rip), %rdi       # 0x33c7
movl %ebp, %esi
movl %r14d, %edx
xorl %eax, %eax
callq 0x2070
movq %r15, %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x20b0
movq 0x8(%rsp), %rdi
movq 0x20(%rsp), %rsi
movq %r13, %rdx
movq %r15, %rcx
callq 0x2050
xorl %ebx, %ebx
movq %rbp, 0x28(%rsp)
cmpq 0x30(%rsp), %rbx
je 0x29ad
leaq 0x50(%rsp), %r14
movq %r14, %rdi
leaq 0xcc3(%rip), %rsi       # 0x33df
movl %ebp, %edx
movl %ebx, %ecx
xorl %eax, %eax
callq 0x20e0
andl $0x0, (%rsp)
movq %r14, %rdi
leaq 0x90c(%rip), %rsi       # 0x3041
callq 0x20c0
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x29bf
movq 0x8(%rsp), %r14
movl (%r14,%rbx,4), %ecx
movq %rbp, %rsi
xorl %ebp, %ebp
leaq 0xc99(%rip), %rdi       # 0x33f6
movl %ebx, %edx
xorl %eax, %eax
callq 0x2070
movq %rbx, 0x38(%rsp)
movl (%r14,%rbx,4), %esi
movq %r15, %rdi
xorl %edx, %edx
callq 0x20b0
cmpl $0xfffe0000, %ebp       # imm = 0xFFFE0000
je 0x2991
pushq $0x9
popq %rdi
callq 0x2030
movq %rsp, %rdi
movq %r12, %rbx
movq %r12, %rsi
movq %r13, %rdx
movq %r15, %rcx
callq 0x2050
movl (%rsp), %r12d
movl %r12d, %r14d
shrl $0x18, %r14d
movl %r12d, %r13d
shrl $0x10, %r13d
movl $0xff, %eax
andl %eax, %r13d
movq %r15, %rdi
movl %r12d, %r15d
shrl $0x8, %r15d
andl %eax, %r15d
movl %r12d, %ebp
andl %eax, %ebp
testl %r12d, %r12d
js 0x2809
leaq 0x28b7(%rip), %rax      # 0x5090
movq (%rax,%r14,8), %rdx
leaq 0xc37(%rip), %rdi       # 0x341b
movl %r14d, %esi
xorl %eax, %eax
callq 0x2070
leaq 0xc3f(%rip), %rdi       # 0x3434
movl %r13d, %esi
movl %r15d, %edx
movl %ebp, %ecx
xorl %eax, %eax
callq 0x2070
jmp 0x296a
callq 0x2090
addl $-0x4, %eax
addl $0xffffff30, %r14d      # imm = 0xFFFFFF30
cmpl $0x2f, %r14d
ja 0x291c
leaq 0x853(%rip), %rdx       # 0x307c
movslq (%rdx,%r14,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
leaq 0xc1f(%rip), %rdi       # 0x3458
jmp 0x2943
leaq 0x10f7(%rip), %rdi      # 0x393c
jmp 0x2943
leaq 0x11cb(%rip), %rdi      # 0x3a1c
jmp 0x295e
leaq 0xf58(%rip), %rdi       # 0x37b5
jmp 0x2943
leaq 0x12cc(%rip), %rdi      # 0x3b35
jmp 0x295e
leaq 0x1240(%rip), %rdi      # 0x3ab5
jmp 0x2943
leaq 0xe69(%rip), %rdi       # 0x36ea
jmp 0x2943
leaq 0xc46(%rip), %rdi       # 0x34d3
jmp 0x2943
leaq 0xcc0(%rip), %rdi       # 0x3559
jmp 0x2943
leaq 0x11bd(%rip), %rdi      # 0x3a62
jmp 0x295e
leaq 0xec3(%rip), %rdi       # 0x3774
jmp 0x2943
leaq 0x10e3(%rip), %rdi      # 0x39a0
jmp 0x295e
leaq 0xcd3(%rip), %rdi       # 0x359c
jmp 0x2943
leaq 0xe54(%rip), %rdi       # 0x3726
jmp 0x2943
leaq 0xc3b(%rip), %rdi       # 0x3516
jmp 0x2943
leaq 0xbb0(%rip), %rdi       # 0x3494
jmp 0x2943
leaq 0x1208(%rip), %rdi      # 0x3af5
jmp 0x2943
leaq 0xcf1(%rip), %rdi       # 0x35e7
jmp 0x2943
leaq 0xd27(%rip), %rdi       # 0x3626
jmp 0x2943
leaq 0xef1(%rip), %rdi       # 0x37f9
jmp 0x2943
leaq 0xf33(%rip), %rdi       # 0x3844
jmp 0x295e
leaq 0xd9c(%rip), %rdi       # 0x36b6
jmp 0x295e
leaq 0x124f(%rip), %rdi      # 0x3b72
movl %r12d, %esi
movl %eax, %edx
jmp 0x2963
leaq 0xd41(%rip), %rdi       # 0x3672
jmp 0x2943
leaq 0xfb8(%rip), %rdi       # 0x38f2
jmp 0x295e
leaq 0xf4b(%rip), %rdi       # 0x388e
movl %eax, %esi
movl %r13d, %edx
movl %r15d, %ecx
movl %ebp, %r8d
xorl %eax, %eax
callq 0x2070
jmp 0x296a
leaq 0x1081(%rip), %rdi      # 0x39df
movl %eax, %esi
movl %r12d, %edx
xorl %eax, %eax
callq 0x2070
movq %rsp, %rdi
movq %rbx, %r12
movq %rbx, %rsi
pushq $0x1
popq %r13
movq %r13, %rdx
movq 0x10(%rsp), %rcx
callq 0x20f0
movl (%rsp), %ebp
movq 0x40(%rsp), %r15
jmp 0x2779
movq 0x10(%rsp), %rdi
callq 0x2060
movq 0x38(%rsp), %rbx
incq %rbx
movq 0x28(%rsp), %rbp
jmp 0x2702
incq %rbp
movq 0x18(%rsp), %rbx
leaq 0x4(%rsp), %r14
jmp 0x2696
leaq 0x67e(%rip), %rdi       # 0x3044
leaq 0x50(%rsp), %rsi
jmp 0x29d8
movq 0x8(%rbx), %rsi
leaq 0x66c(%rip), %rdi       # 0x3044
xorl %eax, %eax
callq 0x2070
pushq $0x1
popq %rbx
movl %ebx, %eax
addq $0x258, %rsp            # imm = 0x258
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x118f(%rip), %rdi      # 0x3b8c
callq 0x2040
movq %r15, %rdi
callq 0x2060
xorl %ebx, %ebx
jmp 0x29e2