-
Notifications
You must be signed in to change notification settings - Fork 3
/
z80machinecode.js
143 lines (112 loc) · 3.27 KB
/
z80machinecode.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
var _getIterator = require('babel-runtime/core-js/get-iterator')['default'];
var _interopRequireDefault = require('babel-runtime/helpers/interop-require-default')['default'];
Object.defineProperty(exports, '__esModule', {
value: true
});
exports.loadByteIntoRegister = loadByteIntoRegister;
exports.callRoutine = callRoutine;
exports.printString = printString;
var _toBinary = require('to-binary');
var _toBinary2 = _interopRequireDefault(_toBinary);
var _fs = require('fs');
var bits = function bits(str) {
return parseInt(str, 2);
};
exports.bits = bits;
var lg = console.log;
exports.lg = lg;
var accumulator = '111';
exports.accumulator = accumulator;
var registerA = '111';
exports.registerA = registerA;
var registerB = '000';
exports.registerB = registerB;
var B = '000';
exports.B = B;
var printCharacter = '010';
exports.printCharacter = printCharacter;
var screen = 2;
exports.screen = screen;
var blueBorder = 1;
exports.blueBorder = blueBorder;
var setBorderColor = 8859;
exports.setBorderColor = setBorderColor;
var selectChannel = 0x1601;
exports.selectChannel = selectChannel;
var outputBytes = [];
exports.outputBytes = outputBytes;
var binOut = function binOut(data) {
return outputBytes.push(data);
};
exports.binOut = binOut;
var toBuff = function toBuff() {
return new Buffer(outputBytes);
};
exports.toBuff = toBuff;
var saveBin = function saveBin(fname) {
return (0, _fs.writeFileSync)(fname, toBuff());
};
exports.saveBin = saveBin;
// refer to Z80 CPU User Manual (UM008008-0116)
// available on zilog.com
// LD r, n page 69
function loadByteIntoRegister(_ref) {
var data = _ref.data;
var register = _ref.register;
binOut(bits('00' + register + '110'));
binOut(data);
}
// CALL nn page 279
function callRoutine(_ref2) {
var address = _ref2.address;
binOut(bits('11001101'));
binOut(address & 0x00FF);
binOut((address & 0xFF00) >> 8);
}
// RST p page 290
var callSystem = function callSystem(_ref3) {
var routine = _ref3.routine;
return binOut(bits('11' + routine + '111'));
};
exports.callSystem = callSystem;
var restart = callSystem;
exports.restart = restart;
// RET page 283
var return_ = function return_() {
return binOut(bits('11001001'));
};
exports.return_ = return_;
function printString(str) {
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = _getIterator(str), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var ch = _step.value;
loadByteIntoRegister({ data: ch.charCodeAt(), register: accumulator });
callSystem({ routine: printCharacter });
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator['return']) {
_iterator['return']();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
}
loadByteIntoRegister({ data: blueBorder, register: accumulator });
callRoutine({ address: setBorderColor });
loadByteIntoRegister({ data: screen, register: accumulator });
callRoutine({ address: selectChannel });
printString("Hello to Speccy from JavaScript.");
return_();
lg('Saving to tst.bin:');
lg(toBuff());
saveBin('tst.bin');