2014-12-04 17:43:58 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdint.h>
|
2014-12-07 08:37:16 +00:00
|
|
|
#include <stdlib.h>
|
2014-12-04 17:43:58 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2014-12-07 08:37:16 +00:00
|
|
|
static uint8_t mem[0x10000];
|
|
|
|
static uint8_t key = 0x80;
|
|
|
|
static uint8_t last = 0x00;
|
2014-12-04 17:43:58 +00:00
|
|
|
|
2014-12-07 04:54:47 +00:00
|
|
|
void prn(int c) {
|
2014-12-07 07:35:46 +00:00
|
|
|
uint8_t fp;
|
|
|
|
__asm__("movs %0, r12": "=r"(fp));
|
2014-12-07 08:37:16 +00:00
|
|
|
fprintf(stderr, "### $%02x ###\n", c);
|
|
|
|
fflush(stderr);
|
2014-12-07 07:35:46 +00:00
|
|
|
__asm__("movs r12, %0":: "r"(fp));
|
2014-12-07 04:54:47 +00:00
|
|
|
}
|
|
|
|
|
2014-12-04 17:43:58 +00:00
|
|
|
void cpu6502_dump(
|
|
|
|
uint16_t pc, uint8_t a, uint8_t x, uint8_t y, uint8_t sp, uint8_t sr) {
|
2014-12-07 07:35:46 +00:00
|
|
|
uint8_t fp;
|
|
|
|
__asm__("movs %0, r12": "=r"(fp));
|
2014-12-07 08:37:16 +00:00
|
|
|
fprintf(stderr, "*** dump *** PC=$%04x A=$%02x X=$%02x Y=$%02x SP=$%02x "
|
|
|
|
"NV-B_DIZC=%d%d-%d_%d%d%d%d\n",
|
|
|
|
pc, a, x, y, sp, (sr >> 7) & 1, (sr >> 6) & 1, (sr >> 4) & 1,
|
|
|
|
(sr >> 3) & 1, (sr >> 2) & 1, (sr >> 1) & 1, sr & 1);
|
|
|
|
fflush(stderr);
|
2014-12-07 07:35:46 +00:00
|
|
|
__asm__("movs r12, %0":: "r"(fp));
|
2014-12-04 17:43:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t cpu6502_load(uint16_t addr) {
|
2014-12-07 07:35:46 +00:00
|
|
|
uint8_t fp;
|
2014-12-07 08:37:16 +00:00
|
|
|
uint8_t result = 0;
|
2014-12-07 07:35:46 +00:00
|
|
|
__asm__("movs %0, r12": "=r"(fp));
|
2014-12-07 08:37:16 +00:00
|
|
|
if (0x0400 <= addr && addr <= 0x7ff) {
|
|
|
|
// Fake text VRAM.
|
|
|
|
result = mem[addr];
|
|
|
|
} else if ((0x0300 <= addr && addr <= 0x03ff) ||
|
|
|
|
(0x0900 <= addr && addr <= 0x0fff)) {
|
|
|
|
// Fake memory impl to make it run on low memory chip.
|
|
|
|
result = last;
|
|
|
|
} else if (0x1000 <= addr & addr < 0xbfff) {
|
|
|
|
// Bus error due to minimum memory installation.
|
|
|
|
return 0xff;
|
|
|
|
fprintf(stdout, "\e[1;1Hinvalid load $%04x\n", addr);
|
|
|
|
fflush(stdout);
|
|
|
|
abort();
|
|
|
|
} else if (0xc000 <= addr & addr < 0xcfff) {
|
|
|
|
// I/O emulation.
|
|
|
|
if (0xc000 == addr)
|
|
|
|
result = key;
|
|
|
|
if (0xc010 == addr)
|
|
|
|
key &= 0x7f;
|
|
|
|
} else {
|
|
|
|
// Installed memory.
|
|
|
|
result = mem[addr];
|
|
|
|
}
|
|
|
|
fprintf(stderr, "load $%04x => $%02x\n", addr, result);
|
|
|
|
fflush(stderr);
|
2014-12-07 07:35:46 +00:00
|
|
|
__asm__("movs r12, %0":: "r"(fp));
|
2014-12-07 08:37:16 +00:00
|
|
|
return result;
|
2014-12-04 17:43:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void cpu6502_store(uint16_t addr, uint8_t data) {
|
2014-12-07 07:35:46 +00:00
|
|
|
uint8_t fp;
|
|
|
|
__asm__("movs %0, r12": "=r"(fp));
|
2014-12-07 08:37:16 +00:00
|
|
|
if (0x0400 <= addr && addr <= 0x7ff) {
|
|
|
|
// Fake text VRAM.
|
|
|
|
char ascii[0x40] =
|
|
|
|
"@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_ !\"#$%&'()*+,-./0123456789:;<=>?";
|
|
|
|
mem[addr] = data;
|
|
|
|
int y = (addr - 0x400) >> 7;
|
|
|
|
int x = addr & 0x7f;
|
|
|
|
if (x >= 0x50) {
|
|
|
|
x -= 0x50;
|
|
|
|
y += 0x10;
|
|
|
|
} else if (x >= 0x28) {
|
|
|
|
x -= 0x28;
|
|
|
|
y += 0x08;
|
|
|
|
}
|
|
|
|
if (y < 24)
|
|
|
|
printf("\e[%d;%dH%c", y + 2, x + 1, ascii[data & 0x3f]);
|
|
|
|
} else if ((0x0300 <= addr && addr <= 0x03ff) ||
|
|
|
|
(0x0900 <= addr && addr <= 0x0fff)) {
|
|
|
|
// Fake memory impl to make it run on low memory chip.
|
|
|
|
last = data;
|
|
|
|
} else if (0x1000 <= addr & addr < 0xcfff) {
|
|
|
|
// Bus error due to minimum memory installation.
|
|
|
|
return;
|
|
|
|
fprintf(stdout, "\e[1;1Hinvalid store $%04x\n", addr);
|
|
|
|
fflush(stdout);
|
|
|
|
abort();
|
|
|
|
} else {
|
|
|
|
// Installed memory.
|
|
|
|
mem[addr] = data;
|
|
|
|
}
|
|
|
|
fprintf(stderr, "store $%04x <= $%02x\n", addr, data);
|
|
|
|
fflush(stderr);
|
2014-12-07 07:35:46 +00:00
|
|
|
__asm__("movs r12, %0":: "r"(fp));
|
2014-12-04 17:43:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
|
|
|
FILE* fp = fopen("applebasic.rom", "rb");
|
2014-12-07 15:21:06 +00:00
|
|
|
//FILE* fp = fopen("apple2o.rom", "rb");
|
2014-12-04 17:43:58 +00:00
|
|
|
memset(mem, 0, 0x10000);
|
|
|
|
fread(&mem[0xd000], 1, 0x3000, fp);
|
|
|
|
fclose(fp);
|
|
|
|
cpu6502_reset();
|
2014-12-07 08:37:16 +00:00
|
|
|
fprintf(stderr, "quit: $%04x\n", cpu6502_run());
|
2014-12-04 17:43:58 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|