// ==============================================================
// RTL generated by Vivado(TM) HLS - High-Level Synthesis from C, C++ and SystemC
// Version: 2016.2
// Copyright (C) 1986-2016 Xilinx, Inc. All Rights Reserved.
// 
// ===========================================================

#include "clock_top_write_mem.h"
#include "AESL_pkg.h"

using namespace std;

namespace ap_rtl {

const sc_logic clock_top_write_mem::ap_const_logic_1 = sc_dt::Log_1;
const sc_logic clock_top_write_mem::ap_const_logic_0 = sc_dt::Log_0;
const sc_lv<6> clock_top_write_mem::ap_ST_st1_fsm_0 = "1";
const sc_lv<6> clock_top_write_mem::ap_ST_st2_fsm_1 = "10";
const sc_lv<6> clock_top_write_mem::ap_ST_st3_fsm_2 = "100";
const sc_lv<6> clock_top_write_mem::ap_ST_st4_fsm_3 = "1000";
const sc_lv<6> clock_top_write_mem::ap_ST_st5_fsm_4 = "10000";
const sc_lv<6> clock_top_write_mem::ap_ST_st6_fsm_5 = "100000";
const sc_lv<32> clock_top_write_mem::ap_const_lv32_0 = "00000000000000000000000000000000";
const sc_lv<1> clock_top_write_mem::ap_const_lv1_1 = "1";
const sc_lv<32> clock_top_write_mem::ap_const_lv32_1 = "1";
const sc_lv<32> clock_top_write_mem::ap_const_lv32_3 = "11";
const sc_lv<32> clock_top_write_mem::ap_const_lv32_4 = "100";
const sc_lv<1> clock_top_write_mem::ap_const_lv1_0 = "0";
const sc_lv<32> clock_top_write_mem::ap_const_lv32_5 = "101";

clock_top_write_mem::clock_top_write_mem(sc_module_name name) : sc_module(name), mVcdFile(0) {

    SC_METHOD(thread_ap_clk_no_reset_);
    dont_initialize();
    sensitive << ( ap_clk.pos() );

    SC_METHOD(thread_addr_cast_fu_71_p1);
    sensitive << ( addr );

    SC_METHOD(thread_ap_done);
    sensitive << ( ap_start );
    sensitive << ( ap_sig_cseq_ST_st1_fsm_0 );
    sensitive << ( ap_sig_cseq_ST_st6_fsm_5 );

    SC_METHOD(thread_ap_idle);
    sensitive << ( ap_start );
    sensitive << ( ap_sig_cseq_ST_st1_fsm_0 );

    SC_METHOD(thread_ap_ready);
    sensitive << ( ap_sig_cseq_ST_st6_fsm_5 );

    SC_METHOD(thread_ap_sig_23);
    sensitive << ( ap_CS_fsm );

    SC_METHOD(thread_ap_sig_51);
    sensitive << ( ap_CS_fsm );

    SC_METHOD(thread_ap_sig_58);
    sensitive << ( ap_CS_fsm );

    SC_METHOD(thread_ap_sig_65);
    sensitive << ( ap_CS_fsm );

    SC_METHOD(thread_ap_sig_80);
    sensitive << ( ap_CS_fsm );

    SC_METHOD(thread_ap_sig_cseq_ST_st1_fsm_0);
    sensitive << ( ap_sig_23 );

    SC_METHOD(thread_ap_sig_cseq_ST_st2_fsm_1);
    sensitive << ( ap_sig_51 );

    SC_METHOD(thread_ap_sig_cseq_ST_st4_fsm_3);
    sensitive << ( ap_sig_58 );

    SC_METHOD(thread_ap_sig_cseq_ST_st5_fsm_4);
    sensitive << ( ap_sig_65 );

    SC_METHOD(thread_ap_sig_cseq_ST_st6_fsm_5);
    sensitive << ( ap_sig_80 );

    SC_METHOD(thread_mem_ack_read_read_fu_65_p2);
    sensitive << ( mem_ack );
    sensitive << ( ap_sig_cseq_ST_st4_fsm_3 );

    SC_METHOD(thread_mem_addr);
    sensitive << ( addr_cast_fu_71_p1 );
    sensitive << ( addr_cast_reg_85 );
    sensitive << ( ap_sig_cseq_ST_st2_fsm_1 );
    sensitive << ( ap_sig_cseq_ST_st4_fsm_3 );
    sensitive << ( ap_sig_cseq_ST_st5_fsm_4 );

    SC_METHOD(thread_mem_addr_ap_vld);
    sensitive << ( ap_sig_cseq_ST_st2_fsm_1 );
    sensitive << ( ap_sig_cseq_ST_st4_fsm_3 );
    sensitive << ( ap_sig_cseq_ST_st5_fsm_4 );

    SC_METHOD(thread_mem_dout);
    sensitive << ( data );
    sensitive << ( ap_sig_cseq_ST_st2_fsm_1 );
    sensitive << ( ap_sig_cseq_ST_st4_fsm_3 );
    sensitive << ( ap_sig_cseq_ST_st5_fsm_4 );

    SC_METHOD(thread_mem_dout_ap_vld);
    sensitive << ( ap_sig_cseq_ST_st2_fsm_1 );
    sensitive << ( ap_sig_cseq_ST_st4_fsm_3 );
    sensitive << ( ap_sig_cseq_ST_st5_fsm_4 );

    SC_METHOD(thread_mem_wreq);
    sensitive << ( ap_sig_cseq_ST_st2_fsm_1 );
    sensitive << ( ap_sig_cseq_ST_st4_fsm_3 );
    sensitive << ( ap_sig_cseq_ST_st5_fsm_4 );

    SC_METHOD(thread_mem_wreq_ap_vld);
    sensitive << ( ap_sig_cseq_ST_st2_fsm_1 );
    sensitive << ( ap_sig_cseq_ST_st4_fsm_3 );
    sensitive << ( ap_sig_cseq_ST_st5_fsm_4 );

    SC_METHOD(thread_ap_NS_fsm);
    sensitive << ( ap_start );
    sensitive << ( ap_CS_fsm );
    sensitive << ( mem_ack_read_read_fu_65_p2 );

    ap_CS_fsm = "000001";
    static int apTFileNum = 0;
    stringstream apTFilenSS;
    apTFilenSS << "clock_top_write_mem_sc_trace_" << apTFileNum ++;
    string apTFn = apTFilenSS.str();
    mVcdFile = sc_create_vcd_trace_file(apTFn.c_str());
    mVcdFile->set_time_unit(1, SC_PS);
    if (1) {
#ifdef __HLS_TRACE_LEVEL_PORT_HIER__
    sc_trace(mVcdFile, ap_clk, "(port)ap_clk");
    sc_trace(mVcdFile, ap_rst, "(port)ap_rst");
    sc_trace(mVcdFile, ap_start, "(port)ap_start");
    sc_trace(mVcdFile, ap_done, "(port)ap_done");
    sc_trace(mVcdFile, ap_idle, "(port)ap_idle");
    sc_trace(mVcdFile, ap_ready, "(port)ap_ready");
    sc_trace(mVcdFile, addr, "(port)addr");
    sc_trace(mVcdFile, data, "(port)data");
    sc_trace(mVcdFile, mem_addr, "(port)mem_addr");
    sc_trace(mVcdFile, mem_addr_ap_vld, "(port)mem_addr_ap_vld");
    sc_trace(mVcdFile, mem_dout, "(port)mem_dout");
    sc_trace(mVcdFile, mem_dout_ap_vld, "(port)mem_dout_ap_vld");
    sc_trace(mVcdFile, mem_wreq, "(port)mem_wreq");
    sc_trace(mVcdFile, mem_wreq_ap_vld, "(port)mem_wreq_ap_vld");
    sc_trace(mVcdFile, mem_ack, "(port)mem_ack");
#endif
#ifdef __HLS_TRACE_LEVEL_INT__
    sc_trace(mVcdFile, ap_CS_fsm, "ap_CS_fsm");
    sc_trace(mVcdFile, ap_sig_cseq_ST_st1_fsm_0, "ap_sig_cseq_ST_st1_fsm_0");
    sc_trace(mVcdFile, ap_sig_23, "ap_sig_23");
    sc_trace(mVcdFile, addr_cast_fu_71_p1, "addr_cast_fu_71_p1");
    sc_trace(mVcdFile, addr_cast_reg_85, "addr_cast_reg_85");
    sc_trace(mVcdFile, ap_sig_cseq_ST_st2_fsm_1, "ap_sig_cseq_ST_st2_fsm_1");
    sc_trace(mVcdFile, ap_sig_51, "ap_sig_51");
    sc_trace(mVcdFile, ap_sig_cseq_ST_st4_fsm_3, "ap_sig_cseq_ST_st4_fsm_3");
    sc_trace(mVcdFile, ap_sig_58, "ap_sig_58");
    sc_trace(mVcdFile, ap_sig_cseq_ST_st5_fsm_4, "ap_sig_cseq_ST_st5_fsm_4");
    sc_trace(mVcdFile, ap_sig_65, "ap_sig_65");
    sc_trace(mVcdFile, mem_ack_read_read_fu_65_p2, "mem_ack_read_read_fu_65_p2");
    sc_trace(mVcdFile, ap_sig_cseq_ST_st6_fsm_5, "ap_sig_cseq_ST_st6_fsm_5");
    sc_trace(mVcdFile, ap_sig_80, "ap_sig_80");
    sc_trace(mVcdFile, ap_NS_fsm, "ap_NS_fsm");
#endif

    }
}

clock_top_write_mem::~clock_top_write_mem() {
    if (mVcdFile) 
        sc_close_vcd_trace_file(mVcdFile);

}

void clock_top_write_mem::thread_ap_clk_no_reset_() {
    if ( ap_rst.read() == ap_const_logic_1) {
        ap_CS_fsm = ap_ST_st1_fsm_0;
    } else {
        ap_CS_fsm = ap_NS_fsm.read();
    }
    if (esl_seteq<1,1,1>(ap_const_logic_1, ap_sig_cseq_ST_st2_fsm_1.read())) {
        addr_cast_reg_85 = addr_cast_fu_71_p1.read();
    }
}

void clock_top_write_mem::thread_addr_cast_fu_71_p1() {
    addr_cast_fu_71_p1 = esl_zext<8,7>(addr.read());
}

void clock_top_write_mem::thread_ap_done() {
    if (((esl_seteq<1,1,1>(ap_const_logic_0, ap_start.read()) && 
          esl_seteq<1,1,1>(ap_const_logic_1, ap_sig_cseq_ST_st1_fsm_0.read())) || 
         esl_seteq<1,1,1>(ap_const_logic_1, ap_sig_cseq_ST_st6_fsm_5.read()))) {
        ap_done = ap_const_logic_1;
    } else {
        ap_done = ap_const_logic_0;
    }
}

void clock_top_write_mem::thread_ap_idle() {
    if ((esl_seteq<1,1,1>(ap_const_logic_0, ap_start.read()) && 
         esl_seteq<1,1,1>(ap_const_logic_1, ap_sig_cseq_ST_st1_fsm_0.read()))) {
        ap_idle = ap_const_logic_1;
    } else {
        ap_idle = ap_const_logic_0;
    }
}

void clock_top_write_mem::thread_ap_ready() {
    if (esl_seteq<1,1,1>(ap_const_logic_1, ap_sig_cseq_ST_st6_fsm_5.read())) {
        ap_ready = ap_const_logic_1;
    } else {
        ap_ready = ap_const_logic_0;
    }
}

void clock_top_write_mem::thread_ap_sig_23() {
    ap_sig_23 = esl_seteq<1,1,1>(ap_CS_fsm.read().range(0, 0), ap_const_lv1_1);
}

void clock_top_write_mem::thread_ap_sig_51() {
    ap_sig_51 = esl_seteq<1,1,1>(ap_const_lv1_1, ap_CS_fsm.read().range(1, 1));
}

void clock_top_write_mem::thread_ap_sig_58() {
    ap_sig_58 = esl_seteq<1,1,1>(ap_const_lv1_1, ap_CS_fsm.read().range(3, 3));
}

void clock_top_write_mem::thread_ap_sig_65() {
    ap_sig_65 = esl_seteq<1,1,1>(ap_const_lv1_1, ap_CS_fsm.read().range(4, 4));
}

void clock_top_write_mem::thread_ap_sig_80() {
    ap_sig_80 = esl_seteq<1,1,1>(ap_const_lv1_1, ap_CS_fsm.read().range(5, 5));
}

void clock_top_write_mem::thread_ap_sig_cseq_ST_st1_fsm_0() {
    if (ap_sig_23.read()) {
        ap_sig_cseq_ST_st1_fsm_0 = ap_const_logic_1;
    } else {
        ap_sig_cseq_ST_st1_fsm_0 = ap_const_logic_0;
    }
}

void clock_top_write_mem::thread_ap_sig_cseq_ST_st2_fsm_1() {
    if (ap_sig_51.read()) {
        ap_sig_cseq_ST_st2_fsm_1 = ap_const_logic_1;
    } else {
        ap_sig_cseq_ST_st2_fsm_1 = ap_const_logic_0;
    }
}

void clock_top_write_mem::thread_ap_sig_cseq_ST_st4_fsm_3() {
    if (ap_sig_58.read()) {
        ap_sig_cseq_ST_st4_fsm_3 = ap_const_logic_1;
    } else {
        ap_sig_cseq_ST_st4_fsm_3 = ap_const_logic_0;
    }
}

void clock_top_write_mem::thread_ap_sig_cseq_ST_st5_fsm_4() {
    if (ap_sig_65.read()) {
        ap_sig_cseq_ST_st5_fsm_4 = ap_const_logic_1;
    } else {
        ap_sig_cseq_ST_st5_fsm_4 = ap_const_logic_0;
    }
}

void clock_top_write_mem::thread_ap_sig_cseq_ST_st6_fsm_5() {
    if (ap_sig_80.read()) {
        ap_sig_cseq_ST_st6_fsm_5 = ap_const_logic_1;
    } else {
        ap_sig_cseq_ST_st6_fsm_5 = ap_const_logic_0;
    }
}

void clock_top_write_mem::thread_mem_ack_read_read_fu_65_p2() {
    mem_ack_read_read_fu_65_p2 = mem_ack.read();
}

void clock_top_write_mem::thread_mem_addr() {
    if ((esl_seteq<1,1,1>(ap_const_logic_1, ap_sig_cseq_ST_st4_fsm_3.read()) || 
         esl_seteq<1,1,1>(ap_const_logic_1, ap_sig_cseq_ST_st5_fsm_4.read()))) {
        mem_addr = addr_cast_reg_85.read();
    } else if (esl_seteq<1,1,1>(ap_const_logic_1, ap_sig_cseq_ST_st2_fsm_1.read())) {
        mem_addr = addr_cast_fu_71_p1.read();
    } else {
        mem_addr = "XXXXXXXX";
    }
}

void clock_top_write_mem::thread_mem_addr_ap_vld() {
    if ((esl_seteq<1,1,1>(ap_const_logic_1, ap_sig_cseq_ST_st2_fsm_1.read()) || 
         esl_seteq<1,1,1>(ap_const_logic_1, ap_sig_cseq_ST_st4_fsm_3.read()) || 
         esl_seteq<1,1,1>(ap_const_logic_1, ap_sig_cseq_ST_st5_fsm_4.read()))) {
        mem_addr_ap_vld = ap_const_logic_1;
    } else {
        mem_addr_ap_vld = ap_const_logic_0;
    }
}

void clock_top_write_mem::thread_mem_dout() {
    mem_dout = data.read();
}

void clock_top_write_mem::thread_mem_dout_ap_vld() {
    if ((esl_seteq<1,1,1>(ap_const_logic_1, ap_sig_cseq_ST_st2_fsm_1.read()) || 
         esl_seteq<1,1,1>(ap_const_logic_1, ap_sig_cseq_ST_st4_fsm_3.read()) || 
         esl_seteq<1,1,1>(ap_const_logic_1, ap_sig_cseq_ST_st5_fsm_4.read()))) {
        mem_dout_ap_vld = ap_const_logic_1;
    } else {
        mem_dout_ap_vld = ap_const_logic_0;
    }
}

void clock_top_write_mem::thread_mem_wreq() {
    if (esl_seteq<1,1,1>(ap_const_logic_1, ap_sig_cseq_ST_st4_fsm_3.read())) {
        mem_wreq = ap_const_lv1_1;
    } else if ((esl_seteq<1,1,1>(ap_const_logic_1, ap_sig_cseq_ST_st2_fsm_1.read()) || 
                esl_seteq<1,1,1>(ap_const_logic_1, ap_sig_cseq_ST_st5_fsm_4.read()))) {
        mem_wreq = ap_const_lv1_0;
    } else {
        mem_wreq =  (sc_lv<1>) ("X");
    }
}

void clock_top_write_mem::thread_mem_wreq_ap_vld() {
    if ((esl_seteq<1,1,1>(ap_const_logic_1, ap_sig_cseq_ST_st2_fsm_1.read()) || 
         esl_seteq<1,1,1>(ap_const_logic_1, ap_sig_cseq_ST_st4_fsm_3.read()) || 
         esl_seteq<1,1,1>(ap_const_logic_1, ap_sig_cseq_ST_st5_fsm_4.read()))) {
        mem_wreq_ap_vld = ap_const_logic_1;
    } else {
        mem_wreq_ap_vld = ap_const_logic_0;
    }
}

void clock_top_write_mem::thread_ap_NS_fsm() {
    switch (ap_CS_fsm.read().to_uint64()) {
        case 1 : 
            if (!esl_seteq<1,1,1>(ap_start.read(), ap_const_logic_0)) {
                ap_NS_fsm = ap_ST_st2_fsm_1;
            } else {
                ap_NS_fsm = ap_ST_st1_fsm_0;
            }
            break;
        case 2 : 
            ap_NS_fsm = ap_ST_st3_fsm_2;
            break;
        case 4 : 
            ap_NS_fsm = ap_ST_st4_fsm_3;
            break;
        case 8 : 
            if (!esl_seteq<1,1,1>(ap_const_lv1_0, mem_ack_read_read_fu_65_p2.read())) {
                ap_NS_fsm = ap_ST_st5_fsm_4;
            } else {
                ap_NS_fsm = ap_ST_st4_fsm_3;
            }
            break;
        case 16 : 
            ap_NS_fsm = ap_ST_st6_fsm_5;
            break;
        case 32 : 
            ap_NS_fsm = ap_ST_st1_fsm_0;
            break;
        default : 
            ap_NS_fsm =  (sc_lv<6>) ("XXXXXX");
            break;
    }
}

}

