#include "system.cuh"
#include <assert.h>
#include <string>

__device__ __host__ void job::job_release(int time) {
	assert(_state == Created);
	_release_time = time;
	_state = Ready;
}

__device__ __host__ void job::activate() {
	assert(_state == Ready);
	_state = Running;
}

__device__ __host__ void job::preempt() {
	assert(_state == Running);
	_state = Ready;
}

__device__ __host__ void job::terminate(int time)
{
	assert(_state == Completed);
	_response_time = time;
	if (missed())
	{
		//TODO: add simulator funciton
	}
}

__device__ __host__ int job::get_deadline() {
	return _task->_relative_deadline + _release_time;
}

__device__ __host__ int job::get_remain() { return _task->_execution_time - _executed_cycle; }

__device__ __host__ bool job::completed() { return _state == Completed; }

__device__ __host__ bool job::missed() {
	if (completed()) {
		return _response_time > get_deadline();
	}
	return false;
}

__device__ void job::run()
{
	assert(_state == Running);
	_executed_cycle++;
	if (get_remain() == 0)
	{
		_state = Completed;
	}
}

STATE job::get_state() { return _state; }
	.file	"tmpxft_002a1fc9_00000000-6_system.cudafe1.cpp"
	.text
#APP
	.globl _ZSt21ios_base_library_initv

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB3656:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE3656:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.align 2
	.globl	_ZN3job11job_releaseEi
	.type	_ZN3job11job_releaseEi, @function
_ZN3job11job_releaseEi:
.LFB3644:
	.cfi_startproc
	endbr64
	movl	%esi, 12(%rdi)
	movl	$1, 20(%rdi)
	ret
	.cfi_endproc
.LFE3644:
	.size	_ZN3job11job_releaseEi, .-_ZN3job11job_releaseEi
	.align 2
	.globl	_ZN3job8activateEv
	.type	_ZN3job8activateEv, @function
_ZN3job8activateEv:
.LFB3645:
	.cfi_startproc
	endbr64
	movl	$2, 20(%rdi)
	ret
	.cfi_endproc
.LFE3645:
	.size	_ZN3job8activateEv, .-_ZN3job8activateEv
	.align 2
	.globl	_ZN3job7preemptEv
	.type	_ZN3job7preemptEv, @function
_ZN3job7preemptEv:
.LFB3646:
	.cfi_startproc
	endbr64
	movl	$1, 20(%rdi)
	ret
	.cfi_endproc
.LFE3646:
	.size	_ZN3job7preemptEv, .-_ZN3job7preemptEv
	.align 2
	.globl	_ZN3job9terminateEi
	.type	_ZN3job9terminateEi, @function
_ZN3job9terminateEi:
.LFB3647:
	.cfi_startproc
	endbr64
	movl	%esi, 16(%rdi)
	ret
	.cfi_endproc
.LFE3647:
	.size	_ZN3job9terminateEi, .-_ZN3job9terminateEi
	.align 2
	.globl	_ZN3job12get_deadlineEv
	.type	_ZN3job12get_deadlineEv, @function
_ZN3job12get_deadlineEv:
.LFB3648:
	.cfi_startproc
	endbr64
	movq	(%rdi), %rdx
	movl	12(%rdi), %eax
	addl	8(%rdx), %eax
	ret
	.cfi_endproc
.LFE3648:
	.size	_ZN3job12get_deadlineEv, .-_ZN3job12get_deadlineEv
	.align 2
	.globl	_ZN3job10get_remainEv
	.type	_ZN3job10get_remainEv, @function
_ZN3job10get_remainEv:
.LFB3649:
	.cfi_startproc
	endbr64
	movq	(%rdi), %rax
	movl	12(%rax), %eax
	subl	8(%rdi), %eax
	ret
	.cfi_endproc
.LFE3649:
	.size	_ZN3job10get_remainEv, .-_ZN3job10get_remainEv
	.align 2
	.globl	_ZN3job9completedEv
	.type	_ZN3job9completedEv, @function
_ZN3job9completedEv:
.LFB3650:
	.cfi_startproc
	endbr64
	cmpl	$4, 20(%rdi)
	sete	%al
	ret
	.cfi_endproc
.LFE3650:
	.size	_ZN3job9completedEv, .-_ZN3job9completedEv
	.align 2
	.globl	_ZN3job6missedEv
	.type	_ZN3job6missedEv, @function
_ZN3job6missedEv:
.LFB3651:
	.cfi_startproc
	endbr64
	xorl	%eax, %eax
	cmpl	$4, 20(%rdi)
	jne	.L9
	movq	(%rdi), %rdx
	movl	12(%rdi), %eax
	addl	8(%rdx), %eax
	cmpl	%eax, 16(%rdi)
	setg	%al
