Mercurial > hg > Members > kono > os9 > sbc09
annotate io.c @ 46:ec9f494497e1
vdisk fix
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Fri, 20 Jul 2018 11:48:19 +0900 |
parents | 07c84761da6f |
children | 15f1e1b49928 |
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 | |
46 | 80 * IOPAGE + 0x43 LSN1 / VDISK Curdir pd number (2 byte) |
81 * IOPAGE + 0x44 LSN0 | |
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 |
a6db579d8c11
level 2 rom preparing...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
4
diff
changeset
|
84 * |
a6db579d8c11
level 2 rom preparing...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
4
diff
changeset
|
85 * |
1 | 86 */ |
87 | |
88 #define SECSIZE 256 | |
89 | |
19 | 90 |
0 | 91 int timer = 1; |
92 struct termios termsetting; | |
20 | 93 struct termios newterm; |
94 struct itimerval timercontrol; | |
0 | 95 |
20 | 96 int tflags; |
0 | 97 int xmstat; /* 0= no XMODEM transfer, 1=send, 2=receiver */ |
98 unsigned char xmbuf[132]; | |
99 int xidx; | |
100 int acknak; | |
101 int rcvdnak; | |
102 int blocknum; | |
34 | 103 int timer_irq = 2 ; // 2 = FIRQ, 1 = IRQ |
0 | 104 |
105 FILE *infile; | |
106 FILE *xfile; | |
20 | 107 FILE *logfile; |
1 | 108 FILE *disk[] = {0,0}; |
0 | 109 |
46 | 110 #ifdef USE_VDISK |
111 extern void do_vdisk(int c); | |
112 #endif | |
113 | |
114 | |
9 | 115 #ifdef USE_MMU |
116 extern char *prog ; // for disass | |
11 | 117 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
|
118 #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
|
119 #else |
b26c23331d02
add more function on vdisk
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
35
diff
changeset
|
120 #define pmem(a) (&mem[a]) |
9 | 121 #endif |
122 | |
44
b26c23331d02
add more function on vdisk
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
35
diff
changeset
|
123 |
20 | 124 extern int bpskip ; |
125 extern int stkskip ; | |
126 extern FILE *logfile; | |
0 | 127 |
1 | 128 void do_timer(int,int); |
129 void do_disk(int,int); | |
4 | 130 void do_mmu(int,int); |
0 | 131 |
132 int char_input(void) { | |
133 int c, w, sum; | |
134 if (!xmstat) { | |
135 if (infile) { | |
136 c = getc(infile); | |
137 if (c == EOF) { | |
138 fclose(infile); | |
139 infile = 0; | |
140 return char_input(); | |
141 } | |
142 if (c == '\n') | |
143 c = '\r'; | |
144 return c; | |
2
31d96e2b364e
add virtual hd option to v09
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1
diff
changeset
|
145 } else { |
31d96e2b364e
add virtual hd option to v09
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1
diff
changeset
|
146 usleep(100); |
0 | 147 return getchar(); |
2
31d96e2b364e
add virtual hd option to v09
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1
diff
changeset
|
148 } |
0 | 149 } else if (xmstat == 1) { |
150 if (xidx) { | |
151 c = xmbuf[xidx++]; | |
152 if (xidx == 132) { | |
153 xidx = 0; | |
154 rcvdnak = EOF; | |
155 acknak = 6; | |
156 } | |
157 } else { | |
158 if ((acknak == 21 && rcvdnak == 21) || (acknak == 6 && rcvdnak == 6)) { | |
159 rcvdnak = 0; | |
160 memset(xmbuf, 0, 132); | |
161 w = fread(xmbuf + 3, 1, 128, xfile); | |
162 if (w) { | |
163 printf("Block %3d transmitted, ", blocknum); | |
164 xmbuf[0] = 1; | |
165 xmbuf[1] = blocknum; | |
166 xmbuf[2] = 255 - blocknum; | |
167 blocknum = (blocknum + 1) & 255; | |
168 sum = 0; | |
169 for (w = 3; w < 131; w++) | |
170 sum = (sum + xmbuf[w]) & 255; | |
171 xmbuf[131] = sum; | |
172 acknak = 6; | |
173 c = 1; | |
174 xidx = 1; | |
175 } else { | |
176 printf("EOT transmitted, "); | |
177 acknak = 4; | |
178 c = 4; | |
179 } | |
180 } else if (rcvdnak == 21) { | |
181 rcvdnak = 0; | |
182 printf("Block %3d retransmitted, ", xmbuf[1]); | |
183 c = xmbuf[xidx++]; /*retransmit the same block */ | |
184 } else | |
185 c = EOF; | |
186 } | |
187 return c; | |
188 } else { | |
189 if (acknak == 4) { | |
190 c = 6; | |
191 acknak = 0; | |
192 fclose(xfile); | |
193 xfile = 0; | |
194 xmstat = 0; | |
195 } else if (acknak) { | |
196 c = acknak; | |
197 acknak = 0; | |
198 } else | |
199 c = EOF; | |
200 if (c == 6) | |
201 printf("ACK\n"); | |
202 if (c == 21) | |
203 printf("NAK\n"); | |
204 return c; | |
205 } | |
206 } | |
207 | |
9 | 208 int do_input(int a) { |
0 | 209 static int c, f = EOF; |
18 | 210 if (a == 0+(IOPAGE&0x1ff)) { |
0 | 211 if (f == EOF) |
212 f = char_input(); | |
34 | 213 if (f != EOF) { |
0 | 214 c = f; |
34 | 215 mem[(IOPAGE&0xfe00) + a] = c; |
216 } | |
217 mem[(IOPAGE&0xfe00) + a] = c = 2 + (f != EOF); | |
218 return c; | |
18 | 219 } else if (a == 1+(IOPAGE&0x1ff)) { /*data port*/ |
0 | 220 if (f == EOF) |
221 f = char_input(); | |
222 if (f != EOF) { | |
223 c = f; | |
224 f = EOF; | |
34 | 225 mem[(IOPAGE&0xfe00) + a] = c; |
0 | 226 } |
227 return c; | |
228 } | |
18 | 229 return mem[(IOPAGE&0xfe00) + a]; |
0 | 230 } |
231 | |
232 void do_output(int a, int c) { | |
233 int i, sum; | |
18 | 234 if (a == 1+(IOPAGE&0x1ff)) { /* ACIA data port,ignore address */ |
0 | 235 if (!xmstat) { |
236 if (logfile && c != 127 && (c >= ' ' || c == '\n')) | |
237 putc(c, logfile); | |
238 putchar(c); | |
239 fflush(stdout); | |
240 } else if (xmstat == 1) { | |
241 rcvdnak = c; | |
242 if (c == 6 && acknak == 4) { | |
243 fclose(xfile); | |
244 xfile = 0; | |
245 xmstat = 0; | |
246 } | |
247 if (c == 6) | |
248 printf("ACK\n"); | |
249 if (c == 21) | |
250 printf("NAK\n"); | |
251 if (c == 24) { | |
252 printf("CAN\n"); | |
253 fclose(xfile); | |
254 xmstat = 0; | |
255 xfile = 0; | |
256 } | |
257 } else { | |
258 if (xidx == 0 && c == 4) { | |
259 acknak = 4; | |
260 printf("EOT received, "); | |
261 } | |
262 xmbuf[xidx++] = c; | |
263 if (xidx == 132) { | |
264 sum = 0; | |
265 for (i = 3; i < 131; i++) | |
266 sum = (sum + xmbuf[i]) & 255; | |
267 if (xmbuf[0] == 1 && xmbuf[1] == 255 - xmbuf[2] | |
268 && sum == xmbuf[131]) | |
269 acknak = 6; | |
270 else | |
271 acknak = 21; | |
272 printf("Block %3d received, ", xmbuf[1]); | |
273 if (blocknum == xmbuf[1]) { | |
274 blocknum = (blocknum + 1) & 255; | |
275 fwrite(xmbuf + 3, 1, 128, xfile); | |
276 } | |
277 xidx = 0; | |
278 } | |
279 } | |
18 | 280 } else if (a >= 0x40+(IOPAGE&0x1ff)) { /* disk */ |
11 | 281 do_disk(a,c); |
18 | 282 } else if (a >= 0x30+(IOPAGE&0x1ff)) { /* timer */ |
11 | 283 do_timer(a,c); |
18 | 284 } else if (a >= 0x10+(IOPAGE&0x1ff)) { /* mmu */ |
4 | 285 do_mmu(a,c); |
35 | 286 #ifdef USE_MMU |
287 } else { /* fixed ram */ | |
288 mem[ a + 0xfe00 ] = c; | |
289 #endif | |
0 | 290 } |
291 } | |
292 | |
293 | |
1 | 294 void do_timer(int a, int c) { |
295 struct itimerval timercontrol; | |
18 | 296 if (a==0x30+(IOPAGE&0x1ff) && c==0x8f) { |
1 | 297 timercontrol.it_interval.tv_sec = 0; |
298 timercontrol.it_interval.tv_usec = 20000; | |
299 timercontrol.it_value.tv_sec = 0; | |
300 timercontrol.it_value.tv_usec = 20000; | |
34 | 301 timer_irq = 1; |
1 | 302 setitimer(ITIMER_REAL, &timercontrol, NULL); |
34 | 303 mem[(IOPAGE&0xfe00)+a]=c; |
18 | 304 } else if (a==0x30+(IOPAGE&0x1ff) && c==0x80) { |
1 | 305 timercontrol.it_interval.tv_sec = 0; |
306 timercontrol.it_interval.tv_usec = 0; | |
307 setitimer(ITIMER_REAL, &timercontrol, NULL); | |
34 | 308 mem[(IOPAGE&0xfe00)+a]=c; |
18 | 309 } else if (a==0x30+(IOPAGE&0x1ff) && c==0x04) { |
1 | 310 time_t tm = time(0); |
311 struct tm *t = localtime(&tm); | |
7
a6db579d8c11
level 2 rom preparing...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
4
diff
changeset
|
312 mem[IOPAGE+0x31] = t->tm_year; |
a6db579d8c11
level 2 rom preparing...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
4
diff
changeset
|
313 mem[IOPAGE+0x32] = t->tm_mon; |
a6db579d8c11
level 2 rom preparing...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
4
diff
changeset
|
314 mem[IOPAGE+0x33] = t->tm_mday; |
a6db579d8c11
level 2 rom preparing...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
4
diff
changeset
|
315 mem[IOPAGE+0x34] = t->tm_hour; |
a6db579d8c11
level 2 rom preparing...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
4
diff
changeset
|
316 mem[IOPAGE+0x35] = t->tm_min; |
a6db579d8c11
level 2 rom preparing...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
4
diff
changeset
|
317 mem[IOPAGE+0x36] = t->tm_sec; |
1 | 318 } else { |
18 | 319 mem[(IOPAGE&0xfe00)+a]=c; |
1 | 320 } |
321 } | |
322 | |
44
b26c23331d02
add more function on vdisk
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
35
diff
changeset
|
323 |
1 | 324 void do_disk(int a, int c) { |
18 | 325 if (a!=0x40+(IOPAGE&0x1ff)) { |
326 mem[(IOPAGE&0xfe00)+a]=c; | |
1 | 327 return; |
328 } | |
7
a6db579d8c11
level 2 rom preparing...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
4
diff
changeset
|
329 int drv = mem[IOPAGE+0x41]; |
a6db579d8c11
level 2 rom preparing...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
4
diff
changeset
|
330 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
|
331 int buf = (mem[IOPAGE+0x45]<<8) + mem[IOPAGE+0x46]; |
1 | 332 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
|
333 Byte *phy = pmem(buf); |
1 | 334 if (c==0x81) { |
335 if (lseek(fileno(disk[drv]),lsn*SECSIZE,SEEK_SET)==-1) goto error; | |
30 | 336 if (read(fileno(disk[drv]),phy,SECSIZE)==-1) goto error; |
1 | 337 } else if (c==0x55) { |
338 if (lseek(fileno(disk[drv]),lsn*SECSIZE,SEEK_SET)==-1) goto error; | |
30 | 339 if (write(fileno(disk[drv]),phy,SECSIZE)==-1) goto error; |
46 | 340 #ifdef USE_VDISK |
44
b26c23331d02
add more function on vdisk
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
35
diff
changeset
|
341 } else { |
b26c23331d02
add more function on vdisk
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
35
diff
changeset
|
342 do_vdisk(c); |
b26c23331d02
add more function on vdisk
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
35
diff
changeset
|
343 #endif |
1 | 344 } |
7
a6db579d8c11
level 2 rom preparing...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
4
diff
changeset
|
345 mem[IOPAGE+0x40] = 0; |
1 | 346 return; |
347 error : | |
7
a6db579d8c11
level 2 rom preparing...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
4
diff
changeset
|
348 mem[IOPAGE+0x40] = 0xff; |
1 | 349 } |
350 | |
4 | 351 void do_mmu(int a, int c) |
352 { | |
353 #ifdef USE_MMU | |
354 | |
18 | 355 if (a==0x11+(IOPAGE&0x1ff)) { |
13 | 356 if (c&1) { |
23 | 357 mmu = &mem[0xffa8]; |
13 | 358 } else { |
23 | 359 mmu = &mem[0xffa0]; |
4 | 360 } |
361 } | |
18 | 362 mem[(IOPAGE&0xfe00)+a] = c; // other register such as 0xffa0-0xffaf |
4 | 363 #endif |
364 } | |
365 | |
0 | 366 void timehandler(int sig) { |
367 attention = 1; | |
368 irq = 2; | |
34 | 369 mem[(IOPAGE&0xfe00)+0x30] |= 0x10 ; |
370 // signal(SIGALRM, timehandler); | |
0 | 371 } |
372 | |
373 void handler(int sig) { | |
374 escape = 1; | |
375 attention = 1; | |
376 bpskip = 0; | |
377 stkskip = 0; | |
378 } | |
379 | |
20 | 380 void init_term(void) { |
381 tcgetattr(0, &termsetting); | |
382 tflags = fcntl(0, F_GETFL, 0); | |
383 } | |
384 | |
0 | 385 void set_term(char c) { |
386 signal(SIGQUIT, SIG_IGN); | |
387 signal(SIGTSTP, SIG_IGN); | |
388 signal(SIGINT, handler); | |
389 signal(SIGUSR1, handler); | |
390 newterm = termsetting; | |
391 newterm.c_iflag = newterm.c_iflag & ~INLCR & ~ICRNL; | |
392 newterm.c_lflag = newterm.c_lflag & ~ECHO & ~ICANON; | |
393 newterm.c_cc[VTIME] = 0; | |
394 newterm.c_cc[VMIN] = 1; | |
395 newterm.c_cc[VINTR] = escchar; | |
396 tcsetattr(0, TCSAFLUSH, &newterm); | |
397 fcntl(0, F_SETFL, tflags | O_NDELAY); /* Make input from stdin non-blocking */ | |
398 signal(SIGALRM, timehandler); | |
399 timercontrol.it_interval.tv_sec = 0; | |
400 timercontrol.it_interval.tv_usec = 20000; | |
401 timercontrol.it_value.tv_sec = 0; | |
402 timercontrol.it_value.tv_usec = 20000; | |
403 if (timer) | |
404 setitimer(ITIMER_REAL, &timercontrol, NULL); | |
405 } | |
20 | 406 |
407 void restore_term(void) { | |
22 | 408 termsetting.c_iflag = termsetting.c_iflag | INLCR | ICRNL; |
20 | 409 tcsetattr(0, TCSAFLUSH, &termsetting); |
410 fcntl(0, F_SETFL, tflags); | |
411 signal(SIGALRM, SIG_IGN); | |
412 } | |
413 | |
414 | |
415 | |
416 |