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