RPGHacker / asar

(Now) official repository of the SNES assembler Asar, originally created by Alcaro
Other
195 stars 42 forks source link

allow math in labels #281

Closed gizaha closed 1 year ago

gizaha commented 1 year ago

It would be handful to do math between labels. For example: !table_size #= .table_end - .table

.table db 1,2,3,4,5 ..end

OR

!current_pc #= pc ;there were many instances that i was needed to do math with program counter

OR (example of bank crossing) .start if (.start + filesize("data.bin"))>>16 != .start>>16 print "bank cross!" else incbin "data.bin" ;safe to include, wont overflow bank endif

jeffythedragonslayer commented 1 year ago

Welcome to GitHub. We typically put code in monospaced font blocks, like this:

std::cout << "hello world\n";

You can find that tool here as you are composing your post:

image

and use the Preview tab to see what it looks like before posting.

Alcaro commented 1 year ago

While useful in theory, it's kinda tricky to implement in practice.

org $008000
lda Mylabel,x
pc:
if pc == $008003
Mylabel = $018000
else
Mylabel = $008000
endif

If Mylabel is in the same bank as the LDA, it will be .w, and pc will be $008003 at the if. But then Mylabel is $018000. If Mylabel is not in the same bank as the LDA, it will be .l, and pc will be $008004 at the if. But then Mylabel is $008000.

We allow some operations on pc, but there are also several restrictions, to avoid the aforementioned paradox.

gizaha commented 1 year ago

Thanks for the reply, i didn't knew that. I search for a way to detect if the binary i want to include fits current bank, otherwise switch the bank and inlude it in next.

RPGHacker commented 1 year ago

Can you give us a bit more context on what you're trying to accomplish and what you're working with? Maybe we can offer workarounds. For example, are you able to use freedata/freecode? From what I remember, freedata tries to find the first free space in the ROM large enough to hold your data and only moves stuff into an empty bank when it doesn't fit into a prior, non-empty one. Though I'm not sure if that provides enough flexibility for you, or if you can even use freedata in the first place. Either way, if you can share some more details, we might be able to propose workarounds.

gizaha commented 1 year ago

No need to explain anything because: Asar means Snes, Including files that are affected from bank overflow is clearly to be DMAed. Multiple files to be DMAed is clearly frame data.

I have a macro includes a group of frames, i want to now before the include, if the frame fits the current bank, otherwise change bank and include it here.

On Sun, Feb 19, 2023 at 5:12 PM RPG Hacker @.***> wrote:

Can you give us a bit more context on what you're trying to accomplish and what you're working with? Maybe we can offer workarounds. For example, are you able to use freedata/freecode? From what I remember, freedata tries to find the first free space in the ROM large enough to hold your data and only moves stuff into an empty bank when it doesn't fit into a prior, non-empty one. Though I'm not sure if that provides enough flexibility for you, or if you can even use freedata in the first place. Either way, if you can share some more details, we might be able to propose workarounds.

— Reply to this email directly, view it on GitHub https://github.com/RPGHacker/asar/issues/281#issuecomment-1436014116, or unsubscribe https://github.com/notifications/unsubscribe-auth/AKWMIZDMWSTD332WWLX52ULWYIZ55ANCNFSM6AAAAAAVASM7VM . You are receiving this because you authored the thread.Message ID: @.***>

RPGHacker commented 1 year ago

Well, there's still a lot of unknowns here. Are you trying to patch an existing ROM, or creating a homebrew? What mapper are you using? LoROM? HiROM? Something else entirely? Are you using freedata for automatic space allocation, or using org for manual placement? All of these things matter.

I can't give you a solution that's guaranteed to work without enough context. I'll just assume you're working on a homebrew, you know what mapper you're using (and by extension, how much space there is in a bank), and you are in control of where to begin with data insertion. If so, I'm pretty sure you can cook up a solution that does pretty much what you want it to do using the filesize(), macros and defines.

