-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
221 lines (187 loc) · 5.22 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/**
==================== X-PONG ====================
*
* X-PONG is a game like the classic Pong but with
* different structure and functionality. It was writtes
* as a project for the KTH course "IS1200 VT23 Computer
* Hardware Engineering".
*
* Most code in this project has been written by the
* group itself. However, some of the code (marked out)
* was taken from previous labs.
*
* @author Fridh, William & Åhlin Pontus
* @link https://github.com/williamFridh
*
* @author Åhlin Pontus
* @link https://github.com/PontusAhlin
*/
/**
* Include Libraries
*/
#include <stdint.h> // Declarations of uint_32 and the like.
#include <pic32mx.h> // Declarations of system-specific addresses etc.
#include <stdbool.h> // Support for boolean
#include "menu.h"
#include "highscore.h"
#include "shieldDisplay.h"
#include "game.h"
#include "mips.h"
#include "main.h"
/**
* Settings
*
* This section holds global settings that
* itsn't solely related to the game or menu.
*/
bool in_game = false;
volatile int * trise = (volatile int *) 0xbf886100; // Defined pointer to TRISE (debugging)
int ledVal = 0; // Debugging
int btn_lock = false; // Used preventing rapid clicks in the menu
int btn_data; // Hold button data
/**
* Set In Game
*
* This function is used for updating the in_game value.
* It can be called upon for instance when a game should
* start or when a game should end.
*
* @param {bool}Target state
*
* @author Fridh, William
*/
void setInGame(bool newState) {
in_game = newState;
}
/**
* Update Screen
*
* Check if a game is running or not and call
* correct render function based on that.
*
* @author Fridh, William
*/
void updateScreen(void) {
if (in_game) {
renderGame();
} else {
renderMenu();
}
}
/**
* Set Button Data
*
* This function handles the button data differently depending
* on the vale of the boolean in_game. It modifies the bits of
* btn_data and handles btn_lock if the user isn't in a game.
*
* @author Fridh, William
*/
void setBtnData(void) {
const int current_btn_data = ((PORTD & 0xe0) >> 4) | ((PORTF & 0x2) >> 1);
if (in_game) {
btn_data = btn_data | current_btn_data;
btn_lock = false;
} else {
if (!btn_lock && current_btn_data) {
btn_data = btn_data | current_btn_data;
btn_lock = true;
} else {
if (!current_btn_data) {
btn_lock = false;
}
}
}
}
/**
* Reset Button Data
*
* Resets the button data. Usually called from within handleBtnData.
*
* @author Fridh, William
*/
void resetBtnData(void) {
btn_data = 0;
}
/**
* Handle Button Data
*
* Decides what to do with the button data.
* Passes it forward to the relevant function.
*
* @author Fridh, William
*/
void handleBtnData(void) {
if (in_game) {
gameButtonTriggered(btn_data); // Send button data to the game button handler
} else {
menuButtonTriggered(btn_data); // Send button data to the menu button handler
}
}
/**
* Interrupt Service Routine
*
* This function detects and resets intruptflags.
* It uses interupt counters for adjusting the game
* and input speed.
*
* @authors Fridh, William & Åhlin Pontus
*/
int timerInteruptCounterInput = 0;
int timerInteruptCounterProgram = 0;
void user_isr(void) {
if (IFS(0) & 0x100) { // Check for relevant flag
timerInteruptCounterInput++;
timerInteruptCounterProgram++;
if (
(in_game && timerInteruptCounterProgram == 50) || // Different speed in game
(!in_game && timerInteruptCounterProgram == 255) // Slower speed in menu
) {
handleBtnData(); // Handle button data
resetBtnData(); // Reset button data
if (!pvpMode) playAi(difficulty); // Play AI
timerInteruptCounterProgram = 0;
}
if (timerInteruptCounterInput == 15) {
updateScreen(); // Update screen
timerInteruptCounterInput = 0;
}
IFS(0) = IFS(0) & 0xfffffeff; // Reset interrupt flag
random_number++; // Incrmeent random number
}
}
/**
* Initilize Timer
*
* Initilizes and configures the timer.
*
* @author Åhlin Pontus
*/
void initTimer(void) {
T2CON = 0x0; // Stopping timer and setting the prescaler to 1/1
PR2 = ((80000000 / 256)/ 10); // Setting the period for the timer
TMR2 = 0; // Ticks to PR2
IECSET(0) = 0x100; // Enable interrupts
IPC(2) = 0xC; // Enable a interrupt priority
T2CONSET = 0x8000; // Starting timer
enable_interrupt();
}
/**
* Main Function
*
* The main function is called on startup and will continue
* running infinite thanks to the while-loop.
*
* @authors Fridh, William & Åhlin Pontus
*/
int main(void) {
*trise = *trise & 0xffffff00; // Set ports 0-7 as outputs (debugging)
PORTE = 0;
TRISDSET = 0xe0; // Set buttons 2-4 as inputs
TRISFSET = 0x2; // Set button 1 as inputs
initShield(); // Initilize display
initTimer(); // Initilize timer
initHighscore(); // Initilize highscore
while(1) setBtnData(); // Inifinite loop for listening for input
return 0;
}