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