~groovestomp/gsnes

gsnes/mapper000.c -rw-r--r-- 2.6 KiB
9de07668 — GrooveStomp Foreground rendering works 1 year, 4 months ago
                                                                                
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
/******************************************************************************
  GrooveStomp's NES Emulator
  Copyright (c) 2019 Aaron Oman (GrooveStomp)

  File: mapper000.c
  Created: 2019-11-04
  Updated: 2019-12-02
  Author: Aaron Oman
  Notice: GNU AGPLv3 License

  Based off of: One Lone Coder NES Emulator Copyright (C) 2019 Javidx9
  This program comes with ABSOLUTELY NO WARRANTY.
  This is free software, and you are welcome to redistribute it under certain
  conditions; See LICENSE for details.
 ******************************************************************************/
//! \file mapper000.c
#include <stdlib.h> // malloc, free

#include "mapper000.h"

struct mapper000 {
        uint8_t prgBanks;
        uint8_t chrBanks;
};

void *Mapper000_Init(uint8_t prgBanks, uint8_t chrBanks) {
        struct mapper000 *mapper = (struct mapper000 *)calloc(1, sizeof(struct mapper000));
        if (NULL == mapper) {
                return NULL;
        }

        mapper->prgBanks = prgBanks;
        mapper->chrBanks = chrBanks;

        return (void *)mapper;
}

void Mapper000_Deinit(void *interface) {
        if (NULL == interface)
                return;

        free((struct mapper000 *)interface);
}

void Mapper000_Reset(void *mapper) {
}

uint16_t Mask(struct mapper000 *mapper) {
        if (mapper->prgBanks > 1) {
                return 0x7FFF;
        } else {
                return 0x3FFF;
        }
}

bool Mapper000_MapCpuRead(void *interface, uint16_t addr, uint32_t *mappedAddr) {
        struct mapper000 *mapper = (struct mapper000 *)interface;

        if (addr >= 0x8000 && addr <= 0xFFFF) {
                *mappedAddr = addr & Mask(mapper);
                return true;
        }

        return false;
}

bool Mapper000_MapCpuWrite(void *interface, uint16_t addr, uint32_t *mappedAddr) {
        struct mapper000 *mapper = (struct mapper000 *)interface;

        if (addr >= 0x8000 && addr <= 0xFFFF) {
                *mappedAddr = addr & Mask(mapper);
                return true;
        }

        return false;
}

bool Mapper000_MapPpuRead(void *interface, uint16_t addr, uint32_t *mappedAddr) {
        if (addr >= 0x0000 && addr <= 0x1FFF) {
                *mappedAddr = addr;
                return true;
        }

        return false;
}

bool Mapper000_MapPpuWrite(void *interface, uint16_t addr, uint32_t *mappedAddr) {
        struct mapper000 *mapper = (struct mapper000 *)interface;

        if (addr >= 0x0000 && addr <= 0x1FFF) {
                if (0 == mapper->chrBanks) {
                        // Treat as RAM.
                        *mappedAddr = addr;
                        return true;
                }
        }

        return false;
}