/*
** HDL4SE: 软件Verilog综合仿真平台
** Copyright (C) 2021-2021, raoxianhong<raoxianhong@163.net>
** LCOM: 轻量级组件对象模型
** Copyright (C) 2021-2021, raoxianhong<raoxianhong@163.net>
** All rights reserved.
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are met:
**
** * Redistributions of source code must retain the above copyright notice,
**   this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright notice,
**   this list of conditions and the following disclaimer in the documentation
**   and/or other materials provided with the distribution.
** * The name of the author may be used to endorse or promote products
**   derived from this software without specific prior written permission.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
** AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
** ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
** LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
** CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
** SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
** INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
** CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
** ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
** THE POSSIBILITY OF SUCH DAMAGE.
*/


/*
* counter.c
    202105180851: rxh, initial version
*/
#include "stdlib.h"
#include "stdio.h"

#include "object.h"
#include "bignumber.h"
#include "hdl4secell.h"

static IHDL4SEUnit** hdl4seCreateDec2seg(IHDL4SEModule** parent, char* instanceparam, char* name) { /* module dec2seg */
	IHDL4SEUnit** unit_const[11];
	IHDL4SEUnit** unit_mux16 = NULL;
	IHDL4SEModule** module_dec2seg = NULL;
	IHDL4SEUnit** unit_dec2seg = NULL;
	int i;
	char temp[128];
	char* constparam[11] = {
		"8, 8'b00111111",
		"8, 8'b00000110",
		"8, 8'b01011011",
		"8, 8'b01001111",
		"8, 8'b01100110",
		"8, 8'b01101101",
		"8, 8'b01111101",
		"8, 8'b00000111",
		"8, 8'b01111111",
		"8, 8'b01101111",
		"8, 8'b01111001"
	};

	/* 生成模块对象 */
	unit_dec2seg = hdl4seCreateUnit(parent, CLSID_HDL4SE_MODULE, instanceparam, name);
	/* 得到对象的IHDL4SEModule 接口 */
	objectQueryInterface(unit_dec2seg, IID_HDL4SEMODULE, (void **)&module_dec2seg);
	/* 增加端口 */
	objectCall3(module_dec2seg, AddPort, 4, PORTTYPE_INPUT,  "0.dec");
	objectCall3(module_dec2seg, AddPort, 8, PORTTYPE_OUTPUT, "1.seg");

	for (i = 0; i < 11; i++) {
		char tempname[32];
		sprintf(tempname, "const_cst%d", i);
		unit_const[i] = hdl4seCreateUnit(module_dec2seg, CLSID_HDL4SE_CONST, constparam[i], tempname);
	}
	/* 生成数据选择器unit_mux */
	unit_mux16 = hdl4seCreateUnit(module_dec2seg, CLSID_HDL4SE_MUX16, "8", "mux_dec");
	/*mux的输入连接到输入端口dec和线网constall上*/
	objectCall3(unit_mux16, Connect, 0, unit_dec2seg, 0);
	for (i = 0; i < 10; i++) {
		objectCall3(unit_mux16, Connect, i+1, unit_const[i], 0);
	}
	for (; i < 16; i++) {
		objectCall3(unit_mux16, Connect, i+1, unit_const[10], 0);
	}
	/* 译码模块的输出seg连接到数据先择器的输出*/
	objectCall3(unit_dec2seg, Connect, 1, unit_mux16, 17);

	/*释放module接口*/
	objectRelease(module_dec2seg);
	/*返回unit接口*/
	return unit_dec2seg;
}

