Commit Graph

57 Commits

Author SHA1 Message Date
Haven1433
8541249524 writing non-aligned thumb code with ldr commands should work 2022-03-20 20:57:25 -05:00
Haven1433
c1d56fdfa4 Allow register/number <<= operation in thumb
Create a command that is an alias, so the user only has to write one register if they're doing a <<=
2021-12-10 11:09:46 -06:00
Haven1433
c3ad9d3927 Expand a += b to a = a + b for low register instructions
The behavior of the += version of `add` is undefined if both registers are low registers. In this case, we need to patch the instruction to replace it with the more traditional 3 register version: a = a + b.
2021-12-07 20:01:12 -06:00
Benjamin Popp
d52c8a7819 Correctly parse delay-load values with offsets 2021-10-04 16:22:52 -05:00
Benjamin Popp
ec027fb05c Fix assembler bug: multiple labels to the same address
When adjusting addresses based on 4-byte word alignment, make sure to adjust all the labels, not just the first one found for that address.
2021-10-04 16:02:56 -05:00
Benjamin Popp
80c642f24f Add ability to automatically repoint thumb code
* When given 4 instructions, that's enough space to replace it with a load, a branch, and an address.
* If the 4 instructions are such that they don't have any external dependencies (they don't contain a branch or a load), they can be safely moved to a new address.

These two points together mean that we can arbitrarily (and safely) expand almost any function. This should be very useful for both users and prototyping.
2021-09-23 21:57:05 -05:00
Benjamin Popp
ae80071ce5 fix scripting bugs
* script repointing should during ?????? pointer resolution: make sure that the pointer resolves to a different address than the script repoints to. This happened because we were looking for freespace for the new pointer and the moved script at the same time.
* when auto-repointing a script, clear the old pointer format and update their anchors with the new destination. This is not happening automatically because scripts don't hold all their length like other runs do, so repointing them left their pointers behind.
2021-07-29 23:35:13 -05:00
Benjamin Popp
2ad6cb1f56 bl instruction needs to be more permissive
allow address to start with 0x
allow address to have 08000000 added to it
2021-07-21 12:52:01 -05:00
Benjamin Popp
9feb55ad9e Defensive code to prevent a crash
if the code contains multiple labels in a row (which is legal, but weird), the old version of this method would crash.
2021-04-17 21:41:23 -05:00
Benjamin Popp
a49a53c494 Fix overzealous format clearing
When adding a thumb routine, we want to clear out any pointers within the data and write new pointer runs for the new pointers. But we want to keep any anchors that point to the start of the routine.
2021-04-01 08:47:31 -05:00
Benjamin Popp
52a89e5657 Allow thumb compiler to make metadata changes.
Replace the existing method with a pair of methods.
One method requires a token, so that it can make metadata changes.
The other returns a set of runs along with its set of compiled bytes, so that pointers can be adjusted.
2021-03-29 14:15:54 -05:00
Benjamin Popp
c40af6676d Different ldr shouldn't insert the same word multiple times.
Track the target value of deferred ldr instructions so that we can insert each different word only once.
2021-03-29 08:36:57 -05:00
Benjamin Popp
5ea2b312cd Allow deferred instructions to defer named pointers 2021-03-29 07:58:48 -05:00
Benjamin Popp
a1e51724b6 Code cleanup 2021-03-28 21:20:38 -05:00
Benjamin Popp
8409aa199c Introduce Unmapped Constants
Unmapped Constants are much like Unmapped Pointers: they're tracked by the Model, the metadata, and undo, but they don't have a spot in the file. This allows us to create constants using the existing syntax `@constant=value` which can then be referenced in thumb code using `.word`s.
* New test to show that we can save unmapped constants from the ViewModel to Metadata
* New test to show that we can load unmapped constants from Metadata to the Model
* New test to show that we can undo the adding of unmapped constants
* New test to show that unmapped constants can be used in thumb words
2021-03-21 16:23:55 -05:00
Benjamin Popp
0cd4f9613b Improve thumb parser / code search
* ldr can only have positive offsets, no sign.
* During code search, don't follow conditional branch instructions if they're not preceded by a condition. This prevents going on wild chases of data that _looks_ like an instruction, but is actually pointer data. This matters for things like switch statements.
* Improve documentation for thumb  registers.
2021-03-08 23:11:57 -06:00
Benjamin Popp
1edb2d4202 Update search-for-field-usages routine
Since we're looking for where the field of table is used, go ahead and put the code in ArrayRun. Then we only have to pass in an object that knows how to parse thumb code and the index of the field to find usages of.

