Skip to content

Instantly share code, notes, and snippets.

@agrif
Created February 8, 2020 22:14
Show Gist options
  • Save agrif/3329d5591aff256077e2325ed0e3d49d to your computer and use it in GitHub Desktop.
Save agrif/3329d5591aff256077e2325ed0e3d49d to your computer and use it in GitHub Desktop.
#include <stdbool.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
__sfr __at 0x00 ioLED;
__sfr __at 0x10 ioID0;
__sfr __at 0x11 ioID1;
__sfr __at 0x12 ioID2;
__sfr __at 0x13 ioID3;
__sfr __at 0x14 ioID4;
__sfr __at 0x15 ioID5;
__sfr __at 0x16 ioID6;
__sfr __at 0x17 ioID7;
__sfr __at 0x20 ioSerialData;
__sfr __at 0x21 ioSerialRValid;
__sfr __at 0x22 ioSerialRAvailL;
__sfr __at 0x23 ioSerialRAvailH;
__sfr __at 0x24 ioSerialEnable;
__sfr __at 0x25 ioSerialInterrupt;
__sfr __at 0x26 ioSerialWSpaceL;
__sfr __at 0x27 ioSerialWSpaceH;
__sfr __at 0x30 ioKEY;
__sfr __at 0x38 ioKEYint;
__sfr __at 0x3c ioKEYedge;
uint8_t memID[8];
uint8_t interrupt_count[2];
// uart
uint8_t uart_read_next = 0;
uint8_t uart_read_avail = 0;
uint8_t uart_read[128];
void interrupt_1(void) __critical __interrupt(1) {
uint8_t data;
uint8_t valid;
interrupt_count[0] += 1;
while (true) {
data = ioSerialData;
valid = ioSerialRValid;
if (valid & (1 << 7)) {
if (uart_read_avail < 128) {
uint8_t i = uart_read_next + uart_read_avail;
i %= 128;
uart_read[i] = data;
uart_read_avail++;
}
} else {
break;
}
}
}
// key
uint8_t waitkey[2] = {0, 0};
void interrupt_2(void) __critical __interrupt(2) {
uint8_t data;
data = ioKEYedge;
ioKEYedge = 0;
interrupt_count[1] += 1;
if (data & 0b01)
waitkey[0] = 1;
if (data & 0b10)
waitkey[1] = 1;
}
void wait_for_key(int i) {
while (!waitkey[i]) {
__asm__("halt");
}
waitkey[i] = 0;
}
void putchar(char c) {
ioSerialData = c & 0xff;
}
char getchar(void) {
char c;
while (uart_read_avail == 0) {
__asm__("halt");
}
c = uart_read[uart_read_next];
uart_read_next += 1;
uart_read_next %= 128;
uart_read_avail--;
return c;
}
void sleep(uint8_t amt) {
while (amt > 0) {
uint16_t counter = 0xffff;
while (counter > 0)
counter--;
amt--;
}
}
void copy_system_id(void) {
memID[0] = ioID0;
memID[1] = ioID1;
memID[2] = ioID2;
memID[3] = ioID3;
memID[4] = ioID4;
memID[5] = ioID5;
memID[6] = ioID6;
memID[7] = ioID7;
}
void main() {
uint8_t flags;
interrupt_count[0] = 0;
interrupt_count[1] = 0;
copy_system_id();
flags = ioSerialEnable;
ioSerialEnable = flags | 1;
ioKEYint = 0b11;
__asm__("ei");
ioLED = 0;
while (true) {
wait_for_key(1);
ioLED += 1;
//printf("set LEDs to: 0x%02x", ioLED);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment