hdl4se_macros.h 8.2 KB
Newer Older
饶先宏's avatar
饶先宏 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
/*
** 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.
*/

/*
* hdl4se_macros.h
  修改记录:
	202107072033: rxh, initial version
*/

#ifndef __HDL4SE_MACROS_H
#define __HDL4SE_MACROS_H

#ifdef __cplusplus
extern "C" {
#endif

#ifndef _ASMLANGUAGE

enum port_direct {
	PORT_INPUT,
	PORT_OUTPUT,
	PORT_INOUT
};

typedef struct sModulePortInfo {
	int index;
	int portdirect;
	const char* name;
	int width;
	int isunsigned;
}ModulePortInfo;

#define H4S_PORT(module_name) static const ModulePortInfo module_name##_port_info_list[PORT_COUNT] = {
#define H4S_IN(index, name, width) {index, PORT_INPUT, #name, width, 1},
#define H4S_OUT(index, name, width) {index, PORT_INPUT, #name, width, 1},
饶先宏's avatar
饶先宏 已提交
64 65
#define H4S_IN_SIGNED(index, name, width) {index, PORT_INPUT, #name, width, 0},
#define H4S_OUT_SIGNED(index, name, width) {index, PORT_INPUT, #name, width, 0},
饶先宏's avatar
饶先宏 已提交
66 67
#define H4S_END_PORT };

饶先宏's avatar
饶先宏 已提交
68
#define MODULE_DECLARE(module_name) \
饶先宏's avatar
饶先宏 已提交
69 70 71 72
struct _sHDL4SE_##module_name; \
typedef struct _sHDL4SE_##module_name sHDL4SE_##module_name; \
typedef int (*module_name##_func)(sHDL4SE_##module_name * pobj); \
struct _sHDL4SE_##module_name{ \
饶先宏's avatar
饶先宏 已提交
73 74 75 76 77
	OBJECT_HEADER \
	INTERFACE_DECLARE(IHDL4SEUnit) \
	HDL4SEUNIT_VARDECLARE \
	DLIST_VARDECLARE \
	\
饶先宏's avatar
饶先宏 已提交
78 79 80 81
	IHDL4SEModule **__parent; \
	char* __name; \
	int __datavalid; \
	char * __parameters[PARAM_COUNT + 1]; \
饶先宏's avatar
饶先宏 已提交
82
	\
饶先宏's avatar
饶先宏 已提交
83 84 85 86
	IBigNumber** __inputdata; \
	int __port_datavalid[PORT_COUNT]; \
	IBigNumber** __port_data[PORT_COUNT];\
	IHDL4SEUnit** __input_unit[PORT_COUNT]; \
饶先宏's avatar
饶先宏 已提交
87
    module_name##_func __output_gen_func[PORT_COUNT]; \
饶先宏's avatar
饶先宏 已提交
88
	int           __input_index[PORT_COUNT]; \
饶先宏's avatar
饶先宏 已提交
89
	module_name##_func __clock_setup_func;
饶先宏's avatar
饶先宏 已提交
90

饶先宏's avatar
饶先宏 已提交
91
#define END_MODULE_DECLARE(module_name) }; \
饶先宏's avatar
饶先宏 已提交
92
 \
饶先宏's avatar
饶先宏 已提交
93 94 95
OBJECT_FUNCDECLARE(module_name, MODULE_CLSID); \
HDL4SEUNIT_FUNCDECLARE(module_name, MODULE_CLSID, sHDL4SE_##module_name); \
DLIST_FUNCIMPL(module_name, clsid, sHDL4SE_##module_name); \
饶先宏's avatar
饶先宏 已提交
96
 \
饶先宏's avatar
饶先宏 已提交
97
OBJECT_FUNCIMPL(module_name, sHDL4SE_##module_name, MODULE_CLSID); \
饶先宏's avatar
饶先宏 已提交
98
 \
饶先宏's avatar
饶先宏 已提交
99 100 101
QUERYINTERFACE_BEGIN(module_name, MODULE_CLSID) \
QUERYINTERFACE_ITEM(IID_HDL4SEUNIT, IHDL4SEUnit, sHDL4SE_##module_name) \
QUERYINTERFACE_ITEM(IID_DLIST, IDList, sHDL4SE_##module_name) \
饶先宏's avatar
饶先宏 已提交
102 103
QUERYINTERFACE_END \
 \
饶先宏's avatar
饶先宏 已提交
104
static const char* module_name##ModuleInfo() \
饶先宏's avatar
饶先宏 已提交
105 106
{ \
	return MODULE_VERSION_STRING; \
饶先宏's avatar
饶先宏 已提交
107
}
饶先宏's avatar
饶先宏 已提交
108
 
饶先宏's avatar
饶先宏 已提交
109 110
#define MODULE_INIT(module_name) \
static int module_name##Create(const PARAMITEM* pParams, int paramcount, HOBJECT* pObject) \
饶先宏's avatar
饶先宏 已提交
111
{ \
饶先宏's avatar
饶先宏 已提交
112 113 114
	sHDL4SE_##module_name* pobj; \
	int __i; \
	pobj = (sHDL4SE_##module_name*)malloc(sizeof(sHDL4SE_##module_name)); \
饶先宏's avatar
饶先宏 已提交
115 116
	if (pobj == NULL) \
		return -1; \
饶先宏's avatar
饶先宏 已提交
117
	memset(pobj, 0, sizeof(sizeof(sHDL4SE_##module_name))); \
饶先宏's avatar
饶先宏 已提交
118 119
	*pObject = 0; \
	HDL4SEUNIT_VARINIT(pobj, clsid); \
饶先宏's avatar
饶先宏 已提交
120 121
	INTERFACE_INIT(IHDL4SEUnit, pobj, module_name, hdl4se_unit); \
	DLIST_VARINIT(pobj, module_name); \
饶先宏's avatar
饶先宏 已提交
122
	\
饶先宏's avatar
饶先宏 已提交
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
	for (__i = 0; __i < PORT_COUNT; __i++) {\
		pobj->__input_unit[__i] = NULL; \
		pobj->__port_data[__i] = bigintegerCreate2( \
					module_name##_port_info_list[__i].width, \
					module_name##_port_info_list[__i].isunsigned \
				); \
	} \
	pobj->__inputdata = bigintegerCreate(64); \
	pobj->__name = NULL; \
	pobj->__parent = NULL; \
	pobj->__datavalid = 0; \
	for (__i = 0; __i < paramcount; __i++) { \
		if (pParams[__i].name == PARAMID_HDL4SE_UNIT_NAME) { \
			if (pobj->__name != NULL) \
				free(pobj->__name); \
			pobj->__name = strdup(pParams[__i].pvalue); \
饶先宏's avatar
饶先宏 已提交
139
		} \
饶先宏's avatar
饶先宏 已提交
140 141 142 143 144
		else if (pParams[__i].name == PARAMID_HDL4SE_UNIT_PARENT) { \
			pobj->__parent = (IHDL4SEModule**)pParams[__i].pvalue; \
		} \
		else if (pParams[__i].name == PARAMID_HDL4SE_UNIT_INSTANCE_PARAMETERS) { \
			hdl4se_split_string((const char*)pParams[__i].pvalue, pobj->__parameters, PARAM_COUNT, ','); \
饶先宏's avatar
饶先宏 已提交
145 146 147
		} \
	}

饶先宏's avatar
饶先宏 已提交
148 149 150
#define END_MODULE_INIT(module_name) \
	OBJECT_RETURN_GEN(module_name, pobj, pObject, MODULE_CLSID); \
	return EIID_OK; \
饶先宏's avatar
饶先宏 已提交
151 152
}

饶先宏's avatar
饶先宏 已提交
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
static int hdl4se_set_output_func(const ModulePortInfo* portlist, int portcount, const char* signal_list, void * func, void** function_list)
{
	char name[256];
	const char* inp = signal_list;
	char* namep = name;
	while (*inp != 0) {
		int ch = *inp;
		if (ch == '_' || (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {
			*namep++ = ch;
		}
		else {
			if (namep != name || ch == '*') {
				if (ch >= '0' && ch <= '9') {
					*namep++ = ch;
				}
				else {
					/*find a token*/
					int i;
					*namep = 0;
					for (i = 0; i < portcount; i++) {
						if (portlist[i].portdirect == PORT_OUTPUT
							|| portlist[i].portdirect == PORT_INOUT) {
							if (ch == '*' || strcmp(name, portlist[i].name) == 0) {
								function_list[i] = func;
							}
						}
					}
					namep = name;
				}
			}
		}
		inp++;
	}
}

#define GEN_OUTPUT_FUNC(module_name, signal_list, func)  hdl4se_set_output_func(module_name##_port_info_list, PORT_COUNT, signal_list, (void *)func, (void **)pobj->__output_gen_func)
#define AT_CLK_FUNC(module_name, func) pobj->__clock_setup_func = func
饶先宏's avatar
饶先宏 已提交
190

饶先宏's avatar
饶先宏 已提交
191 192
#define DEFINE_FUNC(module_name, funcname) \
static int funcname(sHDL4SE_##module_name* pobj) {
饶先宏's avatar
饶先宏 已提交
193

饶先宏's avatar
饶先宏 已提交
194
#define END_DEFINE_FUNC return 0; }
饶先宏's avatar
饶先宏 已提交
195

饶先宏's avatar
饶先宏 已提交
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
#define MODULE_DEINIT(module_name) \
static void module_name##Destroy(HOBJECT object) \
{ \
	sHDL4SE_##module_name* pobj; \
	int i; \
	pobj = (sHDL4SE_##module_name*)objectThis(object); \
	if (pobj->__name != NULL) \
		free(pobj->__name); \
	for (i = 0; i < PORT_COUNT; i++) { \
		objectRelease(pobj->__input_unit[i]); \
		objectRelease(pobj->__port_data[i]); \
	} \
	for (i = 0; i < PARAM_COUNT + 1; i++) { \
		if (pobj->__parameters[i] != NULL) \
			free(pobj->__parameters[i]); \
	} \
	objectRelease(pobj->__inputdata); \

#define END_MODULE_DEINIT(module_name) \
	memset(pobj, 0, sizeof(sHDL4SE_##module_name)); \
	free(pobj); \
} \
 \
static int module_name##Valid(HOBJECT object) \
{ \
	return 1; \
} \
 \
static int module_name##_hdl4se_unit_GetName(HOBJECT object, const char** pname) \
{ \
	sHDL4SE_##module_name* pobj; \
	pobj = (sHDL4SE_##module_name*)objectThis(object); \
	*pname = pobj->__name; \
	return 0; \
} \
\
static int module_name##_hdl4se_unit_ConnectInput(HOBJECT object, int index, HOBJECT from, int fromindex) \
{ \
	int i; \
	sHDL4SE_##module_name* pobj; \
	IHDL4SEUnit** unit = NULL; \
	pobj = (sHDL4SE_##module_name*)objectThis(object); \
	for (i = 0; i < PORT_COUNT; i++) { \
		if (index == i) { \
			if (module_name##_port_info_list[i].portdirect == PORT_INPUT) { \
				\
				if (0 == objectQueryInterface(from, IID_HDL4SEUNIT, (void**)&unit)) { \
					\
						objectRelease(pobj->__input_unit[i]); \
						pobj->__input_unit[i] = unit; \
						pobj->__input_index[i] = fromindex; \
				} \
			} \
			else { \
				/* error: connect output port to driver*/ \
			} \
		} \
	} \
	return 0; \
} \
饶先宏's avatar
饶先宏 已提交
256 257


饶先宏's avatar
饶先宏 已提交
258
int hdl4se_split_string(const char * string, char ** substr, int substrcount, int gap);
饶先宏's avatar
饶先宏 已提交
259 260 261 262 263 264 265 266

#endif /* _ASMLANGUAGE */

#ifdef __cplusplus
}
#endif

#endif /* __HDL4SE_MACROS_H */