// File: Ziang95[P]Tomasulo_simulator/clock.cpp
// Function: clk_tick::clk_automat(int)
bool clk_tick::clk_automat(int freq)
{
    if (freq > 500)
    {
        err_log("Frequency should be no greater than 500");
        return false;
    }
    while (true)
    {
        while (!all_unit_ready_for_one())
        {
            string outp = "";
            for (auto a : clk_wait_list)
                outp += to_string(*a) + ",";
            msg_log("In waiting for vdd = 1, " + outp, 4);
            #ifdef _WIN32
            Sleep(500/freq);
            #else
            usleep(500/freq);
            #endif
        }
        pthread_mutex_lock(&boradcast_lock);
        vdd = 1;
        prog_cyc++;
        if (prog_cyc == 1)
            msg_log("Program Started\n", 0);
        pthread_cond_broadcast(&vdd_1);
        pthread_mutex_unlock(&boradcast_lock);
        while (!all_unit_ready_for_zero())
        {
            string outp = "";
            for (auto a : clk_wait_list)
                outp += to_string(*a) + ",";
            msg_log("In waiting for vdd = 0, " + outp, 4);
            #ifdef _WIN32
            Sleep(500/freq);
            #else
            usleep(500/freq);
            #endif
        }
        pthread_mutex_lock(&boradcast_lock);
        vdd = 0;
        pthread_cond_broadcast(&vdd_0);
        pthread_mutex_unlock(&boradcast_lock);
        if (instr_ended && mem_ended)
            return true;
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1f0, %rsp            # imm = 0x1F0
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x1d0(%rbp)
cmpl $0x1f4, -0x14(%rbp)     # imm = 0x1F4
jle 0x11b0a
leaq -0x39(%rbp), %rdi
movq %rdi, -0x1d8(%rbp)
callq 0x4600
movq -0x1d8(%rbp), %rdx
leaq 0x10820(%rip), %rsi     # 0x222c8
leaq -0x38(%rbp), %rdi
callq 0x44c0
jmp 0x11ab3
leaq -0x38(%rbp), %rdi
callq 0x11880
jmp 0x11abe
leaq -0x38(%rbp), %rdi
callq 0x4240
leaq -0x39(%rbp), %rdi
callq 0x43d0
movb $0x0, -0x1(%rbp)
jmp 0x1203d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x11afc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x4240
leaq -0x39(%rbp), %rdi
callq 0x43d0
jmp 0x1204b
jmp 0x11b0c
jmp 0x11b0e
callq 0x11760
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x11b24
jmp 0x11d07
leaq -0x71(%rbp), %rdi
movq %rdi, -0x1e0(%rbp)
callq 0x4600
movq -0x1e0(%rbp), %rdx
leaq 0x10e60(%rip), %rsi     # 0x229a2
leaq -0x70(%rbp), %rdi
callq 0x44c0
jmp 0x11b4d
leaq -0x71(%rbp), %rdi
callq 0x43d0
leaq 0x1e2ab(%rip), %rax     # 0x2fe08
movq %rax, -0x80(%rbp)
leaq 0x1e2a0(%rip), %rdi     # 0x2fe08
callq 0x9c40
movq %rax, -0x88(%rbp)
leaq 0x1e28d(%rip), %rdi     # 0x2fe08
callq 0x9ad0
movq %rax, -0x90(%rbp)
leaq -0x88(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0x122d0
testb $0x1, %al
jne 0x11ba3
jmp 0x11c86
leaq -0x88(%rbp), %rdi
callq 0x12310
movq (%rax), %rax
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rax
movl (%rax), %esi
leaq -0xd8(%rbp), %rdi
callq 0x8410
jmp 0x11bd0
leaq 0x10719(%rip), %rdx     # 0x222f0
leaq -0xb8(%rbp), %rdi
leaq -0xd8(%rbp), %rsi
callq 0x8810
jmp 0x11bec
leaq -0x70(%rbp), %rdi
leaq -0xb8(%rbp), %rsi
callq 0x4460
jmp 0x11bfe
leaq -0xb8(%rbp), %rdi
callq 0x4240
leaq -0xd8(%rbp), %rdi
callq 0x4240
leaq -0x88(%rbp), %rdi
callq 0x12330
jmp 0x11b87
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x71(%rbp), %rdi
callq 0x43d0
jmp 0x1204b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x11cf9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x11c78
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0xb8(%rbp), %rdi
callq 0x4240
leaq -0xd8(%rbp), %rdi
callq 0x4240
jmp 0x11cf9
leaq 0x10665(%rip), %rsi     # 0x222f2
leaq -0xf8(%rbp), %rdi
leaq -0x70(%rbp), %rdx
callq 0xf080
jmp 0x11c9f
leaq -0xf8(%rbp), %rdi
movl $0x4, %esi
callq 0x12060
jmp 0x11cb2
leaq -0xf8(%rbp), %rdi
callq 0x4240
movl -0x14(%rbp), %ecx
xorl %edx, %edx
movl $0x1f4, %eax            # imm = 0x1F4
idivl %ecx
movl %eax, %edi
callq 0x44e0
jmp 0x11cd3
leaq -0x70(%rbp), %rdi
callq 0x4240
jmp 0x11b0e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0xf8(%rbp), %rdi
callq 0x4240
leaq -0x70(%rbp), %rdi
callq 0x4240
jmp 0x1204b
leaq 0x1d6a2(%rip), %rdi     # 0x2f3b0
callq 0x4510
movq -0x1d0(%rbp), %rax
movb $0x1, (%rax)
movl 0x4(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x4(%rax)
cmpl $0x1, 0x4(%rax)
jne 0x11dc0
leaq -0x119(%rbp), %rdi
movq %rdi, -0x1e8(%rbp)
callq 0x4600
movq -0x1e8(%rbp), %rdx
leaq 0x105ba(%rip), %rsi     # 0x2230b
leaq -0x118(%rbp), %rdi
callq 0x44c0
jmp 0x11d5f
leaq -0x118(%rbp), %rdi
xorl %esi, %esi
callq 0x12060
jmp 0x11d6f
leaq -0x118(%rbp), %rdi
callq 0x4240
leaq -0x119(%rbp), %rdi
callq 0x43d0
jmp 0x11dc0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x11daf
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x118(%rbp), %rdi
callq 0x4240
leaq -0x119(%rbp), %rdi
callq 0x43d0
jmp 0x1204b
movq -0x1d0(%rbp), %rdi
addq $0x10, %rdi
callq 0x4130
leaq 0x1d5d9(%rip), %rdi     # 0x2f3b0
callq 0x42c0
callq 0x11940
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x11df2
jmp 0x11fed
leaq -0x141(%rbp), %rdi
movq %rdi, -0x1f0(%rbp)
callq 0x4600
movq -0x1f0(%rbp), %rdx
leaq 0x10b8f(%rip), %rsi     # 0x229a2
leaq -0x140(%rbp), %rdi
callq 0x44c0
jmp 0x11e21
leaq -0x141(%rbp), %rdi
callq 0x43d0
leaq 0x1dfd4(%rip), %rax     # 0x2fe08
movq %rax, -0x150(%rbp)
leaq 0x1dfc6(%rip), %rdi     # 0x2fe08
callq 0x9c40
movq %rax, -0x158(%rbp)
leaq 0x1dfb3(%rip), %rdi     # 0x2fe08
callq 0x9ad0
movq %rax, -0x160(%rbp)
leaq -0x158(%rbp), %rdi
leaq -0x160(%rbp), %rsi
callq 0x122d0
testb $0x1, %al
jne 0x11e7d
jmp 0x11f66
leaq -0x158(%rbp), %rdi
callq 0x12310
movq (%rax), %rax
movq %rax, -0x168(%rbp)
movq -0x168(%rbp), %rax
movl (%rax), %esi
leaq -0x1a8(%rbp), %rdi
callq 0x8410
jmp 0x11eaa
leaq 0x1043f(%rip), %rdx     # 0x222f0
leaq -0x188(%rbp), %rdi
leaq -0x1a8(%rbp), %rsi
callq 0x8810
jmp 0x11ec6
leaq -0x140(%rbp), %rdi
leaq -0x188(%rbp), %rsi
callq 0x4460
jmp 0x11edb
leaq -0x188(%rbp), %rdi
callq 0x4240
leaq -0x1a8(%rbp), %rdi
callq 0x4240
leaq -0x158(%rbp), %rdi
callq 0x12330
jmp 0x11e61
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x141(%rbp), %rdi
callq 0x43d0
jmp 0x1204b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x11fdf
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x11f58
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x188(%rbp), %rdi
callq 0x4240
leaq -0x1a8(%rbp), %rdi
callq 0x4240
jmp 0x11fdf
leaq 0x103af(%rip), %rsi     # 0x2231c
leaq -0x1c8(%rbp), %rdi
leaq -0x140(%rbp), %rdx
callq 0xf080
jmp 0x11f82
leaq -0x1c8(%rbp), %rdi
movl $0x4, %esi
callq 0x12060
jmp 0x11f95
leaq -0x1c8(%rbp), %rdi
callq 0x4240
movl -0x14(%rbp), %ecx
xorl %edx, %edx
movl $0x1f4, %eax            # imm = 0x1F4
idivl %ecx
movl %eax, %edi
callq 0x44e0
jmp 0x11fb6
leaq -0x140(%rbp), %rdi
callq 0x4240
jmp 0x11ddc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x1c8(%rbp), %rdi
callq 0x4240
leaq -0x140(%rbp), %rdi
callq 0x4240
jmp 0x1204b
leaq 0x1d3bc(%rip), %rdi     # 0x2f3b0
callq 0x4510
movq -0x1d0(%rbp), %rdi
movb $0x0, (%rdi)
addq $0x40, %rdi
callq 0x4130
leaq 0x1d39d(%rip), %rdi     # 0x2f3b0
callq 0x42c0
movq -0x1d0(%rbp), %rax
testb $0x1, 0x2(%rax)
je 0x12038
movq -0x1d0(%rbp), %rax
testb $0x1, 0x3(%rax)
je 0x12038
movb $0x1, -0x1(%rbp)
jmp 0x1203d
jmp 0x11b0c
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x1f0, %rsp            # imm = 0x1F0
popq %rbp
retq
movq -0x48(%rbp), %rdi
callq 0x45f0
nopw %cs:(%rax,%rax)