-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.js
141 lines (118 loc) · 3.18 KB
/
utils.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
// **** machine-specific utility functions ****
function cpu_status() {
const state = cpu.getState();
return `A=${hex(state.a)} BC=${hex(state.b)}${hex(state.c)} DE=${hex(state.d)}${hex(state.e)} HL=${hex(state.h)}${hex(state.l)} IX=${hex(state.ix,4)} IY=${hex(state.iy,4)} SP=${hex(state.sp,4)} PC=${hex(state.pc,4)} S=${state.flags.S}, Z=${state.flags.Z}, Y=${state.flags.Y}, H=${state.flags.H}, X=${state.flags.X}, P=${state.flags.P}, N=${state.flags.N}, C=${state.flags.C}`;
}
async function crun(filename) {
await load(filename);
//await print_string("\nrun:\n");
pasteLine("RUN\r\n");
}
function paste(text) {
const lines = text.split("\n");
for(let t=0; t<lines.length; t++) {
const linea = lines[t];
console.log(linea);
pasteLine(linea);
pasteChar(13); // CR
}
}
function pasteLine(line) {
renderFrame();
for(let t=0;t<line.length;t++) {
let c = line.charCodeAt(t);
pasteChar(c);
}
}
function pasteChar(c) {
SIO_receiveChar(c);
}
function zap() {
ram.forEach((e,i)=>ram[i]=0x00);
let state = cpu.getState();
state.halted = true;
cpu.setState(state);
}
function power() {
zap();
setTimeout(()=>cpu.reset(), 200);
}
function saveState() {
const saveObject = {
ram: Array.from(ram),
cpu: cpu.getState()
};
window.localStorage.setItem(`lm80c_emu_state`, JSON.stringify(saveObject));
}
function restoreState() {
try
{
let s = window.localStorage.getItem(`lm80c_emu_state`);
if(s === null) return;
s = JSON.parse(s);
copyArray( s.ram, ram);
cpu.setState(s.cpu);
}
catch(error)
{
}
}
function dumpPointers() {
console.log(`
+------------------------+ <- (0x${hex(PROGND,4)}) ${hex(mem_read_word(PROGND),4)}
| BASIC program |
+------------------------+ <- TXTTAB (0x${hex(BASTXT,4)}) ${hex(mem_read_word(BASTXT),4)}
| System variables |
+------------------------+ 0x8000
`);
}
let debugBefore = undefined;
let debugAfter = undefined;
function dumpStack() {
const sp = cpu.getState().sp;
for(let t=sp;t<=0xffff;t+=2) {
const word = mem_read_word(t);
console.log(`${hex(t,4)}: ${hex(word,4)} (${word})`);
}
}
function make_lm(start, end, rows) {
let s;
s = `10 FOR T=&H${hex(start,4)} TO &H${hex(end,4)}\n`;
s+= `20 READ B:POKE T,B\n`;
s+= `30 NEXT\n`;
s+= `40 SYS &H${hex(start,4)}\n`;
s+= `50 END\n`;
let nline = 1000;
if(rows==undefined) rows=8;
for(let r=start;r<=end;r+=rows) {
s+= `${nline} DATA `;
for(let c=0;c<rows && (r+c)<=end;c++) {
const byte = mem_read(r+c);
s+= `${byte}`;
if(c!=rows-1 && (r+c)!=end) s+=",";
}
s+="\n";
nline += 10;
}
console.log(s);
}
let counter = 0;
let counter_avg = 0;
function start_counter() {
counter = new Date().valueOf();
}
function stop_counter() {
let now = new Date().valueOf();
let cnt = counter;
if(cnt === 0) cnt = now
let elapsed = now - cnt;
counter_avg = 0.9 * counter_avg + 0.1 * elapsed;
return counter_avg;
}
let LED = 0;
function led_read() {
return LED;
}
function led_write(value) {
LED = value;
}