-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzx48k.cpp
More file actions
396 lines (363 loc) · 8.18 KB
/
zx48k.cpp
File metadata and controls
396 lines (363 loc) · 8.18 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
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
/*
* zx48k.cpp
*
* Created on: 20 mar 2016
* Author: wpk
*/
#include <strings.h>
#include <iostream>
#include <fstream>
#include <ctime>
#include <cassert>
#include <unistd.h> // TODO remove (usleep)
#include <boost/program_options.hpp>
#include "zx48k.h"
using namespace std;
namespace po = boost::program_options;
constexpr int zx48k::MEMORY_SIZE;
namespace {
constexpr int COLS = 352;
constexpr int LINES = 296;
constexpr int TOP_BORDER_LINES = 48;
constexpr int BOTTOM_BORDER_LINES = 56;
constexpr int LEFT_BORDER_COLS = 48;
constexpr int RIGHT_BORDER_COLS = 48;
constexpr uint32_t zxpalette[16] = {
0x00000000,
0x000000CD,
0x00CD0000,
0x00CD00CD,
0x0000CD00,
0x0000CDCD,
0x00CDCD00,
0x00CDCDCD,
0x00000000,
0x000000FF,
0x00FF0000,
0x00FF00FF,
0x0000FF00,
0x0000FFFF,
0x00FFFF00,
0x00FFFFFF };
}
zx48k::zx48k() :
EmuSDL(COLS, LINES, 2, 2),
cpu(*this),
romfile("testrom.bin")
{}
void zx48k::initialize() {
memset(memory, 0xaa, MEMORY_SIZE);
std::ifstream fin(romfile, std::ios::in | std::ios::binary);
if (fin.fail()) {
throw ifstream::failure("Can't open ROM file");
}
fin.seekg(0, std::ios_base::end);
uint64_t len = fin.tellg();
fin.seekg(0);
fin.read((char*) memory,len);
fin.close();
border = 7;
if (tapfile != "") {
tape = new zxtape(tapfile);
}
}
void zx48k::parse_opts(int argc, char ** argv) {
po::options_description desc("Allowed options");
desc.add_options()
("help", "display this message help message")
("rom", po::value<string>(), "ROM file to use")
("tap", po::value<string>(), "Tape to load")
("trace", po::bool_switch()->default_value(false), "Enable tracing")
;
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, desc), vm);
po::notify(vm);
if (vm.count("help")) {
cout << desc << "\n";
exit(1);
}
if (vm.count("rom")) {
romfile = vm["rom"].as<string>();
}
if (vm.count("tap")) {
tapfile = vm["tap"].as<string>();
}
if (vm["trace"].as<bool>()) {
trace = true;
}
}
uint32_t zx48k::readmem(uint16_t address) {
if (trace) {
cout << "READMEM " << std::hex << (int) address << " " << (int) (memory[address] & 0xff) << endl;
}
return * ((uint32_t*) (memory + address));
return memory[address] + (memory[address+1] << 8) + (memory[address+2] << 16) + (memory[address + 3] << 24);
}
void zx48k::writemem(uint16_t address, uint8_t v) {
assert(address < MEMORY_SIZE);
if (address >= 0x4000) {
memory[address] = v;
if (trace) {
cout << "WRITEMEM " << std::hex << (int) address << " " << (int) v << endl;
}
}
}
static uint8_t sdlkey2spectrum(SDL_Keycode k) {
switch (k) {
case SDLK_LSHIFT:
return 0 << 3 | 0;
case SDLK_z:
return 0 << 3 | 1;
case SDLK_x:
return 0 << 3 | 2;
case SDLK_c:
return 0 << 3 | 3;
case SDLK_v:
return 0 << 3 | 4;
case SDLK_a:
return 1 << 3 | 0;
case SDLK_s:
return 1 << 3 | 1;
case SDLK_d:
return 1 << 3 | 2;
case SDLK_f:
return 1 << 3 | 3;
case SDLK_g:
return 1 << 3 | 4;
case SDLK_q:
return 2 << 3 | 0;
case SDLK_w:
return 2 << 3 | 1;
case SDLK_e:
return 2 << 3 | 2;
case SDLK_r:
return 2 << 3 | 3;
case SDLK_t:
return 2 << 3 | 4;
case SDLK_1:
return 3 << 3 | 0;
case SDLK_2:
return 3 << 3 | 1;
case SDLK_3:
return 3 << 3 | 2;
case SDLK_4:
return 3 << 3 | 3;
case SDLK_5:
return 3 << 3 | 4;
case SDLK_0:
return 4 << 3 | 0;
case SDLK_9:
return 4 << 3 | 1;
case SDLK_8:
return 4 << 3 | 2;
case SDLK_7:
return 4 << 3 | 3;
case SDLK_6:
return 4 << 3 | 4;
case SDLK_p:
return 5 << 3 | 0;
case SDLK_o:
return 5 << 3 | 1;
case SDLK_i:
return 5 << 3 | 2;
case SDLK_u:
return 5 << 3 | 3;
case SDLK_y:
return 5 << 3 | 4;
case SDLK_RETURN:
return 6 << 3 | 0;
case SDLK_l:
return 6 << 3 | 1;
case SDLK_k:
return 6 << 3 | 2;
case SDLK_j:
return 6 << 3 | 3;
case SDLK_h:
return 6 << 3 | 4;
case SDLK_SPACE:
return 7 << 3 | 0;
case SDLK_RSHIFT:
return 7 << 3 | 1;
case SDLK_m:
return 7 << 3 | 2;
case SDLK_n:
return 7 << 3 | 3;
case SDLK_b:
return 7 << 3 | 4;
default:
return 0xff;
}
}
void zx48k::writeio(uint16_t address, uint8_t v) {
if ((address & 0x1) == 0) { // ULA, might be & 0xff == 0xfe
ear = v & 0x10;
mic = v & 0x08;
border = v & 0x07;
} else if ((address & 0x8002) == 0x8000) { //ay
cout << "WRITEAY" << "\n";
} else {
// cout << "WRITEIO " << std::hex << (int) address << " " << (int) v << "\n";
}
}
uint8_t zx48k::readio(uint16_t address) {
uint8_t out = 0;
if ((address & 0xff) == 0xfe) { // keyboard routines
out = 0xbf;
uint8_t lines = ~(address >> 8);
for (auto sdlkey: get_keys()) {
uint8_t key = sdlkey2spectrum(sdlkey);
if (key != 0xff) {
uint8_t kline = 1 << (key >> 3);
uint8_t kaddr = 1 << (key & 0b111);
if (kline & lines) {
out &= ~(kaddr); // drop the line pressed
}
}
}
if (tape && tape->ear()) {
out |= 1<<6;
}
}
return out;
}
void zx48k::scanline(int y) {
if (y >= LINES) { // 16 empty scanlines at the end
return;
} else if ((y < 48) || (y >= 240) ) {
// TODO unloopize it
for (int x=0; x<352; x++) {
uint32_t pixel = COLS * y + x;
pixels[pixel] = zxpalette[border];
}
} else {
for (int x = 0; x < 48; x++ ) {
uint32_t pixel = COLS * y + x;
pixels[pixel] = zxpalette[border];
}
for (int xx = 0; xx < 32; xx++) {
int yy = y - TOP_BORDER_LINES;
// 0 1 0 y7 y6 y2 y1 y0 y5 y4 y3 x4 x3 x2 x1 x0
uint16_t addr = (1 << 14) | xx |
(((yy >> 3) & 0x7) << 5) |
((yy&7) << 8) |
((yy>>6) << 11);
uint8_t attrs = memory[0x5800 + xx + 32*(yy/8)];
uint8_t ink = attrs & 0x7;
uint8_t paper = (attrs >> 3) & 0x7;
if (attrs & 0b01000000) { // 'bright'
ink |= 0x8;
paper |= 0x8;
}
if ((attrs & 0b10000000) && flashstate) {
ink ^= paper;
paper ^= ink;
ink ^= paper;
}
for (int i = 0; i < 8; i++) {
uint32_t pixel = COLS * y + 48 + 8*xx + i;
bool lit = memory[addr] & (1 << (7-i));
pixels[pixel] = zxpalette[lit ? ink : paper];
}
}
// TODO unloopize it
for (int x = 304; x < 352; x++) {
uint32_t pixel = COLS * y + x;
pixels[pixel] = zxpalette[border];
}
}
}
void zx48k::run() {
uint64_t lastcycles = 0;
int line = 0;
int frame = 0;
uint64_t v_diff = 0;
uint64_t d_diff = 0;
int64_t acc_delay = 0;
struct timespec tv_s, tv_e;
clock_gettime(CLOCK_MONOTONIC, &tv_s);
for (;;) {
if (trace) {
std::cout << cpu.get_trace();
}
uint64_t cycles = cpu.tick();
uint64_t diff = cycles - lastcycles;
lastcycles = cycles;
if (tape) {
tape->update_ticks(diff);
}
v_diff += diff;
d_diff += diff;
// draw lines that should be drawn now.
while (v_diff > 224) {
scanline(line++);
if (line >= 312) {
line = 0;
// process input
readinput();
if (key_pressed(SDLK_F5)) {
trace = true;
}
if (key_pressed(SDLK_F6)) {
trace = false;
}
if (key_pressed(SDLK_F7) && tape) {
tape->reset();
tape->go();
}
if (key_pressed(SDLK_F8)) {
if (!debounce) {
turbo = !turbo;
debounce = true;
cout << string("Turbo mode ") << (turbo ? "on" : "off") << std::endl;
}
} else {
debounce = false;
}
if (key_pressed(SDLK_F4)) {
cout << "Quitting..." << std::endl;
return;
}
cpu.interrupt();
if (!turbo) {
redrawscreen();
}
if (++frame == 16) {
flashstate = !flashstate;
frame = 0;
if (turbo) {
redrawscreen();
}
}
}
v_diff-=224;
}
// correct timing:
// 'd_diff' cycles should take us d_diff * 285ns, if we're too late - too bad
// if we're too soon - wait
// do it only if d_diff > 10000 to save time on unnecessary clock_gettimes
// TODO move it into emusdl
if (d_diff > 10000) {
clock_gettime(CLOCK_MONOTONIC, &tv_e);
uint64_t real_time = (tv_e.tv_sec - tv_s.tv_sec) * 1000000000 +
tv_e.tv_nsec - tv_s.tv_nsec;
uint64_t handl_time = d_diff * 285; // 3.5MHz == 1/285ns
d_diff = 0;
acc_delay += handl_time - real_time;
if (acc_delay < 0 || turbo) {
acc_delay = 0;
}
if (acc_delay > 1000000) {
uint64_t c = acc_delay/1000000;
acc_delay -= c*1000000;
SDL_Delay(c);
}
clock_gettime(CLOCK_MONOTONIC, &tv_s);
}
}
}
int main(int argc, char ** argv) {
zx48k * emu = new zx48k();
emu->parse_opts(argc, argv);
emu->initialize();
emu->run();
}