Xemu [doxygen]  hyppo 0a42be3a057156924bc1b626a687bd6e27349c45 @ Sat 19 Mar 02:15:11 CET 2022
m65-memcontent-generator.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 
3 # A work-in-progess MEGA65 (Commodore 65 clone origins) emulator
4 # Part of the Xemu project, please visit: https://github.com/lgblgblgb/xemu
5 # Copyright (C)2016-2022 LGB (Gábor Lénárt) <lgblgblgb@gmail.com>
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 
21 import sys
22 
23 PROTOTYPE = "const Uint8"
24 
25 # This must be incremented by ONE every time, when memcontent.c changes, or even
26 # if sdcontent.c is changed in a way to write new files, new content, or whatever
27 # to the SD-card as part of the "update system files" process.
28 MEMCONTENT_VERSION_ID = 2
29 
30 FILE_DB = {
31  # -------------------------------------------------------------------- #
32  # mega65-core-base-name | file-id |SD-image-name(None=not-on-SD) #
33  #--------------------------------------------------------------------- #
34  "HICKUP.M65": ("hickup", None ),
35  "COLOURRAM.BIN": ("cramutils", None ),
36  "BANNER.M65": ("banner", "BANNER.M65" ),
37  "FREEZER.M65": ("freezer", "FREEZER.M65" ),
38  "ONBOARD.M65": ("onboard", "ONBOARD.M65" ), # Do we really need this on SD-card?
39  "mega65.rom": ("initrom", None ),
40  #"megaflash-a200t.prg": ("megaflash", None ),
41  "AUDIOMIX.M65": ("audiomix", "AUDIOMIX.M65" ),
42  "C64THUMB.M65": ("c64thumb", "C64THUMB.M65" ),
43  "C65THUMB.M65": ("c65thumb", "C65THUMB.M65" ),
44  "ROMLOAD.M65": ("romload", "ROMLOAD.M65" ), # Do we really need this on SD-card?
45  "SPRITED.M65": ("sprited", "SPRITED.M65" ),
46  "charrom.bin": ("chrwom", None ),
47  "MAKEDISK.M65": ("makedisk", "MAKEDISK.M65" ),
48 }
49 
50 HEADER = """/* !THIS IS A GENERATED FILE! DO NOT EDIT!
51  * Instead, say 'make recreatememcontent' to re-generate this file
52  * from binary data from the MEGA65 project. Please note, that MEGA65 is
53  * an open source, GNU/GPL project, like Xemu. Thus, it's valid
54  * to use binaries from it, as it's from the compiled version of MEGA65
55  * which is available in source form at https://github.com/MEGA65/mega65-core
56  * always, as per GNU/GPL. */
57 
58 """
59 
60 
61 def bin_dump(data):
62  out, ct = "{\n\t", 0
63  while True:
64  out += "0x{:02X}".format(data[ct])
65  ct += 1
66  if ct == len(data):
67  return out + "\n}"
68  out += ","
69  if ct % 32 == 0:
70  out += "\n\t"
71 
72 
73 
74 if __name__ == "__main__":
75  if len(set(FILE_DB.values())) != len(FILE_DB):
76  sys.stderr.write("Internal DB problem, redundancy in FILE_DB!\n")
77  sys.exit(1)
78  if len(sys.argv) < 4:
79  sys.stderr.write("Bad usage.\n")
80  sys.exit(1)
81  files_done = set()
82  c_file = sys.argv[1]
83  h_file = sys.argv[2]
84  in_files = sys.argv[3:]
85  c_data = HEADER
86  h_data = HEADER
87  h_data += "#ifndef XEMU_MEGA65_{}_INCLUDED\n".format(h_file.upper().replace(".", "_"))
88  h_data += "#define XEMU_MEGA65_{}_INCLUDED\n".format(h_file.upper().replace(".", "_"))
89  h_data += """\n// This must be incremented by ONE every time, when memcontent.c changes, or even
90 // if sdcontent.c is changed in a way to write new files, new content, or whatever
91 // to the SD-card as part of the "update system files" process. Edit this in the python generator though, not in this file!
92 #define MEMCONTENT_VERSION_ID {}\n\n""".format(MEMCONTENT_VERSION_ID)
93  c_data += "#include \"xemu/emutools_basicdefs.h\"\n"
94  c_data += "#include \"memcontent.h\"\n\n"
95  h_data += "// Special structure array for system files update on the SD-image\n"
96  c_data += "// Special structure array for system files update on the SD-image\n"
97  on_sd = sorted([k for k, v in FILE_DB.items() if v[1] is not None])
98  h_data += "struct meminitdata_sdfiles_st { const Uint8 *p; const char *fn; const int size; };\n"
99  h_data += "#define MEMINITDATA_SDFILES_ITEMS {}\n".format(len(on_sd))
100  h_data += "extern const struct meminitdata_sdfiles_st meminitdata_sdfiles_db[MEMINITDATA_SDFILES_ITEMS];\n"
101  c_data += "const struct meminitdata_sdfiles_st meminitdata_sdfiles_db[MEMINITDATA_SDFILES_ITEMS] = {\n"
102  print("Adding files also as SD-content: {}".format(" ".join(on_sd)))
103  for a in on_sd:
104  c_data += "\t{"
105  c_data += " meminitdata_{}, \"{}\", MEMINITDATA_{}_SIZE ".format(FILE_DB[a][0], FILE_DB[a][1].upper(), FILE_DB[a][0].upper())
106  c_data += "},\n"
107  c_data += "};\n\n"
108  for fn in in_files:
109  fn_base = fn.split("/")[-1]
110  if fn_base not in FILE_DB:
111  sys.stderr.write("Unknown file encountered: {}\n".format(fn))
112  sys.exit(1)
113  fn_id = FILE_DB[fn_base][0]
114  if fn_id in files_done:
115  sys.stderr.write("ERROR: file {} is used more than once by id \"{}\"!\n".format(fn, fn_id))
116  sys.exit(1)
117  files_done.add(fn_id)
118  with open(fn, "rb") as data:
119  data = data.read()
120  print("Using file {} (${:X} bytes) as {} ...".format(fn, len(data), fn_id))
121  if len(data) < 1:
122  sys.stderr.write("ERROR: file {} is zero byte long!\n".format(fn))
123  sys.exit(1)
124  h_data += "\n// Generated as \"{}\" from file {} (${:X} bytes)\n".format(fn_id, fn, len(data))
125  h_data += "#define MEMINITDATA_{}_SIZE 0x{:X}\n".format(fn_id.upper(), len(data))
126  h_data += "extern {} meminitdata_{}[MEMINITDATA_{}_SIZE];\n".format(PROTOTYPE, fn_id, fn_id.upper())
127  c_data += "\n// Generated as \"{}\" from file {} (${:X} bytes)\n".format(fn_id, fn, len(data))
128  c_data += "{} meminitdata_{}[MEMINITDATA_{}_SIZE] = {};\n".format(PROTOTYPE, fn_id, fn_id.upper(), bin_dump(data))
129  h_data += "\n#endif\n"
130  # OK, now write out the result ...
131  with open(c_file, "wt") as f: f.write(c_data)
132  with open(h_file, "wt") as f: f.write(h_data)
133  for k, v in FILE_DB.items():
134  if v[0] not in files_done:
135  print("Warning: entity {} was not specified (via filename {})\n".format(v[0], k))
136  sys.exit(0)
m65-memcontent-generator.bin_dump
def bin_dump(data)
Definition: m65-memcontent-generator.py:61