C Gameboy将主要文件分开

时间:2012-02-26 21:11:08

标签: c makefile gameboy

我正在为一个课堂作业开发一个gameboy项目。有人告诉我,我们需要一个单独的“函数”文件和“主”文件。这样做我疯了。我创建了一个头文件“myLib.h”,其中包含了“myLib.c”中函数的定义和原型。在我的主文件中,我从“myLib.c”调用一个函数,它编译,但不起作用。我已将“myLib.c”添加到我的make文件中。文件如下:

myLib.h

typedef unsigned short u16;
typedef unsigned char u8;
typedef unsigned int u32;

#define REG_DISPCTL *(unsigned short *)0x4000000
#define MODE3 3
#define BG2_ENABLE  (1<<10)
#define SCANLINECOUNTER *(volatile u16 *)0x4000006

#define COLOR(r, g, b) ((r) | (g)<<5 | (b)<<10)

#define RED COLOR(31,0,0)
#define GREEN COLOR(0,31,0)
#define BLUE COLOR(0,0,31)
#define MAGENTA COLOR(31, 0, 31)
#define YELLOW COLOR(31, 31, 0)
#define CYAN COLOR(0,31,31)
#define WHITE COLOR(31,31,31)
#define BLACK 0


#define OFFSET(r, c, numcols)  ((r)*(numcols) + (c))


extern unsigned short *videoBuffer;

// Buttons

#define BUTTON_A        (1<<0)
#define BUTTON_B        (1<<1)
#define BUTTON_SELECT           (1<<2)
#define BUTTON_START            (1<<3)
#define BUTTON_RIGHT            (1<<4)
#define BUTTON_LEFT     (1<<5)
#define BUTTON_UP       (1<<6)
#define BUTTON_DOWN     (1<<7)
#define BUTTON_R        (1<<8)
#define BUTTON_L        (1<<9)

#define BUTTONS *(unsigned int *)0x4000130

#define KEY_DOWN_NOW(key)  (~(BUTTONS) & key)

/* DMA */

typedef struct
{
    const volatile void *src;
    const volatile void *dst;
    u32                  cnt;
} DMA_CONTROLLER;

#define DMA ((volatile DMA_CONTROLLER *) 0x040000B0)

#define REG_DMA0SAD         *(const volatile u32*)0x40000B0  // source address
#define REG_DMA0DAD         *(volatile u32*)0x40000B4  // destination address
#define REG_DMA0CNT         *(volatile u32*)0x40000B8  // control register

// DMA channel 1 register definitions
#define REG_DMA1SAD         *(const volatile u32*)0x40000BC  // source address
#define REG_DMA1DAD         *(volatile u32*)0x40000C0  // destination address
#define REG_DMA1CNT         *(volatile u32*)0x40000C4  // control register

// DMA channel 2 register definitions
#define REG_DMA2SAD         *(const volatile u32*)0x40000C8  // source address
#define REG_DMA2DAD         *(volatile u32*)0x40000CC  // destination address
#define REG_DMA2CNT         *(volatile u32*)0x40000D0  // control register

// DMA channel 3 register definitions
#define REG_DMA3SAD         *(volatile u32*)0x40000D4   // source address
#define REG_DMA3DAD         *(volatile u32*)0x40000D8  // destination address
#define REG_DMA3CNT         *(volatile u32*)0x40000DC  // control register

// Defines
#define DMA_CHANNEL_0 0
#define DMA_CHANNEL_1 1
#define DMA_CHANNEL_2 2
#define DMA_CHANNEL_3 3

#define DMA_DESTINATION_INCREMENT (0 << 21)
#define DMA_DESTINATION_DECREMENT (1 << 21)
#define DMA_DESTINATION_FIXED (2 << 21)
#define DMA_DESTINATION_RESET (3 << 21)

#define DMA_SOURCE_INCREMENT (0 << 23)
#define DMA_SOURCE_DECREMENT (1 << 23)
#define DMA_SOURCE_FIXED (2 << 23)

#define DMA_REPEAT (1 << 25)

#define DMA_16 (0 << 26)
#define DMA_32 (1 << 26)

#define DMA_NOW (0 << 28)
#define DMA_AT_VBLANK (1 << 28)
#define DMA_AT_HBLANK (2 << 28)
#define DMA_AT_REFRESH (3 << 28)

#define DMA_IRQ (1 << 30)
#define DMA_ON (1 << 31)






// Prototypes
void setPixel(int , int , u16 );
void drawRect(int row, int col, int height, int width, u16 color);
void waitForVblank();
void fillScreen(u16 color);
void delay(int);

myLib.c

#include "myLib.h"

unsigned short *videoBuffer = (unsigned short *)0x6000000;
void setPixel(int row, int col, u16 color)
{
    videoBuffer[OFFSET(row, col, 240)] = color;
}

void drawRect(int row, int col, int height, int width, 
        volatile u16 color)
{
    int r;

    for(r=0;r<height; r++)
    {

        REG_DMA3SAD = (u32)&color;
        REG_DMA3DAD = (u32)(&videoBuffer[OFFSET(row+r, col, 240)]);
        REG_DMA3CNT = width | DMA_SOURCE_FIXED | 
                  DMA_DESTINATION_INCREMENT | DMA_ON;

    }
}

void waitForVblank()
{
    while(SCANLINECOUNTER > 160);
    while(SCANLINECOUNTER < 160);

}

