~groovestomp/gsnes

3f511defcdeda4907e8818f3d630f517f4486e61 — GrooveStomp 1 year, 5 months ago b0f087c
Found some issues in cartridge implementation - not done yet.
3 files changed, 49 insertions(+), 34 deletions(-)

M cart.c
M cpu.c
M mapper000.c
M cart.c => cart.c +33 -25
@@ 4,7 4,7 @@

  File: cart.c
  Created: 2019-11-03
  Updated: 2019-11-27
  Updated: 2019-12-02
  Author: Aaron Oman
  Notice: GNU AGPLv3 License



@@ 17,7 17,7 @@
#include <stdint.h>
#include <stdio.h> // fopen
#include <stdbool.h> // bool
#include <stdlib.h> // malloc
#include <stdlib.h> // calloc, free

#include "cart.h"
#include "util.h"


@@ 56,7 56,7 @@ struct header {
};

struct cart *CartInit(char *filename) {
        struct cart *cart = (struct cart *)malloc(sizeof(struct cart));
        struct cart *cart = (struct cart *)calloc(1, sizeof(struct cart));
        if (NULL == cart) {
                return NULL;
        }


@@ 91,15 91,23 @@ struct cart *CartInit(char *filename) {

        // "Discover" file format.
        uint8_t file_type = 1;

        if (0 == file_type) {
                // TODO unhandled file_type
        }

        if (1 == file_type) {
                cart->prgBanks = header.prgRomChunks;
                cart->prgMem = (uint8_t *)malloc(cart->prgBanks * KB_AS_B(16));
                objs_read = fread(cart->prgMem, 1, cart->prgBanks * KB_AS_B(16), f);
                if (objs_read < cart->prgBanks * KB_AS_B(16)) {
                cart->prgMem = (uint8_t *)calloc(cart->prgBanks, KB_AS_B(16));
                if (NULL == cart->prgMem) {
                        fclose(f);
                        free(cart);
                        return NULL;
                        // TODO Couldn't allocate memory for prgMem - set appropriate error
                }

                objs_read = fread(cart->prgMem, KB_AS_B(16), cart->prgBanks, f);
                if (objs_read < cart->prgBanks) {
                        fclose(f);
                        free(cart);
                        return NULL;


@@ 107,24 115,21 @@ struct cart *CartInit(char *filename) {
                }

                cart->chrBanks = header.chrRomChunks;
                if (0 == cart->chrBanks) {
                        cart->chrMem = (uint8_t *)malloc(KB_AS_B(8));
                        objs_read = fread(cart->chrMem, 1, KB_AS_B(8), f);
                        if (objs_read < KB_AS_B(8)) {
                                fclose(f);
                                free(cart);
                                return NULL;
                                // TODO Couldn't read data from file - set appropriate error
                        }
                } else {
                        cart->chrMem = (uint8_t *)malloc(cart->chrBanks * KB_AS_B(8));
                        objs_read = fread(cart->chrMem, 1, cart->chrBanks * KB_AS_B(8), f);
                        if (objs_read < cart->chrBanks * KB_AS_B(8)) {
                                fclose(f);
                                free(cart);
                                return NULL;
                                // TODO Couldn't read data from file - set appropriate error
                        }
                int numBanks = (0 == cart->chrBanks) ? 1 : cart->chrBanks;

                cart->chrMem = (uint8_t *)calloc(numBanks, KB_AS_B(8));
                if (NULL == cart->chrMem) {
                        fclose(f);
                        free(cart);
                        return NULL;
                        // TODO Couldn't read data from file - set appropriate error
                }
                objs_read = fread(cart->chrMem, numBanks, KB_AS_B(8), f);
                if (objs_read < numBanks) {
                        fclose(f);
                        free(cart);
                        return NULL;
                        // TODO Couldn't read data from file - set appropriate error
                }
        }



@@ 144,7 149,10 @@ struct cart *CartInit(char *filename) {

                        cart->mapper = cart->mapperInit(cart->prgBanks, cart->chrBanks);
                        if (NULL == cart->mapper) {
                                // TODO handle cart->mapper not being initialized
                                fclose(f);
                                free(cart);
                                return NULL;
                                // TODO Couldn't initialize mapper - set appropriate error
                        }
                        break;
                }

M cpu.c => cpu.c +2 -2
@@ 1022,8 1022,6 @@ uint8_t JSR(struct cpu *cpu) {
        return 0;
}

// TODO(AARONO): Finish reviewing instructions from here downward.

//! \brief Load Into Accumulator
//!
//! A = data


@@ 1240,6 1238,8 @@ uint8_t RTI(struct cpu *cpu) {

//! \brief ??
//!
//! Restore cpu state but not the status flags after processing an interrupt.
//!
//! \param[in,out] cpu
//! \return 0 This instruction will take no additional cycles
uint8_t RTS(struct cpu *cpu) {

M mapper000.c => mapper000.c +14 -7
@@ 4,7 4,7 @@

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



@@ 24,7 24,7 @@ struct mapper000 {
};

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


@@ 39,18 39,25 @@ void Mapper000_Deinit(void *interface) {
        if (NULL == interface)
                return;

        free(interface);
        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;
        uint8_t mask = (mapper->prgBanks > 1) ? 0x7FFF : 0x3FFF;

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



@@ 59,10 66,9 @@ bool Mapper000_MapCpuRead(void *interface, uint16_t addr, uint32_t *mappedAddr) 

bool Mapper000_MapCpuWrite(void *interface, uint16_t addr, uint32_t *mappedAddr) {
        struct mapper000 *mapper = (struct mapper000 *)interface;
        uint8_t mask = (mapper->prgBanks > 1) ? 0x7FFF : 0x3FFF;

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



@@ 80,6 86,7 @@ bool Mapper000_MapPpuRead(void *interface, uint16_t addr, uint32_t *mappedAddr) 

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.