From 0d32ef7b8867e1212783e44c7feba658acc034fd Mon Sep 17 00:00:00 2001 From: Stefan Rueger Date: Sun, 18 Aug 2024 01:54:05 +0100 Subject: [PATCH] Reformat spacing/comments in src/avrpart.c --- src/avrpart.c | 489 ++++++++++++++++++++++++-------------------------- 1 file changed, 235 insertions(+), 254 deletions(-) diff --git a/src/avrpart.c b/src/avrpart.c index dea8c888..64682b92 100644 --- a/src/avrpart.c +++ b/src/avrpart.c @@ -28,9 +28,7 @@ #include "avrdude.h" #include "libavrdude.h" -/*** - *** Elementary functions dealing with OPCODE structures - ***/ +// Elementary functions dealing with OPCODE structures OPCODE *avr_new_opcode(void) { return (OPCODE *) mmt_malloc(sizeof(OPCODE)); @@ -41,6 +39,7 @@ static OPCODE *avr_dup_opcode(const OPCODE *op) { return NULL; OPCODE *m = (OPCODE *) mmt_malloc(sizeof(*m)); + memcpy(m, op, sizeof(*m)); return m; @@ -50,8 +49,7 @@ void avr_free_opcode(OPCODE *op) { mmt_free(op); } - -// returns position 0..31 of highest bit set or INT_MIN if no bit is set +// Returns position 0..31 of highest bit set or INT_MIN if no bit is set int intlog2(unsigned int n) { int ret; @@ -64,7 +62,6 @@ int intlog2(unsigned int n) { return ret; } - /* * avr_set_bits() * @@ -74,12 +71,12 @@ int avr_set_bits(const OPCODE *op, unsigned char *cmd) { int i, j, bit; unsigned char mask; - for (i=0; i<32; i++) { - if (op->bit[i].type == AVR_CMDBIT_VALUE || op->bit[i].type == AVR_CMDBIT_IGNORE) { - j = 3 - i / 8; - bit = i % 8; + for(i = 0; i < 32; i++) { + if(op->bit[i].type == AVR_CMDBIT_VALUE || op->bit[i].type == AVR_CMDBIT_IGNORE) { + j = 3 - i/8; + bit = i%8; mask = 1 << bit; - if (op->bit[i].value && op->bit[i].type == AVR_CMDBIT_VALUE) + if(op->bit[i].value && op->bit[i].type == AVR_CMDBIT_VALUE) cmd[j] = cmd[j] | mask; else cmd[j] = cmd[j] & ~mask; @@ -89,7 +86,6 @@ int avr_set_bits(const OPCODE *op, unsigned char *cmd) { return 0; } - /* * avr_set_addr() * @@ -101,13 +97,13 @@ int avr_set_addr(const OPCODE *op, unsigned char *cmd, unsigned long addr) { unsigned long value; unsigned char mask; - for (i=0; i<32; i++) { - if (op->bit[i].type == AVR_CMDBIT_ADDRESS) { - j = 3 - i / 8; - bit = i % 8; + for(i = 0; i < 32; i++) { + if(op->bit[i].type == AVR_CMDBIT_ADDRESS) { + j = 3 - i/8; + bit = i%8; mask = 1 << bit; value = addr >> op->bit[i].bitno & 0x01; - if (value) + if(value) cmd[j] = cmd[j] | mask; else cmd[j] = cmd[j] & ~mask; @@ -117,15 +113,14 @@ int avr_set_addr(const OPCODE *op, unsigned char *cmd, unsigned long addr) { return 0; } - /* * avr_set_addr_mem() * * Set address bits in the specified command based on the memory, opcode and - * address; addr must be a word address for flash or, for all other memories, - * a byte address; returns 0 on success and -1 on error (no memory or no - * opcode) or, if positive, bn+1 where bn is bit number of the highest - * necessary bit that the opcode does not provide. + * address; addr must be a word address for flash or, for all other memories, a + * byte address; returns 0 on success and -1 on error (no memory or no opcode) + * or, if positive, bn+1 where bn is bit number of the highest necessary bit + * that the opcode does not provide. */ int avr_set_addr_mem(const AVRMEM *mem, int opnum, unsigned char *cmd, unsigned long addr) { int ret, isflash, lo, hi, memsize, pagesize; @@ -138,11 +133,11 @@ int avr_set_addr_mem(const AVRMEM *mem, int opnum, unsigned char *cmd, unsigned return -1; isflash = mem_is_in_flash(mem); - memsize = mem->size >> isflash; // word addresses for flash + memsize = mem->size >> isflash; // Word addresses for flash pagesize = mem->page_size >> isflash; - // compute range lo..hi of needed address bits - switch(opnum) { + // Compute range lo..hi of needed address bits + switch (opnum) { case AVR_OP_READ: case AVR_OP_WRITE: case AVR_OP_READ_LO: @@ -150,23 +145,23 @@ int avr_set_addr_mem(const AVRMEM *mem, int opnum, unsigned char *cmd, unsigned case AVR_OP_WRITE_LO: case AVR_OP_WRITE_HI: lo = 0; - hi = intlog2(memsize-1); // memsize = 1 implies no addr bit is needed + hi = intlog2(memsize - 1); // memsize = 1 implies no addr bit is needed break; case AVR_OP_LOADPAGE_LO: case AVR_OP_LOADPAGE_HI: lo = 0; - hi = intlog2(pagesize-1); + hi = intlog2(pagesize - 1); break; case AVR_OP_LOAD_EXT_ADDR: lo = 16; - hi = intlog2(memsize-1); + hi = intlog2(memsize - 1); break; case AVR_OP_WRITEPAGE: lo = intlog2(pagesize); - hi = intlog2(memsize-1); + hi = intlog2(memsize - 1); break; case AVR_OP_CHIP_ERASE: @@ -182,21 +177,22 @@ int avr_set_addr_mem(const AVRMEM *mem, int opnum, unsigned char *cmd, unsigned hi = 15; unsigned char avail[32]; + memset(avail, 0, sizeof avail); - for(int i=0; i<32; i++) { + for(int i = 0; i < 32; i++) { if(op->bit[i].type == AVR_CMDBIT_ADDRESS) { int bitno, j, bit; unsigned char mask; bitno = op->bit[i].bitno & 31; - j = 3 - i / 8; - bit = i % 8; + j = 3 - i/8; + bit = i%8; mask = 1 << bit; avail[bitno] = 1; // 'a' bit with number outside bit range [lo, hi] is set to 0 - if (bitno >= lo && bitno <= hi? (addr >> bitno) & 1: 0) + if(bitno >= lo && bitno <= hi? (addr >> bitno) & 1: 0) cmd[j] = cmd[j] | mask; else cmd[j] = cmd[j] & ~mask; @@ -205,32 +201,31 @@ int avr_set_addr_mem(const AVRMEM *mem, int opnum, unsigned char *cmd, unsigned ret = 0; if(lo >= 0 && hi < 32 && lo <= hi) - for(int bn=lo; bn <= hi; bn++) - if(!avail[bn]) // necessary bit bn misses in opcode - ret = bn+1; + for(int bn = lo; bn <= hi; bn++) + if(!avail[bn]) // Necessary bit bn misses in opcode + ret = bn + 1; return ret; } - /* * avr_set_input() * - * Set input data bits in the specified command based on the opcode, - * and the data byte. + * Set input data bits in the specified command based on the opcode, and the + * data byte. */ int avr_set_input(const OPCODE *op, unsigned char *cmd, unsigned char data) { int i, j, bit; unsigned char value; unsigned char mask; - for (i=0; i<32; i++) { - if (op->bit[i].type == AVR_CMDBIT_INPUT) { - j = 3 - i / 8; - bit = i % 8; + for(i = 0; i < 32; i++) { + if(op->bit[i].type == AVR_CMDBIT_INPUT) { + j = 3 - i/8; + bit = i%8; mask = 1 << bit; value = data >> op->bit[i].bitno & 0x01; - if (value) + if(value) cmd[j] = cmd[j] | mask; else cmd[j] = cmd[j] & ~mask; @@ -240,26 +235,24 @@ int avr_set_input(const OPCODE *op, unsigned char *cmd, unsigned char data) { return 0; } - /* * avr_get_output() * - * Retrieve output data bits from the command results based on the - * opcode data. + * Retrieve output data bits from the command results based on the opcode data. */ int avr_get_output(const OPCODE *op, const unsigned char *res, unsigned char *data) { int i, j, bit; unsigned char value; unsigned char mask; - for (i=0; i<32; i++) { - if (op->bit[i].type == AVR_CMDBIT_OUTPUT) { - j = 3 - i / 8; - bit = i % 8; + for(i = 0; i < 32; i++) { + if(op->bit[i].type == AVR_CMDBIT_OUTPUT) { + j = 3 - i/8; + bit = i%8; mask = 1 << bit; value = ((res[j] & mask) >> bit) & 0x01; value = value << op->bit[i].bitno; - if (value) + if(value) *data = *data | value; else *data = *data & ~value; @@ -269,19 +262,17 @@ int avr_get_output(const OPCODE *op, const unsigned char *res, unsigned char *da return 0; } - /* * avr_get_output_index() * - * Calculate the byte number of the output data based on the - * opcode data. + * Calculate the byte number of the output data based on the opcode data. */ int avr_get_output_index(const OPCODE *op) { int i, j; - for (i=0; i<32; i++) { - if (op->bit[i].type == AVR_CMDBIT_OUTPUT) { - j = 3 - i / 8; + for(i = 0; i < 32; i++) { + if(op->bit[i].type == AVR_CMDBIT_OUTPUT) { + j = 3 - i/8; return j; } } @@ -289,13 +280,11 @@ int avr_get_output_index(const OPCODE *op) { return -1; } - -/*** - *** Elementary functions dealing with AVRMEM structures - ***/ +// Elementary functions dealing with AVRMEM structures AVRMEM *avr_new_mem(void) { AVRMEM *m = (AVRMEM *) mmt_malloc(sizeof(*m)); + m->desc = cache_string(""); m->page_size = 1; // Ensure not 0 m->initval = -1; // Unknown value represented as -1 @@ -307,6 +296,7 @@ AVRMEM *avr_new_mem(void) { // Create memory from name and size AVRMEM *avr_new_memory(const char *name, int size) { AVRMEM *m = (AVRMEM *) mmt_malloc(sizeof(*m)); + m->desc = cache_string(name); m->page_size = 1; // Ensure not 0 m->size = size; @@ -320,6 +310,7 @@ AVRMEM *avr_new_memory(const char *name, int size) { AVRMEM_ALIAS *avr_new_memalias(void) { AVRMEM_ALIAS *m = (AVRMEM_ALIAS *) mmt_malloc(sizeof *m); + m->desc = cache_string(""); return m; } @@ -333,33 +324,32 @@ const char *avr_mem_name(const AVRPART *p, const AVRMEM *mem) { ret[n/2] = 0; AVRMEM_ALIAS *alias = avr_find_memalias(p, mem); + if(alias && alias->desc && *alias->desc) { int l = strlen(ret); + ret[l] = '/'; - strncpy(ret+l+1, alias->desc, n-l-1); + strncpy(ret + l + 1, alias->desc, n - l - 1); ret[n] = 0; } return cache_string(ret); } -/* - * Allocate and initialize memory buffers for each of the device's - * defined memory regions. - */ +// Allocate and initialize memory buffers for each of the device's defined memory regions int avr_initmem(const AVRPART *p) { if(p == NULL || p->mem == NULL) return -1; - for (LNODEID ln=lfirst(p->mem); ln; ln=lnext(ln)) { + for(LNODEID ln = lfirst(p->mem); ln; ln = lnext(ln)) { AVRMEM *m = ldata(ln); - m->buf = mmt_malloc(m->size); + + m->buf = mmt_malloc(m->size); m->tags = mmt_malloc(m->size); } return 0; } - AVRMEM *avr_dup_mem(const AVRMEM *m) { AVRMEM *n = avr_new_mem(); @@ -392,7 +382,7 @@ AVRMEM_ALIAS *avr_dup_memalias(const AVRMEM_ALIAS *m) { return n; } -void avr_free_mem(AVRMEM * m) { +void avr_free_mem(AVRMEM *m) { if(m == NULL) return; @@ -404,7 +394,7 @@ void avr_free_mem(AVRMEM * m) { mmt_free(m->tags); m->tags = NULL; } - for(size_t i=0; iop)/sizeof(m->op[0]); i++) { + for(size_t i = 0; i < sizeof(m->op)/sizeof(m->op[0]); i++) { if(m->op[i]) { avr_free_opcode(m->op[i]); m->op[i] = NULL; @@ -429,9 +419,9 @@ AVRMEM_ALIAS *avr_locate_memalias(const AVRPART *p, const char *desc) { l = strlen(desc); matches = 0; match = NULL; - for(ln=lfirst(p->mem_alias); ln; ln=lnext(ln)) { + for(ln = lfirst(p->mem_alias); ln; ln = lnext(ln)) { m = ldata(ln); - if(d1 == *m->desc && !strncmp(m->desc, desc, l)) { // Partial initial match + if(d1 == *m->desc && !strncmp(m->desc, desc, l)) { // Partial initial match match = m; matches++; if(m->desc[l] == 0) // Exact match; return straight away @@ -454,9 +444,9 @@ AVRMEM *avr_locate_mem_noalias(const AVRPART *p, const char *desc) { l = strlen(desc); matches = 0; match = NULL; - for(ln=lfirst(p->mem); ln; ln=lnext(ln)) { + for(ln = lfirst(p->mem); ln; ln = lnext(ln)) { m = ldata(ln); - if(d1 == *m->desc && !strncmp(m->desc, desc, l)) { // Partial initial match + if(d1 == *m->desc && !strncmp(m->desc, desc, l)) { // Partial initial match match = m; matches++; if(m->desc[l] == 0) // Exact match; return straight away @@ -467,7 +457,6 @@ AVRMEM *avr_locate_mem_noalias(const AVRPART *p, const char *desc) { return matches == 1? match: NULL; } - AVRMEM *avr_locate_mem(const AVRPART *p, const char *desc) { AVRMEM *m = avr_locate_mem_noalias(p, desc); @@ -476,6 +465,7 @@ AVRMEM *avr_locate_mem(const AVRPART *p, const char *desc) { // Not yet found: look for matching alias name AVRMEM_ALIAS *a = avr_locate_memalias(p, desc); + return a? a->aliased_mem: NULL; } @@ -484,9 +474,9 @@ AVRMEM *avr_locate_fuse_by_offset(const AVRPART *p, unsigned int off) { AVRMEM *m; if(p && p->mem) - for(LNODEID ln=lfirst(p->mem); ln; ln=lnext(ln)) + for(LNODEID ln = lfirst(p->mem); ln; ln = lnext(ln)) if(mem_is_a_fuse(m = ldata(ln))) - if(off == mem_fuse_offset(m) || (m->size == 2 && off-1 == mem_fuse_offset(m))) + if(off == mem_fuse_offset(m) || (m->size == 2 && off - 1 == mem_fuse_offset(m))) return m; return NULL; @@ -496,10 +486,11 @@ AVRMEM *avr_locate_fuse_by_offset(const AVRPART *p, unsigned int off) { AVRMEM *avr_locate_mem_by_type(const AVRPART *p, Memtype type) { AVRMEM *m; Memtype off = type & MEM_FUSEOFF_MASK; + type &= ~(Memtype) MEM_FUSEOFF_MASK; if(p && p->mem) - for(LNODEID ln=lfirst(p->mem); ln; ln=lnext(ln)) + for(LNODEID ln = lfirst(p->mem); ln; ln = lnext(ln)) if((m = ldata(ln))->type & type) if(type != MEM_IS_A_FUSE || off == mem_fuse_offset(m)) return m; @@ -514,8 +505,9 @@ unsigned int avr_data_offset(const AVRPART *p) { AVRMEM_ALIAS *avr_find_memalias(const AVRPART *p, const AVRMEM *m_orig) { if(p && p->mem_alias && m_orig) - for(LNODEID ln=lfirst(p->mem_alias); ln; ln=lnext(ln)) { + for(LNODEID ln = lfirst(p->mem_alias); ln; ln = lnext(ln)) { AVRMEM_ALIAS *m = ldata(ln); + if(m->aliased_mem == m_orig) return m; } @@ -535,8 +527,7 @@ int avr_locate_upidx(const AVRPART *p) { idx = upidxname(p->desc); if(idx < 0) - pmsg_error("uP_table neither knows mcuid %d nor part %s\n", - p->mcuid, p->desc && *p->desc? p->desc: "???"); + pmsg_error("uP_table neither knows mcuid %d nor part %s\n", p->mcuid, p->desc && *p->desc? p->desc: "???"); return idx; } @@ -548,10 +539,10 @@ const Avrintel *avr_locate_uP(const AVRPART *p) { return idx < 0? NULL: uP_table + idx; } - // Return pointer to config table for the part and set number of config bitfields const Configitem *avr_locate_configitems(const AVRPART *p, int *ncp) { int idx = avr_locate_upidx(p); + if(idx < 0) return NULL; @@ -560,8 +551,9 @@ const Configitem *avr_locate_configitems(const AVRPART *p, int *ncp) { } // Return pointer to ISR table for the part and set number of interrupts -const char * const *avr_locate_isrtable(const AVRPART *p, int *nip) { +const char *const *avr_locate_isrtable(const AVRPART *p, int *nip) { int idx = avr_locate_upidx(p); + if(idx < 0) return NULL; @@ -572,6 +564,7 @@ const char * const *avr_locate_isrtable(const AVRPART *p, int *nip) { // Return pointer to register file for the part and set number of registers const Register_file *avr_locate_register_file(const AVRPART *p, int *nrp) { int idx = avr_locate_upidx(p); + if(idx < 0) return NULL; @@ -580,25 +573,25 @@ const Register_file *avr_locate_register_file(const AVRPART *p, int *nrp) { } /* - * Return pointer to a register that uniquely matches the argument reg or - * NULL if no or more than one register matches the reg argument. + * Return pointer to a register that uniquely matches the argument reg or NULL + * if no or more than one register matches the reg argument. * - * Register names have the form module.name or module.instance.name. The - * caller provides a matching function which can be str_eq, str_starts, - * str_matched_by etc. If reg is a full, existing register name, eg, - * porta.out then a pointer to that register entry is returned irrespective - * of the matching function. avr_locate_register() also tries to match the - * last colon-separated segments (instance.name or name) using the provided - * matching function. If reg is the same as instance.name or name then the - * matching function switches to str_eq(). This allows the only ADC register - * adc.adc to be addressed by adc under a lax str_begins() matching even - * though there are other registers that start with adc, eg, adc.adcsra. + * Register names have the form module.name or module.instance.name. The caller + * provides a matching function which can be str_eq, str_starts, str_matched_by + * etc. If reg is a full, existing register name, eg, porta.out then a pointer + * to that register entry is returned irrespective of the matching function. + * avr_locate_register() also tries to match the last colon-separated segments + * (instance.name or name) using the provided matching function. If reg is the + * same as instance.name or name then the matching function switches to + * str_eq(). This allows the only ADC register adc.adc to be addressed by adc + * under a lax str_begins() matching even though there are other registers that + * start with adc, eg, adc.adcsra. */ const Register_file *avr_locate_register(const Register_file *rgf, int nr, const char *reg, - int (*match)(const char *, const char*)) { + int (*match)(const char *, const char *)) { - if(!rgf || nr < 1 || !reg || !match) + if (!rgf || nr < 1 || !reg || !match) return NULL; const Register_file *ret = NULL; @@ -606,15 +599,16 @@ const Register_file *avr_locate_register(const Register_file *rgf, int nr, const for(int i = 0; i < nr; i++) { int reg_matched = 0; + // Match against module.instance.name, instance.name or name - for(const char *p = rgf[i].reg; p; p = strchr(p, '.'), p = p? p+1: p) + for(const char *p = rgf[i].reg; p; p = strchr(p, '.'), p = p? p + 1: p) if(match(p, reg)) { - if(p == rgf[i].reg && (eqmatch || str_eq(p, reg))) // Reg is full name: return straight away - return rgf+i; - if(!eqmatch && str_eq(p, reg)) // reg same as segment: switch to str_eq() matching + if(p == rgf[i].reg && (eqmatch || str_eq(p, reg))) // reg is full name: return straight away + return rgf + i; + if(!eqmatch && str_eq(p, reg)) // reg same as segment: switch to str_eq() matching return avr_locate_register(rgf, nr, reg, str_eq); if(!reg_matched++) // Record a matching register only once - nmatches++, ret = rgf+i; + nmatches++, ret = rgf + i; } } @@ -624,32 +618,33 @@ const Register_file *avr_locate_register(const Register_file *rgf, int nr, const /* * Return a NULL terminated malloc'd list of pointers to matching registers * - * Register names have the form module.name or module.instance.name. The - * caller provides a matching function which can be str_eq, str_starts, - * str_matched_by etc. If reg is a full, existing register name, eg, - * porta.out then the returned list is confined to this specific entry - * irrespective of the matching function. avr_locate_registerlist() also - * tries to match the last colon-separated segments (instance.name or name) - * using the provided matching function. If the argument reg is the same as - * instance.name or name then the matching function switches to str_eq() - * reducing the returned list to those that match that full segment. This - * behaviour can be suppressed by specifying a pattern for reg, eg, adc* - * together with the matching function str_matched_by. + * Register names have the form module.name or module.instance.name. The caller + * provides a matching function which can be str_eq, str_starts, str_matched_by + * etc. If reg is a full, existing register name, eg, porta.out then the + * returned list is confined to this specific entry irrespective of the + * matching function. avr_locate_registerlist() also tries to match the last + * colon-separated segments (instance.name or name) using the provided matching + * function. If the argument reg is the same as instance.name or name then the + * matching function switches to str_eq() reducing the returned list to those + * that match that full segment. This behaviour can be suppressed by specifying + * a pattern for reg, eg, adc* together with the matching function + * str_matched_by. */ const Register_file **avr_locate_registerlist(const Register_file *rgf, int nr, const char *reg, - int (*match)(const char *, const char*)) { + int (*match)(const char *, const char *)) { - const Register_file **ret = mmt_malloc(sizeof rgf*(nr>0? nr+1: 1)), **r = ret; + const Register_file **ret = mmt_malloc(sizeof rgf*(nr > 0? nr + 1: 1)), **r = ret; int eqmatch = match == str_eq; if(rgf && reg && match) for(int i = 0; i < nr; i++) { int reg_matched = 0; + // Match against module.instance.name, instance.name or name - for(const char *p = rgf[i].reg; p; p = strchr(p, '.'), p = p? p+1: p) + for(const char *p = rgf[i].reg; p; p = strchr(p, '.'), p = p? p + 1: p) if(match(p, reg)) { - if(p == rgf[i].reg && (eqmatch || str_eq(p, reg))) { // Reg is full name: return only that - ret[0] = rgf+i; + if(p == rgf[i].reg && (eqmatch || str_eq(p, reg))) { // Reg is full name: return only that + ret[0] = rgf + i; ret[1] = NULL; return ret; } @@ -657,8 +652,8 @@ const Register_file **avr_locate_registerlist(const Register_file *rgf, int nr, mmt_free(ret); return avr_locate_registerlist(rgf, nr, reg, str_eq); } - if(!reg_matched++) // Record a matching register only once - *r++ = rgf+i; + if(!reg_matched++) // Record a matching register only once + *r++ = rgf + i; } } *r = NULL; @@ -675,9 +670,9 @@ const Register_file **avr_locate_registerlist(const Register_file *rgf, int nr, * then a pointer to that is returned irrespective of the matching function. */ const Configitem *avr_locate_config(const Configitem *cfg, int nc, const char *name, - int (*match)(const char *, const char*)) { + int (*match)(const char *, const char *)) { - if(!cfg || nc < 1 || !name || !match) + if (!cfg || nc < 1 || !name || !match) return NULL; const Configitem *ret = NULL; @@ -685,9 +680,9 @@ const Configitem *avr_locate_config(const Configitem *cfg, int nc, const char *n for(int i = 0; i < nc; i++) { if(match(cfg[i].name, name)) { - if(match == str_eq || str_eq(cfg[i].name, name)) // Full name specified: return straight away - return cfg+i; - nmatches++, ret = cfg+i; + if(match == str_eq || str_eq(cfg[i].name, name)) // Full name specified: return straight away + return cfg + i; + nmatches++, ret = cfg + i; } } @@ -703,19 +698,19 @@ const Configitem *avr_locate_config(const Configitem *cfg, int nc, const char *n * matching function. */ const Configitem **avr_locate_configlist(const Configitem *cfg, int nc, const char *name, - int (*match)(const char *, const char*)) { + int (*match)(const char *, const char *)) { - const Configitem **ret = mmt_malloc(sizeof cfg*(nc>0? nc+1: 1)), **r = ret; + const Configitem **ret = mmt_malloc(sizeof cfg*(nc > 0? nc + 1: 1)), **r = ret; if(cfg && name && match) { for(int i = 0; i < nc; i++) if(match(cfg[i].name, name)) { - if(match == str_eq || str_eq(cfg[i].name, name)) { // Full name specified: return straight away - ret[0] = cfg+i; + if(match == str_eq || str_eq(cfg[i].name, name)) { // Full name specified: return straight away + ret[0] = cfg + i; ret[1] = NULL; return ret; } - *r++ = cfg+i; + *r++ = cfg + i; } } *r = NULL; @@ -736,12 +731,14 @@ static AVRMEM *avr_locate_config_mem_c_value(const PROGRAMMER *pgm, const AVRPAR } const Configitem *c = avr_locate_config(cfg, nc, cname, str_contains); + if(!c) { pmsg_error("%s does not have a unique config item matched by %s\n", p->desc, cname); return NULL; } AVRMEM *mem = str_starts(c->memstr, "lock")? avr_locate_lock(p): avr_locate_fuse_by_offset(p, c->memoffset); + if(!mem) mem = avr_locate_mem(p, c->memstr); if(!mem) { @@ -755,6 +752,7 @@ static AVRMEM *avr_locate_config_mem_c_value(const PROGRAMMER *pgm, const AVRPAR } int fusel = 0; + for(int i = 0; i < mem->size; i++) if(led_read_byte(pgm, p, mem, i, (unsigned char *) &fusel + i) < 0) { pmsg_error("cannot read from %s's %s memory\n", p->desc, mem->desc); @@ -785,7 +783,7 @@ int avr_set_config_value(const PROGRAMMER *pgm, const AVRPART *p, const char *cn const Configitem *c; int fusel; - if(!(mem=avr_locate_config_mem_c_value(pgm, p, cname, &c, &fusel))) + if(!(mem = avr_locate_config_mem_c_value(pgm, p, cname, &c, &fusel))) return -1; if((value << c->lsh) & ~c->mask) @@ -804,9 +802,8 @@ int avr_set_config_value(const PROGRAMMER *pgm, const AVRPART *p, const char *cn return 0; } - static const char *print_num(const char *fmt, int n) { - return str_ccprintf(n<10? "%d": fmt, n); + return str_ccprintf(n < 10? "%d": fmt, n); } static int num_len(const char *fmt, int n) { @@ -814,7 +811,7 @@ static int num_len(const char *fmt, int n) { } void avr_mem_display(FILE *f, const PROGRAMMER *pgm, const AVRPART *p, const char *prefix) { - const char *table_colum[] = {"Memory", "Size", "Pg size", "Offset"}; + const char *table_colum[] = { "Memory", "Size", "Pg size", "Offset" }; const char *table_padding = "-------------------------------"; const int memory_col = 0, offset_col = 3; int m_char_max[4]; @@ -823,16 +820,18 @@ void avr_mem_display(FILE *f, const PROGRAMMER *pgm, const AVRPART *p, const cha for(int i = 0; i < 4; i++) m_char_max[i] = strlen(table_colum[i]); - for (LNODEID ln=lfirst(p->mem); ln; ln=lnext(ln)) { + for(LNODEID ln = lfirst(p->mem); ln; ln = lnext(ln)) { if(avr_mem_exclude(pgm, p, (m = ldata(ln)))) continue; - int m_size[] = {0, m->size, m->page_size, m->offset}; + int m_size[] = { 0, m->size, m->page_size, m->offset }; // Max column widths for(int i = 0; i < 4; i++) { - int len = i == memory_col? (int) strlen(avr_mem_name(p, m)): + int len = + i == memory_col? (int) strlen(avr_mem_name(p, m)): num_len(i == offset_col? "0x%04x": "%d", m_size[i]); // size/pgsize/offset + if(m_char_max[i] < len) m_char_max[i] = len; } @@ -849,10 +848,8 @@ void avr_mem_display(FILE *f, const PROGRAMMER *pgm, const AVRPART *p, const cha m_char_max[2], table_colum[2], m_char_max[3], table_colum[3], prefix, - m_char_max[0], table_padding, - m_char_max[1], table_padding, - m_char_max[2], table_padding, - m_char_max[3], table_padding); + m_char_max[0], table_padding, m_char_max[1], table_padding, m_char_max[2], + table_padding, m_char_max[3], table_padding); } else { fprintf(f, "\n%s%-*s %*s %-*s\n" @@ -861,13 +858,11 @@ void avr_mem_display(FILE *f, const PROGRAMMER *pgm, const AVRPART *p, const cha m_char_max[0], table_colum[0], m_char_max[1], table_colum[1], m_char_max[2], table_colum[2], - prefix, - m_char_max[0], table_padding, - m_char_max[1], table_padding, + prefix, m_char_max[0], table_padding, m_char_max[1], table_padding, m_char_max[2], table_padding); } - for (LNODEID ln=lfirst(p->mem); ln; ln=lnext(ln)) { + for(LNODEID ln = lfirst(p->mem); ln; ln = lnext(ln)) { if(avr_mem_exclude(pgm, p, (m = ldata(ln)))) continue; @@ -876,22 +871,19 @@ void avr_mem_display(FILE *f, const PROGRAMMER *pgm, const AVRPART *p, const cha fprintf(f, "%s%-*s %*d %*d %*s \n", prefix, m_char_max[0], avr_mem_name(p, m), - m_char_max[1], m->size, - m_char_max[2], m->page_size, + m_char_max[1], m->size, m_char_max[2], m->page_size, m_char_max[3], print_num("0x%04x", m->offset)); } else { fprintf(f, "%s%-*s %*d %*d\n", - prefix, - m_char_max[0], avr_mem_name(p, m), - m_char_max[1], m->size, - m_char_max[2], m->page_size); + prefix, m_char_max[0], avr_mem_name(p, m), m_char_max[1], + m->size, m_char_max[2], m->page_size); } } } int avr_variants_display(FILE *f, const AVRPART *p, const char *prefix) { const char *table_padding = "-------------------------------"; - const char *var_table_column[] = {"Variants", "Package", "F max", "T range", "V range"}; + const char *var_table_column[] = { "Variants", "Package", "F max", "T range", "V range" }; char var_tok[5][50]; int var_tok_len[5]; @@ -900,17 +892,17 @@ int avr_variants_display(FILE *f, const AVRPART *p, const char *prefix) { if(lsize(p->variants)) { // Split, eg, "ATtiny841-SSU: SOIC14, Fmax=16 MHz, T=[-40 C, 85 C], Vcc=[1.7 V, 5.5 V]" - for(LNODEID ln=lfirst(p->variants); ln; ln=lnext(ln)) + for(LNODEID ln = lfirst(p->variants); ln; ln = lnext(ln)) if(5 == sscanf(ldata(ln), "%49[^:]: %49[^,], Fmax=%49[^,], T=%48[^]]], Vcc=%48[^]]]", - var_tok[0], var_tok[1], var_tok[2], var_tok[3], var_tok[4])) + var_tok[0], var_tok[1], var_tok[2], var_tok[3], var_tok[4])) for(int i = 0; i < 5; i++) if(var_tok_len[i] < (int) strlen(var_tok[i])) - var_tok_len[i] = strlen(var_tok[i]) + (i>2); // Add 1 for closing interval bracket + var_tok_len[i] = strlen(var_tok[i]) + (i > 2); // Add 1 for closing interval bracket // Print variants table header fprintf(f, "\n%s%-*s %-*s %-*s %-*s %-*s\n" - "%s%.*s--%.*s--%.*s--%.*s--%.*s\n", + "%s%.*s--%.*s--%.*s--%.*s--%.*s\n", prefix, var_tok_len[0], var_table_column[0], var_tok_len[1], var_table_column[1], @@ -919,25 +911,21 @@ int avr_variants_display(FILE *f, const AVRPART *p, const char *prefix) { var_tok_len[4], var_table_column[4], prefix, var_tok_len[0], table_padding, - var_tok_len[1], table_padding, - var_tok_len[2], table_padding, - var_tok_len[3], table_padding, - var_tok_len[4], table_padding); + var_tok_len[1], table_padding, var_tok_len[2], table_padding, + var_tok_len[3], table_padding, var_tok_len[4], table_padding); // Print variants table content - for(LNODEID ln=lfirst(p->variants); ln; ln=lnext(ln)) + for(LNODEID ln = lfirst(p->variants); ln; ln = lnext(ln)) if(5 == sscanf(ldata(ln), "%49[^:]: %49[^,], Fmax=%49[^,], T=%48[^]]], Vcc=%48[^]]]", - var_tok[0], var_tok[1], var_tok[2], var_tok[3], var_tok[4])) { + var_tok[0], var_tok[1], var_tok[2], var_tok[3], var_tok[4])) { strcat(var_tok[3], "]"); strcat(var_tok[4], "]"); fprintf(f, "%s%-*s %-*s %-*s %-*s %-*s\n", prefix, var_tok_len[0], var_tok[0], - var_tok_len[1], var_tok[1], - var_tok_len[2], var_tok[2], - var_tok_len[3], var_tok[3], - var_tok_len[4], var_tok[4]); + var_tok_len[1], var_tok[1], var_tok_len[2], var_tok[2], var_tok_len[3], + var_tok[3], var_tok_len[4], var_tok[4]); } return 0; @@ -945,10 +933,7 @@ int avr_variants_display(FILE *f, const AVRPART *p, const char *prefix) { return -1; } - -/* - * Elementary functions dealing with AVRPART structures - */ +// Elementary functions dealing with AVRPART structures AVRPART *avr_new_part(void) { AVRPART *p = (AVRPART *) mmt_malloc(sizeof(AVRPART)); @@ -969,7 +954,7 @@ AVRPART *avr_new_part(void) { // Default values p->mcuid = -1; p->hvupdi_variant = -1; - p->autobaud_sync = 0x30; // STK_GET_SYNC + p->autobaud_sync = 0x30; // STK_GET_SYNC memset(p->signature, 0xFF, 3); p->reset_disposition = RESET_DEDICATED; p->retry_pulse = PIN_AVR_SCK; @@ -981,7 +966,6 @@ AVRPART *avr_new_part(void) { return p; } - AVRPART *avr_dup_part(const AVRPART *d) { AVRPART *p = avr_new_part(); @@ -992,16 +976,19 @@ AVRPART *avr_dup_part(const AVRPART *d) { p->variants = lcreat(NULL, 0); p->mem = lcreat(NULL, 0); p->mem_alias = lcreat(NULL, 0); - for(LNODEID ln=lfirst(d->mem); ln; ln=lnext(ln)) { + for(LNODEID ln = lfirst(d->mem); ln; ln = lnext(ln)) { AVRMEM *m = ldata(ln); AVRMEM *m2 = avr_dup_mem(m); + ladd(p->mem, m2); // See if there is any alias for it - for(LNODEID ln2=lfirst(d->mem_alias); ln2; ln2=lnext(ln2)) { + for(LNODEID ln2 = lfirst(d->mem_alias); ln2; ln2 = lnext(ln2)) { AVRMEM_ALIAS *a = ldata(ln2); - if (a->aliased_mem == m) { + + if(a->aliased_mem == m) { // Yes, duplicate it, adjust the pointer and add to new list AVRMEM_ALIAS *a2 = avr_dup_memalias(a); + a2->aliased_mem = m2; ladd(p->mem_alias, a2); } @@ -1015,17 +1002,17 @@ AVRPART *avr_dup_part(const AVRPART *d) { return p; } -void avr_free_part(AVRPART * d) { - ldestroy_cb(d->mem, (void(*)(void *)) avr_free_mem); +void avr_free_part(AVRPART *d) { + ldestroy_cb(d->mem, (void (*)(void *)) avr_free_mem); d->mem = NULL; - ldestroy_cb(d->mem_alias, (void(*)(void *)) avr_free_memalias); + ldestroy_cb(d->mem_alias, (void (*)(void *)) avr_free_memalias); d->mem_alias = NULL; ldestroy_cb(d->variants, mmt_f_free); d->variants = NULL; - /* do not free d->parent_id and d->config_file */ - for(size_t i=0; iop)/sizeof(d->op[0]); i++) { - if (d->op[i] != NULL) { + // Do not free d->parent_id and d->config_file + for(size_t i = 0; i < sizeof(d->op)/sizeof(d->op[0]); i++) { + if(d->op[i] != NULL) { avr_free_opcode(d->op[i]); d->op[i] = NULL; } @@ -1034,13 +1021,13 @@ void avr_free_part(AVRPART * d) { } AVRPART *locate_part(const LISTID parts, const char *partdesc) { - AVRPART * p = NULL; + AVRPART *p = NULL; int found = 0; if(!parts || !partdesc) return NULL; - for (LNODEID ln1=lfirst(parts); ln1 && !found; ln1=lnext(ln1)) { + for(LNODEID ln1 = lfirst(parts); ln1 && !found; ln1 = lnext(ln1)) { p = ldata(ln1); if(part_eq(p, partdesc, str_caseeq)) found = 1; @@ -1051,9 +1038,10 @@ AVRPART *locate_part(const LISTID parts, const char *partdesc) { AVRPART *locate_part_by_avr910_devcode(const LISTID parts, int devcode) { if(parts) - for (LNODEID ln1=lfirst(parts); ln1; ln1=lnext(ln1)) { - AVRPART * p = ldata(ln1); - if (p->avr910_devcode == devcode) + for(LNODEID ln1 = lfirst(parts); ln1; ln1 = lnext(ln1)) { + AVRPART *p = ldata(ln1); + + if(p->avr910_devcode == devcode) return p; } @@ -1063,9 +1051,10 @@ AVRPART *locate_part_by_avr910_devcode(const LISTID parts, int devcode) { // Return pointer to first part that has signature sig (unless all 0xff or all 0x00); NULL if no match AVRPART *locate_part_by_signature_pm(const LISTID parts, unsigned char *sig, int sigsize, int prog_modes) { if(parts && sigsize == 3) { - for(LNODEID ln=lfirst(parts); ln; ln=lnext(ln)) { + for(LNODEID ln = lfirst(parts); ln; ln = lnext(ln)) { AVRPART *p = ldata(ln); - if(!*p->id || *p->id == '.') // Skip stump entries + + if(!*p->id || *p->id == '.') // Skip stump entries continue; if(!is_memset(p->signature, 0xff, 3) && !is_memset(p->signature, 0, 3)) if(!memcmp(p->signature, sig, 3) && p->prog_modes & prog_modes) @@ -1082,30 +1071,32 @@ AVRPART *locate_part_by_signature(const LISTID parts, unsigned char *sig, int si // Return whether two signatures represent SW-compatible parts int avr_sig_compatible(const unsigned char *sig1, const unsigned char *sig2) { // SW-compatible parts (same memories, interrupts and regfiles) despite different signatures - static const struct { unsigned char sig[3], equ[3]; } compat[] = { - {{0x1e, 0x97, 0x06}, {0x1e, 0x97, 0x05}}, // ATmega1284 vs ATmega1284P - {{0x1e, 0xa7, 0x03}, {0x1e, 0xa7, 0x02}}, // ATmega1284RFR2 vs ATmega128RFR2 - {{0x1e, 0x94, 0x0f}, {0x1e, 0x94, 0x0a}}, // ATmega164A vs ATmega164P=ATmega164PA - {{0x1e, 0x94, 0x10}, {0x1e, 0x94, 0x07}}, // ATmega165A vs ATmega165=ATmega165=ATmega165PA - {{0x1e, 0x94, 0x06}, {0x1e, 0x94, 0x0b}}, // ATmega168=ATmega168A vs ATmega168P=ATmega168PA - {{0x1e, 0x94, 0x11}, {0x1e, 0x94, 0x05}}, // ATmega169A vs ATmega169=ATmega169P=ATmega169PA - {{0x1e, 0xa8, 0x03}, {0x1e, 0xa8, 0x02}}, // ATmega2564RFR2 vs ATmega256RFR2 - {{0x1e, 0x95, 0x15}, {0x1e, 0x95, 0x08}}, // ATmega324A vs ATmega324P - {{0x1e, 0x95, 0x15}, {0x1e, 0x95, 0x11}}, // ATmega324A vs ATmega324PA - {{0x1e, 0x95, 0x08}, {0x1e, 0x95, 0x11}}, // ATmega324P vs ATmega324PA - {{0x1e, 0x95, 0x06}, {0x1e, 0x95, 0x0e}}, // ATmega3250=ATmega3250A vs ATmega3250P=ATmega3250PA - {{0x1e, 0x95, 0x05}, {0x1e, 0x95, 0x0d}}, // ATmega325=ATmega325A vs ATmega325P=ATmega325PA - {{0x1e, 0x95, 0x04}, {0x1e, 0x95, 0x0c}}, // ATmega3290=ATmega3290A vs ATmega3290P=ATmega3290PA - {{0x1e, 0x95, 0x03}, {0x1e, 0x95, 0x0b}}, // ATmega329=ATmega329A vs ATmega329P=ATmega329PA - {{0x1e, 0x92, 0x05}, {0x1e, 0x92, 0x0a}}, // ATmega48=ATmega48A vs ATmega48P=ATmega48PA - {{0x1e, 0x92, 0x05}, {0x1e, 0x92, 0x0a}}, // ATmega48=ATmega48A vs ATmega48P=ATmega48PA - {{0x1e, 0x96, 0x09}, {0x1e, 0x96, 0x0a}}, // ATmega644=ATmega644A vs ATmega644P=ATmega644PA - {{0x1e, 0xa6, 0x03}, {0x1e, 0xa6, 0x02}}, // ATmega644RFR2 vs ATmega64RFR2 - {{0x1e, 0x96, 0x06}, {0x1e, 0x96, 0x0e}}, // ATmega6450=ATmega6450A vs ATmega6450P - {{0x1e, 0x96, 0x05}, {0x1e, 0x96, 0x0d}}, // ATmega645=ATmega645A vs ATmega645P - {{0x1e, 0x96, 0x04}, {0x1e, 0x96, 0x0c}}, // ATmega6490=ATmega6490A vs ATmega6490P - {{0x1e, 0x96, 0x03}, {0x1e, 0x96, 0x0b}}, // ATmega649=ATmega649A vs ATmega649P - {{0x1e, 0x93, 0x0a}, {0x1e, 0x93, 0x0f}}, // ATmega88=ATmega88A=ATA6612C vs ATmega88P=ATmega88PA + static const struct { + unsigned char sig[3], equ[3]; + } compat[] = { + {{0x1e, 0x97, 0x06}, {0x1e, 0x97, 0x05}}, // ATmega1284 vs ATmega1284P + {{0x1e, 0xa7, 0x03}, {0x1e, 0xa7, 0x02}}, // ATmega1284RFR2 vs ATmega128RFR2 + {{0x1e, 0x94, 0x0f}, {0x1e, 0x94, 0x0a}}, // ATmega164A vs ATmega164P=ATmega164PA + {{0x1e, 0x94, 0x10}, {0x1e, 0x94, 0x07}}, // ATmega165A vs ATmega165=ATmega165=ATmega165PA + {{0x1e, 0x94, 0x06}, {0x1e, 0x94, 0x0b}}, // ATmega168=ATmega168A vs ATmega168P=ATmega168PA + {{0x1e, 0x94, 0x11}, {0x1e, 0x94, 0x05}}, // ATmega169A vs ATmega169=ATmega169P=ATmega169PA + {{0x1e, 0xa8, 0x03}, {0x1e, 0xa8, 0x02}}, // ATmega2564RFR2 vs ATmega256RFR2 + {{0x1e, 0x95, 0x15}, {0x1e, 0x95, 0x08}}, // ATmega324A vs ATmega324P + {{0x1e, 0x95, 0x15}, {0x1e, 0x95, 0x11}}, // ATmega324A vs ATmega324PA + {{0x1e, 0x95, 0x08}, {0x1e, 0x95, 0x11}}, // ATmega324P vs ATmega324PA + {{0x1e, 0x95, 0x06}, {0x1e, 0x95, 0x0e}}, // ATmega3250=ATmega3250A vs ATmega3250P=ATmega3250PA + {{0x1e, 0x95, 0x05}, {0x1e, 0x95, 0x0d}}, // ATmega325=ATmega325A vs ATmega325P=ATmega325PA + {{0x1e, 0x95, 0x04}, {0x1e, 0x95, 0x0c}}, // ATmega3290=ATmega3290A vs ATmega3290P=ATmega3290PA + {{0x1e, 0x95, 0x03}, {0x1e, 0x95, 0x0b}}, // ATmega329=ATmega329A vs ATmega329P=ATmega329PA + {{0x1e, 0x92, 0x05}, {0x1e, 0x92, 0x0a}}, // ATmega48=ATmega48A vs ATmega48P=ATmega48PA + {{0x1e, 0x92, 0x05}, {0x1e, 0x92, 0x0a}}, // ATmega48=ATmega48A vs ATmega48P=ATmega48PA + {{0x1e, 0x96, 0x09}, {0x1e, 0x96, 0x0a}}, // ATmega644=ATmega644A vs ATmega644P=ATmega644PA + {{0x1e, 0xa6, 0x03}, {0x1e, 0xa6, 0x02}}, // ATmega644RFR2 vs ATmega64RFR2 + {{0x1e, 0x96, 0x06}, {0x1e, 0x96, 0x0e}}, // ATmega6450=ATmega6450A vs ATmega6450P + {{0x1e, 0x96, 0x05}, {0x1e, 0x96, 0x0d}}, // ATmega645=ATmega645A vs ATmega645P + {{0x1e, 0x96, 0x04}, {0x1e, 0x96, 0x0c}}, // ATmega6490=ATmega6490A vs ATmega6490P + {{0x1e, 0x96, 0x03}, {0x1e, 0x96, 0x0b}}, // ATmega649=ATmega649A vs ATmega649P + {{0x1e, 0x93, 0x0a}, {0x1e, 0x93, 0x0f}}, // ATmega88=ATmega88A=ATA6612C vs ATmega88P=ATmega88PA }; if(!memcmp(sig1, sig2, 3)) @@ -1122,29 +1113,25 @@ int avr_sig_compatible(const unsigned char *sig1, const unsigned char *sig2) { } /* - * Iterate over the list of avrparts given as "avrparts", and - * call the callback function cb for each entry found. cb is being - * passed the following arguments: - * . the name of the avrpart (for -p) - * . the descriptive text given in the config file - * . the name of the config file this avrpart has been defined in - * . the line number of the config file this avrpart has been defined at - * . the "cookie" passed into walk_avrparts() (opaque client data) + * Iterate over the list of avrparts given as "avrparts", and call the callback + * function cb for each entry found. cb is being passed the following arguments: + * - Name of the avrpart (for -p) + * - Descriptive text given in the config file + * - Name of the config file this avrpart has been defined in + * - Line number of the config file this avrpart has been defined at + * - Cookie passed into walk_avrparts() (opaque client data) */ -void walk_avrparts(LISTID avrparts, walk_avrparts_cb cb, void *cookie) -{ +void walk_avrparts(LISTID avrparts, walk_avrparts_cb cb, void *cookie) { LNODEID ln1; - AVRPART * p; + AVRPART *p; - for (ln1 = lfirst(avrparts); ln1; ln1 = lnext(ln1)) { + for(ln1 = lfirst(avrparts); ln1; ln1 = lnext(ln1)) { p = ldata(ln1); cb(p->id, p->desc, p->config_file, p->lineno, cookie); } } -/* - * Compare function to sort a list of parts - */ +// Compare function to sort a list of parts static int sort_avrparts_compare(const AVRPART *p1, const AVRPART *p2) { if(p1 == NULL || p1->desc == NULL || p2 == NULL || p2->desc == NULL) return 0; @@ -1152,15 +1139,11 @@ static int sort_avrparts_compare(const AVRPART *p1, const AVRPART *p2) { return strcasecmp(p1->desc, p2->desc); } -/* - * Sort the list avrparts of parts - */ -void sort_avrparts(LISTID avrparts) -{ - lsort(avrparts,(int (*)(void*, void*)) sort_avrparts_compare); +// Sort the list avrparts of parts +void sort_avrparts(LISTID avrparts) { + lsort(avrparts, (int (*)(void *, void *)) sort_avrparts_compare); } - void avr_display(FILE *f, const PROGRAMMER *pgm, const AVRPART *p, const char *prefix, int verbose) { fprintf(f, "%sAVR part : %s\n", prefix, p->desc); fprintf(f, "%sProgramming modes : %s\n", prefix, str_prog_modes(p->prog_modes)); @@ -1171,9 +1154,8 @@ void avr_display(FILE *f, const PROGRAMMER *pgm, const AVRPART *p, const char *p } } - char cmdbitchar(CMDBIT cb) { - switch(cb.type) { + switch (cb.type) { case AVR_CMDBIT_IGNORE: return 'x'; case AVR_CMDBIT_VALUE: @@ -1189,22 +1171,20 @@ char cmdbitchar(CMDBIT cb) { } } - char *cmdbitstr(CMDBIT cb) { char space[32]; *space = cmdbitchar(cb); if(*space == 'a') - sprintf(space+1, "%d", cb.bitno); + sprintf(space + 1, "%d", cb.bitno); else space[1] = 0; return mmt_strdup(space); } - const char *opcodename(int opnum) { - switch(opnum) { + switch (opnum) { case AVR_OP_READ: return "read"; case AVR_OP_WRITE: @@ -1234,7 +1214,6 @@ const char *opcodename(int opnum) { } } - // Unique string representation of an opcode char *opcode2str(const OPCODE *op, int opnum, int detailed) { char cb, space[1024], *sp = space; @@ -1244,10 +1223,10 @@ char *opcode2str(const OPCODE *op, int opnum, int detailed) { return mmt_strdup("NULL"); // Can the opcode be printed in a compact way? Only if i, o and a bits are systematic. - for(int i=31; i >= 0; i--) - switch(op->bit[i].type) { + for(int i = 31; i >= 0; i--) + switch (op->bit[i].type) { case AVR_CMDBIT_ADDRESS: - if(i<8 || i>23 || op->bit[i].bitno != (opnum == AVR_OP_LOAD_EXT_ADDR? i+8: i-8)) + if(i < 8 || i > 23 || op->bit[i].bitno != (opnum == AVR_OP_LOAD_EXT_ADDR? i + 8: i - 8)) compact = 0; break; case AVR_CMDBIT_INPUT: @@ -1259,10 +1238,10 @@ char *opcode2str(const OPCODE *op, int opnum, int detailed) { if(detailed) *sp++ = '"'; - for(int i=31; i >= 0; i--) { + for(int i = 31; i >= 0; i--) { cb = cmdbitchar(op->bit[i]); printbit = cb == 'a' || ((strchr("io", cb) && op->bit[i].bitno != i%8)); - *sp++ = !detailed && !compact && printbit? toupper(cb): cb; // Disambiguate tsv output + *sp++ = !detailed && !compact && printbit? toupper(cb): cb; // Disambiguate tsv output if(!compact && printbit) { sprintf(sp, "%d", op->bit[i].bitno); sp += strlen(sp); @@ -1288,24 +1267,26 @@ char *opcode2str(const OPCODE *op, int opnum, int detailed) { return mmt_strdup(space); } - // Returns 1 if the part pointed to by p matches the string or pattern s under the function cmp(s, ...) int part_eq(AVRPART *p, const char *s, int (*cmp)(const char *, const char *)) { // Matching id or desc? OK - if(cmp(s, p->id) || cmp(s, p->desc)) + if (cmp(s, p->id) || cmp(s, p->desc)) return 1; // Check against all variants, either up to colon or up to dash size_t desclen = strlen(p->desc), variantlen, dashlen; char query[1024]; + for(LNODEID ln = lfirst(p->variants); ln; ln = lnext(ln)) { const char *q = (const char *) ldata(ln), *qdash = strchr(q, '-'), *qcolon = strchr(q, ':'); - variantlen = qcolon? (size_t) (qcolon-q): strlen(q); - dashlen = qdash? (size_t) (qdash-q): variantlen; - if(variantlen < sizeof query) { // Sanity: should not expect such long strings + + variantlen = qcolon? (size_t) (qcolon - q): strlen(q); + dashlen = qdash? (size_t) (qdash - q): variantlen; + if(variantlen < sizeof query) { // Sanity: should not expect such long strings // Variant names should be unique order numbers, but don't check (again) if it's the same as p->desc if(variantlen != desclen || memcmp(q, p->desc, desclen)) { - memcpy(query, q, variantlen); query[variantlen] = 0; + memcpy(query, q, variantlen); + query[variantlen] = 0; if(cmp(s, query)) return 1; // The name before dash should normally be p->desc and the dash is meant to come before the colon