~groovestomp/gsnes

d128a3c40766428a0a6c4fdc5515927ff8160276 — GrooveStomp 1 year, 5 months ago cfc13a7
Backgrounds are working
6 files changed, 44 insertions(+), 36 deletions(-)

M bus.c
M cart.c
M graphics.c
M main.c
M ppu.c
M ppu.h
M bus.c => bus.c +6 -5
@@ 14,7 14,7 @@
  conditions; See LICENSE for details.
 ******************************************************************************/
//! \file bus.c
#include <stdlib.h> // malloc, free
#include <stdlib.h> // calloc, free

#include "bus.h"
#include "cpu.h"


@@ 31,13 31,14 @@ struct bus {
};

struct bus *BusInit(struct cpu *cpu, struct ppu *ppu) {
        struct bus *bus = (struct bus *)malloc(sizeof(struct bus));
        struct bus *bus = (struct bus *)calloc(1, sizeof(struct bus));
        if (NULL == bus)
                return NULL;

        bus->cpuRam = (uint8_t *)malloc(KB_AS_B(2));
        for (int i = 0; i < KB_AS_B(2); i++) {
                bus->cpuRam[i] = 0x00;
        bus->cpuRam = (uint8_t *)calloc(KB_AS_B(2), sizeof(uint8_t));
        if (NULL == bus->cpuRam) {
                BusDeinit(bus);
                return NULL;
        }

        bus->cpu = cpu;

M cart.c => cart.c +3 -1
@@ 72,6 72,7 @@ struct cart *CartInit(char *filename) {
                free(cart);
                return NULL;
        }

        struct header header;
        size_t objs_read = fread(&header, sizeof(struct header), 1, f);
        if (objs_read != 1) {


@@ 124,7 125,8 @@ struct cart *CartInit(char *filename) {
                        return NULL;
                        // TODO Couldn't read data from file - set appropriate error
                }
                objs_read = fread(cart->chrMem, numBanks, KB_AS_B(8), f);

                objs_read = fread(cart->chrMem, KB_AS_B(8), numBanks, f);
                if (objs_read < numBanks) {
                        fclose(f);
                        free(cart);

M graphics.c => graphics.c +9 -5
@@ 4,7 4,7 @@

  File: graphics.c
  Created: 2019-06-25
  Updated: 2019-11-19
  Updated: 2019-12-03
  Author: Aaron Oman
  Notice: GNU GPLv3 License



@@ 319,20 319,24 @@ void GraphicsDrawSprite(struct graphics *graphics, int x, int y, struct sprite *
        if (NULL == sprite) return;

        if (scale > 1) {
                for (int i = 0; i < sprite->width; i++)
                        for (int j = 0; j < sprite->height; j++)
                for (int i = 0; i < sprite->width; i++) {
                        for (int j = 0; j < sprite->height; j++) {
                                int j1 = sprite->height - j - 1;
                                for (int is = 0; is < scale; is++)
                                        for (int js = 0; js < scale; js++) {
                                                uint32_t color = SpriteGetPixel(sprite, i, j);
                                                int xp = x + (i * scale) + is;
                                                int yp = y + (j * scale) + js;
                                                int yp = y + (j1 * scale) + js;
                                                GraphicsPutPixel(graphics, xp, yp, color);
                                        }
                        }
                }
        } else {
                for (int i = 0; i < sprite->width; i++)
                        for (int j = 0; j < sprite->height; j++) {
                                int yp = sprite->height - j - 1;
                                uint32_t color = SpriteGetPixel(sprite, i, j);
                                GraphicsPutPixel(graphics, x + i, y + j, color);
                                GraphicsPutPixel(graphics, x + i, y + yp, color);
                        }
        }
}

M main.c => main.c +1 -9
@@ 67,7 67,7 @@ void Deinit(int code) {
void Init() {
        char *ttf_filename = "/usr/share/fonts/truetype/ubuntu/UbuntuMono-R.ttf";

        cart = CartInit("nestest.nes");
        cart = CartInit("ice_climber.nes");
        if (NULL == cart) {
                fprintf(stderr, "Couldn't load cart");
                Deinit(1);


@@ 264,14 264,6 @@ int main(int argc, char **argv) {

                GraphicsDrawSprite(graphics, 0, 0, PpuScreen(ppu), 3);

                for (int y = 0; y < 30; y++) {
                        for (int x = 0; x < 32; x++) {
                                char buf[5];
                                HexToString(PpuGetNameTable(ppu, 0)[y * 32 + x], 2, buf, 5);
                                GraphicsDrawText(graphics, x * 16, y * 16, buf, 15, ColorBlack.rgba);
                        }
                }

                GraphicsEnd(graphics);
        }


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

  File: ppu.c
  Created: 2019-11-03
  Updated: 2019-12-01
  Updated: 2019-12-03
  Author: Aaron Oman
  Notice: GNU AGPLv3 License



@@ 726,6 726,8 @@ uint8_t PpuRead(struct ppu *ppu, uint16_t addr) {
                uint16_t pattern_index = (addr & 0x1000) >> 12;
                data = ppu->patternTables[pattern_index][addr & 0x0FFF];
        } else if (addr >= 0x2000 && addr <= 0x3EFF) {
                addr &= 0x0FFF;

                switch (CartMirroring(ppu->cart)) {
                        case MIRROR_VERTICAL: {
                                if (addr >= 0x0000 && addr <= 0x03FF)


@@ 737,6 739,7 @@ uint8_t PpuRead(struct ppu *ppu, uint16_t addr) {
                                if (addr >= 0x0C00 && addr <= 0x0FFF)
                                        data = ppu->nameTables[1][addr & 0x03FF];
                        } break;

                        case MIRROR_HORIZONTAL: {
                                if (addr >= 0x0000 && addr <= 0x03FF)
                                        data = ppu->nameTables[0][addr & 0x03FF];


@@ 747,8 750,10 @@ uint8_t PpuRead(struct ppu *ppu, uint16_t addr) {
                                if (addr >= 0x0C00 && addr <= 0x0FFF)
                                        data = ppu->nameTables[1][addr & 0x03FF];
                        } break;
                        default:
                                break;

                        default: {

                        } break;
                }
        } else if (addr >= 0x3F00 && addr <= 0x3FFF) { // Palette Memory.
                addr &= 0x001F; // Mask the bottom 5 bits.


@@ 777,6 782,7 @@ void PpuWrite(struct ppu *ppu, uint16_t addr, uint8_t data) {
                ppu->patternTables[pattern_index][addr & 0x0FFF] = data;
        } else if (addr >= 0x2000 && addr <= 0x3EFF) {
                addr &= 0x0FFF;

                switch (CartMirroring(ppu->cart)) {
                        case MIRROR_VERTICAL: {
                                if (addr >= 0x0000 && addr <= 0x03FF)


@@ 788,6 794,7 @@ void PpuWrite(struct ppu *ppu, uint16_t addr, uint8_t data) {
                                if (addr >= 0x0C00 && addr <= 0x0FFF)
                                        ppu->nameTables[1][addr & 0x03FF] = data;
                        } break;

                        case MIRROR_HORIZONTAL: {
                                if (addr >= 0x0000 && addr <= 0x03FF)
                                        ppu->nameTables[0][addr & 0x03FF] = data;


@@ 798,8 805,10 @@ void PpuWrite(struct ppu *ppu, uint16_t addr, uint8_t data) {
                                if (addr >= 0x0C00 && addr <= 0x0FFF)
                                        ppu->nameTables[1][addr & 0x03FF] = data;
                        } break;
                        default:
                                break;

                        default: {

                        } break;
                }
        } else if (addr >= 0x3F00 && addr <= 0x3FFF) { // Palette Memory.
                addr &= 0x001F; // Mask the bottom 5 bits.


@@ 972,13 981,13 @@ struct color *PpuGetColorFromPaletteRam(struct ppu *ppu, uint8_t palette, uint8_

struct sprite *PpuGetPatternTable(struct ppu *ppu, uint8_t i, uint8_t palette) {
        // Loop through all 16x16 tiles
        for (int tileY = 0; tileY < 16; tileY++) {
                for (int tileX = 0; tileX < 16; tileX++) {
        for (uint16_t tileY = 0; tileY < 16; tileY++) {
                for (uint16_t tileX = 0; tileX < 16; tileX++) {
                        // Convert the 2D tile coordinate into a 1D offset into pattern table memory.
                        int byteOffset = tileY * 256 + tileX * 16;
                        uint16_t byteOffset = tileY * 256 + tileX * 16;

                        // Loop through 8 rows of 8 pixels per tile.
                        for (int row = 0; row < 8; row++) {
                        for (uint16_t row = 0; row < 8; row++) {
                                // Each pixel is 2 bits, stored in two separate bit planes.
                                // Each bit plane is 64 bits, which means the LSb
                                // and MSb are always 64 bits (8 bytes) apart.


@@ 987,7 996,7 @@ struct sprite *PpuGetPatternTable(struct ppu *ppu, uint8_t i, uint8_t palette) {

                                // We read 8 bits worth of data, now we iterate
                                // through each column of the current row.
                                for (int col = 0; col < 8; col++) {
                                for (uint16_t col = 0; col < 8; col++) {
                                        uint8_t pixel = (tileLsb & 0x01) + (tileMsb & 0x01);

                                        // Shift each byte right one bit so the


@@ 996,6 1005,8 @@ struct sprite *PpuGetPatternTable(struct ppu *ppu, uint8_t i, uint8_t palette) {
                                        tileLsb >>= 1;
                                        tileMsb >>= 1;

                                        struct color *color = PpuGetColorFromPaletteRam(ppu, palette, pixel);

                                        // Because we are reading the LSb first,
                                        // we are effectively reading right to
                                        // left; so when we draw, we need to


@@ 1006,8 1017,6 @@ struct sprite *PpuGetPatternTable(struct ppu *ppu, uint8_t i, uint8_t palette) {
                                        int x = tileX * 8 + (7 - col);
                                        int y = tileY * 8 + row;

                                        struct color *color = PpuGetColorFromPaletteRam(ppu, palette, pixel);

                                        SpriteSetPixel(ppu->patternTableSprites[i], x, y, color->rgba);
                                }
                        }


@@ 1058,6 1067,6 @@ void PpuSetNmi(struct ppu *ppu, uint8_t trueOrFalse) {
        ppu->nmi = trueOrFalse;
}

uint8_t *PpuGetNameTable(struct ppu *ppu, uint8_t i) {
        return ppu->nameTables[i];
struct sprite *PpuGetNameTable(struct ppu *ppu, uint8_t i) {
        return ppu->nameTableSprites[i];
}

M ppu.h => ppu.h +2 -2
@@ 4,7 4,7 @@

  File: ppu.h
  Created: 2019-11-03
  Updated: 2019-11-30
  Updated: 2019-12-03
  Author: Aaron Oman
  Notice: GNU AGPLv3 License



@@ 84,7 84,7 @@ PpuGetNmi(struct ppu *ppu);
void
PpuSetNmi(struct ppu *ppu, uint8_t trueOrFalse);

uint8_t *
struct sprite *
PpuGetNameTable(struct ppu *ppu, uint8_t i);

#endif // PPU_VERSION