This repository was archived by the owner on Dec 23, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy path6502retro.c
More file actions
533 lines (479 loc) · 14.5 KB
/
Copy path6502retro.c
File metadata and controls
533 lines (479 loc) · 14.5 KB
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
/* vim: set ts=8 sw=8 et cc=80 tw=80: */
/* 6502-Retro - Schematic:
* https://github.com/6502-retro/6502-retro-v3/blob/main/docs/6502-Retro-Bank-V3-kicad8.pdf
*
* ---- Hardware Description --------------------------------------------------
* CPU - WD65C02
* RAM - 64KB
* Pageable Rom - 8K @ 0xE000
* Rom switch via pa6 - Pulled up by resistor, 0 = ROM disabled.
* Pageable Ram - 8K @ 0xC000
* IO Page @ 0xBF00
* 1 VIA with SDCARD attached over SPI. See:
* SPI_CLK via pa0
* SPI_CS via pa1
* SPI_MOSI via pa7
* SPI_MISO via Shift Register.
* SPI_CLK connected to via CB1 for SR clock
* CB2 connected to SDCARD MISO
*
* Other VIA Pins:
* SN76489 /SNWE pa2
* SN76489 SNREADY pa3
* LED pa4
* BUTTON pa5
*
* All of PB is used for SN76589 Auido
* Rockwell 6551 UART
* TMS9918a display (Pico9918 or F18A etc.)
* No externel keyboard input - only serial console. Graphics is output only.
*
* ---- Emulator Description --------------------------------------------------
* We model the SDCARD attached to the VIA by directly writing to to the via
* shift register after all 8 bits of the SPI transfer to the sdcard are
* completed.
*
* we do not model the audio, led or button.
*
* The bootloader had to be altered to work with this emulator. The hardware
* issues a number of empty bytes to the sdcard spi interface before and after
* each sd command. It's not clear why that's required on hardware but the
* emulator does not support this kind of carry-on. For now, the bootloader rom
* must be compiled from the `emulator` branch.
*
* ---- Execute ---------------------------------------------------------------
* ./6502retro \
* -r ../6502-retro/6502-retro-boot/build/rom.raw \
* -S ../6502-retro/6502-retro-os/py_sfs_v2/6502-retro-sdcard.img \
* -T
* Read the EMULATOR.md markdown files in the following repos for information
* on how to make the rom and and sdcard images.
*
* - Rom repository: https://github.com/6502-retro/6502-retro-boot.git
* - OS repository: https://github.com/6502-retro/6502-retro-os.git
*
*/
#include <SDL2/SDL_timer.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <glob.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <poll.h>
#include <stdarg.h>
#include <termios.h>
#include <signal.h>
#include <time.h>
#include <SDL2/SDL.h>
#include "event.h"
#include "serialdevice.h"
#include "ttycon.h"
#include "fake65c02.h"
#include "6522.h"
#include "6551.h"
#include "sdcard.h"
#include "tms9918a.h"
#include "tms9918a_render.h"
#include "sn76489.h"
#define TRACE_MEM 1
#define TRACE_IRQ 2
#define TRACE_CPU 4
#define TRACE_6551 8
#define TRACE_SD 16
#define TRACE_SPI 32
#define TRACE_TMS9918A 64
#define TRACE_VIA 128
#define ROM_SWITCH 0x40
#define SN_RDY_ON 0x08
#define SN_RDY_OFF 0xF7
#define VIA_SR 10
// LED OFF, ROM SWITCH ON, other active lows disabled.
#define VIA_PA_DEFAULT 0xC7
// PA5 and PA3 are inputs */
#define VIA_DDRA_DEFAULT 0xD7
static struct serial_device *con;
static struct termios saved_term, term;
static struct sdcard *sdcard;
static struct tms9918a *vdp;
static struct tms9918a_renderer *vdprend;
static struct via6522 *via1;
static struct m6551 *uart;
static struct sn76489 *sn;
volatile int emulator_done;
static uint8_t fast = 0;
static int trace = 0;
static uint8_t ram[0x10000];
static uint8_t paged_ram[64 * 0x2000];
static uint8_t rom[0x2000];
uint8_t do_read_6502(uint16_t addr, unsigned debug)
{
/* ROM */
bool flash_in = via_get_port_a(via1) & ROM_SWITCH;
if (flash_in && (addr >= 0xE000))
{
if (debug) fprintf(stderr, "Read from rom\n");
return rom[addr - 0xE000];
}
/* Serial */
if (uart && (addr & 0xFFF0) == 0xBF10)
{
if (debug) fprintf(stderr, "Read from uart\n");
return m6551_read(uart, addr & 0x03);
}
/* VIA - SPI to SDCARD */
if (via1 && (addr & 0xFFF0) == 0xBF20)
{
uint8_t data = via_read(via1, addr & 0x0F);
if (addr == 0xBF21) {
bool snrdy = sn76489_ready(sn);
if (snrdy)
data |= SN_RDY_ON;
else
data &= SN_RDY_OFF;
}
if (debug)
fprintf(stderr, "Read from via: %x = %02x\n", addr & 0x0f, data);
return data;
}
/* TMS 9918a */
if (vdp && (addr & 0xFFF0) == 0xBF30)
{
if (debug) fprintf(stderr, "Read from vdp\n");
return tms9918a_read(vdp, addr);
}
/* banked ram */
if (addr >= 0xC000 && addr < 0xE000)
{
uint8_t bank = ram[0xBF00];
if (debug) fprintf(stderr, "Read from bank [%d]\n",bank);
return paged_ram[(bank * 0x2000) + (0xC000-addr)];
}
return ram[addr];
}
uint8_t read6502_debug(uint16_t addr)
{
uint8_t r = do_read_6502(addr, 1);
if (trace & TRACE_MEM) {
fprintf(stderr, "%04X -> %02X\n", addr, r);
}
return r;
}
uint8_t read6502(uint16_t addr)
{
uint8_t r = do_read_6502(addr, 0);
if (trace & TRACE_MEM) {
fprintf(stderr, "%04X -> %02X\n", addr, r);
}
return r;
}
void write6502(uint16_t addr, uint8_t val)
{
/* ROM */
bool flash_in = via_get_port_a(via1) & ROM_SWITCH;
if (flash_in && (addr >= 0xE000))
{
return;
}
/* Serial */
if (uart && (addr & 0xFFF0) == 0xBF10)
{
m6551_write(uart, addr & 0x03, val);
return;
}
/* VIA - SPI to SDCARD */
if (via1 && (addr & 0xFFF0) == 0xBF20)
{
/* we want to make sure that ROM_SWITCH is pulled up when
* setting the direction to input.
*/
if ((addr == 0xBF23) && ((val & ROM_SWITCH) == 0))
{
uint8_t pa = via_get_port_a(via1);
pa |= ROM_SWITCH;
via_write(via1, 1, pa);
}
else if (addr == 0xBF20)
{
sn76489_write(sn, val);
}
else
via_write(via1, addr & 0x0F, val);
return;
}
/* TMS 9918a */
if (vdp && (addr & 0xFFF0) == 0xBF30)
{
tms9918a_write(vdp, addr, val);
return;
}
/* banked ram */
if (addr >= 0xC000 && addr < 0xE000)
{
uint8_t bank = ram[0xBF00];
paged_ram[(bank * 0x2000) + (0xC000-addr)] = val;
return;
}
ram[addr] = val;
return;
}
/* We do this in the 6502 loop instead. Provide a dummy for the device models */
void recalc_interrupts(void)
{
}
/* Every time the via is written to, this function is called. We need to gather
* spi bits and interface with the sdcard module
*/
void via_recalc_outputs(struct via6522 *via)
{
uint8_t port = via_get_port_a(via);
bool clk = port & 1;
bool is_sdcard = !((port >> 1) & 1);
bool mosi = port >> 7;
if (trace & TRACE_SPI)
fprintf(stderr, "SPI: sdcs=%d, sdclk=%d, mosi=%d\n", is_sdcard, clk, mosi);
/* SD_CS edge detection */
static bool last_sdcard;
static uint8_t bit_counter = 0;
if (!last_sdcard && is_sdcard) {
bit_counter = 0;
sd_spi_lower_cs(sdcard);
}
last_sdcard = is_sdcard;
static int init_counter = 0;
static bool initialized = false;
/* For initialization, the client has to pull&release CLK 74 times.
* The SD card should be deselected, because it's not actual
* data transmission - Its possible that the sdcard is erronously
* selected so we catch that here and only assert SD_CS after 74 init
* clocks.
*/
if (!initialized) {
if (clk) {
init_counter++;
if (init_counter >= 74) {
sd_spi_lower_cs(sdcard);
initialized = true;
}
}
return;
}
if (!is_sdcard) {
return;
}
/* SD_CLK edge detection */
static bool last_clk = false;
if (clk == last_clk) {
return;
}
last_clk = clk;
if (!clk) { // only care about rising clock
return;
}
// SD is selected, Clock is high, gather MOSI bits into outbyte.
static uint8_t inbyte, outbyte;
outbyte <<= 1;
outbyte |= mosi;
bit_counter++;
if (bit_counter != 8) {
return;
}
bit_counter = 0;
/* If we have reached this far, then we know that the sdcard is selected
* and the bit counter has reached 8. It's time to ask the sdcard
* library for a new byte.
*/
inbyte = sd_spi_in(sdcard, outbyte);
if (trace & TRACE_SPI)
fprintf(stderr, "SDSPI: %02X -> %02X\n", outbyte, inbyte);
/* We are not modelling the VIA shift register with all of its CB1 and
* CB2 logic. We just insert the byte directly into the SR so the 6502
* can read it out again.
*/
via_write(via, VIA_SR, inbyte);
}
// Stubs - not used here.
void via_handshake_a(struct via6522 *via)
{
}
void via_handshake_b(struct via6522 *via)
{
}
static int romload(const char *path, uint8_t *mem, unsigned int maxsize)
{
int fd;
int size;
fd = open(path, O_RDONLY);
if (fd == -1) {
perror(path);
exit(1);
}
size = read(fd, mem, maxsize);
close(fd);
return size;
}
static void cleanup(int sig)
{
tcsetattr(0, TCSADRAIN, &saved_term);
emulator_done = 1;
}
static void exit_cleanup(void)
{
tcsetattr(0, TCSADRAIN, &saved_term);
sn76489_destroy(sn);
SDL_Quit();
}
void termon(void)
{
if (tcgetattr(0, &term) == 0) {
saved_term = term;
atexit(exit_cleanup);
signal(SIGINT, cleanup);
signal(SIGQUIT, cleanup);
signal(SIGPIPE, cleanup);
term.c_lflag &= ~(ICANON | ECHO);
term.c_cc[VMIN] = 0;
term.c_cc[VTIME] = 1;
term.c_cc[VINTR] = 0;
term.c_cc[VSUSP] = 0;
term.c_cc[VSTOP] = 0;
tcsetattr(0, TCSADRAIN, &term);
}
}
static void irqnotify(void)
{
if (via1 && via_irq_pending(via1))
irq6502();
else if (uart && m6551_irq_pending(uart))
irq6502();
else if (vdp && tms9918a_irq_pending(vdp))
irq6502();
}
static void take_a_nap(void)
{
struct timespec t;
t.tv_sec = 0;
t.tv_nsec = 15000000;
if (nanosleep(&t, NULL))
perror("nanosleep");
}
static void usage(void)
{
fprintf(stderr, "6502retro: [-1] [-r rompath] [-S sdcard] [-T] [-f] [-d debug]\n");
exit_cleanup();
exit(EXIT_FAILURE);
}
int main(int argc, char *argv[])
{
int opt;
int fd;
char *rompath = "6502retro.rom";
char *sdpath = NULL;
unsigned have_tms = 0;
static int tstates = 666; //4mhz / 60 / 10 = 60fps and doing 10 cycles between each call to SDL_Delay()
while ((opt = getopt(argc, argv, "d:fr:S:T")) != -1) {
switch (opt) {
case 'r':
rompath = optarg;
break;
case 'S':
sdpath = optarg;
break;
case 'd':
trace = atoi(optarg);
break;
case 'f':
fast = 1;
break;
case 'T':
have_tms = 1;
break;
default:
usage();
}
}
if (optind < argc)
usage();
uint16_t rsize = romload(rompath, rom, 0x2000);
fprintf(stderr,"Loaded %04x bytes from %s into rom\n", rsize, rompath);
if (rsize != 0x2000) {
fprintf(stderr, "6502retro: invalid BOOT ROM\n");
exit(EXIT_FAILURE);
}
via1 = via_create();
if (via1 && trace & TRACE_VIA)
via_trace(via1, 1);
sdcard = sd_create("sd0");
if (sdpath) {
fd = open(sdpath, O_RDWR);
if (fd == -1) {
perror(sdpath);
exit(1);
}
sd_attach(sdcard, fd);
via_write(via1, 1, VIA_PA_DEFAULT);
via_write(via1, 3, VIA_DDRA_DEFAULT);
}
if (trace & TRACE_SD)
sd_trace(sdcard, 1);
sd_blockmode(sdcard);
ui_init();
if (have_tms) {
vdp = tms9918a_create();
tms9918a_trace(vdp, !!(trace & TRACE_TMS9918A));
vdprend = tms9918a_renderer_create(vdp);
/* SDL init called in tms9918a_renderer_create */
}
termon();
con = &console;
uart = m6551_create();
m6551_trace(uart, trace & TRACE_6551);
m6551_attach(uart, con);
// Add audio device
sn = sn76489_create();
hookexternal(irqnotify);
reset6502();
/* This is the wrong way to do it but it's easier for the moment. We
should track how much real time has occurred and try to keep cycle
matched with that. The scheme here works fine except when the host
is loaded though */
uint64_t start, end;
float elapsedMS;
while (!emulator_done) {
if (vdp) {
start = SDL_GetPerformanceCounter();
}
int i;
for (i = 0; i < 100; i++) {
exec6502(tstates);
via_tick(via1, tstates);
m6551_timer(uart);
}
// Need to poll the sdl event handler quit offten.
if (vdp)
if (ui_event())
emulator_done = 1;
/* leverage the SDL_GetTicks() to figure out how long to wait
* before rendering the next frame. This gives a nice 60hz
* approximation.
*/
if (!fast)
{
if (vdp)
{
tms9918a_rasterize(vdp);
tms9918a_render(vdprend);
end = SDL_GetPerformanceCounter();
elapsedMS = (end -start) / (float)SDL_GetPerformanceFrequency() * 1000.0f;
SDL_Delay( (16.6667f - elapsedMS) > 0 ? 16.6667f - elapsedMS : 0);
}
else
{
take_a_nap();
}
}
}
exit(0);
}