aboutsummaryrefslogtreecommitdiff
Commit message (Collapse)AuthorAgeFilesLines
* readrom: only read the ROM file onceMiquel Sabaté Solà2026-07-161-15/+14
| | | | | | | | | | | | | | | | | | | Since the implementation grew from disassembling a single subroutine, reading the ROM file from inside print_range() made sense. But since we have disassembling of full segments and files now, this reading would be triggered multiple times. Commit d7b1a895de9e ("readrom: add an option to disassemble the full file") avoided the exhaustion of the input by using seek(), but that's just a hack and it's hiding the fact that we are constantly reading the same thing over and over. Constantly reading ROM files isn't that much of a performance issue given how small they are, but it's embarrasing anyways. Fix this by reading the full file once and passing the slice of bytes to the same functions that used to require the file to be passed. Signed-off-by: Miquel Sabaté Solà <mssola@mssola.com>
* readrom: add an option to disassemble the full fileMiquel Sabaté Solà2026-07-161-2/+39
| | | | Signed-off-by: Miquel Sabaté Solà <mssola@mssola.com>
* readrom: allow to disassemble a mappingMiquel Sabaté Solà2026-07-161-22/+117
| | | | Signed-off-by: Miquel Sabaté Solà <mssola@mssola.com>
* readrom: fix versioning on the Cargo manifestMiquel Sabaté Solà2026-07-152-2/+2
| | | | Signed-off-by: Miquel Sabaté Solà <mssola@mssola.com>
* readrom: implement the -d/--disassemble optionMiquel Sabaté Solà2026-07-1511-13/+2752
| | | | | | | | This option can also be coupled with -n/--nasm-directory, and you can then get a human-readable disassembling of any proc or label you might be thinking on. Signed-off-by: Miquel Sabaté Solà <mssola@mssola.com>
* Save known addresses into .nasm/addresses.txtMiquel Sabaté Solà2026-07-153-5/+116
| | | | | | | | | This new file contains all the addresses that are known to the assembler, belonging to either proc's or plain labels. For the former we will further be able to tell the "end" of the proc. With that, the file follows a simple CSV format, with the name, start and end. Signed-off-by: Miquel Sabaté Solà <mssola@mssola.com>
* nasm: code style cleanupMiquel Sabaté Solà2026-07-141-8/+13
| | | | | | | | In the same spirit as commit a012d260db4a ("Minor style fixes from an upgraded clippy"), an update on the toolchain has raised some concerns style-wise. Signed-off-by: Miquel Sabaté Solà <mssola@mssola.com>
* Generalize MemoryRange into RangeMiquel Sabaté Solà2026-07-142-67/+71
| | | | | | | Leading up to also having address ranges exported to callers, which should be using the same infrastructure. Signed-off-by: Miquel Sabaté Solà <mssola@mssola.com>
* Use 'full_name' everywhere in check()Miquel Sabaté Solà2026-07-141-5/+1
| | | | Signed-off-by: Miquel Sabaté Solà <mssola@mssola.com>
* readrom: print the usual PRG-RAM size if availableMiquel Sabaté Solà2026-07-132-1/+4
| | | | | | | | | | If 'has_persistent_memory' has been set on the ROM header, then we must print it even if bytes 8/10 was never set by the header. In fact, the vast majority of ROM files don't have these bytes set but they report a true value for 'has_persistent_memory'. Hence, assume the usual PRG-RAM size in these cases, which is (I hope) the correct one in all cases. Signed-off-by: Miquel Sabaté Solà <mssola@mssola.com>
* Prevent out of bounds panics on range_to_human()Miquel Sabaté Solà2026-07-091-1/+1
| | | | | | | | | | | | | | | | On range_to_human(), if the range is completely broken for some reason, we might get a start = 0 and end = 0. In this case, then we would get into an 'end - 1' computation for an 'usize', resulting in bad arithmetics. This can be avoided altogether if the 'start + 1' computation from the start of the function not only results to be equal, but also larger than 'end'. This, of course, doesn't make much sense, and it's most probably a bandaid; but it has been detected via fuzzy testing with wild inputs. Hence, it's fine if this function doesn't return a coherent string representation for deranged inputs. Signed-off-by: Miquel Sabaté Solà <mssola@mssola.com>
* scripts: separate fuzzy testing into its own fileMiquel Sabaté Solà2026-07-092-9/+13
| | | | | | | This way we can simply run fuzzy testing multiple times from the shell without having to run the full tests too. Signed-off-by: Miquel Sabaté Solà <mssola@mssola.com>
* Minor style fixes from an upgraded clippyMiquel Sabaté Solà2026-07-093-3/+3
| | | | Signed-off-by: Miquel Sabaté Solà <mssola@mssola.com>
* Add support for the asan:fixed-segments commentMiquel Sabaté Solà2026-07-098-24/+370
| | | | | | | | This magic comment allows for the definition of segments that are fixed and for which references are always safe. This goes in tandem with the asan:safe comment, which can then be used more sporadically. Signed-off-by: Miquel Sabaté Solà <mssola@mssola.com>
* Add a warning for unknown cross-mapping referencesMiquel Sabaté Solà2026-07-089-2/+286
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Some segments, like the 'vectors' one, will reference code that is outside of its mapping. But in some other configurations, segments cannot make these cross-mapping references so happily. Imagine: .segment "SWAPPABLE" .proc foo rts .endproc .segment "FIXED" jsr foo Here the assembler will properly detect the address of 'foo' in the context of the 'SWAPPABLE' segment. But what this assembler doesn't know is that this segment is swappable (e.g. UNROM chip). Hence, if the bank being mapped right now is not the one containing the 'SWAPPABLE' segment, then the address computed for 'foo' and used in that 'jsr' instruction will point to something else entirely. This would be similar to a use-after-free bug. This is something that can only be inspected at runtime, and so the assembler cannot be of much help here. Hence, this commit adds a warning so the programmer can understand the potentially dangerous operation. All of that being said, this commit also adds support for "asan:safe" or "check:safe", which is a magic comment that the programmer can write to re-assure the assembler that this operation is fine (e.g. there is a guarantee that the mapped bank is that one we are expecting). Hence, the code above could now be written like so: jsr foo ; check:safe Signed-off-by: Miquel Sabaté Solà <mssola@mssola.com>
* Add the SectionType::Vector enum valueMiquel Sabaté Solà2026-07-082-3/+14
| | | | | | | It's actually valuable to distinguish that part of PRG-ROM that is part of the vectors segment. Signed-off-by: Miquel Sabaté Solà <mssola@mssola.com>
* lib: add descriptions in Cargo.toml filesMiquel Sabaté Solà2026-07-082-1/+3
| | | | Signed-off-by: Miquel Sabaté Solà <mssola@mssola.com>
* Upgrade to the 2024 edition of RustMiquel Sabaté Solà2026-07-0813-289/+384
| | | | | | | And also adjust the code so the clippy from the 2024 edition is fine with it. Signed-off-by: Miquel Sabaté Solà <mssola@mssola.com>
* Let arguments be able to reference their macroMiquel Sabaté Solà2026-07-076-37/+69
| | | | | | | | | | | | | | The enum type for macro arguments has been adjusted so it accepts a String. This is then used to store the name of the macro for this argument. This is a bit of a circlejerk, but in the end it stems from the fact that macro arguments were sort of a hack defined ad-hoc each time they were needed. That being said, if we want to report macro arguments being unused, we need to reference the actual macro definition, not the macro call. Signed-off-by: Miquel Sabaté Solà <mssola@mssola.com>
* Be more specific on unused objectsMiquel Sabaté Solà2026-07-072-3/+8
| | | | | | | | | | It was downright lazy from my end to not inspect the line of the object that was being unused. Fix this by trying to fetch the bundle's node. This right now applies to variable definitions. Objects like macro arguments are pending to be done. Signed-off-by: Miquel Sabaté Solà <mssola@mssola.com>
* Preserve the 'accessed' member on macro argumentsMiquel Sabaté Solà2026-07-075-1/+46
| | | | | | | | | | | | | | | Macro arguments get tampered with every time a new call has to be bundled. Deep down this is done by cloning the underlying object, but this resets any increase on the 'accessed' member, giving always the impression that macro arguments are never accessed. Fix this by at least preserving this member when cloning a macro argument. Note that this is neither the most elegant solution, and probably not the most correct. As in, it will probably get into a non-precise count after some calls. But at least it will be a non-zero value, which is more than enough to what this is actually used for. Signed-off-by: Miquel Sabaté Solà <mssola@mssola.com>
* Do not optimize away unresolved non-jumpsMiquel Sabaté Solà2026-07-071-1/+31
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | In the following code: .macro MACRO ADDR adc ADDR .endmacro MACRO label label: nop The evaluation of the 'adc' instruction went into being re-sized to 1 despite indications of being an address (and hence hinting to a size of 2). This was done because of the optimization to shrink a single byte left arm from absolute to relative addressing. But on unresolved bundles this is bad, as everything may still be zeroed out, and the instruction itself doesn't hint on the end size. This shrinking would then mess with the segment's offset, and we would end up with an offset of 2 instead of 3 for the label 'label'. Note that this is in the same spirit as commit ed3d34b0afb1 ("Only shrink the addressing on resolved bundles"), but now applied to the get_from_left() function which apparently was spared for whatever reason. Signed-off-by: Miquel Sabaté Solà <mssola@mssola.com>
* Refer to the original file for unused objectsMiquel Sabaté Solà2026-06-056-22/+49
| | | | | | | | | | | | | | | | | | | When warning users on unused objects (e.g. variables, proc's), we have to pick up the source of origin for this warning. In places where the original PNode is not available, then we go with the last source we have at hand, as that's the usual way to go (i.e. the error occurred at the current source/context). That being said, for unused objects that's not desirable, because we might otherwise claim the error to happen on the file we first targetted, but it's way more useful to understand where the object was defined. Hence, when defining a variable, address or proc, let's actually store the PNode associated with it as well. This way the checker can hopefully get the source from this PNode and be more clear to the user. Signed-off-by: Miquel Sabaté Solà <mssola@mssola.com>
* tests: check on the existence of jetpac.nesMiquel Sabaté Solà2026-04-301-1/+6
| | | | Signed-off-by: Miquel Sabaté Solà <mssola@mssola.com>
* xa65: sort the flags on the help messageMiquel Sabaté Solà2026-04-301-1/+1
| | | | Signed-off-by: Miquel Sabaté Solà <mssola@mssola.com>
* Fix tabulation on --allow-unused help's messageMiquel Sabaté Solà2026-04-301-1/+1
| | | | Signed-off-by: Miquel Sabaté Solà <mssola@mssola.com>
* ci: override LC_ALL for a more consistent 'sort'Miquel Sabaté Solà2026-04-302-2/+2
| | | | Signed-off-by: Miquel Sabaté Solà <mssola@mssola.com>
* tests: fix the assembler fuzzerMiquel Sabaté Solà2026-04-301-2/+16
| | | | | | We needed to pass the new 'allow_unused' parameter. Signed-off-by: Miquel Sabaté Solà <mssola@mssola.com>
* Rename "raw address" to "label"Miquel Sabaté Solà2026-04-302-3/+3
| | | | | | This is much clearer to the programmer. Signed-off-by: Miquel Sabaté Solà <mssola@mssola.com>
* nasm: add documentation to the 'unused' checksMiquel Sabaté Solà2026-04-301-0/+23
| | | | Signed-off-by: Miquel Sabaté Solà <mssola@mssola.com>
* tests: adapt to the new 'unused' checksMiquel Sabaté Solà2026-04-304-19/+35
| | | | Signed-off-by: Miquel Sabaté Solà <mssola@mssola.com>
* Issue a warning on unused macrosMiquel Sabaté Solà2026-04-301-0/+33
| | | | Signed-off-by: Miquel Sabaté Solà <mssola@mssola.com>
* Don't do the 'accessed' check if "check:ignore" was givenMiquel Sabaté Solà2026-04-301-2/+5
| | | | Signed-off-by: Miquel Sabaté Solà <mssola@mssola.com>
* Allow a magic "check:ignore" commentMiquel Sabaté Solà2026-04-301-1/+1
| | | | | | | This is basically the same as "asan:ignore", but to the programmer it will sound less weird on non-ASAN uses. Signed-off-by: Miquel Sabaté Solà <mssola@mssola.com>
* nasm/xa65: add the --allow-unused optionMiquel Sabaté Solà2026-04-303-2/+31
| | | | | | | | | Since the addition of the check() function in commit 6a9dea943c07 ("Add a new 'check' stage in assemble()"), it can be annoying to some users that the check for unused/unreferenced object applies by default. Hence, add a flag so this can be disabled. Signed-off-by: Miquel Sabaté Solà <mssola@mssola.com>
* Add a new 'check' stage in assemble()Miquel Sabaté Solà2026-04-302-108/+108
| | | | | | | | This is actually an expansion from asan(), but it also checks for stuff that goes outside of the realm of address sanitation. In this aspect, asan() has been integrated inside of the new check() function. Signed-off-by: Miquel Sabaté Solà <mssola@mssola.com>
* Send an error for unused .proc'sMiquel Sabaté Solà2026-04-285-42/+111
| | | | | | | | | | | | | | | | | We cannot safely detect all scenarios in which there is dead code, but we can safely do it for .proc's. Even if they are not called directly, they might be referenced via jump tables and shenanigans like that. Long story short, if you are not referencing a .proc in any meaningful way, then we have dead code. A pattern could also be given in which a function that was too long has been splitted into smaller functions which are not called directly. This is, in my opinion, an anti-pattern (as we generally expect an rts or a jmp from .proc's), and anyways can be avoided via the use of __fallthrough__ if the programmer is really set to write this kind of code. Signed-off-by: Miquel Sabaté Solà <mssola@mssola.com>
* nasm: add documentation on the exit status codeMiquel Sabaté Solà2026-04-281-0/+9
| | | | | | | I apparently never bothered to do so. It's a good idea to have this as it might not be all that expected. Signed-off-by: Miquel Sabaté Solà <mssola@mssola.com>
* nasm: call exit() right after error detectionMiquel Sabaté Solà2026-04-281-90/+88
| | | | | | | | The 'if error_count == 0' branch came from old code that accumulated over and over. Since this was the only branch taken before exit, let's prefer the other way around so we can remove one indentation level. Signed-off-by: Miquel Sabaté Solà <mssola@mssola.com>
* nasm: use buffered writing for full ROMs as wellMiquel Sabaté Solà2026-04-281-4/+8
| | | | | | | | This was already implemented for the --split-segments option. Not let's bring this when building full ROM files, as writing things byte by byte is slow. Signed-off-by: Miquel Sabaté Solà <mssola@mssola.com>
* nasm: avoid an unnecessary loop when writing bytesMiquel Sabaté Solà2026-04-281-5/+3
| | | | Signed-off-by: Miquel Sabaté Solà <mssola@mssola.com>
* nasm: add the --split-segments optionMiquel Sabaté Solà2026-04-282-33/+81
| | | | | | | | | | | | This allows the programmer to tell nasm to leave each segment into its own .out file. This can be useful when writing some specific segments into different chips. The caveat is that filling is not applied, and hence it's up to the programmer to account for that if two or more segments happened to be on the same mapping and needed some alignment in between. Signed-off-by: Miquel Sabaté Solà <mssola@mssola.com>
* nasm: add a description on the README.md fileMiquel Sabaté Solà2026-04-251-0/+14
| | | | Signed-off-by: Miquel Sabaté Solà <mssola@mssola.com>
* tests: update submodules to their latest commitsMiquel Sabaté Solà2026-04-253-0/+0
| | | | Signed-off-by: Miquel Sabaté Solà <mssola@mssola.com>
* Add the context's name into memory rangesMiquel Sabaté Solà2026-04-252-4/+8
| | | | | | | | This is applied whenever a given memory range name is not under the global context. The end result is that scopes will be shown into the memory.txt file when using '--write-info'. Signed-off-by: Miquel Sabaté Solà <mssola@mssola.com>
* Add the .min and the .max control expressionsMiquel Sabaté Solà2026-04-243-0/+91
| | | | Signed-off-by: Miquel Sabaté Solà <mssola@mssola.com>
* Allow scoped names in __fallthrough__Miquel Sabaté Solà2026-04-243-1/+11
| | | | | | Fixes 4f1a9c660108 ("Add the .fallthrough control statement") Signed-off-by: Miquel Sabaté Solà <mssola@mssola.com>
* Add a big endian control statementMiquel Sabaté Solà2026-04-243-5/+73
| | | | | | | | | | | By default everything is little endian, and that should always be the case. That being said, if a literal is better expressed in big endian format for whatever reason, you can now use the .be or the .bigendian control statements. The terribly named .dbyt control statement can also be used for this purpose, but that's just to be compatible with the implementation from ca65. Signed-off-by: Miquel Sabaté Solà <mssola@mssola.com>
* Add the .version pseudo-variableMiquel Sabaté Solà2026-04-243-0/+45
| | | | Signed-off-by: Miquel Sabaté Solà <mssola@mssola.com>
* ci: upgrade to actions/checkout@v6Miquel Sabaté Solà2026-04-241-5/+5
| | | | | | | This fixes a deprecation warning on an outdated Node.js version being used. Signed-off-by: Miquel Sabaté Solà <mssola@mssola.com>