FPCALC.C 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 == 0x1b) // escape
  61. {
  62. BDOS_PrintcConsole('\n');
  63. return 1;
  64. }
  65. }
  66. // number input, dot or sign
  67. else if ( (c >= '0' && c <= '9') || c == '.' || c == '-')
  68. {
  69. if (CALC_state == CALC_STATE_INPUTA || CALC_state == CALC_STATE_INPUTB)
  70. {
  71. // add to buffer and print character
  72. CALC_buffer[CALC_buffer_idx] = c;
  73. CALC_buffer_idx++;
  74. CALC_buffer[CALC_buffer_idx] = 0; // terminate
  75. BDOS_PrintcConsole(c);
  76. }
  77. }
  78. else if (c == 0x8) // backspace
  79. {
  80. if (CALC_state == CALC_STATE_INPUTA || CALC_state == CALC_STATE_INPUTB)
  81. {
  82. // replace last char in buffer by 0 (if not at start)
  83. if (CALC_buffer_idx != 0)
  84. {
  85. CALC_buffer_idx--;
  86. CALC_buffer[CALC_buffer_idx] = 0;
  87. BDOS_PrintcConsole(c);
  88. }
  89. }
  90. }
  91. else if (c == 0xa) // newline/enter
  92. {
  93. switch(CALC_state)
  94. {
  95. case CALC_STATE_INPUTA:
  96. if (CALC_buffer_idx > 0)
  97. {
  98. BDOS_PrintcConsole('\n');
  99. CALC_a = FP_StringToFP(CALC_buffer);
  100. CALC_buffer_idx = 0;
  101. BDOS_PrintConsole("Input B: ");
  102. CALC_state = CALC_STATE_INPUTB;
  103. }
  104. break;
  105. case CALC_STATE_INPUTB:
  106. if (CALC_buffer_idx > 0)
  107. {
  108. BDOS_PrintcConsole('\n');
  109. CALC_b = FP_StringToFP(CALC_buffer);
  110. CALC_buffer_idx = 0;
  111. BDOS_PrintConsole("Operation: ");
  112. CALC_state = CALC_STATE_INPUTOP;
  113. }
  114. break;
  115. }
  116. }
  117. else if (c == 0x1b) // escape
  118. {
  119. BDOS_PrintcConsole('\n');
  120. return 1;
  121. }
  122. }
  123. return 0;
  124. }
  125. int main()
  126. {
  127. BDOS_PrintlnConsole("Fixed-point calculator test\n");
  128. word stop = 0;
  129. while (!stop)
  130. {
  131. stop = calcLoop();
  132. }
  133. return 'q';
  134. }
  135. void interrupt()
  136. {
  137. // handle all interrupts
  138. word i = getIntID();
  139. switch(i)
  140. {
  141. case INTID_TIMER1:
  142. timer1Value = 1; // notify ending of timer1
  143. break;
  144. case INTID_TIMER2:
  145. break;
  146. case INTID_UART0:
  147. break;
  148. case INTID_GPU:
  149. break;
  150. case INTID_TIMER3:
  151. break;
  152. case INTID_PS2:
  153. break;
  154. case INTID_UART1:
  155. break;
  156. case INTID_UART2:
  157. break;
  158. }
  159. }