-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathautoSim.cpp
257 lines (234 loc) · 8.23 KB
/
autoSim.cpp
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
#include "simulieren-6502.h"
#include <cctype>
#include <cstdlib>
#include <cstdio>
#include <cstdint>
#include <cstring>
#include <unistd.h>
#include <fcntl.h>
extern uint8_t A, X, Y, stackPointer;
extern uint16_t programCounter;
extern bool flagNegative, flagOverflow, flagBRK, flagDecimal, flagIRQdisable, flagZero, flagCarry;
#define BUF_SIZE 1024
#define MEMORY_SIZE 0x10000
#define OUTPUT_ADDR 0x7FFF
uint8_t memory[MEMORY_SIZE];
uint16_t breakpoint = 0000;
void printRegs() {
printf("]A = $%02X\tX = $%02X\tY = $%02X\n", A, X, Y);
printf("]PC = $%04X\tSP = $%02X\n", programCounter, stackPointer);
printf("]Status register: %c%c-%c%c%c%c%c\n", flagNegative?'N':'n',
flagOverflow?'V':'v', flagBRK?'B':'b', flagDecimal?'D':'d',
flagIRQdisable?'I':'i', flagZero?'Z':'z', flagCarry?'C':'c');
}
uint8_t readByte(uint16_t address) {
if (address == OUTPUT_ADDR) {
printf(">");
memory[address] = getc(stdin);
}
return memory[address];
}
void writeByte(uint16_t address, uint8_t data){
if (address == OUTPUT_ADDR) {
printf("%c", data);
}
memory[address] = data;
}
// loads an Intel Hex (I8HEX) file
void loadHexFile(char *filename = NULL) {
//printf("hex loading not yet implemented.\n");
//return;
// open file
FILE *hexFile = fopen(filename, "r");
if (hexFile == NULL) {
printf("]Error opening hex file: %s", filename);
perror("");
return;
}
char lineBuf[BUF_SIZE];
unsigned char byteCount, recType; // number of bytes stored in the line and the record type
unsigned char checksum; // stored checksum
int accumulate; // the running total of the calculated checksum
unsigned short address; // target address of a data line
unsigned char data[255]; // a data line may contain 0xFF bytes
int linecount = 0; // how many lines have been processed
unsigned int i; // loop counter. value is dropped through and the variable is re-used.
// while dataLeft
while (fgets(lineBuf, BUF_SIZE, hexFile) != NULL) {
// parse in a hex record
sscanf(lineBuf, ":%02hhX%04hX%02hhX", &byteCount, &address, &recType);
for (i = 0; i < byteCount; i++) {
sscanf(&lineBuf[2 * i + 9], "%02hhX", &data[i]);
}
// i is left one higher
sscanf(&lineBuf[2 * i + 9], "%02hhX", (unsigned char *)&checksum);
// calculate checksum
accumulate = byteCount + ((address & 0xFF00) >> 8) + (address & 0x00FF) + recType;
for (i = 0; i < byteCount; i++) {
accumulate += data[i];
}
// checksum should == -accumulate
if ((-accumulate & 0x00FF) != checksum) {
printf("]Checksum error on line %i!\n", linecount);
printf("]Stored checksum: %02X. Calculated checksum: %02X.\n", checksum, (-accumulate & 0x00FF));
}
linecount++;
// type 0x00 = data with 16-bit start addr
if (recType == 0x00) {
// write to memory
for (i = 0; i < byteCount; i++) {
memory[address+i] = data[i];
//printf("%04X %02X\n", i, data[i]);
}
} else { // type 0x01 = EOF
break;
}
}
// close file
fclose(hexFile);
printf("]%i lines loaded.\n", linecount);
}
/* commands:
view registers v
reset r
load Intel Hex file l - prompts for filename
set PC s aaaa
set breakpoint b aaaa
write byte w aaaa dd
read byte r aaaa
run for n instructions x nnnn
run for one instruction x
free-run f - stop this mode with ^A
quit q
*/
int main(int argc, char *argv[]) {
//autoSim [filename [start addr [breakpoint]]]
if (argc > 1) {
// auto-start
// load in the program
loadHexFile(argv[1]);
//reset the emulated processor
reset6502(true);
// if provided, read in and set the start address
if (argc > 2) {
sscanf(argv[2], "%hX", &programCounter);
//programCounter = atoi(argv[2]);
}
// if provided, read in and set the breakpoint
if (argc > 3) {
sscanf(argv[3], "%hX", &breakpoint);
}
while (true) {
do6502();
if (programCounter == breakpoint) {
printf("]Breakpoint hit!\n");
printRegs();
break;
}
}
}
char cmd;
uint8_t data;
uint16_t address;
char buf[BUF_SIZE];
int matched;
// set up the non-blocking copy of stdin for the free-running mode
//int stdin_NB_FD = dup(STDIN_FILENO);
//fcntl(stdin_NB_FD, F_SETFL, (fcntl(stdin_NB_FD, F_GETFL, 0)|O_NONBLOCK));
//FILE *stdin_NB = fdopen(stdin_NB_FD, "r");
//printf("6502 sim\n");
// main program loop
while (true) {
// print registers
//printRegs();
printf("\n]>");
// get input
if(!fgets(buf, BUF_SIZE, stdin)) {
perror("Error reading STDIN");
printf("Terminating...\n");
exit(EXIT_FAILURE);
}
matched = sscanf(buf, "%c %hx %hhx", &cmd, &address, &data);
// act on it
if (matched == 0) {
printf("]Doing nothing.\n");
} else if (matched == 1) {
// r, x, q, l
if (tolower(cmd) == 'q') {
break;
}
switch (tolower(cmd)) {
case 'r':
//reset6502(false);
reset6502(true);
break;
case 'x': // execute one
do6502();
printRegs();
break;
case 'l':
char filename[BUF_SIZE];
// get file name
printf("]Filename: ");
fgets(filename, BUF_SIZE, stdin);
filename[strcspn(filename, "\r\n")] = 0;
// load hex file
loadHexFile(filename);
break;
case 'v':
printRegs();
break;
case 'f':
while (true) {
do6502();
if (programCounter == breakpoint) {
printf("]Breakpoint hit!\n");
printRegs();
break;
}
}
break;
default:
printf("]Unrecognized command\n");
break;
}
} else if (matched == 2) {
// s, r, x
switch (tolower(cmd)) {
case 's':
programCounter = (uint16_t) address;
break;
case 'r':
printf("]$%04X: $%02X\n", address, readByte(address));
break;
case 'x':
printf("]Executing $%X(%i) instructions\n", address, address);
for (unsigned int i = 0; i < address; i++){
do6502();
if (programCounter == breakpoint) {
printf("]Breakpoint hit!\n");
printRegs();
break;
}
}
printRegs();
break;
case 'b':
breakpoint = address;
printf("]Set breakpoint at address %04X\n", breakpoint);
break;
default:
printf("]Unrecognized command\n");
break;
}
} else if (matched == 3) {
// w
if (tolower(cmd) == 'w') {
writeByte(address, data);
} else {
printf("]Unrecognized command\n");
}
}
}
exit(EXIT_SUCCESS);
}