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