-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
85 lines (74 loc) · 1.45 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/**
* @file 8086 Emulator
* @author treelite(c.xinle@gmail.com)
*/
import fs from 'fs';
import CPU from './lib/CPU';
import Video from './lib/Video';
/**
* 8086模拟器
*
* @class
*/
class Emulator {
/**
* 构造函数
*
* @public
* @constructor
*/
constructor() {
let memory = this.memory = new Buffer(Math.pow(2, 16) - 1);
let cpu = this.cpu = new CPU(memory);
let video = this.video = new Video();
cpu.on('writevideo', (address, data) => video.write(address, data));
}
/**
* 加载程序
*
* @public
* @param {string|Buffer} program 程序
*/
load(program) {
if (typeof program === 'string') {
program = fs.readFileSync(program);
}
this.memory.fill(0);
program.copy(this.memory);
this.cpu.reset();
}
/**
* 运行
*
* @public
* @return {Object} 运行结果
*/
run() {
return this.cpu.run();
}
/**
* 单步运行
*
* @public
* @return {*}
*/
step() {
return this.cpu.next();
}
/**
* Dump data
*
* @public
* @return {Object}
*/
dump() {
let memory = this.memory;
let dumpMemory = new Buffer(memory.length);
memory.copy(dumpMemory);
return {
register: this.cpu.dump(),
memory: dumpMemory
};
}
}
export default Emulator;