Browse Source

Added files

Signed-off-by: Tyrolyean <tyrolyean@tyrolyean.net>
master
Tyrolyean 2 years ago
parent
commit
5aabef4c9d
No known key found for this signature in database
GPG Key ID: 81EC9BAC5E9667C6
  1. 53
      .gitignore
  2. 70
      Makefile
  3. 54
      include/16550.h
  4. 49
      include/avr.h
  5. 45
      include/dac.h
  6. 51
      include/game.h
  7. 22
      include/interrupt.h
  8. 47
      include/sound.h
  9. 81
      include/structures.h
  10. 126
      src/16550.c
  11. 207
      src/dac.c
  12. 378
      src/game.c
  13. 36
      src/interrupt.c
  14. 112
      src/main.c
  15. 261
      src/sound.c
  16. 480
      src/structures.c

53
.gitignore vendored

@ -0,0 +1,53 @@
# Prerequisites
*.d
# Object files
*.o
*.ko
*.obj
*.elf
# Linker output
*.ilk
*.map
*.exp
# Precompiled Headers
*.gch
*.pch
# Libraries
*.lib
*.a
*.la
*.lo
# Shared objects (inc. Windows DLLs)
*.dll
*.so
*.so.*
*.dylib
# Executables
*.exe
*.out
*.app
*.i*86
*.x86_64
*.hex
# Debug files
*.dSYM/
*.su
*.idb
*.pdb
# Kernel Module Compile Results
*.mod*
*.cmd
.tmp_versions/
modules.order
Module.symvers
Mkfile.old
dkms.conf

70
Makefile