void fillScreen(volatile u16 color)
{
    REG_DMA3SAD = (u32)&color;
    REG_DMA3DAD = (u32)videoBuffer;
    REG_DMA3CNT = (160*240) | DMA_SOURCE_FIXED | 
                  DMA_DESTINATION_INCREMENT | DMA_ON;
}
void delay(int n)
{
    int i;
    volatile int x;
    for(i=0; i<10000*n; i++)
    {
        x = x + 1;
    }
}

的main.c

#include <stdio.h>
#include <stdlib.h>
#include "myLib.h"

int main() {
    int i = 0;
    int j = 0;
    for(i = 0; i < 160; i++){
        for(j = 0; j < 240; j++){
            {
                setPixel(i, j, RED);
            }
        }
    }
    return (EXIT_SUCCESS);
}

生成文件

########################################
## CS1372 Dual GBA/Console Makefile   ##
## Updated: 1/08/2010 - Drew Bratcher ##
########################################

# Student instructions:
# - Ensure that the paths are correct
# - Edit the SOURCES line with a space-separated list of your .c files
# - Keep updating SOURCES every time you add a new source file
# - Select "Release" in NetBeans if this is a GBA project
# - Select "Debug" in NetBeans if this is a console project
# - Make sure the Linker Output in NetBeans is set to "Program" for all configurations


# --- Project Settings (Change these for your proejct)
# PRODUCT_NAME should match your Linker Output in NetBeans
# SOURCES should be The .c files in your project
PRODUCT_NAME       = Program

###### LIST ALL C Files in your project here
SOURCES            = main.c myLib.c 

###### --- System Settings (Update these for your system)
###### Mac example paths
DKPATH             = C:/CS1372-Tools/devkitARM/devkitARM/bin
CCPATH             = C:/cygwin/bin
VBASIM             = C:/CS1372-Tools/VisualBoyAdvance-1.7.2/VisualBoyAdvance.exe
#DKPATH             = C:/devkitARM/bin
#CCPATH             = C:/cygwin/bin
#VBASIM             = C:/



FIND               = find
COPY               = cp -r

# --- File Names
ELF_NAME           = $(PRODUCT_NAME).elf
ROM_NAME           = $(PRODUCT_NAME).gba
BIN_NAME           = $(PRODUCT_NAME)

# --- Debug and Release Selection
# Don't change this or it'll break your Makefile
# If you need to override the selection, uncomment one:
#CONF               = Debug
#CONF               = Release
ifeq ($(CONF),Debug)
DEBUG              = yes
endif

ifndef DEBUG
# ============ RELEASE MODE
# --- GBA Settings
MODEL              = -mthumb-interwork -mthumb
SPECS              = -specs=gba.specs

# --- Archiver
AS                 = $(DKPATH)/arm-eabi-as
ASFLAGS            = -mthumb-interwork

# --- Compiler
CC                 = $(DKPATH)/arm-eabi-gcc
CFLAGS             = $(MODEL) -O2 -Wall -pedantic -Wextra -Werror -ansi -std=c99 -D_ROM=$(ROM_NAME) -D_VBA=$(VBASIM)
CC_WRAP            = $(CCPATH)/gcc
CFLAGS_WRAP        = -O2 -Wall -pedantic -Wextra -Werror -ansi -std=c99 -D_ROM='"$(ROM_NAME)"' -D_VBA='"$(VBASIM)"'

# --- Linker
LD                 = $(DKPATH)/arm-eabi-gcc
LDFLAGS            = $(SPECS) $(MODEL) -lm

# --- Object/Executable Packager
OBJCOPY            = $(DKPATH)/arm-eabi-objcopy
OBJCOPYFLAGS       = -O binary

# --- ROM Fixer
GBAFIX             = $(DKPATH)/gbafix

# --- Delete
RM                 = rm -f

OBJECTS = $(filter-out gba_wrapper%,$(SOURCES:.c=.o))

# --- Main build target
all : build $(BIN_NAME)

run : build
    $(VBASIM) $(ROM_NAME)

build : UNZIP $(ROM_NAME)

$(BIN_NAME) : gba_wrapper.c
    $(CC_WRAP) $(CFLAGS_WRAP) -o $@ $^

# --- Build .elf file into .gba ROM file
$(ROM_NAME) : $(ELF_NAME)
    $(OBJCOPY) $(OBJCOPYFLAGS) $(ELF_NAME) $(ROM_NAME)
    $(GBAFIX) $(ROM_NAME)

# --- Build .o files into .elf file
$(ELF_NAME) : $(OBJECTS)
    $(LD) $(OBJECTS) $(LDFLAGS) -o $@

# -- Build .c files into .o files
$(OBJECTS) : %.o : %.c
    $(CC) $(CFLAGS) -c $< -o $@

# ============ RELEASE MODE
else
# ============ DEBUG MODE
# --- Compiler
CC                 = $(CCPATH)/gcc
CFLAGS             = -D_DEBUG -O2 -Wall -pedantic -Wextra -Werror -ansi -std=c99 -ggdb

all : build

run : build
    ./$(BIN_NAME)

clean :

build : UNZIP $(BIN_NAME)

$(BIN_NAME) : $(SOURCES)
    $(CC) -o $(BIN_NAME) $(CFLAGS) $(SOURCES)

# ============ DEBUG MODE
endif

# ============ Common
UNZIP :
    -@$(FIND) . -iname "*.zip" -exec unzip -n {} \; -exec echo "This project must be rebuilt" \; -exec rm {} \;

clean:
    $(RM) $(ROM_NAME)
    $(RM) $(ELF_NAME)
    $(RM) $(BIN_NAME)
    $(RM) *.o
# ============ Common

1 个答案:

答案 0 :(得分:0)

我错过了REG_DISPCTL = MODE3 | BG2_ENABLE;