Controller tester working in emulator not on NES

Are you new to 6502, NES, or even programming in general? Post any of your questions here. Remember - the only dumb question is the question that remains unasked.

Moderator: Moderators

Post Reply
The Rook
Posts: 19
Joined: Mon Jul 13, 2015 8:04 am

Controller tester working in emulator not on NES

Post by The Rook » Fri Apr 03, 2020 9:34 pm

I wrote a simple controller test ROM using information from nesdoug.com and the "Making Games for the NES" book by Steven Hugg.

The ROM works fine in FCEUX but when I copy the ROM to my Everdrive N8 and try it on my Analogue NT, it freezes up after pressing a few buttons on the controller.

I'm using Visual Studio Code to write and compile my code. My project is setup like the examples from nesdoug.com. Same folder structures, same use of the compile.bat file. I use the nrom_32k_vert.cfg from nesdoug.com's 01_Hello example.

This is my first attempt at writing something for the NES. I'm not sure what I'm missing. Any help would be appreciated.

I've attached a recent build of the ROM and here is the code to my main file, ControllerTest.c:

Code: Select all

#include "LIB/neslib.h"
#include "ControllerTest.h"

#define BLACK 0x0f
#define DK_GY 0x00
#define LT_GY 0x10
#define WHITE 0x30
#define LT_BL 0x3C
#define RED   0x05

#define SPRITE_PAD_LR			0xB4
#define SPRITE_PAD_UD			0xB5
#define SPRITE_SELECT_START		0xB6
#define SPRITE_B_A				0xB7

#define H_FLIP	0x40
#define V_FLIP	0x80
#define HV_FLIP	0xC0

const unsigned char bg_palette[] = {
	BLACK, DK_GY, LT_GY, WHITE,			// background palette 0
	BLACK, DK_GY, LT_GY, LT_BL,			// background palette 1
	BLACK, DK_GY, LT_GY, RED,			// background palette 2
	BLACK, DK_GY, LT_GY, BLACK			// background palette 3
};

const unsigned char spr_palette[] = {
	BLACK, WHITE, WHITE, WHITE, 		// sprite palette 0
	BLACK, BLACK, BLACK, BLACK,			// sprite palette 1
	BLACK, BLACK, BLACK, BLACK,			// sprite palette 2
	BLACK, BLACK, BLACK, BLACK			// sprite palette 3
};

void draw_screen()
{
	vram_adr(NAMETABLE_A);
	vram_unrle(nametable_compressed);
	//vram_write(nametable, sizeof(nametable));
}

void setup_graphics()
{
	// Screen off.
	ppu_off();
	// Hide all sprites.
	oam_clear();
	// Set the palettes.
	pal_bg(bg_palette);
	pal_spr(spr_palette);
	// Screen on.
	ppu_on_all();
}

void main (void)
{
	char pad;			// Controller flags.
	unsigned char i;	// for loop index.
	unsigned char y;	// y offset of the sprites.

	// Draw screen.
	draw_screen();

  	// Setup graphics.
  	setup_graphics();

	while (1)
	{
		// Infinite loop.

		// Clear all sprites from sprite buffer.
		oam_clear();

		// Check the controllers.
		for (i = 0; i < 2; i++)
		{
			// Poll controller i (0-1)
			pad = pad_poll(i);

			// Update the y offset.
			y = i * 80;

			// Check the D-Pad.
			if (pad & PAD_LEFT) { oam_spr(37, 76 + y, SPRITE_PAD_LR, 0); }
			if (pad & PAD_UP) { oam_spr(44, 69 + y, SPRITE_PAD_UD, 0); }
			if (pad & PAD_RIGHT) { oam_spr(52, 76 + y, SPRITE_PAD_LR, 0); }
			if (pad & PAD_DOWN) { oam_spr(44, 84 + y, SPRITE_PAD_UD, 0); }
			
			// Check the Select and Start buttons
			if (pad & PAD_SELECT) 
			{ 
				oam_spr(68, 82 + y, SPRITE_SELECT_START, 0);
				oam_spr(76, 82 + y, SPRITE_SELECT_START, H_FLIP);
			}
			if (pad & PAD_START)
			{
				oam_spr(83, 82 + y, SPRITE_SELECT_START, 0);
				oam_spr(91, 82 + y, SPRITE_SELECT_START, H_FLIP);
			}
			
			// Check the B and A buttons.
			if (pad & PAD_B)
			{
				oam_spr(105, 78 + y, SPRITE_B_A, 0);
				oam_spr(113, 78 + y, SPRITE_B_A, H_FLIP);
				oam_spr(105, 86 + y, SPRITE_B_A, V_FLIP);
				oam_spr(113, 86 + y, SPRITE_B_A, HV_FLIP);
			}
			if (pad & PAD_A)
			{
				oam_spr(121, 78 + y, SPRITE_B_A, 0);
				oam_spr(129, 78 + y, SPRITE_B_A, H_FLIP);
				oam_spr(121, 86 + y, SPRITE_B_A, V_FLIP);
				oam_spr(129, 86 + y, SPRITE_B_A, HV_FLIP);
			}
		}

		// Wait till beginning of the frame.
		//ppu_wait_nmi(); 
		ppu_wait_frame();
	}
}
Attachments
Controller Test (2020-03-27).nes
(40.02 KiB) Downloaded 24 times

Post Reply