@ -0,0 +1,70 @@
MKDIR_P := mkdir -p
CP := cp
MV := mv
CC := avr-gcc
CCC := avr-g++
RM_RF = rm -rf
OCPY := avr-objcopy
AVRDUDE := avrdude
PORT := /dev/ttyACM0
#PORT := usb
#BOARD := atmega328p
BOARD := atmega2560
#PROGRAMMER := arduino
PROGRAMMER := usbasp
#PROGBOARD := m328p
PROGBOARD := m2560
BAUD_RATE_PROG := 115200
FLASH_CMD := $(AVRDUDE) -b $(BAUD_RATE_PROG) -p $(PROGBOARD) -D -P $(PORT) -c $(PROGRAMMER)
AVRSIZE := avr-size
AVRSIZE_FLAGS := -C --mcu=$(PROGBOARD)
# directories
CWD := $(realpath .)
BINDIR := $(CWD)/bin
BUILDDIR := $(CWD)/build
SRCDIR := $(CWD)/src
INCLUDEDIR := $(CWD)/include
# flas
CFLAGS := -mmcu=$(BOARD) -Os -I$(INCLUDEDIR) -Wall -Wextra
LDFLAGS := -mmcu=$(BOARD)
# target files
DIRS_TARGET := $(BINDIR) $(BUILDDIR)
TARGET := $(BINDIR)/mc.hex
TARGET_ELF := $(BINDIR)/mc.elf
SRCFILES := $(wildcard $(SRCDIR)/*.c)
OBJFILES := $(patsubst $(SRCDIR)/%.c,$(BUILDDIR)/%.o,$(SRCFILES))
# fancy targets
all: directories $(TARGET)
directories: $(DIRS_TARGET)
# less fancy targets
$(DIRS_TARGET):
$(MKDIR_P) $@
$(TARGET) : $(TARGET_ELF)
$(OCPY) -O ihex -j .text -j .data $^ $@
$(TARGET_ELF): $(OBJFILES)
$(CC) $(LDFLAGS) -o $@ $^
$(AVRSIZE) $(AVRSIZE_FLAGS) $@
$(BUILDDIR)/%.o: $(SRCDIR)/%.c
$(CC) $(CFLAGS) -c -o $@ $<
flash: $(TARGET)
# For atmega 2560
$(FLASH_CMD) -e -U hfuse:w:0xD9:m -U lfuse:w:0xDF:m -U efuse:w:0xFF:m -U flash:w:$^:i -U efuse:w:0xfa:m
# For atmega 328p:
#$(FLASH_CMD) -e -U hfuse:w:0xD9:m -U efuse:w:0xFF:m -U lfuse:w:0xDF:m -U flash:w:$^:i -U efuse:w:0xfd:m
# If unknown or no fuse bits available over programmer
#$(FLASH_CMD) -U flash:w:$^:i
$(FLASH_CMD) -U flash:v:$^:i
clean:
$(RM_RF) $(DIRS_TARGET)

54
include/16550.h

@ -0,0 +1,54 @@
/* Copyright © 2020 tyrolyean
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _16550_H
#define _16550_H
#include <stdint.h>
#include <stdio.h>
/* Registers in the 16550 UART */
#define UART_REG_DLLS 0
#define UART_REG_DLMS 1
#define UART_REG_TXRX 0
#define UART_REG_IER 1
#define UART_REG_IIR 2
#define UART_REG_LCR 3
#define UART_REG_MCR 4
#define UART_REG_LSR 5
#define UART_REG_MSR 6
#define UART_REG_SCR 7
/* The printf function in the background performs automatic column wrapping by
* finding the first space character within 10 characters ofthe display columns
* and replacingit with a newline character
*/
#define DISPLAY_COLUMNS 80
void init_16550();
void write_to_uart(uint8_t addr, uint8_t data);
uint8_t read_from_uart(uint8_t addr);
void init_uart();
int println(const char* output,...);
int putchar_16550(char var, FILE *stream);
void writechar_16550(char var);
void routine_uart();
#endif

49
include/avr.h

@ -0,0 +1,49 @@
/* Copyright (C) 2020 tyrolyean
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _AVR_H_TEXT
#define _AVR_H_TEXT
#define F_CPU 16000000UL
#include <avr/io.h>
/* Shift values for the peripherials on the control bus PORTL */
#define MR_SHIFT 0
#define WR_SHIFT 1
#define RD_SHIFT 2
#define CS_UART_SHIFT 3
#define CS_DAC_SHIFT 4
#define ADDR_REG PORTK
#define DATA_REG PORTF
#define CTRL_REG PORTL
#define ADDR_DDR_REG DDRK
#define DATA_DDR_REG DDRF
#define CTRL_DDR_REG DDRL
/* Included here to prevent accidental redefinition of F_CPU */
#include <util/delay.h>
/* Time it takes for the bus lanes to become stable for read and write access */
#define BUS_HOLD_US 1
void set_addr(uint8_t addr);
#endif

45
include/dac.h

@ -0,0 +1,45 @@
/* Copyright © 2020 tyrolyean
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _DAC_H
#define _DAC_H
#include <stdint.h>
void write_to_dac(uint8_t addr, uint8_t data);
uint8_t read_from_dac(uint8_t addr);
/* The operation modes of the dac used for generation of different tones */
#define DAC_MODE_SILENT 0
#define DAC_MODE_SINE 1
#define DAC_MODE_SQUARE 2
#define DAC_MODE_SAW 3
#define DAC_MODE_NOISE 4
#define DAC_MODE_TRIANGLE 5
extern uint8_t dac_mode;
/* This variable is used to deviate the frequency from the baseline frequency
* of around 1kHz. If this integer is positive it makes the produced waveform
* longer, if it is negative the produced waveform becomes less sharp, but the
* frequency goes up. 0 is the baseline */
extern int16_t dac_frequency_deviation;
void feed_dac();
void routine_dac();
void update_sound();
#endif

51
include/game.h

@ -0,0 +1,51 @@
/* Copyright © 2020 tyrolyean
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _GAME_H
#define _GAME_H
#include <stdint.h>
#include <stdbool.h>
extern char command_buffer[100];
extern uint8_t command_buffer_pointer;
#define COMPUTER_STATE_NOTHING 0
#define COMPUTER_STATE_KEYBOARD 1
#define COMPUTER_STATE_FLOPPY 2
extern uint8_t computer_state; /* FSM for the computer */
extern bool bear_shot;
extern uint8_t current_room;
void routine_game();
void prepare_command_buffer();
void ingest_user_char(char in);
void perform_action(uint8_t action_id);
void move_direction(uint8_t direction);
void describe_room(uint8_t room, bool auto_desc);
void print_inventory();
void print_room_item();
void consume_room_item(const char* item_name);
void use_item(const char* item_name);
void use_item_id(uint8_t item_id);
bool perform_computer_action(uint8_t action_id);
void reset_game(bool fleshed);
void init_game();
#endif

22
include/interrupt.h

@ -0,0 +1,22 @@
/* Copyright © 2020 tyrolyean
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _INTERRUPT_H
#define _INTERRUPT_H
void init_interrupts();
#endif

47
include/sound.h

@ -0,0 +1,47 @@
/* Copyright © 2020 tyrolyean
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _SOUND_H
#define _SOUND_H
#include <stdint.h>
#include <avr/pgmspace.h>
struct tone_t {
uint8_t waveform;
int16_t frequency_deviation; /* */
uint16_t length; /* The tone length in milliseconds */
};
/* The end marker tells the sound routines where the end of a track is. It
* is required to be present at the end of EVERY track, otherwise the
* routine runs into undefined behaviour */
#define TRACK_END_MARKER {DAC_MODE_SILENT, 0, 0}
extern const struct tone_t computer_room_track[] PROGMEM;
extern const struct tone_t lonely_road_track[] PROGMEM;
#define INTRO_LEN 10350
extern const struct tone_t intro_track[] PROGMEM;
extern const struct tone_t * current_track;
extern int16_t global_frequency_offset; /* Added to every playing song */
void update_sound();
void init_sound();
#endif

