forked from gurcei/m65dbg
-
Notifications
You must be signed in to change notification settings - Fork 16
/
m65.c
2604 lines (2364 loc) · 77.1 KB
/
m65.c
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
/* vim: set expandtab shiftwidth=2 tabstop=2: */
/*
Load the specified program into memory on the C65GS via the serial monitor.
We add some convenience features:
1. If an optional file name is provided, then we stuff the keyboard buffer
with the LOAD command. We check if we are in C65 mode, and if so, do GO64
(look for reversed spaces at $0800 for C65 ROM detection). Keyboard buffer @ $34A,
buffer length @ $D0 in C65 mode, same as C128. Then buffer is $277 in C64
mode, buffer length @ $C6 in C64 mode.
2. If an optional bitstream file is provided, then we use fpgajtag to load
the bitstream via JTAG. fpgajtag is now compiled internally to this program.
Copyright (C) 2014-2020 Paul Gardner-Stephen
Portions Copyright (C) 2013 Serval Project Inc.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <time.h>
#include <strings.h>
#include <string.h>
#include <ctype.h>
#include <sys/time.h>
#include <errno.h>
#include <getopt.h>
#include <inttypes.h>
#include <pthread.h>
#include "m65.h"
#include "screen_shot.h"
#define SLOW_FACTOR 1
#define SLOW_FACTOR2 1
#define DEBUG 0 // change to 1 later to debug
#ifdef WINDOWS
#include <windows.h>
#undef SLOW_FACTOR
#define SLOW_FACTOR 1
#define SLOW_FACTOR2 1
// #define do_usleep usleep
void do_usleep(__int64 usec)
{
HANDLE timer;
LARGE_INTEGER ft;
ft.QuadPart = -(10*usec); // Convert to 100 nanosecond interval, negative value indicates relative time
timer = CreateWaitableTimer(NULL, TRUE, NULL);
SetWaitableTimer(timer, &ft, 0, NULL, NULL, 0);
WaitForSingleObject(timer, INFINITE);
CloseHandle(timer);
}
#else
#include <termios.h>
#define do_usleep usleep
#endif
#ifdef __APPLE__
#include <sys/ioctl.h>
#include <CoreFoundation/CoreFoundation.h>
#include <IOKit/IOKitLib.h>
#include <IOKit/serial/IOSerialKeys.h>
#include <IOKit/serial/ioss.h>
#include <IOKit/IOBSD.h>
#endif
#ifdef WINDOWS
#define bzero(b,len) (memset((b), '\0', (len)), (void) 0)
#define bcopy(b1,b2,len) (memmove((b2), (b1), (len)), (void) 0)
FILE iobs[3];
FILE *__imp___acrt_iob_func(void)
{
iobs[0]=*stdin;
iobs[1]=*stdout;
iobs[2]=*stderr;
return iobs;
}
#endif
#ifdef __APPLE__
static const int B1000000 = 1000000;
static const int B1500000 = 1500000;
static const int B2000000 = 2000000;
static const int B4000000 = 4000000;
#endif
static time_t start_time=0;
int osk_enable=0;
int not_already_loaded=1;
int halt=0;
int usedk=0;
extern bool unix_socket_flag;
// 0 = old hard coded monitor, 1= Kenneth's 65C02 based fancy monitor
// Only REALLY old bitstreams don't have the new monitor
int new_monitor=1;
int viciv_mode_report(unsigned char *viciv_regs);
int fetch_ram(unsigned long address,unsigned int count,unsigned char *buffer);
int push_ram(unsigned long address,unsigned int count,unsigned char *buffer);
int do_screen_shot(void);
int process_char(unsigned char c,int live);
int process_line(char *line,int live);
int process_waiting(PORT_TYPE fd);
int fpgajtag_main(char *bitstream,char *serialport);
void init_fpgajtag(const char *serialno, const char *filename, uint32_t file_idcode);
int xilinx_boundaryscan(char *xdc,char *bsdl,char *sensitivity);
void set_vcd_file(char *name);
void do_exit(int retval);
void usage(void)
{
fprintf(stderr,"MEGA65 cross-development tool.\n");
fprintf(stderr,"usage: m65 [-l <serial port>] [-s <230400|2000000|4000000>] [-b <FPGA bitstream> [-v <vivado.bat>] [[-k <hickup file>] [-R romfile] [-U flashmenufile] [-C charromfile]] [-c COLOURRAM.BIN] [-B breakpoint] [-o] [-d diskimage.d81] [-j] [-J <XDC,BSDL[,sensitivity list]> [-V <vcd file>]] [[-1] [<-t|-T> <text>] [-f FPGA serial ID] [filename]] [-H] [-E|-L] [-Z <flash addr>]\n");
fprintf(stderr," -l - Name of serial port to use, e.g., /dev/ttyUSB1\n");
fprintf(stderr," -s - Speed of serial port in bits per second. This must match what your bitstream uses.\n");
fprintf(stderr," (Older bitstream use 230400, and newer ones 2000000 or 4000000).\n");
fprintf(stderr," -b - Name of bitstream file to load.\n");
fprintf(stderr," -v - The location of the Vivado executable to use for -b on Windows.\n");
fprintf(stderr," -K - Use DK backend for libUSB, if available\n");
fprintf(stderr," -k - Name of hickup file to forcibly use instead of the HYPPO in the bitstream.\n");
fprintf(stderr," NOTE: You can use bitstream and/or HYPPO from the Jenkins server by using @issue/tag/hardware\n"
" for the bitstream, and @issue/tag for HYPPO.\n");
fprintf(stderr," -J - Do JTAG boundary scan of attached FPGA, using the provided XDC and BSDL files.\n");
fprintf(stderr," A sensitivity list can also be provided, to restrict the set of signals monitored.\n");
fprintf(stderr," This will likely be required when producing VCD files, as they can only log ~80 signals.\n");
fprintf(stderr," -j Do JTAG operation(s), and nothing else.\n");
fprintf(stderr," -V - Write JTAG change log to VCD file, instead of to stdout.\n");
fprintf(stderr," -R - ROM file to preload at $20000-$3FFFF.\n");
fprintf(stderr," -U - Flash menu file to preload at $50000-$57FFF.\n");
fprintf(stderr," -C - Character ROM file to preload.\n");
fprintf(stderr," -c - Colour RAM contents to preload.\n");
fprintf(stderr," -4 - Switch to C64 mode before exiting.\n");
fprintf(stderr," -H - Halt CPU after loading ROMs.\n");
fprintf(stderr," -1 - Load as with ,8,1 taking the load address from the program, instead of assuming $0801\n");
fprintf(stderr," -r - Automatically RUN programme after loading.\n");
fprintf(stderr," -o - Enable on-screen keyboard\n");
fprintf(stderr," -d - Enable virtual D81 access\n");
fprintf(stderr," -p - Force PAL video mode\n");
fprintf(stderr," -n - Force NTSC video mode\n");
fprintf(stderr," -F - Force reset on start\n");
fprintf(stderr," -t - Type text via keyboard virtualisation.\n");
fprintf(stderr," -T - As above, but also provide carriage return\n");
fprintf(stderr," -B - Set a breakpoint on synchronising, and then immediately exit.\n");
fprintf(stderr," -E - Enable streaming of video via ethernet.\n");
fprintf(stderr," -L - Enable streaming of CPU instruction log via ethernet.\n");
fprintf(stderr," -f - Specify which FPGA to reconfigure when calling fpgajtag\n");
fprintf(stderr," -S - Show the text-mode screen\n");
fprintf(stderr," -Z - Zap (reconfigure) FPGA from specified hex address in flash.\n");
fprintf(stderr," filename - Load and run this file in C64 mode before exiting.\n");
fprintf(stderr,"\n");
exit(-3);
}
int cpu_stopped=0;
int pal_mode=0;
int ntsc_mode=0;
int reset_first=0;
int boundary_scan=0;
char boundary_xdc[1024]="";
char boundary_bsdl[1024]="";
char jtag_sensitivity[1024]="";
int hyppo_report=0;
unsigned char hyppo_buffer[1024];
int counter =0;
#ifdef WINDOWS
extern PORT_TYPE fd;
#else
extern PORT_TYPE fd;
#endif
int state=99;
unsigned int name_len,name_lo,name_hi,name_addr=-1;
int do_go64=0;
int do_run=0;
int comma_eight_comma_one=0;
int ethernet_video=0;
int ethernet_cpulog=0;
int virtual_f011=0;
char *d81file=NULL;
char *filename=NULL;
char *romfile=NULL;
char *flashmenufile=NULL;
char *charromfile=NULL;
char *colourramfile=NULL;
FILE *f=NULL;
FILE *fd81=NULL;
char *search_path=".";
char *bitstream=NULL;
char *vivado_bat=NULL;
char *hyppo=NULL;
char *fpga_serial=NULL;
char *serial_port=NULL; // XXX do a better job auto-detecting this
int serial_speed=2000000;
char modeline_cmd[1024]="";
int break_point=-1;
int jtag_only=0;
uint32_t zap_addr;
int zap=0;
int saw_c64_mode=0;
int saw_c65_mode=0;
int hypervisor_paused=0;
int screen_shot=0;
int screen_rows_remaining=0;
int screen_address=0;
int next_screen_address=0;
int screen_line_offset=0;
int screen_line_step=0;
int screen_width=0;
unsigned char screen_line_buffer[256];
char *type_text=NULL;
int type_text_cr=0;
#define READ_SECTOR_BUFFER_ADDRESS 0xFFD6c00
#define WRITE_SECTOR_BUFFER_ADDRESS 0xFFD6c00
int sdbuf_request_addr = 0;
unsigned char sd_sector_buf[512];
int saved_track = 0;
int saved_sector = 0;
int saved_side = 0;
void timestamp_msg(char *msg)
{
if (!start_time) start_time=time(0);
#ifdef WINDOWS
fprintf(stderr,"[T+%I64dsec] %s",(long long)time(0)-start_time,msg);
#else
fprintf(stderr,"[T+%lldsec] %s",(long long)time(0)-start_time,msg);
#endif
return;
}
int slow_write(PORT_TYPE fd,char *d,int l)
{
// UART is at 2Mbps, but we need to allow enough time for a whole line of
// writing. 100 chars x 0.5usec = 500usec. So 1ms between chars should be ok.
int i;
#if DEBUG
printf("\nWriting ");
for(i=0;i<l;i++)
{
if (d[i]>=' ') printf("%c",d[i]); else printf("[$%02X]",d[i]);
}
printf("\n");
char line[1024];
//fgets(line,1024,stdin);
#endif
for(i=0;i<l;i++)
{
if (serial_speed==4000000) do_usleep(1000*SLOW_FACTOR); else do_usleep(2000*SLOW_FACTOR);
int w=serialport_write(fd,(unsigned char *)&d[i],1);
while (w<1) {
if (serial_speed==4000000) do_usleep(500*SLOW_FACTOR); else do_usleep(1000*SLOW_FACTOR);
w=serialport_write(fd,(unsigned char *)&d[i],1);
}
}
if (unix_socket_flag)
do_usleep(30000*SLOW_FACTOR);
return 0;
}
int slow_write_safe(PORT_TYPE fd,char *d,int l)
{
// There is a bug at the time of writing that causes problems
// with the CPU's correct operation if various monitor commands
// are run when the CPU is running.
// Stopping the CPU before and then resuming it after running a
// command solves the problem.
// The only problem then is if we have a breakpoint set (like when
// getting ready to load a program), because we might accidentally
// resume the CPU when it should be stopping.
// (We can work around this by using the fact that the new UART
// monitor tells us when a breakpoint has been reached.
if (!cpu_stopped)
slow_write(fd,"t1\r",3);
slow_write(fd,d,l);
if (!cpu_stopped) {
// printf("Resuming CPU after writing string\n");
slow_write(fd,"t0\r",3);
}
return 0;
}
// From os.c in serval-dna
long long gettime_us()
{
long long retVal = -1;
do
{
struct timeval nowtv;
// If gettimeofday() fails or returns an invalid value, all else is lost!
if (gettimeofday(&nowtv, NULL) == -1)
{
break;
}
if (nowtv.tv_sec < 0 || nowtv.tv_usec < 0 || nowtv.tv_usec >= 1000000)
{
break;
}
retVal = nowtv.tv_sec * 1000000LL + nowtv.tv_usec;
}
while (0);
return retVal;
}
unsigned long long gettime_ms()
{
struct timeval nowtv;
// If gettimeofday() fails or returns an invalid value, all else is lost!
if (gettimeofday(&nowtv, NULL) == -1)
perror("gettimeofday");
return nowtv.tv_sec * 1000LL + nowtv.tv_usec / 1000;
}
int stop_cpu(void)
{
if (cpu_stopped) {
printf("CPU already stopped.\n");
return 1;
}
// Stop CPU
printf("Stopping CPU\n");
do_usleep(50000);
slow_write(fd,"t1\r",3);
cpu_stopped=1;
return 0;
}
int start_cpu(void)
{
// Stop CPU
if (cpu_stopped) {
timestamp_msg("");
fprintf(stderr,"Starting CPU\n");
}
do_usleep(50000);
slow_write(fd,"t0\r",3);
cpu_stopped=0;
return 0;
}
int load_file(char *filename,int load_addr,int patchHyppo)
{
char cmd[1024];
FILE *f=fopen(filename,"rb");
if (!f) {
fprintf(stderr,"Could not open file '%s'\n",filename);
exit(-2);
}
do_usleep(50000);
unsigned char buf[65536];
int max_bytes;
int byte_limit=4096;
max_bytes=0x10000-(load_addr&0xffff);
if (max_bytes>byte_limit) max_bytes=byte_limit;
int b=fread(buf,1,max_bytes,f);
while(b>0) {
if (patchHyppo) {
printf("patching...\n");
// Look for BIT $nnnn / BIT $1234, and change to JMP $nnnn to skip
// all SD card activities
for(int i=0;i<(b-5);i++)
{
if ((buf[i]==0x2c)
&&(buf[i+3]==0x2c)
&&(buf[i+4]==0x34)
&&(buf[i+5]==0x12)) {
fprintf(stderr,"Patching Hyppo @ $%04x to skip SD card and ROM checks.\n",
0x8000+i);
buf[i]=0x4c;
}
}
}
printf("Read to $%04x (%d bytes)\n",load_addr,b);
fflush(stdout);
// load_addr=0x400;
// XXX - The l command requires the address-1, and doesn't cross 64KB boundaries.
// Thus writing to $xxx0000 requires adding 64K to fix the actual load address
int munged_load_addr=load_addr;
if ((load_addr&0xffff)==0x0000) {
munged_load_addr+=0x10000;
}
#ifdef WINDOWS_GUS
// Windows doesn't seem to work with the l fast-load monitor command
printf("Asking Gus to write data...\n");
for(int i=0;i<b;i+=16) {
int ofs=0;
sprintf(cmd,"s%x",load_addr+i); ofs=strlen(cmd);
for(int j=0;(j<16)&&(i+j)<b;j++) { sprintf(&cmd[ofs]," %x",buf[i+j]); ofs=strlen(cmd); }
sprintf(&cmd[ofs],"\r"); ofs=strlen(cmd);
slow_write(fd,cmd,strlen(cmd));
}
#else
// The old uart monitor could handle being given a 28-bit address for the end address,
// but Kenneth's implementation requires it be a 16 bit address.
// Also, Kenneth's implementation doesn't need the -1, so we need to know which version we
// are talking to.
if (new_monitor)
sprintf(cmd,"l%x %x\r",load_addr,(load_addr+b)&0xffff);
else
sprintf(cmd,"l%x %x\r",munged_load_addr-1,(munged_load_addr+b-1)&0xffff);
// printf(" command ='%s'\n",cmd);
slow_write(fd,cmd,strlen(cmd));
do_usleep(5000*SLOW_FACTOR);
int n=b;
unsigned char *p=buf;
while(n>0) {
int w=serialport_write(fd,p,n);
if (w>0) { p+=w; n-=w; } else do_usleep(1000*SLOW_FACTOR);
}
if (serial_speed==230400) do_usleep(10000+50*b*SLOW_FACTOR);
else if (serial_speed==2000000)
// 2mbit/sec / 11bits/char (inc space) = ~5.5usec per char
do_usleep(6*b*SLOW_FACTOR2);
else
// 4mbit/sec / 11bits/char (inc space) = ~2.6usec per char
do_usleep(3*b*SLOW_FACTOR2);
#endif
load_addr+=b;
max_bytes=0x10000-(load_addr&0xffff);
if (max_bytes>byte_limit) max_bytes=byte_limit;
b=fread(buf,1,max_bytes,f);
}
fclose(f);
char msg[1024];
snprintf(msg,1024,"File '%s' loaded.\n",filename);
timestamp_msg(msg);
return 0;
}
int mega65_poke(unsigned int addr,unsigned char value)
{
return push_ram(addr,1,&value);
}
unsigned char mega65_peek(unsigned int addr)
{
unsigned char b;
fetch_ram(addr,1,&b);
return b;
}
int restart_hyppo(void)
{
// Start executing in new hyppo
if (!halt) {
printf("Re-Starting CPU in new HYPPO\n");
do_usleep(50000);
slow_write(fd,"g8100\r",6);
do_usleep(10000);
slow_write(fd,"t0\r",3);
cpu_stopped=0;
}
return 0;
}
void print_spaces(FILE *f,int col)
{
for(int i=0;i<col;i++)
printf(" ");
}
int dump_bytes(int col, char *msg,unsigned char *bytes,int length)
{
print_spaces(stderr,col);
printf("%s:\n",msg);
for(int i=0;i<length;i+=16) {
print_spaces(stderr,col);
printf("%04X: ",i);
for(int j=0;j<16;j++)
{
if (j == 8)
printf(" "); // add an extra space after the 8th byte, for easier readability
if (i+j<length) printf(" %02X",bytes[i+j]); else printf(" ");
}
printf(" | ");
for(int j=0;j<16;j++) if (i+j<length) {
if (bytes[i+j]>=0x20&&bytes[i+j]<0x7f) {
printf("%c",bytes[i+j]);
} else printf(".");
}
printf("\n");
}
return 0;
}
int first_load=1;
int first_go64=1;
unsigned char viciv_regs[0x100];
int mode_report=0;
int read_and_print(PORT_TYPE fd)
{
char buff[8192];
int r=serialport_read(fd,(unsigned char *)buff,8192);
buff[r]=0;
printf("%s\n",buff);
return 0;
}
int stuff_keybuffer(char *s)
{
int buffer_addr=0x277;
int buffer_len_addr=0xc6;
if (saw_c65_mode) {
buffer_addr=0x2b0;
buffer_len_addr=0xd0;
}
timestamp_msg("Injecting string into key buffer at ");
fprintf(stderr,"$%04X : ",buffer_addr);
for(int i=0;s[i];i++) {
if (s[i]>=' '&&s[i]<0x7c) fprintf(stderr,"%c",s[i]); else fprintf(stderr,"[$%02x]",s[i]);
}
fprintf(stderr,"\n");
char cmd[1024];
snprintf(cmd,1024,"s%x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\rs%x %02x\r",
buffer_addr,s[0],s[1],s[2],s[3],s[4],s[5],s[6],s[7],s[8],s[9],
buffer_len_addr,(int)strlen(s));
return slow_write(fd,cmd,strlen(cmd));
}
long long last_virtual_time=0;
int last_virtual_writep=0;
int last_virtual_track=-1;
int last_virtual_sector=-1;
int last_virtual_side=-1;
int virtual_f011_read(int device,int track,int sector,int side)
{
long long start=gettime_ms();
#ifdef WINDOWS
fprintf(stderr,"T+%I64d ms : Servicing hypervisor request for F011 FDC sector read.\n",
gettime_ms()-start);
#else
fprintf(stderr,"T+%lld ms : Servicing hypervisor request for F011 FDC sector read.\n",
gettime_ms()-start);
#endif
fprintf(stderr, "device: %d track: %d sector: %d side: %d\n", device, track, sector, side);
if(fd81 == NULL) {
fd81 = fopen(d81file, "rb+");
if(!fd81) {
fprintf(stderr, "Could not open D81 file: '%s'\n", d81file);
exit(-1);
}
}
// Only actually load new sector contents if we don't think it is a duplicate request
if (((gettime_ms()-last_virtual_time)>100)
||(last_virtual_writep)
||(last_virtual_track!=track)
||(last_virtual_sector!=sector)
||(last_virtual_side!=side)
)
{
last_virtual_time=gettime_ms();
last_virtual_track=track;
last_virtual_sector=sector;
last_virtual_side=side;
/* read the block */
unsigned char buf[512];
int b=-1;
int physical_sector=( side==0 ? sector-1 : sector+9 );
int result = fseek(fd81, (track*20+physical_sector)*512, SEEK_SET);
if(result) {
fprintf(stderr, "Error finding D81 sector %d @ 0x%x\n", result, (track*20+physical_sector)*512);
exit(-2);
}
else {
b=fread(buf,1,512,fd81);
fprintf(stderr, " bytes read: %d @ 0x%x\n", b,(track*20+physical_sector)*512);
if(b==512) {
// dump_bytes(0,"The sector",buf,512);
char cmd[1024];
/* send block to m65 memory */
if (new_monitor)
sprintf(cmd,"l%x %x\r",READ_SECTOR_BUFFER_ADDRESS,
(READ_SECTOR_BUFFER_ADDRESS+0x200)&0xffff);
else
sprintf(cmd,"l%x %x\r",READ_SECTOR_BUFFER_ADDRESS-1,
READ_SECTOR_BUFFER_ADDRESS+0x200-1);
slow_write(fd,cmd,strlen(cmd));
do_usleep(1000*SLOW_FACTOR);
int n=0x200;
unsigned char *p=buf;
// fprintf(stderr,"%s\n",cmd);
// dump_bytes(0,"F011 virtual sector data",p,512);
while(n>0) {
int w=serialport_write(fd,p,n);
if (w>0) { p+=w; n-=w; } else do_usleep(1000*SLOW_FACTOR);
}
if (serial_speed==230400) do_usleep(10000+50*b*SLOW_FACTOR);
else do_usleep(10000+6*b*SLOW_FACTOR);
#ifdef WINDOWS
printf("T+%I64d ms : Block sent.\n",gettime_ms()-start);
#else
printf("T+%lld ms : Block sent.\n",gettime_ms()-start);
#endif
}
}
}
/* signal done/result */
mega65_poke(0xffd3068,side);
timestamp_msg("Finished V-FDC read.\n");
return 0;
}
void show_hyppo_report(void)
{
// Buffer starats at $BC00 in HYPPO
// $BC00 - $BCFF = DOS work area
// $BD00 - $BDFF = Process Descriptor
// $BE00 - $BEFF = Stack
// $BF00 - $BFFF = ZP
unsigned char syspart_buffer[0x40];
fetch_ram(0xfffbbc0,0x40,syspart_buffer);
fetch_ram(0xfffbc00,0x400,hyppo_buffer);
printf("HYPPO status:\n");
printf("Disk count = $%02x\n",hyppo_buffer[0x001]);
printf("Default Disk = $%02x\n",hyppo_buffer[0x002]);
printf("Current Disk = $%02x\n",hyppo_buffer[0x003]);
printf("Disk Table offset = $%02x\n",hyppo_buffer[0x004]);
printf("Cluster of current directory = $%02x%02x%02x%02x\n",
hyppo_buffer[0x008],hyppo_buffer[0x007],hyppo_buffer[0x006],hyppo_buffer[0x005]);
printf("opendir_cluster = $%02x%02x%02x%02x\n",
hyppo_buffer[0x00c],hyppo_buffer[0x00b],hyppo_buffer[0x00a],hyppo_buffer[0x009]);
printf("opendir_sector = $%02x\n",hyppo_buffer[0x00d]);
printf("opendir_entry = $%02x\n",hyppo_buffer[0x00e]);
// Dirent struct follows:
// 64 bytes for file name
printf("dirent structure:\n");
printf(" Filename = '%s'\n",&hyppo_buffer[0x00f]);
printf(" Filename len = $%02x\n",hyppo_buffer[0x04f]);
printf(" Short name = '%s'\n",&hyppo_buffer[0x050]);
printf(" Start cluster = $%02x%02x%02x%02x\n",
hyppo_buffer[0x060],hyppo_buffer[0x05f],hyppo_buffer[0x05e],hyppo_buffer[0x05d]);
printf(" File length = $%02x%02x%02x%02x\n",
hyppo_buffer[0x064],hyppo_buffer[0x063],hyppo_buffer[0x062],hyppo_buffer[0x061]);
printf(" ATTRIB byte = $%02x\n",hyppo_buffer[0x065]);
printf("Requested filename len = $%02x\n",hyppo_buffer[0x66]);
printf("Requested filename = '%s'\n",&hyppo_buffer[0x67]);
printf("sectorsread = $%02x%02x\n",
hyppo_buffer[0xaa],hyppo_buffer[0xa9]);
printf("bytes_remaining = $%02x%02x%02x%02x\n",
hyppo_buffer[0xae],hyppo_buffer[0x0ad],hyppo_buffer[0xac],hyppo_buffer[0xab]);
printf("current sector = $%02x%02x%02x%02x, ",
hyppo_buffer[0xb2],hyppo_buffer[0x0b1],hyppo_buffer[0xb0],hyppo_buffer[0xaf]);
printf("current cluster = $%02x%02x%02x%02x\n",
hyppo_buffer[0xb6],hyppo_buffer[0x0b5],hyppo_buffer[0xb4],hyppo_buffer[0xb3]);
printf("current sector in cluster = $%02x\n",hyppo_buffer[0xb7]);
for(int fd=0;fd<4;fd++) {
int fd_o=0xb8+fd*0x10;
printf("File descriptor #%d:\n",fd);
printf(" disk ID = $%02x, ",hyppo_buffer[fd_o+0]);
printf(" mode = $%02x\n",hyppo_buffer[fd_o+1]);
printf(" start cluster = $%02x%02x%02x%02x, ",
hyppo_buffer[fd_o+5],hyppo_buffer[fd_o+4],hyppo_buffer[fd_o+3],hyppo_buffer[fd_o+2]);
printf(" current cluster = $%02x%02x%02x%02x\n",
hyppo_buffer[fd_o+9],hyppo_buffer[fd_o+8],hyppo_buffer[fd_o+7],hyppo_buffer[fd_o+6]);
printf(" sector in cluster = $%02x, ",hyppo_buffer[fd_o+10]);
printf(" offset in sector = $%02x%02x\n",hyppo_buffer[fd_o+12],hyppo_buffer[fd_o+11]);
// printf(" file offset = $%02x%02x (x 256 bytes? not used?)\n",
// hyppo_buffer[fd_o+14],hyppo_buffer[fd_o+13]);
}
printf("Current file descriptor # = $%02x\n",hyppo_buffer[0xf4]);
printf("Current file descriptor offset = $%02x\n",hyppo_buffer[0xf5]);
printf("Dos error code = $%02x\n",hyppo_buffer[0xf6]);
printf("SYSPART error code = $%02x\n",hyppo_buffer[0xf7]);
printf("SYSPART present = $%02x\n",hyppo_buffer[0xf8]);
printf("SYSPART start = $%02x%02x%02x%02x\n",
syspart_buffer[3],syspart_buffer[2],syspart_buffer[1],syspart_buffer[0]);
}
int monitor_sync(void)
{
/* Synchronise with the monitor interface.
Send #<token> until we see the token returned to us.
*/
unsigned char read_buff[8192];
// Begin by sending a null command and purging input
char cmd[8192];
cmd[0]=0x15; // ^U
cmd[1]='#'; // prevent instruction stepping
cmd[2]=0x0d; // Carriage return
do_usleep(20000); // Give plenty of time for things to settle
slow_write_safe(fd,cmd,3);
// printf("Wrote empty command.\n");
do_usleep(20000); // Give plenty of time for things to settle
int b=1;
// Purge input
// printf("Purging input.\n");
while(b>0) {
b=serialport_read(fd,read_buff,8192);
// if (b>0) dump_bytes(2,"Purged input",read_buff,b);
}
for(int tries=0;tries<10;tries++) {
#ifdef WINDOWS
snprintf(cmd,1024,"#%08x\r",rand());
#else
snprintf(cmd,1024,"#%08lx\r",random());
#endif
// printf("Writing token: '%s'\n",cmd);
slow_write_safe(fd,cmd,strlen(cmd));
for(int i=0;i<10;i++) {
do_usleep(10000*SLOW_FACTOR);
b=serialport_read(fd,read_buff,8192);
if (b<0) b=0;
if (b>8191) b=8191;
read_buff[b]=0;
// if (b>0) dump_bytes(2,"Sync input",read_buff,b);
// if (b>0) dump_bytes(0,"read_data",read_buff,b);
if (strstr((char *)read_buff,cmd)) {
// printf("Found token. Synchronised with monitor.\n");
state=99;
return 0;
}
}
do_usleep(10000*SLOW_FACTOR);
}
printf("Failed to synchronise with the monitor.\n");
return 1;
}
void progress_to_RTI(void)
{
int bytes=0;
int match_state=0;
int b=0;
unsigned char buff[8192];
slow_write_safe(fd,"tc\r",3);
while(1) {
b=serialport_read(fd,buff,8192);
if (b>0) dump_bytes(2,"RTI search input",buff,b);
if (b>0) {
bytes+=b;
buff[b]=0;
for(int i=0;i<b;i++) {
if (match_state==0&&buff[i]=='R') { match_state=1; }
else if (match_state==1&&buff[i]=='T') { match_state=2; }
else if (match_state==2&&buff[i]=='I') {
slow_write_safe(fd,"\r",1);
fprintf(stderr,"RTI seen after %d bytes\n",bytes);
return;
} else match_state=0;
}
}
fflush(stdout);
}
}
int get_pc(void)
{
/*
Get current programme counter value of CPU
*/
slow_write_safe(fd,"r\r",2);
do_usleep(50000);
unsigned char buff[8192];
int b=serialport_read(fd,buff,8192);
if (b<0) b=0;
if (b>8191) b=8191;
buff[b]=0;
// if (b>0) dump_bytes(2,"PC read input",buff,b);
char *s=strstr((char *)buff,"\n,");
if (s) return strtoll(&s[6],NULL,16);
else return -1;
}
int breakpoint_pc=-1;
int breakpoint_set(int pc)
{
char cmd[8192];
monitor_sync();
start_cpu();
snprintf(cmd,8192,"b%x\r",pc);
breakpoint_pc=pc;
slow_write(fd,cmd,strlen(cmd));
// XXX any t0 or t1 cancels a queued breakpoint,
// so must be avoided
return 0;
}
int breakpoint_wait(void)
{
char read_buff[8192];
char pattern[16];
snprintf(pattern,16,"\n,077");
int match_state=0;
// Now read until we see the requested PC
timestamp_msg("");
fprintf(stderr,"Waiting for breakpoint at $%04X to trigger.\n",breakpoint_pc);
while(1) {
int b=serialport_read(fd,(unsigned char *)read_buff,8192);
for(int i=0;i<b;i++) {
if (read_buff[i]==pattern[match_state]) {
if (match_state==4) {
timestamp_msg("");
fprintf(stderr,"Breakpoint @ $%04X triggered.\n",breakpoint_pc);
slow_write(fd,"t1\r",3);
cpu_stopped=1;
// printf("stopped following breakpoing.\n");
return 0;
} else match_state++;
} else {
match_state=0;
}
}
// if (b>0) dump_bytes(2,"Breakpoint wait input",read_buff,b);
}
}
int push_ram(unsigned long address,unsigned int count,unsigned char *buffer)
{
char cmd[8192];
for(unsigned int offset=0;offset<count;)
{
int b=count-offset;
// Limit to same 64KB slab
if (b>(0xffff-((address+offset)&0xffff)))
b=(0xffff-((address+offset)&0xffff));
if (b>4096) b=4096;
monitor_sync();
if (new_monitor)
sprintf(cmd,"l%lx %lx\r",address+offset,(address+offset+b)&0xffff);
else
sprintf(cmd,"l%lx %lx\r",address+offset-1,address+offset+b-1);
slow_write(fd,cmd,strlen(cmd));
do_usleep(1000*SLOW_FACTOR);
int n=b;
unsigned char *p=&buffer[offset];
while(n>0) {
int w=serialport_write(fd,p,n);
if (w>0) { p+=w; n-=w; } else do_usleep(1000*SLOW_FACTOR);
}
if (serial_speed==230400) do_usleep(10000+50*b*SLOW_FACTOR);
else if (serial_speed==2000000)
// 2mbit/sec / 11bits/char (inc space) = ~5.5usec per char
do_usleep(6*b*SLOW_FACTOR2);
else
// 4mbit/sec / 11bits/char (inc space) = ~2.6usec per char
do_usleep(3*b*SLOW_FACTOR2);
offset+=b;
}
return 0;
}
int fetch_ram(unsigned long address,unsigned int count,unsigned char *buffer)
{
/* Fetch a block of RAM into the provided buffer.
This greatly simplifies many tasks.
*/
unsigned long addr=address;
unsigned long end_addr;
char cmd[8192];
unsigned char read_buff[8192];
char next_addr_str[8192];
int ofs=0;
// fprintf(stderr,"Fetching $%x bytes @ $%x\n",count,address);
#ifdef __CYGWIN__
monitor_sync();
#endif
while(addr<(address+count)) {
if ((address+count-addr)<17) {
snprintf(cmd,8192,"m%X\r",(unsigned int)addr);
end_addr=addr+0x10;
} else {
snprintf(cmd,8192,"M%X\r",(unsigned int)addr);
end_addr=addr+0x100;
}
//printf("Sending '%s'\n",cmd);
slow_write_safe(fd,cmd,strlen(cmd));
while(addr!=end_addr) {
snprintf(next_addr_str,8192,"\n:%08X:",(unsigned int)addr);
int b=serialport_read(fd,&read_buff[ofs],8192-ofs);
if (b<0) b=0;
if ((ofs+b)>8191) b=8191-ofs;
// if (b) dump_bytes(0,"read data",&read_buff[ofs],b);
read_buff[ofs+b]=0;
ofs+=b;
char *s=strstr((char *)read_buff,next_addr_str);
if (s&&(strlen(s)>=42)) {
char b=s[42]; s[42]=0;
if (0) {
printf("Found data for $%08x:\n%s\n",
(unsigned int)addr,
s);
}
s[42]=b;
for(int i=0;i<16;i++) {
// Don't write more bytes than requested
if ((addr-address+i)>=count) break;
char hex[3];
hex[0]=s[1+10+i*2+0];
hex[1]=s[1+10+i*2+1];
hex[2]=0;
buffer[addr-address+i]=strtol(hex,NULL,16);
}