-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement NES controller input polling
- Loading branch information
Showing
7 changed files
with
236 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
#include "./controller.h" | ||
|
||
void create_controller(controller_t *controller) { | ||
controller->joy[0] = 0; | ||
controller->joy[1] = 0; | ||
|
||
controller->shift[0] = 0; | ||
controller->shift[1] = 0; | ||
|
||
controller->strobe = false; | ||
} | ||
|
||
void destroy_controller(controller_t *controller) {} | ||
|
||
void write_strobe_controller(controller_t *controller, unsigned char value) { | ||
controller->strobe = value & 1; | ||
if (controller->strobe) { | ||
controller->shift[0] = controller->joy[0]; | ||
controller->shift[1] = controller->joy[1]; | ||
} | ||
} | ||
|
||
unsigned char read_joy1_controller(controller_t *controller) { | ||
unsigned char result = controller->shift[0] & 1; | ||
controller->shift[0] >>= 1; | ||
controller->shift[0] |= 0x80; | ||
return result; | ||
} | ||
|
||
unsigned char read_joy2_controller(controller_t *controller) { | ||
unsigned char result = controller->shift[1] & 1; | ||
controller->shift[1] >>= 1; | ||
controller->shift[1] |= 0x80; | ||
return result; | ||
} | ||
|
||
void set_joy1_controller(controller_t *controller, | ||
bool a, | ||
bool b, | ||
bool select, | ||
bool start, | ||
bool up, | ||
bool down, | ||
bool left, | ||
bool right) { | ||
controller->joy[0] = 0; | ||
controller->joy[0] |= a; | ||
controller->joy[0] |= b << 1; | ||
controller->joy[0] |= select << 2; | ||
controller->joy[0] |= start << 3; | ||
controller->joy[0] |= up << 4; | ||
controller->joy[0] |= down << 5; | ||
controller->joy[0] |= left << 6; | ||
controller->joy[0] |= right << 7; | ||
} | ||
|
||
void set_joy2_controller(controller_t *controller, | ||
bool a, | ||
bool b, | ||
bool select, | ||
bool start, | ||
bool up, | ||
bool down, | ||
bool left, | ||
bool right) { | ||
controller->joy[0] = 0; | ||
controller->joy[1] |= a; | ||
controller->joy[1] |= b << 1; | ||
controller->joy[1] |= select << 2; | ||
controller->joy[1] |= start << 3; | ||
controller->joy[1] |= up << 4; | ||
controller->joy[1] |= down << 5; | ||
controller->joy[1] |= left << 6; | ||
controller->joy[1] |= right << 7; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
#ifndef CONTROLLER_H | ||
#define CONTROLLER_H | ||
|
||
#include <stdbool.h> | ||
|
||
/** | ||
* @brief NES input controller state. | ||
* | ||
*/ | ||
typedef struct { | ||
/** | ||
* @brief Button states of the controllers. | ||
* | ||
*/ | ||
unsigned char joy[2]; | ||
|
||
/** | ||
* @brief Shift registers for each controller. | ||
* | ||
*/ | ||
unsigned char shift[2]; | ||
|
||
/** | ||
* @brief Flag that determines if the controller input should be polled. | ||
* | ||
*/ | ||
bool strobe; | ||
} controller_t; | ||
|
||
/** | ||
* @brief Create a controller object. | ||
* | ||
* @param controller | ||
*/ | ||
void create_controller(controller_t *controller); | ||
|
||
/** | ||
* @brief Destroy a controller object. | ||
* | ||
* @param controller | ||
*/ | ||
void destroy_controller(controller_t *controller); | ||
|
||
/** | ||
* @brief Write to $4016. | ||
* | ||
* @param controller | ||
* @param strobe | ||
*/ | ||
void write_strobe_controller(controller_t *controller, unsigned char value); | ||
|
||
/** | ||
* @brief Read the button states of the first controller. | ||
* | ||
* @param controller | ||
* @return unsigned char | ||
*/ | ||
unsigned char read_joy1_controller(controller_t *controller); | ||
|
||
/** | ||
* @brief Read the button states of the second controller. | ||
* | ||
* @param controller | ||
* @return unsigned char | ||
*/ | ||
unsigned char read_joy2_controller(controller_t *controller); | ||
|
||
/** | ||
* @brief Set the button states of the first controller. | ||
* | ||
* @param controller | ||
* @param a | ||
* @param b | ||
* @param select | ||
* @param start | ||
* @param up | ||
* @param down | ||
* @param left | ||
* @param right | ||
*/ | ||
void set_joy1_controller(controller_t *controller, | ||
bool a, | ||
bool b, | ||
bool select, | ||
bool start, | ||
bool up, | ||
bool down, | ||
bool left, | ||
bool right); | ||
|
||
/** | ||
* @brief Set the button states of the second controller. | ||
* | ||
* @param controller | ||
* @param a | ||
* @param b | ||
* @param select | ||
* @param start | ||
* @param up | ||
* @param down | ||
* @param left | ||
* @param right | ||
*/ | ||
void set_joy2_controller(controller_t *controller, | ||
bool a, | ||
bool b, | ||
bool select, | ||
bool start, | ||
bool up, | ||
bool down, | ||
bool left, | ||
bool right); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters