// File: 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
subq $0x30, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x0, -0x8(%rbp)
je 0x12c2
cmpl $0x1, -0x8(%rbp)
jne 0x12dd
movl -0x8(%rbp), %esi
leaq 0xd65(%rip), %rdi       # 0x2031
movb $0x0, %al
callq 0x1060
movl $0x1, %edi
callq 0x10d0
movl $0xa, %edi
leaq -0xe9(%rip), %rsi       # 0x1200
callq 0x1080
movl -0x8(%rbp), %esi
movq -0x10(%rbp), %rax
movq (%rax), %rdx
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x10(%rbp), %rax
movq 0x10(%rax), %r8
movq -0x10(%rbp), %rax
movq 0x18(%rax), %r9
movq -0x10(%rbp), %rax
movq 0x20(%rax), %r10
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
leaq 0xd4d(%rip), %rdi       # 0x2074
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
movb $0x0, %al
callq 0x1060
movl $0x0, 0x2d5f(%rip)      # 0x40a0
movl $0x5, 0x2d61(%rip)      # 0x40ac
movl $0x1, 0x2d4f(%rip)      # 0x40a4
movl $0xa, 0x2d51(%rip)      # 0x40b0
movl $0x2, 0x2d3f(%rip)      # 0x40a8
movl $0xa, 0x2d41(%rip)      # 0x40b4
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x10c0
movl %eax, 0x2d0e(%rip)      # 0x4094
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x10c0
movl %eax, 0x2cff(%rip)      # 0x4098
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x10c0
movl %eax, 0x2cf0(%rip)      # 0x409c
callq 0x1040
movl %eax, -0x14(%rbp)
movq $0x0, -0x20(%rbp)
leaq 0xce5(%rip), %rdi       # 0x20a8
leaq 0xccd(%rip), %rsi       # 0x2097
callq 0x10b0
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0x13f2
leaq 0xcb8(%rip), %rdi       # 0x2099
movb $0x0, %al
callq 0x1060
movl $0x1, %edi
callq 0x10d0
movq -0x20(%rbp), %rdi
movl -0x14(%rbp), %edx
leaq 0xceb(%rip), %rsi       # 0x20eb
movb $0x0, %al
callq 0x1090
movq -0x20(%rbp), %rdi
callq 0x1050
movl 0x2c7e(%rip), %edi      # 0x4094
movl $0xa, %esi
callq 0x10a0
leaq 0xc8b(%rip), %rdi       # 0x20b2
movb $0x0, %al
callq 0x1060
movl $0x0, 0x2c80(%rip)      # 0x40b8
cmpl $0x3, 0x2c79(%rip)      # 0x40b8
jge 0x1477
movl 0x2c71(%rip), %esi      # 0x40b8
movslq 0x2c6a(%rip), %rcx      # 0x40b8
leaq 0x2c3f(%rip), %rax      # 0x4094
movl (%rax,%rcx,4), %edx
leaq 0xc80(%rip), %rdi       # 0x20df
movb $0x0, %al
callq 0x1060
movl 0x2c4c(%rip), %eax      # 0x40b8
addl $0x1, %eax
movl %eax, 0x2c43(%rip)      # 0x40b8
jmp 0x1438
leaq 0xc71(%rip), %rdi       # 0x20ef
movb $0x0, %al
callq 0x1060
movl $0x0, 0x2c29(%rip)      # 0x40b8
cmpl $0x3, 0x2c22(%rip)      # 0x40b8
jge 0x14d9
movslq 0x2c19(%rip), %rcx      # 0x40b8
leaq 0x2bfa(%rip), %rax      # 0x40a0
movl (%rax,%rcx,4), %esi
movslq 0x2c08(%rip), %rcx      # 0x40b8
leaq 0x2bf5(%rip), %rax      # 0x40ac
movl (%rax,%rcx,4), %edx
leaq 0xc25(%rip), %rdi       # 0x20e6
movb $0x0, %al
callq 0x1060
movl 0x2bea(%rip), %eax      # 0x40b8
addl $0x1, %eax
movl %eax, 0x2be1(%rip)      # 0x40b8
jmp 0x148f
leaq 0xc4c(%rip), %rdi       # 0x212c
movb $0x0, %al
callq 0x1060
movl $0x0, 0x2bc7(%rip)      # 0x40b8
cmpl $0x3, 0x2bc0(%rip)      # 0x40b8
jge 0x154b
movl 0x2bb8(%rip), %esi      # 0x40b8
movslq 0x2bb1(%rip), %rcx      # 0x40b8
leaq 0x2b86(%rip), %rax      # 0x4094
movl (%rax,%rcx,4), %edx
leaq 0xc46(%rip), %rdi       # 0x215e
movb $0x0, %al
callq 0x1060
movslq 0x2b92(%rip), %rcx      # 0x40b8
leaq 0x2b67(%rip), %rax      # 0x4094
movl (%rax,%rcx,4), %edi
movl $0x13, %esi
callq 0x10a0
movl 0x2b78(%rip), %eax      # 0x40b8
addl $0x1, %eax
movl %eax, 0x2b6f(%rip)      # 0x40b8
jmp 0x14f1
leaq 0xc20(%rip), %rdi       # 0x2172
movb $0x0, %al
callq 0x1060
leaq 0xc4a(%rip), %rdi       # 0x21aa
movb $0x0, %al
callq 0x1060
movl $0x5, %edi
callq 0x10e0
leaq 0xc6a(%rip), %rdi       # 0x21e2
movb $0x0, %al
callq 0x1060
movl $0x0, 0x2b33(%rip)      # 0x40bc
movl 0x2b2d(%rip), %esi      # 0x40bc
movslq 0x2b26(%rip), %rcx      # 0x40bc
leaq 0x2b03(%rip), %rax      # 0x40a0
movslq (%rax,%rcx,4), %rcx
leaq 0x2aec(%rip), %rax      # 0x4094
movl (%rax,%rcx,4), %edx
leaq 0xc61(%rip), %rdi       # 0x2213
movb $0x0, %al
callq 0x1060
movslq 0x2afc(%rip), %rcx      # 0x40bc
leaq 0x2ad9(%rip), %rax      # 0x40a0
movslq (%rax,%rcx,4), %rcx
leaq 0x2ac2(%rip), %rax      # 0x4094
movl (%rax,%rcx,4), %edi
movl $0x12, %esi
callq 0x10a0
movslq 0x2ad6(%rip), %rcx      # 0x40bc
leaq 0x2abf(%rip), %rax      # 0x40ac
movl (%rax,%rcx,4), %esi
leaq 0xc3a(%rip), %rdi       # 0x2231
movb $0x0, %al
callq 0x1060
movslq 0x2ab7(%rip), %rcx      # 0x40bc
leaq 0x2aa0(%rip), %rax      # 0x40ac
imull $0x3e8, (%rax,%rcx,4), %edi # imm = 0x3E8
callq 0x10f0
movslq 0x2a9d(%rip), %rcx      # 0x40bc
leaq 0x2a7a(%rip), %rax      # 0x40a0
movslq (%rax,%rcx,4), %rcx
leaq 0x2a63(%rip), %rax      # 0x4094
movl (%rax,%rcx,4), %edi
movl $0x13, %esi
callq 0x10a0
movl 0x2a78(%rip), %eax      # 0x40bc
addl $0x1, %eax
movl $0x3, %ecx
cltd
idivl %ecx
movl %edx, 0x2a67(%rip)      # 0x40bc
jmp 0x1589