fadden / 6502bench

A workbench for developing 6502 code
https://6502bench.com
Apache License 2.0
166 stars 33 forks source link

Data structure #121

Closed BacchusFLT closed 2 years ago

BacchusFLT commented 2 years ago

There is already a variety of options on how to structure data in the Operand menu, but in order to take that to the next level, I would like to suggest an option of adding data in a "struct" like fashion.

One example is commodore 64 sprites; they are three bytes wide (24 bit) and 21 bytes high with a padding byte before the next sprite, which means the sprites appear on 64 byte intervals. Here a struct would be a label on the first line (sprite name), 21 lines that are three bytes wide (the sprite body) and then a padding byte in the end, before the struct repeats. Possibly one would also want the bytes in binary format so you can easily see the sprite.

A Cartridge header is also one such defined struct (two words and then the string CBM80).

Similar structs would apply to screens, bitmaps, fonts and other objects. I'm thinking a setup with a definition file, where one could add more formats, which would build a pull down in the Operand menu. Quite frankly, the strings are already examples of such structs, so it's already there in one form.

fadden commented 2 years ago

I've had the basic notion of support for structs on the list for a while. I think most of the work would be on the UI side, to create a "struct definition editor". Simple structs that are just a list of fixed-width items are easy, but when they have multiple variable-width pieces, some of which are optional, it can get complicated in a hurry. You want to be able to drag & drop items and add attributes, and preview the struct with actual data to see if you're getting it right.

Defining structs in a file would be easier to write since the edit UI could be skipped. Limiting it to C-style structs makes life easier but even then you need attributes for endianness and word alignment vs. packed, and have to decide what to do with nested structs. I don't know if it would be enough to parse the struct definition as flat data; treating it as a program that drives the formatting might be necessary.

Mostly I'm not sure how much is "enough".

Right now the closest thing is to format the data with an extension script. For example, the Apple II ProDOS 8 and GS/OS scripts will format the contents of OS call parameter blocks, which have 8/16/32-bit fields and string pointers. However, scripts need to be triggered by something (like an OS call), which may not make sense for a struct.

As far as the underlying mechanics go, selecting a region of data and giving it some sort of "struct tag" would be pretty straightforward. As you point out, it's not so different from string formatting.

In terms of your specific example of a C64 sprite, you can create a visualization if you want to see how the image looks. If you create the project with one of the C64 system entries, you'll already have RT:Commodore/VisC64.cs loaded. Select the first byte of the sprite and hit Alt-V twice. Pick the format from the pop-up menu at the top and adjust the parameters below. The manual explains a bit more.

It's not quite as nice as a C64-specific tool would be, but it's enough to see which shapes are which, and it's a lot easier on the eyes than ASCII art in the comment field. OTOH, if you generate assembly code rather than HTML, you lose the visualization, so dressing the data up nicely has advantages. The "densely-packed bytes... with a limit of 3 bytes per line" feature is intended for quickly formatting this sort of thing, but it's currently limited to hex output only, which isn't so good for bitmaps.

BacchusFLT commented 2 years ago

I haven't played with the visualisation before now, but it worked very nicely for displaying a sprite. Worked really handy. I would also like to be able to select a block of multiple sprites. It's quite tedious to manually create a block of say 50 sprites one by one. And as per above, visualisation for other objects (fonts - hires and multicolour as well as bitmaps. The latter is a lot more tricky as you need the bitmap, a screen and a colourmap to build a picture and they might not be placed next to each other or in any special order).

But that was forking the discussion into visualisation. Sticking to the struct, having it something like

struct:

