|
| 1 | +#include <stdio.h> |
| 2 | +#ifndef _WIN32 |
| 3 | +#include <sys/stat.h> |
| 4 | + |
| 5 | +#include "perfect6502.h" |
| 6 | + |
| 7 | +/************************************************************ |
| 8 | + * |
| 9 | + * Interface to OS Library Code / Monitor |
| 10 | + * |
| 11 | + ************************************************************/ |
| 12 | + |
| 13 | +/* imported by runtime.c */ |
| 14 | +unsigned char A, X, Y, S, P; |
| 15 | +unsigned short PC; |
| 16 | +int N, Z, C; |
| 17 | + |
| 18 | +void |
| 19 | +init_monitor() |
| 20 | +{ |
| 21 | + FILE *f; |
| 22 | + f = fopen("apple1basic.bin", "r"); |
| 23 | + fread(memory + 0xE000, 1, 4096, f); |
| 24 | + fclose(f); |
| 25 | + |
| 26 | + memory[0xfffc] = 0x00; |
| 27 | + memory[0xfffd] = 0xE0; |
| 28 | + |
| 29 | +} |
| 30 | + |
| 31 | + |
| 32 | +void |
| 33 | +charout(char ch) { |
| 34 | + unsigned short S = readSP(); |
| 35 | + unsigned char a = 1 + memory[0x0100+S+1] | memory[0x0100+((S+2) & 0xFF)] << 8; |
| 36 | + |
| 37 | + /* |
| 38 | + * Apple I BASIC prints every character received |
| 39 | + * from the terminal. UNIX terminals do this |
| 40 | + * anyway, so we have to avoid printing every |
| 41 | + * line again |
| 42 | + */ |
| 43 | + if (a==0xe2a6) /* character echo */ |
| 44 | + return; |
| 45 | + if (a==0xe2b6) /* CR echo */ |
| 46 | + return; |
| 47 | + |
| 48 | + /* |
| 49 | + * Apple I BASIC prints a line break and 6 spaces |
| 50 | + * after every 37 characters. UNIX terminals do |
| 51 | + * line breaks themselves, so ignore these |
| 52 | + * characters |
| 53 | + */ |
| 54 | + if (a==0xe025 && (ch==10 || ch==' ')) |
| 55 | + return; |
| 56 | + |
| 57 | + /* INPUT */ |
| 58 | + if (a==0xe182) { |
| 59 | +#if _WIN32 |
| 60 | + if (!isatty(0)) |
| 61 | + return; |
| 62 | +#else |
| 63 | + struct stat st; |
| 64 | + fstat(0, &st); |
| 65 | + if (S_ISFIFO (st.st_mode)) |
| 66 | + return; |
| 67 | +#endif |
| 68 | + } |
| 69 | +#endif |
| 70 | + |
| 71 | + putc(ch, stdout); |
| 72 | + fflush(stdout); |
| 73 | +} |
| 74 | + |
| 75 | +void |
| 76 | +handle_monitor() |
| 77 | +{ |
| 78 | + if (readRW()) { |
| 79 | + unsigned short a = readAddressBus(); |
| 80 | + if ((a & 0xFF1F) == 0xD010) { |
| 81 | + unsigned char c = getchar(); |
| 82 | + if (c == 10) |
| 83 | + c = 13; |
| 84 | + c |= 0x80; |
| 85 | + writeDataBus(c); |
| 86 | + } |
| 87 | + if ((a & 0xFF1F) == 0xD011) { |
| 88 | + if (readPC() == 0xE006) |
| 89 | + /* if the code is reading a character, we have one ready */ |
| 90 | + writeDataBus(0x80); |
| 91 | + else |
| 92 | + /* if the code checks for a STOP condition, nothing is pressed */ |
| 93 | + writeDataBus(0); |
| 94 | + } |
| 95 | + if ((a & 0xFF1F) == 0xD012) { |
| 96 | + /* 0x80 would mean we're not yet ready to receive a character */ |
| 97 | + writeDataBus(0); |
| 98 | + } |
| 99 | + } else { |
| 100 | + unsigned short a = readAddressBus(); |
| 101 | + unsigned char d = readDataBus(); |
| 102 | + if ((a & 0xFF1F) == 0xD012) { |
| 103 | + unsigned char temp8 = d & 0x7F; |
| 104 | + if (temp8 == 13) |
| 105 | + temp8 = 10; |
| 106 | + charout(temp8); |
| 107 | + } |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +int |
| 112 | +main() |
| 113 | +{ |
| 114 | + int clk = 0; |
| 115 | + |
| 116 | + initAndResetChip(); |
| 117 | + |
| 118 | + /* set up memory for user program */ |
| 119 | + init_monitor(); |
| 120 | + |
| 121 | + /* emulate the 6502! */ |
| 122 | + for (;;) { |
| 123 | + step(); |
| 124 | + clk = !clk; |
| 125 | + if (!clk) |
| 126 | + handle_monitor(); |
| 127 | + |
| 128 | +// chipStatus(); |
| 129 | + //if (!(cycle % 1000)) printf("%d\n", cycle); |
| 130 | + }; |
| 131 | +} |
0 commit comments