Metadata-Version: 2.5 Name: 6502-python Version: 0.2.0.dev0 Summary: Readable, pure-Python NMOS 6502 instruction-core reference implementation Project-URL: Homepage, https://github.com/alewman/6502-python Project-URL: Repository, https://github.com/alewman/6502-python Project-URL: Issues, https://github.com/alewman/6502-python/issues Author: alewman License: MIT License Copyright (c) 2026 alewman Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. License-File: LICENSE Classifier: Development Status :: 3 - Alpha Classifier: Intended Audience :: Developers Classifier: License :: OSI Approved :: MIT License Classifier: Operating System :: OS Independent Classifier: Programming Language :: Python :: 3 Classifier: Programming Language :: Python :: 3.12 Classifier: Programming Language :: Python :: 3.13 Classifier: Programming Language :: Python :: 3.14 Classifier: Programming Language :: Python :: Implementation :: CPython Classifier: Programming Language :: Python :: Implementation :: PyPy Classifier: Topic :: Software Development :: Libraries Classifier: Topic :: System :: Emulators Requires-Python: >=3.11 Provides-Extra: dev Requires-Dist: pytest>=8; extra == 'dev' Requires-Dist: ruff==0.16.8; extra == 'dev' Description-Content-Type: text/markdown # 6502-python `6502-python` is a dependency-free NMOS 6502 core package. The project distribution name is `6502-python`; import it in Python as `sixfiveohtwo`. It follows the shape and contract of [z80-python](https://github.com/alewman/z80-python), the family's reference core. It runs on CPython 3.12+ and PyPy 3.11; CI tests CPython 3.12-3.14 and PyPy. ## Installation Install the distribution from PyPI (or from a checkout containing this project): ```console python -m pip install 6502-python ``` The distribution name contains a hyphen, but the import package uses the flat module name `sixfiveohtwo`: ```python from sixfiveohtwo import CPU, InterruptLines ``` ## Embedding The host owns memory and supplies a byte-addressable object implementing `read_byte(address)` and `write_byte(address, value)`. The core does not allocate a memory map or provide machine-specific devices. For example, this is a complete in-memory host bus: ```python class HostMemory: def __init__(self): self.data = bytearray(0x10000) def read_byte(self, address): if not 0 <= address <= 0xFFFF: raise ValueError("address must fit in 16 bits") return self.data[address] def write_byte(self, address, value): if not 0 <= address <= 0xFFFF: raise ValueError("address must fit in 16 bits") if not 0 <= value <= 0xFF: raise ValueError("value must fit in 8 bits") self.data[address] = value memory = HostMemory() lines = InterruptLines() cpu = CPU(memory, lines=lines) ``` `CPU` owns mutable register state in `cpu.state`; `cpu.memory` remains the host-provided bus and `cpu.lines` exposes the line object. A `CPUState` can also be supplied to `CPU(..., state=existing_state)` when the host needs to initialize or restore registers. ### Reset and interrupt lines Drive RESET and IRQ as levels. They are reported whenever the host samples an instruction boundary, so keep them asserted for as long as required: ```python cpu.set_reset(True) reset_sample = cpu.sample_instruction_boundary() cpu.set_reset(False) cpu.set_irq(True) irq_sample = cpu.sample_instruction_boundary() cpu.set_irq(False) ``` NMI is edge-latched. A low-to-high transition, or an explicit `signal_nmi()` call, creates one pending NMI indication. The indication is consumed by the next `sample_instruction_boundary()` call; drive NMI low before another rising edge can be recognized: ```python cpu.set_nmi(True) # latch one rising edge pending = cpu.pending_interrupt_boundary() # inspect; does not consume NMI accepted = cpu.sample_instruction_boundary() # accepted.nmi is True cpu.set_nmi(False) cpu.signal_nmi() # latch without a persistent level accepted = cpu.sample_instruction_boundary() ``` `pending_interrupt_boundary()` returns the current RESET and IRQ levels and whether NMI is pending without consuming it. `sample_instruction_boundary()` returns an immutable `InterruptBoundary` and consumes the pending NMI flag. At an instruction boundary, `CPU.step()` accepts RESET first, then pending NMI, then an asserted IRQ when the interrupt-disable flag is clear. IRQ and NMI push PC and status (with B clear), set I, fetch their vectors, and account for seven cycles. A masked IRQ remains asserted and is deferred until interrupts are enabled. During a BRK sequence, an NMI sampled before vector fetch has priority for that vector: BRK still pushes its post-padding PC and B-set status, but the NMI vector is selected. The sequence remains seven cycles and reports its accepted event order as `("BRK", "NMI")`. ## v1 scope and exclusions This project is an **embeddable instruction core**, not a complete computer or console emulator. The v1 target is the original NMOS 6502 instruction set, operating against host-provided memory and interrupt lines. The pure-Python NMOS 6502 core covers all 256 NMOS 6502 opcodes, including documented and undocumented instructions: ADC, AND, ASL, BCC, BCS, BEQ, BIT, BMI, BNE, BPL, BRK, BVC, BVS, CLC, CLD, CLI, CLV, CMP, CPX, CPY, DEC, DEX, DEY, EOR, INC, INX, INY, JMP, JSR, LDA, LDX, LDY, LSR, NOP, ORA, PHA, PHP, PLA, PLP, ROL, ROR, RTI, RTS, SBC, SEC, SED, SEI, STA, STX, STY, TAX, TAY, TSX, TXA, TXS, and TYA. Their documented addressing modes are in scope: accumulator, immediate, implied, relative, zero page, zero-page indexed (X or Y), absolute, absolute indexed (X or Y), indirect, indexed indirect (X), and indirect indexed (Y), where each mode is valid for the corresponding instruction. Decimal mode is intended to match NMOS 6502 behavior, including BCD ADC and SBC arithmetic and the NMOS-specific status-flag results. This is a fidelity target for the instruction core, rather than a claim that every host machine's surrounding hardware behaves identically. The v1 core explicitly does **not** provide or emulate: - 65C02 or 65816 instructions, extensions, or behavior; - memory maps or machine-specific host machines; - cartridges or other devices; or - cycle-accurate bus-pin activity or bus-pin timing claims. The host remains responsible for the memory and device environment around the core. See [Embedding](#embedding) for the host-memory and interrupt-line contract. ## Validation with SingleStepTests The test suite can validate one-instruction NMOS 6502 behavior against the MIT-licensed [SingleStepTests 65x02 corpus](https://github.com/SingleStepTests/65x02), using immutable revision `2f6980a2d95757486c7bee24355c360e40e2a224` from the `SingleStepTests/65x02` repository. From the project root, fetch it with: ```console python scripts/fetch_test_vectors.py ``` The pinned corpus is stored at the gitignored project-relative path `tests/6502_test_vectors/6502/`. Pytest remains offline: it never fetches vectors, and missing local vectors produce a skip with the fetch command. With the corpus present, the runner executes all JSON records and checks cycle counts, A/X/Y/PC/SP, the packed status byte and persistent flags, sparse final RAM, and memory mutations. This validates instruction-core behavior for all 256 NMOS 6502 opcodes, not cycle-accurate bus pins or a complete host machine. It does not establish correctness for machine-specific maps/devices, host-bus integration, or other CPU variants. See [the detailed validation documentation](docs/validation.md) for scope and provenance.