forked from EtchedPixels/Virtual65
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplatform.c
245 lines (219 loc) · 4.1 KB
/
platform.c
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
#include "config.h"
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <time.h>
#include <sys/ioctl.h>
#include <termios.h>
#include "6502.h"
static uint8_t ram[128][8192];
static uint8_t bank[8] = { 0, 1, 2, 3, 4, 5, 6, 7 };
static uint8_t timer_int;
static uint16_t blk;
static uint8_t disk;
static uint8_t diskstat;
FILE *diskfile;
void dump_mem(void);
static uint8_t 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;
}
static uint8_t io_read(uint16_t addr)
{
addr -= 0xFE00;
if (addr < 8)
return bank[addr];
if (addr == 0x10) {
uint8_t v = timer_int;
timer_int = 0;
return v;
}
if (addr == 0x20)
return next_char();
if (addr == 0x21)
return check_chario();
if (addr == 0x30)
return disk;
if (addr == 0x31)
return blk >> 8;
if (addr == 0x32)
return blk & 0xFF;
if (addr == 0x34)
return fgetc(diskfile);
if (addr == 0x35) {
uint8_t v = diskstat;
diskstat = 0;
return v;
}
return 0xFF;
}
static void io_write(uint16_t addr, uint8_t value)
{
addr -= 0xFE00;
if (addr < 8) {
bank[addr] = value & 127;
// printf("bank %d -> %d\n", addr, value);
return;
}
if (addr == 0x20) {
putchar(value);
fflush(stdout);
return;
}
if (addr == 0x30) {
disk = value;
return;
}
if (addr == 0x31) {
blk &= 0xFF;
blk |= value << 8;
return;
}
if (addr == 0x32) {
blk &= 0xFF00;
blk |= value;
return;
}
if (addr == 0x33) {
if (disk != 0)
diskstat = 0x02;
else
diskstat = (fseek(diskfile, blk << 9, SEEK_SET) == -1) ? 0x01 : 0;
return;
}
if (addr == 0x34) {
fputc(value, diskfile);
return;
}
if (addr == 0x3F)
{
dump_mem();
}
if (addr == 0x40 && value == 0xA5)
exit(0);
}
uint8_t read6502(uint16_t addr)
{
uint8_t bnum = bank[addr >> 13];
if (addr >= 0xFE00 && addr <= 0xFFF0)
return io_read(addr);
else {
//printf("R Bank %d Off %d\n", bnum, addr & 0x1FFF);
return ram[bnum][addr & 0x1FFF];
}
}
void write6502(uint16_t addr, uint8_t value)
{
uint8_t bnum = bank[addr >> 13];
if (addr >= 0xFE00 && addr <= 0xFFF0)
io_write(addr, value);
else {
//printf("W Bank %d Off %d %02X\n", bnum, addr & 0x1FFF, value);
ram[bnum][addr & 0x1FFF] = value;
}
}
static void system_process(void)
{
if (timer_int)
irq6502();
}
static struct termios saved_term, term;
static void cleanup(int sig)
{
ioctl(0, TCSETS, &saved_term);
exit(1);
}
static void exit_cleanup(void)
{
ioctl(0, TCSETS, &saved_term);
}
static void take_a_nap(void)
{
struct timespec t;
t.tv_sec = 0;
t.tv_nsec = 10000000;
if (nanosleep(&t, NULL))
perror("nanosleep");
}
void dump_mem(void)
{
for (int j = 0; j < 0x0280; j++) //FE20
{
unsigned int byte = read6502(0x0000 + j);
if(byte != 0x00)
printf("%04x %02x\n", j, byte);
}
for (int k = 0xFC00; k < 0xFD00; k++) //FE20
{
unsigned int byte = read6502(0x0000 + k);
if(byte != 0x00)
printf("%04x %02x\n", k, byte);
}
return;
}
int main(int argc, char *argv[])
{
if (ioctl(0, TCGETS, &term) == 0) {
saved_term = term;
atexit(exit_cleanup);
signal(SIGINT, cleanup);
signal(SIGQUIT, cleanup);
term.c_iflag &= ~ICANON;
term.c_cc[VMIN] = 1;
term.c_cc[VTIME] = 0;
ioctl(0, TCSETS, &term);
}
diskfile = fopen("disk0", "r+");
if (diskfile == NULL) {
perror("disk0");
exit(1);
}
if (fread(ram[7] + 0x1C00, 512, 1, diskfile) != 1) {
fprintf(stderr, "Unable to read boot image.\n");
exit(1);
}
write6502(0xFFFC, 0x00);
write6502(0xFFFD, 0xFC);
/* for (int i = 0; i < 512; i++)
{
printf("%x\n", read6502(0xFC00 + i));
}
*/
reset6502();
hookexternal(system_process);
while (1) {
exec6502(41943);
take_a_nap();
timer_int++;
}
}