static IHDL4SEUnit** hdl4seCreateCounter(IHDL4SEModule** parent, char* instanceparam, char* name) { /* module counter */
	IHDL4SEModule** module_counter = NULL;
	IHDL4SEUnit** unit_counter = NULL;
	int width, maxvalue, resetvalue;
	char temp[128];
	sscanf(instanceparam, "%d, %d, %d", &width, &maxvalue, &resetvalue);
	/* 生成模块对象 */
	unit_counter = hdl4seCreateUnit(parent, CLSID_HDL4SE_MODULE, instanceparam, name);
	/* 得到对象的IHDL4SEModule 接口 */
	objectQueryInterface(unit_counter, IID_HDL4SEMODULE, (void **)&module_counter);
	/* 增加端口 */
	objectCall3(module_counter, AddPort, 1, PORTTYPE_INPUT, "0.nwReset");
	objectCall3(module_counter, AddPort, 1, PORTTYPE_INPUT, "1.wCounterIt");
	objectCall3(module_counter, AddPort, width, PORTTYPE_OUTPUT, "2.bCounter");
	objectCall3(module_counter, AddPort, 1, PORTTYPE_OUTPUT, "3.wCounterOverflow");

	/*WIDTH宽度的寄存器用来保存计数器的值*/
	sprintf(temp, "%d", width);
	IHDL4SEUnit** reg_bCurrentCounter = hdl4seCreateUnit(module_counter, CLSID_HDL4SE_REG, temp, "bCurrentCounter");

	objectCall3(unit_counter, Connect, 2, reg_bCurrentCounter, 1);

	sprintf(temp, "%d, %d", width, maxvalue);
	IHDL4SEUnit** unit_const_MAXVALUE = hdl4seCreateUnit(module_counter, CLSID_HDL4SE_CONST, temp, "const_MAXVALUE");

	sprintf(temp, "%d, %d", width, resetvalue);
	IHDL4SEUnit** unit_const_RESETVALUE = hdl4seCreateUnit(module_counter, CLSID_HDL4SE_CONST, temp, "const_RESETVALUE");

	sprintf(temp, "%d, %d, 1, %d", width, width, BINOP_EQ);
	IHDL4SEUnit** unit_binop_EQ_bCurrentCounter_MAXVALUE = hdl4seCreateUnit(module_counter, CLSID_HDL4SE_BINOP, temp, "binop_EQ_bCurrentCounter_MAXVALUE");
	objectCall3(unit_binop_EQ_bCurrentCounter_MAXVALUE, Connect, 0, reg_bCurrentCounter, 1);
	objectCall3(unit_binop_EQ_bCurrentCounter_MAXVALUE, Connect, 1, unit_const_MAXVALUE, 0);
	
	sprintf(temp, "%d, %d, 1, %d", width, width, BINOP_AND);
	IHDL4SEUnit** unit_binop_overflow = hdl4seCreateUnit(module_counter, CLSID_HDL4SE_BINOP, temp, "unit_binop_overflow");
	objectCall3(unit_binop_overflow, Connect, 0, unit_binop_EQ_bCurrentCounter_MAXVALUE, 2);
	objectCall3(unit_binop_overflow, Connect, 1, unit_counter, 1);
	objectCall3(unit_counter, Connect, 3, unit_binop_overflow, 2);
	/* bCurrentCounter+1 用加法器实现 */
	  /* 常数 1 */
	sprintf(temp, "%d, %d", width, 1);
	IHDL4SEUnit** unit_const_One = hdl4seCreateUnit(module_counter, CLSID_HDL4SE_CONST, temp, "const_One");

	/* bCurrentCounter + 1 */
	sprintf(temp, "%d, %d, %d, %d", width, width, width, BINOP_ADD);
	IHDL4SEUnit** unit_binop_bCurrentCounterPlusOne = hdl4seCreateUnit(module_counter, CLSID_HDL4SE_BINOP, temp, "binop_bCurrentCounterInc");
	objectCall3(unit_binop_bCurrentCounterPlusOne, Connect, 0, reg_bCurrentCounter, 1);
	objectCall3(unit_binop_bCurrentCounterPlusOne, Connect, 1, unit_const_One, 0);

	/* if语句用数据选择器实现 */
	sprintf(temp, "%d", width);
	IHDL4SEUnit** unit_mux_bCounter_If_EQMAXvalue = hdl4seCreateUnit(module_counter, CLSID_HDL4SE_MUX2, temp, "mux_bCurrentCounter_if_wCounterIt");
	objectCall3(unit_mux_bCounter_If_EQMAXvalue, Connect, 0, unit_binop_EQ_bCurrentCounter_MAXVALUE, 2);
	objectCall3(unit_mux_bCounter_If_EQMAXvalue, Connect, 1, unit_binop_bCurrentCounterPlusOne, 2);
	objectCall3(unit_mux_bCounter_If_EQMAXvalue, Connect, 2, unit_const_RESETVALUE, 0);

	IHDL4SEUnit** unit_const_0 = hdl4seCreateUnit(module_counter, CLSID_HDL4SE_CONST, "1, 0", "const_0");
	IHDL4SEUnit** unit_const_1 = hdl4seCreateUnit(module_counter, CLSID_HDL4SE_CONST, "1, 1", "const_1");

	sprintf(temp, "%d", width);
	IHDL4SEUnit** unit_mux_bCurrentCounter_if_nwReset = hdl4seCreateUnit(module_counter, CLSID_HDL4SE_MUX2, temp, "mux_bCurrentCounter_if_nwReset");
	objectCall3(unit_mux_bCurrentCounter_if_nwReset, Connect, 0, module_counter, 1);
	objectCall3(unit_mux_bCurrentCounter_if_nwReset, Connect, 1, reg_bCurrentCounter, 0);
	objectCall3(unit_mux_bCurrentCounter_if_nwReset, Connect, 2, unit_mux_bCounter_If_EQMAXvalue, 2);

	IHDL4SEUnit** unit_mux_bCurrentCounter = hdl4seCreateUnit(module_counter, CLSID_HDL4SE_MUX2, temp, "mux_bCurrentCounter");
	objectCall3(unit_mux_bCurrentCounter, Connect, 0, module_counter, 0);
	objectCall3(unit_mux_bCurrentCounter, Connect, 1, unit_const_RESETVALUE, 0);
	objectCall3(unit_mux_bCurrentCounter, Connect, 2, unit_mux_bCurrentCounter_if_nwReset, 2);
	objectCall3(reg_bCurrentCounter, Connect, 0, unit_mux_bCurrentCounter, 3);

	/*释放module接口*/
	objectRelease(module_counter);
	/*返回unit接口*/
	return unit_counter;

}

