michaelcmartin/Ophis — pull request #3 by catseye (2012-05-08 22:24:33+00:00)
Title: Have .incbin take an optional offset and length for source bytes.

This change extends the syntax of `.incbin` to be:

.incbin "FILENAME"[, OFFSET[, LENGTH]]

where OFFSET and LENGTH are literal numbers. The bytes to be included are read from the file starting at the OFFSET, and at most LENGTH bytes are included.

The main practical use of this would be for including binary data from some file format which includes headers, when you don't want to include the headers in your binary. As a specific example, Commodre 64 sprites saved as `.PRG` files include a two-byte load address before the actual data. To include only the sprite data, you could say `.incbin "sprites.prg", 2`. (The use case for the length is admittedly a little less well-motivated, but consistency seems to call for it. It could be useful to trim out junk from files padded to certain multiples of a chunk size, perhaps.)

I'm certainly not married to this specific syntax, and would be open any suggestions for a better one.

If you think this is something you'd like to see in Ophis, I can think of a few ways to improve it: I could figure out how to use `parse_expr` and use it to allow arbitrary Ophis expressions for OFFSET and LENGTH; and I could add a test case under `tests/` for `.include` and the various forms of `.incbin`.