-- Copyright (c) 2023 Adrian "asie" Siekierka
--
-- Licensed under the Apache License, Version 2.0 with LLVM Exceptions,
-- See https://github.com/llvm-mos/llvm-mos-sdk/blob/main/LICENSE for license
-- information.

print([[/**
 * Automatically generated by generate-rompoke-table-linkscript.lua.
 *
 * This script generates a ROM poke table (nes/misc/rompoke/README.md) with
 * a dynamic size determined by the symbol __rom_poke_table_size.
 *
 * Each section defines one byte of the table, from .rom_poke_table_0
 * to .rom_poke_table_255. Each section is filled in its totality with the
 * respective value 0 to 255; however, "totality" means either one byte
 * (for sections N <= __rom_poke_table_size) or zero bytes, omitting the value
 * (for sections N > __rom_poke_table_size). This has the effect of only
 * emitting a byte in "__rom_poke_table_size" consecutive sections, creating
 * the desired dynamically sized table.
 *
 * The fill pattern chooses to emit all four bytes as a repeating sequence,
 * as the value is interpreted as big-endian, even though we only use one byte
 * of that.
 */]])

print("SECTIONS {")
for i=0,255 do
  print(string.format("  .rom_poke_table_%d : {", i))
  if i == 0 then
    print("    __rom_poke_table = .;")
  end
	print(string.format("    FILL(0x%08X)", i * 0x1010101))
	print(string.format("    . = . + (__rom_poke_table_size > %d ? 1 : 0);", i))
  print("  } >c_readonly")
end
print("}")
