add h and c files

This commit is contained in:
Safyrus 2022-02-03 21:12:13 +01:00
parent 52c5ce0de1
commit 76f100b8cb
5 changed files with 279 additions and 0 deletions

90
include/constant.h Normal file
View File

@ -0,0 +1,90 @@
#ifndef _CONSTANT_H_
#define _CONSTANT_H_
#include "type.h"
// ----------
// PPU
// ----------
#define PPU_CTRL 0x2000
#define PPU_MASK 0x2001
#define PPU_STATUS 0x2002
#define PPU_SCROLL 0x2005
#define PPU_ADDR 0x2006
#define PPU_DATA 0x2007
// PPU MASK
#define PPU_MASK_GREY 0b00000001
#define PPU_MASK_BKG8 0b00000010
#define PPU_MASK_SPR8 0b00000100
#define PPU_MASK_BKG 0b00001000
#define PPU_MASK_SPR 0b00010000
#define PPU_MASK_R 0b00100000
#define PPU_MASK_G 0b01000000
#define PPU_MASK_B 0b10000000
// PPU CTRL
#define PPU_CTRL_NM_1 0b00000001
#define PPU_CTRL_NM_2 0b00000010
#define PPU_CTRL_INC 0b00000100
#define PPU_CTRL_SPR 0b00001000
#define PPU_CTRL_BKG 0b00010000
#define PPU_CTRL_SPR_SIZE 0b00100000
#define PPU_CTRL_SEL 0b01000000
#define PPU_CTRL_NMI 0b10000000
// ----------
// APU
// ----------
#define APU 0x4000
#define APU_SQ1_VOL 0x4000
#define APU_SQ1_SWEEP 0x4001
#define APU_SQ1_LO 0x4002
#define APU_SQ1_HI 0x4003
#define APU_SQ2_VOL 0x4004
#define APU_SQ2_SWEEP 0x4005
#define APU_SQ2_LO 0x4006
#define APU_SQ2_HI 0x4007
#define APU_TRI_LINEAR 0x4008
#define APU_TRI_LO 0x400A
#define APU_TRI_HI 0x400B
#define APU_NOISE_VOL 0x400C
#define APU_NOISE_LO 0x400E
#define APU_NOISE_HI 0x400F
#define APU_DMC_FREQ 0x4010
#define APU_DMC_RAW 0x4011
#define APU_DMC_START 0x4012
#define APU_DMC_LEN 0x4013
#define APU_SND_CHN 0x4015
#define APU_CTRL 0x4015
#define APU_STATUS 0x4015
#define APU_FRAME 0x4017
// ----------
// OAM
// ----------
#define OAMDMA 0x4014
// ----------
// IO
// ----------
#define IO_JOY1 0x4016
#define IO_JOY2 0x4017
// ----------
// NMI
// ----------
#define NMI_DONE 0b10000000
#define NMI_SCRL 0b00010000
#define NMI_PLT 0b00001000
#define NMI_ATR 0b00000100
#define NMI_SPR 0b00000010
#define NMI_BKG 0b00000001
#endif // _CONSTANT_H_

11
include/ram_variables.h Normal file
View File

@ -0,0 +1,11 @@
#ifndef _RAM_VARIABLES_
#define _RAM_VARIABLES_
#include "type.h"
extern uint8_t game_state;
#define OAM_SIZE 256
extern uint8_t OAM[OAM_SIZE];
#endif // _RAM_VARIABLES_

7
include/type.h Normal file
View File

@ -0,0 +1,7 @@
#ifndef _TYPE_H_
#define _TYPE_H_
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
#endif // _TYPE_H_

81
include/zp_variables.h Normal file
View File

