nopa 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. nopa/__init__.py +10 -0
  2. nopa/__main__.py +6 -0
  3. nopa/assemble.py +179 -0
  4. nopa/basic.py +158 -0
  5. nopa/context.py +338 -0
  6. nopa/cpu.py +452 -0
  7. nopa/directives.py +556 -0
  8. nopa/disassemble.py +210 -0
  9. nopa/evaluate.py +80 -0
  10. nopa/generators.py +38 -0
  11. nopa/imports/stack.py +55 -0
  12. nopa/imports/target/c128/basic.py +9 -0
  13. nopa/imports/target/c128/cia.py +35 -0
  14. nopa/imports/target/c128/ioport.py +4 -0
  15. nopa/imports/target/c128/kernal.py +43 -0
  16. nopa/imports/target/c128/machine.py +5 -0
  17. nopa/imports/target/c128/mmu.py +14 -0
  18. nopa/imports/target/c128/sid.py +34 -0
  19. nopa/imports/target/c128/vdc.py +4 -0
  20. nopa/imports/target/c128/vic.py +105 -0
  21. nopa/imports/target/c16/basic.py +9 -0
  22. nopa/imports/target/c16/kernal.py +43 -0
  23. nopa/imports/target/c16/machine.py +4 -0
  24. nopa/imports/target/c16/ted.py +55 -0
  25. nopa/imports/target/c64/basic.py +9 -0
  26. nopa/imports/target/c64/cia.py +35 -0
  27. nopa/imports/target/c64/ioport.py +4 -0
  28. nopa/imports/target/c64/kernal.py +43 -0
  29. nopa/imports/target/c64/keyboard.py +66 -0
  30. nopa/imports/target/c64/machine.py +3 -0
  31. nopa/imports/target/c64/music.py +149 -0
  32. nopa/imports/target/c64/sid.py +34 -0
  33. nopa/imports/target/c64/vic.py +103 -0
  34. nopa/imports/target/cx16/basic.py +12 -0
  35. nopa/imports/target/cx16/kernal.py +61 -0
  36. nopa/imports/target/cx16/machine.py +28 -0
  37. nopa/imports/target/cx16/vera.py +52 -0
  38. nopa/imports/target/cx16/via.py +35 -0
  39. nopa/imports/target/cx16/ym2151.py +33 -0
  40. nopa/imports/target/plus4/basic.py +9 -0
  41. nopa/imports/target/plus4/kernal.py +43 -0
  42. nopa/imports/target/plus4/machine.py +4 -0
  43. nopa/imports/target/plus4/ted.py +55 -0
  44. nopa/imports/target/vic20/basic.py +9 -0
  45. nopa/imports/target/vic20/kernal.py +40 -0
  46. nopa/imports/target/vic20/machine.py +9 -0
  47. nopa/imports/target/vic20/via.py +35 -0
  48. nopa/imports/target/vic20/vic.py +37 -0
  49. nopa/imports/utility.py +72 -0
  50. nopa/imports/vector.py +13 -0
  51. nopa/instructions.py +133 -0
  52. nopa/lexer.py +178 -0
  53. nopa/main.py +174 -0
  54. nopa/text.py +92 -0
  55. nopa-0.1.0.dist-info/METADATA +1423 -0
  56. nopa-0.1.0.dist-info/RECORD +60 -0
  57. nopa-0.1.0.dist-info/WHEEL +5 -0
  58. nopa-0.1.0.dist-info/entry_points.txt +2 -0
  59. nopa-0.1.0.dist-info/licenses/LICENSE.txt +21 -0
  60. nopa-0.1.0.dist-info/top_level.txt +1 -0