81
include/structures.h

@ -0,0 +1,81 @@
/* Copyright © 2020 tyrolyean
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _STRUCTURES_H
#define _STRUCTURES_H
#include <sound.h>
#include <stdint.h>
#include <avr/pgmspace.h>
#include <stdbool.h>
#define NUM_ACTIONS 0x0A
#define ACTION_HELP 0x00
#define ACTION_NORTH 0x01
#define ACTION_SOUTH 0x02
#define ACTION_WEST 0x03
#define ACTION_EAST 0x04
#define ACTION_DESCRIBE 0x05
#define ACTION_USE 0x06
#define ACTION_INVENTORY 0x07
#define ACTION_SEARCH 0x08
#define ACTION_TAKE 0x09
/* The direction in the direction table is the action_id - 1 */
#define DIRECTION_NORTH 0x00
#define DIRECTION_SOUTH 0x01
#define DIRECTION_WEST 0x02
#define DIRECTION_EAST 0x03
#define NUM_ROOMS 0x0A
#define ROOM_NOTHING 0x00
#define ROOM_LONELYROAD 0x01
#define ROOM_SNDIRTROAD 0x02
#define ROOM_FIREPLACE 0x03
#define ROOM_EWSTREET 0x04
#define ROOM_OLDHOUSE 0x05
#define ROOM_LIVINGROOM 0x06
#define ROOM_ATTIC 0x07
#define ROOM_BASEMENT 0x08
#define ROOM_COMPUTER 0x09
#define NUM_ITEMS 0x07
#define ITEM_FLOPPY 0x00
#define ITEM_SCREWDRIVER 0x01
#define ITEM_KEYBOARD 0x02
#define ITEM_SAUSAGE 0x03
#define ITEM_FLESH 0x04
#define ITEM_KEY 0x05
#define ITEM_PISTOL 0x06
extern const uint8_t sine_table[256] PROGMEM;
extern const uint8_t noise_table[1024] PROGMEM;
extern char* sndirtroad_msg[2];
extern char* user_action_req_msgs[3];
extern const char* info_table[];
extern const char* action_table[NUM_ACTIONS];
extern const char* room_table[NUM_ROOMS];
extern char* room_description_table[NUM_ROOMS];
extern uint8_t room_map_table[NUM_ROOMS][4];
extern bool room_visited_table[NUM_ROOMS];
extern const char* item_table[NUM_ITEMS];
extern bool inventory[NUM_ITEMS];
extern int8_t item_room_map[NUM_ROOMS];
extern const struct tone_t * room_track_map[NUM_ROOMS];
#endif

126
src/16550.c

