Modularize code

This commit is contained in:
Neale Pickett 2013-03-31 20:10:04 -06:00
parent 0c20cb9993
commit 679ee4d014
2 changed files with 9 additions and 74 deletions

View File

@ -21,6 +21,8 @@ upload: .upload
$(PROG).hex: $(PROG) $(PROG).hex: $(PROG)
avr-objcopy -O ihex -R .eeprom -R .fuse -R .lock -R .signature $< $@ avr-objcopy -O ihex -R .eeprom -R .fuse -R .lock -R .signature $< $@
main: main.o avr.o
clean: clean:
rm -f $(PROG) *.hex .upload rm -f $(PROG) *.hex .upload

81
main.c
View File

@ -1,8 +1,7 @@
#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdbool.h> #include <stdbool.h>
#include "avr.h"
// Number of shift registers in your scoreboard // Number of shift registers in your scoreboard
// If you want scores to go over 199, you need 8 // If you want scores to go over 199, you need 8
@ -11,9 +10,7 @@ const int nsr = 6;
// //
// Timing stuff // Timing stuff
// //
// Make sure JIFFY_uS is going to be an integer and not a float!
#define JIFFIES_PER_SECOND 50
#define JIFFY_uS (1000000 / JIFFIES_PER_SECOND)
volatile uint32_t jiffies = 0; volatile uint32_t jiffies = 0;
volatile bool tick = false; // Set high when jiffy clock ticks volatile bool tick = false; // Set high when jiffy clock ticks
@ -32,23 +29,6 @@ enum {
} state = SETUP; } state = SETUP;
#define cbi(byt, bit) (byt &= ~_BV(bit))
#define sbi(byt, bit) (byt |= _BV(bit))
#define MODE _BV(0)
#define SIN _BV(1)
#define SCLK _BV(2)
#define XLAT _BV(3)
// Connect GSCLK to SCLK
// Connect BLANK to XLAT
// TRUST ME, THIS TOTALLY WORKS
#define NESCLK _BV(4)
#define NESLTCH _BV(5)
#define NESSOUT _BV(6)
// NES Controller buttons // NES Controller buttons
#define BTN_A _BV(7) #define BTN_A _BV(7)
@ -71,13 +51,6 @@ const uint8_t setup_digits[] = {
0x1b, 0x12, 0x72 0x1b, 0x12, 0x72
}; };
const
#define mode(on) bit(PORTD, MODE, on)
#define sin(on) bit(PORTD, SIN, on)
#define sclk(on) bit(PORTD, SCLK, on)
#define xlat(on) bit(PORTD, XLAT, on)
void void
latch() latch()
{ {
@ -92,23 +65,6 @@ pulse()
sclk(false); sclk(false);
} }
volatile uint32_t micros = 0;
// Interrupt called every 1024 µs
SIGNAL(TIMER0_OVF_vect)
{
uint32_t m = micros;
m += 1024;
if (m >= JIFFY_uS) {
m %= JIFFY_uS;
tick = true;
jiffies += 1;
}
micros = m;
}
void void
write(uint8_t number) write(uint8_t number)
{ {
@ -189,18 +145,18 @@ nesprobe()
uint8_t ret = 0; uint8_t ret = 0;
static uint8_t last_controller = 0; static uint8_t last_controller = 0;
PORTD |= NESLTCH; nesltch(true);
PORTD &= ~NESLTCH; nesltch(false);
for (i = 0; i < 8; i += 1) { for (i = 0; i < 8; i += 1) {
state <<= 1; state <<= 1;
if (PIND & NESSOUT) { if (nesout()) {
// Button not pressed // Button not pressed
} else { } else {
state |= 1; state |= 1;
} }
PORTD |= NESCLK; nesclk(true);
PORTD &= ~NESCLK; nesclk(false);
} }
// Only report button down events. // Only report button down events.
@ -244,12 +200,6 @@ update_controller()
if (val & BTN_RIGHT) { if (val & BTN_RIGHT) {
score_b += inc; score_b += inc;
} }
if (val) {
PORTB = 0xff;
} else {
PORTB = 0;
}
} }
/* /*
@ -258,29 +208,12 @@ update_controller()
* *
*/ */
void
init(void)
{
// Set timer 0 interrupt clock divider to 64
TCCR0B = 0x03;
// enable timer 0 overflow interrupt
TIMSK0 = 0x01;
// Enable interrupts
sei();
}
void void
setup() setup()
{ {
int i; int i;
DDRD = ~(NESSOUT);
DDRB = 0xff;
PORTD = 0;
// Datasheet says you have to do this before DC initialization. // Datasheet says you have to do this before DC initialization.
// In practice it doesn't seem to matter, but what the hey. // In practice it doesn't seem to matter, but what the hey.
draw(); draw();