// Initialize hardware void init() { // Disable the watchdog timer *AT91C_WDTC_WDMR = AT91C_WDTC_WDDIS; lcd_init(); keyboard_init(); lcd_print_int(0); }
void print_all_ticks(void) { lcd_print_int(1,1,ticks[0]); lcd_print_int(1,4,ticks[1]); lcd_print_int(1,8,ticks[2]); lcd_print_int(1,12,ticks[3]); lcd_print_int(2,1,ticks[4]); lcd_print_int(2,4,ticks[5]); }
int main(){ init(); struct entry entry; // User input loop while (1){ // get either number and operation, or just an operation keyboard_get_entry(&entry); // Process the entry // If there was a number in this entry, save to stack if (entry.number != INT_MAX){ push(entry.number); } // If the action was the input key, or the stack is too small to do an operation if (entry.operation == '\r' || stack_pointer < 2){ continue; } // Save the operands as the last 2 things on the stack int a = get(stack_pointer - 2); int b = get(stack_pointer - 1); // Store the answer int ans; // Only used in division int is_neg; switch (entry.operation){ // Addition case '+': ans = a + b; break; // Subtraction case '-': ans = a - b; break; // Multiplication case '*': ans = a * b; break; // Division // Our toolchain doesn't let us divide by a variable, so this code is nasty case '/': // If Kevin tries to divide by zero if (b == 0){ taunt_user(); continue; } // Determine whether the result will be negative is_neg = (a * b < 0); // Take absolute value of operands if (a < 0) a *= -1; if (b < 0) b *= -1; ans = 0; // Calculate a / b // Subtract a - b until a is zero, counting how many times we subtracted while (a > 0){ a -= b; ans++; } // If there is a remainder if (a < 0){ // Always round down ans--; } // Correct the sign of the result if (is_neg){ ans *= -1; } break; default: break; } // Take the 2 operands off the stack pop(); pop(); // Save answer to the end of the stack push(ans); lcd_print_int(ans); } return 0; }