@ -0,0 +1,126 @@
/* Copyright © 2020 tyrolyean
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "avr.h"
#include "16550.h"
#include "structures.h"
#include "game.h"
#include <stdio.h>
static FILE stdout_16550 = FDEV_SETUP_STREAM(putchar_16550, NULL,
_FDEV_SETUP_WRITE);
void write_to_uart(uint8_t addr, uint8_t data){
set_addr(addr);
DATA_DDR_REG = 0xFF;
DATA_REG = data;
CTRL_REG &= ~(1<<WR_SHIFT) & ~(1<<CS_UART_SHIFT);
_delay_us(BUS_HOLD_US); /*Wait for the data and signal lanes to become stable*/
CTRL_REG |= 1<<CS_UART_SHIFT | 1<<WR_SHIFT;
return;
}
uint8_t read_from_uart(uint8_t addr){
uint8_t data = 0x00;
set_addr(addr);
DATA_DDR_REG = 0x00;
/* Sometimes the avr "forgets" to alter the port register for one cycle
* which usually isn't a problem but has lead to instability. Worthy
* one clock cycle */
DATA_REG = 0x00;
CTRL_REG &= ~(1<<RD_SHIFT) & ~(1<<CS_UART_SHIFT);
_delay_us(BUS_HOLD_US); /* Wait for the data and signal lanes to become
* stable*/
data = PINF;
CTRL_REG |= 1<<CS_UART_SHIFT | 1<<RD_SHIFT;
return data;
}
void init_uart(){
write_to_uart(UART_REG_LCR,0x83);
write_to_uart(UART_REG_DLLS,0x03);
write_to_uart(UART_REG_DLMS,0x00);
write_to_uart(UART_REG_LCR,0x03);
stdout = &stdout_16550;
printf("INIT\r\n");
return;
}
int println(const char* output, ...){
va_list arg;
va_start (arg, output);
vprintf(output,arg);
printf("\n");
return 0;
}
/* Writes a character to the 16550 uart for transmission with cr lf magic */
int putchar_16550(char var, FILE *stream __attribute__((unused))){
static uint8_t column_cnt = 0;
column_cnt++;
if(var == ' ' && column_cnt > (DISPLAY_COLUMNS - 10)){
putchar_16550('\n',NULL);
return 0;
}
if(var == '\n'){
column_cnt = 0x00;
writechar_16550('\r');
}
writechar_16550(var);
return 0;
}
/* Writes a character to the 16550 uart for transmission without cr lf magic */
void writechar_16550(char var){
for(uint32_t i = 0; i < 1000000; i++ ){
uint8_t received = read_from_uart(UART_REG_LSR);
if((received & (1<<5))){
break;
}
}
write_to_uart(UART_REG_TXRX,var);
return;
}
void routine_uart(){
uint8_t received = read_from_uart(UART_REG_LSR);
if(received & 0x01){
received = read_from_uart(UART_REG_TXRX);
ingest_user_char(received);
if(received == '\r'){
writechar_16550('\n');
}
writechar_16550(received); /* Echo back */
}
return;
}

207
src/dac.c

@ -0,0 +1,207 @@
/* Copyright © 2020 tyrolyean
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "avr.h"
#include "dac.h"
#include "structures.h"
#include <stdbool.h>
uint8_t dac_mode = DAC_MODE_SINE;
int16_t dac_frequency_deviation = 0;
void write_to_dac(uint8_t addr, uint8_t data){
set_addr(addr);
DATA_DDR_REG = 0xFF;
DATA_REG = data;
CTRL_REG &= ~(1<<WR_SHIFT) & ~(1<<CS_DAC_SHIFT);
_delay_us(BUS_HOLD_US); /*Wait for the data and signal lanes to become stable*/
CTRL_REG |= 1<<CS_DAC_SHIFT;
CTRL_REG |= 1<<WR_SHIFT;
return;
}
uint8_t read_from_dac(uint8_t addr){
uint8_t data = 0x00;
set_addr(addr);
DATA_DDR_REG = 0x00;
/* Sometimes the avr "forgets" to alter the port register for one cycle
* which usually isn't a problem but has lead to instability. Worthy
* one clock cycle */
DATA_REG = 0x00;
CTRL_REG &= ~(1<<RD_SHIFT) & ~(1<<CS_DAC_SHIFT);
_delay_us(BUS_HOLD_US); /* Wait for the data and signal lanes to become
* stable*/
data = PINF;
CTRL_REG |= 1<<CS_DAC_SHIFT | 1<<RD_SHIFT;
return data;
}
void feed_dac(){
/* Internal counter for positioning inside the currently playing
* waveform */
static uint8_t threash = 0x00;
/* Used to generate the desired frequency offset if the waveform should
* be made "longer" --> the frequency made lower from baseline
*/
static int16_t freq_delay_cnt = 0x00;
switch(dac_mode){
default:
case DAC_MODE_SILENT:
for(uint8_t i = 0; i < 0xFF; i++){
write_to_dac(i%2, 0);
}
break;
case DAC_MODE_SINE:
/* Generates a sine from a predetermined sine table in program
* space */
for(uint8_t i = 0; i < (0xFF/2); i++){
write_to_dac(1,
pgm_read_byte(&sine_table[threash]));
write_to_dac(0,
pgm_read_byte(&sine_table[threash]));
if(dac_frequency_deviation >=0){
freq_delay_cnt++;
if(freq_delay_cnt >=
dac_frequency_deviation){
freq_delay_cnt = 0;
threash++;
}
}else{
threash -= dac_frequency_deviation;
}
}
break;
case DAC_MODE_SQUARE:
/* Generates a square wave tone */
for(uint8_t i = 0; i < (0xFF/2); i++){
if(threash > (0xFF/2)){
write_to_dac(0, 0xFF);
write_to_dac(1, 0xFF);
}else{
write_to_dac(0, 0);
write_to_dac(1, 0);
}
if(dac_frequency_deviation >=0){
freq_delay_cnt++;
if(freq_delay_cnt >=
dac_frequency_deviation){
freq_delay_cnt = 0;
threash++;
}
}else{
threash -= dac_frequency_deviation;
}
}
break;
case DAC_MODE_SAW:
/* Generates a saw wave tone */
for(uint8_t i = 0; i < (0xFF/2); i++){
write_to_dac(0, threash);
write_to_dac(1, threash);
if(dac_frequency_deviation >=0){
freq_delay_cnt++;
if(freq_delay_cnt >=
dac_frequency_deviation){
freq_delay_cnt = 0;
threash++;
}
}else{
threash -= dac_frequency_deviation;
}
}
break;
case DAC_MODE_NOISE:
/* Generates white noise from a predetermined LUT
*/
for(uint8_t i = 0; i < (0xFF/2); i++){
static uint16_t noise_cnt = 0;
write_to_dac(1,
pgm_read_byte(&noise_table[noise_cnt]));
write_to_dac(0,
pgm_read_byte(&noise_table[noise_cnt]));
noise_cnt++; /* Doesn't have frequency diversion
*/
if(noise_cnt >= 1024){
noise_cnt = 0;
}
}
break;
case DAC_MODE_TRIANGLE:
/* Generates a triangle wave tone */
for(uint8_t i = 0; i < (0xFF/2); i++){
static int8_t direction = 1;
if((threash == 0xFF) | !threash){
direction = -direction;
}
write_to_dac(0, threash);
write_to_dac(1, threash);
if(dac_frequency_deviation >=0){
freq_delay_cnt++;
if(freq_delay_cnt >=
dac_frequency_deviation){
freq_delay_cnt = 0;
threash += direction;
}
}else{
if((dac_frequency_deviation *
direction) >
(0xFF - threash)){
threash = 0xFF;
continue;
}
threash = (dac_frequency_deviation *
direction);
}
}
break;
}
return;
}
void routine_dac(){
uint8_t received = read_from_dac(0x00);
if(!(received & (0x01<<0))){
feed_dac();
}
return;
}

