??? 05/22/12 23:42 Read: times |
#187466 - Where can one learn Intermediate C techniques for 8051 |
C is really frustrating on the 8051, and I'm not sure how to get more proficient. There are many examples which teach basics (here is the memory map, this is a register/timer/port, etc), but where can the intermediate 8051 c programmer turn to see how to structure 8051 applications to overcome the the complexities that emerge in larger code bases (32k - 64k range)? And how about testing strategies for those applications? Unit testing with something like Unity?
Here's a stupid example with 4 possible implementations of a table look-up function: typedef unsigned char U8; typedef unsigned char S8; typedef unsigned int U16; #define NUMBER_OF_GROUPS 9 #define UNITS_PER_GROUP 8 unsigned char code ID_TBL[NUMBER_OF_GROUPS * UNITS_PER_GROUP] = { 0x01, 0x02, 0x03, 0x04, 0x65, 0x66, 0x67, 0x68, /* Group 01 */ 0x05, 0x06, 0x07, 0x08, 0x69, 0x70, 0x71, 0x72, /* Group 02 */ 0x09, 0x10, 0x11, 0x12, 0x73, 0x74, 0x75, 0x76, /* Group 03 */ 0x13, 0x14, 0x15, 0x16, 0x77, 0x78, 0x79, 0x80, /* Group 04 */ 0x17, 0x18, 0x19, 0x20, 0x81, 0x82, 0x83, 0x84, /* Group 05 */ 0x21, 0x22, 0x23, 0x24, 0x85, 0x86, 0x87, 0x88, /* Group 06 */ 0x25, 0x26, 0x27, 0x28, 0x89, 0x90, 0x91, 0x92, /* Group 07 */ 0x29, 0x30, 0x31, 0x32, 0x93, 0x94, 0x95, 0x96, /* Group 08 */ 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x40 /* Group 09 */ }; typedef struct { U8 num; U16 price; U8 num_units; U8 unit_status; /* char code *name; */ } group_t; /* code size = 19 bytes */ U8 get_unit_id1(group_t xdata *pgr, U8 unit_indx) { U8 idata x; x = (pgr->num - 1) * UNITS_PER_GROUP; return ID_TBL[x + unit_indx]; } /* code size = 20 bytes */ U8 get_unit_id2(group_t xdata *pgr, U8 unit_indx) { U8 idata x; x = ((pgr->num - 1) * UNITS_PER_GROUP) + unit_indx; return ID_TBL[x]; } /* code size = 25 bytes */ U8 get_unit_id3(group_t xdata *pgr, U8 unit_indx) { U8 idata x; x = pgr->num - 1; x *= UNITS_PER_GROUP; x += unit_indx; x = ID_TBL[x]; return x; } /* code size = 33 bytes */ U8 get_unit_id4(group_t xdata *pgr, U8 unit_indx) { return ID_TBL[((pgr->num - 1) * UNITS_PER_GROUP) + unit_indx]; } At any compiler optimization level (Keil C51 V9.01) the same logical code could cost you between 19 and 33 bytes of code depending on how you feel like writing it. Interestingly, the most "idiomatic" C way costs the most, which has been my experience trying to implement C techniques on the 8051. And all of this is just for a "simple" look-up. The frustrating part is I'm not exactly sure what I'm supposed to learn from this exercise, since the various cases are just various attempts to use lower level constructs in order to help the compiler out. It is certainly not obvious what the best (meaning efficient use of code space in this context) approach might be in other cases. The answer seems to be that one must fiddle with absolutely everything one writes to see what the compiler will give in the specific circumstance. It's good advise, but very distracting when trying to solve the actual application problem. However, the real point of this example was that one must start asking questions such as "Why don't you just expose the table directly?", and "Is passing around pointers to structs even necessary for this application?". These are the questions that I don't know how to answer. They are really design decision questions which will affect hundreds of little implementation decisions like the example above. How does one effectively learn the very specific craft of 8051 C programming absent some kind of mentor or larger well-written 8051 projects to study? |