nopa/__init__.py ADDED
@@ -0,0 +1,10 @@
1
+ #! /usr/bin/env python3
2
+ """Nop Assembler: a 6502/Python hybrid cross assembler for Commodore computers."""
3
+
4
+ __version__ = '0.1.0'
5
+
6
+ from .assemble import assembleFile
7
+ from .disassemble import disassembleFile
8
+ from .context import Context
9
+
10
+ __all__ = ['assembleFile', 'disassembleFile', 'Context']
nopa/__main__.py ADDED
@@ -0,0 +1,6 @@
1
+ #! /usr/bin/env python3
2
+ """Run the command line with python -m nopa, the same as the nopa command."""
3
+
4
+ from .main import main
5
+
6
+ main()
nopa/assemble.py ADDED
@@ -0,0 +1,179 @@
1
+ #! /usr/bin/env python3
2
+ """The assembler: assembles source files into memory, in multiple passes."""
3
+
4
+ import keyword
5
+ import os
6
+ import glob
7
+
8
+ from . import directives
9
+ from . import evaluate
10
+ from . import generators
11
+ from . import instructions
12
+ from . import lexer
13
+ from . import text
14
+ from .context import Context
15
+ from .cpu import CPU, CPU_6502, CPU_65C02, CPU_R65C02, CPU_W65C02, isMnemonic, lo, hi
16
+
17
+ # max passes until all symbols should have been resolved
18
+ MAX_PASSES = 10
19
+
20
+ DEFAULT_ORIGIN = 0x1000 # the address when there is no .org
21
+
22
+ MAX_NESTING = 100 # imports and generators nested deeper are an error, e.g. a file that imports itself
23
+
24
+ TARGETS = ('c128', 'c16', 'c64', 'cx16', 'plus4', 'vic20') # the folders in imports/target
25
+
26
+
27
+ def _assembleAssignment(tokens: list, context: Context):
28
+ """Assign the value of a Python expression to a symbol: name = expression.
29
+
30
+ Upper case symbols are constants: once they have a value, they can't
31
+ be assigned a different value. In a namespace, the symbol gets its
32
+ namespaced name, e.g. speed in spr is spr.speed.
33
+ """
34
+ symbol = context.namespacedName(tokens[0].string)
35
+ expression = lexer.join(tokens[2:])
36
+ value, ok = evaluate.expression(expression, context)
37
+ if ok: # None is a value too
38
+ if symbol.rsplit('.', 1)[-1].isupper(): # the name, without a namespace, e.g. SPEED in spr.SPEED
39
+ if symbol in context.symbols and context.symbols[symbol] != value:
40
+ context.reportError(f'Constant "{symbol}" cannot be changed from {context.symbols[symbol]} to {value}')
41
+ return # the constant keeps its value
42
+ context.setSymbol(symbol, value)
43
+
44
+
45
+ def assembleFile(inFile: str, target: str | None = None, cpu: CPU = CPU_6502,
46
+ defines: dict[str, str] | None = None) -> Context:
47
+ """Assemble inFile. Returns the context of the last pass: memory, symbols, messages and errors,
48
+ for the caller to use, e.g. to save the memory to a file. The target is one of TARGETS,
49
+ whose imports are added to the generic ones. Each pass starts with the cpu, which .cpu can change.
50
+ The defines, {name: expression}, are assigned before the source, so upper case names are constants.
51
+ Raises OSError if inFile can't be read, and ValueError for an unknown target.
52
+
53
+ Passes are repeated until the output stops changing (max. MAX_PASSES), so
54
+ labels can be used before they are defined.
55
+ """
56
+ if target is not None and target not in TARGETS:
57
+ raise ValueError(f'Unknown target: {target}')
58
+
59
+ path = os.path.dirname(inFile)
60
+
61
+ symbols = {'lo': lo, 'hi': hi,
62
+ 'ENCODING_SCREEN_UPPER': text.ENCODING_SCREEN_UPPER,
63
+ 'ENCODING_SCREEN_MIXED': text.ENCODING_SCREEN_MIXED,
64
+ 'ENCODING_PETSCII_UPPER': text.ENCODING_PETSCII_UPPER,
65
+ 'ENCODING_PETSCII_MIXED': text.ENCODING_PETSCII_MIXED,
66
+ 'CPU_6502': CPU_6502,
67
+ 'CPU_65C02': CPU_65C02,
68
+ 'CPU_R65C02': CPU_R65C02,
69
+ 'CPU_W65C02': CPU_W65C02,
70
+ 'SOURCE_FOLDER': path} # for generators that read files
71
+
72
+ root = os.path.abspath(os.path.dirname(__file__))
73
+ imports = sorted(glob.glob(os.path.join(root, 'imports/*.py')))
74
+ if target:
75
+ imports += sorted(glob.glob(os.path.join(root, 'imports/target', target, '*.py')))
76
+
77
+ for filename in imports:
78
+ with open(filename) as f:
79
+ exec(compile(f.read(), filename, 'exec'), symbols) # the file name shows in tracebacks
80
+
81
+ lastMemory: dict[int, int] = {}
82
+ lastReferences: set[str] = set()
83
+ builtinNames: set[str] = set() # the symbols before the source, which code in a namespace sees, set in the first pass
84
+ chunks = {} # the tokenized files and generated code, by name: tokenized once, for all passes
85
+
86
+ for _ in range(MAX_PASSES):
87
+ # a new context each pass, but symbols are kept from the last pass
88
+ context = Context(symbols)
89
+ context.currentAddress = DEFAULT_ORIGIN
90
+ context.path = path
91
+ context.cpu = cpu
92
+ context.lastReferences = lastReferences
93
+ symbols['has'] = lambda text: instructions.has(text, context)
94
+
95
+ # the defines, assigned in each pass as if before the first line, so a variable starts with its value
96
+ for index, (name, value) in enumerate((defines or {}).items()):
97
+ context.pushReadPointer( ('<command-line>', index) ) # where an error is, as in gcc
98
+ _assembleAssignment(lexer.tokenize(f'{name} = {value}')[:-1], context)
99
+ context.popReadPointer()
100
+
101
+ if not builtinNames: # in the first pass, after the -D defines and before the first line of source
102
+ builtinNames = set(symbols)
103
+ context.builtinNames = builtinNames
104
+ context.pushReadPointer( (inFile, 0) )
105
+
106
+ # assemble line by line, from the file on top of the read pointer stack
107
+ while True:
108
+ readPointer = context.readPointer()
109
+ if readPointer is None: break
110
+ if len(context.readPointerStack) > MAX_NESTING: # at the first line of the file nested too deep
111
+ context.reportError(f'Imports and generators nested more than {MAX_NESTING} deep')
112
+ context.popReadPointer() # back to the file that imported it, after the .import
113
+ continue
114
+
115
+ filename, lineIndex = readPointer
116
+
117
+ if filename not in chunks:
118
+ chunks[filename] = list(lexer.splitByNewline(lexer.tokenizeFile(filename)))
119
+
120
+ allChunks = chunks[filename]
121
+
122
+ if lineIndex < len(allChunks):
123
+
124
+ lineTokens = allChunks[lineIndex]
125
+ advance = True
126
+
127
+ # label
128
+ if len(lineTokens) >= 2 and lineTokens[1].string == ':':
129
+ label = lineTokens[0].string
130
+ if label.isupper():
131
+ context.reportError(f'Capitals are for constants, not labels: "{label}"')
132
+ elif label.startswith('_'):
133
+ context.reportError(f'Local label without a global label before it: "{label}"')
134
+ label = context.namespacedName(label) # the rules above are for the name as written, the rest for its full name
135
+ if label in context.labels:
136
+ context.reportError(f'Label was already defined: "{label}"')
137
+ context.setSymbol(label, context.currentAddress)
138
+ context.labels.add(label)
139
+ lineTokens = lineTokens[2:]
140
+
141
+ if len(lineTokens) >= 1 and context.cpu.isMnemonic(lineTokens[0].string):
142
+ instructions.assemble(lineTokens, context)
143
+
144
+ elif len(lineTokens) >= 3 and lineTokens[0].string.isidentifier() and not keyword.iskeyword(lineTokens[0].string) and lineTokens[1].string == '=':
145
+ _assembleAssignment(lineTokens, context)
146
+
147
+ elif len(lineTokens) >= 2 and lineTokens[0].string == '.':
148
+ advance = directives.assemble(lineTokens, context, chunks)
149
+
150
+ elif len(lineTokens) >= 2 and lineTokens[0].string == '@':
151
+ advance = generators.assemble(lineTokens, context, chunks)
152
+
153
+ elif len(lineTokens) > 0 and isMnemonic(lineTokens[0].string): # an instruction of another cpu
154
+ context.reportError(f'Not an instruction of the {context.cpu.name}: "{lineTokens[0].string}", see .cpu and --cpu')
155
+
156
+ elif len(lineTokens) > 0:
157
+ context.reportError('Invalid syntax')
158
+
159
+ if advance:
160
+ context.advanceReadPointer()
161
+
162
+ else:
163
+ context.popReadPointer() # end of file, back to the importing file
164
+
165
+ directives.reportOpenBlocks(context)
166
+
167
+ # done when there are no errors, and the output and references are unchanged
168
+ if len(context.errors) == 0 and context.memory == lastMemory and context.references == lastReferences:
169
+ break
170
+
171
+ # prepare for next pass
172
+ lastMemory = dict(context.memory)
173
+ lastReferences = context.references
174
+
175
+ else: # no pass left, and the output still changed in the last one, with or without errors
176
+ if not context.errors: # else they say what failed
177
+ context.reportError(f'Failed to assemble: "{inFile}"')
178
+
179
+ return context
nopa/basic.py ADDED
@@ -0,0 +1,158 @@
1
+ #! /usr/bin/env python3
2
+ """The versions of Commodore BASIC: their keywords and tokens, and generators for a BASIC program.
3
+
4
+ Each target chooses the version of its machine, in imports/target/<target>/basic.py.
5
+ """
6
+
7
+ # The keywords of BASIC 1.0, with tokens from $80, in token order. As in the PET ROM
8
+ _BASIC_1_KEYWORDS = (
9
+ 'END', 'FOR', 'NEXT', 'DATA', 'INPUT#', 'INPUT', 'DIM', 'READ', 'LET', 'GOTO', 'RUN', 'IF',
10
+ 'RESTORE', 'GOSUB', 'RETURN', 'REM', 'STOP', 'ON', 'WAIT', 'LOAD', 'SAVE', 'VERIFY', 'DEF',
11
+ 'POKE', 'PRINT#', 'PRINT', 'CONT', 'LIST', 'CLR', 'CMD', 'SYS', 'OPEN', 'CLOSE', 'GET', 'NEW',
12
+ 'TAB(', 'TO', 'FN', 'SPC(', 'THEN', 'NOT', 'STEP', '+', '-', '*', '/', '^', 'AND', 'OR', '>',
13
+ '=', '<', 'SGN', 'INT', 'ABS', 'USR', 'FRE', 'POS', 'SQR', 'RND', 'LOG', 'EXP', 'COS', 'SIN',
14
+ 'TAN', 'ATN', 'PEEK', 'LEN', 'STR$', 'VAL', 'ASC', 'CHR$', 'LEFT$', 'RIGHT$', 'MID$')
15
+
16
+ # BASIC 2.0 adds GO, with the token $cb. As in the PET, VIC-20 and C64 ROMs
17
+ _BASIC_2_KEYWORDS = _BASIC_1_KEYWORDS + ('GO',)
18
+
19
+ # BASIC 3.5 adds these keywords, with tokens from $cc. As in the Plus/4 ROM
20
+ _BASIC_3_5_KEYWORDS = _BASIC_2_KEYWORDS + (
21
+ 'RGR', 'RCLR', 'RLUM', 'JOY', 'RDOT', 'DEC', 'HEX$', 'ERR$', 'INSTR', 'ELSE', 'RESUME', 'TRAP',
22
+ 'TRON', 'TROFF', 'SOUND', 'VOL', 'AUTO', 'PUDEF', 'GRAPHIC', 'PAINT', 'CHAR', 'BOX', 'CIRCLE',
23
+ 'GSHAPE', 'SSHAPE', 'DRAW', 'LOCATE', 'COLOR', 'SCNCLR', 'SCALE', 'HELP', 'DO', 'LOOP', 'EXIT',
24
+ 'DIRECTORY', 'DSAVE', 'DLOAD', 'HEADER', 'SCRATCH', 'COLLECT', 'COPY', 'RENAME', 'BACKUP',
25
+ 'DELETE', 'RENUMBER', 'KEY', 'MONITOR', 'USING', 'UNTIL', 'WHILE')
26
+
27
+ # BASIC 4.0 adds these disk keywords, with tokens from $cc. As in the PET ROM
28
+ _BASIC_4_KEYWORDS = _BASIC_2_KEYWORDS + (
29
+ 'CONCAT', 'DOPEN', 'DCLOSE', 'RECORD', 'HEADER', 'COLLECT', 'BACKUP', 'COPY', 'APPEND', 'DSAVE',
30
+ 'DLOAD', 'CATALOG', 'RENAME', 'SCRATCH', 'DIRECTORY')
31
+
32
+ # BASIC 4.0+ adds these keywords, with tokens from $db. As in the CBM-II ROMs
33
+ _BASIC_4_PLUS_KEYWORDS = _BASIC_4_KEYWORDS + (
34
+ 'DCLEAR', 'BANK', 'BLOAD', 'BSAVE', 'KEY', 'DELETE', 'ELSE', 'TRAP', 'RESUME', 'DISPOSE', 'PUDEF',
35
+ 'USING', 'ERR$', 'INSTR')
36
+
37
+ # BASIC 7.0 has the keywords of BASIC 3.5, except RLUM, as $ce is a prefix for two-byte tokens. As in the C128 ROM
38
+ _BASIC_7_KEYWORDS = tuple(None if k == 'RLUM' else k for k in _BASIC_3_5_KEYWORDS)
39
+
40
+ # The keywords of BASIC 7.0 with two-byte tokens: a prefix, and a second byte from $02. None is unused.
41
+ # BASIC tries them before the other keywords, in this order, so DOPEN is not DO and PEN. As in the C128 ROM
42
+ _BASIC_7_PREFIXED = {
43
+ 0xfe: ('BANK', 'FILTER', 'PLAY', 'TEMPO', 'MOVSPR', 'SPRITE', 'SPRCOLOR', 'RREG', 'ENVELOPE', 'SLEEP',
44
+ 'CATALOG', 'DOPEN', 'APPEND', 'DCLOSE', 'BSAVE', 'BLOAD', 'RECORD', 'CONCAT', 'DVERIFY', 'DCLEAR',
45
+ 'SPRSAV', 'COLLISION', 'BEGIN', 'BEND', 'WINDOW', 'BOOT', 'WIDTH', 'SPRDEF', 'QUIT', 'STASH', None,
46
+ 'FETCH', None, 'SWAP', 'OFF', 'FAST', 'SLOW'),
47
+ 0xce: ('POT', 'BUMP', 'PEN', 'RSPPOS', 'RSPRITE', 'RSPCOLOR', 'XOR', 'RWINDOW', 'POINTER'),
48
+ }
49
+
50
+ # The symbol names of keywords with other characters than letters, e.g. BASIC_TOKEN_PLUS for +.
51
+ # Otherwise $ and ( are left out, e.g. BASIC_TOKEN_CHR for CHR$
52
+ _TOKEN_NAMES = {'INPUT#': 'INPUT_FILE', 'PRINT#': 'PRINT_FILE', '+': 'PLUS', '-': 'MINUS', '*': 'MULTIPLY',
53
+ '/': 'DIVIDE', '^': 'POWER', '>': 'GREATER', '=': 'EQUAL', '<': 'LESS'}
54
+
55
+ BASIC_TOKEN_PI = 0xff # the pi character, in all versions
56
+
57
+
58
+ class Basic:
59
+ """A version of Commodore BASIC: its keywords, which BASIC stores as tokens."""
60
+
61
+ def __init__(self, version: str, keywords: tuple, prefixed: dict | None = None):
62
+ """keywords: the keywords with one-byte tokens, from $80, in token order. None is unused.
63
+ prefixed: the keywords with two-byte tokens, {prefix: keywords}, with the second byte from $02.
64
+ BASIC tries them first, in this order.
65
+ """
66
+ self.version = version
67
+ self.keywords = keywords
68
+ self.prefixed = prefixed or {}
69
+ # the bytes of each keyword, in the order BASIC tries them
70
+ self._tokens = [(k, [prefix, 2 + i]) for prefix, ks in self.prefixed.items() for i, k in enumerate(ks) if k]
71
+ self._tokens += [(k, [0x80 + i]) for i, k in enumerate(keywords) if k]
72
+ self._tokens += [('?', [0x80 + keywords.index('PRINT')])] # ? is short for PRINT
73
+
74
+ def __repr__(self) -> str:
75
+ return f'Basic({self.version!r})'
76
+
77
+ def tokens(self) -> dict:
78
+ """The BASIC_TOKEN_ symbols: the token of each keyword with a one-byte token, and the pi character."""
79
+ tokens = {f'BASIC_TOKEN_{_TOKEN_NAMES.get(k, k.rstrip("$("))}': 0x80 + i for i, k in enumerate(self.keywords) if k}
80
+ return tokens | {'BASIC_TOKEN_PI': BASIC_TOKEN_PI}
81
+
82
+ def encode(self, text: str) -> list:
83
+ """The bytes of a BASIC line, as BASIC stores it: keywords become tokens, and the rest stays as text.
84
+
85
+ Keywords are not converted in strings, after REM, or in DATA, up to the next ":".
86
+ Raises UnicodeEncodeError for characters that BASIC can't store.
87
+ """
88
+ text = text.upper() # BASIC keywords are in upper case, and so is its text in the default character set
89
+ data: list[int] = []
90
+ index = 0
91
+ inString = False # between two " characters
92
+ literal = None # REM or DATA, while in its text
93
+
94
+ while index < len(text):
95
+ # the keyword at this position, trying them in the order BASIC does, so INPUT# comes before INPUT
96
+ found = None
97
+ if not inString and not literal:
98
+ found = next(((k, t) for k, t in self._tokens if text.startswith(k, index)), None)
99
+
100
+ if found:
101
+ # the token replaces the whole keyword
102
+ keyword, token = found
103
+ data += token
104
+ index += len(keyword)
105
+ literal = keyword if keyword in ('REM', 'DATA') else None
106
+ else:
107
+ # any other character is stored as it is. A " starts or ends a string
108
+ inString ^= text[index] == '"'
109
+ # a ":" ends a DATA statement, unless it is in a string. REM always lasts to the end of the line
110
+ if literal == 'DATA' and text[index] == ':' and not inString:
111
+ literal = None
112
+ if text[index] == 'Π': # the pi character, π, in upper case
113
+ data.append(BASIC_TOKEN_PI)
114
+ else:
115
+ data.append(text[index].encode('ascii')[0])
116
+ index += 1
117
+
118
+ return data
119
+
120
+ def symbols(self) -> dict:
121
+ """The symbols of a target with this version: the BASIC_TOKEN_ values, and the generators."""
122
+
123
+ def basicLine(lineNumber, text):
124
+ """A BASIC line, with keywords as tokens, e.g. "POKE 53280,0:SYS 2061"."""
125
+ data = self.encode(text) + [0]
126
+ return f"""
127
+ .word _ + 2 + 2 + {len(data)}
128
+ .word {lineNumber}
129
+ .byte {', '.join(map(str, data))}
130
+ """
131
+
132
+ def basicSys(lineNumber, address):
133
+ """A BASIC line with just a SYS to the address, the same as basicLine(lineNumber, f"SYS{address}")."""
134
+ return basicLine(lineNumber, f"SYS{address}")
135
+
136
+ return self.tokens() | {'basicStart': _basicStart, 'basicLine': basicLine, 'basicSys': basicSys, 'basicEnd': _basicEnd}
137
+
138
+
139
+ def _basicStart():
140
+ """Set the address to the start of the BASIC program, BASIC_START_ADDRESS, which each target defines."""
141
+ return """
142
+ .org BASIC_START_ADDRESS
143
+ """
144
+
145
+
146
+ def _basicEnd():
147
+ """The end of the BASIC program."""
148
+ return """
149
+ .word 0
150
+ """
151
+
152
+
153
+ BASIC_1 = Basic('1.0', _BASIC_1_KEYWORDS)
154
+ BASIC_2 = Basic('2.0', _BASIC_2_KEYWORDS)
155
+ BASIC_3_5 = Basic('3.5', _BASIC_3_5_KEYWORDS)
156
+ BASIC_4 = Basic('4.0', _BASIC_4_KEYWORDS)
157
+ BASIC_4_PLUS = Basic('4.0+', _BASIC_4_PLUS_KEYWORDS)
158
+ BASIC_7 = Basic('7.0', _BASIC_7_KEYWORDS, _BASIC_7_PREFIXED)