2013-02-26 22:49:02 -07:00
|
|
|
#include <stdint.h>
|
2013-03-17 23:42:17 -06:00
|
|
|
#include <stdlib.h>
|
2013-03-11 17:14:29 -06:00
|
|
|
#include <stdbool.h>
|
2013-04-01 21:17:18 -06:00
|
|
|
|
|
|
|
#include <avr/io.h>
|
|
|
|
#include <util/delay.h>
|
|
|
|
|
2013-06-30 14:57:44 -06:00
|
|
|
#include "avr.h"
|
2014-01-11 21:02:24 -07:00
|
|
|
#include "config.h"
|
2013-03-11 18:28:25 -06:00
|
|
|
|
2013-06-30 14:57:44 -06:00
|
|
|
//
|
2013-03-30 20:03:10 -06:00
|
|
|
// Timing stuff
|
2013-06-30 14:57:44 -06:00
|
|
|
//
|
|
|
|
|
|
|
|
//
|
|
|
|
// 2**32 deciseconds = 13.610221 years
|
|
|
|
//
|
|
|
|
// As long as you unplug your scoreboard once every 10 years or so,
|
|
|
|
// you're good.
|
|
|
|
//
|
2014-01-12 08:49:38 -07:00
|
|
|
volatile uint32_t jiffies = 0; // Elapsed time in deciseconds (tenths of a second)
|
2013-06-30 14:57:44 -06:00
|
|
|
volatile bool tick = false; // Set high when jiffy clock ticks
|
2013-03-31 20:10:04 -06:00
|
|
|
|
2013-03-11 23:14:30 -06:00
|
|
|
// Clocks are in deciseconds
|
2014-02-02 10:05:50 -07:00
|
|
|
int16_t score_a = 0;
|
|
|
|
int16_t score_b = 0;
|
2014-02-02 15:23:41 -07:00
|
|
|
int16_t period_clock = PERIOD_DEFAULT;
|
|
|
|
int16_t jam_duration = JAM_DEFAULT;
|
|
|
|
int16_t lineup_duration = LINEUP_DEFAULT;
|
|
|
|
int16_t jam_clock = JAM_DEFAULT;
|
2013-03-11 23:14:30 -06:00
|
|
|
enum {
|
2014-02-02 12:31:33 -07:00
|
|
|
TIMEOUT = 0,
|
2013-03-11 23:14:30 -06:00
|
|
|
JAM,
|
|
|
|
LINEUP,
|
2013-07-28 22:40:38 -06:00
|
|
|
KONAMI
|
2014-02-02 12:31:33 -07:00
|
|
|
} state = TIMEOUT;
|
|
|
|
bool setup = true;
|
2013-02-26 22:49:02 -07:00
|
|
|
|
2013-03-15 23:17:38 -06:00
|
|
|
|
2013-03-17 23:42:17 -06:00
|
|
|
// NES Controller buttons
|
|
|
|
|
2013-03-27 22:47:17 -06:00
|
|
|
#define BTN_A _BV(7)
|
|
|
|
#define BTN_B _BV(6)
|
|
|
|
#define BTN_SELECT _BV(5)
|
|
|
|
#define BTN_START _BV(4)
|
|
|
|
#define BTN_UP _BV(3)
|
|
|
|
#define BTN_DOWN _BV(2)
|
|
|
|
#define BTN_LEFT _BV(1)
|
|
|
|
#define BTN_RIGHT _BV(0)
|
2013-03-11 17:14:29 -06:00
|
|
|
|
2013-07-28 22:40:38 -06:00
|
|
|
const uint8_t konami_code[] = {
|
|
|
|
BTN_UP, BTN_UP, BTN_DOWN, BTN_DOWN,
|
|
|
|
BTN_LEFT, BTN_RIGHT, BTN_LEFT, BTN_RIGHT,
|
|
|
|
BTN_B, BTN_A,
|
|
|
|
0
|
|
|
|
};
|
|
|
|
int konami_pos = 0;
|
|
|
|
const uint8_t test_pattern[] = {
|
|
|
|
_BV(2), _BV(3), _BV(4), _BV(5), _BV(6), _BV(1), _BV(0), _BV(7)
|
|
|
|
};
|
|
|
|
|
2013-03-11 23:14:30 -06:00
|
|
|
const uint8_t seven_segment_digits[] = {
|
2014-01-11 21:02:24 -07:00
|
|
|
// 0 1 2 3 4 5 6 7 8 9
|
2013-03-30 20:03:10 -06:00
|
|
|
0x7b, 0x60, 0x37, 0x76, 0x6c, 0x5e, 0x5f, 0x70, 0x7f, 0x7e
|
|
|
|
};
|
|
|
|
|
2013-03-11 20:17:09 -06:00
|
|
|
|
2014-01-11 21:02:24 -07:00
|
|
|
// keyed by state
|
|
|
|
const uint8_t indicator[] = {
|
2014-02-02 15:23:41 -07:00
|
|
|
// t, J, L, -
|
|
|
|
0x0f, 0x63, 0x0b, 0x04
|
2014-01-11 21:02:24 -07:00
|
|
|
};
|
|
|
|
|
2014-02-02 10:05:50 -07:00
|
|
|
#define max(a, b) ((a)>(b)?(a):(b))
|
2014-02-02 15:23:41 -07:00
|
|
|
#define min(a, b) ((a)<(b)?(a):(b))
|
|
|
|
|
2014-02-02 10:05:50 -07:00
|
|
|
|
2013-03-11 17:14:29 -06:00
|
|
|
void
|
|
|
|
latch()
|
|
|
|
{
|
2013-07-28 21:42:25 -06:00
|
|
|
sltch(true);
|
|
|
|
sltch(false);
|
2013-03-11 17:14:29 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-03-11 18:28:25 -06:00
|
|
|
pulse()
|
|
|
|
{
|
|
|
|
sclk(true);
|
|
|
|
sclk(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
write(uint8_t number)
|
2013-03-11 17:14:29 -06:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int j;
|
2013-06-30 14:57:44 -06:00
|
|
|
|
2013-03-11 17:14:29 -06:00
|
|
|
// MSB first
|
2013-03-11 18:28:25 -06:00
|
|
|
for (i = 7; i >= 0; i -= 1) {
|
2013-03-11 17:14:29 -06:00
|
|
|
sin(number & (1 << i));
|
2013-07-28 21:42:25 -06:00
|
|
|
pulse();
|
2013-03-11 17:14:29 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-11 18:28:25 -06:00
|
|
|
void
|
|
|
|
write_num(uint16_t number, int digits)
|
|
|
|
{
|
|
|
|
int i;
|
2013-03-30 20:03:10 -06:00
|
|
|
|
2013-03-11 18:28:25 -06:00
|
|
|
for (i = 0; i < digits; i += 1) {
|
2013-07-18 22:46:55 -06:00
|
|
|
uint8_t out = seven_segment_digits[number % 10];
|
2013-07-28 21:42:25 -06:00
|
|
|
|
2013-07-15 20:56:29 -06:00
|
|
|
// Overflow indicator
|
2013-07-18 22:46:55 -06:00
|
|
|
if ((i == digits - 1) && (number > 9)) {
|
2013-07-28 21:42:25 -06:00
|
|
|
// Blink to indicate double-rollover
|
|
|
|
if ((number > 19) && (jiffies % 3 == 0)) {
|
|
|
|
// leave it blank
|
|
|
|
} else {
|
|
|
|
out ^= 0x80;
|
|
|
|
}
|
2013-07-15 20:56:29 -06:00
|
|
|
}
|
2013-06-30 14:57:44 -06:00
|
|
|
|
2013-07-15 20:56:29 -06:00
|
|
|
write(out);
|
2013-07-18 22:46:55 -06:00
|
|
|
number /= 10;
|
2013-03-11 18:28:25 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-12 11:05:18 -07:00
|
|
|
uint16_t
|
|
|
|
clock_of_jiffies(int16_t jiffies)
|
2013-03-11 23:14:30 -06:00
|
|
|
{
|
2014-01-12 11:05:18 -07:00
|
|
|
uint16_t seconds;
|
|
|
|
uint16_t ret;
|
2013-03-15 23:17:38 -06:00
|
|
|
|
2014-01-12 11:05:18 -07:00
|
|
|
// People read "0:00" as the time being out.
|
|
|
|
// Add 0.9 seconds to make the ALU's truncation be a "round up"
|
|
|
|
seconds = (abs(jiffies) + 9) / 10;
|
2013-06-30 14:57:44 -06:00
|
|
|
|
2014-01-12 11:05:18 -07:00
|
|
|
ret = (seconds / 60) * 100; // Minutes
|
|
|
|
ret += seconds % 60; // Seconds
|
2013-08-05 21:01:58 -06:00
|
|
|
|
2014-01-12 11:05:18 -07:00
|
|
|
return ret;
|
|
|
|
}
|
2014-01-11 15:59:48 -07:00
|
|
|
|
2014-01-12 11:25:54 -07:00
|
|
|
inline uint16_t
|
|
|
|
write_pclock()
|
|
|
|
{
|
|
|
|
uint16_t pclk = clock_of_jiffies(period_clock);
|
|
|
|
bool blank = ((state == TIMEOUT) && (jiffies % 8 == 0));
|
|
|
|
|
2013-07-28 21:42:25 -06:00
|
|
|
// Period clock
|
|
|
|
if (blank) {
|
|
|
|
write(0);
|
|
|
|
write(0);
|
|
|
|
write(0);
|
|
|
|
write(0);
|
2013-03-30 20:03:10 -06:00
|
|
|
} else {
|
2013-07-28 21:42:25 -06:00
|
|
|
write_num(pclk, 4);
|
2013-03-30 20:03:10 -06:00
|
|
|
}
|
2014-01-12 11:25:54 -07:00
|
|
|
}
|
2013-06-30 14:57:44 -06:00
|
|
|
|
2013-07-28 21:42:25 -06:00
|
|
|
|
2013-03-11 23:14:30 -06:00
|
|
|
/*
|
|
|
|
* Update all the digits
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
draw()
|
|
|
|
{
|
2014-01-12 11:05:18 -07:00
|
|
|
uint16_t jclk = clock_of_jiffies(jam_clock);
|
2014-01-11 21:54:50 -07:00
|
|
|
|
2013-07-28 22:40:38 -06:00
|
|
|
// Segments test mode
|
2014-01-12 11:05:18 -07:00
|
|
|
if (KONAMI == state) {
|
2013-07-28 21:42:25 -06:00
|
|
|
int i;
|
2014-01-12 08:49:38 -07:00
|
|
|
|
2013-07-28 21:42:25 -06:00
|
|
|
for (i = 0; i < 12; i += 1) {
|
2014-01-12 08:49:38 -07:00
|
|
|
write(test_pattern[jiffies % (sizeof test_pattern)]);
|
2013-04-03 21:52:46 -06:00
|
|
|
}
|
2014-01-12 08:49:38 -07:00
|
|
|
|
2013-07-28 22:40:38 -06:00
|
|
|
latch();
|
|
|
|
pulse();
|
|
|
|
return;
|
2013-04-03 21:52:46 -06:00
|
|
|
}
|
2013-07-28 21:42:25 -06:00
|
|
|
|
2014-01-11 21:02:24 -07:00
|
|
|
write_num(score_b, SCORE_DIGITS);
|
2013-06-30 14:57:44 -06:00
|
|
|
|
2014-01-12 11:25:54 -07:00
|
|
|
write_num(jclk, 2);
|
|
|
|
#ifdef JAM_SPLIT
|
|
|
|
write_pclock();
|
|
|
|
#endif
|
2014-01-11 21:02:24 -07:00
|
|
|
write_num(jclk / 100, JAM_DIGITS - 2);
|
2014-01-12 11:25:54 -07:00
|
|
|
#ifdef JAM_INDICATOR
|
2014-01-11 21:02:24 -07:00
|
|
|
write(indicator[state]);
|
|
|
|
#endif
|
2013-07-28 21:42:25 -06:00
|
|
|
|
2014-01-12 11:25:54 -07:00
|
|
|
#ifndef JAM_SPLIT
|
|
|
|
write_pclock();
|
|
|
|
#endif
|
|
|
|
|
2014-01-11 21:02:24 -07:00
|
|
|
write_num(score_a, SCORE_DIGITS);
|
2013-07-28 21:42:25 -06:00
|
|
|
|
|
|
|
// Tell chips to start displaying new values
|
2013-03-11 23:14:30 -06:00
|
|
|
latch();
|
|
|
|
pulse();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2013-03-15 23:17:38 -06:00
|
|
|
* Probe the NES controller
|
2013-03-11 23:14:30 -06:00
|
|
|
*/
|
2013-03-15 23:17:38 -06:00
|
|
|
uint8_t
|
|
|
|
nesprobe()
|
2013-03-11 23:14:30 -06:00
|
|
|
{
|
2013-03-15 23:17:38 -06:00
|
|
|
int i;
|
|
|
|
uint8_t state = 0;
|
|
|
|
|
2013-03-31 20:10:04 -06:00
|
|
|
nesltch(true);
|
|
|
|
nesltch(false);
|
2013-06-30 14:57:44 -06:00
|
|
|
|
2013-03-15 23:17:38 -06:00
|
|
|
for (i = 0; i < 8; i += 1) {
|
|
|
|
state <<= 1;
|
2013-03-31 20:10:04 -06:00
|
|
|
if (nesout()) {
|
2013-03-15 23:17:38 -06:00
|
|
|
// Button not pressed
|
|
|
|
} else {
|
|
|
|
state |= 1;
|
|
|
|
}
|
2013-03-31 20:10:04 -06:00
|
|
|
nesclk(true);
|
|
|
|
nesclk(false);
|
2013-03-11 23:14:30 -06:00
|
|
|
}
|
2013-06-30 14:57:44 -06:00
|
|
|
|
2013-03-17 23:42:17 -06:00
|
|
|
// Only report button down events.
|
2013-03-31 21:54:31 -06:00
|
|
|
return state;
|
2013-03-15 23:17:38 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
update_controller()
|
|
|
|
{
|
2014-02-02 15:23:41 -07:00
|
|
|
static uint8_t last_held = 0;
|
2013-07-28 22:40:38 -06:00
|
|
|
static uint32_t last_change = 0;
|
2014-02-02 15:23:41 -07:00
|
|
|
static uint32_t last_typematic = 0;
|
2014-02-02 12:31:33 -07:00
|
|
|
uint8_t held;
|
2013-03-31 21:54:31 -06:00
|
|
|
uint8_t pressed;
|
2014-02-02 15:23:41 -07:00
|
|
|
int typematic = 0;
|
2013-03-30 20:03:10 -06:00
|
|
|
int inc = 1;
|
2013-06-30 14:57:44 -06:00
|
|
|
|
2014-02-02 12:31:33 -07:00
|
|
|
held = nesprobe();
|
2014-02-02 15:23:41 -07:00
|
|
|
pressed = (last_held ^ held) & held;
|
|
|
|
|
|
|
|
// Set up typematic acceleration
|
|
|
|
if (last_held != held) {
|
|
|
|
// Debounce
|
|
|
|
if (last_change == jiffies) {
|
|
|
|
return;
|
|
|
|
}
|
2013-07-02 22:31:54 -06:00
|
|
|
last_change = jiffies;
|
2014-02-02 15:23:41 -07:00
|
|
|
last_typematic = jiffies;
|
|
|
|
last_held = held;
|
|
|
|
typematic = 1;
|
|
|
|
} else if (jiffies > last_typematic) {
|
|
|
|
last_typematic = jiffies;
|
|
|
|
if (jiffies - last_change < 6) {
|
|
|
|
typematic = 0;
|
|
|
|
} else if (jiffies - last_change < 40) {
|
|
|
|
typematic = 1;
|
|
|
|
} else if (jiffies - last_change < 80) {
|
|
|
|
typematic = 10;
|
|
|
|
} else {
|
|
|
|
typematic = 20;
|
|
|
|
}
|
|
|
|
}
|
2014-02-02 12:31:33 -07:00
|
|
|
|
2013-07-28 22:40:38 -06:00
|
|
|
if (pressed == konami_code[konami_pos]) {
|
|
|
|
konami_pos += 1;
|
2014-01-12 08:49:38 -07:00
|
|
|
|
2013-07-28 22:40:38 -06:00
|
|
|
if (konami_code[konami_pos] == 0) {
|
|
|
|
state = KONAMI;
|
|
|
|
konami_pos = 0;
|
|
|
|
return;
|
|
|
|
} else if (konami_pos > 3) {
|
|
|
|
return;
|
|
|
|
}
|
2014-02-02 15:23:41 -07:00
|
|
|
} else if (pressed) {
|
2014-02-02 10:01:48 -07:00
|
|
|
konami_pos = 0;
|
2013-07-28 22:40:38 -06:00
|
|
|
}
|
2013-07-28 21:42:25 -06:00
|
|
|
// Select means subtract
|
2014-02-02 12:31:33 -07:00
|
|
|
if (held & BTN_SELECT) {
|
2013-07-28 21:42:25 -06:00
|
|
|
inc = -1;
|
|
|
|
}
|
2014-02-02 15:23:41 -07:00
|
|
|
|
|
|
|
if (setup && (held & BTN_START) && (pressed & BTN_SELECT)) {
|
|
|
|
jam_duration += 30 * 10;
|
|
|
|
if (jam_duration > -60 * 10) {
|
|
|
|
jam_duration = -120 * 10;
|
|
|
|
}
|
|
|
|
jam_clock = jam_duration;
|
|
|
|
}
|
2013-03-15 23:17:38 -06:00
|
|
|
|
2013-03-31 21:54:31 -06:00
|
|
|
if ((pressed & BTN_A) && ((state != JAM) || (jam_clock == 0))) {
|
2013-03-30 20:03:10 -06:00
|
|
|
state = JAM;
|
2013-03-31 16:25:43 -06:00
|
|
|
jam_clock = jam_duration;
|
2013-03-11 23:14:30 -06:00
|
|
|
}
|
2013-06-30 14:57:44 -06:00
|
|
|
|
2013-03-31 21:54:31 -06:00
|
|
|
if ((pressed & BTN_B) && ((state != LINEUP) || (jam_clock == 0))) {
|
2013-06-30 14:57:44 -06:00
|
|
|
state = LINEUP;
|
2013-03-31 16:25:43 -06:00
|
|
|
jam_clock = lineup_duration;
|
2013-03-30 20:03:10 -06:00
|
|
|
}
|
|
|
|
|
2013-03-31 21:54:31 -06:00
|
|
|
if ((pressed & BTN_START) && (state != TIMEOUT)) {
|
2013-03-30 20:03:10 -06:00
|
|
|
state = TIMEOUT;
|
|
|
|
jam_clock = 1;
|
|
|
|
}
|
2013-06-30 14:57:44 -06:00
|
|
|
|
2014-02-02 12:31:33 -07:00
|
|
|
if ((held & BTN_START) && (state == TIMEOUT)) {
|
|
|
|
if (held & BTN_UP) {
|
2014-02-02 15:23:41 -07:00
|
|
|
period_clock -= typematic * 10;
|
2013-07-02 22:31:54 -06:00
|
|
|
}
|
2014-02-02 12:31:33 -07:00
|
|
|
if (held & BTN_DOWN) {
|
2014-02-02 15:23:41 -07:00
|
|
|
period_clock += typematic * 10;
|
2014-02-02 12:31:33 -07:00
|
|
|
}
|
2014-02-02 15:23:41 -07:00
|
|
|
|
|
|
|
period_clock = min(period_clock, 0);
|
|
|
|
period_clock = max(period_clock, -90 * 30 * 10);
|
2014-02-02 12:31:33 -07:00
|
|
|
} else {
|
2014-02-02 15:23:41 -07:00
|
|
|
// Score adjustment and clock adjustment are mutually exclusive
|
|
|
|
if (held & BTN_LEFT) {
|
|
|
|
score_a += typematic * inc;
|
|
|
|
score_a = max(score_a, 0);
|
2014-02-02 12:31:33 -07:00
|
|
|
}
|
|
|
|
|
2014-02-02 15:23:41 -07:00
|
|
|
if (held & BTN_RIGHT) {
|
|
|
|
score_b += typematic * inc;
|
|
|
|
score_b = max(score_b, 0);
|
2013-07-02 22:31:54 -06:00
|
|
|
}
|
2013-03-17 23:42:17 -06:00
|
|
|
}
|
2014-02-02 12:31:33 -07:00
|
|
|
|
|
|
|
if (state != TIMEOUT) {
|
|
|
|
setup = false;
|
2013-03-30 20:03:10 -06:00
|
|
|
}
|
2013-03-15 23:17:38 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2013-03-30 20:03:10 -06:00
|
|
|
*
|
|
|
|
* Main program
|
|
|
|
*
|
2013-03-15 23:17:38 -06:00
|
|
|
*/
|
2013-03-30 20:03:10 -06:00
|
|
|
|
2013-03-11 23:14:30 -06:00
|
|
|
|
2013-03-30 20:03:10 -06:00
|
|
|
void
|
|
|
|
loop()
|
|
|
|
{
|
2013-07-28 21:42:25 -06:00
|
|
|
update_controller();
|
2013-03-30 20:03:10 -06:00
|
|
|
|
|
|
|
if (tick) {
|
|
|
|
tick = false;
|
2013-03-15 23:17:38 -06:00
|
|
|
|
2013-07-28 21:42:25 -06:00
|
|
|
if (jiffies % 5 == 0) {
|
2013-05-04 20:39:31 -06:00
|
|
|
PORTB ^= 0xff;
|
|
|
|
}
|
2013-06-30 14:57:44 -06:00
|
|
|
|
2013-07-02 19:54:39 -06:00
|
|
|
switch (state) {
|
2013-07-28 21:42:25 -06:00
|
|
|
case JAM:
|
|
|
|
case LINEUP:
|
|
|
|
if (period_clock) {
|
|
|
|
period_clock += 1;
|
|
|
|
}
|
|
|
|
// fall through
|
|
|
|
case TIMEOUT:
|
2014-02-02 12:31:33 -07:00
|
|
|
if (jam_clock && !setup) {
|
2013-07-28 21:42:25 -06:00
|
|
|
jam_clock += 1;
|
|
|
|
}
|
2013-03-07 23:55:05 -07:00
|
|
|
}
|
2013-02-26 22:49:02 -07:00
|
|
|
}
|
2013-07-28 21:42:25 -06:00
|
|
|
|
|
|
|
draw();
|
2013-02-26 22:49:02 -07:00
|
|
|
}
|
|
|
|
|
2013-03-30 20:03:10 -06:00
|
|
|
int
|
|
|
|
main(void)
|
2013-03-07 23:55:05 -07:00
|
|
|
{
|
2014-02-02 12:31:33 -07:00
|
|
|
avr_init();
|
2013-03-30 20:03:10 -06:00
|
|
|
for (;;) {
|
|
|
|
loop();
|
2013-03-29 22:39:44 -06:00
|
|
|
}
|
2013-03-30 20:03:10 -06:00
|
|
|
return 0;
|
2013-03-07 23:55:05 -07:00
|
|
|
}
|