aboutsummaryrefslogtreecommitdiff
Commit message (Collapse)AuthorAgeFilesLines
...
* Use a buffered reader for incbinMiquel Sabaté Solà2025-08-171-2/+3
| | | | | | | | | Iterating via `bytes()` on a file is inefficient as the default implementation calls `read` on each byte, which can be costly on bytes which are not in memory like files. This is extra important for statements like `incbin` as included files can be rather big. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Apply suggested style fixes from clippyMiquel Sabaté Solà2025-08-179-84/+64
| | | | Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* parser: Fix disambiguation paren on first argumentMiquel Sabaté Solà2025-08-171-3/+99
| | | | | | | | | | | | | | | | | | | Arguments can be put inside of enclosing parenthesis, but the parser was assuming that if an opening paren was found when parsing the first argument on an argument list, then that was all it was needed to be parsed. This though conflicts with situations like: .byte ($01 << 2) | ($01 << 1) In this case, the parser would have ignored everything past the first closing paren. This commit provides a fix in which if an operation is found past the first enclosing parenthesis, then this assumption is discarded in favor of a parenthesis being used for disambiguating on an arithmetical/logical expression. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* xa65: Fix minor style issuesMiquel Sabaté Solà2025-01-231-3/+3
| | | | Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Add a configuration for MMC1Miquel Sabaté Solà2025-01-233-17/+17
| | | | Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* xa65: Create hex dumps for both binariesMiquel Sabaté Solà2025-01-231-0/+49
| | | | | | | This allows to compare both binaries more easily with tools such as vimdiff and the likes. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Add support for a single equal sign operatorMiquel Sabaté Solà2025-01-231-1/+4
| | | | Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Drop lazy_static dependencyMiquel Sabaté Solà2025-01-225-498/+1703
| | | | | | | Apparently the standard library had LazyLock which is enough for what I wanted to achieve with lazy_static. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Implement echo control statementsMiquel Sabaté Solà2025-01-223-2/+48
| | | | Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* nasm: Implement the -D flagMiquel Sabaté Solà2025-01-228-5/+134
| | | | | | | This allows users to define variables directly from the command line, which is useful for testing purposes. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* github: Add dependabot configurationMiquel Sabaté Solà2025-01-221-0/+6
| | | | Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Remove dependency on TOMLMiquel Sabaté Solà2025-01-2213-550/+121
| | | | | | | | | | | | | | | | | | | When I introduced this dependency it looked like a good idea to have a better-looking replacement to cl65's cfg format. That being said, the end result wasn't *much* prettier either, and the end result could be even bigger and equally confusing. Since 5f48de69f46d ("Add support for cfg files") there is quite the framework in order to support regular cl65's cfg files. Hence, this commit takes another approach: let's tune this format to a more compressed and simplified one. This is now the current "nasm cfg" format, and it allowed us to re-use a lot of code while also being more to the point for NES/Famicom development than the original cfg format. With this new format, we can now remove the dependency on TOML and all of the inner dependencies which were quite a lot. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Remove the dependency on clapMiquel Sabaté Solà2025-01-227-301/+260
| | | | | | | | | | | | | | | All three binaries (i.e. readrom, nasm and xa65) used it, and to be honest rolling our own argument parsing was actually pretty easy to achieve. This frees us from a big dependency and it also frees us from inner dependencies such as 'clap_derive' which prevented us from being able to build binaries purely statically. As a side effect, we can now display help/version messages which are more like I'm used to, and certain checks can be moved in the parsing directly instead of having to be done later. As a cherry on top, there is some type masturbation that gets removed along the way. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Remove dependency to the rand crateMiquel Sabaté Solà2025-01-226-195/+20
| | | | | | | | This dependency was easily avoidable and it brought with it a lot of inner dependencies of its own, most notably 'zerocopy-derive', which forbid us to compile the affected programs purely statically. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* tests: Add aoc2023.nes to end-to-end testsMiquel Sabaté Solà2025-01-222-0/+12
| | | | Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Be more explicit on empty identifiersMiquel Sabaté Solà2025-01-211-6/+3
| | | | | | | If the programmer only wrote a single special character for an identifier, then we will consider it empty to avoid shenanigans. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Allow dot characters on identifiersMiquel Sabaté Solà2025-01-211-19/+67
| | | | | | | | In fact, they were always allowed, but they were in kind of grey area, as they could be defined but not used, and sometimes they could be used in the middle of identifiers, which was unexpected. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Introduce looking ahead when parsing literalsMiquel Sabaté Solà2025-01-212-193/+296
| | | | | | | | | | | | | | | | | | | | | | There were certain situations in which syntax ambiguity could arise. For example, the parser as it stood could treat a valid expression such as 'lda #$80 >> 2' in an unexpected 'lda #$(80 >> 2)'. This is of course bad, and it came from the fact that literals don't have enclosing characters, and white spaces are not enough to provide disambiguation in some cases. Because of this fact, the parser now has a "look ahead" capability similar to many other parsers, and it's applied for now only to literals. This looking ahead actually honors parenthesis, so these can be added if the programmer wants to explicitely disambiguate an expression. This involved quite the heavy lifting, and some functions like 'parse_expression_with_identifier' had to be removed with the rewrite. This had the side effect of having (hopefully) more sane functions all around, and the parser also has a better capability to differentiate between regular Values and Calls. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Track the level of recursive callsMiquel Sabaté Solà2025-01-201-18/+35
| | | | | | | | | This is in preparation to some heavy lifting that is to be done to the parser so it more properly handles nested expressions, but in general it's a good idea to have some limits to functions that expect to be called recursively quite heavily. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Whitelist valid identifier charactersMiquel Sabaté Solà2025-01-162-8/+27
| | | | | | | | Instead of making up a list of characters that end an identifier, do the other way around since it's far less cumbersome and it prevents from silly bugs such as "var+1" being considered a single identifier. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Permit 16-bit decimal valuesMiquel Sabaté Solà2025-01-161-9/+18
| | | | | | | | | | As a remnant of old code, the 'parse_decimal' function was not allowing for decimal values larger than 8-bits. This was not the case in other areas such as 'parse_hexadecimal', and in the rest of the code we already cover that immediates are not too big in instructions. Hence, this restriction can be lift up and allow up to 16-bit decimal literals. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Implement .ifdef/.ifndef statementsMiquel Sabaté Solà2025-01-164-4/+78
| | | | | | | They are just synonyms for ".if .defined" and ".if !.defined" respectively. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Implement .def/.defined expressionsMiquel Sabaté Solà2025-01-163-0/+57
| | | | | | | This can be combined with .if/.elsif statements just like any other expression. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Implement .if/.elsif/.else statementsMiquel Sabaté Solà2025-01-164-43/+258
| | | | Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Add boolean operatorsMiquel Sabaté Solà2025-01-163-11/+100
| | | | | | | | | | | Allow for expressions that evaluate to a boolean expression. This in turn mean that the value is just set to 0 or 1 depending on the given condition. As with other assemblers, only a value of 0 evaluates to 0, and others go to 1. So, something like "1 && 2" evaluates to 1 even if it doesn't make much sense at first glance (as an assembler we just assume that the programmer knows what it's doing). Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Fix error with strings containing equal operatorMiquel Sabaté Solà2025-01-151-10/+7
| | | | | | | | | | | As with e27a1593c8d8 ("Allow semicolons inside of strings"), the parser was too naive and regarded any '=' operator as part of an assignment, despite that it could be art of string literal. Luckily the fix was already done inside of the parsing of assignments, we just needed to move it up. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Allow semicolons inside of stringsMiquel Sabaté Solà2025-01-151-9/+43
| | | | | | | | | | | | The parser was too naive and assumed that a semicolon immediately implied an inline comment, and that might not just be the case as it is with string literals. Hence, the end of each semantic line has to consider not only whether there is an inline comment, but also if a string literal is being used and whether it surrounds or not the given semicolon. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Simplify string arguments for control statementsMiquel Sabaté Solà2025-01-152-28/+51
| | | | | | | | | | | Some control statements (e.g. '.incbin', '.asciiz') only require a double-quoted string as an argument. In fact, for these functions there's only one argument required, which is this string one. Given this fact, the parsing on these functions don't have to go through the (expensive) general argument parsing function, and they can simply assume that a double-quoted string will be provided. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Disambiguate unary/binary operatorsMiquel Sabaté Solà2025-01-151-1/+8
| | | | | | | Some operators like '<' and '<<' could be mistakingly be treated as the other. Let's remove this ambiguity when checking for unary operators. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Move check for ASCII-only strings into the parserMiquel Sabaté Solà2025-01-152-9/+6
| | | | Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Split 'parse_expression' into more functionsMiquel Sabaté Solà2025-01-151-70/+86
| | | | | | | | The other cases in which fetching an "identifier" is not needed were starting to pile up. Hence, split them into separate functions and allow 'parse_expression' to be more simple. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Parse a string literal as a single objectMiquel Sabaté Solà2025-01-151-0/+47
| | | | | | | | | Before this it was left to the `parse_identifier` to figure things out, but this was prone to silly errors like "a: b", in which it would mistake it as the start of a label. Instead of any of this, just consume a string literal if it has been detected. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Add .byt as a synonym for .byteMiquel Sabaté Solà2025-01-141-0/+1
| | | | | | Not that I like it, but there are existing code which already uses this. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Implement .asciiz control statementMiquel Sabaté Solà2025-01-143-10/+72
| | | | Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* readrom: Print addresses for vectorsMiquel Sabaté Solà2025-01-141-1/+39
| | | | Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Implement the .res control statementMiquel Sabaté Solà2025-01-143-0/+100
| | | | | | | | | | | | | Similarly to other assemblers, this allows the programmer to write a definite amount of bytes with the same values. Compared to other assemblers there are two things to notice. First, there is a limit to it (i.e. whatever can fit in 2 bytes). Second, if the fill value is not provided, then it will default to the current mapping's fill value, or just 0x00 if the current mapping doesn't define one of its own. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* tests: Update code.nes so -Werror can be passedMiquel Sabaté Solà2025-01-133-29/+8
| | | | | | | | | Update the underlying code.nes test data so it uses a tighter configuration for NROM which in turn frees nasm from complaining about empty segments. This makes room to allo for -Werror so we catch warnings that might appear in the future. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Add support for cfg filesMiquel Sabaté Solà2025-01-133-3/+559
| | | | | | | | In order to make tools like `xa65` work seamlessly with existing configurations, allow this format too instead of making developers switch to a new file with TOML syntax. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Add TODO file into .gitignoreMiquel Sabaté Solà2025-01-131-0/+1
| | | | Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* xa65: The config flag should be uppercase on shortMiquel Sabaté Solà2025-01-101-2/+2
| | | | | | | For compatibility with cl65, it's preferrable to keep the short version of the flag as with cl65. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* xa65: Provide the target flagMiquel Sabaté Solà2025-01-101-0/+15
| | | | | | This is meant for compatibility with ca65. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* readrom: Remove the derive feature from clapMiquel Sabaté Solà2025-01-102-14/+18
| | | | | | | This allows us to statically build the 'readrom' binary, as that feature from clap prevented that option. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Remove anyhow from xa65 and readromMiquel Sabaté Solà2025-01-106-47/+71
| | | | | | | | Following in the footsteps of c20c991dded8 ("Make errors from nasm itself more cohesive"), 'anyhow' might provide an easy framework, but it lends towards a less cohesive experience. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Make errors from nasm itself more cohesiveMiquel Sabaté Solà2025-01-101-40/+44
| | | | | | | | | | | | | The errors reported from the library had a slightly different format than those that happened in the main file for nasm. This is because in these cases we were just returning an Err type and Rust handles that through a special impl. Instead of going the route of type masturbation, let's go the C route and have a 'die' function that simply prints the message and calls exit(1), Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Introduce xa65Miquel Sabaté Solà2025-01-104-0/+152
| | | | | | | | This is a bridge between 'nasm' and 'cl65'. That is, it runs the same command on both 'nasm' and 'cl65', compares the resulting binaries, and gives back the binary from 'cl65'. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Improve the message on bad file includesMiquel Sabaté Solà2025-01-103-17/+9
| | | | | | Also remove some pending TODOs. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* tests: Bundle end-to-end tests into a tarballMiquel Sabaté Solà2025-01-1012-1367/+19
| | | | | | | | This way I can simply compress all the examples to traverse with a single file, and thus no longer polluting this repository from code from other repositories. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Properly fill the context stack when forcing itMiquel Sabaté Solà2025-01-102-16/+67
| | | | | | | | | | | | | When calling `force_context_switch` the stack was mindlessly pushing the given name without taking into consideration how scopes are to be laid out. This made some variables/addresses that were previously preserved no longer reachable when crunching pending nodes. This patch also makes `force_context_switch` reset the stack before doing anything at all, which means that `force_context_pop` was no longer relevant. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Improve error message on unknown variable/scopeMiquel Sabaté Solà2025-01-092-11/+9
| | | | Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Prevent assignments on relative referencesMiquel Sabaté Solà2025-01-091-1/+17
| | | | | | | | | | | | It is a really weird thing to do, but on a twisted way I can see how someone could think of such a monstrosity. Panicking on such a case is not valid because that's not the fault from the assembler but from the programmer, and so a proper message should be displayed instead. This was detected via the fuzzer, but it was a side effect from e7c5e63d04f6 ("Evaluate bare numbers as decimal values"). Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>