Just a quick Pre-processor Question =)

ZX80, ZX 81, ZX Spectrum, TS2068 and other clones
Post Reply
Zetr0
Member
Posts: 33
Joined: Mon Aug 22, 2011 12:38 pm

Just a quick Pre-processor Question =)

Post by Zetr0 »

Hell there my fellow Z88DK'ers

I have been using the "most excellent" ZX Paintbrush for much mischief lately and have been using this and ASM to get to grips with the spectrum display file and assembly in general.

Now, I would like to do some more useful things with these using C/Z88DK - so is there a way I can initialise say 192 Bytes (unsigned char) using the binary output of ZX Paintprush;?


ZX Paintbrush "copy to clipboard" output is stored as "0" and "1" for each pixel (bit) within a byte, 8 of these make a Cell.

Now with PASMO it was just a "0x" pre-fix before the field of 8 "0" / "1"s and the pre-processor would compile this to a byte value - would Z88dk have such a pre-processor suffix to process binary values into byte values to be stored in an array?

i.e.

Code: Select all

unsigned char byte = 0b10101010;
Thanks for reading
alvin
Well known member
Posts: 1872
Joined: Mon Jul 16, 2007 7:39 pm

Post by alvin »

Pasmo uses "0x" for binary? That prefix is a pretty solid standard for hex.

The c pre-processor does not do binary constants (see https://stackoverflow.com/questions/261 ... -in-c-or-c for some really complicated solutions to this). However, m4 runs before cpp in z88dk and that can do binary and could change the binary constant to hex before cpp sees it.

But you can do the binary in asm just like you are doing for pasmo. Or you could just import an entire bin file as paintbrush outputs if you want to get away from copy and paste.

To do the byte definition in asm, you'd inform the c compiler of the block of data's presence with something like this:

Code: Select all

extern unsigned char bytes[];   // assuming there's more than one
And then in an asm file:

Code: Select all

SECTION data_user

PUBLIC _bytes

_bytes:

   defb %10000000, %00001111
In c, "bytes[0]" would be the first byte and "bytes[1]" would be the second.

To compile, just add that asm file to the compile line.


If you want to use the entire binary file that zx paintbrush produces, the asm file would just import the binary:

Code: Select all

SECTION data_user

PUBLIC _bytes, _bytes_end

_bytes:

   BINARY "paintbrush.bin"

_bytes_end:
"bytes[0]" would still return the first byte, etc. I added a second label "_bytes_end" so that you can find out how long the binary data is. In c you could "extern unsigned char bytes_end[];" and then "bytes_end - bytes" would be the length of that binary data.

I think z80asm will also accept a leading ""0b" for binary instead of "%" and possibly a trailing "b" but I'm not sure off the top of my head.


The other option I mentioned is using m4 to preprocess the file before cpp. You can do this by adding ".m4" to the end of your source file. "test.c" -> "test.c.m4" or "test.asm" -> "test.asm.m4". Then you give the m4 file to zcc and it will pass it though m4 to generate "test.c" or "test.asm" which is then processed like normal. These files "test.c" or "test.asm" are written to the same directory as "test.c.m4" or "test.asm.m4" so will overwrite any file of that name there. It has to be done this way to make sure include paths are properly resolved and it is really what you want done anyway. One of the options above is probably better then getting m4 to do something.
User avatar
dom
Well known member
Posts: 1302
Joined: Sun Jul 15, 2007 10:01 pm

Post by dom »

It looks like implementations are free to add support for a 0b prefix "An implementation may accept other forms of constant expressions.
" - gcc supports it for example.

It's a sensible extension, so I was going to add it to sccz80 with a caveat that it's not supported by sdcc. Except I just checked, and it is.

The build tomorrow will have support for it!
Zetr0
Member
Posts: 33
Joined: Mon Aug 22, 2011 12:38 pm

Post by Zetr0 »

@dom

You sir, have just made my Christmas card list!
Post Reply