FPCALC.C 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. // Simple calculator to test floating points
  2. #define word char
  3. #include "LIB/MATH.C"
  4. #include "LIB/FP.C"
  5. #include "LIB/STDLIB.C"
  6. #include "LIB/SYS.C"
  7. #define CALC_BUFFER_MAX_LEN 32
  8. #define CALC_STATE_INPUTSTART 0
  9. #define CALC_STATE_INPUTA 1
  10. #define CALC_STATE_INPUTB 2
  11. #define CALC_STATE_INPUTOP 3
  12. // The current number that is being typed
  13. char CALC_buffer[CALC_BUFFER_MAX_LEN];
  14. word CALC_buffer_idx = 0;
  15. fixed_point_t CALC_a = 0;
  16. fixed_point_t CALC_b = 0;
  17. char CALC_state = CALC_STATE_INPUTSTART;
  18. word calcLoop()
  19. {
  20. if (CALC_state == CALC_STATE_INPUTSTART)
  21. {
  22. BDOS_PrintConsole("Input A: ");
  23. CALC_state = CALC_STATE_INPUTA;
  24. }
  25. if (HID_FifoAvailable())
  26. {
  27. word c = HID_FifoRead();
  28. if (CALC_state == CALC_STATE_INPUTOP)
  29. {
  30. if (c == '+')
  31. {
  32. BDOS_PrintlnConsole("+");
  33. char buffer[24];
  34. FP_FPtoString(CALC_a + CALC_b, buffer, 5);
  35. BDOS_PrintConsole("Result = ");
  36. BDOS_PrintlnConsole(buffer);
  37. CALC_state = CALC_STATE_INPUTA;
  38. BDOS_PrintConsole("\n\nInput A: ");
  39. }
  40. else if (c == '-')
  41. {
  42. BDOS_PrintlnConsole("-");
  43. char buffer[24];
  44. FP_FPtoString(CALC_a - CALC_b, buffer, 5);
  45. BDOS_PrintConsole("Result = ");
  46. BDOS_PrintlnConsole(buffer);
  47. CALC_state = CALC_STATE_INPUTA;
  48. BDOS_PrintConsole("\n\nInput A: ");
  49. }
  50. else if (c == '*')
  51. {
  52. BDOS_PrintlnConsole("*");
  53. char buffer[24];
  54. FP_FPtoString(FP_Mult(CALC_a, CALC_b), buffer, 5);
  55. BDOS_PrintConsole("Result = ");
  56. BDOS_PrintlnConsole(buffer);
  57. CALC_state = CALC_STATE_INPUTA;
  58. BDOS_PrintConsole("\n\nInput A: ");
  59. }
  60. else if (c == '/')
  61. {
  62. BDOS_PrintlnConsole("/");
  63. char buffer[24];
  64. FP_FPtoString(FP_Div(CALC_a, CALC_b), buffer, 5);
  65. BDOS_PrintConsole("Result = ");
  66. BDOS_PrintlnConsole(buffer);
  67. CALC_state = CALC_STATE_INPUTA;
  68. BDOS_PrintConsole("\n\nInput A: ");
  69. }
  70. else if (c == 0x1b) // escape
  71. {
  72. BDOS_PrintcConsole('\n');
  73. return 1;
  74. }
  75. }
  76. // number input, dot or sign
  77. else if ( (c >= '0' && c <= '9') || c == '.' || c == '-')
  78. {
  79. if (CALC_state == CALC_STATE_INPUTA || CALC_state == CALC_STATE_INPUTB)
  80. {
  81. // add to buffer and print character
  82. CALC_buffer[CALC_buffer_idx] = c;
  83. CALC_buffer_idx++;
  84. CALC_buffer[CALC_buffer_idx] = 0; // terminate
  85. BDOS_PrintcConsole(c);
  86. }
  87. }
  88. else if (c == 0x8) // backspace
  89. {
  90. if (CALC_state == CALC_STATE_INPUTA || CALC_state == CALC_STATE_INPUTB)
  91. {
  92. // replace last char in buffer by 0 (if not at start)
  93. if (CALC_buffer_idx != 0)
  94. {
  95. CALC_buffer_idx--;
  96. CALC_buffer[CALC_buffer_idx] = 0;
  97. BDOS_PrintcConsole(c);
  98. }
  99. }
  100. }
  101. else if (c == 0xa) // newline/enter
  102. {
  103. switch(CALC_state)
  104. {
  105. case CALC_STATE_INPUTA:
  106. if (CALC_buffer_idx > 0)
  107. {
  108. BDOS_PrintcConsole('\n');
  109. CALC_a = FP_StringToFP(CALC_buffer);
  110. CALC_buffer_idx = 0;
  111. BDOS_PrintConsole("Input B: ");
  112. CALC_state = CALC_STATE_INPUTB;
  113. }
  114. break;
  115. case CALC_STATE_INPUTB:
  116. if (CALC_buffer_idx > 0)
  117. {
  118. BDOS_PrintcConsole('\n');
  119. CALC_b = FP_StringToFP(CALC_buffer);
  120. CALC_buffer_idx = 0;
  121. BDOS_PrintConsole("Operation: ");
  122. CALC_state = CALC_STATE_INPUTOP;
  123. }
  124. break;
  125. }
  126. }
  127. else if (c == 0x1b) // escape
  128. {
  129. BDOS_PrintcConsole('\n');
  130. return 1;
  131. }
  132. }
  133. return 0;
  134. }
  135. int main()
  136. {
  137. BDOS_PrintlnConsole("Fixed-point calculator test\n");
  138. word stop = 0;
  139. while (!stop)
  140. {
  141. stop = calcLoop();
  142. }
  143. return 'q';
  144. }
  145. void interrupt()
  146. {
  147. // handle all interrupts
  148. word i = getIntID();
  149. switch(i)
  150. {
  151. case INTID_TIMER1:
  152. timer1Value = 1; // notify ending of timer1
  153. break;
  154. case INTID_TIMER2:
  155. break;
  156. case INTID_UART0:
  157. break;
  158. case INTID_GPU:
  159. break;
  160. case INTID_TIMER3:
  161. break;
  162. case INTID_PS2:
  163. break;
  164. case INTID_UART1:
  165. break;
  166. case INTID_UART2:
  167. break;
  168. }
  169. }