378
src/game.c

@ -0,0 +1,378 @@
/* Copyright © 2020 tyrolyean
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "game.h"
#include "structures.h"
#include "16550.h"
#include <string.h>
#include <stdio.h>
#include <ctype.h>
char command_buffer[100];
uint8_t command_buffer_pointer = 0x00;
uint8_t current_room = ROOM_LONELYROAD;
uint8_t computer_state = 0;
bool bear_shot = false;
void routine_game(){
if(command_buffer_pointer >= sizeof(command_buffer)){
command_buffer_pointer = 0x00;
memset(command_buffer, 0, sizeof(command_buffer));
println("\nToo much input!");
return;
}
if(command_buffer[command_buffer_pointer-1] == '\n' ||
command_buffer[command_buffer_pointer-1] == '\r'){
/* A command from the user has been received, we are ready to
* do something!*/
int8_t action_id = -1;
for(size_t i = 0; i < sizeof(action_table)/sizeof(const char*);
i++){
if(strncasecmp(action_table[i], command_buffer,
strlen(action_table[i])) == 0){
action_id = i;
break;
}
}
if(action_id < 0){
println(info_table[1]);
}else{
perform_action(action_id);
}
command_buffer_pointer = 0x00;
memset(command_buffer, 0, sizeof(command_buffer));
}
return;
}
void ingest_user_char(char in){
if(in == 0x7F /* DELETE CHAR */){
command_buffer[command_buffer_pointer--] = 0x00;
}else{
command_buffer[command_buffer_pointer++] = in;
}
return;
}
void perform_action(uint8_t action_id){
putchar_16550('\n', NULL);
switch(action_id){
default:
case ACTION_HELP:
println("You can:");
for(size_t i = 0; i < NUM_ACTIONS; i++){
println(" %s",action_table[i]);
}
break;
case ACTION_DESCRIBE:
describe_room(current_room, false);
break;
case ACTION_NORTH:
case ACTION_SOUTH:
case ACTION_WEST:
case ACTION_EAST:
move_direction(action_id -1);
break;
case ACTION_INVENTORY:
print_inventory();
break;
case ACTION_SEARCH:
print_room_item();
break;
case ACTION_TAKE:
consume_room_item(command_buffer+
strlen(action_table[ACTION_TAKE])+1);
break;
case ACTION_USE:
use_item(command_buffer+
strlen(action_table[ACTION_USE])+1);
break;
};
println(info_table[3]);
return;
}
void move_direction(uint8_t direction){
if(!room_map_table[current_room][direction]){
println(info_table[4]);
return;
}
println("Moving towards %s",action_table[direction+1]);
current_room = room_map_table[current_room][direction];
describe_room(current_room,true);
current_track = room_track_map[current_room];
return;
}
void describe_room(uint8_t room, bool auto_desc){
println(room_table[room]);
if(room_visited_table[room] && auto_desc){
room_visited_table[room] = true;
return;
}
room_visited_table[room] = true;
putchar_16550('\n', NULL);
println(room_description_table[room]);
return;
}
void print_inventory(){
bool found_item = false;
for(size_t i = 0; i < NUM_ITEMS; i++){
if(inventory[i]){
found_item = true;
break;
}
}
if(found_item){
println("You have:");
for(size_t i = 0; i < NUM_ITEMS; i++){
if(inventory[i]){
println(" %s",item_table[i]);
}
}
}else{
println("Your inventory is empty");
}
return;
}
void print_room_item(){
if(item_room_map[current_room] < 0){
println(info_table[6]);
}else{
println("You found a %s",
item_table[item_room_map[current_room]]);
}
return;
}
void consume_room_item(const char* item_name){
if(item_room_map[current_room] < 0){
println(info_table[6]);
}else{
for(int8_t i = 0; i < NUM_ITEMS; i++){
if(strncasecmp(item_table[i], item_name,
strlen(item_table[i])) == 0){
if(item_room_map[current_room] != i){
break;
}
println("You took the %s",
item_table[item_room_map[
current_room]]);
inventory[item_room_map[current_room]] = true;
item_room_map[current_room] = -1;
return;
}
}
println("That's not here...");
}
return;
}
void use_item(const char* item_name){
for(size_t i = 0; i < NUM_ITEMS; i++){
if(strncasecmp(item_table[i], item_name,
strlen(item_table[i])) == 0){
use_item_id(i);
return;
}
}
println(info_table[2]);
return;
}
void use_item_id(uint8_t item_id){
if(!inventory[item_id]){
println(info_table[2]);
return;
}
switch(item_id){
case ITEM_SAUSAGE:
if(current_room == ROOM_SNDIRTROAD){
inventory[ITEM_SAUSAGE] = false;
room_map_table[current_room][DIRECTION_NORTH] =
ROOM_FIREPLACE;
println(info_table[10]);
return;
}
break;
case ITEM_PISTOL:
if(current_room == ROOM_SNDIRTROAD){
room_map_table[current_room][DIRECTION_NORTH] =
ROOM_FIREPLACE;
println(info_table[7]);
bear_shot = true;
return;
} else if(current_room == ROOM_NOTHING){
/* The real end i guess. I won't put this in the
* info table as it *SHOULD* only be displayed
* once.
*/
println("You cry for help. It's no use. You "
"attempt to shoot yourself. It's no "
"use... You run out of bullets");
inventory[ITEM_PISTOL] = false;
return;
}
break;
case ITEM_KEY:
if(current_room == ROOM_EWSTREET){
inventory[ITEM_KEY] = false;
room_map_table[current_room][DIRECTION_NORTH] =
ROOM_OLDHOUSE;
println(info_table[13]);
return;
}
break;
case ITEM_FLOPPY:
case ITEM_FLESH:
case ITEM_SCREWDRIVER:
case ITEM_KEYBOARD:
if(current_room == ROOM_COMPUTER){
if(perform_computer_action(item_id)){
return;
}
}
break;
};
println(info_table[2]);
return;
}
bool perform_computer_action(uint8_t item_id){
static bool fleshed = false;
if(item_id == ITEM_KEYBOARD &&
computer_state == COMPUTER_STATE_NOTHING){
computer_state = COMPUTER_STATE_KEYBOARD;
inventory[item_id] = false;
println("You connected the keyboard");
return true;
}
if(item_id == ITEM_FLOPPY &&
computer_state == COMPUTER_STATE_KEYBOARD){
computer_state = COMPUTER_STATE_FLOPPY;
inventory[item_id] = false;
println("You inseted the floppy disk");
return true;
}
if(item_id == ITEM_FLESH &&
computer_state == COMPUTER_STATE_KEYBOARD){
computer_state = COMPUTER_STATE_FLOPPY;
inventory[item_id] = false;
println("You inserted the flesh into the floppy drive");
fleshed = true;
return true;
}
if(item_id == ITEM_SCREWDRIVER &&
computer_state == COMPUTER_STATE_FLOPPY){
computer_state = COMPUTER_STATE_FLOPPY;
inventory[item_id] = false;
/* Perform a reset of the game */
println("You start the computer with the screwdriver, sit down"
" and watch it boot into a textadventure:");
reset_game(fleshed);
return true;
}
return false;
}
/* Resets the game into "original" state or the bear dead state or the FLESH
* game state. FLESH is basically the end. You need to reset your computer
* afterwards.
*/
void reset_game(bool fleshed){
/* clear inventory */
memset(inventory, 0, sizeof(inventory));
/* Reset rooms visited */
memset(room_visited_table, 0, sizeof(inventory));
/* Reset items in rooms */
for(size_t i = 0; i < NUM_ROOMS; i++){
item_room_map[i] = -1;
}
if(fleshed){
current_room = ROOM_NOTHING;
inventory[ITEM_PISTOL] = true;
}else if(bear_shot){
current_room = ROOM_LONELYROAD;
room_description_table[ROOM_SNDIRTROAD] = sndirtroad_msg[1];
item_room_map[ROOM_SNDIRTROAD] = ITEM_FLESH;
}else{
current_room = ROOM_LONELYROAD;
room_map_table[ROOM_SNDIRTROAD][DIRECTION_NORTH] = 0x00;
item_room_map[ROOM_SNDIRTROAD] = ITEM_SAUSAGE;
item_room_map[ROOM_LONELYROAD] = ITEM_PISTOL;
item_room_map[ROOM_BASEMENT] = ITEM_FLOPPY;
}
item_room_map[ROOM_EWSTREET] = ITEM_KEY;
item_room_map[ROOM_OLDHOUSE] = ITEM_SCREWDRIVER;
item_room_map[ROOM_ATTIC] = ITEM_KEYBOARD;
computer_state = COMPUTER_STATE_NOTHING;
room_map_table[ROOM_EWSTREET][DIRECTION_NORTH] = 0x00;
current_track = room_track_map[current_room];
describe_room(current_room, false);
return;
}
void init_game(){
room_description_table[ROOM_SNDIRTROAD] = sndirtroad_msg[0];
info_table[3] = user_action_req_msgs[0];
return;
}

