-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathagnes.c
2583 lines (2266 loc) · 82.8 KB
/
agnes.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
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
SPDX-License-Identifier: MIT
agnes
https://http://github.com/kgabis/agnes
Copyright (c) 2022 Krzysztof Gabis
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#define AGNES_AMALGAMATED
#ifdef _MSC_VER
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#endif /* _CRT_SECURE_NO_WARNINGS */
#endif /* _MSC_VER */
#include "agnes.h"
//-----------------------------------------------------------------------------
// Headers
//-----------------------------------------------------------------------------
//FILE_START:common.h
#ifndef common_h
#define common_h
#include <stdbool.h>
#include <stdint.h>
#include <stddef.h>
#ifdef AGNES_AMALGAMATED
#define AGNES_INTERNAL static
#else
#define AGNES_INTERNAL
#endif
#define AGNES_GET_BIT(byte, bit_ix) (((byte) >> (bit_ix)) & 1)
#endif /* common_h */
//FILE_END
//FILE_START:agnes_types.h
#ifndef agnes_types_h
#define agnes_types_h
#ifndef AGNES_AMALGAMATED
#include "common.h"
#include "agnes.h"
#endif
/************************************ CPU ************************************/
typedef enum {
INTERRPUT_NONE = 0,
INTERRUPT_NMI = 1,
INTERRUPT_IRQ = 2
} cpu_interrupt_t;
typedef struct cpu {
struct agnes *agnes;
uint16_t pc;
uint8_t sp;
uint8_t acc;
uint8_t x;
uint8_t y;
uint8_t flag_carry;
uint8_t flag_zero;
uint8_t flag_dis_interrupt;
uint8_t flag_decimal;
uint8_t flag_overflow;
uint8_t flag_negative;
uint32_t stall;
uint64_t cycles;
cpu_interrupt_t interrupt;
} cpu_t;
/************************************ PPU ************************************/
typedef struct {
uint8_t y_pos;
uint8_t tile_num;
uint8_t attrs;
uint8_t x_pos;
} sprite_t;
typedef struct ppu {
struct agnes *agnes;
uint8_t nametables[4 * 1024];
uint8_t palette[32];
uint8_t screen_buffer[AGNES_SCREEN_HEIGHT * AGNES_SCREEN_WIDTH];
int scanline;
int dot;
uint8_t ppudata_buffer;
uint8_t last_reg_write;
struct {
uint16_t v;
uint16_t t;
uint8_t x;
uint8_t w;
} regs;
struct {
bool show_leftmost_bg;
bool show_leftmost_sprites;
bool show_background;
bool show_sprites;
} masks;
uint8_t nt;
uint8_t at;
uint8_t at_latch;
uint16_t at_shift;
uint8_t bg_hi;
uint8_t bg_lo;
uint16_t bg_hi_shift;
uint16_t bg_lo_shift;
struct {
uint16_t addr_increment;
uint16_t sprite_table_addr;
uint16_t bg_table_addr;
bool use_8x16_sprites;
bool nmi_enabled;
} ctrl;
struct {
bool in_vblank;
bool sprite_overflow;
bool sprite_zero_hit;
} status;
bool is_odd_frame;
uint8_t oam_address;
uint8_t oam_data[256];
sprite_t sprites[8];
int sprite_ixs[8];
int sprite_ixs_count;
} ppu_t;
/********************************** MAPPERS **********************************/
typedef enum {
MIRRORING_MODE_NONE,
MIRRORING_MODE_SINGLE_LOWER,
MIRRORING_MODE_SINGLE_UPPER,
MIRRORING_MODE_HORIZONTAL,
MIRRORING_MODE_VERTICAL,
MIRRORING_MODE_FOUR_SCREEN
} mirroring_mode_t;
typedef struct mapper0 {
struct agnes *agnes;
unsigned prg_bank_offsets[2];
bool use_chr_ram;
uint8_t chr_ram[8 * 1024];
} mapper0_t;
typedef struct mapper1 {
struct agnes *agnes;
uint8_t shift;
int shift_count;
uint8_t control;
int prg_mode;
int chr_mode;
int chr_banks[2];
int prg_bank;
unsigned chr_bank_offsets[2];
unsigned prg_bank_offsets[2];
bool use_chr_ram;
uint8_t chr_ram[8 * 1024];
uint8_t prg_ram[8 * 1024];
} mapper1_t;
typedef struct mapper2 {
struct agnes *agnes;
unsigned prg_bank_offsets[2];
uint8_t chr_ram[8 * 1024];
} mapper2_t;
typedef struct mapper4 {
struct agnes *agnes;
unsigned prg_mode;
unsigned chr_mode;
bool irq_enabled;
int reg_ix;
uint8_t regs[8];
uint8_t counter;
uint8_t counter_reload;
unsigned chr_bank_offsets[8];
unsigned prg_bank_offsets[4];
uint8_t prg_ram[8 * 1024];
bool use_chr_ram;
uint8_t chr_ram[8 * 1024];
} mapper4_t;
/********************************* GAMEPACK **********************************/
typedef struct {
const uint8_t *data;
unsigned prg_rom_offset;
unsigned chr_rom_offset;
int prg_rom_banks_count;
int chr_rom_banks_count;
bool has_prg_ram;
unsigned char mapper;
} gamepack_t;
/******************************** CONTROLLER *********************************/
typedef struct controller {
uint8_t state;
uint8_t shift;
} controller_t;
/*********************************** AGNES ***********************************/
typedef struct agnes {
cpu_t cpu;
ppu_t ppu;
uint8_t ram[2 * 1024];
gamepack_t gamepack;
controller_t controllers[2];
bool controllers_latch;
union {
mapper0_t m0;
mapper1_t m1;
mapper2_t m2;
mapper4_t m4;
} mapper;
mirroring_mode_t mirroring_mode;
} agnes_t;
#endif /* agnes_types_h */
//FILE_END
//FILE_START:cpu.h
#ifndef cpu_h
#define cpu_h
#ifndef AGNES_AMALGAMATED
#include "common.h"
#endif
typedef struct agnes agnes_t;
typedef struct cpu cpu_t;
AGNES_INTERNAL void cpu_init(cpu_t *cpu, agnes_t *agnes);
AGNES_INTERNAL int cpu_tick(cpu_t *cpu);
AGNES_INTERNAL void cpu_update_zn_flags(cpu_t *cpu, uint8_t val);
AGNES_INTERNAL void cpu_stack_push8(cpu_t *cpu, uint8_t val);
AGNES_INTERNAL void cpu_stack_push16(cpu_t *cpu, uint16_t val);
AGNES_INTERNAL uint8_t cpu_stack_pop8(cpu_t *cpu);
AGNES_INTERNAL uint16_t cpu_stack_pop16(cpu_t *cpu);
AGNES_INTERNAL uint8_t cpu_get_flags(const cpu_t *cpu);
AGNES_INTERNAL void cpu_restore_flags(cpu_t *cpu, uint8_t flags);
AGNES_INTERNAL void cpu_set_dma_stall(cpu_t *cpu);
AGNES_INTERNAL void cpu_trigger_nmi(cpu_t *cpu);
AGNES_INTERNAL void cpu_trigger_irq(cpu_t *cpu);
AGNES_INTERNAL void cpu_write8(cpu_t *cpu, uint16_t addr, uint8_t val);
AGNES_INTERNAL uint8_t cpu_read8(cpu_t *cpu, uint16_t addr);
AGNES_INTERNAL uint16_t cpu_read16(cpu_t *cpu, uint16_t addr);
#endif /* cpu_h */
//FILE_END
//FILE_START:ppu.h
#ifndef ppu_h
#define ppu_h
#ifndef AGNES_AMALGAMATED
#include "common.h"
#endif
typedef struct agnes agnes_t;
typedef struct ppu ppu_t;
AGNES_INTERNAL void ppu_init(ppu_t *ppu, agnes_t *agnes);
AGNES_INTERNAL void ppu_tick(ppu_t *ppu, bool *out_new_frame);
AGNES_INTERNAL uint8_t ppu_read_register(ppu_t *ppu, uint16_t reg);
AGNES_INTERNAL void ppu_write_register(ppu_t *ppu, uint16_t addr, uint8_t val);
#endif /* ppu_h */
//FILE_END
//FILE_START:instructions.h
#ifndef opcodes_h
#define opcodes_h
#ifndef AGNES_AMALGAMATED
#include "common.h"
#endif
typedef enum {
ADDR_MODE_NONE = 0,
ADDR_MODE_ABSOLUTE,
ADDR_MODE_ABSOLUTE_X,
ADDR_MODE_ABSOLUTE_Y,
ADDR_MODE_ACCUMULATOR,
ADDR_MODE_IMMEDIATE,
ADDR_MODE_IMPLIED,
ADDR_MODE_IMPLIED_BRK,
ADDR_MODE_INDIRECT,
ADDR_MODE_INDIRECT_X,
ADDR_MODE_INDIRECT_Y,
ADDR_MODE_RELATIVE,
ADDR_MODE_ZERO_PAGE,
ADDR_MODE_ZERO_PAGE_X,
ADDR_MODE_ZERO_PAGE_Y
} addr_mode_t;
typedef struct cpu cpu_t;
typedef int (*instruction_op_fn)(cpu_t *cpu, uint16_t addr, addr_mode_t mode);
typedef struct {
const char *name;
uint8_t opcode;
uint8_t cycles;
bool page_cross_cycle;
addr_mode_t mode;
instruction_op_fn operation;
} instruction_t;
AGNES_INTERNAL instruction_t* instruction_get(uint8_t opcode);
AGNES_INTERNAL uint8_t instruction_get_size(addr_mode_t mode);
#endif /* opcodes_h */
//FILE_END
//FILE_START:mapper.h
#ifndef mapper_h
#define mapper_h
#ifndef AGNES_AMALGAMATED
#include "common.h"
#endif
typedef struct agnes agnes_t;
AGNES_INTERNAL bool mapper_init(agnes_t *agnes);
AGNES_INTERNAL uint8_t mapper_read(agnes_t *agnes, uint16_t addr);
AGNES_INTERNAL void mapper_write(agnes_t *agnes, uint16_t addr, uint8_t val);
AGNES_INTERNAL void mapper_pa12_rising_edge(agnes_t *agnes);
#endif /* mapper_h */
//FILE_END
//FILE_START:mapper0.h
#ifndef mapper0_h
#define mapper0_h
#ifndef AGNES_AMALGAMATED
#include "common.h"
#endif
typedef struct mapper0 mapper0_t;
typedef struct agnes agnes_t;
AGNES_INTERNAL void mapper0_init(mapper0_t *mapper, agnes_t *agnes);
AGNES_INTERNAL uint8_t mapper0_read(mapper0_t *mapper, uint16_t addr);
AGNES_INTERNAL void mapper0_write(mapper0_t *mapper, uint16_t addr, uint8_t val);
#endif /* mapper0_h */
//FILE_END
//FILE_START:mapper1.h
#ifndef mapper1_h
#define mapper1_h
#ifndef AGNES_AMALGAMATED
#include "common.h"
#endif
typedef struct mapper1 mapper1_t;
typedef struct agnes agnes_t;
AGNES_INTERNAL void mapper1_init(mapper1_t *mapper, agnes_t *agnes);
AGNES_INTERNAL uint8_t mapper1_read(mapper1_t *mapper, uint16_t addr);
AGNES_INTERNAL void mapper1_write(mapper1_t *mapper, uint16_t addr, uint8_t val);
#endif /* mapper1_h */
//FILE_END
//FILE_START:mapper2.h
#ifndef mapper2_h
#define mapper2_h
#ifndef AGNES_AMALGAMATED
#include "common.h"
#endif
typedef struct mapper2 mapper2_t;
typedef struct agnes agnes_t;
AGNES_INTERNAL void mapper2_init(mapper2_t *mapper, agnes_t *agnes);
AGNES_INTERNAL uint8_t mapper2_read(mapper2_t *mapper, uint16_t addr);
AGNES_INTERNAL void mapper2_write(mapper2_t *mapper, uint16_t addr, uint8_t val);
#endif /* mapper2_h */
//FILE_END
//FILE_START:mapper4.h
#ifndef mapper4_h
#define mapper4_h
#ifndef AGNES_AMALGAMATED
#include "common.h"
#endif
typedef struct mapper4 mapper4_t;
typedef struct agnes agnes_t;
AGNES_INTERNAL void mapper4_init(mapper4_t *mapper, agnes_t *agnes);
AGNES_INTERNAL uint8_t mapper4_read(mapper4_t *mapper, uint16_t addr);
AGNES_INTERNAL void mapper4_write(mapper4_t *mapper, uint16_t addr, uint8_t val);
AGNES_INTERNAL void mapper4_pa12_rising_edge(mapper4_t *mapper);
#endif /* mapper4_h */
//FILE_END
//-----------------------------------------------------------------------------
// C files
//-----------------------------------------------------------------------------
//FILE_START:agnes.c
#include <stdlib.h>
#include <string.h>
#ifndef AGNES_AMALGAMATED
#include "agnes.h"
#include "common.h"
#include "agnes_types.h"
#include "cpu.h"
#include "ppu.h"
#include "mapper.h"
#endif
#define AGNES_IMPL_VERSION_MAJOR 0
#define AGNES_IMPL_VERSION_MINOR 2
#define AGNES_IMPL_VERSION_PATCH 0
#if (AGNES_VERSION_MAJOR != AGNES_IMPL_VERSION_MAJOR)\
|| (AGNES_VERSION_MINOR != AGNES_IMPL_VERSION_MINOR)\
|| (AGNES_VERSION_PATCH != AGNES_IMPL_VERSION_PATCH)
#error "Version mismatch"
#endif
typedef struct {
uint8_t magic[4];
uint8_t prg_rom_banks_count;
uint8_t chr_rom_banks_count;
uint8_t flags_6;
uint8_t flags_7;
uint8_t prg_ram_banks_count;
uint8_t flags_9;
uint8_t flags_10;
uint8_t zeros[5];
} ines_header_t;
typedef struct agnes_state {
agnes_t agnes;
} agnes_state_t;
static uint8_t get_input_byte(const agnes_input_t* input);
static agnes_color_t g_colors[64] = {
{0x7c, 0x7c, 0x7c, 0xff}, {0x00, 0x00, 0xfc, 0xff}, {0x00, 0x00, 0xbc, 0xff}, {0x44, 0x28, 0xbc, 0xff},
{0x94, 0x00, 0x84, 0xff}, {0xa8, 0x00, 0x20, 0xff}, {0xa8, 0x10, 0x00, 0xff}, {0x88, 0x14, 0x00, 0xff},
{0x50, 0x30, 0x00, 0xff}, {0x00, 0x78, 0x00, 0xff}, {0x00, 0x68, 0x00, 0xff}, {0x00, 0x58, 0x00, 0xff},
{0x00, 0x40, 0x58, 0xff}, {0x00, 0x00, 0x00, 0xff}, {0x00, 0x00, 0x00, 0xff}, {0x00, 0x00, 0x00, 0xff},
{0xbc, 0xbc, 0xbc, 0xff}, {0x00, 0x78, 0xf8, 0xff}, {0x00, 0x58, 0xf8, 0xff}, {0x68, 0x44, 0xfc, 0xff},
{0xd8, 0x00, 0xcc, 0xff}, {0xe4, 0x00, 0x58, 0xff}, {0xf8, 0x38, 0x00, 0xff}, {0xe4, 0x5c, 0x10, 0xff},
{0xac, 0x7c, 0x00, 0xff}, {0x00, 0xb8, 0x00, 0xff}, {0x00, 0xa8, 0x00, 0xff}, {0x00, 0xa8, 0x44, 0xff},
{0x00, 0x88, 0x88, 0xff}, {0x00, 0x00, 0x00, 0xff}, {0x00, 0x00, 0x00, 0xff}, {0x00, 0x00, 0x00, 0xff},
{0xf8, 0xf8, 0xf8, 0xff}, {0x3c, 0xbc, 0xfc, 0xff}, {0x68, 0x88, 0xfc, 0xff}, {0x98, 0x78, 0xf8, 0xff},
{0xf8, 0x78, 0xf8, 0xff}, {0xf8, 0x58, 0x98, 0xff}, {0xf8, 0x78, 0x58, 0xff}, {0xfc, 0xa0, 0x44, 0xff},
{0xf8, 0xb8, 0x00, 0xff}, {0xb8, 0xf8, 0x18, 0xff}, {0x58, 0xd8, 0x54, 0xff}, {0x58, 0xf8, 0x98, 0xff},
{0x00, 0xe8, 0xd8, 0xff}, {0x78, 0x78, 0x78, 0xff}, {0x00, 0x00, 0x00, 0xff}, {0x00, 0x00, 0x00, 0xff},
{0xfc, 0xfc, 0xfc, 0xff}, {0xa4, 0xe4, 0xfc, 0xff}, {0xb8, 0xb8, 0xf8, 0xff}, {0xd8, 0xb8, 0xf8, 0xff},
{0xf8, 0xb8, 0xf8, 0xff}, {0xf8, 0xa4, 0xc0, 0xff}, {0xf0, 0xd0, 0xb0, 0xff}, {0xfc, 0xe0, 0xa8, 0xff},
{0xf8, 0xd8, 0x78, 0xff}, {0xd8, 0xf8, 0x78, 0xff}, {0xb8, 0xf8, 0xb8, 0xff}, {0xb8, 0xf8, 0xd8, 0xff},
{0x00, 0xfc, 0xfc, 0xff}, {0xf8, 0xd8, 0xf8, 0xff}, {0x00, 0x00, 0x00, 0xff}, {0x00, 0x00, 0x00, 0xff},
};
agnes_t* agnes_make(void) {
agnes_t *agnes = (agnes_t*)malloc(sizeof(*agnes));
if (!agnes) {
return NULL;
}
memset(agnes, 0, sizeof(*agnes));
memset(agnes->ram, 0xff, sizeof(agnes->ram));
return agnes;
}
bool agnes_load_ines_data(agnes_t *agnes, void *data, size_t data_size) {
if (data_size < sizeof(ines_header_t)) {
return false;
}
ines_header_t *header = (ines_header_t*)data;
if (strncmp((char*)header->magic, "NES\x1a", 4) != 0) {
return false;
}
unsigned prg_rom_offset = sizeof(ines_header_t);
bool has_trainer = AGNES_GET_BIT(header->flags_6, 2);
if (has_trainer) {
prg_rom_offset += 512;
}
agnes->gamepack.chr_rom_banks_count = header->chr_rom_banks_count;
agnes->gamepack.prg_rom_banks_count = header->prg_rom_banks_count;
if (AGNES_GET_BIT(header->flags_6, 3)) {
agnes->mirroring_mode = MIRRORING_MODE_FOUR_SCREEN;
} else {
agnes->mirroring_mode = AGNES_GET_BIT(header->flags_6, 0) ? MIRRORING_MODE_VERTICAL : MIRRORING_MODE_HORIZONTAL;
}
agnes->gamepack.mapper = ((header->flags_6 & 0xf0) >> 4) | (header->flags_7 & 0xf0);
unsigned prg_rom_size = header->prg_rom_banks_count * (16 * 1024);
unsigned chr_rom_size = header->chr_rom_banks_count * (8 * 1024);
unsigned chr_rom_offset = prg_rom_offset + prg_rom_size;
if ((chr_rom_offset + chr_rom_size) > data_size) {
return false;
}
agnes->gamepack.data = (const uint8_t *)data;
agnes->gamepack.prg_rom_offset = prg_rom_offset;
agnes->gamepack.chr_rom_offset = chr_rom_offset;
bool ok = mapper_init(agnes);
if (!ok) {
return false;
}
cpu_init(&agnes->cpu, agnes);
ppu_init(&agnes->ppu, agnes);
return true;
}
void agnes_set_input(agnes_t *agn, const agnes_input_t *input_1, const agnes_input_t *input_2) {
if (input_1 != NULL) {
agn->controllers[0].state = get_input_byte(input_1);
}
if (input_2 != NULL) {
agn->controllers[1].state = get_input_byte(input_2);
}
}
size_t agnes_state_size() {
return sizeof(agnes_state_t);
}
void agnes_dump_state(const agnes_t *agnes, agnes_state_t *out_res) {
memmove(out_res, agnes, sizeof(agnes_t));
out_res->agnes.gamepack.data = NULL;
out_res->agnes.cpu.agnes = NULL;
out_res->agnes.ppu.agnes = NULL;
switch (out_res->agnes.gamepack.mapper) {
case 0: out_res->agnes.mapper.m0.agnes = NULL; break;
case 1: out_res->agnes.mapper.m1.agnes = NULL; break;
case 2: out_res->agnes.mapper.m2.agnes = NULL; break;
case 4: out_res->agnes.mapper.m4.agnes = NULL; break;
}
}
bool agnes_restore_state(agnes_t *agnes, const agnes_state_t *state) {
const uint8_t *gamepack_data = agnes->gamepack.data;
memmove(agnes, state, sizeof(agnes_t));
agnes->gamepack.data = gamepack_data;
agnes->cpu.agnes = agnes;
agnes->ppu.agnes = agnes;
switch (agnes->gamepack.mapper) {
case 0: agnes->mapper.m0.agnes = agnes; break;
case 1: agnes->mapper.m1.agnes = agnes; break;
case 2: agnes->mapper.m2.agnes = agnes; break;
case 4: agnes->mapper.m4.agnes = agnes; break;
}
return true;
}
bool agnes_tick(agnes_t *agnes, bool *out_new_frame) {
int cpu_cycles = cpu_tick(&agnes->cpu);
if (cpu_cycles == 0) {
return false;
}
int ppu_cycles = cpu_cycles * 3;
for (int i = 0; i < ppu_cycles; i++) {
ppu_tick(&agnes->ppu, out_new_frame);
}
return true;
}
bool agnes_next_frame(agnes_t *agnes) {
while (true) {
bool new_frame = false;
bool ok = agnes_tick(agnes, &new_frame);
if (!ok) {
return false;
}
if (new_frame) {
break;
}
}
return true;
}
agnes_color_t agnes_get_screen_pixel(const agnes_t *agnes, int x, int y) {
int ix = (y * AGNES_SCREEN_WIDTH) + x;
uint8_t color_ix = agnes->ppu.screen_buffer[ix];
return g_colors[color_ix & 0x3f];
}
void agnes_destroy(agnes_t *agnes) {
free(agnes);
}
static uint8_t get_input_byte(const agnes_input_t* input) {
uint8_t res = 0;
res |= input->a << 0;
res |= input->b << 1;
res |= input->select << 2;
res |= input->start << 3;
res |= input->up << 4;
res |= input->down << 5;
res |= input->left << 6;
res |= input->right << 7;
return res;
}
//FILE_END
//FILE_START:cpu.c
#include <string.h>
#ifndef AGNES_AMALGAMATED
#include "cpu.h"
#include "ppu.h"
#include "agnes_types.h"
#include "instructions.h"
#include "mapper.h"
#endif
static uint16_t cpu_read16_indirect_bug(cpu_t *cpu, uint16_t addr);
static uint16_t get_instruction_operand(cpu_t *cpu, addr_mode_t mode, bool *out_pages_differ);
static int handle_interrupt(cpu_t *cpu);
static bool check_pages_differ(uint16_t a, uint16_t b);
void cpu_init(cpu_t *cpu, agnes_t *agnes) {
memset(cpu, 0, sizeof(cpu_t));
cpu->agnes = agnes;
cpu->pc = cpu_read16(cpu, 0xfffc); // RESET
cpu->sp = 0xfd;
cpu_restore_flags(cpu, 0x24);
}
int cpu_tick(cpu_t *cpu) {
if (cpu->stall > 0) {
cpu->stall--;
return 1;
}
int cycles = 0;
if (cpu->interrupt != INTERRPUT_NONE) {
cycles += handle_interrupt(cpu);
}
uint8_t opcode = cpu_read8(cpu, cpu->pc);
instruction_t *ins = instruction_get(opcode);
if (ins->operation == NULL) {
return 0;
}
uint8_t ins_size = instruction_get_size(ins->mode);
bool page_crossed = false;
uint16_t addr = get_instruction_operand(cpu, ins->mode, &page_crossed);
cpu->pc += ins_size;
cycles += ins->cycles;
cycles += ins->operation(cpu, addr, ins->mode);
if (page_crossed && ins->page_cross_cycle) {
cycles += 1;
}
cpu->cycles += cycles;
return cycles;
}
void cpu_update_zn_flags(cpu_t *cpu, uint8_t val) {
cpu->flag_zero = val == 0;
cpu->flag_negative = AGNES_GET_BIT(val, 7);
}
void cpu_stack_push8(cpu_t *cpu, uint8_t val) {
uint16_t addr = 0x0100 + (uint16_t)(cpu->sp);
cpu_write8(cpu, addr, val);
cpu->sp--;
}
void cpu_stack_push16(cpu_t *cpu, uint16_t val) {
cpu_stack_push8(cpu, val >> 8);
cpu_stack_push8(cpu, val);
}
uint8_t cpu_stack_pop8(cpu_t *cpu) {
cpu->sp++;
uint16_t addr = 0x0100 + (uint16_t)(cpu->sp);
uint8_t res = cpu_read8(cpu, addr);
return res;
}
uint16_t cpu_stack_pop16(cpu_t *cpu) {
uint16_t lo = cpu_stack_pop8(cpu);
uint16_t hi = cpu_stack_pop8(cpu);
uint16_t res = (hi << 8) | lo;
return res;
}
uint8_t cpu_get_flags(const cpu_t *cpu) {
uint8_t res = 0;
res |= cpu->flag_carry << 0;
res |= cpu->flag_zero << 1;
res |= cpu->flag_dis_interrupt << 2;
res |= cpu->flag_decimal << 3;
res |= cpu->flag_overflow << 6;
res |= cpu->flag_negative << 7;
return res;
}
void cpu_restore_flags(cpu_t *cpu, uint8_t flags) {
cpu->flag_carry = AGNES_GET_BIT(flags, 0);
cpu->flag_zero = AGNES_GET_BIT(flags, 1);
cpu->flag_dis_interrupt = AGNES_GET_BIT(flags, 2);
cpu->flag_decimal = AGNES_GET_BIT(flags, 3);
cpu->flag_overflow = AGNES_GET_BIT(flags, 6);
cpu->flag_negative = AGNES_GET_BIT(flags, 7);
}
void cpu_trigger_nmi(cpu_t *cpu) {
cpu->interrupt = INTERRUPT_NMI;
}
void cpu_trigger_irq(cpu_t *cpu) {
if (!cpu->flag_dis_interrupt) {
cpu->interrupt = INTERRUPT_IRQ;
}
}
void cpu_set_dma_stall(cpu_t *cpu) {
cpu->stall = (cpu->cycles & 0x1) ? 514 : 513;
}
void cpu_write8(cpu_t *cpu, uint16_t addr, uint8_t val) {
agnes_t *agnes = cpu->agnes;
if (addr < 0x2000) {
agnes->ram[addr & 0x7ff] = val;
} else if (addr < 0x4000) {
ppu_write_register(&agnes->ppu, 0x2000 | (addr & 0x7), val);
} else if (addr == 0x4014) {
ppu_write_register(&agnes->ppu, 0x4014, val);
} else if (addr == 0x4016) {
agnes->controllers_latch = val & 0x1;
if (agnes->controllers_latch) {
agnes->controllers[0].shift = agnes->controllers[0].state;
agnes->controllers[1].shift = agnes->controllers[1].state;
}
} else if (addr < 0x4018) { // apu and io
} else if (addr < 0x4020) { // disabled
} else {
mapper_write(agnes, addr, val);
}
}
uint8_t cpu_read8(cpu_t *cpu, uint16_t addr) {
agnes_t *agnes = cpu->agnes;
uint8_t res = 0;
if (addr >= 0x4020) { // moved to top because it's the most common case
res = mapper_read(agnes, addr);
} else if (addr < 0x2000) {
res = agnes->ram[addr & 0x7ff];
} else if (addr < 0x4000) {
res = ppu_read_register(&agnes->ppu, 0x2000 | (addr & 0x7));
} else if (addr < 0x4016) {
// apu
} else if (addr < 0x4018) {
int controller = addr & 0x1; // 0: 0x4016, 1: 0x4017
if (agnes->controllers_latch) {
agnes->controllers[controller].shift = agnes->controllers[controller].state;
}
res = agnes->controllers[controller].shift & 0x1;
agnes->controllers[controller].shift >>= 1;
}
return res;
}
uint16_t cpu_read16(cpu_t *cpu, uint16_t addr) {
uint8_t lo = cpu_read8(cpu, addr);
uint8_t hi = cpu_read8(cpu, addr + 1);
return (hi << 8) | lo;
}
static uint16_t cpu_read16_indirect_bug(cpu_t *cpu, uint16_t addr) {
uint8_t lo = cpu_read8(cpu, addr);
uint8_t hi = cpu_read8(cpu, (addr & 0xff00) | ((addr + 1) & 0x00ff));
return (hi << 8) | lo;
}
static uint16_t get_instruction_operand(cpu_t *cpu, addr_mode_t mode, bool *out_pages_differ) {
*out_pages_differ = false;
switch (mode) {
case ADDR_MODE_ABSOLUTE: {
return cpu_read16(cpu, cpu->pc + 1);
}
case ADDR_MODE_ABSOLUTE_X: {
uint16_t addr = cpu_read16(cpu, cpu->pc + 1);
uint16_t res = addr + cpu->x;
*out_pages_differ = check_pages_differ(addr, res);
return res;
}
case ADDR_MODE_ABSOLUTE_Y: {
uint16_t addr = cpu_read16(cpu, cpu->pc + 1);
uint16_t res = addr + cpu->y;
*out_pages_differ = check_pages_differ(addr, res);
return res;
}
case ADDR_MODE_IMMEDIATE: {
return cpu->pc + 1;
}
case ADDR_MODE_INDIRECT: {
uint16_t addr = cpu_read16(cpu, cpu->pc + 1);
return cpu_read16_indirect_bug(cpu, addr);
}
case ADDR_MODE_INDIRECT_X: {
uint8_t addr = cpu_read8(cpu, (cpu->pc + 1));
return cpu_read16_indirect_bug(cpu, (addr + cpu->x) & 0xff);
}
case ADDR_MODE_INDIRECT_Y: {
uint8_t arg = cpu_read8(cpu, cpu->pc + 1);
uint16_t addr2 = cpu_read16_indirect_bug(cpu, arg);
uint16_t res = addr2 + cpu->y;
*out_pages_differ = check_pages_differ(addr2, res);
return res;
}
case ADDR_MODE_ZERO_PAGE: {
return cpu_read8(cpu, cpu->pc + 1);
}
case ADDR_MODE_ZERO_PAGE_X: {
return (cpu_read8(cpu, cpu->pc + 1) + cpu->x) & 0xff;
}
case ADDR_MODE_ZERO_PAGE_Y: {
return (cpu_read8(cpu, cpu->pc + 1) + cpu->y) & 0xff;
}
case ADDR_MODE_RELATIVE: {
uint8_t addr = cpu_read8(cpu, cpu->pc + 1);
if (addr < 0x80) {
return cpu->pc + addr + 2;
} else {
return cpu->pc + addr + 2 - 0x100;
}
}
default: {
return 0;
}
}
}
static int handle_interrupt(cpu_t *cpu) {
uint16_t addr = 0;
if (cpu->interrupt == INTERRUPT_NMI) {
addr = 0xfffa;
} else if (cpu->interrupt == INTERRUPT_IRQ) {
addr = 0xfffe;
} else {
return 0;
}
cpu->interrupt = INTERRPUT_NONE;
cpu_stack_push16(cpu, cpu->pc);
uint8_t flags = cpu_get_flags(cpu);
cpu_stack_push8(cpu, flags | 0x20);
cpu->pc = cpu_read16(cpu, addr);
cpu->flag_dis_interrupt = true;
return 7;
}
static bool check_pages_differ(uint16_t a, uint16_t b) {
return (0xff00 & a) != (0xff00 & b);
}
//FILE_END
//FILE_START:ppu.c
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#ifndef AGNES_AMALGAMATED
#include "ppu.h"
#include "agnes_types.h"
#include "cpu.h"
#include "mapper.h"
#endif
static void scanline_visible_pre(ppu_t *ppu, bool *out_new_frame);
static void inc_hori_v(ppu_t *ppu);
static void inc_vert_v(ppu_t *ppu);
static void emit_pixel(ppu_t *ppu);
static uint16_t get_bg_color_addr(ppu_t *ppu);
static uint16_t get_sprite_color_addr(ppu_t *ppu, int *out_sprite_ix, bool *out_behind_bg);
static void eval_sprites(ppu_t *ppu);
static void set_pixel_color_ix(ppu_t *ppu, int x, int y, uint8_t color_ix);
static uint8_t ppu_read8(ppu_t *ppu, uint16_t addr);
static void ppu_write8(ppu_t *ppu, uint16_t addr, uint8_t val);
static uint16_t mirror_address(ppu_t *ppu, uint16_t addr);
static unsigned g_palette_addr_map[32] = {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
0x00, 0x11, 0x12, 0x13, 0x04, 0x15, 0x16, 0x17, 0x08, 0x19, 0x1a, 0x1b, 0x0c, 0x1d, 0x1e, 0x1f,
};
void ppu_init(ppu_t *ppu, agnes_t *agnes) {
memset(ppu, 0, sizeof(ppu_t));
ppu->agnes = agnes;
ppu_write_register(ppu, 0x2000, 0);
ppu_write_register(ppu, 0x2001, 0);
}
void ppu_tick(ppu_t *ppu, bool *out_new_frame) {
bool rendering_enabled = ppu->masks.show_background || ppu->masks.show_sprites;
// https://wiki.nesdev.com/w/index.php/PPU_frame_timing#Even.2FOdd_Frames
if (rendering_enabled && ppu->is_odd_frame && ppu->dot == 339 && ppu->scanline == 261) {
ppu->dot = 0;
ppu->scanline = 0;
ppu->is_odd_frame = !ppu->is_odd_frame;
} else {
ppu->dot++;
if (ppu->dot > 340){
ppu->dot = 0;
ppu->scanline++;
}
if (ppu->scanline > 261) {
ppu->scanline = 0;
ppu->is_odd_frame = !ppu->is_odd_frame;
}
}
if (ppu->dot == 0) {
return;
}
bool scanline_visible = ppu->scanline >= 0 && ppu->scanline < 240;
bool scanline_pre = ppu->scanline == 261;
bool scanline_post = ppu->scanline == 241;
if (rendering_enabled && (scanline_visible || scanline_pre)) {
scanline_visible_pre(ppu, out_new_frame);
}
if (ppu->dot == 1) {
if (scanline_pre) {
ppu->status.sprite_overflow = false;
ppu->status.sprite_zero_hit = false;
ppu->status.in_vblank = false;
} else if (scanline_post) {
ppu->status.in_vblank = true;
*out_new_frame = true;