.L9:
	ret
	.cfi_endproc
.LFE3651:
	.size	_ZN3job6missedEv, .-_ZN3job6missedEv
	.align 2
	.globl	_ZN3job3runEv
	.type	_ZN3job3runEv, @function
_ZN3job3runEv:
.LFB3652:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	popq	%rax
	.cfi_def_cfa_offset 8
	subq	$24, %rsp
	.cfi_def_cfa_offset 32
	movl	$1, 12(%rsp)
	movl	12(%rsp), %edi
	call	exit@PLT
	.cfi_endproc
.LFE3652:
	.size	_ZN3job3runEv, .-_ZN3job3runEv
	.align 2
	.globl	_ZN3job9get_stateEv
	.type	_ZN3job9get_stateEv, @function
_ZN3job9get_stateEv:
.LFB3653:
	.cfi_startproc
	endbr64
	movl	20(%rdi), %eax
	ret
	.cfi_endproc
.LFE3653:
	.size	_ZN3job9get_stateEv, .-_ZN3job9get_stateEv
	.section	.text.startup,"ax",@progbits
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB3679:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	leaq	_ZL15__fatDeviceText(%rip), %rdi
	call	__cudaRegisterFatBinary@PLT
	movq	%rax, %rdi
	movq	%rax, _ZL20__cudaFatCubinHandle(%rip)
	call	__cudaRegisterFatBinaryEnd@PLT
	leaq	_ZL26__cudaUnregisterBinaryUtilv(%rip), %rdi
	popq	%rdx
	.cfi_def_cfa_offset 8
	jmp	atexit@PLT
	.cfi_endproc
.LFE3679:
	.size	_ZL24__sti____cudaRegisterAllv, .-_ZL24__sti____cudaRegisterAllv
	.section	.init_array,"aw"
	.align 8
	.quad	_ZL24__sti____cudaRegisterAllv
	.section	.nvFatBinSegment,"aw"
	.align 8
	.type	_ZL15__fatDeviceText, @object
	.size	_ZL15__fatDeviceText, 24
_ZL15__fatDeviceText:
	.long	1180844977
	.long	1
	.quad	fatbinData
	.quad	0
	.local	_ZL20__cudaFatCubinHandle
	.comm	_ZL20__cudaFatCubinHandle,8,8
	.ident	"GCC: (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	1f - 0f
	.long	4f - 1f
	.long	5
0:
	.string	"GNU"
1:
	.align 8
	.long	0xc0000002
	.long	3f - 2f
2:
	.long	0x3
3:
	.align 8
4:

#ifndef _SYSTEM_H_CUDA
#define _SYSTEM_H_CUDA

#include <iostream>
#include <string>
#include <assert.h>

using namespace std;


enum STATE { Created, Ready, Running, Waiting, Completed };


__device__ struct task {
	__device__ __host__ task() {}
	__device__ __host__ task(int phase, int period, int relative_deadline, int execution_time) : _phase(phase), _period(period), _relative_deadline(relative_deadline), _execution_time(execution_time) {}
	int _phase;
	int _period;
	int _relative_deadline;
	int _execution_time;
};


class Taskset {

};

class job
{
public:
	__device__ __host__ job(task* tsk)
	{
		_task = tsk; // TODO: check if copy works
		_executed_cycle = 0;
		_state = Created;
	}

	__device__ __host__ void job_release(int time);
	__device__ __host__ void activate();
	__device__ __host__ int get_deadline();
	__device__ __host__ int get_remain();
	__device__ __host__ bool completed();
	__device__ __host__ bool missed();
	__device__ __host__ void preempt();
	__device__ __host__ void terminate(int time);
	__device__ void run();
	STATE get_state();

private:
	task* _task;
	int _executed_cycle;
	int _release_time;
	int _response_time;
	STATE _state;
};

#endif // _SYSTEM_H_CUDA
	.text
	.file	"system.hip"
                                        # Start of file scope inline assembly
	.globl	_ZSt21ios_base_library_initv

                                        # End of file scope inline assembly
	.type	__hip_cuid_,@object             # @__hip_cuid_
	.bss
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               # 0x0
	.size	__hip_cuid_, 1

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym __hip_cuid_

	.text
	.p2alignl 7, 3214868480
	.fill 96, 4, 3214868480
	.type	__hip_cuid_,@object             ; @__hip_cuid_
	.section	.bss,"aw",@nobits
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               ; 0x0
	.size	__hip_cuid_, 1

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym __hip_cuid_
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.amdgpu_metadata
---
amdhsa.kernels:  []
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
