-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisscrn.cpp
3391 lines (2752 loc) · 81.6 KB
/
disscrn.cpp
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
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// disscrn.cpp
// ncurses-based screen handling for disassembler
#include "disscrn.h"
#include "dissave.h"
#include "discpu.h"
#include "discmt.h"
#include <ctype.h>
#include <signal.h>
//#define DEBUG_POS // show debug info of cursor pos, etc.
#define CRLF "\r\n\e[0K"
#define BEL "\x07"
// global screen handler object
DisScrn scrn;
// control-C status flag
bool DisScrn::_ctrl_c = false;
// =====================================================
// dispatch table for single-char commands
struct cmd_char_t {
char key;
void (DisScrn::* func)(void);
};
struct cmd_char_t char_cmds[] =
{
// { 'q', &DisScrn::do_cmd_q }, // exit program
{ 'a', &DisScrn::do_cmd_a }, // disasssemble as ascii
{ 'b', &DisScrn::do_cmd_b }, // disasssemble as bytes
{ 'h', &DisScrn::do_cmd_h }, // disassemble as hex
{ 'w', &DisScrn::do_cmd_w }, // disassemble as words
{ 0x17, &DisScrn::do_cmd_w1 }, // disassemble as word address minus one
{ 'W', &DisScrn::do_cmd_W }, // shft-W disassemble as reverse words
{ '\\', &DisScrn::do_cmd_lng }, // disassemble as longs
{ 'd', &DisScrn::do_cmd_d }, // disassemble as decimal word
{ 'D', &DisScrn::do_cmd_D }, // disassemble as decimal long
{ 'I', &DisScrn::do_cmd_I }, // disassemble as binary
{ 'X', &DisScrn::do_cmd_X }, // disassemble as big-endian binary X
{ '_', &DisScrn::do_cmd_ebc }, // disasssemble as ebcdic
{ 'O', &DisScrn::do_cmd_O }, // disassemble as little-endian binary O
{ 'o', &DisScrn::do_cmd_o }, // disassemble as offset table entry
{ '|', &DisScrn::do_cmd_rl }, // shft-\ disassemble as reverse longs
{ '*', &DisScrn::do_cmd_rpt }, // repeat previous b/w/|/a/h command
{ 'x', &DisScrn::do_cmd_x }, // disassemble as raw
{ 'c', &DisScrn::do_cmd_c }, // disassemble as code
{ 'C', &DisScrn::do_cmd_C }, // disassemble as code until lfflag or illegal
{ 'T', &DisScrn::do_cmd_T }, // shft-T trace disassembly from _sel
{ 0x14, &DisScrn::do_cmd_cT }, // ctrl-T trace disassembly from refaddr
{ '^', &DisScrn::do_cmd_lbl }, // toggle label type at refaddr
{ 0x0C, &DisScrn::do_cmd_cL }, // ctrl-L toggle label type
{ 'L', &DisScrn::do_cmd_cL }, // shift-L toggle label type
{ 'l', &DisScrn::do_cmd_L }, // L toggle pre-instruction blank line
{ 0x12, &DisScrn::do_cmd_cR }, // ctrl-R toggle NOREF for this code address
{ 0x15, &DisScrn::do_cmd_cU }, // ctrl-U save undo buffer
{ 'U', &DisScrn::do_cmd_U }, // shft-U swap undo buffer
{ '[', &DisScrn::do_cmd_prv }, // go to previous label
{ ']', &DisScrn::do_cmd_nxt }, // go to next label
{ '<', &DisScrn::do_cmd_bak }, // go to previous address from @ or :addr
{ '>', &DisScrn::do_cmd_fwd }, // go to next stacked address from @ or :addr
{ '@', &DisScrn::do_cmd_ref }, // go to refaddr
{ '(', &DisScrn::do_cmd_les }, // less data
{ ')', &DisScrn::do_cmd_mor }, // more data
{ '`', &DisScrn::do_cmd_cen }, // recenter screen
{ '~', &DisScrn::do_cmd_top }, // move selecton to near top of screen
{ '!', &DisScrn::do_cmd_cln }, // clean up current label if not referenced
{ '"', &DisScrn::do_cmd_hint }, // toggle hint flags in current instr
{ '$', &DisScrn::do_cmd_defhint }, // toggle default hint flags
{ 0, NULL }
};
// =====================================================
// dispatch table for command-line commands
struct cmd_line_t {
const char *cmd;
void (DisScrn::* func)(char *p);
};
struct cmd_line_t line_cmds[] =
{
{ "quit", &DisScrn::do_cmd_quit }, // "quit" - exit program
{ "q", &DisScrn::do_cmd_quit }, // "q" - exit program
{ "lst", &DisScrn::do_cmd_list }, // "lst" - save disassembly listing
{ "list", &DisScrn::do_cmd_list }, // "list" - save disassembly listing
{ "l", &DisScrn::do_cmd_label }, // "l" - set/clear a label
{ "label", &DisScrn::do_cmd_label }, // "label" - set/clear a label
{ "asm" , &DisScrn::do_cmd_asm }, // "asm" - save asm source listing
{ "save", &DisScrn::do_cmd_save }, // "save" - save disassembly state
{ "wq", &DisScrn::do_cmd_wq }, // "wq" - save state and quit
{ "w", &DisScrn::do_cmd_save }, // "w" - save disassembly state
{ "load", &DisScrn::do_cmd_load }, // "load" - load binary and disassembly state
// { "l", &DisScrn::do_cmd_load }, // "l" - load binary and disassembly state
// { "new", &DisScrn::do_cmd_new }, // "new" - unload current file
{ "rst", &DisScrn::do_cmd_rst }, // "rst" - set lengths after Z80 RST instrs
// { "go", &DisScrn::do_cmd_go }, // "go" - go to address
// { "g", &DisScrn::do_cmd_go }, // "g" - go to address
{ "$", &DisScrn::do_cmd_end }, // "$" - go to end
{ "cpu", &DisScrn::do_cmd_cpu }, // "cpu" - set default CPU type
{ "defcpu", &DisScrn::do_cmd_defcpu }, // "defcpu" - set default CPU type
{ "tab", &DisScrn::do_cmd_tab }, // "tab" - set/show tab stops
{ "tabs", &DisScrn::do_cmd_tab }, // "tabs" - set/show tab stops
{ NULL, NULL }
};
// =====================================================
// handle SIGINT
// Control-C signal handler
void finish(int sig)
{
// let the application handle control-C
if (sig == SIGINT) {
scrn.ctrl_c();
return;
}
end_screen();
// ===== do your non-curses wrapup here
exit(0);
}
// =====================================================
// SIGWINCH signal handler
static bool sigwinched = false;
void sigwinch(int UNUSED sig)
{
// use a flag instead of trying to do ncurses calls
// in what could be an interrupt context
sigwinched = true;
}
// =====================================================
extern "C" void my_assert_func(const char *expr, const char *file, int line)
{
// clear the screen and exit curses
end_screen();
printf("%s:%d assertion failed: '%s'\n", file, line, expr);
#if 0
printf("_start = %.4X:%d\n", (int) scrn._start.addr, scrn._start.line);
printf("_top = %.4X:%d\n", (int) scrn._top.addr, scrn._top.line);
printf("_sel = %.4X:%d\n", (int) scrn._sel.addr, scrn._sel.line);
printf("_end = %.4X:%d\n", (int) scrn._end.addr, scrn._end.line);
printf("_sel_row = %3d\n", scrn._sel_row);
// printf("curs_row = %3d\n", screen_row(_sel));
// printf("end_row = %3d\n", screen_row(_end));
printf("max_row() = %3d\n", scrn.max_row());
printf("get_len() = %3d\n", scrn._sel.get_len());
printf("attr = %.2X\n", rom.get_attr(scrn._sel.addr));
#endif
exit(0);
}
// =====================================================
void setup_screen()
{
// set up curses
initscr(); // initialize the curses library
keypad(stdscr, TRUE); // enable extended function key mapping
set_escdelay(25); // set escape delay of 25ms to allow ESC key
nonl(); // tell curses not to do NL->CR/NL on output
cbreak(); // take input chars one at a time, no wait for \n
noecho(); // don't echo input
#if 0 // disable colors for now
if (has_colors()) {
start_color();
// Simple color assignment, often all we need. Color pair 0 cannot
// be redefined. This example uses the same value for the color
// pair as for the foreground color, though of course that is not
// necessary:
init_pair(1, COLOR_RED, COLOR_BLACK);
init_pair(2, COLOR_GREEN, COLOR_BLACK);
init_pair(3, COLOR_YELLOW, COLOR_BLACK);
init_pair(4, COLOR_BLUE, COLOR_BLACK);
init_pair(5, COLOR_CYAN, COLOR_BLACK);
init_pair(6, COLOR_MAGENTA, COLOR_BLACK);
init_pair(7, COLOR_WHITE, COLOR_BLACK);
}
#endif
// set up signal handlers
signal(SIGINT, finish); // ctrl-C
signal(SIGTSTP, SIG_IGN); // disable ctrl-Z
signal(SIGQUIT, SIG_IGN); // disable ctrl-backslash
// set up (proper) SIGWINCH handler
// apparently it needs to be registered without the SA_RESTART flag
// whatever that means: https://stackoverflow.com/questions/53822353/
struct sigaction old_action;
struct sigaction new_action;
new_action.sa_handler = sigwinch;
new_action.sa_flags = 0; // no flags!
sigemptyset(&new_action.sa_mask);
sigaction(SIGWINCH, &new_action, &old_action);
// set up non-blocking keyboard input to allow eating ctrl-C's
timeout(500);
}
// =====================================================
void end_screen()
{
// clean up after curses
endwin();
}
// =====================================================
void DisScrn::ctrl_c()
{
// flag was not cleared, something probably got wedged
// this means that if something hangs, you can still
// press ctrl-C twice to get out
if (_ctrl_c) {
// try to clean up the terminal before exiting
finish(0);
}
// set the flag
_ctrl_c = true;
}
// =====================================================
//
// displays debug information on the screen
// called at the end of print_screen()
void DisScrn::debug_info()
{
int row = 1; // first row for debug info
int col = 80; // column for debug info
wmove(_win, row++, col);
wprintw(_win, " _start = %.4X:%d ", (int) _start.addr, (int) _start.line);
wmove(_win, row++, col);
wprintw(_win, " _top = %.4X:%d ", (int) _top.addr, (int) _top.line);
wmove(_win, row++, col);
wprintw(_win, " _sel = %.4X:%d ", (int) _sel.addr, (int) _sel.line);
wmove(_win, row++, col);
wprintw(_win, " _end = %.4X:%d ", (int) _end.addr, (int) _end.line);
wmove(_win, row++, col);
wprintw(_win, " _sel_row = %3d ", _sel_row);
// wmove(_win, row++, col);
// wprintw(_win, " curs_row = %3d ", screen_row(_sel));
// wmove(_win, row++, col);
// wprintw(_win, " end_row = %3d ", screen_row(_end));
wmove(_win, row++, col);
wprintw(_win, " max_row() = %3d ", max_row());
wmove(_win, row++, col);
wprintw(_win, " get_len() = %3d ", _sel.get_len());
wmove(_win, row++, col);
wprintw(_win, " attr = %.2X ", rom.get_attr(_sel.addr));
// spit it out
wrefresh(_win);
}
// =====================================================
// void init_scrn()
//
// This initializes the screen handler object.
//
// Note that an explicit constructor is not used, to avoid potential
// problems with it calling other objects. The screen handler object
// is declared at global scope and there is not a definited order
// for constructors.
void DisScrn::init_scrn(bool reset)
{
disline.get_start_addr(_start); // start address
disline.get_end_addr(_end); // line *after* last line
_sel_row = 0;
_count = 0;
_win = stdscr;
_in_input = 0;
_cmd[0] = 0;
_err[0] = 0;
_errbeep = false;
_search[0] = 0;
_quit = false;
_rpt_key = 0;
_rpt_count = 1;
memset(_goto_stk, 0, sizeof _goto_stk);
_goto_sp = 0;
if (reset) {
_top = _start;
_sel = _start;
} else {
recenter();
}
print_screen();
}
// =====================================================
// push address to _goto_stk
void DisScrn::push_addr(addr_t addr, addr_t next)
{
// don't push a null
if (!addr) {
return;
}
if (_goto_sp == GOTO_SIZE) {
// stack is full, just drop the oldest one
for (int i = 0; i < GOTO_SIZE - 1; i++) {
_goto_stk[i] = _goto_stk[i+1];
}
_goto_sp--;
}
_goto_stk[_goto_sp++] = addr;
// try to push forward the "next" address
if (next) {
push_addr(next);
_goto_sp--;
}
}
// =====================================================
// pop address from _goto_stk
addr_t DisScrn::pop_addr()
{
// stack is empty, return fail
if (_goto_sp == 0) {
return 0;
}
return _goto_stk[--_goto_sp];
}
// =====================================================
// un-pop address from _goto_stk
addr_t DisScrn::un_pop_addr()
{
// already at end
if (_goto_sp == GOTO_SIZE) {
return 0;
}
if (_goto_stk[_goto_sp + 1]) {
return _goto_stk[++_goto_sp];
}
return 0;
}
// =====================================================
// returns the screen row which contains ad (starting at _top)
// returns -1 if before _top
// returns -2 if beyond max_row()
int DisScrn::screen_row(addrline_t ad) const
{
// check if sel is before top of screen
if (ad.addr < _top.addr) {
return -1;
}
// count lines from _top until ad
addrline_t addr = _top;
int row = 0;
while (row <= max_row()) {
if (addr == ad) {
// selection found
return row;
}
if (addr == _end) {
// ad is after _end, (but it shouldn't be)
break;
}
addr.next_line();
row++;
}
// past end of screen
return -2;
}
// =====================================================
// attempts to make sure that _sel is on the screen
void DisScrn::set_sel_line(int row)
{
// set _top to 'row' lines before _sel, stopping at _start
addrline_t addr = _sel;
for (int i = 0; i < row && addr != _start; i++) {
addr.prev_line();
}
_top = addr;
// if _end is on screen, try to keep it at bottom of screen
// find the screen row of the _end line
int end_row = screen_row(_end);
if (end_row >= 0 && end_row < max_row()) {
// need to move screen back down by "max_row() - end_row" lines
// but if _top ever reaches _start then stop
// (because the screen is bigger than entire file)
for (int i = max_row() - end_row; (i > 0) && (_top != _start); i--) {
_top.prev_line();
}
}
}
// =====================================================
// attempts to make sure that _sel is on the screen
// strict == true ensures that the selection is not
// too close to the bottom or top of the screen
void DisScrn::recenter(bool strict)
{
// thresholds are numlines/5 and numlines*4/5
int threshold = max_row() / 5;
if (threshold > 10) {
threshold = 10;
}
// find the screen row of _sel
int row = screen_row(_sel);
if (row == -1) {
// if _sel is before _top, count back threshold from _sel and set _top
set_sel_line(threshold);
return;
}
if (row == -2) {
// if _sel is after _top + maxrows,
// try to place _top at 3/4 threshold back from _sel
set_sel_line(max_row() - threshold);
return;
}
// positive row number is on screen, may not need to re-center
if (strict) {
// if in first 1/4 of screen, move _top back
if (row < threshold) {
set_sel_line(threshold);
return;
}
// if in last 1/4 of screen, move _top forward
if (row > max_row() - threshold) {
set_sel_line(max_row() - threshold);
return;
}
}
}
// =====================================================
void DisScrn::error(const char *s)
{
if (s && s[0] == 0x07 /* BEL */) {
_errbeep = true;
s++;
}
if (s && s[0]) {
// if error message, save it and clear count
strcpy(_err, s);
_count = 0;
} else {
// no error message, just clear it
_err[0] = 0;
}
status_line();
wrefresh(_win);
}
// =====================================================
// void print_line(int addr, int UNUSED sub_line)
//
// This print one line of the screen.
// - addr = the address of the line being printed
// - sub_line = which line of the address to print
//
void DisScrn::print_line(struct addrline_t addr)
{
// use DisLine
char s[256]; // storage for line text
disline.get_text(addr, s);
// check remaining width of screen
int w = getmaxx(_win) - getcurx(_win);
if (strlen(s) >= w) {
strcpy(s + w - 3, "...");
}
wprintw(_win, "%s", s);
// return number of lines to advance?
// (in future, possibly return negative if more sub_lines?)
}
// =====================================================
// void set_cursor(void);
//
void DisScrn::set_cursor()
{
if (_in_input) {
wmove(_win, /*row=*/ 0, /*col=*/ _cmd_pos - _cmd_col + 1);
} else {
// put cursor on right edge of screen to indicate scroll position
// note that this is based on address, not lines, so "long" lines
// will scroll faster!
float pos = (float) (_sel.addr - _start.addr) / (float) rom._size
* (float) (max_row());
wmove(_win, /*row=*/ 1 + (int) (pos + 0.5), /*col=*/ getmaxx(_win) - 1);
}
}
// =====================================================
// void status_line(void);
//
void DisScrn::status_line()
{
wmove(_win, /*row=*/ 0, /*col=*/ 0);
if (_in_input) {
// display current command
// default to start with prompt character
char c = _in_input;
// determine max width of command line in the window
int maxwid = getmaxx(_win) - 2;
// compute X position of cursor
int xpos = _cmd_pos - _cmd_col;
// check if beyond either edge of the command line space
if (xpos < 0) {
// before left edge, add xpos to _cmd_col, max 0
_cmd_col += xpos;
if (_cmd_col < 0) {
_cmd_col = 0;
}
} else if (xpos > maxwid) {
// after right edge, add xpos - maxwid to _cmd_col
_cmd_col += xpos - maxwid;
}
// determine how much of _cmd is after _cmd_col
int n = strlen(_cmd + _cmd_col);
if (n > maxwid) {
n = maxwid;
}
n += _cmd_col;
// if not at first char, start the line with '<' character
if (_cmd_col) {
// start with a '<' to show that something is off to the left
c = '<';
}
// see if end of string is outside the window range
char cc = _cmd[n];
if (cc) {
// temporarily lop off the end of _cmd
_cmd[n] = 0;
}
// print what can be shown
wprintw(_win, "%c%s", c, _cmd + _cmd_col);
if (cc) {
// start with a '>' to show that something is off to the right
wprintw(_win, ">");
_cmd[n] = cc;
}
wclrtoeol(_win);
} else {
// display status bar, in inverse video
wattron(_win, A_REVERSE);
#if 0
// what to display? I dunno, how about the current address?
if (defCpu->_addrwid == ADDR_16) {
wprintw(_win, " ADDR = %.4X", _sel.addr);
} else {
wprintw(_win, " ADDR = %.6X", _sel.addr);
}
#endif
if (_count) {
wprintw(_win, " COUNT = %d", _count);
}
if (_err[0]) {
wprintw(_win, " %s", _err);
}
// fill from cursor column to width-1 with inverse blanks
for (int i = getcurx(_win); i < getmaxx(_win)-1; i++) {
waddch(_win, ' ');
}
wattroff(_win, A_REVERSE);
}
set_cursor();
}
// =====================================================
// void print_screen()
//
// This refreshes the entire disasembly display.
// It seems rather wasteful to redraw the entire screen after
// every key, but ncurses will (should) optimize redraw for us.
void DisScrn::print_screen()
{
// recovery attempt in case _sel_row has fallen off the screen
// don't want to do this recursively because it's much more difficult
// to do it exactly once that way
bool tried = false;
TryAgain:
// set disassembly to screen format
disline.line_cols = disline.SCRN_COLS;
// ensure that we haven't gone off the rails
// how can this happen?
// - be on a raw byte (mData)
// - do a trace disassembly
// - the byte you are sitting on becomes an instruction
// - but not the first byte of the instruction
// check that _sel line is not ATTR_CONT
while (rom.get_attr(_sel.addr) & ATTR_CONT) {
_sel.addr--;
_sel.line = 0;
}
// check that _top line is not ATTR_CONT
while (rom.get_attr(_top.addr) & ATTR_CONT) {
_top.addr--;
_top.line = 0;
}
// check that _sel sub-line is in range
int n = _sel.num_lines();
if (_sel.line >= n) {
_sel.line = n - 1;
}
// check that _top sub-line is in range
n = _top.num_lines();
if (_top.line >= n) {
_top.line = n - 1;
}
#if 0
// now ensure that _sel hasn't sneaked above _top
if ((_sel.addr < _top.addr) ||
((_sel.addr == _top.addr) && (_sel.line < _top.line))) {
_top = _sel;
}
#else
// try to make sure that _sel is on screen
recenter();
#endif
struct addrline_t addr = _top; // get top line of screen
_sel_row = -1; // default to selected row not yet found
// print status line
status_line();
// loop to print visible rows
// note that row=0, col=0 is top left of screen
for (int row = 0; row <= max_row(); row++) {
// go to line of display
wmove(_win, row+1, /*col=*/ 0);
// if this is the selected address and line, highlight it
bool inverse = (addr == _sel);
if (inverse) {
_sel_row = row; // _sel_row has been found
wattron(_win, A_REVERSE);
}
// print the line for that row, then advance to next addr/line
print_line(addr);
// unfortunately wclrtoeol() doesn't propagate the reverse attribute
if (inverse) {
// fill from cursor column to width-1 with inverse blanks
for (int i = getcurx(_win); i < getmaxx(_win)-1; i++) {
waddch(_win, ' ');
}
}
wclrtoeol(_win);
// clear attributes for next line
wattroff(_win, A_REVERSE);
// stop if at END line, otherwise advance to next line
if (addr == _end) {
break;
}
addr.next_line();
}
// clear rest of screen just in case
wclrtobot(_win);
#ifdef DEBUG_POS
debug_info();
#endif
// put cursor on highlighted line
set_cursor();
// mark screen for refresh
wrefresh(_win);
// problem if _sel_row not found
// what to do if this happens? probably could set _top = _sel and try again
// might also want to check if it's > max_row()
if (_sel_row < 0 && !tried) {
_top = _sel;
tried = true;
goto TryAgain;
} else {
assert(_sel_row >= 0);
}
}
// =====================================================
void DisScrn::do_cmd_quit(char *p)
{
char word[256];
int token = GetWord(p, word);
if (!rom._changed || token == '!') {
_quit = true;
}
error("file changed, use :wq or :q! to exit");
}
// =====================================================
void DisScrn::do_cmd_list(char UNUSED *p)
{
disline.write_listing("lst");
char s[256+21];
sprintf(s, "listing saved to %s.lst", rom._fname);
error(s);
}
// =====================================================
void DisScrn::do_cmd_asm(char UNUSED *p)
{
disline.write_asm("asm");
char s[256+21];
sprintf(s, "listing saved to %s.asm", (char *) rom._fname);
error(s);
}
// =====================================================
void DisScrn::do_cmd_save(char UNUSED *p)
{
DisSave save;
save.save_file();
char s[256+10];
sprintf(s, "saved %s.ctl", (char *) rom._fname);
error(s);
}
// =====================================================
void DisScrn::do_cmd_wq(char UNUSED *p)
{
*p = 0;
do_cmd_save(p);
do_cmd_quit(p);
}
// =====================================================
// :load "filename"
void DisScrn::do_cmd_load(char *p)
{
DisSave save;
// attempt to get filename
char fname[256] = {0};
GetString(p, fname);
// reload same file if fname is "-"
if (fname[0] == '-' && fname[1] == 0) {
strcpy(fname, rom._fname);
}
if (!fname[0]) {
error("no binary file name specified");
return;
}
// get ofs, base, size if entered
char word[256];
size_t ofs = 0;
addr_t base = 0;
size_t size = 0;
int force = 0;
int token = GetWord(p, word);
while (token && !_err[0]) {
if (token == '!') {
force |= save.FORCE_FNAME;
} else {
// make sure parameter is hexadecimal
if (!HexValid(word+1)) {
error("invalid hex parameter");
} else
switch (tolower(word[0])) {
case 'b':
base = HexVal(word+1);
force |= save.FORCE_BASE;
break;
case 's':
size = HexVal(word+1);
force |= save.FORCE_SIZE;
break;
case 'o':
ofs = HexVal(word+1);
force |= save.FORCE_OFS;
break;
default:
error("invalid parameter");
break;
}
}
token = GetWord(p, word);
}
if (!_err[0]) {
// now try to load the file
if (save.load_file(fname, ofs, base, size, force)) {
// failed to load, don't reset screen pointers
// binary data has been cleared if failure during load
return;
}
init_scrn(false); // do not reset saved _top and _sel
}
}
// =====================================================
void DisScrn::do_cmd_new(char UNUSED *p)
{
rom.unload();
init_scrn();
}
// =====================================================
void DisScrn::do_cmd_rst(char UNUSED *p)
{
char word[256];
if (GetWord(p, word)) {
// confirm eight digits
if (strlen(word) == sizeof rom._rst_xtra) {
p = word;
for (int i = 0; i < sizeof rom._rst_xtra; i++) {
if (!isdigit(*p)) {
goto err;
}
rom._rst_xtra[i] = *p++ - '0';
}
} else {
err:
error("RST lengths must be 8 digits");
return;
}
}
// show current rst_xtra
p = stpcpy(word, "RST lengths = ");
for (int i = 0; i < sizeof rom._rst_xtra; i++) {
*p++ = rom._rst_xtra[i] + '0';
}
*p = 0;
// force screen update to re-scan any visible RST instrs
print_screen();
error(word);
}
// =====================================================
void DisScrn::do_cmd_go(char *p)
{
char word[256];
if (GetWord(p, word)) {
int addr = HexVal(word);
// if before start of binary image, use start address
if (addr < rom._base) {
addr = rom._base;
}
// if not beyond end of binary image, go to address
if (addr <= rom.get_end()) {
// save current _sel.addr on stack
push_addr(_sel.addr, addr);
// select line and redraw screen
_sel.line_start(addr);
recenter(true);
print_screen();
return;
}
}
error("invalid address");
}
// =====================================================
void DisScrn::do_cmd_end(char UNUSED *p)
{
_sel.line_start(_end.addr);
recenter(true);
print_screen();
}
// =====================================================
void DisScrn::do_cmd_cpu(char *p)
{
char word[256];
if (GetWord(p, word)) {
class CPU *cpu = CPU::get_cpu(word);
if (cpu) {
cpu->set_cur_cpu();
sprintf(word, "current CPU type is now '%s'", cpu->_name);
error(word);
print_screen();
return;
}
} else {
sprintf(word, "current CPU type = '%s'", curCpu->_name);
error(word);
return;