Palettes, attribute fetch fix (thanks @nzldvd).
This commit is contained in:
parent
0df53d43f8
commit
542f4c8149
7 changed files with 34 additions and 18 deletions
|
@ -1,4 +1,4 @@
|
|||
#include "types.hpp"
|
||||
#include "common.hpp"
|
||||
|
||||
#ifndef CARTRIDGE_HPP
|
||||
#define CARTRIDGE_HPP
|
||||
|
|
|
@ -1,14 +1,16 @@
|
|||
#include <cstdint>
|
||||
|
||||
#ifndef TYPES_HPP
|
||||
#define TYPES_HPP
|
||||
#ifndef COMMON_HPP
|
||||
#define COMMON_HPP
|
||||
|
||||
|
||||
#define NTH_BIT(x, n) (((x) >> (n)) & 1)
|
||||
|
||||
/* Integer type shortcuts */
|
||||
typedef uint8_t u8; typedef int8_t s8;
|
||||
typedef uint16_t u16; typedef int16_t s16;
|
||||
typedef uint32_t u32; typedef int32_t s32;
|
||||
typedef uint64_t u64; typedef int64_t s64;
|
||||
typedef uint64_t u64; typedef int64_t s64;
|
||||
|
||||
|
||||
#endif // TYPES_HPP
|
||||
#endif // COMMON_HPP
|
|
@ -93,8 +93,8 @@ template<u8& s, u8& d> void tr() { upd_nz(d = s); T; }
|
|||
template<> void tr<X,S>() { S = X; T; } // TSX, exception.
|
||||
|
||||
/* Stack operations */
|
||||
void PLP() { T; T; P.reg = (pop() & 0b11001111) | (P.reg & 0b00110000); }
|
||||
void PHP() { T; push(P.reg | (1 << 4)); }
|
||||
void PLP() { T; T; P.reg = (pop() & 0b11001111) | (P.reg & 0b00110000); } // Ignore bits 4 and 5.
|
||||
void PHP() { T; push(P.reg | (1 << 4)); } // B flag set.
|
||||
void PLA() { T; T; A = pop(); upd_nz(A); }
|
||||
void PHA() { T; push(A); }
|
||||
|
||||
|
@ -220,6 +220,7 @@ void reset()
|
|||
PC = rd16(0xFFFC);
|
||||
}
|
||||
|
||||
/* Turn on the CPU */
|
||||
void power()
|
||||
{
|
||||
P.reg = 0x34;
|
||||
|
@ -229,6 +230,7 @@ void power()
|
|||
reset();
|
||||
}
|
||||
|
||||
/* Start the execution */
|
||||
void run()
|
||||
{
|
||||
while (true)
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
#include "types.hpp"
|
||||
#include "common.hpp"
|
||||
|
||||
#ifndef CPU_HPP
|
||||
#define CPU_HPP
|
||||
|
@ -17,7 +17,7 @@ union Flags
|
|||
bool c : 1; // Carry.
|
||||
bool z : 1; // Zero.
|
||||
bool i : 1; // Interrupt priority.
|
||||
bool d : 1; // Decimal (useless).
|
||||
bool d : 1; // Decimal (unused).
|
||||
bool b : 1;
|
||||
bool unused : 1;
|
||||
bool v : 1; // Overflow.
|
||||
|
@ -25,7 +25,7 @@ union Flags
|
|||
};
|
||||
u8 reg;
|
||||
|
||||
bool get(u8 i) { return reg & (1 << i); }
|
||||
bool get(u8 i) { return reg & (1 << i); }
|
||||
void set(u8 i, bool v) { reg = v ? (reg | (1 << i)) : (reg & ~(1 << i)); }
|
||||
};
|
||||
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
int main(int argc, char *argv[])
|
||||
{
|
||||
Cartridge::load(argv[1]);
|
||||
PPU::init();
|
||||
PPU::power();
|
||||
CPU::power();
|
||||
|
||||
CPU::run();
|
||||
|
|
22
src/ppu.cpp
22
src/ppu.cpp
|
@ -30,6 +30,16 @@ u8 atShiftL, atShiftH; u16 bgShiftL, bgShiftH;
|
|||
int scanline, cycle;
|
||||
bool frameOdd;
|
||||
|
||||
u32 nes_rgb[] =
|
||||
{ 0x7C7C7C, 0x0000FC, 0x0000BC, 0x4428BC, 0x940084, 0xA80020, 0xA81000, 0x881400,
|
||||
0x503000, 0x007800, 0x006800, 0x005800, 0x004058, 0x000000, 0x000000, 0x000000,
|
||||
0xBCBCBC, 0x0078F8, 0x0058F8, 0x6844FC, 0xD800CC, 0xE40058, 0xF83800, 0xE45C10,
|
||||
0xAC7C00, 0x00B800, 0x00A800, 0x00A844, 0x008888, 0x000000, 0x000000, 0x000000,
|
||||
0xF8F8F8, 0x3CBCFC, 0x6888FC, 0x9878F8, 0xF878F8, 0xF85898, 0xF87858, 0xFCA044,
|
||||
0xF8B800, 0xB8F818, 0x58D854, 0x58F898, 0x00E8D8, 0x787878, 0x000000, 0x000000,
|
||||
0xFCFCFC, 0xA4E4FC, 0xB8B8F8, 0xD8B8F8, 0xF8B8F8, 0xF8A4C0, 0xF0D0B0, 0xFCE0A8,
|
||||
0xF8D878, 0xD8F878, 0xB8F8B8, 0xB8F8D8, 0x00FCFC, 0xF8D8F8, 0x000000, 0x000000 };
|
||||
|
||||
inline bool rendering() { return mask.bg || mask.spr; }
|
||||
|
||||
/* Access PPU memory */
|
||||
|
@ -125,11 +135,13 @@ inline void reload_shift()
|
|||
{
|
||||
bgShiftL = (bgShiftL & 0xFF00) | bgL;
|
||||
bgShiftH = (bgShiftH & 0xFF00) | bgH;
|
||||
atShiftL = (atShiftL << 1) | (at & 1);
|
||||
atShiftH = (atShiftH << 1) | ((at & 2) >> 1);
|
||||
if (!(vAddr.cX % 2))
|
||||
{
|
||||
atShiftL = (atShiftL << 1) | (at & 1);
|
||||
atShiftH = (atShiftH << 1) | ((at & 2) >> 1);
|
||||
}
|
||||
}
|
||||
|
||||
#define NTH_BIT(v, i) (((v) >> (i)) & 1)
|
||||
void pixel()
|
||||
{
|
||||
u8 bgBits = (NTH_BIT(bgShiftH, 15 - fX) << 1) |
|
||||
|
@ -139,7 +151,7 @@ void pixel()
|
|||
u8 palInd = (atBits << 2) | bgBits;
|
||||
|
||||
if (scanline < 240 and cycle >= 1 and cycle <= 256)
|
||||
((u32*) s->pixels) [scanline * 256 + (cycle - 1)] = rendering() ? (palette[palInd] * 4) << 8 : 0;
|
||||
((u32*) s->pixels) [scanline * 256 + (cycle - 1)] = rendering() ? nes_rgb[palette[palInd]] : 0;
|
||||
|
||||
bgShiftL <<= 1; bgShiftH <<= 1;
|
||||
}
|
||||
|
@ -205,7 +217,7 @@ void step()
|
|||
scanline = cycle ? scanline : (scanline + 1);
|
||||
}
|
||||
|
||||
void init()
|
||||
void power()
|
||||
{
|
||||
SDL_Init(SDL_INIT_VIDEO);
|
||||
s = SDL_SetVideoMode(256, 240, 32, 0);
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
#include "types.hpp"
|
||||
#include "common.hpp"
|
||||
|
||||
#ifndef PPU_HPP
|
||||
#define PPU_HPP
|
||||
|
@ -70,7 +70,7 @@ union Addr
|
|||
unsigned r : 15;
|
||||
};
|
||||
|
||||
void init();
|
||||
void power();
|
||||
void step();
|
||||
template <bool write> u8 access(u16 index, u8 v = 0);
|
||||
|
||||
|
|
Reference in a new issue