Mercurial > hg > Members > kono > os9 > sbc09
annotate io.c @ 2:31d96e2b364e
add virtual hd option to v09
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Mon, 02 Jul 2018 21:39:55 +0900 |
parents | 3c736a81b886 |
children | 831ac057ea86 |
rev | line source |
---|---|
0 | 1 /* 6809 Simulator V09. |
2 | |
3 created 1993,1994 by L.C. Benschop. | |
4 copyleft (c) 1994-2014 by the sbc09 team, see AUTHORS for more details. | |
5 license: GNU General Public License version 2, see LICENSE for more details. | |
6 | |
7 This program simulates a 6809 processor. | |
8 | |
9 System dependencies: short must be 16 bits. | |
10 char must be 8 bits. | |
11 long must be more than 16 bits. | |
12 arrays up to 65536 bytes must be supported. | |
13 machine must be twos complement. | |
14 Most Unix machines will work. For MSODS you need long pointers | |
15 and you may have to malloc() the mem array of 65536 bytes. | |
16 | |
17 Define BIG_ENDIAN if you have a big-endian machine (680x0 etc) | |
18 | |
19 Special instructions: | |
20 SWI2 writes char to stdout from register B. | |
21 SWI3 reads char from stdout to register B, sets carry at EOF. | |
22 (or when no key available when using term control). | |
23 SWI retains its normal function. | |
24 CWAI and SYNC stop simulator. | |
25 | |
26 */ | |
27 | |
28 #include<stdio.h> | |
29 #include<stdlib.h> | |
30 #include<ctype.h> | |
31 #include<signal.h> | |
32 #include<sys/time.h> | |
33 | |
34 #include <unistd.h> | |
35 #include <fcntl.h> | |
36 #include <string.h> | |
1 | 37 #include <time.h> |
0 | 38 |
39 #ifdef USE_TERMIOS | |
40 #include <termios.h> | |
41 #endif | |
42 | |
43 #define engine extern | |
44 #include "v09.h" | |
45 | |
1 | 46 /* |
47 * IO Map | |
48 * | |
49 * 0xe000 - 0xe100 | |
50 * | |
51 * 0xe000 ACIA control | |
52 * 0xe001 ACIA data | |
53 * | |
54 * 0xe010 Timer control 0x8f start timer/0x80 stop timer/0x04 update date | |
55 * 0xe011- YY/MM/DD/HH/MM/SS | |
56 * | |
57 * 0xe020 Disk control 0x81 read/0x55 write 0 ... ok / 0xff .. error | |
58 * 0xe021 drive no | |
59 * 0xe022 LSN2 | |
60 * 0xe023 LSN1 | |
61 * 0xe024 LSN0 | |
62 * 0xe025 ADR2 | |
63 * 0xe026 ADR1 | |
64 * | |
65 */ | |
66 | |
67 #define SECSIZE 256 | |
68 | |
0 | 69 int tflags; |
70 int timer = 1; | |
71 struct termios termsetting; | |
72 | |
73 int xmstat; /* 0= no XMODEM transfer, 1=send, 2=receiver */ | |
74 unsigned char xmbuf[132]; | |
75 int xidx; | |
76 int acknak; | |
77 int rcvdnak; | |
78 int blocknum; | |
79 | |
80 FILE *logfile; | |
81 FILE *infile; | |
82 FILE *xfile; | |
1 | 83 FILE *disk[] = {0,0}; |
0 | 84 |
85 extern void hexadump( unsigned char *b, int l, int loc, int w); | |
86 extern void disasm(int,int); | |
87 | |
1 | 88 void do_timer(int,int); |
89 void do_disk(int,int); | |
0 | 90 |
91 int char_input(void) { | |
92 int c, w, sum; | |
93 if (!xmstat) { | |
94 if (infile) { | |
95 c = getc(infile); | |
96 if (c == EOF) { | |
97 fclose(infile); | |
98 infile = 0; | |
99 return char_input(); | |
100 } | |
101 if (c == '\n') | |
102 c = '\r'; | |
103 return c; | |
2
31d96e2b364e
add virtual hd option to v09
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1
diff
changeset
|
104 } else { |
31d96e2b364e
add virtual hd option to v09
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1
diff
changeset
|
105 usleep(100); |
0 | 106 return getchar(); |
2
31d96e2b364e
add virtual hd option to v09
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1
diff
changeset
|
107 } |
0 | 108 } else if (xmstat == 1) { |
109 if (xidx) { | |
110 c = xmbuf[xidx++]; | |
111 if (xidx == 132) { | |
112 xidx = 0; | |
113 rcvdnak = EOF; | |
114 acknak = 6; | |
115 } | |
116 } else { | |
117 if ((acknak == 21 && rcvdnak == 21) || (acknak == 6 && rcvdnak == 6)) { | |
118 rcvdnak = 0; | |
119 memset(xmbuf, 0, 132); | |
120 w = fread(xmbuf + 3, 1, 128, xfile); | |
121 if (w) { | |
122 printf("Block %3d transmitted, ", blocknum); | |
123 xmbuf[0] = 1; | |
124 xmbuf[1] = blocknum; | |
125 xmbuf[2] = 255 - blocknum; | |
126 blocknum = (blocknum + 1) & 255; | |
127 sum = 0; | |
128 for (w = 3; w < 131; w++) | |
129 sum = (sum + xmbuf[w]) & 255; | |
130 xmbuf[131] = sum; | |
131 acknak = 6; | |
132 c = 1; | |
133 xidx = 1; | |
134 } else { | |
135 printf("EOT transmitted, "); | |
136 acknak = 4; | |
137 c = 4; | |
138 } | |
139 } else if (rcvdnak == 21) { | |
140 rcvdnak = 0; | |
141 printf("Block %3d retransmitted, ", xmbuf[1]); | |
142 c = xmbuf[xidx++]; /*retransmit the same block */ | |
143 } else | |
144 c = EOF; | |
145 } | |
146 return c; | |
147 } else { | |
148 if (acknak == 4) { | |
149 c = 6; | |
150 acknak = 0; | |
151 fclose(xfile); | |
152 xfile = 0; | |
153 xmstat = 0; | |
154 } else if (acknak) { | |
155 c = acknak; | |
156 acknak = 0; | |
157 } else | |
158 c = EOF; | |
159 if (c == 6) | |
160 printf("ACK\n"); | |
161 if (c == 21) | |
162 printf("NAK\n"); | |
163 return c; | |
164 } | |
165 } | |
166 | |
167 int do_input( a) { | |
168 static int c, f = EOF; | |
169 if (a == 0) { | |
170 if (f == EOF) | |
171 f = char_input(); | |
172 if (f != EOF) | |
173 c = f; | |
174 return 2 + (f != EOF); | |
175 } else if (a == 1) { /*data port*/ | |
176 if (f == EOF) | |
177 f = char_input(); | |
178 if (f != EOF) { | |
179 c = f; | |
180 f = EOF; | |
181 } | |
182 return c; | |
1 | 183 } else if ((a&0xf0) == 0x10) { /* timer */ |
184 return mem[IOPAGE + a]; | |
185 } else if ((a&0xf0) == 0x20) { /* disk */ | |
186 return mem[IOPAGE + a]; | |
0 | 187 } |
188 return 0; | |
189 } | |
190 | |
191 void do_output(int a, int c) { | |
192 int i, sum; | |
193 if (a == 1) { /* ACIA data port,ignore address */ | |
194 if (!xmstat) { | |
195 if (logfile && c != 127 && (c >= ' ' || c == '\n')) | |
196 putc(c, logfile); | |
197 putchar(c); | |
198 fflush(stdout); | |
199 } else if (xmstat == 1) { | |
200 rcvdnak = c; | |
201 if (c == 6 && acknak == 4) { | |
202 fclose(xfile); | |
203 xfile = 0; | |
204 xmstat = 0; | |
205 } | |
206 if (c == 6) | |
207 printf("ACK\n"); | |
208 if (c == 21) | |
209 printf("NAK\n"); | |
210 if (c == 24) { | |
211 printf("CAN\n"); | |
212 fclose(xfile); | |
213 xmstat = 0; | |
214 xfile = 0; | |
215 } | |
216 } else { | |
217 if (xidx == 0 && c == 4) { | |
218 acknak = 4; | |
219 printf("EOT received, "); | |
220 } | |
221 xmbuf[xidx++] = c; | |
222 if (xidx == 132) { | |
223 sum = 0; | |
224 for (i = 3; i < 131; i++) | |
225 sum = (sum + xmbuf[i]) & 255; | |
226 if (xmbuf[0] == 1 && xmbuf[1] == 255 - xmbuf[2] | |
227 && sum == xmbuf[131]) | |
228 acknak = 6; | |
229 else | |
230 acknak = 21; | |
231 printf("Block %3d received, ", xmbuf[1]); | |
232 if (blocknum == xmbuf[1]) { | |
233 blocknum = (blocknum + 1) & 255; | |
234 fwrite(xmbuf + 3, 1, 128, xfile); | |
235 } | |
236 xidx = 0; | |
237 } | |
238 } | |
1 | 239 } else if ((a&0xf0) == 0x10) { /* timer */ |
240 do_timer(a,c); | |
241 } else if ((a&0xf0) == 0x20) { /* disk */ | |
242 do_disk(a,c); | |
0 | 243 } |
244 } | |
245 | |
246 void restore_term(void) { | |
247 tcsetattr(0, TCSAFLUSH, &termsetting); | |
248 fcntl(0, F_SETFL, tflags); | |
249 signal(SIGALRM, SIG_IGN); | |
250 } | |
251 | |
252 void do_exit(void) { | |
253 restore_term(); | |
254 exit(0); | |
255 } | |
256 | |
1 | 257 void do_timer(int a, int c) { |
258 struct itimerval timercontrol; | |
259 if (a==0x10 && c==0x8f) { | |
260 timercontrol.it_interval.tv_sec = 0; | |
261 timercontrol.it_interval.tv_usec = 20000; | |
262 timercontrol.it_value.tv_sec = 0; | |
263 timercontrol.it_value.tv_usec = 20000; | |
264 setitimer(ITIMER_REAL, &timercontrol, NULL); | |
265 } else if (a==0x10 && c==0x80) { | |
266 timercontrol.it_interval.tv_sec = 0; | |
267 timercontrol.it_interval.tv_usec = 0; | |
268 setitimer(ITIMER_REAL, &timercontrol, NULL); | |
269 } else if (a==0x10 && c==0x04) { | |
270 time_t tm = time(0); | |
271 struct tm *t = localtime(&tm); | |
272 mem[IOPAGE+0x11] = t->tm_year; | |
273 mem[IOPAGE+0x12] = t->tm_mon; | |
274 mem[IOPAGE+0x13] = t->tm_mday; | |
275 mem[IOPAGE+0x14] = t->tm_hour; | |
276 mem[IOPAGE+0x15] = t->tm_min; | |
277 mem[IOPAGE+0x16] = t->tm_sec; | |
278 } else { | |
279 mem[IOPAGE+a]=c; | |
280 } | |
281 } | |
282 | |
283 void do_disk(int a, int c) { | |
284 if (a!=0x20) { | |
285 mem[IOPAGE+a]=c; | |
286 return; | |
287 } | |
288 int drv = mem[IOPAGE+0x21]; | |
289 int lsn = (mem[IOPAGE+0x22]<<16) + (mem[IOPAGE+0x23]<<8) + mem[IOPAGE+0x24]; | |
290 int buf = (mem[IOPAGE+0x25]<<8) + mem[IOPAGE+0x26]; | |
291 if (drv > 1 || disk[drv]==0) goto error; | |
292 if (c==0x81) { | |
293 if (lseek(fileno(disk[drv]),lsn*SECSIZE,SEEK_SET)==-1) goto error; | |
294 if (read(fileno(disk[drv]),&mem[buf],SECSIZE)==-1) goto error; | |
295 } else if (c==0x55) { | |
296 if (lseek(fileno(disk[drv]),lsn*SECSIZE,SEEK_SET)==-1) goto error; | |
297 if (write(fileno(disk[drv]),&mem[buf],SECSIZE)==-1) goto error; | |
298 } | |
299 mem[IOPAGE+0x20] = 0; | |
300 return; | |
301 error : | |
302 mem[IOPAGE+0x20] = 0xff; | |
303 } | |
304 | |
0 | 305 typedef struct bp { |
306 int address; | |
307 int count; | |
308 struct bp *next; | |
309 } BP, *BPTR; | |
310 | |
311 BPTR breakpoint = 0; | |
312 int bpskip = 0; | |
313 int trskip = 0; | |
314 int stkskip = 0; | |
315 | |
316 int getarg(char *buf, char** next) { | |
317 return strtol(buf,(char**)next,0); | |
318 } | |
319 | |
320 void printhelp(void) | |
321 { | |
322 printf( | |
323 " s [count] one step trace\n" | |
324 " n step over\n" | |
325 " f finish this call (until stack pop)\n" | |
326 " b [adr] set break point\n" | |
327 " l break point list\n" | |
328 " d [n] delte break point list\n" | |
329 " c [count] continue;\n" | |
330 " x [adr] dump\n" | |
331 " xi [adr] disassemble\n" | |
1 | 332 " 0 file disk drive 0 image\n" |
333 " 1 file disk drive 1 image\n" | |
0 | 334 " L file start log to file\n" |
335 " S file set input file\n" | |
336 " X exit\n" | |
337 " q exit\n" | |
338 " U file upload from srecord file \n" | |
339 " D file download to srecord file \n" | |
340 " R do reset\n" | |
341 " h,? print this\n" | |
342 ); | |
343 } | |
344 | |
345 void do_escape(void) { | |
346 char s[80]; | |
347 int adr,skip; | |
348 if (bpskip) { // skip unbreak instruction | |
349 bpskip--; | |
350 for(BPTR b = breakpoint; b ; b=b->next) { | |
351 if (pcreg==b->address) { | |
352 if (b->count) b->count--; | |
353 if (b->count==0) { | |
354 goto restart0; | |
355 } | |
356 } | |
357 } | |
358 return; | |
359 } | |
360 if (stkskip) { // skip until return | |
361 if (sreg < stkskip ) return; | |
362 } | |
363 restart0: | |
364 stkskip = 0; | |
365 restore_term(); | |
366 do_trace(stdout); | |
367 if (trskip>1) { // show trace and step | |
368 trskip--; | |
369 set_term(escchar); | |
370 return; | |
371 } | |
372 restart: | |
373 printf("v09>"); | |
374 fgets(s, 80, stdin); | |
375 if (s[0]) | |
376 s[strlen(s) - 1] = 0; | |
377 switch (s[0]) { | |
378 case 's': // one step trace | |
379 trskip = 1; | |
380 if (s[1]) { | |
381 trskip = getarg(s+1,0); | |
382 } | |
383 bpskip = 0; | |
384 attention = escape = 1; | |
385 break; | |
386 case 'n': // step over | |
387 stkskip = sreg; | |
388 attention = escape = 1; | |
389 break; | |
390 case 'f': // finish this call (until stack pop) | |
391 stkskip = sreg + 2; | |
392 attention = escape = 1; | |
393 break; | |
394 case 'b': // set break point | |
395 { | |
396 BPTR bp = calloc(1,sizeof(BP)); | |
397 bp->next = breakpoint; | |
398 breakpoint = bp; | |
399 bp->count = 1; | |
400 if (s[1]) { | |
401 char *next; | |
402 bp->address = getarg(s+1,&next); | |
403 if (next[0]) { | |
404 bp->count = getarg(next,&next); | |
405 } | |
406 } else { | |
407 bp->address = pcreg; | |
408 } | |
409 } | |
410 bpskip = -1; | |
411 goto restart; | |
412 case 'l': // break point list | |
413 for(BPTR bp = breakpoint; bp ; bp = bp->next) { | |
414 printf("%x %i\n", bp->address, bp->count); | |
415 } | |
416 goto restart; | |
417 case 'd': // delte break point list | |
418 if (s[1]) { | |
419 int trskip = getarg(s+1,0); | |
420 BPTR *prev = &breakpoint; | |
421 for(BPTR bp = breakpoint; bp ; bp = bp->next) { | |
422 if (trskip-- == 0) { | |
423 if (bp) { | |
424 *prev = bp->next; | |
425 } | |
426 break; | |
427 } | |
428 prev = &bp->next; | |
429 } | |
430 } | |
431 goto restart; | |
432 case 'c': // continue; | |
433 bpskip = -1; | |
434 attention = escape = 1; | |
435 if (s[1]) { | |
436 bpskip = getarg(s+1,0); | |
437 } | |
438 break; | |
439 case 'x': // dump | |
440 skip = 1; | |
441 if (s[1]=='i') skip=2; | |
442 if (s[skip]) { | |
443 char *next; | |
444 int adr = getarg(s+skip,&next); | |
445 int len = 32; | |
446 if (next[0]) { | |
447 len = getarg(next,&next); | |
448 } | |
449 if (skip==2) { | |
450 disasm(adr,adr+len); | |
451 } else { | |
452 for(int i=0; len > 0 ; i+=16, len-=16) { | |
453 hexadump(mem+adr+i,len>16?16:len,adr+i,16); | |
454 } | |
455 } | |
456 } else | |
457 disasm(pcreg,pcreg+32); | |
458 goto restart; | |
459 case 'L': | |
460 if (logfile) | |
461 fclose(logfile); | |
462 logfile = 0; | |
463 if (s[1]) { | |
1 | 464 int i=1; while(s[i]==' ') i++; |
465 logfile = fopen(s + i, "w"); | |
0 | 466 } |
467 break; | |
468 case 'S': | |
469 if (infile) | |
470 fclose(infile); | |
471 infile = 0; | |
472 if (s[1]) { | |
1 | 473 int i=1; while(s[i]==' ') i++; |
474 infile = fopen(s + i, "r"); | |
0 | 475 } |
476 break; | |
477 case 'h': | |
478 case '?': | |
479 printhelp(); | |
480 goto restart; | |
481 case 'X': | |
482 case 'q': | |
483 if (!xmstat) | |
484 do_exit(); | |
485 else { | |
486 xmstat = 0; | |
487 fclose(xfile); | |
488 xfile = 0; | |
489 } | |
490 break; | |
1 | 491 case '0': |
492 case '1': | |
493 { FILE **drv = &disk[ s[0]-'0'] ; | |
494 if (*drv) | |
495 fclose(*drv); | |
496 *drv = 0; | |
497 if (s[1]) { | |
498 int i=1; while(s[i]==' ') i++; | |
499 *drv = fopen(s + i, "r+b"); | |
500 if ( *drv == 0 ) { printf("can't open %s\n", &s[i]); } | |
501 } | |
502 } | |
503 break; | |
0 | 504 case 'U': |
505 if (xfile) | |
506 fclose(xfile); | |
507 xfile = 0; | |
508 if (s[1]) { | |
1 | 509 int i=1; while(s[i]==' ') i++; |
510 xfile = fopen(s + i, "rb"); | |
511 if ( xfile == 0 ) { printf("can't open %s\n", &s[i]); } | |
0 | 512 } |
513 if (xfile) | |
514 xmstat = 1; | |
515 else | |
516 xmstat = 0; | |
517 xidx = 0; | |
518 acknak = 21; | |
519 rcvdnak = EOF; | |
520 blocknum = 1; | |
521 break; | |
522 case 'D': | |
523 if (xfile) | |
524 fclose(xfile); | |
525 xfile = 0; | |
526 if (s[1]) { | |
1 | 527 int i=1; while(s[i]==' ') i++; |
528 xfile = fopen(s + i, "wb"); | |
529 if ( xfile == 0 ) { printf("can't open %s\n", &s[i]); } | |
0 | 530 } |
531 if (xfile) | |
532 xmstat = 2; | |
533 else | |
534 xmstat = 0; | |
535 xidx = 0; | |
536 acknak = 21; | |
537 blocknum = 1; | |
538 break; | |
539 case 'R': | |
540 pcreg = (mem[0xfffe] << 8) + mem[0xffff]; | |
541 break; | |
542 } | |
543 if (tracing||breakpoint||trskip||bpskip||stkskip) { attention = escape = 1; } | |
544 else attention = 0; | |
545 set_term(escchar); | |
546 } | |
547 | |
548 void timehandler(int sig) { | |
549 attention = 1; | |
550 irq = 2; | |
551 signal(SIGALRM, timehandler); | |
552 } | |
553 | |
554 void handler(int sig) { | |
555 escape = 1; | |
556 attention = 1; | |
557 bpskip = 0; | |
558 stkskip = 0; | |
559 } | |
560 | |
561 void set_term(char c) { | |
562 struct termios newterm; | |
563 struct itimerval timercontrol; | |
564 signal(SIGQUIT, SIG_IGN); | |
565 signal(SIGTSTP, SIG_IGN); | |
566 signal(SIGINT, handler); | |
567 signal(SIGUSR1, handler); | |
568 tcgetattr(0, &termsetting); | |
569 newterm = termsetting; | |
570 newterm.c_iflag = newterm.c_iflag & ~INLCR & ~ICRNL; | |
571 newterm.c_lflag = newterm.c_lflag & ~ECHO & ~ICANON; | |
572 newterm.c_cc[VTIME] = 0; | |
573 newterm.c_cc[VMIN] = 1; | |
574 newterm.c_cc[VINTR] = escchar; | |
575 tcsetattr(0, TCSAFLUSH, &newterm); | |
576 tflags = fcntl(0, F_GETFL, 0); | |
577 fcntl(0, F_SETFL, tflags | O_NDELAY); /* Make input from stdin non-blocking */ | |
578 signal(SIGALRM, timehandler); | |
579 timercontrol.it_interval.tv_sec = 0; | |
580 timercontrol.it_interval.tv_usec = 20000; | |
581 timercontrol.it_value.tv_sec = 0; | |
582 timercontrol.it_value.tv_usec = 20000; | |
583 if (timer) | |
584 setitimer(ITIMER_REAL, &timercontrol, NULL); | |
585 } |