// File: /workspace/github_cpp_makefile_star2_O1/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 0x1566
movq %rsi, %rbx
leaq -0x99(%rip), %rsi       # 0x11fc
movl $0xa, %edi
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 0xdb7(%rip), %rdi       # 0x2074
movl %r14d, %esi
xorl %eax, %eax
callq 0x1070
movl $0x0, 0x2dcf(%rip)      # 0x40a0
movl $0x5, 0x2dd1(%rip)      # 0x40ac
movabsq $0x200000001, %rax      # imm = 0x200000001
movq %rax, 0x2db8(%rip)      # 0x40a4
movabsq $0xa0000000a, %rax      # imm = 0xA0000000A
movq %rax, 0x2db3(%rip)      # 0x40b0
movq 0x8(%rbx), %rdi
callq 0x10c0
movl %eax, 0x2d88(%rip)      # 0x4094
movq 0x10(%rbx), %rdi
callq 0x10c0
movl %eax, 0x2d7d(%rip)      # 0x4098
movq 0x18(%rbx), %rdi
callq 0x10c0
movl %eax, 0x2d72(%rip)      # 0x409c
callq 0x1050
movl %eax, %ebx
leaq 0xd70(%rip), %rdi       # 0x20a8
leaq 0xd58(%rip), %rsi       # 0x2097
callq 0x10b0
testq %rax, %rax
je 0x1579
movq %rax, %r14
leaq 0xd67(%rip), %rsi       # 0x20be
movq %rax, %rdi
movl %ebx, %edx
xorl %eax, %eax
callq 0x1090
movq %r14, %rdi
callq 0x1060
movl 0x2d23(%rip), %edi      # 0x4094
movl $0xa, %esi
callq 0x10a0
leaq 0xd7c(%rip), %rdi       # 0x20fe
callq 0x1040
movl $0x0, 0x2d27(%rip)      # 0x40b8
leaq 0x2cfc(%rip), %r15      # 0x4094
leaq 0xd13(%rip), %rbx       # 0x20b2
xorl %eax, %eax
movslq %eax, %rsi
movl (%r15,%rsi,4), %edx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x1070
movl 0x2d00(%rip), %ecx      # 0x40b8
leal 0x1(%rcx), %eax
movl %eax, 0x2cf7(%rip)      # 0x40b8
cmpl $0x2, %ecx
jl 0x13a1
leaq 0xd5d(%rip), %rdi       # 0x212a
callq 0x1040
movl $0x0, 0x2cdc(%rip)      # 0x40b8
leaq 0x2cbd(%rip), %r12      # 0x40a0
leaq 0x2cc2(%rip), %r13      # 0x40ac
leaq 0xcc8(%rip), %rbx       # 0x20b9
xorl %eax, %eax
cltq
movl (%r12,%rax,4), %esi
movl (%r13,%rax,4), %edx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x1070
movl 0x2caa(%rip), %ecx      # 0x40b8
leal 0x1(%rcx), %eax
movl %eax, 0x2ca1(%rip)      # 0x40b8
cmpl $0x2, %ecx
jl 0x13f3
leaq 0xd43(%rip), %rdi       # 0x2166
callq 0x1040
movl $0x0, 0x2c86(%rip)      # 0x40b8
leaq 0xc89(%rip), %rbx       # 0x20c2
xorl %eax, %eax
movslq %eax, %rsi
movl (%r15,%rsi,4), %edx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x1070
movslq 0x2c65(%rip), %rax      # 0x40b8
movl (%r15,%rax,4), %edi
movl $0x13, %esi
callq 0x10a0
movl 0x2c51(%rip), %ecx      # 0x40b8
leal 0x1(%rcx), %eax
movl %eax, 0x2c48(%rip)      # 0x40b8
cmpl $0x2, %ecx
jl 0x143b
leaq 0xd1b(%rip), %rdi       # 0x2197
callq 0x1040
leaq 0xd46(%rip), %rdi       # 0x21ce
callq 0x1040
movl $0x5, %edi
callq 0x10e0
leaq 0xd67(%rip), %rdi       # 0x2205
callq 0x1040
movl $0x0, 0x2c0f(%rip)      # 0x40bc
leaq 0xc22(%rip), %rbx       # 0x20d6
leaq 0xc39(%rip), %r14       # 0x20f4
movslq 0x2bfa(%rip), %rsi      # 0x40bc
movslq (%r12,%rsi,4), %rax
movl (%r15,%rax,4), %edx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x1070
movslq 0x2be1(%rip), %rax      # 0x40bc
movslq (%r12,%rax,4), %rax
movl (%r15,%rax,4), %edi
movl $0x12, %esi
callq 0x10a0
movslq 0x2bc8(%rip), %rax      # 0x40bc
movl (%r13,%rax,4), %esi
movq %r14, %rdi
xorl %eax, %eax
callq 0x1070
movslq 0x2bb2(%rip), %rax      # 0x40bc
imull $0x3e8, (%r13,%rax,4), %edi # imm = 0x3E8
callq 0x10f0
movslq 0x2b9d(%rip), %rax      # 0x40bc
movslq (%r12,%rax,4), %rax
movl (%r15,%rax,4), %edi
movl $0x13, %esi
callq 0x10a0
movl 0x2b85(%rip), %eax      # 0x40bc
movl %eax, %ecx
incl %ecx
movslq %ecx, %rcx
imulq $0x55555556, %rcx, %rcx # imm = 0x55555556
movq %rcx, %rdx
shrq $0x3f, %rdx
shrq $0x20, %rcx
addl %edx, %ecx
leal (%rcx,%rcx,2), %ecx
negl %ecx
addl %ecx, %eax
incl %eax
movl %eax, 0x2b5b(%rip)      # 0x40bc
jmp 0x14bb
leaq 0xac4(%rip), %rdi       # 0x2031
movl %r14d, %esi
xorl %eax, %eax
callq 0x1070
jmp 0x1587
leaq 0xb19(%rip), %rdi       # 0x2099
xorl %eax, %eax
callq 0x1070
movl $0x1, %edi
callq 0x10d0