|
12 | 12 | # ---------------------------------------------------------------------- |
13 | 13 |
|
14 | 14 | import sys |
15 | | -import os |
16 | | -import argparse |
17 | | - |
18 | | -import asmparse |
19 | | -import zxbpp |
20 | | - |
21 | | -import api.config |
22 | | -from api.config import OPTIONS |
23 | | -from api import global_ |
24 | | - |
25 | | -# Release version |
26 | | -VERSION = '1.13.1' |
27 | | - |
28 | | - |
29 | | -def main(args=None): |
30 | | - # Initializes asm parser state |
31 | | - api.config.init() |
32 | | - asmparse.init() |
33 | | - zxbpp.init() |
34 | | - |
35 | | - # Create option parser |
36 | | - o_parser = argparse.ArgumentParser(prog='zxbasm') |
37 | | - o_parser.add_argument('PROGRAM', type=str, help='ASM program file') |
38 | | - o_parser.add_argument("-d", "--debug", action="count", default=OPTIONS.Debug.value, |
39 | | - help="Enable verbosity/debugging output") |
40 | | - |
41 | | - o_parser.add_argument("-O", "--optimize", type=int, dest="optimization_level", |
42 | | - help="Sets optimization level. 0 = None", default=OPTIONS.optimization.value) |
43 | | - |
44 | | - o_parser.add_argument("-o", "--output", type=str, dest="output_file", |
45 | | - help="Sets output file. Default is input filename with .bin extension", default=None) |
46 | | - |
47 | | - o_parser.add_argument("-T", "--tzx", action="store_true", dest="tzx", default=False, |
48 | | - help="Sets output format to tzx (default is .bin)") |
49 | | - |
50 | | - o_parser.add_argument("-t", "--tap", action="store_true", dest="tap", default=False, |
51 | | - help="Sets output format to tzx (default is .bin)") |
52 | | - |
53 | | - o_parser.add_argument("-B", "--BASIC", action="store_true", dest="basic", default=False, |
54 | | - help="Creates a BASIC loader which load the rest of the CODE. Requires -T ot -t") |
55 | | - |
56 | | - o_parser.add_argument("-a", "--autorun", action="store_true", default=False, |
57 | | - help="Sets the program to auto run once loaded (implies --BASIC)") |
58 | | - |
59 | | - o_parser.add_argument("-e", "--errmsg", type=str, dest="stderr", default=OPTIONS.StdErrFileName.value, |
60 | | - help="Error messages file (standard error console by default") |
61 | | - |
62 | | - o_parser.add_argument("-M", "--mmap", type=str, dest="memory_map", default=None, |
63 | | - help="Generate label memory map") |
64 | | - |
65 | | - o_parser.add_argument("-b", "--bracket", action="store_true", default=False, |
66 | | - help="Allows brackets only for memory access and indirections") |
67 | | - |
68 | | - o_parser.add_argument('-N', "--zxnext", action="store_true", default=False, |
69 | | - help="Enable ZX Next extra ASM opcodes!") |
70 | | - |
71 | | - o_parser.add_argument("--version", action="version", version="%(prog)s " + VERSION) |
72 | | - |
73 | | - options = o_parser.parse_args(args) |
74 | | - |
75 | | - if not os.path.exists(options.PROGRAM): |
76 | | - o_parser.error("No such file or directory: '%s'" % options.PROGRAM) |
77 | | - sys.exit(2) |
78 | | - |
79 | | - OPTIONS.Debug.value = int(options.debug) |
80 | | - OPTIONS.inputFileName.value = options.PROGRAM |
81 | | - OPTIONS.outputFileName.value = options.output_file |
82 | | - OPTIONS.optimization.value = options.optimization_level |
83 | | - OPTIONS.use_loader.value = options.autorun or options.basic |
84 | | - OPTIONS.autorun.value = options.autorun |
85 | | - OPTIONS.StdErrFileName.value = options.stderr |
86 | | - OPTIONS.memory_map.value = options.memory_map |
87 | | - OPTIONS.bracket.value = options.bracket |
88 | | - OPTIONS.zxnext.value = options.zxnext |
89 | | - |
90 | | - if options.tzx: |
91 | | - OPTIONS.output_file_type.value = 'tzx' |
92 | | - elif options.tap: |
93 | | - OPTIONS.output_file_type.value = 'tap' |
94 | | - |
95 | | - if not OPTIONS.outputFileName.value: |
96 | | - OPTIONS.outputFileName.value = os.path.splitext( |
97 | | - os.path.basename(OPTIONS.inputFileName.value))[0] + os.path.extsep + OPTIONS.output_file_type.value |
98 | | - |
99 | | - if OPTIONS.StdErrFileName.value: |
100 | | - OPTIONS.stderr.value = open(OPTIONS.StdErrFileName.value, 'wt') |
101 | | - |
102 | | - if int(options.tzx) + int(options.tap) > 1: |
103 | | - o_parser.error("Options --tap, --tzx and --asm are mutually exclusive") |
104 | | - return 3 |
105 | | - |
106 | | - if OPTIONS.use_loader.value and not options.tzx and not options.tap: |
107 | | - o_parser.error('Option --BASIC and --autorun requires --tzx or tap format') |
108 | | - return 4 |
109 | | - |
110 | | - # Configure the preprocessor to use the asm-preprocessor-lexer |
111 | | - zxbpp.setMode('asm') |
112 | | - |
113 | | - # Now filter them against the preprocessor |
114 | | - zxbpp.main([OPTIONS.inputFileName.value]) |
115 | | - |
116 | | - # Now output the result |
117 | | - asm_output = zxbpp.OUTPUT |
118 | | - asmparse.assemble(asm_output) |
119 | | - if global_.has_errors: |
120 | | - return 1 |
121 | | - |
122 | | - if not asmparse.MEMORY.memory_bytes: # empty seq. |
123 | | - asmparse.warning(0, "Nothing to assemble. Exiting...") |
124 | | - return 0 |
125 | | - |
126 | | - current_org = max(asmparse.MEMORY.memory_bytes.keys() or [0]) + 1 |
127 | | - |
128 | | - for label, line in asmparse.INITS: |
129 | | - expr_label = asmparse.Expr.makenode(asmparse.Container(asmparse.MEMORY.get_label(label, line), line)) |
130 | | - asmparse.MEMORY.add_instruction(asmparse.Asm(0, 'CALL NN', expr_label)) |
131 | | - |
132 | | - if len(asmparse.INITS) > 0: |
133 | | - if asmparse.AUTORUN_ADDR is not None: |
134 | | - asmparse.MEMORY.add_instruction(asmparse.Asm(0, 'JP NN', asmparse.AUTORUN_ADDR)) |
135 | | - else: |
136 | | - asmparse.MEMORY.add_instruction( |
137 | | - asmparse.Asm(0, 'JP NN', min(asmparse.MEMORY.orgs.keys()))) # To the beginning of binary |
138 | | - |
139 | | - asmparse.AUTORUN_ADDR = current_org |
140 | | - |
141 | | - if OPTIONS.memory_map.value: |
142 | | - with open(OPTIONS.memory_map.value, 'wt') as f: |
143 | | - f.write(asmparse.MEMORY.memory_map) |
144 | | - |
145 | | - asmparse.generate_binary(OPTIONS.outputFileName.value, OPTIONS.output_file_type.value) |
146 | | - return global_.has_errors |
147 | | - |
| 15 | +import zxbasm |
148 | 16 |
|
149 | 17 | if __name__ == '__main__': |
150 | | - sys.exit(main()) |
| 18 | + sys.exit(zxbasm.main()) |
0 commit comments