Have each step just do a single search and return all results. This makes all the loops in one place, while all the searching is clearly separated.

Use a `FindAllCommands` method to handle watching for `mov` instructions and branch instructions, as well as watching for an instruction that uses the specified register and matches a predicate. This makes the algorithm more flexible and a bit easier to read, because it makes the top-level method fairly straightforward.
2021-03-07 23:09:55 -06:00
Benjamin Popp
3f866f1be8 First pass at searching for movestat[...].power uses.
No active code yet. Works for Emerald/FireRed.
2021-03-05 09:08:20 -06:00
Benjamin Popp
7502075ca2 Display known anchor names in bl commands 2021-02-18 22:02:29 -06:00
Benjamin Popp
92f73ddb87 Allow thumb script words and ldr commands to use anchors with offsets 2021-02-17 13:38:58 -06:00
Benjamin Popp
feacfd1a19 Don't add the nop before .words if the code starts at a non-multiple of 4 2021-01-21 13:05:14 -06:00
Benjamin Popp
6a4a3f62a2 Allow thumb in paste scripts
* ViewPort needs to be able to understand thumb directives. This means that .whatever must be interpreted as a directive and not as a constant. So constant names are required to *contain* at least one dot. For example, `.some.number`.
* When reading data that's been pasted in, if you find a `.thumb` directive, read until you find a `.end` directive, then interpret that whole thing as a thumb script, all at once.
* allow the `.end` directive to appear in thumb code
* `.align` directives should work in the viewport. Other directives that appear towards the top of thumb scripts before the `thumb` directive should be ignored.
2021-01-20 15:46:54 -06:00
Benjamin Popp
86a1706647 Thumb parse improvements
* Allow parsing numbers even if they don't start with the # prefix.
* Allow parsing of various ldr/str commands without needing to include an offset.
2021-01-20 07:56:40 -06:00
Benjamin Popp
8217f92c1e Allow pc/lr to be inside {} for push/pop instruction
This more closely matches the spec: https://developer.arm.com/documentation/dui0204/j/arm-and-thumb-instructions/memory-access-instructions/push-and-pop
2021-01-19 22:27:18 -06:00
Benjamin Popp
c5b56211f4 Thumb compiler improvements
Allow # on numbers during delayed ldr instructions
Expand selection based on increased code length as the user edits the script.
2020-10-10 21:23:27 -05:00
Benjamin Popp
ea22201fe9 Improve thumb compiler
* `ldr rn, #` requires an input to be a multiple of 4
* `ldrh rn, #` requires an input to be a multiple of 2
* `ldrb rn, #` requires an input to be a multiple of 1
* `add sp, #` and `sub sp, #` require the input to be a multiple of 4
* `add rd, sp, #` and `add rd, pc, #` require the input to be a multiple of 4
2020-10-07 09:03:01 -05:00
Benjamin Popp
53e6479706 Make it possible to compile Tarma's thumb to bin (part 1)
* Adding in the source / bin files for testing
* Added the "skip instruction" to handle macros that need have no meaning
* Added a "PatchInstruction" method to do some instruction conversions: add negative to subtract positive, etc
* Fixed a bug that prevented me from opening .bin files.
2020-10-05 22:34:58 -05:00
Benjamin Popp
40c81c614e Thumb compiler improvements
* Allow labels to be at the start of a line that has more stuff on it
* Allow words to start with the 0x hexadecimal prefix
* Allow pointer words to display the anchor during decompile
* Allow inline words to have a +1 modifier
2020-09-09 22:46:37 -05:00
Benjamin Popp
f6e9127f2d allow inline words when compiling thumb
example:  ldr r0, =255
The instuction gets queued to be written later, and space is left for it in the form of a `nop`.
Then the next time we hit the end of a section (b, bx, or pop pc), we write the word and we go back and write the instruction, now that we know the location of the word.
2020-09-09 07:38:55 -05:00
Benjamin Popp
107b1b8c1f Allow parsing hex numbers 2020-09-08 19:01:36 -05:00
Benjamin Popp
30f6a6ac73 Fix thumb compiler bug
4-byte align when writing words
2020-06-05 15:42:52 -05:00
Benjamin Popp
8b1517f740 Don't crash when editing thumb code 2020-06-05 14:14:24 -05:00
Benjamin Popp
ffe37fa9f4 Fix failing test
Also use automated code review
2020-05-27 23:28:39 -05:00
Benjamin Popp
4572cc217b Implement the ability to add uncompressed sprite/palette runs via ViewPort.Edit
There's a lot of refactor I need for this section of the codebase (parsing run formats) but that can come later.
2020-03-25 20:57:39 -05:00
Benjamin Popp
3e8fd018a1 Refactor Singletons
Previously, the creating a new ViewPort loaded from 2 text files and creating a new HardcodeTablesMoel loaded from 1 text file. This is a lot of parsing!

