Sorry for not being clear enough, I'm not talking about optimisation or anything, and the language is 6502 assembly of course.
I am limited to 32kb of ROM, I'm soon running out of space, and after doing profiling, it turns out unfortunately the code is what takes (by far) the most ROM space, so if I have to compress something that'd have to be the code itself (which is, unfortunately, also the hardest thing to compress, (before white noise haha) ).
The difficulty doesn't come from the compressability of the data itself (common opcodes are more frequent than others so this can be exploited), but from the fact you want to pack in a mix of compressed and uncompressed code together in the same ROM where each one can reference the other.
Quote:
- compile/assemble the whole program in one go so that forward references can be resolved in the second (or later) pass. This is the approach taken by absolute assemblers.
Could you please explain more how you do this ? Unless your assembler is specifically made for this kind of applications (compressing while compiling) I don't think it's possible to do this in one pass.
Quote:
compile/assemble the program in a number of sections using an output format that indicates where forward references are in the generated code that will need patching during a final linking phase. This is the approach taken by relocatable assemblers and high level compilers languages like languages C.
This sounds like what WLA-DX (the assembler I'm using) is doing. Yes, I know, it's not a very good one, however I made the (bad) decision of using it when I started my project long ago, so I'm pretty much stuck to it unless I spend hours converting assembly code from an assembler to another (I could do it if I have to).
So I'd have to compile the compressed code into a .o object file, then adapt my copressor program so it converts this .o into another compressed .o which still have the references, and finally compile the remaining (normal, not compressed) code into another .o and link both .o together ?
Sounds good enough, but I'd have to find how .o files works internally (as well as adapt my compressor to work with those files). Sounds like a great challenge.
PS :
After some thoughts, working with .o files would not fix the fundamental problem in any way. The problem is cross-referencing, and there is (likely) no true solution for the following reasons :
1) When the main code references anything within the compressed code, it either refers to something different because it's either scrambled (compressed) data in ROM, or a location in RAM after decompression. Fortunately the only thing that really needs to be referenced is the start address of the routines (where it's jsr-ed) so this is solved by having some kind of compression management routine, that will either decompress + call the routine in RAM, or call it if it's already there. If any other kind of reference is needed, then the code practically can't be compressed
2) When the compressed code references anything within the main itself, things are all right as soon as it's a relative reference (such as the branches instructions) but will go horribly wrong if an absolute jmp or jsr is present. This can be solved if (and only if) we know the absolute address where the code is going to be decompressed in RAM, and we can make the jmp and jsr directly point in this RAM area.
3) When the compressed code makes references to the main (not compressed) code or lookup tables, things are going horribly wrong. This is a snake eating it's tail kind of problem, as we need the code to be compressed to be known and fully defined, so the arguments of instructions with absolute referneces (jmp, jsr, lda or whathever) should be known, but at the same time we should compile the main (not compressed) code after the compressed one, which is a contradiction.
So 1) and 2) are manageable although it's some additional work, but 3) is a real problem, and even with all tools in the world I can't think there is a practical solution. If anyone has a bright idea here I'd be infinitely grateful.