// File: /workspace/github_cpp_makefile_star2_O2/CDISC2020[P]Projet_satellite_agile/src/ARINC/kernel_arinc.c
// Function: main
int main(int argc, char *argv[]) 
{
	if ((argc==0) || (argc==1) ) {
		printf("Ooops! T'as oublié les arguments, les pid des process! (argc=%d)\n", argc);
		exit(1);
	}
	
	signal(SIGUSR1, update_pids);

	printf("*** TRACE : %d %s %s %s %s %s %s \n", argc, argv[0], argv[1],argv[2],argv[3],argv[4],argv[5]);

	// Initialisation of P_in_frame and Tframe (A)
	P_in_frame[0]=0; 	Tframe[0]=SFDIR1;
	P_in_frame[1]=1; 	Tframe[1]=SCOM1;
	P_in_frame[2]=2;	Tframe[2]=SPM1;

	// Reading partition ids (pids) (N)
		pid[0] = atoi(argv[1]) ; // FIRST process-partition P1
		pid[1] = atoi(argv[2]) ; // SECOND process-partition P2
		pid[2] = atoi(argv[3]) ; // THIRD process-partition P3


	// Writing ARINC pid in a file
	int pid_ARINC=getpid();
	FILE* fichierPid=NULL;
	fichierPid=fopen("pid_ARINC","w");
	if(!fichierPid)
	{
		printf("Failed to open pid_ARINC");
		exit(1);
	}
	fprintf(fichierPid,"%d\n",pid_ARINC);
	fclose(fichierPid);
	kill(pid[0], SIGUSR1); // telling FDIR it's done so it can read it



	// Main body of the program
	printf("********* LIST OF PARTITION IDS************\n");

	for (i=0; i<N; i++)
		printf("pids - P%d:	%d\n",i,pid[i]);

	printf("********* LIST OF PARTITION IN THE TIME FRAME  ************\n");

	for (i=0; i<A; i++)
		printf("P%d:	%d\n",P_in_frame[i],Tframe[i]);

	printf("********* STOPPING ALL PARTITION IDS************\n");

	for (i=0; i<N; i++){
		printf("STOP - P%d:     %d\n",i,pid[i]);
		kill (pid[i],  SIGSTOP);
	}

	printf("********* WAITING BEF0RE SCHEDULING LOOP  ************\n");
	printf("********* All partitions must be stopped  ************\n");

	sleep (5);



	printf("********* STARTING SCHEDULING LOOP ************\n");


	active_p=0; // active process active_p set to 0 (1st process in major frame)

	while (1) {  // Scheduler infinite loop

		// next process to schedule
		printf("Active P(%d) – PID: %d – ", active_p, pid[P_in_frame[active_p]]);
		kill (pid[P_in_frame [active_p]], SIGCONT);

		// scheduler waiting for active_p time budget
		printf("T: %d ms\n", Tframe[active_p]);
		usleep (Tframe[active_p]*1000); // parameter in usec

		// stop active_p after time budget elapsed
		kill (pid[P_in_frame[active_p]], SIGSTOP);

		// set active_p to next (modulo A, number of partitions per time frame)
		active_p=(active_p + 1) % A;
		
	} // end of while - end of main loop

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %edi, %r14d
cmpl $0x1, %edi
jbe 0x1528
movq %rsi, %rbx
leaq -0x99(%rip), %rsi       # 0x11fc
pushq $0xa
popq %rdi
callq 0x1080
movq (%rbx), %rdx
movq 0x8(%rbx), %rcx
movq 0x10(%rbx), %r8
movq 0x18(%rbx), %r9
movups 0x20(%rbx), %xmm0
movups %xmm0, (%rsp)
leaq 0xdb9(%rip), %rdi       # 0x2074
movl %r14d, %esi
xorl %eax, %eax
callq 0x1070
andl $0x0, 0x2dd4(%rip)      # 0x40a0
movl $0x5, 0x2dd6(%rip)      # 0x40ac
movabsq $0x200000001, %rax      # imm = 0x200000001
movq %rax, 0x2dbd(%rip)      # 0x40a4
movabsq $0xa0000000a, %rax      # imm = 0xA0000000A
movq %rax, 0x2db8(%rip)      # 0x40b0
movq 0x8(%rbx), %rdi
callq 0x10c0
movl %eax, 0x2d8d(%rip)      # 0x4094
movq 0x10(%rbx), %rdi
callq 0x10c0
movl %eax, 0x2d82(%rip)      # 0x4098
movq 0x18(%rbx), %rdi
callq 0x10c0
movl %eax, 0x2d77(%rip)      # 0x409c
callq 0x1050
movl %eax, %ebx
leaq 0xd75(%rip), %rdi       # 0x20a8
leaq 0xd5d(%rip), %rsi       # 0x2097
callq 0x10b0
testq %rax, %rax
je 0x153b
movq %rax, %r14
leaq 0xd6c(%rip), %rsi       # 0x20be
movq %rax, %rdi
movl %ebx, %edx
xorl %eax, %eax
callq 0x1090
movq %r14, %rdi
callq 0x1060
movl 0x2d28(%rip), %edi      # 0x4094
pushq $0xa
popq %rsi
callq 0x10a0
leaq 0xd83(%rip), %rdi       # 0x20fe
callq 0x1040
leaq 0x2d0d(%rip), %r13      # 0x4094
leaq 0xd24(%rip), %rbx       # 0x20b2
xorl %esi, %esi
movl %esi, 0x2d22(%rip)      # 0x40b8
cmpl $0x2, %esi
jg 0x13b7
movslq %esi, %rax
movl (%r13,%rax,4), %edx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x1070
movl 0x2d05(%rip), %esi      # 0x40b8
incl %esi
jmp 0x1390
leaq 0xd6c(%rip), %rdi       # 0x212a
callq 0x1040
leaq 0x2cd6(%rip), %rbx      # 0x40a0
leaq 0x2cdb(%rip), %r15      # 0x40ac
leaq 0xce1(%rip), %r14       # 0x20b9
xorl %eax, %eax
movl %eax, 0x2cd8(%rip)      # 0x40b8
cmpl $0x2, %eax
jg 0x1402
cltq
movl (%rbx,%rax,4), %esi
movl (%r15,%rax,4), %edx
movq %r14, %rdi
xorl %eax, %eax
callq 0x1070
movl 0x2cba(%rip), %eax      # 0x40b8
incl %eax
jmp 0x13da
leaq 0xd5d(%rip), %rdi       # 0x2166
callq 0x1040
leaq 0xcad(%rip), %r15       # 0x20c2
pushq $0x13
popq %r14
xorl %esi, %esi
movl %esi, 0x2c97(%rip)      # 0x40b8
cmpl $0x2, %esi
jg 0x1456
movslq %esi, %rax
movl (%r13,%rax,4), %edx
movq %r15, %rdi
xorl %eax, %eax
callq 0x1070
movslq 0x2c79(%rip), %rax      # 0x40b8
movl (%r13,%rax,4), %edi
movl %r14d, %esi
callq 0x10a0
movl 0x2c66(%rip), %esi      # 0x40b8
incl %esi
jmp 0x141b
leaq 0xd3a(%rip), %rdi       # 0x2197
callq 0x1040
leaq 0xd65(%rip), %rdi       # 0x21ce
callq 0x1040
pushq $0x5
popq %rdi
callq 0x10e0
leaq 0xd88(%rip), %rdi       # 0x2205
callq 0x1040
andl $0x0, 0x2c33(%rip)      # 0x40bc
pushq $0x13
popq %r12
pushq $0x3
popq %r14
xorl %edx, %edx
leaq 0x2c12(%rip), %r15      # 0x40ac
movslq %edx, %rsi
movslq (%rbx,%rsi,4), %rax
movl (%r13,%rax,4), %edx
leaq 0xc29(%rip), %rdi       # 0x20d6
xorl %eax, %eax
callq 0x1070
movslq 0x2c01(%rip), %rax      # 0x40bc
movslq (%rbx,%rax,4), %rax
movl (%r13,%rax,4), %edi
pushq $0x12
popq %rsi
callq 0x10a0
movslq 0x2be9(%rip), %rax      # 0x40bc
movl (%r15,%rax,4), %esi
leaq 0xc16(%rip), %rdi       # 0x20f4
xorl %eax, %eax
callq 0x1070
movslq 0x2bd0(%rip), %rax      # 0x40bc
imull $0x3e8, (%r15,%rax,4), %edi # imm = 0x3E8
callq 0x10f0
movslq 0x2bbc(%rip), %rax      # 0x40bc
movslq (%rbx,%rax,4), %rax
movl (%r13,%rax,4), %edi
movl %r12d, %esi
callq 0x10a0
movl 0x2ba5(%rip), %eax      # 0x40bc
incl %eax
cltd
idivl %r14d
movl %edx, 0x2b99(%rip)      # 0x40bc
jmp 0x149a
leaq 0xb02(%rip), %rdi       # 0x2031
movl %r14d, %esi
xorl %eax, %eax
callq 0x1070
jmp 0x1549
leaq 0xb57(%rip), %rdi       # 0x2099
xorl %eax, %eax
callq 0x1070
pushq $0x1
popq %rdi
callq 0x10d0