How parabirb/6902 works
This project is a lightweight 6502 emulator library written in JavaScript. Its primary purpose is to simulate a 6502 CPU by stepping through clock cycles and executing instructions, with the ability to interface with several “adapters” (for LCDs, LEDs, RAM, ROM, and VIA) that represent peripheral devices or memory. The README and file structure confirm that this is not a full-stack application but rather a modular emulation engine, with key concerns centered on emulating CPU cycles and integrating various adapters for different system components.
Main Components Identification
• CPU Module: Represented by the file src/lib/cpu.js, this is the core component that handles clock cycles and instruction execution.
• Instruction Set Module: The src/lib/instructions directory houses individual files for each 6502 instruction (adc.js, and.js, asl.js, etc.). This layer defines how each instruction behaves.
• Adapters Module: The src/lib/adapters directory contains different adapter files (lcd.js, led.js, map.js, ram.js, rom.js, via.js) to interface the CPU with various peripherals and memory systems.
• Supporting Modules: Files such as src/lib/flags.js and src/lib/index.js are crucial for managing CPU state and for providing a public API for the emulator.
• Test Examples: The src/test folder contains sample binary and JavaScript files (e.g., blink.js, hello_world.js) that demonstrate how to use the emulator.
Relationships and Interactions
• The CPU uses the instruction set to know what operations are executed during each clock cycle.
• From CPU to each adapter (ROM, RAM, LED, LCD, VIA) indicating the data flow (memory access or I/O operations).
• From the instruction set to the CPU signaling operation code lookups.
• The public API (src/lib/index.js) acts as the entry point to configuring the CPU, initializing the data bus with an adapter (for example, rom(buffer)), and stepping through cycles.
Architectural Patterns and Design Principles
• Modularity: The design emphasizes keeping CPU operations, instruction definitions, and peripheral adapters separated into distinct modules.
• Extensibility: With a clear adapter interface, additional peripheral emulation can be plugged in without modifying core CPU behavior.
• Simplicity in abstraction: The CPU’s one clock cycle per instruction simplification is a design decision that simplifies the model and is a key point in the architecture.
• CPU (src/lib/cpu.js) at the center.
• Instruction Set (src/lib/instructions) linked directly to the CPU.
• Adapter Modules (src/lib/adapters) arranged peripherally, each with its label (lcd, led, map, ram, rom, via).
• Public Interface (src/lib/index.js) showing how external configuration and integration occurs.
• Test/Examples (src/test) connected to the Public API, illustrating practical usage.
• With labels for each connection, noting dependencies and the abstraction layers.