36
src/interrupt.c

@ -0,0 +1,36 @@
/* Copyright © 2020 tyrolyean
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "interrupt.h"
#include "dac.h"
#include <avr/interrupt.h>
void init_interrupts(){
/* Initialize the timer interrupt */
TCCR0A = (1 << COM0A1) | (1 << WGM11);
TCCR0B = (1 << CS02);
OCR0A = 64; /* Interrupt fires every Millisecond */
TIMSK0 = (1 << OCIE0A);
}
ISR(TIMER0_COMPA_vect)
{
update_sound();
}

112
src/main.c

@ -0,0 +1,112 @@
/* Copyright © 2020 tyrolyean
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "avr.h"
#include "structures.h"
#include "16550.h"
#include "dac.h"
#include "interrupt.h"
#include "sound.h"
#include "game.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <avr/wdt.h>
#include <avr/interrupt.h>
/* Included early on the prevent the watchdog from killing the already killed
* microcontroller because of init delay
*/
uint8_t mcusr_mirror __attribute__ ((section (".noinit")));
void get_mcusr(void) \
__attribute__((naked)) \
__attribute__((section(".init3")));
void get_mcusr(void)
{
mcusr_mirror = MCUSR;
MCUSR = 0;
wdt_disable();
}
/* Function used to be more complicated, still remains for readability reasons
*/
void set_addr(uint8_t addr){
ADDR_REG = addr;
return;
}
void reset_modules(){
/* Setup Data Direction Registers and populate with sane default
values */
DATA_DDR_REG = 0xFF; /* Data Bus */
ADDR_DDR_REG = 0xFF; /* Address Bus */
CTRL_DDR_REG= 0xFF; /* Control Bus */
ADDR_REG = 0x00;
DATA_REG = 0x00;
CTRL_REG = 0x00;
/* Cleanly reset the dac and the uart */
CTRL_REG |= (1<<WR_SHIFT) | (1<<RD_SHIFT) | (1<<CS_DAC_SHIFT) |
(1<<MR_SHIFT) | (1<<CS_UART_SHIFT);
_delay_us(100);
CTRL_REG &= ~(1<<MR_SHIFT);
return;
}
int routine();
int main(){
/* Disable interrupts during initialisation phase */
cli();
reset_modules();
init_uart();
init_interrupts();
init_game();
current_track = intro_track;
memset(room_visited_table, 0, NUM_ROOMS);
memset(inventory, 0, NUM_ITEMS);
describe_room(current_room,false);
sei();
/* Enable the hardware watchdog. In case the microcontroller fails to
* finish it's task within the specified time, the watchdog will reset
* the atmel cookie.
*/
wdt_enable(WDTO_8S);
while(1){
wdt_reset();
routine();
}
return 0;
}
int routine(){
routine_dac();
routine_uart();
routine_game();
return 0;
}

