Mercurial > hg > Members > kono > os9 > sbc09
annotate io.c @ 49:947cbecdd8d5
read and dir worked.
exec failed
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Sat, 21 Jul 2018 19:25:53 +0900 |
parents | ea1b17311bf3 |
children | 51b437557f42 |
rev | line source |
---|---|
9 | 1 /* 6808 Simulator V092 |
34 | 2 created 1993,1994 by L.C. Benschop. copyleft (c) 1994-2014 |
3 by the sbc09 team, see AUTHORS for more details. license: GNU | |
4 General Public License version 2, see LICENSE for more details. | |
0 | 5 |
6 This program simulates a 6809 processor. | |
7 | |
8 System dependencies: short must be 16 bits. | |
9 char must be 8 bits. | |
10 long must be more than 16 bits. | |
11 arrays up to 65536 bytes must be supported. | |
12 machine must be twos complement. | |
13 Most Unix machines will work. For MSODS you need long pointers | |
14 and you may have to malloc() the mem array of 65536 bytes. | |
15 | |
16 Define BIG_ENDIAN if you have a big-endian machine (680x0 etc) | |
17 | |
18 Special instructions: | |
19 SWI2 writes char to stdout from register B. | |
20 SWI3 reads char from stdout to register B, sets carry at EOF. | |
21 (or when no key available when using term control). | |
22 SWI retains its normal function. | |
23 CWAI and SYNC stop simulator. | |
24 | |
25 */ | |
26 | |
27 #include<stdio.h> | |
28 #include<stdlib.h> | |
29 #include<ctype.h> | |
30 #include<signal.h> | |
31 #include<sys/time.h> | |
32 | |
33 #include <unistd.h> | |
34 #include <fcntl.h> | |
35 #include <string.h> | |
1 | 36 #include <time.h> |
0 | 37 |
38 #ifdef USE_TERMIOS | |
39 #include <termios.h> | |
40 #endif | |
41 | |
42 #define engine extern | |
43 #include "v09.h" | |
44 | |
1 | 45 /* |
3 | 46 * IO Map ( can be overrupped by ROM ) |
47 * | |
46 | 48 * In do_input/do_output call, we cannot access 6809 registers, since it is in i*reg, |
49 * which is a local variable of interpr | |
50 * | |
7
a6db579d8c11
level 2 rom preparing...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
4
diff
changeset
|
51 * IOPAGE ~ IOPAGE+0x7f |
a6db579d8c11
level 2 rom preparing...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
4
diff
changeset
|
52 * for OS9 level2 |
a6db579d8c11
level 2 rom preparing...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
4
diff
changeset
|
53 * IOPAGE 0xff80 means ioport beging 0xff80 but IOPAGE itself starts 0xff00 |
34 | 54 * 0xfe00-0xff7f, 0xffe0-0xffff can be used as RAM in fixed area in level2 |
55 * and these are ROM in level1 | |
56 * | |
1 | 57 * |
3 | 58 * IOPAGE + 0x00 ACIA control |
59 * IOPAGE + 0x01 ACIA data | |
1 | 60 * |
7
a6db579d8c11
level 2 rom preparing...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
4
diff
changeset
|
61 * IOPAGE + 0x11 MMU Taskreg 0 system map, 1 user map |
a6db579d8c11
level 2 rom preparing...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
4
diff
changeset
|
62 * IOPAGE + 0x20-0x27 MMU reg system map |
a6db579d8c11
level 2 rom preparing...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
4
diff
changeset
|
63 * IOPAGE + 0x28-0x2f MMU reg user map |
3 | 64 * |
65 * on reset tr==0 and only IOPAGE is valid | |
66 * translatation occur only on non-IOPAGE | |
67 * mem == phymem + 0x70000 | |
68 * phy addr = phymem[ ( mmu[ adr >> 13 ] <<13 ) + (adr & 0x1fff ) ] | |
69 * tr=0 mmu=IOPAGE+0xa0 | |
70 * tr=1 mmu=IOPAGE+0xa8 | |
1 | 71 * |
7
a6db579d8c11
level 2 rom preparing...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
4
diff
changeset
|
72 * IOPAGE + 0x30 Timer control 0x8f start timer/0x80 stop timer/0x04 update date |
34 | 73 * read 0x10 bit menas timer |
7
a6db579d8c11
level 2 rom preparing...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
4
diff
changeset
|
74 * IOPAGE + 0x31- YY/MM/DD/HH/MM/SS |
a6db579d8c11
level 2 rom preparing...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
4
diff
changeset
|
75 * |
a6db579d8c11
level 2 rom preparing...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
4
diff
changeset
|
76 * IOPAGE + 0x40 Disk control 0x81 read/0x55 write 0 ... ok / 0xff .. error |
45 | 77 * 0xd1- VDISK command |
78 * IOPAGE + 0x41 drive no / VDISK drv | |
79 * IOPAGE + 0x42 LSN2 / VDISK sysmode 0 for system, 1 for user | |
49 | 80 * IOPAGE + 0x43 LSN1 |
81 * IOPAGE + 0x44 LSN0 / VDISK Curdir pd number | |
46 | 82 * IOPAGE + 0x45 ADR2 / VDISK caller stack |
7
a6db579d8c11
level 2 rom preparing...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
4
diff
changeset
|
83 * IOPAGE + 0x46 ADR1 |
47 | 84 * IOPAGE + 0x47 / VDISK path descriptor address (Y) |
85 * IOPAGE + 0x48 | |
7
a6db579d8c11
level 2 rom preparing...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
4
diff
changeset
|
86 * |
a6db579d8c11
level 2 rom preparing...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
4
diff
changeset
|
87 * |
1 | 88 */ |
89 | |
90 #define SECSIZE 256 | |
91 | |
19 | 92 |
0 | 93 int timer = 1; |
94 struct termios termsetting; | |
20 | 95 struct termios newterm; |
96 struct itimerval timercontrol; | |
0 | 97 |
20 | 98 int tflags; |
0 | 99 int xmstat; /* 0= no XMODEM transfer, 1=send, 2=receiver */ |
100 unsigned char xmbuf[132]; | |
101 int xidx; | |
102 int acknak; | |
103 int rcvdnak; | |
104 int blocknum; | |
34 | 105 int timer_irq = 2 ; // 2 = FIRQ, 1 = IRQ |
0 | 106 |
107 FILE *infile; | |
108 FILE *xfile; | |
20 | 109 FILE *logfile; |
1 | 110 FILE *disk[] = {0,0}; |
0 | 111 |
46 | 112 #ifdef USE_VDISK |
113 extern void do_vdisk(int c); | |
114 #endif | |
115 | |
116 | |
9 | 117 #ifdef USE_MMU |
118 extern char *prog ; // for disass | |
11 | 119 extern Byte * mem0(Byte *iphymem, Word adr, Byte *immu) ; |
44
b26c23331d02
add more function on vdisk
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
35
diff
changeset
|
120 #define pmem(a) mem0(phymem,a,mmu) |
b26c23331d02
add more function on vdisk
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
35
diff
changeset
|
121 #else |
b26c23331d02
add more function on vdisk
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
35
diff
changeset
|
122 #define pmem(a) (&mem[a]) |
9 | 123 #endif |
124 | |
44
b26c23331d02
add more function on vdisk
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
35
diff
changeset
|
125 |
20 | 126 extern int bpskip ; |
127 extern int stkskip ; | |
128 extern FILE *logfile; | |
0 | 129 |
1 | 130 void do_timer(int,int); |
131 void do_disk(int,int); | |
4 | 132 void do_mmu(int,int); |
0 | 133 |
134 int char_input(void) { | |
135 int c, w, sum; | |
136 if (!xmstat) { | |
137 if (infile) { | |
138 c = getc(infile); | |
139 if (c == EOF) { | |
140 fclose(infile); | |
141 infile = 0; | |
142 return char_input(); | |
143 } | |
144 if (c == '\n') | |
145 c = '\r'; | |
146 return c; | |
2
31d96e2b364e
add virtual hd option to v09
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1
diff
changeset
|
147 } else { |
31d96e2b364e
add virtual hd option to v09
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1
diff
changeset
|
148 usleep(100); |
0 | 149 return getchar(); |
2
31d96e2b364e
add virtual hd option to v09
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1
diff
changeset
|
150 } |
0 | 151 } else if (xmstat == 1) { |
152 if (xidx) { | |
153 c = xmbuf[xidx++]; | |
154 if (xidx == 132) { | |
155 xidx = 0; | |
156 rcvdnak = EOF; | |
157 acknak = 6; | |
158 } | |
159 } else { | |
160 if ((acknak == 21 && rcvdnak == 21) || (acknak == 6 && rcvdnak == 6)) { | |
161 rcvdnak = 0; | |
162 memset(xmbuf, 0, 132); | |
163 w = fread(xmbuf + 3, 1, 128, xfile); | |
164 if (w) { | |
165 printf("Block %3d transmitted, ", blocknum); | |
166 xmbuf[0] = 1; | |
167 xmbuf[1] = blocknum; | |
168 xmbuf[2] = 255 - blocknum; | |
169 blocknum = (blocknum + 1) & 255; | |
170 sum = 0; | |
171 for (w = 3; w < 131; w++) | |
172 sum = (sum + xmbuf[w]) & 255; | |
173 xmbuf[131] = sum; | |
174 acknak = 6; | |
175 c = 1; | |
176 xidx = 1; | |
177 } else { | |
178 printf("EOT transmitted, "); | |
179 acknak = 4; | |
180 c = 4; | |
181 } | |
182 } else if (rcvdnak == 21) { | |
183 rcvdnak = 0; | |
184 printf("Block %3d retransmitted, ", xmbuf[1]); | |
185 c = xmbuf[xidx++]; /*retransmit the same block */ | |
186 } else | |
187 c = EOF; | |
188 } | |
189 return c; | |
190 } else { | |
191 if (acknak == 4) { | |
192 c = 6; | |
193 acknak = 0; | |
194 fclose(xfile); | |
195 xfile = 0; | |
196 xmstat = 0; | |
197 } else if (acknak) { | |
198 c = acknak; | |
199 acknak = 0; | |
200 } else | |
201 c = EOF; | |
202 if (c == 6) | |
203 printf("ACK\n"); | |
204 if (c == 21) | |
205 printf("NAK\n"); | |
206 return c; | |
207 } | |
208 } | |
209 | |
9 | 210 int do_input(int a) { |
0 | 211 static int c, f = EOF; |
18 | 212 if (a == 0+(IOPAGE&0x1ff)) { |
0 | 213 if (f == EOF) |
214 f = char_input(); | |
34 | 215 if (f != EOF) { |
0 | 216 c = f; |
34 | 217 mem[(IOPAGE&0xfe00) + a] = c; |
218 } | |
219 mem[(IOPAGE&0xfe00) + a] = c = 2 + (f != EOF); | |
220 return c; | |
18 | 221 } else if (a == 1+(IOPAGE&0x1ff)) { /*data port*/ |
0 | 222 if (f == EOF) |
223 f = char_input(); | |
224 if (f != EOF) { | |
225 c = f; | |
226 f = EOF; | |
34 | 227 mem[(IOPAGE&0xfe00) + a] = c; |
0 | 228 } |
229 return c; | |
230 } | |
18 | 231 return mem[(IOPAGE&0xfe00) + a]; |
0 | 232 } |
233 | |
234 void do_output(int a, int c) { | |
235 int i, sum; | |
18 | 236 if (a == 1+(IOPAGE&0x1ff)) { /* ACIA data port,ignore address */ |
0 | 237 if (!xmstat) { |
238 if (logfile && c != 127 && (c >= ' ' || c == '\n')) | |
239 putc(c, logfile); | |
240 putchar(c); | |
241 fflush(stdout); | |
242 } else if (xmstat == 1) { | |
243 rcvdnak = c; | |
244 if (c == 6 && acknak == 4) { | |
245 fclose(xfile); | |
246 xfile = 0; | |
247 xmstat = 0; | |
248 } | |
249 if (c == 6) | |
250 printf("ACK\n"); | |
251 if (c == 21) | |
252 printf("NAK\n"); | |
253 if (c == 24) { | |
254 printf("CAN\n"); | |
255 fclose(xfile); | |
256 xmstat = 0; | |
257 xfile = 0; | |
258 } | |
259 } else { | |
260 if (xidx == 0 && c == 4) { | |
261 acknak = 4; | |
262 printf("EOT received, "); | |
263 } | |
264 xmbuf[xidx++] = c; | |
265 if (xidx == 132) { | |
266 sum = 0; | |
267 for (i = 3; i < 131; i++) | |
268 sum = (sum + xmbuf[i]) & 255; | |
269 if (xmbuf[0] == 1 && xmbuf[1] == 255 - xmbuf[2] | |
270 && sum == xmbuf[131]) | |
271 acknak = 6; | |
272 else | |
273 acknak = 21; | |
274 printf("Block %3d received, ", xmbuf[1]); | |
275 if (blocknum == xmbuf[1]) { | |
276 blocknum = (blocknum + 1) & 255; | |
277 fwrite(xmbuf + 3, 1, 128, xfile); | |
278 } | |
279 xidx = 0; | |
280 } | |
281 } | |
18 | 282 } else if (a >= 0x40+(IOPAGE&0x1ff)) { /* disk */ |
11 | 283 do_disk(a,c); |
18 | 284 } else if (a >= 0x30+(IOPAGE&0x1ff)) { /* timer */ |
11 | 285 do_timer(a,c); |
18 | 286 } else if (a >= 0x10+(IOPAGE&0x1ff)) { /* mmu */ |
4 | 287 do_mmu(a,c); |
35 | 288 #ifdef USE_MMU |
289 } else { /* fixed ram */ | |
290 mem[ a + 0xfe00 ] = c; | |
291 #endif | |
0 | 292 } |
293 } | |
294 | |
295 | |
1 | 296 void do_timer(int a, int c) { |
297 struct itimerval timercontrol; | |
18 | 298 if (a==0x30+(IOPAGE&0x1ff) && c==0x8f) { |
1 | 299 timercontrol.it_interval.tv_sec = 0; |
300 timercontrol.it_interval.tv_usec = 20000; | |
301 timercontrol.it_value.tv_sec = 0; | |
302 timercontrol.it_value.tv_usec = 20000; | |
34 | 303 timer_irq = 1; |
1 | 304 setitimer(ITIMER_REAL, &timercontrol, NULL); |
34 | 305 mem[(IOPAGE&0xfe00)+a]=c; |
18 | 306 } else if (a==0x30+(IOPAGE&0x1ff) && c==0x80) { |
1 | 307 timercontrol.it_interval.tv_sec = 0; |
308 timercontrol.it_interval.tv_usec = 0; | |
309 setitimer(ITIMER_REAL, &timercontrol, NULL); | |
34 | 310 mem[(IOPAGE&0xfe00)+a]=c; |
18 | 311 } else if (a==0x30+(IOPAGE&0x1ff) && c==0x04) { |
1 | 312 time_t tm = time(0); |
313 struct tm *t = localtime(&tm); | |
7
a6db579d8c11
level 2 rom preparing...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
4
diff
changeset
|
314 mem[IOPAGE+0x31] = t->tm_year; |
a6db579d8c11
level 2 rom preparing...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
4
diff
changeset
|
315 mem[IOPAGE+0x32] = t->tm_mon; |
a6db579d8c11
level 2 rom preparing...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
4
diff
changeset
|
316 mem[IOPAGE+0x33] = t->tm_mday; |
a6db579d8c11
level 2 rom preparing...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
4
diff
changeset
|
317 mem[IOPAGE+0x34] = t->tm_hour; |
a6db579d8c11
level 2 rom preparing...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
4
diff
changeset
|
318 mem[IOPAGE+0x35] = t->tm_min; |
a6db579d8c11
level 2 rom preparing...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
4
diff
changeset
|
319 mem[IOPAGE+0x36] = t->tm_sec; |
1 | 320 } else { |
18 | 321 mem[(IOPAGE&0xfe00)+a]=c; |
1 | 322 } |
323 } | |
324 | |
44
b26c23331d02
add more function on vdisk
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
35
diff
changeset
|
325 |
1 | 326 void do_disk(int a, int c) { |
18 | 327 if (a!=0x40+(IOPAGE&0x1ff)) { |
328 mem[(IOPAGE&0xfe00)+a]=c; | |
1 | 329 return; |
330 } | |
7
a6db579d8c11
level 2 rom preparing...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
4
diff
changeset
|
331 int drv = mem[IOPAGE+0x41]; |
a6db579d8c11
level 2 rom preparing...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
4
diff
changeset
|
332 int lsn = (mem[IOPAGE+0x42]<<16) + (mem[IOPAGE+0x43]<<8) + mem[IOPAGE+0x44]; |
a6db579d8c11
level 2 rom preparing...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
4
diff
changeset
|
333 int buf = (mem[IOPAGE+0x45]<<8) + mem[IOPAGE+0x46]; |
1 | 334 if (drv > 1 || disk[drv]==0) goto error; |
44
b26c23331d02
add more function on vdisk
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
35
diff
changeset
|
335 Byte *phy = pmem(buf); |
1 | 336 if (c==0x81) { |
337 if (lseek(fileno(disk[drv]),lsn*SECSIZE,SEEK_SET)==-1) goto error; | |
30 | 338 if (read(fileno(disk[drv]),phy,SECSIZE)==-1) goto error; |
1 | 339 } else if (c==0x55) { |
340 if (lseek(fileno(disk[drv]),lsn*SECSIZE,SEEK_SET)==-1) goto error; | |
30 | 341 if (write(fileno(disk[drv]),phy,SECSIZE)==-1) goto error; |
46 | 342 #ifdef USE_VDISK |
44
b26c23331d02
add more function on vdisk
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
35
diff
changeset
|
343 } else { |
b26c23331d02
add more function on vdisk
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
35
diff
changeset
|
344 do_vdisk(c); |
48
ea1b17311bf3
dir /v0 worked but dir /v0/src and chd /v0; dir does not
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
47
diff
changeset
|
345 return; |
44
b26c23331d02
add more function on vdisk
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
35
diff
changeset
|
346 #endif |
1 | 347 } |
7
a6db579d8c11
level 2 rom preparing...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
4
diff
changeset
|
348 mem[IOPAGE+0x40] = 0; |
1 | 349 return; |
350 error : | |
7
a6db579d8c11
level 2 rom preparing...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
4
diff
changeset
|
351 mem[IOPAGE+0x40] = 0xff; |
1 | 352 } |
353 | |
4 | 354 void do_mmu(int a, int c) |
355 { | |
356 #ifdef USE_MMU | |
357 | |
18 | 358 if (a==0x11+(IOPAGE&0x1ff)) { |
13 | 359 if (c&1) { |
23 | 360 mmu = &mem[0xffa8]; |
13 | 361 } else { |
23 | 362 mmu = &mem[0xffa0]; |
4 | 363 } |
364 } | |
18 | 365 mem[(IOPAGE&0xfe00)+a] = c; // other register such as 0xffa0-0xffaf |
4 | 366 #endif |
367 } | |
368 | |
0 | 369 void timehandler(int sig) { |
370 attention = 1; | |
371 irq = 2; | |
34 | 372 mem[(IOPAGE&0xfe00)+0x30] |= 0x10 ; |
373 // signal(SIGALRM, timehandler); | |
0 | 374 } |
375 | |
376 void handler(int sig) { | |
377 escape = 1; | |
378 attention = 1; | |
379 bpskip = 0; | |
380 stkskip = 0; | |
381 } | |
382 | |
20 | 383 void init_term(void) { |
384 tcgetattr(0, &termsetting); | |
385 tflags = fcntl(0, F_GETFL, 0); | |
386 } | |
387 | |
0 | 388 void set_term(char c) { |
389 signal(SIGQUIT, SIG_IGN); | |
390 signal(SIGTSTP, SIG_IGN); | |
391 signal(SIGINT, handler); | |
392 signal(SIGUSR1, handler); | |
393 newterm = termsetting; | |
394 newterm.c_iflag = newterm.c_iflag & ~INLCR & ~ICRNL; | |
395 newterm.c_lflag = newterm.c_lflag & ~ECHO & ~ICANON; | |
396 newterm.c_cc[VTIME] = 0; | |
397 newterm.c_cc[VMIN] = 1; | |
398 newterm.c_cc[VINTR] = escchar; | |
399 tcsetattr(0, TCSAFLUSH, &newterm); | |
400 fcntl(0, F_SETFL, tflags | O_NDELAY); /* Make input from stdin non-blocking */ | |
401 signal(SIGALRM, timehandler); | |
402 timercontrol.it_interval.tv_sec = 0; | |
403 timercontrol.it_interval.tv_usec = 20000; | |
404 timercontrol.it_value.tv_sec = 0; | |
405 timercontrol.it_value.tv_usec = 20000; | |
406 if (timer) | |
407 setitimer(ITIMER_REAL, &timercontrol, NULL); | |
408 } | |
20 | 409 |
410 void restore_term(void) { | |
22 | 411 termsetting.c_iflag = termsetting.c_iflag | INLCR | ICRNL; |
20 | 412 tcsetattr(0, TCSAFLUSH, &termsetting); |
413 fcntl(0, F_SETFL, tflags); | |
414 signal(SIGALRM, SIG_IGN); | |
415 } | |
416 | |
417 | |
418 | |
419 |