From fa1afff57ef7a1a1a7912d4ab88d221eae74f84f Mon Sep 17 00:00:00 2001 From: "yu.dongliang" <18588496441@163.com> Date: Sun, 23 Jun 2024 19:20:02 +0800 Subject: [PATCH] add NAND / NOR / NOT gate in native/eda --- core/scf_core_types.h | 1 + native/eda/scf_eda.h | 2 + native/eda/scf_eda_inst.c | 6 +- native/eda/scf_eda_pack.c | 208 +++++++++++++++++++++++++++++++++----- native/eda/scf_eda_pack.h | 59 ++++++++++- pack/main.c | 2 +- pack/scf_pack.c | 187 ++++++++++++++++++---------------- pack/scf_pack.h | 14 +-- parse/scf_parse.c | 11 +- 9 files changed, 365 insertions(+), 125 deletions(-) diff --git a/core/scf_core_types.h b/core/scf_core_types.h index 7326090..ba80cbd 100644 --- a/core/scf_core_types.h +++ b/core/scf_core_types.h @@ -216,6 +216,7 @@ enum scf_core_types { SCF_VAR_VOID, SCF_VAR_BIT, SCF_VAR_BIT2, + SCF_VAR_BIT3, SCF_VAR_BIT4, SCF_VAR_U16, SCF_VAR_U32, diff --git a/native/eda/scf_eda.h b/native/eda/scf_eda.h index 6b147c3..cc84d93 100644 --- a/native/eda/scf_eda.h +++ b/native/eda/scf_eda.h @@ -33,6 +33,8 @@ static inline int eda_variable_size(scf_variable_t* v) return 1; if (SCF_VAR_BIT2 == v->type) return 2; + if (SCF_VAR_BIT3 == v->type) + return 3; if (SCF_VAR_BIT4 == v->type) return 4; diff --git a/native/eda/scf_eda_inst.c b/native/eda/scf_eda_inst.c index e219ee8..7292521 100644 --- a/native/eda/scf_eda_inst.c +++ b/native/eda/scf_eda_inst.c @@ -885,7 +885,11 @@ static int _eda_inst_mul_handler(scf_native_t* ctx, scf_3ac_code_t* c) k--; } - n_adds = k; + if (j == k) + n_adds = j + 1; + else + n_adds = j; + scf_logd("i: %d, n_adds: %d, j: %d, k: %d\n\n", i, n_adds, j, k); } out->pins[i] = adds[0]; diff --git a/native/eda/scf_eda_pack.c b/native/eda/scf_eda_pack.c index cd251bf..c56a19f 100644 --- a/native/eda/scf_eda_pack.c +++ b/native/eda/scf_eda_pack.c @@ -12,34 +12,183 @@ static int component_pins[SCF_EDA_Components_NB] = SCF_EDA_Diode_NB, SCF_EDA_NPN_NB, SCF_EDA_PNP_NB, + + SCF_EDA_NAND_NB, + SCF_EDA_NOR_NB, + SCF_EDA_NOT_NB, }; -static scf_edata_t component_datas[] = +static int __diode_path_off(ScfEpin* p0, ScfEpin* p1) +{ + if (SCF_EDA_Diode_NEG == p0->id) + return 1; + return 0; +} + +static int __npn_path_off(ScfEpin* p0, ScfEpin* p1) +{ + if (SCF_EDA_NPN_E == p0->id) + return 1; + + if (!p1 || SCF_EDA_NPN_E == p1->id) + return 0; + return 1; +} + +static int __npn_shared(ScfEpin* p) +{ + if (SCF_EDA_NPN_E == p->id) + return 1; + return 0; +} + +static int __pnp_path_off(ScfEpin* p0, ScfEpin* p1) +{ + if (SCF_EDA_PNP_E == p0->id) + return 0; + return 1; +} + +static int __pnp_shared(ScfEpin* p) +{ + if (SCF_EDA_PNP_E == p->id) + return 1; + return 0; +} + +static int __nand_path_off(ScfEpin* p0, ScfEpin* p1) +{ + if (SCF_EDA_NAND_NEG == p0->id) + return 1; + + if (SCF_EDA_NAND_POS == p0->id) { + if (p1 && (SCF_EDA_NAND_IN0 == p1->id || SCF_EDA_NAND_IN1 == p1->id)) + return 1; + + } else if (p1 && SCF_EDA_NAND_NEG != p1->id) + return 1; + return 0; +} + +static int __nand_shared(ScfEpin* p) +{ + if (SCF_EDA_NAND_NEG == p->id || SCF_EDA_NAND_POS == p->id) + return 1; + return 0; +} + +static int __nor_path_off(ScfEpin* p0, ScfEpin* p1) { - {SCF_EDA_None, 0, 0, 0, 0, 0, 0, 0, 0}, - {SCF_EDA_Battery, 0, SCF_EDA_Battery_POS, 0, 0, 1e-9, 1e9, 0, 0}, + if (SCF_EDA_NOR_NEG == p0->id) + return 1; - {SCF_EDA_Resistor, 0, 0, 0, 0, 1e4, 0, 0, 0}, - {SCF_EDA_Capacitor, 0, 0, 0, 0, 10, 0.1, 0, 0}, - {SCF_EDA_Inductor, 0, 0, 0, 0, 10, 0, 1e3, 0}, + if (SCF_EDA_NOR_POS == p0->id) { + if (p1 && (SCF_EDA_NOR_IN0 == p1->id || SCF_EDA_NOR_IN1 == p1->id)) + return 1; + + } else if (p1 && SCF_EDA_NOR_NEG != p1->id) + return 1; + return 0; +} + +static int __nor_shared(ScfEpin* p) +{ + if (SCF_EDA_NOR_NEG == p->id || SCF_EDA_NOR_POS == p->id) + return 1; + return 0; +} + +static int __not_path_off(ScfEpin* p0, ScfEpin* p1) +{ + if (SCF_EDA_NOT_NEG == p0->id) + return 1; + + if (SCF_EDA_NOT_POS == p0->id) { + if (p1 && SCF_EDA_NOT_IN == p1->id) + return 1; + + } else if (p1 && SCF_EDA_NOT_NEG != p1->id) + return 1; + return 0; +} + +static int __not_shared(ScfEpin* p) +{ + if (SCF_EDA_NOT_NEG == p->id || SCF_EDA_NOT_POS == p->id) + return 1; + return 0; +} + +static ScfEops __diode_ops = +{ + __diode_path_off, + NULL, }; -static scf_edata_t pin_datas[] = +static ScfEops __npn_ops = { - {SCF_EDA_None, 0, 0, 0, 0, 0, 0, 0, 0}, + __npn_path_off, + __npn_shared, +}; - {SCF_EDA_Diode, 0, SCF_EDA_Diode_NEG, 0, 0, 750, 0, 0, 0}, +static ScfEops __pnp_ops = +{ + __pnp_path_off, + __pnp_shared, +}; - {SCF_EDA_NPN, 0, SCF_EDA_NPN_B, 0, 0, 750, 0, 0, 0}, - {SCF_EDA_NPN, 0, SCF_EDA_NPN_C, 0, 0, 3, 0, 0, 250}, +static ScfEops __nand_ops = +{ + __nand_path_off, + __nand_shared, +}; - {SCF_EDA_PNP, 0, SCF_EDA_PNP_B, 0, 0, 750, 0, 0, 0}, - {SCF_EDA_PNP, 0, SCF_EDA_PNP_C, 0, 0, 3, 0, 0, 250}, +static ScfEops __nor_ops = +{ + __nor_path_off, + __nor_shared, }; -static scf_edata_t* _pin_find_data(const uint64_t type, const uint64_t model, const uint64_t pid) +static ScfEops __not_ops = { - scf_edata_t* ed; + __not_path_off, + __not_shared, +}; + +static ScfEdata component_datas[] = +{ + {SCF_EDA_None, 0, 0, 0, 0, 0, 0, 0, 0, NULL, NULL}, + {SCF_EDA_Battery, 0, SCF_EDA_Battery_POS, 0, 0, 1e-9, 1e9, 0, 0, NULL, NULL}, + + {SCF_EDA_Resistor, 0, 0, 0, 0, 1e4, 0, 0, 0, NULL, NULL}, + {SCF_EDA_Capacitor, 0, 0, 0, 0, 10, 0.1, 0, 0, NULL, NULL}, + {SCF_EDA_Inductor, 0, 0, 0, 0, 10, 0, 1e3, 0, NULL, NULL}, + + {SCF_EDA_Diode, 0, 0, 0, 0, 0, 0, 0, 0, &__diode_ops, NULL}, + {SCF_EDA_NPN, 0, 0, 0, 0, 0, 0, 0, 0, &__npn_ops, NULL}, + {SCF_EDA_PNP, 0, 0, 0, 0, 0, 0, 0, 0, &__pnp_ops, NULL}, + + {SCF_EDA_NAND, 0, 0, 0, 0, 0, 0, 0, 0, &__nand_ops, "./cpk/nand.cpk"}, + {SCF_EDA_NOR, 0, 0, 0, 0, 0, 0, 0, 0, &__nor_ops, "./cpk/nor.cpk"}, + {SCF_EDA_NOT, 0, 0, 0, 0, 0, 0, 0, 0, &__not_ops, "./cpk/not.cpk"}, +}; + +static ScfEdata pin_datas[] = +{ + {SCF_EDA_None, 0, 0, 0, 0, 0, 0, 0, 0, NULL, NULL}, + + {SCF_EDA_Diode, 0, SCF_EDA_Diode_NEG, 0, 0, 750, 0, 0, 0, NULL, NULL}, + + {SCF_EDA_NPN, 0, SCF_EDA_NPN_B, 0, 0, 750, 0, 0, 0, NULL, NULL}, + {SCF_EDA_NPN, 0, SCF_EDA_NPN_C, 0, 0, 3, 0, 0, 250, NULL, NULL}, + + {SCF_EDA_PNP, 0, SCF_EDA_PNP_B, 0, 0, 750, 0, 0, 0, NULL, NULL}, + {SCF_EDA_PNP, 0, SCF_EDA_PNP_C, 0, 0, 3, 0, 0, 250, NULL, NULL}, +}; + +static ScfEdata* _pin_find_data(const uint64_t type, const uint64_t model, const uint64_t pid) +{ + ScfEdata* ed; int i; for (i = 0; i < sizeof(pin_datas) / sizeof(pin_datas[0]); i++) { @@ -52,9 +201,9 @@ static scf_edata_t* _pin_find_data(const uint64_t type, const uint64_t model, co return NULL; } -static scf_edata_t* _component_find_data(const uint64_t type, const uint64_t model) +ScfEdata* scf_ecomponent__find_data(const uint64_t type, const uint64_t model) { - scf_edata_t* ed; + ScfEdata* ed; int i; for (i = 0; i < sizeof(component_datas) / sizeof(component_datas[0]); i++) { @@ -342,7 +491,7 @@ int scf_epin__del_component(ScfEpin* pin, uint64_t cid, uint64_t pid) ScfEcomponent* scf_ecomponent__alloc(uint64_t type) { ScfEcomponent* c; - scf_edata_t* ed; + ScfEdata* ed; if (type >= SCF_EDA_Components_NB) return NULL; @@ -353,13 +502,24 @@ ScfEcomponent* scf_ecomponent__alloc(uint64_t type) c->type = type; - ed = _component_find_data(c->type, c->model); + ed = scf_ecomponent__find_data(c->type, c->model); if (ed) { - c->v = ed->v; - c->a = ed->a; - c->r = ed->r; - c->uf = ed->uf; - c->uh = ed->uh; + c->v = ed->v; + c->a = ed->a; + c->r = ed->r; + c->uf = ed->uf; + c->uh = ed->uh; + c->ops = ed->ops; + + if (ed->cpk) { + c->n_cpk = strlen(ed->cpk) + 1; + + c->cpk = strdup(ed->cpk); + if (!c->cpk) { + ScfEcomponent_free(c); + return NULL; + } + } } int i; diff --git a/native/eda/scf_eda_pack.h b/native/eda/scf_eda_pack.h index cf2a246..2f094ab 100644 --- a/native/eda/scf_eda_pack.h +++ b/native/eda/scf_eda_pack.h @@ -15,6 +15,10 @@ enum { SCF_EDA_NPN, SCF_EDA_PNP, + SCF_EDA_NAND, + SCF_EDA_NOR, + SCF_EDA_NOT, + SCF_EDA_Components_NB, }; @@ -74,6 +78,38 @@ enum { SCF_EDA_PNP_NB = SCF_EDA_NPN_NB, }; +enum { + SCF_EDA_NAND_NEG, + SCF_EDA_NAND_POS, + + SCF_EDA_NAND_IN0, + SCF_EDA_NAND_IN1, + SCF_EDA_NAND_OUT, + + SCF_EDA_NAND_NB, +}; + +enum { + SCF_EDA_NOR_NEG, + SCF_EDA_NOR_POS, + + SCF_EDA_NOR_IN0, + SCF_EDA_NOR_IN1, + SCF_EDA_NOR_OUT, + + SCF_EDA_NOR_NB, +}; + +enum { + SCF_EDA_NOT_NEG, + SCF_EDA_NOT_POS, + + SCF_EDA_NOT_IN, + SCF_EDA_NOT_OUT, + + SCF_EDA_NOT_NB, +}; + typedef struct { uint64_t type; uint64_t model; @@ -86,7 +122,10 @@ typedef struct { double uf; double uh; double hfe; -} scf_edata_t; + + void* ops; + char* cpk; +} ScfEdata; typedef struct { SCF_PACK_DEF_VAR(int, x0); @@ -102,11 +141,18 @@ SCF_PACK_INFO_VAR(ScfLine, x1), SCF_PACK_INFO_VAR(ScfLine, y1), SCF_PACK_END(ScfLine) +typedef struct scf_eops_s ScfEops; typedef struct scf_epin_s ScfEpin; typedef struct scf_ecomponent_s ScfEcomponent; typedef struct scf_efunction_s ScfEfunction; typedef struct scf_eboard_s ScfEboard; +struct scf_eops_s +{ + int (*off )(ScfEpin* p0, ScfEpin* p1); + int (*shared)(ScfEpin* p); +}; + struct scf_epin_s { SCF_PACK_DEF_VAR(uint64_t, id); @@ -116,6 +162,8 @@ struct scf_epin_s SCF_PACK_DEF_VARS(uint64_t, tos); SCF_PACK_DEF_VAR(uint64_t, c_lid); + SCF_PACK_DEF_OBJ(ScfEcomponent, IC); + SCF_PACK_DEF_VAR(double, v); SCF_PACK_DEF_VAR(double, a); @@ -229,6 +277,10 @@ struct scf_ecomponent_s SCF_PACK_DEF_VAR(uint64_t, model); SCF_PACK_DEF_OBJS(ScfEpin, pins); + SCF_PACK_DEF_VARS(uint8_t, cpk); + SCF_PACK_DEF_OBJ(ScfEfunction, f); + SCF_PACK_DEF_OBJ(ScfEops, ops); + SCF_PACK_DEF_VAR(double, v); SCF_PACK_DEF_VAR(double, a); @@ -238,6 +290,7 @@ struct scf_ecomponent_s SCF_PACK_DEF_VAR(double, uf); SCF_PACK_DEF_VAR(double, uh); + SCF_PACK_DEF_VAR(int64_t, count); SCF_PACK_DEF_VAR(int64_t, color); SCF_PACK_DEF_VAR(int, status); @@ -255,6 +308,8 @@ SCF_PACK_INFO_VAR(ScfEcomponent, type), SCF_PACK_INFO_VAR(ScfEcomponent, model), SCF_PACK_INFO_OBJS(ScfEcomponent, pins, ScfEpin), +SCF_PACK_INFO_VARS(ScfEcomponent, cpk, uint8_t), + SCF_PACK_INFO_VAR(ScfEcomponent, v), SCF_PACK_INFO_VAR(ScfEcomponent, a), @@ -327,11 +382,11 @@ int scf_epin__del_component(ScfEpin* pin, uint64_t cid, uint64_t pid) ScfEcomponent* scf_ecomponent__alloc (uint64_t type); int scf_ecomponent__add_pin(ScfEcomponent* c, ScfEpin* pin); int scf_ecomponent__del_pin(ScfEcomponent* c, ScfEpin* pin); +ScfEdata* scf_ecomponent__find_data(const uint64_t type, const uint64_t model); ScfEfunction* scf_efunction__alloc (const char* name); int scf_efunction__add_component(ScfEfunction* f, ScfEcomponent* c); int scf_efunction__del_component(ScfEfunction* f, ScfEcomponent* c); - int scf_efunction__add_eline (ScfEfunction* f, ScfEline* el); int scf_efunction__del_eline (ScfEfunction* f, ScfEline* el); diff --git a/pack/main.c b/pack/main.c index 7b6abb8..82e503b 100644 --- a/pack/main.c +++ b/pack/main.c @@ -38,7 +38,7 @@ int main() printf("z0: %p, z1: %p\n", z0, z1); uint8_t* buf = NULL; - int len = 0; + long len = 0; B_pack(&b, &buf, &len); diff --git a/pack/scf_pack.c b/pack/scf_pack.c index 1d440fe..e49ead7 100644 --- a/pack/scf_pack.c +++ b/pack/scf_pack.c @@ -1,12 +1,12 @@ #include"scf_pack.h" -int __scf_pack_one_index(uint8_t* pack, uint64_t u, int shift) +long __scf_pack_one_index(uint8_t* pack, uint64_t u, long shift) { - int max = -1; - int bits = 1u << shift; - int i; - int j; - int k; + long max = -1; + long bits = 1u << shift; + long i; + long j; + long k; j = 0; k = 3; @@ -61,15 +61,15 @@ int __scf_pack_one_index(uint8_t* pack, uint64_t u, int shift) return j + 1; } -int __scf_pack_byte_map(uint8_t* pack, uint64_t u, int shift) +long __scf_pack_byte_map(uint8_t* pack, uint64_t u, long shift) { uint8_t* p = (uint8_t*)&u; uint8_t map = 0; - int bytes = 1u << shift >> 3; - int i; - int j; - int k; + long bytes = 1u << shift >> 3; + long i; + long j; + long k; pack[0] |= 0x4; j = 0; @@ -101,12 +101,12 @@ int __scf_pack_byte_map(uint8_t* pack, uint64_t u, int shift) return j + 1; } -int __scf_pack2(uint8_t* pack, uint64_t u, int shift) +long __scf_pack2(uint8_t* pack, uint64_t u, long shift) { - int sum = 0; - int not = 0; - int bits = 1u << shift; - int i; + long sum = 0; + long not = 0; + long bits = 1u << shift; + long i; for (i = 0; i < bits; i++) { if (u & (1ull << i)) @@ -135,13 +135,13 @@ int __scf_pack2(uint8_t* pack, uint64_t u, int shift) return __scf_pack_byte_map(pack, u, shift); } -int __scf_unpack2(void* p, int shift, const uint8_t* buf, int len) +long __scf_unpack2(void* p, long shift, const uint8_t* buf, long len) { - int bits = 1u << shift; - int max = -1; - int i; - int j; - int k; + long bits = 1u << shift; + long max = -1; + long i; + long j; + long k; if (len < 1) return -EINVAL; @@ -261,16 +261,16 @@ int __scf_unpack2(void* p, int shift, const uint8_t* buf, int len) *(uint64_t*)p = u; break; default: - scf_loge("bits %d Not support!\n", bits); + scf_loge("bits %ld Not support!\n", bits); return -EINVAL; break; }; - scf_logd("u: %#lx, j: %d, bits: %d\n", u, j, bits); + scf_logd("u: %#lx, j: %d, bits: %ld\n", u, j, bits); return j; } -int __scf_unpack(void* p, int size, const uint8_t* buf, int len) +long __scf_unpack(void* p, long size, const uint8_t* buf, long len) { switch (size) { case 1: @@ -288,25 +288,11 @@ int __scf_unpack(void* p, int size, const uint8_t* buf, int len) break; case 4: -#if 0 - if (4 <= len) { - *(uint32_t*)p = *(uint32_t*)buf; - return 4; - } -#else return __scf_unpack2(p, 5, buf, len); -#endif break; case 8: -#if 0 - if (8 <= len) { - *(uint64_t*)p = *(uint64_t*)buf; - return 8; - } -#else return __scf_unpack2(p, 6, buf, len); -#endif break; default: scf_loge("data type NOT support!\n"); @@ -316,10 +302,10 @@ int __scf_unpack(void* p, int size, const uint8_t* buf, int len) return -EINVAL; } -int __scf_pack(void* p, int size, uint8_t** pbuf, int* plen) +long __scf_pack(void* p, long size, uint8_t** pbuf, long* plen) { uint8_t pack[64]; - int len = 0; + long len = 0; switch (size) { case 1: @@ -331,29 +317,21 @@ int __scf_pack(void* p, int size, uint8_t** pbuf, int* plen) len = 2; break; case 4: -#if 1 len = __scf_pack2(pack, *(uint32_t*)p, 5); if (len < 0) return len; -#else - *(uint32_t*)pack = *(uint32_t*)p; - len = 4; -#endif - scf_logd("p: %p, %d, len: %d\n\n", p, *(uint32_t*)p, len); + + scf_logd("p: %p, %d, len: %ld\n\n", p, *(uint32_t*)p, len); break; case 8: -#if 1 len = __scf_pack2(pack, *(uint64_t*)p, 6); if (len < 0) return len; -#else - *(uint64_t*)pack = *(uint64_t*)p; - len = 8; -#endif - scf_logd("p: %p, %ld, %#lx, %lg, len: %d\n\n", p, *(uint64_t*)p, *(uint64_t*)p, *(double*)p, len); + + scf_logd("p: %p, %ld, %#lx, %lg, len: %ld\n\n", p, *(uint64_t*)p, *(uint64_t*)p, *(double*)p, len); break; default: - scf_loge("data size '%d' NOT support!\n", size); + scf_loge("data size '%ld' NOT support!\n", size); return -EINVAL; break; }; @@ -368,7 +346,7 @@ int __scf_pack(void* p, int size, uint8_t** pbuf, int* plen) return 0; } -int scf_pack(void* p, scf_pack_info_t* infos, int n_infos, uint8_t** pbuf, int* plen) +long scf_pack(void* p, scf_pack_info_t* infos, long n_infos, uint8_t** pbuf, long* plen) { if (!p || !infos || n_infos < 1 || !pbuf || !plen) return -EINVAL; @@ -376,10 +354,10 @@ int scf_pack(void* p, scf_pack_info_t* infos, int n_infos, uint8_t** pbuf, int* if (!*pbuf) *plen = 0; -// printf("\n"); +// prlongf("\n"); scf_logd("p: %p\n", p); - int i; + long i; for (i = 0; i < n_infos; i++) { scf_logd("name: %s, size: %ld, offset: %ld, noffset: %ld, msize: %ld, members: %p, n_members: %ld\n", infos[i].name, infos[i].size, infos[i].offset, infos[i].noffset, infos[i].msize, infos[i].members, infos[i].n_members); @@ -388,22 +366,22 @@ int scf_pack(void* p, scf_pack_info_t* infos, int n_infos, uint8_t** pbuf, int* void* a = *(void**)(p + infos[i].offset); long n = *(long* )(p + infos[i].noffset); - int j; + long j; scf_logd("a: %p, n: %ld, infos[i].msize: %ld, infos[i].noffset: %ld\n", a, n, infos[i].msize, infos[i].noffset); for (j = 0; j < n; j++) { if (infos[i].members) { - int ret = scf_pack(*(void**)(a + j * infos[i].msize), infos[i].members, infos[i].n_members, pbuf, plen); + long ret = scf_pack(*(void**)(a + j * infos[i].msize), infos[i].members, infos[i].n_members, pbuf, plen); if (ret < 0) { - scf_loge("ret: %d\n", ret); + scf_loge("ret: %ld\n", ret); return ret; } } else { - int ret = __scf_pack(a + j * infos[i].msize, infos[i].msize, pbuf, plen); + long ret = __scf_pack(a + j * infos[i].msize, infos[i].msize, pbuf, plen); if (ret < 0) { - scf_loge("ret: %d\n", ret); + scf_loge("ret: %ld\n", ret); return ret; } } @@ -414,17 +392,17 @@ int scf_pack(void* p, scf_pack_info_t* infos, int n_infos, uint8_t** pbuf, int* if (infos[i].members) { - int ret = scf_pack(*(void**)(p + infos[i].offset), infos[i].members, infos[i].n_members, pbuf, plen); + long ret = scf_pack(*(void**)(p + infos[i].offset), infos[i].members, infos[i].n_members, pbuf, plen); if (ret < 0) { - scf_loge("ret: %d\n", ret); + scf_loge("ret: %ld\n", ret); return ret; } continue; } - int ret = __scf_pack(p + infos[i].offset, infos[i].size, pbuf, plen); + long ret = __scf_pack(p + infos[i].offset, infos[i].size, pbuf, plen); if (ret < 0) { - scf_loge("ret: %d\n", ret); + scf_loge("ret: %ld\n", ret); return ret; } @@ -434,20 +412,20 @@ int scf_pack(void* p, scf_pack_info_t* infos, int n_infos, uint8_t** pbuf, int* return 0; } -int scf_unpack(void** pp, scf_pack_info_t* infos, int n_infos, const uint8_t* buf, int len) +long scf_unpack(void** pp, scf_pack_info_t* infos, long n_infos, const uint8_t* buf, long len) { if (!pp || !infos || n_infos < 1 || !buf || len < 1) return -EINVAL; - int size = infos[n_infos - 1].offset + infos[n_infos - 1].size; + long size = infos[n_infos - 1].offset + infos[n_infos - 1].size; void* p = calloc(1, size); if (!p) return -ENOMEM; - int i; - int j; - int k = 0; + long i; + long j; + long k = 0; for (i = 0; i < n_infos; i++) { @@ -464,18 +442,18 @@ int scf_unpack(void** pp, scf_pack_info_t* infos, int n_infos, const uint8_t* bu for (j = 0; j < n; j++) { if (infos[i].members) { - int ret = scf_unpack((void**)(a + j * infos[i].msize), infos[i].members, infos[i].n_members, buf + k, len - k); + long ret = scf_unpack((void**)(a + j * infos[i].msize), infos[i].members, infos[i].n_members, buf + k, len - k); if (ret < 0) { - scf_loge("ret: %d\n", ret); + scf_loge("ret: %ld\n", ret); return ret; } k += ret; } else { - int ret = __scf_unpack(a + j * infos[i].msize, infos[i].msize, buf + k, len - k); + long ret = __scf_unpack(a + j * infos[i].msize, infos[i].msize, buf + k, len - k); if (ret < 0) { - scf_loge("ret: %d\n", ret); + scf_loge("ret: %ld\n", ret); return ret; } @@ -488,9 +466,9 @@ int scf_unpack(void** pp, scf_pack_info_t* infos, int n_infos, const uint8_t* bu if (infos[i].members) { - int ret = scf_unpack((void**)(p + infos[i].offset), infos[i].members, infos[i].n_members, buf + k, len - k); + long ret = scf_unpack((void**)(p + infos[i].offset), infos[i].members, infos[i].n_members, buf + k, len - k); if (ret < 0) { - scf_loge("ret: %d\n", ret); + scf_loge("ret: %ld\n", ret); return ret; } @@ -498,9 +476,9 @@ int scf_unpack(void** pp, scf_pack_info_t* infos, int n_infos, const uint8_t* bu continue; } - int ret = __scf_unpack(p + infos[i].offset, infos[i].size, buf + k, len - k); + long ret = __scf_unpack(p + infos[i].offset, infos[i].size, buf + k, len - k); if (ret < 0) { - scf_loge("ret: %d\n", ret); + scf_loge("ret: %ld\n", ret); return ret; } @@ -511,13 +489,13 @@ int scf_unpack(void** pp, scf_pack_info_t* infos, int n_infos, const uint8_t* bu return k; } -int scf_unpack_free(void* p, scf_pack_info_t* infos, int n_infos) +long scf_unpack_free(void* p, scf_pack_info_t* infos, long n_infos) { if (!p || !infos || n_infos < 1) return -EINVAL; - int i; - int j; + long i; + long j; for (i = 0; i < n_infos; i++) { if (infos[i].noffset >= 0) { @@ -529,9 +507,9 @@ int scf_unpack_free(void* p, scf_pack_info_t* infos, int n_infos) if (infos[i].members) { for (j = 0; j < n; j++) { - int ret = scf_unpack_free(*(void**)(a + j * infos[i].msize), infos[i].members, infos[i].n_members); + long ret = scf_unpack_free(*(void**)(a + j * infos[i].msize), infos[i].members, infos[i].n_members); if (ret < 0) { - scf_loge("ret: %d\n", ret); + scf_loge("ret: %ld\n", ret); return ret; } } @@ -543,9 +521,9 @@ int scf_unpack_free(void* p, scf_pack_info_t* infos, int n_infos) } if (infos[i].members) { - int ret = scf_unpack_free(*(void**)(p + infos[i].offset), infos[i].members, infos[i].n_members); + long ret = scf_unpack_free(*(void**)(p + infos[i].offset), infos[i].members, infos[i].n_members); if (ret < 0) { - scf_loge("ret: %d\n", ret); + scf_loge("ret: %ld\n", ret); return ret; } } @@ -555,3 +533,40 @@ int scf_unpack_free(void* p, scf_pack_info_t* infos, int n_infos) free(p); return 0; } + +long scf_pack_read(uint8_t** pbuf, const char* cpk) +{ + if (!pbuf || !cpk) + return -EINVAL; + + FILE* fp = fopen(cpk, "rb"); + if (!fp) + return -EINVAL; + + fseek(fp, 0, SEEK_END); + + long len = ftell(fp); + if (len < 0) { + fclose(fp); + return -EINVAL; + } + + uint8_t* buf = calloc(1, len); + if (!buf) { + fclose(fp); + return -ENOMEM; + } + + fseek(fp, 0, SEEK_SET); + + if (fread(buf, 1, len, fp) != len) { + free(buf); + fclose(fp); + return -EINVAL; + } + + fclose(fp); + + *pbuf = buf; + return len; +} diff --git a/pack/scf_pack.h b/pack/scf_pack.h index 133497b..97984d5 100644 --- a/pack/scf_pack.h +++ b/pack/scf_pack.h @@ -16,9 +16,9 @@ struct scf_pack_info_s long n_members; }; -int scf_pack (void* p, scf_pack_info_t* infos, int n_infos, uint8_t** pbuf, int* plen); -int scf_unpack (void** pp, scf_pack_info_t* infos, int n_infos, const uint8_t* buf, int len); -int scf_unpack_free(void* p, scf_pack_info_t* infos, int n_infos); +long scf_pack (void* p, scf_pack_info_t* infos, long n_infos, uint8_t** pbuf, long* plen); +long scf_unpack (void** pp, scf_pack_info_t* infos, long n_infos, const uint8_t* buf, long len); +long scf_unpack_free(void* p, scf_pack_info_t* infos, long n_infos); #define SCF_PACK_DEF_VAR(type, var) type var #define SCF_PACK_DEF_VARS(type, vars) long n_##vars; type* vars @@ -45,17 +45,19 @@ static scf_pack_info_t scf_pack_info_##type[] = { #define SCF_PACK_END(type) \ }; \ -static int type##_pack(type* p, uint8_t** pbuf, int* plen) \ +static long type##_pack(type* p, uint8_t** pbuf, long* plen) \ { \ return scf_pack(p, scf_pack_info_##type, SCF_PACK_N_INFOS(type), pbuf, plen); \ } \ -static int type##_unpack(type** pp, uint8_t* buf, int len) \ +static long type##_unpack(type** pp, uint8_t* buf, long len) \ { \ return scf_unpack((void**)pp, scf_pack_info_##type, SCF_PACK_N_INFOS(type), buf, len); \ } \ -static int type##_free(type* p) \ +static long type##_free(type* p) \ { \ return scf_unpack_free(p, scf_pack_info_##type, SCF_PACK_N_INFOS(type)); \ } +long scf_pack_read(uint8_t** pbuf, const char* cpk); + #endif diff --git a/parse/scf_parse.c b/parse/scf_parse.c index 708dd2d..bb9930b 100644 --- a/parse/scf_parse.c +++ b/parse/scf_parse.c @@ -25,6 +25,7 @@ scf_base_type_t base_types[] = {SCF_VAR_VOID, "void", 1}, {SCF_VAR_BIT, "bit", 1}, {SCF_VAR_BIT2, "bit2_t", 1}, + {SCF_VAR_BIT3, "bit3_t", 1}, {SCF_VAR_BIT4, "bit4_t", 1}, {SCF_VAR_INT, "int", 4}, @@ -2036,7 +2037,7 @@ static int _add_debug_file_names(scf_parse_t* parse) return 0; } -int scf_eda_write_pb(scf_parse_t* parse, const char* out, scf_vector_t* functions, scf_vector_t* global_vars) +int scf_eda_write_cpk(scf_parse_t* parse, const char* out, scf_vector_t* functions, scf_vector_t* global_vars) { scf_function_t* f; ScfEboard* b; @@ -2065,16 +2066,16 @@ int scf_eda_write_pb(scf_parse_t* parse, const char* out, scf_vector_t* function } uint8_t* buf = NULL; - int len = 0; + long len = 0; - int ret = ScfEboard_pack(b, &buf, &len); + long ret = ScfEboard_pack(b, &buf, &len); if (ret < 0) { ScfEboard_free(b); free(buf); return ret; } - scf_loge("len: %d\n", len); + scf_loge("len: %ld\n", len); ScfEboard_free(b); b = NULL; @@ -2161,7 +2162,7 @@ int scf_parse_compile(scf_parse_t* parse, const char* out, const char* arch, int } if (!strcmp(arch, "eda")) - return scf_eda_write_pb(parse, out, functions, NULL); + return scf_eda_write_cpk(parse, out, functions, NULL); global_vars = scf_vector_alloc(); if (!global_vars) { -- 2.25.1