Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Non-constant literal initialization is not supported #98

Open
agg23 opened this issue Jan 2, 2021 · 3 comments
Open

Non-constant literal initialization is not supported #98

agg23 opened this issue Jan 2, 2021 · 3 comments

Comments

@agg23
Copy link
Contributor

agg23 commented Jan 2, 2021

The compiler allows you to initialize a global variable with a literal: const byte test = 1, but doesn't actually generate any code to initialize those variables on program start.

word input = 1000
byte output

void main() {
  func(input)

  if (output == 0) {
    asm {
      kil
    }
  }

  while (true) {}
}

void func(word func_input) {
  output = func_input.hi
}

void nmi() {
  
}

void irq() {

}

Built with java -jar millfork.jar main.mfk -o build/rom.nes -t nes_small -g -s -fsource-in-asm -fillegals

@KarolS
Copy link
Owner

KarolS commented Jan 6, 2021

I guess the compiler should warn about it, but initialization of variables on ROM-based targets requires manual handling right now: https://karols.github.io/millfork/api/rom-vs-ram.html

In your case, it should just be enough to call init_rw_memory() near the beginning of the program.

@agg23
Copy link
Contributor Author

agg23 commented Jan 6, 2021

Interesting. Good to know that exists. What is the rationale behind not running it immediately on start?

@KarolS
Copy link
Owner

KarolS commented Jan 6, 2021

It lets you defer it for later if you want to do some hardware initialization beforehand, and also it is in line with Millfork's "middle-level language" philosophy. It also has bonus points of being able to reset your program's memory with a single line of code (as you can call init_rw_memory() whenever you want as many times you want).

Currently, Millfork is not designed to have any kind of runtime, it just goes straight to your main function in the simplest and fastest way necessary. In comparison, C runtime does many things like memory initialization before it finally calls main.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants