forked from nippur72/gpmodelt-emu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
x.js
452 lines (382 loc) · 12.1 KB
/
x.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
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
// find call addresses
let calls = {};
debugBefore = ()=> {
const pc = cpu.getState().pc;
if(mem_read(pc) == 0xCD) {
// it's call
const address = mem_read_word(pc+1);
if(calls[address] === undefined) {
console.log(`${hex(pc,4)} CALL ${hex(address,4)}`);
calls[address] = true;
}
}
}
// count t-states
let ct = 0;
debugBefore = ()=> {
const pc = cpu.getState().pc;
if(pc === 0x0282) ct = cycles;
if(pc === 0x028b) console.log(`${cycles-ct}`);
}
// measure turbo tape bit length and threshold
(function() {
let hits = {};
let hv = [];
let counter = 0;
let values = [];
let record = false;
debugBefore = ()=> {
const { a, pc } = cpu.getState();
// start recording after returning from "CALL sync_tape"
if(pc === 0x8917) record = true;
// log the value in A register before "CP THRESHOLD"
if(pc === 0x8971) {
if(record) {
counter++;
values[counter] = a;
const index = `L${a}`;
hits[index] = (hits[index] || 0) + 1;
hv[a] = (hv[a] || 0) + 1;
// if(counter % 512 === 0) console.log(hits);
}
}
// final print after loading file, before "ld (0x83e9), de"
if(pc === 0x8956) {
hv.forEach((e,i)=>{ console.log(`${i}: ${Math.round((e/counter)*100)}`)});
record = false;
hv = [];
counter = 0;
}
}
let print = function() {
console.log(values.join("\n"));
};
return print;
})();
// turbo tape sync header
let _counter = 0;
debugBefore = ()=> {
const { pc, d, e, a } = cpu.getState();
if(pc === 0x8971) {
_counter++;
const de = e+d*256;
if(_counter < 1024) console.log(`${hex(de,4)} (${_counter})`);
//if(_counter < 1024) console.log(`${bin(de,16)} (${_counter})`);
//if(_counter < 1024) console.log(`${hex(a)} ${_counter}`);
}
}
// logs when writing in mem at specific address
function hooked_mem_write(address, value) {
mem_write(address, value);
if(address === 0x4000) {
console.log(`writing in mem ${hex(address,4)}: ${hex(value)} from pc=${hex(cpu.getState().pc,4)}`);
}
}
cpu = new Z80({ mem_read, mem_write: hooked_mem_write, io_read, io_write });
// logs when RST 30 is called
debugBefore = (function() {
let lastpc = 0;
return function() {
if(lastpc === 0x800f) {
// there was a call to RST 30
console.log(cpu_status());
}
lastpc = cpu.getState().pc;
};
})();
// logs when PC = BA00h (CPM entry)
debugBefore = (function() {
let lastpc = 0;
return function() {
if(lastpc === 0xBA00) {
// there was a call to RST 30
console.log(`CP/M entry point reached`);
}
lastpc = cpu.getState().pc;
};
})();
/*
// *******************************************************************************
// input test signal
let phase = 0;
let tone = 748.7;
function cloadAudioSamples(n) {
tapeHighPtr += (n*tapeSampleRate);
if(tapeHighPtr >= cpuSpeed) {
tapeHighPtr-=cpuSpeed;
const audio = Math.sin(phase) * 0.75;
phase += (2 * Math.PI * tone / 44100);
phase = phase % (2 * Math.PI);
cassette_bit_in = audio > 0 ? 1 : 0;
}
}
*/
// generate tone burst vsync
//let tone = 49.62 * 60;
let tone = 49.556 * 80
let freq = tone;
let duty = 0.5;
let seq = [1, 1, 0, 1, 1, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1];
(function() {
let phase = 0;
let silent = false;
let seqp = 0;
speakerSound.onaudioprocess = function(e) {
const output = e.outputBuffer.getChannelData(0);
// playback what is in the audio buffer
for(let i=0; i<bufferSize; i++) {
//const audio = Math.sin(phase) * 0.75;
const audio = phase <= (duty * 2 * Math.PI) ? 0.75 : -0.75;
output[i] = silent ? -0.75 : audio;
phase += (2 * Math.PI * freq / 48000);
if(phase > 2 * Math.PI) {
//const bitm = Math.random()< 0.5 ? 1 : 2;
const b = seq[(seqp++)% seq.length];
if(b === 0) { silent = false; freq = tone; }
else if(b === 1) { silent = false; freq = tone * 2; }
else if(b === -1) { silent = true; freq = tone*2; }
}
phase = phase % (2 * Math.PI);
}
}
})();
//***************************************************
// generate random bits sync with raster
let tone = 500.78;
let duty = 0.5;
let inv = false;
(function() {
let phase = 0;
let freq = tone;
let cnt = 0;
let scnt = 0;
speakerSound.onaudioprocess = function(e) {
const output = e.outputBuffer.getChannelData(0);
for(let i=0; i<bufferSize; i++) {
const audio = phase <= (duty * 2 * Math.PI) ? 0.75 : -0.75;
output[i] = inv ? audio : -audio;
phase += (2 * Math.PI * freq / 48000);
if(phase > 2 * Math.PI) {
const bitm = Math.random()< 0.5 ? 1 : 4;
if(cnt === 0 ) freq = tone / 2;
else if(cnt === 1 ) freq = tone / 2;
else freq = tone;
}
phase = phase % (2 * Math.PI);
scnt++;
if(scnt > 48000 / 500.78)
{
scnt -= 48000 / 500.78;
cnt++;
cnt = cnt % 10;
}
}
}
})();
//***************************************************
// generate random bits square wave with duty cycle
let tone = 500.78;
let duty = 0.5;
let inv = false;
(function() {
let phase = 0;
let freq = tone;
speakerSound.onaudioprocess = function(e) {
const output = e.outputBuffer.getChannelData(0);
for(let i=0; i<bufferSize; i++) {
const audio = phase <= (duty * 2 * Math.PI) ? 0.75 : -0.75;
output[i] = inv ? audio : -audio;
phase += (2 * Math.PI * freq / 48000);
if(phase > 2 * Math.PI) {
const bitm = Math.random()< 0.5 ? 1 : 4;
freq = tone * bitm;
}
phase = phase % (2 * Math.PI);
}
}
})();
//***************************************************
// generate pure square wave with duty cycle
let freq = 501.25;
let duty = 0.5;
let inv = false;
(function() {
let phase = 0;
speakerSound.onaudioprocess = function(e) {
const output = e.outputBuffer.getChannelData(0);
for(let i=0; i<bufferSize; i++) {
const audio = phase <= (duty * 2 * Math.PI) ? 0.75 : -0.75;
output[i] = inv ? audio : -audio;
phase += (2 * Math.PI * freq / 48000);
phase = phase % (2 * Math.PI);
}
}
})();
// logs when unknown io bits are changed
(function() {
let old_io_bit_7;
let old_caps_lock_bit;
let old_io_bit_4;
debugBefore = ()=> {
old_io_bit_7 = io_bit_7;
old_caps_lock_bit = caps_lock_bit;
old_io_bit_4 = io_bit_4;
}
debugAfter = ()=> {
let { pc } = cpu.getState();
if(old_io_bit_7 != io_bit_7) console.log(`bit 7 changed from ${old_io_bit_7 } to ${io_bit_7 } at ${hex(pc,4)}`);
if(old_caps_lock_bit != caps_lock_bit) console.log(`bit 6 changed from ${old_caps_lock_bit} to ${caps_lock_bit} at ${hex(pc,4)}`);
if(old_io_bit_4 != io_bit_4) console.log(`bit 4 changed from ${old_io_bit_4 } to ${io_bit_4 } at ${hex(pc,4)}`);
}
})();
// installs debug on RST 38
(function() {
// writes a RET at 0038h
mem_write(0x0038, 0xc9);
// install debug function
debugBefore = (function() {
let previous_pc = 0;
return function() {
const pc = cpu.getState().pc;
if(pc === 0x0038) {
// there was a call to RST 38
console.log(`RST 38h called from ${hex(previous_pc, 4)}`);
console.log(cpu_status());
}
};
})();
console.log("debug RST 38h installed");
})();
// breakpoints debugger
let brk = [];
(function() {
// install debug function
debugBefore = (function() {
return function() {
const pc = cpu.getState().pc;
if(brk[pc] === true) {
console.log(cpu_status());
}
};
})();
console.log("breakpoints debugger installed");
})();
// installs debug on RST 38
(function() {
// writes a RET at 0038h
mem_write(0x0038, 0xc9);
// install debug function
debugBefore = (function() {
let previous_pc = 0;
return function() {
const pc = cpu.getState().pc;
if(pc === 0x0038) {
// there was a call to RST 38
console.log(`RST 38h called from ${hex(previous_pc, 4)}`);
console.log(cpu_status());
}
};
})();
console.log("debug RST 38h installed");
})();
// breakpoints debugger
(function() {
// install debug function
debugBefore = (function() {
return function() {
const { pc, sp } = cpu.getState();
if(pc >= 0x6000 && pc <= 0x7fff) {
console.log(`${hex(pc,4)} sp ${hex(sp,4)}`);
}
};
})();
console.log("breakpoints debugger installed");
})();
// breakpoints debugger
(function() {
// install debug function
debugBefore = (function() {
return function() {
const { pc, sp, h, l } = cpu.getState();
if(pc == 0x0100) {
console.log(`we are at ${hex(pc,4)}`);
}
};
})();
console.log("breakpoints debugger installed");
})();
// fill video memory
(function() {
for(let t=0xC000;t<0xCFFF;t++) mem_write(t, t & 0xFF);
})();
function bitmapChars(chars) {
let w = [];
for(let t=0; t<chars.length; t++) {
let b = chars[t];
let s = [];
for(i=7; i>=0; i--) {
if((b & (1<<i))) s.push(" ");
else s.push("X");
}
w.push(s.join(""));
}
console.log(w.join("\n"));
}
/*
istruzioni per copiare da i8080 emulator:
let z = new Emulator()
// paste saveAs() routine
let ccc = z.memio.ram.slice(0x880+256,0x880+256+34*256);
let blob = new Blob([new Uint8Array(ccc)], {type: "application/octet-stream"});
saveAs(blob, "cpm48.com");
*/
function pasteLong(str) {
let lines = str.split("\n");
str.forEach(line=>paste(line));
}
// finds if certain memory location are visited in read or write
let debug_addr = new Uint8Array(65536).fill(0);
function mark_addr(from,to) {
for(let t=from;t<=to;t++) debug_addr[t] = 0xFF;
}
function mem_read(address) {
if(debug_addr[address]===0xFF) console.warn(`reading from ${hex(address,4)}h pc=${hex(cpu.getState().pc,4)}h`);
return memory[address];
}
function mem_write(address, value) {
// TODO replicate video memory pages
if(debug_addr[address]==0xFF) console.warn(`writing to ${hex(address,4)}h value ${hex(value)}h pc=${hex(cpu.getState().pc,4)}h`);
if(address <= 0xCFFF) memory[address] = value;
else console.warn(`ROM write at address ${hex(address,4)}h value ${hex(value)}h pc=${hex(cpu.getState().pc,4)}h`);
}
mark_addr(0xE005,0xE024);
mark_addr(0xEAD1,0xEBFF);
// logs when
paste(`MBASIC\r\n\r\nprint chr$(22)"00"\r\n\r\nSYSTEM\r\n\r\n`); poly88 = true;
// *************************************************************************************
// patches T20 CP/M for additional drive C:
for(let t=0; t<16; t++) mem_write(0xba8b+t, mem_read(0xba6b+t));
mem_write_word(0xba7b-2, 0xBD36+31);
dumpMem(0xba8b,0xba8b+16);
memory[0xE663] = 42;
// *************************************************************************************
// count t-states
let ct = 0;
debugBefore = ()=> {
const pc = cpu.getState().pc;
if(pc === 0x026a) {
console.log(`${cycles-ct}`);
ct = cycles;
}
}
// *************************************************************************************
// bold font charset
(function() {
function rev(e) {
return ~(~e | ~(e<<1)) | 1;
}
rom_GCE_M1_U3.forEach((e,i)=>rom_GCE_M1_U3[i]=rev(e));
rom_GCE_M2_U4.forEach((e,i)=>rom_GCE_M2_U4[i]=rev(e));
rom_GCAR_A_U7.forEach((e,i)=>rom_GCAR_A_U7[i]=rev(e));
rom_GCAR_B_U8.forEach((e,i)=>rom_GCAR_B_U8[i]=rev(e));
})();