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