261
src/sound.c

@ -0,0 +1,261 @@
/* Copyright © 2020 tyrolyean
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "sound.h"
#include "sound.h"
#include "dac.h"
#include <avr/pgmspace.h>
#include <stddef.h>
const struct tone_t computer_room_track[] PROGMEM =
{
{DAC_MODE_SQUARE, 0,100},
{DAC_MODE_SQUARE, 10,100},
{DAC_MODE_SQUARE, 0,100},
{DAC_MODE_SQUARE, 10,100},
{DAC_MODE_SQUARE, 0,100},
{DAC_MODE_NOISE, 0,200},
{DAC_MODE_SINE, 10,50},
{DAC_MODE_SINE, -10,50},
{DAC_MODE_SINE, 10,50},
{DAC_MODE_SINE, -10,50},
{DAC_MODE_SQUARE, 0,100},
{DAC_MODE_SQUARE, 10,100},
{DAC_MODE_SQUARE, 0,100},
{DAC_MODE_SQUARE, 10,100},
{DAC_MODE_SQUARE, 0,100},
{DAC_MODE_NOISE, 0,200},
{DAC_MODE_SINE, 10,50},
{DAC_MODE_SINE, -10,50},
{DAC_MODE_SINE, 10,50},
{DAC_MODE_SINE, -10,50},
{DAC_MODE_SQUARE, 0,100},
{DAC_MODE_SQUARE, 10,100},
{DAC_MODE_SQUARE, 0,100},
{DAC_MODE_SQUARE, 10,100},
{DAC_MODE_SQUARE, 0,100},
{DAC_MODE_NOISE, 0,200},
{DAC_MODE_SINE, 10,50},
{DAC_MODE_SINE, -10,50},
{DAC_MODE_SINE, 10,50},
{DAC_MODE_SINE, -10,50},
{DAC_MODE_SQUARE, 0,100},
{DAC_MODE_SQUARE, 10,100},
{DAC_MODE_SQUARE, 0,100},
{DAC_MODE_SQUARE, 10,100},
{DAC_MODE_SQUARE, 0,100},
{DAC_MODE_NOISE, 0,200},
{DAC_MODE_SINE, 10,50},
{DAC_MODE_SINE, -10,50},
{DAC_MODE_SINE, 10,50},
{DAC_MODE_SINE, -10,50},
{DAC_MODE_SQUARE, 0,100},
{DAC_MODE_SQUARE, 10,100},
{DAC_MODE_SQUARE, 0,100},
{DAC_MODE_SQUARE, 10,100},
{DAC_MODE_SQUARE, 0,100},
{DAC_MODE_NOISE, 0,200},
{DAC_MODE_SINE, 10,50},
{DAC_MODE_SINE, -10,50},
{DAC_MODE_SINE, 10,50},
{DAC_MODE_SINE, -10,50},
{DAC_MODE_SQUARE, -2,100},
{DAC_MODE_SQUARE, 8,100},
{DAC_MODE_SQUARE, -2,100},
{DAC_MODE_SQUARE, 8,100},
{DAC_MODE_SQUARE, -2,100},
{DAC_MODE_NOISE, 0,200},
{DAC_MODE_SINE, 12,50},
{DAC_MODE_SINE, -8,50},
{DAC_MODE_SINE, 12,50},
{DAC_MODE_SINE, -8,50},
{DAC_MODE_SQUARE, -3,100},
{DAC_MODE_SQUARE, 7,100},
{DAC_MODE_SQUARE, -3,100},
{DAC_MODE_SQUARE, 7,100},
{DAC_MODE_SQUARE, -3,100},
{DAC_MODE_NOISE, 0,200},
<