123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- // Simple calculator to test floating points
- #define word char
- #include "LIB/MATH.C"
- #include "LIB/FP.C"
- #include "LIB/STDLIB.C"
- #include "LIB/SYS.C"
- #define CALC_BUFFER_MAX_LEN 32
- #define CALC_STATE_INPUTSTART 0
- #define CALC_STATE_INPUTA 1
- #define CALC_STATE_INPUTB 2
- #define CALC_STATE_INPUTOP 3
- // The current number that is being typed
- char CALC_buffer[CALC_BUFFER_MAX_LEN];
- word CALC_buffer_idx = 0;
- fixed_point_t CALC_a = 0;
- fixed_point_t CALC_b = 0;
- char CALC_state = CALC_STATE_INPUTSTART;
- word calcLoop()
- {
- if (CALC_state == CALC_STATE_INPUTSTART)
- {
- BDOS_PrintConsole("Input A: ");
- CALC_state = CALC_STATE_INPUTA;
- }
- if (HID_FifoAvailable())
- {
- word c = HID_FifoRead();
- if (CALC_state == CALC_STATE_INPUTOP)
- {
- if (c == '+')
- {
- BDOS_PrintlnConsole("+");
- char buffer[24];
- FP_FPtoString(CALC_a + CALC_b, buffer, 5);
- BDOS_PrintConsole("Result = ");
- BDOS_PrintlnConsole(buffer);
- CALC_state = CALC_STATE_INPUTA;
- BDOS_PrintConsole("\n\nInput A: ");
- }
- else if (c == '-')
- {
- BDOS_PrintlnConsole("-");
- char buffer[24];
- FP_FPtoString(CALC_a - CALC_b, buffer, 5);
- BDOS_PrintConsole("Result = ");
- BDOS_PrintlnConsole(buffer);
- CALC_state = CALC_STATE_INPUTA;
- BDOS_PrintConsole("\n\nInput A: ");
- }
- else if (c == '*')
- {
- BDOS_PrintlnConsole("*");
- char buffer[24];
- FP_FPtoString(FP_Mult(CALC_a, CALC_b), buffer, 5);
- BDOS_PrintConsole("Result = ");
- BDOS_PrintlnConsole(buffer);
- CALC_state = CALC_STATE_INPUTA;
- BDOS_PrintConsole("\n\nInput A: ");
- }
- else if (c == '/')
- {
- BDOS_PrintlnConsole("/");
- char buffer[24];
- FP_FPtoString(FP_Div(CALC_a, CALC_b), buffer, 5);
- BDOS_PrintConsole("Result = ");
- BDOS_PrintlnConsole(buffer);
- CALC_state = CALC_STATE_INPUTA;
- BDOS_PrintConsole("\n\nInput A: ");
- }
- else if (c == 0x1b) // escape
- {
- BDOS_PrintcConsole('\n');
- return 1;
- }
- }
- // number input, dot or sign
- else if ( (c >= '0' && c <= '9') || c == '.' || c == '-')
- {
- if (CALC_state == CALC_STATE_INPUTA || CALC_state == CALC_STATE_INPUTB)
- {
- // add to buffer and print character
- CALC_buffer[CALC_buffer_idx] = c;
- CALC_buffer_idx++;
- CALC_buffer[CALC_buffer_idx] = 0; // terminate
- BDOS_PrintcConsole(c);
- }
- }
- else if (c == 0x8) // backspace
- {
- if (CALC_state == CALC_STATE_INPUTA || CALC_state == CALC_STATE_INPUTB)
- {
- // replace last char in buffer by 0 (if not at start)
- if (CALC_buffer_idx != 0)
- {
- CALC_buffer_idx--;
- CALC_buffer[CALC_buffer_idx] = 0;
- BDOS_PrintcConsole(c);
- }
- }
- }
- else if (c == 0xa) // newline/enter
- {
- switch(CALC_state)
- {
- case CALC_STATE_INPUTA:
- if (CALC_buffer_idx > 0)
- {
- BDOS_PrintcConsole('\n');
- CALC_a = FP_StringToFP(CALC_buffer);
- CALC_buffer_idx = 0;
- BDOS_PrintConsole("Input B: ");
- CALC_state = CALC_STATE_INPUTB;
- }
- break;
- case CALC_STATE_INPUTB:
- if (CALC_buffer_idx > 0)
- {
- BDOS_PrintcConsole('\n');
- CALC_b = FP_StringToFP(CALC_buffer);
- CALC_buffer_idx = 0;
- BDOS_PrintConsole("Operation: ");
- CALC_state = CALC_STATE_INPUTOP;
- }
- break;
- }
- }
- else if (c == 0x1b) // escape
- {
- BDOS_PrintcConsole('\n');
- return 1;
- }
- }
- return 0;
- }
- int main()
- {
- BDOS_PrintlnConsole("Fixed-point calculator test\n");
- word stop = 0;
- while (!stop)
- {
- stop = calcLoop();
- }
- return 'q';
- }
- void interrupt()
- {
- // handle all interrupts
- word i = getIntID();
- switch(i)
- {
- case INTID_TIMER1:
- timer1Value = 1; // notify ending of timer1
- break;
- case INTID_TIMER2:
- break;
- case INTID_UART0:
- break;
- case INTID_GPU:
- break;
- case INTID_TIMER3:
- break;
- case INTID_PS2:
- break;
- case INTID_UART1:
- break;
- case INTID_UART2:
- break;
- }
- }
|