4 ms·
Does c65 turn structs into parallel arrays? If you have something like struct Monster { unsigned char hitPoints; unsigned char damage; u
by greggman 9y ago
Does c65 turn structs into parallel arrays?
If you have something like
struct Monster {
unsigned char hitPoints;
unsigned char damage;
unsigned char shieldLevel;
char* name;
};
static Monster s_monsters[] = {
{ 5, 1, 0, "orc", },
{ 50, 10, 5, "dragon", },
{ 10, 3, 1, "goblin", },
};
that's a no-no on 6502. You generally need to store those in parallel byte arrays including the string pointer's upper and lower bytes each in separate arrays. That was usually done with assembler macros or compile time tools but it was important not to have to generate addresses that have to manually assembled in 6502. Similarly there's no multiply so math like sizeof(Monster) * index.
- 0xcde4c3db 9y agocc65 has a whole page on how to deal with the fact that it's a single-pass compiler with hardly any optimization [1]. It's almost more like a macro assembler than an actual compiler. http://www.cc65.org/doc/coding.html http://www.cc65.org/doc/coding.html
- IcePic 9y agoThen again, I managed to compile the code for the crypto hash Blake2s with it, producing code that validated all of the (few) test vectors I threw at it, so whatever expectations one might have on "actual compilers", for me, cc65 does fulfil a lot of them.
- mwcampbell 9y agoDidn't John Carmack use structures rather than parallel arrays even in Apple II assembly? Source: one of his old .plan files, reproduced verbatim in _Masters of Doom_ Edit: Found it online here: https://github.com/ESWAT/john-carmack-plan-archive/blob/master/by_day/johnc_plan_19980204.txt https://github.com/ESWAT/john-carmack-plan-archive/blob/mast...
- to3m 9y agoI wrote a few lines about 6502 vs Z80 a couple of years ago: https://news.ycombinator.com/item?id=10766264 https://news.ycombinator.com/item?id=10766264 (parent of linked-to comment may also be of interest) - the discussion there was about relative efficiency of the two CPUs, but the addressing mode limitations that make one approach a bit inefficient on the 6502 are the same limitations that make it a bit inconvenient. I wonder what Carmack had in mind. --- P.S. In my comment there, I didn't mention the possibility of having the arrays interleaved. Then it looks like a struct in memory (byte 0 is X and byte is DX, say), and you add the field offset to the base of the array. It's not uncommon for assemblers to have some syntax to make it easy to specify the layout of each struct, to simplify future changes, e.g.: STRUCT ITEM X DS.B 1 DX DS.B 1 ENDSTRUCT CLC LDA TABLE+ITEM.X,X ADC TABLE+ITEM.DX,X STA TABLE+ITEM.X,X LDA #0 STA TABLE+ITEM.DX,X The disadvantages of this are that it reduces the number of accessible items (the X register is only 8 bits...); it makes moving from item to item more difficult (all you can do with the X register is increment or decrement it, but now you need to add arbitrary values to it); and it means there are invalid item indexes (e.g., in the example above, index 1 is not valid). And the advantages... well, there aren't any, really, I don't think? Everything has to be done 1 byte at a time, so it makes no difference where the bytes are relative to one another... the code ends up the same anyway. (If anything, because you can do INX/DEX to get from item to item, it's actually slightly neater to have a separate table for each byte.) So you might as well do that.
- greggman 9y agoAsked to see if someone speaks up https://stackoverflow.com/questions/45667636/advantages-of-arrays-of-structures-over-parallel-arrays-in-6502-assembly-languag https://stackoverflow.com/questions/45667636/advantages-of-a...
- boomlinde 9y agoI don't think it does, and I don't think it's possible to make a nearly standards compliant C compiler that does. A struct is a contiguous piece of memory, which makes things like memcpy and malloc generally possible.