-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCpcVmRsx.js
More file actions
200 lines (159 loc) · 4.81 KB
/
CpcVmRsx.js
File metadata and controls
200 lines (159 loc) · 4.81 KB
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
// CpcVmRsx.js - CPC Virtual Machine: RSX
// (c) Marco Vieth, 2020
// https://benchmarko.github.io/CPCBasic/
//
"use strict";
var Utils;
if (typeof require !== "undefined") {
Utils = require("./Utils.js"); // eslint-disable-line global-require
}
function CpcVmRsx(oVm) {
this.rsxInit(oVm);
}
CpcVmRsx.prototype = {
rsxInit: function (oVm) {
this.oVm = oVm;
this.rsxTemporary = {}; // temporary/dynamic RSX registered at runtime
},
rsxIsAvailable: function (sName) {
return (sName in this.rsxTemporary) || (sName in this); // check temporary first, then permanent
},
rsxRegister: function (oRsxConstructor) {
// Register temporary RSX from constructor
// Constructor should be a function that returns an object with getRsxCommands() method
var oRsxModule, oRsxCommands, sCommand;
oRsxModule = new oRsxConstructor();
if (oRsxModule.getRsxCommands) {
oRsxCommands = oRsxModule.getRsxCommands();
for (sCommand in oRsxCommands) {
if (oRsxCommands.hasOwnProperty(sCommand)) {
this.rsxTemporary[sCommand] = oRsxCommands[sCommand];
}
}
}
},
rsxReset: function () {
// Clear temporary RSX (called when loading new example)
this.rsxTemporary = {};
},
rsxExec: function (sName) { // varargs
var aArgs, fnRsx;
// Check temporary RSX first, then permanent
if (sName in this.rsxTemporary) {
// Temporary RSX: call with this = CpcVm (the VM instance)
fnRsx = this.rsxTemporary[sName];
aArgs = Array.prototype.slice.call(arguments, 1);
fnRsx.apply(this.oVm, aArgs);
} else if (typeof this[sName] === "function") {
// Permanent RSX: call with this = CpcVmRsx
fnRsx = this[sName];
aArgs = Array.prototype.slice.call(arguments, 1);
fnRsx.apply(this, aArgs);
} else {
throw this.oVm.vmComposeError(Error(), 28, "|" + sName); // Unknown command
}
},
a: function () {
this.oVm.vmNotImplemented("|A");
},
b: function () {
this.oVm.vmNotImplemented("|B");
},
basic: function () { // not an AMSDOS command
Utils.console.log("basic: |BASIC");
this.oVm.vmStop("reset", 90);
},
cpm: function () {
this.oVm.vmNotImplemented("|CPM");
},
fnGetVariableByAddress: function (iIndex) {
return this.oVm.oVariables.getVariableByIndex(iIndex) || "";
},
dir: function (sFileMask) { // optional; string or addressOf number
var iStream = 0;
if (typeof sFileMask === "number") { // assuming addressOf
sFileMask = this.fnGetVariableByAddress(sFileMask);
} else if (typeof sFileMask !== "string") {
sFileMask = ""; // default
}
if (sFileMask) {
sFileMask = this.oVm.vmAdaptFilename(sFileMask, "|DIR");
}
this.oVm.vmStop("fileDir", 45, false, {
iStream: iStream,
sCommand: "|dir",
sFileMask: sFileMask
});
},
disc: function () {
this.oVm.vmNotImplemented("|DISC");
},
disc_in: function () { // eslint-disable-line camelcase
this.oVm.vmNotImplemented("|DISC.IN");
},
disc_out: function () { // eslint-disable-line camelcase
this.oVm.vmNotImplemented("|DISC.OUT");
},
drive: function () {
this.oVm.vmNotImplemented("|DRIVE");
},
era: function (sFileMask) {
var iStream = 0;
if (typeof sFileMask === "number") { // assuming addressOf
sFileMask = this.fnGetVariableByAddress(sFileMask);
}
sFileMask = this.oVm.vmAdaptFilename(sFileMask, "|ERA");
this.oVm.vmStop("fileEra", 45, false, {
iStream: iStream,
sCommand: "|era",
sFileMask: sFileMask
});
},
ren: function (sNew, sOld) {
var iStream = 0;
if (typeof sNew === "number") { // assuming addressOf
sNew = this.fnGetVariableByAddress(sNew);
}
if (typeof sOld === "number") { // assuming addressOf
sOld = this.fnGetVariableByAddress(sOld);
}
sNew = this.oVm.vmAdaptFilename(sNew, "|REN");
sOld = this.oVm.vmAdaptFilename(sOld, "|REN");
this.oVm.vmStop("fileRen", 45, false, {
iStream: iStream,
sCommand: "|ren",
sNew: sNew,
sOld: sOld
});
},
tape: function () {
this.oVm.vmNotImplemented("|TAPE");
},
tape_in: function () { // eslint-disable-line camelcase
this.oVm.vmNotImplemented("|TAPE.IN");
},
tape_out: function () { // eslint-disable-line camelcase
this.oVm.vmNotImplemented("|TAPE.OUT");
},
user: function () {
this.oVm.vmNotImplemented("|USER");
},
mode: function (iMode, s) {
var oWinData, i, oWin;
iMode = this.oVm.vmInRangeRound(iMode, 0, 3, "|MODE");
this.oVm.iMode = iMode;
oWinData = this.oVm.mWinData[this.oVm.iMode];
Utils.console.log("rsxMode: (test)", iMode, s);
for (i = 0; i < this.oVm.iStreamCount - 2; i += 1) { // for window streams
oWin = this.oVm.aWindow[i];
Object.assign(oWin, oWinData);
}
this.oVm.oCanvas.changeMode(iMode); // or setMode?
},
renum: function () { // optional args: new number, old number, step, keep line (only for |renum)
this.oVm.renum.apply(this.oVm, arguments);
}
};
if (typeof module !== "undefined" && module.exports) {
module.exports = CpcVmRsx;
}