6502-emulator 中文文档教程
CPU6502 Emulator
用于 MOS6502(WDC 65c02 兼容)CPU 的 javascript 模拟器。 设计用于模拟使用 6502 的自定义逻辑板。
所有内存读/写调用委托函数,允许自定义内存逻辑、I/O 逻辑和地址解码。
Configuration
```javascript 1.8 const cpu = new CPU6502({ accessMemory, // CPU 读/写内存时调用的函数 logInstructions: false, // 将每条指令输出到控制台,用于高级调试 logInternalState: false, // 在每条指令后输出内部处理器状态以进行高级调试 maxInstructions: 50 // 50条指令后自动暂停执行 });
## Usage
### Available methods
JavaScript 1.8 const cpu = new CPU6502();
cpu.reset(); //触发复位序列 cpu.triggerIRQB(); //触发IRQB中断 cpu.triggerNMIB(); // 触发 NMIB 中断
cpu.pauseClock(); //暂停时钟,停止执行指令 cpu.startClock(); // 启动/恢复时钟
// 访问内部状态 console.log(cpu.rega); console.log(cpu.regx); 控制台日志(cpu.reg_y); 控制台日志(cpu.programCounter); 控制台日志(cpu.stackPointer); 控制台日志(cpu.processorStatus);
### Example 1: Executing simple machine code
JavaScript 1.8 import { CPU6502, ReadWrite } from "6502-emulator";
// 指令操作码 const INOOP = 0xea; const ILDA = 0xa9; const ISTA = 0x8d; const IJMP = 0x4c;
//设置内存 const ram = new Uint8ClampedArray(0xffff); // 64kb 内存 ram.fill(I_NOOP); // 用 noop 指令填充 ram
// 将重置向量存储为 0200(小端) ram[0xfffc] = 0x00; ram[0xfffd] = 0x02;
// 创建一个基本程序 ram.set( [ 我LDA, 0x55, // lda 55 我 STA, 0x00, 0x60, // 55 -> 6000(输出 55 到地址 0x6000)
I_LDA, 0xaa, // lda AA
I_STA, 0x00, 0x60, // AA -> 6000 (output AA to address 0x6000)
I_JMP, 0x00, 0x02 // jump back to start of program
], 0x0200 );
const accessMemory = (readWrite, address, value) =>; { // 捕获对 0x6000 的写入作为魔法输出地址,打印到控制台 如果(地址=== 0x6000 && readWrite === ReadWrite.write){ console.log("输出:", value.toString(16)); 返回; }
// 将值写入 RAM(处理器正在从 [地址] 读取) 如果(readWrite === ReadWrite.read){ 返回内存[地址]; }
// 在 RAM 中存储值(处理器正在将 [value] 写入 [address]) ram[地址] = 值; };
const cpu = new CPU6502({ accessMemory }); // 触发重置以启动时钟 & 跳转到重置向量 cpu.reset();
### Example 2: Load ROM image from disk
JavaScript 1.8 import { CPU6502, ReadWrite } from "6502-emulator";
// 从磁盘加载图像 const ramImagePath = "./myROMFile"; const ramImage = fs.readFileSync(ramImagePath);
//设置内存 const ram = Uint8ClampedArray.from(ramImage);
const accessMemory = (readWrite, address, value) =>; { // 捕获对 0x6000 的写入作为魔法输出地址,打印到控制台 如果(地址=== 0x6000 && readWrite === ReadWrite.write){ console.log("输出:", value.toString(16)); 返回; }
// 捕获写入 0x6005 作为魔法输出地址,暂停时钟 如果(地址=== 0x6005 && readWrite === ReadWrite.write){ console.log("Exit captured! pausing clock"); cpu.pauseClock(); 返回; }
// 将值写入 RAM(处理器正在从 [地址] 读取) 如果(readWrite === ReadWrite.read){ 返回内存[地址]; }
// 在 RAM 中存储值(处理器正在将 [value] 写入 [address]) ram[地址] = 值; }
const cpu = new CPU6502({ accessMemory }); // 触发重置以启动时钟 & 跳转到重置向量 cpu.reset(); ```
CPU6502 Emulator
A javascript emulator for the MOS6502 (WDC 65c02 compatible) CPU. Designed for simulating custom logic boards that use the 6502.
All memory read/writes call out to a delegate function, allowing custom memory logic, I/O logic and address decoding.
Configuration
```javascript 1.8 const cpu = new CPU6502({ accessMemory, // function to be called when CPU reads/writes to memory logInstructions: false, // output each instruction to console, for advanced debugging logInternalState: false, // output internal processor state after each instruction for advanced debugging maxInstructions: 50 // automatically pause execution after 50 instructions });
## Usage
### Available methods
javascript 1.8 const cpu = new CPU6502();
cpu.reset(); // trigger reset sequence cpu.triggerIRQB(); // trigger IRQB interrupt cpu.triggerNMIB(); // trigger NMIB interrupt
cpu.pauseClock(); // pause clock, stop executing instructions cpu.startClock(); // start/resume clock
// accessing internal state console.log(cpu.rega); console.log(cpu.regx); console.log(cpu.reg_y); console.log(cpu.programCounter); console.log(cpu.stackPointer); console.log(cpu.processorStatus);
### Example 1: Executing simple machine code
javascript 1.8 import { CPU6502, ReadWrite } from "6502-emulator";
// instruction opcodes const INOOP = 0xea; const ILDA = 0xa9; const ISTA = 0x8d; const IJMP = 0x4c;
// set up memory const ram = new Uint8ClampedArray(0xffff); // 64kb ram ram.fill(I_NOOP); // fill ram with noop instructions
// store reset vector as 0200 (little endian) ram[0xfffc] = 0x00; ram[0xfffd] = 0x02;
// create a basic program ram.set( [ ILDA, 0x55, // lda 55 ISTA, 0x00, 0x60, // 55 -> 6000 (output 55 to address 0x6000)
I_LDA, 0xaa, // lda AA
I_STA, 0x00, 0x60, // AA -> 6000 (output AA to address 0x6000)
I_JMP, 0x00, 0x02 // jump back to start of program
], 0x0200 );
const accessMemory = (readWrite, address, value) => { // capture a write to 0x6000 as a magic output address, print to console if (address === 0x6000 && readWrite === ReadWrite.write) { console.log("Output: ", value.toString(16)); return; }
// write value to RAM (processor is reading from [address]) if (readWrite === ReadWrite.read) { return ram[address]; }
// store value in RAM (processor is writing [value] to [address]) ram[address] = value; };
const cpu = new CPU6502({ accessMemory }); // trigger a reset to start the clock & jump to the reset vector cpu.reset();
### Example 2: Load ROM image from disk
javascript 1.8 import { CPU6502, ReadWrite } from "6502-emulator";
// load image from disk const ramImagePath = "./myROMFile"; const ramImage = fs.readFileSync(ramImagePath);
// set up memory const ram = Uint8ClampedArray.from(ramImage);
const accessMemory = (readWrite, address, value) => { // capture a write to 0x6000 as a magic output address, print to console if (address === 0x6000 && readWrite === ReadWrite.write) { console.log("Output: ", value.toString(16)); return; }
// capture a write to 0x6005 as a magic output address, pause the clock if (address === 0x6005 && readWrite === ReadWrite.write) { console.log("Exit captured! pausing clock"); cpu.pauseClock(); return; }
// write value to RAM (processor is reading from [address]) if (readWrite === ReadWrite.read) { return ram[address]; }
// store value in RAM (processor is writing [value] to [address]) ram[address] = value; }
const cpu = new CPU6502({ accessMemory }); // trigger a reset to start the clock & jump to the reset vector cpu.reset(); ```
你可能也喜欢
- 2d-polygon-contains-polygon 中文文档教程
- 360medics-interface-components 中文文档教程
- 3d-array-to-string 中文文档教程
- @0x706b/diagnostic-languageserver 中文文档教程
- @10play/phoenix-text 中文文档教程
- @3liv/rijs 中文文档教程
- @404coder/addtimestamp-webpack-plugin 中文文档教程
- @4lch4/koa-router-printer 中文文档教程
- @62d/generator-62d 中文文档教程
- @a11y-kit/utils 中文文档教程