gizaha commented 1 year ago

All these are irellevant. Revert to my original issue, i need to read pc position. Filesize is useless since i cannot read pc.

On Sun, Feb 19, 2023 at 5:45 PM RPG Hacker @.***> wrote:

Well, there's still a lot of unknowns here. Are you trying to patch an existing ROM, or creating a homebrew? What mapper are you using? LoROM? HiROM? Something else entirely? Are you using freedata for automatic space allocation, or using org for manual placement? All of these things matter.

I can't give you a solution that's guaranteed to work without enough context. I'll just assume you're working on a homebrew, you know what mapper you're using (and by extension, how much space there is in a bank), and you are in control of where to begin with data insertion. If so, I'm pretty sure you can cook up a solution that does pretty much what you want it to do using the filesize(), macros and defines.

— Reply to this email directly, view it on GitHub https://github.com/RPGHacker/asar/issues/281#issuecomment-1436021864, or unsubscribe https://github.com/notifications/unsubscribe-auth/AKWMIZCYUTETXLFO7B3NF4LWYI53JANCNFSM6AAAAAAVASM7VM . You are receiving this because you authored the thread.Message ID: @.***>

RPGHacker commented 1 year ago

It IS relevant. If you have control over insert location, then you can keep track of PC yourself very easily and don't need a built-in function for.

A simple "get current PC" function is unlikely to make it to Asar without some major compromises, due to issues already noted by Alcaro above. It can only ever work reliably in very specific circumstances.

gizaha commented 1 year ago

Example to understand my problem: ;we are somewhere at e9 bank label_e9: ;it knows pc, but we can only use it like snes code dw label_e9, not asar code incbin "file.bin" ;we know it's length, does it fit in current bank?

I don't want "get current pc" function, i want to add labels with pc, or do pc>>16 to get it's bank.

RPGHacker commented 1 year ago

i need to read pc position.

I don't want "get current pc" function

So which one is it now? 🤷‍♂️

to add labels with pc, or do pc>>16 to get it's bank.

Both of those things are already possible in Asar, just not in the contexts you want (like if statements). And as mentioned above, functionality like that isn't likely to be added to Asar anytime soon, due to complexity and caveats already mentioned above, so if it's something you need right now, you HAVE to use a workaround. Either that, or completely change the approach of what you're doing.

Going ahead and closing the issue now, since we already explained our reasoning and provided workaround, so I don't think there's anything left for us to do here at this time.

gizaha commented 1 year ago

You didn't answer the riddle: macro put() incbin "frame1.bin" incbin "frame2.bin" endmacro Find a way to know if some of the includes would cause bank overflow. Make EVERY approach you want. Use ANY workaround.

I accept the "asar doesn't support this", i don't accept the "use another way to do it". There is no other way since you can't do math with labels.

randomdude999 commented 1 year ago

do you have any code in the same bank, before the frame data? or can you put the frame data right at the start of the bank?

gizaha commented 1 year ago

do you have any code in the same bank, before the frame data? or can you put the frame data right at the start of the bank?

I put data in the start of the bank, but there are too many that overflow many banks.

randomdude999 commented 1 year ago

if you can put it right at the start, then you can keep track of the pc with a define, like this:

!curpc #= $e98000
macro putfile(name,label)
  if !curpc+filesize("<name>")&$ff0000 != !curpc&$ff0000 ; if we would overflow the bank,
    !curpc #= !curpc&$ff0000+$018000 ; go to the start of the next bank
    org !curpc
  endif
  <label>: incbin "<name>"
  !curpc #= !curpc+filesize("<name>")
endmacro
%putfile("frame1.bin",label_frame1)
%putfile("frame2.bin",label_frame2) ; etc

(the "go to the next bank" part here assumes lorom. you can change $018000 to $010000 for hirom i think.)

gizaha commented 1 year ago

Correct, that's what i will do. Thanks.