-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSystem.ts
239 lines (219 loc) · 5.97 KB
/
System.ts
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import { EventEmitter } from "events";
import { clearInterval } from "timers";
import { VIA6522 } from "./6522VIA";
import { CPU } from "./CPU";
import { Keyboard } from "./Keyboard";
import { RA8875 } from "./RA8875";
import { RAM } from "./RAM";
import { ROM } from "./ROM";
import { addr } from "./Utils";
export const ROM_START = 0xc000;
export const RAM_START = 0;
export const RAM_END = 0xafff;
export const VIA1_START = 0xb000;
export const VIA1_END = 0xb0ff;
export const VIA2_START = 0xb200;
export const VIA2_END = 0xb2ff;
type MemoryDevice = {
start: Address;
end: Address;
device: AddressBus;
type: "ram" | "rom" | "via";
};
export interface HandshakeDevice {
highToLowCallback: undefined | (() => void);
lowToHighCallback: undefined | (() => void);
readByte(): Byte;
writeByte(b: Byte): void;
}
export interface PHI2Listener {
phi2(count: number): void;
}
export class System extends EventEmitter implements AddressBus {
public ram: RAM;
private rom?: ROM;
public cpu: CPU;
public via1: VIA6522;
public via2: VIA6522;
private memoryDevices: MemoryDevice[];
private memMap: Map<Address, MemoryDevice> = new Map();
public breakpoints: Set<Address> = new Set();
private interval?: NodeJS.Timeout;
public pcToInstructionMap: Map<Address, { size: number }> = new Map();
public phi2Listener: PHI2Listener[] = [];
public constructor(ra8875: RA8875, keyboard: Keyboard) {
super();
this.pcToInstructionMap = new Map();
this.ram = new RAM();
this.cpu = new CPU(this);
this.via1 = new VIA6522(this, VIA1_START, new Map([[4, ra8875]]), keyboard);
this.via2 = new VIA6522(this, VIA2_START, new Map());
this.memoryDevices = [];
this.memoryDevices.push({
start: RAM_START,
end: RAM_END,
device: this.ram,
type: "ram",
});
if (this.rom) {
this.memoryDevices.push({
start: ROM_START,
end: 0xffff,
device: this.rom,
type: "rom",
});
}
this.memoryDevices.push({
device: this.via1,
start: VIA1_START,
end: VIA1_END,
type: "via",
});
this.memoryDevices.push({
device: this.via2,
start: VIA2_START,
end: VIA2_END,
type: "via",
});
this.prepareMemoryMap();
}
private prepareMemoryMap() {
this.memMap.clear();
for (let i = 0; i < Math.pow(2, 16); i++) {
const entry = this.memoryDevices.find((p) => p.start <= i && p.end >= i);
if (entry) {
this.memMap.set(i, entry);
}
}
}
public loadRom(rom: ROM) {
this.rom = rom;
this.pcToInstructionMap = new Map();
this.memoryDevices = this.memoryDevices.filter((p) => p.type !== "rom");
this.memoryDevices.push({
start: ROM_START,
end: 0xffff,
device: this.rom,
type: "rom",
});
this.prepareMemoryMap();
}
public reset() {
this.cpu?.reset();
}
public step() {
this.cpu.step();
this.sendEvent("stopOnStep");
}
public start(
rom: ROM,
debug: boolean,
stopOnEntry: boolean,
pcToInstructionMap: Map<Address, { size: number }>
) {
this.loadRom(rom);
this.pcToInstructionMap = pcToInstructionMap;
this.reset();
if (debug) {
// TODO: verify breakpoints
if (stopOnEntry) {
this.sendEvent("stopOnEntry");
} else {
this.run();
}
} else {
this.run();
}
}
public stepOut() {
let depth = 1;
const oneRun = () => {
for (let i = 0; i < 10000; i++) {
const opcode = this.cpu.step();
if (opcode === 0x20 || opcode === 0x22 || opcode === 0xfc) {
depth++;
}
if (opcode === 0x60 || opcode === 0x6b) {
depth--;
}
if (depth === 0) {
this.sendEvent("stopOnStep");
return;
}
}
this.interval = setTimeout(oneRun, 0);
};
this.interval = setTimeout(oneRun, 0);
}
public stepOver() {
const opcode = this.cpu.step();
if (opcode === 0x20 || opcode === 0x22 || opcode === 0xfc) {
this.stepOut();
} else {
this.sendEvent("stopOnStep");
}
}
public run() {
const oneRun = () => {
for (let i = 0; i < 10000; i++) {
this.cpu.step();
if (this.breakpoints.has(addr(this.cpu.PBR.byte, this.cpu.PC.word))) {
clearInterval(this.interval);
this.interval = undefined;
this.sendEvent("stopOnBreakpoint");
return;
}
}
};
this.interval = setInterval(oneRun, 0);
}
public pause() {
if (this.interval) {
clearInterval(this.interval);
this.interval = undefined;
}
this.sendEvent("stopOnStep");
}
public phi2(count: number) {
this.phi2Listener && this.phi2Listener.forEach((l) => l.phi2(count));
}
public read(addr: Address): Byte {
return this.resolveAddress(addr).read(addr);
}
public readWord(addr: Address): Word {
return this.resolveAddress(addr).readWord(addr);
}
public readSesqui(addr: Address): Sesqui {
return this.resolveAddress(addr).readSesqui(addr);
}
public readSlice(ad: Address, length: number): Uint8Array {
const entry = this.memMap.get(ad);
if (entry) {
const possibleEnd = entry.end < ad + length ? entry.end - ad : length;
return entry.device.readSlice(ad, possibleEnd);
}
return new Uint8Array();
}
public write(ad: Address, data: Byte): void {
this.resolveAddress(ad).write(ad, data);
}
public writeWord(ad: Address, data: Word): void {
this.resolveAddress(ad).writeWord(ad, data);
}
public writeSlice(ad: Address, data: Uint8Array) {
this.resolveAddress(ad).writeSlice(ad, data);
}
private resolveAddress(ad: Address): AddressBus {
const entry = this.memMap.get(ad);
if (entry) {
return entry.device;
}
this.sendEvent("stopOnException", new Error("No device found for: " + ad));
throw new Error("No device found for: " + ad);
}
public sendEvent(event: string, ...args: any[]): void {
setTimeout(() => {
this.emit(event, ...args);
}, 0);
}
}