-
Notifications
You must be signed in to change notification settings - Fork 0
/
c64.c
executable file
·712 lines (598 loc) · 16.2 KB
/
c64.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
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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
/*
* Copyright (c) 2021 Erwin Rol <[email protected]
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* This file was based on the rockbox sidplayer, which was based on the
* tinysid engine.
*
****************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
* $Id$
*
* SID Codec for rockbox based on the TinySID engine
*
* Written by Tammo Hinrichs (kb) and Rainer Sinsch in 1998-1999
* Ported to rockbox on 14 April 2006
*
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* kb explicitly points out that this emulation sounds crappy, though
* we decided to put it open source so everyone can enjoy sidmusic
* on rockbox
*
* v1.1
* Added 16-04-2006: Rainer Sinsch
* Removed all time critical floating point operations and
* replaced them with quick & dirty integer calculations
*
* Added 17-04-2006: Rainer Sinsch
* Improved quick & dirty integer calculations for the resonant filter
* Improved audio quality by 4 bits
*
* v1.2
* Added 17-04-2006: Dave Chapman
* Improved file loading
*
* Added 17-04-2006: Rainer Sinsch
* Added sample routines
* Added cia timing routines
* Added fast forwarding capabilities
* Corrected bug in sid loading
*
* v1.2.1
* Added 04-05-2006: Rainer Sinsch
* Implemented Marco Alanens suggestion for subsong selection:
* Select the subsong by seeking: Each second represents a subsong
*
*/
#include "sid.h"
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include "mos6510.h"
#include "hal.h"
#include "hw_reg_access.h"
#include "SID_PLAYER_hw_platform.h"
static struct mos6510 cpu;
static uint8_t bval;
static uint16_t wval;
static void c64_set_mem8(uint16_t address, uint8_t val)
{
HW_set_8bit_reg((addr_t)CORESDR_AHB_C1_0 + (address), val);
}
static uint8_t c64_get_mem8(uint16_t address)
{
return (HW_get_8bit_reg((addr_t)CORESDR_AHB_C1_0 + (address)) & 0xFF);
}
uint8_t c64_getmem(uint16_t addr)
{
return c64_get_mem8(addr);
}
void c64_setmem(uint16_t addr, uint8_t value)
{
if ((addr & 0xfc00) == 0xd400) {
sid_poke(addr & 0x1f, value);
} else {
c64_set_mem8(addr, value);
}
}
void c64_memcpy(uint16_t dest, const uint8_t* src, uint32_t size)
{
if (dest + size <= 64*1024) {
for (int i = 0; i < size; i++) {
c64_set_mem8(dest + i, src[i]);
}
}
}
void c64_memset(uint16_t dest, uint8_t val, uint32_t size)
{
if (dest + size <= 64*1024) {
for (int i = 0; i < size; i++) {
c64_set_mem8(dest + i, val);
}
}
}
static uint8_t getaddr(uint8_t mode)
{
uint16_t ad,ad2;
switch(mode) {
case MOS6510_MODE_IMP:
return 0;
case MOS6510_MODE_IMM:
return c64_getmem(cpu.pc++);
case MOS6510_MODE_ABS:
ad = c64_getmem(cpu.pc++);
ad |= 256 * c64_getmem(cpu.pc++);
return c64_getmem(ad);
case MOS6510_MODE_ABSX:
ad = c64_getmem(cpu.pc++);
ad |= 256*c64_getmem(cpu.pc++);
ad2 = ad + cpu.x;
return c64_getmem(ad2);
case MOS6510_MODE_ABSY:
ad = c64_getmem(cpu.pc++);
ad |= 256 * c64_getmem(cpu.pc++);
ad2 = ad + cpu.y;
return c64_getmem(ad2);
case MOS6510_MODE_ZP:
ad = c64_getmem(cpu.pc++);
return c64_getmem(ad);
case MOS6510_MODE_ZPX:
ad = c64_getmem(cpu.pc++);
ad += cpu.x;
return c64_getmem(ad & 0xff);
case MOS6510_MODE_ZPY:
ad = c64_getmem(cpu.pc++);
ad += cpu.y;
return c64_getmem(ad & 0xff);
case MOS6510_MODE_INDX:
ad = c64_getmem(cpu.pc++);
ad += cpu.x;
ad2 = c64_getmem(ad&0xff);
ad++;
ad2 |= c64_getmem(ad & 0xff) << 8;
return c64_getmem(ad2);
case MOS6510_MODE_INDY:
ad = c64_getmem(cpu.pc++);
ad2 = c64_getmem(ad);
ad2 |= c64_getmem((ad+1) & 0xff) << 8;
ad = ad2 + cpu.y;
return c64_getmem(ad);
case MOS6510_MODE_ACC:
return cpu.a;
}
return 0;
}
static void setaddr(uint8_t mode, uint8_t val)
{
uint16_t ad,ad2;
switch(mode)
{
case MOS6510_MODE_ABS:
ad = c64_getmem(cpu.pc - 2);
ad |= 256 * c64_getmem(cpu.pc - 1);
c64_setmem(ad, val);
return;
case MOS6510_MODE_ABSX:
ad = c64_getmem(cpu.pc-2);
ad |= 256 * c64_getmem(cpu.pc - 1);
ad2 = ad + cpu.x;
c64_setmem(ad2, val);
return;
case MOS6510_MODE_ZP:
ad = c64_getmem(cpu.pc - 1);
c64_setmem(ad, val);
return;
case MOS6510_MODE_ZPX:
ad = c64_getmem(cpu.pc - 1);
ad += cpu.x;
c64_setmem(ad & 0xff, val);
return;
case MOS6510_MODE_ACC:
cpu.a = val;
return;
}
}
static void putaddr(uint8_t mode, uint8_t val)
{
uint16_t ad,ad2;
switch(mode)
{
case MOS6510_MODE_ABS:
ad = c64_getmem(cpu.pc++);
ad |= c64_getmem(cpu.pc++) << 8;
c64_setmem(ad, val);
return;
case MOS6510_MODE_ABSX:
ad = c64_getmem(cpu.pc++);
ad |= c64_getmem(cpu.pc++) << 8;
ad2 = ad+cpu.x;
c64_setmem(ad2, val);
return;
case MOS6510_MODE_ABSY:
ad = c64_getmem(cpu.pc++);
ad |= c64_getmem(cpu.pc++) << 8;
ad2 = ad + cpu.y;
c64_setmem(ad2, val);
return;
case MOS6510_MODE_ZP:
ad = c64_getmem(cpu.pc++);
c64_setmem(ad, val);
return;
case MOS6510_MODE_ZPX:
ad = c64_getmem(cpu.pc++);
ad += cpu.x;
c64_setmem(ad & 0xff, val);
return;
case MOS6510_MODE_ZPY:
ad = c64_getmem(cpu.pc++);
ad += cpu.y;
c64_setmem(ad & 0xff, val);
return;
case MOS6510_MODE_INDX:
ad = c64_getmem(cpu.pc++);
ad += cpu.x;
ad2 = c64_getmem(ad & 0xff);
ad++;
ad2 |= c64_getmem(ad & 0xff) << 8;
c64_setmem(ad2, val);
return;
case MOS6510_MODE_INDY:
ad = c64_getmem(cpu.pc++);
ad2 = c64_getmem(ad);
ad2 |= c64_getmem((ad + 1) & 0xff) << 8;
ad = ad2 + cpu.y;
c64_setmem(ad, val);
return;
case MOS6510_MODE_ACC:
cpu.a = val;
return;
}
}
static void setflags(uint8_t flag, int cond)
{
if (cond) {
cpu.p |= flag;
} else {
cpu.p &= ~flag;
}
}
static void push(uint8_t val)
{
c64_setmem(0x100 + cpu.s, val);
if (cpu.s) {
cpu.s--;
}
}
static uint8_t pop(void)
{
if (cpu.s < 0xff) {
cpu.s++;
}
return c64_getmem(0x100 + cpu.s);
}
static void branch(uint8_t flag)
{
int8_t dist;
dist = (int8_t)getaddr(MOS6510_MODE_IMM);
wval = cpu.pc + dist;
if (flag) {
cpu.pc = wval;
}
}
void c64_cpu_reset(void)
{
cpu.a = 0x00;
cpu.x = 0x00;
cpu.y = 0x00;
cpu.p = 0x00;
cpu.s = 0xff;
cpu.pc = 0xfffc;
}
void c64_cpu_reset_to(uint16_t new_pc, uint8_t new_a)
{
cpu.a = new_a;
cpu.x = 0x00;
cpu.y = 0x00;
cpu.p = 0x00;
cpu.s = 0xff;
cpu.pc = new_pc;
}
static void c64_cpu_step(void)
{
int c;
uint8_t opc = c64_getmem(cpu.pc++);
uint8_t cmd = mos6510_opcode_table[opc].type;
uint8_t addr = mos6510_opcode_table[opc].mode;
switch (cmd)
{
case MOS6510_TYPE_ADC:
wval = (uint16_t)cpu.a + getaddr(addr) + ((cpu.p & MOS6510_FLAG_C) ? 1 : 0);
setflags(MOS6510_FLAG_C, wval & 0x100);
cpu.a = (uint8_t)wval;
setflags(MOS6510_FLAG_Z, !cpu.a);
setflags(MOS6510_FLAG_N, cpu.a & 0x80);
setflags(MOS6510_FLAG_V, (!!(cpu.p & MOS6510_FLAG_C)) ^ (!!(cpu.p & MOS6510_FLAG_N)));
break;
case MOS6510_TYPE_AND:
bval = getaddr(addr);
cpu.a &= bval;
setflags(MOS6510_FLAG_Z, !cpu.a);
setflags(MOS6510_FLAG_N, cpu.a & 0x80);
break;
case MOS6510_TYPE_ASL:
wval = getaddr(addr);
wval <<= 1;
setaddr(addr,(uint8_t)wval);
setflags(MOS6510_FLAG_Z, !wval);
setflags(MOS6510_FLAG_N, wval & 0x80);
setflags(MOS6510_FLAG_C, wval & 0x100);
break;
case MOS6510_TYPE_BCC:
branch(!(cpu.p & MOS6510_FLAG_C));
break;
case MOS6510_TYPE_BCS:
branch(cpu.p & MOS6510_FLAG_C);
break;
case MOS6510_TYPE_BNE:
branch(!(cpu.p & MOS6510_FLAG_Z));
break;
case MOS6510_TYPE_BEQ:
branch(cpu.p & MOS6510_FLAG_Z);
break;
case MOS6510_TYPE_BPL:
branch(!(cpu.p & MOS6510_FLAG_N));
break;
case MOS6510_TYPE_BMI:
branch(cpu.p & MOS6510_FLAG_N);
break;
case MOS6510_TYPE_BVC:
branch(!(cpu.p & MOS6510_FLAG_V));
break;
case MOS6510_TYPE_BVS:
branch(cpu.p & MOS6510_FLAG_V);
break;
case MOS6510_TYPE_BIT:
bval = getaddr(addr);
setflags(MOS6510_FLAG_Z, !(cpu.a & bval));
setflags(MOS6510_FLAG_N, bval & 0x80);
setflags(MOS6510_FLAG_V, bval & 0x40);
break;
case MOS6510_TYPE_BRK:
cpu.pc = 0; /* Just quit the emulation */
break;
case MOS6510_TYPE_CLC:
setflags(MOS6510_FLAG_C, 0);
break;
case MOS6510_TYPE_CLD:
setflags(MOS6510_FLAG_D, 0);
break;
case MOS6510_TYPE_CLI:
setflags(MOS6510_FLAG_I, 0);
break;
case MOS6510_TYPE_CLV:
setflags(MOS6510_FLAG_V, 0);
break;
case MOS6510_TYPE_CMP:
bval = getaddr(addr);
wval = (uint16_t)cpu.a - bval;
setflags(MOS6510_FLAG_Z, !wval);
setflags(MOS6510_FLAG_N, wval & 0x80);
setflags(MOS6510_FLAG_C, cpu.a >= bval);
break;
case MOS6510_TYPE_CPX:
bval = getaddr(addr);
wval = (uint16_t)cpu.x-bval;
setflags(MOS6510_FLAG_Z, !wval);
setflags(MOS6510_FLAG_N, wval & 0x80);
setflags(MOS6510_FLAG_C, cpu.x >= bval);
break;
case MOS6510_TYPE_CPY:
bval = getaddr(addr);
wval = (uint16_t)cpu.y - bval;
setflags(MOS6510_FLAG_Z, !wval);
setflags(MOS6510_FLAG_N, wval & 0x80);
setflags(MOS6510_FLAG_C, cpu.y >= bval);
break;
case MOS6510_TYPE_DEC:
bval = getaddr(addr);
bval--;
setaddr(addr, bval);
setflags(MOS6510_FLAG_Z, !bval);
setflags(MOS6510_FLAG_N, bval & 0x80);
break;
case MOS6510_TYPE_DEX:
cpu.x--;
setflags(MOS6510_FLAG_Z, !cpu.x);
setflags(MOS6510_FLAG_N, cpu.x & 0x80);
break;
case MOS6510_TYPE_DEY:
cpu.y--;
setflags(MOS6510_FLAG_Z, !cpu.y);
setflags(MOS6510_FLAG_N, cpu.y & 0x80);
break;
case MOS6510_TYPE_EOR:
bval = getaddr(addr);
cpu.a ^= bval;
setflags(MOS6510_FLAG_Z, !cpu.a);
setflags(MOS6510_FLAG_N, cpu.a & 0x80);
break;
case MOS6510_TYPE_INC:
bval = getaddr(addr);
bval++;
setaddr(addr, bval);
setflags(MOS6510_FLAG_Z, !bval);
setflags(MOS6510_FLAG_N, bval & 0x80);
break;
case MOS6510_TYPE_INX:
cpu.x++;
setflags(MOS6510_FLAG_Z, !cpu.x);
setflags(MOS6510_FLAG_N, cpu.x & 0x80);
break;
case MOS6510_TYPE_INY:
cpu.y++;
setflags(MOS6510_FLAG_Z, !cpu.y);
setflags(MOS6510_FLAG_N, cpu.y & 0x80);
break;
case MOS6510_TYPE_JMP:
wval = c64_getmem(cpu.pc++);
wval |= 256 * c64_getmem(cpu.pc++);
switch (addr)
{
case MOS6510_MODE_ABS:
cpu.pc = wval;
break;
case MOS6510_MODE_IND:
cpu.pc = c64_getmem(wval);
cpu.pc |= 256 * c64_getmem(wval + 1);
break;
}
break;
case MOS6510_TYPE_JSR:
push((cpu.pc + 1) >> 8);
push((cpu.pc + 1));
wval = c64_getmem(cpu.pc++);
wval |= 256 * c64_getmem(cpu.pc++);
cpu.pc = wval;
break;
case MOS6510_TYPE_LDA:
cpu.a = getaddr(addr);
setflags(MOS6510_FLAG_Z, !cpu.a);
setflags(MOS6510_FLAG_N, cpu.a & 0x80);
break;
case MOS6510_TYPE_LDX:
cpu.x = getaddr(addr);
setflags(MOS6510_FLAG_Z, !cpu.x);
setflags(MOS6510_FLAG_N, cpu.x & 0x80);
break;
case MOS6510_TYPE_LDY:
cpu.y = getaddr(addr);
setflags(MOS6510_FLAG_Z, !cpu.y);
setflags(MOS6510_FLAG_N, cpu.y&0x80);
break;
case MOS6510_TYPE_LSR:
bval = getaddr(addr);
wval = (uint8_t)bval;
wval >>= 1;
setaddr(addr, (uint8_t)wval);
setflags(MOS6510_FLAG_Z, !wval);
setflags(MOS6510_FLAG_N, wval & 0x80);
setflags(MOS6510_FLAG_C, bval & 1);
break;
case MOS6510_TYPE_NOP:
break;
case MOS6510_TYPE_ORA:
bval = getaddr(addr);
cpu.a |= bval;
setflags(MOS6510_FLAG_Z, !cpu.a);
setflags(MOS6510_FLAG_N, cpu.a & 0x80);
break;
case MOS6510_TYPE_PHA:
push(cpu.a);
break;
case MOS6510_TYPE_PHP:
push(cpu.p);
break;
case MOS6510_TYPE_PLA:
cpu.a=pop();
setflags(MOS6510_FLAG_Z,!cpu.a);
setflags(MOS6510_FLAG_N,cpu.a&0x80);
break;
case MOS6510_TYPE_PLP:
cpu.p=pop();
break;
case MOS6510_TYPE_ROL:
bval = getaddr(addr);
c = !!(cpu.p & MOS6510_FLAG_C);
setflags(MOS6510_FLAG_C, bval & 0x80);
bval <<= 1;
bval |= c;
setaddr(addr, bval);
setflags(MOS6510_FLAG_N, bval & 0x80);
setflags(MOS6510_FLAG_Z,!bval);
break;
case MOS6510_TYPE_ROR:
bval = getaddr(addr);
c = !!(cpu.p&MOS6510_FLAG_C);
setflags(MOS6510_FLAG_C, bval & 1);
bval >>= 1;
bval |= 128 * c;
setaddr(addr, bval);
setflags(MOS6510_FLAG_N, bval & 0x80);
setflags(MOS6510_FLAG_Z, !bval);
break;
case MOS6510_TYPE_RTI:
/* Treat RTI like RTS */
case MOS6510_TYPE_RTS:
wval = pop();
wval |= pop() << 8;
cpu.pc = wval + 1;
break;
case MOS6510_TYPE_SBC:
bval = getaddr(addr) ^ 0xff;
wval=(uint16_t)cpu.a + bval + ((cpu.p & MOS6510_FLAG_C) ? 1 : 0);
setflags(MOS6510_FLAG_C, wval & 0x100);
cpu.a = (uint8_t)wval;
setflags(MOS6510_FLAG_Z, !cpu.a);
setflags(MOS6510_FLAG_N, cpu.a > 127);
setflags(MOS6510_FLAG_V, (!!(cpu.p & MOS6510_FLAG_C)) ^ (!!(cpu.p & MOS6510_FLAG_N)));
break;
case MOS6510_TYPE_SEC:
setflags(MOS6510_FLAG_C, 1);
break;
case MOS6510_TYPE_SED:
setflags(MOS6510_FLAG_D, 1);
break;
case MOS6510_TYPE_SEI:
setflags(MOS6510_FLAG_I, 1);
break;
case MOS6510_TYPE_STA:
putaddr(addr, cpu.a);
break;
case MOS6510_TYPE_STX:
putaddr(addr, cpu.x);
break;
case MOS6510_TYPE_STY:
putaddr(addr, cpu.y);
break;
case MOS6510_TYPE_TAX:
cpu.x = cpu.a;
setflags(MOS6510_FLAG_Z, !cpu.x);
setflags(MOS6510_FLAG_N, cpu.x & 0x80);
break;
case MOS6510_TYPE_TAY:
cpu.y = cpu.a;
setflags(MOS6510_FLAG_Z, !cpu.y);
setflags(MOS6510_FLAG_N, cpu.y & 0x80);
break;
case MOS6510_TYPE_TSX:
cpu.x = cpu.s;
setflags(MOS6510_FLAG_Z, !cpu.x);
setflags(MOS6510_FLAG_N, cpu.x & 0x80);
break;
case MOS6510_TYPE_TXA:
cpu.a = cpu.x;
setflags(MOS6510_FLAG_Z, !cpu.a);
setflags(MOS6510_FLAG_N, cpu.a & 0x80);
break;
case MOS6510_TYPE_TXS:
cpu.s = cpu.x;
break;
case MOS6510_TYPE_TYA:
cpu.a = cpu.y;
setflags(MOS6510_FLAG_Z, !cpu.a);
setflags(MOS6510_FLAG_N, cpu.a & 0x80);
break;
}
}
void c64_cpu_jsr(uint16_t new_pc, uint8_t new_a)
{
cpu.a = new_a;
cpu.x = 0x00;
cpu.y = 0x00;
cpu.p = 0x00;
cpu.s = 0xFF;
cpu.pc = new_pc;
push(0);
push(0);
while (cpu.pc > 1)
c64_cpu_step();
}
void c64_init()
{
c64_memset(0x0000, 0, 64*1024);
c64_cpu_reset();
}