You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

192 lines
5.3 KiB
C

/*
* Apple I emulator by matthilde
*
* apple1.c
*
* This is an Apple I emulator powered by emu6502. It is an application
* of my CPU emulator just for fun.
*/
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
#include <termios.h>
#include <errno.h>
#include "emu6502.h"
/*
* Raw Mode
*/
struct termios orig_termios;
void disableRawMode() {
if (tcsetattr(0, TCSAFLUSH, &orig_termios) == -1)
exit(2);
}
void sigintdrm(int a) { (void)a; disableRawMode(); exit(0); }
void enableRawMode() {
if (tcgetattr(0, &orig_termios) == -1) exit(2);
signal(SIGINT, sigintdrm);
atexit(disableRawMode);
struct termios raw = orig_termios;
raw.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
raw.c_oflag &= ~(OPOST);
raw.c_cflag |= (CS8);
raw.c_lflag &= ~(ECHO | ICANON | IEXTEN);
raw.c_cc[VMIN] = 0;
raw.c_cc[VTIME] = 1;
if (tcsetattr(0, TCSAFLUSH, &raw) == -1)
exit(2);
}
//////////////////////////////////////////////////////////////////////
// Woz Monitor ROM
// FF00-FFFF
const byte wozmon[256] = {
0xd8, 0x58, 0xa0, 0x7f, 0x8c, 0x12, 0xd0, 0xa9, 0xa7, 0x8d,
0x11, 0xd0, 0x8d, 0x13, 0xd0, 0xc9, 0xdf, 0xf0, 0x13, 0xc9,
0x9b, 0xf0, 0x03, 0xc8, 0x10, 0x0f, 0xa9, 0xdc, 0x20, 0xef,
0xff, 0xa9, 0x8d, 0x20, 0xef, 0xff, 0xa0, 0x01, 0x88, 0x30,
0xf6, 0xad, 0x11, 0xd0, 0x10, 0xfb, 0xad, 0x10, 0xd0, 0x99,
0x00, 0x02, 0x20, 0xef, 0xff, 0xc9, 0x8d, 0xd0, 0xd4, 0xa0,
0xff, 0xa9, 0x00, 0xaa, 0x0a, 0x85, 0x2b, 0xc8, 0xb9, 0x00,
0x02, 0xc9, 0x8d, 0xf0, 0xd4, 0xc9, 0xae, 0x90, 0xf4, 0xf0,
0xf0, 0xc9, 0xba, 0xf0, 0xeb, 0xc9, 0xd2, 0xf0, 0x3b, 0x86,
0x28, 0x86, 0x29, 0x84, 0x2a, 0xb9, 0x00, 0x02, 0x49, 0xb0,
0xc9, 0x0a, 0x90, 0x06, 0x69, 0x88, 0xc9, 0xfa, 0x90, 0x11,
0x0a, 0x0a, 0x0a, 0x0a, 0xa2, 0x04, 0x0a, 0x26, 0x28, 0x26,
0x29, 0xca, 0xd0, 0xf8, 0xc8, 0xd0, 0xe0, 0xc4, 0x2a, 0xf0,
0x97, 0x24, 0x2b, 0x50, 0x10, 0xa5, 0x28, 0x81, 0x26, 0xe6,
0x26, 0xd0, 0xb5, 0xe6, 0x27, 0x4c, 0x44, 0xff, 0x6c, 0x24,
0x00, 0x30, 0x2b, 0xa2, 0x02, 0xb5, 0x27, 0x95, 0x25, 0x95,
0x23, 0xca, 0xd0, 0xf7, 0xd0, 0x14, 0xa9, 0x8d, 0x20, 0xef,
0xff, 0xa5, 0x25, 0x20, 0xdc, 0xff, 0xa5, 0x24, 0x20, 0xdc,
0xff, 0xa9, 0xba, 0x20, 0xef, 0xff, 0xa9, 0xa0, 0x20, 0xef,
0xff, 0xa1, 0x24, 0x20, 0xdc, 0xff, 0x86, 0x2b, 0xa5, 0x24,
0xc5, 0x28, 0xa5, 0x25, 0xe5, 0x29, 0xb0, 0xc1, 0xe6, 0x24,
0xd0, 0x02, 0xe6, 0x25, 0xa5, 0x24, 0x29, 0x07, 0x10, 0xc8,
0x48, 0x4a, 0x4a, 0x4a, 0x4a, 0x20, 0xe5, 0xff, 0x68, 0x29,
0x0f, 0x09, 0xb0, 0xc9, 0xba, 0x90, 0x02, 0x69, 0x06, 0x2c,
0x12, 0xd0, 0x30, 0xfb, 0x8d, 0x12, 0xd0, 0x60, 0x00, 0x00,
0x00, 0x0f, 0x00, 0xff, 0x00, 0x00,
};
// Lower RAM (32kb) 0000-7FFF
byte lowram[0x8000];
// Upper RAM (4kb) E000-EFFF
byte highram[0x1000];
// KBD at 0xd010
byte kbdreg = 0;
// KBDCR at 0xd011
byte kbdcr = 0;
void update_key_register() {
char c = 0x00;
int ret = read(0, &c, 1);
if (ret == -1 && errno != EAGAIN) exit(2);
if (ret != -1 && c) {
kbdcr = 0x80; // set bit 7 on key press
switch (c) {
case 0xd: kbdreg = 0x8d; break;
case 033: kbdreg = 0x9b; break;
case 0x8: kbdreg = 0xdf; break;
default: kbdreg = c; break;
}
} else kbdcr = 0;
}
#define MEM_IN_RANGE(a,b) (addr >= a && addr <= b)
// Write memory function hook
void apple1_write_memory(CPU6502* cpu, word addr, byte v) {
switch (addr) {
case 0xd010:
case 0xd011:
break;
case 0xd012:
v &= 0x7f;
if (v == 0x7f)
putchar(0x8);
else {
putchar(v);
if ((v & 0x7f) == 0xd) putchar(0xa);
}
fflush(stdout);
break;
case 0xd013:
break;
default:
if (MEM_IN_RANGE(0x0000, 0x7fff))
lowram[addr] = v;
else if (MEM_IN_RANGE(0xe000, 0xefff))
highram[addr - 0xe000] = v;
}
}
// Read memory function hook
byte apple1_read_memory(CPU6502* cpu, word addr) {
switch (addr) {
// Keyboard input
case 0xd010: kbdcr = 0; return kbdreg | 0x80; break;
// Keyboard Control Register
case 0xd011:
update_key_register(); return kbdcr; break;
// kbdreg = getchar(); return 0x80; break;
case 0xd012:
case 0xd013:
return 0; break;
default:
if (MEM_IN_RANGE(0x0000, 0x7fff))
return lowram[addr];
else if (MEM_IN_RANGE(0xe000, 0xefff))
return highram[addr - 0xe000];
else if (MEM_IN_RANGE(0xff00, 0xffff)) {
return wozmon[addr - 0xff00];
}
return 0;
break;
}
}
int main(int argc, char **argv) {
printf("Apple I Emulator running on emu6502 by matthilde\n\n");
if (argc == 2) {
FILE *f = fopen(argv[1], "r");
int c;
for (int i = 0; (c = fgetc(f)) != EOF; ++i)
highram[i] = c;
fclose(f);
}
enableRawMode();
/*
char c;
for (;;) {
if (read(0, &c, 1) == -1 && errno != EAGAIN) return 2;
if (c == 'e')
break;
else {
printf("%c \r", c);
fflush(stdout);
}
}
disableRawMode();
*/
CPU6502 cpu;
cpu.write = apple1_write_memory;
cpu.read = apple1_read_memory;
emu6502_init_cpu(&cpu);
emu6502_reset(&cpu);
for (;;) {
emu6502_run_opcode(&cpu);
}
disableRawMode();
return 0;
}