-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathzx.h
786 lines (640 loc) · 25.5 KB
/
zx.h
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
/* ZX Spectrum Emulator.
https://github.com/kosarev/zx
Copyright (C) 2017-2021 Ivan Kosarev.
Published under the MIT license.
*/
#include <algorithm>
#include "z80/z80.h"
namespace zx {
using z80::fast_u8;
using z80::fast_u16;
using z80::fast_u32;
using z80::least_u8;
using z80::least_u16;
using z80::unreachable;
using z80::make16;
using z80::mask16;
using z80::inc16;
using z80::dec16;
template<typename T>
T non_constexpr() {
return T();
}
template<typename T>
constexpr T div_exact(T a, T b) {
return a % b == 0 ? a / b : non_constexpr<T>();
}
template<typename T>
constexpr T div_ceil(T a, T b) {
return (a + b - 1) / b;
}
template<typename T>
constexpr bool is_multiple_of(T a, T b) {
return b != 0 && a % b == 0;
}
template<typename T>
constexpr bool round_up(T a, T b) {
return div_ceil(a, b) * b;
}
typedef fast_u32 events_mask;
const events_mask no_events = 0;
const events_mask machine_stopped = 1u << 0; // TODO: Eliminate.
const events_mask end_of_frame = 1u << 1;
const events_mask ticks_limit_hit = 1u << 2;
const events_mask fetches_limit_hit = 1u << 3;
const events_mask breakpoint_hit = 1u << 4;
const events_mask custom_event = 1u << 31;
typedef fast_u8 memory_marks;
const memory_marks no_marks = 0;
const memory_marks breakpoint_mark = 1u << 0;
const memory_marks visited_instr_mark = 1u << 7;
const unsigned memory_image_size = 0x10000; // 64K bytes.
typedef least_u8 memory_image_type[memory_image_size];
class disassembler : public z80::z80_disasm<disassembler> {
public:
typedef z80::z80_disasm<disassembler> base;
disassembler(fast_u16 addr, const memory_image_type &memory)
: addr(addr), memory(memory)
{}
void on_emit(const char *out) {
std::snprintf(output_buff, max_output_buff_size, "%s", out);
}
fast_u8 on_read_next_byte() {
fast_u8 n = memory[mask16(addr)];
addr = inc16(addr);
return n;
}
fast_u16 on_get_last_read_addr() const {
return dec16(addr);
}
const char *on_disassemble() {
// Skip prefixes.
base::on_disassemble();
while(get_iregp_kind() != z80::iregp::hl)
base::on_disassemble();
return output_buff;
}
private:
fast_u16 addr;
const memory_image_type &memory;
static const std::size_t max_output_buff_size = 32;
char output_buff[max_output_buff_size];
};
template<typename D>
class spectrum48 : public z80::z80_cpu<D> {
public:
typedef z80::z80_cpu<D> base;
typedef fast_u32 ticks_type;
spectrum48() {
uint_fast32_t rnd = 0xde347a01;
for(auto &cell : self().on_get_memory()) {
cell = static_cast<least_u8>(rnd);
rnd = (rnd * 0x74392cef) ^ (rnd >> 16);
}
}
events_mask get_events() const { return events; }
void stop() { events |= machine_stopped; }
void on_tick(unsigned t) {
ticks_since_int += t;
// Handle stopping by hitting a specified number of ticks.
if(ticks_to_stop) {
if(ticks_to_stop > t) {
ticks_to_stop -= t;
} else {
ticks_to_stop = 0;
events |= ticks_limit_hit;
}
}
}
ticks_type get_ticks() const { return ticks_since_int; }
void set_memory_byte(fast_u16 addr, fast_u8 n) {
assert(addr < memory_image_size);
self().on_get_memory()[addr] = static_cast<least_u8>(n);
}
fast_u8 on_read(fast_u16 addr) {
assert(addr < memory_image_size);
return self().on_get_memory()[addr];
}
void on_write(fast_u16 addr, fast_u8 n) {
// Do not alter ROM.
if(addr >= 0x4000)
set_memory_byte(addr, n);
}
void handle_contention() {
// TODO: We sample ~INT during the last tick of the
// previous instruction, so we add 1 to the contention
// base to compensate that.
const ticks_type cont_base = 14335 + 1;
if(ticks_since_int < cont_base)
return;
if(ticks_since_int >= cont_base + screen_height * ticks_per_line)
return;
ticks_type ticks_since_new_line =
(ticks_since_int - cont_base) % ticks_per_line;
const unsigned pixels_per_tick = 2;
if(ticks_since_new_line >= screen_width / pixels_per_tick)
return;
unsigned ticks_since_new_ula_cycle = ticks_since_new_line % 8;
unsigned delay = ticks_since_new_ula_cycle == 7 ?
0 : 6 - ticks_since_new_ula_cycle;
on_tick(delay);
}
void handle_memory_contention(fast_u16 addr) {
if(addr >= 0x4000 && addr < 0x8000)
handle_contention();
}
fast_u8 on_fetch_cycle() {
handle_memory_contention(self().get_pc());
return base::on_fetch_cycle();
}
fast_u8 on_m1_fetch_cycle() {
// Handle stopping by hitting a specified number of fetches.
// TODO: Rename fetches_to_stop -> m1_fetches_to_stop.
if(fetches_to_stop && --fetches_to_stop == 0)
events |= fetches_limit_hit;
return base::on_m1_fetch_cycle();
}
#if 0 // TODO
fast_u8 on_m1_fetch_cycle() {
fast_u8 n = self().on_fetch_cycle();
self().on_inc_r_reg();
return n;
}
#endif
fast_u8 on_read_cycle(fast_u16 addr) {
handle_memory_contention(addr);
return base::on_read_cycle(addr);
}
void on_write_cycle(fast_u16 addr, fast_u8 n) {
// TODO: Render to (current_tick + 1) and then update
// the byte as the new value is sampled at
// the 2nd tick of the output cycle.
// TODO: The "+ 1" thing is still wrong as there may
// be contentions in the middle.
render_screen_to_tick(get_ticks() + 1);
handle_memory_contention(addr);
base::on_write_cycle(addr, n);
}
void handle_contention_tick() {
handle_memory_contention(addr_bus_value);
on_tick(1);
}
void on_read_cycle_extra_1t() {
handle_contention_tick();
}
void on_read_cycle_extra_2t() {
handle_contention_tick();
handle_contention_tick();
}
void on_write_cycle_extra_2t() {
handle_contention_tick();
handle_contention_tick();
}
void handle_port_contention(fast_u16 addr) {
if(addr < 0x4000 || addr >= 0x8000) {
if((addr & 1) == 0) {
on_tick(1);
handle_contention();
on_tick(3);
} else {
on_tick(4);
}
} else {
if((addr & 1) == 0) {
handle_contention();
on_tick(1);
handle_contention();
on_tick(3);
} else {
handle_contention();
on_tick(1);
handle_contention();
on_tick(1);
handle_contention();
on_tick(1);
handle_contention();
on_tick(1);
}
}
}
fast_u8 on_input_cycle(fast_u16 addr) {
handle_port_contention(addr);
fast_u8 n = self().on_input(addr);
if(FILE *trace = get_trace_file()) {
std::fprintf(trace, "read_port %04x %02x\n",
static_cast<unsigned>(addr),
static_cast<unsigned>(n));
std::fflush(trace);
}
return n;
}
bool is_marked_addr(fast_u16 addr, memory_marks marks) const {
return (memory_marks[mask16(addr)] & marks) != 0;
}
bool is_breakpoint_addr(fast_u16 addr) const {
return is_marked_addr(addr, breakpoint_mark);
}
void mark_addr(fast_u16 addr, memory_marks marks) {
addr = mask16(addr);
memory_marks[addr] = static_cast<least_u8>(memory_marks[addr] | marks);
}
void mark_addrs(fast_u16 addr, fast_u16 size, memory_marks marks) {
for(fast_u16 i = 0; i != size; ++i)
mark_addr(addr + i, marks);
}
void on_set_pc(fast_u16 pc) {
// Catch breakpoints.
if(is_breakpoint_addr(pc))
events |= breakpoint_hit;
base::on_set_pc(pc);
}
fast_u8 on_input(fast_u16 addr) {
z80::unused(addr);
return 0xbf;
}
void on_output_cycle(fast_u16 addr, fast_u8 n) {
if((addr & 0xff) == 0xfe) {
// TODO: Render to (current_tick + 1) and then update
// the border color as the new value is sampled at
// the 2nd tick of the output cycle.
// TODO: The "+ 1" thing is still wrong as there may
// be contentions in the middle.
render_screen_to_tick(get_ticks() + 1);
border_color = n & 0x7;
}
handle_port_contention(addr);
}
void on_set_addr_bus(fast_u16 addr) {
addr_bus_value = addr;
}
void on_3t_exec_cycle() {
handle_contention_tick();
handle_contention_tick();
handle_contention_tick();
}
void on_4t_exec_cycle() {
handle_contention_tick();
handle_contention_tick();
handle_contention_tick();
handle_contention_tick();
}
void on_5t_exec_cycle() {
handle_contention_tick();
handle_contention_tick();
handle_contention_tick();
handle_contention_tick();
handle_contention_tick();
}
void disable_int_on_ei() {
if(!int_after_ei_allowed)
base::disable_int_on_ei();
}
static const unsigned memory_image_size = 0x10000; // 64K bytes.
typedef least_u8 memory_image_type[memory_image_size];
static const ticks_type ticks_per_frame = 69888;
static const ticks_type ticks_per_line = 224;
static const ticks_type ticks_per_active_int = 32;
// Four bits per frame pixel in brightness:grb format.
static const unsigned bits_per_frame_pixel = 4;
static const unsigned brightness_bit = 3;
static const unsigned green_bit = 2;
static const unsigned red_bit = 1;
static const unsigned blue_bit = 0;
static const unsigned brightness_mask = 1u << brightness_bit;
static const unsigned green_mask = 1u << green_bit;
static const unsigned red_mask = 1u << red_bit;
static const unsigned blue_mask = 1u << blue_bit;
// Eight frame pixels per chunk. The leftmost pixel occupies the most
// significant bits.
static const unsigned pixels_per_frame_chunk = 8;
// The type of frame chunks.
static const unsigned frame_chunk_width = 32;
static_assert(
bits_per_frame_pixel * pixels_per_frame_chunk <= frame_chunk_width,
"The frame chunk width is too small!");
typedef uint_least32_t frame_chunk;
// The dimensions of the viewable area.
// TODO: Support the NTSC geometry.
static const unsigned screen_width = 256;
static const unsigned screen_height = 192;
static const unsigned border_width = 48;
static const unsigned top_border_height = 48;
static const unsigned bottom_border_height = 40;
static const unsigned frame_width =
border_width + screen_width + border_width;
static const unsigned frame_height =
top_border_height + screen_height + bottom_border_height;
// We want screen, border and frame widths be multiples of chunk widths to
// simplify the processing code and to benefit from aligned memory accesses.
static const unsigned chunks_per_border_width =
div_exact(border_width, pixels_per_frame_chunk);
static const unsigned chunks_per_screen_line =
div_exact(screen_width, pixels_per_frame_chunk);
static const unsigned chunks_per_frame_line =
div_exact(frame_width, pixels_per_frame_chunk);
typedef frame_chunk screen_chunks_type[frame_height][chunks_per_frame_line];
const screen_chunks_type &get_screen_chunks() { return screen_chunks; }
// TODO: Name the constants.
// TODO: private
void start_new_frame() {
ticks_since_int %= ticks_per_frame;
render_tick = 0;
++frame_counter;
if(frame_counter % 16 == 0)
flash_mask ^= 0xffff;
}
// TODO: private
static fast_u16 get_pixel_pattern_addr(unsigned frame_line,
unsigned pixel_in_line) {
assert(frame_line >= 64);
assert(frame_line < 64 + screen_height);
assert(pixel_in_line >= border_width);
assert(pixel_in_line < border_width + screen_width);
fast_u16 addr = 0x4000;
// Adjust the address according to the third of the
// screen.
unsigned line = frame_line - 64;
addr += 0x800 * (line / 64);
line %= 64;
// See which character line it is.
addr += 0x20 * (line / 8);
line %= 8;
// Then, adjust according to the pixel line within the
// character line.
addr += 0x100 * line;
// Finally, apply the offset in line.
addr += (pixel_in_line - border_width) / 8;
return addr;
}
// TODO: private
static fast_u16 get_colour_attrs_addr(unsigned frame_line,
unsigned pixel_in_line) {
assert(frame_line >= 64);
assert(frame_line < 64 + screen_height);
assert(pixel_in_line >= border_width);
assert(pixel_in_line < border_width + screen_width);
fast_u16 addr = 0x5800;
unsigned line = frame_line - 64;
addr += 0x20 * (line / 8);
addr += (pixel_in_line - border_width) / 8;
return addr;
}
// TODO: Name the constants.
// TODO: Optimize.
void render_screen_to_tick(ticks_type end_tick) {
static_assert(bits_per_frame_pixel == 4,
"Unsupported frame pixel format!");
static_assert(pixels_per_frame_chunk == 8,
"Unsupported frame chunk format!");
// TODO: Render the border by whole chunks when possible.
while(render_tick < end_tick) {
// The tick since the beam was at the imaginary
// beginning (the top left corner) of the frame.
ticks_type frame_tick = render_tick + border_width / 2 - 8 / 2; // TODO
// Latch screen area bytes. Note that the first time
// we do that when the beam is outside of the screen
// area.
auto frame_line = static_cast<unsigned>(frame_tick / ticks_per_line);
auto pixel_in_line = static_cast<unsigned>(frame_tick % ticks_per_line) * 2;
bool is_screen_latching_area =
frame_line >= 64 &&
frame_line < 64 + screen_height &&
pixel_in_line >= border_width - 8 &&
pixel_in_line < border_width + screen_width - 8;
if(is_screen_latching_area && render_tick % 8 == 0) {
fast_u16 pattern_addr = get_pixel_pattern_addr(
frame_line, pixel_in_line + 8);
// TODO
// printf("%d -> 0x%04x\n", (int) render_tick, (unsigned) addr);
latched_pixel_pattern = make16(on_read(pattern_addr),
on_read(pattern_addr + 1));
fast_u16 attr_addr = get_colour_attrs_addr(
frame_line, pixel_in_line + 8);
latched_colour_attrs = make16(on_read(attr_addr),
on_read(attr_addr + 1));
}
// Render the screen area.
const unsigned top_hidden_lines = 64 - top_border_height;
bool is_screen_area = frame_line >= 64 &&
frame_line < 64 + screen_height &&
pixel_in_line >= border_width &&
pixel_in_line < border_width + screen_width;
if(is_screen_area) {
// TODO: Support rendering by whole chunks for
// better performance.
unsigned chunk_index = pixel_in_line / pixels_per_frame_chunk;
unsigned pixel_in_chunk = pixel_in_line % pixels_per_frame_chunk;
unsigned screen_line = frame_line - top_hidden_lines;
frame_chunk *line_chunks = screen_chunks[screen_line];
frame_chunk *chunk = &line_chunks[chunk_index];
unsigned pixel_in_cycle = (pixel_in_line - border_width) % 16;
if(pixel_in_cycle == 0) {
latched_pixel_pattern2 = latched_pixel_pattern;
latched_colour_attrs2 = latched_colour_attrs;
}
auto attr = static_cast<unsigned>(latched_colour_attrs2 >> ((15 - pixel_in_cycle) / 8 * 8));
unsigned brightness = attr >> (6 - brightness_bit) & brightness_mask;
unsigned ink_color = ((attr >> 0) & 0x7) | brightness;
unsigned paper_color = ((attr >> 3) & 0x7) | brightness;
// TODO: We can compute the whole chunk as soon
// as we read the bytes. And then just
// apply them here.
unsigned pixels_value = 0;
fast_u16 pattern = latched_pixel_pattern2;
if((attr & 0x80) != 0)
pattern ^= flash_mask;
pixels_value |= (pattern & ((1u << 15) >> pixel_in_cycle)) != 0 ?
(ink_color << 28) : (paper_color << 28);
pixels_value |= (pattern & ((1u << 14) >> pixel_in_cycle)) != 0 ?
(ink_color << 24) : (paper_color << 24);
pixels_value >>= pixel_in_chunk * 4;
// printf("%d, pixel_in_cycle %d\n", (int) render_tick, (int) pixel_in_cycle);
unsigned pixels_mask = 0xff000000 >> (pixel_in_chunk * 4);
*chunk = (*chunk & ~pixels_mask) | pixels_value;
++render_tick;
continue;
}
// Render the border area.
// TODO: We can simply initialize the render tick
// with the first visible tick value and stop
// the rendering loop at the last visible tick.
// Just don't forget about latching.
bool is_visible_area =
frame_line >= top_hidden_lines &&
frame_line < 64 + screen_height + bottom_border_height &&
pixel_in_line < frame_width;
if(is_visible_area) {
if(render_tick % 4 == 0)
latched_border_color = border_color;
unsigned chunk_index = pixel_in_line / pixels_per_frame_chunk;
unsigned pixel_in_chunk = pixel_in_line % pixels_per_frame_chunk;
// TODO: Support rendering by whole chunks for
// better performance.
unsigned screen_line = frame_line - top_hidden_lines;
frame_chunk *line_chunks = screen_chunks[screen_line];
frame_chunk *chunk = &line_chunks[chunk_index];
unsigned pixels_value = (0x11000000 * latched_border_color) >> (pixel_in_chunk * 4);
unsigned pixels_mask = 0xff000000 >> (pixel_in_chunk * 4);
*chunk = (*chunk & ~pixels_mask) | pixels_value;
++render_tick;
continue;
}
++render_tick;
}
}
// TODO: Move to the private section.
unsigned frame_counter = 0;
ticks_type render_tick = 0;
unsigned latched_border_color = 0;
fast_u16 latched_pixel_pattern = 0;
fast_u16 latched_colour_attrs = 0;
fast_u16 latched_pixel_pattern2 = 0;
fast_u16 latched_colour_attrs2 = 0;
fast_u16 flash_mask = 0;
void render_screen() {
render_screen_to_tick(ticks_per_frame);
}
typedef uint_least32_t pixel_type;
typedef pixel_type pixels_buffer_type[frame_height][frame_width];
static const std::size_t pixels_buffer_size = sizeof(pixels_buffer_type);
void get_frame_pixels(pixels_buffer_type &buffer) {
static_assert(is_multiple_of(frame_width, pixels_per_frame_chunk),
"Fractional number of chunks per line is not supported!");
static_assert(bits_per_frame_pixel == 4,
"Unsupported frame pixel format!");
static_assert(pixels_per_frame_chunk == 8,
"Unsupported frame chunk format!");
pixel_type *pixels = *buffer;
std::size_t p = 0;
for(const auto &screen_line : screen_chunks) {
for(auto chunk : screen_line) {
pixels[p++] = translate_color((chunk >> 28) & 0xf);
pixels[p++] = translate_color((chunk >> 24) & 0xf);
pixels[p++] = translate_color((chunk >> 20) & 0xf);
pixels[p++] = translate_color((chunk >> 16) & 0xf);
pixels[p++] = translate_color((chunk >> 12) & 0xf);
pixels[p++] = translate_color((chunk >> 8) & 0xf);
pixels[p++] = translate_color((chunk >> 4) & 0xf);
pixels[p++] = translate_color((chunk >> 0) & 0xf);
}
}
}
events_mask run() {
// Normalize the ticks-since-int counter.
if (ticks_since_int >= ticks_per_frame)
start_new_frame();
// Reset events.
events = no_events;
// Execute instructions that fit the current frame.
while(!events && ticks_since_int < ticks_per_frame) {
if(!int_suppressed) {
// ~INT is sampled during the last tick of the
// previous instruction, so we have to see
// whether ~INT was active during that last tick
// and not the current tick.
ticks_type previous_tick = ticks_since_int - 1;
if(previous_tick < ticks_per_active_int)
on_handle_active_int();
}
on_step();
}
// Signal end-of-frame, if it's the case.
if(ticks_since_int >= ticks_per_frame)
events |= end_of_frame;
return events;
}
FILE *get_trace_file() {
if(!trace_enabled)
return nullptr;
static FILE *trace = nullptr;
if(!trace)
trace = std::fopen("zx_trace", "w");
return trace;
}
void trace_state() {
FILE *trace = get_trace_file();
if(!trace)
return;
if(self().get_iregp_kind() != z80::iregp::hl)
return;
fast_u16 pc = self().get_pc();
bool new_rom_instr =
pc < 0x4000 && !is_marked_addr(pc, visited_instr_mark);
disassembler disasm(pc, self().on_get_memory());
std::fprintf(trace,
"%7u "
"PC:%04x AF:%04x BC:%04x DE:%04x HL:%04x IX:%04x IY:%04x "
"SP:%04x WZ:%04x IR:%04x iff1:%u "
"%02x%02x%02x%02x%02x%02x%02x%02x %s%s\n",
static_cast<unsigned>(ticks_since_int),
static_cast<unsigned>(pc),
static_cast<unsigned>(self().get_af()),
static_cast<unsigned>(self().get_bc()),
static_cast<unsigned>(self().get_de()),
static_cast<unsigned>(self().get_hl()),
static_cast<unsigned>(self().get_ix()),
static_cast<unsigned>(self().get_iy()),
static_cast<unsigned>(self().get_sp()),
static_cast<unsigned>(self().get_wz()),
static_cast<unsigned>(self().get_ir()),
static_cast<unsigned>(self().get_iff1()),
static_cast<unsigned>(on_read((pc + 0) & 0xffff)),
static_cast<unsigned>(on_read((pc + 1) & 0xffff)),
static_cast<unsigned>(on_read((pc + 2) & 0xffff)),
static_cast<unsigned>(on_read((pc + 3) & 0xffff)),
static_cast<unsigned>(on_read((pc + 4) & 0xffff)),
static_cast<unsigned>(on_read((pc + 5) & 0xffff)),
static_cast<unsigned>(on_read((pc + 6) & 0xffff)),
static_cast<unsigned>(on_read((pc + 7) & 0xffff)),
disasm.on_disassemble(), new_rom_instr ? " [new]" : "");
std::fflush(trace);
}
void on_step() {
trace_state();
mark_addr(self().get_pc(), visited_instr_mark);
base::on_step();
}
bool on_handle_active_int() {
bool int_initiated = base::on_handle_active_int();
if(FILE *trace = get_trace_file()) {
if(int_initiated) {
std::fprintf(trace, "INT accepted\n");
} else {
std::fprintf(trace, "INT ignored (int_disabled=%u, iff1=%u)\n",
self().is_int_disabled(), self().get_iff1());
}
std::fflush(trace);
}
return int_initiated;
}
protected:
using base::self;
pixel_type translate_color(unsigned c) {
uint_fast32_t r = 0;
r |= (c & red_mask) << (16 - red_bit);
r |= (c & green_mask) << (8 - green_bit);
r |= (c & blue_mask) << (0 - blue_bit);
// TODO: Use the real coefficients.
r *= (c & brightness_mask) ? 0xff : 0xcc;
return static_cast<pixel_type>(r);
}
events_mask events = no_events;
ticks_type ticks_since_int = 0;
ticks_type ticks_to_stop = 0; // Null means no limit.
ticks_type fetches_to_stop = 0; // Null means no limit.
fast_u16 addr_bus_value = 0;
unsigned border_color = 0;
// True if interrupts shall not be initiated at the beginning
// of frames.
bool int_suppressed = false;
// True if interrupt can occur after EI instruction. Some
// emulators such as SPIN allow that, so we should be able to
// do the same to support RZX files produced by them.
bool int_after_ei_allowed = false;
bool trace_enabled = false;
private:
screen_chunks_type screen_chunks;
least_u8 memory_marks[memory_image_size] = {};
};
} // namespace zx