IHDL4SEUnit** hdl4seCreateMain(IHDL4SEModule** parent, char* instanceparam, char* name) 
{ /* module main */
	IHDL4SEModule** module_main;
	IHDL4SEUnit** unit_main;
	char temp[128];
	int i;
	/* 生成模块对象 */
	unit_main = hdl4seCreateUnit(parent, CLSID_HDL4SE_MODULE, instanceparam, name);
	/* 得到对象的IHDL4SEModule 接口 */
	objectQueryInterface(unit_main, IID_HDL4SEMODULE, (void **)&module_main);
	/* 增加端口 */
	objectCall3(module_main, AddPort, 1, PORTTYPE_INPUT, "0.nwReset");
	objectCall3(module_main, AddPort, 1, PORTTYPE_OUTPUT, "1.wWrite");
	objectCall3(module_main, AddPort, 32, PORTTYPE_OUTPUT, "2.bWriteAddr");
	objectCall3(module_main, AddPort, 32, PORTTYPE_OUTPUT, "3.bWriteData");
	objectCall3(module_main, AddPort, 4, PORTTYPE_OUTPUT, "4.bWriteMask");
	objectCall3(module_main, AddPort, 1, PORTTYPE_OUTPUT, "5.wRead");
	objectCall3(module_main, AddPort, 32, PORTTYPE_OUTPUT, "6.bReadAddr");
	objectCall3(module_main, AddPort, 32, PORTTYPE_INPUT, "7.bReadData");

	IHDL4SEUnit** const_1b1 = hdl4seCreateUnit(module_main, CLSID_HDL4SE_CONST, "1,1", "const_bReadAddr");
	objectCall3(unit_main, Connect, 1, const_1b1, 1); /* 简化处理，一直写 */
	IHDL4SEUnit** reg_bWriteAddr = hdl4seCreateUnit(module_main, CLSID_HDL4SE_REG, "32", "reg_bWriteAddr");
	objectCall3(unit_main, Connect, 2, reg_bWriteAddr, 1);
	IHDL4SEUnit** reg_bWriteData = hdl4seCreateUnit(module_main, CLSID_HDL4SE_REG, "32", "reg_bWriteData");
	objectCall3(unit_main, Connect, 3, reg_bWriteData, 1);
	IHDL4SEUnit** reg_bWriteMask = hdl4seCreateUnit(module_main, CLSID_HDL4SE_REG, "4", "reg_bWriteMask");
	objectCall3(unit_main, Connect, 4, reg_bWriteMask, 1);
	IHDL4SEUnit** const_0_wRead = hdl4seCreateUnit(module_main, CLSID_HDL4SE_CONST, "1, 1", "const_0_wRead");
	objectCall3(unit_main, Connect, 5, const_0_wRead, 0);
	IHDL4SEUnit** const_bReadAddr = hdl4seCreateUnit(module_main, CLSID_HDL4SE_CONST, "32, 32'hF000_0000", "const_bReadAddr");
	objectCall3(unit_main, Connect, 6, const_bReadAddr, 0);

	IHDL4SEUnit** split_bReadData_wButton012 = hdl4seCreateUnit(module_main, CLSID_HDL4SE_SPLIT4, "32, 1, 0, 1, 1, 1, 2, 1, 3", "bReadData_wButton012");
	objectCall3(split_bReadData_wButton012, Connect, 0, unit_main, 7);

	sprintf(temp, "1, 1, %d", UNOP_NOT);
	IHDL4SEUnit** unop_Button0NotPressed = hdl4seCreateUnit(module_main, CLSID_HDL4SE_UNOP, temp, "unop_Button0NotPressed");
	objectCall3(unop_Button0NotPressed, Connect, 0, split_bReadData_wButton012, 1);
	sprintf(temp, "1, 1, %d", UNOP_NOT);
	IHDL4SEUnit** unop_Button1NotPressed = hdl4seCreateUnit(module_main, CLSID_HDL4SE_UNOP, temp, "unop_Button1NotPressed");
	objectCall3(unop_Button1NotPressed, Connect, 0, split_bReadData_wButton012, 2);

	sprintf(temp, "1, 1, 1, %d", BINOP_AND);
	IHDL4SEUnit** binop_counterit = hdl4seCreateUnit(module_main, CLSID_HDL4SE_BINOP, temp, "binop_counterit");
	objectCall3(binop_counterit, Connect, 0, unop_Button1NotPressed, 1);
	objectCall3(binop_counterit, Connect, 1, split_bReadData_wButton012, 3);

	sprintf(temp, "1, 1, 1, %d", BINOP_AND);
	IHDL4SEUnit** binop_resetcounter = hdl4seCreateUnit(module_main, CLSID_HDL4SE_BINOP, temp, "binop_resetcounter");
	objectCall3(binop_resetcounter, Connect, 0, unop_Button0NotPressed, 1);
	objectCall3(binop_resetcounter, Connect, 1, unit_main, 0);


	IHDL4SEUnit** counter[10];
	IHDL4SEUnit** dec2seg[10];
	IHDL4SEUnit** reg_overflow[10];
	for (i = 0; i < 10; i++) {
		sprintf(temp, "counter%d", i);
		counter[i] = hdl4seCreateCounter(module_main, "4, 9, 0", temp);
		sprintf(temp, "dec2seg%d", i);
		dec2seg[i] = hdl4seCreateDec2seg(module_main, "", temp);
		sprintf(temp, "reg_overflow%d", i);
		reg_overflow[i] = hdl4seCreateUnit(module_main, CLSID_HDL4SE_REG, "1", temp);
		objectCall3(counter[i], Connect, 0, binop_resetcounter, 2);
		if (i == 0) {
			objectCall3(counter[i], Connect, 1, binop_counterit, 2);
		}
		else {
			objectCall3(counter[i], Connect, 1, counter[i - 1], 3);
		}
		objectCall3(dec2seg[i], Connect, 0, counter[i], 2);
		objectCall3(reg_overflow[i], Connect, 0, counter[i], 3);
	}

	sprintf(temp, "1, 1, 1, %d", BINOP_OR);
	IHDL4SEUnit** binop_or98 = hdl4seCreateUnit(module_main, CLSID_HDL4SE_BINOP, temp, "or98");
	objectCall3(binop_or98, Connect, 0, reg_overflow[8], 1);
	objectCall3(binop_or98, Connect, 1, reg_overflow[7], 1);

	sprintf(temp, "1, 1, 1, %d", BINOP_OR);
	IHDL4SEUnit** binop_or76 = hdl4seCreateUnit(module_main, CLSID_HDL4SE_BINOP, temp, "or76");
	objectCall3(binop_or76, Connect, 0, reg_overflow[6], 1);
	objectCall3(binop_or76, Connect, 1, reg_overflow[5], 1);

	sprintf(temp, "1, 1, 1, %d", BINOP_OR);
	IHDL4SEUnit** binop_or54 = hdl4seCreateUnit(module_main, CLSID_HDL4SE_BINOP, temp, "or54");
	objectCall3(binop_or54, Connect, 0, reg_overflow[4], 1);
	objectCall3(binop_or54, Connect, 1, reg_overflow[3], 1);

	sprintf(temp, "1, 1, 1, %d", BINOP_OR);
	IHDL4SEUnit** binop_or32 = hdl4seCreateUnit(module_main, CLSID_HDL4SE_BINOP, temp, "or32");
	objectCall3(binop_or32, Connect, 0, reg_overflow[2], 1);
	objectCall3(binop_or32, Connect, 1, reg_overflow[1], 1);

	sprintf(temp, "1, 1, 1, %d", BINOP_OR);
	IHDL4SEUnit** binop_or10 = hdl4seCreateUnit(module_main, CLSID_HDL4SE_BINOP, temp, "or10");
	objectCall3(binop_or10, Connect, 0, reg_overflow[0], 1);
	objectCall3(binop_or10, Connect, 1, binop_counterit, 2);

	sprintf(temp, "1, 1, 1, %d", BINOP_OR);
	IHDL4SEUnit** binop_or7654 = hdl4seCreateUnit(module_main, CLSID_HDL4SE_BINOP, temp, "or7654");
	objectCall3(binop_or7654, Connect, 0, binop_or76, 2);
	objectCall3(binop_or7654, Connect, 1, binop_or54, 2);

	sprintf(temp, "1, 1, 1, %d", BINOP_OR);
	IHDL4SEUnit** binop_or3210 = hdl4seCreateUnit(module_main, CLSID_HDL4SE_BINOP, temp, "or3210");
	objectCall3(binop_or3210, Connect, 0, binop_or32, 2);
	objectCall3(binop_or3210, Connect, 1, binop_or10, 2);



	IHDL4SEUnit** const_b8b0 = hdl4seCreateUnit(module_main, CLSID_HDL4SE_CONST, "8, 0", "const_b8b0");
	IHDL4SEUnit** const_b4b0000 = hdl4seCreateUnit(module_main, CLSID_HDL4SE_CONST, "4, 0", "const_b4b0000");
	IHDL4SEUnit** const_b4b1100 = hdl4seCreateUnit(module_main, CLSID_HDL4SE_CONST, "4, 4'b1100", "const_b4b1100");
	IHDL4SEUnit** const_b32b0 = hdl4seCreateUnit(module_main, CLSID_HDL4SE_CONST, "32, 0", "const_b32b0");
	IHDL4SEUnit** const_b32hf0000018 = hdl4seCreateUnit(module_main, CLSID_HDL4SE_CONST, "32, 32'hf0000018", "const_b32hf0000018");
	IHDL4SEUnit** const_b32hf0000014 = hdl4seCreateUnit(module_main, CLSID_HDL4SE_CONST, "32, 32'hf0000014", "const_b32hf0000014");
	IHDL4SEUnit** const_b32hf0000010 = hdl4seCreateUnit(module_main, CLSID_HDL4SE_CONST, "32, 32'hf0000010", "const_b32hf0000010");

	IHDL4SEUnit** bind4_b0098 = hdl4seCreateUnit(module_main, CLSID_HDL4SE_BIND4, "8,8,8,8", "bind4_0098");
	objectCall3(bind4_b0098, Connect, 0, dec2seg[8], 1);
	objectCall3(bind4_b0098, Connect, 1, dec2seg[9], 1);
	objectCall3(bind4_b0098, Connect, 2, const_b8b0, 1);
	objectCall3(bind4_b0098, Connect, 3, const_b8b0, 1);

	IHDL4SEUnit** bind4_b7654 = hdl4seCreateUnit(module_main, CLSID_HDL4SE_BIND4, "8,8,8,8", "bind4_7654");
	objectCall3(bind4_b7654, Connect, 0, dec2seg[4], 1);
	objectCall3(bind4_b7654, Connect, 1, dec2seg[5], 1);
	objectCall3(bind4_b7654, Connect, 2, dec2seg[6], 1);
	objectCall3(bind4_b7654, Connect, 3, dec2seg[7], 1);

	IHDL4SEUnit** bind4_b3210 = hdl4seCreateUnit(module_main, CLSID_HDL4SE_BIND4, "8,8,8,8", "bind4_3210");
	objectCall3(bind4_b3210, Connect, 0, dec2seg[0], 1);
	objectCall3(bind4_b3210, Connect, 1, dec2seg[1], 1);
	objectCall3(bind4_b3210, Connect, 2, dec2seg[2], 1);
	objectCall3(bind4_b3210, Connect, 3, dec2seg[3], 1);

	IHDL4SEUnit** mux_bWriteMask_if_bCounterChanged0 = hdl4seCreateUnit(module_main, CLSID_HDL4SE_MUX2, "4", "mux_bWriteMask_if_bCounterChanged0");
	objectCall3(mux_bWriteMask_if_bCounterChanged0, Connect, 0, binop_or3210, 2);
	objectCall3(mux_bWriteMask_if_bCounterChanged0, Connect, 1, reg_bWriteMask, 1);
	objectCall3(mux_bWriteMask_if_bCounterChanged0, Connect, 2, const_b4b0000, 0);

	IHDL4SEUnit** mux_bWriteAddr_if_bCounterChanged0 = hdl4seCreateUnit(module_main, CLSID_HDL4SE_MUX2, "32", "mux_bWriteAddr_if_bCounterChanged0");
	objectCall3(mux_bWriteAddr_if_bCounterChanged0, Connect, 0, binop_or3210, 2);
	objectCall3(mux_bWriteAddr_if_bCounterChanged0, Connect, 1, reg_bWriteAddr, 1);
	objectCall3(mux_bWriteAddr_if_bCounterChanged0, Connect, 2, const_b32hf0000010, 0);

	IHDL4SEUnit** mux_bWriteData_if_bCounterChanged0 = hdl4seCreateUnit(module_main, CLSID_HDL4SE_MUX2, "32", "mux_bWriteData_if_bCounterChanged0");
	objectCall3(mux_bWriteData_if_bCounterChanged0, Connect, 0, binop_or3210, 2);
	objectCall3(mux_bWriteData_if_bCounterChanged0, Connect, 1, reg_bWriteData, 1);
	objectCall3(mux_bWriteData_if_bCounterChanged0, Connect, 2, bind4_b3210, 0);

	IHDL4SEUnit** mux_bWriteMask_if_bCounterChanged1 = hdl4seCreateUnit(module_main, CLSID_HDL4SE_MUX2, "4", "mux_bWriteMask_if_bCounterChanged1");
	objectCall3(mux_bWriteMask_if_bCounterChanged1, Connect, 0, binop_or7654, 2);
	objectCall3(mux_bWriteMask_if_bCounterChanged1, Connect, 1, mux_bWriteMask_if_bCounterChanged0, 3);
	objectCall3(mux_bWriteMask_if_bCounterChanged1, Connect, 2, const_b4b0000, 0);

	IHDL4SEUnit** mux_bWriteAddr_if_bCounterChanged1 = hdl4seCreateUnit(module_main, CLSID_HDL4SE_MUX2, "32", "mux_bWriteAddr_if_bCounterChanged1");
	objectCall3(mux_bWriteAddr_if_bCounterChanged1, Connect, 0, binop_or7654, 2);
	objectCall3(mux_bWriteAddr_if_bCounterChanged1, Connect, 1, mux_bWriteAddr_if_bCounterChanged0, 3);
	objectCall3(mux_bWriteAddr_if_bCounterChanged1, Connect, 2, const_b32hf0000014, 0);

	IHDL4SEUnit** mux_bWriteData_if_bCounterChanged1 = hdl4seCreateUnit(module_main, CLSID_HDL4SE_MUX2, "32", "mux_bWriteData_if_bCounterChanged1");
	objectCall3(mux_bWriteData_if_bCounterChanged1, Connect, 0, binop_or7654, 2);
	objectCall3(mux_bWriteData_if_bCounterChanged1, Connect, 1, mux_bWriteData_if_bCounterChanged0, 3);
	objectCall3(mux_bWriteData_if_bCounterChanged1, Connect, 2, bind4_b7654, 0);

	IHDL4SEUnit** mux_bWriteMask_if_bCounterChanged2 = hdl4seCreateUnit(module_main, CLSID_HDL4SE_MUX2, "4", "mux_bWriteMask_if_bCounterChanged2");
	objectCall3(mux_bWriteMask_if_bCounterChanged2, Connect, 0, binop_or98, 2);
	objectCall3(mux_bWriteMask_if_bCounterChanged2, Connect, 1, mux_bWriteMask_if_bCounterChanged1, 3);
	objectCall3(mux_bWriteMask_if_bCounterChanged2, Connect, 2, const_b4b1100, 0);

	IHDL4SEUnit** mux_bWriteAddr_if_bCounterChanged2 = hdl4seCreateUnit(module_main, CLSID_HDL4SE_MUX2, "32", "mux_bWriteAddr_if_bCounterChanged2");
	objectCall3(mux_bWriteAddr_if_bCounterChanged2, Connect, 0, binop_or98, 2);
	objectCall3(mux_bWriteAddr_if_bCounterChanged2, Connect, 1, mux_bWriteAddr_if_bCounterChanged1, 3);
	objectCall3(mux_bWriteAddr_if_bCounterChanged2, Connect, 2, const_b32hf0000018, 0);

	IHDL4SEUnit** mux_bWriteData_if_bCounterChanged2 = hdl4seCreateUnit(module_main, CLSID_HDL4SE_MUX2, "32", "mux_bWriteData_if_bCounterChanged2");
	objectCall3(mux_bWriteData_if_bCounterChanged2, Connect, 0, binop_or98, 2);
	objectCall3(mux_bWriteData_if_bCounterChanged2, Connect, 1, mux_bWriteData_if_bCounterChanged1, 3);
	objectCall3(mux_bWriteData_if_bCounterChanged2, Connect, 2, bind4_b0098, 0);

	IHDL4SEUnit** mux_bWriteMask_if_nwReset = hdl4seCreateUnit(module_main, CLSID_HDL4SE_MUX2, "4", "mux_bWriteMask_if_nwReset");
	objectCall3(mux_bWriteMask_if_nwReset, Connect, 0, unit_main, 0);
	objectCall3(mux_bWriteMask_if_nwReset, Connect, 2, mux_bWriteMask_if_bCounterChanged2, 3);
	objectCall3(mux_bWriteMask_if_nwReset, Connect, 1, const_b4b0000, 0);

	IHDL4SEUnit** mux_bWriteAddr_if_nwReset = hdl4seCreateUnit(module_main, CLSID_HDL4SE_MUX2, "32", "mux_bWriteAddr_if_nwReset");
	objectCall3(mux_bWriteAddr_if_nwReset, Connect, 0, unit_main, 0);
	objectCall3(mux_bWriteAddr_if_nwReset, Connect, 2, mux_bWriteAddr_if_bCounterChanged2, 3);
	objectCall3(mux_bWriteAddr_if_nwReset, Connect, 1, const_b32b0, 0);

	IHDL4SEUnit** mux_bWriteData_if_nwReset = hdl4seCreateUnit(module_main, CLSID_HDL4SE_MUX2, "32", "mux_bWriteData_if_nwReset");
	objectCall3(mux_bWriteData_if_nwReset, Connect, 0, unit_main, 0);
	objectCall3(mux_bWriteData_if_nwReset, Connect, 2, mux_bWriteData_if_bCounterChanged2, 3);
	objectCall3(mux_bWriteData_if_nwReset, Connect, 1, const_b32b0, 0);

	objectCall3(reg_bWriteMask, Connect, 0, mux_bWriteMask_if_nwReset, 3);
	objectCall3(reg_bWriteAddr, Connect, 0, mux_bWriteAddr_if_nwReset, 3);
	objectCall3(reg_bWriteData, Connect, 0, mux_bWriteData_if_nwReset, 3);

	/*释放module接口*/
	objectRelease(module_main);
	/*返回unit接口*/
	return unit_main;
}