, ,< display>, , , Wouldn't that capture most? A definition row gives an output row, and that row would contain multiple items of multiple types "structtypename" would be any name given (unique), and that would also be a type (so that you can nest) "type" would be all the normal types you already have; byte, word or 24 bit, or one of the other structs. display would be if it would show as hex, dec, binary, char, address and address with a code start A sprite would be something like this: struct: spriterow 3, byte, binary end struct: sprite 21, spriterow 1, byte end or struct: sprite 21, struct: 3, byte, binary end 1, byte end If you also want to cover the cases like zero terminated strings (when length is unknown), then that is getting more complex. /Pontus Berg Bergatrollet AB CEO & Owner Tel/SMS: +46 735 082860 www.bergatrollet.se Den ons 24 nov. 2021 kl 07:00 skrev Andy McFadden ***@***.*** >: > I've had the basic notion of support for structs on the list for a while. > I think most of the work would be on the UI side, to create a "struct > definition editor". Simple structs that are just a list of fixed-width > items are easy, but when they have multiple variable-width pieces, some of > which are optional, it can get complicated in a hurry. You want to be able > to drag & drop items and add attributes, and preview the struct with actual > data to see if you're getting it right. > > Defining structs in a file would be easier to write since the edit UI > could be skipped. Limiting it to C-style structs makes life easier but even > then you need attributes for endianness and word alignment vs. packed, and > have to decide what to do with nested structs. I don't know if it would be > enough to parse the struct definition as flat data; treating it as a > program that drives the formatting might be necessary. > > Mostly I'm not sure how much is "enough". > > Right now the closest thing is to format the data with an extension > script. For example, the Apple II ProDOS 8 and GS/OS scripts will format > the contents of OS call parameter blocks, which have 8/16/32-bit fields and > string pointers. However, scripts need to be triggered by something (like > an OS call), which may not make sense for a struct. > > As far as the underlying mechanics go, selecting a region of data and > giving it some sort of "struct tag" would be pretty straightforward. As you > point out, it's not so different from string formatting. > > In terms of your specific example of a C64 sprite, you can create a > visualization if you want to see how the image looks. If you create the > project with one of the C64 system entries, you'll already have > RT:Commodore/VisC64.cs loaded. Select the first byte of the sprite and hit > Alt-V twice. Pick the format from the pop-up menu at the top and adjust the > parameters below. The manual > explains a > bit more. > > It's not quite as nice as a C64-specific tool would be, but it's enough to > see which shapes are which, and it's a lot easier on the eyes than ASCII > art in the comment field. OTOH, if you generate assembly code rather than > HTML, you lose the visualization, so dressing the data up nicely has > advantages. The "densely-packed bytes... with a limit of 3 bytes per line" > feature is intended for quickly formatting this sort of thing, but it's > currently limited to hex output only, which isn't so good for bitmaps. > > — > You are receiving this because you authored the thread. > Reply to this email directly, view it on GitHub > , > or unsubscribe > > . > Triage notifications on the go with GitHub Mobile for iOS > > or Android > . > >
fadden commented 2 years ago

Visualizers have access to the contents of the entire file. The place where you create them just determines the default value for the offset parameter, which is itself entirely optional. I've rendered full-screen bitmaps that were split into separate parts of the file, bitmaps where every few lines have some weird alignment restriction, etc. You can create a single visualization that includes multiple images, such as a font or sprite sheet. The SMB CHR ROM image, which has 512 individual graphics laid out in a grid with labeled axes, was drawn by a visualizer. (The 8x8 font is built in to SourceGen.) There is no multi-create feature, but the visualization editor remembers the parameter values you used last time, so if you have a bunch of identically-formatted sprites you can click the start and just Alt+V Alt+V Return Return for each one.

The trick with visualizers is that you have to write C# code. This gives them incredible flexibility but raises a barrier in terms of who can create them. A simpler UI or file format could have been used, but that would have made it harder to generate totally arbitrary images, like a Battlezone map drawn from the obstacle type & position table.

A good interface makes the simple things easy and the complicated things possible, and visualizers are not particularly easy. In that respect, a discussion about visualizers (and extension scripts in general) is very relevant, because they provide an example of a solution that addressed a problem in a very general way, but isn't very useful because in practice hardly anyone can actually make use of it. (Mostly unrelated: remember the script I sent for formatting inline strings in that cartridge project? That's now a standard component in the runtime, so that common situations can be handled without needing to write code.)

I don't think we need a struct layout and formatting mechanism with the same degree of flexibility as an extension script; it's overkill and nobody would use it. We do need a way to describe something fairly complex, with variable-length parts, and we have to be able to define appearance as well as structure. The trick is finding the sweet spot where we can solve "enough" of the problem.

fadden commented 2 years ago

Moved to TO DO list.