ICALC.C 4.7 KB

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