Skip to content

Commit

Permalink
refactor indents
Browse files Browse the repository at this point in the history
  • Loading branch information
jiangyy committed Feb 13, 2014
1 parent e4ba2dc commit 87fbc16
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 110 deletions.
2 changes: 1 addition & 1 deletion src/emu6502/memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ byte memory_readb(word address)
void memory_writeb(word address, byte data)
{
// DMA transfer
int i;
int i;
if (address == 0x4014) {
for (i = 0; i < 256; i++) {
ppu_sprram_write(cpu_ram_read((0x100 * data) + i));
Expand Down
24 changes: 12 additions & 12 deletions src/emu6502/ppu.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ inline void ppu_ram_write(word address, byte data)

void ppu_draw_background_scanline(bool mirror)
{
int tile_x;
int tile_x;
for (tile_x = ppu_shows_background_in_leftmost_8px() ? 0 : 1; tile_x < 32; tile_x++) {
// Skipping off-screen pixels
if (((tile_x << 3) - ppu.PPUSCROLL_X + (mirror ? 256 : 0)) > 256)
Expand All @@ -144,7 +144,7 @@ void ppu_draw_background_scanline(bool mirror)
byte l = ppu_ram_read(tile_address + y_in_tile);
byte h = ppu_ram_read(tile_address + y_in_tile + 8);

int x;
int x;
for (x = 0; x < 8; x++) {
byte color = ppu_l_h_addition_table[l][h][x];

Expand All @@ -166,12 +166,12 @@ void ppu_draw_background_scanline(bool mirror)
palette_attribute &= 3;

word palette_address = 0x3F00 + (palette_attribute << 2);
int idx = ppu_ram_read(palette_address + color);
int idx = ppu_ram_read(palette_address + color);
pal c = palette[idx];

ppu_screen_background[(tile_x << 3) + x][ppu.scanline] = color;
pixbuf_add(bg, (tile_x << 3) + x - ppu.PPUSCROLL_X + (mirror ? 256 : 0), ppu.scanline + 1, c.r, c.g, c.b, idx);
pixbuf_add(bg, (tile_x << 3) + x - ppu.PPUSCROLL_X + (mirror ? 256 : 0), ppu.scanline + 1, c.r, c.g, c.b, idx);
}
}
}
Expand All @@ -180,7 +180,7 @@ void ppu_draw_background_scanline(bool mirror)
void ppu_draw_sprite_scanline()
{
int scanline_sprite_count = 0;
int n;
int n;
for (n = 0; n < 0x100; n += 4) {
byte sprite_x = PPU_SPRRAM[n + 3];
byte sprite_y = PPU_SPRRAM[n];
Expand All @@ -207,21 +207,21 @@ void ppu_draw_sprite_scanline()

byte palette_attribute = PPU_SPRRAM[n + 2] & 0x3;
word palette_address = 0x3F10 + (palette_attribute << 2);
int x;
int x;
for (x = 0; x < 8; x++) {
int color = hflip ? ppu_l_h_addition_flip_table[l][h][x] : ppu_l_h_addition_table[l][h][x];

// Color 0 is transparent
if (color != 0) {
int screen_x = sprite_x + x;
int idx = ppu_ram_read(palette_address + color);
int idx = ppu_ram_read(palette_address + color);
pal c = palette[idx];

if (PPU_SPRRAM[n + 2] & 0x20) {
pixbuf_add(bbg, screen_x, sprite_y + y_in_tile + 1, c.r, c.g, c.b, idx);
pixbuf_add(bbg, screen_x, sprite_y + y_in_tile + 1, c.r, c.g, c.b, idx);
}
else {
pixbuf_add(fg, screen_x, sprite_y + y_in_tile + 1, c.r, c.g, c.b, idx);
pixbuf_add(fg, screen_x, sprite_y + y_in_tile + 1, c.r, c.g, c.b, idx);
}

// Checking sprite 0 hit
Expand Down Expand Up @@ -375,7 +375,7 @@ void ppu_init()
ppu_2007_first_read = true;

// Initializing low-high byte-pairs for pattern tables
int h, l, x;
int h, l, x;
for (h = 0; h < 0x100; h++) {
for (l = 0; l < 0x100; l++) {
for (x = 0; x < 8; x++) {
Expand All @@ -394,7 +394,7 @@ void ppu_sprram_write(byte data)
void ppu_set_background_color(byte color)
{
pal c = palette[color];
nes_set_bg_color(c.r, c.g, c.b, color);
nes_set_bg_color(c.r, c.g, c.b, color);
}

void ppu_set_mirroring(byte mirroring)
Expand Down
2 changes: 1 addition & 1 deletion src/emu6502/psg.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ inline byte psg_io_read(word address)
// Joystick 1
if (address == 0x4016) {
if (p++ < 9) {
return nes_key_state(p);
return nes_key_state(p);
}
}
return 0;
Expand Down
169 changes: 88 additions & 81 deletions src/fce/hal.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,37 @@ HAL是硬件抽象层(hardware abstract layer)。
在这里实现以下抽象函数:
1) nes_hal_init()
HAL的初始化。这个函数会在模拟器完成初始化后调用。在这里实现模拟器的
初始化工作
HAL的初始化。这个函数会在模拟器完成初始化后调用。在这里实现模拟器的
初始化工作
2) nes_set_bg_color(r, g, b, c)
设置背景色,r, g, b分别是红、绿、蓝分量的数值(0-255)。
c是54色表示的内部代码,可以考虑使用
设置背景色,r, g, b分别是红、绿、蓝分量的数值(0-255)。
c是54色表示的内部代码,可以考虑使用
3) nes_draw_pixel(*p)
在视频缓冲区中绘制Pixel p,其p->x, p->y是点坐标(左上角为原点,横边
为x轴)。
r, g, b, c为颜色信息。
在视频缓冲区中绘制Pixel p,其p->x, p->y是点坐标(左上角为原点,横边
为x轴)。
r, g, b, c为颜色信息。
4) nes_flip_display()
将视频缓冲区内的内容显示到屏幕上,然后用设置好的背景色填充视频缓冲区。
将视频缓冲区内的内容显示到屏幕上,然后用设置好的背景色填充视频缓冲区。
5) wait_for_frame()
你首先需要设置一个1/FPS (FPS=60)s的定时器。
然后,模拟器会不断执行wait_for_frame,每一帧执行、绘制完毕后,会调用
wait_for_frame(),在这个函数里你需要等待直到下次定时器事件触发。
换句话说,你需要保证下面程序里的do_something()以每秒60次的频率执行:
while (1) {
wait_for_frame();
do_something();
}
你首先需要设置一个1/FPS (FPS=60)s的定时器。
然后,模拟器会不断执行wait_for_frame,每一帧执行、绘制完毕后,会调用
wait_for_frame(),在这个函数里你需要等待直到下次定时器事件触发。
换句话说,你需要保证下面程序里的do_something()以每秒60次的频率执行:
while (1) {
wait_for_frame();
do_something();
}
6) int nes_key_state(int b)
询问按键状态,返回0代表松开,1代表按下。不同的b代表询问不同的按键:
0 - Power,总是为1
1 - 手柄上的A键
2 - 手柄上的B键
3 - 手柄上的SELECT键
4 - 手柄上的START键
5 - 手柄上的UP键
6 - 手柄上的DOWN键
7 - 手柄上的LEFT键
8 - 手柄上的RIGHT键
询问按键状态,返回0代表松开,1代表按下。不同的b代表询问不同的按键:
0 - Power,总是为1
1 - 手柄上的A键
2 - 手柄上的B键
3 - 手柄上的SELECT键
4 - 手柄上的START键
5 - 手柄上的UP键
6 - 手柄上的DOWN键
7 - 手柄上的LEFT键
8 - 手柄上的RIGHT键
*/
#include "nes/hal.h"
#include "nes/fce.h"
Expand All @@ -50,79 +50,86 @@ int vtx_sz = 0;


/* 控制时间的函数,函数每次完成计算后会被调用,保证每秒执行FPS次 */
void wait_for_frame() {
while (1)
{
ALLEGRO_EVENT event;
al_wait_for_event(fce_event_queue, &event);
if (event.type == ALLEGRO_EVENT_TIMER) break;
}
asm volatile ("nop");
void wait_for_frame()
{
while (1)
{
ALLEGRO_EVENT event;
al_wait_for_event(fce_event_queue, &event);
if (event.type == ALLEGRO_EVENT_TIMER) break;
}
asm volatile ("nop");
}

/* 设置背景色为(r, g, b),等价于NES内部颜色代码c
如果不设置,Super Mario将看到黑色而不是蓝色的背景 */
void nes_set_bg_color(int r, int g, int b, int c) {
al_clear_to_color(al_map_rgb(r, g, b));
void nes_set_bg_color(int r, int g, int b, int c)
{
al_clear_to_color(al_map_rgb(r, g, b));
}

/* 在屏幕上绘制一个像素点
但在这个实现里,我们只是保存这个像素点,在flip_display时统一绘制 */
void nes_draw_pixel(Pixel *p) {
ALLEGRO_VERTEX px;
px.x = p->x;
px.y = p->y;
px.z = 0;
px.color = al_map_rgb(p->r, p->g, p->b);
memcpy(&vtx[vtx_sz++], &px, sizeof(ALLEGRO_VERTEX));
void nes_draw_pixel(Pixel *p)
{
ALLEGRO_VERTEX px;
px.x = p->x;
px.y = p->y;
px.z = 0;
px.color = al_map_rgb(p->r, p->g, p->b);
memcpy(&vtx[vtx_sz++], &px, sizeof(ALLEGRO_VERTEX));
}

/* 初始化一切需要的内容。
在这里主要是调用allegro库,初始化定时器、键盘和屏幕 */
void nes_hal_init() {
al_init();
al_init_primitives_addon();
al_install_keyboard();
al_create_display(SCREEN_WIDTH, SCREEN_HEIGHT);
void nes_hal_init()
{
al_init();
al_init_primitives_addon();
al_install_keyboard();
al_create_display(SCREEN_WIDTH, SCREEN_HEIGHT);

fce_timer = al_create_timer(1.0 / FPS);
fce_event_queue = al_create_event_queue();
al_register_event_source(fce_event_queue, al_get_timer_event_source(fce_timer));
al_start_timer(fce_timer);
fce_timer = al_create_timer(1.0 / FPS);
fce_event_queue = al_create_event_queue();
al_register_event_source(fce_event_queue, al_get_timer_event_source(fce_timer));
al_start_timer(fce_timer);
}

/* 这个函数每秒会被调用FPS次,每次调用时更新屏幕内容 */
void nes_flip_display() {
al_draw_prim(vtx, NULL, NULL, 0, vtx_sz, ALLEGRO_PRIM_POINT_LIST);
al_flip_display();
vtx_sz = 0;
void nes_flip_display()
{
al_draw_prim(vtx, NULL, NULL, 0, vtx_sz, ALLEGRO_PRIM_POINT_LIST);
al_flip_display();
vtx_sz = 0;
}

/* 询问编码为b的按键是否被按下(返回1为按下) */
int nes_key_state(int b) {
ALLEGRO_KEYBOARD_STATE state;
al_get_keyboard_state(&state);
switch (b) {
case 0: // On / Off
return 1;
case 1: // A
return al_key_down(&state, ALLEGRO_KEY_K);
case 2: // B
return al_key_down(&state, ALLEGRO_KEY_J);
case 3: // SELECT
return al_key_down(&state, ALLEGRO_KEY_U);
case 4: // START
return al_key_down(&state, ALLEGRO_KEY_I);
case 5: // UP
return al_key_down(&state, ALLEGRO_KEY_W);
case 6: // DOWN
return al_key_down(&state, ALLEGRO_KEY_S);
case 7: // LEFT
return al_key_down(&state, ALLEGRO_KEY_A);
case 8: // RIGHT
return al_key_down(&state, ALLEGRO_KEY_D);
default:
return 1;
}
int nes_key_state(int b)
{
ALLEGRO_KEYBOARD_STATE state;
al_get_keyboard_state(&state);
switch (b)
{
case 0: // On / Off
return 1;
case 1: // A
return al_key_down(&state, ALLEGRO_KEY_K);
case 2: // B
return al_key_down(&state, ALLEGRO_KEY_J);
case 3: // SELECT
return al_key_down(&state, ALLEGRO_KEY_U);
case 4: // START
return al_key_down(&state, ALLEGRO_KEY_I);
case 5: // UP
return al_key_down(&state, ALLEGRO_KEY_W);
case 6: // DOWN
return al_key_down(&state, ALLEGRO_KEY_S);
case 7: // LEFT
return al_key_down(&state, ALLEGRO_KEY_A);
case 8: // RIGHT
return al_key_down(&state, ALLEGRO_KEY_D);
default:
return 1;
}
}

31 changes: 16 additions & 15 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,23 @@ litenes来自开源项目mynes

static char rom[1048576];

int main(int argc, char *argv[]) {
if (argc != 2) {
fprintf(stderr, "Usage: mynes romfile.nes\n");
exit(1);
}
FILE *fp = fopen(argv[1], "r");
if (fp == NULL) {
fprintf(stderr, "Open rom file failed.\n");
exit(1);
}
fread(rom, sizeof(rom), 1, fp);
int main(int argc, char *argv[])
{
if (argc != 2) {
fprintf(stderr, "Usage: mynes romfile.nes\n");
exit(1);
}
FILE *fp = fopen(argv[1], "r");
if (fp == NULL) {
fprintf(stderr, "Open rom file failed.\n");
exit(1);
}
fread(rom, sizeof(rom), 1, fp);
if (fce_load_rom(rom) != 0) {
fprintf(stderr, "Invalid or unsupported rom.\n");
exit(1);
}
fprintf(stderr, "Invalid or unsupported rom.\n");
exit(1);
}
fce_init();
fce_run();
return 0;
return 0;
}

0 comments on commit 87fbc16

Please sign in to comment.