This repository was archived by the owner on Dec 23, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtiny68k.c
More file actions
738 lines (661 loc) · 14.2 KB
/
tiny68k.c
File metadata and controls
738 lines (661 loc) · 14.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
#include <sys/select.h>
#include <signal.h>
#include <termios.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#include <netinet/in.h>
#include "v68.h"
#include "m68k.h"
#include "m68kcpu.h"
#include "ide.h"
/* 16MB RAM except for the top 32K which is I/O */
static uint8_t ram[(16 << 20) - 32768];
/* IDE controller */
static struct ide_controller *ide;
static int rc2014;
uint8_t fc;
/* Read/write macros */
#define READ_BYTE(BASE, ADDR) (BASE)[ADDR]
#define READ_WORD(BASE, ADDR) (((BASE)[ADDR]<<8) | \
(BASE)[(ADDR)+1])
#define READ_LONG(BASE, ADDR) (((BASE)[ADDR]<<24) | \
((BASE)[(ADDR)+1]<<16) | \
((BASE)[(ADDR)+2]<<8) | \
(BASE)[(ADDR)+3])
#define WRITE_BYTE(BASE, ADDR, VAL) (BASE)[ADDR] = (VAL)&0xff
#define WRITE_WORD(BASE, ADDR, VAL) (BASE)[ADDR] = ((VAL)>>8) & 0xff; \
(BASE)[(ADDR)+1] = (VAL)&0xff
#define WRITE_LONG(BASE, ADDR, VAL) (BASE)[ADDR] = ((VAL)>>24) & 0xff; \
(BASE)[(ADDR)+1] = ((VAL)>>16)&0xff; \
(BASE)[(ADDR)+2] = ((VAL)>>8)&0xff; \
(BASE)[(ADDR)+3] = (VAL)&0xff
/* Simple polled serial port */
static unsigned int check_chario(void)
{
fd_set i, o;
struct timeval tv;
unsigned int r = 0;
FD_ZERO(&i);
FD_SET(0, &i);
FD_ZERO(&o);
FD_SET(1, &o);
tv.tv_sec = 0;
tv.tv_usec = 0;
if (select(2, &i, NULL, NULL, &tv) == -1) {
perror("select");
exit(1);
}
if (FD_ISSET(0, &i))
r |= 1;
if (FD_ISSET(1, &o))
r |= 2;
return r;
}
static unsigned int next_char(void)
{
char c;
if (read(0, &c, 1) != 1) {
printf("(tty read without ready byte)\n");
return 0xFF;
}
return c;
}
/* Interrupt controller */
static unsigned int irq_pending;
static void irq_compute(void)
{
int i;
if (irq_pending) {
for (i = 7; i >= 0; i--) {
if (irq_pending & (1 << i)) {
m68k_set_irq(i);
return;
}
}
} else
m68k_set_irq(0);
}
/* Until we plug any RC2014 emulation into this */
static uint8_t rc2014_inb(uint8_t address)
{
return 0xFF;
}
static void rc2014_outb(uint8_t address, uint8_t data)
{
}
/* 68681 DUART */
struct duart_port {
uint8_t mr1;
uint8_t mr2;
uint8_t sr;
uint8_t csr;
uint8_t rx;
uint8_t mrp;
uint8_t txdis;
uint8_t rxdis;
};
struct duart {
struct duart_port port[2];
uint8_t ipcr;
uint8_t isr;
int32_t ct; /* We overflow this temporarily */
uint16_t ctr;
uint8_t ctstop;
uint8_t imr;
uint8_t ivr;
uint8_t opcr;
uint8_t acr;
uint8_t irq;
};
struct duart duart;
/* DUART is on IPL1 */
static void duart_irq_calc(struct duart *d)
{
d->irq = d->isr & d->imr;
if (d->irq)
irq_pending |= 1 << 2;
else
irq_pending &= ~(1 << 2);
irq_compute();
}
static void duart_irq_raise(struct duart *d, uint8_t m)
{
if (!(d->isr & m)) {
d->isr |= m;
duart_irq_calc(d);
}
}
static void duart_irq_lower(struct duart *d, uint8_t m)
{
if (d->isr & m) {
d->isr &= ~m;
duart_irq_calc(d);
}
}
static uint8_t duart_input(struct duart *d, int port)
{
if (d->port[port].sr & 0x01) {
d->port[port].sr ^= 0x01;
duart_irq_lower(d, 2 << (4 * port));
}
return d->port[port].rx;
}
static void duart_output(struct duart *d, int port, int value)
{
duart_irq_lower(d, 1 << (4 * port));
if (d->port[port].txdis)
return;
if (d->port[port].sr & 0x04) {
if (port == 0) {
uint8_t v = value & 0xFF;
write(1, &v, 1);
}
d->port[port].sr &= 0xF3;
duart_irq_calc(d);
}
}
static void duart_command(struct duart *d, int port, int value)
{
switch ((value & 0xE0) >> 4) {
case 0:
break;
case 1:
d->port[port].mrp = 0;
break;
case 2:
/* Reset RX A */
d->port[port].rxdis = 1;
break;
case 3:
break;
case 4:
d->port[port].sr &= 0x0F;
break;
case 5:
duart_irq_lower(d, 4 << (4 * port));
duart_irq_calc(d);
break;
case 6:
break; /* Literally start break */
case 7:
break; /* Stop break */
}
if (value & 1)
d->port[port].rxdis = 0;
if (value & 2)
d->port[port].rxdis = 1;
if (value & 4)
d->port[port].txdis = 0;
if (value & 8)
d->port[port].txdis = 1;
}
/* Simulate 1/100th second of action */
static void duart_count(struct duart *d, int n)
{
/* We are clocked at ??? so divide as needed */
uint16_t clock = 184; /* 1843200 so not entirely accurate
FIXME: we could track partial clocks */
if (n == 16)
clock /= 16; /* Again needs accuracy sorting */
/* Counter mode can be stopped */
if (!(d->acr & 0x40))
if (d->ctstop)
return;
d->ct -= clock;
while (d->ct < 0) {
d->ct += d->ctr;
duart_irq_raise(d, 0x08);
}
}
static void duart_tick(void)
{
uint8_t r = check_chario();
if ((r & 1) && duart.port[0].rxdis == 0) {
duart.port[0].rx = next_char();
duart.port[0].sr |= 0x01;
duart_irq_raise(&duart, 0x02);
}
if (r & 2) {
if (!duart.port[0].txdis && !(duart.port[0].sr & 0x04)) {
duart.port[0].sr |= 0x0C;
duart_irq_raise(&duart, 0x01);
}
if (!duart.port[1].txdis && !(duart.port[1].sr & 0x04)) {
duart.port[1].sr |= 0x0C;
duart_irq_raise(&duart, 0x10);
}
}
switch ((duart.acr & 0x70) >> 4) {
/* Clock and timer modes */
case 0: /* Counting IP2 */
break;
case 1: /* Counting TxCA */
break;
case 2: /* Counting TxCB */
break;
case 3: /* Counting EXT/x1 clock / 16 */
duart_count(&duart, 16);
break;
case 4: /* Timer on IP2 */
break;
case 5: /* Timer on IP2/16 */
break;
case 6: /* Timer on X1/CLK */
duart_count(&duart, 1);
break;
case 7: /* Timer on X1/CLK / 16 */
duart_count(&duart, 16);
break;
}
}
static void duart_reset(struct duart *d)
{
d->ctr = 0xFFFF;
d->ct = 0x0000;
d->acr = 0xFF;
d->isr = 0;
d->imr = 0;
d->port[0].mrp = 0;
d->port[0].sr = 0x00;
d->port[1].mrp = 0;
d->port[1].sr = 0x00;
}
static unsigned int duart_read(unsigned int address)
{
if (!(address & 1))
return 0x00;
switch (address >> 1) {
case 0x00: /* MR1A/MR2A */
if (duart.port[0].mrp)
return duart.port[0].mr2;
duart.port[0].mrp = 1;
return duart.port[0].mr1;
case 0x01: /* SRA */
return duart.port[0].sr;
case 0x02: /* BRG test */
case 0x03: /* RHRA */
return duart_input(&duart, 0);
case 0x04: /* IPCR */
return duart.ipcr;
case 0x05: /* ISR */
return duart.isr;
case 0x06: /* CTU */
return duart.ct >> 8;
case 0x07: /* CTL */
return duart.ct & 0xFF;
case 0x08: /* MR1B/MR2B */
if (duart.port[1].mrp)
return duart.port[1].mr2;
duart.port[1].mrp = 1;
return duart.port[1].mr1;
case 0x09: /* SRB */
return duart.port[1].sr;
case 0x0A: /* 1x/16x Test */
case 0x0B: /* RHRB */
return duart_input(&duart, 1);
case 0x0C: /* IVR */
return duart.ivr;
case 0x0D: /* IP */
return 0xff; /* duart.ip; */
case 0x0E: /* START */
duart.ct = duart.ctr;
duart.ctstop = 0;
return 0xFF;
case 0x0F: /* STOP */
duart.ctstop = 1;
duart_irq_lower(&duart, 0x08);
return 0xFF;
}
return 0xFF;
}
static void duart_write(unsigned int address, unsigned int value)
{
int bgrc = 0;
if (!(address & 1))
return;
value &= 0xFF;
switch (address >> 1) {
case 0x00:
if (duart.port[0].mrp)
duart.port[0].mr2 = value;
else
duart.port[0].mr1 = value;
break;
case 0x01:
duart.port[0].csr = value;
bgrc = 1;
break;
case 0x02:
duart_command(&duart, 0, value);
break;
case 0x03:
duart_output(&duart, 0, value);
break;
case 0x04:
duart.acr = value;
duart_irq_calc(&duart);
bgrc = 1;
break;
case 0x05:
duart.imr = value;
duart_irq_calc(&duart);
break;
case 0x06:
duart.ctr &= 0xFF;
duart.ctr |= value << 8;
break;
case 0x07:
duart.ctr &= 0xFF00;
duart.ctr |= value;
break;
case 0x08:
if (duart.port[1].mrp)
duart.port[1].mr2 = value;
else
duart.port[1].mr1 = value;
break;
case 0x09:
duart.port[1].csr = value;
break;
case 0x0A:
duart_command(&duart, 1, value);
break;
case 0x0B:
duart_output(&duart, 1, value);
break;
case 0x0C:
duart.ivr = value;
break;
case 0x0D:
duart.opcr = value;
break;
case 0x0E:
duart.opcr |= value;
break;
case 0x0F:
duart.opcr &= ~value;
break;
}
if (bgrc) {
printf("BGR %d\n", duart.acr >> 7);
printf("CSR %d\n", duart.port[0].csr >> 4);
}
}
int cpu_irq_ack(int level)
{
if (!(irq_pending & (1 << level)))
return M68K_INT_ACK_SPURIOUS;
if (level == 2)
return duart.ivr;
return M68K_INT_ACK_SPURIOUS;
}
/* TODO v2 board added an RTC */
static unsigned int do_io_readb(unsigned int address)
{
if (rc2014 && address >= 0xFF8000 && address <= 0xFF8FFF)
return rc2014_inb(((address - 0xFF8000) >> 1) & 0xFF);
/* SPI is not modelled */
if (address >= 0xFFD000 && address <= 0xFFDFFF)
return 0xFF;
/* ATA CF */
/* FIXME: FFE010-01F sets CS1 */
if (address >= 0xFFE000 && address <= 0xFFEFFF)
return ide_read8(ide, (address & 31) >> 1);
/* DUART */
return duart_read(address & 31);
}
static void do_io_writeb(unsigned int address, unsigned int value)
{
if (address == 0xFFFFFF) {
printf("<%c>", value);
return;
}
if (rc2014 && address >= 0xFF8000 && address <= 0xFF8FFF) {
rc2014_outb(((address - 0xFF8000) >> 1) & 0xFF, value);
return;
}
/* SPI is not modelled */
if (address >= 0xFFD000 && address <= 0xFFDFFF)
return;
/* ATA CF */
if (address >= 0xFFE000 && address <= 0xFFEFFF) {
ide_write8(ide, (address & 31) >> 1, value);
return;
}
/* DUART */
duart_write(address & 31, value);
}
/* Read data from RAM, ROM, or a device */
unsigned int cpu_read_byte(unsigned int address)
{
address &= 0xFFFFFF;
if (rc2014) {
/* Musashi can't emulate this properly it seems */
if (address >= 0x800000 && address <= 0xFF8000) {
fprintf(stderr, "R: bus error at %d\n", address);
return 0xFF;
}
if (address <= 0xFF8000)
address &= 0x1FFFFF;
}
if (address < sizeof(ram))
return ram[address];
return do_io_readb(address);
}
unsigned int cpu_read_word(unsigned int address)
{
address &= 0xFFFFFF;
if (rc2014) {
if (address >= 0x800000 && address < 0xFF8000) {
fprintf(stderr, "R: bus error at %d\n", address);
return 0xFFFF;
}
/* RAM wraps four times */
if (address <= 0xFF8000)
address &= 0x1FFFFF;
}
if (address < sizeof(ram) - 1)
return READ_WORD(ram, address);
else if (address >= 0xFFE000 && address <= 0xFFEFFF)
return htons(ide_read16(ide, (address & 31) >> 1));
return (cpu_read_byte(address) << 8) | cpu_read_byte(address + 1);
}
unsigned int cpu_read_word_dasm(unsigned int address)
{
return cpu_read_word(address);
}
unsigned int cpu_read_long(unsigned int address)
{
return (cpu_read_word(address) << 16) | cpu_read_word(address + 2);
}
unsigned int cpu_read_long_dasm(unsigned int address)
{
return cpu_read_long(address);
}
void cpu_write_byte(unsigned int address, unsigned int value)
{
if (rc2014) {
if (address >= 0x800000 && address <= 0xFF8000) {
fprintf(stderr, "W: bus error at %06X\n", address);
return;
}
if (address <= 0xFF8000)
address &= 0x1FFFFF;
}
if (address < sizeof(ram))
ram[address] = value;
else
do_io_writeb(address, (value & 0xFF));
}
void cpu_write_word(unsigned int address, unsigned int value)
{
address &= 0xFFFFFF;
if (rc2014) {
if (address >= 0x800000 && address <= 0xFF8000) {
fprintf(stderr, "W: bus error at %06X\n", address);
return;
}
if (address <= 0xFF8000)
address &= 0x1FFFFF;
}
if (address < sizeof(ram) - 1) {
WRITE_WORD(ram, address, value);
} else if (address >= 0xFFE000 && address <= 0xFFEFFF)
ide_write16(ide, (address & 31) >> 1, ntohs(value));
else {
/* Corner cases */
cpu_write_byte(address, value >> 8);
cpu_write_byte(address + 1, value & 0xFF);
}
}
void cpu_write_long(unsigned int address, unsigned int value)
{
address &= 0xFFFFFF;
cpu_write_word(address, value >> 16);
cpu_write_word(address + 2, value & 0xFFFF);
}
void cpu_write_pd(unsigned int address, unsigned int value)
{
address &= 0xFFFFFF;
cpu_write_word(address + 2, value & 0xFFFF);
cpu_write_word(address, value >> 16);
}
void cpu_instr_callback(void)
{
}
static void device_init(void)
{
irq_pending = 0;
ide_reset_begin(ide);
duart_reset(&duart);
}
static struct termios saved_term, term;
static void cleanup(int sig)
{
tcsetattr(0, 0, &saved_term);
exit(1);
}
static void exit_cleanup(void)
{
tcsetattr(0, 0, &saved_term);
}
static void take_a_nap(void)
{
struct timespec t;
t.tv_sec = 0;
t.tv_nsec = 100000;
if (nanosleep(&t, NULL))
perror("nanosleep");
}
void cpu_pulse_reset(void)
{
device_init();
}
void usage(void)
{
fprintf(stderr, "tiny68k [-0][-1][-2][-e][-r].\n");
exit(1);
}
int main(int argc, char *argv[])
{
int fd;
int cputype = M68K_CPU_TYPE_68000;
int fast = 0;
int opt;
const char *romname, *diskname;
fprintf(stderr, "This version of Tiny68K is deprecated, please see the RC2014 emulators\n");
while((opt = getopt(argc, argv, "012er")) != -1) {
switch(opt) {
case '0':
cputype = M68K_CPU_TYPE_68000;
break;
case '1':
cputype = M68K_CPU_TYPE_68010;
break;
case '2':
cputype = M68K_CPU_TYPE_68020;
break;
case 'e':
cputype = M68K_CPU_TYPE_68EC020;
break;
case 'r':
rc2014 = 1;
break;
case 'f':
fast = 1;
break;
default:
usage();
}
}
if (tcgetattr(0, &term) == 0) {
saved_term = term;
atexit(exit_cleanup);
signal(SIGINT, SIG_IGN);
signal(SIGQUIT, cleanup);
signal(SIGTSTP, SIG_IGN);
term.c_lflag &= ~ICANON;
term.c_iflag &= ~(ICRNL | IGNCR);
term.c_cc[VMIN] = 1;
term.c_cc[VTIME] = 0;
term.c_cc[VINTR] = 0;
term.c_cc[VSUSP] = 0;
term.c_cc[VEOF] = 0;
term.c_lflag &= ~(ECHO | ECHOE | ECHOK);
tcsetattr(0, 0, &term);
}
if (optind < argc)
usage();
memset(ram, 0xA7, sizeof(ram));
/* Boot data into memory */
if (rc2014) {
romname = "t68krc.rom";
diskname = "t68krc.img";
} else {
romname = "tiny68k.rom";
diskname = "tiny68k.img";
}
fd = open(romname, O_RDONLY);
if (fd == -1) {
perror(romname);
exit(1);
}
if (read(fd, ram, 0x8000) < 0x1000) {
fprintf(stderr, "%s: too short.\n", romname);
exit(1);
}
close(fd);
m68k_init();
m68k_set_cpu_type(cputype);
m68k_pulse_reset();
fd = open(diskname, O_RDWR);
if (fd == -1) {
perror(diskname);
exit(1);
}
ide = ide_allocate("hd0");
if (ide == NULL)
exit(1);
if (ide_attach(ide, 0, fd))
exit(1);
/* Init devices */
device_init();
m68k_pulse_reset();
while (1) {
/* A 10MHz 68000 should do 1000 cycles per 1/10000th of a
second. We do a blind 0.01 second sleep so we are actually
emulating a bit under 10Mhz - which will do fine for
testing this stuff */
m68k_execute(1000);
duart_tick();
if (!fast)
take_a_nap();
}
}