@ -0,0 +1,81 @@
#ifndef _ZP_VARIABLES_
#define _ZP_VARIABLES_
#include "type.h"
// NMI Flags to activate graphical update
// Note: You cannot activate all updates.
// You need to have a execution time
// < ~2273 cycles (2000 to be sure)
// 7 bit 0
// ---- ----
// E..R PASB
// | | ||||
// | | |||+- Background tiles update
// | | ||| Execution time depend on data
// | | ||| (cycles ~= 16 + 38*p + for i:p do (14*p[i].n))
// | | ||| (p=packet number, p[i].n = packet data size)
// | | ||+-- Sprites update (513+ cycles)
// | | |+--- Nametables attributes update (821 cycles)
// | | +---- Palettes update (356 cycles)
// | +------ Scroll update (31 cycles)
// +--------- 1 when NMI has ended, should be set to 0 after reading.
// If let to 1, it means the NMI is disable
extern uint8_t nmi_flags;
#pragma zpsym ("nmi_flags")
// Scroll X position
extern uint8_t scroll_x;
#pragma zpsym ("scroll_x")
// Scroll Y position
extern uint8_t scroll_y;
#pragma zpsym ("scroll_y")
// Nametable high adress to update attributes for
// $23 = Nametable 1
// $27 = Nametable 2
// $2B = Nametable 3
// $2F = Nametable 4
extern uint8_t atr_nametable;
#pragma zpsym ("atr_nametable")
// value of the PPU_MASK (need to be refresh manually)
extern uint8_t ppu_ctrl_val;
#pragma zpsym ("ppu_ctrl_val")
// Attributes data to send to PPU during VBLANK
#define ATTRIBUTES_SIZE 64
extern uint8_t attributes[ATTRIBUTES_SIZE];
#pragma zpsym ("attributes")
// Palettes data to send to PPU during VBLANK
// byte 0 = transparente color
// byte 1-3 = background palette 1
// byte 4-6 = background palette 2
// ...
// byte 13-16 = sprite palette 1
// ...
#define PALETTES_SIZE 25
extern uint8_t palettes[PALETTES_SIZE];
#pragma zpsym ("palettes")
// Background data to send to PPU during VBLANK
// Packet structure:
// byte 0 = v.ssssss (v= vertical draw, s= size)
// byte 1-2 = ppu adress
// byte 3-s = tile data
// packet of size 0 means there is no more data to draw
#define BACKGROUND_SIZE 128
extern uint8_t background[BACKGROUND_SIZE];
#pragma zpsym ("background")
#endif // _ZP_VARIABLES_

90
src/main.c Normal file
View File

@ -0,0 +1,90 @@
#include "constant.h"
#include "zp_variables.h"
#include "ram_variables.h"
#include "famistudio_cc65.h"
// #pragma bss-name(push, "ZEROPAGE")
// test variable in zeropage
// uint8_t zeroPageVar;
// #pragma bss-name(push, "BSS")
// test variable in RAM
// uint8_t ramVar;
extern uint8_t music_data_arpeggio[];
void init()
{
famistudio_init(1, music_data_arpeggio);
famistudio_music_play(0);
// zeroPageVar = 0;
// ramVar = 0;
}
void loop()
{
static uint8_t i = 0;
// set PPU_MASK to draw background and sprites
*(uint8_t *)PPU_MASK = PPU_MASK_BKG + PPU_MASK_BKG8 + PPU_MASK_SPR + PPU_MASK_SPR8;
// tell to the nmi what we want to do for the next frame
nmi_flags = NMI_SCRL + NMI_PLT + NMI_BKG + NMI_ATR + NMI_SPR;
// tall the nmi we want to change the attruvtes of the first nametable
atr_nametable = 0x23;
// reset attributes
do
{
attributes[i] = 0;
++i;
} while (i < ATTRIBUTES_SIZE);
// reset paletttes
i = 0;
do
{
palettes[i] = i;
++i;
} while (i < PALETTES_SIZE);
// update sprite
OAM[0] = 0x40;
OAM[1] = 0x01;
OAM[2] = 0x00;
OAM[3] = game_state;
// update background
background[0] = 0x05;
background[1] = 0x20;
background[2] = game_state;
background[3] = 0x04;
background[4] = 0x00;
background[5] = 0x01;
background[6] = 0x02;
background[7] = 0x03;
background[8] = 0x00;
// inc variables
// ++zeroPageVar;
// ++ramVar;
++game_state;
}
void main()
{
init();
while (1)
{
// wait for the next frame
while ((nmi_flags & 0x80) == 0)
{
}
famistudio_update();
loop();
}
}