Refactor to do the parsing once, in the EditorViewModel, using a Singleton class to hold the non-editable loaded data. Inject that data into the appropriate structers later on.

To prevent massive refactoring in the tests, not every ViewPort needs to be given a Singletons instance. Instead, allow a ViewPort to still do its own parsing. However, BaseViewModelTestClass has been updated to make this work for a majority of tests, along with changes to AutoSearchTests's fixture and several of the Setup methods that are used by multiple tests.
2020-02-08 21:25:48 -06:00
Benjamin Popp
851da6880d Update code tool to allow it to write code
This is currently not a user-exposed feature, but it was very helpful while researching how to make the proper changes for move-expansion.

It shouldn't be exposed (CodeTool.IsReadOnly=false) until I figure out how to handle edge cases and compile error reporting better.
2020-02-06 21:42:49 -06:00
Benjamin Popp
c850895e0b Move Expansion Bugfixes
The code didn't quite do what I meant for it to do. Fixed the broken offsets, fixed the broken code, and made the operation use the undo system.

The code still doesn't have the effect I expected, but at least it's now making the changes I expected.
2020-02-01 22:18:00 -06:00
Benjamin Popp
375e9c7bb2 Update Tutor Expansion
There's a bug in tutor expansion: if you try to use tutor 16 (0x10) in a script, it no longer asks what pokemon you want but just tries to teach it to the first. This is incorrect.

While looking into this, go ahead and update the tutor code to be easier to work with in the future.
2019-12-10 22:12:17 -06:00
Benjamin Popp
1f8428c810 Autoformat 2019-11-24 14:41:05 -06:00
Benjamin Popp
49a4054aa4 Thumb Bugfix
decompiled thumb code for "mov r7, r8" was showing up as "mov r15, r0" because I was getting the high bit wrong. Added a test that showed how it should be, and fixed it. But writing the reverse test for the compiler found a similar bug in the compiler. That's also fixed.
2019-10-08 11:40:40 -05:00
Benjamin Popp
32ddcfd8e6 Allow Thumb code comments 2019-10-08 10:09:48 -05:00
Benjamin Popp
b6da614b95 lsl should select the correct version 2019-07-27 22:15:36 -05:00
Benjamin Popp
dbe054d090 ldrh should compile correctly 2019-07-27 22:06:32 -05:00
Benjamin Popp
ab1ace2535 First pass at new implemantiton of making TMs Expandable
There's likely still some bugs. But the code compiles, and the code it generates doesn't crash when generating. Code likely still does not generate correctly, let alone have the desired effect.
2019-07-26 19:00:49 -05:00
Benjamin Popp
a5c97b86be Compilation works for .word directive and pc-relative ldr 2019-07-21 13:44:50 -05:00
Benjamin Popp
3ecd5485c7 Thumb Compiler works with labels
not only do I need the thumb compiler to work with basic commands: I also need it to work with branch commands that refer to a label, either elsewhere in the function or recalled by the model.
2019-07-20 21:19:42 -05:00
Benjamin Popp
759612214c Thumb Compiler part 1 2019-07-19 22:02:59 -05:00
Benjamin Popp
60a5988510 bl is 2-instructions wide
almost all thumb instructions are 2 bytes wide. But bl is 4 bytes wide. The first two bytes include the high half of where to jump, and the next two bytes contain the low half of where to jump. But when loading all 4 bytes at once, this gets reversed. bl allows you to jump to functions 22-bits away. (x*2+4 from current address, where x is a 22 bit signed int).
2019-06-22 21:55:25 -05:00
Benjamin Popp
9f92c4cd2e general thumb parser fixes
* push lists are the opposite order from pop lists
* if there's a 'push lr' command, that's an interesting address
* bl and blx commands have interesting stuff after them, so don't count those as range-enders
* show code even if the selection is backwards (end-to-start)
2019-06-21 22:36:20 -05:00
Benjamin Popp
f3f97658ce ldr is word-aligned, even if the thumb instruction isn't 2019-06-21 21:01:31 -05:00